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