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