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