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