- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
< ><FONT color=#ff0000>函数名: sbrk </FONT>
* G. q8 A$ T5 t- x; |功 能: 改变数据段空间位置 / S: C' F5 B8 R4 z6 o3 t7 y
用 法: char *sbrk(int incr);
2 W) Y1 Y* W0 t0 r2 E* Q) M程序例: </P>
: g0 `) v8 N8 x5 u1 \ K" W5 n< ><FONT color=#0000ff>#include <STDIO.H>* B3 G |1 {3 [8 N3 U
#include <ALLOC.H></FONT></P>
& Q) x5 n" A. s< ><FONT color=#0000ff>int main(void)
3 @. ^" S! j! c W{
5 }. E% b7 A; Z; B$ |# ]/ ~! F" Jprintf("Changing allocation with sbrk()\n"); 5 d- s/ ]: Y7 a G1 ^0 j3 G
printf("Before sbrk() call: %lu bytes free\n",
+ n; c+ X3 Z& G( O. r5 y(unsigned long) coreleft()); . k. y8 M: n- Q- N, G9 ~
sbrk(1000);
4 Q; p" ^ v/ U8 ?$ e# N6 P9 Hprintf(" After sbrk() call: %lu bytes free\n", 1 v* S* ^ j' t) |# P
(unsigned long) coreleft());
( _( }# B8 J P1 yreturn 0; 6 J3 T+ p6 D W
}
/ h! S* o; V& B! ^% B
, P2 d' z+ [& i, X5 X7 ^* c. |</FONT></P>
8 r. k* k/ k/ e" C< ><FONT color=#ff0000>函数名: scanf </FONT>
- }7 u/ W3 `/ w4 j( d$ s功 能: 执行格式化输入
5 m- H1 z. p5 e; ~$ S用 法: int scanf(char *format[,argument,...]);
2 d: n$ ?1 W. _+ K/ M, V程序例: </P>
2 V e) _8 ^7 p( |< ><FONT color=#0000ff>#include <STDIO.H>& I; x, h/ o* X6 L2 a
#include <CONIO.H></FONT></P>
4 j5 B7 L6 [9 P$ M$ |6 s( c< ><FONT color=#0000ff>int main(void)
- O. H ^/ i% I{ 9 s K8 o0 p k$ v' ^7 C
char label[20]; 1 O: a: F- e) I, N1 [& F
char name[20]; # f2 k- }/ A% Q
int entries = 0; - i; j8 g' j- `7 Q4 F1 w* f
int loop, age; 3 ]: ]+ U7 j. D3 Y0 `0 w/ p e8 x8 t
double salary; </FONT></P>9 ~% v" z+ }" G+ K8 l; @2 O
< ><FONT color=#0000ff>struct Entry_struct # _4 s; l, k$ [: ?& X
{
, O6 ^( S3 H) C, M( \, H( }char name[20];
" u6 Q2 y1 D3 e8 n& z6 }7 }int age; ; R) h- H( z) j* ?. z0 ~- v W
float salary;
, E1 ~! B, h: m6 c N% y) m} entry[20]; </FONT></P>
, ?! {, R9 o+ W, A9 s< ><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
) Q; z' A% N& u9 B: d/ E; ?: j$ V+ Gprintf("\n\nPlease enter a label for the chart: ");
' U% ?% T5 b2 T; q( `scanf("%20s", label); + p6 f6 p. X- C1 G/ T
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>* k' y8 g: Z. g" {8 s7 t
< ><FONT color=#0000ff>/* Input number of entries as an integer */
: Y% B7 g* U* {# I7 t% zprintf("How many entries will there be? (less than 20) "); ' }- M5 a, @ k2 t
scanf("%d", &entries); 3 c5 U& d2 T E* m
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
* J, t9 H- d0 q" q! k, r+ V4 c< ><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ * T# }& y; c' w y
for (loop=0;loop<ENTRIES;++LOOP) % \. \4 ]( B' D, ]3 A- T; @$ E- v
{
2 E' q! @/ s/ L% e# Nprintf("Entry %d\n", loop); " F1 h0 h- K- L5 j! y9 `
printf(" Name : ");
, G7 x+ Y; O8 J" G: u; j9 Qscanf("%[A-Za-z]", entry[loop].name); 7 y& z2 u& v1 {/ B/ ^, w0 x
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>) H$ M5 c9 J9 F" R9 c
< ><FONT color=#0000ff>/* input an age as an integer */ 4 ?) E" s0 @6 e& Y
printf(" Age : ");
0 w" l3 _ p5 i$ p7 U. M. `6 U. ]scanf("%d", &entry[loop].age);
8 I! h0 M! A% d0 Vfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
, z* V! J2 u A, a: f/ j< ><FONT color=#0000ff>/* input a salary as a float */ ( J; u) `3 D: o- H5 N) E
printf(" Salary : "); / C5 p1 r5 J. t3 X
scanf("%f", &entry[loop].salary); ; W3 a& O4 L* A2 R9 l
fflush(stdin); /* flush the input stream in case of bad input */ 7 y! Q/ B3 T% n# r8 o
} </FONT></P>
3 ^) J& v( p% G- @- I< ><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */
+ o* b% A- t- ^3 Vprintf("\nPlease enter your name, age and salary\n");
9 m$ }5 X* \, @. f- M, {& s" escanf("%20s %d %lf", name, &age, &salary); ( Z9 ]! Z ^; } O8 ]2 E' R
</FONT></P>& N% g4 X6 ]1 c. S! A. U
< ><FONT color=#0000ff>/* Print out the data that was input */ / }) j* }; O, k
printf("\n\nTable %s\n",label);
, J$ S3 H7 z, l2 T, h4 iprintf("Compiled by %s age %d $%15.2lf\n", name, age, salary); 3 d/ ]3 M! ~* T
printf("-----------------------------------------------------\n"); 8 G& }- d0 d) R, T2 Y0 O
for (loop=0;loop<ENTRIES;++LOOP)
( M% O& D% [* U4 `1 |" ] printf("%4d | %-20s | %5d | %15.2lf\n",
# w0 D. n& M$ y$ h: V7 Nloop + 1,
: \ u3 V+ E/ f$ T7 N9 jentry[loop].name, 0 } i- X9 ?0 B O4 U
entry[loop].age,
- s: v$ d& U8 {/ H! v; _$ |4 w: q3 W0 O3 mentry[loop].salary);
; M7 M/ P1 {1 dprintf("-----------------------------------------------------\n"); 2 o' P, m9 c T( W2 ^
return 0; * W& C$ V% E7 o3 n
}
1 D3 `/ p6 o& H/ `$ C* [</FONT>. f0 y: ^1 [- ?( C
</P>; J9 J# {" d( E5 e
< ><FONT color=#ff0000>函数名: searchpath </FONT>
5 M# d E# O z% l; R4 _) J功 能: 搜索DOS路径
( X+ U9 k5 e. j6 `6 O8 n用 法: char *searchpath(char *filename); 3 [0 `: j* I& @! l. R; L
程序例: </P>7 F& H- O& h& y! S% {; r0 w
< ><FONT color=#0000ff>#include <STDIO.H>2 p9 U* o- t( W" n% N
#include <DIR.H></FONT></P>
9 Z; ?3 v5 k1 G! F< ><FONT color=#0000ff>int main(void)
: X/ o2 w b Y( m( E5 S( D; C{ 2 f g7 N7 _: z8 d% I: a6 E
char *p; </FONT></P>" v0 F& Q. g. R
< ><FONT color=#0000ff>/* Looks for TLINK and returns a pointer ) ~! @ C$ f. W6 G
to the path */
/ ^5 B2 K. `8 |, d# s! c4 h0 Mp = searchpath("TLINK.EXE"); 9 Y. U8 ~2 M, J, L
printf("Search for TLINK.EXE : %s\n", p); </FONT></P>
1 l- \2 N$ k2 C" |< ><FONT color=#0000ff>/* Looks for non-existent file */ , E/ u- `' l* t$ s4 C) U: n! Q
p = searchpath("NOTEXIST.FIL"); 8 D3 \+ t+ p5 x( t
printf("Search for NOTEXIST.FIL : %s\n", p); </FONT></P>+ j, x8 `- {$ v0 z2 b( s
< ><FONT color=#0000ff>return 0; . S4 y( N* ?& D" k$ S9 e
}
2 T- A3 ?2 U- [ S" \1 A% ^( c! m( K* @) c1 V) u: V! y' e1 N
</FONT></P>1 G9 y' [0 U \$ M0 F$ e
< ><FONT color=#ff0000>函数名: sector </FONT>
: H" c( D7 L% [# J9 z8 o5 E& E功 能: 画并填充椭圆扇区
, r' K* _! @6 r6 f" Z0 F% ]$ m2 y用 法: void far sector(int x, int y, int stangle, int endangle); & M/ X& w( q8 ?. U
程序例: </P>( R, _$ i, S9 L/ V% z+ ~
< ><FONT color=#0000ff>#include <GRAPHICS.H>, ?6 F4 Y8 Z& O) N) u- r
#include <STDLIB.H>
6 c1 k9 w: Y6 u2 Q( j$ Q; N o' {#include <STDIO.H>+ p5 b% S3 i' j' J/ @) ~& g
#include <CONIO.H></FONT></P>2 ^: m! \8 o/ N
< ><FONT color=#0000ff>int main(void) . T1 Z3 c( C0 H; }
{ 9 s; F8 S2 G. J, `
/* request auto detection */
' }/ f$ K0 L' w5 s2 A5 f# [& jint gdriver = DETECT, gmode, errorcode;
9 |/ k& c: A9 l3 yint midx, midy, i; $ n. E: q9 I( I7 O
int stangle = 45, endangle = 135;
; ` ]# Y/ ~" [+ a/ y% }. W/ jint xrad = 100, yrad = 50; </FONT></P>
4 g" N3 ` c# C4 p! s+ l< ><FONT color=#0000ff>/* initialize graphics and local variables */ $ k8 O7 q5 x( j' t. R
initgraph(&gdriver, &gmode, ""); </FONT></P>
3 t- F. H* H# Z2 O< ><FONT color=#0000ff>/* read result of initialization */ - L6 W4 ^: P9 ~4 o7 g9 ?* W8 A2 M
errorcode = graphresult(); 5 S$ ]# ^. U5 d$ K5 |% ^6 R
if (errorcode != grOk) /* an error occurred */
^, j* F3 ^* a) v. A! b3 T{ 7 \% ^9 q5 U. S1 R
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 5 I K1 q# j7 E# \9 M1 \/ Y7 n
printf(" ress any key to halt:");
/ k& `: i! [! D/ H% C2 Cgetch();
! w L2 V- a5 b0 S- [exit(1); /* terminate with an error code */
0 F( ^0 K3 o9 y! g3 _; Y} </FONT></P>
3 u& h" _! M$ k< ><FONT color=#0000ff>midx = getmaxx() / 2;
0 w7 z* ?5 H0 g" i! x! W. U( Nmidy = getmaxy() / 2; </FONT></P>' E5 Z6 z: @( j2 j
< ><FONT color=#0000ff>/* loop through the fill patterns */
2 n! x4 O( @; H/ I( ]" o. R, [for (i=EMPTY_FILL; i<USER_FILL; <br i++)> { $ P) _3 i8 a* o" T- [
/* set the fill style */ $ [1 d4 Y, d' J: B5 }% W: C; t+ [
setfillstyle(i, getmaxcolor()); </FONT></P>
1 V0 O5 X9 m# X9 G- _# F+ l< ><FONT color=#0000ff>/* draw the sector slice */ 4 M, ~4 g* D( _* H
sector(midx, midy, stangle, endangle, xrad, yrad); </FONT></P>
- N) X }: E6 e; i$ J b& C< ><FONT color=#0000ff>getch(); 4 k- F1 r, I3 ]3 Q2 }
} </FONT></P>1 Z7 `# j- r( B& Y' P/ l' Q1 L$ ]6 J
<P><FONT color=#0000ff>/* clean up */
3 x3 W9 s! {8 a" Kclosegraph();
3 @" X, w- {, l) D. Treturn 0;
+ B* p/ q- e' R0 P* q( O6 k}
9 B* S, ^ R$ @. B. s4 {</FONT></P>
# J* _5 Q( X$ ?% j* o3 ]( w5 p<P><FONT color=#ff0000>函数名: segread </FONT>2 V4 c& k. ]% K! r* R- I9 @
功 能: 读段寄存器值
3 k+ o1 c. j4 Q5 H O用 法: void segread(struct SREGS *segtbl);
" M5 B" d) \: C' _$ z程序例: </P>
7 G' h0 t& G* b9 c, t<P><FONT color=#0000ff>#include <STDIO.H>; }! B, k2 Y, t$ N- G' ]
#include <DOS.H></FONT></P>$ `0 E; q2 U* B& |# z+ l
<P><FONT color=#0000ff>int main(void) ' M, e$ |; n9 W. x
{ & T" T" m2 ^# K b, K! q1 B
struct SREGS segs; </FONT></P>
) s7 O% q$ x5 K- ^; k<P><FONT color=#0000ff>segread(&segs);
" I, Z2 _/ j( p" x& k; H. O4 u' nprintf("Current segment register settings\n\n"); 0 D8 J n* e8 V' d
printf("CS: %X DS: %X\n", segs.cs, segs.ds);
' z' K; t1 z% D1 U8 M& qprintf("ES: %X SS: %X\n", segs.es, segs.ss); </FONT></P>
* ^ ]/ @# B/ Q$ x3 R<P><FONT color=#0000ff>return 0;
! A& q" S: _, j6 ~} </FONT>/ o2 e# J, Q2 ^3 T# Z5 _
4 t; Y+ S1 a2 D</P>/ @1 K! n4 w8 u! M, h2 i0 E# C) P7 J
<P><FONT color=#ff0000>函数名: setactivepage </FONT>0 ^9 S# C1 Y3 { o% R1 U+ `- h" v
功 能: 设置图形输出活动页 $ @/ o2 Q- f8 a {9 a* A, v
用 法: void far setactivepage(int pagenum);
. N8 n+ q0 f S2 H/ {% `程序例: </P>4 |# P. M# |, x
<P><FONT color=#0000ff>#include <GRAPHICS.H>
* B7 i& F, \7 Y7 d#include <STDLIB.H>+ @& T9 e1 l9 P n
#include <STDIO.H>- _8 p3 l! W( n$ w& C7 _& A6 l0 z
#include <CONIO.H></FONT></P>
! s7 a0 W$ m7 T<P><FONT color=#0000ff>int main(void)
8 E b& f; Q( \; p V{
, y+ q! Y9 j1 e9 M: l( {9 y/* select a driver and mode that supports */ 9 Q Q" ^" j3 ^
/* multiple pages. */ , k" b0 ]. M- [/ q$ v) V3 m
int gdriver = EGA, gmode = EGAHI, errorcode;
( l& l* @4 G/ S( \5 y! Fint x, y, ht; </FONT></P>
; S2 z+ @- E& E8 T- b8 I<P><FONT color=#0000ff>/* initialize graphics and local variables */
/ h8 P5 |; b" sinitgraph(&gdriver, &gmode, ""); </FONT></P>) g% b+ i% @% V0 j+ E- M
<P><FONT color=#0000ff>/* read result of initialization */
+ @9 d4 m5 F: w4 X7 y0 Verrorcode = graphresult(); & r! A9 H6 L& W$ b
if (errorcode != grOk) /* an error occurred */
8 c9 B: j+ y2 d$ R8 r{
- r4 m1 J( b9 E- bprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 3 y2 }* `* j+ e' n$ c, h
printf("Press any key to halt:"); 1 C* z" h0 @/ a7 T# s9 K
getch(); 0 _! t3 J I0 F
exit(1); /* terminate with an error code */ - F% E& q s. K) a* r' a
} </FONT></P>; i) Y% y' o4 {& _: D
<P><FONT color=#0000ff>x = getmaxx() / 2;
& M( _4 S M# yy = getmaxy() / 2;
, n* B/ m$ b& n0 x: M6 Rht = textheight("W"); </FONT></P>% r x6 K4 X- K( z1 K: C3 U
<P><FONT color=#0000ff>/* select the off screen page for drawing */
0 M& h$ Y2 `8 M& G. R' \# psetactivepage(1); </FONT></P>
6 R6 V H& c( K( V" G: k: s/ H9 e4 Y<P><FONT color=#0000ff>/* draw a line on page #1 */ + Q. E& i' B! \) ^4 R) N
line(0, 0, getmaxx(), getmaxy()); </FONT></P>$ n3 h- R& Q3 g+ A) P* n
<P><FONT color=#0000ff>/* output a message on page #1 */
* G' }8 d6 n: j9 z. U. Lsettextjustify(CENTER_TEXT, CENTER_TEXT); 8 W# T( M+ W( a4 i8 q
outtextxy(x, y, "This is page #1:"); & a$ |! {& X5 |8 D
outtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
m& ]2 p0 @, }3 W/ l4 b, c1 @<P><FONT color=#0000ff>/* select drawing to page #0 */
5 [5 ~1 p; E! _4 {- Lsetactivepage(0); </FONT></P>8 A' {' E8 t) c8 U
<P><FONT color=#0000ff>/* output a message on page #0 */ ' P+ y# T) v1 A' N
outtextxy(x, y, "This is page #0."); " v( G; D' O# U; o0 f m
outtextxy(x, y+ht, "Press any key to view page #1:");
& ~2 s+ G' V+ _, J5 |getch(); </FONT></P>- H7 J0 j! A, m! y/ V/ i
<P><FONT color=#0000ff>/* select page #1 as the visible page */
3 @0 J: n6 L& ~setvisualpage(1); </FONT></P>
5 n) ?; k* M0 J4 W; ~<P><FONT color=#0000ff>/* clean up */
5 C7 R+ f5 w# H9 ~' S* Kgetch();
: V' t. f- F T) q; {$ ~closegraph();
M% [1 O2 z% E! c; b5 V" J" q6 Xreturn 0; 6 v: m% \6 O; {# w! G7 M6 w
} ! G7 a; N( l% }" X
</FONT>
: z3 o4 \2 L. Q; G* @6 G</P>8 v; E" c/ U! _" }- P0 r
<P><FONT color=#ff0000>函数名: setallpallette </FONT>" N- Q4 ^' A z* s' s9 ^, e+ q# _' s
功 能: 按指定方式改变所有的调色板颜色
+ b& w3 b! n# I1 M用 法: void far setallpallette(struct palette, far *pallette);
6 L9 n& O9 V0 `* ~程序例: </P>! L& [7 ]9 {9 h0 Z1 M) l$ c& y4 g
<P><FONT color=#0000ff>#include <GRAPHICS.H>2 @: S1 v6 ~. k4 S
#include <STDLIB.H>9 G' h6 X$ \# X
#include <STDIO.H>+ f* D' Q( C _7 m4 x. B( L
#include <CONIO.H></FONT></P>% T3 [& {5 C! B0 O
<P><FONT color=#0000ff>int main(void)
: }; X' w/ p" R6 U% ^{
6 z# [$ u5 X9 ~, u* N% u0 Q' T/* request auto detection */ 2 z2 t/ A8 {$ D# }7 c" ?
int gdriver = DETECT, gmode, errorcode; : d5 Q$ }0 Q- q4 [- a& R% n0 S
struct palettetype pal; " t0 s k; | F, `& p7 ?6 `+ X
int color, maxcolor, ht;
k; h" n0 x( r; J( h& {int y = 10; / {2 v9 q8 e2 P! W
char msg[80]; </FONT></P>; H4 ~ g6 G% b+ Z6 j6 k% o. F
<P><FONT color=#0000ff>/* initialize graphics and local variables */
; j4 [; f3 M+ e0 e# \8 L6 y. O) i" Pinitgraph(&gdriver, &gmode, ""); </FONT></P>
7 I) V" B; f8 C- `" |1 D<P><FONT color=#0000ff>/* read result of initialization */
- u W2 w0 z0 K8 P2 K% `) [errorcode = graphresult(); 0 {5 f" B: j7 e: r! m
if (errorcode != grOk) /* an error occurred */ 2 x- J2 L* ]$ q: A P
{
. _, j, \$ i# Sprintf("Graphics error: %s\n", grapherrormsg(errorcode)); " k1 Z# A- ^9 u) k
printf("Press any key to halt:");
1 F% F! ]" H( p. K- K7 Rgetch(); 7 h# Z1 Q: i8 I# M% t
exit(1); /* terminate with an error code */
( T+ |4 x2 a3 t/ Y} </FONT></P>
* R5 j, B {1 _<P><FONT color=#0000ff>maxcolor = getmaxcolor();
9 G3 e: ]; m" Q4 M4 w8 vht = 2 * textheight("W"); </FONT></P>' a5 P; b4 F# y/ p7 A+ ^6 m! _1 x6 @
<P><FONT color=#0000ff>/* grab a copy of the palette */
" C3 K! J! i- Rgetpalette(&pal); </FONT></P>
6 ]- s; p. r+ h: Y6 e* V; f! N<P><FONT color=#0000ff>/* display the default palette colors */ 9 Y' u9 _& N, M* R" Q
for (color=1; color<=maxcolor; color++)
4 C6 H6 F3 S- W5 A{ 9 \9 |; x4 k/ M: y, s
setcolor(color);
8 \& h/ A. I& [' M' d' s! asprintf(msg, "Color: %d", color);
) F/ r. P0 l( O. u: M J' Z/ nouttextxy(1, y, msg); . a% m5 H8 P# R2 I$ }$ N( E
y += ht; 6 k% m j5 a. L1 U6 @
} </FONT></P>
7 y7 `( I& E! j H<P><FONT color=#0000ff>/* wait for a key */
/ C+ o9 d0 x3 ~+ e% pgetch(); </FONT></P>
8 ?- A( r* }% L0 J- w<P><FONT color=#0000ff>/* black out the colors one by one */
2 ]% y2 k# w! N0 X. Ofor (color=1; color<=maxcolor; color++) 0 K" @6 I9 m0 w K- ]
{
* h9 m% T, x- P6 nsetpalette(color, BLACK);
0 g+ m% K, `$ o0 w r* ~8 ?getch(); . q. W& `2 Q4 [. y L
} </FONT></P>
6 j7 b$ \8 L4 Q1 q<P><FONT color=#0000ff>/* restore the palette colors */
# a/ r- L8 t% Isetallpalette(&pal); </FONT></P>
+ R' J6 ^: s4 f" r. Z<P><FONT color=#0000ff>/* clean up */ $ Y* D# Z3 T4 H
getch(); 7 w! t A' E9 k2 x) Z( h/ a3 W( S
closegraph(); ' ?. S& U7 ~4 a
return 0; & {7 s6 s! R, V( \6 f- d6 C6 M" G+ R
} 7 f6 g* s9 J# {3 g
% J3 X. ^( v- \* s* g</FONT></P>
6 k7 d5 E- a x1 z" U$ Y3 p+ s5 S3 \+ W<P><FONT color=#ff0000>函数名: setaspectratio </FONT>
! y9 [4 U' a$ y R功 能: 设置图形纵横比
4 }7 V. b+ x% l* ] N$ u, @- c用 法: void far setaspectratio(int xasp, int yasp); . D) ?6 z7 v! c& }8 S9 u% E6 k+ p
程序例: </P>5 \$ e4 p. e2 n: s3 }- ?
<P><FONT color=#0000ff>#include <GRAPHICS.H>
+ ?* F/ [, B! M1 g, J8 f#include <STDLIB.H>
+ m3 {5 N/ n; ?#include <STDIO.H>% a$ h7 a+ S c: Q3 ]7 ~$ D; ]. ^' K
#include <CONIO.H></FONT></P>
0 M6 h. l/ i. h+ E7 ]<P><FONT color=#0000ff>int main(void) " R9 A' d# f+ C! O6 E. E
{
5 Z/ k: D# _+ B( j) I* p/* request auto detection */ 8 P8 a8 ?2 t5 ?8 G
int gdriver = DETECT, gmode, errorcode; " o6 @% _) ^7 S3 y7 v9 u, V
int xasp, yasp, midx, midy; </FONT></P>
e6 q, M2 c& R<P><FONT color=#0000ff>/* initialize graphics and local variables */ ! Z4 t* [% H) C9 ~9 `' h2 D5 b
initgraph(&gdriver, &gmode, ""); </FONT></P>6 c; s# T9 [, F* v
<P><FONT color=#0000ff>/* read result of initialization */
% Y! l' p1 J+ _) g, {errorcode = graphresult(); % [& X$ s {, W+ G& Y+ o
if (errorcode != grOk) /* an error occurred */ z$ _2 ]$ n( e: I6 @
{
# W% K" w3 K9 }) X5 x( q/ J; }3 S+ vprintf("Graphics error: %s\n", grapherrormsg(errorcode));
" x1 }' `5 f9 g n1 _printf("Press any key to halt:");
3 j3 ^* q6 |8 F, i- o5 lgetch();
& H4 F, X, J) T' C+ _. t- G1 rexit(1); /* terminate with an error code */
+ a( @' E2 W }- ^8 C' t2 P} </FONT></P>
3 s2 Y' `0 P: K' d<P><FONT color=#0000ff>midx = getmaxx() / 2;
8 K( X- T r$ c/ J1 E/ W) c0 }midy = getmaxy() / 2;
0 d+ d" {$ q# ]# z5 y% H. {5 usetcolor(getmaxcolor()); </FONT></P>
3 k, J( `/ Z& X& E# U- Y$ g) Q q2 S<P><FONT color=#0000ff>/* get current aspect ratio settings */ / E$ i6 T/ s- ^* K' _
getaspectratio(&xasp, &yasp); </FONT></P>: V* a' _; s' g. l T. J3 {
<P><FONT color=#0000ff>/* draw normal circle */ 8 `, R- K% l- v/ r
circle(midx, midy, 100); + n3 R9 }# [5 ~) {
getch(); </FONT></P>& @, D( p5 X, s
<P><FONT color=#0000ff>/* claer the screen */ $ m) ]8 I2 i% w! Y! N
cleardevice(); </FONT></P>" A9 U1 w1 x* o! ^" K3 ~& Z2 q
<P><FONT color=#0000ff>/* adjust the aspect for a wide circle */ ' _ P; S$ i+ J/ n
setaspectratio(xasp/2, yasp);
4 ^$ z- E' ? _. z/ kcircle(midx, midy, 100); # E& H3 M& z' I# X
getch(); </FONT></P>
1 Q/ ^- e$ U* A0 P( Q<P><FONT color=#0000ff>/* adjust the aspect for a narrow circle */
9 J% s B) S, f! a4 |cleardevice(); ! h: ^. M8 k. `% J+ b, q4 ?8 Y9 A
setaspectratio(xasp, yasp/2); 9 ~. E* t; I+ v# z
circle(midx, midy, 100); </FONT></P>8 T6 M5 c# \$ d- v3 I+ M
<P><FONT color=#0000ff>/* clean up */
2 n. R' @) W2 a7 M' S' rgetch();
4 G: \- N1 y' v1 Cclosegraph(); # A9 p" k. K4 R+ D6 N1 ?- M
return 0; ) y+ F& ~+ T6 ]/ m/ k: `! @8 F
}
. b/ q2 F0 R; F J+ ~- H</FONT>3 D8 V" L+ b- u `
</P> P2 g# d7 v& [" F* g
<P><FONT color=#ff0000>函数名: setbkcolor </FONT>
( q) {$ f5 g. C4 z( ?2 ]# ~功 能: 用调色板设置当前背景颜色 " A/ N. T9 Y7 [/ Y v
用 法: void far setbkcolor(int color); ~) ^3 |: N/ M' f' x( Y' x
程序例: </P>
/ P' b% f) w a8 H+ `8 ^<P><FONT color=#0000ff>#include <GRAPHICS.H>
9 l2 p5 a9 l& {, W#include <STDLIB.H>
9 ~% j, [4 K( Q#include <STDIO.H>
' s; u% l/ v4 p. [4 a. M4 {" \#include <CONIO.H></FONT></P>
" ?$ s0 E1 I2 S<P><FONT color=#0000ff>int main(void) * O1 M/ g O$ j: g
{ : j+ Q# m+ l* K0 Y1 E5 p
/* select a driver and mode that supports */
- X8 V- V* K- |; v8 Y4 t/* multiple background colors. */
9 n O9 x* { c5 T, h. jint gdriver = EGA, gmode = EGAHI, errorcode; 4 ?7 v; f) O, d3 }1 e
int bkcol, maxcolor, x, y; * Y3 _% k- \: z" Y$ v' X
char msg[80]; </FONT></P>
) W( U1 q |) `% x6 M2 {<P><FONT color=#0000ff>/* initialize graphics and local variables */
) [% y1 a! g$ Binitgraph(&gdriver, &gmode, ""); </FONT></P>$ m4 g8 i+ [" z! ^, ~/ g7 e
<P><FONT color=#0000ff>/* read result of initialization */
8 d% `: C) M( Y2 ^% x( Z' { x" t9 oerrorcode = graphresult();
4 V/ s" x( A3 x* {" K$ bif (errorcode != grOk) /* an error occurred */
! p6 y6 e- X$ ^6 [& b{
( I' E& n8 C- f3 Jprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ) C& B" @; {8 E" B& h
printf("Press any key to halt:");
6 `! g; c6 G0 y7 S. dgetch(); 2 U5 [ D0 r, f Q9 W9 F" A7 W; l
exit(1); /* terminate with an error code */ 1 _ ^0 f( O3 y+ y
} </FONT></P>
$ H' D: ]; w' \- c1 [# P<P><FONT color=#0000ff>/* maximum color index supported */
4 t: I6 k+ d: j$ J6 K( c f4 wmaxcolor = getmaxcolor(); </FONT></P>
+ d' y$ [1 K3 w2 f3 F<P><FONT color=#0000ff>/* for centering text messages */
( t9 M, W9 i. b5 N3 b$ u3 esettextjustify(CENTER_TEXT, CENTER_TEXT);
$ R/ m) V6 u! Y& mx = getmaxx() / 2; ) `; R6 Z ?( |* ]; [
y = getmaxy() / 2; </FONT></P>
5 J" U2 D% V/ I! F) y9 l j<P><FONT color=#0000ff>/* loop through the available colors */
6 V3 _6 x5 O' ~9 s/ o! Mfor (bkcol=0; bkcol<=maxcolor; bkcol++) 0 Q% h8 T O; ?" P+ `) a
{
0 x5 `$ `4 ]7 K) l/* clear the screen */
3 }# K( @& k+ \ Ocleardevice(); </FONT></P>
: b4 x& L/ ~# Z- C2 [+ T( J<P><FONT color=#0000ff>/* select a new background color */
& {0 [) {* Y# I) p3 R' r8 }setbkcolor(bkcol); </FONT></P>
; j: `7 C0 }2 @( z<P><FONT color=#0000ff>/* output a messsage */ , U& g$ M/ [3 h& x8 t) Z$ Y& J
if (bkcol == WHITE) 1 f) U) ~, l G a1 ]1 Y
setcolor(EGA_BLUE); ) R; s: F* W* x- y9 R
sprintf(msg, "Background color: %d", bkcol);
) O/ W( V. o% J* s- F0 qouttextxy(x, y, msg);
* `+ F" i7 L$ w* [4 ?% A I6 Qgetch();
" }3 ^$ _- k: I5 P6 [, f" O} </FONT></P>+ H# i0 g2 h5 E' {- k# a8 q
<P><FONT color=#0000ff>/* clean up */ + W3 |9 P x- `" e) ?4 P4 X2 v
closegraph();
0 Q: N E) [7 h" \return 0; $ Z# g4 u: p5 Q# e
} </FONT>
) D! k$ C% e9 ]6 y7 B" |) K7 g0 n; \/ a. G, ^
</P>
8 B& X7 Y4 A" ^8 X<P><FONT color=#ff0000>函数名: setblock </FONT>
# q, ]$ C) Z/ ^2 \2 E' J6 j功 能: 修改先前已分配的DOS存储段大小
8 n1 M& l" i! m% N( L用 法: int setblock(int seg, int newsize);
6 [9 X0 [9 E" F6 f程序例: </P># { R! r$ n3 {3 d" S8 h
<P><FONT color=#0000ff>#include <DOS.H>" `9 s" s* F5 ?9 {( o0 Q/ q+ v }
#include <ALLOC.H>% w. P9 `4 H7 L. H1 [2 H
#include <STDIO.H>; A8 F( w p) q7 b' f* p
#include <STDLIB.H></FONT></P># Y& | ?7 u7 w- s5 Q
<P><FONT color=#0000ff>int main(void)
; [# k6 d% ^2 h$ w3 a& b; c{
) @+ B7 s- E6 ~" n' |6 C! |" ^unsigned int size, segp; & E6 I) v+ z+ r8 U \) H n* L
int stat; </FONT></P>( ]7 o. A% l: q% P1 l' L( i& |7 S
<P><FONT color=#0000ff>size = 64; /* (64 x 16) = 1024 bytes */ 8 ^, ~. _$ n0 k; K+ Q$ _
stat = allocmem(size, &segp);
! T5 _* e3 M6 l, q1 P' gif (stat == -1) 8 h1 p; u6 w* o# O. i3 w
printf("Allocated memory at segment: %X\n", segp);
; T0 u; D5 {0 F$ Q% Uelse ( ?! L, [! s' ~ w
{
$ c7 h; g; Z6 _1 L# ]printf("Failed: maximum number of paragraphs available is %d\n",
% C q; y. v& @% astat);
' i) F% ?. m, H# W3 [exit(1); 7 N& _( Y9 h8 f5 u! u, b
} </FONT></P>
5 c/ ~! [, j( a3 T v<P><FONT color=#0000ff>stat = setblock(segp, size * 2);
, s, l" F1 [+ v$ Q/ d4 B4 a M# Rif (stat == -1) : H6 x0 L+ U" w0 a5 x5 j
printf("Expanded memory block at segment: %X\n", segp);
1 `1 B9 e% K4 w8 |5 H5 U; q5 f& @else 9 j& x& T. U5 E: A
printf("Failed: maximum number of paragraphs available is %d\n", # X8 ]6 O% k. b$ u
stat); </FONT></P>: V8 C* N& ~. t4 T o9 S
<P><FONT color=#0000ff>freemem(segp); </FONT></P>
, T4 W: d# d2 n<P><FONT color=#0000ff>return 0; * W& T A/ _" Z
}
9 M% Z O6 q& J- U+ ]7 g, u0 @</FONT>
]7 {6 I) X: S" o( L</P>6 J- S- Z; k# w/ P
<P><FONT color=#ff0000>函数名: setbuf </FONT>9 |# A `, O; T, `) P0 X1 _) k
功 能: 把缓冲区与流相联
* _" V4 _1 Y( F* O+ `& s用 法: void setbuf(FILE *steam, char *buf); 9 t1 p# U# s% Y+ V" ]- ~( T
程序例: </P>8 S' L+ x* i# v% W8 g$ B, _
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
# ^+ M: |- Y) g A<P><FONT color=#0000ff>/* BUFSIZ is defined in stdio.h */ * @' o: o9 L# r
char outbuf[BUFSIZ]; </FONT></P>
7 n* `8 |. J) p# M! z4 o<P><FONT color=#0000ff>int main(void) 1 S; w6 Y) \9 L+ n" x$ K
{
( a9 \/ N" D2 ~& J8 G/* attach a buffer to the standard output stream */
0 ]- w, w( C" X" s- psetbuf(stdout, outbuf); </FONT></P>8 L$ e- p& t3 U% }5 U/ Y: s
<P><FONT color=#0000ff>/* put some characters into the buffer */
X1 T! N& ]7 M/ l2 Aputs("This is a test of buffered output.\n\n"); 2 P8 h9 s$ ~) _; ^
puts("This output will go into outbuf\n");
, T/ _7 z: |3 K, {. qputs("and won't appear until the buffer\n"); . u, ^: C) H4 f' Y4 v X! s/ \
puts("fills up or we flush the stream.\n"); </FONT></P>3 @( j3 |2 H% u6 j2 l' Q% j
<P><FONT color=#0000ff>/* flush the output buffer */
2 p( Z" v r1 ifflush(stdout); </FONT></P>9 r- L _1 u2 \9 a5 L
<P><FONT color=#0000ff>return 0;
& z" m+ \- K0 k. R% u/ _5 h}
' M# l$ n6 u3 @ X: o3 w0 D# {</FONT>
, p4 r# x7 v- u/ \6 d2 j0 P P, k</P>: g1 v5 d/ G- g/ E% G+ S& E
<P><FONT color=#ff0000>函数名: setcbrk</FONT>
9 b! ?4 z* Y6 M; a* O9 j2 ~4 ?功 能: 设置Control-break
" ^7 E6 V* G+ L1 m z用 法: int setcbrk(int value); 5 P( h: @) J( U" [$ A: O
程序例: </P>
% I8 _% m, t5 ~. R) F7 T& c& ?# |<P><FONT color=#0000ff>#include <DOS.H>
6 S5 D' B H# h# d#include <CONIO.H>. e p ~- M7 L% d
#include <STDIO.H></FONT></P>
% B7 b7 x* l: e+ u% |0 R; @ z+ a<P><FONT color=#0000ff>int main(void) 0 O a+ u% }5 @( K; C% W/ \
{
. U+ Y" y2 U- Z6 @1 s( Wint break_flag; </FONT></P>0 A# E1 z+ r& ?- Y
<P><FONT color=#0000ff>printf("Enter 0 to turn control break off\n"); 9 j% F$ c! n" Q- _% T
printf("Enter 1 to turn control break on\n"); </FONT></P>$ ?: {3 _6 Z" T+ e! l9 J
<P><FONT color=#0000ff>break_flag = getch() - 0; </FONT></P>
* t; Z" C H& [<P><FONT color=#0000ff>setcbrk(break_flag); </FONT></P>! o2 s5 g0 @% Y
<P><FONT color=#0000ff>if (getcbrk()) - m" j& l8 g$ M
printf("Cntrl-brk flag is on\n");
5 }/ E# ^6 T3 J- Y+ nelse 6 q9 c f l, O; j3 m
printf("Cntrl-brk flag is off\n");
/ x" B. ^+ Y! t* i, Zreturn 0; $ F( ^$ u7 D' q5 `4 V" L, q( V
} " n. x0 I+ O" u7 A
9 I* T, }4 F: d$ W! M" V</FONT>
9 x, J. ]$ o# ]) ]9 O0 k; N</P>5 a: m% B9 F1 q, E
<P><FONT color=#ff0000>函数名: setcolor</FONT> # D: P2 w# ?1 Z
功 能: 设置当前画线颜色
% K3 x; x. R8 c/ u" t1 V7 Z- L用 法: void far setcolor(int color); ' B2 c8 J ?- r* S$ n; B. W7 t
程序例: </P>) t# |9 o7 x" ~+ [# s( N
<P><FONT color=#0000ff>#include <GRAPHICS.H>) _" a# e3 r, U8 B" c
#include <STDLIB.H>
. ^6 ]4 Z* @( G3 P- z, _' g#include <STDIO.H>- l& b+ B) L3 Z
#include <CONIO.H></FONT></P>: _% y1 Z4 R0 Z7 Z! F2 b: ~8 J
<P><FONT color=#0000ff>int main(void) ) _' i/ V! K9 X0 O4 [& }2 B
{ , W0 H4 R- y: {" T7 ?/ T$ z9 h
/* select a driver and mode that supports */ 8 e3 R. L0 A& a. {2 G, @) x5 ^
/* multiple drawing colors. */ - d4 Y2 y: e) D2 C3 T& S- p
int gdriver = EGA, gmode = EGAHI, errorcode; . L; U: W$ d$ i$ b+ H1 ?
int color, maxcolor, x, y; 9 G5 `6 E: z% h' M
char msg[80]; </FONT></P>1 \! Q' [! ]; L5 N! t. m2 }; d/ E
<P><FONT color=#0000ff>/* initialize graphics and local variables */
- A |1 p4 t/ Q7 Jinitgraph(&gdriver, &gmode, ""); </FONT></P>) [! X; g8 I( y/ F- q
<P><FONT color=#0000ff>/* read result of initialization */
4 m5 _0 X. w0 j& h* A* D; l/ }errorcode = graphresult(); ( N3 Q% c% J. G; M! X
if (errorcode != grOk) /* an error occurred */ j8 X. t8 y4 |! i8 \; W- i. K
{ , ~6 W9 n* B9 ~0 e5 \
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 9 c e! U. |4 [
printf("Press any key to halt:");
) o% f4 {) K; b$ i% Vgetch();
" t: \3 j. |" V" Y! qexit(1); /* terminate with an error code */
2 m/ R: F6 V5 {, C, u} </FONT></P>, P7 y) }* W8 |0 |
<P><FONT color=#0000ff>/* maximum color index supported */
7 E' j1 n5 b3 i2 `( y: d, g Wmaxcolor = getmaxcolor(); </FONT></P>
) u' d) U) p8 D4 S) L. ~<P><FONT color=#0000ff>/* for centering text messages */
$ W. Y( s- g5 }' u) [+ a: ]settextjustify(CENTER_TEXT, CENTER_TEXT);
6 j( ~. ?2 ^3 R* m1 H2 ?5 Sx = getmaxx() / 2; * e5 p ^ x( p0 X: t1 M/ p
y = getmaxy() / 2; </FONT></P>
/ [. I5 t9 E: y9 D+ U<P><FONT color=#0000ff>/* loop through the available colors */ ) D+ p0 p/ T/ z9 M4 L& ^
for (color=1; color<=maxcolor; color++)
3 M1 k/ v5 `) k p{ 6 w0 K1 e8 C0 G+ v0 A( X2 z
/* clear the screen */
8 p0 o$ `+ d4 S9 c7 F& K; Wcleardevice(); </FONT></P>
7 E: J8 b( W, U, f% F3 ^<P><FONT color=#0000ff>/* select a new background color */
) P/ j) I q* t9 }2 P& Osetcolor(color); </FONT></P>
4 q7 d4 L2 o% h9 C8 d<P><FONT color=#0000ff>/* output a messsage */ [" V7 _6 ]6 Q" i" A# Z* G
sprintf(msg, "Color: %d", color);
8 f! T! b9 |- l8 Houttextxy(x, y, msg); # D9 ?) U. w9 ?4 X7 e
getch();
6 m; }8 e7 G) k& h- k- W} </FONT></P>
- ]6 c$ s" J. Y- U# A<P><FONT color=#0000ff>/* clean up */ 3 `' [, n4 t" X' H& Q- N
closegraph();
* V0 J7 |+ E: |( ]9 vreturn 0;
' x* \/ O- v- q; e7 R8 ?}
) [- k5 L$ W6 Q6 D' S</FONT>
, W# Y$ g; ]# N; y- |% F# B I</P>
6 n4 y! m7 W0 C3 L5 [# n, n: Y& G<P><FONT color=#ff0000>函数名: setdate </FONT>. w3 t( [ i9 u& p
功 能: 设置DOS日期
" @" i, V* Z& D. U4 C4 @. E* V用 法: void setdate(struct date *dateblk);
( N, T0 y# W6 Q; }/ u程序例: </P># L% B `; m' V- q7 i
<P><FONT color=#0000ff>#include <STDIO.H>
1 g. X7 U H6 b#include <PROCESS.H>5 K# E0 k" S# C0 e
#include <DOS.H></FONT></P>4 b- G1 N8 V K1 B5 {
<P><FONT color=#0000ff>int main(void) " F8 }- Q6 ^' {) b4 [& k
{ 5 |- I* O. s; H! B: M" H! |# k; r
struct date reset; * [5 F: m Q) n" E# t8 e6 x+ P% m
struct date save_date; </FONT></P>/ @% O6 p8 e3 X* T( r: t
<P><FONT color=#0000ff>getdate(&save_date);
& k n7 g% h! U+ s9 t8 {3 Oprintf("Original date:\n");
/ h" X, ]. m2 M" v- isystem("date"); </FONT></P>
1 |' ` p2 P. B% N! |<P><FONT color=#0000ff>reset.da_year = 2001;
0 X7 B2 N# M! o8 L* _" preset.da_day = 1;
* i6 l7 V* n6 r9 s4 K/ qreset.da_mon = 1;
! m7 k# C {% ~0 f4 Tsetdate(&reset); </FONT></P>4 s1 |7 V' `: \7 m
<P><FONT color=#0000ff>printf("Date after setting:\n"); : T$ q' C# F0 i2 h8 m
system("date"); </FONT></P>6 M2 s( E+ t4 M9 O' v" z
<P><FONT color=#0000ff>setdate(&save_date); # U' X) _" ], r& Q
printf("Back to original date:\n"); % z* |: x! C% K' i
system("date"); </FONT></P>3 h7 J" _' ^5 R& u9 Y
<P><FONT color=#0000ff>return 0;
% A( J% H' o0 }0 r4 t! N} ; h+ l' a* S: ]8 F1 M6 ]: [
</FONT>" z1 y/ u) L. O% c0 ]0 R- C
</P>
) K: c7 o, T3 A$ V& ]* J<P><FONT color=#ff0000>函数名: setdisk </FONT>
! P& z) R% n) {# G( u; a, \9 ?功 能: 设置当前磁盘驱动器 5 ^7 {' y, P/ M
用 法: int setdisk(int drive); 2 {' W0 v! {6 u6 K" Y: a# J
程序例: </P>5 F0 T4 r2 i# @5 |5 w R/ _ h- J% C
<P><FONT color=#0000ff>#include <STDIO.H>
, `& e: ^$ s7 E- I#include <DIR.H></FONT></P>
0 [' a3 f6 S. o% Y2 T<P><FONT color=#0000ff>int main(void)
! @! d0 E! i4 P' C$ _2 x{ 0 r" g1 w, N$ q( i0 ?
int save, disk, disks; </FONT></P>
1 n p7 r: r3 P1 d<P><FONT color=#0000ff>/* save original drive */ 5 @" E1 o' H$ k# f$ ~
save = getdisk(); </FONT></P>8 ?4 o4 E6 V7 M1 ]3 V+ R
<P><FONT color=#0000ff>/* print number of logic drives */
0 U8 X4 C) J# h. |- ^disks = setdisk(save);
' H2 R" W2 j/ l+ U& pprintf("%d logical drives on the system\n\n", disks); </FONT></P>) A U% R/ x" B" w, D: M! b, H+ j
<P><FONT color=#0000ff>/* print the drive letters available */
( {/ F/ C7 |' C3 z0 W/ i9 rprintf("Available drives:\n"); ) ^4 w6 V) ^, n. d& d" T7 B, H5 j
for (disk = 0;disk < 26;++disk) ; s) e+ x# Z. T$ d, j0 _
{ " @; I2 [& G4 C. _3 o$ }' q
setdisk(disk); # f) y5 N1 j5 Q5 s( Y( s
if (disk == getdisk())
1 u% { p: S+ U* t0 h0 eprintf("%c: drive is available\n", disk + 'a'); 0 S: @, }; E9 C; |/ E
} ' }- J1 K0 p7 E" }
setdisk(save); </FONT></P>' e Q. G p; S' X4 Y
<P><FONT color=#0000ff>return 0; ) H( x' r4 R; k1 F( U
} </FONT>1 B2 j( a1 S: m: ~6 y* V
+ E$ E! f! l4 w# a</P>
+ x0 d/ V" C# Q% v: U3 T3 J7 ]<P><FONT color=#ff0000>函数名: setdta </FONT>
/ D; c/ \# K/ t$ d+ M, I% ^功 能: 设置磁盘传输区地址 ( [5 {3 O' j( `1 g# j# S) E
用 法: void setdta(char far *dta); # P" H) y* U$ p0 H# w
程序例: </P>2 G& w6 g9 t0 p+ n) V
<P><FONT color=#0000ff>#include <PROCESS.H>
; B" t7 l$ E" [1 n# C: x#include <STRING.H>" Z; \" O/ V @ h
#include <STDIO.H>
9 y3 W4 W. U# M! i#include <DOS.H></FONT></P>
+ Q5 ^& ~- d6 n- P& U* t<P><FONT color=#0000ff>int main(void)
6 T$ O" [' } V( a{
* a3 n, o3 G% V' S% ?: [char line[80], far *save_dta; , Q# @# p/ ^/ l8 d: s
char buffer[256] = "SETDTA test!"; ! H" |: c) n! Y: u! ~8 c' ^
struct fcb blk;
. R4 ` L1 H% L: j/ d9 iint result; </FONT></P>! j7 w; L8 w, p
<P><FONT color=#0000ff>/* get new file name from user */ " @; D# R, o3 F
printf("Enter a file name to create:");
# A; P+ A7 D: h) |( Sgets(line); </FONT></P>$ b _( T0 ]2 b& k. }& p
<P><FONT color=#0000ff>/* parse the new file name to the dta */ 7 T1 a% ^/ Q8 A, ~ V$ `% c# a
parsfnm(line, &blk, 1); . @. P1 l A, _4 A: U( H
printf("%d %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>( L8 M3 G4 j1 K& Y
<P><FONT color=#0000ff>/* request DOS services to create file */ % c- Y B, \9 o8 s" t
if (bdosptr(0x16, &blk, 0) == -1) 1 [) B$ R4 G6 ` c3 r1 d
{ * z$ A* m( A Z; i3 v: }/ i8 P. l# _
perror("Error creating file"); 3 L: H4 _- R5 F+ }; j! {& u6 D
exit(1);
; z J# K! ^) Y0 p} </FONT></P>
5 o9 F& c% k; T5 {' b0 a<P><FONT color=#0000ff>/* save old dta and set new dta */ & k9 `! ]; j( n
save_dta = getdta();
1 d1 x d; d4 u+ A D2 Hsetdta(buffer); </FONT></P>, R" K9 j/ c+ t# x6 `
<P><FONT color=#0000ff>/* write new records */
+ ~" U5 ~" h0 |. h* k2 ?blk.fcb_recsize = 256;
- @, q) }/ `! z4 n+ D3 ~blk.fcb_random = 0L; $ T) I* ?3 S/ C" O, R3 K* u. @
result = randbwr(&blk, 1); ) N) y0 s4 O5 j
printf("result = %d\n", result); </FONT></P>
. L# _' X. D$ E; m<P><FONT color=#0000ff>if (!result)
: }& h: c1 m% Xprintf("Write OK\n"); " x% Z' \2 h9 W; }$ u) Z# i& }% `
else
# ]+ q+ A" D: ?{ c3 G8 x3 A1 i: B
perror("Disk error"); 8 b$ L1 ]. r1 m) b- B: p" E, [
exit(1); ) {" h& P1 G( r
} </FONT></P>
9 H- ]+ V/ u8 J( n" F8 C2 C<P><FONT color=#0000ff>/* request DOS services to close the file */
7 m; s: g) {( K- h nif (bdosptr(0x10, &blk, 0) == -1)
( M5 K3 y/ L$ Z; t }* p* E{
: `/ n |: w: {% l+ ]5 t5 ^perror("Error closing file"); / f1 W ^1 U9 {; s; c0 F1 {
exit(1);
8 N( l3 I0 j9 M# `& v} </FONT></P>5 B' n" `: X$ b7 d) E
<P><FONT color=#0000ff>/* reset the old dta */ / @1 a M0 l. i2 J
setdta(save_dta); * Y* w8 K) K( t6 W" B/ F
return 0; 5 q7 A) ?$ j0 F% t; m5 D
}
' v4 i* I1 J" J( }0 Y0 ^3 r</FONT>0 W6 l& Z* X7 O/ R& N
</P>2 r; Z; e% C; u0 e+ Z/ v
<P><FONT color=#ff0000>函数名: setfillpattern </FONT>- y; U! S7 w) q! _" y5 A
功 能: 选择用户定义的填充模式 9 r) `% n4 d& K
用 法: void far setfillpattern(char far *upattern, int color); ; m! V7 Q) x: r( t+ p5 h4 I
程序例: </P>* F7 K! x+ S6 E
<P><FONT color=#0000ff>#include <GRAPHICS.H>8 v4 R+ R8 B6 d
#include <STDLIB.H>
8 S$ v& ?: x* n H+ R#include <STDIO.H>
6 Q9 k% R2 @& _" p; Z- Y" s( H#include <CONIO.H></FONT></P>
# Z9 K) ^% h3 W4 I<P><FONT color=#0000ff>int main(void) 8 l1 h. S$ q' B1 q2 e1 Q
{ 7 n, K, I- Q7 ?$ s7 h2 L6 N/ J
/* request auto detection */ 5 w8 t& }/ O: h) U
int gdriver = DETECT, gmode, errorcode; ( P: L' x5 o+ K0 F: Y8 t
int maxx, maxy; </FONT></P>
` n) Z" U5 }<P><FONT color=#0000ff>/* a user defined fill pattern */ / }' v5 A# m/ c7 F, E& F
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; </FONT></P>
* B- g& l1 R9 \# [+ }<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 W0 g' c, q* x) S5 O7 w
initgraph(&gdriver, &gmode, ""); </FONT></P>' ~/ G' b. t- E
<P><FONT color=#0000ff>/* read result of initialization */ % Y% h/ L# A! I' r& b9 L% r3 I) @3 ~ @
errorcode = graphresult(); 1 k# l. B; b5 ^$ K& p
if (errorcode != grOk) /* an error occurred */
0 Z, a9 u+ M' H# E4 L2 d9 v. n5 I{
) D0 I+ G5 Y7 K, Nprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 6 u, o; g4 R% R/ A( d# b
printf("Press any key to halt:");
/ E) e `* H# V$ h* d6 d) Vgetch();
. V1 z+ X" @/ H( H) R4 {* ^' Jexit(1); /* terminate with an error code */ % g. H' q$ v$ I
} </FONT></P>& D3 x$ s8 z2 w. [1 y3 p
<P><FONT color=#0000ff>maxx = getmaxx(); / H* \ O# o/ j6 B" k6 S7 x
maxy = getmaxy(); . ~! }4 }% q6 [! V; K" Y8 s
setcolor(getmaxcolor()); </FONT></P>
4 j s. s0 q" ?$ J$ T/ v% C<P><FONT color=#0000ff>/* select a user defined fill pattern */
: y% A$ G: F# x5 gsetfillpattern(pattern, getmaxcolor()); </FONT></P>
- i% G! f# W; D; f& [$ m* K<P><FONT color=#0000ff>/* fill the screen with the pattern */
6 _. f9 ?( I1 Ubar(0, 0, maxx, maxy); </FONT></P>+ |9 {0 q( b5 }" r. y3 h* ]
<P><FONT color=#0000ff>/* clean up */
" T$ f# D! B( _2 L& vgetch();
" ]4 @9 ^3 _; w4 ?* o8 gclosegraph();
6 l+ o. _6 ~+ j6 r+ c8 greturn 0; 8 l! m7 ~1 @" [ H8 y
}
( p8 T, ~2 f7 u4 Y$ P</FONT>. A( k/ u1 a$ ^4 X0 s/ P' z& H
</P>0 U; G3 s# t/ O
<P><FONT color=#ff0000>函数名: setfillstyle </FONT>
3 z3 Q% ? b j8 \功 能: 设置填充模式和颜色
" n! M5 N/ ^. G; g" t+ a用 法: void far setfillstyle(int pattern, int color);
5 I0 I5 }" Q: Y O5 X程序例: </P>4 C9 Q6 h( a5 z3 |& T' t1 n
<P><FONT color=#0000ff>#include <GRAPHICS.H>
2 m! o- E8 ]( d# I( i5 Q4 r#include <STDLIB.H>
% j' B6 B) z& c5 t0 ~& U4 c#include <STRING.H>; D% m2 K0 Y+ w3 P6 g) C: {0 S4 ]% H% k
#include <STDIO.H>
2 Z9 X5 z" Q2 g- Y: F- P' z+ n" W' v; `#include <CONIO.H></FONT></P>
1 [6 _4 M! g" B<P><FONT color=#0000ff>/* the names of the fill styles supported */
* B3 W9 K2 k3 gchar *fname[] = { "EMPTY_FILL",
( Y7 \ Z7 q, X* S"SOLID_FILL", ; S6 z1 E; Z. I, {7 R8 S( G" D
"LINE_FILL", ) H4 l: Y9 K9 v% Q0 B! I
"LTSLASH_FILL",
/ A U2 U' m& r1 X0 I1 e"SLASH_FILL", g b1 ?* z8 @, y
"BKSLASH_FILL",
4 x9 z5 ~2 p* B" z3 P3 t"LTBKSLASH_FILL", 9 y/ q& r ~) [7 W
"HATCH_FILL", ) w: U" K( M; t4 b2 R: Q
"XHATCH_FILL",
8 Q0 O9 x/ Y$ {7 {* Q. o2 K"INTERLEAVE_FILL", 3 z. U8 ]3 |6 [! m" V& ^) n
"WIDE_DOT_FILL", 6 j" u( `# N& C# c, H4 }0 q
"CLOSE_DOT_FILL", 1 U" n, y* W% Z3 d; S
"USER_FILL" . r% u. K$ {# _* A' u5 ~0 t
}; </FONT></P>/ l, ^: o8 d! F3 H$ d) G
<P><FONT color=#0000ff>int main(void)
* T, l9 `5 e0 V. f8 D{ - S% o m+ {6 o) t
/* request auto detection */
& g4 B/ F: P' ^6 ~2 @2 Sint gdriver = DETECT, gmode, errorcode; % B6 j D' J8 t' h0 @: X0 z( C
int style, midx, midy;
5 _0 J5 p; i& P1 M kchar stylestr[40]; </FONT></P># W: r) l/ Y2 E9 g. q7 e! t
<P><FONT color=#0000ff>/* initialize graphics and local variables */
% T% H# Q/ Q% j) q( uinitgraph(&gdriver, &gmode, ""); </FONT></P>/ U$ m" d/ ?; r7 o# K
<P><FONT color=#0000ff>/* read result of initialization */ & I) b% X& @, k v4 K4 b
errorcode = graphresult();
/ t! x( t- S/ w: U( f+ o dif (errorcode != grOk) /* an error occurred */ 7 I$ v5 u5 B4 B+ ?( C
{ , y/ y/ D3 b$ t+ d
printf("Graphics error: %s\n", grapherrormsg(errorcode));
& F% K u( z& B' a+ Uprintf("Press any key to halt:"); + m! `1 B. Y5 r" D2 ?% ~1 p
getch(); m. k) Z. W6 ]7 Y. G
exit(1); /* terminate with an error code */
8 X7 n X+ M& {" r- J. A3 \} </FONT></P>4 h" ?; e) z( k& d( S! x
<P><FONT color=#0000ff>midx = getmaxx() / 2;
6 a7 v; T7 U. _2 L* b6 F, F, Smidy = getmaxy() / 2; </FONT></P>
4 f8 m8 U( p/ f3 a& d9 D' Q<P><FONT color=#0000ff>for (style = EMPTY_FILL; style < USER_FILL; style++)
. S! i- |0 @9 V. E{
( d: z- w6 a# i4 h/* select the fill style */ 4 J7 ?) e5 F: ~7 ?
setfillstyle(style, getmaxcolor()); </FONT></P>; z$ z8 I0 s- K5 k
<P><FONT color=#0000ff>/* convert style into a string */
. m2 w1 s7 g, h9 P4 _: y1 I! e/ Dstrcpy(stylestr, fname[style]); </FONT></P>
4 l) \6 {- M* m2 ?<P><FONT color=#0000ff>/* fill a bar */ : }# r( [1 x! l3 P: h0 _2 Q/ t/ f" y& e
bar3d(0, 0, midx-10, midy, 0, 0); </FONT></P>
7 h5 y5 c+ z& g" q<P><FONT color=#0000ff>/* output a message */
& V5 d o! ~1 a, _ E" Z7 nouttextxy(midx, midy, stylestr); </FONT></P>
% p% c: s% n! V( v# H<P><FONT color=#0000ff>/* wait for a key */
3 e, n* [. F. e) _, c0 y zgetch();
9 [5 O& T3 u( w% ocleardevice(); & U3 v" I, ~7 t# C( |/ i0 V9 V$ @6 y
} </FONT></P>2 U5 O( W, b# R7 V, @/ ~1 v
<P><FONT color=#0000ff>/* clean up */
b" c1 j3 b3 x) o( Fgetch(); 4 a+ I; _7 G( D" }2 A
closegraph(); 1 F7 s5 c0 Y7 u$ v, ^# A0 h; }4 ~
return 0;
1 x7 U! \# A) m% H3 @} </FONT>& c" z3 d H& d8 x/ |+ U0 h
4 p* b* ]9 a4 Z8 x$ z
</P>: T, u# `- k* n. X. I
<P><FONT color=#ff0000>函数名: setftime </FONT>
: w7 V* {. Y3 |$ j9 l功 能: 设置文件日期和时间 0 }* {! c4 s- l1 j
用 法: int setftime(int handle, struct ftime *ftimep);
0 Q& o" A( e) R( t! I& s# _* ?程序例: </P>' ~1 A$ f/ N7 u/ L8 I
<P><FONT color=#0000ff>#include <STDIO.H>" ^& t+ |0 [ _" U" a& A
#include <PROCESS.H>
) ]8 p0 e6 z. e7 ]% x#include <FCNTL.H>
* Q" I1 w9 ]' I$ M0 U4 o4 e( P#include <IO.H></FONT></P>
+ U3 g. p* G3 O( F1 I# @0 p1 {<P><FONT color=#0000ff>int main(void) . g- _( H& I& B0 ]
{
* O; \6 j ]. h8 |, u/ Cstruct ftime filet; 6 L& ^ l$ ^1 R% w% \% P: b. q
FILE *fp; </FONT></P>
3 d" j+ w0 Z+ e, P) |. m+ ^( s1 B<P><FONT color=#0000ff>if ((fp = fopen("TEST.$$$", "w")) == NULL)
2 z, ^, b) t/ Q+ k% X{
7 m( d2 w/ N" y9 pperror("Error:"); * C7 x/ O0 v1 q' H& V1 e
exit(1);
' }0 I0 p6 g$ [6 i& Y. S t2 e} </FONT></P>
2 l* R' I h5 f- Q) a$ y& r; R% P C<P><FONT color=#0000ff>fprintf(fp, "testing...\n"); </FONT></P>" F: p2 p0 A& X& Q
<P><FONT color=#0000ff>/* load ftime structure with new time and date */ + W: M; I; Q& o0 a/ V
filet.ft_tsec = 1;
8 n/ [; N) j5 \ s, Hfilet.ft_min = 1;
- W3 U! d9 Q: q* z! z0 Nfilet.ft_hour = 1;
( S2 F6 I( _- M' t- b' Pfilet.ft_day = 1;
) `; A3 `) }2 H8 Tfilet.ft_month = 1; ( @6 z1 p8 n; t: V! R3 |
filet.ft_year = 21; </FONT></P>6 S1 K D* f! H6 E G
<P><FONT color=#0000ff>/* show current directory for time and date */
# o/ T4 Y1 u+ n2 S5 o. Jsystem("dir TEST.$$$"); </FONT></P>
h; j2 O: V4 z4 L# W% E<P><FONT color=#0000ff>/* change the time and date stamp*/
' i* }# K- t7 p3 U) s6 v; Asetftime(fileno(fp), &filet); </FONT></P>
: e# s) u0 \! T* B$ c<P><FONT color=#0000ff>/* close and remove the temporary file */ ( } L" e) Q* Z0 N$ b& o
fclose(fp); </FONT></P>
9 Z2 a3 T+ y( F( T/ c; Q4 q% D<P><FONT color=#0000ff>system("dir TEST.$$$"); </FONT></P>5 W9 E5 n4 Y2 @2 G, ~2 e* ^) m
<P><FONT color=#0000ff>unlink("TEST.$$$");
3 m4 g4 r& E# K+ O3 p! areturn 0;
/ B8 o: a( ^8 i" x' Q}
- O% v+ @, N. W! R) k) J% s. [$ p4 V</FONT>
* q5 _8 E/ o8 ?! a</P>' n: X5 g# i" F5 N. [
<P><FONT color=#ff0000>函数名: setgraphbufsize </FONT>
# P h- ]* F& q" c( V0 O功 能: 改变内部图形缓冲区的大小 : s. F5 F+ F4 k1 g4 n
用 法: unsigned far setgraphbufsize(unsigned bufsize); 1 ~& I/ l, h( n
程序例: </P>
: @& N0 U# r( C+ D/ v. ]3 e; N( x<P><FONT color=#0000ff>#include <GRAPHICS.H>
4 G! Z/ s1 W a2 ~ Z1 y. V. Z#include <STDLIB.H>
' {9 G% e1 I0 N* S& ]#include <STDIO.H># W+ ]$ N; u: {# @4 H$ S
#include <CONIO.H></FONT></P>2 `: p' g! s2 g# C
<P><FONT color=#0000ff>#define BUFSIZE 1000 /* internal graphics buffer size */ </FONT></P>
. D5 |5 u: N! ^& g: r7 S1 \<P><FONT color=#0000ff>int main(void)
: ?, [* n1 P( e, D& F% ]1 Z{ $ ~2 W4 D% H* l
/* request auto detection */
& T$ o/ S. o! q8 |int gdriver = DETECT, gmode, errorcode; 6 G- y: S9 g0 |9 W- N6 N9 O
int x, y, oldsize;
7 H2 D. I' C+ Q9 \ k7 L; M7 _char msg[80]; </FONT></P>' p! o; b2 B" } G8 G' r
<P><FONT color=#0000ff>/* set the size of the internal graphics buffer */
3 O( ~+ e' w- k/* before making a call to initgraph. */
5 f# v% d: \7 I5 v& U6 I8 b) Aoldsize = setgraphbufsize(BUFSIZE); </FONT></P>6 `# V2 Z4 N$ I m. S1 G9 u \
<P><FONT color=#0000ff>/* initialize graphics and local variables */ " B: P; H+ }( J8 i% X( w# x0 H
initgraph(&gdriver, &gmode, ""); </FONT></P>
9 Y+ o% [$ P9 T7 U R<P><FONT color=#0000ff>/* read result of initialization */
/ f) b0 Z- B, F2 x7 S3 terrorcode = graphresult();
: D+ L* i0 n. u7 G9 fif (errorcode != grOk) /* an error occurred */
! @3 U0 p6 L6 w4 V4 t{ / q6 S6 {1 n# p$ h. i! E! d- J
printf("Graphics error: %s\n", grapherrormsg(errorcode));
+ P3 u; W+ K- }2 Bprintf("Press any key to halt:"); & V O! _8 R/ b$ Z* i# J( p
getch(); 5 \& \2 g8 L) {: k* j
exit(1); /* terminate with an error code */ ( @/ u5 b( a5 `4 I
} </FONT></P>, l% f6 a1 D: d* Y5 T
<P><FONT color=#0000ff>x = getmaxx() / 2; ) Y: ~ P- g: M; c; @
y = getmaxy() / 2; </FONT></P>
( l: D( v, y" n: q; m; e<P><FONT color=#0000ff>/* output some messages */
3 ?3 p# X- {0 r- e, q8 }sprintf(msg, "Graphics buffer size: %d", BUFSIZE); ( |; k8 q$ y4 z
settextjustify(CENTER_TEXT, CENTER_TEXT);
5 P) I, l( v4 [, J2 ]6 nouttextxy(x, y, msg); 0 Z8 S% b4 W- ^, h/ b+ m3 y# t
sprintf(msg, "Old graphics buffer size: %d", oldsize); " y- C" e. z5 B- }/ q! w
outtextxy(x, y+textheight("W"), msg); </FONT></P>
- G( ~3 Z- Y; G6 a' X4 u<P><FONT color=#0000ff>/* clean up */
: y8 u0 s. c) ~ y6 \$ [getch(); # n' C6 s N' X# U) a7 n) w
closegraph(); ( c' G; I% P+ x: f! I) p. b
return 0;
8 @$ ~5 Z2 q N) \9 T, Y}
# e8 I9 J8 K1 N+ V- w' [+ N' ]; _) f! I0 ^
9 |% l. y8 {9 G6 B4 M
</FONT></P>! S' d% T- B' e
<P><FONT color=#ff0000>函数名: setgraphmode </FONT>
. _& J7 U+ d2 E8 ]+ i3 o1 c6 R功 能: 将系统设置成图形模式且清屏 . J5 o. g2 e; A' S, _
用 法: void far setgraphmode(int mode); % @* h6 U& R5 U" c8 c
程序例: $ V _/ h) l. z, ~% g( S7 c
! a+ J* \+ {7 \( Y<FONT color=#0000ff>#include <STDLIB.H>
. n( J' ~9 D" M8 d5 y7 u#include <STDIO.H>
% w) v$ F9 g5 y F' V/ t6 G#include <GRAPHICS.H>#include <CONIO.H></FONT></P>
9 l3 a3 C, F( }7 Z8 n<P><FONT color=#0000ff>int main(void)
. p( _3 A/ l( v. Z. z{ ! L( i- j2 K' r
/* request auto detection */
9 @: U: B, R/ D4 H; Mint gdriver = DETECT, gmode, errorcode;
* K8 t2 R9 b, Z' {) f# Sint x, y; </FONT></P>' d, t# A' w' S g! m1 c
<P><FONT color=#0000ff>/* initialize graphics and local variables */
5 ~2 c, U" o# W1 ginitgraph(&gdriver, &gmode, ""); </FONT></P>. K& t+ t+ ^. b% B/ Y
<P><FONT color=#0000ff>/* read result of initialization */
( O: v4 q6 ]# U3 j) n& Aerrorcode = graphresult(); # z6 V9 t1 S4 m9 q; X
if (errorcode != grOk) /* an error occurred */
, [! R" h9 o9 }! k- x{ ( V$ t6 C; x% I: s5 t0 s* I
printf("Graphics error: %s\n", grapherrormsg(errorcode));
& G7 {' k- x+ ~& O7 nprintf("Press any key to halt:"); 8 U' J5 K! |% G# d
getch();
+ C3 j P6 K( z- }3 Dexit(1); /* terminate with an error code */
9 P: `# j! ]/ Q2 x* G; s} </FONT></P>4 I1 x. i; v- f0 O9 @( ]( V- Q, \
<P><FONT color=#0000ff>x = getmaxx() / 2; 6 y. p1 t3 ]5 W8 g0 Q
y = getmaxy() / 2; </FONT></P>
1 H# J) h8 S# @. ]5 g- H; W8 \; o% d( R* M<P><FONT color=#0000ff>/* output a message */
& M, k, m! N* n$ s6 xsettextjustify(CENTER_TEXT, CENTER_TEXT); , T2 M0 o/ G# a
outtextxy(x, y, "Press any key to exit graphics:");
+ c- F: w* L) ]5 n( W& Kgetch(); </FONT></P>9 b4 A! J5 O3 _, P+ }# V q
<P><FONT color=#0000ff>/* restore system to text mode */
2 M$ |+ A& p' {* H8 Rrestorecrtmode();
8 P4 K; ]% G4 L0 Pprintf("We're now in text mode.\n"); ! W+ I* S7 s4 H" `( [. k: B
printf("Press any key to return to graphics mode:");
( Y( f7 i( I0 |5 Q1 \getch(); </FONT></P>) ]: x$ K0 y; X0 b) k
<P><FONT color=#0000ff>/* return to graphics mode */
+ c" L1 b# C1 |+ {4 V5 @setgraphmode(getgraphmode()); </FONT></P>
1 Q2 b, z- n* U G- d0 J% J<P><FONT color=#0000ff>/* output a message */
& {0 X+ _; y$ t3 E5 F7 h( r4 `1 b1 xsettextjustify(CENTER_TEXT, CENTER_TEXT);
" o o& H) E B2 h ]outtextxy(x, y, "We're back in graphics mode."); / Z c4 D( ~; l7 I1 L, z
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>; e+ R, i# s% e1 w) g/ {
<P><FONT color=#0000ff>/* clean up */
/ g2 ^+ r/ i7 ]1 [getch(); 4 z! r1 l4 y- b% a$ J
closegraph();
) d5 ~1 i' f: L: B2 `. m/ |return 0;
% C+ O5 M, l; L; I0 J, e}
6 l8 b# s" A. J p8 s6 n5 @- ]' }( S& \' x7 T
" x! G( ?3 J* o, E* b" R8 G* j) z' v</FONT><FONT color=#ff0000></FONT></P>
/ t5 F/ o9 C/ c<P><FONT color=#ff0000>函数名: setjmp </FONT>
: W3 `, }! F4 q* U4 d. n功 能: 非局部转移
b' I4 ^+ z0 S用 法: int setjmp(jmp_buf env);
+ X/ _. o( g. w. f7 |. }程序例: </P>3 C9 g2 F. s6 n" O! q S& |
<P><FONT color=#0000ff>#include <STDIO.H>/ s! N/ E% k" w6 ^3 X; D
#include <PROCESS.H>
, V0 D8 p) l" g7 v4 H; H#include <SETJMP.H></FONT></P>
' s; C4 j# A) V. B( V<P><FONT color=#0000ff>void subroutine(void); </FONT></P>0 m3 t3 N5 ]7 ?8 {- F# V
<P><FONT color=#0000ff>jmp_buf jumper; </FONT></P>
2 e e6 s9 U( _; c. u6 X<P><FONT color=#0000ff>int main(void)
1 `& X8 Z0 Z4 y" k{ & U! Q- H- O2 J0 ]( R
int value; </FONT></P>
/ b2 Z8 ^. B5 l<P><FONT color=#0000ff>value = setjmp(jumper); + q8 p8 L; C% L
if (value != 0) 0 Q! \4 {5 {# l. N- J( {
{
* ]0 v" m! H- I8 y0 Cprintf("Longjmp with value %d\n", value); 5 _. f, n3 n! y2 l1 U
exit(value);
8 U) Z! _ d* F/ ~: b" i4 K}
/ y" m; u! f) ^5 \7 {# Nprintf("About to call subroutine ... \n");
, Y; r0 i! q" }' L! ^) W7 i `subroutine();
- \4 B: T. h& E; Jreturn 0;
) W/ p+ @) l1 }} </FONT></P>
& P4 |# a0 {* R! M' Y- g% G; ]<P><FONT color=#0000ff>void subroutine(void)
$ _9 l: z1 L: U% U7 q; |{ 3 ?: g4 |9 G* _3 e4 d, S
longjmp(jumper,1); 1 N( t6 o. @3 T( X
}
! k8 v! H/ ?4 w; R! ^/ M& d</FONT>
' D- t5 X: W% Q+ ?' m! C</P>
) _* _' ]1 ^* w9 E( ?! I<P><FONT color=#ff0000>函数名: setlinestyle </FONT>
, J# Q: k( c$ g+ |7 U- m% v) p功 能: 设置当前画线宽度和类型
" ~ ]2 Q$ L# z& @用 法: void far setlinestyle(int linestype, unsigned upattern);
: _& ^: T( V$ U" {, c" p' R5 R* X程序例: </P>
' b% o3 U1 _ R+ N) R* W* ]4 u<P><FONT color=#0000ff>#include <GRAPHICS.H>2 W$ ^! _6 u0 |; }/ l$ e
#include <STDLIB.H>
% K( T1 Y1 E* I* t#include <STRING.H>
, j" A5 \: ?( Q* U#include <STDIO.H>
* q3 r, g7 N- @8 l, j" K#include <CONIO.H></FONT></P>
9 z R! e8 l5 R) I) `( ~<P><FONT color=#0000ff>/* the names of the line styles supported */ ; A2 j/ | O$ q7 L3 y
char *lname[] = {
7 i" a# m2 l" i& m, F"SOLID_LINE", ! }" N/ w0 ], d3 K" n* O
"DOTTED_LINE",
& B0 e- s' |4 K& T, D"CENTER_LINE", & q% U8 t) Y$ i9 T2 C- F* h
"DASHED_LINE",
8 Z7 S6 i2 _" X3 j' ~6 Z8 i"USERBIT_LINE"
% [) J5 X9 b0 H) J; w; w}; </FONT></P>
$ D/ O) _4 d+ T2 D3 H<P><FONT color=#0000ff>int main(void)
, s) [' g" T: U9 A9 l{ , y! T% n! \& F
/* request auto detection */
$ [: m9 M# v, B3 {int gdriver = DETECT, gmode, errorcode; </FONT></P>
: ^+ D9 v7 ?: [+ S. M( X<P><FONT color=#0000ff>int style, midx, midy, userpat;
9 ]7 t' M/ f) C* ?* `) Nchar stylestr[40]; </FONT></P>
& D, D* H7 }5 I' `2 m: F/ {: M' F<P><FONT color=#0000ff>/* initialize graphics and local variables */
4 P7 X( z6 P$ Z. D, C6 H3 u Jinitgraph(&gdriver, &gmode, ""); </FONT></P>
9 h, z$ h( V( I$ ^' v2 H; D<P><FONT color=#0000ff>/* read result of initialization */
. S/ v" W7 k/ A ]7 Q9 Rerrorcode = graphresult();
& p1 K t9 R0 ?# v: g; d4 k0 Mif (errorcode != grOk) /* an error occurred */ * G0 G, h) X) {7 v6 c9 O' Z
{ 5 K8 x- M% d( S4 R9 A2 }
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 y5 N- v$ @( d7 B
printf("Press any key to halt:");
/ M* w+ P a8 ^3 ggetch(); 3 _7 C/ S5 m7 c- }4 v" H1 B- ^
exit(1); /* terminate with an error code */
2 d7 w% S, y! Y2 V. A} </FONT></P>7 K2 N( t2 L$ D, i4 }
<P><FONT color=#0000ff>midx = getmaxx() / 2;
8 g3 N9 v9 }/ K5 ]; Emidy = getmaxy() / 2; </FONT></P>
& D# b* G# _3 |0 j<P><FONT color=#0000ff>/* a user defined line pattern */
! i- M o B, s. V* |: e/* binary: "0000000000000001" */ 6 Q; I- d( R: B) E% [% ~
userpat = 1; </FONT></P>: c. v$ V j3 X! i$ A
<P><FONT color=#0000ff>for (style=SOLID_LINE; style<=USERBIT_LINE; style++) & h! W0 i6 g: W g
{
2 J/ O& P8 E& @% ^/* select the line style */ ~8 D8 _6 E8 f! t, s
setlinestyle(style, userpat, 1); </FONT></P>; v- J3 l! a# R+ {, c0 V
<P><FONT color=#0000ff>/* convert style into a string */ 2 t( y5 O( Y% w# e' l- Y
strcpy(stylestr, lname[style]); </FONT></P>$ _- P% Z$ u; i% c9 k$ h7 O
<P><FONT color=#0000ff>/* draw a line */ 7 Y: t6 I; X, f7 |: Z; ^" `; D
line(0, 0, midx-10, midy); </FONT></P>
! E k3 i. m3 \- Z9 I. B<P><FONT color=#0000ff>/* draw a rectangle */ 8 K6 N2 y* _/ O+ h# `# Y, |
rectangle(0, 0, getmaxx(), getmaxy()); </FONT></P>) A9 q3 j2 T+ [( p8 f% j
<P><FONT color=#0000ff>/* output a message */
, n. y5 V! k( ?2 C. vouttextxy(midx, midy, stylestr); </FONT></P>
: a- p) G4 v' |+ P<P><FONT color=#0000ff>/* wait for a key */
* I; x% I# s0 s0 \- i, dgetch();
& ] T l4 k) B$ ~5 Vcleardevice(); # X! Q X$ ?) j% p
} </FONT></P>8 z! M# ~+ m! N( s% W$ [
<P><FONT color=#0000ff>/* clean up */
( n; v7 g+ D! z' Z3 V7 C- d0 G; ?closegraph();
. S( L; s2 a; F l5 A; Greturn 0;
8 ^* A4 k5 ?3 P/ i* L" b} </FONT>2 P$ }6 U. A) q, y0 E* i
; O* c! P# x P* r' w" `0 L1 D3 D# ^( l
</P>& S6 i) M/ |$ X R6 K8 _" L _
<P><FONT color=#ff0000>函数名: setmem </FONT>
5 `% Y& s& ]9 W- r' S- j* `0 e功 能: 存值到存储区
1 i' ? |6 E2 s1 M" E用 法: void setmem(void *addr, int len, char value); 9 f$ u5 g6 r! j, F
程序例: </P>
% f) K$ E. N% i( \1 [- y2 W<P><FONT color=#0000ff>#include <STDIO.H>
% I- V, r5 {, `# \& x- o#include <ALLOC.H>2 K0 S/ \- T% f# q$ V# [+ }
#include <MEM.H></FONT></P>
" a% U" o) X# q/ [5 D<P><FONT color=#0000ff>int main(void)
, f9 T" X9 X/ g9 C* w- o2 k+ e{
' J- R" T4 X& N9 a/ Jchar *dest; </FONT></P>8 Y# N2 f/ j. ]( A; E
<P><FONT color=#0000ff>dest = calloc(21, sizeof(char));
# \+ W7 F* j" B! F, jsetmem(dest, 20, 'c');
6 N5 P% W9 I4 ]0 N$ L6 V* ^printf("%s\n", dest); </FONT></P>' c2 ~6 v: L3 u
<P><FONT color=#0000ff>return 0;
( A: l7 T8 ^3 o/ R1 C} </FONT>" w7 l. r, ], Y
2 F! p) O6 j( Q R- e8 x$ n4 {& I
- h9 j- v4 i' _4 |; q, E; ]% k
</P>
' A( C R8 X C# X+ L& ]% c<P><FONT color=#ff0000>函数名: setmode </FONT>0 V/ }- P' L& S' D
功 能: 设置打开文件方式 2 E+ T, |, d; G6 ~; E; f
用 法: int setmode(int handle, unsigned mode); * Q5 G7 |0 N' p
程序例: </P>( n. E* _4 z* N f1 K( n
<P><FONT color=#0000ff>#include <STDIO.H>
3 s% p( g- f( h% o5 d#include <FCNTL.H>" O/ W H- y- g# V3 O, ~
#include <IO.H></FONT></P>9 r/ l3 D5 @4 a7 C0 {5 Q+ j
<P><FONT color=#0000ff>int main(void)
2 k/ v/ a0 j& |% a, f2 G3 G" m{
( Z- |1 S1 [9 \, ]$ e( D& Lint result; </FONT></P>
A1 N" l7 C, z<P><FONT color=#0000ff>result = setmode(fileno(stdprn), O_TEXT);
) B0 @9 Z1 r T1 d7 l _if (result == -1) - u6 B% |. V9 K8 G5 U/ j
perror("Mode not available\n");
7 q, ?# E3 p0 ?" O) W8 v/ r. Eelse
. b$ a& H: T+ hprintf("Mode successfully switched\n");
4 j1 z$ T9 s6 O8 ~- e" Xreturn 0;
; `+ B+ C! B8 P6 f. H. }7 j} 0 F" J6 h7 Y/ ~, b4 \$ N2 q2 X ~
! q6 ~, `" X! W, T, g, o
</FONT># M4 u c4 j V/ i* B+ n6 T* q
</P>
& y; |) [5 p6 c<P><FONT color=#ff0000>函数名: setpalette </FONT>% S- O8 i1 Y) O
功 能: 改变调色板的颜色 " Z& S6 G( m+ p. B. Z) |. z
用 法: void far setpalette(int index, int actural_color);
+ ^* a8 [9 O5 W3 E/ m5 @4 C程序例: </P>
& k# C* [/ a$ J+ h8 @$ i" ^<P><FONT color=#0000ff>#include <GRAPHICS.H>, E" t' ?, O& X! q
#include <STDLIB.H>2 j7 Z2 W5 t& Q; m8 z
#include <STDIO.H>
/ m( Q$ B, p' C2 N' N3 h- x' f* |8 m#include <CONIO.H></FONT></P>$ r$ r3 l; X8 e: U2 W0 r
<P><FONT color=#0000ff>int main(void) ; y! O u: [* R3 J
{ ) t; }( A' f6 v' d2 {1 A( F' i7 m
/* request auto detection */
1 h+ w6 S- }/ }int gdriver = DETECT, gmode, errorcode; 2 F' E2 P t! d2 z3 W' M7 [/ D* C
int color, maxcolor, ht; 1 c+ u( @, h5 D8 p. K' D) [# h
int y = 10; : O3 J% r5 b5 H, a
char msg[80]; </FONT></P>
3 e# C" v0 e8 {. S, U9 p5 N7 l5 u<P><FONT color=#0000ff>/* initialize graphics and local variables */ & ^" Y( r( B" V) R7 W/ N2 L' f
initgraph(&gdriver, &gmode, ""); </FONT></P>5 o* r5 q8 L# S' K B$ k
<P><FONT color=#0000ff>/* read result of initialization */ ( ]8 x% x) c: G* \
errorcode = graphresult();
2 w7 a$ D# f" r6 y' {: E% Dif (errorcode != grOk) /* an error occurred */ $ m. B, ?6 _$ a( }& ]: [
{ 8 t- W6 N% h. Z6 z$ L, F$ L
printf("Graphics error: %s\n", grapherrormsg(errorcode));
4 ?- C3 v. N( G7 M" B2 F- sprintf("Press any key to halt:"); 5 t: g1 T% w) X" z
getch(); 0 W1 U" ^; a: n1 V% }: D) ^
exit(1); /* terminate with an error code */
4 ~- S, B# X& ?* }. ]} </FONT></P>4 _. ^& I- e9 K( v0 k7 o! _
<P><FONT color=#0000ff>maxcolor = getmaxcolor();
1 h9 a# R& V1 c& A( h* }2 vht = 2 * textheight("W"); </FONT></P>
$ J$ @: g3 Z+ R) G1 [# J/ l<P><FONT color=#0000ff>/* display the default colors */ , L* W" W0 n7 l* x
for (color=1; color<=maxcolor; color++)
5 t6 z0 v, F& z0 u# E, r' g{
8 i3 @7 t" H6 m' p Isetcolor(color); . @ k. j6 [" ?0 X
sprintf(msg, "Color: %d", color); / H' Y* E7 M1 `" l: p: o
outtextxy(1, y, msg); 9 ~# y0 X8 \2 a' L/ y2 k( g
y += ht; & H" b' H. Q" [1 ^) C! s X" A
} </FONT></P>
, M' k/ O, L: K1 F5 o' p: A<P><FONT color=#0000ff>/* wait for a key */
2 ^7 Q& W- O7 ?- f8 Rgetch(); </FONT></P>
+ a+ N1 {( z7 {7 e. U2 `& O<P><FONT color=#0000ff>/* black out the colors one by one */
- t0 j- C4 C efor (color=1; color<=maxcolor; color++)
. u: A# x \. t& B- n. X{
' x$ [: r7 |; d: _/ asetpalette(color, BLACK); ' c2 v, {8 P1 ~& q0 d
getch(); + P( n! g, ~. z6 C
} </FONT></P>
2 M% x, A! Z7 F, i$ A<P><FONT color=#0000ff>/* clean up */ . f8 Z3 I ?! O, I( Y* \" t% ]! N8 O8 N0 y
closegraph();
3 T# |" J- |- L; j6 @1 ?return 0; ^5 H7 v2 M1 ]6 k+ }8 J1 P, _5 a
}
; G; o5 ?2 e( n" D+ v4 H" v</FONT>
" I% L' y, a, k8 e1 ^2 o( D</P>& A8 @( x, O2 |8 u
<P><FONT color=#ff0000>函数名: setrgbpalette </FONT>
& E- ^: u( o' Z9 X' U/ t功 能: 定义IBM8514图形卡的颜色 - [+ j+ ]" j& r9 |) x6 _
用 法: void far setrgbpalette(int colornum, int red, int green, int blue);
4 n# U- y. @# e6 x/ g p! ]1 }* f程序例: </P>4 @3 ~3 U3 P7 v) ~3 y4 w! ^3 u. P
<P><FONT color=#0000ff>#include <GRAPHICS.H>
1 \5 s- a+ F3 i% d, Q; E0 J#include <STDLIB.H>
, U( A3 }4 Z/ ]+ X( s$ U8 `0 A U#include <STDIO.H>/ P/ S& S @; ]; E
#include <CONIO.H></FONT></P>* N- ^$ V* x# f, e
<P><FONT color=#0000ff>int main(void)
% W2 W* X5 b/ o{ * I3 X, O5 z) o) s6 F Q8 k
/* select a driver and mode that supports the use */
- s: Q: Z/ c# v R( \8 ~/* of the setrgbpalette function. */ ' @ E4 c/ k$ a
int gdriver = VGA, gmode = VGAHI, errorcode; & W- a ]1 u f2 E% \* ]2 D
struct palettetype pal; ( l. X+ X- W5 P3 @
int i, ht, y, xmax; </FONT></P>. A8 Y3 ]0 f; h0 g3 I/ a
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ; l# P' G, M2 h7 D! G
initgraph(&gdriver, &gmode, ""); </FONT></P>! F: A- d' c" i D9 H% O8 e
<P><FONT color=#0000ff>/* read result of initialization */
4 }: N! Y3 b! P0 q/ Nerrorcode = graphresult();
$ ~" k1 g6 q" M) J4 B( a: Z9 ^# B4 `if (errorcode != grOk) /* an error occurred */ ' ~# U( ~8 v2 c8 |" N# M% k
{
8 N! }9 P. A7 S5 c2 y. ` D; dprintf("Graphics error: %s\n", grapherrormsg(errorcode));
6 S. I7 t- [+ v7 H# y" K, p* ?printf("Press any key to halt:"); 8 s& q$ K) u) b
getch();
# I5 x; | U) W$ `% e4 {exit(1); /* terminate with an error code */
$ } G: \! n# U+ U" I" P$ x& c9 S} </FONT></P>
5 f* g/ U$ [3 ?1 T8 x: i' x$ x; ], q<P><FONT color=#0000ff>/* grab a copy of the palette */ 9 y N7 w0 t2 P ~ I/ V2 B% x
getpalette(&pal); </FONT></P>
' a$ Z+ [1 }; x* X8 W<P><FONT color=#0000ff>/* create gray scale */ : k: E4 O+ p7 z4 q8 ]# {9 N
for (i=0; i<PAL.SIZE; <br i++)> setrgbpalette(pal.colors, i*4, i*4, i*4); </FONT></P>" w8 e9 S& a% b! Q0 v
<P><FONT color=#0000ff>/* display the gray scale */
1 k) h( N. M* s. pht = getmaxy() / 16; : [, ~% l7 }! |/ x+ C/ W
xmax = getmaxx(); ' D! f) B1 d4 @% \
y = 0;
% E8 l. }5 @) w: zfor (i=0; i<PAL.SIZE; <br i++)> { 5 l3 O S, [2 K: r- n% S* w- @
setfillstyle(SOLID_FILL, i); 7 u6 ` M/ {2 E# x$ b+ F; Q2 j$ I1 b. @
bar(0, y, xmax, y+ht); - l) j* \ m# Q% S$ G
y += ht;
7 V: Q. r! O+ h} </FONT></P>; t# t& I" c; X# _9 S
<P><FONT color=#0000ff>/* clean up */
( f1 n3 G( y. T L8 tgetch(); 9 [+ q* s4 o9 z( Q; u! a; \5 i' l/ G
closegraph();
$ l: F g" r0 [1 h9 dreturn 0;
, W" O% K( A( Y# h- z% u} + o2 g& q5 p' [6 I
</FONT>
$ o. P0 F+ Q! M' r( p$ i4 G2 l/ C5 C" J# {' v
</P>2 Z* b4 l9 ]% \
<P><FONT color=#ff0000>函数名: settextjustify </FONT>
* J$ k* k$ ]1 X% b功 能: 为图形函数设置文本的对齐方式 # x' W8 ~( @" ?0 p3 }- i% a
用 法: void far settextjustify(int horiz, int vert);
1 n1 M- e% ?( t" i程序例: </P>
) J: b" Z/ f* X6 g8 V+ l5 v<P><FONT color=#0000ff>#include <GRAPHICS.H>. Q& f! f% S$ J* q- L
#include <STDLIB.H>
& A) e. w2 j% x5 w0 P#include <STDIO.H>
' @2 h- C0 s4 f2 o1 z% i0 A#include <CONIO.H></FONT></P>
8 w8 C$ y# x2 V; }1 d: [/ v<P><FONT color=#0000ff>/* function prototype */ 6 U! y, V/ m# z. K" z
void xat(int x, int y); </FONT></P>2 n0 U8 U- C% I
<P><FONT color=#0000ff>/* horizontal text justification settings */ . v; W0 Y- O- x4 s k7 T3 w- ^/ k
char *hjust[] = { "LEFT_TEXT",
* d3 d/ b. H& b2 H0 x"CENTER_TEXT",
+ {; z2 H h8 p( s1 S"RIGHT_TEXT"
0 }. P! j" }, h) `1 w! O}; </FONT></P>
4 b" W9 t5 S# W6 a4 p' |<P><FONT color=#0000ff>/* vertical text justification settings */
! {; Q9 N; ]/ [2 r, e5 Gchar *vjust[] = { "LEFT_TEXT", 0 G% ?) }# S& }, g% Z
"CENTER_TEXT",
9 O# v% ~) A p9 e: L"RIGHT_TEXT" 5 r3 p8 `# l, P0 P5 T
}; </FONT></P>
M4 D* r5 H/ W! f7 e2 p<P><FONT color=#0000ff>int main(void)
4 J! q: q0 o6 M$ j' R- s2 y{ $ ?2 G0 u1 S+ @, c& n9 D* k0 d! ?
/* request auto detection */ . o0 B5 \- U& U+ ^% }
int gdriver = DETECT, gmode, errorcode; " n0 a. A& U% u+ V' z- {2 P
int midx, midy, hj, vj; ; U; ~$ C: e2 J5 w3 ~
char msg[80]; </FONT></P>
( a5 i( v5 {) \ G6 B<P><FONT color=#0000ff>/* initialize graphics and local variables */
( o O% ~7 a J/ U: winitgraph(&gdriver, &gmode, ""); </FONT></P>+ d, w# \! E. S i8 w9 k
<P><FONT color=#0000ff>/* read result of initialization */
: Q( [3 }0 w7 O, c. w1 eerrorcode = graphresult();
% P1 k; D. H! B5 V+ [* _6 D; Sif (errorcode != grOk) /* an error occurred */ P7 k( c# ~" {. w$ {. G1 i
{
8 z# f/ W' d# R: ~2 A) m: k+ mprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 w; l7 [* B4 @
printf("Press any key to halt:"); - s; D! r9 k+ z$ D* h
getch();
+ ^5 o% `! V. \exit(1); /* terminate with an error code */
8 x+ f: Y2 o* G6 n8 E. C1 i} </FONT></P>
2 G5 w3 t( r1 k& j8 x<P><FONT color=#0000ff>midx = getmaxx() / 2; 5 ?7 G; C" v4 d ~# C6 H' s
midy = getmaxy() / 2; </FONT></P> `: @1 H* q g: A* r
<P><FONT color=#0000ff>/* loop through text justifications */
^8 f# {3 [2 T, H: ]3 ^: }2 I! k: yfor (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++)
1 i' l( n5 @0 x! r9 yfor (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++)
6 k4 B! k* Y8 a d' c( W{
' B/ N* j) d. e) y E; N5 lcleardevice();
2 }( ?# G' z9 W. L& ]* u/* set the text justification */
8 s/ |3 \ i+ g( t* k6 Msettextjustify(hj, vj); </FONT></P>
" U/ Z0 b) h4 F9 O& |) c$ @) i: G) L<P><FONT color=#0000ff>/* create a message string */
V0 ~3 ^, L& E, s& ~8 s) K* R1 Asprintf(msg, "%s %s", hjust[hj], vjust[vj]); </FONT></P>, M }% K( k3 i+ l
<P><FONT color=#0000ff>/* create cross hairs on the screen */
) C G; d; N) g5 J6 t/ C( lxat(midx, midy); </FONT></P>
' }& }! e" D9 A: G. B2 ?% ?<P><FONT color=#0000ff>/* output the message */ # Z5 x l# m) s7 Y, K1 F4 e
outtextxy(midx, midy, msg);
- e, w8 p8 o4 b" ` A- egetch();
& g: G, _) @1 X$ c} </FONT></P>
* Z: ?, q9 s4 ?( J+ f<P><FONT color=#0000ff>/* clean up */ ; d% {, k8 Z0 O5 |, I
closegraph();
/ Z8 I* V D' F$ z- Y! \1 M% S! Treturn 0;
. F4 O" z- a M2 u- p} </FONT></P>
& u9 I& u7 U! e# \1 ]<P><FONT color=#0000ff>/* draw an "x" at (x, y) */
6 F/ A% w6 [: W: Cvoid xat(int x, int y)
4 u8 c1 T( X9 f( ^$ ~, P{ % a% f1 y- h' Z" h; b# T
line(x-4, y, x+4, y); " C+ F- C j0 |5 V) Z
line(x, y-4, x, y+4); ( T2 p3 Z* p5 M9 W
} </FONT>
2 s4 v9 @% p( H/ a& `
( y# ~% k7 f7 S* F+ s2 _5 o</P>
2 n, c, A6 e$ ~. b! n3 }" o- {" N<P><FONT color=#ff0000>函数名: settextstyle </FONT>' u% h. B& O; |) N9 |
功 能: 为图形输出设置当前的文本属性
( s, Y& V' Y; J' V用 法: void far settextstyle (int font, int direction, char size);
# B! B9 e! b6 z. W Z0 [; C* u2 P程序例: </P>
3 g" `* d$ ~6 b! v F- ?, M7 N<P><FONT color=#0000ff>#include <GRAPHICS.H>1 b$ ^+ T& Y, G: F) c; `/ a( f
#include <STDLIB.H>
5 }3 Z3 q7 S( {5 U8 I) v#include <STDIO.H>
& ]1 T$ m% A2 d" ]' l w: q" `#include <CONIO.H></FONT></P>& Q! X8 q' v$ ?/ q
<P><FONT color=#0000ff>/* the names of the text styles supported */
% Z6 B5 c3 f5 `) }char *fname[] = { "DEFAULT font", - o7 v# J9 [6 u6 t2 T0 h; z( G6 S3 x
"TRIPLEX font",
3 V, N" `5 c0 M: x"SMALL font", * H) \8 D, \0 j( c# M1 T
"SANS SERIF font", " s6 J8 l7 f7 E' L7 h
"GOTHIC font" 3 I; Z7 j; K" Q3 n! G( w& U/ S* s- B
}; </FONT></P>
- l0 R& }1 a0 ~9 B- H5 U( r<P><FONT color=#0000ff>int main(void) / v# @6 I& x; f3 K- U
{ 9 E1 `4 W# r) G, J1 @4 x0 m% E
/* request auto detection */
" j0 G4 Y) w. w8 Lint gdriver = DETECT, gmode, errorcode; 1 k1 o J7 Z7 {
int style, midx, midy; 7 g; E" B( ~; n
int size = 1; </FONT></P>
4 E9 |8 V' X& o8 `<P><FONT color=#0000ff>/* initialize graphics and local variables */ 2 n$ p$ u( X- ^. T: l5 }
initgraph(&gdriver, &gmode, ""); </FONT></P>
' r) H. x6 K# w7 W<P><FONT color=#0000ff>/* read result of initialization */
: \0 n& D* w* h j- k1 [6 }0 Serrorcode = graphresult(); 9 c" D! T% v5 k2 T: U# o+ w1 I
if (errorcode != grOk) /* an error occurred */
4 D0 L) E. J. K! P- o/ o{
$ x4 V, n2 z7 Q9 m. Y9 X. X1 h' ]! _printf("Graphics error: %s\n", grapherrormsg(errorcode)); $ a7 {6 e1 f" A- B
printf("Press any key to halt:");
9 i: u/ B- Q4 W4 x: a2 k" dgetch();
8 X! v) _/ n8 T8 j/ |1 x7 Mexit(1); /* terminate with an error code */
3 p2 M+ s4 h3 u7 Z} </FONT></P>' e% N6 e: u4 F
<P><FONT color=#0000ff>midx = getmaxx() / 2; w) K9 I' F# s6 U; n* T% V0 S
midy = getmaxy() / 2; </FONT></P>6 M6 [9 h6 m6 U M/ C
<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>% {" b, {4 Y5 k1 R2 |
<P><FONT color=#0000ff>/* loop through the available text styles */ ) z( ~1 j4 k1 |* p9 ?$ ]7 R
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) $ T& ~- o( t3 b6 z' k
{ - ?" ?/ j( |5 t2 \
cleardevice();
' l# _) w: B: \8 S5 @2 G Xif (style == TRIPLEX_FONT) , W' j9 v- _' b1 `3 b
size = 4; </FONT></P>
" ?( a0 u+ {! |0 j ?/ w<P><FONT color=#0000ff>/* select the text style */ I4 Z9 S, h. |6 ^" S/ Z1 [) {, Q
settextstyle(style, HORIZ_DIR, size); </FONT></P>
6 K( o* V/ L* Q& ^2 A3 F9 I! E! L<P><FONT color=#0000ff>/* output a message */ " Y7 n" ~; h) J+ w/ w: J2 |
outtextxy(midx, midy, fname[style]);
; c: T& L, o) m+ {getch();
; ?/ o3 ]1 C+ G0 N, v ]( F' j} </FONT></P>
6 w0 b8 p; r" g4 V( a<P><FONT color=#0000ff>/* clean up */ 6 _7 a) T! ^5 h1 k
closegraph(); , F$ f* C# L Y
return 0; 0 v, V; J' ]! J' {, t
}
6 s8 r/ |% Y8 r# n</FONT>+ g, U, w o" ~5 h+ H. G% e2 y
</P>, R8 Z _0 a2 [: {. Q
<P><FONT color=#ff0000>函数名: settextstyle </FONT>
! Q. l; y! U) s) u0 N# G功 能: 为图形输出设置当前的文本属性
( h5 T) l1 w0 R! x用 法: void far settextstyle (int font, int direction, char size); 2 j: S# j4 O% A. x& A* Z7 i' w+ X" C
程序例: </P>
1 U Y0 r% s. J- ^<P><FONT color=#0000ff>#include <GRAPHICS.H>
1 N3 J6 i8 x" d! W( O#include <STDLIB.H>
+ f' \& g9 b) u% a3 G& O0 R#include <STDIO.H>
1 j! `7 P! ?3 S! ~#include <CONIO.H></FONT></P>
* Q# S. z" f# g: u# x1 n<P><FONT color=#0000ff>/* the names of the text styles supported */
6 ~2 s! `6 c" G2 \' F4 s$ K0 uchar *fname[] = { "DEFAULT font", 3 w- ^0 `6 s( C- }5 W. A4 K
"TRIPLEX font", ) x u8 Z: C7 V; G; w
"SMALL font", 1 y* K& S$ m9 B8 I
"SANS SERIF font",
! f$ }. }+ P- M& @"GOTHIC font" 8 t# {4 |. \. a
}; </FONT></P>' D' R8 u) K, b/ @
<P><FONT color=#0000ff>int main(void) 5 y; i+ S8 M: C7 q; J: Q( x Z% @! X
{ ; n, H& d; a3 K# b; _
/* request auto detection */ 3 C0 I8 w5 G, v' Z" t4 t
int gdriver = DETECT, gmode, errorcode;
7 x" e+ u- i* [( I9 Oint style, midx, midy;
4 E) X" m- ~/ b' y" ^7 ~) |int size = 1; </FONT></P>
& {5 ^! l# g0 n) n( E! c<P><FONT color=#0000ff>/* initialize graphics and local variables */ 5 U$ I+ n- `5 `6 q" e3 H
initgraph(&gdriver, &gmode, ""); </FONT></P>
! T, Z* O- |7 A- Q7 S<P><FONT color=#0000ff>/* read result of initialization */ " B2 _- l i; \
errorcode = graphresult(); / r s! Q( X) k. \9 b( c& ^5 l
if (errorcode != grOk) /* an error occurred */
! y+ [3 v+ v+ G9 `1 K{ . y5 _7 ]% F/ m, X B
printf("Graphics error: %s\n", grapherrormsg(errorcode));
8 g2 Z4 `: U$ o/ k$ r# O* J+ ^* S" fprintf("Press any key to halt:");
7 {& h8 R# p/ T/ ^$ }( J: Igetch();
2 P% v4 r1 _6 K3 \7 f9 Kexit(1); /* terminate with an error code */ . P1 S% S, J, Y& G/ v
} </FONT></P>" N+ S' v' @8 q q
<P><FONT color=#0000ff>midx = getmaxx() / 2; 9 u0 \7 b( f) R, u: R) k& K
midy = getmaxy() / 2; </FONT></P>
* ^" Y# ?- Y. |8 O<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>, h g% ]$ y% p: ^5 F% ^
<P><FONT color=#0000ff>/* loop through the available text styles */ % m4 s& f/ E% L) K* K' M6 p
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) * m# t5 ^0 M( a$ w, |
{
! k7 S. s8 d& t4 P- i" Pcleardevice();
4 j1 k, q+ f6 Yif (style == TRIPLEX_FONT) * Y' H( G1 U) H. G7 e
size = 4; </FONT></P>
" g2 x: l c: q; M, M" C5 n& ~<P><FONT color=#0000ff>/* select the text style */
( }# h8 M& M; h" P. N- Hsettextstyle(style, HORIZ_DIR, size); </FONT></P>
! J- a4 z* g" d) ]7 V9 u* \! N<P><FONT color=#0000ff>/* output a message */ - }- w" u- L A0 V
outtextxy(midx, midy, fname[style]); ) D! c' O! q8 \2 L
getch(); 6 U$ J7 F6 H/ a2 b
} </FONT></P>/ @$ ]+ P1 \$ W/ B! c0 A
<P><FONT color=#0000ff>/* clean up */ , M9 F+ r: D7 g3 L
closegraph(); ; x" F) K9 l" ?% b' r7 b1 E; [
return 0; % N0 x0 F' I& f# W( N2 n
} </FONT>
" Z+ X1 R' e+ N6 ^- k0 r* |+ f
</P>. D( T9 u& h: |$ a+ W7 Q
<P><FONT color=#ff0000>函数名: settime </FONT>: ]4 p& b2 M0 c* H: o3 ?* w
功 能: 设置系统时间 0 N; h7 q, U0 I0 z' X6 i
用 法: void settime(struct time *timep); 0 r4 C0 H4 U9 }
程序例: </P>
4 s# d& }5 k/ T( A3 [ [1 o$ T4 L<P><FONT color=#0000ff>#include <STDIO.H>, I @8 Q1 [: W9 ]0 ~& P7 I
#include <DOS.H></FONT></P>
. r, Z; C* L5 i" n; R<P><FONT color=#0000ff>int main(void) 5 u2 A8 ]* _$ N
{ 9 S' h& e7 [) O" U
struct time t; </FONT></P>
c& C! }% o' c' V% g! o, V* B<P><FONT color=#0000ff>gettime(&t); $ ]0 j1 F( p# q
printf("The current minute is: %d\n", t.ti_min); ) }, y$ V, a1 W8 @% q$ l
printf("The current hour is: %d\n", t.ti_hour); . ?8 U: [( ]# v) [( u9 t j
printf("The current hundredth of a second is: %d\n", t.ti_hund);
, }) ]& `# N* mprintf("The current second is: %d\n", t.ti_sec); </FONT></P>2 c; T# Y: E# d/ t. N
<P><FONT color=#0000ff>/* Add one to the minutes struct element and then call settime */ ; W: p/ {, Y: w6 z( k
t.ti_min++;
2 H; Y, v1 I/ v2 Q3 Y# ysettime(&t); </FONT></P>
, l1 s: [, r. L: f$ x<P><FONT color=#0000ff>return 0;
$ S" |3 w8 r- f( y5 T3 c9 E. V} </FONT>5 p0 O" A; c: d: C4 Q* P } x
% T( I7 F( S8 b+ K</P>
$ E) p9 ~/ u" X4 A<P><FONT color=#ff0000>函数名: setusercharsize </FONT>
8 x; T, C3 t. ^7 ]0 I功 能: 为矢量字体改变字符宽度和高度 9 u# w3 g# V, R' }* K" e1 O% ^
用 法: void far setusercharsize(int multx, int dirx, int multy, int diry); ) ^9 u) `; ?% |% d8 A# W
程序例: </P>' G' ^% ^9 k' | S1 G+ j" G( P
<P><FONT color=#0000ff>#include <GRAPHICS.H>
1 f1 Q! P1 G3 R4 R- n$ I& A1 X2 M#include <STDLIB.H>
7 P# V2 F- a* ^#include <STDIO.H>
# m: [1 d8 _7 z% X1 N# o#include <CONIO.H></FONT></P>! }% J* ~2 W: Q" E- N8 a+ _! w: A
<P><FONT color=#0000ff>int main(void) ' \, d/ J/ K7 R) e" r2 D# M. \+ K# s) v$ L% Y
{
' K6 f/ F8 Y; k# M# }/ ]3 w/* request autodetection */ " x }1 z: ^! T( n3 o* Z# f
int gdriver = DETECT, gmode, errorcode; </FONT></P> p3 e( s" X* d3 y) T* _; G
<P><FONT color=#0000ff>/* initialize graphics and local variables */
! C' l! P$ j! l [# minitgraph(&gdriver, &gmode, ""); </FONT></P>
0 z6 M8 |3 z" g* o8 I! |% J* [<P><FONT color=#0000ff>/* read result of initialization */
+ ~" O/ s* p N" S$ Qerrorcode = graphresult();
1 u! J' c" U# W E6 b% r: y) K5 [4 Qif (errorcode != grOk) /* an error occurred */
9 t! u! T# z# `! K- P3 d7 ~{
; q0 ]# O2 T+ M8 G& Y+ dprintf("Graphics error: %s\n", grapherrormsg(errorcode)); * f' C/ P2 x4 r2 L$ b
printf("Press any key to halt:"); # {1 O, e* A8 J
getch(); & k% ~ f" j4 n* }4 N
exit(1); /* terminate with an error code */
" F& i8 w. m( j* \& ~} </FONT></P>- T1 k S$ H" M1 | M
<P><FONT color=#0000ff>/* select a text style */ & _* I: {0 \ G0 R! ^! l
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); </FONT></P> d( }' b8 ]5 u3 N4 k X, |
<P><FONT color=#0000ff>/* move to the text starting position */
, O0 u$ ~( P! \& |6 h. cmoveto(0, getmaxy() / 2); </FONT></P>9 U% f+ K% A& q2 @' D: w: a6 j. s% {* F, r
<P><FONT color=#0000ff>/* output some normal text */ ) F. f) [9 ~. e
outtext("Norm "); </FONT></P>
3 X( y. G$ X/ h1 Y7 Q% S- B<P><FONT color=#0000ff>/* make the text 1/3 the normal width */ 7 w' |8 f9 _5 g V8 P
setusercharsize(1, 3, 1, 1); ' v: n1 D4 @+ ]. }
outtext("Short "); </FONT></P>
9 J! V% [% z7 D5 L+ p) s<P><FONT color=#0000ff>/* make the text 3 times normal width */ * Q! ]& L$ G# f, I0 M7 p
setusercharsize(3, 1, 1, 1);
) p" C; k! g: p6 T: Bouttext("Wide"); </FONT></P># O3 \: ~. ?8 v6 E
<P><FONT color=#0000ff>/* clean up */ ! _% [2 M& R7 s$ X
getch();
; l* n% N' s( [) x+ Z. Q8 [0 [closegraph(); - P5 Y# t( P9 p) s! Q
return 0; # M. h, m" c6 T) P D" S
} </FONT>
$ {( q- [+ j" V2 |. t9 B9 u' e</P>0 P9 M# U% m; x& c
<P><FONT color=#ff0000>函数名: setvbuf </FONT>) r1 A2 a* F$ f. y% D
功 能: 把缓冲区与流相关
: A4 D' F, `7 e9 t+ w/ y5 s! q. r! m用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
1 I5 G. |2 l( u( U程序例: </P>
- S& J/ f# J, u$ D' z<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>4 D4 u0 Z1 r6 c) ~8 f4 u! Y
<P><FONT color=#0000ff>int main(void) 2 Z& G8 w1 `, i* v; A' l, S
{ 8 e, O4 _1 ~# v3 I
FILE *input, *output; 8 I: R: N: z+ w5 a
char bufr[512]; </FONT></P>" n; l# D" e v9 B: u- Z
<P><FONT color=#0000ff>input = fopen("file.in", "r+b");
2 \$ p/ Z9 j* k/ Goutput = fopen("file.out", "w"); </FONT></P>) P: y4 l' S& F/ y1 K
<P><FONT color=#0000ff>/* set up input stream for minimal disk access,
' k/ O: @* i1 p2 f! Cusing our own character buffer */ * R Z- w; {: y( m: _/ u; N
if (setvbuf(input, bufr, _IOFBF, 512) != 0) 2 K' o6 F0 M% @4 | h( ?" P
printf("failed to set up buffer for input file\n");
+ `" z( d, \/ T7 Velse
7 y, v ]+ E: P/ e( lprintf("buffer set up for input file\n"); </FONT></P>
, t. g- k: u$ S g- n5 Z: L<P><FONT color=#0000ff>/* set up output stream for line buffering using space that 6 M3 T U2 W( l2 X
will be obtained through an indirect call to malloc */ # k2 l" y+ W: p0 E8 d
if (setvbuf(output, NULL, _IOLBF, 132) != 0)
/ q+ Q4 _# l9 x' rprintf("failed to set up buffer for output file\n"); % L' Q, a2 ]" d7 [ B2 J& ?
else
( w) O" \% N% e1 k' _6 tprintf("buffer set up for output file\n"); </FONT></P>
" ], C8 V8 `- r; C: f' O" u<P><FONT color=#0000ff>/* perform file I/O here */ </FONT></P>& Y |6 ^7 v9 J+ R% l) _4 M- L# R/ w! f
<P><FONT color=#0000ff>/* close files */ 4 e. V4 ~' g* b
fclose(input);
! ^ v1 I/ @) K0 q& Jfclose(output);
. V# o1 m- O5 N; @9 Jreturn 0; 5 q6 s, o1 ]3 r q" i- w% _- E4 S
}
E0 v {/ @- L( W+ r/ l</FONT>. S& {4 t( |+ n* U8 x
8 T' g) r/ j" `</P>! _6 E9 G9 G! H. V" Z; O
<P><FONT color=#ff0000>函数名: setvect </FONT>2 K& r8 A' I# U7 H8 W3 u
功 能: 设置中断矢量入口 - k# w* m g- }8 e+ J
用 法: void setvect(int intr_num, void interrupt(*isr)()); % |3 t/ F9 B* a4 ]4 A. u8 ~
程序例: </P>
* j- }3 @0 g2 \- J( j, [<P><FONT color=#0000ff>/***NOTE: 8 v R. u; i; _( D& k& _
This is an interrupt service routine. You can NOT compile this ! F+ C" |7 [0 W" R* z. u
program with Test Stack Overflow turned on and get an executable
8 o5 M# C1 ]1 k" Y' b" J& Xfile which will operate correctly. */ </FONT></P>; x1 e8 Q9 q: } k+ A/ l
<P><FONT color=#0000ff>#include <STDIO.H>
' R$ D0 N) `( R9 E/ b; u3 @#include <DOS.H>
8 f* K9 I+ \* F9 S) X#include <CONIO.H></FONT></P>- g1 y9 x1 X4 M# [1 R8 y ]( t! I
<P><FONT color=#0000ff>#define INTR 0X1C /* The clock tick interrupt */ </FONT></P>
2 j y7 i" C0 H" {# @# E0 b<P><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>
5 G$ h+ A) [( O# @0 i0 B<P><FONT color=#0000ff>int count=0; </FONT></P>. h7 z. Q' B6 h' f5 ^. L
<P><FONT color=#0000ff>void interrupt handler(void) 7 r) ^+ m, k( b% z5 c5 O( X: U, u7 H
{
) K5 \, T$ _) M) \/* increase the global counter */
/ Z1 _( x2 e4 s' b2 T" u, h7 rcount++; </FONT></P>* U9 L: h6 V; ]* T
<P><FONT color=#0000ff>/* call the old routine */ : H! L6 c/ w8 {4 U
oldhandler(); 2 ` m# V6 {& G$ s. s1 m& m* D
} </FONT></P>: A6 Z1 F: K+ v# f9 t! P
<P><FONT color=#0000ff>int main(void) 5 M8 U. l& r1 v- k; W
{ ( w2 x" u; v7 I. G
/* save the old interrupt vector */
+ b) D0 {; A7 E- D+ b1 f; boldhandler = getvect(INTR); </FONT></P>' c: W! d' T8 B$ Z1 j/ S
<P><FONT color=#0000ff>/* install the new interrupt handler */ & [5 U; `3 K$ u5 S+ z7 g# U$ g
setvect(INTR, handler); </FONT></P>) y) O1 \+ n2 {" L1 F
<P><FONT color=#0000ff>/* loop until the counter exceeds 20 */ 1 F) m. J' |5 E0 Y" g8 M! f: G* r) S
while (count < 20)
8 j* Q6 u% _* R$ h; L# {- P/ gprintf("count is %d\n",count); </FONT></P>5 ~5 C4 d: a9 k- d; o4 W
<P><FONT color=#0000ff>/* reset the old interrupt handler */ 5 r* D* S% V9 ~5 c) t6 Y+ t' d
setvect(INTR, oldhandler); </FONT></P>) a5 F! f) v0 K% }/ D1 r; K7 l) u
<P><FONT color=#0000ff>return 0;
9 k5 V/ q1 x& p& z6 C N} 1 V0 H! k- n3 }
</FONT>; O' J' x# n$ k5 P/ q7 [$ G& z
</P>
* a; ~- Z9 M) ^5 X0 D$ Z" D<P><FONT color=#ff0000>函数名: setverify </FONT>7 }: {- g. G- g( k O2 N
功 能: 设置验证状态 # N" z0 |0 c4 G. ]0 S1 d& F# I
用 法: void setverify(int value);
5 I7 W9 p, J( _5 N/ A4 q程序例: </P>
0 l7 m: j! P% o<P><FONT color=#0000ff>#include <STDIO.H> z# `1 ]/ |6 l9 d* B0 y
#include <CONIO.H>
5 l8 | g: q% I+ E: Z/ d& K#include <DOS.H></FONT></P>. k" |0 m0 x& E; H& b+ e
<P><FONT color=#0000ff>int main(void)
' [8 V( o. e' T1 [5 W{ . M7 T8 X1 `/ ?8 \
int verify_flag; </FONT></P>
2 j( L1 R$ ]7 B<P><FONT color=#0000ff>printf("Enter 0 to set verify flag off\n"); 4 m& P2 j# y* I5 f4 X! l$ n
printf("Enter 1 to set verify flag on\n"); </FONT></P>; l1 D9 a0 i5 O# X
<P><FONT color=#0000ff>verify_flag = getch() - 0; </FONT></P>/ f( ]6 m# W# O3 J4 ^/ P6 l. k# }
<P><FONT color=#0000ff>setverify(verify_flag); </FONT></P>! ?, B* t$ y$ C+ J" F( N" s8 p3 Q
<P><FONT color=#0000ff>if (getverify()) " x$ w9 ^* Q I* e0 r
printf("DOS verify flag is on\n");
; C) K m' a1 }3 w9 M+ `3 |5 \& u( Delse 0 a: F) A: e7 C' h. Y; ]- X
printf("DOS verify flag is off\n"); </FONT></P>
, d& @$ Q+ ~" C<P><FONT color=#0000ff>return 0; : C" v8 G3 s' _3 s6 Z8 r
}
' l N+ u; F6 X! j<FONT color=#ff0000>
4 b. n- r- J, {</FONT></FONT></P>. }/ ^# S) |3 g& a" W* M& j
<P><FONT color=#ff0000>函数名: setviewport </FONT>
" V4 A4 _, I+ R, p3 g3 M功 能: 为图形输出设置当前视口 # @$ P6 t( v5 e# Z; T
用 法: void far setviewport(int left, int top, int right,
5 J7 g5 M9 B4 E. \& l7 Aint bottom, int clipflag); 7 |& j7 [5 O# a' q2 W
程序例: </P>0 \: Y" Q2 D" U0 s' b
<P><FONT color=#0000ff>#include <GRAPHICS.H>
8 W- z1 o$ O/ {* |. ~" N. {# h' K#include <STDLIB.H>3 l R( ~% B/ Q
#include <STDIO.H>- B2 c1 T! L3 K9 {! {' d; A9 w
#include <CONIO.H></FONT></P>+ v( P( _8 N. [3 M' S
<P><FONT color=#0000ff>#define CLIP_ON 1 /* activates clipping in viewport */ </FONT></P>, `6 y5 Z* [. p! D# K+ V' C4 w
<P><FONT color=#0000ff>int main(void) ; N: e# C) O8 ?9 b
{
/ h" c+ x$ c: S) T7 [/* request auto detection */
; b7 h* O: H4 C; x0 Z8 Hint gdriver = DETECT, gmode, errorcode; </FONT></P>! r+ e' x s% F
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ! i7 z3 _5 m9 L. v: Q: E
initgraph(&gdriver, &gmode, ""); </FONT></P>
* r. O! T; r9 k- v. m$ M0 G- t8 d<P><FONT color=#0000ff>/* read result of initialization */
4 Y5 X" P3 ~2 P" ~( a4 Ferrorcode = graphresult(); ( l/ I9 H% [; n3 h
if (errorcode != grOk) /* an error occurred */ & p: Q+ c$ R/ M4 T+ k, V
{
4 {4 A( ~% w/ m) q2 t% C; `printf("Graphics error: %s\n", grapherrormsg(errorcode));
7 V- v: H: [% s- ?( bprintf("Press any key to halt:");
8 G: L3 g+ f" ~* Q: F7 Mgetch(); % r$ I: @: g4 d6 a
exit(1); /* terminate with an error code */ 6 [ _" s) f1 Z. `' L9 b' H
} </FONT></P>5 ^' K; A$ D* {, G" I( L, K
<P><FONT color=#0000ff>setcolor(getmaxcolor()); </FONT></P>& }' A: N/ y z: ^' W! R6 b# }
<P><FONT color=#0000ff>/* message in default full-screen viewport */
( k8 E A* W. y' pouttextxy(0, 0, "* <-- (0, 0) in default viewport"); </FONT></P># ?) q* w+ J C/ L$ }) Q1 _" f
<P><FONT color=#0000ff>/* create a smaller viewport */
, `' c$ y- K; o/ [8 t( Xsetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); </FONT></P>
2 X" }! o" P/ T' P<P><FONT color=#0000ff>/* display some text */ ' u# \/ b/ b( y, w1 ]
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); </FONT></P>4 V4 C6 J8 o: C, l
<P><FONT color=#0000ff>/* clean up */
$ U; v! \2 `9 l& @getch();
& q1 f3 q! f6 W; [3 s' y* K K1 |closegraph();
' }* A% C6 a9 r i; E% @0 g$ o' H- Yreturn 0; & V. I" P5 p) u
}
4 W# g! Q1 T0 J# |( d</FONT>
+ e: z, |+ O0 h! ~* V4 X# H- r6 {</P>8 t" c# B5 Q9 l/ t* [, J7 S% z# H
<P><FONT color=#ff0000>函数名: setvisualpage </FONT>/ `9 K$ Q" b% n8 W: T
功 能: 设置可见图形页号 - w) L2 V: n' W1 x" B5 z7 E! _
用 法: void far setvisualpage(int pagenum);
* |! A8 V, k. g# q! Y: \2 W1 C程序例: </P>
5 f- X8 d) l% S$ C: e4 @' R<P><FONT color=#0000ff>#include <GRAPHICS.H>4 X3 a7 _% i2 I. N7 V7 s6 C
#include <STDLIB.H>2 Z2 T; d: {+ j. ^4 G/ Z
#include <STDIO.H>
$ ~3 D$ i0 a! @#include <CONIO.H></FONT></P>- X% E( a1 V/ q
<P><FONT color=#0000ff>int main(void) 4 `4 A* r8 `% L* w5 ^) r
{ 1 Z( f+ X6 `1 {+ s% q
/* select a driver and mode that supports */
8 l. g4 k; D( P" ~/ G& m7 H/* multiple pages. */ 5 H& j9 E, G9 y
int gdriver = EGA, gmode = EGAHI, errorcode; 6 \" j+ B- u: O- _0 N5 e! J5 o
int x, y, ht; </FONT></P>
' }4 |8 I8 J/ s<P><FONT color=#0000ff>/* initialize graphics and local variables */
' B: d3 b% X- Z' Minitgraph(&gdriver, &gmode, ""); </FONT></P>+ L0 |) O! H+ X5 O2 v
<P><FONT color=#0000ff>/* read result of initialization */
6 w) m0 [) m* @! i$ Z3 _errorcode = graphresult();
Z% o( h9 F( \6 r% Xif (errorcode != grOk) /* an error occurred */ W8 Z; S* N2 g( c% m( V/ L
{ 6 m C: n0 {1 E' U+ C
printf("Graphics error: %s\n", grapherrormsg(errorcode));
7 o% p( D0 `' B2 iprintf("Press any key to halt:");
9 M3 T: v7 ?4 `' K. M. g4 M& Ggetch(); 8 x' Y" d- ]% L+ A9 f2 G
exit(1); /* terminate with an error code */ + k) \0 i' I& ?2 \
} </FONT></P>
) K1 C* R+ w5 \0 _! n l<P><FONT color=#0000ff>x = getmaxx() / 2; " W7 b9 f/ G. j* p# I6 C
y = getmaxy() / 2; # X3 r$ }$ k6 [+ A* l8 A* l
ht = textheight("W"); </FONT></P>* t# B; Q" ]7 l1 C
<P><FONT color=#0000ff>/* select the off screen page for drawing */
6 |; }) S( a$ i2 ], Q/ csetactivepage(1); </FONT></P>) }7 o: q! K. n! ^* n
<P><FONT color=#0000ff>/* draw a line on page #1 */
8 N6 l" I" G! n- ^! Uline(0, 0, getmaxx(), getmaxy()); </FONT></P>
D& D! j* S4 Y% q$ \<P><FONT color=#0000ff>/* output a message on page #1 */ * k6 D7 b5 P/ X8 c+ l' S
settextjustify(CENTER_TEXT, CENTER_TEXT);
2 r, c5 t5 K) nouttextxy(x, y, "This is page #1:");
. D. i0 d' X& C4 [& `, mouttextxy(x, y+ht, "Press any key to halt:"); </FONT></P>+ a+ ?9 i! f4 U6 k; M4 I
<P><FONT color=#0000ff>/* select drawing to page #0 */ ; A7 {3 {- _8 @; r+ Y& m
setactivepage(0); </FONT></P>
7 s6 Y% f! p0 }/ f/ H$ O+ e<P><FONT color=#0000ff>/* output a message on page #0 */
5 Q8 W' ~4 u" A* J% jouttextxy(x, y, "This is page #0."); / d4 ~7 k8 Y; v$ i
outtextxy(x, y+ht, "Press any key to view page #1:"); ! ^) h/ @3 A9 Q, _3 ^; T. W; p
getch(); </FONT></P>! Y2 X4 N- s1 w( ?5 I! a& ]
<P><FONT color=#0000ff>/* select page #1 as the visible page */
, Z+ L; \. F4 Q5 |% G, V# Qsetvisualpage(1); </FONT></P>
" t- T T$ \5 h8 J2 I<P><FONT color=#0000ff>/* clean up */ " J T8 @, p% H
getch();
) d! }' Y: M- W+ A* g+ K0 I" w& y7 {closegraph();
9 \" R0 @! F5 p( \: [, J) Treturn 0; 0 g2 ]7 K( Y8 O# N
} </FONT>
8 T/ f* K9 J- P9 w, o( e# @3 N' x% p: t1 e
</P>: A! i' ^. w8 V3 D) ?, N
<P><FONT color=#ff0000>函数名: setwritemode </FONT>
% f# G% c5 {8 m9 v- T, {功 能: 设置图形方式下画线的输出模式 : M% Y5 a( S8 J% }1 `" d4 X
用 法: void far setwritemode(int mode);
7 J! {3 _2 Q* n7 I% t o; t程序例: </P>
! `9 _5 d5 Q5 I7 D0 v8 Q' ?4 S<P><FONT color=#0000ff>#include <GRAPHICS.H>
5 `- z2 ^; T: ^' p4 _#include <STDLIB.H>( x r. _1 a6 F4 f8 ?
#include <STDIO.H>( Q* ^2 T, B9 N- P0 v
#include <CONIO.H></FONT></P>9 w q. e+ W1 v3 t& y
<P><FONT color=#0000ff>int main() ' s7 ?& x# K: s0 {" \* A
{ . B$ n" [( J! `; b
/* request auto detection */ ' F. A; P- c" y, ?2 o3 [
int gdriver = DETECT, gmode, errorcode; - D3 Z/ y% L4 v. w5 N
int xmax, ymax; </FONT></P># ]: `0 x1 W S8 _
<P><FONT color=#0000ff>/* initialize graphics and local variables */ : c6 R' q5 ~& m: \; d0 J3 f
initgraph(&gdriver, &gmode, ""); </FONT></P># e, Y4 r) a- P5 S
<P><FONT color=#0000ff>/* read result of initialization */
% ?3 O |. T& ^! u8 p/ u( Terrorcode = graphresult();
% j" H2 _4 E2 |& V! D# Lif (errorcode != grOk) /* an error occurred */ ; M4 E7 R0 b/ |- R0 j4 G, N
{ " H6 I- E2 [; k
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 \4 D$ [, F+ n/ y. B f
printf("Press any key to halt:");
/ W, u ^" @! S. [getch(); k9 ^! y6 @+ e7 j) b! U
exit(1); /* terminate with an error code */
- D& X2 o: b! X- a} </FONT></P>3 m6 t/ I0 c2 t' D/ k% j
<P><FONT color=#0000ff>xmax = getmaxx();
0 i4 i7 @. J! J' zymax = getmaxy(); </FONT></P>
' t% e, J& X. [9 a- I<P><FONT color=#0000ff>/* select XOR drawing mode */ ' T& a. O3 { L9 y' C& d
setwritemode(XOR_PUT); </FONT></P>
6 ?1 x6 p- O+ r; v<P><FONT color=#0000ff>/* draw a line */ 9 w& I; [" t+ \! G# U. c2 G
line(0, 0, xmax, ymax);
! @0 y4 h+ P: R3 g; ^) \0 z: hgetch(); </FONT></P>' [$ X3 a& u/ h- d( [( g p& E
<P><FONT color=#0000ff>/* erase the line by drawing over it */
0 ^% Q3 [) E- [% s2 mline(0, 0, xmax, ymax); ! s' @$ e2 C& N6 a5 J
getch(); </FONT></P>
7 h) D' x: @1 d8 S7 |& w9 l8 v<P><FONT color=#0000ff>/* select overwrite drawing mode */
( S7 L9 H: j6 ~. ]# R9 O, O2 n* Rsetwritemode(COPY_PUT); </FONT></P>) j5 n S: ?2 _8 J( ]
<P><FONT color=#0000ff>/* draw a line */ 0 @$ j6 m6 q% }/ ]' d+ F1 i
line(0, 0, xmax, ymax); </FONT></P>0 s( \2 v$ e4 |' i% J+ k# B4 O2 V. P" t
<P><FONT color=#0000ff>/* clean up */ * I: W4 K, g# K9 t
getch();
: w; n# u8 M7 Wclosegraph();
' W% B- y/ q- I2 E, c- p9 Yreturn 0;
- W$ B$ @. h/ z}
, S0 q8 a5 n! _, Y# o</FONT># r# N/ A# `" O N/ \/ X, p: p# J2 \: B
</P>
0 D, A5 t' c) S2 `' b<P><FONT color=#ff0000>函数名: signal</FONT> ) X* m- R( o; J
功 能: 设置某一信号的对应动作 , h- m# i9 g& b/ w- B2 I
用 法: int signal(int sig, sigfun fname); ! u. q8 l7 _9 Y7 J% M( e
程序例: </P>
, b$ S/ B) g3 k' e! ^; F6 v& q, X<P><FONT color=#0000ff>/* This example installs a signal handler routine for SIGFPE,
6 ^$ E' b- Z' l8 w6 \2 v f+ Lcatches an integer overflow condition, makes an adjustment - |& p' ?, r k8 g& j$ j
to AX register, and returns. This example program MAY cause : ~& u! @: c2 \4 `: O. M. A
your computer to crash, and will produce runtime errors
! n* M' @8 `& m8 H/ @4 U4 E( h5 Ydepending on which memory model is used. ! s2 K8 u! C' t) w1 T" p2 g/ s; D# [2 k
*/ </FONT></P>
) T! o. h7 N# j5 x7 z<P><FONT color=#0000ff>#pragma inline , A! u. n7 O1 C) X
#include <STDIO.H>
. \8 ~. R$ ~7 u( r& M5 r4 _#include <SIGNAL.H></FONT></P>: O9 U! K2 I j6 S# g0 T
<P><FONT color=#0000ff>void Catcher(int sig, int type, int *reglist)
' y6 A" c6 C; G" K; Y2 S0 a{
5 n/ |- g, K% u k' n# ?4 D% o+ B, Xprintf("Caught it!\n"); / u' o9 A* V. ?
*(reglist + 8) = 3; /* make return AX = 3 */ : V3 M7 M/ ]1 B) r) p/ Y
} </FONT></P>0 ~) z! u0 X G* g# p* w2 c( V. w
<P><FONT color=#0000ff>int main(void)
; C8 B+ U+ O0 r. A{
& g! g' B) w: H M4 M$ {signal(SIGFPE, Catcher); ( i: d/ `9 N3 B" c% p. ?0 n
asm mov ax,07FFFH /* AX = 32767 */
- m& B; y' m: U$ n* s$ X( Z. x Nasm inc ax /* cause overflow */ ! U0 A7 d7 X7 f- T; c v
asm into /* activate handler */ </FONT></P>* ]/ e8 G' T( T2 ^- E Z; o
<P><FONT color=#0000ff>/* The handler set AX to 3 on return. If that hadn't happened, 9 x* W9 ]& _, n [. ~4 x
there would have been another exception when the next 'into' 6 f- }2 E' l4 k
was executed after the 'dec' instruction. */
# _/ S3 d4 D Zasm dec ax /* no overflow now */ " b* s) V" r0 o% t
asm into /* doesn't activate */
' f+ Q; Q* z6 f7 lreturn 0;
& W' r3 K" r( d* `7 `$ o# d} ( h9 l7 i; s6 P& f# J
5 R/ `9 u4 t+ U' h- \% v5 e1 e% s5 d; t) \8 |" L
</FONT></P>8 x; Z" \5 A( X8 G
<P><FONT color=#ff0000>函数名: sin </FONT> R% @# J2 u t0 r& k y
功 能: 正弦函数
( ~0 X+ S: Y4 n+ [9 z0 }/ |用 法: double sin(double x);
2 B. m) L; J7 \; M- |* H程序例: </P>
4 W$ m" z/ z9 B9 n<P><FONT color=#0000ff>#include <STDIO.H>
! f8 @9 e- C6 K: S4 Z; X#include <MATH.H></FONT></P>
# ?' u7 ?& v8 E/ L ?<P><FONT color=#0000ff>int main(void)
4 X. V% P& v, A, }" {{
' F) C! e$ b' ]: N9 K/ ~' ydouble result, x = 0.5; </FONT></P>: B# n& d% F3 `' c5 q9 ]( F( m
<P><FONT color=#0000ff>result = sin(x); ?8 N' b9 a7 X% e
printf("The sin() of %lf is %lf\n", x, result); 9 @/ {4 ^, Q# K, L
return 0;
4 u1 m' C+ i& H) M3 n# N} 8 ~" F' B0 w" c! |- B* f! ^% ?
4 ]; V& j+ a+ n- p! x6 i- o2 u
</FONT></P>1 z* ]4 W& T b2 T
<P><FONT color=#ff0000>函数名: sinh </FONT>$ D- z; K0 { I! O8 i
功 能: 双曲正弦函数 * L( S+ ]. `, K" p& H- x- O6 ~$ T1 }
用 法: double sinh(double x); 7 V. m- i- N+ ?& }, J
程序例: </P>0 M3 _4 Y% Y2 _# [
<P><FONT color=#0000ff>#include <STDIO.H>. b( Q: O; J* V; Z. k8 ]' i6 H0 B
#include <MATH.H></FONT></P>5 r6 q& T" P9 W5 P: ?$ j( w
<P><FONT color=#0000ff>int main(void)
7 u/ v) A1 O) \7 b% ]) A{
- ]& C! B7 Q9 x- Y% F1 o' R' ?4 Pdouble result, x = 0.5; </FONT></P>4 l$ ~% r. @: Z* ~, Q9 S
<P><FONT color=#0000ff>result = sinh(x);
+ S' ~. M3 |2 |- N; k* S0 x+ Cprintf("The hyperbolic sin() of %lf is %lf\n", x, result); ' z. @" u7 F& h) w4 ~0 U
return 0;
5 Y* A' R1 C2 o* G}
3 h! a. \4 [" O# n$ x a7 C8 a% t+ J; _9 ]
1 Q; v7 g/ `. _9 R0 ~
</FONT></P>
9 \. [* ^, @3 u# \/ m<P><FONT color=#ff0000>函数名: sleep </FONT>
1 s4 M0 U5 s% C& a. n功 能: 执行挂起一段时间
5 q/ }5 h* r" y' H# [( V, p6 }1 K用 法: unsigned sleep(unsigned seconds); / d9 d) m; w$ ` ?/ x8 I
程序例: </P>
$ ~, R5 I+ g5 g<P><FONT color=#0000ff>#include <DOS.H>
! c# Z$ @ O* _6 \#include <STDIO.H></FONT></P>
# l! f- V' N: Z& B% E) {<P><FONT color=#0000ff>int main(void) / M. A5 i( ^ w/ W
{ 6 D) s' z) u g# K* M5 A
int i; </FONT></P>
& {+ k- i- S- B B3 p7 B. B<P><FONT color=#0000ff>for (i=1; i<5; i++)
3 I$ ]4 U2 l; G2 }- W- n{
2 F9 ?4 D& D9 E' E( wprintf("Sleeping for %d seconds\n", i);
1 [0 g$ a! p: a6 d; Msleep(i);
9 Z" \5 |& x* c8 Z( D' a: f2 s} , k/ L' n- ]1 Y5 n0 i
return 0;
7 u1 s' y5 B" u6 d# {- P5 Q} , n6 a: t! b! }. x W
5 ]9 e; X# T' C$ S$ o" A) o# u
</FONT>
# X# \, P6 w6 u1 L- P# d6 P</P>: N, |1 M. B6 Z. n, R6 c& I
<P><FONT color=#ff0000>函数名: sopen </FONT>! |2 o0 Y6 t! `2 @. c& S$ X# d
功 能: 打开一共享文件
3 u3 C! g3 U- J1 F5 {用 法: int sopen(char *pathname, int access, int shflag, int permiss);
3 W# A- `4 b# [! f( q% p' z程序例: </P>, J5 y' B4 B8 U7 |9 O+ T+ z
<P><FONT color=#0000ff>#include <IO.H>+ D) I' s6 l1 G
#include <FCNTL.H>3 o3 h ~2 f/ P; Q
#include <SYS\STAT.H>" U8 c5 I6 N- ~# b6 I' X
#include <PROCESS.H>2 i0 |) u s$ ^
#include <SHARE.H>1 g" G0 o# I6 w# O7 U; S; F `
#include <STDIO.H></FONT></P>
' f2 r7 B- N1 N: t( o<P><FONT color=#0000ff>int main(void)
9 j- G! j3 B+ S1 F+ w6 D# p3 Y{ 4 j7 [5 i7 Z7 n9 N' ?1 M* D) b
int handle; 8 g" u8 E7 u0 l1 J
int status; </FONT></P>+ T/ D8 S3 C" d$ F( n; P0 I
<P><FONT color=#0000ff>handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD); </FONT></P>+ G2 O, f: r |
<P><FONT color=#0000ff>if (!handle) 3 N- f% q2 P5 f. t
{
1 F9 u+ r, u0 g& T" c$ fprintf("sopen failed\n");
% e/ a% A# Y, n# u* Rexit(1);
, M0 ]7 P1 E( [5 m/ r% e& T} </FONT></P>
$ A0 [! e5 ~' {% D1 V5 R<P><FONT color=#0000ff>status = access("c:\\autoexec.bat", 6);
4 c5 R0 |9 M( v% F' fif (status == 0)
1 k! i5 P- H- U: a8 c- nprintf("read/write access allowed\n");
! P2 Z5 G0 Z5 D- Yelse
. l B1 J V+ c: }4 x6 l' Y2 |5 }0 bprintf("read/write access not allowed\n"); </FONT></P>: J* T/ ~9 w3 m
<P><FONT color=#0000ff>close(handle); 8 {3 r" e. `+ @+ g- [* H1 Q" U/ V' K
return 0; " _0 u$ }7 n( s& ? w' w
} a: U1 M Q% }9 k" q
- T: _0 n } B9 j( J& b</FONT>
! K S' p# N, V$ N; s. V</P>/ [% B" x6 G. z( k
<P><FONT color=#ff0000>函数名: sound </FONT>4 \2 e7 |7 f3 S5 G, T4 T
功 能: 以指定频率打开PC扬声器 # p, N/ o: K4 u2 f% N# H" K0 J
用 法: void sound(unsigned frequency); # B2 L4 ^- n4 Y( ^2 E3 }
程序例: </P>
* Z+ J' v' u* K( ]# x# `<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds.
: m( Z2 B: ? T# dYour PC may not be able to emit a 7-Hz tone. */ 2 N6 P9 q& C2 U
#include <DOS.H></FONT></P>
$ Q# R, r( u% x1 {! K$ g5 ]; B<P><FONT color=#0000ff>int main(void) + n7 X! `7 T$ a, H: W% |
{
6 A9 Y' X" z) |; m' B8 }sound(7); ) H+ g( `1 J+ v' I6 a
delay(10000);
& z1 R e1 w& g8 knosound(); 6 h, H) S0 L( H" b4 ^5 C; d
return 0; 7 q# R' l! J3 u P
} </FONT>
: }5 M- `' r0 ?! t
( l. }4 z/ J6 h3 h+ J$ b- h6 C4 o2 z. ?
! m& @! r. `* v0 | C6 c</P>" C! S T0 }( @2 K1 q
<P><FONT color=#ff0000>函数名: spawnl </FONT>
4 E% ?3 F6 j& b5 T- G6 f5 A6 i& j功 能: 创建并运行子程序 " C/ }, l0 f1 [0 z5 D; o6 f9 }2 T
用 法: int spawnl(int mode, char *pathname, char *arg0,
1 z8 O* p: A7 L' M; M2 Z7 Darg1, ... argn, NULL); ) u/ x2 @3 N4 w: t& L
程序例: </P>
7 e% s( }( M9 A' a( P! c<P><FONT color=#0000ff>#include <PROCESS.H>
4 t/ f9 h6 t* k( \4 {% O; p#include <STDIO.H>
" X: Q& y5 A6 Y#include <CONIO.H></FONT></P>
( U& ~2 ^1 N( C) r0 v0 S# J! j& y<P><FONT color=#0000ff>int main(void) 4 [1 p) p2 N- [, {' o) X1 [1 U) ^
{
% X4 a: U/ c& z( Oint result; </FONT></P>
) R$ q5 i% `5 Y1 f" Z; Y7 `<P><FONT color=#0000ff>clrscr(); ( z; R( y' h6 R# r$ [+ z
result = spawnl(P_WAIT, "tcc.exe", NULL); / c: L6 ?& w2 m3 T
if (result == -1) ! L4 Y3 l, K$ V; z
{
$ D0 y& D4 g& d$ dperror("Error from spawnl");
3 } J0 G7 b% Y" H# Aexit(1); ! d4 m. ~. G, o5 j/ S) D
}
' A- q" _1 f; ]return 0;
% c5 g& {: ^. T4 G$ K} 9 y& e1 u0 ?' v1 o# g
</FONT>& l: _% \/ Y2 ^' H
</P>/ `7 t# b5 ~& J7 x
<P><FONT color=#ff0000>函数名: spawnle </FONT>% P2 D: [- v+ L$ {. y+ C
功 能: 创建并运行子程序 + l8 V& C. `+ n2 _% K
用 法: int spawnle(int mode, char *pathname, char *arg0, & v- ^# R* S: h v
arg1,..., argn, NULL); 3 s) {; b0 |& G) m
程序例: </P>
" {; Z2 S. |; k# J<P><FONT color=#0000ff>/* spawnle() example */ </FONT></P>
* ?" z( ~+ J) ^, I# s* o* n5 h<P><FONT color=#0000ff>#include <PROCESS.H>
5 P& H7 d4 Y0 B) c/ g2 V#include <STDIO.H>
7 s5 J3 c* A9 N2 |2 w: [' z#include <CONIO.H></FONT></P>4 J1 o( w8 W) `) q
<P><FONT color=#0000ff>int main(void)
1 o5 {% I; F4 L% P& f0 F: M0 z{ $ c8 E4 n/ |0 o7 z
int result; </FONT></P>
' t& `+ i. Y ~9 [: o: ^2 B) @<P><FONT color=#0000ff>clrscr();
( h3 Q: A8 u6 b! g2 ]6 dresult = spawnle(P_WAIT, "tcc.exe", NULL, NULL);
7 I! ^6 _% R/ wif (result == -1) 7 k* g+ K, S) N5 u2 g( l
{ b: C! b% P' w% A5 E$ f
perror("Error from spawnle");
+ K; N; _% W! B5 S( ~5 lexit(1);
% f* x$ ^8 w& y& N}
g* z S/ c; y) Treturn 0;
& m( L* a0 A( d* |9 e7 H4 L} " p3 r% E1 f" I6 l! P5 w# T+ S
8 P% o& M6 N4 b
; U) Y% m$ c1 U2 N" }: L6 j% L) [</FONT></P>
7 W8 _$ o0 W5 e- w9 m7 W* u<P><FONT color=#ff0000>函数名: sprintf </FONT>* m. n- @2 W& |" g; i% J6 a1 ], f
功 能: 送格式化输出到字符串中 1 S9 w, D: n% Q- m6 v
用 法: int sprintf(char *string, char *farmat [,argument,...]); 4 f" @3 C9 g5 [8 z/ O& I- |* }
程序例: </P>
6 Y2 R9 _" f% \' u, S) K3 s5 E8 u* _<P><FONT color=#0000ff>#include <STDIO.H>
5 R0 u+ N7 C% M4 T$ l' E4 _#include <MATH.H></FONT></P>
4 _2 E# F. R7 C6 e; m2 s<P><FONT color=#0000ff>int main(void)
p7 ^- }, h5 i3 G$ ~ F* Y6 D{ & `0 [2 U! t* F8 w
char buffer[80]; </FONT></P>
$ \/ f& e1 }6 b8 ~* w<P><FONT color=#0000ff>sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
; q; r! a8 W( _; `5 [puts(buffer);
8 {" o& b& {7 F4 W$ W" Rreturn 0;
" w! V2 d# v' p% d. w7 q0 P}
( h, x, }2 Q& ?. D</FONT>5 X+ d) q8 s9 V$ A+ o2 V
</P>
5 f9 \# ?5 C5 I0 W% w<P><FONT color=#ff0000>函数名: sqrt </FONT>
. _6 H% Z0 a6 X0 R9 K/ T功 能: 计算平方根
$ ~5 E& R3 e/ E2 o$ i用 法: double sqrt(double x); : Q0 b( ^$ [! ~ k
程序例: </P>5 }4 m3 _7 m$ ^
<P><FONT color=#0000ff>#include <MATH.H>
7 p9 z! Q; j- s+ h" T#include <STDIO.H></FONT></P>6 o; M6 ~9 {' D" Y% w
<P><FONT color=#0000ff>int main(void)
& I! p$ R! q& j% r" ~: G{ 1 s/ J6 T) x% V/ N7 `8 A& v+ p
double x = 4.0, result; </FONT></P>
- W+ Y ^) f: j9 y8 _ s" J u; y<P><FONT color=#0000ff>result = sqrt(x); % c2 C4 b' T, r0 _/ s0 `2 G8 J( X
printf("The square root of %lf is %lf\n", x, result);
6 Y. k5 e1 ~* Preturn 0;
\# F o+ T* g) o* u}
+ C3 J2 l& _4 K! D</FONT></P>
0 z# X4 D& b. o0 j0 z<P><FONT color=#ff0000>函数名: srand </FONT>
2 G$ a8 o5 |$ r( k! k功 能: 初始化随机数发生器 & C& U8 o0 y0 {$ S" e' t- d4 \
用 法: void srand(unsigned seed);
9 \& \ R& z. [# {& X0 [程序例: </P>' v" A( [! Z( P3 B
<P><FONT color=#0000ff>#include <STDLIB.H>/ G/ |+ |! \/ R, Z3 h9 f, B
#include <STDIO.H> ]6 R# r; w. W5 E
#include <TIME.H></FONT></P>$ D7 g% L5 [1 H6 p& x
<P><FONT color=#0000ff>int main(void)
1 w$ ]$ y1 ?- d5 ]' b9 o6 h{ ( ?) V5 s/ [) h7 t3 [0 V
int i; : L \# Z" a; F& B
time_t t; </FONT></P>- q6 J2 K7 E& p3 j& _
<P><FONT color=#0000ff>srand((unsigned) time(&t));
5 }' |2 B5 C% B% k+ F/ y5 K- jprintf("Ten random numbers from 0 to 99\n\n");
! v& S* Q' }5 v$ h9 sfor(i=0; i<10; i++) % ^+ j; a& y3 X/ y5 T6 s7 e
printf("%d\n", rand() % 100);
0 S* P, i7 Z9 j9 \$ n/ @1 Hreturn 0;
$ f6 L+ [5 L% `4 w6 P& I' K" w} & a# p7 Z5 W+ Z9 s5 n- Z8 Q
</FONT>
0 s$ E8 p9 {% O& P2 J7 Y</P>
; p, H; B% q3 j% H% Z) ~5 x<P><FONT color=#ff0000>函数名: sscanf </FONT>- J5 h X1 Y5 b7 n1 ?* c" w
功 能: 执行从字符串中的格式化输入 ' y1 o1 b) u9 Z0 a/ ?. x4 `, l
用 法: int sscanf(char *string, char *format[,argument,...]); 3 R9 W# _1 U- C; Y) t' Q
程序例: </P>9 T+ I+ @& X; A' N( \3 W# `
<P><FONT color=#0000ff>#include <STDIO.H>
: O# j3 Q0 v/ E% M% \5 x8 H( p#include <CONIO.H></FONT></P>% t" E) x5 `9 ]
<P><FONT color=#0000ff>int main(void)
9 C, B( C8 Q# X/ _: v{ 1 V/ C \5 p2 M1 c
char label[20];
2 s2 B! M& X1 N. I* w! m5 e jchar name[20]; # |5 [3 D" Q3 d0 U
int entries = 0; & w8 f* k/ K! {. x" U+ ]' _9 g7 v
int loop, age;
3 O8 ~2 g& @: Sdouble salary; </FONT></P>2 O3 t- x2 M' V+ D! m) D
<P><FONT color=#0000ff>struct Entry_struct + T2 [! O2 h) X% S: i% y7 }/ P+ x
{ 2 U7 U1 @+ @' g0 @
char name[20];
8 [5 n2 X1 N6 Y8 ^7 T/ n; Iint age; E7 F" R/ y# t# j" K' a2 @0 S# L
float salary; : c: q- }' p( q8 ]# \" E4 c& ?# O" o
} entry[20]; </FONT></P>! [1 G" _: Y- {3 Y m
<P><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
4 o/ `" _0 M) _9 X% Q4 Pprintf("\n\nPlease enter a label for the chart: "); % F% A( K' T, s j
scanf("%20s", label); % [/ g% G' U1 n0 y! K+ A$ U7 Z
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
5 A7 H1 \5 b6 i<P><FONT color=#0000ff>/* Input number of entries as an integer */ 8 o. Q8 a( g( m
printf("How many entries will there be? (less than 20) ");
* t: `6 Y( t( h8 s4 t8 ~" \scanf("%d", &entries); ( G" Y! O2 q. T' V& e% `
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
) j" I3 H* Y5 O3 L<P><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ ( A3 c7 x$ W& W+ F& q
for (loop=0;loop<ENTRIES;++LOOP)
4 x0 J* A& \$ a1 E$ p { + {# U- X, i$ o. u
printf("Entry %d\n", loop);
7 M! H9 J) V- k: c. F o) _printf(" Name : ");
$ n) B0 z( }( l( k0 T8 dscanf("%[A-Za-z]", entry[loop].name); 1 U, g% e8 y+ @( v: ?" y
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
$ Y( R! S' d7 o+ C<P><FONT color=#0000ff>/* input an age as an integer */
: a+ ^! L; ^# W: R7 hprintf(" Age : ");
& n# ^7 f" Q- K1 u# U8 Cscanf("%d", &entry[loop].age);
* |/ m1 j8 s& v7 k% Jfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
' N, i4 r0 U$ s<P><FONT color=#0000ff>/* input a salary as a float */ 0 W1 P/ R3 |4 N" l0 X
printf(" Salary : ");
$ d, r) @ m, V( S1 m8 j- jscanf("%f", &entry[loop].salary);
* _/ @( w m, M" O H7 Hfflush(stdin); /* flush the input stream in case of bad input */ $ w% h% ]0 {( y2 s3 M& o
} </FONT></P>
- b5 X6 |* E3 Y" R; c; S/ m<P><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */
0 f# J5 p3 C7 b; `! eprintf("\nPlease enter your name, age and salary\n");
: n9 ` x M7 g1 m* ]scanf("%20s %d %lf", name, &age, &salary); 9 w$ w# m3 \5 }* H
</FONT></P>
, x# M5 Q; X% E/ f0 G4 ?7 j<P><FONT color=#0000ff>/* Print out the data that was input */
2 q- U2 _ n) }8 R2 qprintf("\n\nTable %s\n",label); ; @: `" `, j& a0 @" Q% J) x4 B, p
printf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
7 W Y# t f4 Z* Q" j( yprintf("-----------------------------------------------------\n"); 9 J% Z6 B/ Q, t" J" Y" D
for (loop=0;loop<ENTRIES;++LOOP)
" ]6 B" s$ P3 l. S* @ i' s7 }7 l8 w printf("%4d | %-20s | %5d | %15.2lf\n",
3 U9 \& h3 X6 \8 P8 m: G2 C4 F2 hloop + 1,
/ H% h/ \' t! j2 {& sentry[loop].name, 9 s% ]* @3 v9 P- T1 b: R
entry[loop].age,
1 J: f0 O. O% U6 j- e( b# mentry[loop].salary); 5 e+ k0 i+ I+ s& ?9 ^
printf("-----------------------------------------------------\n");
% W% S8 b. O) h( E8 r" K4 breturn 0; 7 M# e% ?$ i+ E$ v
}
# L. w" I! W/ g q
4 k0 f& Y' @5 n+ Z- W( s</FONT></P>4 j6 U* ~/ d) ]$ s
<P><FONT color=#ff0000>函数名: stat </FONT>7 ^7 u) Z" p% X: M6 _' ^/ E
功 能: 读取打开文件信息 2 S& `! H' k/ s0 K5 N
用 法: int stat(char *pathname, struct stat *buff); , G# z0 k2 w9 m8 o1 F# s( t x
程序例: </P>9 A! V; C7 X% x
<P><FONT color=#0000ff>#include <SYS\STAT.H>3 Z, S. m- `8 w: C G$ ^
#include <STDIO.H>
u1 N4 b, b" p$ g* @. m5 z/ u7 M#include <TIME.H></FONT></P>
2 Z4 ]& p; L, _3 M( Z6 P7 K* {; G<P><FONT color=#0000ff>#define FILENAME "TEST.$$$" </FONT></P>" w- n$ m$ g) [! T9 ^1 h3 F
<P><FONT color=#0000ff>int main(void) / a0 d, @# q6 D. v' |) y
{ 1 x k4 v- L" y7 y8 ~2 Q. N. C
struct stat statbuf; " n6 x+ I% i2 M% U9 e
FILE *stream; </FONT></P>1 K g3 u# l6 t) H2 `1 L
<P><FONT color=#0000ff>/* open a file for update */
. a! Y3 e6 T z% P% C3 w# zif ((stream = fopen(FILENAME, "w+")) == NULL)
* U# [# [; u, R; v9 w- A# Z/ ?{
" |, [' S# ]% W; e7 E5 G# E: Jfprintf(stderr, "Cannot open output file.\n");
+ w, T0 t. f$ x0 b( P3 }: oreturn(1); Z9 t- r f9 `" i6 S% k
} </FONT></P>
+ a( Y4 s3 T/ c+ r<P><FONT color=#0000ff>/* get information about the file */
, T1 ?4 R: _$ L- ustat(FILENAME, &statbuf); </FONT></P>( x0 Y F' n: j7 r, G R' c) `: e
<P><FONT color=#0000ff>fclose(stream); </FONT></P>$ D! i b: J w0 T/ _0 F* V! w$ g
<P><FONT color=#0000ff>/* display the information returned */ ! ?4 x1 a/ j T# D1 o0 @
if (statbuf.st_mode & S_IFCHR) + {, C8 l$ T8 Y! |9 w# F; t
printf("Handle refers to a device.\n"); . u" _$ C* }4 k5 P5 [2 c
if (statbuf.st_mode & S_IFREG) " W2 \' d9 g: t& J$ _
printf("Handle refers to an ordinary file.\n"); 2 `, @2 W3 `# Y9 k
if (statbuf.st_mode & S_IREAD)
" z: [# v- z0 u( }5 k" Aprintf("User has read permission on file.\n");
l9 H" P H/ G7 Y2 P! f lif (statbuf.st_mode & S_IWRITE) 3 c, E: E% f0 K' ?- c4 s
printf("User has write permission on file.\n"); </FONT></P> _+ |8 c: Q7 o- P2 X1 ], x
<P><FONT color=#0000ff>printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
0 Q% g8 }7 l- @6 i+ Fprintf("Size of file in bytes: %ld\n", statbuf.st_size);
* b1 z7 U% E; S+ A) o! vprintf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
4 ^" H2 [; b+ [1 Rreturn 0; 2 _& D Y1 ]' u; h% ?) A
}
+ D7 f: N/ \* e3 ^( f- C/ X6 M' h) A. @
</FONT>
: Z8 m1 W4 z7 O1 U) L. P3 a! _</P>
* U X' E+ w* p* K; ?5 c8 d8 ^+ a<P><FONT color=#ff0000>函数名: _status87 </FONT>
% @/ o" r7 v7 n4 Y3 d功 能: 取浮点状态
1 L0 q+ H: X" M2 v用 法: unsigned int _status87(void);
" ^5 W* P9 v" y程序例: </P>7 H: p: u) S. C, v
<P><FONT color=#0000ff>#include <STDIO.H>
6 E3 ?6 I8 T, v( u8 M8 l#include <FLOAT.H></FONT></P>
- T/ j7 }5 C1 r) |0 m% S; }<P><FONT color=#0000ff>int main(void) - S7 w4 k, H& ?* j% ~4 v' G
{ 9 Y4 T2 `4 I" I- y
float x; 1 [ C1 s0 H* F& {: F8 J
double y = 1.5e-100; </FONT></P>, o1 _: Z: _: `; Q5 i% W) }2 l
<P><FONT color=#0000ff>printf("Status 87 before error: %x\n", _status87()); </FONT></P>! _6 i: U7 K# m7 a- s5 [
<P><FONT color=#0000ff>x = y; /* <-- force an error to occur */ # a3 C$ H. i! U2 L
y = x; </FONT></P>0 M( _; b; m4 W
<P><FONT color=#0000ff>printf("Status 87 after error : %x\n", _status87()); , T, Z1 l, H3 Z& ?( a9 I$ }
return 0;
% Y K( X1 l T. @9 ^$ I) x- C} * i6 x$ L0 ?* n4 @$ c
</FONT>) n! h! d- c0 j8 b% n0 n
</P>3 `" k) t3 ^7 S! U% G- \
<P><FONT color=#ff0000>函数名: stime </FONT>
8 C% Y% N% L$ I V功 能: 设置时间
( m! R4 |/ p6 S* B; y用 法: int stime(long *tp); 5 W6 R5 ~. f) K0 k
程序例: </P>' i0 c: L% o' X9 C9 u. z% y7 y* O
<P><FONT color=#0000ff>#include <STDIO.H>! r2 ]7 f# ^' D( P8 F4 F6 D* ^
#include <TIME.H>
) ^: ^) D& o' A7 K3 t t#include <DOS.H></FONT></P>$ c) I; c- E r& o. ~
<P><FONT color=#0000ff>int main(void) & P$ p" T2 f5 f; z# k
{ ( n% V. I9 F' Z# i2 O
time_t t; : w5 [0 w& t; u2 |) |- U
struct tm *area; </FONT></P>: v! ^$ L& [8 E
<P><FONT color=#0000ff>t = time(NULL);
) v. w- |5 r; ?: V( a0 y& J9 Harea = localtime(&t);
4 U [2 i* i0 ?1 {& C! V1 Aprintf("Number of seconds since 1/1/1970 is: %ld\n", t);
' c/ y' [. t2 m6 F$ Cprintf("Local time is: %s", asctime(area)); </FONT></P>/ M* [1 }* y. l! y! E
<P><FONT color=#0000ff>t++; * N, j2 S8 R& A, H) q
area = localtime(&t); ( U( l1 Q! b! y, W, ] T9 H
printf("Add a second: %s", asctime(area)); </FONT></P>
- b: B! p+ x# I# J: U<P><FONT color=#0000ff>t += 60; ; g" x) G) a* A1 j& }* X% b
area = localtime(&t); 4 j( Z/ X% J$ D' X5 i
printf("Add a minute: %s", asctime(area)); </FONT></P>( g/ \2 m: B6 @' [
<P><FONT color=#0000ff>t += 3600;
5 V$ f& K. j( g2 X- farea = localtime(&t); # `- D) n6 T3 h1 x
printf("Add an hour: %s", asctime(area)); </FONT></P>& z9 G5 ], \: Z4 M( l5 G1 @- H
<P><FONT color=#0000ff>t += 86400L;
" d4 X( P* |7 I6 A5 ~$ q" j# Larea = localtime(&t);
3 e3 p' C- X) P0 D# J9 xprintf("Add a day: %s", asctime(area)); </FONT></P>
" x D* \9 q4 T G, {9 W! L/ k3 k; l<P><FONT color=#0000ff>t += 2592000L;
" D8 \- n8 {$ varea = localtime(&t);
4 u# R2 \" ^4 |6 Q" ^printf("Add a month: %s", asctime(area)); </FONT></P>
7 c3 r+ Z3 [9 L8 _. ?, \<P><FONT color=#0000ff>t += 31536000L; % J0 Z% ]; o7 G1 d1 p8 ?! J6 G/ S
area = localtime(&t); ; m: H# o% m) m+ L
printf("Add a year: %s", asctime(area));
. F! i1 ^' P9 v \5 xreturn 0;
4 q) R3 k8 x3 C- d7 V3 Q$ C} </FONT>
, u" j% S/ p% V" F3 K8 F. @: K, s, w6 R1 J3 X+ c' W9 K3 h) N
; Q7 a4 F* W* S `$ ^1 g</P>
?9 R& o4 O' \4 D& F& b/ k% J<P><FONT color=#ff0000>函数名: stpcpy </FONT>
* u- b0 b8 l- G' Q5 `' Y% s0 J功 能: 拷贝一个字符串到另一个
+ O; p: ?3 g" i0 K7 P用 法: char *stpcpy(char *destin, char *source);
: l, q7 C& Q3 x5 G' x程序例: </P># B( U% S) K! W5 }. I8 l. a
<P><FONT color=#0000ff>#include <STDIO.H>
- r0 T% {8 ?% A2 C0 I/ K ]8 _2 x#include <STRING.H></FONT></P>% j& k J3 g1 U; G g2 z+ l2 u7 ~
<P><FONT color=#0000ff>int main(void)
- K0 n, p! _5 C. S5 L" q+ m{
8 |6 c1 e+ V0 ]% h2 e+ l! fchar string[10];
( W4 O( U P6 B- t' h3 ]2 x9 \/ Bchar *str1 = "abcdefghi"; </FONT></P>" K6 p% m5 L# N1 w$ L
<P><FONT color=#0000ff>stpcpy(string, str1); ' R1 p$ P4 k. F' L5 K9 H' K: L
printf("%s\n", string);
) H) V: i3 C+ {( areturn 0; 4 q! ]: D" M* n2 m
}
7 R- M2 q' U2 B5 D; r8 v4 q1 O, R
4 c. d9 ]1 E# b& ?: Y! n% [</FONT>
. R& f* Z) o$ o% |</P>
% s( K- [2 N: U8 o, W<P><FONT color=#ff0000>函数名: strcat </FONT>. i: e# T8 `9 I5 L3 ]
功 能: 字符串拼接函数 " Q: W7 ^ J( B3 y! d
用 法: char *strcat(char *destin, char *source); & U- F9 K7 }) F
程序例: </P>& l* T# u v" u( v( `" a
<P><FONT color=#0000ff>#include <STRING.H>
5 C- G1 d' S0 P8 J#include <STDIO.H></FONT></P>
0 r! \6 [- |2 }7 o0 \: Q<P><FONT color=#0000ff>int main(void)
y4 ^* D& p \) `. h0 z6 J% T: y5 z{ 9 H1 g0 Z# v7 r: X5 R: h$ _; x
char destination[25]; ' r% P& Q5 J; ^- c/ ?+ V1 F* H
char *blank = " ", *c = "C++", *Borland = "Borland"; </FONT></P>
, {: d+ Q u1 x; J( E/ O3 Q<P><FONT color=#0000ff>strcpy(destination, Borland); 5 S2 u- ?( V- U, u; S
strcat(destination, blank);
% w% d- @6 A, Q1 h/ [& I3 T1 l, G$ kstrcat(destination, c); </FONT></P>! a" L( I. o1 d0 a$ R8 ?; U- K
<P><FONT color=#0000ff>printf("%s\n", destination); + O, V0 x8 x$ q) h( W2 a# g l
return 0;
1 k! E# \5 R! w4 X7 z% d}
5 M* [( k% J2 b8 `0 J. b* d, c$ z/ K4 N+ _
</FONT><FONT color=#ff0000>
: e/ H4 R- m* v$ {, W0 ^! N1 C</FONT></P>
1 w2 x- s K8 c% e" |6 b$ H) R' D<P><FONT color=#ff0000>函数名: strchr </FONT>
|% C i% ^' X功 能: 在一个串中查找给定字符的第一个匹配之处\
! p# y. G( w$ \/ C+ K用 法: char *strchr(char *str, char c);
9 T8 b9 e5 d- y1 _程序例: </P>
+ Q7 ^7 D2 b; ]! K B8 j<P><FONT color=#0000ff>#include <STRING.H>; p x) s% Z/ q; C
#include <STDIO.H></FONT></P>6 C4 D1 i/ w; ]" c: Y
<P><FONT color=#0000ff>int main(void) ) r) }. S# @. ?6 n
{
" J( X/ D; F c: ichar string[15];
* r; P# X6 \. ?+ ~: C3 H" \, U7 a0 {6 l8 ]char *ptr, c = 'r'; </FONT></P>
' g/ ^. i( @+ x2 V. M7 ^5 ~<P><FONT color=#0000ff>strcpy(string, "This is a string"); ; U( {, m" d& @! a1 v5 d# ]
ptr = strchr(string, c);
' _2 v& L$ b: t3 W' G _4 m: u9 rif (ptr) 6 y) S# w" y' E+ ~8 d+ [
printf("The character %c is at position: %d\n", c, ptr-string); ( ]8 O C) ^5 B
else 3 [4 ?) h# i/ w1 t S
printf("The character was not found\n");
8 M5 }4 E: c9 y, @ Greturn 0; & ?2 \$ ~4 M. p
}
( A+ h" ~5 x& t/ L5 \</FONT>; f9 L# X+ s: I- u
: y7 v. c& M, v1 V3 _: Y
</P>3 U- g1 h& f0 b5 h/ c/ c/ c
<P><FONT color=#ff0000>函数名: strcmp</FONT>
8 r4 {! E7 b; C7 M功 能: 串比较
/ x: x V& B" |2 ]/ a1 H& w用 法: int strcmp(char *str1, char *str2);
/ m0 z; |7 L& V K8 @+ A: w程序例: </P>% y t% a5 r L- i/ Y1 F2 t
<P><FONT color=#0000ff>#include <STRING.H>
# G+ y8 X; M$ J0 h#include <STDIO.H></FONT></P>
3 J: L; e5 m' ?<P><FONT color=#0000ff>int main(void)
" n- o0 ~; h( V. p* B$ A2 x{ 8 f1 o, ]9 Z- ]* P* O$ t
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
4 l0 p4 J; m. R* I# D4 xint ptr; </FONT></P>( S. ?! y; ]# P C- j% C
<P><FONT color=#0000ff>ptr = strcmp(buf2, buf1);
U% {1 P3 s8 F3 V% e; e* V( Oif (ptr > 0) 3 I \4 n- u5 Z3 b# E+ I- p" x$ r
printf("buffer 2 is greater than buffer 1\n");
( N, q( g' [# _- |# I0 o8 Y$ gelse
( g+ X% ~* k) y: t9 e- Sprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
' a" F. Q U4 y) t0 t: B: x% X<P><FONT color=#0000ff>ptr = strcmp(buf2, buf3);
/ E( {( p L% A3 p- @2 C" x% Rif (ptr > 0)
) f [ ] ~% u0 t+ B) R9 M$ @9 rprintf("buffer 2 is greater than buffer 3\n");
/ h( p& H. Q0 }2 _$ Yelse : Q2 E% H5 N% q2 E0 Z
printf("buffer 2 is less than buffer 3\n"); </FONT></P>4 c( f1 s ?$ T9 P: _! k/ [
<P><FONT color=#0000ff>return 0;
6 b1 {# y( K. x; W8 r8 w}
; T1 B" D- `7 K1 _: Z4 ?. |0 w% M# U- P
+ H) f) e. s3 p# O+ \1 g3 C0 u
</FONT></P>
6 o1 c7 h8 [7 f/ T6 Y" F<P><FONT color=#ff0000>函数名: strncmpi </FONT>- @' C3 Z. b# H2 U" b( G
功 能: 将一个串中的一部分与另一个串比较, 不管大小写 , ^& u- u/ }5 o8 x$ o% w
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen); : [) Y1 y0 M* _3 E# J
程序例: </P>
% E3 {& C6 T. l* {<P><FONT color=#0000ff>#include <STRING.H>; y0 c. I0 E0 B& Q2 P
#include <STDIO.H></FONT></P>
, J2 Y9 x; U- h, C<P><FONT color=#0000ff>int main(void)
- j0 M6 ]6 u8 y9 g" O: ^, t{ 5 |* V, s5 {3 \( k5 @* l
char *buf1 = "BBB", *buf2 = "bbb";
2 I3 c; h6 C$ e) fint ptr; </FONT></P>3 `. g* H9 X7 h+ T+ s
<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>
4 A, B7 u& L) }: L8 a$ v<P><FONT color=#0000ff>if (ptr > 0)
* `' l: k0 p! e3 Z Q& e5 J+ @printf("buffer 2 is greater than buffer 1\n"); </FONT></P>- K" Y! u W' P. N3 v! S3 ?1 V
<P><FONT color=#0000ff>if (ptr < 0) ! g8 u/ A$ f- i
printf("buffer 2 is less than buffer 1\n"); </FONT></P>1 K+ ]! N( h/ [4 W1 d
<P><FONT color=#0000ff>if (ptr == 0) . X" x6 i& y9 u9 E( D
printf("buffer 2 equals buffer 1\n"); </FONT></P>
0 Y3 d( r. p0 C& I<P><FONT color=#0000ff>return 0; 2 Y' l0 D9 L' G! J
}
) C( g: T" o' ^' @3 R9 w
" B* ^4 ]1 E2 Z% I4 T5 m2 Z9 T6 |</FONT>5 ~( o# j' Q3 o9 {; ?/ t
</P>
+ ?/ T/ k4 v/ q+ C6 D* b5 J<P><FONT color=#ff0000>函数名: strcpy </FONT>& a3 n: M0 z# h+ c* A& o e
功 能: 串拷贝
0 I2 v4 q9 N* {" k( _- v/ |- _1 ~用 法: char *strcpy(char *str1, char *str2); 9 J; M3 |5 h Z/ C
程序例: </P>
3 v3 u S& v2 N1 D<P><FONT color=#0000ff>#include <STDIO.H>% B! i- V0 f& N! W% ~! S+ \
#include <STRING.H></FONT></P>
[! m7 G7 _4 Z<P><FONT color=#0000ff>int main(void)
% u- W0 s. Y1 H9 |: }5 p2 W/ X{
. C0 ]* i% ^9 U5 X! Y& schar string[10]; / D. P/ M* G3 n3 |4 r- i2 |
char *str1 = "abcdefghi"; </FONT></P>; k, q" c) x' P
<P><FONT color=#0000ff>strcpy(string, str1); 3 k" u/ ^% N$ J* O
printf("%s\n", string); E2 T1 u7 J' q! ], [ b8 A2 C, [
return 0;
, }2 A5 e2 ^2 h! @: F$ P7 t}
7 v# ^: l0 | _4 _# A2 d. D- o</FONT>2 q3 v* K b. C7 l/ k3 A
' s6 t+ F/ j, u$ R) i! ]</P>7 Q! i( ~5 d: Y+ b
<P><FONT color=#ff0000>函数名: strcspn </FONT>
' n) f: F5 ~# G# M! K0 S功 能: 在串中查找第一个给定字符集内容的段 . o# l0 U" s$ h$ h$ b$ G# @$ c
用 法: int strcspn(char *str1, char *str2);
: B/ ]7 m0 o( ^% S( r! W3 r程序例: </P>
9 W( x- G; _# o5 J4 V3 u+ r& T<P><FONT color=#0000ff>#include <STDIO.H>* |' \- `+ d5 I9 J7 _
#include <STRING.H>; `/ z9 ?4 h3 X- |8 j
#include <ALLOC.H></FONT></P>" F4 E6 t, v/ N( I& z7 H [* V r2 L) [
<P><FONT color=#0000ff>int main(void) 4 t3 i# a, l/ a0 O( E
{ : G& [9 J ]( }* n- w2 [
char *string1 = "1234567890"; 2 {5 w r$ Y1 G$ }. z
char *string2 = "747DC8";
7 R% U/ A4 Q+ [1 e* rint length; </FONT></P>
' W& p3 I; h) _<P><FONT color=#0000ff>length = strcspn(string1, string2);
% k7 I$ O, P8 g6 F) i. \" f* Sprintf("Character where strings intersect is at position %d\n", length); </FONT></P>- ~+ s/ {! ?, W% K3 A. ]5 K
<P><FONT color=#0000ff>return 0; 5 F9 j! P9 ?# K8 g- Z
} ; j+ b$ z+ J; h* b, Q
</FONT>
! ~/ m/ L' J' ?; I) ?7 n' [. X+ @% @
</P>& ~1 n/ i3 F z! K9 ~2 p2 j, Z
<P><FONT color=#ff0000>函数名: strdup </FONT>% P! v/ @: {4 ?4 W& }$ N
功 能: 将串拷贝到新建的位置处 ! h6 G' U5 e/ U% J) {! W
用 法: char *strdup(char *str); % H9 C- G* h! M9 }5 ^- [. \! w
程序例: </P>
2 f1 I, A' B' L+ S; h: w; p<P><FONT color=#0000ff>#include <STDIO.H>* T0 L' O+ r9 A0 P0 P9 B& d0 ~
#include <STRING.H>
' U6 z7 l- k! s6 {9 v1 a#include <ALLOC.H></FONT></P>
6 i* U' a5 V [0 b& a<P><FONT color=#0000ff>int main(void)
" W7 f, k+ Q9 C0 x{
8 w8 _5 b" e$ H: ]7 tchar *dup_str, *string = "abcde"; </FONT></P>
) U; e: G7 K+ E% R' z3 b( D<P><FONT color=#0000ff>dup_str = strdup(string);
( f; W/ ]! O* {printf("%s\n", dup_str);
1 Q" }# r7 Z9 X( Y6 Wfree(dup_str); </FONT></P>
" L j! v0 E) b<P><FONT color=#0000ff>return 0; & r5 t% Z- B% Q q4 G
}
3 s, I- s8 [# _% G" @* r; H" {: d' n1 A' ^9 H
</FONT>0 X3 @2 b6 E' U& }( B
</P>
& H# B7 x4 c. d: I. n<P><FONT color=#ff0000>函数名: stricmp </FONT>
2 z6 ^ u) U- a功 能: 以大小写不敏感方式比较两个串 ' C) }- K( |3 I* X2 l# I' S& w! [! v
用 法: int stricmp(char *str1, char *str2);
0 w @, Y6 S" d程序例: </P>
3 W K% x1 }. a3 u* d0 @6 r<P><FONT color=#0000ff>#include <STRING.H>- U- ^: {3 e( M! @
#include <STDIO.H></FONT></P> [) o' G. Q! J* u
<P><FONT color=#0000ff>int main(void) + U) t, T1 ~0 x
{ ! }/ q U! n1 M8 r$ C
char *buf1 = "BBB", *buf2 = "bbb";
, `; z" g' K( {# m8 P( j5 |5 p; Rint ptr; </FONT></P>
4 v: E1 D9 z8 X9 s& c7 d, Q* A<P><FONT color=#0000ff>ptr = stricmp(buf2, buf1); </FONT></P>
! L" Z# u( Z$ J# M1 u( X8 d; w<P><FONT color=#0000ff>if (ptr > 0)
, o: n8 S% r( {3 G9 M& n1 B/ a7 bprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
4 O: Z6 R! p, e- O<P><FONT color=#0000ff>if (ptr < 0) + }- ~4 U7 X4 s7 [1 I# O' I
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
; q1 g+ H( R5 _, U `<P><FONT color=#0000ff>if (ptr == 0) & c% K. ?% c, G) d3 m* { h
printf("buffer 2 equals buffer 1\n"); </FONT></P>3 P- S* D6 |: ~0 }) M5 e
<P><FONT color=#0000ff>return 0;
9 o" m: R1 Z, B! ~6 e} . k8 { g* q: V4 I" j
</FONT>3 r0 l1 d1 D! ?* _
</P>& k" t" {# ^. C* h( X
<P><FONT color=#ff0000>函数名: strerror </FONT>
1 R3 x6 \6 j6 ?" n% M9 d! }功 能: 返回指向错误信息字符串的指针
( h: i7 b f# w: u用 法: char *strerror(int errnum);
8 |' d$ o0 E8 |* F* k# l# M程序例: </P>
& i; y1 t% o3 }# M, o [" o8 q<P><FONT color=#0000ff>#include <STDIO.H>0 X" e. u4 [1 e
#include <ERRNO.H></FONT></P>, q# G. R. l& d! ]
<P><FONT color=#0000ff>int main(void) * d" Z+ ] u* A$ E2 n
{
1 F9 }" p! Z4 A1 p) l5 mchar *buffer; 5 }2 H; \: P7 f x
buffer = strerror(errno); 8 f, L* b& J0 z' S; m' x2 C
printf("Error: %s\n", buffer); / P- h3 x/ }& r8 Q. S% @& K
return 0; 9 n0 J/ X$ _$ g; J
} v3 `8 X2 N; z2 s- e: M3 p
1 K+ J9 h3 u$ |
</FONT> W* w5 O( L* x: F2 e, n8 P
</P>
& b1 P V+ w6 V, o" q& S% _% E) A<P><FONT color=#ff0000>函数名: strcmpi </FONT>2 P! S" y; h# O3 ~' ^+ u1 B1 i7 S: C
功 能: 将一个串与另一个比较, 不管大小写 + ]8 s$ ?* L$ c/ D1 i8 F, C% m% e
用 法: int strcmpi(char *str1, char *str2); ^; _, o4 b# w3 `# f
程序例: </P>- N, K/ C/ [& v {* q
<P><FONT color=#0000ff>#include <STRING.H>( X: w& w, Y3 x; R# ?( f
#include <STDIO.H></FONT></P>$ M+ u2 O3 R2 e5 V1 ?3 P
<P><FONT color=#0000ff>int main(void)
+ q/ P. h) _- P* L* d( j{
5 T: D6 [/ ~" s1 `$ achar *buf1 = "BBB", *buf2 = "bbb"; # K+ V+ {4 k0 E% v# V
int ptr; </FONT></P>7 N9 h x. `# v5 ]% n- ~
<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>+ e* Z% a% Q. B! t; h R. |
<P><FONT color=#0000ff>if (ptr > 0) ! Y- A/ y3 j9 i8 K [/ u! H
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>8 Y9 i7 D$ `( L+ j& g8 |4 N
<P><FONT color=#0000ff>if (ptr < 0) 7 k! f2 d0 H& q7 ]( s- A& q8 I
printf("buffer 2 is less than buffer 1\n"); </FONT></P>" p4 F) H/ i& \2 U( f1 Q2 {* f
<P><FONT color=#0000ff>if (ptr == 0) 0 h6 ? d2 ?) S9 ]: c4 ~/ [0 G
printf("buffer 2 equals buffer 1\n"); </FONT></P>; ]1 Q$ y# _# p8 o
<P><FONT color=#0000ff>return 0;
' H5 ^8 x1 T5 h5 w4 H/ L7 ?} , Q4 A9 Q. N1 w/ s' R( o& V
</FONT>
7 ?% f* F* h9 q, ]% W, q G) U; E. ^5 \
</P>
7 I. o7 l0 C& v+ `" ^+ W1 L) l, d: c/ R<P><FONT color=#ff0000>函数名: strncmp </FONT>
) ?& x2 w! x# R: W8 u, @; N( `, r- I功 能: 串比较
) ]6 _' j' y: |: y$ q0 Y, Y用 法: int strncmp(char *str1, char *str2, int maxlen); " B4 U3 x9 e( M# k- J
程序例: </P>
: X$ H4 J X$ ^% U- j9 A5 S<P><FONT color=#0000ff>#include <STRING.H>2 @% u; x: V2 {8 e$ |' _
#include <STDIO.H></FONT></P>5 E# W$ l3 e$ A u) n
<P><FONT color=#0000ff>int main(void) </FONT></P>
+ H6 T' z* y" L% I9 |5 O<P><FONT color=#0000ff>{
* a, \+ s5 W! m) u5 K; s- \- u1 z' {char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; ! Y% J4 H8 k% ]$ w
int ptr; </FONT></P>
$ i. u0 z( Y; } s; a0 O. r7 p<P><FONT color=#0000ff>ptr = strncmp(buf2,buf1,3);
' J) ^+ j& Y1 @' E" oif (ptr > 0)
4 t Z) n( U) u+ {! _, v/ h) Sprintf("buffer 2 is greater than buffer 1\n");
" Q8 z9 e: y* celse
/ x( D+ u/ ~; b# x9 z- Q6 cprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
9 g* M! g: Q. @1 F. j: e<P><FONT color=#0000ff>ptr = strncmp(buf2,buf3,3); 6 v5 ]- f+ x& h
if (ptr > 0)
( [! s3 [% E9 n1 j9 Gprintf("buffer 2 is greater than buffer 3\n"); 2 M% N# {) _4 k* w7 k7 R
else 5 l7 {" Y- M7 l/ L
printf("buffer 2 is less than buffer 3\n"); </FONT></P>
: w! p3 [! h' q" h* Z6 k<P><FONT color=#0000ff>return(0); ) g# Z6 @+ v& Z9 a& c
}
! m% P1 Z0 k. U" C! h0 Q# @+ @( w/ t5 @ i
</FONT></P>1 h: O% N$ }; J- z1 m+ [ w
<P><FONT color=#ff0000>函数名: strncmpi </FONT>% p; o8 v4 I8 {% N
功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
! O" _9 w( i( t' e用 法: int strncmpi(char *str1, char *str2); ; |( n; ~: r# i& U7 d R
程序例: </P>& {& o x4 Q) ?) [" g1 U
<P><FONT color=#0000ff>#include <STRING.H>
4 H. d; R( D1 q& d# e# h4 u#include <STDIO.H></FONT></P>
" d+ `3 x, |' L2 H1 n9 U<P><FONT color=#0000ff>int main(void)
+ x7 X! v q9 {# z{ . e! p; Q6 O! I1 ~' c- @
char *buf1 = "BBBccc", *buf2 = "bbbccc"; $ e2 {' \' I5 O, a I
int ptr; </FONT></P>
4 u1 N' _! y/ Q ?" s- G3 t<P><FONT color=#0000ff>ptr = strncmpi(buf2,buf1,3); </FONT></P>
. h6 D# i6 `: M: ] G# G# j( O<P><FONT color=#0000ff>if (ptr > 0)
8 V0 W o. T) h7 |! Vprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
9 D( d: v! e' S3 M8 C* a<P><FONT color=#0000ff>if (ptr < 0)
9 U% D7 O2 e2 F, q' [printf("buffer 2 is less than buffer 1\n"); </FONT></P>
& v" X) `# E& T4 x, O+ a. F5 k7 p- E<P><FONT color=#0000ff>if (ptr == 0) ' `" Z/ r& W6 `8 o/ c
printf("buffer 2 equals buffer 1\n"); </FONT></P>
- \/ ^0 E) j$ `- v2 g<P><FONT color=#0000ff>return 0; </FONT>
2 ?/ D3 P% H7 v6 K W- I}
( J2 j: l K/ H+ A
# G+ ~& r, c& q9 d( Y0 W B4 n</P>- U& E7 O# y1 @1 ^
<P><FONT color=#ff0000>函数名: strncpy </FONT>
. s8 I: g! f7 ~% o功 能: 串拷贝
% L8 n7 `, n' J+ N' T用 法: char *strncpy(char *destin, char *source, int maxlen);
- v2 c- d# @, u5 T程序例: </P>
9 h$ B' v7 e: Y; G0 i+ V- F8 |<P><FONT color=#0000ff>#include <STDIO.H>
8 }6 N/ k0 {9 p) |#include <STRING.H></FONT></P>: f, _' ~& o( z: W3 O' _
<P><FONT color=#0000ff>int main(void) ! T+ W$ m) ]7 c- p. B' J
{
0 N8 u: Z1 _+ P( \# Y' |char string[10];
* m1 l% a3 M) X/ ]+ {1 g6 Pchar *str1 = "abcdefghi"; </FONT></P>
3 t O3 k& S9 v7 A( ~& }<P><FONT color=#0000ff>strncpy(string, str1, 3); & o" l7 w0 g' T$ d" g3 w
string[3] = '\0'; ' K: r! s8 G$ Z" T7 Z8 ?/ X) ^
printf("%s\n", string);
' {7 b+ d1 i" X: Z x9 Ireturn 0; 2 C9 H0 e b# t1 n6 K7 \ L
} 2 R" Y; `' L+ @7 }! F
</FONT>
# z& F+ l5 H. U3 f$ e</P>
9 C- d8 s& r( V8 i9 Y" O<P><FONT color=#ff0000>函数名: strnicmp </FONT>
- V' n! y3 s) C* E4 Q功 能: 不注重大小写地比较两个串
& C. @, H, ?1 D0 ]- k" O& q用 法: int strnicmp(char *str1, char *str2, unsigned maxlen); 4 n+ N, m2 H0 ]
程序例: </P>
* k! i6 e. M) A: P+ c6 s' J% y$ C<P><FONT color=#0000ff>#include <STRING.H>
0 ]2 ]% z* Y: l* I' f#include <STDIO.H></FONT></P>
4 I% g' [# Z( p8 B& W<P><FONT color=#0000ff>int main(void) q1 |3 o; K. {/ i
{
# h. Z! X+ s- f& M% P4 I+ c" }char *buf1 = "BBBccc", *buf2 = "bbbccc"; & U* d R. W% ~ `
int ptr; </FONT></P>! g3 z$ Y/ w& |5 S2 x
<P><FONT color=#0000ff>ptr = strnicmp(buf2, buf1, 3); </FONT></P>
. o: j& O. n. Q/ o% ]<P><FONT color=#0000ff>if (ptr > 0)
! E* N- Z2 {# P+ bprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
2 ^8 }" y7 r$ c, g; S3 l<P><FONT color=#0000ff>if (ptr < 0) 0 g0 Z5 \5 `6 _$ _2 q) q; |
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
8 N6 ?& r% t& P [, h' d<P><FONT color=#0000ff>if (ptr == 0)
( U9 i# o/ A, ]& s ]5 ?printf("buffer 2 equals buffer 1\n"); </FONT></P>% V: }5 l& @2 d/ m- O
<P><FONT color=#0000ff>return 0; / G9 H5 @% \" ?: L' R6 i8 E( G
} 7 ] r4 ]. e. i
& J. q8 H1 R, F' T u
</FONT>
! A- K3 q: [. }. ]( C+ Z$ B1 h</P>
/ f6 U, p, }/ K& `. |<P><FONT color=#ff0000>函数名: strnset </FONT>
, Y. U3 J* ?* z/ y; E8 o. M功 能: 将一个串中的所有字符都设为指定字符 & |" }8 X2 ^ i8 H3 H# ]- v' a
用 法: char *strnset(char *str, char ch, unsigned n);
( W! x' L/ u8 k* S u4 r程序例: </P>
: n+ T9 n7 V: Y* h3 S<P><FONT color=#0000ff>#include <STDIO.H>
. ?: w7 q& @# f4 h4 {% |8 [6 q#include <STRING.H></FONT></P>) ?8 j0 U9 o; A
<P><FONT color=#0000ff>int main(void)
2 B4 V: b, t- E$ M# ^: h{ : [3 w. Y t4 E' P0 i; c+ j
char *string = "abcdefghijklmnopqrstuvwxyz"; + T" G# a- P" p3 R; I3 S
char letter = 'x'; </FONT></P>
; q% l6 x8 w% a3 w1 \<P><FONT color=#0000ff>printf("string before strnset: %s\n", string); 9 m! K. F! j% v+ n3 P# Q8 j
strnset(string, letter, 13);
# s4 j, _8 n2 cprintf("string after strnset: %s\n", string); </FONT></P>
0 j, A# V! N* i2 E0 V<P><FONT color=#0000ff>return 0;
- C! k$ p5 T! v1 Q% A} - ~: P( @- Q: u( X1 z
</FONT>1 i' O/ m, J; d6 h! h4 u+ [0 z
</P>
( F: Z1 c2 X, H7 T7 g0 d( ~! u<P><FONT color=#ff0000>函数名: strpbrk </FONT>
( X }7 v# K4 }# S功 能: 在串中查找给定字符集中的字符
" b8 c; a! E9 \用 法: char *strpbrk(char *str1, char *str2); 3 H. \, u! d+ ~8 v
程序例: </P>
8 D k" K- c6 C6 N<P><FONT color=#0000ff>#include <STDIO.H>
" a4 t" u; y+ `9 a#include <STRING.H></FONT></P>
2 L9 M g$ a% h: Z1 Q1 J" h5 R( i<P><FONT color=#0000ff>int main(void)
- a" B" d/ F: I- H{
7 z# J6 p" S! H; t3 achar *string1 = "abcdefghijklmnopqrstuvwxyz"; 9 v) g" N' x1 e
char *string2 = "onm";
+ c- g/ |; n0 Kchar *ptr; </FONT></P>' ]1 n4 P M) L7 j: `
<P><FONT color=#0000ff>ptr = strpbrk(string1, string2); </FONT></P>2 J: P& i' f/ g" |- R7 E. U' J
<P><FONT color=#0000ff>if (ptr)
+ E4 P. ^7 o4 G3 `printf("strpbrk found first character: %c\n", *ptr); ' s: D. s$ x& m/ }4 h0 D
else . p) w C3 R* H; Q5 P
printf("strpbrk didn't find character in set\n"); </FONT></P>
. W% G7 X5 i( e0 A<P><FONT color=#0000ff>return 0; % N* g4 h1 o+ B: x- U3 o
}
3 W1 a, e+ Y/ w: r/ Y+ W8 H
; j* v7 o' a7 H</FONT>- c' v/ _) F$ @
</P>, H, t5 S. @5 r, }1 V( |2 I
<P><FONT color=#ff0000>函数名: strrchr </FONT>
$ q! t9 M( \: C7 L; f功 能: 在串中查找指定字符的最后一个出现 ! ~2 A" } G1 q$ P" ?$ K
用 法: char *strrchr(char *str, char c); ' _- J* j4 D ^1 U
程序例: </P>2 j8 ~$ z2 G$ o* i, B6 Z0 j/ {% `
<P><FONT color=#0000ff>#include <STRING.H>
. i* C! ]/ `5 d8 y4 V7 R1 n1 C#include <STDIO.H></FONT></P>, F! T) R; A, {3 K9 e2 Y8 {
<P><FONT color=#0000ff>int main(void) ' `0 b X$ F& Y" T( E0 t& q
{ / H# e8 e8 j1 @
char string[15];
) o2 U M) ^6 G5 g7 ~3 I4 @char *ptr, c = 'r'; </FONT></P>
0 [8 S& |/ n% h& V<P><FONT color=#0000ff>strcpy(string, "This is a string"); : ~2 [3 Z4 j/ ^) B6 w
ptr = strrchr(string, c); : _. y9 b s% `9 y6 ]
if (ptr)
2 p5 N9 y& t. J: ]7 G# E: K& l# @printf("The character %c is at position: %d\n", c, ptr-string); " T y% r) x. x& M9 \6 ?$ ~, u2 m
else 4 ^/ L6 t# R- r! ]9 N4 r
printf("The character was not found\n");
1 {. n1 g' `- N0 ]& Rreturn 0;
6 `& Q. W! L6 {} </FONT>
2 W3 p. u2 G' G3 ~- A4 o' D& u+ i! {
9 ]$ {7 B2 u7 u3 q3 T8 b( L</P>+ D4 o; C7 ]1 A9 H+ U
<P><FONT color=#ff0000>函数名: strrev </FONT>
- ?5 t9 [# t* z/ R( I8 g功 能: 串倒转 " w; C, _4 E4 z
用 法: char *strrev(char *str);
9 C" @' N# D: e' U, j程序例: </P>
- K" I: N: _2 P- E8 D<P><FONT color=#0000ff>#include <STRING.H>" ?+ C* E9 F' C, O# K
#include <STDIO.H></FONT></P>4 M0 ?# i& |- S v' y( x
<P><FONT color=#0000ff>int main(void) 8 q. i F& @# H" d9 F
{
5 m1 }' [$ f; b# c* y' Jchar *forward = "string"; </FONT></P>) D, w: b+ Y5 y Z( J
<P><FONT color=#0000ff>printf("Before strrev(): %s\n", forward);
, T' M S( D$ b% V+ A }strrev(forward);
' k0 V+ ^$ M1 ?$ ~# N7 |2 S6 {9 Uprintf("After strrev(): %s\n", forward);
! B, A; _3 a! ]0 n, u$ r N( @return 0;
( f4 T, D; E" A3 Y y0 M} </FONT>
4 X* T. r* p- N" j5 x( z</P>
- H4 G5 U1 z; ~+ U' R+ b- F<P><FONT color=#ff0000>函数名: strset </FONT>- a8 L" c. t# v2 g. T+ F$ S: ?2 ?6 o
功 能: 将一个串中的所有字符都设为指定字符
8 P' N9 s( a! v0 h! @用 法: char *strset(char *str, char c);
3 | C% Z( T5 y: D" a程序例: </P>
6 q) D o. E8 @<P><FONT color=#0000ff>#include <STDIO.H>- S: c; H/ p3 }; B# n4 T0 H; w7 E
#include <STRING.H></FONT></P>
+ P% Q7 ^7 v& N<P><FONT color=#0000ff>int main(void) ( C- V2 R* |: H3 C2 X( l2 R* b
{
$ J' w9 j8 M4 K' X) vchar string[10] = "123456789";
" l+ V; a9 v3 o* ~% |( K8 \: E9 Gchar symbol = 'c'; </FONT></P>
: ~& g n+ b+ K, t- m0 Q+ \% S<P><FONT color=#0000ff>printf("Before strset(): %s\n", string); 8 t% b& y' {- n
strset(string, symbol);
- N6 l; `6 L g& T1 k# ^" xprintf("After strset(): %s\n", string); $ b# f( [: U: `
return 0;
& f( B% M9 w1 s; ]9 d}
" k ? N& [9 ^, P: v, x1 p* j
5 b) H, b' @. x3 `1 E</FONT>
$ D* _% t" v% z9 Q1 J$ v" s</P>6 ?4 x9 g$ o, [. x4 T( ~$ W* g
<P><FONT color=#ff0000>函数名: strspn </FONT>8 L+ r+ n, w/ W4 w" t$ x
功 能: 在串中查找指定字符集的子集的第一次出现 , P$ u; t/ |' g9 [( {% e4 t
用 法: int strspn(char *str1, char *str2); ! J8 _* V3 t/ Z, v# t4 \+ u8 q
程序例: </P>
2 E4 W+ F9 n8 T9 v) C0 @0 c6 k S<P><FONT color=#0000ff>#include <STDIO.H>; T5 g1 V/ h0 ?; p2 H" J& G
#include <STRING.H>
C5 n0 b/ V8 e8 ~0 k3 E#include <ALLOC.H></FONT></P>
8 t& _ [$ a x, u<P><FONT color=#0000ff>int main(void) " u0 V a% w) w6 x) f! _& Y( B( d
{
& [0 g1 e1 B- b( c6 W- N9 o* bchar *string1 = "1234567890";
% h& U: d4 [ Pchar *string2 = "123DC8"; # I* V2 _! g) _" D* K6 u1 r
int length; </FONT></P>
2 H* z5 `5 G7 i2 I$ [$ }, C6 R<P><FONT color=#0000ff>length = strspn(string1, string2); . f$ S" g4 x2 g, f }
printf("Character where strings differ is at position %d\n", length);
! X# V' u; {0 }* _& greturn 0; 2 A( z) w0 [' ^/ y% z& \1 F8 w1 N! J
} 2 w& M ^3 ?5 d$ Z
</FONT>' Q0 ]" C7 w, \- i$ i
</P>
& B i9 h3 ]% f. h8 i% E; Z<P><FONT color=#ff0000>函数名: strstr </FONT>
- a0 _; m. M( G# Z* P* a功 能: 在串中查找指定字符串的第一次出现 5 g- N1 j I' C; e [; ]
用 法: char *strstr(char *str1, char *str2);
. U7 m. ~& F. h" i程序例: </P>. O+ b! }- {% T7 v; ?' e- f
<P><FONT color=#0000ff>#include <STDIO.H>; h' k& B! b% b u2 H7 d5 z. |2 b
#include <STRING.H></FONT></P>6 J5 h) T3 l6 a7 B
<P><FONT color=#0000ff>int main(void)
5 T( C. K. g k' e. `, _0 {% b{ , j0 ^! Q4 s, ~" ^- Z8 w% r3 T
char *str1 = "Borland International", *str2 = "nation", *ptr; </FONT></P>( o* F5 H% {2 }7 w7 G3 C
<P><FONT color=#0000ff>ptr = strstr(str1, str2);
1 K7 r. Y. E4 i0 u5 {printf("The substring is: %s\n", ptr); % D) g9 n( C4 J* R
return 0;
7 u: {4 Y' T+ W} </FONT>
. t6 C7 R) j+ g1 V$ U+ \2 C. l% }8 ~
</P>* ^9 |- |/ K0 v! r: I6 J# b% |
<P><FONT color=#ff0000>函数名: strtod </FONT>
# `+ {1 A' v; O- D" @% D6 C功 能: 将字符串转换为double型值
* u: O L1 v1 L1 F用 法: double strtod(char *str, char **endptr);
8 `1 c2 q5 d5 L4 w: I( @) M程序例: </P>4 S5 f( T& U. ]( X
<P><FONT color=#0000ff>#include <STDIO.H>
' A4 Q. g/ P w#include <STDLIB.H></FONT></P>5 e- H& t, ]/ C `
<P><FONT color=#0000ff>int main(void) 8 [ u# ?8 b' P5 Q3 j
{ * ~( ^, {" n4 S; _1 E% h# ?$ r; O
char input[80], *endptr; 6 p" ]6 j' ~" v
double value; </FONT></P>3 x" M7 n1 E7 l8 f) Q" o- q9 J/ ~, I0 H
<P><FONT color=#0000ff>printf("Enter a floating point number:"); % t, F. _4 _# H" z! ~) ^ j
gets(input);
" d6 M o9 M; h- W" g; S8 v% Wvalue = strtod(input, &endptr); 7 S1 h! z5 z1 {; o- c* I% K7 K
printf("The string is %s the number is %lf\n", input, value); ! k' M4 `# C' k, O$ G
return 0;
/ E/ ?! n3 a( ?( B}
" T7 h! e2 Z0 d, w$ o8 y</FONT>& W0 Y3 X4 n: ]. X3 e% s
* J( f4 z3 e8 A</P>
# O1 _( c, ?: [) K<P><FONT color=#ff0000>函数名: strtok </FONT>, k5 L" _; F8 k0 u
功 能: 查找由在第二个串中指定的分界符分隔开的单词 ! e, Q+ y U: u7 s e
用 法: char *strtok(char *str1, char *str2);
; i& G- S+ ?% @( F程序例: </P>
, J! ^& b* T9 {8 @7 ]; H/ [; z<P><FONT color=#0000ff>#include <STRING.H>
& X2 K7 v. b5 M. j$ ?7 W ?#include <STDIO.H></FONT></P>$ `% N6 `- n# b& j
<P><FONT color=#0000ff>int main(void) ; z# u5 x9 _- {9 d/ p/ i
{
7 g! k2 d6 ^+ z- V7 [1 Nchar input[16] = "abc,d"; : V6 R! H4 D# j0 C
char *p; </FONT></P>
1 R6 F6 d8 F& U<P><FONT color=#0000ff>/* strtok places a NULL terminator
% G0 ~. k* M# |( D; Oin front of the token, if found */
8 x$ Z) [1 f( H$ [p = strtok(input, ","); ! X" x3 |4 b; J! Z+ X( d+ A9 T
if (p) printf("%s\n", p); </FONT></P>4 q( _* Y5 E0 ]2 j7 b+ a
<P><FONT color=#0000ff>/* A second call to strtok using a NULL ; b9 e5 G: t- X% M) B1 `
as the first parameter returns a pointer
3 v# {- z1 K+ s& Cto the character following the token */ 8 t8 n$ X- N& Y
p = strtok(NULL, ",");
$ z# {: _. @2 h( F4 Kif (p) printf("%s\n", p);
2 Z) \. J! Z6 Qreturn 0; ' J* Z: G o4 S( a
}
0 `* l+ m' W6 V! ^/ U* T* b6 {7 g" Z8 o. t+ h% u+ k1 s8 ~
0 @7 ]6 b9 t4 o- V9 A9 N
</FONT></P>+ g2 a- I9 M# d; S2 l% k6 y- |# u
<P><FONT color=#ff0000>函数名: strtol </FONT>( G: w! h7 o% U3 c
功 能: 将串转换为长整数
* |. z, X2 P+ R4 o用 法: long strtol(char *str, char **endptr, int base); 9 t, ~0 \( V8 e" s9 O q
程序例: </P>
+ Q3 Q4 I% T3 ?<P><FONT color=#0000ff>#include <STDLIB.H>
5 L0 F: ?! I, ~% B2 \#include <STDIO.H></FONT></P>7 o3 e, K8 ]9 ?+ b. _
<P><FONT color=#0000ff>int main(void)
6 B) F+ C* Y# N{ - g0 Y9 s4 n$ U$ K+ e$ d Q+ P
char *string = "87654321", *endptr;
1 c4 i: F# k# P8 L; mlong lnumber; </FONT></P>. M# ?; w/ U- T8 R! m0 J
<P><FONT color=#0000ff>/* strtol converts string to long integer */ " o+ k4 ~! D' ~' i' Z- A2 I+ o# W
lnumber = strtol(string, &endptr, 10);
$ O9 [7 J- i2 i4 U4 u& jprintf("string = %s long = %ld\n", string, lnumber); </FONT></P>, o/ z0 s4 p3 V, j( {3 }5 W! \7 y
<P><FONT color=#0000ff>return 0; - g* V+ J v) t" s& Z2 M" ^
} </FONT>
$ C5 U7 P7 m. r4 y; ?% t, Q</P>9 D( H0 t* E/ U/ M" ^8 u K
<P><FONT color=#ff0000>函数名: strupr </FONT>, R5 |3 @8 Z8 o$ Y
功 能: 将串中的小写字母转换为大写字母 5 b- b& B" M& I l* N% W
用 法: char *strupr(char *str); n' B4 U; U4 w
程序例: </P>
8 x! A9 X. u9 B. }# y, H# z" Y<P><FONT color=#0000ff>#include <STDIO.H>
- X; i9 y* t4 n& ^#include <STRING.H></FONT></P>4 i' |) d& P8 s% `: I! s( W
<P><FONT color=#0000ff>int main(void)
' G& {% ]; M& Y: K$ F{
E( M7 ^8 v, g" @char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; </FONT></P>6 |1 X' _- b( P* W. J
<P><FONT color=#0000ff>/* converts string to upper case characters */
, [* n4 P8 c b. ~ptr = strupr(string); 3 |% j* w* F9 n' k: p# s/ y) Q
printf("%s\n", ptr);
$ G% h6 Z: l& qreturn 0;
8 e8 i3 f7 F' w# o* z} " a8 H" I! r3 X
9 t$ p( |6 t4 v: h. R1 t
</FONT>
( l: ]6 Z4 }7 ]* Z; R</P>- [+ {* f- y3 c
<P><FONT color=#ff0000>函数名: swab </FONT>' Q5 o9 Q+ A3 z$ C0 d! t$ _2 H4 y
功 能: 交换字节
5 @ v- v o* T- _ u s用 法: void swab (char *from, char *to, int nbytes); 2 k: x) e( M( o) V9 C
程序例: </P>
( z% H2 O: o1 F6 |" A5 Y<P><FONT color=#0000ff>#include <STDLIB.H>
* V& a7 l: b, {$ F4 `! r6 Z% m#include <STDIO.H>9 J+ B) r: v2 N+ D" z
#include <STRING.H></FONT></P>
7 x; M) G8 b U, R, I4 s<P><FONT color=#0000ff>char source[15] = "rFna koBlrna d"; # K# G) A, W# a, U2 m! G+ V
char target[15]; </FONT></P>
6 ^: N9 k3 L' D) @& B, V<P><FONT color=#0000ff>int main(void)
6 }! M% ^: l5 y{ 3 o) a+ R! e, Y+ E
swab(source, target, strlen(source));
% m0 T) t( q, W( A) Z$ G- C) V- Nprintf("This is target: %s\n", target);
9 q4 R$ g. {7 V D, d/ X8 s3 jreturn 0; 7 ^, ?, }- ~8 L( i7 r/ u
}
, _! B8 |; `$ `0 P( ]% F</FONT>. x" S C& H7 q: T/ W: T$ b2 E( `
' f0 D8 Q5 m* j ~& I8 X# Y7 T& s5 G
</P>: T8 V- x: g, U/ Z! _' p, ?
<P><FONT color=#ff0000>函数名: system </FONT>
# j% o1 R5 _0 ?* M& o0 x6 B功 能: 发出一个DOS命令 9 O! ^7 v" S: X( l; d
用 法: int system(char *command); 2 Y5 M- @) V6 N5 H
程序例: </P>
7 m( k% s( K* E$ o* H2 x) n/ \( i<P><FONT color=#0000ff>#include <STDLIB.H>2 X2 P, D. K+ E
#include <STDIO.H></FONT></P>
' M* ?3 V8 D5 H+ ]/ c<P><FONT color=#0000ff>int main(void) $ T& M" n& {* E8 g
{ 4 m& q, c; r* O
printf("About to spawn command.com and run a DOS command\n");
, e5 z+ y8 f6 ?! Csystem("dir"); 2 w5 J. a3 v2 X% C
return 0; ; w, H. [) ^" S3 \
} </FONT></P> |
zan
|