- 在线时间
- 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>
! \: o# S' ~% y6 L# K& C1 s功 能: 改变数据段空间位置
9 ~5 W! i; t- U用 法: char *sbrk(int incr); 4 I9 Q: p% `! h% U% i
程序例: </P>( H- c5 w, R* M. V6 f9 S% X. h0 L/ l
< ><FONT color=#0000ff>#include <STDIO.H>9 j: y- z% q. V6 o
#include <ALLOC.H></FONT></P> I. ]- R6 |/ }/ }. ]5 j6 C5 j) _5 ?
< ><FONT color=#0000ff>int main(void) 0 y4 F( g8 R* y0 { R0 A
{ 0 v. C# N7 i/ B0 F N0 \5 a0 i, A
printf("Changing allocation with sbrk()\n");
% S2 D' W3 \/ C: uprintf("Before sbrk() call: %lu bytes free\n",
3 K- y% d: Z# v0 d0 v(unsigned long) coreleft());
; K# u' d6 X' v$ p! |( `, ssbrk(1000); " f1 ^4 h$ z% t" S. m; u
printf(" After sbrk() call: %lu bytes free\n",
: m; j! R& M% D$ S7 @6 U(unsigned long) coreleft());
$ U3 G: R Z3 `5 O& R- d& ~ _; areturn 0; 3 n* y4 l9 O; V* K- x$ k% F
}
6 d. |- ~' r" s( T7 K/ \1 d8 R/ F4 d& h0 r+ f- H8 w
</FONT></P>
$ H1 D/ ?+ m6 P< ><FONT color=#ff0000>函数名: scanf </FONT>
! A- ~2 ?2 q& L% v% E功 能: 执行格式化输入
5 l4 X6 x# f F) M/ v用 法: int scanf(char *format[,argument,...]); 5 O9 u% F: k; d9 l
程序例: </P>7 e7 o, q) C& n" F( g9 {: x
< ><FONT color=#0000ff>#include <STDIO.H>% A5 ^1 ?; z& J
#include <CONIO.H></FONT></P>
) a3 ?& j( e* g: i0 O< ><FONT color=#0000ff>int main(void) ) V! X1 B: C2 `, V
{ 2 X$ w% }' }1 e2 ~, \
char label[20];
T3 g! V/ e2 _1 p& }3 P$ v3 Ichar name[20];
& C. E9 {+ Y0 Nint entries = 0; & c# E* ^8 V( X" t% n$ S
int loop, age; 8 ~1 l$ v0 w+ a$ k
double salary; </FONT></P>- Y3 ~3 h3 T1 b9 H x2 ~
< ><FONT color=#0000ff>struct Entry_struct 2 y. N1 H3 Z5 f$ G, G" C
{ ' O& o# S" }5 ~
char name[20]; # d+ t2 J- C* Y; s% ? R1 d
int age; " W0 j. J9 F+ b; A* |! g* H
float salary; ' U1 e* P+ A$ t. w& ?
} entry[20]; </FONT></P>
& Y# h8 n0 M& {7 z9 o7 q e< ><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */ ; \5 `' M9 F0 r# R8 t
printf("\n\nPlease enter a label for the chart: ");
! m0 i, t- ?& mscanf("%20s", label);
+ H, g! \" e& g3 pfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>& w$ Y" C, K' V& T8 ?2 H, W1 d' N
< ><FONT color=#0000ff>/* Input number of entries as an integer */ ( l7 m$ d1 ] B/ A" w
printf("How many entries will there be? (less than 20) ");
2 d- V5 R7 j8 _, ?scanf("%d", &entries);
( j" Z# L( t o/ U# `, afflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>9 y) \$ U! E* [2 v0 F: y! M
< ><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ $ b& p; m, g) m1 P. m3 ]" Y2 f
for (loop=0;loop<ENTRIES;++LOOP) 2 R7 I3 A' B# a2 S9 k* \, }+ X7 t
{ " T4 I# U# Y" _( \2 n0 u& T
printf("Entry %d\n", loop);
& I4 A# V7 Q! t* K8 \( X7 lprintf(" Name : "); - U1 i$ z7 w% s% {- Q5 h8 u5 N n
scanf("%[A-Za-z]", entry[loop].name);
/ W. A3 U3 M% v% |. |" o( sfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>2 q7 o* Z2 V8 N
< ><FONT color=#0000ff>/* input an age as an integer */
' w/ U4 R# Y9 I8 R5 \9 O$ kprintf(" Age : "); 1 j9 |( G9 S) U; B# J( y
scanf("%d", &entry[loop].age);
" s0 V o2 P( |1 D( P& J; rfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>) f% n0 D- x( g# N* {. `1 }
< ><FONT color=#0000ff>/* input a salary as a float */ ) h" {$ A0 n9 b1 O/ P6 C- Z
printf(" Salary : ");
5 ?+ `8 F- N: @ f: A! z# q6 P" [scanf("%f", &entry[loop].salary);
3 q; i% Z; \* M0 I8 W: ^fflush(stdin); /* flush the input stream in case of bad input */
! {9 h/ j% Y2 y} </FONT></P>1 ]1 k7 m8 `' n( {. E* J2 Y
< ><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */
5 {" ?* m3 ^! K6 `, H2 fprintf("\nPlease enter your name, age and salary\n");
9 p. i6 w k" z' K" cscanf("%20s %d %lf", name, &age, &salary); ; J1 F0 d H7 @+ Y/ ~/ R9 n
</FONT></P>( Z: K( }2 @2 \. C* I6 U9 w0 B
< ><FONT color=#0000ff>/* Print out the data that was input */
! \# h7 I0 @' jprintf("\n\nTable %s\n",label);
- `' U0 C! s2 Z! T! z+ mprintf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
; W) D" H& F+ [& d& kprintf("-----------------------------------------------------\n");
$ Z1 I* G; b/ ?0 g. H- h6 C% Kfor (loop=0;loop<ENTRIES;++LOOP) ; e6 w( d. p+ b; [$ a9 `# \
printf("%4d | %-20s | %5d | %15.2lf\n", & N; r$ }& k% p+ q. ^) W% [
loop + 1, 0 J2 H$ _/ U3 W6 P# t
entry[loop].name, * C( ~8 }6 |/ i! M$ n
entry[loop].age,
, y. i' e& W2 lentry[loop].salary);
( T% Q3 R* n6 o% _- C( Aprintf("-----------------------------------------------------\n");
/ M: V; v9 F1 greturn 0; / H8 G, X# [5 h. p6 u
} % U1 ? g& P% Y' X
</FONT>
! |6 Y2 n/ Y6 I- N2 z" h2 {</P># n( E2 c2 H6 [6 B5 ]
< ><FONT color=#ff0000>函数名: searchpath </FONT>
+ v: p9 j- ?3 K! n t8 R# R0 n功 能: 搜索DOS路径
2 V. `6 z' ^. d! N; w0 s用 法: char *searchpath(char *filename);
/ c& {! S2 J' \! t/ c程序例: </P>
7 b) U* b5 S9 Y: \1 v< ><FONT color=#0000ff>#include <STDIO.H>& T1 R S N# ~" Z1 P% d( U
#include <DIR.H></FONT></P>: S4 D. o$ s+ S, h0 w: o( G
< ><FONT color=#0000ff>int main(void)
9 U5 g& u8 l! B: h{ / F: q1 @* L- J
char *p; </FONT></P>$ r* ~* O( ?, k& s% X6 S2 i
< ><FONT color=#0000ff>/* Looks for TLINK and returns a pointer 3 b! Y5 @, H, K0 U# b: m/ c
to the path */
9 B; L9 H. b, Mp = searchpath("TLINK.EXE");
% x+ @5 e$ S, k m' gprintf("Search for TLINK.EXE : %s\n", p); </FONT></P>
' s! j" M& N" c< ><FONT color=#0000ff>/* Looks for non-existent file */
/ f5 {: L2 J4 J3 Y2 Vp = searchpath("NOTEXIST.FIL"); : G+ a$ M+ O/ h2 K% p: ~
printf("Search for NOTEXIST.FIL : %s\n", p); </FONT></P>
3 |+ E& G( q- x2 ?( U; t' t, \< ><FONT color=#0000ff>return 0;
! g6 V$ e4 y5 M$ D! n7 o} ! L. j' @% V! B
t6 j& u% U" C6 R# X
</FONT></P> o! ?* b& f& k) A5 `) w
< ><FONT color=#ff0000>函数名: sector </FONT>+ O6 L, T1 a$ [: d. ~
功 能: 画并填充椭圆扇区 R0 I5 P( r1 p: c
用 法: void far sector(int x, int y, int stangle, int endangle);
& }3 e" u. h# ^3 F程序例: </P>; y9 b0 _! q1 E! j$ @$ i
< ><FONT color=#0000ff>#include <GRAPHICS.H>
( F! s! e& U+ f3 v#include <STDLIB.H>2 R3 N! e/ I3 i$ {7 J' G' x! }
#include <STDIO.H>* a0 ^! V: B6 m: _3 s' g
#include <CONIO.H></FONT></P>
/ t* ~; T, V5 d$ a G' a+ Z3 w< ><FONT color=#0000ff>int main(void)
9 s# R9 z' b& p! ^& k: L{
. b; Q, N! m, E/* request auto detection */
( B; o: Z" Q1 y5 z+ nint gdriver = DETECT, gmode, errorcode; ' K* {" u+ w3 P9 v& x
int midx, midy, i; 4 P$ M7 `" {! }0 J
int stangle = 45, endangle = 135; 3 A5 c0 t& `% [& z9 k+ ~
int xrad = 100, yrad = 50; </FONT></P>
, w E9 m, m" b; [5 D< ><FONT color=#0000ff>/* initialize graphics and local variables */
P6 J; I. e V6 `6 {" a4 w( G+ ginitgraph(&gdriver, &gmode, ""); </FONT></P>
6 J Y! T/ t3 @( \- h/ u< ><FONT color=#0000ff>/* read result of initialization */ . v# {) }6 s3 _0 Z+ i
errorcode = graphresult(); 2 q" [, s7 w' O' R2 M( H
if (errorcode != grOk) /* an error occurred */
( o& A( v$ g5 `- y( x* d{ ( X' R+ ]9 E3 F* b4 L$ M0 y
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 u7 e$ F s' M0 L9 m
printf(" ress any key to halt:");
; l6 @8 t! w2 ~$ y1 [getch();
p5 O1 J$ R3 [; oexit(1); /* terminate with an error code */
; H0 h; {) E: |+ w3 z- }} </FONT></P>
* m4 {) L" I2 r- C< ><FONT color=#0000ff>midx = getmaxx() / 2;
' E2 t, K5 w* v; qmidy = getmaxy() / 2; </FONT></P>
3 V) h1 i- t9 k. d+ D. b `< ><FONT color=#0000ff>/* loop through the fill patterns */
- D' A j% h: t* \" }for (i=EMPTY_FILL; i<USER_FILL; <br i++)> { w) \7 q/ |0 {8 l2 j/ J
/* set the fill style */ - N# o* y! ]) j' V0 _: k4 {2 W
setfillstyle(i, getmaxcolor()); </FONT></P>
" Z ]* V* b& ]8 A4 F6 [+ B< ><FONT color=#0000ff>/* draw the sector slice */
0 W$ b6 ^9 S' f0 Fsector(midx, midy, stangle, endangle, xrad, yrad); </FONT></P>
% U. ?" @4 Z, M' Z< ><FONT color=#0000ff>getch(); ; V! o( q$ H- V8 Q3 h* D
} </FONT></P>' @( i+ [3 v q6 h
<P><FONT color=#0000ff>/* clean up */
; a# {: G' s, |6 @closegraph(); 9 L" [- t# y; n0 H" |& |
return 0; 2 B4 g, a1 g9 `
} - U) Q, J0 H! B8 M6 U
</FONT></P>0 E# q; ^9 {+ k& l
<P><FONT color=#ff0000>函数名: segread </FONT>
: h, ]% c' P3 \功 能: 读段寄存器值 ! C2 V# D- ]! \
用 法: void segread(struct SREGS *segtbl); . y% m2 L7 `/ d6 {
程序例: </P>
! {3 x7 `7 K" y7 ?6 y<P><FONT color=#0000ff>#include <STDIO.H>, @6 \" c% k# E7 t- ^; {; T4 Q
#include <DOS.H></FONT></P>. Y6 X. T9 N/ n3 c
<P><FONT color=#0000ff>int main(void)
9 J$ t8 J; i8 I1 T% j% e4 p' B{
0 O, Y5 ?9 S% c1 ]2 b; R; [5 \4 h% [struct SREGS segs; </FONT></P>
" Z C) I5 [% \% j- l* h* D<P><FONT color=#0000ff>segread(&segs); # P% i' m' j- W8 k9 G
printf("Current segment register settings\n\n");
6 N) J6 B; Y1 qprintf("CS: %X DS: %X\n", segs.cs, segs.ds);
3 m( U# f$ U# l9 a+ zprintf("ES: %X SS: %X\n", segs.es, segs.ss); </FONT></P>
% C9 X9 a( ~) ^' F$ u! L- T. t7 c<P><FONT color=#0000ff>return 0; ' c e5 |3 e* E h
} </FONT>1 }. n! N* G. {' @; l- Q/ C
6 J% @, _: L: ^</P>
: J, i+ _( Y3 _) D% C. {! l* }<P><FONT color=#ff0000>函数名: setactivepage </FONT>+ d% V, @) y# E. u& H# o3 F/ R
功 能: 设置图形输出活动页
$ k$ F8 K/ x7 b4 q- N& L用 法: void far setactivepage(int pagenum); 1 e* ^2 L' @/ E
程序例: </P>/ N9 @( x' `( ?. G- F, _9 x0 l
<P><FONT color=#0000ff>#include <GRAPHICS.H>; W7 z+ d( U( Q
#include <STDLIB.H>
% C3 \7 A5 Y, q |#include <STDIO.H>' _- D8 e/ V6 Z4 A. y
#include <CONIO.H></FONT></P>* m2 Q0 F6 \, x
<P><FONT color=#0000ff>int main(void) 1 r8 {* B" T, M* ^9 X/ |
{ $ m6 P1 m4 i7 M4 X8 S1 L' O
/* select a driver and mode that supports */ % q. k; m x. D3 Z
/* multiple pages. */
5 F' N. I' C1 I& |: o: E+ fint gdriver = EGA, gmode = EGAHI, errorcode; / t& l+ F$ J4 k
int x, y, ht; </FONT></P>+ o/ f1 L# Z) _3 |" Y6 W" B
<P><FONT color=#0000ff>/* initialize graphics and local variables */ & z( a' r1 x+ I
initgraph(&gdriver, &gmode, ""); </FONT></P>$ Y1 W% o T$ u( i) ~! y
<P><FONT color=#0000ff>/* read result of initialization */ ! Y# x5 K8 {4 |, d" Z4 R2 a+ v
errorcode = graphresult(); / I' g- P# l! ~* Y/ [ c
if (errorcode != grOk) /* an error occurred */ ) n7 t$ @0 D& l6 \
{ ! X: d" B* s r( z
printf("Graphics error: %s\n", grapherrormsg(errorcode));
. w$ N7 K; V: |( G4 i' K' |printf("Press any key to halt:");
) d$ R1 T$ M6 O7 n, T* c; r7 |getch();
" z- B/ m2 }/ k4 V( Vexit(1); /* terminate with an error code */
/ [, @+ _+ h& n2 g: u} </FONT></P>9 K$ Y, Q3 U! B
<P><FONT color=#0000ff>x = getmaxx() / 2; 9 P( }1 A2 x. }
y = getmaxy() / 2; 1 C7 N; |+ A2 y0 t6 P
ht = textheight("W"); </FONT></P>
+ U. W) X0 w5 x<P><FONT color=#0000ff>/* select the off screen page for drawing */
! U" _" o6 {3 N2 Rsetactivepage(1); </FONT></P>
- F4 \' W- Q" c: s* \/ Q' t5 M<P><FONT color=#0000ff>/* draw a line on page #1 */
$ e2 V$ C) f2 ?1 K8 b/ uline(0, 0, getmaxx(), getmaxy()); </FONT></P>& \8 V& J# X& g9 y0 B
<P><FONT color=#0000ff>/* output a message on page #1 */ , Z- X+ C/ W7 o$ L7 d- `
settextjustify(CENTER_TEXT, CENTER_TEXT); $ s/ s+ T$ S- f; L: }
outtextxy(x, y, "This is page #1:"); 2 u: R% y4 i! D/ U+ E
outtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
+ C- c. ~' H# N* H<P><FONT color=#0000ff>/* select drawing to page #0 */
- {7 P6 B, v6 x- U+ ?setactivepage(0); </FONT></P>7 x: A9 O% u+ R0 F8 O4 v
<P><FONT color=#0000ff>/* output a message on page #0 */
) Y0 u" z. w% Y1 vouttextxy(x, y, "This is page #0."); - d `/ X) q; X8 t3 B
outtextxy(x, y+ht, "Press any key to view page #1:");
4 V3 t5 f K) _) ^0 |getch(); </FONT></P>
6 V# v- \. R5 H( D<P><FONT color=#0000ff>/* select page #1 as the visible page */ . x6 V7 ?) s: R# Q# V$ e0 o; F
setvisualpage(1); </FONT></P>+ ~# R( o5 ?, I; J1 D9 V6 ^- t; X
<P><FONT color=#0000ff>/* clean up */ ! L, R2 S4 w) e. W5 }
getch();
- B! V: J6 {) k) lclosegraph();
* S! L% y$ \$ B& lreturn 0;
. U! x/ W# y$ T" M( X4 i}
+ U: G' e$ b! s</FONT>
" G1 a' ]. b1 W$ v* Q: x</P>
6 L c6 b3 q: i+ z5 [<P><FONT color=#ff0000>函数名: setallpallette </FONT> \' w5 {% B) i$ X
功 能: 按指定方式改变所有的调色板颜色 ! z: W) \1 V: F4 g1 B& A6 k' X; v- H
用 法: void far setallpallette(struct palette, far *pallette); 5 [$ I" f, T3 z% `- `
程序例: </P>. P6 Y! c5 ]5 r5 X: n8 t' q/ L/ i
<P><FONT color=#0000ff>#include <GRAPHICS.H>
, D& Y, q5 Y2 s#include <STDLIB.H>
9 z; m" B" W6 I#include <STDIO.H>
) y/ B. k. U3 ^#include <CONIO.H></FONT></P>
/ @6 L: N* L A4 n {<P><FONT color=#0000ff>int main(void) 2 \- \: \3 y. E: N7 \) }- d
{ 6 i& i4 H4 }9 t
/* request auto detection */
2 a: x5 n% ?/ C% C9 xint gdriver = DETECT, gmode, errorcode;
- ^+ {3 u4 h* j# g$ Gstruct palettetype pal;
6 S0 n* f, K8 r2 q& Y+ Hint color, maxcolor, ht; # R0 j7 N. Y$ m" B J& W
int y = 10;
; d: T4 n% t4 uchar msg[80]; </FONT></P>
$ [) P( C) f2 P. t$ u<P><FONT color=#0000ff>/* initialize graphics and local variables */ 9 `+ r# M/ u" J5 _/ n U
initgraph(&gdriver, &gmode, ""); </FONT></P>0 t. O9 K0 {" ?; w' R
<P><FONT color=#0000ff>/* read result of initialization */
* K' h+ A5 I8 L$ |3 j0 h& werrorcode = graphresult();
( w0 C6 Z% V9 h4 q& ^' nif (errorcode != grOk) /* an error occurred */
" l- B7 u8 J6 |9 Y5 F{
- B5 f' ?( ~( [$ Kprintf("Graphics error: %s\n", grapherrormsg(errorcode));
( l, `! W# j: V* @printf("Press any key to halt:");
& t' m' I; Y1 y. Dgetch();
, \! w! p) b' T$ `2 O0 W2 \exit(1); /* terminate with an error code */
0 g! H- D& _( `( h( M% a$ x+ o7 W} </FONT></P>
6 g% K& m5 p& N$ V! B<P><FONT color=#0000ff>maxcolor = getmaxcolor(); # h4 }, l' b- S/ o8 \' M( g( t
ht = 2 * textheight("W"); </FONT></P>
- t7 F ~: f2 B" d<P><FONT color=#0000ff>/* grab a copy of the palette */
0 L; h0 e5 U% U7 }% dgetpalette(&pal); </FONT></P>. z" h3 }) O/ r
<P><FONT color=#0000ff>/* display the default palette colors */
8 E6 k# _* ^; I; Ofor (color=1; color<=maxcolor; color++) % |& Q8 M; N9 f) q1 D: }6 A
{
, g! @- }2 r& N' I* H1 d- msetcolor(color); 7 W9 b$ i, r; ~+ G: d: k3 L7 ]
sprintf(msg, "Color: %d", color); 3 i4 n+ J) D$ X9 d0 d: F% R$ Z
outtextxy(1, y, msg);
7 R' u8 |8 C, S" ~. ry += ht; * K" x8 @+ v! B. D3 `( G( W, H
} </FONT></P>9 S$ O# L3 o6 y8 H1 q
<P><FONT color=#0000ff>/* wait for a key */
" A3 h" h8 A) K. B# Rgetch(); </FONT></P>! b0 G( n4 x+ X9 @ c
<P><FONT color=#0000ff>/* black out the colors one by one */
: ]. j5 `; e$ J* vfor (color=1; color<=maxcolor; color++) 0 p4 Q. X3 Q* z& b" `* v1 x9 @
{ & u' g/ }: b1 W+ M. L" Q
setpalette(color, BLACK);
* i" r, V8 \' a4 W5 Egetch(); , E) n, G- ? e$ D
} </FONT></P>+ W& T& [* ~" X, u2 Z
<P><FONT color=#0000ff>/* restore the palette colors */ 6 y& f* q0 z' B/ y. l5 G
setallpalette(&pal); </FONT></P>
2 @( M: Z, q9 a) i- y<P><FONT color=#0000ff>/* clean up */ . a2 E t, R9 V0 {
getch();
( j! |% r1 Y$ }1 C" N* V0 t% zclosegraph();
6 L1 t3 g( h8 t/ A8 ^( P3 q ^return 0; + l2 e5 o/ V9 S8 m
} % \( s: M; W" d4 _% x+ o* t/ C
$ j- {& ^! \6 `! ]& ]
</FONT></P>
) E+ Z3 H' [( Y0 h/ I<P><FONT color=#ff0000>函数名: setaspectratio </FONT>) P O' Y2 b& B$ Q: e
功 能: 设置图形纵横比 ' P; y3 [8 X! o8 C
用 法: void far setaspectratio(int xasp, int yasp); ) f2 i2 n( w2 L' O& `+ B+ W
程序例: </P>
1 Z. H; `; V" p9 @<P><FONT color=#0000ff>#include <GRAPHICS.H>
# W6 s5 G3 t! K% `$ m& p5 f#include <STDLIB.H> k) X$ b X) i2 ~+ U* w
#include <STDIO.H>
5 K) F+ s! {& @$ K5 K#include <CONIO.H></FONT></P>
+ i- ?6 {! e' G2 ~4 q( M5 Y<P><FONT color=#0000ff>int main(void) : { Z( d3 F5 ? [$ i
{
6 \1 U5 ~5 b. O0 n: l% K$ i1 v/* request auto detection */ 5 a5 A$ x3 O) C$ D2 X; X% M
int gdriver = DETECT, gmode, errorcode;
" k7 a5 L* T% R& oint xasp, yasp, midx, midy; </FONT></P>
8 _/ R O2 V( g<P><FONT color=#0000ff>/* initialize graphics and local variables */
+ w% R9 S7 g' P; d3 Ainitgraph(&gdriver, &gmode, ""); </FONT></P># O6 e- J( J3 `' L: i! d
<P><FONT color=#0000ff>/* read result of initialization */
" I9 O4 y# P' m: \$ t! aerrorcode = graphresult();
g# y- e8 ]* q0 w- r3 [if (errorcode != grOk) /* an error occurred */
. y1 e, D3 `* q{
7 k& D& p+ W6 j: ^printf("Graphics error: %s\n", grapherrormsg(errorcode)); $ D2 a1 |. R1 h$ M. \# J
printf("Press any key to halt:");
3 O* ~) b% H. Ngetch();
. C' U8 T. i1 w; j1 texit(1); /* terminate with an error code */
3 B9 B2 {( b" A u* G+ r} </FONT></P>/ C. t, F+ @8 ~( k8 H" M! _+ B0 l
<P><FONT color=#0000ff>midx = getmaxx() / 2; , n3 q) c/ b8 _# W4 }
midy = getmaxy() / 2; ; y* k) S. l+ j/ @ b- v
setcolor(getmaxcolor()); </FONT></P>6 L7 l6 C$ P! e9 Q; H6 R
<P><FONT color=#0000ff>/* get current aspect ratio settings */ 9 \7 @2 j9 c) b( [) ]$ w% c$ @
getaspectratio(&xasp, &yasp); </FONT></P>) ?$ e) y% Z+ r* q- U
<P><FONT color=#0000ff>/* draw normal circle */ 8 p$ P( y% K# C" m
circle(midx, midy, 100); , ~& M( S& K5 _3 U5 g
getch(); </FONT></P>
+ ?8 n' E! X- D; \5 Z( y<P><FONT color=#0000ff>/* claer the screen */
; ~" o, N; z; ?1 H, W) qcleardevice(); </FONT></P>
9 D! y( A: z [5 s3 y( G<P><FONT color=#0000ff>/* adjust the aspect for a wide circle */
j, E3 ^% h6 q2 R# e2 jsetaspectratio(xasp/2, yasp); " [' ]- b: x) {3 c- ^
circle(midx, midy, 100);
& [2 _" l7 k% D& J5 wgetch(); </FONT></P>
6 C& h3 O7 k5 H/ a- ]+ o9 f3 e; A5 k<P><FONT color=#0000ff>/* adjust the aspect for a narrow circle */ ( Q6 T2 { X) ^: _! w1 b. D
cleardevice();
Q( ^7 Z( J, N- p S) ?setaspectratio(xasp, yasp/2); 6 `- z% [" i! @' R" a0 [4 _+ d X
circle(midx, midy, 100); </FONT></P>
# [# W- Q# e% H7 d3 a2 D5 @' N<P><FONT color=#0000ff>/* clean up */ 9 _7 t# {0 ~1 N" Q4 i" ]
getch();
' K& H& u o3 V7 I6 ^: B/ n# kclosegraph();
, p4 v" V# k& b4 b8 d/ Jreturn 0;
8 _3 a# C& G$ l% h) B) y& G} , J7 T/ l- m+ z. B
</FONT>
h0 k; h& w& g' l- Z+ p, ]5 m</P> Y: _2 B3 p2 O
<P><FONT color=#ff0000>函数名: setbkcolor </FONT>2 a: ]6 @3 a7 L
功 能: 用调色板设置当前背景颜色 3 z1 y# o$ k* f) m _
用 法: void far setbkcolor(int color); / d" v9 z+ B6 V! z+ H) h
程序例: </P>' Y* B/ g' U7 h! ?% R H0 W# a
<P><FONT color=#0000ff>#include <GRAPHICS.H>. \, w! X q: P2 i8 S( V( Y" n
#include <STDLIB.H>
: `) f, C. V7 V8 T8 v; Z# R( w) j#include <STDIO.H>/ @* r& G- V$ K
#include <CONIO.H></FONT></P>
0 i$ z `4 g1 S<P><FONT color=#0000ff>int main(void)
: H0 U0 ~7 v' n( C# X8 @{
. L8 N6 ^% e. j; @1 k: k/* select a driver and mode that supports */
* [/ d+ d3 a# ^% w0 q/ }2 B, k/* multiple background colors. */
; ^- A# k0 u$ u/ B8 g: Jint gdriver = EGA, gmode = EGAHI, errorcode;
/ y( q+ t. Y& z+ y2 cint bkcol, maxcolor, x, y;
# b+ w( A5 z( x: t. g& gchar msg[80]; </FONT></P>& }4 }0 |9 |9 } n; ~
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 1 i( e, F0 x% ]3 Z, l
initgraph(&gdriver, &gmode, ""); </FONT></P># P$ p. ]4 b" ]$ n
<P><FONT color=#0000ff>/* read result of initialization */ ; q) ]" G* X0 P/ s1 Y0 s% @
errorcode = graphresult(); # `; i. m& F7 c- `( B
if (errorcode != grOk) /* an error occurred */ : T/ ]4 N# h# ^
{
: |8 s+ r9 }! r2 x9 N m) |printf("Graphics error: %s\n", grapherrormsg(errorcode));
/ z7 X0 j4 ~8 Y: [- Uprintf("Press any key to halt:");
# G6 Z s7 l& t( l* ? ^getch(); ! m; c+ N% K8 G+ h, w2 L; z$ `" n
exit(1); /* terminate with an error code */
! y! b/ G9 f. a3 D( ]: D} </FONT></P>$ w2 B7 M9 v: y5 N. x
<P><FONT color=#0000ff>/* maximum color index supported */ 8 }: T5 Z2 d& m. A% l
maxcolor = getmaxcolor(); </FONT></P> ]3 T" }- i3 g. r U
<P><FONT color=#0000ff>/* for centering text messages */ `, r- l3 S/ Q( e. a
settextjustify(CENTER_TEXT, CENTER_TEXT);
5 M0 j. W& @: {& K# ?# n& @x = getmaxx() / 2;
! ~% I) [' |- j" `* L7 u1 _. ~, qy = getmaxy() / 2; </FONT></P>
$ | v' k. P6 V9 d0 n<P><FONT color=#0000ff>/* loop through the available colors */
v4 N; ]$ P5 @- e) Yfor (bkcol=0; bkcol<=maxcolor; bkcol++) 0 M4 D; f. g& M) t; F
{
% V6 a3 K \' o. R/* clear the screen */ . Z6 ?6 P, D# \5 s& y. @
cleardevice(); </FONT></P>
2 D' Q* `7 b' R) V- u1 i( ?<P><FONT color=#0000ff>/* select a new background color */ ) i2 ]$ K$ @; z! r! l$ @
setbkcolor(bkcol); </FONT></P>
) g( z6 |$ X# T3 U7 v2 s<P><FONT color=#0000ff>/* output a messsage */
( p8 O4 V4 X7 M6 W8 Fif (bkcol == WHITE)
K R- ?& ]; `% G; i$ tsetcolor(EGA_BLUE);
: I- Z% y Q w: msprintf(msg, "Background color: %d", bkcol);
# N1 S" }7 K' \# q) d+ u! |outtextxy(x, y, msg);
' ]4 @+ s0 h% ?& `( F+ O: ugetch();
# Q" o1 }- S& X* D9 D6 S& `} </FONT></P>% ?3 i1 a4 V6 P
<P><FONT color=#0000ff>/* clean up */
, o0 Z; m; X/ O3 ~& g' nclosegraph();
+ p& f3 Y3 |8 ~; H, V5 xreturn 0; 0 l3 Z" e( o# T4 i. ~8 C4 H
} </FONT>
% ^$ Q3 i# I; O$ ?8 I* _" j+ r" m& U9 ]
</P>2 D2 E, V( Q1 O2 ~' M. S& F
<P><FONT color=#ff0000>函数名: setblock </FONT>
4 G( }* `! ^4 L9 C功 能: 修改先前已分配的DOS存储段大小
& B/ ^* c+ m& v- }用 法: int setblock(int seg, int newsize);
- h7 }3 v! B6 P% z' ?% _0 W7 m程序例: </P>7 y2 b) f7 m6 X4 P6 ?0 k7 a. c
<P><FONT color=#0000ff>#include <DOS.H>
& k" E7 E: `, X1 m1 `' u#include <ALLOC.H>
, Y9 {- i& e& i# P#include <STDIO.H>
* A6 ^7 m9 o/ T& D#include <STDLIB.H></FONT></P>; d' _8 |/ d" b0 L- A& k* C
<P><FONT color=#0000ff>int main(void)
" m3 K# C* V, Y# l5 ~7 x5 _8 l{
$ x% O" z" M# R! J+ P# b Ounsigned int size, segp;
- ?# t) Q3 S3 o; z! [2 r( h5 Fint stat; </FONT></P>
; Y: Q7 t8 d, v7 F" O {<P><FONT color=#0000ff>size = 64; /* (64 x 16) = 1024 bytes */
- z) a4 f* S& lstat = allocmem(size, &segp);
' ]4 ]4 B- f" i, ~% C0 E! l5 N9 Pif (stat == -1)
' a- _8 Y) Q" H7 `printf("Allocated memory at segment: %X\n", segp); 9 Z3 ~# b1 m/ d7 K, b; }
else 3 J2 e. @; v, ]' @) D9 r" Q
{ 4 u4 }1 n* s" j1 M) m* t0 V
printf("Failed: maximum number of paragraphs available is %d\n",
& x* A0 K& s' y5 c1 Istat);
+ r" Y- z2 R0 J% xexit(1); % j6 u+ H+ p. x9 R, g5 U# h- ?
} </FONT></P>
" x+ p6 n. E E4 d! c<P><FONT color=#0000ff>stat = setblock(segp, size * 2); ) Y1 ~2 P5 N1 n" M& J1 \
if (stat == -1)
$ x p: ~# N+ \printf("Expanded memory block at segment: %X\n", segp);
, H" ^3 f% G# s i9 aelse ; c% }+ R5 |; j4 e2 H/ V( ]4 D
printf("Failed: maximum number of paragraphs available is %d\n", 8 \$ m+ W( ^. y- R( V! Z+ G
stat); </FONT></P>/ L; a+ V/ f8 B2 f" x
<P><FONT color=#0000ff>freemem(segp); </FONT></P>. Z! G* @0 g" E6 I6 a6 i. v
<P><FONT color=#0000ff>return 0;
7 f3 c- c L* p. T- N}
9 w0 k& A' A% @! q5 d) q</FONT>3 O! a: e( x, I7 y3 k( v/ _
</P>
2 E4 [) T& B# P# i: r3 A& A<P><FONT color=#ff0000>函数名: setbuf </FONT>
% `2 ^+ g, q5 k& T! L) `功 能: 把缓冲区与流相联
/ I4 c9 W7 O7 _0 J3 R* G4 Q. F用 法: void setbuf(FILE *steam, char *buf);
. y" g1 y0 B& h6 o9 w' `程序例: </P>
`" S% v" |7 V) E9 L8 t. g; Z<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>" W* D5 i3 J0 Y5 e ~1 c( O
<P><FONT color=#0000ff>/* BUFSIZ is defined in stdio.h */
# _' r3 k; T% n, Q& ~$ W# [/ Qchar outbuf[BUFSIZ]; </FONT></P>) w* p4 K; }& }; U+ K8 p' |
<P><FONT color=#0000ff>int main(void)
% b& P+ [ S- A) _' K" {, N5 a{ 3 O7 Y2 W6 \3 F; g6 m
/* attach a buffer to the standard output stream */ " }% v, I2 o' l& e
setbuf(stdout, outbuf); </FONT></P>" S7 r. V$ u+ X
<P><FONT color=#0000ff>/* put some characters into the buffer */
* ~( x" x6 S0 r/ D$ Yputs("This is a test of buffered output.\n\n"); % O) [' U9 n. Q |7 P
puts("This output will go into outbuf\n"); 5 L% W/ n. g0 ?+ E, X
puts("and won't appear until the buffer\n"); $ y3 w$ E: i- F& f7 t9 W k
puts("fills up or we flush the stream.\n"); </FONT></P>
6 C8 W! u k6 p8 }4 d' E+ L<P><FONT color=#0000ff>/* flush the output buffer */
6 }4 i6 T9 e/ y N, K1 {. e; ]" offlush(stdout); </FONT></P>
' A2 E7 }2 L& E: R- [! N7 c: M<P><FONT color=#0000ff>return 0;
R4 i! q9 P( c" m# x4 y; n5 G( W} $ s, o: p7 z0 B6 ?# J- P1 C
</FONT>$ [6 ?5 Z$ F& a$ b0 N
</P>
, y6 [8 }- ^- W9 Q: S<P><FONT color=#ff0000>函数名: setcbrk</FONT>
5 |5 N9 _- l2 M- i G: A; [功 能: 设置Control-break 7 e" o- j; N L; o, Q% Z* W
用 法: int setcbrk(int value);
* H: W+ J) R) Y6 c; I# h程序例: </P>
; O9 n, E- x( t<P><FONT color=#0000ff>#include <DOS.H>: S+ Z0 {) Y; [8 R! O; ^
#include <CONIO.H>' x7 w" P. f# |+ o4 Y6 ]. M8 P
#include <STDIO.H></FONT></P>% [" s4 L5 I, B/ ^
<P><FONT color=#0000ff>int main(void) , z% _9 W c2 i x
{
* {* ]/ @; X' V* n: Jint break_flag; </FONT></P>
2 B- m$ Q5 M8 R. k, E3 U<P><FONT color=#0000ff>printf("Enter 0 to turn control break off\n");
; v0 \1 E( B6 r2 W; \% q0 zprintf("Enter 1 to turn control break on\n"); </FONT></P>' Z, ]7 p+ o& ~3 v7 d
<P><FONT color=#0000ff>break_flag = getch() - 0; </FONT></P>1 j" ?5 X' s- P! K S
<P><FONT color=#0000ff>setcbrk(break_flag); </FONT></P>
2 I( N9 s, H! _4 p$ e<P><FONT color=#0000ff>if (getcbrk()) $ Z) r' B. @( C- F. R# y
printf("Cntrl-brk flag is on\n");
5 h5 m" V/ |9 ? [else
V( @; K7 i! s# S+ z# @7 kprintf("Cntrl-brk flag is off\n"); r, O, _2 ]' r6 G0 k r# P
return 0; ( D! }% q; F% b# Z
}
' q* v- X" Z2 \8 `/ ?! ^* i8 x
2 D6 ?$ M! x0 x+ J</FONT>. _7 ^/ u& R X2 |
</P>1 X: S! W& k, Z
<P><FONT color=#ff0000>函数名: setcolor</FONT> 3 O/ f$ L7 r0 ^, y' G
功 能: 设置当前画线颜色
/ C$ u$ n+ Z- s; b- D% @: [用 法: void far setcolor(int color); $ z0 Q2 C. `; K- \$ i( R; u
程序例: </P>& I- [) ~# {- ~
<P><FONT color=#0000ff>#include <GRAPHICS.H>. w5 \! \' p% Q1 N$ g2 Z
#include <STDLIB.H>
3 O: C2 U' l$ m: [#include <STDIO.H>8 _8 V1 o& j O* {8 K I* M$ q
#include <CONIO.H></FONT></P>6 g# j6 n: B" y& F. r9 o
<P><FONT color=#0000ff>int main(void)
; A i6 x' G' P' D- u{
O5 W" |: W( G8 x/* select a driver and mode that supports */ 2 o- x7 e0 ^3 A9 T5 k" e
/* multiple drawing colors. */
3 T, h6 w" D2 |; M# gint gdriver = EGA, gmode = EGAHI, errorcode; $ W' y- ^& n' y7 u' D, m
int color, maxcolor, x, y;
, e; Y* T5 T/ cchar msg[80]; </FONT></P>
8 b6 M3 D* D1 x4 D: Q$ P" w<P><FONT color=#0000ff>/* initialize graphics and local variables */ - M) z; o5 H( _4 [4 j
initgraph(&gdriver, &gmode, ""); </FONT></P>
! h: V k5 m% Z# a; L<P><FONT color=#0000ff>/* read result of initialization */ ! f" `- S& A7 q& ?# a* R
errorcode = graphresult();
; M) ] C% g F, k4 yif (errorcode != grOk) /* an error occurred */ ' N+ j1 o$ I$ J# `/ Y
{ : k3 e- T. S& e' p, @& q! I- N
printf("Graphics error: %s\n", grapherrormsg(errorcode)); * W) K3 F! Q( t) y1 a2 Q
printf("Press any key to halt:");
* o0 c' B O/ L/ Z* Ogetch(); ( j2 k1 v( s1 w7 O1 Q
exit(1); /* terminate with an error code */ 3 p- Z' B, s m* U8 d
} </FONT></P>
4 U1 k, g- {* Y2 k V<P><FONT color=#0000ff>/* maximum color index supported */ * H" s; G, I) k: h
maxcolor = getmaxcolor(); </FONT></P>4 Z' z. L- S% I$ c
<P><FONT color=#0000ff>/* for centering text messages */ ( \* x& g; H3 F7 J' h! `
settextjustify(CENTER_TEXT, CENTER_TEXT); % }0 i* g/ n" O& q) g
x = getmaxx() / 2;
0 S9 A3 R h' y$ ?( p9 V+ ^2 F2 py = getmaxy() / 2; </FONT></P>. a% \* G# Q) t) n3 W
<P><FONT color=#0000ff>/* loop through the available colors */
1 X3 C9 u9 K6 E3 L+ ofor (color=1; color<=maxcolor; color++) " t# L# t* G: E2 f
{ ; J1 } L- _+ Z" R! k) B
/* clear the screen */
9 b9 s: S. I" b- }& d! z* J; C1 Fcleardevice(); </FONT></P>
6 T% A; F0 V% f- ^/ ]3 B- n: S<P><FONT color=#0000ff>/* select a new background color */
! }' ^. E+ E9 u" Asetcolor(color); </FONT></P>1 t/ N# e2 v+ U. q) r8 ^9 `
<P><FONT color=#0000ff>/* output a messsage */
( i; G3 i7 ~- h7 ?/ ?sprintf(msg, "Color: %d", color); # D0 J& j$ [# e1 A
outtextxy(x, y, msg); - A' p2 h6 r" d; Q2 S0 W
getch();
. I( b c, }8 o& U/ L} </FONT></P>
8 m7 d( I: v7 D3 D<P><FONT color=#0000ff>/* clean up */ 3 C- U1 @) A3 ^6 V' B! a0 M7 Y
closegraph(); . @; G5 e: ?9 y1 x; r
return 0;
3 P; X$ E% ?2 s0 o' ?5 U* i}
- ?( U2 v6 ]! a) g</FONT>
# m+ p$ f, z/ O2 n! x5 b. C</P>5 I6 P' _4 r& }: p
<P><FONT color=#ff0000>函数名: setdate </FONT>
0 c3 t b6 s; {; M" u2 w功 能: 设置DOS日期 + X( ~+ Q7 c* n, ]5 C# G+ `7 F
用 法: void setdate(struct date *dateblk);
2 O6 n$ L% c: g/ H程序例: </P>
- K. U6 u/ [! b( r<P><FONT color=#0000ff>#include <STDIO.H>9 Y& m7 P' ]/ P' [# \! u& u
#include <PROCESS.H>0 ]$ t* z4 A$ y7 D* g1 m1 M; k
#include <DOS.H></FONT></P>
0 b6 r) [( s: \. ]' P- Y<P><FONT color=#0000ff>int main(void) . r2 K8 V+ x6 x
{
5 k1 H# b; d( [struct date reset; 9 t9 V4 z+ j. T& `% l5 W3 l/ X
struct date save_date; </FONT></P>+ Z; N. w, m# ]9 E+ ?) T! D
<P><FONT color=#0000ff>getdate(&save_date); % D6 P6 Q: H$ }2 d8 A: j/ R
printf("Original date:\n"); - ? I* W0 U$ v9 _
system("date"); </FONT></P>
+ N7 ^% d( E! ?+ k<P><FONT color=#0000ff>reset.da_year = 2001;
% y9 k ~' u, D( g; p$ ^reset.da_day = 1;
- s/ O9 I, m7 V, ^. Q; Y5 P9 yreset.da_mon = 1; " k3 e8 [' T9 m2 @
setdate(&reset); </FONT></P>
! J& V$ J0 W1 ]- X9 n" E<P><FONT color=#0000ff>printf("Date after setting:\n"); n( ~5 A- E5 ?: `0 x
system("date"); </FONT></P>
% H. ~7 {* I8 E: \8 W% J% |<P><FONT color=#0000ff>setdate(&save_date);
3 b/ |( [- b- D7 Zprintf("Back to original date:\n"); " N0 P+ q3 b7 E F" H
system("date"); </FONT></P>! {: C9 A& e* J! R0 }6 e3 g
<P><FONT color=#0000ff>return 0;
6 B1 m" n" n& z# w}
! t8 d- b' j4 F! R4 H }9 ~" L</FONT>
, D d$ q% S. E- J5 K4 D' t. ^. e</P># |4 L! \9 l E3 @: z. X
<P><FONT color=#ff0000>函数名: setdisk </FONT>
$ D) J0 d5 [9 M5 D功 能: 设置当前磁盘驱动器 & m) P" O" J1 t+ `+ o7 }3 @
用 法: int setdisk(int drive);
9 R, v% v6 v0 ?" G, r9 a8 K2 n程序例: </P>" d+ A! L+ {2 g9 M$ c/ L4 ]8 R" o
<P><FONT color=#0000ff>#include <STDIO.H>
! {6 A# l; Q6 {, L#include <DIR.H></FONT></P>
# J" B {1 \- X- O4 q* v8 |<P><FONT color=#0000ff>int main(void) : P9 n- s0 [ }2 w8 D, o
{ 4 S8 l7 M3 L7 E
int save, disk, disks; </FONT></P>& l+ |& j6 \+ K/ e9 v
<P><FONT color=#0000ff>/* save original drive */
& z' i5 P/ y5 F6 ?: A3 W4 e, U9 dsave = getdisk(); </FONT></P>: ?. ~' t7 n3 H! W
<P><FONT color=#0000ff>/* print number of logic drives */
; K: e" V8 H6 ]5 Z' B! tdisks = setdisk(save);
9 |3 r6 N' ^, Z |0 aprintf("%d logical drives on the system\n\n", disks); </FONT></P>+ b M& A N. A$ @! C% {4 w
<P><FONT color=#0000ff>/* print the drive letters available */
! X$ Z3 s8 t- a- q7 Y3 `printf("Available drives:\n");
0 L0 o9 j( L- _& Vfor (disk = 0;disk < 26;++disk)
& [$ L' ?$ k! I% m t7 F4 g{ 6 K/ ~$ C+ S0 X; ^0 {- I) F
setdisk(disk); ' S ^; T! j& Q" P
if (disk == getdisk()) 7 W$ v; [" @1 r; m
printf("%c: drive is available\n", disk + 'a'); - x7 K' D$ _! l- s* Z- ~ h
} * j" k2 |) K- n. V+ u- g
setdisk(save); </FONT></P>; f& Z- R b4 W) V/ D
<P><FONT color=#0000ff>return 0; . ^" z3 n* z5 K8 ]( j# s; ^
} </FONT>
+ d* {1 V2 \# ]/ Q. _/ `
3 ~- R$ |( n `& T4 r</P>. y; b( }) {" g9 \/ @
<P><FONT color=#ff0000>函数名: setdta </FONT>( I9 K4 Z/ V7 ~* `! L
功 能: 设置磁盘传输区地址
3 b# Q& q) }0 s用 法: void setdta(char far *dta); . B. R6 M; S9 f6 f/ H. D
程序例: </P>
( h7 q, U; q) D<P><FONT color=#0000ff>#include <PROCESS.H>
- f! N2 W! E; A#include <STRING.H>8 C( t B9 o: ~. m9 p3 o- {/ K
#include <STDIO.H>5 S w/ q @# t1 I
#include <DOS.H></FONT></P> B+ G- Y/ }2 E5 h9 T' f- r4 F' U
<P><FONT color=#0000ff>int main(void)
5 q0 p7 k- b Z6 [+ ^$ y; ?& ~{
9 U8 M9 z+ ^3 a3 ] Q% ?$ r. hchar line[80], far *save_dta;
+ V1 g8 i. `* `7 I+ X( Hchar buffer[256] = "SETDTA test!";
4 G" J& B+ N% T2 j9 [- l: nstruct fcb blk;
9 [+ b4 X# K% H% ~5 cint result; </FONT></P>
b6 F- d- r; t) h& e$ B5 H<P><FONT color=#0000ff>/* get new file name from user */ # m! U7 }0 t' W: t: [
printf("Enter a file name to create:"); % o5 M5 j" o' G* }
gets(line); </FONT></P>3 ?8 l }4 Z6 o2 r: \, i" M2 F
<P><FONT color=#0000ff>/* parse the new file name to the dta */ & J, w. d3 m' M' H7 M- ^
parsfnm(line, &blk, 1); * D5 D2 s( f. \7 U8 w) b
printf("%d %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
B' k& e! t, ?4 ^- o( D' y W( ]* d<P><FONT color=#0000ff>/* request DOS services to create file */
, x9 ~4 r3 k% C; M9 ?# Q! r Nif (bdosptr(0x16, &blk, 0) == -1) 9 O3 `( U: M. m0 W3 S
{
4 m' g8 K3 Z/ V. Hperror("Error creating file");
% i, n6 Q! a/ X7 M$ D5 \9 _exit(1); / B4 o4 J7 J* ?; Y3 J6 |8 m) s
} </FONT></P>
1 R$ ?/ t7 ^' `- o U9 J% h B<P><FONT color=#0000ff>/* save old dta and set new dta */ - g: L- s! W# t8 }
save_dta = getdta();
4 }8 }) v2 R# X- r( Csetdta(buffer); </FONT></P>
3 s& c0 ` b! R$ `& Z# ^<P><FONT color=#0000ff>/* write new records */
$ r' x3 E4 X, Dblk.fcb_recsize = 256;
$ T0 O" S5 v& R5 F k) P Cblk.fcb_random = 0L; + D M! F$ g( |" x9 C2 Q+ H
result = randbwr(&blk, 1); # U7 E; \% Z- l7 I4 o2 G1 \
printf("result = %d\n", result); </FONT></P> @% m0 c2 B, H4 E) o
<P><FONT color=#0000ff>if (!result) 9 O" ?3 B4 {' e7 x
printf("Write OK\n");
! J. Z7 R9 d; @$ }; u: qelse
- w2 Q6 O; R" z4 M{ 7 ?! b0 d; R5 O& }5 j
perror("Disk error");
( Y% x% y2 O1 Q+ yexit(1);
* I9 N' ~) p1 d} </FONT></P>
$ K& C% m; F3 f0 s<P><FONT color=#0000ff>/* request DOS services to close the file */ ; Q3 {/ g8 g) i) D: Y. e
if (bdosptr(0x10, &blk, 0) == -1) ' g1 l/ K7 X/ N# q) c
{
6 E+ u8 x* E) C7 o; G/ tperror("Error closing file"); ' }: m! \; S9 Z( ~9 ]
exit(1); 9 N+ J: a0 Y! R, G
} </FONT></P>" c0 b/ n$ m, L, t+ B
<P><FONT color=#0000ff>/* reset the old dta */ / V: @' ^2 d& o( ^9 ^6 n0 B
setdta(save_dta);
( U* c# E' p2 @/ s0 G5 Yreturn 0; 9 B; u3 Q9 z' R8 t7 ^
}
: u& f8 b+ c. E3 k: L</FONT>
; @+ j7 p6 Z* k% ~7 Y& d7 a</P>
! u* u% q8 @( ]' _2 p) N- @( f<P><FONT color=#ff0000>函数名: setfillpattern </FONT>
@- u% g# V( E( B功 能: 选择用户定义的填充模式
' `+ u2 g! k' F! N8 d. D用 法: void far setfillpattern(char far *upattern, int color);
/ c% U3 n3 I. `程序例: </P>. ~* y( d8 l6 Y. H! y- ^
<P><FONT color=#0000ff>#include <GRAPHICS.H>
+ w! X; q$ y0 Q#include <STDLIB.H>/ h5 u' v J! N5 E" Y3 K2 X! a
#include <STDIO.H>9 V6 J! u* \ M9 N
#include <CONIO.H></FONT></P>! H5 q! a$ q* N( h+ m
<P><FONT color=#0000ff>int main(void) 6 s1 C9 n1 w) D# u: x
{ - P9 E3 p( z- S' z# B$ g2 A
/* request auto detection */
7 `5 Y2 J8 ~* A/ f1 ]* ^int gdriver = DETECT, gmode, errorcode;
7 `) q6 j- Y, u0 L7 lint maxx, maxy; </FONT></P>" r- Y& z& O7 Y; @7 T
<P><FONT color=#0000ff>/* a user defined fill pattern */
0 Z0 `3 l: H2 E$ mchar pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; </FONT></P>2 @9 o0 c! p9 j# t% w8 ?
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ; D8 e, j s* u( F5 ]
initgraph(&gdriver, &gmode, ""); </FONT></P>
- M! [* a4 d7 }+ q5 ^<P><FONT color=#0000ff>/* read result of initialization */ - ^! u, k: |7 {+ j; b b. u6 @
errorcode = graphresult(); # v2 @; ? L( w- u# n
if (errorcode != grOk) /* an error occurred */
% m! V i) g" B& I$ E6 |{
, L% P' J( L; n/ H- M- Y8 L, sprintf("Graphics error: %s\n", grapherrormsg(errorcode));
4 G3 [' A. t, b' I% wprintf("Press any key to halt:"); $ B" J& U) B- a0 g
getch();
& M& K1 x* e" N6 Nexit(1); /* terminate with an error code */
$ R+ D" [! j0 R3 C, x3 r} </FONT></P>/ _; X" B* ~' g& T. K( C+ ?, e
<P><FONT color=#0000ff>maxx = getmaxx();
/ A6 {& L3 i4 |. \maxy = getmaxy();
5 d* d1 o/ ~" Asetcolor(getmaxcolor()); </FONT></P>' K& f" |* a% Y5 f3 \/ M8 m8 L
<P><FONT color=#0000ff>/* select a user defined fill pattern */ ; j. k! W& j* ~3 H# U5 }
setfillpattern(pattern, getmaxcolor()); </FONT></P>5 A' F1 z0 P3 ~3 ]$ \9 z5 R+ ?
<P><FONT color=#0000ff>/* fill the screen with the pattern */
O+ u* p+ _% F: c. [bar(0, 0, maxx, maxy); </FONT></P>$ O! X: W1 H5 [6 }7 j# v9 \7 ~
<P><FONT color=#0000ff>/* clean up */ 0 p; Q( p, C3 m' M1 a2 W% U
getch();
: Z. x0 g$ ~+ W: G& M' dclosegraph();
8 T0 L8 t# Q" A* k0 j5 zreturn 0; # U9 S! { @' v F2 P% K
} 5 L& L1 w: m+ `( O/ M$ U
</FONT>6 n, B2 a" x. b$ h
</P>
! r( n7 b" A7 j2 ?3 r- S<P><FONT color=#ff0000>函数名: setfillstyle </FONT>9 o o+ K+ q7 e1 S6 |" `
功 能: 设置填充模式和颜色
i; @: s( G6 H& G用 法: void far setfillstyle(int pattern, int color);
. B: M1 A" ]( [6 a程序例: </P>6 Q2 k0 M2 {4 I+ V6 j6 Q
<P><FONT color=#0000ff>#include <GRAPHICS.H>' B4 v$ ^# Z7 w! [# C' B7 T
#include <STDLIB.H>$ Y6 w4 N* _, Q1 p; x+ {5 I
#include <STRING.H>8 b& K! M ]7 `8 T6 h. G
#include <STDIO.H>
+ [ F2 C0 |) i#include <CONIO.H></FONT></P>
- g$ A; z( T% W- N' S- B7 H2 V, v& X<P><FONT color=#0000ff>/* the names of the fill styles supported */ 8 ?* \7 A" y) r3 }$ K: |9 y
char *fname[] = { "EMPTY_FILL",
% ]" y/ J1 B3 @" f" k6 w"SOLID_FILL", 1 Y6 d3 [/ T: a1 w% @2 o; }
"LINE_FILL", - v U& f* _1 q' N1 U3 ~# I
"LTSLASH_FILL", , g3 ^* ~, m& l& P0 e( _: I
"SLASH_FILL",
0 m+ L" t/ p0 ~# U) d/ D7 E"BKSLASH_FILL", 3 F7 d+ |) z! n" i' y9 R
"LTBKSLASH_FILL", : Y( d* q# \ A, Z5 L
"HATCH_FILL",
& b5 y9 U9 v' g; z( ~. F, b"XHATCH_FILL", 8 z2 k% R1 G: L4 _3 }
"INTERLEAVE_FILL",
9 w1 L+ [) `5 P) d3 y4 E9 `"WIDE_DOT_FILL", ) W0 V9 T2 h5 n
"CLOSE_DOT_FILL", 2 ]4 o) }; a" c% @ B
"USER_FILL"
5 n$ d8 t( s4 u* c* q3 G+ k/ O, B}; </FONT></P>1 s. K- B7 D5 b9 m |1 x
<P><FONT color=#0000ff>int main(void)
7 \3 m2 [8 i! W% G{ ' c5 z) x2 k7 S$ E
/* request auto detection */ 9 l, `" p! E& [- H' I
int gdriver = DETECT, gmode, errorcode;
: _( J) P/ A. e% [int style, midx, midy; ; V- n; D2 z3 \' ^6 K- w8 w
char stylestr[40]; </FONT></P>& F& @8 D$ E6 I! m
<P><FONT color=#0000ff>/* initialize graphics and local variables */
+ @# M5 Z7 z& oinitgraph(&gdriver, &gmode, ""); </FONT></P>2 v8 o. J/ ^6 C `' s- p
<P><FONT color=#0000ff>/* read result of initialization */ * N7 Y6 o' P) L9 R
errorcode = graphresult();
, S! _# z) t# T/ E5 D7 Y4 Zif (errorcode != grOk) /* an error occurred */ 7 g5 ~- a6 \/ k7 J: ]
{
. g V H" S6 _, D& K* y* h$ z2 Pprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ( Z4 o$ F& Y3 z0 `/ {
printf("Press any key to halt:");
. F8 g# C8 F: ?; l& u; Q% t: t egetch();
3 H9 A6 q+ @. y6 O! [exit(1); /* terminate with an error code */ / W/ y- M- d; G/ H. i1 i
} </FONT></P>% R, @4 u& |1 x
<P><FONT color=#0000ff>midx = getmaxx() / 2; 0 Y& p$ p8 Z2 J5 ]! Z4 ^
midy = getmaxy() / 2; </FONT></P>
: K7 U* y$ P5 W9 H<P><FONT color=#0000ff>for (style = EMPTY_FILL; style < USER_FILL; style++)
% N7 I7 N' c0 ?3 v* L" w3 T0 u2 d{ & G) R; `6 j. G
/* select the fill style */ 9 i) y: z6 [% J0 Z$ q' }( H
setfillstyle(style, getmaxcolor()); </FONT></P>: H+ q: e- ^0 d# V
<P><FONT color=#0000ff>/* convert style into a string */ * l6 l! X$ u( c3 x
strcpy(stylestr, fname[style]); </FONT></P># K0 ^ [- `- K) C
<P><FONT color=#0000ff>/* fill a bar */
4 V5 H$ x7 Y9 o2 x( |/ e3 kbar3d(0, 0, midx-10, midy, 0, 0); </FONT></P>; ~0 o5 u& S2 `+ {
<P><FONT color=#0000ff>/* output a message */ 0 ?, P) {: {% m' P* }& j" N/ W
outtextxy(midx, midy, stylestr); </FONT></P>
* H: b% k6 X# V( ] E. H: R. c<P><FONT color=#0000ff>/* wait for a key */
5 V9 a# m- |1 O# q: ogetch(); . B3 O x2 N( N$ |5 L
cleardevice(); 2 ~- z% G7 S% V% x
} </FONT></P>: Z$ w! U3 ~) n) s c
<P><FONT color=#0000ff>/* clean up */ $ g9 y2 A0 k3 v; c3 X% N
getch(); + k9 s! `0 Y5 ^1 c$ S/ X+ _2 V
closegraph();
% V5 C& E+ M0 f, V4 E6 {return 0;
5 c' h0 b) [+ D7 J7 H |} </FONT>6 c5 L9 ]' J' l5 t o1 a3 w
% F( G8 i) Q1 {: Y$ S9 v+ d4 Z+ \3 @
</P>7 `0 ]/ l6 T- K. |/ k+ m
<P><FONT color=#ff0000>函数名: setftime </FONT>+ R( Z+ n7 x7 E3 |% y; n
功 能: 设置文件日期和时间 4 E! N# t `) ?) g5 M
用 法: int setftime(int handle, struct ftime *ftimep);
% t% W) e+ \, o7 Y' `6 ?程序例: </P>% |- s0 f- q4 Y; j& R* z4 o" f% E
<P><FONT color=#0000ff>#include <STDIO.H>% n* T7 \. u. s* j3 B
#include <PROCESS.H>
6 A O: X: }) W2 q: U% ~#include <FCNTL.H>& R& t4 c& W1 {8 i* ~$ e! `
#include <IO.H></FONT></P>8 q2 i, g6 `8 I9 r4 V& r! h/ x
<P><FONT color=#0000ff>int main(void)
( l; G0 Y4 g4 Z0 P. z# x{
: z. b1 J: M4 H0 {; _struct ftime filet;
! l7 @: c* W, G" t# L- X( P# RFILE *fp; </FONT></P>
9 ~- \/ F% C/ Y( n<P><FONT color=#0000ff>if ((fp = fopen("TEST.$$$", "w")) == NULL)
* f9 O2 Y6 w- S5 L. Q{ 6 m) h; j" V" ?% H
perror("Error:");
; G' p% v( W$ Z" Yexit(1);
7 B k5 S }$ N! l) \2 r# ^4 Z} </FONT></P>' E" C& q* e& m
<P><FONT color=#0000ff>fprintf(fp, "testing...\n"); </FONT></P>
$ h& r5 f2 x l, T+ H$ K3 K% s<P><FONT color=#0000ff>/* load ftime structure with new time and date */ ' n: q B$ v$ `& X& z: C5 r
filet.ft_tsec = 1;
& I) x' i3 m! J* b% ^& A8 }* f; A; l! Wfilet.ft_min = 1;
F3 T9 D8 p: Q o! d+ pfilet.ft_hour = 1;
' s" H- f |! ?7 C4 f& p' ?. Bfilet.ft_day = 1;
9 j% B9 t9 w. k' ^9 p$ o" `filet.ft_month = 1; 8 ~+ T4 K0 v, x3 X
filet.ft_year = 21; </FONT></P>
" z$ U" J) h6 s; j* g8 ?<P><FONT color=#0000ff>/* show current directory for time and date */ 7 _3 \6 @2 S. V" h% Q. k5 D
system("dir TEST.$$$"); </FONT></P>* S& o7 p3 |% W4 ]
<P><FONT color=#0000ff>/* change the time and date stamp*/
1 H4 d1 B: W- ?. ?; z' Zsetftime(fileno(fp), &filet); </FONT></P>
# F& p3 p% o# A; y& O<P><FONT color=#0000ff>/* close and remove the temporary file */ ) d5 G# Z5 T1 w) y) X7 U7 A, e
fclose(fp); </FONT></P>1 H" o, M+ k# c0 f
<P><FONT color=#0000ff>system("dir TEST.$$$"); </FONT></P>
( ]8 J5 M4 k5 `; S<P><FONT color=#0000ff>unlink("TEST.$$$");
. c2 F1 ~9 T$ j0 ireturn 0; 9 r' b. E" O. {& [
} j9 U4 z* o6 E9 u' d
</FONT>. E6 l. L7 o u- b" ?' x! u
</P>
1 ]! a" `/ ^. Z% |<P><FONT color=#ff0000>函数名: setgraphbufsize </FONT>
+ x% a* `& v- Q* j" o8 R8 N功 能: 改变内部图形缓冲区的大小 , B* Y3 O T6 Z- N3 B' O$ T& r
用 法: unsigned far setgraphbufsize(unsigned bufsize);
; N. R5 m# ~2 Z1 g" ~ g; f- ? N程序例: </P> i6 E7 k) |- ~" X
<P><FONT color=#0000ff>#include <GRAPHICS.H>' t! y: F( C+ `! r7 @. U/ `
#include <STDLIB.H>) a/ r9 d; w3 A
#include <STDIO.H>
- M( _& x/ `0 g; ^: w#include <CONIO.H></FONT></P>
' h* P7 ` }+ J0 c* i/ g& i3 y<P><FONT color=#0000ff>#define BUFSIZE 1000 /* internal graphics buffer size */ </FONT></P>4 `' q/ Z. M% L/ r
<P><FONT color=#0000ff>int main(void)
5 F1 P( ^0 @ h{
6 |! U; j ~; g2 T7 A( B4 N9 X/* request auto detection */
5 Q( c8 V6 b" o' ^# [4 w# eint gdriver = DETECT, gmode, errorcode; 8 D4 } `# a* ?
int x, y, oldsize;
" L. x3 H7 ?6 f3 Ychar msg[80]; </FONT></P>
& K4 l: x3 j! D$ d+ W# Z$ z/ Y<P><FONT color=#0000ff>/* set the size of the internal graphics buffer */ & O& m9 a/ ^# z* h1 H: E
/* before making a call to initgraph. */ ( ~* a$ C2 k' l* h0 B
oldsize = setgraphbufsize(BUFSIZE); </FONT></P>
" I4 x2 J2 ~- J! d<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 z. R! |" W' l% ?: `7 L
initgraph(&gdriver, &gmode, ""); </FONT></P>
: o" W4 q3 g+ s5 x7 C0 M' M/ c! }<P><FONT color=#0000ff>/* read result of initialization */ ' g3 J1 U; ]/ d- D+ x4 p/ S7 ?( |5 ]0 r
errorcode = graphresult();
Z T$ s. M8 w5 cif (errorcode != grOk) /* an error occurred */
% E& q' @* Q& v+ c# S+ I{ & i" T1 b9 S; o
printf("Graphics error: %s\n", grapherrormsg(errorcode));
n. L2 W! e2 r) e; Aprintf("Press any key to halt:");
* [3 b2 G- o3 v6 X( |. f/ Ygetch(); # B# J% a }9 ~6 {' L3 Q7 U
exit(1); /* terminate with an error code */
& v/ P( O6 N- X& W: _} </FONT></P>
2 R3 l% d2 w6 [<P><FONT color=#0000ff>x = getmaxx() / 2;
6 j. ~4 K; s j. ?5 E/ ey = getmaxy() / 2; </FONT></P>
9 W1 Y9 v( W4 c<P><FONT color=#0000ff>/* output some messages */
3 `7 l' t5 N" I% m6 J- p# X; vsprintf(msg, "Graphics buffer size: %d", BUFSIZE); ! ~9 M5 E6 [% H( O8 x
settextjustify(CENTER_TEXT, CENTER_TEXT); : @+ ~2 X5 ]. A* n2 ]7 U
outtextxy(x, y, msg);
4 h/ d w9 O5 Ysprintf(msg, "Old graphics buffer size: %d", oldsize);
. u+ [6 E4 N6 n7 c* a5 y. Wouttextxy(x, y+textheight("W"), msg); </FONT></P>% j: j2 }5 U3 |5 [) U
<P><FONT color=#0000ff>/* clean up */ 5 M% h8 H5 H" }/ n8 U
getch();
! M% w. L% I# J" ~closegraph();
* X6 q5 B+ M2 s7 Y! Oreturn 0;
1 l1 S9 c8 \ N( p/ h# [} ' S+ y5 i4 }( R+ L) G) _% X; J6 X
/ M6 @) \5 c" N: r* w: a4 a, R2 a
% g" Y2 S6 F8 s3 R, Y
</FONT></P>" a: h9 L! n2 ^4 E+ Q$ F
<P><FONT color=#ff0000>函数名: setgraphmode </FONT>
1 X# `% v3 n) a) `9 V功 能: 将系统设置成图形模式且清屏
+ Q$ m& q g9 r# ?3 S- }用 法: void far setgraphmode(int mode);
! W, K; a: ]. G8 r5 P- T* ?程序例: 9 N1 B6 ?( r6 K5 e6 t+ [
- Q J2 `: d/ R% ~
<FONT color=#0000ff>#include <STDLIB.H>
: ]! S7 Z$ Z0 g8 Z/ _& r#include <STDIO.H>6 d- f# J) \& f: g: [7 T; x8 a1 B
#include <GRAPHICS.H>#include <CONIO.H></FONT></P>
# J3 ?* D$ r6 }9 `3 J<P><FONT color=#0000ff>int main(void)
# j% g3 q7 L6 ^# V/ `& P+ j{
8 }" i+ r6 [/ C! O/* request auto detection */
( u, Z* g$ f0 |% W7 l- k# }" f2 Dint gdriver = DETECT, gmode, errorcode;
. Z( I( @" s: w/ O4 ~, l; iint x, y; </FONT></P>6 |- k, B# S" g3 T& M0 W1 t
<P><FONT color=#0000ff>/* initialize graphics and local variables */
" v2 i$ P9 H9 _; ginitgraph(&gdriver, &gmode, ""); </FONT></P>% q' D- K5 ]% _" J- B& y* N
<P><FONT color=#0000ff>/* read result of initialization */
: H6 b, u' z9 D b+ d/ p5 Ferrorcode = graphresult();
{, X0 k% C$ I* k. U6 Gif (errorcode != grOk) /* an error occurred */
) j% i' j" Z' F' s{ ! Z' \' q! i, s. O$ C
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ( P) f; y$ O6 s5 N
printf("Press any key to halt:");
9 u* o( n* C, L0 Ygetch();
. D. Z1 p- y/ {( fexit(1); /* terminate with an error code */ 4 ~3 z: j, o) h+ y, a/ w: z* \
} </FONT></P>; u2 z; y9 U! r5 ?$ B2 L
<P><FONT color=#0000ff>x = getmaxx() / 2; 3 U4 N; e2 s { ]' Y" y$ _2 E
y = getmaxy() / 2; </FONT></P>
# w7 t! X' `2 F; t# M<P><FONT color=#0000ff>/* output a message */ $ O ^% J& @- M0 r1 ^% Z+ ?
settextjustify(CENTER_TEXT, CENTER_TEXT); 6 f9 P/ `. L# y# l3 B
outtextxy(x, y, "Press any key to exit graphics:"); 3 P' ?( O5 w8 y$ y2 C
getch(); </FONT></P>/ t( x. p/ P/ }& t$ _ S) K$ B4 ^
<P><FONT color=#0000ff>/* restore system to text mode */
; _1 ~( L0 C1 W4 j9 trestorecrtmode();
7 w; ^3 O7 Q+ ~3 I8 Zprintf("We're now in text mode.\n");
3 B0 g; d m$ [9 b) I% Q0 J5 @% l) jprintf("Press any key to return to graphics mode:");
: Z$ j! ]# S7 d/ w9 O# fgetch(); </FONT></P>
% B2 Q" _0 Q" Z5 Y<P><FONT color=#0000ff>/* return to graphics mode */
3 f l1 g- ?+ Y2 Dsetgraphmode(getgraphmode()); </FONT></P>
1 h/ Q, w! g8 K) _( \<P><FONT color=#0000ff>/* output a message */ : }. G( c2 c5 j/ k; e
settextjustify(CENTER_TEXT, CENTER_TEXT);
2 ^. w6 p, S; M0 j0 x% Louttextxy(x, y, "We're back in graphics mode.");
0 i( I8 r2 W. Pouttextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>- A. y8 s2 a5 a- O9 a* c# P
<P><FONT color=#0000ff>/* clean up */ 8 N0 u5 N1 {/ W. v
getch();
9 Y: V3 T: N9 w9 Rclosegraph();
, Y3 ^0 N3 T2 \5 d) Y& q0 Preturn 0; , e. w; O+ W# d% G
} Z5 l) U3 H( `4 [+ k, R) ~
3 _+ } X) ~0 L0 G6 n. Q* l
a J2 k" L1 k</FONT><FONT color=#ff0000></FONT></P>
o" r7 A, S/ M" U+ Q. Y' G<P><FONT color=#ff0000>函数名: setjmp </FONT>3 ?3 K3 p) U! B6 M8 O: k
功 能: 非局部转移
+ L( o/ u5 L3 ~$ F8 Z. D k, _+ ^用 法: int setjmp(jmp_buf env);
& i$ d( }, O: T程序例: </P>
. g3 l4 C- K8 E; T8 o7 `<P><FONT color=#0000ff>#include <STDIO.H>
: z9 s: y5 S- q0 ]! {#include <PROCESS.H>
4 \8 {. d, G5 o#include <SETJMP.H></FONT></P>
4 ?3 y3 }2 s# Z I9 [<P><FONT color=#0000ff>void subroutine(void); </FONT></P>% A3 ~+ e8 q7 K$ G" m! `
<P><FONT color=#0000ff>jmp_buf jumper; </FONT></P>
' v0 Y6 X! g- H% d2 Q" {<P><FONT color=#0000ff>int main(void) , b, }$ E+ m" V, v2 `
{ - j0 b/ \# g+ B9 `
int value; </FONT></P>. l1 Y2 }1 T- {* K6 W
<P><FONT color=#0000ff>value = setjmp(jumper); ! V. g( r/ G) H+ T
if (value != 0)
6 \' N7 f% }" K3 [{
1 w7 u& x& W) N# O5 U @, Tprintf("Longjmp with value %d\n", value);
# ?7 e1 d$ e3 P, f8 U/ Bexit(value);
. P4 F, W9 ^5 u5 o}
$ h% {' i( |& @' u' t" d/ i1 U* l; Bprintf("About to call subroutine ... \n"); , g* X7 D: L, ]9 O+ B; r
subroutine();
, v' g1 T6 m6 @: Q, L( Rreturn 0; + n/ G+ S5 h# a
} </FONT></P>, P# a& `5 W; T
<P><FONT color=#0000ff>void subroutine(void) , a% |5 b3 P7 Y' z
{
0 Y. N$ y1 g4 B: P* |& Dlongjmp(jumper,1);
! K6 N. W6 y) o) ]+ O5 P$ l}
j9 H! S- l) v% R* L0 v</FONT>; x- r) Q% Q S
</P>
% r' G( \. J$ \% @/ h/ y) w" O<P><FONT color=#ff0000>函数名: setlinestyle </FONT>
, _' S+ Z7 v4 |+ ~功 能: 设置当前画线宽度和类型
* w: T! D/ ?+ I1 _4 m用 法: void far setlinestyle(int linestype, unsigned upattern); ' f, e4 F; h' L; k* t, b
程序例: </P>& B& A2 w/ U( Z( _% m5 ?
<P><FONT color=#0000ff>#include <GRAPHICS.H>
$ W5 b5 U D7 i7 S+ n#include <STDLIB.H>
/ S E T/ k, w; D#include <STRING.H>2 \" H/ ^* s- V0 Q0 v( b+ l; J
#include <STDIO.H>
2 q7 H3 [1 h4 J6 D6 O#include <CONIO.H></FONT></P>6 \ j" I$ Z0 X& A' M) ~) m
<P><FONT color=#0000ff>/* the names of the line styles supported */
8 |, u v0 w1 _5 |3 c% s; l3 D4 ?char *lname[] = { % Q/ d) }2 [, B
"SOLID_LINE", ! p3 [# U% x1 z d
"DOTTED_LINE",
# x8 K4 A3 H4 q3 S- y, o; ~& a7 r"CENTER_LINE",
) F" g5 X3 B! c0 w6 o"DASHED_LINE", 4 }, a' z+ F; y# Z7 C3 ^
"USERBIT_LINE"
+ C& K3 H" H/ X" h}; </FONT></P>
: H/ D0 J/ w5 s<P><FONT color=#0000ff>int main(void) 0 K9 v: j9 ^- P& S5 ?
{
6 V0 u. K/ ^% o/* request auto detection */ ) s+ L1 C7 r1 p8 g' u9 a
int gdriver = DETECT, gmode, errorcode; </FONT></P>5 C7 c u, P. I$ n
<P><FONT color=#0000ff>int style, midx, midy, userpat; [! I6 r8 R: e
char stylestr[40]; </FONT></P>
5 W4 X- S+ c3 {; {* s5 D5 v3 G<P><FONT color=#0000ff>/* initialize graphics and local variables */ ! k/ Q x7 a/ {7 g# E
initgraph(&gdriver, &gmode, ""); </FONT></P>, Q! L+ i+ o5 C9 x9 X: ]& o
<P><FONT color=#0000ff>/* read result of initialization */
2 g3 z( s9 c0 A% P, m) [5 {, yerrorcode = graphresult(); % x: b) h) H+ R5 P
if (errorcode != grOk) /* an error occurred */
. B( P, H/ ?0 N M6 P* K0 h- f& [{
. \- B4 @: V/ R; L3 n) ?printf("Graphics error: %s\n", grapherrormsg(errorcode));
8 {# D) |/ P, N& g" |, a% {printf("Press any key to halt:"); % }! G2 |' D$ z9 {
getch(); % E0 }/ ]9 P: O2 E2 r: n! p
exit(1); /* terminate with an error code */ 7 {( G6 a' I# \/ m) \1 g8 Y& \( ^
} </FONT></P> L5 V" q" W4 t6 W* E
<P><FONT color=#0000ff>midx = getmaxx() / 2; ( Z- h9 {- d5 H2 s
midy = getmaxy() / 2; </FONT></P>/ w$ A, K& i4 F: R2 v
<P><FONT color=#0000ff>/* a user defined line pattern */ ! ?; y0 F! Q) M( k
/* binary: "0000000000000001" */ ( j, c3 |/ N1 S E0 A8 N
userpat = 1; </FONT></P>
6 R% P# {) M4 x; g<P><FONT color=#0000ff>for (style=SOLID_LINE; style<=USERBIT_LINE; style++) 7 @( ]0 v3 m3 \4 J
{ " v' E7 c! ~" u8 {% E7 r4 s6 b
/* select the line style */
: P- A" I ~+ }& z5 V# Csetlinestyle(style, userpat, 1); </FONT></P>
. J, U0 V/ T2 f' ?0 S<P><FONT color=#0000ff>/* convert style into a string */
' A3 @0 E' \& r. {. Estrcpy(stylestr, lname[style]); </FONT></P>
+ ^4 [6 e! B9 j6 u/ p; y<P><FONT color=#0000ff>/* draw a line */ 0 l! _ i4 U& [- p f
line(0, 0, midx-10, midy); </FONT></P>
% S4 s1 B/ c" g2 l9 H `) s" y<P><FONT color=#0000ff>/* draw a rectangle */ % o/ a t- j7 E. j7 V) i
rectangle(0, 0, getmaxx(), getmaxy()); </FONT></P>
6 s( W: t4 S' ]6 H$ Z6 _<P><FONT color=#0000ff>/* output a message */ 8 u* Q+ ^. r) F! u
outtextxy(midx, midy, stylestr); </FONT></P>
& X5 J+ j2 W! K: ^0 g# F5 Y0 A<P><FONT color=#0000ff>/* wait for a key */ " i8 ?, g3 m3 r Y( Y
getch();
, y2 ]$ p" P0 b9 g$ hcleardevice();
Y$ i* @# o: V9 w- v} </FONT></P>
, n7 V6 j4 ~6 [6 o- X/ G2 r7 A<P><FONT color=#0000ff>/* clean up */
! Z4 V& v6 g0 ?5 l# D+ v9 [closegraph();
- m N2 M( m$ j/ ?" l! c; Kreturn 0;
7 ^- t8 m6 `( e' z) C4 V6 n} </FONT>, P+ U# I* v5 y
, T9 w0 Q% V/ W' D/ }
# B$ i; P1 P7 c2 o. v</P>2 ?2 B$ t9 s; |# T4 ^
<P><FONT color=#ff0000>函数名: setmem </FONT>. b6 ? ?* D! W8 u. y4 s& I1 y
功 能: 存值到存储区 # r# ^( \) I# H- ]. f- m! l1 U
用 法: void setmem(void *addr, int len, char value);
5 r, k0 ^. |; r( p/ H: q程序例: </P>3 F7 X i2 a) {. h3 Q# ^
<P><FONT color=#0000ff>#include <STDIO.H>
+ Y5 Z# g0 U% H G5 J3 D$ m9 P#include <ALLOC.H>9 r( o! F- j0 Y& ]! a1 b8 c A
#include <MEM.H></FONT></P>- n' x, p" B( i' w& r s r
<P><FONT color=#0000ff>int main(void) - R+ e' v1 t; g; w0 X4 ~3 K
{ 4 t# u, k1 j$ r+ {) b( d9 ~
char *dest; </FONT></P>
& G4 M2 h; N4 j4 z5 a2 R<P><FONT color=#0000ff>dest = calloc(21, sizeof(char)); ( I4 P# W9 g# R& _; c
setmem(dest, 20, 'c'); ; x* s& K2 d. m4 D
printf("%s\n", dest); </FONT></P>- X: h, e& @8 g: q( H }$ i
<P><FONT color=#0000ff>return 0;
3 b/ x8 H7 B8 ?5 [, P) G+ ^% i' s} </FONT>9 P6 V1 J# T' {: ^, K$ D- F# A
) H5 W& \6 Q% w( w: v. E; l; y1 g- ]. ]
</P>
0 j- g V. V5 |( q. e. R7 J<P><FONT color=#ff0000>函数名: setmode </FONT>
1 n% t B/ P9 l8 m4 D: |( o功 能: 设置打开文件方式 " J" g1 O, K& \4 _! F
用 法: int setmode(int handle, unsigned mode);
) a' {: \. r8 ~ S. I程序例: </P>! q" }9 n1 \5 q! [. b
<P><FONT color=#0000ff>#include <STDIO.H>
+ E0 X' z9 x2 k9 ]9 g' \#include <FCNTL.H>% C" g. d; a, p( |. B
#include <IO.H></FONT></P>2 b+ z( c' j8 A" C
<P><FONT color=#0000ff>int main(void)
+ S! b; v% P# T{ : g' L6 s, ]9 j9 r3 x/ Q5 L6 m4 q
int result; </FONT></P>
h; ]1 _: w! [, Q1 o0 T4 m1 v<P><FONT color=#0000ff>result = setmode(fileno(stdprn), O_TEXT); $ g% Z( }3 R E. e$ G Z
if (result == -1) 3 e* M! ], Q/ m+ k! ]
perror("Mode not available\n");
0 I* `8 M+ K+ U. g+ welse 2 g8 q# e/ Z& |& G/ P" E
printf("Mode successfully switched\n"); ! D j; ]6 B; L/ E0 R0 X0 {1 C
return 0;
. t* ?( p/ z1 [}
' c8 l1 y+ `7 C
# C) S! @& u) Z7 V1 U+ Q</FONT>
2 ]6 k C2 C8 Q2 {; k</P>
9 x' N0 ?# _' r" ]1 B$ @3 J<P><FONT color=#ff0000>函数名: setpalette </FONT>
- i- J$ b8 b. a- L0 H9 X% @2 g, T5 {功 能: 改变调色板的颜色 4 G& T! L3 S) v& K' k) P& Q
用 法: void far setpalette(int index, int actural_color); / w0 ~" Q7 |; H7 J7 b+ T5 ~
程序例: </P>
' `8 \4 f* h) L" h<P><FONT color=#0000ff>#include <GRAPHICS.H>
6 e" w' U4 {. S, b4 O#include <STDLIB.H>
9 l( L) s' O& V- r4 ]#include <STDIO.H> z8 J* D: F( l: P* ]( B- i
#include <CONIO.H></FONT></P>, Q D# a5 b- \" Z5 n
<P><FONT color=#0000ff>int main(void) 9 O1 i8 F1 z' T0 H8 ^" o5 F% I
{
* |7 ]5 H& M9 y( ^ r/* request auto detection */ ) J+ o0 e( q4 O4 e( d5 p
int gdriver = DETECT, gmode, errorcode; : n" i' u$ {! k# e+ ~
int color, maxcolor, ht; b0 b1 t0 M' O- b% `0 A! ~
int y = 10;
5 W0 a" e4 s/ Q/ bchar msg[80]; </FONT></P>
( y- i+ }; g& t* @# v( M/ B0 |5 w<P><FONT color=#0000ff>/* initialize graphics and local variables */
3 F0 y9 t6 E# |3 q# v3 ~/ V) Minitgraph(&gdriver, &gmode, ""); </FONT></P>- ^# I9 S+ N6 T) U2 g+ H B
<P><FONT color=#0000ff>/* read result of initialization */ ' @, s! O! Y/ i. F" a9 c9 W6 ~% u
errorcode = graphresult();
2 v# ?. Z) `6 V5 z) ]7 |" pif (errorcode != grOk) /* an error occurred */ - ^; }# u4 f& U9 D! K
{
0 j- d8 K1 ]0 p& X/ V; eprintf("Graphics error: %s\n", grapherrormsg(errorcode));
7 h4 a, D+ g0 \* a' n8 Z9 hprintf("Press any key to halt:");
3 f0 _( p8 h3 x6 }; S, Ogetch();
; k" @" [; e* {* N+ Aexit(1); /* terminate with an error code */ ) C. x* d" W, I3 _2 e4 F5 t% o, y
} </FONT></P>/ c! I) v& d7 K
<P><FONT color=#0000ff>maxcolor = getmaxcolor();
( A/ K& Y7 W8 Z+ T( Wht = 2 * textheight("W"); </FONT></P>: X2 [* q3 l2 K" G+ h' X
<P><FONT color=#0000ff>/* display the default colors */
# u8 C4 O7 `3 g: ?- Z0 J; L: ifor (color=1; color<=maxcolor; color++) ! S; S+ o1 G" T0 i, b$ G
{
* i; @ M: \! B& T5 r8 |% N4 n6 bsetcolor(color); % r9 V! x6 N3 X( h/ z! L% L# C
sprintf(msg, "Color: %d", color); 3 R' A! u. @. u7 j* M
outtextxy(1, y, msg); 9 Y5 F- @1 H1 W3 C! f5 ~7 m7 ]
y += ht;
7 J; {& v& ?5 B- e5 e x9 O6 [} </FONT></P>! h1 B1 r) O5 q0 Y& A; Z. {
<P><FONT color=#0000ff>/* wait for a key */
/ o# _% G3 i0 m7 ]1 R& z% n# }% Pgetch(); </FONT></P># e1 b* B% U5 h# M" _' I; W$ Y# i6 [
<P><FONT color=#0000ff>/* black out the colors one by one */
0 |- I6 G% ~' \2 z: jfor (color=1; color<=maxcolor; color++)
4 G" V! g2 {8 {& F{ 3 M: F2 I* @% @' k+ y) ]" Y
setpalette(color, BLACK); 9 Q+ Z8 F8 t. ]) ]
getch();
+ x- m0 v- j; L$ s( E} </FONT></P>: i, l2 Y" f1 m; ?0 E0 d
<P><FONT color=#0000ff>/* clean up */
7 `! u( F1 f h7 l" a) t2 R( l- Aclosegraph(); 5 ~2 D) H" Q0 o. A6 `% R* r
return 0;
$ g& y; T9 ^% C8 P _1 ?. W} 0 F5 C& r! V: ]+ u& J9 d" P% v/ P
</FONT> u& a. Q8 N+ ~- x
</P>0 m4 e9 N0 g6 C% f- b
<P><FONT color=#ff0000>函数名: setrgbpalette </FONT>
3 T3 L( C0 ?* \; w功 能: 定义IBM8514图形卡的颜色 7 X! V# L6 a, S _. N7 f6 T
用 法: void far setrgbpalette(int colornum, int red, int green, int blue); ; Z$ X# r# _+ }8 S( r$ f
程序例: </P>
4 Y3 v7 F* G- \0 i9 B1 Q6 a% i<P><FONT color=#0000ff>#include <GRAPHICS.H># d, z$ R. Y2 ?2 d* W" D6 q$ k
#include <STDLIB.H>
4 ]$ S! l {( _- S) w, Q#include <STDIO.H>
8 t9 N! `, R0 d. h8 Y- j2 ]& I#include <CONIO.H></FONT></P>& f, W) {4 c6 l7 O, \) U
<P><FONT color=#0000ff>int main(void)
! \$ L; r' P' A' o$ m& \ J4 c, s{ - _/ G8 l! T, K
/* select a driver and mode that supports the use */
4 `; N6 X! m v3 f$ p/* of the setrgbpalette function. */ 5 L4 q0 d: f% G& b, G
int gdriver = VGA, gmode = VGAHI, errorcode; ( Y) i; K4 @& S! S9 V- r7 p# k. N
struct palettetype pal; ) L" X8 i4 s6 A6 r2 i" |: q
int i, ht, y, xmax; </FONT></P>8 Y7 l- G. g' [3 b# {$ m7 D% T
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 1 H0 \ O$ _- @' n4 V0 E
initgraph(&gdriver, &gmode, ""); </FONT></P>( B- r2 N& p2 b; g6 P
<P><FONT color=#0000ff>/* read result of initialization */ # h$ q. j' N! i
errorcode = graphresult(); ' I4 V8 C% I) p% c9 w
if (errorcode != grOk) /* an error occurred */
! @ I/ ?" k3 ~2 |* |' P& z{
! _0 P) C8 v5 Hprintf("Graphics error: %s\n", grapherrormsg(errorcode));
9 S" R- X M, U# Jprintf("Press any key to halt:"); 4 G9 e5 c% c* R' l
getch();
( o: `+ Z. H# Z6 B$ ~exit(1); /* terminate with an error code */
9 ~( i/ O" W3 b @0 @! u) o} </FONT></P>
% k$ R4 X# c: r! k, s- a<P><FONT color=#0000ff>/* grab a copy of the palette */
# G( Q+ {/ N3 u/ igetpalette(&pal); </FONT></P>' n2 B- E: F0 y) S$ t
<P><FONT color=#0000ff>/* create gray scale */
7 r# k0 ?+ A& i, ?for (i=0; i<PAL.SIZE; <br i++)> setrgbpalette(pal.colors, i*4, i*4, i*4); </FONT></P>
2 y9 E! T, l$ I2 L1 m5 ~<P><FONT color=#0000ff>/* display the gray scale */ % K# G. ?, N$ f8 ~( y+ n
ht = getmaxy() / 16; 3 k& C% \% D+ w) I5 J
xmax = getmaxx(); / \$ r5 V$ A `" j. n) l$ M" q4 M
y = 0;
" O1 N* c0 I. G! ? L" Q5 A9 `for (i=0; i<PAL.SIZE; <br i++)> { ; Z' H4 X0 ]+ B' G+ ?9 ~& g
setfillstyle(SOLID_FILL, i);
1 B" ^1 Q! u1 ^0 Lbar(0, y, xmax, y+ht); ' I+ r' k0 t! f# ^
y += ht; 9 P0 G- D2 o/ x- j" E
} </FONT></P>! v- ^# j, ~3 V7 ?* N1 G# J- p0 b
<P><FONT color=#0000ff>/* clean up */
+ D! F; v, _- I7 M6 Wgetch(); - S! P: Q; _) D* H! w9 @# d2 e
closegraph(); 1 R/ `" N& |2 O- g1 A$ x
return 0; 3 x6 L8 C4 D5 z7 o
} & j% s- ^6 I" c, M7 U, j
</FONT>+ P$ F/ E% Z$ q, T
[ j8 ?4 y( v& _" ]
</P>
) j, v: W0 j# c5 @<P><FONT color=#ff0000>函数名: settextjustify </FONT>
1 ]% D% {& N( A% h) g功 能: 为图形函数设置文本的对齐方式
% ]5 v! I7 `) M) [& p用 法: void far settextjustify(int horiz, int vert);
( q" z! s! b+ G3 `, L程序例: </P>0 z% b; r( c* E& {2 o3 K6 o, T! G
<P><FONT color=#0000ff>#include <GRAPHICS.H>2 J* d( J! y) m$ p8 b" n6 ]
#include <STDLIB.H>- b: q4 l' |7 E1 R" `$ e" [
#include <STDIO.H>
' o1 w% a8 V. O#include <CONIO.H></FONT></P>
! y f" S: M" `0 i2 x1 ^<P><FONT color=#0000ff>/* function prototype */
0 |# g( J& p% s' {void xat(int x, int y); </FONT></P>5 p( |3 ]& P/ v @" A P
<P><FONT color=#0000ff>/* horizontal text justification settings */
# N+ a8 C' ^, c8 |4 T% dchar *hjust[] = { "LEFT_TEXT",
/ q, Q$ T; u! Z" _9 r( ]"CENTER_TEXT",
' ^8 Q" h2 X" r f"RIGHT_TEXT"
7 J! u* L) ^+ A* `% A0 g}; </FONT></P>" z( u: T; m# x
<P><FONT color=#0000ff>/* vertical text justification settings */ ( Z4 H7 ^ c8 g
char *vjust[] = { "LEFT_TEXT",
# Y3 G4 _) h) t! @"CENTER_TEXT",
2 ~# H( B: m5 V"RIGHT_TEXT" , J3 K( J6 B6 P7 y
}; </FONT></P>! G9 L% I1 s# {+ q: V7 j6 v. f
<P><FONT color=#0000ff>int main(void)
|% _+ T% ]" E; S{ ( s. D0 ?. |7 u7 F
/* request auto detection */
& q6 k0 }' g+ e7 r2 e( Oint gdriver = DETECT, gmode, errorcode; 1 a( ~2 |, j' n3 ^1 P
int midx, midy, hj, vj;
/ C$ p9 [3 {: U2 dchar msg[80]; </FONT></P>
# |5 r" m5 H c* p<P><FONT color=#0000ff>/* initialize graphics and local variables */
4 Z- `+ W& {: Ginitgraph(&gdriver, &gmode, ""); </FONT></P>
9 [& j, @ M) m9 [! Y. `/ c$ p4 f' D2 f<P><FONT color=#0000ff>/* read result of initialization */ + ~, z# J6 y, `
errorcode = graphresult();
" p; T4 x' r5 s6 S# \0 R. rif (errorcode != grOk) /* an error occurred */ + ] p) J, R* p" k' S
{
/ D1 a) n- a1 }0 m' Q6 ?printf("Graphics error: %s\n", grapherrormsg(errorcode));
) U4 X! }$ \3 |0 }7 ^" h3 qprintf("Press any key to halt:");
: j0 F9 H5 c, K5 K s) r' kgetch(); 2 x9 W6 w5 P# `2 }
exit(1); /* terminate with an error code */
. L r8 i* L- n4 ]+ w0 j} </FONT></P>
! j- F$ x& b! i/ i+ W<P><FONT color=#0000ff>midx = getmaxx() / 2;
9 D! ~9 W6 k R J$ omidy = getmaxy() / 2; </FONT></P>! W3 O- K! [6 \6 r$ ~; R1 A
<P><FONT color=#0000ff>/* loop through text justifications */ 0 D* p* H9 d- a0 L! K
for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++)
0 w: A" S* k* ^" x) K7 G( g. I7 Ifor (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) + P6 ]# Z! i x/ P" `% V, u
{
0 x3 d6 t" s4 Q( ^! tcleardevice(); . i; k; G% r/ t: n1 @) l$ O8 I
/* set the text justification */ 6 o$ S! Y- q8 V* C' d ^* f
settextjustify(hj, vj); </FONT></P>
7 ?" W3 b% }6 a9 e<P><FONT color=#0000ff>/* create a message string */
, | y0 H N( c+ i7 dsprintf(msg, "%s %s", hjust[hj], vjust[vj]); </FONT></P>
1 R1 X7 A1 B0 ], k! @3 [<P><FONT color=#0000ff>/* create cross hairs on the screen */
) {! l8 m' j6 N5 M3 t/ O8 U2 I- Sxat(midx, midy); </FONT></P>% B5 g& G$ ~' i! m$ W2 ]
<P><FONT color=#0000ff>/* output the message */ " N0 v7 C9 x! a' R, K, N
outtextxy(midx, midy, msg);
+ _5 _3 N8 ~9 n% g# e3 P e$ Ggetch();
3 k% C1 }( m) _2 S! X} </FONT></P>- o6 S1 H0 T9 ?( K& p/ g9 Z
<P><FONT color=#0000ff>/* clean up */
3 g$ E& }1 n. W& S: {closegraph(); . X& ]' `- u6 t+ L9 _+ C
return 0; 8 K1 O" r2 \2 N/ e' o
} </FONT></P>
3 T* [, x6 c7 R0 q<P><FONT color=#0000ff>/* draw an "x" at (x, y) */ 5 J! V! c) ^9 [: y+ a
void xat(int x, int y) , K5 o" ~1 u: W j( |+ f
{ & x, U! K0 q0 C
line(x-4, y, x+4, y); 8 h3 f; @/ k$ v0 a( N' v5 ]9 L. G
line(x, y-4, x, y+4);
' ?% |8 F! H) w* @} </FONT>; f, X' c* g/ Y u) A
$ V% @4 e8 u/ F0 k
</P>
$ }8 L1 ]8 z0 g% F3 N. N6 }<P><FONT color=#ff0000>函数名: settextstyle </FONT>+ o1 ]9 x3 h# u* \' G
功 能: 为图形输出设置当前的文本属性 @" G' Q3 {4 _! v) s" f
用 法: void far settextstyle (int font, int direction, char size);
9 }( [8 C& u" f! t9 ~. h" P5 G9 F程序例: </P>
7 }. O' y9 J% g7 e. @<P><FONT color=#0000ff>#include <GRAPHICS.H>$ N9 T: e7 z! m
#include <STDLIB.H>0 k! k2 I8 t- {. ?5 m- G6 V
#include <STDIO.H>
/ s/ y( D& p. v6 G4 j2 n#include <CONIO.H></FONT></P>% R( x5 M9 N% P* U8 ]
<P><FONT color=#0000ff>/* the names of the text styles supported */
. s* _0 p& H6 uchar *fname[] = { "DEFAULT font",
0 U( H9 i) l( R" |" z"TRIPLEX font", 2 i# O. M* w- R, p1 a; f; Q
"SMALL font", 0 x) m) F. \% b# r4 u% J' T
"SANS SERIF font", 8 \5 A# f8 [3 l% P. |( B- C
"GOTHIC font"
# A: ~# q# E6 c& G m; u}; </FONT></P>
1 I7 u7 I+ E- U2 d5 M! l<P><FONT color=#0000ff>int main(void)
4 `7 P" g. ]) [ W9 u3 b2 y2 W{
: \7 D8 o, O! Z/* request auto detection */
4 V1 W* ]/ x a4 q: Hint gdriver = DETECT, gmode, errorcode;
% e- y/ @: D5 i1 f. J, ~0 _4 }9 sint style, midx, midy; 2 z* w5 ]5 l5 q; a+ O
int size = 1; </FONT></P>( Q2 U* T* g, ]/ J$ y; V
<P><FONT color=#0000ff>/* initialize graphics and local variables */ / i. Q5 V+ a, {/ ?6 W8 B
initgraph(&gdriver, &gmode, ""); </FONT></P>
& h: j& t! }3 N+ x7 o; |9 u<P><FONT color=#0000ff>/* read result of initialization */
: A; {1 g. Z( R/ u; Berrorcode = graphresult();
! X5 d+ P# \7 }0 G1 C/ ~if (errorcode != grOk) /* an error occurred */
& f% L! a- x5 @" d{
2 @4 E: `9 k% T& D* P- hprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 2 F5 V& h+ |! }- W: T
printf("Press any key to halt:");
8 q" @8 }' \( l4 K( x9 Ggetch(); $ e! P: B U2 I" v e# m D
exit(1); /* terminate with an error code */
+ v1 ?' a5 D$ |} </FONT></P> `* |- ^5 B+ k$ t! Y. @
<P><FONT color=#0000ff>midx = getmaxx() / 2; & i, B* |% a& V& H- M7 W
midy = getmaxy() / 2; </FONT></P>! _; \" v4 T8 L
<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P># B4 y+ @# z: s8 W) I& Y# N
<P><FONT color=#0000ff>/* loop through the available text styles */ / T+ A3 J) A8 K3 b" O% G. O
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
0 Z9 y% b# q/ ?& J Y1 X{ ! u. Z) Z, l& u1 A( n& \# l
cleardevice(); - n# x$ E) A1 i
if (style == TRIPLEX_FONT)
8 n* p9 p& k* a/ v0 d( [size = 4; </FONT></P>* ^# f2 Q9 i# H% L1 |) g& w
<P><FONT color=#0000ff>/* select the text style */ / Z5 K+ N% x3 T; m+ C8 c$ W
settextstyle(style, HORIZ_DIR, size); </FONT></P># r; I" B; b! ]* |* A+ I
<P><FONT color=#0000ff>/* output a message */ 9 b' l. y" X" s. x; _4 |# N" M' s6 I8 M
outtextxy(midx, midy, fname[style]);
. W& n) K; \, \, A. V9 Rgetch();
& e b" x5 G6 ~" J2 t0 X} </FONT></P>
5 ]( U8 B+ a1 d<P><FONT color=#0000ff>/* clean up */ 0 O, T$ x- W- w. r
closegraph();
7 z) T& `( [( u6 o* Vreturn 0;
8 J" V* ~9 ~: O5 x6 ]} 3 ?3 j% C @, ` w- A- \
</FONT>+ x& T5 `* K0 n. f$ i6 |
</P>
/ m) Z$ g" g2 Z& \: Y& P+ {. X<P><FONT color=#ff0000>函数名: settextstyle </FONT>
B! b; _3 t( `/ @% H% t- `, S! R. k. ^功 能: 为图形输出设置当前的文本属性 ( Y% ]; q0 t! `
用 法: void far settextstyle (int font, int direction, char size); ( H( j8 ~: o: o
程序例: </P>) k# d; z9 H3 j& x. j) x5 [
<P><FONT color=#0000ff>#include <GRAPHICS.H> F+ e+ p# e9 p/ e
#include <STDLIB.H>2 X% [9 x/ A7 Y" X. l
#include <STDIO.H>
; ~( @# K* f" h! X4 x6 \#include <CONIO.H></FONT></P>, d( i, [9 e* k; Y
<P><FONT color=#0000ff>/* the names of the text styles supported */ & v' l. {+ A* O7 f3 x
char *fname[] = { "DEFAULT font", , r4 x( C3 ?" l6 h8 x4 t5 R* j' q
"TRIPLEX font",
4 T# i- E J6 p5 {7 q# X( C8 B) I4 }"SMALL font", : M0 Z9 r/ ~) w4 F6 o: u- t8 h4 |- v
"SANS SERIF font",
5 z2 W, n$ G; q3 ?+ H"GOTHIC font" 8 q/ V- q5 U- Q9 U% j5 L
}; </FONT></P>
. p |9 ` ~3 L4 D, W* }: c<P><FONT color=#0000ff>int main(void) 7 L, S* K( B4 f% U8 y+ }4 `1 i# L3 h
{ 4 C1 u# c1 V0 J% k/ z9 A) C
/* request auto detection */
: |: ]( V0 x6 {int gdriver = DETECT, gmode, errorcode; 4 e* U* [( ~! W. s- o* F
int style, midx, midy; . R, p* H' b# m- S& D. w
int size = 1; </FONT></P>
! ~+ P6 ^& o& m) c! b<P><FONT color=#0000ff>/* initialize graphics and local variables */ ! J' k$ ^! C/ K
initgraph(&gdriver, &gmode, ""); </FONT></P>
0 S! @. x, c! u) x+ |<P><FONT color=#0000ff>/* read result of initialization */
a9 K, ^( D F' f( M1 [! aerrorcode = graphresult();
; V0 w) d" }2 Sif (errorcode != grOk) /* an error occurred */ 4 U) Z$ U& C6 {) t& f4 Z
{
* L$ P- D% _4 N/ I) S& i1 ]0 nprintf("Graphics error: %s\n", grapherrormsg(errorcode));
. t+ F3 E$ X I$ {: R) `printf("Press any key to halt:");
* H5 x/ P G6 B. |getch(); ( {9 @$ v7 S2 q1 V! ?
exit(1); /* terminate with an error code */ ! n- r/ e A/ m$ o5 v j6 F
} </FONT></P>
0 z$ o7 @# x, V<P><FONT color=#0000ff>midx = getmaxx() / 2; . U; E7 E* Y6 J" C
midy = getmaxy() / 2; </FONT></P>
! V1 K* D$ ~) m& x* f<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
% W! O3 g% q/ j: k7 w( L0 k( s* X( I<P><FONT color=#0000ff>/* loop through the available text styles */
1 T9 q: N7 e4 sfor (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
, t/ O0 J r9 s9 I1 ]9 ~* }8 z{ , F w& u0 [* F& s
cleardevice(); 4 M0 q2 T, {; i6 U9 c5 z
if (style == TRIPLEX_FONT)
7 X n3 b& }. f- e# a4 q: zsize = 4; </FONT></P>
+ S' P. y; g+ l T7 ~2 P<P><FONT color=#0000ff>/* select the text style */
1 q, b9 L, z5 O% t5 T' ]3 \+ ~9 Y: Nsettextstyle(style, HORIZ_DIR, size); </FONT></P>
/ ^% Z; ?2 v/ [, a& v$ ?<P><FONT color=#0000ff>/* output a message */
. o" J0 e% j: V, |outtextxy(midx, midy, fname[style]);
! F6 O0 n8 X; r. o, C+ `getch();
! f/ Y: c! i9 V0 _" g; l} </FONT></P>
5 ~. A' R$ O" |8 d* u<P><FONT color=#0000ff>/* clean up */
5 d! Y( ?: i- O9 Q vclosegraph(); $ D4 u5 ?* P) h
return 0; : I, R3 i- g- d
} </FONT>
8 s5 t4 d9 F7 A9 f( B% T% N7 A1 X5 W1 F& u1 i
</P>; m$ P7 ]5 f# N, l
<P><FONT color=#ff0000>函数名: settime </FONT>
" Y; y, a Y% X+ w" T功 能: 设置系统时间 0 r2 L( c& Z. |4 J
用 法: void settime(struct time *timep); 0 u* n' M- A/ h
程序例: </P>+ t* b3 ~3 b" p K
<P><FONT color=#0000ff>#include <STDIO.H>0 w$ L* I6 }# m; F
#include <DOS.H></FONT></P>
3 X% r- n$ \% Z$ x3 u9 Q<P><FONT color=#0000ff>int main(void)
% p% R+ \) `4 }" j9 L{
* W& t1 s4 M' t& N9 Jstruct time t; </FONT></P>
7 {" t0 n: O5 D<P><FONT color=#0000ff>gettime(&t); ( o8 v) t e/ N$ Y' E
printf("The current minute is: %d\n", t.ti_min);
4 `) g9 l, n% U0 hprintf("The current hour is: %d\n", t.ti_hour);
6 e! X' m2 }; ]: h# D" X: I" Xprintf("The current hundredth of a second is: %d\n", t.ti_hund);
$ k5 ]* o& z8 ?2 W- o9 uprintf("The current second is: %d\n", t.ti_sec); </FONT></P>
0 u4 v- P2 W6 c! [: D7 k<P><FONT color=#0000ff>/* Add one to the minutes struct element and then call settime */
& G3 X; H3 J, L& `t.ti_min++; * T' Z$ O3 I( w
settime(&t); </FONT></P>
' T& }- K+ u H9 P; ^8 n<P><FONT color=#0000ff>return 0; , T6 A" G2 L0 ^; b* N
} </FONT> V; T1 W( Q! \5 B7 z [0 o
Z* d- o3 h0 D* C: q3 g</P>' j9 Q, }1 \, _4 M- U8 i% \. d
<P><FONT color=#ff0000>函数名: setusercharsize </FONT>% }8 X% s+ [% k( B R2 n
功 能: 为矢量字体改变字符宽度和高度
/ h [, k0 X9 K" G8 ~用 法: void far setusercharsize(int multx, int dirx, int multy, int diry);
7 w+ f! V& x; R+ a5 C程序例: </P>
& h$ K2 H) B& G) m8 O% X<P><FONT color=#0000ff>#include <GRAPHICS.H>, d7 ?. N& N. R* T( c3 Z" B3 x
#include <STDLIB.H>
d8 c) K2 \3 u3 O#include <STDIO.H>
3 [8 N! O; ?4 b. }+ {9 \#include <CONIO.H></FONT></P>4 I: | y+ @4 V& W& i/ K
<P><FONT color=#0000ff>int main(void)
$ x. F# H6 p- Z{
) L8 H& d- E; C/* request autodetection */ $ P. Z! t2 X: {
int gdriver = DETECT, gmode, errorcode; </FONT></P>
6 G6 @+ M; a! X4 l- h" P- m, \, M- X<P><FONT color=#0000ff>/* initialize graphics and local variables */
# i* `1 ~+ d$ e) }initgraph(&gdriver, &gmode, ""); </FONT></P>% U# R2 z) y/ o4 S) X4 J" C
<P><FONT color=#0000ff>/* read result of initialization */
6 k) o; U1 y" j+ \8 v( herrorcode = graphresult(); ! @) r2 T; G* o1 H
if (errorcode != grOk) /* an error occurred */
% `3 i! p% x) [$ S3 K# L; m+ o( [{ 4 N x- X0 x: A- W
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ! L! E3 ]4 Q y* t; ?
printf("Press any key to halt:"); % V& |2 l7 j' b
getch();
- A. u2 c0 e3 `$ M5 Aexit(1); /* terminate with an error code */ + {4 L2 y6 b% H4 o$ C% w6 o
} </FONT></P>) J, d: _" Q& \! k. e- B
<P><FONT color=#0000ff>/* select a text style */ & L4 J# n' D( N& @
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); </FONT></P>
/ z" o7 ?" m' ?<P><FONT color=#0000ff>/* move to the text starting position */
' v2 e0 C, K7 z2 E0 a Z& f! e4 smoveto(0, getmaxy() / 2); </FONT></P>6 I1 T. N2 C5 [
<P><FONT color=#0000ff>/* output some normal text */ 3 Z+ c1 M8 E* e
outtext("Norm "); </FONT></P>
4 O! U$ l$ a2 x- [/ J: L5 S" b<P><FONT color=#0000ff>/* make the text 1/3 the normal width */ * t/ l, [7 G' o" Y1 K! l
setusercharsize(1, 3, 1, 1);
7 B3 E5 z9 }) I1 u" `" jouttext("Short "); </FONT></P>+ c5 i$ ]1 m* N
<P><FONT color=#0000ff>/* make the text 3 times normal width */ 1 o9 o8 X" P" o4 d
setusercharsize(3, 1, 1, 1); & A& V: |$ h ^8 q5 r7 Y6 g
outtext("Wide"); </FONT></P>
7 j' x0 L* n' y1 l7 ]. l6 P* x( ~<P><FONT color=#0000ff>/* clean up */ ! u* s) X! T. E
getch(); / {# u' @) X3 g1 s
closegraph();
1 q" x5 V. n8 `- x; Greturn 0; 1 a2 ?* i" `3 j" \
} </FONT>
' P. c9 Y' o. }! \( \% l: S# O</P>
( y$ s, M- {: ^: v<P><FONT color=#ff0000>函数名: setvbuf </FONT>
1 N) A A( t2 z功 能: 把缓冲区与流相关
; I+ z9 ?: h4 o$ l# [6 h& \: c用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
- I' @6 h# g) j. H程序例: </P>
, W/ o2 c9 i' M' K6 L( q/ Z<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
4 C9 S. G4 w6 M! `, J: `<P><FONT color=#0000ff>int main(void)
# z+ f3 n. o4 a4 _3 J{
' u3 v) J" i3 H& E) e' VFILE *input, *output; 4 ~0 n. F+ K7 x" u ^- u& w
char bufr[512]; </FONT></P>
' V" o- \6 w5 j- M$ J; u7 U9 n0 W<P><FONT color=#0000ff>input = fopen("file.in", "r+b");
; e: A. e( b/ J! A" s- Q* r, S" Foutput = fopen("file.out", "w"); </FONT></P>
: G+ @: x# i/ F3 z! f<P><FONT color=#0000ff>/* set up input stream for minimal disk access, / B1 n+ g- `) G# U& `5 }7 o
using our own character buffer */
9 A5 U* @. x) F. K2 m. a% g& \if (setvbuf(input, bufr, _IOFBF, 512) != 0)
% J1 G( r! y& Wprintf("failed to set up buffer for input file\n");
% Z$ U$ ^9 R, D/ P( r7 D) H- belse
. r0 O T0 X$ g* F8 e, @. |9 wprintf("buffer set up for input file\n"); </FONT></P>
' X5 P$ E7 Q* {; x7 S/ a1 u<P><FONT color=#0000ff>/* set up output stream for line buffering using space that 6 I) {/ M# d7 L; y4 S0 @+ \6 v3 w6 M
will be obtained through an indirect call to malloc */
- ~3 b$ J. \& g, Uif (setvbuf(output, NULL, _IOLBF, 132) != 0)
, F0 p( M* k) [! D1 e4 X3 gprintf("failed to set up buffer for output file\n"); # n. i) c) q- [( `
else
1 H: G& A: {( t5 P/ _! _printf("buffer set up for output file\n"); </FONT></P>, _3 R7 y0 [0 a8 G5 p
<P><FONT color=#0000ff>/* perform file I/O here */ </FONT></P>
, c `+ J: Q/ l8 w5 [* U. K<P><FONT color=#0000ff>/* close files */
- B$ r4 J+ p# w$ C& nfclose(input); # R1 D6 P' C# V4 @* D2 s) r* U, b
fclose(output);
1 h; ]) s( q6 ?return 0;
6 F/ |; t# b$ x6 d- O3 m. A6 Q4 W}
: U7 ?) ]3 e0 k0 Y% m' h</FONT>* w: Q( X* j) r/ W5 q( ~
; L0 A& f+ u7 Y1 N1 k& I
</P>/ }2 c7 d3 A2 ]
<P><FONT color=#ff0000>函数名: setvect </FONT>
$ h( I7 e4 d8 J9 m* X: m功 能: 设置中断矢量入口 7 o) [! H3 d# k1 a
用 法: void setvect(int intr_num, void interrupt(*isr)()); + C/ D& L" k2 f& J/ D* k \
程序例: </P>
. \, Q6 Q2 a7 W% f4 p& W<P><FONT color=#0000ff>/***NOTE:
! q+ l3 o7 O8 R: q. ^/ eThis is an interrupt service routine. You can NOT compile this
) B/ ~+ b$ y5 b" g+ nprogram with Test Stack Overflow turned on and get an executable ( M% R, ^. C8 Q3 T
file which will operate correctly. */ </FONT></P>
/ _& o4 ^/ ~) Y, ]+ ?& V0 A$ C<P><FONT color=#0000ff>#include <STDIO.H>
" c9 l1 A2 a+ E' p! D#include <DOS.H>5 V- b. ]! R7 d6 M
#include <CONIO.H></FONT></P>
+ a# }7 a/ T9 i: U9 b<P><FONT color=#0000ff>#define INTR 0X1C /* The clock tick interrupt */ </FONT></P>; T& ^, u( N6 m; w& @1 k
<P><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>
- r! y4 q; t) E0 r. Z4 Z4 w4 ^. f% [<P><FONT color=#0000ff>int count=0; </FONT></P>2 y3 ~1 z9 I' s- G
<P><FONT color=#0000ff>void interrupt handler(void) . c7 b! e! @2 d, S, [5 W
{
; o. P8 y+ R# @/ O" b9 r/* increase the global counter */
; _' n4 Q: y) i% m* b5 Dcount++; </FONT></P>: P/ y$ N9 i7 w: R
<P><FONT color=#0000ff>/* call the old routine */
0 r0 q% q1 f9 ~1 m3 zoldhandler(); q3 T2 n' _. }0 c' e9 Z6 Q s
} </FONT></P>
; _# H# V0 X$ \0 A$ B* E+ J<P><FONT color=#0000ff>int main(void) r( z3 H6 V' ?8 g1 X I
{ 3 ~ l. t& e0 ^9 ]& j2 `
/* save the old interrupt vector */ & T0 e2 ?3 P- R7 \/ y4 F
oldhandler = getvect(INTR); </FONT></P>* w1 ^ P z7 n+ p8 J% Z7 i
<P><FONT color=#0000ff>/* install the new interrupt handler */ 6 L3 Q/ s7 ]/ Z6 f5 z7 Y
setvect(INTR, handler); </FONT></P>/ h0 e$ j7 L$ a e2 o
<P><FONT color=#0000ff>/* loop until the counter exceeds 20 */ & P+ A! m! `; u# r$ t% a- B+ i
while (count < 20)
; Z# v \ U1 e; ]printf("count is %d\n",count); </FONT></P>- g6 f0 {# D7 J; Z4 S+ o
<P><FONT color=#0000ff>/* reset the old interrupt handler */
5 L2 S0 K( Y, i4 ssetvect(INTR, oldhandler); </FONT></P>7 [# h7 ^, c2 x- G) V- ?! m; v
<P><FONT color=#0000ff>return 0; 6 D+ |/ R' r% H S7 X! h: E$ s
}
& s) e# Y8 |' |* a8 E$ ~</FONT>& ]2 q5 Z$ q3 N( k9 b, O; B) H
</P>5 P/ O$ {! Z) S/ V3 N
<P><FONT color=#ff0000>函数名: setverify </FONT>
/ p+ P2 ^& v( o: p0 Q6 c- l: i功 能: 设置验证状态 - u( y% J; g/ t. ]/ `4 }5 u6 U
用 法: void setverify(int value);
: H, f. a, H/ d; e: B程序例: </P>+ y, _* s: c% y3 l! \& Y
<P><FONT color=#0000ff>#include <STDIO.H>6 U+ m- u' Q) l
#include <CONIO.H>
F- ^: ~. l0 B% s#include <DOS.H></FONT></P>4 U6 i2 O, k; N9 x
<P><FONT color=#0000ff>int main(void) / b Q( n) m( O. Y) H) e) f2 @
{
) n' L. O) F3 Oint verify_flag; </FONT></P>
7 L0 ^+ z; Y0 S1 {' [<P><FONT color=#0000ff>printf("Enter 0 to set verify flag off\n");
2 e, l3 Q& G& F S: V9 Vprintf("Enter 1 to set verify flag on\n"); </FONT></P>
% {( E. \6 ~; V/ z" @<P><FONT color=#0000ff>verify_flag = getch() - 0; </FONT></P>- t7 E* a5 z% p8 _7 T7 R
<P><FONT color=#0000ff>setverify(verify_flag); </FONT></P>
2 e! ^9 _ C1 N, j0 o' g<P><FONT color=#0000ff>if (getverify()) ' c6 r7 a3 `, ^. J
printf("DOS verify flag is on\n"); $ c% E+ A9 a; B( U2 r
else ! p/ O' M4 r& x7 i) w, c
printf("DOS verify flag is off\n"); </FONT></P>
, e7 ]# i1 n/ E/ g. N0 x4 R<P><FONT color=#0000ff>return 0;
4 ~, ^5 D0 \6 ?! q9 Z8 W* X6 D} 5 C) I: {& a' N; D0 K
<FONT color=#ff0000>
# G) K# g8 x& w& V- Y; c8 t; {</FONT></FONT></P>
" X" p2 G# l0 W$ M0 X# \7 [<P><FONT color=#ff0000>函数名: setviewport </FONT>
. G G, n! b* ~功 能: 为图形输出设置当前视口 , r U& J9 t& p4 D5 `
用 法: void far setviewport(int left, int top, int right,
! _' g' M. f. @" O; Rint bottom, int clipflag);
! X9 B; T1 K/ R: u程序例: </P>
6 e+ X3 g' e* a& t4 J* C<P><FONT color=#0000ff>#include <GRAPHICS.H>
3 w* k8 @, s& d, v, J. Y7 i#include <STDLIB.H>
3 G% }5 ^) [' C! C# S/ K0 w5 M#include <STDIO.H>* ?4 [5 F. c: d I
#include <CONIO.H></FONT></P>/ C$ o, G) d$ f
<P><FONT color=#0000ff>#define CLIP_ON 1 /* activates clipping in viewport */ </FONT></P>
: d( D! \" `3 e. ]<P><FONT color=#0000ff>int main(void)
: x4 q% u% w1 G$ c" c{ # r# E9 }# ]2 V& |0 v
/* request auto detection */ I; ~. V$ Z. O& I2 E8 y1 j
int gdriver = DETECT, gmode, errorcode; </FONT></P>
. p- v* O4 e) G/ @/ ]. Z0 K<P><FONT color=#0000ff>/* initialize graphics and local variables */ % ]2 W2 C9 f0 }' ?6 r- {
initgraph(&gdriver, &gmode, ""); </FONT></P>0 v/ B3 g% d7 T- G9 J
<P><FONT color=#0000ff>/* read result of initialization */
0 y A/ \/ L' Q/ m1 Werrorcode = graphresult();
4 j8 W f% ?- U9 b7 x- z& y9 X# bif (errorcode != grOk) /* an error occurred */ ' C( w9 `" F* q9 f$ s2 Y4 i/ n
{
4 p& b" n; x5 n6 B* `' mprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 2 s+ L7 C( b! Y; K# ?0 j
printf("Press any key to halt:");
$ q7 {5 [0 q$ r+ c0 ^getch(); ; D3 q! [' ]5 K- Q4 V. Z
exit(1); /* terminate with an error code */
0 z0 w( m! u. T9 q& J$ ~ p} </FONT></P>- x* Y6 |+ F) y/ I% p8 K6 v4 C) O
<P><FONT color=#0000ff>setcolor(getmaxcolor()); </FONT></P>3 h, [: e7 Y" j* u
<P><FONT color=#0000ff>/* message in default full-screen viewport */ ) h6 v, a) J7 t8 @
outtextxy(0, 0, "* <-- (0, 0) in default viewport"); </FONT></P>
: y9 R3 T" s9 l0 t$ `, o<P><FONT color=#0000ff>/* create a smaller viewport */ * v: I( D( @: P4 x4 ?1 W
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); </FONT></P>, I3 P4 j3 P9 d7 U/ `6 }6 Z
<P><FONT color=#0000ff>/* display some text */ 2 I! L+ u0 e7 m
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); </FONT></P>7 y) K- H: X* u, ~1 S
<P><FONT color=#0000ff>/* clean up */ : p. t( \$ c' {: K
getch(); 0 }7 j. _! H3 A' T/ c6 E: d4 [
closegraph();
& z, \; ~! Z% h _( a- a( f1 {return 0;
* E J, {5 [! L; z5 o} 3 [2 s1 q# _$ U+ }+ A/ t
</FONT>
0 M0 u4 r9 s' N1 P</P>
" f1 _5 w2 e' k5 H; ^9 T$ O<P><FONT color=#ff0000>函数名: setvisualpage </FONT>* S% P& b- d, C# g5 w# f$ ]* Q
功 能: 设置可见图形页号 5 ?4 g. [ a9 e, f3 x/ c6 ?
用 法: void far setvisualpage(int pagenum); ! w% G+ M' N+ B+ _! o6 C
程序例: </P>8 y5 k9 p9 s. R. T
<P><FONT color=#0000ff>#include <GRAPHICS.H>" |" f5 X7 p# k$ n) A7 s
#include <STDLIB.H>! w3 `( a T; {( j: G" x
#include <STDIO.H>
9 j( S) M* v, q9 Z( N#include <CONIO.H></FONT></P>: q) f* M, f# s3 x& N( F3 F4 s* \
<P><FONT color=#0000ff>int main(void) 2 e- m5 l s# O: ]5 _3 ^
{
; ]( W; x( a* A/ O+ o: g/* select a driver and mode that supports */
; g5 [) r% R" A" ^. t! t9 O3 ~/* multiple pages. */
) o$ n) o: F* M1 m- Cint gdriver = EGA, gmode = EGAHI, errorcode;
' u8 W! W/ c, h7 K8 Zint x, y, ht; </FONT></P>
% v9 t# o# }4 c+ ~<P><FONT color=#0000ff>/* initialize graphics and local variables */ 8 D8 {( d% P/ Q
initgraph(&gdriver, &gmode, ""); </FONT></P>" \; N, V( R8 g& L" n! d
<P><FONT color=#0000ff>/* read result of initialization */
: n$ N- t2 k& n! Gerrorcode = graphresult();
8 p1 ~$ w- S' m* V$ y. mif (errorcode != grOk) /* an error occurred */ # t. u# g. m4 {6 J R. Z( V! o
{ + Y# ~% i9 t. S/ w# w! z* I
printf("Graphics error: %s\n", grapherrormsg(errorcode)); # I g$ {) O$ v# p$ i3 {) e
printf("Press any key to halt:");
" ]3 ^5 R* K9 r( |% D+ jgetch();
' ]) W4 l! D5 S" Nexit(1); /* terminate with an error code */
1 D- e# G" E) q} </FONT></P>; b$ [$ ?/ ?: ^; o1 P0 R
<P><FONT color=#0000ff>x = getmaxx() / 2;
- x- F- G( Z- U' n* W0 Fy = getmaxy() / 2; 3 i! L+ t+ h% i$ C9 [1 o/ g/ Z
ht = textheight("W"); </FONT></P>
' G7 i# M' H9 |2 d7 v& U$ t J2 q<P><FONT color=#0000ff>/* select the off screen page for drawing */
+ {2 {5 m( @2 u jsetactivepage(1); </FONT></P>
! t' @! N7 H' X' X<P><FONT color=#0000ff>/* draw a line on page #1 */
' M# C. ]/ Z0 {2 \line(0, 0, getmaxx(), getmaxy()); </FONT></P>
9 e$ S. u* E) z<P><FONT color=#0000ff>/* output a message on page #1 */ ( ^0 E& H: i9 l6 g' Y- |9 s
settextjustify(CENTER_TEXT, CENTER_TEXT); % K: J& {1 D$ L
outtextxy(x, y, "This is page #1:");
+ N# L8 A# V8 V X* S0 t% V$ t; routtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
: H; A, n9 X- p% p/ ?<P><FONT color=#0000ff>/* select drawing to page #0 */
! g* `6 a4 Q4 wsetactivepage(0); </FONT></P>0 `* m2 d/ A: X' }
<P><FONT color=#0000ff>/* output a message on page #0 */
2 E7 t0 X9 T0 W5 {outtextxy(x, y, "This is page #0."); 8 a7 M% f, |! j K- b( }2 j6 N
outtextxy(x, y+ht, "Press any key to view page #1:"); % B- r! S: L) [/ p A
getch(); </FONT></P>
$ c4 J6 k" a$ W G0 n<P><FONT color=#0000ff>/* select page #1 as the visible page */
" Q. q8 i+ a G: o8 [3 Zsetvisualpage(1); </FONT></P>
0 S) u0 C7 b- a<P><FONT color=#0000ff>/* clean up */
& n1 e# U6 |4 M6 @- t- Pgetch(); 7 |4 W5 p) w8 Y: O
closegraph();
7 |/ {# X: l- ?# y, X" D: p+ Breturn 0; " a# s8 `3 l t8 N; A9 ?/ z
} </FONT>8 i; H, A4 Q, k! e3 `
0 J2 o# j" N" x# y
</P>4 y$ K: _1 `! |- T1 O
<P><FONT color=#ff0000>函数名: setwritemode </FONT>
9 {! O- c3 @" l% ]9 \功 能: 设置图形方式下画线的输出模式 ! Y% S, {+ X' U2 T
用 法: void far setwritemode(int mode);
+ h% e% p' `, T% M程序例: </P>
; Q6 ~1 n* S" E5 @) j0 a# o; Y" q<P><FONT color=#0000ff>#include <GRAPHICS.H>
' [9 I, _; ~5 S; c* E) {# \- q( f#include <STDLIB.H>! W6 l+ q/ D: R3 f
#include <STDIO.H>
: |& z }* g6 n5 F#include <CONIO.H></FONT></P>
( e2 M' X+ Z2 w c( y+ d1 T<P><FONT color=#0000ff>int main() " c, Y7 W" Y2 b1 j3 z; |
{
8 d/ |$ S* K. u/* request auto detection */ 0 \" q! M: W& c8 O& k
int gdriver = DETECT, gmode, errorcode;
& S" Q8 e- O+ C* R O ]int xmax, ymax; </FONT></P>+ y1 Z6 {! O0 D- P! i
<P><FONT color=#0000ff>/* initialize graphics and local variables */ " _: V6 ~5 ?. K+ M. @
initgraph(&gdriver, &gmode, ""); </FONT></P>5 f0 [- G( j; ~0 Z
<P><FONT color=#0000ff>/* read result of initialization */ 6 a3 u' C7 |: C# y' T0 Y
errorcode = graphresult(); 7 X( I" O; c" b5 H
if (errorcode != grOk) /* an error occurred */ 5 |3 o. s: y+ ?7 b4 Y) b
{ + L; {. g% T. V8 b; _' H% ~
printf("Graphics error: %s\n", grapherrormsg(errorcode)); , B# P1 V, b) p/ ^5 x
printf("Press any key to halt:"); B0 H& D6 H4 g5 m- ?
getch(); & x! Q5 ]# H' Z4 F1 Y8 J% O
exit(1); /* terminate with an error code */ - F: B3 j- `* Z5 k* \6 Z/ |# b
} </FONT></P>
( t* q1 @0 m7 Q$ H, u) q<P><FONT color=#0000ff>xmax = getmaxx();
& j" J4 I3 b4 n' L" `; {1 Vymax = getmaxy(); </FONT></P>. _, K ?2 I- e6 e+ ^) G& m$ `
<P><FONT color=#0000ff>/* select XOR drawing mode */ 6 Y Y$ T, L0 G/ ]- {9 s' V8 e
setwritemode(XOR_PUT); </FONT></P>
/ j- {5 S7 J: X/ v9 T9 O, k<P><FONT color=#0000ff>/* draw a line */ & V) H2 K4 N1 r, k- L
line(0, 0, xmax, ymax);
& x9 D5 l. o i9 I7 h' c: w6 w0 hgetch(); </FONT></P>2 a D9 M/ E1 n% M. |( o I% k
<P><FONT color=#0000ff>/* erase the line by drawing over it */ ( {2 o) k* Y5 K$ n8 }9 s# |, s& S
line(0, 0, xmax, ymax); 6 h' ^3 [( A; {8 {6 e# s: C I. h
getch(); </FONT></P>
4 c6 Z3 `# U8 ^<P><FONT color=#0000ff>/* select overwrite drawing mode */ ' i9 @% t) \* Y/ a* j
setwritemode(COPY_PUT); </FONT></P>/ N4 y2 P) U: Z- C
<P><FONT color=#0000ff>/* draw a line */ 7 {3 g& x; t/ A3 q" x% X# B
line(0, 0, xmax, ymax); </FONT></P>+ g% Z- E) r8 ` x0 s9 r
<P><FONT color=#0000ff>/* clean up */
9 @* J2 e: }( J5 i; p3 mgetch(); 7 [% U/ G, K) Z8 Z# N
closegraph();
- R9 o% h3 S9 W( G. N7 Hreturn 0;
* |+ T( S$ m. ]3 ]+ Z}
! h( J1 r* T8 a</FONT>
, N, r3 ^7 r: N</P>
1 g) d/ o5 F# u+ w$ Z4 _- D<P><FONT color=#ff0000>函数名: signal</FONT> 2 n6 K5 |* h& r& O
功 能: 设置某一信号的对应动作 2 j' u3 I* n/ g' e& Q; [0 }; a) j0 `
用 法: int signal(int sig, sigfun fname);
! u8 X8 Y1 B3 W( q$ ]程序例: </P>
6 x% N9 U1 @4 O) J' N( |3 a' Y<P><FONT color=#0000ff>/* This example installs a signal handler routine for SIGFPE,
8 L0 w/ e$ e+ c/ X3 [! kcatches an integer overflow condition, makes an adjustment
: v) J# }! J x: w7 Yto AX register, and returns. This example program MAY cause + G" I! z1 o2 h6 \7 {
your computer to crash, and will produce runtime errors
2 @) S4 j- ?+ a. kdepending on which memory model is used.
% }; N% o9 _( D: {( @*/ </FONT></P>) W1 ~1 V$ p6 h. H1 t0 s% b
<P><FONT color=#0000ff>#pragma inline ; Z$ T5 T4 z' Q+ |: ^4 `8 J
#include <STDIO.H>
E) W* n% y, {. c) r* W#include <SIGNAL.H></FONT></P>8 Y/ Z; t( q% f- d5 x: S; H% \
<P><FONT color=#0000ff>void Catcher(int sig, int type, int *reglist) " r0 ^6 X4 F# Z4 z- i" v; t/ h
{ 9 G2 E( G+ Z9 |0 X, \. E; [9 ^
printf("Caught it!\n"); * Q2 v0 A' b: T* y0 _
*(reglist + 8) = 3; /* make return AX = 3 */ " z; g/ c$ R5 f3 I1 z6 P
} </FONT></P># m2 O& p1 Q8 n8 C% G& Z
<P><FONT color=#0000ff>int main(void)
. o& `$ a9 O, E2 T) X{ 9 [* ^) O M" e& p0 X: ~
signal(SIGFPE, Catcher); # j- p% \4 M3 y5 U3 j4 l
asm mov ax,07FFFH /* AX = 32767 */
4 A3 r% v4 o H. B* o$ w |asm inc ax /* cause overflow */
5 c$ \4 f+ M) G8 t/ kasm into /* activate handler */ </FONT></P>
9 ]" a ~1 E N6 x' F+ B<P><FONT color=#0000ff>/* The handler set AX to 3 on return. If that hadn't happened, ( ]3 E& E$ M- u2 _( f1 X
there would have been another exception when the next 'into' / M9 w6 ^$ Y" B- E
was executed after the 'dec' instruction. */ # d% G& _4 z( w. N
asm dec ax /* no overflow now */ & ]. F9 K- l5 Y, r+ j& V* f& P
asm into /* doesn't activate */
8 m$ \# x9 M/ T1 u8 m& Treturn 0; 7 k' R) a0 Q, K: G9 p& d0 O
} * G7 ]* [! d, A" ]( \- h( B0 D
T4 U, K% ^1 f% h) n8 T, d8 |4 s8 v% }! }9 Q: d
</FONT></P>& a* X. N. i8 s. E! l; a* F
<P><FONT color=#ff0000>函数名: sin </FONT>
: ?) R( M) P+ _5 \/ k: K$ e功 能: 正弦函数
+ U: W+ x4 ]8 Z* u* D: v用 法: double sin(double x); 3 a' p; `1 d8 f' i+ z0 I9 t
程序例: </P>+ {& [ s% L1 g+ b
<P><FONT color=#0000ff>#include <STDIO.H>0 P S" v6 [! x; T/ H& Z! w+ ]3 V
#include <MATH.H></FONT></P>
2 V8 c' p( J. F" Y<P><FONT color=#0000ff>int main(void) 8 c1 T6 f9 Y" Y9 X. g, Z. j9 M
{
6 T: Q% T! `" u0 r5 @2 q' v% O) Q. M5 Pdouble result, x = 0.5; </FONT></P>
- g& p% v2 n* V# i3 p<P><FONT color=#0000ff>result = sin(x); }% \+ M* G' T: g/ `- y) W
printf("The sin() of %lf is %lf\n", x, result); % N6 A1 b. \4 t0 z! I
return 0;
: b6 Q8 a1 q+ J. J& F}
% c `* Z" z& i/ I4 { k: u" _
</FONT></P>
% b4 c6 i9 d; |5 i* J( {: X2 h<P><FONT color=#ff0000>函数名: sinh </FONT>- ]( b6 k# J* m
功 能: 双曲正弦函数
3 W/ I$ M( h& w& C G1 }9 R, f用 法: double sinh(double x); q+ ^2 u4 w S% ^
程序例: </P>
# v7 T# b1 G/ H( `& M: \! s: R9 o<P><FONT color=#0000ff>#include <STDIO.H>7 P5 O/ M* o; n- |% t: H
#include <MATH.H></FONT></P>( T& N/ w! R3 I# A0 p4 \! t( R( m
<P><FONT color=#0000ff>int main(void)
- O; ]7 E1 z. Y1 K$ K{
; h8 E' g3 Y# K# D5 jdouble result, x = 0.5; </FONT></P>
+ z; V/ N3 i7 K" v% {5 n9 s<P><FONT color=#0000ff>result = sinh(x); 9 t) _& \1 z" p1 @& r+ f
printf("The hyperbolic sin() of %lf is %lf\n", x, result);
+ e0 ?# c9 @8 T) ^( Jreturn 0;
9 L+ S/ T Z3 V7 a; t5 L} : o3 V9 \3 c& q9 \
5 Y$ W# O% L$ w6 j! Y
2 h. t& J# }6 N( G5 q+ c% H/ A
</FONT></P>5 j* K6 W3 c7 H% P, g4 K) K
<P><FONT color=#ff0000>函数名: sleep </FONT>4 p2 D0 X: R8 S% A4 b
功 能: 执行挂起一段时间 0 T5 k. x6 U$ e; N7 W
用 法: unsigned sleep(unsigned seconds); / q3 D: e( l3 t; x$ ]
程序例: </P>
/ e" [* }" h0 k& u<P><FONT color=#0000ff>#include <DOS.H>
. k% Z* R4 h0 v; U#include <STDIO.H></FONT></P>- W8 P( F' N: J1 y
<P><FONT color=#0000ff>int main(void)
" C% u/ F1 P7 u1 U7 q7 }( |{ 6 Y, y1 e$ P8 \) w: B
int i; </FONT></P>$ c- O! U- g. N. D5 m
<P><FONT color=#0000ff>for (i=1; i<5; i++)
' t# t) |+ o6 l h2 O{ ) _+ l9 q3 Y: O. i: u3 J7 r2 t0 g" z) Y
printf("Sleeping for %d seconds\n", i); 5 C1 `* ^7 p: O
sleep(i); % V: z9 U8 g8 a5 y ], X
}
' d" {, H% _1 P0 x# ^1 W6 z, {: S0 F* @return 0;
' o9 h& ^% A8 _3 {* w6 A0 d8 Y3 m} 3 _9 t3 j$ g3 `4 p D
: p" q! H8 J3 s$ {! G</FONT>
2 N: Z3 ], v8 I) p3 W/ G0 R</P> W7 y- [" x2 [+ a: W
<P><FONT color=#ff0000>函数名: sopen </FONT>
5 j+ p+ H9 |+ a3 R功 能: 打开一共享文件
6 g1 A7 ?2 X2 }用 法: int sopen(char *pathname, int access, int shflag, int permiss);
+ d1 u' T# ~3 J程序例: </P>
$ U, T' c2 t1 a0 ]1 U: H<P><FONT color=#0000ff>#include <IO.H>4 C7 v* J/ {9 Q. F2 x0 C
#include <FCNTL.H>6 U3 o* P1 p! v8 I8 n S
#include <SYS\STAT.H>
% }" s4 y$ M1 e% w3 A, \#include <PROCESS.H>) c3 N! d3 | F3 D" b$ R! d8 @6 P
#include <SHARE.H>
0 n# r- e w9 J+ T" o) m#include <STDIO.H></FONT></P>- t* `# i3 U1 x1 i( Q6 u
<P><FONT color=#0000ff>int main(void) / X: d: M% d) L/ @5 M
{ 2 W; i3 k! z% I4 c8 l3 I
int handle;
1 U" s" z) w1 m v* o% U% Rint status; </FONT></P># m" Q# w* D( h" G
<P><FONT color=#0000ff>handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD); </FONT></P>
0 a7 R$ S& S/ |8 t$ }$ g5 s! j. q5 e<P><FONT color=#0000ff>if (!handle)
. K# B; g( V% P{ 7 \& B9 p9 [' `7 O3 D0 s) ?* w6 Z
printf("sopen failed\n");
" {! Z# ^% C {9 T" e6 U) S9 j2 R f7 Dexit(1);
. a% {$ ~/ G. ]( B+ {7 Y4 m} </FONT></P>
P. \$ z0 J0 {) |" ^9 s- z/ l1 f' u<P><FONT color=#0000ff>status = access("c:\\autoexec.bat", 6);
6 L5 j2 E/ s5 Fif (status == 0) * B2 p' k# S+ X4 R
printf("read/write access allowed\n");
3 Z2 t8 \$ w }else ) [- _* b, l6 n6 J- }: X
printf("read/write access not allowed\n"); </FONT></P>+ z* @ ] T- y% P
<P><FONT color=#0000ff>close(handle); 2 B4 Q* w% z( v. N9 [* ~' b7 P
return 0; ' Z3 `4 e" E6 S# V
} 3 V7 s2 n& o2 f, R3 P
$ b- ~) |/ m( ?* {
</FONT>6 Y2 o+ w1 B: n* ~
</P>0 u# ?5 a; X5 M% ]% v2 ?
<P><FONT color=#ff0000>函数名: sound </FONT>( k2 _7 b4 h4 j. l% _
功 能: 以指定频率打开PC扬声器 7 A3 c' \) @, j+ Q; M" S0 }
用 法: void sound(unsigned frequency);
* S+ w0 ]6 `: ?2 h程序例: </P>
) _" }2 q( b( v1 j5 d/ X5 e<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. % o' v) E: l# k- t
Your PC may not be able to emit a 7-Hz tone. */ - {" u( k% X9 V( k. F9 f
#include <DOS.H></FONT></P>
# A8 F* R) f2 ?% D4 b1 ]<P><FONT color=#0000ff>int main(void) , X d( T, E! Y3 u, c
{
7 D# v* ^' L. F; h& ?# l5 O6 zsound(7); 8 [) N6 j0 y0 P r/ {5 w$ k
delay(10000); : j4 Y0 t. J |: W! A
nosound(); - l% o8 m* o9 c% y6 g7 [6 V
return 0;
5 U$ ?1 j: j' V$ |} </FONT>; r5 O* W: ?1 |3 F+ J; I
% ?: {, j$ C" M" `2 O
0 |# r9 K. h9 g$ L8 t9 q
</P>3 l% q" W7 t+ `( t( K2 m
<P><FONT color=#ff0000>函数名: spawnl </FONT>: X! h. f0 K3 r& n9 m) O
功 能: 创建并运行子程序 - R, Z8 O& H& t( Z- V
用 法: int spawnl(int mode, char *pathname, char *arg0,
5 b0 P$ g |7 C% A' d8 j5 Oarg1, ... argn, NULL); 7 c% U/ H. m5 k) c# K" U& ~ I
程序例: </P># M8 m2 U: ~2 j T* G
<P><FONT color=#0000ff>#include <PROCESS.H>
K% |1 S. B$ L4 V#include <STDIO.H>
2 l7 B. t) q( |5 M0 T x' |#include <CONIO.H></FONT></P> [5 ^4 s, O2 }0 V
<P><FONT color=#0000ff>int main(void) : i& N- L! r: B; U% W
{ : |0 m& h/ x" t3 G/ Y7 Z& g
int result; </FONT></P>
8 r A" A/ |) s; V1 D<P><FONT color=#0000ff>clrscr();
4 n# s: E- z8 [2 Zresult = spawnl(P_WAIT, "tcc.exe", NULL);
o4 g; _# S# z6 V/ _) [* jif (result == -1) , M4 \% C. }$ L# z' p: H4 H
{
( Z2 t3 ^. Y) f7 Wperror("Error from spawnl");
2 z+ I- @0 ?6 x, f" X* Z5 cexit(1); : `0 K/ d7 x6 R
}
$ G/ r2 [* [& @1 q! _/ `( Rreturn 0;
+ q& h @8 I. t" E' R# m) p; X}
( |. x4 W( E0 J</FONT>' M" z }8 X" A- g6 ~! s
</P>
" J9 Y& ]3 @6 y<P><FONT color=#ff0000>函数名: spawnle </FONT>
' F! w& U7 @9 n功 能: 创建并运行子程序 / R0 m4 h9 e# V: J& m3 k
用 法: int spawnle(int mode, char *pathname, char *arg0,
2 y) E) Y5 E- h- U0 oarg1,..., argn, NULL); * a! S) I8 o9 [, y0 j' V1 o
程序例: </P>2 U8 i( B1 ~$ l3 R$ m8 U. }; E
<P><FONT color=#0000ff>/* spawnle() example */ </FONT></P>3 I; U% w' `8 u
<P><FONT color=#0000ff>#include <PROCESS.H>
% A) E( F/ y# K& w#include <STDIO.H>1 H8 O) O+ f0 @" O$ y! N
#include <CONIO.H></FONT></P>
8 s& }. v5 K2 f& e9 r<P><FONT color=#0000ff>int main(void) ( {+ I+ u9 o9 i8 K9 j' r. t
{ 1 ^! P1 Z& L. e2 t. B
int result; </FONT></P>( `" o4 o( a8 o2 E& a& s) Z N
<P><FONT color=#0000ff>clrscr(); . A3 W$ Z" q; p+ I1 h8 @
result = spawnle(P_WAIT, "tcc.exe", NULL, NULL);
+ J' Y8 V' s5 Nif (result == -1) ! @2 k* K3 K! C
{
/ L2 u9 y' g: j0 G+ W3 e' u. P& qperror("Error from spawnle"); ! B% o/ Q! P6 O9 N% r
exit(1); 4 E% A1 e8 j/ d% R$ R9 N/ w
} - z" U8 \/ c# o( o# K K
return 0; 8 j& O' \" u6 i8 P, u
} & N e6 u) }# Z4 E. O+ F; ]
' T' R$ w/ o. t5 l. ?
4 n7 E0 m4 `5 `+ \/ Q7 b</FONT></P>
+ \2 x1 ?/ K/ ?* U' e' ]<P><FONT color=#ff0000>函数名: sprintf </FONT>
, |/ [6 M# j# _2 P3 g- V功 能: 送格式化输出到字符串中
8 |4 ]. {1 Y4 A! C; a0 ]% ~用 法: int sprintf(char *string, char *farmat [,argument,...]);
6 |; a2 G% o1 F- b) n程序例: </P>
, X* K! f3 O, x& f<P><FONT color=#0000ff>#include <STDIO.H>! o# w! \1 \: ^9 l8 v8 H1 Y1 ^, l9 p
#include <MATH.H></FONT></P>
5 s+ o- w5 ]: m1 Q* k. Y( f<P><FONT color=#0000ff>int main(void)
/ Y) A+ c) J l5 c7 A{ ' ]1 m M0 v) g# x) j6 F: q
char buffer[80]; </FONT></P>
9 X0 j: V; f3 @5 u9 {* Z% y6 Y, b<P><FONT color=#0000ff>sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
+ q: f) g( g+ f9 T9 ~# W4 j( Z/ n7 Iputs(buffer); : S4 J" j/ j# X* q- Z3 |; j
return 0; 8 y8 y: O# a8 Z) \! |7 n- y* g9 q1 l, k
}
, }: N5 j3 |' k</FONT>
9 H$ ~5 [) p3 H& n& p) a- L9 l$ q# Y</P> n, o( F3 p3 F, Q$ F2 X" q
<P><FONT color=#ff0000>函数名: sqrt </FONT>
( p$ {7 @( [* E& g功 能: 计算平方根 3 E- D* o% j8 m- `* S& k
用 法: double sqrt(double x); 3 _& i; U* ~" H- M
程序例: </P>
( U7 d p2 N( H' Z<P><FONT color=#0000ff>#include <MATH.H>; r; U( R6 T2 T6 B# y/ o' T) Y& L& y
#include <STDIO.H></FONT></P>
- t# n- W) B$ M9 G<P><FONT color=#0000ff>int main(void)
6 E; V7 e' c0 y; T7 h: ?) b{ ) \% T+ ]4 C% t0 g, ]& {5 ]
double x = 4.0, result; </FONT></P>
w" i5 m% k( W& z( Z+ X+ u<P><FONT color=#0000ff>result = sqrt(x);
0 c m { p; ?. T/ tprintf("The square root of %lf is %lf\n", x, result); $ A% _6 l @* w
return 0; ; ~: D5 K/ R7 G; j4 }1 r! w9 x( B
}
! P! \/ G" r% }</FONT></P>+ {9 x2 v: i% \" I3 K2 z- T7 n
<P><FONT color=#ff0000>函数名: srand </FONT>
8 Z$ R! f! L) s/ b2 z8 d功 能: 初始化随机数发生器 : H& m4 s+ b% A* j" s+ G. J# U, X
用 法: void srand(unsigned seed); * q3 ~' Y1 P0 F
程序例: </P>
$ e, x! M6 ]" B! u- n/ d0 W<P><FONT color=#0000ff>#include <STDLIB.H>( r( B7 B: ?3 I) M1 q S$ U
#include <STDIO.H># ]8 e8 D4 C4 d \' m
#include <TIME.H></FONT></P>+ }1 ~& Z' Y/ C) W. X
<P><FONT color=#0000ff>int main(void) , |$ q' D. U& ]$ e3 x9 Z
{
% D" D5 _3 Y9 Y1 tint i; % C7 a* Z' i% c2 z
time_t t; </FONT></P>" }: ?7 T. S2 q; e. u$ o r+ m
<P><FONT color=#0000ff>srand((unsigned) time(&t));
8 p# R! `3 [; {2 z% j' s- g: Lprintf("Ten random numbers from 0 to 99\n\n");
, j# j4 `) \3 ^- ~( i Cfor(i=0; i<10; i++) % D5 m# W5 _7 [, l7 l
printf("%d\n", rand() % 100); 8 G7 N8 V7 l' U* M0 x
return 0; ' ~. R9 u8 @" n$ n) n, X6 H
} ' D( _9 N' I" G3 ~/ i( c( r
</FONT>
z2 y u; P$ ~' b2 ^- a</P>% c% E/ C( v& B8 P: f
<P><FONT color=#ff0000>函数名: sscanf </FONT>
9 a5 ?3 g5 i5 G; I/ u/ a& ]功 能: 执行从字符串中的格式化输入 2 b" X/ T" d* s8 y; B
用 法: int sscanf(char *string, char *format[,argument,...]);
# g, y% r, L/ |程序例: </P>9 T$ T1 `# I5 A) e3 M9 A2 e! p; I2 R
<P><FONT color=#0000ff>#include <STDIO.H>
) j) F* ~/ z$ a E3 k! N#include <CONIO.H></FONT></P>
( [0 `9 e0 m4 `, e<P><FONT color=#0000ff>int main(void)
6 ?) p# w6 r' X7 v' s{ 4 A. `4 |5 Z. }1 p
char label[20];
! k7 X1 n$ c+ P( a, V; i! Echar name[20];
* m2 y& _/ @' z. v7 ^8 k6 wint entries = 0;
L. m; b/ i3 b0 J" |/ g# J: O' }int loop, age;
% K: F+ t# v6 {+ e: d% c5 odouble salary; </FONT></P>& A1 d; u/ Z0 ]. K- b
<P><FONT color=#0000ff>struct Entry_struct
. {$ E0 O4 I- f, |! k4 D* L{ + Y B& \8 r6 q
char name[20]; . L: I. k1 \- S& D: h
int age;
! G5 q" S& ?( H! q1 w; Ffloat salary; ! D- ~5 m- v- s; w, x3 M1 m7 G
} entry[20]; </FONT></P>
7 z( D* d! L, R: \# P7 E. u# h+ g<P><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
# U9 E) g! u S5 Uprintf("\n\nPlease enter a label for the chart: ");
( T6 E6 o6 @$ l; [9 {1 Nscanf("%20s", label);
2 T! W& J, r4 P' efflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
0 m p, e/ w3 e; l3 n# \% O4 f- ~0 F<P><FONT color=#0000ff>/* Input number of entries as an integer */
, N6 F8 ~$ F7 ^printf("How many entries will there be? (less than 20) "); . z, z* s, P: T- M9 m9 l
scanf("%d", &entries); . S ]) P1 P4 w
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
$ m3 T2 U% R7 @% Z! [<P><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ - `! f- i1 o% s# @, m! }
for (loop=0;loop<ENTRIES;++LOOP) " | N& V; b$ r' ]0 R2 J. t
{ ! i6 i1 q9 ~2 m: I6 E2 l. B
printf("Entry %d\n", loop); 2 s$ G2 F8 R+ C" i' S4 Y
printf(" Name : "); 0 R/ t- o1 Q4 u4 R6 D
scanf("%[A-Za-z]", entry[loop].name); ' D8 u. R" o8 R* w# ^; \ S
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
/ l+ V7 l! o) G) {- T& O<P><FONT color=#0000ff>/* input an age as an integer */ ; x; l) I+ h6 T' W
printf(" Age : ");
0 Y. D# V1 N+ M# u5 v) i2 yscanf("%d", &entry[loop].age);
- n- f# }3 e% X) q \+ l4 rfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>/ A8 J: r0 e) _7 w
<P><FONT color=#0000ff>/* input a salary as a float */ * }4 {- {6 F& ^ N( m. Z2 ]
printf(" Salary : ");
8 `+ N2 }2 f. T8 R1 c7 ]$ P2 U4 Sscanf("%f", &entry[loop].salary);
( L! z/ M% r' h. mfflush(stdin); /* flush the input stream in case of bad input */ & v) v. y& u: P3 f `' z7 t
} </FONT></P>5 L# b- l) t; P/ _$ Z
<P><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */ * D; C6 R" A7 l5 H* n
printf("\nPlease enter your name, age and salary\n"); - u* \- |9 j5 \
scanf("%20s %d %lf", name, &age, &salary); 0 N3 Z& C% S: }1 L9 k+ X4 s, K
</FONT></P>8 |6 E! l! m. {8 x, B8 b
<P><FONT color=#0000ff>/* Print out the data that was input */
" q" t+ R' F% L" ^4 Oprintf("\n\nTable %s\n",label);
" i. k8 ^( v, J7 |printf("Compiled by %s age %d $%15.2lf\n", name, age, salary); 3 b$ o7 Z0 q" ]
printf("-----------------------------------------------------\n"); . z4 e6 [+ U, r: U# U) l1 Q
for (loop=0;loop<ENTRIES;++LOOP)
0 e; M: I" a; e ~' Z3 N printf("%4d | %-20s | %5d | %15.2lf\n",
1 a3 Z4 ^. M1 j' oloop + 1, 1 S* x. y' g: `+ _
entry[loop].name, 5 u: ~7 A, ?+ s
entry[loop].age,
^7 `- L+ f# E2 S, |entry[loop].salary);
1 z u& z( ?* i- O+ u$ |printf("-----------------------------------------------------\n");
" H( U }0 H [; o2 D; R3 C9 }return 0;
3 i2 N! v2 |: {5 s% X} 3 G! O/ E$ c4 e# k/ Q: d
, `! e$ Q3 U7 |/ {8 E</FONT></P>
& R. a" @. D# T, U, {<P><FONT color=#ff0000>函数名: stat </FONT>
1 D$ U5 i3 D$ S4 `% |: G- U8 z功 能: 读取打开文件信息
9 V8 E( v' l7 {" k. `5 c2 T用 法: int stat(char *pathname, struct stat *buff); 0 L' e: h0 h8 x+ T- u% y' v0 |
程序例: </P>/ Q. m& ^! B( j& t1 G
<P><FONT color=#0000ff>#include <SYS\STAT.H>
5 Q& i/ q" j7 @: e5 o1 g#include <STDIO.H>
: c5 I, Y+ U! E( X6 L9 H#include <TIME.H></FONT></P>! j' B/ r( G+ V. D. r( j, u! W0 j5 g
<P><FONT color=#0000ff>#define FILENAME "TEST.$$$" </FONT></P>: ~& D G. o" Y3 e' O& f
<P><FONT color=#0000ff>int main(void)
! S$ }# [# @/ I- k9 O* A. M{ : X; e' I# D, l* a- [6 c+ j
struct stat statbuf; 5 d% V1 ?3 A$ n8 W9 q
FILE *stream; </FONT></P>
( h/ ?6 h, W. T/ K+ K) t1 x( T<P><FONT color=#0000ff>/* open a file for update */
: I+ j% O- i6 V, Y; f; E0 zif ((stream = fopen(FILENAME, "w+")) == NULL) ' l8 |' e2 S3 k: L6 s% e
{
" ^ r6 O, Y( kfprintf(stderr, "Cannot open output file.\n"); . F$ {9 k- E/ |8 N2 N7 X
return(1); 5 y( s9 L! j9 t
} </FONT></P>- y0 _4 g5 I+ \1 `& v0 T0 S, q
<P><FONT color=#0000ff>/* get information about the file */ ; N8 _) J& S- X9 G1 B% [
stat(FILENAME, &statbuf); </FONT></P>( S9 `1 \) X- L
<P><FONT color=#0000ff>fclose(stream); </FONT></P>
y! u- j" S" W \& o) [: r! T9 t<P><FONT color=#0000ff>/* display the information returned */
2 D" O! L( h1 Q% l3 s# Pif (statbuf.st_mode & S_IFCHR) & C/ }( j# K5 V# M# Q
printf("Handle refers to a device.\n"); - b N% D2 m: K+ r/ y# |1 i5 [+ g
if (statbuf.st_mode & S_IFREG)
& c; s( V4 u1 ~) Z* B a* x9 I' X' i4 qprintf("Handle refers to an ordinary file.\n");
* W8 r' N; e$ s+ v2 Cif (statbuf.st_mode & S_IREAD)
" \) w, J" J7 Jprintf("User has read permission on file.\n"); # i G: r6 Y! V' S( R
if (statbuf.st_mode & S_IWRITE) 6 w% h! S% R3 V! l' k0 b( l
printf("User has write permission on file.\n"); </FONT></P>8 R1 _2 g" I5 a% T4 u, _
<P><FONT color=#0000ff>printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
, T7 l3 W R, [* yprintf("Size of file in bytes: %ld\n", statbuf.st_size); . d) W/ m8 [$ j, p$ p
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime)); ) v$ t) c9 f5 K+ ]2 V
return 0;
- K# T; C( k" B3 P} - N& P v% S0 N- j# j9 @
6 f8 n- V( V: e6 c- [) ?4 K/ |
</FONT>! A* L) B8 ]6 ], y0 a# u
</P>
/ V# |% P+ K3 B( c<P><FONT color=#ff0000>函数名: _status87 </FONT>
0 Y0 g8 e5 s. g. q% @) D r功 能: 取浮点状态 # U, @+ e$ A6 X% h
用 法: unsigned int _status87(void); 8 l3 `( @# t" A' V) s" V) Z4 t3 O
程序例: </P>' ? q/ w5 ?7 R
<P><FONT color=#0000ff>#include <STDIO.H>0 Z7 W2 Q3 t2 `: z
#include <FLOAT.H></FONT></P>
) ~5 ?& [7 Y. l<P><FONT color=#0000ff>int main(void) % K% S: c! V5 R! j
{ 6 F; I/ I9 V: y) ?3 g6 g7 w
float x;
/ d3 E/ V5 h. e- ]4 edouble y = 1.5e-100; </FONT></P>
. b8 F% R; |1 j5 u<P><FONT color=#0000ff>printf("Status 87 before error: %x\n", _status87()); </FONT></P>% j3 e2 P% i5 E2 X- b' I3 W
<P><FONT color=#0000ff>x = y; /* <-- force an error to occur */ 7 e7 p" I5 Q$ I8 q- ~$ m' B& N
y = x; </FONT></P>
' \8 C" S% u" k; U<P><FONT color=#0000ff>printf("Status 87 after error : %x\n", _status87()); 1 g# V; K0 N6 J) U8 A1 D: B( }
return 0; - M5 R( l( e* U( z6 s( i; n) B
}
$ B$ B* B: l/ g; ] o</FONT>
4 e+ c, v- N: a</P>3 ^1 f) n2 ?# F( e' j L
<P><FONT color=#ff0000>函数名: stime </FONT>6 [- L( u( e4 {+ `
功 能: 设置时间
( r+ F4 t- ^; s2 `用 法: int stime(long *tp);
" J* O8 q2 U) R# ^) }程序例: </P>
2 I; y+ S3 P! d8 K<P><FONT color=#0000ff>#include <STDIO.H>- {; |" Z4 t, x! M- {
#include <TIME.H>. l& _8 k. B, v
#include <DOS.H></FONT></P>/ L3 M2 |5 f4 f8 x8 V
<P><FONT color=#0000ff>int main(void)
6 M% l9 ~1 N3 }* d{ ) j8 z/ _- v+ ~2 d! b% V: `( ^
time_t t;
$ h' ?5 S; J( ~2 _4 Q5 K, Ostruct tm *area; </FONT></P>
. ^6 w3 A6 j0 T<P><FONT color=#0000ff>t = time(NULL);
" C+ J$ G3 \# r: [; Marea = localtime(&t);
) G2 ]* `- ?( q& @8 nprintf("Number of seconds since 1/1/1970 is: %ld\n", t); 4 f( ^3 R' v" E1 L- a/ `
printf("Local time is: %s", asctime(area)); </FONT></P>3 R1 Q1 \8 t- Q7 K$ S
<P><FONT color=#0000ff>t++;
! `4 ]4 m0 J7 garea = localtime(&t);
% ?9 u/ a U* z6 E7 wprintf("Add a second: %s", asctime(area)); </FONT></P>
6 y9 \/ ]' `, F, ?+ n5 U) Y<P><FONT color=#0000ff>t += 60; ' c) D6 w9 a5 u4 {2 d3 s# R
area = localtime(&t); & K1 W, L7 w: Z8 u; T# k
printf("Add a minute: %s", asctime(area)); </FONT></P>2 D3 F: H9 z/ Y* Z! [2 Y0 @+ g
<P><FONT color=#0000ff>t += 3600;
& I8 u/ a0 Y- c1 }) I, ]! _area = localtime(&t); ' S- P1 W& U9 J) |, e( `- ?7 v$ G
printf("Add an hour: %s", asctime(area)); </FONT></P>
) U. n+ m9 p, A! W# Z4 P<P><FONT color=#0000ff>t += 86400L;
9 m$ e6 X8 _, j* u$ u m# i/ |area = localtime(&t); ! ]: p3 H- g8 q
printf("Add a day: %s", asctime(area)); </FONT></P>
* ` X/ Q( z, L<P><FONT color=#0000ff>t += 2592000L;
0 ]; w$ R' M+ Y6 k" R# Y( Z: ^area = localtime(&t);
/ e- e ]1 x H$ w( _) Kprintf("Add a month: %s", asctime(area)); </FONT></P>
$ j: ]: ^3 _- h1 w<P><FONT color=#0000ff>t += 31536000L; - B0 L" y* c8 P
area = localtime(&t); 1 {5 F$ }# ^ x, H
printf("Add a year: %s", asctime(area));
) q- C4 M. e5 y5 I6 Sreturn 0; " x1 n1 m8 Y- I
} </FONT>
. A. Z3 s6 Z1 d/ A1 I* x* q1 g, X" s6 }$ Z( ?& N- O
( W6 z& I" L, w/ f0 M1 N
</P>
% Y Q R' j0 S<P><FONT color=#ff0000>函数名: stpcpy </FONT>- G' k- _/ q3 l/ D- x
功 能: 拷贝一个字符串到另一个 # w2 Y/ u: o) b- ?
用 法: char *stpcpy(char *destin, char *source);
, P2 N$ r% G, n/ F7 i: P5 G" [% Y7 O1 i程序例: </P>
$ z( A& O$ M0 `: ?7 g* v<P><FONT color=#0000ff>#include <STDIO.H>
8 T7 u# W1 S k" r8 v; l#include <STRING.H></FONT></P>3 c3 V% Z6 T! t- K7 Z G0 A
<P><FONT color=#0000ff>int main(void)
! s- B2 l( o/ ^2 s& K" B/ r/ O{
, U4 ~* u+ @) qchar string[10]; $ K) D6 P: D G- s4 e
char *str1 = "abcdefghi"; </FONT></P>
" Y- J! s5 W4 V% V c<P><FONT color=#0000ff>stpcpy(string, str1);
2 T- ^* K5 ?8 B1 mprintf("%s\n", string); % G) X$ V( s P' R" E( {$ M7 r( f8 |
return 0;
. A, u1 K( M; H& _) U) ~' O5 X} 4 l/ x0 T# w5 d k
7 @* [" T( \( w5 X8 g
</FONT>
, {6 y$ D- @ ^; l4 p4 I6 k</P>+ k. Z6 V. D+ {) O2 M3 Z4 [% A$ q0 H
<P><FONT color=#ff0000>函数名: strcat </FONT>
2 e! Q5 p3 A* T) p功 能: 字符串拼接函数
; U1 e) e. a" k! \' T用 法: char *strcat(char *destin, char *source);
$ _5 l$ L2 ~4 k# Z% x! w/ ]程序例: </P>
; o9 g/ m+ U( p+ n( V5 v. L" B<P><FONT color=#0000ff>#include <STRING.H>
* g% q+ j) |0 H! [! g5 V#include <STDIO.H></FONT></P>
: s& v/ y9 x; s8 ~+ p$ j5 ^<P><FONT color=#0000ff>int main(void)
5 u+ {9 U6 @- t( f- P) p O{ 3 k9 x# r) T, N: Y1 P* R
char destination[25];
6 I6 i2 p/ Q l1 Zchar *blank = " ", *c = "C++", *Borland = "Borland"; </FONT></P>. [* |: ~1 O) H9 g2 ~( z8 e: f
<P><FONT color=#0000ff>strcpy(destination, Borland);
; Z; w* L& y7 x) { |' e' `" F0 Zstrcat(destination, blank); , Z& ^( R6 c* }$ y0 W' G
strcat(destination, c); </FONT></P>
/ Z3 U7 q# l8 s! z0 n4 z<P><FONT color=#0000ff>printf("%s\n", destination); , T( | I: F; J% h6 B3 k
return 0;
- x! {! Z2 J9 k6 }* ^* _+ G; n) E3 H}
7 x5 M6 l; p0 M9 o' ~" X
" z" G8 ?" w# q1 y0 s3 r</FONT><FONT color=#ff0000>
2 t" V Q/ ^* O- _</FONT></P>
1 t* D/ Y, V# `+ C4 I8 g<P><FONT color=#ff0000>函数名: strchr </FONT>
) j, x# i2 Q- z2 ^8 n" q# S功 能: 在一个串中查找给定字符的第一个匹配之处\ * T' R9 G. w$ i3 A
用 法: char *strchr(char *str, char c); / F! t; V7 w% B7 |
程序例: </P>
4 k4 O$ }' D- t6 M+ V" V8 i<P><FONT color=#0000ff>#include <STRING.H>
: Z0 T1 d8 U# ^8 y$ c#include <STDIO.H></FONT></P>
c2 k1 }4 |$ x# S- }- [<P><FONT color=#0000ff>int main(void) - p. ~. P1 O" x; @: I' s5 {
{
" r; M j5 r. q# _: P b! u+ jchar string[15];
+ G( s8 u3 [' ^: C" A- V. \; Schar *ptr, c = 'r'; </FONT></P>
. V2 X% c* q8 m8 l! C& j<P><FONT color=#0000ff>strcpy(string, "This is a string"); / S1 g% N! ?3 L6 X! J8 r
ptr = strchr(string, c); 8 @6 }2 d1 A+ n u' A
if (ptr) 7 ~9 Q( Q4 R$ x9 u5 q. A L
printf("The character %c is at position: %d\n", c, ptr-string);
! h ?6 A. s. R# W! B3 M5 Helse 9 \5 @$ t* B& P: ^
printf("The character was not found\n"); . b4 w( D4 l. o# q3 y- R3 ?( H
return 0; - X" M1 _7 L# }/ r; g0 n5 |
} 5 }# T( |8 l/ n0 d! \
</FONT>' E$ Y2 x) U8 k4 Z
' h0 j; a, E' k! F</P># L$ g/ n3 O' H$ Q1 d+ J
<P><FONT color=#ff0000>函数名: strcmp</FONT> 2 m! y2 ~& w9 F8 |
功 能: 串比较 ' n: h5 S9 h8 j8 B Y5 h) v' d
用 法: int strcmp(char *str1, char *str2);
: |4 F+ ?- [5 r- p+ }$ }程序例: </P>
+ \* W0 [' p' i& J v<P><FONT color=#0000ff>#include <STRING.H>
% U# ?: N. [0 O: L- W2 q8 O+ \#include <STDIO.H></FONT></P>
/ {7 l0 m0 e) c<P><FONT color=#0000ff>int main(void)
/ X$ }( h( V( P# Q" f{
, D0 B; T8 o g, fchar *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; g# R- b5 {9 o+ }) ~5 c# ] B
int ptr; </FONT></P>5 b8 H3 _5 v# s6 g7 I8 H, V5 q0 J7 i
<P><FONT color=#0000ff>ptr = strcmp(buf2, buf1); 1 N& M' R. k$ J
if (ptr > 0) + ~% @( j8 Q$ D+ s
printf("buffer 2 is greater than buffer 1\n");
4 ^+ H( |1 p7 k4 delse
3 p3 {* b; v: `" Wprintf("buffer 2 is less than buffer 1\n"); </FONT></P>- ^. @$ L0 o8 M+ h! n* i* w- ?
<P><FONT color=#0000ff>ptr = strcmp(buf2, buf3); 4 }% T" l$ a. z' S: H
if (ptr > 0)
! h% Z" x$ v2 O/ L8 w- s0 e9 Iprintf("buffer 2 is greater than buffer 3\n");
7 X$ T* Z$ t0 g3 Oelse
/ N3 g- D5 c$ K! W3 j5 Z1 G- @: Sprintf("buffer 2 is less than buffer 3\n"); </FONT></P>
! k/ D" B, c" p0 E" x7 Q<P><FONT color=#0000ff>return 0; 0 ~) a& [8 ^; D
} - j( g, z% F* j
3 c! a, f# u& o5 [
6 s) h4 f a5 W4 s& `' b& t3 s, d9 `</FONT></P># |9 i% d! L9 n; w! ^' o
<P><FONT color=#ff0000>函数名: strncmpi </FONT>- `9 x$ k. A$ \7 D( Q
功 能: 将一个串中的一部分与另一个串比较, 不管大小写 : [, Q4 p/ ~3 I8 p( i0 h% X
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
# z( }3 Z% X" K6 q程序例: </P>' a# ]# k% O s# N% C4 e; Z
<P><FONT color=#0000ff>#include <STRING.H>
8 Y! f- j6 M& z#include <STDIO.H></FONT></P>
/ P6 \" ^$ n) e5 H1 ?, G<P><FONT color=#0000ff>int main(void) + O1 f( n1 k' a6 v
{ $ O0 {. j! ?% Q* O2 X
char *buf1 = "BBB", *buf2 = "bbb"; " t! I. P; H9 I& \
int ptr; </FONT></P>8 T, T( c: i7 m$ N
<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>
7 e/ J" O% d8 R( ?. Z8 \<P><FONT color=#0000ff>if (ptr > 0) a' s+ H T0 }! g) Y
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>: c: I! b4 d. N$ p
<P><FONT color=#0000ff>if (ptr < 0)
( l" W$ K$ s3 t- l. q- Eprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
# e; ^9 U/ Y9 v7 a R7 J<P><FONT color=#0000ff>if (ptr == 0) ( q( \# f1 x- w/ z+ u4 i
printf("buffer 2 equals buffer 1\n"); </FONT></P>6 |' I8 R7 X& q
<P><FONT color=#0000ff>return 0; & P* W- t% {7 W
} 3 u+ {' b A. o% H# N6 O6 S4 j
7 m! Z, c% z# r: u9 a3 H% e
</FONT>( e" `7 `% l7 z$ `: s. L
</P>
+ ]+ R5 S% i5 f @<P><FONT color=#ff0000>函数名: strcpy </FONT>
& Z9 R8 O& z5 P7 U0 K功 能: 串拷贝 ' ?$ {4 h' C5 K! D% C! i
用 法: char *strcpy(char *str1, char *str2); : ^) M6 i# {- o7 ~- l. @( P" I$ Y
程序例: </P>
7 o" l1 V/ \. X @. |& V. x<P><FONT color=#0000ff>#include <STDIO.H>& S/ \" _2 ^6 b& V
#include <STRING.H></FONT></P>, f+ H* o& ~# }1 e
<P><FONT color=#0000ff>int main(void)
: z6 a! ^! p; C2 A- |8 q{
. [; E5 f. G+ D( Pchar string[10];
+ m8 L! D7 R" l- j4 c' C3 |char *str1 = "abcdefghi"; </FONT></P>5 t0 I/ h2 \& Y4 `6 {# G P- i
<P><FONT color=#0000ff>strcpy(string, str1);
, }* E1 G+ R4 c2 M$ U/ A" b$ C! Eprintf("%s\n", string);
/ r9 }& N. l! @) Sreturn 0; ) ]0 j. @$ P3 k2 P$ R
} 1 |& |+ w6 e5 Z' P; A; a# Q
</FONT>5 t' W4 N& a4 r
: H; E6 P1 J' ?7 k</P>: x, y L5 B& |. f8 G
<P><FONT color=#ff0000>函数名: strcspn </FONT># E5 f8 a3 q: O) r7 f
功 能: 在串中查找第一个给定字符集内容的段 % L, `6 p/ [( t/ k
用 法: int strcspn(char *str1, char *str2); 3 C% m! W* B% b# h8 D o( h6 J3 b
程序例: </P>
' G5 C5 Q: V6 d# r% v& O' N. P<P><FONT color=#0000ff>#include <STDIO.H>* V5 }3 h( v: g) c: p
#include <STRING.H>
8 T. [% ?, h' `#include <ALLOC.H></FONT></P> P* d6 b4 e" ^* e
<P><FONT color=#0000ff>int main(void)
$ ]! F4 A4 |& |6 g% z( m$ W# M{ # z: R$ _! K) ]6 Z; h7 B( d/ w" L
char *string1 = "1234567890"; & T( T, f9 S& b2 U
char *string2 = "747DC8"; 5 T5 V D( i& f$ a. u
int length; </FONT></P>. Y9 \5 s. g$ F
<P><FONT color=#0000ff>length = strcspn(string1, string2); % V2 r, E* h6 H3 E8 R8 L) J
printf("Character where strings intersect is at position %d\n", length); </FONT></P>
r/ x K' i8 b' s<P><FONT color=#0000ff>return 0;
5 g% _% [. B. R' x3 ^! v5 W}
3 x( j' Y9 z% S0 W2 y$ ~</FONT>8 d5 A7 u+ ]. ]
. e- @+ B' l/ N</P>
5 `7 K! J' [% i" W5 O<P><FONT color=#ff0000>函数名: strdup </FONT>0 v" }0 q/ @, t" W
功 能: 将串拷贝到新建的位置处
6 G/ U( c2 X9 T& M. q! ^: u用 法: char *strdup(char *str);
, f# q, }% |. U @程序例: </P>5 ~2 T( g6 v' `8 s; s
<P><FONT color=#0000ff>#include <STDIO.H>* M; L/ D% n: x. F/ ^
#include <STRING.H>
) @9 f, v: n' ]/ q/ y#include <ALLOC.H></FONT></P>
$ [6 e6 G+ u: u0 N% c* [% e- Z<P><FONT color=#0000ff>int main(void) 9 |+ Q$ f% R; x
{
* m9 p; W! J% `9 c/ n- z8 y: ~. `char *dup_str, *string = "abcde"; </FONT></P>
/ @: C9 [$ T1 r1 h+ h<P><FONT color=#0000ff>dup_str = strdup(string);
- G0 F9 s* M+ ^1 Jprintf("%s\n", dup_str);
! g: G4 \6 D1 V( i$ l X3 d) R0 Y8 \7 S" ofree(dup_str); </FONT></P>$ z6 {/ L! |3 N
<P><FONT color=#0000ff>return 0;
- O1 v+ M& Y. n2 i) @+ |}
; A/ I5 S& t5 a8 d- d1 X! n2 k5 p6 e) V4 t4 l
</FONT>6 l- |) G6 l) P3 L
</P>( K- ^4 U0 I) D6 m' {
<P><FONT color=#ff0000>函数名: stricmp </FONT>& Q) b1 r' O7 V/ T$ a$ W& ]
功 能: 以大小写不敏感方式比较两个串
% ~, b" D$ z! I2 S用 法: int stricmp(char *str1, char *str2);
5 n9 X1 b6 S" y3 k程序例: </P>
: n$ c- {& ~% f3 T% Q<P><FONT color=#0000ff>#include <STRING.H>
4 `; E( f. H _$ @8 U6 T( R#include <STDIO.H></FONT></P>
$ T, ]& k0 L7 f, w2 H1 a+ f0 l7 Y \& h' K<P><FONT color=#0000ff>int main(void) # T p, g0 y% d! H" Z5 N3 d
{ 7 @( a# C5 y- g4 t1 ?+ a' n: H s; K
char *buf1 = "BBB", *buf2 = "bbb"; 4 ?: A' ]7 v" n$ [5 W
int ptr; </FONT></P>- @/ K9 H7 X3 f+ S6 I" {' d
<P><FONT color=#0000ff>ptr = stricmp(buf2, buf1); </FONT></P>8 f/ F/ f4 c2 d' I' g
<P><FONT color=#0000ff>if (ptr > 0)
' M, L5 o, C$ {2 v3 D& Q$ S& Sprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>% D0 l/ t4 N* v6 C& U" d
<P><FONT color=#0000ff>if (ptr < 0)
% ` I' ^* O L, G9 O1 Jprintf("buffer 2 is less than buffer 1\n"); </FONT></P>( s8 H1 w$ X$ j5 S/ E. F6 }
<P><FONT color=#0000ff>if (ptr == 0) " g0 K2 N: j. D' G+ @- n! Y* c
printf("buffer 2 equals buffer 1\n"); </FONT></P>
/ A. f. Z( M5 a- R0 |<P><FONT color=#0000ff>return 0; : T, G+ _6 N' o N5 W A! {
}
" z( Y& W+ R: m% { s</FONT>
& ?' K, N2 q3 g4 V, n' Q</P>% P0 i& X8 D' @% M" w4 v
<P><FONT color=#ff0000>函数名: strerror </FONT>
, t$ }3 J0 }# s* V4 v功 能: 返回指向错误信息字符串的指针 8 P j( d% T# r9 ^
用 法: char *strerror(int errnum); ' j$ @. E4 m7 S2 |/ c, Q* \. U
程序例: </P>7 D- m! m" c; T( V
<P><FONT color=#0000ff>#include <STDIO.H>
: u3 q: o3 f* w- y( T4 _#include <ERRNO.H></FONT></P>8 b( U9 y! v& e, U- M
<P><FONT color=#0000ff>int main(void) & A' n) s2 V4 G- J# w O
{ 1 J9 Q5 s- r3 |2 |
char *buffer; 5 [7 } q3 A8 k$ u
buffer = strerror(errno);
) Y- I% P5 {6 _ b' L5 e9 T' w2 Dprintf("Error: %s\n", buffer);
' k& a1 u( A3 D& a1 u G8 R& Hreturn 0; 0 L {9 g6 l+ W0 D' `4 g
} " v7 l9 S' p# C6 g7 K( m
9 i! ]) T8 ?, w7 w+ {7 P5 u/ [</FONT>, t! Q0 u7 \9 e5 H/ |; r% @
</P>% W: m! r1 H0 @; M
<P><FONT color=#ff0000>函数名: strcmpi </FONT>
& W: A% S" M* O6 A5 e, R功 能: 将一个串与另一个比较, 不管大小写
! d' f- ~1 c# f4 S1 ]: `. n0 f; q用 法: int strcmpi(char *str1, char *str2); 5 ~# s/ L9 d/ M2 h/ b z
程序例: </P>
6 B& }% P' p) B& c8 N: h<P><FONT color=#0000ff>#include <STRING.H>. v: b7 m& P: Y/ P+ g0 ~5 X
#include <STDIO.H></FONT></P>
# g1 T# _, d: s. q8 K# w/ `<P><FONT color=#0000ff>int main(void)
- C+ N4 _. a O; I }2 ^* d{ * z: u0 K L/ E/ h
char *buf1 = "BBB", *buf2 = "bbb"; 0 s' q# U0 _3 S' i% J
int ptr; </FONT></P>
; |$ ^5 ~4 Z) {) n8 J<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>* q, P* _" l7 I3 ?* Y
<P><FONT color=#0000ff>if (ptr > 0) 5 L! E; p5 \& B3 d6 ~
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>/ I; f" n% r( Q" J$ i- D9 j
<P><FONT color=#0000ff>if (ptr < 0) 9 x9 r* R# S& t' s
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
3 a- a$ \! f! h* h4 v( x<P><FONT color=#0000ff>if (ptr == 0) ( i# r, k' h3 J! p% ]# ~" g9 h
printf("buffer 2 equals buffer 1\n"); </FONT></P>- ^% T% z9 @% @: q' u( Q% b
<P><FONT color=#0000ff>return 0; . N+ t" d2 T/ M1 n
} , | o. Q9 s9 i" n2 D2 v
</FONT># x3 r/ _# I0 u+ v
, g( k" q- P0 Z: \6 i</P>
c) ?+ Y6 Z ^6 i+ F' `2 J' z" |<P><FONT color=#ff0000>函数名: strncmp </FONT>: v3 ]- r' K; F
功 能: 串比较
' ]( E+ T F: O8 T5 ~4 E1 y- z |用 法: int strncmp(char *str1, char *str2, int maxlen); ) d5 R8 F2 c2 y# E
程序例: </P>
+ W! k |! L7 Y2 N<P><FONT color=#0000ff>#include <STRING.H>
) }! O0 d0 B5 Q; D1 g% B#include <STDIO.H></FONT></P>
" j7 O% p4 c3 m2 K<P><FONT color=#0000ff>int main(void) </FONT></P>
( ]! d0 X$ S+ i. y* R. l0 y" o" m<P><FONT color=#0000ff>{ ' f! p! c* i) q1 |$ [
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; S+ ]1 b! |/ G! z
int ptr; </FONT></P>
* S; x/ d; k% u3 l% c W/ l. l<P><FONT color=#0000ff>ptr = strncmp(buf2,buf1,3); 3 N/ C' `5 ^5 B
if (ptr > 0) * ` o6 I& m4 W8 f6 U
printf("buffer 2 is greater than buffer 1\n");
2 }( x& e2 Q7 C: p5 celse
8 N7 W" f/ z$ _$ I0 uprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
- V/ J V' g. e; `2 q* _$ p<P><FONT color=#0000ff>ptr = strncmp(buf2,buf3,3); , Y! p. r+ J% C- f" h: d$ t
if (ptr > 0)
) r7 E, f# N+ f* d& p; vprintf("buffer 2 is greater than buffer 3\n"); 5 Q$ A2 z$ R" } @( z
else " _1 \1 z, g- |$ R, c0 C
printf("buffer 2 is less than buffer 3\n"); </FONT></P>$ u* A, c/ Z- l( T! b" s: m
<P><FONT color=#0000ff>return(0);
8 @% W1 w; ^+ ]} & c0 S/ J: S# @: O! x1 ~4 x
( a! a; V+ d4 m0 t/ {0 V
</FONT></P>- C7 J' R; s9 o, o
<P><FONT color=#ff0000>函数名: strncmpi </FONT>
: L. u% v# Y ~功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写 + j. X6 g9 `( u' Y& V& [1 E
用 法: int strncmpi(char *str1, char *str2); ) Z- @# ~/ x. W2 w
程序例: </P>
3 y$ b6 } Q! D( t<P><FONT color=#0000ff>#include <STRING.H> ^6 L$ _- a7 ?3 s' s" r+ W9 p
#include <STDIO.H></FONT></P>
. v$ c( F+ w# b<P><FONT color=#0000ff>int main(void)
" |" N# C. _" z7 R% g3 B A- ]( y{
- @2 _) U7 c9 @8 C0 L b3 Mchar *buf1 = "BBBccc", *buf2 = "bbbccc"; ! C8 z, v. U0 u( j7 @
int ptr; </FONT></P>
& {1 @7 G! ` j# L& J8 C<P><FONT color=#0000ff>ptr = strncmpi(buf2,buf1,3); </FONT></P>/ }) B2 @4 o1 R6 [# ~, g( M* g ?
<P><FONT color=#0000ff>if (ptr > 0) + N: A$ |% J' O5 w0 P
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>; ]# j* ?* v L) ?2 t! p9 H
<P><FONT color=#0000ff>if (ptr < 0)
# ~8 z" _. q- ~6 \& E- `* ]printf("buffer 2 is less than buffer 1\n"); </FONT></P>+ |% z- x5 \3 G% n% y2 R3 h' Z
<P><FONT color=#0000ff>if (ptr == 0)
+ S2 o6 i/ X# \$ ^: zprintf("buffer 2 equals buffer 1\n"); </FONT></P>, h9 U5 f* U1 y- h# r9 V |- {
<P><FONT color=#0000ff>return 0; </FONT>& L) O1 q% n# h; q- ?/ [0 d8 ^* b& g3 r
} 8 R4 w+ j8 i1 R3 }
* a: V/ {9 E# o
</P>
8 N( |* _# a' A) n) Y, u! [8 n8 V9 ?<P><FONT color=#ff0000>函数名: strncpy </FONT>
# K/ V. t- |3 k% b功 能: 串拷贝
. A7 h% X+ H) S! Q. m1 L# S7 l用 法: char *strncpy(char *destin, char *source, int maxlen); - L) T7 h, V1 y. i) b( K) g
程序例: </P>' u6 N3 L" x3 y# M
<P><FONT color=#0000ff>#include <STDIO.H>$ X* D9 M7 ?3 j& X' E
#include <STRING.H></FONT></P>* Z3 C3 |* x( |/ k7 s. F8 ?
<P><FONT color=#0000ff>int main(void) F6 }& ^/ \8 [, I( d. T% q) x
{ 6 o' L" ~' P4 l+ ^2 |" y0 D
char string[10];
" a. D! w. T# ~7 @" ^char *str1 = "abcdefghi"; </FONT></P>
0 P& c; F% x# q' G% W: B<P><FONT color=#0000ff>strncpy(string, str1, 3); : e' ~1 s6 `9 P, [7 A
string[3] = '\0'; J' V& l( u) N$ R7 m
printf("%s\n", string); " i4 `. {+ L4 J. ?' h
return 0; , ~. K& P- B5 O- Q% y- z: A
} 8 q8 ?$ @6 ^8 f" P( O5 j$ X* w
</FONT>/ M& I% M B" z/ b
</P>1 Y3 k- s' H+ w
<P><FONT color=#ff0000>函数名: strnicmp </FONT>& Q5 o+ G- D* O4 J
功 能: 不注重大小写地比较两个串
8 m7 G+ }" O& ?# u6 e/ [- d用 法: int strnicmp(char *str1, char *str2, unsigned maxlen); ! |; ^" h0 G) h1 o! ^5 T, o( A0 {
程序例: </P>$ u0 n; I& L* N: Y3 l; D8 l
<P><FONT color=#0000ff>#include <STRING.H>
8 `# q6 H& k# T0 o; D" R( j#include <STDIO.H></FONT></P>. U& {% U% F! |8 b$ j& T# q
<P><FONT color=#0000ff>int main(void)
+ `: S* P0 M5 a7 p, W( S) y{ . R q3 m+ H' M+ J2 L
char *buf1 = "BBBccc", *buf2 = "bbbccc"; . H8 O3 u P& r a9 a* o
int ptr; </FONT></P>9 ^6 A, n- ~0 L4 k
<P><FONT color=#0000ff>ptr = strnicmp(buf2, buf1, 3); </FONT></P>1 l$ ]3 m3 w9 I
<P><FONT color=#0000ff>if (ptr > 0)
7 a |+ G) L9 [) K2 Yprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
L4 h% u# v( J7 O<P><FONT color=#0000ff>if (ptr < 0)
8 s4 p* a8 K7 jprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
* \4 K; i3 b; A8 X0 f<P><FONT color=#0000ff>if (ptr == 0)
5 R4 y3 E' Y+ ]3 L' l/ jprintf("buffer 2 equals buffer 1\n"); </FONT></P>' j2 j. I. @1 n7 z4 _) j3 n
<P><FONT color=#0000ff>return 0;
8 B; l+ f9 f3 Q: g2 i( c} + l0 w7 r9 g" {! C% b1 y
* T4 k; ]1 O) y: K. H
</FONT>
7 r& j1 n) r, }</P>
6 p) A3 _* Z& x. G2 d<P><FONT color=#ff0000>函数名: strnset </FONT>
, V) e/ B2 i2 |1 B( G功 能: 将一个串中的所有字符都设为指定字符
1 g9 D; q4 N* V1 A, B5 u用 法: char *strnset(char *str, char ch, unsigned n);
) J3 V. T0 O; b; A程序例: </P>) D- B7 d* J4 I6 h3 E
<P><FONT color=#0000ff>#include <STDIO.H>
8 E/ l2 \( C, W6 c [: K; ]& g#include <STRING.H></FONT></P>* \+ W' W# |, h" ?
<P><FONT color=#0000ff>int main(void) 6 Z7 S3 Y' i- c$ C1 _
{ 9 E, z+ m. y* \, V# r
char *string = "abcdefghijklmnopqrstuvwxyz";
$ q) D6 z+ m. y% p$ Hchar letter = 'x'; </FONT></P>
( t$ n3 v: F$ \+ }<P><FONT color=#0000ff>printf("string before strnset: %s\n", string);
% ? {2 w% v& X& u$ Y% v- e0 v, Wstrnset(string, letter, 13);
# Y' b, v* G3 n$ K9 b8 s& `2 iprintf("string after strnset: %s\n", string); </FONT></P> e! w: G0 K4 _5 {
<P><FONT color=#0000ff>return 0; 3 [+ O- ?, r7 B- w: e: J
} : U8 I7 v! O& y7 s! j
</FONT>
0 S! Y' Q. j4 B. T$ Y) w' r3 z</P>- n' D, D1 x0 v* |1 x. c. e: e
<P><FONT color=#ff0000>函数名: strpbrk </FONT>
1 F$ p# _( U- D* L' N功 能: 在串中查找给定字符集中的字符 4 I1 p+ I: H3 B9 O, O
用 法: char *strpbrk(char *str1, char *str2); $ F" R' V6 z" Y+ ^2 n) o* w
程序例: </P> m9 P/ a0 ^# U* K/ r f8 u
<P><FONT color=#0000ff>#include <STDIO.H># }6 c3 ]1 E) U1 g3 v
#include <STRING.H></FONT></P>! ~, ]. \7 n5 M( j: w$ e, a/ ^6 i
<P><FONT color=#0000ff>int main(void)
# s+ P& P! c8 v' l, r: [6 F9 I \# o{ 0 l3 p! L8 }" t
char *string1 = "abcdefghijklmnopqrstuvwxyz"; 4 F" J% ]7 W- T5 R) }
char *string2 = "onm"; 6 f: Q7 A7 d$ s
char *ptr; </FONT></P>
" D% r9 L* H/ Z2 Y' l<P><FONT color=#0000ff>ptr = strpbrk(string1, string2); </FONT></P>
$ H% p- W4 G+ ]8 d! ]<P><FONT color=#0000ff>if (ptr)
* r3 S3 ]2 b* V% B. Bprintf("strpbrk found first character: %c\n", *ptr);
' f7 Q( K/ R: d/ W$ ~6 r [else 2 d, x! P+ q3 d# }' `0 _2 Q
printf("strpbrk didn't find character in set\n"); </FONT></P>; }- B- ]. n9 h4 O. {
<P><FONT color=#0000ff>return 0;
7 e5 B" ^9 ]) K& m} % |5 @, Y, ]0 e- d. c5 N- _7 e
1 b/ _, A x* h7 Y
</FONT>8 y# o5 _" `+ O( M0 p1 L3 b# x
</P>
7 k, j7 d9 t+ U9 u+ m/ ]5 ^) h<P><FONT color=#ff0000>函数名: strrchr </FONT>
% t1 p7 {' W+ s6 }$ g功 能: 在串中查找指定字符的最后一个出现
& z; }: I; V5 g" i8 N用 法: char *strrchr(char *str, char c);
3 Y% u f. s; p# ^) i程序例: </P>
7 p- D1 c# a9 Q' ?, b+ E6 J<P><FONT color=#0000ff>#include <STRING.H>
9 F. Y4 H/ G& _8 j8 ]7 h3 k#include <STDIO.H></FONT></P>. ?1 m8 Q |3 Q% q
<P><FONT color=#0000ff>int main(void)
7 K. I+ b/ n0 `* k. b: f! U, m0 _9 @{ : l: d, V# m1 V7 M) E
char string[15];
1 E" O0 j1 X1 M+ P; I( ?char *ptr, c = 'r'; </FONT></P>
3 B$ r' W& V. C1 n/ A<P><FONT color=#0000ff>strcpy(string, "This is a string"); 8 s7 D* u% f. Z7 ?# w- S& F C7 Q
ptr = strrchr(string, c);
6 ?7 \" j* Z" d/ f2 D$ xif (ptr) $ t3 D9 N& N3 U( J- s
printf("The character %c is at position: %d\n", c, ptr-string);
* U5 q. A! m( q9 P. i( }9 s% ielse ( L0 N0 H6 @# }7 R, Q
printf("The character was not found\n");
9 j4 _6 L; q& W5 `! w1 y+ Vreturn 0; 7 G3 x2 o! X! t0 r& U- y6 m; ]( f1 {
} </FONT>
/ b9 u! {2 i' C/ O' l/ `! j" w2 p0 I; }. [; a) P) P
. w. Z2 _. y# |, p</P>7 ~; U; G" T3 `9 B; t
<P><FONT color=#ff0000>函数名: strrev </FONT>: x/ }7 y) A" q
功 能: 串倒转
6 O( n& U% I: l. P' n9 D# Z用 法: char *strrev(char *str); 4 \7 D( \3 s9 _/ F
程序例: </P>
+ I% H' R2 Q2 C( F/ y1 y<P><FONT color=#0000ff>#include <STRING.H>
7 ^9 x& _5 @3 Q5 w' q; h#include <STDIO.H></FONT></P>% Q: K# N1 f$ m' K+ h9 S3 X
<P><FONT color=#0000ff>int main(void)
$ z- | t+ V, o5 _9 v3 V8 ?{ . A( ?* X9 s, b- }% [3 n% s( I
char *forward = "string"; </FONT></P>
3 A8 X% e q# _<P><FONT color=#0000ff>printf("Before strrev(): %s\n", forward);
6 K U9 ]) ~* H* z7 V% Estrrev(forward);
1 a$ L: {8 v4 G; [7 ?/ ]# B B- l; u' ]printf("After strrev(): %s\n", forward); : q( X% \/ W0 N7 ~; x) P1 x6 Y
return 0;
* g1 l' B) b- H, Q/ X} </FONT>
( f# i; S3 [* r' W</P>- A2 i# D$ i9 C6 @
<P><FONT color=#ff0000>函数名: strset </FONT>
( q, g: C! n5 X: |功 能: 将一个串中的所有字符都设为指定字符 ) `8 n& B/ t: J9 p$ r# E+ t* R
用 法: char *strset(char *str, char c); ( n0 m5 d z( }# S& B: h
程序例: </P>1 W+ K* Q& V8 Y, f
<P><FONT color=#0000ff>#include <STDIO.H>) D2 v3 ?! @3 [3 e8 |
#include <STRING.H></FONT></P>) K- f1 `7 z- i/ U1 S, r
<P><FONT color=#0000ff>int main(void) * l/ }0 N3 j, T) C- i3 z7 R' i! i
{
5 b" o6 p. V" r4 n' e: W Q9 bchar string[10] = "123456789"; : g; ^6 i9 O! T2 M8 I! v8 u7 Z
char symbol = 'c'; </FONT></P>
# P+ ?0 S5 o" b" ]7 r( P<P><FONT color=#0000ff>printf("Before strset(): %s\n", string);
8 t7 } D+ L; e7 R' ustrset(string, symbol); $ a& j' x+ S B- W* E/ V
printf("After strset(): %s\n", string); " d6 e) x/ D5 u9 z" t5 t- y! D+ }
return 0;
4 L7 ?$ R: {) `: {} / a- K# U3 v* \# O
+ ]8 o, e/ A* s! \7 O
</FONT>
$ E- _1 T. F6 r9 e- }- s$ ~</P>
2 ]; g* P4 L$ J" A$ r; s<P><FONT color=#ff0000>函数名: strspn </FONT>
1 x5 Z# e5 b+ L$ w功 能: 在串中查找指定字符集的子集的第一次出现 : O# |2 l |0 X2 d- v1 T9 F
用 法: int strspn(char *str1, char *str2); + v; r0 _& j* |
程序例: </P>; N d6 c' z8 h4 H. R. V- U
<P><FONT color=#0000ff>#include <STDIO.H>! j" u4 O" @' g9 w
#include <STRING.H>: a: S5 [- P8 u* D
#include <ALLOC.H></FONT></P>3 Y2 S' H8 Y2 x' J9 `1 f
<P><FONT color=#0000ff>int main(void)
Q0 x$ o' L O{ - V: h p( ]' y. O
char *string1 = "1234567890"; % t6 i& X* y( Y0 B
char *string2 = "123DC8";
! \: j Q& Q$ E. kint length; </FONT></P>- a$ o) t1 x' j! i
<P><FONT color=#0000ff>length = strspn(string1, string2);
9 P* b+ L4 Q1 V Y9 _$ M, o% oprintf("Character where strings differ is at position %d\n", length); q/ l, B! V' A4 ?3 C
return 0;
4 _. S- o, v; B( C}
q: h: k6 g$ E6 ^</FONT>3 T2 h. b8 E& u7 Z6 f1 E+ ^8 ]1 q
</P>; o1 s; ^; d/ O8 @" [
<P><FONT color=#ff0000>函数名: strstr </FONT>
+ p, L3 ?1 H, [' J- \功 能: 在串中查找指定字符串的第一次出现
2 w, w$ s) e. c0 w用 法: char *strstr(char *str1, char *str2); 4 ^6 w1 n" c$ j
程序例: </P>
- K' T- ~* n; `4 c, k<P><FONT color=#0000ff>#include <STDIO.H>: A" A; f2 _0 B0 o# |
#include <STRING.H></FONT></P> _- S- c7 S7 b9 a. C
<P><FONT color=#0000ff>int main(void)
% ~8 n! z1 o% `. z- q{ 4 d/ z5 Y7 _' v5 g$ ^$ B9 a
char *str1 = "Borland International", *str2 = "nation", *ptr; </FONT></P>
$ P" H( e' b; h. J6 y) q1 H. [<P><FONT color=#0000ff>ptr = strstr(str1, str2); 9 g' d; `3 M2 n& _( x
printf("The substring is: %s\n", ptr);
7 q- z4 B/ C9 f# J5 Areturn 0;
2 G ^8 ^" o2 c( n} </FONT> A- H6 I0 T/ {! K1 n
7 u; ^6 Q% c2 B f D
</P>
, P [4 d* E G3 H% d<P><FONT color=#ff0000>函数名: strtod </FONT>$ h0 d9 K% g& \* v8 }. l
功 能: 将字符串转换为double型值
8 t' @, K# R7 P t0 x; b* }用 法: double strtod(char *str, char **endptr); ' N1 G2 d" R! N9 t& Q( e! X R% x
程序例: </P># J0 y" n1 W, o
<P><FONT color=#0000ff>#include <STDIO.H>
' F) L% G" `" n#include <STDLIB.H></FONT></P>7 s# g1 F" J0 X ^/ q
<P><FONT color=#0000ff>int main(void)
7 a5 o2 N. O8 I, \$ f3 N* u6 s5 n. ]{
6 u p C5 ?0 F6 j. @char input[80], *endptr;
: P) ? ~9 z0 xdouble value; </FONT></P>& s: Z3 i" `2 |
<P><FONT color=#0000ff>printf("Enter a floating point number:");
$ Y* z- J- x* D! I7 ]/ c0 w' _gets(input); 0 k( g/ h( ^: ~# L
value = strtod(input, &endptr); 9 k7 }4 o# D7 T7 R
printf("The string is %s the number is %lf\n", input, value); * f$ B1 r+ r+ u8 n0 _+ T( v7 r
return 0;
+ ^( m6 l1 [, p} 4 c) k" u3 V: L- ], N
</FONT>
: d+ D/ L3 r3 J
% U) q8 @$ L$ E$ l' w</P>
/ K U& L1 m8 {6 f' T( g# }<P><FONT color=#ff0000>函数名: strtok </FONT>2 H$ T2 z/ w! ]- D" v; a
功 能: 查找由在第二个串中指定的分界符分隔开的单词 0 u+ k+ t5 u' F: C0 `
用 法: char *strtok(char *str1, char *str2);
1 m- `" k# P7 l& f) Y- N7 O9 y程序例: </P>
* h; P6 M$ n6 A I$ W<P><FONT color=#0000ff>#include <STRING.H>
3 @, G$ e& D9 t, p0 @$ G2 ^#include <STDIO.H></FONT></P>
8 `# y( u; B* {- @<P><FONT color=#0000ff>int main(void)
& d" o/ }7 ~& I) v) J, T; K{ 1 u; ^% D* v/ S0 I
char input[16] = "abc,d"; ! h, u D, y) J0 G
char *p; </FONT></P>
/ D7 @+ @( c, e+ O0 c<P><FONT color=#0000ff>/* strtok places a NULL terminator
! A* F" e! D5 {: v2 G3 tin front of the token, if found */ 1 b2 Q1 Q) p2 z! d! ~
p = strtok(input, ","); 4 c4 B. _" |. N" q g6 z0 z
if (p) printf("%s\n", p); </FONT></P>; k( J8 {# ^4 G% g
<P><FONT color=#0000ff>/* A second call to strtok using a NULL
- T5 g: w. c" Z% W# ^ O0 gas the first parameter returns a pointer
, V7 _; J/ n8 k4 T3 c, J# ]8 @+ w* R7 dto the character following the token */
: k4 L$ g1 D8 S; E7 x1 lp = strtok(NULL, ","); / z4 j- H( n: ? A9 {' ~- I7 i( ?6 a
if (p) printf("%s\n", p); " _- x O+ ?8 @" o" ?! H; I! Z
return 0; 1 y, y& Z7 l6 B% {/ X0 S
} k+ G5 Z- X" P0 d
9 r, }% j/ U! j
0 @& d" N. o, | D8 L</FONT></P>6 p- k. {: K. ]
<P><FONT color=#ff0000>函数名: strtol </FONT>
2 i) V, L7 t& ?功 能: 将串转换为长整数 1 E6 @* H) D7 }# d: b, L( `9 x
用 法: long strtol(char *str, char **endptr, int base);
! c6 q# V. V3 m5 f/ ]程序例: </P>
4 |! o$ R% i# V; r<P><FONT color=#0000ff>#include <STDLIB.H>
2 q+ V/ k* j3 y- M7 @3 J7 m: D% z#include <STDIO.H></FONT></P>1 u$ N) C* U/ |1 @( ], n2 c' L: p
<P><FONT color=#0000ff>int main(void)
' g6 E# [. u% i' ]+ ]{
- w3 Y' K4 _7 Z6 G q7 Ochar *string = "87654321", *endptr;
; V/ \3 c9 F4 j- q) G) {: q$ g& Wlong lnumber; </FONT></P>
1 e/ l1 r- Z& \/ y- F<P><FONT color=#0000ff>/* strtol converts string to long integer */
5 H/ A6 R& A' v9 ylnumber = strtol(string, &endptr, 10); 1 t7 Q* O9 o a# O( Q9 M Q6 T
printf("string = %s long = %ld\n", string, lnumber); </FONT></P>
& V+ J( n+ |% F5 @, R<P><FONT color=#0000ff>return 0;
! A' H9 b% U" y1 c- ^" }* Y1 {} </FONT>' R G, l) I3 h8 p6 A/ e: p
</P>
7 `( z( h2 B. s0 ]<P><FONT color=#ff0000>函数名: strupr </FONT>
6 Q4 a8 N( ?% A: c# [' s' W功 能: 将串中的小写字母转换为大写字母
) K& N" ?3 S3 W! b0 e8 n# Y0 i' w用 法: char *strupr(char *str);
2 N) ]) a% l% g3 C* C$ v1 ]$ y/ @程序例: </P>% C, p/ g" V" d6 v1 ~ b+ K+ Q- i
<P><FONT color=#0000ff>#include <STDIO.H>
4 Y R! Z% A$ q! |( d# I#include <STRING.H></FONT></P>
/ h$ L7 P( @3 B. N/ u, u5 S( I- b<P><FONT color=#0000ff>int main(void) 5 d3 x5 `1 b7 X) w) r5 H l' U
{ 6 P% A9 g3 s* S. G
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; </FONT></P>0 Y( X8 l9 Y c; y
<P><FONT color=#0000ff>/* converts string to upper case characters */
8 [3 a$ C- Q$ M4 m4 e4 Z/ Xptr = strupr(string);
8 x' b. R' p( Q* X. {printf("%s\n", ptr); 3 n# `8 v+ i7 e
return 0; 9 d! K$ u+ @" Q4 R9 m& Y) L
} - C+ L! ^5 m9 D0 n- u( Z3 M
' a% j8 o" `) ~: c</FONT>! Y }: i* j' H8 M- p3 {/ f
</P>
4 q: A+ M: c5 D% p% O<P><FONT color=#ff0000>函数名: swab </FONT>
: K" o( F% O B; y+ {% y功 能: 交换字节 1 p; z3 b% n# `, K1 d x6 |
用 法: void swab (char *from, char *to, int nbytes);
( v. n. v f- r4 j- J; j; O- B' U程序例: </P>
* V1 ~" G# F* r) _- x3 w1 _5 `* f<P><FONT color=#0000ff>#include <STDLIB.H>
3 R6 @. [# d- I; H#include <STDIO.H>* ^5 |& U' U w2 g( \" a& v
#include <STRING.H></FONT></P>
3 W& [: F' c* u) l<P><FONT color=#0000ff>char source[15] = "rFna koBlrna d"; : D6 P) U" J" t- S' k! c8 i5 i
char target[15]; </FONT></P>& n* m* Z8 W7 C0 G4 \5 x) U
<P><FONT color=#0000ff>int main(void) , ?1 B' ]! ?0 L
{
1 w. U4 h, J. n1 f% R, ]# _' xswab(source, target, strlen(source));
. `- C, x' m6 f! Q- `3 ^0 b nprintf("This is target: %s\n", target); " q. H$ c! i# h# U
return 0; ! h, t! b% j- ~
} ! ~2 s& }, C6 ]" C# A7 n9 A# k
</FONT>
% U; a6 t% i- y6 K4 m! f- R# g6 s8 Q' k7 O) g/ ~6 E
</P>
, P1 _5 t7 E: _. R7 D! g<P><FONT color=#ff0000>函数名: system </FONT>& \' ^, \ \; [) w
功 能: 发出一个DOS命令 * B2 x( w2 x, U0 t
用 法: int system(char *command);
' m1 z% Z9 h( O8 `/ f程序例: </P>
1 ~+ V8 v. O5 V( R<P><FONT color=#0000ff>#include <STDLIB.H>9 T- S5 D7 B* h8 V% k& z a/ X. L
#include <STDIO.H></FONT></P>
) }/ ~6 V1 `5 |% a+ g& g<P><FONT color=#0000ff>int main(void) |. R0 d2 Y# d& A/ L
{ $ J- e% @2 y! d9 P/ |1 {, X
printf("About to spawn command.com and run a DOS command\n");
; f+ I1 L _# U/ U0 ]system("dir"); 8 r- w9 M) G c( ?
return 0;
$ D. Y- S! n& |% \% S} </FONT></P> |
zan
|