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