- 在线时间
- 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>8 h: Q; [5 K( N: {+ h; y/ N
功 能: 改变数据段空间位置 # @" H0 J0 G+ k' H, T; F
用 法: char *sbrk(int incr); * D8 ~" i a5 f7 r4 d: g
程序例: </P>
7 X/ g# w$ E* c' ]< ><FONT color=#0000ff>#include <STDIO.H>; L& {# m/ I8 P h' s: x
#include <ALLOC.H></FONT></P>6 p9 S$ V. C& [ ~- h% S
< ><FONT color=#0000ff>int main(void)
- M; z% v9 ^8 k/ \8 q{ * S/ F" L7 y* G6 S0 J( T
printf("Changing allocation with sbrk()\n");
7 l5 \) j1 o! cprintf("Before sbrk() call: %lu bytes free\n", / T5 D: w% `! y
(unsigned long) coreleft()); 9 K5 | x' }$ s
sbrk(1000);
: h. f. t4 Y( {2 T' c; q! @printf(" After sbrk() call: %lu bytes free\n", 8 a0 y7 v* i; D' r2 m
(unsigned long) coreleft());
2 f- l" |0 P: ?: O* q& Ureturn 0; / P8 c7 | N# q8 F' U5 a
}
6 {- x+ X. \+ U3 R+ m3 [
( S& ?( j4 ?+ a' y1 W3 d</FONT></P>0 p# k" y* `4 i7 T: V
< ><FONT color=#ff0000>函数名: scanf </FONT>) U) ^; t+ E: k5 h) B1 z
功 能: 执行格式化输入 * ?, s p: v- H8 z4 K
用 法: int scanf(char *format[,argument,...]);
& F2 k$ e+ E! v, d* F程序例: </P> r) o8 J& N Q$ G
< ><FONT color=#0000ff>#include <STDIO.H>
( B& @7 Z, y' }/ B2 F* _#include <CONIO.H></FONT></P>
$ F1 j W$ U% M0 X: e! {7 b< ><FONT color=#0000ff>int main(void)
! H; R( U; H2 p) C( E$ @' H& E{ , f$ m- | ~: R! E- c6 k. ?
char label[20]; . c3 y" c) v, U8 x, l
char name[20]; # C+ p! D1 j2 ^" D* N
int entries = 0;
% T8 ^6 z. x2 I# |7 @2 {int loop, age; ) N3 b* Q/ }! s( N5 Z* r* b/ K
double salary; </FONT></P>
u# H- O2 ?& }2 U& J- B% ~" u4 W< ><FONT color=#0000ff>struct Entry_struct # e9 \( L1 M2 J) m
{ 7 e2 `2 D& t. ]+ a0 j, u
char name[20]; # a: B6 c* V9 u) c% Q: M2 G" V
int age; 7 T) ?$ i k/ X2 S
float salary;
" F2 n/ L' q6 k p. m} entry[20]; </FONT></P>+ I) e1 _5 c% Y8 V
< ><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
$ a2 `% l0 ~4 \7 `printf("\n\nPlease enter a label for the chart: ");
& H& K" d0 Z x( x0 uscanf("%20s", label); 8 @6 j- t& I1 T6 d5 B9 V
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
1 g. k% c* r! K: W0 P1 d8 H0 u$ w< ><FONT color=#0000ff>/* Input number of entries as an integer */
* `3 X+ o7 o1 j" iprintf("How many entries will there be? (less than 20) "); ) [2 s9 |8 m: [2 f9 Q
scanf("%d", &entries);
0 E2 m. \, K, u( \fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>" _+ S* x! p# E9 N* S( A
< ><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ ' {, c* K+ E( N% C/ O+ Y+ @
for (loop=0;loop<ENTRIES;++LOOP) 8 S# R' ^. \! o* c
{ " w; T6 Q4 H4 }
printf("Entry %d\n", loop);
8 {( ?' k' @. V0 ^printf(" Name : ");
1 t2 C) b5 Q: y$ m% d* ?& Y- H* Sscanf("%[A-Za-z]", entry[loop].name); 4 f+ b. h" K2 [
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
4 ~# b) @8 l' Z5 g# Q< ><FONT color=#0000ff>/* input an age as an integer */ / g1 r5 Q4 n1 ~8 Q" e
printf(" Age : ");
, \' j" K) g, ~* Wscanf("%d", &entry[loop].age); # S& B- [; H' @- Z
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P> [! M% z! C/ o. s
< ><FONT color=#0000ff>/* input a salary as a float */ 6 [0 g1 A- ~' d7 q# ^- w8 }
printf(" Salary : "); $ F* N6 C5 ?, K1 d/ m' \9 J& \
scanf("%f", &entry[loop].salary); 2 u/ H, X- J! I* S5 U+ h/ d0 Z
fflush(stdin); /* flush the input stream in case of bad input */ ( l0 h: A0 V M4 G
} </FONT></P>
2 k# l1 U4 s, Z5 b' F< ><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */ S/ T( C0 k8 q, |3 }
printf("\nPlease enter your name, age and salary\n"); # e$ g6 J5 {4 E& F
scanf("%20s %d %lf", name, &age, &salary);
" ~7 n& P+ S# ~% p) o6 {</FONT></P>4 p! m$ C; G' W' e0 T& J
< ><FONT color=#0000ff>/* Print out the data that was input */
6 D, _# K, r6 Y7 j0 _; {printf("\n\nTable %s\n",label);
( B2 P4 l' q0 Oprintf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
4 S- Y- W% }% j: sprintf("-----------------------------------------------------\n"); % O. \0 O8 j! M( m9 F" S
for (loop=0;loop<ENTRIES;++LOOP) ! i- J5 o% ~9 [& R4 T, H
printf("%4d | %-20s | %5d | %15.2lf\n",
$ M; |: ]' }) _% F7 Vloop + 1, 3 W1 E p- p- s2 c" x
entry[loop].name,
6 x) I2 |/ P' }entry[loop].age, / n; `: j% P3 r0 z- _( {- T- h- }3 |
entry[loop].salary);
: b: x& j& e( r, y3 h6 K: O' bprintf("-----------------------------------------------------\n");
: e; H9 n0 s. |4 u( |return 0;
4 s. g+ @* C" T' x1 G} 6 P* u/ N' Y2 V9 E8 l
</FONT>
. q7 p# ?1 O7 i T; h, O</P>8 j' u, X" ?- \$ @% L- _; J
< ><FONT color=#ff0000>函数名: searchpath </FONT>% A. G8 Z" \ R) L; L
功 能: 搜索DOS路径
( Y3 |8 J+ H6 A6 D2 B1 w/ W3 R用 法: char *searchpath(char *filename);
7 z' ^2 C9 b' y5 n程序例: </P>
. B$ o" y" V3 l- h< ><FONT color=#0000ff>#include <STDIO.H>6 u5 W6 U2 o2 ^4 f* H/ u
#include <DIR.H></FONT></P>% Q( _; h, o6 D& D. ?3 [
< ><FONT color=#0000ff>int main(void)
9 h9 @* j0 Z9 I1 ~8 H- \9 r: T{ + K$ {4 [9 r% S( D3 z- Q7 }3 S
char *p; </FONT></P>
+ n6 s$ _. N G! n9 ^' C0 J, Q< ><FONT color=#0000ff>/* Looks for TLINK and returns a pointer
5 @4 Z& a& O2 oto the path */
" }; @! N3 t+ F7 }- `' Ap = searchpath("TLINK.EXE"); , { z* q2 W+ e5 Y
printf("Search for TLINK.EXE : %s\n", p); </FONT></P>! {) \: ]" T% J0 a: \) K3 A2 B
< ><FONT color=#0000ff>/* Looks for non-existent file */ 1 z0 t$ x- Q, z1 s- _& {
p = searchpath("NOTEXIST.FIL"); % R8 B8 j3 H* i1 w
printf("Search for NOTEXIST.FIL : %s\n", p); </FONT></P># V# N8 O0 U' Z: |% e( b
< ><FONT color=#0000ff>return 0;
) ~" e; E7 i i} 1 g3 w! V; O( ~7 J5 x
% c9 X+ T% o4 @# Q P
</FONT></P>
6 q ~8 p$ G0 B< ><FONT color=#ff0000>函数名: sector </FONT>
8 w- W2 S7 I1 b4 D功 能: 画并填充椭圆扇区 9 G( m, U9 [# R; F
用 法: void far sector(int x, int y, int stangle, int endangle);
" W) |3 x; X! M4 d4 V程序例: </P>2 ^8 J# \0 f% J% g2 E0 J1 e. w
< ><FONT color=#0000ff>#include <GRAPHICS.H>/ ~# b0 `; u/ z# ~
#include <STDLIB.H>
Y8 S# i! B' w# b- _5 Y#include <STDIO.H>- Y: r+ r9 j9 N7 R/ y
#include <CONIO.H></FONT></P>: i0 H$ C( i) h2 c8 z/ N( V3 d5 ~- T
< ><FONT color=#0000ff>int main(void) 8 |8 M$ c- F. u1 ]: {4 N1 e
{ ) R" e2 k5 L/ b
/* request auto detection */
8 I5 X# U! h) ?, E1 ^; }3 J- o" ^+ ?! Vint gdriver = DETECT, gmode, errorcode;
+ y4 r. X7 I( y$ f0 Vint midx, midy, i;
& S; M1 n+ d1 _- }5 U4 [int stangle = 45, endangle = 135; 4 o5 ~& e8 U- z8 L2 c( u
int xrad = 100, yrad = 50; </FONT></P>' ^7 _1 l {8 [3 n8 j T
< ><FONT color=#0000ff>/* initialize graphics and local variables */ + m' f/ G0 U. |) V9 T0 D9 y/ J
initgraph(&gdriver, &gmode, ""); </FONT></P>
+ q3 g, q. e, A1 H1 J< ><FONT color=#0000ff>/* read result of initialization */ . J6 Y+ e" E9 C4 w
errorcode = graphresult(); ! U3 T$ u# C- ]! u* Y/ U
if (errorcode != grOk) /* an error occurred */ ; r; H3 m3 G; u) J4 g5 n" e
{
+ d) \3 b% ~6 B( o" l& d9 [' C; ^/ Zprintf("Graphics error: %s\n", grapherrormsg(errorcode)); " O; `5 O; N7 _- s3 U
printf(" ress any key to halt:");
! E5 Z4 b4 \+ j& g8 M; E) Fgetch();
; v3 s2 K6 ?' c' Y* z) cexit(1); /* terminate with an error code */ 8 |. |' c2 {$ z9 Y2 k) b9 G# y8 P5 z
} </FONT></P>
9 W, k5 z0 c; O; E. D+ _< ><FONT color=#0000ff>midx = getmaxx() / 2; 7 z* E' X" y1 n4 q3 Y
midy = getmaxy() / 2; </FONT></P>5 x# |! @4 v9 Y G1 }, q
< ><FONT color=#0000ff>/* loop through the fill patterns */ 4 ]) F1 W: m& e1 o+ d2 Y
for (i=EMPTY_FILL; i<USER_FILL; <br i++)> { * k7 t, l7 m% A% K8 _. z
/* set the fill style */
; l7 W! q. A y$ ~, _( e3 M3 esetfillstyle(i, getmaxcolor()); </FONT></P>0 W/ k$ }, I1 S; u! w
< ><FONT color=#0000ff>/* draw the sector slice */ 9 _8 @: N, k( P/ h: V: s
sector(midx, midy, stangle, endangle, xrad, yrad); </FONT></P>; ?& }9 @( s+ P
< ><FONT color=#0000ff>getch(); 1 D$ [: w7 ~. e
} </FONT></P>
& \" O1 m$ D2 K+ {<P><FONT color=#0000ff>/* clean up */ . p4 K: U& H6 R: Q& W$ K
closegraph(); , V: T/ ~# z! B6 P( ?
return 0; $ z) [& q) f Z' \& w! K. [' r3 D
} 7 [9 a7 @. p/ Y- H; \
</FONT></P>: H) |. l! f1 ^/ R% p6 Z
<P><FONT color=#ff0000>函数名: segread </FONT>
0 F! I" E, w8 R5 p功 能: 读段寄存器值 7 p4 O. ~# ~$ q& p4 a
用 法: void segread(struct SREGS *segtbl); 5 _/ I5 l( x3 ^0 {
程序例: </P>
( J# M5 R0 v5 A( D+ s<P><FONT color=#0000ff>#include <STDIO.H>
$ O. m) K3 k/ U7 I. f9 j#include <DOS.H></FONT></P>
$ k$ V" ?( U; M( E# \<P><FONT color=#0000ff>int main(void)
! k4 E" V- b( u{
6 s3 u1 S/ q2 ~5 B6 istruct SREGS segs; </FONT></P>4 F. ~' v; D2 C7 o9 O& y
<P><FONT color=#0000ff>segread(&segs);
0 b! U2 s6 `1 F; d5 Vprintf("Current segment register settings\n\n");
4 M& c9 E1 q/ Z$ Nprintf("CS: %X DS: %X\n", segs.cs, segs.ds);
" I) y0 }; Y! I' M) c* ?printf("ES: %X SS: %X\n", segs.es, segs.ss); </FONT></P>2 H. z! B8 ~* C" O3 U1 e0 x+ I
<P><FONT color=#0000ff>return 0; 8 @7 l p( \5 O! G7 m: P
} </FONT>7 K. T4 n! B8 N; _+ f u: ]
, p9 G& T4 w8 V. p0 n
</P>
5 a5 Y+ [ q3 l4 X: Q4 R, h/ l<P><FONT color=#ff0000>函数名: setactivepage </FONT>
/ b; W$ o8 \; w+ u6 Q. i! Z功 能: 设置图形输出活动页
+ |. v% _8 K3 V. [2 G. n用 法: void far setactivepage(int pagenum); " H6 K. d& `& K; ^* D
程序例: </P>
+ q3 H6 Y) X( u<P><FONT color=#0000ff>#include <GRAPHICS.H>& C$ u# w: i' e1 i+ q) _5 k3 C$ Q4 f( v
#include <STDLIB.H>
2 U7 q3 Q& W; k5 M j D5 P#include <STDIO.H>
c6 s/ N+ N0 x3 q#include <CONIO.H></FONT></P>
5 \% k2 ?2 t/ X- \/ [0 p% B- ?<P><FONT color=#0000ff>int main(void)
6 r% l" _+ N2 B: k{
( h$ @' i7 h! k+ x# i- W" I7 ~/* select a driver and mode that supports */ 8 Q- V, X" S" {3 }
/* multiple pages. */
& }4 e8 W/ Q+ b+ pint gdriver = EGA, gmode = EGAHI, errorcode;
( k# [* Z/ N. Y6 o Sint x, y, ht; </FONT></P>
8 A) w7 U; t2 f<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 F. n: o7 i+ G1 l5 w; W
initgraph(&gdriver, &gmode, ""); </FONT></P>
2 a' A$ R4 j B3 J8 O<P><FONT color=#0000ff>/* read result of initialization */
0 x* ]+ y& G# o* merrorcode = graphresult(); 3 e$ [; ]! L* t r4 k; g g
if (errorcode != grOk) /* an error occurred */
: n" l3 [- c9 ]) X: ^$ a{ 6 l5 E6 _$ b! l( g
printf("Graphics error: %s\n", grapherrormsg(errorcode)); - ^$ o! r6 f$ G! l& x7 {
printf("Press any key to halt:"); ! a; W0 o- s( ~* w. V/ N
getch(); 8 U8 X2 i9 g" M% z' ~- G! z+ l
exit(1); /* terminate with an error code */ 6 \ E6 n: O9 z5 k/ M' R i8 I
} </FONT></P>
& D6 A8 b5 ~; n; [: ~0 M) @) Y<P><FONT color=#0000ff>x = getmaxx() / 2;
% D% k9 v$ f% I) L& {y = getmaxy() / 2;
' ?/ Z! D( k H. x( ]! rht = textheight("W"); </FONT></P>+ F# Q! m9 B* S, z$ d K- M5 m
<P><FONT color=#0000ff>/* select the off screen page for drawing */ q& p) b* \+ k, C. r7 x% i: n
setactivepage(1); </FONT></P># _, p; u3 ], |. B1 w
<P><FONT color=#0000ff>/* draw a line on page #1 */
1 b3 ], g: ~/ a( [+ N: u8 Iline(0, 0, getmaxx(), getmaxy()); </FONT></P>2 V( w2 {% P- j M ~
<P><FONT color=#0000ff>/* output a message on page #1 */ 3 F* z. z" m9 d, f6 P# N
settextjustify(CENTER_TEXT, CENTER_TEXT); * n: x' G1 P6 f# X/ I" K% B
outtextxy(x, y, "This is page #1:"); 6 ^: `9 k6 ]3 F' d x5 ?) _
outtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
( Q$ }: I% g9 F6 \6 H% v<P><FONT color=#0000ff>/* select drawing to page #0 */
% u; m3 n- D5 }setactivepage(0); </FONT></P>4 X/ ^5 M1 N2 o$ H6 w
<P><FONT color=#0000ff>/* output a message on page #0 */ 6 C6 o2 u* H2 ?3 O5 Q; w: n
outtextxy(x, y, "This is page #0."); ; i0 H. f H/ z0 r
outtextxy(x, y+ht, "Press any key to view page #1:");
+ F) N3 i' H( ^0 u2 v1 Ygetch(); </FONT></P>) O9 ^% u T( C
<P><FONT color=#0000ff>/* select page #1 as the visible page */
* D8 b8 O2 `( G+ i& I. T4 G w' Wsetvisualpage(1); </FONT></P>; H$ |% y3 p% ~8 V5 v5 t3 t
<P><FONT color=#0000ff>/* clean up */
/ M, w8 r1 D- c5 F' c' `getch(); ( M8 x' {" x o
closegraph();
: |$ E+ \! D$ O! n# G6 ]$ i$ F1 preturn 0;
# W9 `0 ` n8 d}
! l8 D0 R( T0 L6 U1 Z( t4 I7 F</FONT>8 J3 {8 ~7 X% h/ J# J3 O; s7 }
</P>( Y0 @ k _/ e$ }1 y
<P><FONT color=#ff0000>函数名: setallpallette </FONT>
/ S- P: ^) F+ V3 U. w/ L功 能: 按指定方式改变所有的调色板颜色
7 J+ ^9 O; M: {) P/ D" b. w用 法: void far setallpallette(struct palette, far *pallette);
; _7 f& g6 F4 u4 s程序例: </P>
- p' ]1 k5 V& ~3 S1 a0 p& F<P><FONT color=#0000ff>#include <GRAPHICS.H>. H; O \ m( X/ i s2 ~
#include <STDLIB.H>
- N6 P# ~' a9 o2 t/ l L9 ~9 w# g#include <STDIO.H>
3 }& t+ w q T7 T#include <CONIO.H></FONT></P>! K3 \* o9 t& d; C0 q8 e
<P><FONT color=#0000ff>int main(void) . a) a) D% u$ P* Z0 p
{ * }0 A9 Y7 G9 A* [, e6 t
/* request auto detection */
2 i8 T; L) c( V2 J- \& V8 Cint gdriver = DETECT, gmode, errorcode;
9 M( h2 B$ r+ D& w' ~struct palettetype pal; # J* {2 H) o7 O
int color, maxcolor, ht; ! r5 N& z: I* `
int y = 10;
8 c% V" U6 A, }$ h$ D9 U; uchar msg[80]; </FONT></P>
7 x) R1 _0 ?$ L+ `0 x<P><FONT color=#0000ff>/* initialize graphics and local variables */
. E6 L) X/ X3 Z; S) w+ e5 `initgraph(&gdriver, &gmode, ""); </FONT></P>) K2 r" @: G" l2 J
<P><FONT color=#0000ff>/* read result of initialization */
! u- l k2 Z) J7 W5 A- berrorcode = graphresult();
- W( {1 q8 ]* h9 c% B8 e k1 Xif (errorcode != grOk) /* an error occurred */ ! U! R; \ F1 {6 g3 s# s: ~9 U* l
{
3 X" r5 o7 ~( F' Q: K& Eprintf("Graphics error: %s\n", grapherrormsg(errorcode));
: S' `) U* K8 o2 s* @printf("Press any key to halt:");
6 E* V( y6 V: D: l" D) B8 o* Egetch(); / \8 ^% \' H9 j' j
exit(1); /* terminate with an error code */
7 d- S' ]6 c7 }. H/ T# x$ n# [3 d' G} </FONT></P>
+ D$ ]0 _2 K& o" g6 F<P><FONT color=#0000ff>maxcolor = getmaxcolor(); 4 ~8 J! P% h! G- _# y
ht = 2 * textheight("W"); </FONT></P>
5 V; f8 c% E9 h! n4 j/ `0 a<P><FONT color=#0000ff>/* grab a copy of the palette */ : t/ a) ~' G" _; g V
getpalette(&pal); </FONT></P>
; U; C: Z. _, ?# g6 K8 B H<P><FONT color=#0000ff>/* display the default palette colors */ - I6 y: E+ `, {" F( F
for (color=1; color<=maxcolor; color++)
/ P% k7 u: |* J# a7 N{
& l- h7 G6 S2 w: v7 F, `9 {setcolor(color);
5 W7 P7 R8 j& @% Nsprintf(msg, "Color: %d", color);
+ K& J5 K" E8 o# g, Nouttextxy(1, y, msg);
8 f/ j5 v9 d+ C3 }8 Y2 a: Uy += ht; - s7 Q' A3 [, \1 q- Q. o
} </FONT></P>) p& y1 ? X. B |0 ^3 U
<P><FONT color=#0000ff>/* wait for a key */
% }; z! T- L* g4 M! m) Tgetch(); </FONT></P>
4 Y- [) f6 R# Y+ ?- Z) q<P><FONT color=#0000ff>/* black out the colors one by one */ / d1 u5 K/ c7 c, b o8 ]) g! L$ r
for (color=1; color<=maxcolor; color++) % }4 `: _1 O: c- K
{
. q' h! F) v' F. a1 o& ssetpalette(color, BLACK); ) i- l5 x: I6 u* r7 h( h
getch(); 9 R& x3 ^) c* Y& x) j$ ^
} </FONT></P>3 t& p% P& B# s8 v
<P><FONT color=#0000ff>/* restore the palette colors */
5 s7 S# U% S8 B( M. zsetallpalette(&pal); </FONT></P>
0 f2 n2 p6 K- ]# R; _# |<P><FONT color=#0000ff>/* clean up */ : y% q) R1 k0 F. O
getch(); , U' S) ]9 P1 T! y% p" w
closegraph();
! V1 X- q8 |$ [" X C/ a. ereturn 0;
! k' N' N) f& K4 q5 u$ D% w}
$ b: A, c- X2 S& l; l9 I9 Q, F7 i
0 d! W5 X) r, Y1 o( }0 A</FONT></P>8 }2 @) E3 \1 }; [; c6 D
<P><FONT color=#ff0000>函数名: setaspectratio </FONT>+ Z2 T1 ~' I2 z
功 能: 设置图形纵横比 4 p6 |* F7 a" t. e) s
用 法: void far setaspectratio(int xasp, int yasp); 3 g( y5 i6 P, z
程序例: </P>1 X' K/ |5 f. l3 p6 U7 J
<P><FONT color=#0000ff>#include <GRAPHICS.H>
( g; ]) a5 u; S3 O#include <STDLIB.H>7 J/ C9 R$ D K
#include <STDIO.H>
, v5 K7 z* i, L9 Z1 q9 k#include <CONIO.H></FONT></P>
1 q: S. ]) i }6 n7 X2 f* A<P><FONT color=#0000ff>int main(void)
7 T# v% M: n, e' F4 @" \, z2 @{ ; Z+ k: ? q$ G( e6 b* C i
/* request auto detection */ , k, F! b0 n# j% J: l" w9 b8 e# l
int gdriver = DETECT, gmode, errorcode; ( k, \7 c% M5 |) B: `! s
int xasp, yasp, midx, midy; </FONT></P>' _& ]6 F: A$ u2 ]3 S# Y
<P><FONT color=#0000ff>/* initialize graphics and local variables */
/ w6 F& e& A, z& Minitgraph(&gdriver, &gmode, ""); </FONT></P>9 a. p' ^' {* M0 Z' V( L
<P><FONT color=#0000ff>/* read result of initialization */ $ h4 Q2 ~, Y: e4 M8 g0 q
errorcode = graphresult();
( P! X' I* F9 U- C& Yif (errorcode != grOk) /* an error occurred */
# t2 j" G k' E, q{ 8 G" C. u/ G* E" F4 b4 ~
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 ^: n. a- ^" V& o* N3 L! M
printf("Press any key to halt:");
+ Q. S8 u% i# |, V( fgetch();
( u9 K; F8 u8 N P; h6 c5 P- _# Xexit(1); /* terminate with an error code */
% P- j+ q- T3 j} </FONT></P>
( N6 C. [4 V- G. u- s2 U<P><FONT color=#0000ff>midx = getmaxx() / 2; & T. {7 I1 |: t' M
midy = getmaxy() / 2;
l# W. u+ W# }# r! K1 T( p8 t# Zsetcolor(getmaxcolor()); </FONT></P>
9 Z6 L, n+ u5 L: Z( `7 L<P><FONT color=#0000ff>/* get current aspect ratio settings */
& n$ L7 _6 Z. F9 {' h! Y; O1 c. Agetaspectratio(&xasp, &yasp); </FONT></P>
2 d* ~1 h; t$ y8 L) B- i; `2 H<P><FONT color=#0000ff>/* draw normal circle */ & ?( g, {6 J8 Q; V, f
circle(midx, midy, 100); ! s- x/ w/ o% f+ s. f! P$ \( T
getch(); </FONT></P>
5 N$ v' A% ?: [1 ?<P><FONT color=#0000ff>/* claer the screen */ & X. _3 H. ]4 C4 U, n' ?
cleardevice(); </FONT></P>! V- ` O* g* @! o0 C# r" }8 e
<P><FONT color=#0000ff>/* adjust the aspect for a wide circle */ ' W# {2 V# E3 e) U! q+ M
setaspectratio(xasp/2, yasp); 5 P# Z' N3 q* j4 B) p
circle(midx, midy, 100);
4 i. ^! M3 P X' e, u6 l- d; m! Zgetch(); </FONT></P>
$ e% y! ]! R/ ?7 T<P><FONT color=#0000ff>/* adjust the aspect for a narrow circle */ ! }* o+ b1 k2 d4 O: Q, w4 ^3 Q
cleardevice();
" s0 {6 U& _3 @5 H; W; ssetaspectratio(xasp, yasp/2); ! Y/ Z) Z$ [& d# U/ I
circle(midx, midy, 100); </FONT></P>+ ^8 Z! p' C$ o+ n
<P><FONT color=#0000ff>/* clean up */
, N6 G$ l6 \. p) \& o3 cgetch(); 1 i5 M: t! w4 e* [8 W% K
closegraph();
, n* s0 B2 r' V4 Preturn 0; $ `7 A% x5 b8 K3 e; p
} % m0 U7 }8 c6 x3 g. B' n1 O/ q/ M
</FONT>1 G/ H: V/ }& g& J2 L- q+ [
</P>
7 C, E7 K9 i9 [<P><FONT color=#ff0000>函数名: setbkcolor </FONT>
1 c+ i1 ?# G% ~功 能: 用调色板设置当前背景颜色 ! a* u( |9 H1 x# ^8 G
用 法: void far setbkcolor(int color); 3 ?$ T) |/ h1 o3 J* b" V$ C- ]+ e
程序例: </P>
2 |8 _1 z: K" a! ~' e4 n<P><FONT color=#0000ff>#include <GRAPHICS.H># A+ `9 z1 m5 n, N1 h/ v4 @
#include <STDLIB.H>
$ M0 h" G; i" z" l! W6 G) E( Q#include <STDIO.H>3 o4 s" M% k8 w$ T
#include <CONIO.H></FONT></P>4 D5 C3 ~- a4 ~' j; ~- p/ A
<P><FONT color=#0000ff>int main(void) . t0 m* r6 X) [( v. s7 w0 V0 J7 T
{
; c7 ~& L% F/ ~# l: F0 \/* select a driver and mode that supports */
+ X1 x& z0 [9 b" S V7 H5 V5 @! A/* multiple background colors. */
- {: k5 w$ @; L5 T/ o$ qint gdriver = EGA, gmode = EGAHI, errorcode;
1 ]) y% k$ x# {5 O3 sint bkcol, maxcolor, x, y; 2 k' K/ m3 H- B/ b! c" t, D+ z
char msg[80]; </FONT></P>: |: Y. t" o5 S$ t$ k: H' A
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 5 }" f% @' i6 S1 a' k9 i% h. v
initgraph(&gdriver, &gmode, ""); </FONT></P>! G/ u: T# Q! B& H2 n4 p
<P><FONT color=#0000ff>/* read result of initialization */ ' z" c1 a2 V, I5 a$ w5 u% M
errorcode = graphresult();
8 C |: \! O# Z# S, Uif (errorcode != grOk) /* an error occurred */ ; S5 @4 T: ]2 m; e
{
5 K+ P5 ?% p3 M; Z# Eprintf("Graphics error: %s\n", grapherrormsg(errorcode));
( X, N3 c6 y" o/ K* fprintf("Press any key to halt:");
; O2 ~3 V# J0 V9 ~4 jgetch(); & W1 c- J2 R3 L8 j/ L0 p5 q' h
exit(1); /* terminate with an error code */ * @) X$ L/ Q' P
} </FONT></P>
* p' o# B4 z# {+ f<P><FONT color=#0000ff>/* maximum color index supported */
3 [0 A3 Z" U w- q5 E% nmaxcolor = getmaxcolor(); </FONT></P>/ r, Y4 A; N/ W5 T% t, V
<P><FONT color=#0000ff>/* for centering text messages */
' J% ~9 e* \ Ksettextjustify(CENTER_TEXT, CENTER_TEXT);
0 Z8 y( f1 m7 Fx = getmaxx() / 2;
. L5 p% j$ p8 }) m: xy = getmaxy() / 2; </FONT></P>, T, f. Q# s4 A0 I t7 c
<P><FONT color=#0000ff>/* loop through the available colors */
) P5 H) V; F. }9 ]6 `( o+ ifor (bkcol=0; bkcol<=maxcolor; bkcol++)
% S. J+ D' R U* H7 ]' J: y{ 0 g/ o0 ]' ~) L% F2 K
/* clear the screen */
1 G) \- q* V; x w8 R) k# s) t! [, Fcleardevice(); </FONT></P>
: {4 h3 R3 G; E F& d/ q' U<P><FONT color=#0000ff>/* select a new background color */ 6 S; |5 b5 R2 A- W
setbkcolor(bkcol); </FONT></P>4 U' [) E* J* R$ Z8 z# L
<P><FONT color=#0000ff>/* output a messsage */ - M$ x; l, E& E- ~. B6 s5 y( d; H# D
if (bkcol == WHITE) 4 ~/ C; K! H3 T
setcolor(EGA_BLUE);
/ a" z7 l$ e) _sprintf(msg, "Background color: %d", bkcol); 1 K; a4 g% I9 \$ x5 H
outtextxy(x, y, msg);
6 Y) k! z: S9 [4 G2 C4 ^) Hgetch(); : ^6 G. K$ Q: r# D' I
} </FONT></P>
n2 W0 z+ A9 u- a# N K<P><FONT color=#0000ff>/* clean up */ : q8 [: E. F; Z6 o$ w4 c( L& b4 d
closegraph(); ! j: P$ N/ I. a9 H' `- F
return 0;
; e2 c& P( j! J+ Y3 ]4 k+ I# \} </FONT>
0 A& p) k7 o# J7 b: V1 m* M3 r
0 O7 G" p2 H0 Y" F</P>
9 G5 o! P9 I T7 P, S<P><FONT color=#ff0000>函数名: setblock </FONT>
6 l8 k; }; A7 |( ^1 u' z. Q- }功 能: 修改先前已分配的DOS存储段大小 * c, W% u$ o5 [: r
用 法: int setblock(int seg, int newsize);
, W$ j8 g3 T7 ~+ b9 Z& W! S1 u程序例: </P>* F2 B2 _/ u- g) c
<P><FONT color=#0000ff>#include <DOS.H>
+ p* [. v; ~9 h' l8 X9 R$ |#include <ALLOC.H>
/ t a4 M! e6 N, E#include <STDIO.H>
) L8 C+ \3 p" R& N+ V( c#include <STDLIB.H></FONT></P>
8 B/ K( U0 m* j<P><FONT color=#0000ff>int main(void)
5 `0 v [' \6 w7 \: a7 @+ _{
5 q& w. S1 W6 |- M+ S Ounsigned int size, segp; 8 i, A) x' y( I
int stat; </FONT></P># g, f; A: {4 ]; f3 W6 d
<P><FONT color=#0000ff>size = 64; /* (64 x 16) = 1024 bytes */
% [. C# p0 \$ y9 Z7 }# h9 f4 Pstat = allocmem(size, &segp);
7 ~" w! e. c8 A; M/ Wif (stat == -1) $ t" s# U7 Z5 u/ t3 E6 u
printf("Allocated memory at segment: %X\n", segp); 1 e2 Y9 J6 i g- i& X+ |
else 7 r2 C1 ^5 v% B+ u* D0 ?
{
" F( j ~8 `( ?& P/ O$ M* Bprintf("Failed: maximum number of paragraphs available is %d\n",
$ J$ b* L/ ^: Z( i/ nstat);
6 M$ I2 j# C2 `exit(1); 1 Y, X y- D: c* V) J4 [, G5 R3 V
} </FONT></P> J; | D* i: B7 x6 `7 S6 i |
<P><FONT color=#0000ff>stat = setblock(segp, size * 2);
% ~7 y5 i, L$ E/ U9 Tif (stat == -1) " S. q5 ^' V' W) A, q7 F
printf("Expanded memory block at segment: %X\n", segp);
+ z0 X) P4 v6 E8 T+ L& |. selse , s' Z$ _6 K& K/ L. H- \1 N0 g# V
printf("Failed: maximum number of paragraphs available is %d\n",
; t% k* z$ d# y& o3 R7 U* hstat); </FONT></P>
) H) D: e5 ?; B/ I<P><FONT color=#0000ff>freemem(segp); </FONT></P>
7 E; C% N3 w2 p6 B. G R: R# X( O<P><FONT color=#0000ff>return 0;
: `, Z. e7 A; z$ U4 c b}
9 `! i5 J. H& _ X) y</FONT>0 |) S' t" @" [- N" v' T
</P>
' M) t n) a9 x<P><FONT color=#ff0000>函数名: setbuf </FONT>" s/ d. g2 ?; b- c+ m
功 能: 把缓冲区与流相联 # F2 ^+ k, a/ T7 W# @- }" l
用 法: void setbuf(FILE *steam, char *buf); 4 X( L o$ R/ j( w+ R7 s
程序例: </P>
. G' f9 m6 t$ I/ _- q<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
$ a( `- } W! {2 A" ^" x6 v<P><FONT color=#0000ff>/* BUFSIZ is defined in stdio.h */ " e9 K0 Q' ^! `& |
char outbuf[BUFSIZ]; </FONT></P>, L& u. D$ q8 p3 H: K( p
<P><FONT color=#0000ff>int main(void)
5 d' x+ y7 i$ L0 I+ m' v{ ) h/ r% |% Z3 \
/* attach a buffer to the standard output stream */ 7 t% U8 ]7 A% F
setbuf(stdout, outbuf); </FONT></P>
* j6 x" F8 t" q. v" k% G% h<P><FONT color=#0000ff>/* put some characters into the buffer */ ; v6 W3 F' _7 p
puts("This is a test of buffered output.\n\n"); ! Y$ j' K0 b$ t. D% d
puts("This output will go into outbuf\n");
7 e0 D0 q9 x3 [$ `0 q- |puts("and won't appear until the buffer\n");
" H; M- I6 O- L, R: M1 |, ?. O8 Gputs("fills up or we flush the stream.\n"); </FONT></P>
" I! s4 q& ?7 Y( {<P><FONT color=#0000ff>/* flush the output buffer */ & y. K q- ^/ z5 Y
fflush(stdout); </FONT></P>
) ~/ B( j4 n) K<P><FONT color=#0000ff>return 0; ' v: Z- I6 a* S% c
} |) Y e ?% v3 o s
</FONT>$ ?5 u- b- C' L- u: x7 p/ a
</P>
3 g* k: n. o1 b$ E. L7 s: j<P><FONT color=#ff0000>函数名: setcbrk</FONT>
/ ^1 h. f7 U" h" i9 c- V, M2 a& I y功 能: 设置Control-break
( k+ d% N# X$ @7 u! H用 法: int setcbrk(int value); ( _4 b0 G' m' d: h. c& j
程序例: </P>
' m l% ~: G9 L" W+ [<P><FONT color=#0000ff>#include <DOS.H>! F4 H' p& u/ z3 i: \- f# z
#include <CONIO.H>
2 ]/ L) _! p" {& E( Z#include <STDIO.H></FONT></P>% k+ L$ d. X0 Y2 w5 a) H
<P><FONT color=#0000ff>int main(void)
1 s6 O8 `5 c& Y/ x. {{
6 r( z7 Q0 I) c3 b+ i0 Lint break_flag; </FONT></P>" J6 `* L+ R" @
<P><FONT color=#0000ff>printf("Enter 0 to turn control break off\n"); - ]/ M3 z1 w" E1 y4 E
printf("Enter 1 to turn control break on\n"); </FONT></P>5 U1 l# b3 V( ^( Z6 r
<P><FONT color=#0000ff>break_flag = getch() - 0; </FONT></P>
5 v2 x( G1 ]( c<P><FONT color=#0000ff>setcbrk(break_flag); </FONT></P>
% X$ m; J- Q: m6 m% h2 W<P><FONT color=#0000ff>if (getcbrk()) , \5 B* E; p! d9 z* X; O- b
printf("Cntrl-brk flag is on\n");
: c. C0 h$ {6 J. E' u) z1 belse 1 k: T( w3 R6 W: a2 O2 j1 ^
printf("Cntrl-brk flag is off\n");
' H: u8 I$ |4 ]0 T% v! ], ]! Q5 Hreturn 0;
* D3 y8 Q2 d2 ]9 v5 g' U} 7 n4 a0 K+ _6 ]: a$ }3 B$ H9 E1 |, B
2 }+ m6 B, @7 |' l( E- B</FONT>7 x# x m* `% V: J U
</P>
' n1 F( ]5 `* [) V3 ^# o<P><FONT color=#ff0000>函数名: setcolor</FONT>
2 j* r4 ]8 K( J2 P/ \& C功 能: 设置当前画线颜色 * P$ f! k$ b7 t& w4 ?: B
用 法: void far setcolor(int color); 8 |) ~+ U# D8 f$ r% x
程序例: </P>
3 l' C( D8 W3 n9 {8 m/ v3 T<P><FONT color=#0000ff>#include <GRAPHICS.H>* W/ |/ q1 k5 G; |- S/ t, p
#include <STDLIB.H>
) w' `' P0 w0 f1 C# e#include <STDIO.H>
( ], Y2 Q, B0 s% k; E#include <CONIO.H></FONT></P>5 \: `$ ?+ s. |5 n% w
<P><FONT color=#0000ff>int main(void)
0 S) ~+ p) \5 ]8 e4 c5 e{
) n$ {) d- I0 Q4 F' S/ P7 c5 ^/* select a driver and mode that supports */ ; S5 |9 c0 t0 L$ G/ T
/* multiple drawing colors. */ : _ r" @5 y2 r9 G0 {
int gdriver = EGA, gmode = EGAHI, errorcode;
( J# z0 x* J, L1 D F& vint color, maxcolor, x, y;
/ h+ v4 R+ ^: Y) H" ~% L# ychar msg[80]; </FONT></P>9 {0 S$ \5 Z; P( {5 M6 x" ?+ K& g
<P><FONT color=#0000ff>/* initialize graphics and local variables */
2 A" ?. o& C9 S" i3 ~1 N; C$ d% f1 uinitgraph(&gdriver, &gmode, ""); </FONT></P>* I0 ~ F1 z. c3 |/ ^0 r
<P><FONT color=#0000ff>/* read result of initialization */ $ p L! X7 A# H2 O O( E8 j8 _
errorcode = graphresult();
1 f5 y" S2 }: E: [/ p* N' `if (errorcode != grOk) /* an error occurred */
% g+ ^1 N5 z; ?% b6 s{
C. k- E4 v* n. r7 O/ nprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 4 _% N+ ]! k! F5 q8 e W
printf("Press any key to halt:"); 0 S/ y+ X9 N8 V: V0 y& y1 s/ `
getch(); ' j# d) }! T x" o# i$ d2 g+ M
exit(1); /* terminate with an error code */ * Q4 {3 G- P5 s
} </FONT></P>8 W/ A9 P" f* I
<P><FONT color=#0000ff>/* maximum color index supported */
# @+ [: d" l m; @maxcolor = getmaxcolor(); </FONT></P>
7 H: A ?) T: Q4 [<P><FONT color=#0000ff>/* for centering text messages */
9 C3 T3 ]/ B7 a7 E& q0 Qsettextjustify(CENTER_TEXT, CENTER_TEXT);
9 v* h. {# r/ y" D+ bx = getmaxx() / 2;
' I" c* @+ m8 x; Q. z* Y Ly = getmaxy() / 2; </FONT></P>1 U6 T+ P: \* Y9 D7 l5 D
<P><FONT color=#0000ff>/* loop through the available colors */ , c+ p/ N: {6 N2 K/ f5 x% N
for (color=1; color<=maxcolor; color++) * s) ~1 a0 q, c
{
/ O& E' y1 C0 T7 G+ b/* clear the screen */ ' Q. R$ X8 z; c( ^
cleardevice(); </FONT></P>
. }" M8 @5 K" Q# f6 r<P><FONT color=#0000ff>/* select a new background color */
) u+ S7 a; O' i1 p' r+ zsetcolor(color); </FONT></P>4 q, ~1 D9 Q: V! z- u
<P><FONT color=#0000ff>/* output a messsage */
# b7 k% ~0 G0 Z. Q9 Dsprintf(msg, "Color: %d", color); 7 N3 z2 _) I. n) H$ Y( A& y
outtextxy(x, y, msg); 5 K1 T5 T6 `. u$ t j1 l7 x
getch(); $ R1 i- b0 O: ?, t) L2 I
} </FONT></P>0 J7 M( s+ O) G, y, A
<P><FONT color=#0000ff>/* clean up */
D' Z: l' |8 j( F2 J# ^: _closegraph();
9 z+ t" w( \, Areturn 0;
. V5 V0 T3 F3 G) c+ H* a, i} 9 f6 N' H3 K* k: t f6 }
</FONT>) z" q& J/ _6 r! l4 _
</P>
4 d# o: N" H) T0 b( o" j+ T<P><FONT color=#ff0000>函数名: setdate </FONT>0 y* ^# m% e; e3 e' |% F
功 能: 设置DOS日期 8 s6 L9 g* v7 V! E. K, ^
用 法: void setdate(struct date *dateblk);
) l7 h5 t7 K# _3 @% |) J v J- O程序例: </P>, m, d5 M( ^) d' u* ?# e
<P><FONT color=#0000ff>#include <STDIO.H>
0 f1 ^$ Z) Q( [7 D#include <PROCESS.H> M$ _' @$ Q1 `4 L
#include <DOS.H></FONT></P>
* ^' I! }. d" H+ E) i. j<P><FONT color=#0000ff>int main(void) ) v* z) P' ^! Q
{
7 H1 B0 R. R# Bstruct date reset; 5 O: I1 z0 \; e( E( Q/ ~( ~
struct date save_date; </FONT></P>
; u" B8 K3 K: J C5 _$ {% m<P><FONT color=#0000ff>getdate(&save_date); 6 _! {, j5 }* \) Y6 `; \
printf("Original date:\n"); 1 A/ ]# w8 e$ E
system("date"); </FONT></P>
: X: Z2 H3 n8 K; b/ X# _, l0 C<P><FONT color=#0000ff>reset.da_year = 2001; / x( a J( e' C& s t: y
reset.da_day = 1;
, G' L! o; i) W: U0 y& N+ ^2 {reset.da_mon = 1; 7 e5 A5 s. ~7 v' ~% A
setdate(&reset); </FONT></P>
( \/ Y' z& r: t F/ C+ X<P><FONT color=#0000ff>printf("Date after setting:\n"); ) [2 R0 ]/ Y V3 e9 l' Z
system("date"); </FONT></P>
; k# M6 ~& l8 B<P><FONT color=#0000ff>setdate(&save_date);
* s. m* o8 H# X! Rprintf("Back to original date:\n"); # Q% U8 G% D) W9 r) ?
system("date"); </FONT></P>/ ?: Z9 Y0 J X! L" j* B
<P><FONT color=#0000ff>return 0; 1 Y! i) ]5 t% Y+ t: C0 {; M! z9 O
}
! w! ]: W, q+ v</FONT>* l& L X) N! ?2 v" v& X
</P>0 s- r' g/ h+ H7 I, J8 U$ ~
<P><FONT color=#ff0000>函数名: setdisk </FONT>4 v# X/ o" u- A4 b0 e4 P9 p! s* a/ M; Q
功 能: 设置当前磁盘驱动器
8 w s! L5 c6 ?( E* J- ]) P用 法: int setdisk(int drive);
+ W4 i+ {4 P% a# r5 z0 k程序例: </P>; H% z' U$ h/ N, r
<P><FONT color=#0000ff>#include <STDIO.H>
' e9 v0 f+ l9 W#include <DIR.H></FONT></P>/ v7 {" v8 g5 F6 H$ ^4 v6 A
<P><FONT color=#0000ff>int main(void) 6 m, } |3 }& F1 `+ D
{ 9 E1 }# g" b: v2 e& k- e
int save, disk, disks; </FONT></P>6 S6 E2 v8 t: k8 T+ m5 g
<P><FONT color=#0000ff>/* save original drive */ - b# O1 a6 u: X) P4 q! Q" m
save = getdisk(); </FONT></P>, j9 p+ g4 j/ v- L0 {: G0 T$ U
<P><FONT color=#0000ff>/* print number of logic drives */ + B( c9 x) c7 d7 F, J/ c7 O
disks = setdisk(save);
2 |! s" P# Q, l5 V' Q4 Zprintf("%d logical drives on the system\n\n", disks); </FONT></P>( u+ v( [; @2 ~6 J
<P><FONT color=#0000ff>/* print the drive letters available */ $ @$ Y F* b& {+ }/ x3 |0 W. Y3 Z
printf("Available drives:\n"); % E8 T+ O% H( d. }, `& q$ K
for (disk = 0;disk < 26;++disk)
9 Y/ ~6 d( e4 Q$ C( w1 G1 P{
0 W% Y! I8 x* B% E# J1 Wsetdisk(disk);
! t0 j6 Z2 C0 Bif (disk == getdisk()) 4 O: E% I2 g; B; z+ N9 C
printf("%c: drive is available\n", disk + 'a'); / b) B) V* L( p. r/ w
}
4 w0 { F1 b4 }; k* y8 Isetdisk(save); </FONT></P>, [( E# ?$ K( Y% n+ Y- D( a
<P><FONT color=#0000ff>return 0;
/ M. E+ v/ ]/ {. o0 d; i} </FONT># R. V7 [2 P: ~
4 Q" x+ V# P; @2 E5 ~9 g
</P>
+ W1 j1 { e4 m" A$ W<P><FONT color=#ff0000>函数名: setdta </FONT>( ^& y, D6 p6 O4 O0 }' [" \
功 能: 设置磁盘传输区地址
3 S: @, Q2 S" x* S9 }7 N用 法: void setdta(char far *dta);
) T9 }. E5 U( A$ H4 E- u程序例: </P>9 C% y$ n7 m8 M* C$ ~
<P><FONT color=#0000ff>#include <PROCESS.H>6 h: h* C: }7 \$ ]
#include <STRING.H>
! F& J3 h9 j i#include <STDIO.H>% G- A& S# }+ i# s8 P
#include <DOS.H></FONT></P>( E5 [: L- p; A& H8 w
<P><FONT color=#0000ff>int main(void)
+ M6 j) b- J" H8 N4 ?{
Z$ B* D5 ]" A& E4 q: r; u6 z- Y8 Jchar line[80], far *save_dta; ! s9 j9 k$ |! X0 z- `+ s
char buffer[256] = "SETDTA test!";
7 } Z* R/ N0 m& r/ o% C+ rstruct fcb blk; 5 y) U6 q6 i2 [+ M/ Z) d
int result; </FONT></P>' h& d) T; C: Y+ E1 A+ W
<P><FONT color=#0000ff>/* get new file name from user */
& Y V5 }9 l6 S4 @ F4 e$ k5 g& h) Sprintf("Enter a file name to create:");
, A) r6 ]7 z4 K+ v9 mgets(line); </FONT></P>
' V% F) g2 k$ \6 ^<P><FONT color=#0000ff>/* parse the new file name to the dta */
# M# h. a8 w8 e1 T( s0 T, Mparsfnm(line, &blk, 1); ' @. z: y1 c# b. D
printf("%d %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
, F1 L/ V7 k7 o* r6 p* C0 I2 \<P><FONT color=#0000ff>/* request DOS services to create file */ " o# w1 X6 j0 i( u, X/ e+ S% V0 E
if (bdosptr(0x16, &blk, 0) == -1)
5 C4 {2 n2 I0 I% U4 ^; Y; k{ - e6 f, I4 m1 I9 n% Q
perror("Error creating file"); 2 q& A) ~5 n, N4 t: E
exit(1); ( A/ Q, C5 {+ [ U- t6 {
} </FONT></P>
; N* ]8 M% M7 }6 z" w5 l3 @<P><FONT color=#0000ff>/* save old dta and set new dta */ % F' Z+ b" s' y" _' n o
save_dta = getdta();
+ T( ?% n6 W& N j2 E! nsetdta(buffer); </FONT></P>4 v8 Y3 P; ^; }0 S& G
<P><FONT color=#0000ff>/* write new records */ 6 m1 Z1 R) ~4 a/ y# g
blk.fcb_recsize = 256;
$ c" ~1 R$ F( Zblk.fcb_random = 0L; K, Z6 K. e. E" a% @
result = randbwr(&blk, 1);
7 K d3 Q, c! i, @" X' Vprintf("result = %d\n", result); </FONT></P>4 P/ E" K4 f, k% Q7 a3 W: ?
<P><FONT color=#0000ff>if (!result)
5 `1 h+ ` q# H5 g. d2 Gprintf("Write OK\n");
% s- x1 d' e1 C2 @else
5 I# E; |. [2 \+ Y{
) c3 G9 M8 F9 A+ f# @1 l5 S5 R( Operror("Disk error");
3 _: T& q @# W( S9 x* ?+ c& K2 sexit(1); ) ~$ P. k5 N, W$ ~; o* l- K* Q
} </FONT></P>
6 X2 N1 V% b$ e/ S. F2 I<P><FONT color=#0000ff>/* request DOS services to close the file */
8 m# Q, Z# ?% R# P8 [9 ?" J& Tif (bdosptr(0x10, &blk, 0) == -1) 3 r4 h R" _2 J4 L U( L
{
* S. u: v2 Z1 h3 Pperror("Error closing file");
, O) R* t) ]$ R+ hexit(1); f0 v/ E2 |! t* z) w7 X
} </FONT></P>
: V9 i2 y! s0 Y& d3 P4 B9 p$ J<P><FONT color=#0000ff>/* reset the old dta */
: K9 N2 D+ X, s7 H$ Lsetdta(save_dta);
* V3 ~: h/ O9 z S% greturn 0; {. F7 u1 }3 j% }3 [- ]( ~ _, V
}
' ?8 n8 O8 ^* v1 c8 P" r</FONT>
- I, L0 @* v: Z' F, E</P>
0 P, L* I: p! T) X<P><FONT color=#ff0000>函数名: setfillpattern </FONT>
6 d6 p+ ?# K1 j# x! h! Z功 能: 选择用户定义的填充模式 * J+ |/ k% m5 K; A" w/ {9 r0 O
用 法: void far setfillpattern(char far *upattern, int color); 1 C) w% i1 W) b* G. x* ~5 R
程序例: </P>
0 T1 ?, |) S1 }# U- c/ V1 B7 n<P><FONT color=#0000ff>#include <GRAPHICS.H>
" B7 h, f) S4 Y, {' c#include <STDLIB.H>6 K' x9 H1 P& t5 v
#include <STDIO.H>
1 k' R+ l1 A- w2 T. B' p6 x* n1 a#include <CONIO.H></FONT></P>
) A2 B0 _1 k) P& F& [ F<P><FONT color=#0000ff>int main(void) 0 E9 I8 J* \+ i: D+ C! c
{ % a$ \' [ @9 [1 E0 I
/* request auto detection */ 3 a# G7 U) O5 h0 E' j6 [4 [ w) q7 N
int gdriver = DETECT, gmode, errorcode; P) k2 `+ W0 w9 m K
int maxx, maxy; </FONT></P>. Y0 @$ P9 q4 R. a G
<P><FONT color=#0000ff>/* a user defined fill pattern */ 6 n; b/ r. e8 o! C- Y
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; </FONT></P>
5 Z' `6 e& K8 ]1 F9 I( U' U<P><FONT color=#0000ff>/* initialize graphics and local variables */
) Z# B8 q& L4 K+ b% I' L T! Ninitgraph(&gdriver, &gmode, ""); </FONT></P>! x0 D# y f; T0 D2 E$ I* o+ _
<P><FONT color=#0000ff>/* read result of initialization */
! C2 j3 Y, O$ @! h* z. s. Nerrorcode = graphresult();
: W- a1 S( E8 d Dif (errorcode != grOk) /* an error occurred */ ; W0 {; F" a( p: @7 W0 _% c
{ # q" }) ]- V9 D' B
printf("Graphics error: %s\n", grapherrormsg(errorcode));
! l8 P7 T, C/ y) Eprintf("Press any key to halt:"); 6 M; }( ^+ i% E1 ~) v1 W' Z7 }- _
getch();
) k' l. L) [$ V( Nexit(1); /* terminate with an error code */
7 B4 b# X! N& { J' f5 W* v p% m6 R} </FONT></P>
/ ^0 k0 l! _( u5 _4 B<P><FONT color=#0000ff>maxx = getmaxx();
$ B5 R+ n9 O$ ?0 }$ x- k- t" x* Jmaxy = getmaxy(); ( k \! ]/ y& m& o$ l0 g
setcolor(getmaxcolor()); </FONT></P>
$ o7 V: h7 c7 O' _% C7 b6 |<P><FONT color=#0000ff>/* select a user defined fill pattern */ 5 U$ H" T; {3 N& o1 I
setfillpattern(pattern, getmaxcolor()); </FONT></P>& Q9 o, h0 }1 }7 p2 J
<P><FONT color=#0000ff>/* fill the screen with the pattern */
9 g, G- {# e/ hbar(0, 0, maxx, maxy); </FONT></P>' I$ ?/ u' k' h4 Q. l) n. f
<P><FONT color=#0000ff>/* clean up */
" u7 e% Y o; `1 Y. Q' U" m! igetch();
) v7 |: C4 _# v7 R9 z% iclosegraph();
% X1 R: Y+ S8 C" P1 |7 Areturn 0; 1 L& Z& R. `. U* i& L7 Y `
} " e5 v. f' R8 n0 s! P) [
</FONT>% b) R- m0 z/ ^6 L! C3 b
</P>
! H v$ @) [7 ~<P><FONT color=#ff0000>函数名: setfillstyle </FONT>
' T6 D/ w6 D1 h6 V; G X: Y. j功 能: 设置填充模式和颜色 % ]; x& Q) S1 a: \5 e q u
用 法: void far setfillstyle(int pattern, int color); , J# y9 ?9 K5 a- o4 W, q
程序例: </P>
6 k: f. K% L K<P><FONT color=#0000ff>#include <GRAPHICS.H>1 |3 a v X) R, o2 Q
#include <STDLIB.H> C8 t: ]8 V6 u! ^7 w
#include <STRING.H>
7 n9 w2 Y/ L& r2 a) r#include <STDIO.H>: y5 ~) m4 }6 L# L. u9 R
#include <CONIO.H></FONT></P>9 n* V& c) ?) X, q8 L
<P><FONT color=#0000ff>/* the names of the fill styles supported */
" q& t5 I8 z7 y1 H7 p7 ~2 Uchar *fname[] = { "EMPTY_FILL",
5 B" P6 J" z4 C! k9 j5 M; t, W"SOLID_FILL",
- ~+ B5 A/ T; Q, n! M; ?' ~"LINE_FILL",
6 N' F1 }7 \' G0 A& E. O"LTSLASH_FILL", + i* m3 Q! q5 E! u+ D. Y
"SLASH_FILL",
' }" u7 N* @) j9 r"BKSLASH_FILL", & \( c/ f8 @. H
"LTBKSLASH_FILL", . _0 z+ A: L" c- m: Q9 G
"HATCH_FILL", $ S; J; y3 F. y3 g8 F
"XHATCH_FILL", n8 D5 v/ b& k* V
"INTERLEAVE_FILL", 0 r" Z3 o+ @' z: I
"WIDE_DOT_FILL", - }% C7 X8 r* T6 }
"CLOSE_DOT_FILL",
$ x6 D, o1 `5 [/ @0 ]& ]9 b"USER_FILL"
0 c W' q2 M6 q9 h6 I5 r* v}; </FONT></P>9 y0 F7 u! p0 }/ u3 u! ]5 U' d
<P><FONT color=#0000ff>int main(void) & f1 r4 S' m) o* c0 |: c2 m
{
# Q2 ^6 p; [$ z7 F% t2 J/* request auto detection */ 1 e- P2 Z0 g6 j: U7 J
int gdriver = DETECT, gmode, errorcode;
( V. A4 b2 w" ?6 ~) Z2 Lint style, midx, midy; 2 I% B. A" _6 p# A/ I
char stylestr[40]; </FONT></P>5 }. s* }' Z! C' g$ M
<P><FONT color=#0000ff>/* initialize graphics and local variables */
, j$ U( A4 O7 w/ L* finitgraph(&gdriver, &gmode, ""); </FONT></P>4 C5 e/ f: M, h& y( D
<P><FONT color=#0000ff>/* read result of initialization */ . |4 b% i( b. k2 h& ^, g; Z& y
errorcode = graphresult(); 9 S; g: p# m3 |- S& `
if (errorcode != grOk) /* an error occurred */
. B8 g# i# P- y' s{ 9 I" J. E8 w9 F
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 4 k7 x& _, t+ j2 }' G9 V- S8 a0 q
printf("Press any key to halt:");
" U# p0 i( S6 S" Qgetch();
4 n7 P' T% M" ?exit(1); /* terminate with an error code */ / f& M* H4 Q6 H5 R
} </FONT></P>' Z. w& |0 B0 k" X* M* F6 j
<P><FONT color=#0000ff>midx = getmaxx() / 2; 9 [$ q( P4 [ C, c; X# I
midy = getmaxy() / 2; </FONT></P>
" W$ N) w, J! M2 ?<P><FONT color=#0000ff>for (style = EMPTY_FILL; style < USER_FILL; style++) ; W* w% _ r0 m, [' a0 r, L
{ 2 |8 z) \" T. }7 x& v8 R! X- m1 ~
/* select the fill style */
9 f( a0 R: U9 U( [setfillstyle(style, getmaxcolor()); </FONT></P> \+ ?, _1 B( V; S: M5 R1 D/ p0 j
<P><FONT color=#0000ff>/* convert style into a string */
7 J3 U, q" j2 i* Hstrcpy(stylestr, fname[style]); </FONT></P>
E. N3 R: ~8 @, x<P><FONT color=#0000ff>/* fill a bar */ - q m5 S r+ ~+ F5 d8 k, m: J
bar3d(0, 0, midx-10, midy, 0, 0); </FONT></P>" Z* T+ B* {. } u; _' C
<P><FONT color=#0000ff>/* output a message */
; L- ]5 C+ f. ^' V7 touttextxy(midx, midy, stylestr); </FONT></P>
* N1 m, u% P$ a9 B% m( u$ e/ D0 o9 l<P><FONT color=#0000ff>/* wait for a key */
" M- c1 H7 b0 {$ i+ b$ D2 h* \getch(); " _! B2 ]. W6 J8 @& k
cleardevice(); / ?! [2 `, C7 p- G" l8 A
} </FONT></P>0 K" h- {6 J3 b) D. }1 m
<P><FONT color=#0000ff>/* clean up */ " s7 L; ~7 E$ }$ R# D
getch();
5 v, f; }; U4 @. Q, _. b2 q* Q3 ~closegraph();
3 A! D- p6 B' l+ Qreturn 0;
- e$ _1 L8 O* v4 L7 u5 f} </FONT>
0 @: A7 H# @- s! K4 G+ @/ i5 S w" q6 o5 L) \/ @2 L
</P>: t" E+ c* U9 ?$ T
<P><FONT color=#ff0000>函数名: setftime </FONT>
3 F, c" z% z- i! D, g d功 能: 设置文件日期和时间
6 A9 L' ~, G+ U+ E# L- g+ n: \0 S用 法: int setftime(int handle, struct ftime *ftimep); 1 j' U$ e: ^' C! E9 d
程序例: </P>
" J( G# }6 {: ?4 ]# ~<P><FONT color=#0000ff>#include <STDIO.H>
^& D! x7 `0 |$ F! @; F; A#include <PROCESS.H>
% h; t) N/ b# U/ a# c#include <FCNTL.H>
& }- u4 ~4 S, g#include <IO.H></FONT></P>
: @9 Y# k+ h& d/ {# W# z& @<P><FONT color=#0000ff>int main(void) . w' b2 K5 H: ]. @% w
{ $ N& K: Z( p& N; z* C
struct ftime filet; 3 M& I" p4 g. K( ^
FILE *fp; </FONT></P>- V1 B3 m) ?$ n4 e: K
<P><FONT color=#0000ff>if ((fp = fopen("TEST.$$$", "w")) == NULL) 0 E4 T8 L- e7 r7 l# T" g
{ 2 D- O. a2 c1 ?. o7 l
perror("Error:");
8 X: @, u9 k6 }/ G! _7 V9 Y8 x' ]exit(1);
- E/ S9 `) {+ O/ J' T: P d7 c/ p9 ^} </FONT></P>
. Z3 _: K' L9 M! n<P><FONT color=#0000ff>fprintf(fp, "testing...\n"); </FONT></P>5 e: F0 B& a# A& j! l# ~
<P><FONT color=#0000ff>/* load ftime structure with new time and date */ 7 D: u3 l# v1 a9 i9 B
filet.ft_tsec = 1; # A! t) u( N& d# b; \) w) H
filet.ft_min = 1; : i& k+ i* J: M
filet.ft_hour = 1; " r5 i1 X S3 y8 a1 [9 Y1 \$ k
filet.ft_day = 1; $ }. X' u; }; g. q/ ?
filet.ft_month = 1; + Y1 i N3 N/ B7 `% D- k5 X/ j- j
filet.ft_year = 21; </FONT></P>9 U/ B, n! m" v( [
<P><FONT color=#0000ff>/* show current directory for time and date */
" {) B& p# U, ^- t; isystem("dir TEST.$$$"); </FONT></P>7 C( a. C+ r! i* e" V8 S6 e& l- a
<P><FONT color=#0000ff>/* change the time and date stamp*/ 2 w8 A0 M1 f: n' {1 q6 |
setftime(fileno(fp), &filet); </FONT></P># `; h# h9 p: f0 J
<P><FONT color=#0000ff>/* close and remove the temporary file */
6 |8 ?3 k7 x' hfclose(fp); </FONT></P>
2 n7 t* i N: i<P><FONT color=#0000ff>system("dir TEST.$$$"); </FONT></P>
( E4 A+ K7 S2 U! C4 D9 t' X<P><FONT color=#0000ff>unlink("TEST.$$$");
, F8 F) i8 z6 W; k5 greturn 0; 2 n$ ^0 f& n1 w) z6 `
} + N ?0 ?5 n3 y2 D: h5 `
</FONT>
4 A+ p5 {8 j/ T( I" y0 V+ _/ d</P>
f, j0 H! s6 u }5 R: z<P><FONT color=#ff0000>函数名: setgraphbufsize </FONT>6 L9 G6 @# \+ ^7 {- U2 b0 o
功 能: 改变内部图形缓冲区的大小 4 }0 }+ G9 d* U4 K% H8 z5 q
用 法: unsigned far setgraphbufsize(unsigned bufsize); - ^! x4 o. F+ o& h: v
程序例: </P>( ^; r, g8 T1 {) i0 y
<P><FONT color=#0000ff>#include <GRAPHICS.H>3 Q7 K5 ], X1 D) K3 C
#include <STDLIB.H>' s0 O& ]' \& z+ ~0 S( B* L9 A1 G& W
#include <STDIO.H>: p$ W# ?5 K. t( Y; g1 W
#include <CONIO.H></FONT></P>
B( r8 N( \' g3 t, r<P><FONT color=#0000ff>#define BUFSIZE 1000 /* internal graphics buffer size */ </FONT></P>
3 @$ n8 ~0 b2 \$ c, ~0 W- @; }<P><FONT color=#0000ff>int main(void)
4 [1 |1 @! \9 C2 [ a8 I{ + l0 i* W# ^4 L3 {; R, y0 q6 ~
/* request auto detection */ 0 }& `) ?# T {0 A4 i
int gdriver = DETECT, gmode, errorcode;
0 u r v5 Q! r: G- {7 R" Fint x, y, oldsize; 4 O/ I; L0 ?. G- K8 P8 E
char msg[80]; </FONT></P>
: S6 l5 \3 w2 v<P><FONT color=#0000ff>/* set the size of the internal graphics buffer */ - {: }3 F% O& R# l8 U
/* before making a call to initgraph. */
& M$ f& S4 _: w B9 D; g, H: `oldsize = setgraphbufsize(BUFSIZE); </FONT></P>
+ c) `! n" T9 @9 N9 q2 O<P><FONT color=#0000ff>/* initialize graphics and local variables */
3 A- x2 j; r1 e9 t+ p1 o* S7 ]initgraph(&gdriver, &gmode, ""); </FONT></P>; l ]! @7 E, N5 l0 X1 _
<P><FONT color=#0000ff>/* read result of initialization */
+ [+ i# z: A1 gerrorcode = graphresult();
^) N1 ^; v3 I/ @ H$ T Hif (errorcode != grOk) /* an error occurred */ . {6 v3 i& z' D
{ ' w& O O1 L2 p( e& ^+ g
printf("Graphics error: %s\n", grapherrormsg(errorcode));
8 U$ d$ q/ {# A+ j9 F4 qprintf("Press any key to halt:"); 7 E: s& X0 x" o8 U W0 X
getch();
8 N+ D1 \/ S" D: ?* G( d- qexit(1); /* terminate with an error code */
9 f$ t* n7 p: f3 j2 R} </FONT></P>
- J/ r9 E0 ?; v8 y<P><FONT color=#0000ff>x = getmaxx() / 2; 4 M6 u7 d# @0 A$ I G e5 h& \
y = getmaxy() / 2; </FONT></P>
& z$ Z1 l1 H- t) [* ^( n ?<P><FONT color=#0000ff>/* output some messages */ . t7 i* g+ ?3 a& Y
sprintf(msg, "Graphics buffer size: %d", BUFSIZE);
% s! i: |" g0 ] D/ O @/ }( Hsettextjustify(CENTER_TEXT, CENTER_TEXT);
# U% \9 R+ c! A$ _2 Louttextxy(x, y, msg);
' V+ O- `1 o5 Jsprintf(msg, "Old graphics buffer size: %d", oldsize); ( T" n+ N, H$ ?/ L& S# c4 v5 v
outtextxy(x, y+textheight("W"), msg); </FONT></P>* Y+ N4 O& P) d1 M6 P. u
<P><FONT color=#0000ff>/* clean up */ . d( `$ o6 S3 |
getch(); , W: ]5 `9 x1 |; M& {- d' G7 x( [
closegraph(); . P& k4 r! n, X3 i h, S
return 0; * J q, a# ?) L: }( ?- @
}
. m) Y# c, e& j6 f! O% U0 m% F
3 |& Q) ~: C3 [$ \9 O0 u5 J* u) ~" p* @3 K
</FONT></P>
0 R, N# r3 H# Z7 P/ [! z<P><FONT color=#ff0000>函数名: setgraphmode </FONT>- [, z$ n# l% k5 O2 e
功 能: 将系统设置成图形模式且清屏
4 M' ?8 i7 v1 E. n: X* Y8 Y用 法: void far setgraphmode(int mode);
& c% @$ O% w/ |+ U程序例: 5 n/ \$ M# {1 j! ^4 a
5 n& C( @% l0 N4 T
<FONT color=#0000ff>#include <STDLIB.H>
! ~4 j1 ~5 D D#include <STDIO.H>4 e$ ?. J8 K! r
#include <GRAPHICS.H>#include <CONIO.H></FONT></P>4 l! a" c% G' k F- n
<P><FONT color=#0000ff>int main(void)
# x9 `2 W q, `, s/ a{ : I D: [" y, ?7 e
/* request auto detection */
) u8 h" q8 A3 |) D7 }int gdriver = DETECT, gmode, errorcode;
4 e- \% T# E$ W+ v: Aint x, y; </FONT></P>
E6 F) j9 U9 g/ H( \1 `<P><FONT color=#0000ff>/* initialize graphics and local variables */ 9 b: i6 q3 V2 E
initgraph(&gdriver, &gmode, ""); </FONT></P>/ k7 E/ f+ U! @& |
<P><FONT color=#0000ff>/* read result of initialization */
( S h: M# _8 m; a& }errorcode = graphresult(); 7 k0 G' _0 ?7 B& T6 b
if (errorcode != grOk) /* an error occurred */ 3 {$ o* w8 z8 x6 H/ t% i
{
/ o* h o. _* Sprintf("Graphics error: %s\n", grapherrormsg(errorcode));
1 a. D' ?5 f& |2 n8 _printf("Press any key to halt:");
" d/ Q2 U- m$ Sgetch();
7 ^8 I& _/ h- C2 v7 V- V0 Hexit(1); /* terminate with an error code */ 3 o8 }; V( N2 r0 n; }' ~
} </FONT></P>
6 b) M& q( N7 o, E<P><FONT color=#0000ff>x = getmaxx() / 2; 0 E) l+ @' M4 W3 I
y = getmaxy() / 2; </FONT></P>5 T! j6 T* v# M+ V( Y% s$ _
<P><FONT color=#0000ff>/* output a message */ ' R( x4 f: B; d. N% T0 g ]
settextjustify(CENTER_TEXT, CENTER_TEXT); 0 i; T3 E+ @# P7 ~/ c$ s: u
outtextxy(x, y, "Press any key to exit graphics:"); * j! f* H- H, c1 j3 R! W9 N
getch(); </FONT></P>9 ^- N) C5 w7 m3 ~$ E. j
<P><FONT color=#0000ff>/* restore system to text mode */ 5 e) X" a1 A& ~- D
restorecrtmode(); 6 [" Z3 B2 @" {7 j# H/ F2 Y
printf("We're now in text mode.\n");
3 Y) f( T H: a- Iprintf("Press any key to return to graphics mode:"); 2 c; A+ J* J, x/ l
getch(); </FONT></P>
: r% ]8 ^' F+ Z+ ?3 c<P><FONT color=#0000ff>/* return to graphics mode */ # n1 C4 x7 q4 r5 k& N8 ^: s6 H
setgraphmode(getgraphmode()); </FONT></P>$ \) K0 q! n( G, d- K
<P><FONT color=#0000ff>/* output a message */ 4 |! d( s& q% w# w( l) V8 b
settextjustify(CENTER_TEXT, CENTER_TEXT); $ e# G- o# w6 W$ F1 l% M
outtextxy(x, y, "We're back in graphics mode.");
/ W' J& J" L2 |outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>
1 J% N6 ]# U3 j/ |5 M' E<P><FONT color=#0000ff>/* clean up */ 1 h! }' \' C, n O
getch();
$ |2 d7 T$ g) h" Gclosegraph();
- H' X; t- B/ W& ereturn 0;
5 n8 r9 u: u$ U( e}
7 }3 H8 E) {9 u4 w( S8 V% }: Y6 S- S/ k& V x
0 C( J" h8 g$ e& X: S: @</FONT><FONT color=#ff0000></FONT></P>0 v9 j2 K; u& ~7 v8 |
<P><FONT color=#ff0000>函数名: setjmp </FONT>
# B E. k2 @6 K8 R( b: `* X功 能: 非局部转移 : L1 ?; W7 n' X: L% s
用 法: int setjmp(jmp_buf env);
, G* d& p) ^0 h* G# z' `程序例: </P>" k5 y; Y+ m, n/ x) _& d( U& T
<P><FONT color=#0000ff>#include <STDIO.H>/ d; |+ O( f2 ^2 E: n
#include <PROCESS.H>& ~1 B3 Q6 H" z1 b& A8 y
#include <SETJMP.H></FONT></P>
( T& U% i& s4 N$ }; g* o<P><FONT color=#0000ff>void subroutine(void); </FONT></P>
( y) F$ \3 ]- y: N0 X! |) i7 c<P><FONT color=#0000ff>jmp_buf jumper; </FONT></P>
. d: T2 R! [+ d<P><FONT color=#0000ff>int main(void) $ U/ V: Q! c3 G& y6 u: T. q
{
! p+ q; t8 V8 q4 nint value; </FONT></P>8 E: q& r) s2 H# W
<P><FONT color=#0000ff>value = setjmp(jumper); + s7 f8 Q. k' r+ U$ n
if (value != 0)
6 U- ^ e! i: }$ y{
6 M$ N* [; ^/ s" V( U9 j& fprintf("Longjmp with value %d\n", value); ( a. V6 c; g) |# ^
exit(value); + x9 @5 x6 ~& g0 w$ a# h
} / v; j1 g5 V* M/ Y
printf("About to call subroutine ... \n"); ! K% H0 T0 x% w' G2 V0 J$ ]
subroutine();
8 j: U$ X! |' w Q# Z, zreturn 0; , z4 J- @" x% E' j! ~* @
} </FONT></P>9 C( U4 M5 C' _3 R
<P><FONT color=#0000ff>void subroutine(void)
0 S: w% ?2 \& g; d, p5 d{
$ a. o" A. j' Y2 S+ Llongjmp(jumper,1); ; ?2 q( K( k; R/ y- N3 k
}
j2 l5 P) J# d K4 X) P</FONT>: |5 ^) D1 d H1 ^/ d
</P>6 u/ P# e( V. C7 s! \3 b) Z) x
<P><FONT color=#ff0000>函数名: setlinestyle </FONT>
# ?6 e3 U* b! q; p8 {功 能: 设置当前画线宽度和类型
9 P' r2 C5 d7 M, O5 r v( r用 法: void far setlinestyle(int linestype, unsigned upattern);
. b7 s& t% N5 d程序例: </P>
! d3 N8 p: i- ^- e; x<P><FONT color=#0000ff>#include <GRAPHICS.H>
, |4 ?4 c6 S4 ]; f) a5 u#include <STDLIB.H>4 n# j( |0 p( k, e1 @- d5 N
#include <STRING.H>
0 Z( D, A" }1 B( {1 S8 u' r* }9 @#include <STDIO.H>! u+ a1 Z6 U- R1 p; T
#include <CONIO.H></FONT></P>
5 Z7 _9 G! l/ U2 J8 I, h<P><FONT color=#0000ff>/* the names of the line styles supported */
# }' H+ b( d% z) E3 e7 J2 {$ @% xchar *lname[] = { + r' ^1 `9 ^4 R: _! c& G
"SOLID_LINE", ) {& H5 t( ]$ H' h, [* L
"DOTTED_LINE",
% h+ ]) j. d, p: W0 y X( j9 {) i5 F"CENTER_LINE",
/ d' T- b: v U( x }2 Y7 {"DASHED_LINE", ) N& Y- U7 {8 `1 @8 a
"USERBIT_LINE" 4 s3 ] w. S4 x4 \" h
}; </FONT></P>; }9 ]2 }! D7 q
<P><FONT color=#0000ff>int main(void) 2 S2 Z% i" B( @3 n
{
; S2 i& u1 [1 [/* request auto detection */
* G' ?* _8 n7 T7 z Mint gdriver = DETECT, gmode, errorcode; </FONT></P>6 ^5 |7 L2 q8 c7 g4 [
<P><FONT color=#0000ff>int style, midx, midy, userpat; 1 \& {1 ]* i6 @ _2 l2 F
char stylestr[40]; </FONT></P>
1 ?: H. W4 @' U' T/ v- J<P><FONT color=#0000ff>/* initialize graphics and local variables */
# T) ]5 F2 k& j% ]initgraph(&gdriver, &gmode, ""); </FONT></P>% }, k9 u( e3 q9 I. v
<P><FONT color=#0000ff>/* read result of initialization */
" i* b( `( f+ g- o" T& g* t# Q; E- Lerrorcode = graphresult(); 2 c' v: Y" K* _- D% K4 p4 ]6 l. \
if (errorcode != grOk) /* an error occurred */
% {( D+ e3 ] b* r" }1 x+ z{ % E9 o% N; Y. O- _8 _3 s; s
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 5 n6 ]1 Y- i* ?! s6 a- I
printf("Press any key to halt:");
$ J6 U& |) o7 V$ ~; C% Agetch(); # }2 E3 b& D$ u6 m$ v
exit(1); /* terminate with an error code */ # a' C- W! M3 W
} </FONT></P>
9 R2 R2 C0 k0 {" n% B6 j2 j% [: u<P><FONT color=#0000ff>midx = getmaxx() / 2;
* |( Y; k3 {' r2 c2 A" d# \; ]* Nmidy = getmaxy() / 2; </FONT></P>
! U8 [9 {3 f4 \: g) x<P><FONT color=#0000ff>/* a user defined line pattern */
m; t# ?$ B) `+ w2 g! J/* binary: "0000000000000001" */ % {) C0 q$ l' H
userpat = 1; </FONT></P>
& f; n. n: w7 t1 g* N<P><FONT color=#0000ff>for (style=SOLID_LINE; style<=USERBIT_LINE; style++) * x; q5 S% N6 c. z& n( Q
{ " z) u- F y% J+ ^" i- H9 V' [3 E
/* select the line style */
0 e' {% r6 @% l! _; f$ o8 j- dsetlinestyle(style, userpat, 1); </FONT></P>
* S+ K. f; B" X. M2 a<P><FONT color=#0000ff>/* convert style into a string */ k5 }6 _/ y0 Z0 C- N
strcpy(stylestr, lname[style]); </FONT></P>
4 S3 @! |6 v4 c, P+ S+ K<P><FONT color=#0000ff>/* draw a line */ , ~% R: e0 t& L& b9 J$ b
line(0, 0, midx-10, midy); </FONT></P>
1 F4 s/ M: [3 S. K% q<P><FONT color=#0000ff>/* draw a rectangle */ B$ b" w) M( q2 h
rectangle(0, 0, getmaxx(), getmaxy()); </FONT></P>
2 {2 H4 N! K V6 c& k# e<P><FONT color=#0000ff>/* output a message */
8 V' |) M5 v# N+ F. C; L9 @5 y- ]outtextxy(midx, midy, stylestr); </FONT></P>& q3 B0 @5 d; C' x6 j0 r
<P><FONT color=#0000ff>/* wait for a key */ 3 y3 i; T4 o$ {4 E$ y
getch(); . g3 e! p2 _6 h% D3 @
cleardevice();
6 V4 d" K- g4 q" O: i K5 I& q} </FONT></P>
7 c: F/ o u$ h6 R<P><FONT color=#0000ff>/* clean up */ K) ^% U8 y7 ~! h8 v0 s9 a
closegraph(); ; `, z* A# E2 p4 @/ f3 x' I
return 0; ! J" e) }' X" n% k2 `, s4 B5 z
} </FONT>$ |% z6 m2 ?; K* T4 E3 W
& ^, Y% ?7 S( }5 {' ?" v( O
. N( F4 C- Y1 v# ~; ?</P>
5 {/ g& J, H8 `* [; w0 e8 X" g<P><FONT color=#ff0000>函数名: setmem </FONT>5 ^( A4 J* S. I6 _: }8 r9 F" N
功 能: 存值到存储区 6 K+ l: N! O( L+ j$ X
用 法: void setmem(void *addr, int len, char value);
# }, [/ |! o2 H/ p8 Q- j程序例: </P>
6 f" x( ~" ]) s3 v! |<P><FONT color=#0000ff>#include <STDIO.H>7 m5 r& M* h4 [. d6 o' K
#include <ALLOC.H>
1 b" a6 M# `6 n6 q#include <MEM.H></FONT></P>3 h* k: G+ R, b/ g$ \
<P><FONT color=#0000ff>int main(void) ) u4 Y+ F2 W- r! |9 J- x
{
/ j6 r6 o5 C6 K& e. uchar *dest; </FONT></P>
: {3 ^" d/ l& c6 h a8 n4 Q3 `) b<P><FONT color=#0000ff>dest = calloc(21, sizeof(char));
3 E! N4 y9 R: P: Dsetmem(dest, 20, 'c');
( r" l' U" U1 |* Kprintf("%s\n", dest); </FONT></P>" z' L! S+ H* d3 U$ ~! C! N% o
<P><FONT color=#0000ff>return 0;
- i9 U3 T4 x* q# U$ M+ A$ c N: A v} </FONT>$ P2 O* e" C1 ]( K) ]
( B/ o! n. n1 g! k, v; m
/ n$ u( u/ g' i</P>! U. d- E* ?6 r7 T; X2 w; [
<P><FONT color=#ff0000>函数名: setmode </FONT>
4 g8 z9 y8 N; f9 G* g' z4 q功 能: 设置打开文件方式
5 y; m+ O' k) A" A用 法: int setmode(int handle, unsigned mode);
# t$ o( {# Q l0 F n/ w1 w6 R程序例: </P>
( B9 _# C$ a: H h+ k<P><FONT color=#0000ff>#include <STDIO.H>, j3 e- r: R* T S" ^, ?
#include <FCNTL.H>
* r& [; c9 \6 c' M- {7 S#include <IO.H></FONT></P>/ s8 ^; F" U9 P- ^, T0 j; I3 @
<P><FONT color=#0000ff>int main(void) 9 I' @1 N W( r O" B3 p" x
{
8 A* ~! B- i5 d* Y) l# E$ p" uint result; </FONT></P>
* r: E. ]! w, Z$ j J& ~& |<P><FONT color=#0000ff>result = setmode(fileno(stdprn), O_TEXT);
7 b7 G% @2 F/ z# b9 S* Iif (result == -1)
9 _+ C0 y! \/ eperror("Mode not available\n"); ! q0 x7 X6 \6 V9 }. M
else
% b3 [; W) y6 Bprintf("Mode successfully switched\n"); . p w0 c m! G" e3 b; X
return 0; ( h* C) [) _3 B6 D$ W: B
}
* }; U( M. ]0 q/ n; h- t/ T# l+ l0 Y9 q, O# T( O" K, `6 t/ p- a
</FONT>5 E! E7 Q% ] X0 s" \+ D4 Y
</P>
, T# K$ r$ g* q<P><FONT color=#ff0000>函数名: setpalette </FONT>( ^/ A# F! F- ~6 j, d+ N6 o* j
功 能: 改变调色板的颜色 1 K6 ^9 |1 p7 u2 d9 R' P& q
用 法: void far setpalette(int index, int actural_color);
$ Y3 Q' v. ^( r6 w2 H& l程序例: </P>
/ a* g, s) P" @<P><FONT color=#0000ff>#include <GRAPHICS.H>1 V9 X! f# s- w* i- k' ~6 @: F
#include <STDLIB.H>" U: O N+ u& X
#include <STDIO.H>0 y* X% E f5 a- c+ s
#include <CONIO.H></FONT></P>9 J# u% e3 Z! q; R! t) P
<P><FONT color=#0000ff>int main(void)
: w& B* S; w5 E1 y e' [# C8 ?{
# `# T7 G4 V! C7 ?% u/ j5 d- I/* request auto detection */ * t" I9 N) g4 C) {/ `, f
int gdriver = DETECT, gmode, errorcode;
7 Y& j6 }- ~7 w% R _6 Eint color, maxcolor, ht;
! c h q U5 ]! k! `$ [2 z! u" I( Lint y = 10; 7 ^8 y7 P$ }$ y l$ K
char msg[80]; </FONT></P>5 R9 S- u+ J* w2 T* m l
<P><FONT color=#0000ff>/* initialize graphics and local variables */
- D2 D: Q+ ]3 j% Ninitgraph(&gdriver, &gmode, ""); </FONT></P>0 S: @% }3 x& q8 O; s. ~3 Q; N2 z
<P><FONT color=#0000ff>/* read result of initialization */
* i( i# E$ {* k% ~ m6 @errorcode = graphresult();
) I# \8 t- w. _: v) j0 r! Hif (errorcode != grOk) /* an error occurred */
* i8 G R, s9 G4 d3 ?' `% q( D |{
3 v1 f- D" U4 [" {- [+ t1 w0 Yprintf("Graphics error: %s\n", grapherrormsg(errorcode));
& {" Q7 I% J9 K. r' o% o# |) mprintf("Press any key to halt:"); / J$ f! J- Y) z: p% z6 A8 F
getch();
$ H: C( p) B& S; b: Lexit(1); /* terminate with an error code */ : y4 K. f: U Y {+ S& V
} </FONT></P>
3 a" _4 e) c( j C8 e j<P><FONT color=#0000ff>maxcolor = getmaxcolor(); b& P j/ d; r+ X( Z
ht = 2 * textheight("W"); </FONT></P>
/ J! ~* c& d9 v" U! N<P><FONT color=#0000ff>/* display the default colors */ & H1 b8 y$ w+ f
for (color=1; color<=maxcolor; color++)
" N2 k2 X4 t4 D' Q5 T{ & b& k5 D* S7 f/ U
setcolor(color);
Q/ I P f% c0 ^sprintf(msg, "Color: %d", color); 3 D" b( q7 ]# _0 e5 V" E( C- l( H6 b
outtextxy(1, y, msg);
2 y1 M9 \7 [6 I+ K3 Qy += ht;
) z# J! E2 h" S) v* ` k} </FONT></P>
. i% E6 P/ c& h4 Z<P><FONT color=#0000ff>/* wait for a key */ ) x$ q1 V8 e8 h! m& ]3 g% C
getch(); </FONT></P>2 `- K9 u4 Y6 e6 F6 s4 j2 |
<P><FONT color=#0000ff>/* black out the colors one by one */
6 X* s! A( S: x! z7 I; efor (color=1; color<=maxcolor; color++)
9 C" ]+ L2 C# ^& D{ & P6 R7 J; d9 l4 m M7 G
setpalette(color, BLACK);
8 @1 r2 T# O3 `! Ggetch();
z, g( @% A3 l+ O1 v} </FONT></P>
* G5 R7 }, y! V<P><FONT color=#0000ff>/* clean up */
0 ^4 Y- F" T) W; J+ y0 yclosegraph(); - v- ?& ]' n; R. D) y
return 0; ! H! r0 p7 \% t' E9 i1 n( t
} 8 n& I, a9 N7 W( }, g& E
</FONT>5 G3 [) L# u9 C5 z# g* h8 k
</P>
' o- c; G7 j K% y8 u<P><FONT color=#ff0000>函数名: setrgbpalette </FONT>
1 J+ T: z% k! q R% Q6 b* \功 能: 定义IBM8514图形卡的颜色
8 K) M4 |/ u! W用 法: void far setrgbpalette(int colornum, int red, int green, int blue); ) u1 q1 t! B1 J7 U7 M
程序例: </P>9 I; \, }$ L; Z! ]
<P><FONT color=#0000ff>#include <GRAPHICS.H>
+ y N( |& n' R {#include <STDLIB.H>1 ]' S- _5 Y* @8 r; r% \
#include <STDIO.H>
/ \) g1 v: q$ K3 Y#include <CONIO.H></FONT></P>
! r* D" C1 W- q% m: q/ S Z8 a<P><FONT color=#0000ff>int main(void) , @" {5 `' C. _* g
{
8 ~9 n8 H# i- ^/ Z8 _8 E6 L! a/* select a driver and mode that supports the use */ : k7 F& D0 X6 m6 _9 [
/* of the setrgbpalette function. */
. [7 ]3 U8 f( Sint gdriver = VGA, gmode = VGAHI, errorcode;
( t' Y1 B3 T% D, g: M0 K. astruct palettetype pal;
% U. M U( o0 I C m8 qint i, ht, y, xmax; </FONT></P>1 j6 H' e% n2 Q0 S' M& W' { _3 O O
<P><FONT color=#0000ff>/* initialize graphics and local variables */ % r* d" k$ r$ G+ d F' [
initgraph(&gdriver, &gmode, ""); </FONT></P>
1 r3 J% L. s9 G1 V: u<P><FONT color=#0000ff>/* read result of initialization */
/ m: T' A! j( ~5 i' `4 Y- J q- Zerrorcode = graphresult();
h2 h$ l! _5 S" O( ~if (errorcode != grOk) /* an error occurred */ 5 l, t+ s7 i/ T2 _' D
{ 8 m' y; C! j/ U% @2 {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
3 J, o8 m% Z. K* E8 h# p' Q1 g4 Wprintf("Press any key to halt:");
/ j# W/ `. `. W3 z( Ogetch();
+ F+ y" W1 z3 d# l& ]! [9 C! gexit(1); /* terminate with an error code */ 3 ^! F0 e9 s& z+ h, j; d
} </FONT></P>
8 ?& X; e9 q1 s1 Q; A; i, x5 `<P><FONT color=#0000ff>/* grab a copy of the palette */
/ }8 g5 u1 [% w! v/ ggetpalette(&pal); </FONT></P>+ R4 X, A4 y' e
<P><FONT color=#0000ff>/* create gray scale */ $ G5 Z9 b# x' |$ e0 U0 e
for (i=0; i<PAL.SIZE; <br i++)> setrgbpalette(pal.colors, i*4, i*4, i*4); </FONT></P>8 U1 h& u7 g! `9 c7 V
<P><FONT color=#0000ff>/* display the gray scale */
2 [% ~2 r0 |* H8 Kht = getmaxy() / 16;
# v4 _; X" q4 ^5 J. Axmax = getmaxx();
% W1 i- {0 S/ I6 X3 ry = 0;
8 x, K( [( [' Tfor (i=0; i<PAL.SIZE; <br i++)> {
( T) J& |* f/ q) m5 i: T, b2 Zsetfillstyle(SOLID_FILL, i); , @8 n, e& \5 k: q
bar(0, y, xmax, y+ht); # x% z ^+ x6 ]
y += ht; 1 [$ t1 k0 s* x$ G# `$ n8 ~
} </FONT></P>
6 H: k- @, j- m! n& ^2 _<P><FONT color=#0000ff>/* clean up */ 3 L# R, [% V9 N4 E9 s
getch(); : l( @. P" s3 X# R! x; u
closegraph();
1 d% H) a! y8 X, [( V( C+ Wreturn 0;
$ f0 U9 q8 W8 l0 }0 F5 m}
' }( ]; z# n6 Z! x( V* W( T$ T# ]</FONT>
$ O' k1 E, _& ~1 ]$ s
: l- R; q8 b+ L6 N9 {</P>4 X' u9 ~; e! Z- k
<P><FONT color=#ff0000>函数名: settextjustify </FONT>( } _% A/ S7 o1 U8 P
功 能: 为图形函数设置文本的对齐方式
; o2 k: A8 p, b6 ~# [. @' j. I( ~0 k用 法: void far settextjustify(int horiz, int vert); & i# `9 W# l t1 F
程序例: </P>
6 a2 i$ A) l. O- M<P><FONT color=#0000ff>#include <GRAPHICS.H>
- \! L9 Y6 i. L3 V- M#include <STDLIB.H>( s9 V7 A% [ l3 [7 ~2 ^
#include <STDIO.H>
- i1 N3 c8 G8 F6 P: u- J/ C- w4 v8 V#include <CONIO.H></FONT></P>
* M5 _% W! |" B/ [2 K2 h<P><FONT color=#0000ff>/* function prototype */ . M4 w, W0 V7 U7 n
void xat(int x, int y); </FONT></P>2 A. t$ q8 s0 o( Q2 _$ E+ Q1 Y' W
<P><FONT color=#0000ff>/* horizontal text justification settings */
; H8 }4 B. D! u u6 @char *hjust[] = { "LEFT_TEXT", 2 U) S: g9 C# p- G5 z( t' G
"CENTER_TEXT",
s6 s: z z1 c7 u7 Q/ O"RIGHT_TEXT" 2 ]7 ?0 ^ j% d8 Q! P4 _( J4 k) Q
}; </FONT></P>( I% h; @$ g, c- K. ~6 N
<P><FONT color=#0000ff>/* vertical text justification settings */
2 _" a6 J# R7 R" F$ R% J: pchar *vjust[] = { "LEFT_TEXT", # w$ o- J7 I1 l U( ^# Y
"CENTER_TEXT",
# O/ p# D- {; L( t7 M"RIGHT_TEXT" " z7 x' q8 x6 w
}; </FONT></P>
- y/ n7 X, |( n3 E, o- `8 b( e<P><FONT color=#0000ff>int main(void)
" i$ R1 A5 V' Q0 Q# K' _{ 5 \# ?+ N5 T2 G( W% _
/* request auto detection */
+ k; R; W/ {, o0 u) V+ Oint gdriver = DETECT, gmode, errorcode; ! [1 v7 y7 g- M( {3 o5 X
int midx, midy, hj, vj; + O! }7 D7 `% C7 R( x% H
char msg[80]; </FONT></P>" j1 M& U6 o: j1 J* m7 a8 |3 l, X
<P><FONT color=#0000ff>/* initialize graphics and local variables */
M: E+ O" ^7 g# b% m3 }initgraph(&gdriver, &gmode, ""); </FONT></P>
& F0 f- }7 e8 L<P><FONT color=#0000ff>/* read result of initialization */
' v& y( E; g- a# Y) Gerrorcode = graphresult();
( \! t; c( @1 q* oif (errorcode != grOk) /* an error occurred */ / f' K% Z& p; O% u
{
/ m2 ~+ C+ `! O) C) i! ^/ u$ D9 Qprintf("Graphics error: %s\n", grapherrormsg(errorcode));
. ?5 N' }' B+ E3 S7 q( Cprintf("Press any key to halt:");
5 \8 g7 l9 i( ] ugetch();
6 G7 g& _5 c9 b1 [& s$ |" y' wexit(1); /* terminate with an error code */ % \3 ]( n$ y% W6 k5 u0 W6 M n
} </FONT></P>( t* h1 f4 ]* `7 O9 H/ Z# a
<P><FONT color=#0000ff>midx = getmaxx() / 2; 4 s% d7 Q8 n; W) n
midy = getmaxy() / 2; </FONT></P>
: R) s# d2 p. @# b: \3 J- q9 _<P><FONT color=#0000ff>/* loop through text justifications */ . R" A6 P y' \% e3 U
for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++) 3 B x" [8 I6 t. f# f& {- F
for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) . n- V9 ^: X- v) x
{ : O/ E4 B9 U* m! s( O6 B
cleardevice();
6 v% V; [- \/ s/* set the text justification */
7 Q, @# `1 a$ N! }, _, v% Fsettextjustify(hj, vj); </FONT></P>
) w5 [3 x0 K' Q1 l<P><FONT color=#0000ff>/* create a message string */
( o7 ^/ Q4 }5 g; J" E7 J( u: lsprintf(msg, "%s %s", hjust[hj], vjust[vj]); </FONT></P>- i- w. d! w* W K9 a* u) k8 T
<P><FONT color=#0000ff>/* create cross hairs on the screen */
9 O" Y9 D1 \3 `. \( |xat(midx, midy); </FONT></P>' {' ?$ n: M, Q6 F" _
<P><FONT color=#0000ff>/* output the message */ 4 F: s+ O. W, c
outtextxy(midx, midy, msg); 3 m" [+ l6 {* H5 O6 [6 \
getch(); 8 y! i% Q/ |" G+ e, t2 S
} </FONT></P>1 O) c; r) \( Y
<P><FONT color=#0000ff>/* clean up */
; G5 D3 S8 P* X, ~# Aclosegraph(); / S8 ~" I4 p8 T6 L/ V1 K, X" e4 k
return 0; # H b% I W- h' C. _% N, M
} </FONT></P>
5 {) \4 ]4 J" r( x<P><FONT color=#0000ff>/* draw an "x" at (x, y) */
% k: A- c! P9 {, t- ]3 Dvoid xat(int x, int y)
% b7 X" M/ G3 E4 x/ m4 O% \) U{
# G3 j$ l. |6 I: [line(x-4, y, x+4, y);
9 m% e/ T2 T0 I6 Sline(x, y-4, x, y+4);
6 u0 [+ f& Y, z& a} </FONT>1 R0 X2 m/ |( t" ]: E
, X; [$ G% x( g8 H% J1 ?
</P>) E! [# [5 D: z4 ~, Q: m
<P><FONT color=#ff0000>函数名: settextstyle </FONT>. C7 M( G/ u% F7 R5 Q
功 能: 为图形输出设置当前的文本属性 9 q \+ a$ I+ c! K; S
用 法: void far settextstyle (int font, int direction, char size); 4 T3 a6 Q5 J+ Q& _
程序例: </P>$ _+ U6 J! J+ B8 }% n: [8 @
<P><FONT color=#0000ff>#include <GRAPHICS.H>; g0 s# R: ]0 A- \, r6 N/ \
#include <STDLIB.H>
2 m2 v3 B# Y8 O#include <STDIO.H>
. h* ^* `7 c8 S6 U$ i' x#include <CONIO.H></FONT></P>
* {* A+ t; m8 o% y3 @- Y. u<P><FONT color=#0000ff>/* the names of the text styles supported */ 4 e$ n. `0 n A$ K: }7 o8 S
char *fname[] = { "DEFAULT font", , ]1 [7 O( ~ z; K3 s6 K: a2 ~
"TRIPLEX font", 8 P: Z; I9 T0 P1 B ^
"SMALL font",
1 f1 I' t0 ^" i2 _; l"SANS SERIF font",
! Y. D5 U+ D; O; |"GOTHIC font"
2 h5 v8 `+ H4 a0 t; d! I7 U% [}; </FONT></P>% F3 Z8 [# _ @6 ^
<P><FONT color=#0000ff>int main(void)
) e. a( v- f4 O$ H/ D7 L- x$ |{ ! M; \9 P- P; V8 {$ H9 Q+ H$ q7 f
/* request auto detection */
4 }0 P" G+ W! P0 n% j6 F, Mint gdriver = DETECT, gmode, errorcode; 4 D' `( i* D! X. U
int style, midx, midy;
4 E# K8 C z. K- hint size = 1; </FONT></P>
e1 k* D1 x- S y<P><FONT color=#0000ff>/* initialize graphics and local variables */
$ F6 @ o: P8 ~+ k4 o% r7 @8 n% ginitgraph(&gdriver, &gmode, ""); </FONT></P>
# S" e; c$ [) X<P><FONT color=#0000ff>/* read result of initialization */ , \( c) e, f. Z2 x8 ~& l
errorcode = graphresult(); 4 ]3 z. W. c) p: [7 j
if (errorcode != grOk) /* an error occurred */
" X3 P; h& b* ?* ^3 p) Y# E{
- ^% w2 b# ? yprintf("Graphics error: %s\n", grapherrormsg(errorcode));
$ i+ ?6 } C! nprintf("Press any key to halt:");
1 k" ? d g8 Z4 S/ pgetch(); E* B8 S; c( X8 k6 C* }8 O) e/ U
exit(1); /* terminate with an error code */ ( P! z! @# m: O! S1 r0 {" J& \
} </FONT></P>
, U7 A1 p+ J7 ?+ d0 m% c<P><FONT color=#0000ff>midx = getmaxx() / 2; 5 _+ S/ D, P: d9 @1 ?6 |* R% g; E
midy = getmaxy() / 2; </FONT></P>3 [/ M5 ]9 b! }1 m/ @
<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
/ V4 m- l$ M2 c7 V<P><FONT color=#0000ff>/* loop through the available text styles */ 9 [) N u. v. f7 x: m0 D
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
4 _) N; [3 C$ c/ E! O2 _{ $ f+ h" z; m- i1 ]# U
cleardevice(); # d, u1 B+ ^2 M
if (style == TRIPLEX_FONT) ( |- t" }/ h. D. ]$ H
size = 4; </FONT></P>
\6 N; f& d% N5 v- n3 p, V<P><FONT color=#0000ff>/* select the text style */
U: ^# k l# F( |settextstyle(style, HORIZ_DIR, size); </FONT></P>' k. w, H9 c& w, D0 F5 d, _
<P><FONT color=#0000ff>/* output a message */ 8 Z9 C6 W" o2 q4 K; P! V
outtextxy(midx, midy, fname[style]);
* b6 e. D+ R& t( i- p2 `getch();
) H" y7 ~! w# I+ ~} </FONT></P>
1 } G. J o3 O1 I* ?<P><FONT color=#0000ff>/* clean up */ 9 i; u4 p, ^, f4 D
closegraph(); & k g) c$ U8 |; c/ G
return 0; ; f* ?8 r$ Z" F1 U+ L5 c. _
}
$ N. C# K/ r& p. ]9 Z6 F</FONT>
% ~( J( u8 `4 \ a! U</P>
7 [) H5 Q9 U/ ^. J+ i<P><FONT color=#ff0000>函数名: settextstyle </FONT># N4 q. v! q' m- f4 E7 |
功 能: 为图形输出设置当前的文本属性
! H' V3 J& J3 B% n3 v用 法: void far settextstyle (int font, int direction, char size); , x' b( p9 l% q5 J! i
程序例: </P>* \" c7 t M3 m' Q9 z
<P><FONT color=#0000ff>#include <GRAPHICS.H>
5 ` Z2 r, p( n9 Q#include <STDLIB.H>
3 c8 o. Z) I+ d' O0 x' M& Y#include <STDIO.H>7 M0 g2 g# J/ j; J, |% N0 z E
#include <CONIO.H></FONT></P>
3 l' A% A6 M% f' D( t<P><FONT color=#0000ff>/* the names of the text styles supported */
/ f9 l$ g$ V n! K% l1 p9 _ m- Jchar *fname[] = { "DEFAULT font", ! I) t+ N, k" w7 h9 c: [
"TRIPLEX font", . v) T3 H8 k' ~7 C& q& T2 k. c
"SMALL font", 0 B2 \5 J. X$ t f; t
"SANS SERIF font", 8 i3 E D% L& E2 ?' U# Z% \0 Z
"GOTHIC font"
* X4 ]+ P& q2 ?}; </FONT></P>
. O: ?* Y' R8 m: q7 k<P><FONT color=#0000ff>int main(void)
* ^9 J' S& p' \{ , ^* L1 o" X& q; D' {0 r
/* request auto detection */
7 u* S$ ?, Q4 B' v$ R' n9 Vint gdriver = DETECT, gmode, errorcode; 6 @6 C+ i0 O2 W- e% G& P
int style, midx, midy; 3 ^" _. E0 X# z: }) {( O
int size = 1; </FONT></P>
" ~2 [% b8 {& c2 j, f<P><FONT color=#0000ff>/* initialize graphics and local variables */ + D1 ]6 v" V( Y, e
initgraph(&gdriver, &gmode, ""); </FONT></P>0 W& R6 w5 ?- Y r
<P><FONT color=#0000ff>/* read result of initialization */ : O+ r5 s$ t* k" |7 V
errorcode = graphresult();
0 ^* X" O- [: \# Bif (errorcode != grOk) /* an error occurred */
/ ]8 |& z4 x" q- R' H% L/ x{
, |2 J1 l n# w% ?. D% W6 t, [printf("Graphics error: %s\n", grapherrormsg(errorcode));
2 a' k9 b& t9 s/ o; m% Q+ S' Jprintf("Press any key to halt:"); 9 m8 d8 W) b* _& q
getch(); 9 a {: k$ G7 w5 v `5 b) Q$ q
exit(1); /* terminate with an error code */
! I0 B+ ]( g4 R/ I4 Y' H' q$ n3 S} </FONT></P>
% Y3 _3 z" L& p0 W& V* m<P><FONT color=#0000ff>midx = getmaxx() / 2;
3 Y$ S2 q, d9 `# R. Qmidy = getmaxy() / 2; </FONT></P>
; a1 D' O2 c4 V+ Z% K<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
, |% `3 e: J5 Z+ M% D& Q9 {<P><FONT color=#0000ff>/* loop through the available text styles */
0 R/ @' W. [' G- t" U1 j2 vfor (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
. \, n! A5 k/ h+ ~+ j/ v. o3 v1 v{ ) u4 t( h$ \- C6 C0 {; o
cleardevice(); ; i7 f$ R3 H9 E S+ u
if (style == TRIPLEX_FONT)
?# o( R7 e7 m$ X: isize = 4; </FONT></P>
: o; d Q( k1 J<P><FONT color=#0000ff>/* select the text style */ ' U4 s8 R: a5 l1 U" T0 A
settextstyle(style, HORIZ_DIR, size); </FONT></P> L8 \( z, `# l9 t" L. A3 N
<P><FONT color=#0000ff>/* output a message */
& _3 x- g- G5 h4 j% aouttextxy(midx, midy, fname[style]); 3 v6 l" R2 I- e' k% e
getch();
3 ^' X. _! t' v$ d. g( L/ Z( \% p} </FONT></P>) M0 h1 @6 E# Q- k
<P><FONT color=#0000ff>/* clean up */ 6 D. V! M0 c/ |8 P
closegraph(); % b/ [4 p" q' D/ f- m0 {
return 0;
8 B M) _& v) f" N# u- p/ l} </FONT>
& ~: G8 C& q- w% q0 S
0 S( i( \2 m" l# R! p0 F5 i</P>
0 |0 \) V0 V/ c, [<P><FONT color=#ff0000>函数名: settime </FONT>
8 b$ {- y5 q+ _" E* j功 能: 设置系统时间
$ D$ p+ Q5 j* R8 O* q# P. _用 法: void settime(struct time *timep); & c1 r% E+ B( ?& J9 e
程序例: </P>
9 W& R# k7 B$ `3 n+ Y6 S2 B' K$ Q<P><FONT color=#0000ff>#include <STDIO.H>
$ h. n7 o0 ?( H N6 U#include <DOS.H></FONT></P>6 D% Q) }2 t2 G3 K( I6 b
<P><FONT color=#0000ff>int main(void)
& Y2 r _ d/ ~5 `8 G: y' t/ y{
@8 w4 q4 R3 r. y4 ~, \struct time t; </FONT></P>! i& y4 m& {4 c% n. [
<P><FONT color=#0000ff>gettime(&t);
8 p# x, O) P' A2 N {printf("The current minute is: %d\n", t.ti_min);
. i J( Y6 F1 e7 c$ Y Hprintf("The current hour is: %d\n", t.ti_hour);
# @2 o# ?! ?6 t# k$ s, d# pprintf("The current hundredth of a second is: %d\n", t.ti_hund); ) m' c( Q! c3 P, G+ |, }6 S# W
printf("The current second is: %d\n", t.ti_sec); </FONT></P>
% C5 N5 E1 E; l8 Z/ H! i9 S<P><FONT color=#0000ff>/* Add one to the minutes struct element and then call settime */
t( h4 q3 v( Y3 P1 k2 @' S0 @t.ti_min++; ' O, b$ G0 i& P* s0 m+ a* u
settime(&t); </FONT></P>, S# \( P+ F/ }7 c3 H4 I$ x
<P><FONT color=#0000ff>return 0; 6 q* z: p4 S/ W1 \! o5 Z7 H' ]) V
} </FONT>
6 U! u; I3 e# a4 J$ l; @4 T4 T1 q; |8 n$ g4 T7 P% [' e
</P>
9 D+ w0 M# O* J7 l' Q<P><FONT color=#ff0000>函数名: setusercharsize </FONT>
1 u! m7 w$ ]( r e3 l功 能: 为矢量字体改变字符宽度和高度
4 `6 J# W4 x% a% C: m. k用 法: void far setusercharsize(int multx, int dirx, int multy, int diry); 0 o( I. d3 O7 o+ ?; y
程序例: </P>
9 Q8 s# y; T: ?% a<P><FONT color=#0000ff>#include <GRAPHICS.H>
$ J/ u1 ]8 S' f#include <STDLIB.H>; {) N+ U! `2 B3 l6 \# D
#include <STDIO.H>
4 ^9 E7 l; x$ c: T# X5 x: p- ~#include <CONIO.H></FONT></P>" u5 g. ^! ]& @' k: t
<P><FONT color=#0000ff>int main(void) ; C, D; Q& K& @4 X' r. R' `
{ 7 o# P$ X u# \! _: U) X$ D
/* request autodetection */
" S4 ^2 I, h/ F/ Sint gdriver = DETECT, gmode, errorcode; </FONT></P>
6 m1 }* `' \* K; ?) j3 n<P><FONT color=#0000ff>/* initialize graphics and local variables */ 7 |9 I3 p4 T! s4 t# U$ q; @0 n
initgraph(&gdriver, &gmode, ""); </FONT></P>
2 h" P; w) u: h* g4 v! r4 M1 T<P><FONT color=#0000ff>/* read result of initialization */
7 B3 s) @- A! }5 g% J* Y% Jerrorcode = graphresult();
% ~" A k; n- A5 \( v- wif (errorcode != grOk) /* an error occurred */ 9 W% B$ J9 g4 T9 S* A P8 |; |
{ E- k) @8 L: E! K0 t: u3 t
printf("Graphics error: %s\n", grapherrormsg(errorcode)); % w4 l+ I4 C" w7 a
printf("Press any key to halt:"); - V" e& }! I% j
getch();
( P2 \- i, {# s. g8 p y w" Lexit(1); /* terminate with an error code */
0 s: S+ G. h6 t N% r} </FONT></P>
8 _! k* a: i5 n4 z7 f8 f4 K0 ~3 t; o<P><FONT color=#0000ff>/* select a text style */
! R' k! G; u$ gsettextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); </FONT></P>) {) [7 @. l( i6 Z4 o J
<P><FONT color=#0000ff>/* move to the text starting position */ ; H+ m4 t# {' L/ [
moveto(0, getmaxy() / 2); </FONT></P>2 E* T- `9 J6 E9 ]
<P><FONT color=#0000ff>/* output some normal text */ 8 i& J/ M" F9 @( q0 M" E# m$ }# D
outtext("Norm "); </FONT></P>
/ s+ h- z1 M7 l% {( m% ]<P><FONT color=#0000ff>/* make the text 1/3 the normal width */
5 `) K" F/ B* z: y# b7 b) Qsetusercharsize(1, 3, 1, 1);
0 m4 w! s/ l0 W |& V0 [; uouttext("Short "); </FONT></P>
' v: d0 G0 m4 p8 ^, V6 Z5 U( Z2 }<P><FONT color=#0000ff>/* make the text 3 times normal width */
( O8 L' r& L# b$ ~3 J) H8 Bsetusercharsize(3, 1, 1, 1);
+ p+ Z- I' j$ |" Bouttext("Wide"); </FONT></P>* q6 Z+ r$ n0 k
<P><FONT color=#0000ff>/* clean up */
$ }$ Z. [# V% q' lgetch();
( Z, L' K" c% e, I! Zclosegraph(); 9 H# N4 ?+ V9 x1 ]/ K
return 0;
# B1 b' Q% p% G- _# B} </FONT>
8 x1 p6 A: }3 ~</P>
2 B7 q) T4 O/ \) _ r<P><FONT color=#ff0000>函数名: setvbuf </FONT>/ C5 O: ^- Z' n' r. J, \
功 能: 把缓冲区与流相关
" n; j. T/ F$ H- m0 v* T4 x7 k3 x用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
h" |' @0 T* K, E* d- ^+ H7 ^- ~程序例: </P># e |9 G" I5 w% Z, V, ?! u
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>2 f! M) V- A8 Z* e9 J9 M
<P><FONT color=#0000ff>int main(void) % x2 P, Z% e6 u8 ^
{ * |6 B! C% |7 i- e# I7 R
FILE *input, *output; ' h* F0 L4 m6 W
char bufr[512]; </FONT></P>
7 v& K) E6 g& q4 w& y' ^<P><FONT color=#0000ff>input = fopen("file.in", "r+b"); ( ]; X* r8 ~' c( } s
output = fopen("file.out", "w"); </FONT></P>* m$ }+ L9 ?8 G9 K6 z
<P><FONT color=#0000ff>/* set up input stream for minimal disk access,
4 b$ S1 Z# ~, K( F6 M8 Q$ I! |8 cusing our own character buffer */ ) w* u" Q3 C |
if (setvbuf(input, bufr, _IOFBF, 512) != 0)
1 j. E ^. t- a aprintf("failed to set up buffer for input file\n");
2 G7 \+ F6 [0 ^" ~/ @$ \+ Oelse 8 o7 ~9 x; q% G3 ~9 \
printf("buffer set up for input file\n"); </FONT></P>
7 P- d' k4 j$ ^& J/ z7 |<P><FONT color=#0000ff>/* set up output stream for line buffering using space that / o* `5 o! P# e
will be obtained through an indirect call to malloc */ 7 S2 t! U5 M7 h8 w* b
if (setvbuf(output, NULL, _IOLBF, 132) != 0)
( c+ I& ~" T7 b7 Bprintf("failed to set up buffer for output file\n");
9 D; \: g1 B1 a2 delse 0 P$ ]4 B s2 h6 B( ? K& w$ s
printf("buffer set up for output file\n"); </FONT></P>
% l: I% _- f# T/ P: k<P><FONT color=#0000ff>/* perform file I/O here */ </FONT></P>- ?4 O% ^& b8 a' R# Y; m9 g
<P><FONT color=#0000ff>/* close files */ % |. x; o$ I9 x8 z( M, x
fclose(input); " a. ^% O+ S" W, ~/ {
fclose(output); $ n; o! n# K" L8 N+ x& g
return 0;
$ @7 Q$ G2 } ?! R}
( ?( V" b+ Y- }7 B7 R</FONT>& c5 W2 V2 q2 n2 n
- \: K4 G3 _2 @( D! X
</P>( R( _8 V! A: W2 J9 ^6 j$ B* G
<P><FONT color=#ff0000>函数名: setvect </FONT>0 [6 E& y3 J( N& e
功 能: 设置中断矢量入口 ! [7 Q2 V/ l H
用 法: void setvect(int intr_num, void interrupt(*isr)()); + _0 d+ s: v( i8 f
程序例: </P>
! L0 E" |1 S9 n<P><FONT color=#0000ff>/***NOTE: $ m1 r3 p% A) P! h0 N
This is an interrupt service routine. You can NOT compile this
1 Z& K% E" j; Y2 y9 G |program with Test Stack Overflow turned on and get an executable
Y* ]9 p! i; v, ?* f3 V4 vfile which will operate correctly. */ </FONT></P>
# E4 E: m A L+ {( t<P><FONT color=#0000ff>#include <STDIO.H>3 ~/ A4 ~' {8 f+ R5 M; z W3 L
#include <DOS.H>
& p& ?& Y6 u+ O#include <CONIO.H></FONT></P>9 V6 C. a- V1 y: Q& x S: O4 \
<P><FONT color=#0000ff>#define INTR 0X1C /* The clock tick interrupt */ </FONT></P>
8 q8 ]' h/ P$ g! n2 d h<P><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>) Z ^" ~; e" a/ v$ c; t) N" E
<P><FONT color=#0000ff>int count=0; </FONT></P>
( C8 @# S6 ~: I# Q<P><FONT color=#0000ff>void interrupt handler(void)
) A* h& H* r# N{ 4 @) a: p" E* d. E2 }
/* increase the global counter */
2 r: H: `; d% b. c3 tcount++; </FONT></P>! ^5 O, g t8 _" i# V4 S Z! t
<P><FONT color=#0000ff>/* call the old routine */ % a1 n" x7 C4 ?, _: H; J$ o- o
oldhandler();
. ?7 \3 c+ h* |1 v7 p$ l} </FONT></P>0 M6 M3 U5 E- j' e$ @2 W
<P><FONT color=#0000ff>int main(void)
: T6 G& ~( k/ `$ Z+ \{ 3 l0 Z. |# q! V
/* save the old interrupt vector */ % [7 s% x! ]- t$ Y; l4 J
oldhandler = getvect(INTR); </FONT></P>& q+ b0 ^7 @' S- h
<P><FONT color=#0000ff>/* install the new interrupt handler */
0 _. b3 Y' ^/ @2 Ssetvect(INTR, handler); </FONT></P>
2 e0 e' I5 ?; C* _* m( s' |<P><FONT color=#0000ff>/* loop until the counter exceeds 20 */ / A7 t( g8 S! k5 }* R
while (count < 20)
4 @2 H- j! L- S& j: h0 `printf("count is %d\n",count); </FONT></P>9 R% m/ d3 W2 H' X$ \" ?1 B" y) j
<P><FONT color=#0000ff>/* reset the old interrupt handler */
) D; c) M) t9 S1 n L6 |setvect(INTR, oldhandler); </FONT></P>) {0 o" K9 f, O/ J* Q. m* d; U9 `) `* V
<P><FONT color=#0000ff>return 0; ( B2 e7 t4 N/ o' u0 Z
}
5 \. O6 {' g! G1 n5 c# J6 [9 }5 g</FONT>
m3 d+ V' ~( f" i0 w$ `4 b</P>; n. B0 e2 \3 E& U& o4 Z. Y, N9 B
<P><FONT color=#ff0000>函数名: setverify </FONT>9 @ U! b9 a5 T1 @5 y1 }7 [$ M
功 能: 设置验证状态
$ v1 |( f6 F% b# y用 法: void setverify(int value); . m5 _0 Z0 C9 a1 Z
程序例: </P>9 w6 {7 [5 h4 h0 q0 D: t
<P><FONT color=#0000ff>#include <STDIO.H>9 @9 u- ~7 n% Y1 p2 X5 X' F
#include <CONIO.H>! a8 `4 [4 x5 n! l) z/ R6 M: A
#include <DOS.H></FONT></P>( s4 x# C. A1 K4 j- T/ H
<P><FONT color=#0000ff>int main(void)
$ O* V% V8 r6 _5 m{
. A: Q) p* n b* u0 F& y% H0 Dint verify_flag; </FONT></P>* h/ t! B, g1 N: k4 @
<P><FONT color=#0000ff>printf("Enter 0 to set verify flag off\n");
% f: Q/ G7 K; ^: u* Yprintf("Enter 1 to set verify flag on\n"); </FONT></P>6 T- ? G: v) Q V2 C# S' F8 R& O
<P><FONT color=#0000ff>verify_flag = getch() - 0; </FONT></P>
. S7 T0 F# x$ U<P><FONT color=#0000ff>setverify(verify_flag); </FONT></P>
2 J( L+ x1 D! n" ?: s( R3 b6 r<P><FONT color=#0000ff>if (getverify()) % E, W' L M; M0 z, k) }
printf("DOS verify flag is on\n"); & d9 z' S. h" J/ H& w# E
else 6 Y" j; I" v; Q8 d% a0 {6 [
printf("DOS verify flag is off\n"); </FONT></P>7 E% u4 {* S8 I
<P><FONT color=#0000ff>return 0; 9 k2 l) d- i0 X3 u# w! e
} 0 t: j* C0 v) |) T. V
<FONT color=#ff0000>
' x5 {+ z0 v# N. f ?</FONT></FONT></P>
8 T+ D$ M. F; I3 \3 { U<P><FONT color=#ff0000>函数名: setviewport </FONT>" i8 u+ P w0 e, p0 K
功 能: 为图形输出设置当前视口 5 K) h" ?% q1 _: s8 N
用 法: void far setviewport(int left, int top, int right,
0 X# B6 z! i6 w) R1 @int bottom, int clipflag);
7 g% N' P" t5 h7 J2 d: m/ G; [程序例: </P>
3 w$ x, J% O. M<P><FONT color=#0000ff>#include <GRAPHICS.H>
( R! B& |1 c0 F' Q; X( s: b* N#include <STDLIB.H>( W7 d* Z3 u2 k2 j
#include <STDIO.H>
6 Q- Y: I* f4 J#include <CONIO.H></FONT></P>% b: e `9 M! ~+ m
<P><FONT color=#0000ff>#define CLIP_ON 1 /* activates clipping in viewport */ </FONT></P>
, k" I! H' v; V, W$ z( x& O<P><FONT color=#0000ff>int main(void) 8 D& n. Q: m1 k% w
{
7 O5 ~* M) ~8 w6 A) U$ {/* request auto detection */ 5 n4 h7 m3 b& o4 U5 p
int gdriver = DETECT, gmode, errorcode; </FONT></P>
. x b, Y! N5 j' e# w, ^; w/ r<P><FONT color=#0000ff>/* initialize graphics and local variables */
$ @6 i' F8 _) _1 F. ^7 tinitgraph(&gdriver, &gmode, ""); </FONT></P>
! Y. y1 d# g2 _$ A<P><FONT color=#0000ff>/* read result of initialization */
. w: z" ^' ~1 Y- m" g2 ^0 zerrorcode = graphresult(); 3 V+ X3 }5 b, u$ y( Y4 {
if (errorcode != grOk) /* an error occurred */ 2 t8 Z* J$ W# `
{
& N) W- U' C- N( P" wprintf("Graphics error: %s\n", grapherrormsg(errorcode));
: F: U- n! `! m$ Kprintf("Press any key to halt:"); , v/ B6 A3 O w5 Z! h$ \1 O
getch(); 9 E ^5 r% m2 ?4 k! A6 h9 P0 q9 e
exit(1); /* terminate with an error code */ . ~: M2 J _8 ]2 `& f" E' _
} </FONT></P>
# _9 f1 p3 Q i" p3 ~! P<P><FONT color=#0000ff>setcolor(getmaxcolor()); </FONT></P>7 S) D/ X# E9 Z8 M: r; y. ^+ i7 O7 y
<P><FONT color=#0000ff>/* message in default full-screen viewport */
% U) ?% J) o, g w1 f, `outtextxy(0, 0, "* <-- (0, 0) in default viewport"); </FONT></P>
+ y* E; ^' {, m m6 ^' E8 R<P><FONT color=#0000ff>/* create a smaller viewport */ 4 w& Q4 j$ V- B v: H
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); </FONT></P>$ Z1 _9 g# j4 Q, b# k7 H
<P><FONT color=#0000ff>/* display some text */ 8 j' K" ~, e/ \$ p3 B
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); </FONT></P>- F% V8 ?/ N, |" w$ Z' U1 [7 y
<P><FONT color=#0000ff>/* clean up */ " K7 `( x% ^% ?" }* l# |
getch();
5 H* m/ V& e" ^! v+ S& uclosegraph(); 1 m: B. \! q/ V$ u9 Q
return 0;
) Q4 {. u0 Z/ U( z( E, j} - m3 C1 F/ s: o
</FONT>" [3 }8 o3 F; ~- m: N5 E
</P>0 x B! w+ q8 M8 U; J
<P><FONT color=#ff0000>函数名: setvisualpage </FONT>
9 i) y- F- }1 s6 V, i! T功 能: 设置可见图形页号
9 S$ `4 T# r0 E3 q& E! ]! k用 法: void far setvisualpage(int pagenum);
- ^5 i# Z1 n& b) U4 w程序例: </P>+ t" m U b2 @* A' g4 x/ z# c
<P><FONT color=#0000ff>#include <GRAPHICS.H>0 T$ \, b0 C4 d
#include <STDLIB.H>* N/ _" L" Q" o0 Z
#include <STDIO.H>4 \. F. I) d- d! M8 t) `6 C! [+ i
#include <CONIO.H></FONT></P>
5 ^* | p3 z4 c+ t# x8 ^<P><FONT color=#0000ff>int main(void)
+ F* k' M1 {: d. z, d{ . ~$ b/ T* b- q5 n* z
/* select a driver and mode that supports */
0 H+ p1 x. s7 B/* multiple pages. */
, v# k3 F. w1 K7 m- M7 r; Cint gdriver = EGA, gmode = EGAHI, errorcode; 5 I7 z* T c# l- j. W% c
int x, y, ht; </FONT></P>- z& a, u+ B0 ]! j: i
<P><FONT color=#0000ff>/* initialize graphics and local variables */
$ m4 `# B1 r7 R5 W: e/ j* q6 _/ dinitgraph(&gdriver, &gmode, ""); </FONT></P>
, v) ~) ~' _5 L& r; e<P><FONT color=#0000ff>/* read result of initialization */
( T' L0 L7 F* i" H: t6 o! merrorcode = graphresult();
! D' j, M* ~! s: ?8 @: eif (errorcode != grOk) /* an error occurred */
9 A" _/ [; m# |{ 6 m- s( `$ M/ j. V, z2 E4 Y( A
printf("Graphics error: %s\n", grapherrormsg(errorcode)); % g* k5 T( p" D% r
printf("Press any key to halt:"); + v( X6 I$ G# q3 D" Q
getch(); + q8 Z+ s* v* b# j
exit(1); /* terminate with an error code */
( n" W4 T$ g! s& v; z% k: c' Z} </FONT></P>
4 d2 t+ _* U# Y+ i<P><FONT color=#0000ff>x = getmaxx() / 2;
0 q7 p& H* h, l$ Iy = getmaxy() / 2;
# k4 v& j7 f3 N5 g# pht = textheight("W"); </FONT></P>
7 X m" ]; R; M; p% [* O<P><FONT color=#0000ff>/* select the off screen page for drawing */ ' A. x, |% I8 w2 r2 J% P; k
setactivepage(1); </FONT></P>+ A7 p) j* n8 c' {0 x
<P><FONT color=#0000ff>/* draw a line on page #1 */
9 F# \# n+ \/ a4 ~line(0, 0, getmaxx(), getmaxy()); </FONT></P>
$ A$ ?% p* D8 _7 W8 j<P><FONT color=#0000ff>/* output a message on page #1 */
" Y) S+ b# c7 U3 P( h- |settextjustify(CENTER_TEXT, CENTER_TEXT);
: Z3 Y R, j: couttextxy(x, y, "This is page #1:");
& ^; w% X: N/ {outtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
' H" v: F+ [) c9 e$ S* }; @<P><FONT color=#0000ff>/* select drawing to page #0 */
j) p( Z5 |4 P9 E! F$ W0 nsetactivepage(0); </FONT></P>" |% f4 |/ y0 D8 F9 O( F
<P><FONT color=#0000ff>/* output a message on page #0 */
" T" C$ w3 Y+ X2 louttextxy(x, y, "This is page #0."); $ C) I1 U1 e' ]; Z
outtextxy(x, y+ht, "Press any key to view page #1:");
; _5 ]& P% @2 W$ ]5 o7 rgetch(); </FONT></P>
\. Y2 \% H* C<P><FONT color=#0000ff>/* select page #1 as the visible page */ A9 R6 j& R$ ?8 n! g: S: I
setvisualpage(1); </FONT></P>; z. v7 R m. q- T8 g
<P><FONT color=#0000ff>/* clean up */
# g+ R- z% s' H9 _getch(); ! V/ R& H( Q c, `! |: I: L
closegraph();
/ U5 X) l% M# _return 0;
: t2 q( A P/ e$ B# i, g3 t/ o} </FONT>8 L) z0 Z5 h/ m a$ l3 q
1 m, F/ E1 a% j9 B
</P>& X( n- C; H4 |; Z: u/ t; s1 A
<P><FONT color=#ff0000>函数名: setwritemode </FONT>
q9 x- T2 `) R! E2 P# Q功 能: 设置图形方式下画线的输出模式
' l- H+ @! ^, U4 F3 M4 s) }, |9 |# i用 法: void far setwritemode(int mode);
+ n, p! N+ D/ a1 A程序例: </P>8 _" ?- g4 @, D$ z( [! Y
<P><FONT color=#0000ff>#include <GRAPHICS.H>
. r4 ~9 w( ]9 g3 r: _#include <STDLIB.H>2 M7 A% }$ G+ y: n' ~
#include <STDIO.H>
9 U/ n* m0 S4 H4 S$ `#include <CONIO.H></FONT></P>
% r! `& T: K! n) y" i<P><FONT color=#0000ff>int main() ; h& e( L& ^# M- _% Z7 N" Z
{
`. m) q; D) Q3 w2 Y/* request auto detection */
) k. I- D3 R9 c: Wint gdriver = DETECT, gmode, errorcode; - w F" y# T3 P2 u3 `- g$ G
int xmax, ymax; </FONT></P>( P5 k2 |0 t1 e% v U0 V1 e
<P><FONT color=#0000ff>/* initialize graphics and local variables */
7 t6 J$ g! }- D5 m; k+ Sinitgraph(&gdriver, &gmode, ""); </FONT></P>: ~% |0 Q- ~. b2 n* \
<P><FONT color=#0000ff>/* read result of initialization */ 0 p5 M8 o+ Z( \' C2 z$ V
errorcode = graphresult();
$ ?8 q" G; C6 ?9 T1 ?' _+ Uif (errorcode != grOk) /* an error occurred */ ' E3 G" _' j, O \
{
0 b* a/ K. H# v4 s/ Uprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 s5 D# t4 R9 Q7 i7 P. k
printf("Press any key to halt:"); & @ h% }9 \7 U1 i t" l( C
getch(); 0 V- B. r" g$ B" ]. R5 `- T2 @& A1 v
exit(1); /* terminate with an error code */ # Q9 U- W0 ]9 }$ D/ o
} </FONT></P>
" v8 @! M7 J4 Z0 t5 E r, j, d<P><FONT color=#0000ff>xmax = getmaxx(); 2 K, v% [2 H8 |
ymax = getmaxy(); </FONT></P>
% }! ?- V: f5 F2 I' ]<P><FONT color=#0000ff>/* select XOR drawing mode */
1 R0 r% Z8 D1 K8 nsetwritemode(XOR_PUT); </FONT></P>+ ?: E. s* l M( y
<P><FONT color=#0000ff>/* draw a line */ 1 C2 r2 I: t. b, K: S6 F
line(0, 0, xmax, ymax); * R' x* e, p( `8 S9 G" i
getch(); </FONT></P>
5 N& c3 n0 J. b1 k( o, Q<P><FONT color=#0000ff>/* erase the line by drawing over it */ % c% Y: v9 ?( `
line(0, 0, xmax, ymax);
! h F$ u, ?% U# h3 |. hgetch(); </FONT></P>
4 r. r' H, D v9 J: e<P><FONT color=#0000ff>/* select overwrite drawing mode */
" r% J( ~% h8 w5 @) q: vsetwritemode(COPY_PUT); </FONT></P>% f' L6 F2 t9 t+ c
<P><FONT color=#0000ff>/* draw a line */
' a( K7 U! N; tline(0, 0, xmax, ymax); </FONT></P>, {1 c4 M( Z) P6 Q% W" H7 ]
<P><FONT color=#0000ff>/* clean up */ ! l5 r& _* C& N0 M) Y" J
getch(); 7 m/ ?, z. e, K. Z
closegraph();
% h/ y1 r$ x9 q+ mreturn 0; 5 J# q' b# T7 H; Q
}
$ Y: m, c, G, c0 h) _</FONT>
3 B# \" b$ p G' d' y9 x</P>" W( q) O$ M6 A* J
<P><FONT color=#ff0000>函数名: signal</FONT>
+ ~# T6 a7 @$ l功 能: 设置某一信号的对应动作 8 L1 Q& z: J* I8 X1 k. W2 [" j7 F# ^
用 法: int signal(int sig, sigfun fname); 5 M8 b( i0 ]2 @
程序例: </P>
" E( k" b/ @1 I% `<P><FONT color=#0000ff>/* This example installs a signal handler routine for SIGFPE,
6 Y7 m, {3 g3 A4 \6 gcatches an integer overflow condition, makes an adjustment
, Z' U5 V( ~' w8 O: u" xto AX register, and returns. This example program MAY cause + F( k" j. K6 v5 s5 G3 w3 p0 l" B
your computer to crash, and will produce runtime errors % w4 r; C3 C" H! t
depending on which memory model is used.
' H0 x* }+ E8 I0 a*/ </FONT></P>$ Y3 k* k7 ]% B" j' m$ j0 q
<P><FONT color=#0000ff>#pragma inline ' D9 X# U/ B0 q* K
#include <STDIO.H>
/ I; L; d" X w; L7 a, }/ V; T/ _#include <SIGNAL.H></FONT></P>. f3 v7 p1 D6 b
<P><FONT color=#0000ff>void Catcher(int sig, int type, int *reglist)
9 J! ~5 i/ V1 O; F$ {* G{ ) i8 n" t3 E) U* ]9 b: p
printf("Caught it!\n");
" K) u% d( ^- s*(reglist + 8) = 3; /* make return AX = 3 */ 0 E8 t! k8 N2 e8 z& {! ?
} </FONT></P>
9 N0 x, d+ e4 U6 A$ s1 b: Y<P><FONT color=#0000ff>int main(void) . @: [2 N% ~1 K+ E3 S5 J% |
{
. `$ {, a- `8 y L1 S$ C; y+ o9 rsignal(SIGFPE, Catcher);
/ i3 C2 I3 x4 S4 [6 N4 Pasm mov ax,07FFFH /* AX = 32767 */ t- h+ U# R) b8 K1 L+ Z" |1 |
asm inc ax /* cause overflow */
; ]/ l: e# Y7 w6 z- wasm into /* activate handler */ </FONT></P>3 T+ y4 E* i' q# M
<P><FONT color=#0000ff>/* The handler set AX to 3 on return. If that hadn't happened, 1 x, Y% }+ p, |0 l$ \
there would have been another exception when the next 'into' ! v( ~4 l6 ]2 t6 B
was executed after the 'dec' instruction. */ 5 N6 l5 M$ N' U3 c' Z% l V, _ A9 Y+ s
asm dec ax /* no overflow now */
|' \! w% A$ |! P8 o. ^& Fasm into /* doesn't activate */
' h1 z( S& h/ i, qreturn 0;
4 c3 L9 O& B h, r& u! H/ }! `! ~/ z9 p} : O' Y0 ^9 L7 Q
# G8 z. q( ]* V$ T
1 w( o" o$ t ^' B4 T* M, k( ~</FONT></P>
3 P$ ]5 }& W% x8 F( F& z' n<P><FONT color=#ff0000>函数名: sin </FONT>4 r7 r2 A- W! [% c8 f& t3 r
功 能: 正弦函数 * J; D, I; f0 J7 J
用 法: double sin(double x);
) C. D. P- T* K程序例: </P>
: S* M6 w$ M3 v* w8 Z<P><FONT color=#0000ff>#include <STDIO.H>8 w- O9 l: |- a5 ?8 ?* s6 A
#include <MATH.H></FONT></P>1 `/ E% ~4 I6 t( K( u- h- R/ H
<P><FONT color=#0000ff>int main(void) , Z7 B7 H9 a& K; a. X( ~
{
( J: u6 V. O- tdouble result, x = 0.5; </FONT></P>
$ z5 V; ]+ Q2 f7 l C<P><FONT color=#0000ff>result = sin(x);
9 V! F2 ^/ Y2 Yprintf("The sin() of %lf is %lf\n", x, result); 3 t: H( t2 A2 o+ i1 e A3 o
return 0; 0 q( k5 j# G' }+ u
}
; |8 l6 F4 \3 |* q& t. d5 d
7 c W. r. M; Q+ n+ i</FONT></P>* f, b' M3 [9 V
<P><FONT color=#ff0000>函数名: sinh </FONT>
7 A. z+ f2 ?! B功 能: 双曲正弦函数 . g6 H) f8 g8 I$ C) _5 E8 F
用 法: double sinh(double x); * S! C( Z& s' R3 a& H
程序例: </P>
- X9 l' o( k9 K7 m0 e& u<P><FONT color=#0000ff>#include <STDIO.H>
/ D8 ~ L* C1 q: M" R8 I#include <MATH.H></FONT></P>
' Y! b; f' D$ e0 K! E<P><FONT color=#0000ff>int main(void) $ J& ^- b: V- T K7 B
{
- M/ z# H" v! Fdouble result, x = 0.5; </FONT></P>0 l2 k/ I( ^: R/ ^0 y2 u# i
<P><FONT color=#0000ff>result = sinh(x);
% }! ?6 s( l0 [' z* W- Yprintf("The hyperbolic sin() of %lf is %lf\n", x, result); 8 B U5 l7 U8 B/ z
return 0; ! L( c( V. B# I4 W8 W+ P9 U5 C
} 0 m8 m" d7 e# D$ J/ k& [# N
! ?# l% p$ `3 l' o
3 K/ U [2 x4 M7 R& Q) N9 H B</FONT></P>3 C8 e6 `- I$ i o5 E. Y
<P><FONT color=#ff0000>函数名: sleep </FONT>9 T+ E3 S4 L f( s o
功 能: 执行挂起一段时间
% k% T, y. \8 T3 s用 法: unsigned sleep(unsigned seconds); ! Z/ L3 f% b) f. @4 R; o; o
程序例: </P>) y" f- S y6 {1 z
<P><FONT color=#0000ff>#include <DOS.H>. z( L0 Z" B% a2 p+ q7 k5 h
#include <STDIO.H></FONT></P>
4 V' i$ c- `1 Y7 I* {<P><FONT color=#0000ff>int main(void)
. k U" u9 u1 ~3 R7 g8 {{ * W) n2 Q. P2 M: K/ r8 _" w4 X8 X
int i; </FONT></P># v/ N8 [6 e' E1 X9 k. z0 n
<P><FONT color=#0000ff>for (i=1; i<5; i++)
2 f1 |) r8 q( K, z/ q7 F5 V{
. Y: x1 N3 B; r8 ~4 \4 j6 f1 D" lprintf("Sleeping for %d seconds\n", i); ) `( z0 u* r- W4 v9 j; v
sleep(i); ( u# p. H# @% W
} ' _. B7 `/ D! _" A7 h! v
return 0; . A+ X1 `$ f" Q w7 A
}
" ]1 C4 b% E* J& j) t" I, X/ P( B& K8 a! V6 P* _# T6 E9 Y, F+ y
</FONT>) V" P* W5 w2 \ [1 ]* l
</P>
0 C1 P, g/ W$ k/ D# [1 V<P><FONT color=#ff0000>函数名: sopen </FONT> S5 Y0 C' I. N! J0 O2 y& |
功 能: 打开一共享文件 / O2 _9 D( p8 b( ~. [3 ]" h6 n0 l3 M) B( ]
用 法: int sopen(char *pathname, int access, int shflag, int permiss); + H6 s) P) s% g% D9 c. y
程序例: </P>
8 K7 H1 ^4 U6 [' v7 L- h<P><FONT color=#0000ff>#include <IO.H>
. s7 T- M- y3 I$ N#include <FCNTL.H>( \7 f0 t2 T8 {% e
#include <SYS\STAT.H>
" M% G7 m- e7 }5 y#include <PROCESS.H>
% m, E, W/ I7 Y Z9 C- N7 I. s#include <SHARE.H>
$ U+ [( f0 e# w4 f, Y# Z#include <STDIO.H></FONT></P>4 S8 |6 }6 e3 K' W3 N+ w5 T& i
<P><FONT color=#0000ff>int main(void) " L; J5 b) U" V% l' l5 p/ Z
{ : q. ^* }; K; @4 ?) q! C7 P
int handle; ! z4 i% Z5 F6 _, A2 S8 S
int status; </FONT></P>
2 f- @( ?9 W# O' Q$ m, v<P><FONT color=#0000ff>handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD); </FONT></P>! @8 q: T+ Q! c
<P><FONT color=#0000ff>if (!handle) . |8 W5 A% n) o( [" _4 u( y
{ ( m) A' ^ ^$ a7 I j) h; Z
printf("sopen failed\n"); 9 x w) K+ F" C* e0 S+ i: Z6 M
exit(1); $ k% x2 d: b- _. x; f% A3 o
} </FONT></P>
4 t" }* L1 g3 Y2 w# ]0 Z, @2 Y8 s+ X<P><FONT color=#0000ff>status = access("c:\\autoexec.bat", 6); 7 {" [' V8 z l' p0 k
if (status == 0) $ S P- I8 D4 }# W2 c1 e6 w
printf("read/write access allowed\n");
( i6 m. E* q4 _+ W/ g; eelse + J+ K0 s! d: C# I% e- `3 j
printf("read/write access not allowed\n"); </FONT></P>
6 ]- f2 P0 v- Q' v0 M3 R<P><FONT color=#0000ff>close(handle);
" |- |3 n/ y3 l1 ]. Z R+ R8 [# wreturn 0; 8 n/ b! T& Y. R$ X
} $ {0 P6 k; t2 A1 F$ d9 Q t8 o {
/ n+ c, p0 ^" R8 U: m8 K
</FONT>& L, q) h: P7 O# g3 e8 Z) K) y
</P>
: q5 Q# T2 z! @8 k; x<P><FONT color=#ff0000>函数名: sound </FONT>( h [6 @) x3 ~6 d
功 能: 以指定频率打开PC扬声器
& {5 x0 t9 _# D1 f3 j" Z7 A用 法: void sound(unsigned frequency); ) j* J" q. H7 |& [$ }% O, T
程序例: </P>% Z- }7 t$ {2 j
<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. $ u) {: @8 C# Y! d; f( W
Your PC may not be able to emit a 7-Hz tone. */ . p5 O5 ?. k/ `* t4 {7 V' u; j
#include <DOS.H></FONT></P>
( l5 P; k+ k1 k. S7 Z<P><FONT color=#0000ff>int main(void) 3 C. t- m( ?7 o- D2 t' z- T" q
{ 6 S4 R W, Y" T1 S. P7 l% L
sound(7); ; t C& h1 t+ G
delay(10000);
, x7 V \5 L. ]# a; x3 z( P9 ynosound(); ( {2 W$ ]# \& ~- R
return 0; 2 n2 n: X$ }8 v9 J' a
} </FONT>
2 E9 o2 _0 u! v- Z* y. m
4 b: [& j& A* C/ [' h2 B+ M0 {/ E% k* Z& Z8 k+ _
</P>! w+ }/ @# L* u# b- \9 ~+ O( x
<P><FONT color=#ff0000>函数名: spawnl </FONT>6 M# L& U" @" H* X' Z
功 能: 创建并运行子程序 ' G9 O$ S. }9 b5 }% @
用 法: int spawnl(int mode, char *pathname, char *arg0, 0 l% R/ [1 Q* C; K
arg1, ... argn, NULL);
9 c/ W% h q; b程序例: </P>
- A' [2 i. d& g<P><FONT color=#0000ff>#include <PROCESS.H>/ E5 @: |: j& S3 B1 L# n3 K5 g C8 [
#include <STDIO.H>( ?# u/ ?. f& b+ g$ y S
#include <CONIO.H></FONT></P>; X3 i7 B( L8 t# {
<P><FONT color=#0000ff>int main(void) % B+ Z6 L$ \' q' e* S5 }: w
{
' Y1 l: n2 J- d/ M2 Dint result; </FONT></P>
* u v; T3 }+ {" c! p" k<P><FONT color=#0000ff>clrscr(); : B8 E! @- ~6 ]
result = spawnl(P_WAIT, "tcc.exe", NULL); . c6 y j( r0 W/ N% V
if (result == -1) ; `" d5 F3 ~% m# K. K( v3 F
{
; v; _& n; e8 J! r0 a. z) ~perror("Error from spawnl"); ' i( Z& e) m! r' t' A. Q) T8 a7 e
exit(1);
5 e! Z2 l) M+ ]" @$ Z! i} % d( s* X( V, H' l
return 0;
* U) n9 u& v7 T7 q! s} $ W7 ~. n Q2 Z D' W" V( P
</FONT>' E4 u4 C# z: e. o, c
</P>2 R# W: H& S* l6 d: d
<P><FONT color=#ff0000>函数名: spawnle </FONT>' x3 b0 Y! L( O- ?
功 能: 创建并运行子程序
1 Q- W8 q. P7 r0 O- ]! v) s用 法: int spawnle(int mode, char *pathname, char *arg0, ; C4 O9 s. [$ U
arg1,..., argn, NULL); . P! |7 z$ o4 X* S4 D
程序例: </P>7 ?0 O7 ` N9 n8 J2 g5 v9 l
<P><FONT color=#0000ff>/* spawnle() example */ </FONT></P>
. c# u% b$ ~) r) X! J# \<P><FONT color=#0000ff>#include <PROCESS.H>& Q) w1 G" a2 T2 q$ w; M6 Z
#include <STDIO.H>
4 z2 n1 Y$ M. g: L5 j& P#include <CONIO.H></FONT></P>. `2 B6 m) b4 p
<P><FONT color=#0000ff>int main(void) ! \8 r, Z7 W2 _: ?4 N8 ~
{
# S$ e: N2 K. M0 w/ [- Rint result; </FONT></P>, |' z: I) b+ s, H
<P><FONT color=#0000ff>clrscr();
( T" f0 Q$ h: ?; Kresult = spawnle(P_WAIT, "tcc.exe", NULL, NULL); : |1 P: r' J( @0 R" ~' e
if (result == -1)
9 O+ T1 X+ C7 g: j: T! F5 O- R{ " X5 n P4 L# z: L5 g
perror("Error from spawnle");
$ Z8 O5 N! K0 Y7 r9 Vexit(1); $ M0 h1 h+ `0 b1 d6 A. {: j
}
$ e, p4 z, ?4 Q7 f3 Zreturn 0;
p9 o# R! h/ Y4 } g$ `# W} 3 A7 ?: ~; m, O4 ^& }! v7 a: o
. @: Z$ C4 {) n4 ?3 p! R0 W, r b& ~ M/ q" T
</FONT></P>+ E* c5 C; M% |5 C; N) e
<P><FONT color=#ff0000>函数名: sprintf </FONT> R6 V) Z6 W9 s2 K" I0 |8 B
功 能: 送格式化输出到字符串中 , x& d% k' \) w+ i0 f1 @& i! {& N
用 法: int sprintf(char *string, char *farmat [,argument,...]);
7 u+ G `, x3 I" ?2 b8 t程序例: </P>; H& f# C6 R) L2 X1 A
<P><FONT color=#0000ff>#include <STDIO.H>
% M" q8 B, v& b0 u#include <MATH.H></FONT></P>
6 O# R! G# ~& m% X* D<P><FONT color=#0000ff>int main(void) % K0 s5 @8 _5 d( w8 i5 K
{ 4 ~! I' _; n E
char buffer[80]; </FONT></P>
0 I1 }, \9 }6 a, L9 G<P><FONT color=#0000ff>sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
! L2 A, t: _* o' T8 Zputs(buffer); 5 ^! S/ w7 S( }: g! y
return 0;
) z5 M" |1 k8 Z$ _( R7 x}
. M' b* a; j8 n4 Z+ G0 h</FONT>. G7 Z9 ^: R3 P! E A
</P>$ _) i3 b$ O4 d2 B" ?7 ^6 ^: _
<P><FONT color=#ff0000>函数名: sqrt </FONT>
0 T9 Y0 T2 I$ I+ u% v# q功 能: 计算平方根
( n& {% {9 E( F用 法: double sqrt(double x); ( p6 w! P' o9 ]7 B" S
程序例: </P>. t h3 D9 [+ o3 e: D% u+ n
<P><FONT color=#0000ff>#include <MATH.H>
2 @: j5 w6 R- I( _: M( S4 s#include <STDIO.H></FONT></P>
& { y" Z' \7 q0 y f<P><FONT color=#0000ff>int main(void)
@+ D. k. t. z; L; J [ T0 W{
0 [0 O! e# P$ p/ i' Mdouble x = 4.0, result; </FONT></P>
3 D, i) f$ Z9 o; |" _4 U. `* w<P><FONT color=#0000ff>result = sqrt(x);
, V3 T3 i/ V- u8 r' W* \+ t( X: r. ]printf("The square root of %lf is %lf\n", x, result); 4 }& }7 T1 X9 c! V6 x6 ~
return 0;
# f) q, [5 c$ q# }' z} 4 X+ ~9 n9 o* C0 I' H; A
</FONT></P>" H0 n) @( d+ S2 R" ^
<P><FONT color=#ff0000>函数名: srand </FONT>
& A ]5 q2 m8 G9 j* B功 能: 初始化随机数发生器 $ X- p" T2 O$ m) z2 ` Q3 @ j: h, U
用 法: void srand(unsigned seed); / W/ }4 ?" `/ k
程序例: </P>' R7 b( M& t2 k) o- M- C
<P><FONT color=#0000ff>#include <STDLIB.H>
8 c) ?4 n' Q9 x( M% @9 v* T#include <STDIO.H>
' a6 B) F& Y- A. e0 f#include <TIME.H></FONT></P> L9 v" T2 d; O8 g, p9 I
<P><FONT color=#0000ff>int main(void)
+ K m- f2 u/ Q8 j{ * G) B( d8 P1 Y0 w& U8 E7 Q6 O
int i;
- u4 H1 Q8 L2 e2 H9 T% Ttime_t t; </FONT></P>
3 C+ \2 M( r' }9 k) s( b. S<P><FONT color=#0000ff>srand((unsigned) time(&t));
* s8 S& S" ?0 S! D; Xprintf("Ten random numbers from 0 to 99\n\n"); 7 o+ b W$ W5 E0 c" X: g* `
for(i=0; i<10; i++) ! A, ~! L+ G% }2 x3 M9 F9 Z
printf("%d\n", rand() % 100); ' N& v# B' }4 c0 m" f( e" i
return 0; / }& T1 X4 x ]9 L- \/ y
} ) O4 Z2 u+ o7 s$ w& X5 I C
</FONT>$ v" {" R5 X% @ S$ K" r
</P>
7 W8 B% {, v% o# V; C# N/ O<P><FONT color=#ff0000>函数名: sscanf </FONT>- D9 }! t1 [3 {8 s. R$ w. N) z
功 能: 执行从字符串中的格式化输入 8 G* P% e7 N! P: P0 D0 [& w
用 法: int sscanf(char *string, char *format[,argument,...]); / `& m- E' [. Y m
程序例: </P>9 w2 {! ?" q( s9 [/ ^) c
<P><FONT color=#0000ff>#include <STDIO.H>
- i6 T5 b' N' y- N/ c G#include <CONIO.H></FONT></P>! \$ v: o$ b+ I% U4 E2 X
<P><FONT color=#0000ff>int main(void) 9 A- t0 ?. {7 z
{ 6 Q2 |6 i6 {' v! d1 i
char label[20]; % B: I8 s9 v& |/ ~5 O
char name[20]; 6 |8 V$ }* s1 Q4 d6 S$ O' p2 m
int entries = 0;
: f2 O3 U3 X' y7 Z; M# |8 _& Qint loop, age; # o4 w8 u) H8 R1 U) V
double salary; </FONT></P>
; r4 L; P0 p9 Z8 X6 n7 [<P><FONT color=#0000ff>struct Entry_struct
, u0 ?- z: I: k8 k{ 6 k" T! y$ u- N' k6 e6 w3 q
char name[20]; # K. K- b9 y# a6 `/ g: O5 p& M
int age;
. K& C8 Q1 U* q8 b, Cfloat salary;
+ B+ D8 S V( l- D; Z. _9 } I} entry[20]; </FONT></P>! a8 U; ` o+ j, f# b3 i
<P><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */ . m6 z. K: j8 h/ _2 ?
printf("\n\nPlease enter a label for the chart: "); 7 |* U+ X9 a8 {4 g. ]
scanf("%20s", label); ; n9 h' R: |; u2 d8 ^
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
U3 O$ o& q4 p+ A3 |) G X<P><FONT color=#0000ff>/* Input number of entries as an integer */
2 k) p6 K/ Q. C/ d( rprintf("How many entries will there be? (less than 20) "); 5 H! y" s, e0 Y/ z; s2 Y
scanf("%d", &entries);
W2 {+ g1 ~: Pfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
3 ^" X7 O" n9 B; H; C<P><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ + E f, T+ Y4 Y. d8 Q7 `& O
for (loop=0;loop<ENTRIES;++LOOP) % t4 {. @+ j0 F; f2 Z @) I) N
{ 0 t; G, `& |1 \5 |- Z
printf("Entry %d\n", loop); 2 h) z- M: H4 l/ d5 D* ?
printf(" Name : ");
3 A. n6 @9 p2 M) n0 }) @scanf("%[A-Za-z]", entry[loop].name);
0 [3 t: F B3 h. ` I _" _4 hfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>$ V( K4 J+ s0 X* K; ]
<P><FONT color=#0000ff>/* input an age as an integer */
6 b' Q6 F# k- b+ o# R" {, kprintf(" Age : ");
& ?1 K9 N a1 ~scanf("%d", &entry[loop].age); & Y. m! ^. g, o* @
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>/ m& \% G- j, I% E
<P><FONT color=#0000ff>/* input a salary as a float */ 1 \: O! ^/ q7 H O( r$ P0 V
printf(" Salary : ");
. \* S/ U6 V; M0 ~3 J2 \scanf("%f", &entry[loop].salary);
8 I. m+ E' V' z) E3 ?fflush(stdin); /* flush the input stream in case of bad input */
- n( P" _: |% s& S1 N P1 G} </FONT></P>
4 @3 M( M1 C; E<P><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */
% `% P: e1 Z8 Fprintf("\nPlease enter your name, age and salary\n"); ( [( S) I4 j* A+ _0 C' i( M3 A
scanf("%20s %d %lf", name, &age, &salary); 8 y8 }" { L# w% h6 e0 T
</FONT></P>
8 Y( z/ t y- P. w<P><FONT color=#0000ff>/* Print out the data that was input */ + V( d a r) D! |
printf("\n\nTable %s\n",label); 5 Q! e U7 k6 h* A/ p
printf("Compiled by %s age %d $%15.2lf\n", name, age, salary); ) {; J( i1 p( M2 C" P
printf("-----------------------------------------------------\n"); 1 P! Z, m! S- f& N
for (loop=0;loop<ENTRIES;++LOOP) " C3 h. b0 C# q+ M, J0 r
printf("%4d | %-20s | %5d | %15.2lf\n", % a% U8 D0 y. R6 `( V5 g' N$ x
loop + 1, 0 o7 O N3 c* g" ]& _) e+ G; S4 |
entry[loop].name, - O9 \3 _$ C, [; k G& [5 x6 i
entry[loop].age,
% Q2 y% y5 Q7 n. Zentry[loop].salary); + [5 I4 A$ }, q( i7 w9 {( I# O5 A
printf("-----------------------------------------------------\n");
5 `8 R- T1 S& \return 0;
4 B" x8 w: M* g( ~} 3 q g+ Q; g8 x/ N1 c6 m/ S' C
' R5 H1 Y" L' Y# U</FONT></P>
- R' f' C1 u. ^<P><FONT color=#ff0000>函数名: stat </FONT>; K0 ]& V, H' _* {
功 能: 读取打开文件信息
- f; o+ L9 y A7 c$ h用 法: int stat(char *pathname, struct stat *buff);
/ w: I7 ^) j: J程序例: </P>/ K k+ U; T4 ]8 _
<P><FONT color=#0000ff>#include <SYS\STAT.H>
- K: ~5 r; ~( D; n#include <STDIO.H>4 G, w. ]1 }! L; a
#include <TIME.H></FONT></P>
5 j" U! T5 J0 l* ?0 i5 N* M ?/ w<P><FONT color=#0000ff>#define FILENAME "TEST.$$$" </FONT></P>: ~, [* N$ @9 l+ u% B) Y
<P><FONT color=#0000ff>int main(void) & l* \) d3 q5 p2 d4 t. h
{
" b. T$ k% E F6 d+ lstruct stat statbuf; & _% B" y) B* _! k$ X; G
FILE *stream; </FONT></P>
2 L& d, [; d9 D8 G6 R<P><FONT color=#0000ff>/* open a file for update */
1 q; _6 p$ E; [2 e2 x. pif ((stream = fopen(FILENAME, "w+")) == NULL)
# D# Y7 D p. X0 g2 W; u! v7 I{ 4 x* S( F% T d* P" u
fprintf(stderr, "Cannot open output file.\n");
9 V* q q1 |: Y8 f, [ ?return(1);
: H5 ^# q9 ]+ z0 U& ]3 M} </FONT></P>+ c0 D4 A4 M5 ^4 @6 K
<P><FONT color=#0000ff>/* get information about the file */ , F ]' e/ \/ Z- u2 @
stat(FILENAME, &statbuf); </FONT></P>
. ]- H$ [' N4 V, Y/ B! [<P><FONT color=#0000ff>fclose(stream); </FONT></P>1 f$ I( B9 \" H: K
<P><FONT color=#0000ff>/* display the information returned */
; g3 p! U9 h% u" U: o9 Wif (statbuf.st_mode & S_IFCHR)
% b! G% e9 r( r8 _7 _printf("Handle refers to a device.\n"); : v2 w: t; a+ ^9 j
if (statbuf.st_mode & S_IFREG)
2 C# `9 c' x( |/ e% Y# Uprintf("Handle refers to an ordinary file.\n"); + A5 D- s/ q- H6 n. F
if (statbuf.st_mode & S_IREAD)
# S8 @6 {4 G! ]7 R. H9 [9 N) W4 \, E0 wprintf("User has read permission on file.\n");
8 v G) A3 c" c" Aif (statbuf.st_mode & S_IWRITE)
7 y M, z6 v) T0 t2 j# M& L; O' Gprintf("User has write permission on file.\n"); </FONT></P>
/ \; K& O+ S' D2 d<P><FONT color=#0000ff>printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
9 G& t! [5 s8 t3 Yprintf("Size of file in bytes: %ld\n", statbuf.st_size);
3 N) X: d/ R% ?) h2 G# u; H7 Tprintf("Time file last opened: %s\n", ctime(&statbuf.st_ctime)); * R. `3 k+ j( Q. q3 {
return 0; 5 Y0 G' G O4 y3 g
} 1 c. @0 f9 i+ [0 @$ C. O
5 g% q' Y5 ~) c1 f9 E& ]- l
</FONT>
8 _! s8 b$ z5 q' O5 @: K</P>
2 i2 E( H/ Y3 }+ ?" [' S<P><FONT color=#ff0000>函数名: _status87 </FONT>; w! q- Q7 O- a
功 能: 取浮点状态 / {8 j; k3 y/ { a$ p: `; {1 H- u6 k
用 法: unsigned int _status87(void); 4 `, u n, G' F2 z6 N
程序例: </P>/ i7 J+ r' Q5 W9 M- H4 A
<P><FONT color=#0000ff>#include <STDIO.H>
- `" R1 \2 y" }- L: X8 @& J#include <FLOAT.H></FONT></P>
( I/ t S' N/ q B2 N" E9 b- w<P><FONT color=#0000ff>int main(void) * {8 @1 z. `5 w1 k: w
{ & F6 s0 m0 r7 F5 x
float x; . V+ v2 W. c' |! X' h9 f# e
double y = 1.5e-100; </FONT></P>
" F. e' I# r: K& q+ X$ V# x# j+ F+ `<P><FONT color=#0000ff>printf("Status 87 before error: %x\n", _status87()); </FONT></P>1 [ b2 e, i/ y5 J# [, r: X- A
<P><FONT color=#0000ff>x = y; /* <-- force an error to occur */
( ^+ v* }" f$ `# \y = x; </FONT></P>
5 p- J( o( `' j8 v7 T8 V<P><FONT color=#0000ff>printf("Status 87 after error : %x\n", _status87());
* N! e% ^2 J6 p+ V3 Z$ Wreturn 0; # _: l5 _0 ~2 u' b
} 5 x$ r& O, p& i) j" a9 n: g
</FONT>
; t+ y# O e1 e) ?5 D( P</P>
- u9 ^3 I: J$ [9 ^$ J# Z% U<P><FONT color=#ff0000>函数名: stime </FONT> n) @9 ?8 x9 H- I- j. Y6 _: E
功 能: 设置时间 3 B$ V- H: ^# r
用 法: int stime(long *tp);
$ K2 p3 K# X: h) _程序例: </P>5 Y: @. b" {3 Z5 V3 M- x4 F
<P><FONT color=#0000ff>#include <STDIO.H>
0 s/ m0 K, s: t#include <TIME.H>
S5 e* I2 B8 l D7 |+ i#include <DOS.H></FONT></P>& I: Y9 b$ O7 J3 C
<P><FONT color=#0000ff>int main(void) 7 r. [) w# I% a. @2 |4 V- m' ~6 U
{
8 n9 u5 c ?% |time_t t;
. y0 y2 j$ _+ b0 C; dstruct tm *area; </FONT></P>% ] N- k& i; A4 r k# J
<P><FONT color=#0000ff>t = time(NULL); ! Z) K8 ~- r: L
area = localtime(&t); , i% J+ }" u+ j% V( h) c2 J
printf("Number of seconds since 1/1/1970 is: %ld\n", t); " y6 i( D; A( D6 r& N- i" ~6 m2 l
printf("Local time is: %s", asctime(area)); </FONT></P>
+ U! m5 |3 w( q9 T+ L<P><FONT color=#0000ff>t++; 8 z( t* p7 V0 A! w/ m" W9 H
area = localtime(&t);
: ^ X$ x3 I2 F3 T1 xprintf("Add a second: %s", asctime(area)); </FONT></P>. n7 c" P7 a3 Z! |
<P><FONT color=#0000ff>t += 60;
+ s* h0 u5 I: L$ E$ Farea = localtime(&t); ( {9 {+ V% H5 U, ~$ m
printf("Add a minute: %s", asctime(area)); </FONT></P>
( b1 S* F0 z8 J- d6 i, M<P><FONT color=#0000ff>t += 3600;
- m7 t; e# k7 Parea = localtime(&t);
& l7 I- W4 {- W& Z/ ~printf("Add an hour: %s", asctime(area)); </FONT></P>9 c& a% b e, ?7 f6 y1 ^
<P><FONT color=#0000ff>t += 86400L; 7 d$ @0 w1 i0 j
area = localtime(&t); + U! g* j% V. Y$ u, W2 J, U- L R
printf("Add a day: %s", asctime(area)); </FONT></P>6 _2 d1 z% t. ?0 @; t! e
<P><FONT color=#0000ff>t += 2592000L; ; w) G4 h; M: r0 ~- B* r
area = localtime(&t);
2 G( ]# z ]0 Q% O7 B) dprintf("Add a month: %s", asctime(area)); </FONT></P># l! j+ y9 l# h
<P><FONT color=#0000ff>t += 31536000L;
1 A$ P6 \# l" p0 _area = localtime(&t); * Q3 x" p; R0 {# v6 w3 o
printf("Add a year: %s", asctime(area));
{& v# C6 s! T0 j0 T5 Ireturn 0;
+ |( Y. J$ v' h} </FONT>
9 A7 i+ U) l y4 D) J: x5 U o' S' e4 V r8 h) f) X3 A
7 b# Q- [3 ?' A# I7 h; Z8 n) c, D
</P>
; e; J; N# k# Q/ v. |<P><FONT color=#ff0000>函数名: stpcpy </FONT>
! F+ O; M+ q q9 T0 q+ n功 能: 拷贝一个字符串到另一个 1 m2 K# x- I/ \- P" V
用 法: char *stpcpy(char *destin, char *source); 8 J R: _/ ]0 l4 X4 l
程序例: </P>
" f' m6 e3 J- h<P><FONT color=#0000ff>#include <STDIO.H>; c0 ]. X8 n2 ]3 W8 T- ]
#include <STRING.H></FONT></P>
3 c' f5 y7 N4 p( t* ]9 A<P><FONT color=#0000ff>int main(void)
3 E1 L1 X% W# V. H% {5 c2 r# K{
" a4 M6 n2 P- a6 A) X2 L/ y0 vchar string[10]; 7 l: B; Q4 R( m/ `
char *str1 = "abcdefghi"; </FONT></P>
/ t, T7 ~2 v+ ~1 t3 B<P><FONT color=#0000ff>stpcpy(string, str1); 2 P6 z1 y. W! W
printf("%s\n", string); 0 `: c" F2 G8 o
return 0; 6 _+ h2 ~( J5 Q0 p% l( b1 U# N
} % c8 r6 ^, \0 s- }: x
s5 N$ ?, M+ v9 C! [& B</FONT>
: V6 W" L0 e+ R6 k</P>
! T& o9 f: B7 e" x7 M! n<P><FONT color=#ff0000>函数名: strcat </FONT>
1 w/ K- m7 k/ y( d( M* D功 能: 字符串拼接函数
) o$ f, P3 i; V用 法: char *strcat(char *destin, char *source); & h. c1 T% V1 w7 {2 [5 c
程序例: </P>" ^) m. A0 C8 h8 M K. d8 e
<P><FONT color=#0000ff>#include <STRING.H>
( G8 d/ y# N+ s#include <STDIO.H></FONT></P>( @0 i0 y% p; T4 y
<P><FONT color=#0000ff>int main(void) / t4 F8 N" @ [
{
5 j& ^& o/ K6 B2 hchar destination[25]; : R! a7 t1 T- y$ M0 K
char *blank = " ", *c = "C++", *Borland = "Borland"; </FONT></P>
j8 i l: T; n" T+ U<P><FONT color=#0000ff>strcpy(destination, Borland);
" z0 p/ a; J3 t3 N! Z3 Xstrcat(destination, blank);
7 E' h6 Y ~- k3 k, k# ^/ w* _strcat(destination, c); </FONT></P>
2 \4 W d0 A3 g1 Z, E) @<P><FONT color=#0000ff>printf("%s\n", destination); ( X9 i" D& w" ?7 X
return 0;
+ T3 j0 G1 N; j6 k T! V}
; \( y2 t$ V- H4 V
# M% M4 M0 s1 W3 b0 c</FONT><FONT color=#ff0000>
& j0 ^' [1 l3 ?1 D" l6 T' v( u</FONT></P>
6 s8 m+ M1 K6 `2 K& ~$ t4 F2 L<P><FONT color=#ff0000>函数名: strchr </FONT>
s: y; u, t" d3 C5 \功 能: 在一个串中查找给定字符的第一个匹配之处\
6 `+ u$ t/ x2 k5 J% A用 法: char *strchr(char *str, char c); 4 X& H* P0 h3 d
程序例: </P>
. O7 W% X3 T/ [1 p/ o<P><FONT color=#0000ff>#include <STRING.H>
; f% t) \: S" E6 ?2 o#include <STDIO.H></FONT></P>
$ w% s5 Z9 V s4 ]<P><FONT color=#0000ff>int main(void)
% m# J N: G9 a% K{ 3 F- _3 m7 m t% N4 u
char string[15]; * J* {7 c4 \4 |" c8 w( X4 t
char *ptr, c = 'r'; </FONT></P>
+ s3 G* s8 n. O- @( T<P><FONT color=#0000ff>strcpy(string, "This is a string"); ; m+ T6 S2 r' T U( ~
ptr = strchr(string, c);
* ]: x4 I0 C* H5 eif (ptr)
5 C* q6 k$ ]/ h5 {0 o/ Rprintf("The character %c is at position: %d\n", c, ptr-string); 4 C2 L$ g& l. [
else ( p3 h- O5 B6 S
printf("The character was not found\n"); ' M% B9 T( ^; V9 [
return 0; " Q, g) z* H4 f
}
% W/ ]4 C# l* U, {</FONT>
& I2 L& I1 G2 R; n3 ?' V0 I
2 f& K! N" x) K8 M: z; a</P>% |6 M- b2 i$ P) E9 @# S3 a5 k
<P><FONT color=#ff0000>函数名: strcmp</FONT>
5 w" u# g* a6 k4 N; w功 能: 串比较 & S5 C* L: [7 H7 _* ?0 r
用 法: int strcmp(char *str1, char *str2);
& l$ @; B s* t程序例: </P>' e9 R4 F9 B& S# K
<P><FONT color=#0000ff>#include <STRING.H>! m U0 n5 c$ y8 o! d
#include <STDIO.H></FONT></P>
% N4 w$ |3 \9 ` d' A<P><FONT color=#0000ff>int main(void)
! G8 ^" `' N$ Z{
" X1 K6 ]+ [/ O- O7 Nchar *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
+ ?2 `4 u2 g# ?6 \$ C! ]int ptr; </FONT></P>
- z. U1 e, m) {<P><FONT color=#0000ff>ptr = strcmp(buf2, buf1); ) X. R/ _ h1 a1 {! r
if (ptr > 0)
) U+ Y* V m8 Q+ Yprintf("buffer 2 is greater than buffer 1\n"); ; J, v* s) l5 n, J
else
+ S8 l! N1 h5 u, Uprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
# w' _7 [. S+ u/ `- s+ q<P><FONT color=#0000ff>ptr = strcmp(buf2, buf3);
) J* x8 ?- l+ W+ a' V9 cif (ptr > 0)
% @- }/ j) m% U% Y" z4 x* g* C# _& k1 ?printf("buffer 2 is greater than buffer 3\n");
' P2 B# z+ W( uelse 3 K, [) Y+ C, e; L- J* L/ O! x4 t
printf("buffer 2 is less than buffer 3\n"); </FONT></P>
6 `' K4 L$ {! ^0 ~* c% X' y<P><FONT color=#0000ff>return 0; # e2 z8 t: @7 ^( c$ h: n% @; H6 I! d
}
' [/ T! ]2 b( C9 d# e3 S
2 p; A) J: `% Q0 p! T8 n0 f; G7 R2 o# J
</FONT></P>
4 _: Z0 V/ `! P$ ] B3 M<P><FONT color=#ff0000>函数名: strncmpi </FONT>% i: B S2 }' I) A
功 能: 将一个串中的一部分与另一个串比较, 不管大小写 % _! I9 \6 S- I/ g2 F
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen); ) [, n! r8 E" G) c( J- |
程序例: </P>1 _5 Z/ @: I. v7 x j6 D4 D1 T, z
<P><FONT color=#0000ff>#include <STRING.H>. [' K+ I, F [ s+ g$ ]' V$ t1 B
#include <STDIO.H></FONT></P>' }. \# ~+ D7 r$ T. a1 e
<P><FONT color=#0000ff>int main(void)
% I) v0 b" `; X2 I& @* y! y{ $ i8 r* i* w$ e( A2 E% }
char *buf1 = "BBB", *buf2 = "bbb";
" ?( V* x: N- g: f5 o! D; C5 _int ptr; </FONT></P>
0 C2 k4 G% Z/ m1 C" s9 ^<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>; c7 O6 l6 L% e$ W. B8 C! ^ S6 T# i
<P><FONT color=#0000ff>if (ptr > 0) ( P, V5 C+ r" u( S% X7 O
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
1 ]5 ?8 a- X5 n- O<P><FONT color=#0000ff>if (ptr < 0)
7 P9 T* |% Y& q1 Uprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
6 @, `+ T4 a% m& _$ M<P><FONT color=#0000ff>if (ptr == 0)
0 [3 Q0 c. [5 {9 R# a! z: b! s+ iprintf("buffer 2 equals buffer 1\n"); </FONT></P>
+ U* I: B1 L6 K6 K2 G, M<P><FONT color=#0000ff>return 0; $ ~% f; y3 [- ^: O3 L+ _
} $ @" [1 U/ Q) h4 o: f; x
, F6 _- I# ?, J' k8 p
</FONT>1 G/ w4 R1 a6 `8 q% f! x! Y$ N
</P>
: U4 z6 }) Y! `$ s0 z6 t; y/ A/ h2 q( C<P><FONT color=#ff0000>函数名: strcpy </FONT>
& k% p- J! S3 Z功 能: 串拷贝
" J$ E+ ~1 i% r% _4 V- ]用 法: char *strcpy(char *str1, char *str2); $ Q. ~7 y6 L3 t5 o
程序例: </P>* m* j& ]% @4 \9 ^" A
<P><FONT color=#0000ff>#include <STDIO.H>: ~" U. q+ |0 t* U
#include <STRING.H></FONT></P>
& y; |; j" e* Q t7 X<P><FONT color=#0000ff>int main(void) 8 ]! Z. g8 {( _: k, K, f5 G
{ * [, [3 T. b% \# ?
char string[10];
# [ e) K; t. n) dchar *str1 = "abcdefghi"; </FONT></P>2 O7 E# Z6 p' ]: C! G
<P><FONT color=#0000ff>strcpy(string, str1); , z1 h4 V* F" F+ x1 o2 H2 b) K+ ]
printf("%s\n", string); # u" ?0 v- W( ^" D$ h( C0 M2 }
return 0;
: m8 a) a$ k& }8 a) o3 b}
6 _, X4 Q m/ z, y</FONT>; p7 N* @' D" m0 z+ Q! j
; ~1 \9 k" ?' A# ]
</P>
; T) S$ o H( T<P><FONT color=#ff0000>函数名: strcspn </FONT>5 [4 Q+ d4 F. g- _$ w
功 能: 在串中查找第一个给定字符集内容的段 ( c" l) N, m; y$ B& J3 T8 @
用 法: int strcspn(char *str1, char *str2); 6 H! z: X( [3 w1 h1 E; Z
程序例: </P>
- I8 G0 @7 c7 ]! K<P><FONT color=#0000ff>#include <STDIO.H>) v: G( E1 v8 g. u' V0 e
#include <STRING.H>2 j. D7 F* n8 ^8 J( c0 G
#include <ALLOC.H></FONT></P>
9 s; ^' f1 L f% Z; @+ A4 j. `5 t<P><FONT color=#0000ff>int main(void)
$ j+ H) a6 A5 [& w6 [) r w5 K! S9 R{
7 F6 X) f4 |# ychar *string1 = "1234567890"; ! Z8 h( P9 R7 t* T( V$ `
char *string2 = "747DC8";
+ G# a' y+ P" p' Zint length; </FONT></P>
- |. T- @* I1 x2 t<P><FONT color=#0000ff>length = strcspn(string1, string2); + S/ J" k. O& }2 p. _ F2 D
printf("Character where strings intersect is at position %d\n", length); </FONT></P>
, v9 U4 d6 {* q6 |( ^* X5 w2 m# {<P><FONT color=#0000ff>return 0; & G. s) r0 N5 b# k
}
0 X3 k4 T& q3 l7 l( c' U1 A</FONT>( I5 y+ n$ X1 ?: U) h6 X
; k. y9 b. k- T9 y3 l5 j. w
</P>
N4 x' r- O2 C, x# v" ?, ~<P><FONT color=#ff0000>函数名: strdup </FONT>
, l, [5 }$ l2 S* ?# F& R功 能: 将串拷贝到新建的位置处
: @) {( G# y; I I& Q2 i& `用 法: char *strdup(char *str);
5 u0 m0 R3 n$ C! m( U4 s I5 B程序例: </P> a5 D8 [7 k! W6 x# u0 d
<P><FONT color=#0000ff>#include <STDIO.H>) A G& Q3 [2 i! n3 _8 R# W: k3 m
#include <STRING.H>
) |! Y" P @* W. v4 S# }! t#include <ALLOC.H></FONT></P>1 m5 I+ J- Y$ J* g
<P><FONT color=#0000ff>int main(void)
$ N! \+ A) w; E% `& I{
( p& A: v4 _7 G, L1 i. k0 ]char *dup_str, *string = "abcde"; </FONT></P>- }2 ?2 U4 t% ` T" w
<P><FONT color=#0000ff>dup_str = strdup(string);
" d8 V; x! g2 o# I- K. hprintf("%s\n", dup_str); ; _6 r6 V% O3 m( ]0 V* R9 x
free(dup_str); </FONT></P>
. b1 r& B! l( r" J% w) q9 D<P><FONT color=#0000ff>return 0;
+ _% v, ~, Q2 H! h& i! ~( k" i# m0 k}
# ~) i- j |! C1 B( @, E P' w. p
</FONT>
' P; u& V6 f! g1 x</P>
1 b' p n% M5 |; @$ F/ C# K<P><FONT color=#ff0000>函数名: stricmp </FONT>
! n# b$ Z1 \/ p. [9 z3 u功 能: 以大小写不敏感方式比较两个串 $ u6 V$ `; E; a m* i7 [; d5 Y4 F9 o
用 法: int stricmp(char *str1, char *str2);
* n, ~, o8 ]8 T O$ c程序例: </P>/ [+ L6 V7 x, c+ r" p# B
<P><FONT color=#0000ff>#include <STRING.H>
( c4 [' |4 E( E& a#include <STDIO.H></FONT></P>
/ u' H* v' @( V, k<P><FONT color=#0000ff>int main(void) 5 S1 _- h; M( B8 p" B
{
8 y" ]$ v* p1 i5 k2 Nchar *buf1 = "BBB", *buf2 = "bbb"; 9 h) T6 i1 K- \
int ptr; </FONT></P>+ e2 f7 O; \' s3 E% T
<P><FONT color=#0000ff>ptr = stricmp(buf2, buf1); </FONT></P>
5 G Z6 G, c4 B; r: \<P><FONT color=#0000ff>if (ptr > 0)
. w7 X+ P$ a1 ?3 S0 oprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>3 R2 D, l: V$ Z4 P. n+ J* X9 J
<P><FONT color=#0000ff>if (ptr < 0)
7 l' m O8 P0 P( G& Nprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
G/ K( m( ?" J7 n, n* b4 a5 O<P><FONT color=#0000ff>if (ptr == 0) ) o+ {4 N) L W x- u( C& u
printf("buffer 2 equals buffer 1\n"); </FONT></P>
+ B' L# v8 m" y9 n* j$ r2 m<P><FONT color=#0000ff>return 0;
% f H' F6 D- e} 2 \. Z* X0 z& K1 y5 E
</FONT>' W7 Q& ?3 k. V& ~ d) K, ?
</P>
" p: Y+ p+ N3 c3 e) E; P9 W- J<P><FONT color=#ff0000>函数名: strerror </FONT>
9 d+ t4 s8 M: q& Y/ r$ D功 能: 返回指向错误信息字符串的指针
1 E. w0 h6 \ G. |2 n- w% S$ J% d3 k用 法: char *strerror(int errnum);
. [/ P- ~3 K8 W/ Q程序例: </P>; |0 w' ^* F- w% C
<P><FONT color=#0000ff>#include <STDIO.H>
- q8 a+ G8 k' {$ x) F#include <ERRNO.H></FONT></P>
0 M4 x: J- E, K0 P<P><FONT color=#0000ff>int main(void)
7 C- e6 i% @ g3 F" m{
8 d) l) g8 W' N: M; B9 A$ K! Mchar *buffer; M# t( ^2 s7 W' Q) N7 J; t* v0 r
buffer = strerror(errno); ! g& u* a9 h/ e) Q) q
printf("Error: %s\n", buffer); 4 H. V+ E6 E1 o/ H5 W: U M, R% g
return 0;
\+ n0 w$ H4 A. Z1 A+ i; w} & K3 W- [" h; M" |% R
5 ?* k7 R N) w
</FONT>" S7 n+ z0 R5 x+ c4 L
</P>
0 _0 _( ], t8 u<P><FONT color=#ff0000>函数名: strcmpi </FONT>
* X& i7 _. Y. ]4 X; b' q5 C: F4 h功 能: 将一个串与另一个比较, 不管大小写
: L! \* t# C6 K用 法: int strcmpi(char *str1, char *str2);
# ?8 d0 z4 S2 [* d% ~程序例: </P>
9 Z5 K& ` H. x( N4 R. h<P><FONT color=#0000ff>#include <STRING.H>
4 y1 g+ f b8 [ `, i% ]5 K#include <STDIO.H></FONT></P>
- a" E' c1 E- y<P><FONT color=#0000ff>int main(void)
5 \, h1 R6 w7 f) y# L3 \( e+ b{ 6 |0 D; m5 e* k
char *buf1 = "BBB", *buf2 = "bbb"; 2 x. L7 v# t. {# ?3 k
int ptr; </FONT></P>& h5 b& w. i% L" @+ Z) L' q# F
<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>
+ G5 r$ J5 P/ D1 k<P><FONT color=#0000ff>if (ptr > 0)
# F- x/ ^- u% e* Qprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
' a# d5 [* s9 t/ ^, I5 U# j<P><FONT color=#0000ff>if (ptr < 0) ) P' a; p! ?! Q+ G2 D5 v
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
^# P) ^0 t# X8 u1 G4 N- |& E<P><FONT color=#0000ff>if (ptr == 0) 9 J8 C8 z! a! [8 O3 S
printf("buffer 2 equals buffer 1\n"); </FONT></P>
4 y/ {) R7 B3 U" v7 i<P><FONT color=#0000ff>return 0; ! |- S; D. Y0 B& t
}
! M& {. k2 N Z7 o- }) |' b</FONT>
( G# |! X$ _3 W( r7 K+ y# i% I) ]8 Z$ x p6 U
</P>
g9 E& N' g5 e0 C7 V: k<P><FONT color=#ff0000>函数名: strncmp </FONT>
1 R5 K2 q" _" k2 O* G6 X' J功 能: 串比较
* ] P; I" V! d. u% [# X8 I用 法: int strncmp(char *str1, char *str2, int maxlen); - ^$ Q& V6 L. t3 X- _0 u
程序例: </P>2 {, A) B/ [/ z6 A4 I
<P><FONT color=#0000ff>#include <STRING.H>
C ]- N7 b) X#include <STDIO.H></FONT></P>1 V) Y" ~# w: L4 F
<P><FONT color=#0000ff>int main(void) </FONT></P>
! j/ f) e% v: @- w$ f$ e5 d4 M8 \<P><FONT color=#0000ff>{
- y; X/ X9 Y" E2 l: rchar *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; ' @0 a) O- x- G
int ptr; </FONT></P>7 y0 ?' q4 ?2 \* i- J% Q$ u
<P><FONT color=#0000ff>ptr = strncmp(buf2,buf1,3);
/ f! S8 A0 r$ e1 J+ bif (ptr > 0)
% V s2 b" [+ Q' T. n3 c) Sprintf("buffer 2 is greater than buffer 1\n");
4 p# ~9 ?/ }) `) m/ H2 Relse ) U3 }( b1 S. s5 D7 T
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
; ?" u3 }7 X( T" {& ~<P><FONT color=#0000ff>ptr = strncmp(buf2,buf3,3);
9 l2 ?: t8 x1 k, ` K9 [if (ptr > 0)
3 {* N/ R7 N, U) D3 ?# Hprintf("buffer 2 is greater than buffer 3\n");
- m- V5 m! ^8 \, @else
9 S) V$ s$ }0 A' i$ I( c: j& Aprintf("buffer 2 is less than buffer 3\n"); </FONT></P>
0 ^6 }/ S( G2 ?- n, q3 o<P><FONT color=#0000ff>return(0); ' q6 J+ g8 }$ W4 @$ @. B9 _ \( I
} : C4 G/ i2 o' t; [9 ?* A% R
- R4 w \/ t5 T {0 m</FONT></P>+ K2 ], z6 p5 [. y
<P><FONT color=#ff0000>函数名: strncmpi </FONT>6 a6 w# {+ F5 G
功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写 " L% e! c9 x Q0 g ^# l0 w
用 法: int strncmpi(char *str1, char *str2); 8 P4 T6 s- U5 t: L4 O. E7 R
程序例: </P>' p4 x7 ]8 i' Y6 H0 C( L
<P><FONT color=#0000ff>#include <STRING.H>6 K( T# j' i# @9 ~
#include <STDIO.H></FONT></P>" f8 E, Y) G- Y/ x8 \& J
<P><FONT color=#0000ff>int main(void) " l' M6 k& Z+ e8 D" M
{
! C# F' o% _7 b' m0 [; J, t. Lchar *buf1 = "BBBccc", *buf2 = "bbbccc"; $ j* [0 V) q9 A$ e6 t( d
int ptr; </FONT></P>- }* _! l5 g: y) I. C% ]# @) \2 z
<P><FONT color=#0000ff>ptr = strncmpi(buf2,buf1,3); </FONT></P>
9 f. T) N- ]7 K5 J% ?( ^6 K7 J<P><FONT color=#0000ff>if (ptr > 0) + k9 c1 d S* E! G8 O% J* i! [- {
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
! g9 X2 g" A2 W<P><FONT color=#0000ff>if (ptr < 0) ' G5 @# v2 t( d2 V9 m( O9 r! f
printf("buffer 2 is less than buffer 1\n"); </FONT></P>! U, b6 y3 F! i' F
<P><FONT color=#0000ff>if (ptr == 0)
: L9 G) L) k% o* A0 Wprintf("buffer 2 equals buffer 1\n"); </FONT></P>* G# O- b7 L- A" G0 r
<P><FONT color=#0000ff>return 0; </FONT>
* a8 I8 a2 c! y. z$ z" j}
E8 T9 K F2 m4 K6 Q( x( z3 `* o' b r9 P( L) f. [3 D5 G2 ?
</P>( a2 y8 _1 t& ~4 @0 D3 w1 o
<P><FONT color=#ff0000>函数名: strncpy </FONT>/ D( d3 ~# Z3 L! K
功 能: 串拷贝
# p4 G+ c* I. x用 法: char *strncpy(char *destin, char *source, int maxlen); 0 ]' B. D6 b- R! n9 y' r% g5 Y/ X
程序例: </P>
# u9 v, P) K \; W0 h, o<P><FONT color=#0000ff>#include <STDIO.H>6 c' X- y2 E9 F1 \. g' w
#include <STRING.H></FONT></P>
) g4 h" f" t3 B. i+ n<P><FONT color=#0000ff>int main(void)
1 O3 A- i7 y5 n1 e/ D1 Y& z; X+ l{
0 ?9 v: B8 k; j# U @char string[10]; [) J% U. }9 e7 g5 x9 x* q
char *str1 = "abcdefghi"; </FONT></P>
& T' y& B7 o }<P><FONT color=#0000ff>strncpy(string, str1, 3); 1 c% Z+ X, c/ m5 P3 Y+ h4 {
string[3] = '\0'; , |6 |8 y5 V* W- t L. q: ]( x& p
printf("%s\n", string); 8 V- u p/ o* K
return 0; # [" o' z j0 o5 E% F1 A
}
& v% u7 s& T. ~9 h5 b</FONT>
" U; H" t9 q" _1 G</P>( u/ Q# C1 F% `3 I! C8 L! a! v p
<P><FONT color=#ff0000>函数名: strnicmp </FONT>
3 e$ k7 A8 w* h# Z" S/ g8 C n. |功 能: 不注重大小写地比较两个串 + z7 t; a( D" }" E0 H
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
H' W* |2 I' c# |6 x' W程序例: </P>
" C, D0 a+ K! |: r8 G<P><FONT color=#0000ff>#include <STRING.H># U" O: t$ B) z
#include <STDIO.H></FONT></P>
& n& a" l) A. M h6 Z<P><FONT color=#0000ff>int main(void) * _3 d: B: W4 A0 n" m# [, L' p
{
! }5 h: O* B9 d/ v V5 K/ @% b+ mchar *buf1 = "BBBccc", *buf2 = "bbbccc";
2 ^* k: Q, K2 y( w7 Z) L) Pint ptr; </FONT></P>
- E2 _, j! f* U<P><FONT color=#0000ff>ptr = strnicmp(buf2, buf1, 3); </FONT></P>- l% L; W2 B) r4 i: _
<P><FONT color=#0000ff>if (ptr > 0) ( w7 r* E; D" o8 \/ z
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
[( F" K; b7 t. A0 B<P><FONT color=#0000ff>if (ptr < 0) 3 c) L; U$ f+ v, f
printf("buffer 2 is less than buffer 1\n"); </FONT></P>9 I( @' `0 S5 U/ O) c
<P><FONT color=#0000ff>if (ptr == 0)
( x6 x- m' ~4 h3 w- K J0 ?printf("buffer 2 equals buffer 1\n"); </FONT></P>
9 N; n- b0 M! i( b8 z, Y' i& [<P><FONT color=#0000ff>return 0; J _/ I/ }8 p0 l4 n m' ^3 ]1 u
}
0 \" [ S- e( z' I* E: N8 G! m( x7 n/ Z6 W2 t+ N
</FONT>, H" T! d3 D, B$ }4 Q- b# e# N
</P>+ u9 z S4 `. N/ C
<P><FONT color=#ff0000>函数名: strnset </FONT>
& M) w) A o7 _ F- B4 y功 能: 将一个串中的所有字符都设为指定字符
. Q% O/ L% N$ x$ k% X7 j1 }用 法: char *strnset(char *str, char ch, unsigned n);
1 _) w% }- a9 n1 x0 Y3 J程序例: </P>
( W% }' y' A( e5 G<P><FONT color=#0000ff>#include <STDIO.H>5 I! O6 c* J( q* \
#include <STRING.H></FONT></P>
) s J( t3 w: t3 d5 u/ a9 L/ D<P><FONT color=#0000ff>int main(void) ) p- [7 p: {, `: M% U" `
{
4 \( l8 c/ n0 c5 }char *string = "abcdefghijklmnopqrstuvwxyz";
* T( O N& X8 T& W) zchar letter = 'x'; </FONT></P>- p. ]- y8 x+ v! C# ]
<P><FONT color=#0000ff>printf("string before strnset: %s\n", string);
- m2 |/ I6 D: ^8 t0 P$ q2 }- _5 astrnset(string, letter, 13); / b/ R5 Z& a9 j
printf("string after strnset: %s\n", string); </FONT></P>, C9 R2 S4 N0 Y @; f: O2 [
<P><FONT color=#0000ff>return 0;
( V+ q6 M3 c$ t0 z2 w3 A} ) P5 c- W% |) [5 B
</FONT>
9 x$ f5 V) f: M) a</P>
+ U3 F; F( ^/ c. a, W<P><FONT color=#ff0000>函数名: strpbrk </FONT>
5 x8 H2 C$ {* `$ _% G功 能: 在串中查找给定字符集中的字符
+ j4 r: r0 n4 h用 法: char *strpbrk(char *str1, char *str2);
; A* i$ {: q3 M: }% S0 d" S程序例: </P>3 M5 n8 Q& T8 g6 R
<P><FONT color=#0000ff>#include <STDIO.H>
3 m1 J5 C$ Z4 H. G3 O- B$ _; b# J#include <STRING.H></FONT></P>
) I1 y7 q& Y5 l$ ~& Y<P><FONT color=#0000ff>int main(void)
2 H3 t/ X3 W9 n! B1 t7 `{
9 u% q* w( g' g) q: Schar *string1 = "abcdefghijklmnopqrstuvwxyz";
2 n$ E3 N$ m$ Zchar *string2 = "onm";
5 \$ r& {. ^7 L( o2 Z2 Pchar *ptr; </FONT></P>1 h, u+ a8 b; ?# M/ Q; W
<P><FONT color=#0000ff>ptr = strpbrk(string1, string2); </FONT></P>
$ q9 g" l& O: @4 b6 X<P><FONT color=#0000ff>if (ptr) h% M- C# L% @9 a8 B! I) c
printf("strpbrk found first character: %c\n", *ptr);
1 s) u% M; k$ l, h) c( a3 Zelse
4 b5 `9 u8 R, q0 s1 Sprintf("strpbrk didn't find character in set\n"); </FONT></P>
M. g# D# b. e" {+ V( `; D<P><FONT color=#0000ff>return 0; : ?3 c' o8 ?, g6 T
} 3 [& |/ i6 [3 t
" V: K& |, Z" f3 b4 ` Z</FONT>
& A: ? D4 u; ~$ @1 b$ w; K0 U' K</P>) T! X y% h: n+ K4 ~( s/ m
<P><FONT color=#ff0000>函数名: strrchr </FONT>
* X0 b: k- G2 q7 @* y7 y: l功 能: 在串中查找指定字符的最后一个出现 3 D% j2 G6 {) c
用 法: char *strrchr(char *str, char c); ( o7 W% l5 C3 @/ U
程序例: </P>: g, X7 A9 J: O- x$ s8 P
<P><FONT color=#0000ff>#include <STRING.H>
% t' h; ]8 [: g) h; p( @& w% f#include <STDIO.H></FONT></P>
! ?0 l+ z8 ^7 [<P><FONT color=#0000ff>int main(void) s G2 J1 \5 @; M
{ * E2 b1 Q" P" `3 T$ a6 O* j- h0 O
char string[15];
6 L6 V* D- w& w8 P' Zchar *ptr, c = 'r'; </FONT></P>7 Z& O: d" g- z
<P><FONT color=#0000ff>strcpy(string, "This is a string");
# }2 |" D8 W) L. c) k. e4 vptr = strrchr(string, c); R) \# g4 Z! J# L, k, e2 M
if (ptr) + p. J* P. X1 n9 P: O$ c l. W
printf("The character %c is at position: %d\n", c, ptr-string);
8 r2 }; ^. @* u, D! V7 U1 P3 w( n3 Selse " `3 t: e8 B: v, L
printf("The character was not found\n");
( P8 @ ]' ^+ g: w: G3 G# v4 Vreturn 0; - `; Q' y8 [6 n
} </FONT>) B9 C8 U! Y* j8 @, C7 x
# l( x7 i( W4 }
3 a( e; d* x% w</P>- A( t6 w9 S% Z. W! Q7 [2 O# y1 V
<P><FONT color=#ff0000>函数名: strrev </FONT>
6 s9 E3 g; {+ S: V3 }* u0 {. w功 能: 串倒转
1 k" o1 x% U: _# B! B用 法: char *strrev(char *str);
6 `% T1 N' z& m3 g程序例: </P>
; `/ }9 P4 Y8 [2 L3 W<P><FONT color=#0000ff>#include <STRING.H>) O" _5 V/ x; v3 @, c9 Y- o% Q7 Q3 t
#include <STDIO.H></FONT></P>
& Q" P, w9 x9 J0 {<P><FONT color=#0000ff>int main(void) % _2 _$ X$ n; F; f% @, O8 o2 H
{
" n) P, d* F/ @) B4 w; Q- O" g7 cchar *forward = "string"; </FONT></P>% V1 I! A1 b h3 k1 R) n0 T8 Y3 h! h* A
<P><FONT color=#0000ff>printf("Before strrev(): %s\n", forward);
$ w% M' m% ~8 I: m$ N, n# Pstrrev(forward); ! p( |5 {2 |3 D* {$ M$ m
printf("After strrev(): %s\n", forward);
; `9 k9 s9 ]$ h# greturn 0; 9 s" d* t* N7 h# [, l( k
} </FONT>8 N- G9 y. @. M$ o
</P>
6 c9 `" E6 h! u; ~( `. Y* u- {<P><FONT color=#ff0000>函数名: strset </FONT>$ k& @/ i3 l$ m" d W( e. _
功 能: 将一个串中的所有字符都设为指定字符
0 z; v5 I, ^% ~用 法: char *strset(char *str, char c);
3 w9 C. r; s c1 M$ l d程序例: </P>
" J P: J x& l! N; `# l<P><FONT color=#0000ff>#include <STDIO.H>
& g7 G' N# F" J: o#include <STRING.H></FONT></P>8 c* G$ W* f: [5 Z9 M8 k
<P><FONT color=#0000ff>int main(void) . z, w( I4 H. S% E- D" E, J, C
{ - L% H G2 s- j' O& y! z
char string[10] = "123456789"; 8 I1 ?# {3 Z. v% s
char symbol = 'c'; </FONT></P>9 ]( c4 O" ?( B* K% z7 F8 H9 [
<P><FONT color=#0000ff>printf("Before strset(): %s\n", string);
0 }# N0 h6 a% D2 }& X" Kstrset(string, symbol);
' W a k0 `' `/ ^" M3 { g+ C+ Mprintf("After strset(): %s\n", string); 0 D8 N$ Z0 E0 j
return 0;
- c. Q+ ]5 s; A$ D# S+ r}
6 F# a8 ]2 s9 |+ W- J
- u: _7 x2 R) T, {& q1 \, m+ M</FONT>/ ~' S4 a8 E5 v9 u8 ]- W0 }8 {
</P>9 L) y+ J3 ^+ B
<P><FONT color=#ff0000>函数名: strspn </FONT>
7 w# N4 G2 Y( Y K. M功 能: 在串中查找指定字符集的子集的第一次出现
* T& ?" j- J5 R% J5 ~( D用 法: int strspn(char *str1, char *str2); * J3 r4 e6 v2 w6 y+ u
程序例: </P>7 ]! l1 v5 [' m0 ^8 ]
<P><FONT color=#0000ff>#include <STDIO.H>( T# @2 f' [8 j( H9 e
#include <STRING.H>
) E: H# y5 \3 G8 [4 ?#include <ALLOC.H></FONT></P>
+ G! m0 G* T2 {: d# A6 s<P><FONT color=#0000ff>int main(void)
3 y7 f9 ], P, P. v( k' A{ 2 [4 v1 s# o' G: q, u4 K
char *string1 = "1234567890"; " O6 D$ |. Z% w+ J5 n
char *string2 = "123DC8"; 7 y! L q6 |- ?$ F* P' j
int length; </FONT></P>: n e6 ~, W8 P" G( U4 {
<P><FONT color=#0000ff>length = strspn(string1, string2);
4 Q5 v7 J$ B0 o5 W8 e4 `: mprintf("Character where strings differ is at position %d\n", length);
) f, l9 c( L, A3 s# g% yreturn 0;
0 w2 G4 j$ ^3 p8 l9 O0 p} 4 i% e5 S | f- f1 z
</FONT>
/ u) M, f; K* b. Y% H+ z4 i</P>
: \' |' n* ]/ c5 b<P><FONT color=#ff0000>函数名: strstr </FONT>5 ~& E# ^- p3 h$ R4 U
功 能: 在串中查找指定字符串的第一次出现 1 } F _' {- T" _" y$ J$ l T
用 法: char *strstr(char *str1, char *str2); 2 }% X% R4 D1 m- y1 @" a$ G ]
程序例: </P>! K* i' r1 E; M2 Y* y8 }1 z
<P><FONT color=#0000ff>#include <STDIO.H>2 u7 r" m, K) Q
#include <STRING.H></FONT></P>& d' D9 ^9 X/ z. K
<P><FONT color=#0000ff>int main(void) + |0 |8 |2 o! d( }+ {
{ 7 R! j2 l) M4 a* b
char *str1 = "Borland International", *str2 = "nation", *ptr; </FONT></P>
2 m- Y- {" x( A: M2 A2 d<P><FONT color=#0000ff>ptr = strstr(str1, str2); % F: @; G j! y1 k, v I0 @
printf("The substring is: %s\n", ptr); 9 J% x1 b" \) Y# h% l6 @0 t
return 0; ' `+ J9 C* I, H6 ~9 e
} </FONT>. T( C' J2 ?# A/ P
9 m S" T, O0 W7 F, _2 S. w: g- T
</P>
: H& l% }6 s1 P, W7 j<P><FONT color=#ff0000>函数名: strtod </FONT>5 J" V# ^# J7 i* g1 ~7 @/ A
功 能: 将字符串转换为double型值
, M, u9 S! D! ^, j% H3 a- e用 法: double strtod(char *str, char **endptr);
+ A1 A0 J! j z4 Z- `程序例: </P>
0 F) }+ J9 a1 C8 c7 g0 R6 T7 X<P><FONT color=#0000ff>#include <STDIO.H>
/ [' q3 N" ?+ v/ K" K, B#include <STDLIB.H></FONT></P>
9 ]; ^$ q# [: I0 X& G, t" ]2 k2 a<P><FONT color=#0000ff>int main(void)
& r" C0 N! w9 d% P+ J! l0 ^- m{
9 g+ v* \: _" L4 U/ }0 j8 ychar input[80], *endptr; ; @# ^ A3 f( r, |4 Z+ G% d" @- ?
double value; </FONT></P>9 a; v. p3 ?) i7 b6 d. e' h( M
<P><FONT color=#0000ff>printf("Enter a floating point number:"); 8 H4 Y* c& I- M( W. e
gets(input); . o' v. L, s+ I
value = strtod(input, &endptr); , U% w/ {! P0 z* w8 c
printf("The string is %s the number is %lf\n", input, value); . X0 G4 T* b% z2 J
return 0; + H$ D4 ?% t C0 }) ~7 j
}
) d9 C, @- A; H: q5 l) N</FONT>
; \+ D0 r: A& o. t" n D4 ?: A1 |
! `0 y& \) S, A# t/ A0 |</P>+ i! E! e' W) T- A, U) h+ A
<P><FONT color=#ff0000>函数名: strtok </FONT>
6 f$ z6 ]; d8 j' ^) Y9 f9 C功 能: 查找由在第二个串中指定的分界符分隔开的单词 - f9 P+ g; I' B! ^5 w
用 法: char *strtok(char *str1, char *str2);
7 E2 F" e/ l7 d" l1 @6 x程序例: </P>; t4 _- } P/ ?- q, P/ M
<P><FONT color=#0000ff>#include <STRING.H>
8 C W% ? x. D. t) K3 k3 Q( B#include <STDIO.H></FONT></P>+ G& Y8 q" q4 Z0 K! T0 P9 c
<P><FONT color=#0000ff>int main(void)
( a, x, u. e# q' S. o. C, b! I9 r! \{
$ v0 k) z5 U" `char input[16] = "abc,d";
" h2 X* ^+ n; a+ a7 C. D; Cchar *p; </FONT></P>
) E; H2 v: V$ @# x; T# D<P><FONT color=#0000ff>/* strtok places a NULL terminator
# v% Q: O' K1 _+ t( Q Z% S8 | iin front of the token, if found */
9 W: Y5 M* [! b! }p = strtok(input, ","); # B( h8 s5 b$ Q
if (p) printf("%s\n", p); </FONT></P>0 r, [2 }) J" |8 n6 P1 I/ ?
<P><FONT color=#0000ff>/* A second call to strtok using a NULL ; \% ?% a6 L. i* u, |' V5 g
as the first parameter returns a pointer s5 f7 x' w x$ D5 `
to the character following the token */ * @2 i* Y7 _( r, M9 d
p = strtok(NULL, ",");
! `2 @& {( I4 T4 c$ l. {if (p) printf("%s\n", p); 9 z% f. X5 D5 u3 F# H
return 0;
! d' s/ D+ i( Z; s}
* X0 U7 O( ]2 p2 o+ \3 h; J h( j! i4 k3 c. O8 C6 \
- V- N% c" q% r1 }</FONT></P>
% P7 x, w8 N; ^! {<P><FONT color=#ff0000>函数名: strtol </FONT>" e' L" v5 N. m$ g7 S/ F
功 能: 将串转换为长整数
# G1 z& J6 f$ F2 w6 p用 法: long strtol(char *str, char **endptr, int base); # l; f/ S$ Q% z. ?
程序例: </P>
# r- b* @: U, A2 Z<P><FONT color=#0000ff>#include <STDLIB.H>5 `& h! \* j% P S2 E3 n+ |: I( g- Q
#include <STDIO.H></FONT></P>2 E( h3 H5 l0 W& q& y6 N* p8 e& f
<P><FONT color=#0000ff>int main(void)
. y" }1 q, B, A. d. x{ " ]; d9 S, h# @: [# \
char *string = "87654321", *endptr;
& O9 l; H9 P" \) j l5 o( Ulong lnumber; </FONT></P>
% p& b$ D. y: h" P8 [: f l<P><FONT color=#0000ff>/* strtol converts string to long integer */
; N" F9 f% E" I: Flnumber = strtol(string, &endptr, 10); . J6 z' d* c7 N; I
printf("string = %s long = %ld\n", string, lnumber); </FONT></P>
3 ]! k# G' t0 N$ j, P<P><FONT color=#0000ff>return 0; 6 o* [2 k8 m* h
} </FONT>1 u6 ~. @. J1 E& B
</P>) D& E' Z- R* `( U5 c, f
<P><FONT color=#ff0000>函数名: strupr </FONT>
' {% E/ B+ x! N, K' B功 能: 将串中的小写字母转换为大写字母
( t* q2 b( j- R1 g; p* A8 P用 法: char *strupr(char *str); / C- q$ d7 l. d5 B- q( m- b
程序例: </P># U; K4 c5 M" c x2 e
<P><FONT color=#0000ff>#include <STDIO.H>! ~ ]! L% Z% ~. E5 h
#include <STRING.H></FONT></P>0 a" G; ?8 t6 ~+ J
<P><FONT color=#0000ff>int main(void)
@& x+ y: l( ?; ?{
9 Y( u: W' `$ z6 }! V9 Mchar *string = "abcdefghijklmnopqrstuvwxyz", *ptr; </FONT></P>5 ~: ^) ]$ i+ h/ w& A
<P><FONT color=#0000ff>/* converts string to upper case characters */ : A9 h1 [! T" M
ptr = strupr(string);
0 K* R/ v9 J' g% @4 A" B9 Zprintf("%s\n", ptr); - K% Q8 { ^# C" s5 A, H' M
return 0; 6 G/ f- O/ B( B( Q0 S
}
" {2 `2 R3 [4 ^3 ?9 U l7 i
% S& A9 H) q" L3 v</FONT>
' @. B7 P8 y) e' @/ \</P>
/ O# g, U7 t1 m! R. o<P><FONT color=#ff0000>函数名: swab </FONT>
% R# H9 r& d. c4 C* @; F功 能: 交换字节
6 M* q6 f0 c6 Y7 K1 I! x用 法: void swab (char *from, char *to, int nbytes); 4 z5 r S1 O) `
程序例: </P>, s5 K j; g4 ]) B% g/ x
<P><FONT color=#0000ff>#include <STDLIB.H>" d! ]; v, i: p6 w( {" n
#include <STDIO.H>& b4 ?7 S, R | E
#include <STRING.H></FONT></P>. t4 U$ o6 m9 e# C* X' c4 S
<P><FONT color=#0000ff>char source[15] = "rFna koBlrna d"; # \' G& X* h" o ]/ w W! P
char target[15]; </FONT></P>" z1 j% I* j8 L
<P><FONT color=#0000ff>int main(void)
. J8 _! o- n3 S{ ) ?- z( M8 }& [( m0 P7 U6 C# A
swab(source, target, strlen(source)); + N! h/ H" z& g( l& U
printf("This is target: %s\n", target);
8 o+ H9 h# X2 b& Z( \& mreturn 0;
Y# |5 [: T2 l7 a}
3 }4 b9 t$ F' S0 C</FONT>
9 J! {9 P/ n! O4 l$ _0 ?: E
3 ?9 H/ F& w4 o: ?, B</P>
1 Z5 Y' l+ i9 N<P><FONT color=#ff0000>函数名: system </FONT>
! C& p$ k) \& n! p& u \功 能: 发出一个DOS命令 " ?, s5 @& W: s' p/ q
用 法: int system(char *command);
r; X" L1 E$ {6 c程序例: </P>
" z+ ?, ?- L9 x% j5 Z) ?( s<P><FONT color=#0000ff>#include <STDLIB.H>) D& u" E# |) m$ i; \
#include <STDIO.H></FONT></P>
& _2 L+ h" k0 I& n: n$ }5 k<P><FONT color=#0000ff>int main(void) " E! v6 q! a3 V" x- V1 U' O: M9 v
{ / {& ]$ B2 E7 [2 C( ^' F2 r
printf("About to spawn command.com and run a DOS command\n");
% F" |6 D( p+ ~4 Jsystem("dir");
9 r e; L8 x3 D% qreturn 0; 7 ?0 H5 S: h9 d# g+ C& q$ E
} </FONT></P> |
zan
|