- 在线时间
- 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>6 ]* Z; ?0 W% L! C* K7 r# \7 }
功 能: 改变数据段空间位置
( a1 _( r1 \+ A% {7 ^* E用 法: char *sbrk(int incr); + `1 N; [. @7 b8 K
程序例: </P>
: U7 O4 D Y9 D8 L9 K( B. h1 C# L< ><FONT color=#0000ff>#include <STDIO.H>* ]) A, z2 {, I' p& ?
#include <ALLOC.H></FONT></P>' ? j& b. U) x( m& E
< ><FONT color=#0000ff>int main(void)
: p* z0 r4 R, a6 Y; L7 V9 i" g- o- ?{
6 D' d& j" {6 U# xprintf("Changing allocation with sbrk()\n"); / f2 `# l) t: r& J& H' H9 A. V: d
printf("Before sbrk() call: %lu bytes free\n",
8 N+ u' i+ y+ m0 D3 L8 n& ^9 s(unsigned long) coreleft());
i7 j6 K. K& B8 P1 `' r* wsbrk(1000);
' ]1 p' t8 I3 Q" sprintf(" After sbrk() call: %lu bytes free\n", 0 i2 o, v- d4 ^, Y. p" {
(unsigned long) coreleft());
) @( ~$ ?6 i8 zreturn 0;
# [& U& p- z8 T+ I5 r5 V. L}
) \9 t( v- h& F% v5 Y) \* f+ J a- S) v+ j: _: e9 @' N+ _
</FONT></P>
/ [ G r" s2 l( Z x3 g< ><FONT color=#ff0000>函数名: scanf </FONT>1 U! i6 K" ?. M! L
功 能: 执行格式化输入 - ?# ]9 C0 G2 ^5 U# S' ] ~
用 法: int scanf(char *format[,argument,...]);
# O, z( x! @, s8 R1 R$ Q- O程序例: </P>5 ~; r2 t; q9 T5 m
< ><FONT color=#0000ff>#include <STDIO.H>/ t8 c6 F& ^ r& B( U/ p. s+ i
#include <CONIO.H></FONT></P>
6 t0 U8 P$ K2 r- ~: I7 E7 N7 o( @- w0 C< ><FONT color=#0000ff>int main(void)
2 ~# Y) p) T: b- n{ 0 R7 m6 Y, @* R, I
char label[20]; , k7 U0 k2 S4 D( |' V4 i6 }# {
char name[20];
* Y6 M5 D S; b* |# ?- R. M$ rint entries = 0; 8 y# n" ^1 _* a6 D$ [ s! q
int loop, age;
( b; c* n8 X# ^$ k! Idouble salary; </FONT></P>9 X# V+ q: x% p
< ><FONT color=#0000ff>struct Entry_struct
3 X, T% q1 F2 g r{ . M# [2 d* [+ A" b, D. T
char name[20]; ! @7 q# x7 F; U- T7 ]( Q
int age; 2 A* e8 |* ?% ^
float salary;
2 A1 e2 r% K1 R3 P y4 U} entry[20]; </FONT></P>. `& k' y8 d1 L& K, ^
< ><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */ & N7 C, g+ G4 H+ r
printf("\n\nPlease enter a label for the chart: "); # [4 I: u' w. r' n; V# }: T; O1 \
scanf("%20s", label); * c. ~& j4 B7 Q* c( s9 ?
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
% S0 `( e' f8 i# q% `% S( D< ><FONT color=#0000ff>/* Input number of entries as an integer */ C5 a3 c9 O$ w. ?; x! A8 |$ G4 D
printf("How many entries will there be? (less than 20) ");
9 \1 H3 U }' z" J5 Jscanf("%d", &entries);
( v' t, f8 F+ G+ J; ^fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
" P! m7 W- D6 s8 M6 P! k; `: {7 K< ><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */
3 o i" D9 q4 i8 Y$ Ofor (loop=0;loop<ENTRIES;++LOOP) 1 D3 l2 t+ q" H% r/ S0 G s
{ 1 f; f1 M1 Z# k. y
printf("Entry %d\n", loop);
6 r& |! z/ H/ B4 Xprintf(" Name : ");
1 i& C) Q. k7 o0 U2 f, Oscanf("%[A-Za-z]", entry[loop].name);
% B* p6 S0 q: efflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>1 |# n" \+ O+ y* V6 O
< ><FONT color=#0000ff>/* input an age as an integer */ 0 O/ ?7 A7 }/ c) ^6 Z
printf(" Age : ");
' c w9 \' w" H3 @0 T0 l$ N& Y R& Fscanf("%d", &entry[loop].age); 1 p- M3 j, U3 K+ h% t3 {8 [: {
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
9 C B8 D4 s3 z7 h% t$ [8 L2 Q< ><FONT color=#0000ff>/* input a salary as a float */ 3 c( r' e: M! \# P( |) Q
printf(" Salary : "); 2 T2 G% q8 a; G. D
scanf("%f", &entry[loop].salary);
/ o4 y9 [; G! m; q: z$ [& {8 \/ rfflush(stdin); /* flush the input stream in case of bad input */
" X* Q4 r2 t* }+ s1 }} </FONT></P>
. P# j% M8 d. C< ><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */
4 o7 {' n( b$ gprintf("\nPlease enter your name, age and salary\n"); 4 E- H3 W0 b& L. V& X& z* Y
scanf("%20s %d %lf", name, &age, &salary);
, B* |0 h6 P+ Z! }</FONT></P>
& Y4 q7 Z- n# `2 O8 k+ J$ ~< ><FONT color=#0000ff>/* Print out the data that was input */ & x6 h: U/ Z- Z l" ?: \4 H% J
printf("\n\nTable %s\n",label); $ K, m0 B$ Z `3 D4 K/ K6 |
printf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
( z( v2 Z7 R* K) z) U& Dprintf("-----------------------------------------------------\n");
l# I/ @9 ~9 y) J$ zfor (loop=0;loop<ENTRIES;++LOOP)
6 ]! g( L/ |: Y/ X( B7 } printf("%4d | %-20s | %5d | %15.2lf\n",
+ x: |- Z; Y: `loop + 1, ; ~# a6 G1 m5 g( d
entry[loop].name, , r2 d" F6 D; w+ Z5 D) R
entry[loop].age,
( k+ ^! W5 _! g3 v& U0 bentry[loop].salary); i2 y A) f, z* g( O5 C
printf("-----------------------------------------------------\n"); + B+ i/ t j/ s9 M6 R) o
return 0;
0 s$ U: c* B+ N* E3 W# D6 h2 b8 ~8 J} ; k# N1 C3 y3 ]) E* f
</FONT>+ o) n9 A4 A% [9 K3 Q% S
</P>4 ~$ C) M* p2 T' ]) I4 [ `
< ><FONT color=#ff0000>函数名: searchpath </FONT>
4 {% ` l$ R, t功 能: 搜索DOS路径 * y+ k1 }- N+ r4 l9 N* S; S
用 法: char *searchpath(char *filename);
H9 [' x. m6 W5 U+ k% M# f9 y. m程序例: </P>0 p' i( j: R. ^' b- T# U1 x
< ><FONT color=#0000ff>#include <STDIO.H>
- k7 d. z! B' p+ F3 W#include <DIR.H></FONT></P>* R. N7 R# B) p7 V6 ~& K
< ><FONT color=#0000ff>int main(void)
: j# ?5 a& ~) \+ w* W4 X{ % ^' u: z6 f: N% Y& _
char *p; </FONT></P>2 K9 L; |8 A" _; ~% x- _. [& L! ?
< ><FONT color=#0000ff>/* Looks for TLINK and returns a pointer
) A, d8 k, S+ G! uto the path */ p1 d7 k& {/ |4 z/ w, l
p = searchpath("TLINK.EXE"); + p7 Y! v3 ^& e3 ~7 ^
printf("Search for TLINK.EXE : %s\n", p); </FONT></P>
" s7 Y4 {1 u0 c- ]0 n< ><FONT color=#0000ff>/* Looks for non-existent file */ $ E9 Q$ X& I/ Q* {" @
p = searchpath("NOTEXIST.FIL"); / P& }) R" `& a* f
printf("Search for NOTEXIST.FIL : %s\n", p); </FONT></P>% i2 @% m! }+ K1 a; b" A
< ><FONT color=#0000ff>return 0;
' o4 z, j' _# F0 U' {# O" [} - C/ k/ I( X* O. L' l( `2 A, N
# }+ p' T+ H* u4 U' V7 g7 D</FONT></P>
+ P m( l' K9 B< ><FONT color=#ff0000>函数名: sector </FONT>
, M( f1 o7 J( \& f- q0 v% H9 Z0 H2 ?功 能: 画并填充椭圆扇区
) }8 j9 u5 Z t* _用 法: void far sector(int x, int y, int stangle, int endangle);
7 g0 N6 D, v7 O' B$ U" e5 V程序例: </P>* B( H9 v, F8 {: g7 q
< ><FONT color=#0000ff>#include <GRAPHICS.H>
$ O4 z* A; M& K. L* J#include <STDLIB.H># ^2 Z7 a1 F9 M/ C' f* A
#include <STDIO.H>. i% p" z& b1 ?$ n
#include <CONIO.H></FONT></P>. m1 }: ^, L. v) a8 b. |2 F
< ><FONT color=#0000ff>int main(void) : I' ]# Z# N, e& r
{ ! U) k/ m, ?9 F2 B( D3 @
/* request auto detection */
) Q6 K! t0 v+ ^6 U% R: mint gdriver = DETECT, gmode, errorcode;
8 \( G7 S; r8 ~4 d( V v0 \int midx, midy, i; ( o, y3 l5 r5 \6 ]8 c- b1 B% l
int stangle = 45, endangle = 135; / G7 T% J/ l4 _8 n8 x
int xrad = 100, yrad = 50; </FONT></P>( _ {6 A4 Q- x- O* n
< ><FONT color=#0000ff>/* initialize graphics and local variables */
6 L% H3 E5 g. l. {! uinitgraph(&gdriver, &gmode, ""); </FONT></P>
; m) T# O0 ?6 E8 f- m: x# Y! v< ><FONT color=#0000ff>/* read result of initialization */ % N1 f9 U# Z# P/ q; x+ Q& H# }( i
errorcode = graphresult(); ( { a& U q7 N6 t* F$ @
if (errorcode != grOk) /* an error occurred */ 3 L a+ j# r, [
{
" ~' U7 @% ?! H+ q. {printf("Graphics error: %s\n", grapherrormsg(errorcode));
! p' O: ~, u3 y& u$ Aprintf(" ress any key to halt:");
) u! @8 j, s7 b( X9 r0 R( C9 E1 qgetch();
- S, b9 \1 X' lexit(1); /* terminate with an error code */ ' k9 ~: Y' |0 k0 z+ y* B& |
} </FONT></P>
% ]% |5 C* a5 D: W< ><FONT color=#0000ff>midx = getmaxx() / 2; + ` A( G3 C7 d+ X( D4 _. t) Q9 z, M
midy = getmaxy() / 2; </FONT></P>
" \, d: O* p" K" u< ><FONT color=#0000ff>/* loop through the fill patterns */ - O. D7 t0 ]$ s& e
for (i=EMPTY_FILL; i<USER_FILL; <br i++)> {
7 f) A/ A9 O- G7 F5 W+ q1 z$ p/* set the fill style */ ( z. ]2 F0 ]% B$ c
setfillstyle(i, getmaxcolor()); </FONT></P>
, W& D, h. P' H9 e3 a< ><FONT color=#0000ff>/* draw the sector slice */ - A( P8 B# a- D* V& L
sector(midx, midy, stangle, endangle, xrad, yrad); </FONT></P>) P! W, r' i# D7 q
< ><FONT color=#0000ff>getch(); ; w$ z- T- k6 V' S; \9 _$ ]
} </FONT></P>" c5 d! {$ j& y4 u( C
<P><FONT color=#0000ff>/* clean up */
9 z. V U* ?$ P$ r" y6 ~1 P Mclosegraph(); " n a+ m& @! Q/ r( s6 i
return 0;
) g- d) F7 @5 k Y! m9 g# ?2 F} 7 n4 ]% C$ w, b7 n
</FONT></P>
' C/ v7 T* U5 [5 L<P><FONT color=#ff0000>函数名: segread </FONT>
% V8 x, i' R5 i功 能: 读段寄存器值
+ g w$ y8 _' F6 ~用 法: void segread(struct SREGS *segtbl);
- d( j1 R9 u/ G+ G) B+ }程序例: </P>( e0 x. S8 }+ z4 ]
<P><FONT color=#0000ff>#include <STDIO.H>
4 ?2 ]1 s+ g9 {0 D- \ U. d- K#include <DOS.H></FONT></P>: v; I8 k5 z4 T
<P><FONT color=#0000ff>int main(void) ! b/ b! e6 t# N; E% k6 W7 [: _# j- @
{
$ ^% |4 s. o) s& Q7 C% Bstruct SREGS segs; </FONT></P>0 Q+ y( m0 G, _3 o) Z: ]! u4 N' o
<P><FONT color=#0000ff>segread(&segs);
( ?0 F, U! a E% c* N* rprintf("Current segment register settings\n\n");
( s2 _/ k7 t3 K- Iprintf("CS: %X DS: %X\n", segs.cs, segs.ds);
3 H1 r' N3 L# n7 m7 P, v( ?& x* _6 aprintf("ES: %X SS: %X\n", segs.es, segs.ss); </FONT></P>
4 T4 e( y( ?4 T' k<P><FONT color=#0000ff>return 0; ; z) l: q# z* F; B3 U
} </FONT>
: P4 c9 \+ j! W- T8 f- w* x) S- L4 H& x* g3 J
</P>$ O2 S: J$ w0 }) D$ c5 {) t
<P><FONT color=#ff0000>函数名: setactivepage </FONT>7 b. K4 x' T; J- V3 h9 A! {
功 能: 设置图形输出活动页
& u$ o+ v) G$ e* E3 J用 法: void far setactivepage(int pagenum);
4 Y7 m3 o' a6 ^1 K @" R程序例: </P>
/ p1 a0 w3 O; }$ u- V$ ^/ \<P><FONT color=#0000ff>#include <GRAPHICS.H>0 E; E* S6 s# I
#include <STDLIB.H>3 M# g7 j9 o# Q; I: I
#include <STDIO.H>8 T7 }. }& N W0 q9 r6 ]4 A
#include <CONIO.H></FONT></P>
% E* o/ i/ w# `0 d$ v8 w<P><FONT color=#0000ff>int main(void) , P1 `% A" _+ O) x8 f7 Y
{
* `: T4 } z, ]+ g4 Z/* select a driver and mode that supports */ 8 M2 ~5 c3 r. z9 F7 C* N1 k; T# z
/* multiple pages. */
r7 t, l ]$ F8 \$ {int gdriver = EGA, gmode = EGAHI, errorcode;
" m; I( Z, u. n) U& h6 K# o" S) ?8 X0 Xint x, y, ht; </FONT></P>
) D, r3 _; O# j4 U4 n9 g<P><FONT color=#0000ff>/* initialize graphics and local variables */
5 a/ Q# `6 G. X9 n! {/ Binitgraph(&gdriver, &gmode, ""); </FONT></P>' t/ G" S4 A: {2 V* K
<P><FONT color=#0000ff>/* read result of initialization */
+ u) A2 p8 W# perrorcode = graphresult();
5 z7 I1 J* s, N ]if (errorcode != grOk) /* an error occurred */ 3 i; J* X+ N' {8 d1 N. t4 e" T
{
* r. R' b; ?9 E; p# qprintf("Graphics error: %s\n", grapherrormsg(errorcode)); , p; ~3 \3 K0 w$ F' B/ I
printf("Press any key to halt:");
0 ]& H" I5 g f. g/ Y/ D4 {getch();
& `# _7 S6 k% z+ Rexit(1); /* terminate with an error code */
% X+ i6 Z2 r* @% _; u$ ?} </FONT></P>. g* A& I5 Q% R9 B+ c x
<P><FONT color=#0000ff>x = getmaxx() / 2;
: m) x$ V, l1 z7 q9 H' N& dy = getmaxy() / 2;
/ ?; Y# }$ ~- T1 g4 _8 Z Bht = textheight("W"); </FONT></P>
( O. \/ B* p4 n$ x; H6 y: _<P><FONT color=#0000ff>/* select the off screen page for drawing */ & Y0 J4 q. r# ` ^
setactivepage(1); </FONT></P>1 E6 K4 U" D" D- f0 B8 w3 b8 V e
<P><FONT color=#0000ff>/* draw a line on page #1 */
9 Y# M7 T3 t* \* Q; pline(0, 0, getmaxx(), getmaxy()); </FONT></P>2 n& R% F1 G1 h6 z8 y
<P><FONT color=#0000ff>/* output a message on page #1 */
! x- J. t* q/ {* E9 Ksettextjustify(CENTER_TEXT, CENTER_TEXT);
( u: u. {6 q5 }5 O: L k6 K8 M; ?outtextxy(x, y, "This is page #1:");
( m5 |0 e2 A& f. h3 g- Houttextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
) Z; |% D; C+ Y<P><FONT color=#0000ff>/* select drawing to page #0 */
; x9 \8 J) m& ^- d* Bsetactivepage(0); </FONT></P>
2 D/ a) V/ x9 K5 Z/ B& V, y<P><FONT color=#0000ff>/* output a message on page #0 */ ) x6 |. w. G1 y5 E7 y/ C) x8 v9 \
outtextxy(x, y, "This is page #0."); 3 ^4 H( e9 l6 w
outtextxy(x, y+ht, "Press any key to view page #1:");
; i, s, M" T) e3 w8 ^getch(); </FONT></P>( f+ c* r# k0 u" B9 c
<P><FONT color=#0000ff>/* select page #1 as the visible page */
% V7 B. g! n* A( Q, e2 v$ Wsetvisualpage(1); </FONT></P>
" G' d: j, o) g: U8 }/ c0 c<P><FONT color=#0000ff>/* clean up */ + O' l1 y. W& G: D% a( R5 Y
getch();
+ Y( T3 y8 v: \! L- L( nclosegraph(); " ~( {" ~; w: M
return 0;
e* B& S: Z& ~7 _4 e' x}
/ z& b3 U9 a8 ^& I+ `1 e/ H</FONT>8 o q7 I* v( G% d
</P>
+ T- I. h& ]6 H! o6 H<P><FONT color=#ff0000>函数名: setallpallette </FONT>
6 [9 o. j: v: ^% }% T3 E功 能: 按指定方式改变所有的调色板颜色 $ s# a) T# V( c2 d
用 法: void far setallpallette(struct palette, far *pallette);
% v6 Y# w8 a' i' J8 M程序例: </P>
% p/ O5 I5 C5 S. K, i. R<P><FONT color=#0000ff>#include <GRAPHICS.H>
9 S8 B. F+ F2 H: [ ^# ]#include <STDLIB.H>
) }, _2 G( U4 g, u, G& Q4 A" y#include <STDIO.H>* x+ I( J! S! @6 i4 C
#include <CONIO.H></FONT></P>* M( K) a7 `$ U, Z1 @
<P><FONT color=#0000ff>int main(void) : Z4 e$ w1 j, X& L8 z
{ $ \2 A* J. }7 X
/* request auto detection */ 0 N) Y1 R, N5 A& t
int gdriver = DETECT, gmode, errorcode; ! n) Z, x# L8 m8 P
struct palettetype pal;
% y! ^0 T& I t$ F; n% g. z4 oint color, maxcolor, ht;
3 Y+ s1 x0 W' Rint y = 10;
# M! ~+ S6 u- W# ^" ^6 nchar msg[80]; </FONT></P>
2 v, J* P" v' V% V5 ?<P><FONT color=#0000ff>/* initialize graphics and local variables */ ; r) ~/ p# e# R1 _4 J" ]! f
initgraph(&gdriver, &gmode, ""); </FONT></P>
0 J' [. {: [- g<P><FONT color=#0000ff>/* read result of initialization */
* M7 ~) s% ^9 Gerrorcode = graphresult();
+ M0 S4 o: U; g3 I' U) hif (errorcode != grOk) /* an error occurred */
8 ]" k4 X' A" t' ] H1 n{
2 [% x" v3 ]8 Q Aprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 H! w" m7 @0 Q9 j7 G, S
printf("Press any key to halt:");
$ H. ]- G+ @3 `4 E1 Y C3 S6 kgetch(); / C# O6 B; F8 s, Z4 D% b
exit(1); /* terminate with an error code */ 2 M$ S: D$ W3 r- ~" l& Y
} </FONT></P>
, b% t9 n; \" w% J* Z n! S<P><FONT color=#0000ff>maxcolor = getmaxcolor();
+ k$ f# x+ |0 g/ v' [ht = 2 * textheight("W"); </FONT></P>( ~2 Y8 C2 z2 E# K" r3 }1 K
<P><FONT color=#0000ff>/* grab a copy of the palette */
9 O c; M0 Q8 |! D& ~& p- `* \% hgetpalette(&pal); </FONT></P>
5 O/ B: X7 F1 w/ X: Q! _<P><FONT color=#0000ff>/* display the default palette colors */
; W8 M8 X' b' I- f/ jfor (color=1; color<=maxcolor; color++)
" X0 ]8 G8 H6 ], o+ Q9 Z2 r' `{ ! t" W# K# \6 ^# r' U! v; b1 j+ h
setcolor(color); 9 k$ a+ `1 l8 F( H
sprintf(msg, "Color: %d", color);
, D8 p$ I8 A8 S8 w, zouttextxy(1, y, msg); : E* f7 t2 I& L4 g( t6 F5 {
y += ht; ' d4 w5 M6 e2 z; O% p& ?
} </FONT></P>
, X& G J1 p" y( ~<P><FONT color=#0000ff>/* wait for a key */ 0 _3 R7 p( ?( e$ |) }" W4 @
getch(); </FONT></P>
1 T. \* }$ S5 G. j3 J<P><FONT color=#0000ff>/* black out the colors one by one */
1 {. P0 a+ K" ?! u. @% P1 s' N2 dfor (color=1; color<=maxcolor; color++)
% H1 w, ]* m7 Q W4 z{
! W s) O# }: E' B! m" rsetpalette(color, BLACK);
V$ X7 K/ c: b6 ]getch();
# f; C, A, t$ O; `# U4 O, u3 @) ] G} </FONT></P>% g7 s8 M b% N$ n; M
<P><FONT color=#0000ff>/* restore the palette colors */ ( u" }8 C" N2 R7 T6 D5 F1 \0 l! j$ L
setallpalette(&pal); </FONT></P>9 a L1 n8 E4 o0 @; i
<P><FONT color=#0000ff>/* clean up */ / n; c6 W3 q% a
getch(); 8 i3 ]- s" V6 x9 e
closegraph();
& W" U3 M' B+ r' j7 @9 wreturn 0; ) O& v- S& z$ }
} $ i! U/ c: R+ n5 f1 ]9 g+ J/ ~) [
; H! N9 v4 D) a5 W</FONT></P>2 C5 k9 w- H* J9 ^
<P><FONT color=#ff0000>函数名: setaspectratio </FONT>
/ E$ d" l4 m! s" k. e6 k1 H* }功 能: 设置图形纵横比 3 F: [& f/ Y# ^8 R
用 法: void far setaspectratio(int xasp, int yasp); 2 i- O4 Z9 [( z6 e
程序例: </P>% M8 U, x: e6 E1 v
<P><FONT color=#0000ff>#include <GRAPHICS.H>2 f6 F# c( M/ B; e# ? O
#include <STDLIB.H>% T' h4 c! t: g" X+ E. [
#include <STDIO.H>( \* v: M% p. ~' p1 P
#include <CONIO.H></FONT></P>! q9 p, ?: d5 @+ o# j8 v* m. n
<P><FONT color=#0000ff>int main(void)
$ s# ^5 Z1 n9 B' q{
; [0 ~& B4 [. Y; L' C8 m! @& `6 q/* request auto detection */
, R' f" Q9 g+ Aint gdriver = DETECT, gmode, errorcode; 5 s; f6 M) H) F6 l! X
int xasp, yasp, midx, midy; </FONT></P>
7 B" ~6 J4 S8 x- U3 l<P><FONT color=#0000ff>/* initialize graphics and local variables */
1 h6 [/ A2 _ ]/ s. G6 [6 \initgraph(&gdriver, &gmode, ""); </FONT></P>4 B9 W' p, z" C( R* h7 Z A* a4 M
<P><FONT color=#0000ff>/* read result of initialization */ _; n- t) V+ Q' y7 o' k5 F u" l: X4 ~
errorcode = graphresult(); ) a" s# Y1 y! {) S4 u6 N2 B. Z$ X
if (errorcode != grOk) /* an error occurred */
' [: ~6 b( m( p) B. M0 }- d{
1 B4 n# E) ~% U& xprintf("Graphics error: %s\n", grapherrormsg(errorcode));
! r: z1 Y! T" m0 ^$ ^- Vprintf("Press any key to halt:");
; b% s; T* u& c" f7 ^' k- p/ M+ }getch(); . V" i. Y* e& g
exit(1); /* terminate with an error code */
# P# i; k5 m) a' Q) Q} </FONT></P>
7 r) k+ v# I& _; z9 x M# b5 U/ K<P><FONT color=#0000ff>midx = getmaxx() / 2;
5 ?1 V }) A, Q: M7 D: @5 Omidy = getmaxy() / 2;
, f b/ W0 [' I0 X2 ?setcolor(getmaxcolor()); </FONT></P>
, _4 W. W* M. E$ e<P><FONT color=#0000ff>/* get current aspect ratio settings */
+ q/ a+ o2 J( ?( N0 Cgetaspectratio(&xasp, &yasp); </FONT></P>
* S$ c( Y! v' P! W. u<P><FONT color=#0000ff>/* draw normal circle */
' O* \8 |, m+ O% Bcircle(midx, midy, 100);
1 [* N4 j; G5 R. ]getch(); </FONT></P>& x+ R2 ~) s( p/ m: W
<P><FONT color=#0000ff>/* claer the screen */ . e! i. }0 [7 _* X8 a; N
cleardevice(); </FONT></P>8 X, Z4 c' o( G( j+ _( ?2 e
<P><FONT color=#0000ff>/* adjust the aspect for a wide circle */
3 y# i: A, u3 }& R4 T3 a; ^' ~' A8 ksetaspectratio(xasp/2, yasp); 7 H7 R% u) E* S q
circle(midx, midy, 100);
, x' O4 b2 X7 o( e+ w( M2 @getch(); </FONT></P>0 R' b9 D5 ~" W* ~4 Z ?0 @
<P><FONT color=#0000ff>/* adjust the aspect for a narrow circle */
& v6 Q% F7 s" W) B1 Z% k: wcleardevice();
( p. e. I! u- j8 e, W4 U, ^: H2 C: esetaspectratio(xasp, yasp/2); T. X. d5 x: E2 p, ?
circle(midx, midy, 100); </FONT></P>
$ N% w! S9 |% {, K% ^; X/ C" H<P><FONT color=#0000ff>/* clean up */
5 X4 M3 G+ r) M) Mgetch(); " y+ }+ R }9 R$ H3 `0 Q
closegraph();
' T9 t$ v& K$ ~# I0 F$ x7 ^return 0; 3 T% J- h2 m! o+ c# E; m
} * L4 g/ {" H: u$ i! ~2 a# W1 H# u
</FONT>
, m4 W5 g7 K1 h</P>
! `0 j+ o8 q2 D( u& E- c. e5 m<P><FONT color=#ff0000>函数名: setbkcolor </FONT>: G) O* e# N$ l. N9 b+ j+ i6 {
功 能: 用调色板设置当前背景颜色
& k1 J1 x, ]. k4 s6 q5 I' w用 法: void far setbkcolor(int color); 8 b# j/ z* @, n0 w* M' }( J# j
程序例: </P>: ~# r+ I# u! ~+ c. X5 a! }0 j( q( S/ v
<P><FONT color=#0000ff>#include <GRAPHICS.H>
1 G# I, S3 q c* f#include <STDLIB.H>
% W* ?( e; c: e1 V% G#include <STDIO.H>
6 z$ m2 T6 |$ O3 }/ H! J#include <CONIO.H></FONT></P># [3 h% B+ a5 G+ X
<P><FONT color=#0000ff>int main(void) . z8 O# y6 V6 c9 x' ?+ W, [" D
{
7 m, h k) m1 w1 U- C+ u2 z% [$ O/* select a driver and mode that supports */ : X ]- v% l4 X5 k% q" k
/* multiple background colors. */ - H0 B1 i! l+ `! t0 |
int gdriver = EGA, gmode = EGAHI, errorcode;
. B- O( N* `1 J3 Yint bkcol, maxcolor, x, y;
1 `# q. [( `# M% Schar msg[80]; </FONT></P> J6 Y8 v& {9 b: b- C K
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ! S# E r# ~6 U, F: e
initgraph(&gdriver, &gmode, ""); </FONT></P>6 F$ _# g* y) l2 l+ R# ~
<P><FONT color=#0000ff>/* read result of initialization */
6 G- [/ U8 r8 ^& J/ f+ \5 \" a* Rerrorcode = graphresult();
. n& C( {& E; g ?if (errorcode != grOk) /* an error occurred */ % j- L: D0 o8 K* F) I5 U
{ & @) m9 Q2 F5 X" }& d5 ]# X2 j3 x
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 3 e/ u9 W# w- @& g1 j
printf("Press any key to halt:");
# K* G4 W. ^( n0 K# z/ `+ y8 Zgetch();
/ v4 g% E. M+ e( E+ x! ?4 dexit(1); /* terminate with an error code */
4 }2 Z" ^4 _( V. P* _5 D- l M} </FONT></P>+ i9 l+ u7 _- z* ^) S4 @+ N
<P><FONT color=#0000ff>/* maximum color index supported */
. m9 z/ d7 P2 t; g r |1 p. q4 qmaxcolor = getmaxcolor(); </FONT></P>
+ s3 s4 g2 m: h% R4 v<P><FONT color=#0000ff>/* for centering text messages */
& Y) }4 }7 {( | `settextjustify(CENTER_TEXT, CENTER_TEXT); + P) i' v# J4 h( `% i
x = getmaxx() / 2; ' {4 B4 i4 P; B. x3 `# Q( S+ \
y = getmaxy() / 2; </FONT></P>
: ?# m0 M$ \% X. u0 d<P><FONT color=#0000ff>/* loop through the available colors */ - ?4 c* g) S* E7 l
for (bkcol=0; bkcol<=maxcolor; bkcol++) 9 T7 q) { \8 t0 }) C, x$ \
{
$ j" X3 a% n/ L) J! b8 b/* clear the screen */ + t$ N# f: j3 ?% p) _9 V }
cleardevice(); </FONT></P>6 j- i+ d; O" U! l6 r m
<P><FONT color=#0000ff>/* select a new background color */
5 H% J9 s# Q! a0 Q# e& ?! i% |: bsetbkcolor(bkcol); </FONT></P>/ S3 {: c Q* C0 X2 F) C
<P><FONT color=#0000ff>/* output a messsage */ - \! d( w5 t' \8 C
if (bkcol == WHITE) $ `! `9 C1 _& j m( W
setcolor(EGA_BLUE); . |/ B/ Y; ]3 [: j( b
sprintf(msg, "Background color: %d", bkcol);
/ `+ n% M! i! [/ B" w% t* Nouttextxy(x, y, msg);
0 W# @& r( M Ngetch();
" j% T/ b1 ^2 T& B} </FONT></P>
# N# |6 x% q& e<P><FONT color=#0000ff>/* clean up */ 4 E- N9 c1 E0 i+ M" Y# c# N" q6 T; @) t
closegraph();
7 R, Z$ j3 {3 Y- Qreturn 0; + c9 a1 p+ P% g, s8 w
} </FONT>0 O- R* ~) U. ^2 V- C8 Y+ F
: D! h/ f. V4 i
</P>
& U$ d6 {2 m8 n" k+ `<P><FONT color=#ff0000>函数名: setblock </FONT>' q6 i+ X7 \6 R
功 能: 修改先前已分配的DOS存储段大小
8 [' `3 y0 j" ?用 法: int setblock(int seg, int newsize); 4 e) j1 W# y t* ` y: p7 X
程序例: </P>
/ z; |# t( j# `7 H. ~<P><FONT color=#0000ff>#include <DOS.H>6 o. `4 z, l1 ~
#include <ALLOC.H>
& j% t5 e- r1 `/ a#include <STDIO.H>
" k Z3 ]( C/ y3 ^#include <STDLIB.H></FONT></P>- S/ h2 L/ ~6 T( T: }+ y a1 W
<P><FONT color=#0000ff>int main(void)
, a: n1 H& r, H7 k{ 1 O* w5 d- F: g
unsigned int size, segp; 5 ^5 T) l4 M4 u+ {5 w
int stat; </FONT></P>7 \: S+ f9 n; v
<P><FONT color=#0000ff>size = 64; /* (64 x 16) = 1024 bytes */ L9 d. J" U8 n
stat = allocmem(size, &segp);
% y: |+ O g+ Q& v1 C0 [if (stat == -1) 3 E7 `5 n# K. }. q# n1 U: H
printf("Allocated memory at segment: %X\n", segp);
! F5 B7 @- ]4 m J8 L6 g& C4 A4 Zelse ; X! @' p& `, p7 Q" Q3 p
{
( c" c. ^$ F% E! j) c/ Nprintf("Failed: maximum number of paragraphs available is %d\n",
% W. H6 X g3 P+ v0 estat);
% v7 B8 I2 {; W5 ^exit(1); * F4 f5 J: [2 o
} </FONT></P>7 t+ m0 o/ O% f; C/ Y, ?
<P><FONT color=#0000ff>stat = setblock(segp, size * 2); ! ~% D" V$ E9 C; Q' H; {+ T
if (stat == -1)
, b& m. {: {* y" }printf("Expanded memory block at segment: %X\n", segp); + x) n2 r5 c+ {
else + \ Q9 a* h/ \8 e
printf("Failed: maximum number of paragraphs available is %d\n", k8 X( n# ?( m o0 b9 w: H
stat); </FONT></P>9 g! I7 |1 p0 C' U" }" K
<P><FONT color=#0000ff>freemem(segp); </FONT></P>
9 T8 v e+ a' q9 u3 H1 s<P><FONT color=#0000ff>return 0; 9 j6 I) D& S1 s0 P8 S
} ; o- k# p e* L6 I2 H
</FONT>
0 p) o9 A5 r1 _; q: y$ j' j" K</P>
" g# E8 k1 q) R# r* H<P><FONT color=#ff0000>函数名: setbuf </FONT>; L; G5 S* P; U4 B1 V0 ^
功 能: 把缓冲区与流相联
5 h9 N3 X2 [2 [用 法: void setbuf(FILE *steam, char *buf);
; _7 R; v1 A9 V/ P程序例: </P>( M1 D. l" P6 _1 c. W. p9 s
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
; g0 z5 S! |4 X: O( q0 K& a$ R<P><FONT color=#0000ff>/* BUFSIZ is defined in stdio.h */ & f3 m4 y. s$ ~/ I- y
char outbuf[BUFSIZ]; </FONT></P>
1 V5 R/ `+ v# e<P><FONT color=#0000ff>int main(void)
3 I+ @/ }1 T9 ]8 e0 j x; v& Q{
. C6 {6 p- p. e! D/* attach a buffer to the standard output stream */ 9 _2 g6 w' ]. {9 W
setbuf(stdout, outbuf); </FONT></P>8 \1 r; S/ M8 h+ s+ E5 F# i
<P><FONT color=#0000ff>/* put some characters into the buffer */
8 O" F" e+ Q! o N4 O6 T. bputs("This is a test of buffered output.\n\n"); # l. M9 P8 |. ]( V! z3 y
puts("This output will go into outbuf\n"); 6 s; D5 _/ s: }
puts("and won't appear until the buffer\n"); 0 @: ]% d( {0 t/ y
puts("fills up or we flush the stream.\n"); </FONT></P>" g, y, w; z3 L) p' u
<P><FONT color=#0000ff>/* flush the output buffer */ 3 Q/ x8 Z, ]" }
fflush(stdout); </FONT></P>+ j' A/ w+ T- n
<P><FONT color=#0000ff>return 0;
4 _" @: ]. x8 v4 s3 ?4 l% e+ g' [9 ~} 6 j0 g- X0 C+ |6 ]7 g; j2 @' Y, V
</FONT>
$ \5 E; I; u6 R" P</P># @3 k) Q& {% J9 `* M
<P><FONT color=#ff0000>函数名: setcbrk</FONT>
( \+ j }& @9 M" s! ~! @: Q功 能: 设置Control-break $ g- R5 `2 D0 b* b' {# q( c; u
用 法: int setcbrk(int value); 5 |( A0 \" E) G% f) O
程序例: </P>0 ?( c m* q0 Y- u/ s0 y8 K
<P><FONT color=#0000ff>#include <DOS.H>/ [8 Y* ^/ Z% a5 S6 e* m$ r
#include <CONIO.H>; ^' a& o8 [6 _7 [7 v
#include <STDIO.H></FONT></P>
% ~+ K1 s# @2 `. t$ y& l! z<P><FONT color=#0000ff>int main(void)
7 v4 Q, Y7 x. a{ 3 U8 K* x! O$ H) I
int break_flag; </FONT></P>
& A* Y3 P' C: s5 G" O<P><FONT color=#0000ff>printf("Enter 0 to turn control break off\n"); 1 X! e( P1 N, _# P! {; R6 l
printf("Enter 1 to turn control break on\n"); </FONT></P>$ d [9 U2 G; `' B' E: D: \
<P><FONT color=#0000ff>break_flag = getch() - 0; </FONT></P>
& y0 A! U) ^ ^; g<P><FONT color=#0000ff>setcbrk(break_flag); </FONT></P># q/ X3 k4 I) m Q! w
<P><FONT color=#0000ff>if (getcbrk()) 6 r3 ?9 r/ N: m
printf("Cntrl-brk flag is on\n"); 9 s* X. M1 w3 d( ~6 ?4 [" }
else / V4 O( R5 Z7 D# u" }
printf("Cntrl-brk flag is off\n"); $ d& \. p: _2 c7 t7 q
return 0;
: n. j, ?* Y E$ U1 N} 6 A- t* L& ^7 u6 P: A9 E8 ~3 d# {* N
* ? G( L2 O) n/ R1 T$ R* |
</FONT>8 r# {; q5 g& W/ V
</P>
' d6 Z" S4 Q$ f! i8 S: o<P><FONT color=#ff0000>函数名: setcolor</FONT> 8 U# F9 J) I) o2 L, F& l
功 能: 设置当前画线颜色
8 s0 B3 G3 D N( i( `' t, X) U用 法: void far setcolor(int color);
- i: m+ ]1 i7 R6 s2 g: c程序例: </P>
0 J8 ~0 ^9 t4 h @: `7 x" h<P><FONT color=#0000ff>#include <GRAPHICS.H>
6 V7 |5 o8 `' E6 p#include <STDLIB.H>
5 b d% q8 @* G! J! _2 S: }5 I0 `#include <STDIO.H>
! f3 o& S$ s5 X7 q#include <CONIO.H></FONT></P>1 z$ K) W; E) o! f7 X9 j2 p
<P><FONT color=#0000ff>int main(void)
6 ]0 }+ E! H7 {: R2 @+ ~{ $ b+ Q) V2 Z" J Y8 j4 V" N
/* select a driver and mode that supports */ : F" T; h5 Q+ S1 L9 J; x
/* multiple drawing colors. */
7 j: W- Z5 I: e9 x* `" Eint gdriver = EGA, gmode = EGAHI, errorcode;
! R$ F5 u( [, P b6 P% @int color, maxcolor, x, y;
1 n' K. {6 V, I9 Ochar msg[80]; </FONT></P>0 f, H) N) J% j. g; v! P. k2 ?
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 P- V, E& n( n, h
initgraph(&gdriver, &gmode, ""); </FONT></P>9 M3 r( h5 a% o
<P><FONT color=#0000ff>/* read result of initialization */ 2 @4 @6 u/ J6 h- E) e3 ~
errorcode = graphresult(); & y7 k/ k1 f- K
if (errorcode != grOk) /* an error occurred */ ) u& X J! S* `
{ * @8 S2 L5 ?6 _+ m
printf("Graphics error: %s\n", grapherrormsg(errorcode));
" L4 e# M6 L1 T/ [- O" b1 k0 `printf("Press any key to halt:"); 8 F3 {: _ U( E
getch();
" s1 y0 p1 u" ?exit(1); /* terminate with an error code */
* u+ }2 r) _8 A$ N/ s0 x} </FONT></P>: z. \& p# ~4 W
<P><FONT color=#0000ff>/* maximum color index supported */
* C/ {& Z+ B( I8 w* Y# L. K, vmaxcolor = getmaxcolor(); </FONT></P>! k2 s9 G6 @1 b7 w% S, m
<P><FONT color=#0000ff>/* for centering text messages */ " r' i: Y! e. Q7 l8 q, F4 h
settextjustify(CENTER_TEXT, CENTER_TEXT); * r) Q# \% u" n, D2 X ]0 Z
x = getmaxx() / 2; ! z% x- P% ~; E2 I& g: t/ G8 B
y = getmaxy() / 2; </FONT></P>
4 O/ L: j: H F$ P* B<P><FONT color=#0000ff>/* loop through the available colors */
: K/ \9 @+ V( g$ D0 n% C' q0 vfor (color=1; color<=maxcolor; color++) . d K3 ]3 d5 i i S
{ % d& Q4 \2 R) ?& M# [
/* clear the screen */ . [0 B! I% j) d8 k* y. Y
cleardevice(); </FONT></P>
4 O+ Y9 k7 l6 K<P><FONT color=#0000ff>/* select a new background color */
5 g6 k. o( D) b) V5 ssetcolor(color); </FONT></P>9 s6 b- M% w2 M7 r
<P><FONT color=#0000ff>/* output a messsage */ ! u) I* ]3 ~& S$ o( T3 ^
sprintf(msg, "Color: %d", color); 7 |) j% J6 S& r' e1 q6 s3 u. ^
outtextxy(x, y, msg); " i+ l. ? z$ q; m2 c
getch();
3 p8 N' E. d: c' U} </FONT></P>) O* M1 S6 M% w4 z2 D% U
<P><FONT color=#0000ff>/* clean up */
& R6 _+ u G7 E& s& X* ~closegraph();
' g" }; Y$ i9 I$ greturn 0;
, c% h# i( z; L4 B: S+ }; r( S}
. n3 J% A6 v- J7 x6 L</FONT>, K# r6 E. \1 [, U+ Q$ J( Z1 d, G' K
</P>
0 @3 h! j& b$ q1 h% w5 s<P><FONT color=#ff0000>函数名: setdate </FONT>( g# }' B2 v4 X4 T, z
功 能: 设置DOS日期 ! y. B2 r8 z2 @! w
用 法: void setdate(struct date *dateblk);
. |- { M- R$ E. _' I+ B4 _) l2 ^程序例: </P>! {2 b' r; U- Y) L# ^, M
<P><FONT color=#0000ff>#include <STDIO.H>( F% a. n# A% c' {! h. K* l
#include <PROCESS.H>
- X; \6 f1 e: h5 s/ Q0 G$ y" I#include <DOS.H></FONT></P>5 E1 g/ Y0 |) i4 K: F+ s }
<P><FONT color=#0000ff>int main(void) 2 j1 Z. B, D0 M
{ 0 N7 M" S9 ^& Y; U
struct date reset;
" i5 V2 M3 p: l* n0 Q* m; d1 Cstruct date save_date; </FONT></P>
5 c4 r+ T* {$ r# V; u4 E- U6 e, \<P><FONT color=#0000ff>getdate(&save_date); ( S! F' |4 v0 J( G6 |/ h0 c
printf("Original date:\n");
( Z" o( m- M0 U) t; R5 |system("date"); </FONT></P>9 A" C0 o. K8 ~- P) N9 Y
<P><FONT color=#0000ff>reset.da_year = 2001; 0 v8 L; r+ l d$ P: p$ s
reset.da_day = 1;
! J2 q, R: K3 [# Freset.da_mon = 1; ; A1 l) q: x, S
setdate(&reset); </FONT></P>
1 Q# z q( t, z* R8 f. b4 _8 f<P><FONT color=#0000ff>printf("Date after setting:\n"); + j7 v6 W8 |: K
system("date"); </FONT></P>5 i6 {. z) |1 C; H" u. o
<P><FONT color=#0000ff>setdate(&save_date); % ^2 s" d" Q* p" n. T
printf("Back to original date:\n"); + X8 Q, O; J' G7 ?) E# ]( P; f
system("date"); </FONT></P>
, O# O8 k/ { P4 K, I7 Z<P><FONT color=#0000ff>return 0; / F% y) a7 \2 ^
} ( ?/ K8 l! P0 r5 h3 B5 ~; B7 y
</FONT>
# d4 o1 l2 ^) U; z7 ?</P>
* ~" J$ _8 Q; Q# |/ R& f4 k<P><FONT color=#ff0000>函数名: setdisk </FONT>
$ B5 ^( J4 v8 f2 E& K3 G功 能: 设置当前磁盘驱动器 ' P5 t2 h% b9 \: ?
用 法: int setdisk(int drive); $ f, w0 y$ l% h. K6 I: T
程序例: </P>
5 ^; |& D5 H; }0 I8 c8 P# e<P><FONT color=#0000ff>#include <STDIO.H>5 d( W8 T7 |7 S& S) o/ |4 j
#include <DIR.H></FONT></P>/ m# H- ]9 u6 Q! N0 s, Z- ^2 Y
<P><FONT color=#0000ff>int main(void) 1 M' Q( |3 l& ~9 c! d& W
{ 3 f1 g' b" K# r7 ]) C
int save, disk, disks; </FONT></P>3 p; @; v2 n( N
<P><FONT color=#0000ff>/* save original drive */ + J3 E5 ^4 |2 f7 G0 N) T
save = getdisk(); </FONT></P>
* P( a& Y- W* B6 U+ h4 F<P><FONT color=#0000ff>/* print number of logic drives */
0 q: [+ T9 v0 k7 A* c# c* Pdisks = setdisk(save);
0 T* P' Y4 K5 iprintf("%d logical drives on the system\n\n", disks); </FONT></P>( K+ Z9 K9 N, ]/ ]& r/ b
<P><FONT color=#0000ff>/* print the drive letters available */ $ H/ ]3 R" b$ D1 J/ T) x
printf("Available drives:\n"); ; d# z( z7 H) w% z, ~
for (disk = 0;disk < 26;++disk)
& w% z( @8 L# r& f/ {- J7 |# B{ , a- M3 }/ e9 s( k9 ?, ~8 u
setdisk(disk); : I ^8 I5 C" J1 j u* N* ^
if (disk == getdisk()) ! `( z8 C6 E2 S' @
printf("%c: drive is available\n", disk + 'a'); , Z" M7 P4 d8 R% W8 G2 ?% {! ^
} % m% w7 Q a H4 _- O p
setdisk(save); </FONT></P>0 B# a) f6 ^8 a, G
<P><FONT color=#0000ff>return 0; 1 J5 ~5 W2 M! P; T. W: R
} </FONT>
, A1 ^9 C0 E4 _ e! d U' X1 j! z' r8 L8 H
</P>9 H( b: @$ Z- s& d' Y. z2 N3 e
<P><FONT color=#ff0000>函数名: setdta </FONT>( E! h# G4 v* m( i8 X
功 能: 设置磁盘传输区地址
|1 p- R m; h用 法: void setdta(char far *dta); ) L) s4 l S B0 `6 f
程序例: </P>
) W+ Y* R1 x, x3 H0 B9 B; p<P><FONT color=#0000ff>#include <PROCESS.H>
/ j0 f; b5 t7 o( P#include <STRING.H>: @5 S* v# |& e9 Z0 R
#include <STDIO.H>+ p9 c# o$ H& D* [9 l- \% W
#include <DOS.H></FONT></P>
- L: ~9 F+ l7 \& n \% t<P><FONT color=#0000ff>int main(void)
: y9 ]7 n) U2 O, |+ x, ]{
6 o/ \/ A+ w1 t9 J5 C ~char line[80], far *save_dta;
, _0 G1 @2 J$ Gchar buffer[256] = "SETDTA test!";
# O( H5 \/ Y' ^2 I; Q% fstruct fcb blk; 9 G: s+ I0 ]- Y) e8 M
int result; </FONT></P>
" h% f4 [6 [* d4 `. X& K+ k2 |0 t<P><FONT color=#0000ff>/* get new file name from user */ $ v6 C! O; n2 {3 J# E. o/ U
printf("Enter a file name to create:");
; `+ J. j+ n: m7 e/ V- Bgets(line); </FONT></P>4 u/ ^- b3 _8 n8 Q3 [' G" E% t7 R
<P><FONT color=#0000ff>/* parse the new file name to the dta */
& x9 n) ~8 D+ ]parsfnm(line, &blk, 1);
# r2 D0 t2 ^! i# J% z; _. Nprintf("%d %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
! t* @4 n: X1 K! |4 ?<P><FONT color=#0000ff>/* request DOS services to create file */
! s% w* Y& k8 |0 F9 Pif (bdosptr(0x16, &blk, 0) == -1)
' T8 b0 A& J r7 R{
& ]4 N# h0 D' f) j. P O* Jperror("Error creating file");
+ N& d' E! I" V, f! J5 m0 `exit(1); " ?5 Q. S" J" }' o; E
} </FONT></P>, j# i: c- n- L' q; i J6 T
<P><FONT color=#0000ff>/* save old dta and set new dta */
, j/ k, H! Y% {7 {! K2 `& \! Q+ Csave_dta = getdta();
; ? {2 ^( A) a1 u- i0 [3 Rsetdta(buffer); </FONT></P>
1 z/ N8 Q/ E6 Y<P><FONT color=#0000ff>/* write new records */ 0 H5 B3 Z/ C7 s1 R
blk.fcb_recsize = 256;
$ n" _; ~& h @7 Vblk.fcb_random = 0L;
/ E( _/ o s! j& N1 f. r( Cresult = randbwr(&blk, 1);
3 k z, T) v$ R0 j% |$ @9 ]printf("result = %d\n", result); </FONT></P>! m- Y: \& ~. `' }3 b
<P><FONT color=#0000ff>if (!result)
/ m5 L% W* [: d! X' Y1 F; S: lprintf("Write OK\n"); / K9 N& J# v& _
else
7 d7 r- x9 K2 s' B& O{ . u7 V9 i/ g3 x% ^! z& t
perror("Disk error");
2 i' B. G% ~3 u9 Gexit(1); . i3 ]8 B6 i" u- }5 B
} </FONT></P>
0 \ i# C9 l3 b/ D( J<P><FONT color=#0000ff>/* request DOS services to close the file */ 9 a: W6 X+ \. C2 F
if (bdosptr(0x10, &blk, 0) == -1) & J9 W3 p8 r x: I; M5 T2 ]
{
c) `2 G) D4 R4 X- I Wperror("Error closing file"); . W! V! i2 i5 c y O1 _
exit(1); " m# j# A4 B6 J( @' [' I. q( d
} </FONT></P>
- C6 Y( U$ b8 E7 b# P2 K! ~ q<P><FONT color=#0000ff>/* reset the old dta */ - s, Z) p* Z9 B3 g. x+ w9 o
setdta(save_dta);
/ I/ G* i+ S0 [4 b& m) F+ a7 U; |& q. Lreturn 0;
' F0 r J0 n( z4 [}
# H* N3 i& ]8 M* B- S</FONT>
: d: V9 [9 C2 r; I9 f! c0 J</P>4 @: n0 e( b% Y5 e3 N* T: o' |8 q
<P><FONT color=#ff0000>函数名: setfillpattern </FONT>
7 O8 p* A- W. v& o2 v" b功 能: 选择用户定义的填充模式 3 o! `, Q2 Z3 n, \
用 法: void far setfillpattern(char far *upattern, int color); 7 m- u4 K3 o/ D5 J2 [- _4 _
程序例: </P>
) j! l. u. v/ A! d<P><FONT color=#0000ff>#include <GRAPHICS.H>
# [& B1 e5 U" B1 A#include <STDLIB.H>: s- I) E& |0 ]" R/ W# U2 [
#include <STDIO.H>
4 _! E( z4 O! \% i5 E#include <CONIO.H></FONT></P>
8 r; F; F& k7 ?( x; [) M<P><FONT color=#0000ff>int main(void)
+ K A. T M' [- s{ ! I: [" H s5 F8 I7 p t4 r
/* request auto detection */ + `2 j8 U: q7 B9 N2 V5 {
int gdriver = DETECT, gmode, errorcode;
0 n5 C, n6 U. H, z" e2 Xint maxx, maxy; </FONT></P>2 d4 j9 J+ Y# g5 o/ T7 |
<P><FONT color=#0000ff>/* a user defined fill pattern */ . d0 y" g0 W% T0 f/ ^& x
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; </FONT></P>( r% |( l1 l) o2 {3 W) f5 B
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 9 E2 v( |% I3 O
initgraph(&gdriver, &gmode, ""); </FONT></P>
# D: e; ~$ b" B<P><FONT color=#0000ff>/* read result of initialization */ ) z c: F" J7 c' m+ o
errorcode = graphresult(); 2 Z* N0 v H" w. M
if (errorcode != grOk) /* an error occurred */
* k% C, c# } C3 q{
9 d, I0 j6 {6 G J3 Wprintf("Graphics error: %s\n", grapherrormsg(errorcode));
% V- c* ?( D$ l6 K. r$ ]4 r9 Uprintf("Press any key to halt:"); ( ^: I$ D) I/ z3 h4 W" W5 T. D0 L# i
getch(); 9 \" R' H4 E' P4 }9 [
exit(1); /* terminate with an error code */
' u1 I3 {5 h! k0 O/ [3 e} </FONT></P>+ M0 P; k6 ?3 @6 E6 n
<P><FONT color=#0000ff>maxx = getmaxx();
! J/ w1 i2 i( G3 ]5 Bmaxy = getmaxy(); , V9 @6 h- N& b8 `
setcolor(getmaxcolor()); </FONT></P>( D$ @7 `+ l6 W* ]: F8 x
<P><FONT color=#0000ff>/* select a user defined fill pattern */
7 b' g* \$ G& c. E* ssetfillpattern(pattern, getmaxcolor()); </FONT></P>5 D2 ^4 L/ u+ V
<P><FONT color=#0000ff>/* fill the screen with the pattern */ 6 c) `0 P O+ U
bar(0, 0, maxx, maxy); </FONT></P>
# X( l) V6 V3 o \" Z<P><FONT color=#0000ff>/* clean up */
! O+ H& a3 y% U! D8 `getch();
: ~& K, P$ b, t# P, Dclosegraph();
, B. r; W, [4 L% o! }: oreturn 0; , _ K4 J. E# \5 \
} 0 O0 L/ v$ ?/ N
</FONT>& T) @1 O" E3 }) L( i
</P>
5 m' H0 E" l/ W0 |<P><FONT color=#ff0000>函数名: setfillstyle </FONT>: C) K. d7 x8 e6 r- h' }
功 能: 设置填充模式和颜色
, l( r9 I5 y, v+ j; `2 z" z用 法: void far setfillstyle(int pattern, int color);
" S& X6 _( a- p7 [; o程序例: </P>% I$ }( B0 l7 n. J. b) u/ P; C
<P><FONT color=#0000ff>#include <GRAPHICS.H>
0 P/ X. z f* u#include <STDLIB.H>
! I" l3 I0 o6 y) u; M% I#include <STRING.H>6 B8 n; ~1 R5 `6 k5 u7 D
#include <STDIO.H>" J. Q, Z8 b! ]9 P5 @
#include <CONIO.H></FONT></P>
1 G( o0 K S* F r0 H+ a9 v<P><FONT color=#0000ff>/* the names of the fill styles supported */ 4 e" X/ R) I, R8 s) F g* b
char *fname[] = { "EMPTY_FILL",
2 ^& g. F/ R: ~3 G+ H" N" k a( R"SOLID_FILL", 9 x; A# M4 x7 h' q! S
"LINE_FILL", 7 |7 @' c8 y; v0 g: D
"LTSLASH_FILL",
- t# R# y3 D4 [: Z, `9 V"SLASH_FILL",
. F7 a( O, U. ~"BKSLASH_FILL",
3 @# r U5 d4 D$ w9 \5 s8 W& V"LTBKSLASH_FILL",
1 q0 C. a1 q& ^6 W"HATCH_FILL", * @- {# Y* N& W0 Z: ]: t- o' n
"XHATCH_FILL",
: s t( N, H: j"INTERLEAVE_FILL", 4 r" x! I/ Q2 o) l/ _3 \) H$ J
"WIDE_DOT_FILL",
0 @# J! \0 e2 u- ~"CLOSE_DOT_FILL", / O% B9 p0 z$ h6 r' R
"USER_FILL"
t4 k8 Z$ }# R8 M/ r! o}; </FONT></P>* F5 n' q% p6 e0 |) }, ^( u
<P><FONT color=#0000ff>int main(void)
- d2 p. R5 @9 |3 }; S. m) Z) s{ ! G: B1 P. u" q1 E- k
/* request auto detection */ 6 H4 ?- P2 G/ u; b- u
int gdriver = DETECT, gmode, errorcode; 9 X; M% F3 g$ i7 a* c6 A" U
int style, midx, midy; - Q7 y* ^0 j# h$ V
char stylestr[40]; </FONT></P>$ Z" v K. A$ L, z6 R! L
<P><FONT color=#0000ff>/* initialize graphics and local variables */
, N" G- q: b1 ?& e* }* r/ minitgraph(&gdriver, &gmode, ""); </FONT></P>+ K7 w! S0 a8 h
<P><FONT color=#0000ff>/* read result of initialization */ 6 b# s" @) N' H4 T
errorcode = graphresult();
% n& H# W+ I. U8 v v, e# @% R9 N4 qif (errorcode != grOk) /* an error occurred */ $ P. L0 p8 h$ Q9 E" c" v6 {
{ " ~1 H$ \5 h* v# }
printf("Graphics error: %s\n", grapherrormsg(errorcode));
: c% B. |9 C! Nprintf("Press any key to halt:"); ! p" k* |$ _' s. H
getch();
2 N8 O! r9 A7 g# }, @5 `2 N% Eexit(1); /* terminate with an error code */
8 d& x- `) u% i% {1 M} </FONT></P>
4 X8 _4 o# O0 U% t0 I4 \# S3 y* t<P><FONT color=#0000ff>midx = getmaxx() / 2;
! M9 P$ R0 o8 ?: L' c3 P$ D& Z0 z J) rmidy = getmaxy() / 2; </FONT></P>
s/ x# l1 k( }* ?- m) H<P><FONT color=#0000ff>for (style = EMPTY_FILL; style < USER_FILL; style++)
, c/ D9 E4 ]4 h& {5 f$ Z9 y9 o7 F{ . {$ E3 j7 P3 J
/* select the fill style */ " r. j" p. M+ \5 G# j
setfillstyle(style, getmaxcolor()); </FONT></P>5 x! N8 Q, G. m! o( K0 t
<P><FONT color=#0000ff>/* convert style into a string */ ' l5 ]/ [4 O9 f
strcpy(stylestr, fname[style]); </FONT></P># D" T/ [0 s7 x; j2 J8 E3 {
<P><FONT color=#0000ff>/* fill a bar */ , o/ t+ H$ A7 B$ {8 d+ t- g
bar3d(0, 0, midx-10, midy, 0, 0); </FONT></P>
, T: h y! w9 W% ~# ^<P><FONT color=#0000ff>/* output a message */
8 V. w( e6 V0 V: f/ b# O8 \outtextxy(midx, midy, stylestr); </FONT></P>4 D5 q, h) t; ?' Q% L$ o/ N
<P><FONT color=#0000ff>/* wait for a key */
9 x/ V6 `, j0 w3 cgetch(); ; v* j: f+ t( C7 ^0 r' N" n0 V5 S7 O
cleardevice(); ( e) v; `0 F# s5 J8 Q! c
} </FONT></P>
! g( R+ U1 p+ c) w2 N2 i7 G! Q c<P><FONT color=#0000ff>/* clean up */
7 N1 A& @8 I' vgetch(); " w; }$ Z% f+ r+ Q2 g+ h
closegraph(); ! ?3 \, A% ]" i& Y$ }
return 0; ' r% | @2 f `! A: k
} </FONT>
& m' h. W7 a, I7 [4 L
( }1 W5 u; m8 f* Q' x+ b</P>: ^( M) K4 k$ ]2 b8 P/ t
<P><FONT color=#ff0000>函数名: setftime </FONT>( Y: A) P4 \* l2 v6 [4 |( p
功 能: 设置文件日期和时间 + I3 G u* `+ N& `( q$ M$ P
用 法: int setftime(int handle, struct ftime *ftimep); ' s9 d1 ]7 x. p8 I) t
程序例: </P>: \) `7 D+ N o+ @* f% N% _ N% _
<P><FONT color=#0000ff>#include <STDIO.H>
! N! h( t5 U. v4 `" p#include <PROCESS.H>5 `0 ], d9 I& t$ K2 F& F: C1 {
#include <FCNTL.H>6 G0 r3 X! r* ]$ n# s
#include <IO.H></FONT></P>$ M( G2 v n# f( S4 I% V3 L- Z
<P><FONT color=#0000ff>int main(void) X3 n+ h; m: n3 x% T
{
& a0 Q. t2 e( P9 V; @struct ftime filet; ! R' o# r- h) T) q
FILE *fp; </FONT></P>
' k: K6 U3 h# V' I) ~4 X& j i<P><FONT color=#0000ff>if ((fp = fopen("TEST.$$$", "w")) == NULL) 6 r4 ?; D! a$ c: r3 Q! F( w
{
4 a* a- N, a/ w: L, f: V8 y- Cperror("Error:"); ) U5 K) V/ s$ Z# ?+ Z2 D# A
exit(1);
! y. l% S1 a# S7 i+ J) R8 R% z6 q% m} </FONT></P>
, N; n4 E$ J: k! ?5 F<P><FONT color=#0000ff>fprintf(fp, "testing...\n"); </FONT></P>0 A9 T' j% N. E1 I; |# z7 f
<P><FONT color=#0000ff>/* load ftime structure with new time and date */
) B! O( s$ A9 P* ?+ f- U7 d: Hfilet.ft_tsec = 1;
d3 V6 s% P& h/ cfilet.ft_min = 1; 1 d6 K; D( `5 C- G
filet.ft_hour = 1; 1 D. e+ c; [; [4 z- R
filet.ft_day = 1; 3 w# f. k! d2 t$ n
filet.ft_month = 1;
/ R; e2 O2 ]2 K1 w1 sfilet.ft_year = 21; </FONT></P>9 E" H$ `* a2 I9 ^/ k) H& C6 V
<P><FONT color=#0000ff>/* show current directory for time and date */
3 F5 V. F" h7 a6 G. D t8 [9 fsystem("dir TEST.$$$"); </FONT></P>( J$ {( Z3 |, e& C& P7 i$ r: E
<P><FONT color=#0000ff>/* change the time and date stamp*/
- x, z0 `- H- _' d: C7 Zsetftime(fileno(fp), &filet); </FONT></P>$ G3 ]1 M' {8 Q0 Q3 h1 O0 O
<P><FONT color=#0000ff>/* close and remove the temporary file */
2 x) k3 r- T/ dfclose(fp); </FONT></P>
# i) K( z% o! N1 B& h<P><FONT color=#0000ff>system("dir TEST.$$$"); </FONT></P>
2 z: O1 V1 ^. q: S1 U) z- B. V<P><FONT color=#0000ff>unlink("TEST.$$$"); # G8 X) j! i3 g" g' i4 Z( F+ _
return 0;
( }0 ?7 J T" n2 v) \ O} 1 p, V* }3 T) F2 a
</FONT>% G9 I5 n9 s. L3 D. }% e0 ?( z
</P>
; ^, C9 Z/ Y/ A0 v9 M# p" B5 ^<P><FONT color=#ff0000>函数名: setgraphbufsize </FONT>7 e# O; D# }1 U9 _# h# Q
功 能: 改变内部图形缓冲区的大小 2 }+ k1 M" t: k5 g/ z0 |4 D
用 法: unsigned far setgraphbufsize(unsigned bufsize); ' G& Z' V+ z Q, q) N
程序例: </P>' }6 i6 t# X' [! h
<P><FONT color=#0000ff>#include <GRAPHICS.H>
0 K: o8 _2 ^6 ~3 b#include <STDLIB.H>( Q- k( i, }2 w
#include <STDIO.H>) m5 s1 a& g2 b0 ~
#include <CONIO.H></FONT></P>0 u; W t% Z3 Z$ X. i& t
<P><FONT color=#0000ff>#define BUFSIZE 1000 /* internal graphics buffer size */ </FONT></P>
: i7 Z% t0 D$ \$ F- v) J- {# K; \<P><FONT color=#0000ff>int main(void) ) t6 n0 S7 f8 ?& J1 a
{ : [. z4 q9 a! n: R' Y
/* request auto detection */ + B$ ~& m/ i! c, D7 I7 \* [
int gdriver = DETECT, gmode, errorcode; & \& w9 Z2 E8 m2 K, ^
int x, y, oldsize; $ @% H2 P- f! S' Z% ?+ z# F/ v
char msg[80]; </FONT></P>
( A) Q* \9 q# C+ n; ? d2 i<P><FONT color=#0000ff>/* set the size of the internal graphics buffer */ 5 F! u( k4 }/ b8 o( U
/* before making a call to initgraph. */ 9 x1 g4 ?* O" H0 V9 H
oldsize = setgraphbufsize(BUFSIZE); </FONT></P>" i9 ~/ F" j( m* \$ ~4 L
<P><FONT color=#0000ff>/* initialize graphics and local variables */
5 |" [4 m6 R3 `8 Z- Finitgraph(&gdriver, &gmode, ""); </FONT></P>( C- o+ Q+ l) T' ?1 g1 S1 E9 v3 B
<P><FONT color=#0000ff>/* read result of initialization */ 4 q, @! R! y/ ]' j+ k
errorcode = graphresult();
" p2 s# C2 H/ q" o' p/ _if (errorcode != grOk) /* an error occurred */ # c* K2 _ V+ G- V( ^) U$ z" I: K
{ 8 Z6 h1 L) R) G a
printf("Graphics error: %s\n", grapherrormsg(errorcode));
- z/ W: d2 ?$ h4 ?3 U; wprintf("Press any key to halt:"); % e* o0 l" j5 D, _
getch(); 4 Z4 Z) W2 ]/ _, ~) n) V
exit(1); /* terminate with an error code */ * b: h' x( S1 E5 W
} </FONT></P>* T8 z+ t. `6 Z( p% B7 O0 R
<P><FONT color=#0000ff>x = getmaxx() / 2;
6 m! ]* D# C ?" S. |6 {y = getmaxy() / 2; </FONT></P>6 p1 _& t6 m0 l) z
<P><FONT color=#0000ff>/* output some messages */
. q* d. j: X2 c& Tsprintf(msg, "Graphics buffer size: %d", BUFSIZE);
b1 ~" [) k+ \; r! B% R% asettextjustify(CENTER_TEXT, CENTER_TEXT); * [5 D: c0 E5 @% v! }# W
outtextxy(x, y, msg); ?- [& f" [, j! B1 {2 P, O
sprintf(msg, "Old graphics buffer size: %d", oldsize); 3 N) }# J5 r8 c) ?" S3 K) g: y
outtextxy(x, y+textheight("W"), msg); </FONT></P>
% A; i- a* p. `9 O+ h6 O<P><FONT color=#0000ff>/* clean up */ / i, o/ R6 H. q% ~* O
getch();
. ^% h0 L8 \* K$ o( ^6 V: p2 lclosegraph(); , s, j; e8 J; S
return 0;
; |2 v7 P0 c7 D% d} 8 H/ x8 p: r' n# `, Z
( g6 F& P! ^( z- J1 \1 [
) D3 a2 l+ I8 f</FONT></P>- E3 Z: Q- i9 z% R! ?# R
<P><FONT color=#ff0000>函数名: setgraphmode </FONT>8 e# c* Y& K8 R9 W4 I( y
功 能: 将系统设置成图形模式且清屏
' I& R) [+ Y6 f& [* C" W8 ^用 法: void far setgraphmode(int mode);
7 l; |- I1 o' Y: A程序例: 9 L& I5 y# N# \, \" v% j; y. ?
: V* B) x% G( ~. G& x/ H+ `
<FONT color=#0000ff>#include <STDLIB.H>5 N4 W* f) T* v5 p9 g/ H
#include <STDIO.H># ?: D: Y8 M8 H2 H0 O! E" ^, e' F9 t$ l
#include <GRAPHICS.H>#include <CONIO.H></FONT></P>; b7 n( m3 m2 i1 A4 Q8 l2 M9 z
<P><FONT color=#0000ff>int main(void) 1 H7 _6 A4 r% {+ Y3 m) a
{ 7 Q& l7 x# b+ d; R7 ?
/* request auto detection */
. Z) `- T I" ?int gdriver = DETECT, gmode, errorcode;
+ u1 P* p7 o- y2 w6 K- dint x, y; </FONT></P> Z( x, W* b# O" `
<P><FONT color=#0000ff>/* initialize graphics and local variables */
" a" Y% Q+ D) e- ?initgraph(&gdriver, &gmode, ""); </FONT></P>
% E5 z* N4 J1 b$ W<P><FONT color=#0000ff>/* read result of initialization */
1 J; r# ]) I; Z; m. q" gerrorcode = graphresult(); & N- j8 w: n# a% g2 z$ V @6 d9 R& g2 ?
if (errorcode != grOk) /* an error occurred */
8 y$ N7 z' G4 L- Y& ?+ c{
: e2 X- [# m* X+ x' \printf("Graphics error: %s\n", grapherrormsg(errorcode)); % P" z- z* A' B+ V+ ]; d) a
printf("Press any key to halt:"); 6 Z' _9 T3 A t D* S, L' f: q
getch(); ) k* Y9 `: o) P) Y2 q' P0 R
exit(1); /* terminate with an error code */ - h! C% f' H9 k5 U. D. j
} </FONT></P>
% a {( o6 G$ Z) [$ O1 \* B9 u<P><FONT color=#0000ff>x = getmaxx() / 2; 0 V- e9 @3 r4 ?. b0 @" W4 K
y = getmaxy() / 2; </FONT></P>
4 p- Q6 r1 D3 k1 V" |1 H& Y<P><FONT color=#0000ff>/* output a message */ ; B" L5 I3 R9 B
settextjustify(CENTER_TEXT, CENTER_TEXT);
5 x6 k: j K+ ], h [outtextxy(x, y, "Press any key to exit graphics:"); 6 B' Q. j2 Y: C; F
getch(); </FONT></P>8 r3 D! m4 k, j0 D$ f
<P><FONT color=#0000ff>/* restore system to text mode */ $ q5 s& A9 h+ X: L
restorecrtmode();
( K) l5 e6 t$ G! a! Gprintf("We're now in text mode.\n");
: G7 g& Y0 L J F: S9 Oprintf("Press any key to return to graphics mode:"); 9 `8 E) o$ `0 X8 Y* H6 B
getch(); </FONT></P>* O9 i- X$ y- i5 j+ Q
<P><FONT color=#0000ff>/* return to graphics mode */
/ \* \! M% ~' p/ G7 Msetgraphmode(getgraphmode()); </FONT></P>
* n/ B) a" x" Y8 f; B9 I, I7 t2 p% \<P><FONT color=#0000ff>/* output a message */ ( v$ r: P, f, [
settextjustify(CENTER_TEXT, CENTER_TEXT); 0 Z, c6 W; l1 U% g
outtextxy(x, y, "We're back in graphics mode.");
0 h9 e( v- ?9 L# F8 t2 Q. a) uouttextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>& D; l5 ~3 J7 P( m, w- R. j
<P><FONT color=#0000ff>/* clean up */
) ^: \7 b: l2 xgetch(); " z% B( e; Z3 N: C9 { S" m
closegraph();
1 j% t! C% T1 C/ |3 \* o. [return 0; E" w" L% h3 r; W0 C
}
% H+ W8 p# f- X' ~; ~7 c
# J6 k4 w; Z! |; {1 {$ Q
, X% h9 k* o' ?. ?( p8 Y1 u</FONT><FONT color=#ff0000></FONT></P>
5 c [: }+ G [8 e+ z( i<P><FONT color=#ff0000>函数名: setjmp </FONT>' @9 n4 V! _# ~6 C
功 能: 非局部转移 Q* R! p! U7 H9 P7 j! `! }1 X
用 法: int setjmp(jmp_buf env);
+ S& R7 j# l! ~ R# E程序例: </P>
3 c5 u9 R" v( ~2 i: f<P><FONT color=#0000ff>#include <STDIO.H>1 t- t% i6 a' `$ q8 O1 e
#include <PROCESS.H>( a+ Z5 Z( t; h7 K/ ~
#include <SETJMP.H></FONT></P>
5 w3 e# E! f( u/ U0 O<P><FONT color=#0000ff>void subroutine(void); </FONT></P>' ]. `1 b0 I8 c- Z [; R F
<P><FONT color=#0000ff>jmp_buf jumper; </FONT></P>) l o1 ?3 t7 t4 O4 Z6 i
<P><FONT color=#0000ff>int main(void)
+ m6 n$ t$ l' b7 d2 _' m5 U{ $ q% v$ l! s! D# ]" q& ~; m8 R
int value; </FONT></P>* j# f/ z4 M# F9 r
<P><FONT color=#0000ff>value = setjmp(jumper); 4 w# i% E+ X& ^# {# E. N; j( A3 Y
if (value != 0)
% i3 P- b" b7 n ?) c' i" p{ , S5 D9 d3 c$ R- F/ }: N+ s
printf("Longjmp with value %d\n", value); - [, M+ F K8 a1 G" M1 t# L
exit(value); 4 A x# R( E+ Q3 R
}
8 B5 i' b9 M8 Q3 ]* f Cprintf("About to call subroutine ... \n");
$ Z" G- C3 {! g P+ Gsubroutine(); . G. G4 o4 n: u5 c
return 0;
5 |$ ]8 b/ `3 c2 O7 E} </FONT></P>- ]! x% j0 z& l$ Z# J0 H
<P><FONT color=#0000ff>void subroutine(void) 8 S. g1 D5 `: C) j: I- s
{ . W5 p" k1 g" E9 v- q8 L& K3 f+ Q
longjmp(jumper,1); , f9 x- _- w/ N% K# Y( W9 O
}
+ m' C& g' \" N+ N6 L' u1 J</FONT>) u0 S2 t9 n9 i. u
</P>
% f# {! Y% o4 ]<P><FONT color=#ff0000>函数名: setlinestyle </FONT>2 G+ v. J6 K8 N x$ i4 j: b
功 能: 设置当前画线宽度和类型 & ?6 ]7 Q v3 N4 N
用 法: void far setlinestyle(int linestype, unsigned upattern);
4 f4 D/ D" i7 E# f5 R程序例: </P>
+ Y) Q. \( B2 Z9 L9 O5 f e<P><FONT color=#0000ff>#include <GRAPHICS.H>
) ]* V) A% k- i) Y0 C+ E#include <STDLIB.H>
: S5 ?& n) X* x, g0 i- o: c#include <STRING.H>1 K b2 }$ _9 v
#include <STDIO.H>
& J1 |% N9 | a' ]( g9 W#include <CONIO.H></FONT></P>8 Q$ X: @5 L# o
<P><FONT color=#0000ff>/* the names of the line styles supported */ 1 K! ?. }8 e! b) z9 |8 ~3 u! e
char *lname[] = { 5 e8 C5 _ n( k$ L. B5 Z
"SOLID_LINE", 4 X5 L. L) U7 b& K7 F" G
"DOTTED_LINE", ! M1 C& d1 @! o) C
"CENTER_LINE", : m. W; y: ?4 H7 I6 w2 ^# F
"DASHED_LINE",
( K2 [; N0 ~+ [- s7 x( B1 J/ g# R, X"USERBIT_LINE"
9 W5 b, B3 A3 o3 ^6 I+ k6 l. r}; </FONT></P>
0 M0 y8 T- f7 l& @4 g6 e% X5 Y<P><FONT color=#0000ff>int main(void) , [5 d0 |& h/ {$ p4 y* f2 s' Z
{ ( C U. K' U7 z! w: L
/* request auto detection */ 2 g% C9 ?+ t9 n0 L* U
int gdriver = DETECT, gmode, errorcode; </FONT></P>) z" P2 Q; _! x! p: |
<P><FONT color=#0000ff>int style, midx, midy, userpat;
5 F, O4 {$ D/ M% Y1 U$ j8 w# N* ichar stylestr[40]; </FONT></P>0 C5 P0 M$ z2 Y. B; Y3 M. u
<P><FONT color=#0000ff>/* initialize graphics and local variables */ $ g4 a- U/ {, b) P6 p( l" Y$ |7 h
initgraph(&gdriver, &gmode, ""); </FONT></P>% R3 ? _/ N: j; u. l5 h/ S% ~2 T# W
<P><FONT color=#0000ff>/* read result of initialization */ / L' R5 l. B R& _/ n N
errorcode = graphresult(); 3 ~5 a. w7 |8 y8 Q8 O2 l
if (errorcode != grOk) /* an error occurred */ 1 Y4 T. H3 [$ W1 W9 I, j# ]
{
+ O! f+ {% u- W1 D! [printf("Graphics error: %s\n", grapherrormsg(errorcode));
+ u9 E$ L1 \, Cprintf("Press any key to halt:");
# }, h. B' k6 C, ngetch();
' b, A& x, D* k) u6 m8 Fexit(1); /* terminate with an error code */ % t; |# n, K: f, F( i
} </FONT></P>
" v* v' I& T; I! F, D* H<P><FONT color=#0000ff>midx = getmaxx() / 2;
* j& f. w0 J( Y4 C6 Kmidy = getmaxy() / 2; </FONT></P>
! S2 ]3 p, o! V# Y W% D0 n<P><FONT color=#0000ff>/* a user defined line pattern */
) n s/ j, K0 Z" g$ d3 q, k/* binary: "0000000000000001" */
/ G$ y( Z" ~' R1 r1 luserpat = 1; </FONT></P># H8 {( w0 p% j
<P><FONT color=#0000ff>for (style=SOLID_LINE; style<=USERBIT_LINE; style++) 1 n/ j1 S) _% U
{ 5 U* }, c d, O
/* select the line style */ & |! v: f' ?& y; ]7 ~$ |5 _
setlinestyle(style, userpat, 1); </FONT></P>
R4 F+ |' o, W- n/ j<P><FONT color=#0000ff>/* convert style into a string */
( Y, M4 Z) u4 U6 _0 [, ^* [/ Astrcpy(stylestr, lname[style]); </FONT></P>
. |( Q. r0 {4 N<P><FONT color=#0000ff>/* draw a line */ U5 H* h7 M# Q, p$ ~8 i& c
line(0, 0, midx-10, midy); </FONT></P>
+ j* ?+ X' d, G& f, B7 h% N<P><FONT color=#0000ff>/* draw a rectangle */ ' V2 r4 e! C- a8 ^3 t1 G
rectangle(0, 0, getmaxx(), getmaxy()); </FONT></P>
7 B$ W2 E& y1 {<P><FONT color=#0000ff>/* output a message */
$ P( v3 B+ \4 E' `/ souttextxy(midx, midy, stylestr); </FONT></P>
; H/ {& X: K. ~3 t<P><FONT color=#0000ff>/* wait for a key */ ; E* n5 _9 m/ N0 |: Z, B! d7 T% \
getch(); 1 N9 e, E; {, G' ^7 l* K9 w
cleardevice(); # s$ x7 v5 X L* ^8 A
} </FONT></P>
3 S( c& z+ b6 Q# o: t) T# W<P><FONT color=#0000ff>/* clean up */
f: u# e) W. z+ H$ s. @closegraph();
1 c% q; u2 K6 A) L5 preturn 0;
4 M0 \* g5 g9 e% g0 ]} </FONT>
+ a8 @' O% |9 R0 c+ h- X
. A; |/ `& F: _& h$ U1 l4 _) F
</P>
2 _8 C" e4 Q8 Y! N<P><FONT color=#ff0000>函数名: setmem </FONT>
) E6 h; R5 C5 x8 x4 B- l! I( o/ F功 能: 存值到存储区 4 {" @$ l4 m+ |
用 法: void setmem(void *addr, int len, char value);
% N4 k; [$ B5 T, x0 N程序例: </P>3 M4 b5 e( |8 d: h3 F9 J6 W
<P><FONT color=#0000ff>#include <STDIO.H>4 O$ J$ b* O0 `7 m* s+ b
#include <ALLOC.H>: s5 k( Z8 k& L j
#include <MEM.H></FONT></P>" w- z+ @6 u Y& W9 s0 [# b" [( @
<P><FONT color=#0000ff>int main(void) # F9 ]; ?$ H$ y- R: w1 l3 k
{ 7 i' r3 X8 O" S7 Z- e8 p
char *dest; </FONT></P>7 u4 d. N2 z' z4 T& r
<P><FONT color=#0000ff>dest = calloc(21, sizeof(char)); # l2 V$ i$ H* ?8 P j( C
setmem(dest, 20, 'c');
" |7 u1 F" L+ ~, d$ e2 L/ bprintf("%s\n", dest); </FONT></P>$ w& x8 I3 t5 V7 T/ d6 \
<P><FONT color=#0000ff>return 0;
5 h$ V, ?9 M. e- d2 @: K: P$ P' {} </FONT>
( D% o2 M5 @7 |4 [' S2 c: F3 B/ P$ y$ L% J, Z
C5 n$ k7 R3 n( q) ]) i' p8 N. ^+ m
</P>
, F. j5 b& \0 m' j$ m9 A: v7 }: R<P><FONT color=#ff0000>函数名: setmode </FONT>
; W; }/ U% Q5 J; @1 h% e功 能: 设置打开文件方式
/ v; B5 x4 U- f# ~. `用 法: int setmode(int handle, unsigned mode);
$ X; [+ `, r' s8 v B6 n) X程序例: </P>* q/ x7 y) S: ^7 p9 d( _
<P><FONT color=#0000ff>#include <STDIO.H>
% X/ z5 A0 B# p5 b% ] N#include <FCNTL.H>* h3 Q+ d2 ~8 I; ?; M
#include <IO.H></FONT></P>
1 x6 s$ G3 f0 T) u1 [<P><FONT color=#0000ff>int main(void)
- T( _$ H" D! W. m* L6 f& v+ o{ " Q( J$ J7 D* K: W& w3 t2 a
int result; </FONT></P>0 s* d7 `5 p1 C: a7 n2 p
<P><FONT color=#0000ff>result = setmode(fileno(stdprn), O_TEXT);
. i' b6 S5 Q# ?, wif (result == -1) ! ^5 {( ]! N# X6 \$ B) n0 d9 o$ {
perror("Mode not available\n"); : ?$ p7 p @, F" w* f8 M9 w; g
else ; N \1 N! x( Q; C/ t# ^
printf("Mode successfully switched\n"); 5 p8 E5 _( _ b! w6 ^
return 0; 5 K" L1 A/ _* t5 e
} + e1 T1 V4 L$ @5 Z- y
x; B! w, M4 H- p3 A
</FONT>/ } N) ^# a2 l
</P>0 o( ~+ K s8 y1 w- d! M
<P><FONT color=#ff0000>函数名: setpalette </FONT>
/ ~7 {: I& f! X, g; o3 ?功 能: 改变调色板的颜色 ( @! C" e- P- T& Y
用 法: void far setpalette(int index, int actural_color);
x& C: N. m& c9 B) G程序例: </P>
. W8 }7 P H' J' K" l0 L% v<P><FONT color=#0000ff>#include <GRAPHICS.H>$ e% y, M y* f* M" ?
#include <STDLIB.H>
0 D+ Q5 g* o2 |. Z4 ?7 U2 k; F#include <STDIO.H>
0 `, y% U3 M$ n" b#include <CONIO.H></FONT></P># w* O: x$ `9 c: C, z6 R) w
<P><FONT color=#0000ff>int main(void) / F. s) g1 r* n- H) B. i N- F
{
# H9 `8 w2 s( W/* request auto detection */ - [* n+ ~: |) E
int gdriver = DETECT, gmode, errorcode;
[9 u X1 _' A& C jint color, maxcolor, ht; ; {5 h5 X' S: [/ c7 T
int y = 10; . L6 H; U" r% _& ^
char msg[80]; </FONT></P>6 Q8 o4 M# n5 |0 i
<P><FONT color=#0000ff>/* initialize graphics and local variables */
0 c& O- z4 ]4 Y# [/ A6 {4 o4 Tinitgraph(&gdriver, &gmode, ""); </FONT></P>
2 V$ q2 d% A. E' w! y4 h" ? f# ^<P><FONT color=#0000ff>/* read result of initialization */ 8 B# e, O4 r) F4 F9 }, X3 o2 X
errorcode = graphresult(); , J# i/ [% i. l8 n4 |# c" G
if (errorcode != grOk) /* an error occurred */
+ s- w5 H- a! C6 V% N2 | k{
8 c- |3 |* o& iprintf("Graphics error: %s\n", grapherrormsg(errorcode));
2 t4 u- R2 u# n ^0 [$ ~1 o6 ]printf("Press any key to halt:");
% C4 ]' \ K+ I# x. mgetch();
1 ^+ X& ^: p& n4 G0 x) Wexit(1); /* terminate with an error code */
, @1 Y9 n+ `8 f( A# c) [7 K} </FONT></P>
8 x# y5 l7 L2 j F, }* ^% ^<P><FONT color=#0000ff>maxcolor = getmaxcolor();
- w' ?9 a6 ]: @* hht = 2 * textheight("W"); </FONT></P>! Y, m1 _: M5 ?& o7 G; A; `' { {
<P><FONT color=#0000ff>/* display the default colors */
^- g2 Q2 U" A8 c g% ufor (color=1; color<=maxcolor; color++) " Y( v6 {/ c' l- \" S- @8 R. O
{
; ]$ R2 t+ L5 h4 }1 L" Isetcolor(color);
, Z! G% v) o$ j9 }; ~ B9 B0 ]* ~sprintf(msg, "Color: %d", color);
1 D% P6 ?$ L+ o) K, a+ e5 Iouttextxy(1, y, msg);
! z$ W, ^) M0 H2 [y += ht; 5 ?% B& H% `% \* w4 M
} </FONT></P>2 h3 s" H7 s/ b$ U4 \8 U# J
<P><FONT color=#0000ff>/* wait for a key */ 8 i, X* C4 o8 ~7 H$ X0 g ?
getch(); </FONT></P>
* I" ?/ p* c+ E E+ y w1 i<P><FONT color=#0000ff>/* black out the colors one by one */ & s4 q; q, u/ a, v
for (color=1; color<=maxcolor; color++) / J! N; r! V, @2 w. l( B/ W& y
{ % ?9 ~& r0 n! M' f: [3 Z8 l
setpalette(color, BLACK);
' B- d3 ?. I6 p m/ x! [getch();
7 B5 x* t" f; |8 F! H6 k1 ?2 @} </FONT></P>1 G1 e$ q$ B3 E0 v
<P><FONT color=#0000ff>/* clean up */ " I1 N8 Z$ \8 ~/ c8 y+ y
closegraph(); 6 {, [8 N; _$ h* G9 b' m2 O
return 0; 3 {5 [, s1 d; t% p2 C+ p& d7 L. F
} / P- X( ?$ J7 ?0 K* k
</FONT>7 ?+ U# m' m9 ]* o. @4 {7 ~
</P>
; a/ T& C5 Q: I. f, p* f' s<P><FONT color=#ff0000>函数名: setrgbpalette </FONT>
/ v' E* E) K6 B& p6 b g功 能: 定义IBM8514图形卡的颜色 & {% R! ?; c4 b: @. y
用 法: void far setrgbpalette(int colornum, int red, int green, int blue); & G5 X; I/ W& n! f8 \' \
程序例: </P>
* R: z& ~3 a+ s, v0 h<P><FONT color=#0000ff>#include <GRAPHICS.H>
Q1 y4 U3 m- n5 x: \#include <STDLIB.H>
" O! S0 _1 S7 v; }, C#include <STDIO.H>
# D1 \" U/ S0 {: M( @$ y/ S: T( ~. t$ |6 D#include <CONIO.H></FONT></P>
0 N H" q" ^, Z" x: u5 p6 z<P><FONT color=#0000ff>int main(void) + ^+ M7 [$ K! o# i- u& h
{
! K5 N, l8 N. M/* select a driver and mode that supports the use */ 3 L. N/ d; S% y9 O k& i# T9 u
/* of the setrgbpalette function. */
3 D0 e$ k5 X0 o2 R8 c+ b5 o8 L, jint gdriver = VGA, gmode = VGAHI, errorcode;
" P# M h1 f8 w x$ w& dstruct palettetype pal; % Z( N' I+ m$ W1 T
int i, ht, y, xmax; </FONT></P>
9 G/ k t! T3 a' {# }<P><FONT color=#0000ff>/* initialize graphics and local variables */
: {* y$ B- @7 t) p8 C$ V7 q5 sinitgraph(&gdriver, &gmode, ""); </FONT></P>
3 H/ j1 E5 l& O5 ~<P><FONT color=#0000ff>/* read result of initialization */
; X1 \) Y& j# A& l/ d& K; M8 cerrorcode = graphresult();
9 Q% G8 B2 w* I; k5 I- \if (errorcode != grOk) /* an error occurred */
! ^' |% Q3 W0 }/ @{
: l/ k; L( M8 oprintf("Graphics error: %s\n", grapherrormsg(errorcode));
: U; d: {2 @0 F; f) P, i% Cprintf("Press any key to halt:");
9 [- [5 c+ u0 h" m6 B2 ^4 lgetch(); $ M6 c; U Z* n7 K; t4 e# z
exit(1); /* terminate with an error code */ , Z- v$ T3 ~. ]) d# ^" T% h
} </FONT></P>; [" [% Z3 b% r: } u; I
<P><FONT color=#0000ff>/* grab a copy of the palette */ 8 C3 M. m6 q/ S! K7 G( o
getpalette(&pal); </FONT></P>
( f$ r; f: ]. K<P><FONT color=#0000ff>/* create gray scale */
+ K5 x8 A" N/ n. o8 V4 I t+ U- Pfor (i=0; i<PAL.SIZE; <br i++)> setrgbpalette(pal.colors, i*4, i*4, i*4); </FONT></P>3 X, ]# c! w" [$ i' Y5 E/ R
<P><FONT color=#0000ff>/* display the gray scale */ $ M/ `1 c% z; L/ A5 L
ht = getmaxy() / 16;
4 j! p4 Y( n) I5 kxmax = getmaxx();
, w* Z0 H8 i- \# ]) X) uy = 0; $ @' v) n, p9 u4 |! M
for (i=0; i<PAL.SIZE; <br i++)> {
: L' t) L" C6 L& Osetfillstyle(SOLID_FILL, i);
' e3 O$ s7 w/ z; ebar(0, y, xmax, y+ht); ; v! y( ^3 ?5 e8 Q: U- W; v
y += ht;
^9 e+ s- x; k5 X3 J- s/ c2 K6 w} </FONT></P>2 {4 t+ \) a& i7 P4 @! F
<P><FONT color=#0000ff>/* clean up */ $ z' V2 L( e6 l# ^/ N
getch(); & G1 I3 Q$ r% {( e* I6 M. m
closegraph();
/ e- e7 H1 E. p9 E! Y: |; }0 n5 sreturn 0;
, Q# l! i$ k, L' f3 C} , s" o# g$ i* N# R6 d: N
</FONT>% ?& ~6 O' l6 f; L F: ?
2 X- x% M+ o5 e7 X7 ^* N2 L; @* H
</P>
' }" b* o# U) C<P><FONT color=#ff0000>函数名: settextjustify </FONT>
: F/ t5 e1 J/ S8 _; q功 能: 为图形函数设置文本的对齐方式 . v+ s( O; p- u5 x) j
用 法: void far settextjustify(int horiz, int vert); . s& E" j# Z' W
程序例: </P>
- k5 t! q- C k8 g0 y<P><FONT color=#0000ff>#include <GRAPHICS.H>4 K9 r' j% q" c
#include <STDLIB.H>
9 J% Q# e) _' ?) B% m, T#include <STDIO.H>
1 z. h. Q# K! Z#include <CONIO.H></FONT></P>/ Z9 L% T* v( q8 H( i/ A1 J/ E
<P><FONT color=#0000ff>/* function prototype */ ' k$ e3 C+ X V+ M; i8 m+ O
void xat(int x, int y); </FONT></P>
8 b. i5 h4 J8 Z6 D* O5 e- e& j<P><FONT color=#0000ff>/* horizontal text justification settings */
5 f' C9 s3 [; Ychar *hjust[] = { "LEFT_TEXT", " }# z! a1 }8 q, |( l% l
"CENTER_TEXT", + L R5 d$ H4 f
"RIGHT_TEXT"
% y3 ?9 o t+ ?! Z( j}; </FONT></P>
# Z5 e8 l* k: |<P><FONT color=#0000ff>/* vertical text justification settings */
- ^, N- v! Z" g+ I- p$ v# i. i2 ~char *vjust[] = { "LEFT_TEXT",
# G- ^* \: @: u( ]" i4 F7 {& I5 E* L"CENTER_TEXT", * ^: V8 H/ W- k& j) i: t4 W) d
"RIGHT_TEXT"
! t/ }3 C5 a& ~ K0 z5 O}; </FONT></P>
5 W, W5 ^; c* \: A1 K/ D9 @<P><FONT color=#0000ff>int main(void)
" h& D: { f/ S{ 3 K1 R8 C6 {& f" D
/* request auto detection */ 4 v, p, z& N" r+ R) x
int gdriver = DETECT, gmode, errorcode; * O+ t( k) e/ G' a0 S/ X
int midx, midy, hj, vj; & L/ ^/ P5 _3 _: P9 `# n) ~
char msg[80]; </FONT></P>
. G% n* Z& L/ J4 [7 J2 O: w! W<P><FONT color=#0000ff>/* initialize graphics and local variables */ . N* i: K( `$ y) b! _& J
initgraph(&gdriver, &gmode, ""); </FONT></P>( O& H: A7 {: L! e; c5 W" P/ n
<P><FONT color=#0000ff>/* read result of initialization */
4 e5 z% N% ?! C6 `errorcode = graphresult();
& I0 r3 {" ?8 o1 ]$ x' g; iif (errorcode != grOk) /* an error occurred */
4 I5 e! b) t0 o0 P. ?3 M( p{
* E& S, ~3 e( R- `printf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 ?9 A [* G- N; g+ i S# ^% @
printf("Press any key to halt:"); 6 d4 A% }; L5 B, r7 k g
getch();
, K4 c7 A4 l* aexit(1); /* terminate with an error code */ , x& W/ B- k# {9 Z- \; P* Z8 T
} </FONT></P>' [; Z# o% u, Y( _# _
<P><FONT color=#0000ff>midx = getmaxx() / 2; " q1 B% |/ p: J5 l
midy = getmaxy() / 2; </FONT></P>
- I$ U! x" G, l" W& f4 E3 R. r# {<P><FONT color=#0000ff>/* loop through text justifications */ : P5 {; p7 z$ R4 C* l5 G- J& E0 q
for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++) " i) y0 n* T- A! y( U
for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) 4 ~* @- e# P% R" z( ^
{
8 A; Z H Q* v; R. M- mcleardevice();
& {- r) O4 ]/ z1 }% X* O; c/* set the text justification */
' T2 v0 C, w. N, H+ f l5 O6 L* wsettextjustify(hj, vj); </FONT></P>) ~. i Q1 c+ e9 V+ |
<P><FONT color=#0000ff>/* create a message string */
' y. P' ], I% R6 Bsprintf(msg, "%s %s", hjust[hj], vjust[vj]); </FONT></P>1 F% D/ }" X: t; {
<P><FONT color=#0000ff>/* create cross hairs on the screen */
/ I9 W9 ~* f0 e, b) j z2 Nxat(midx, midy); </FONT></P>2 G* G3 @/ y( T6 n$ ]
<P><FONT color=#0000ff>/* output the message */
; K& W. t% S9 A/ E/ X' vouttextxy(midx, midy, msg);
& j5 B& O; y+ y1 j% igetch();
# a: U4 f% A, |0 l} </FONT></P>; W9 O; _+ i- f2 K& q; j, K
<P><FONT color=#0000ff>/* clean up */
1 v1 [* r/ u, \9 |1 dclosegraph(); % t+ a1 n2 X. s. _
return 0;
- h; o: Q" X a& x} </FONT></P>
% \3 N! L. y+ }1 d/ v" z<P><FONT color=#0000ff>/* draw an "x" at (x, y) */
8 G% J: O; p" j/ P9 O. i3 d0 ~: Bvoid xat(int x, int y) 4 e2 I, @: G: b/ F6 m6 ?
{ 3 ~; w- k: o! b. S( y
line(x-4, y, x+4, y);
/ }& c5 ~! ^! z4 d" r: xline(x, y-4, x, y+4);
5 t' K9 W J) D$ {} </FONT>
, f( u0 C9 {4 m j1 l! C4 r" R7 v @" a: w+ j) g# c
</P># N' |) G4 a6 }4 q
<P><FONT color=#ff0000>函数名: settextstyle </FONT>7 N" M9 {4 b4 [- T; a
功 能: 为图形输出设置当前的文本属性
# v9 Y4 B0 k- d/ V用 法: void far settextstyle (int font, int direction, char size); + H' Q) z% t& o# ]9 O! K% V
程序例: </P>
. W }# e+ p; _# i' t4 Z7 Y<P><FONT color=#0000ff>#include <GRAPHICS.H>
4 c. }2 |: m5 v% f4 _; a#include <STDLIB.H>
8 ]% B' Z7 l; s( `8 ^: z) m8 ^7 n1 d#include <STDIO.H>
# N# x6 x& _* Y D#include <CONIO.H></FONT></P>
X, p6 ^8 e m& V9 P<P><FONT color=#0000ff>/* the names of the text styles supported */ : J7 T' r7 Y Z0 S6 c
char *fname[] = { "DEFAULT font",
/ r/ F, u1 G$ {+ V4 t0 I+ |"TRIPLEX font", ( `- X8 B/ r% o3 ?; H5 ^& u
"SMALL font",
$ K: p" s* F. Z: n"SANS SERIF font",
) F: q: t: T% y/ f( s+ O0 z3 b. i, W"GOTHIC font"
: m) w( \, r8 g1 Q! L. o# l. R}; </FONT></P>6 S$ s% T5 p5 H2 b$ {; w3 t0 O
<P><FONT color=#0000ff>int main(void) , \6 W) L; N- B& s4 L
{ , S* B: O4 M$ A; g5 @
/* request auto detection */
I+ `4 ]8 t: R1 O6 q9 `int gdriver = DETECT, gmode, errorcode;
( N2 F6 ^ a% e0 E% k, mint style, midx, midy; % R8 I! I8 U) y
int size = 1; </FONT></P> A9 L' u8 O) a1 R v& G& B# E
<P><FONT color=#0000ff>/* initialize graphics and local variables */
0 h. c3 ]3 r4 N# I" L) E3 g" ninitgraph(&gdriver, &gmode, ""); </FONT></P>
$ K( S. a. i. r- s! p8 R. Y4 W<P><FONT color=#0000ff>/* read result of initialization */ " {: l* {) v% a n/ X
errorcode = graphresult(); " u$ x D' {8 B; M+ R' L
if (errorcode != grOk) /* an error occurred */ ! D* P* f% ^; p" L2 ?9 _4 t( Y' M
{
}6 F/ k+ X" {printf("Graphics error: %s\n", grapherrormsg(errorcode));
4 t( Y% F* d0 |: oprintf("Press any key to halt:"); 1 B% u# M, U2 Z5 I& B8 a$ k
getch();
5 N% w0 v6 w! s5 q% [3 [0 V; U# {exit(1); /* terminate with an error code */
! R* l- I4 V8 t6 q1 H} </FONT></P>
3 t" R" `5 }. m' Y% p<P><FONT color=#0000ff>midx = getmaxx() / 2; # y8 G0 g4 ^5 y: {
midy = getmaxy() / 2; </FONT></P>4 c" }8 Y. ?: v
<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
+ k" K9 h/ n6 w3 @4 h3 V<P><FONT color=#0000ff>/* loop through the available text styles */
- a- d, ~& m Q/ Tfor (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
. `. w/ T. |% H: l9 b& \- U3 o{ , Z: {8 g j/ |* S N3 w- ?% d6 ^; L9 i
cleardevice();
0 B4 W2 }( j C1 v; z3 Aif (style == TRIPLEX_FONT)
- ?4 P/ `1 ~+ `0 W9 Bsize = 4; </FONT></P>0 u4 v( Q7 I. z8 c: i5 B2 y
<P><FONT color=#0000ff>/* select the text style */
. e% \' `- z3 i* K- Ssettextstyle(style, HORIZ_DIR, size); </FONT></P>
6 {/ Z! t; G" m* s* g$ C$ V: M<P><FONT color=#0000ff>/* output a message */ , M) [* G' R# \7 `8 q
outtextxy(midx, midy, fname[style]);
+ G, W8 W5 S3 `* m# R! e( egetch();
3 B- p1 u" u/ y5 Y} </FONT></P>
1 e- }' j& }2 ?- V<P><FONT color=#0000ff>/* clean up */ ' t: S- a3 x8 ]7 E1 X% r+ ?9 }
closegraph();
1 H$ Q& y$ r$ Qreturn 0; 6 z$ w: p/ J1 a; e0 I4 S- m
} ( o% X/ a+ K. o5 j( U) @! T
</FONT>$ z: C; M# c3 A, { }2 B3 ?# \
</P>
( p1 _, M2 C: q2 B<P><FONT color=#ff0000>函数名: settextstyle </FONT>3 @ G! M$ V$ E/ g' C
功 能: 为图形输出设置当前的文本属性
7 u. @4 h* \- J0 I7 u用 法: void far settextstyle (int font, int direction, char size);
7 @, }' U: t( ~* y2 D9 N, n程序例: </P>
3 Q0 q9 `$ m4 D<P><FONT color=#0000ff>#include <GRAPHICS.H>
' F. S& [) k) ]' J) [7 E4 W#include <STDLIB.H>
: d/ G! s" h8 u#include <STDIO.H>8 c- f! M, S' H* ~. X+ N
#include <CONIO.H></FONT></P>
: [& @" V t$ C6 J. N<P><FONT color=#0000ff>/* the names of the text styles supported */ * f( [$ z# t* V) ]
char *fname[] = { "DEFAULT font",
3 _+ W' J4 V r$ m"TRIPLEX font", 4 R# ^& s; X: C2 I" {' r. K
"SMALL font", * x8 n8 _8 W$ }3 H# F, V
"SANS SERIF font", * Y+ D( O( q. h: l8 ~+ E% \
"GOTHIC font" : P$ H6 h& P. X* k, v
}; </FONT></P>
9 c- @0 _ K8 k4 N) i7 w<P><FONT color=#0000ff>int main(void)
}9 s* z5 f. |. Z8 M0 f1 W{
# D' U8 H( i7 e/* request auto detection */ ' S4 D3 r" w. M2 S
int gdriver = DETECT, gmode, errorcode; % |- @' D4 @- b/ L
int style, midx, midy;
7 W# y1 `: V% K( |: N6 O8 D% V* Tint size = 1; </FONT></P>
5 \* i1 J3 I* J1 @<P><FONT color=#0000ff>/* initialize graphics and local variables */ : e; _! G" s7 x' M k
initgraph(&gdriver, &gmode, ""); </FONT></P>
( b/ ]) s0 b: n# G9 ~; K" |<P><FONT color=#0000ff>/* read result of initialization */ / i3 z/ R" `9 _% Q
errorcode = graphresult();
+ l6 G. T& z* ^2 p2 M3 T4 n- J% kif (errorcode != grOk) /* an error occurred */
9 z' N* i5 j! E9 G{
# h/ f( @4 W/ m5 Nprintf("Graphics error: %s\n", grapherrormsg(errorcode));
; f, l% D6 D3 oprintf("Press any key to halt:");
0 \$ E* L9 I" c) Vgetch(); $ A' s- [5 u- R
exit(1); /* terminate with an error code */
/ g c [: K! }0 `: {/ x: K& Z: I/ g} </FONT></P>0 r, }/ s/ |" l, U
<P><FONT color=#0000ff>midx = getmaxx() / 2; 2 Z8 x1 W' C6 e4 s1 `
midy = getmaxy() / 2; </FONT></P>
4 _3 |* W6 N( @+ y4 {<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
8 ?3 T0 D2 {2 f# @<P><FONT color=#0000ff>/* loop through the available text styles */
W9 w$ M8 u! b0 ^4 W7 F1 {) ffor (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) - H q E n, a! y
{
1 W, }) |! i, O9 O- c7 Acleardevice();
/ R0 ^& _9 W0 w7 M4 rif (style == TRIPLEX_FONT) " D3 Q! u! C- `; A8 m M6 H
size = 4; </FONT></P>3 a1 K) h2 V0 n! `0 A- e. z; B
<P><FONT color=#0000ff>/* select the text style */ ; _7 P5 j' Y+ l
settextstyle(style, HORIZ_DIR, size); </FONT></P>
4 t: l3 ~% K. G<P><FONT color=#0000ff>/* output a message */
9 Q, i2 j+ [& y" Fouttextxy(midx, midy, fname[style]); : u5 G- m. P; M# a
getch(); / O, c; Y! F! a9 J6 y6 K
} </FONT></P>
2 Z' u* O! Z& _. d" {1 U! |<P><FONT color=#0000ff>/* clean up */
" N. S9 n( y% ? e* d- y" R4 _closegraph();
w8 b" m# `5 ireturn 0;
0 z0 E0 h1 ^# ~. k2 A} </FONT>
: l9 P* D0 t% i: v' y3 T
2 b4 U4 T# L; N: J</P>
& L0 s+ q) P% o, S4 M3 I3 G5 F<P><FONT color=#ff0000>函数名: settime </FONT>
" P5 ?1 g% @2 Y6 J$ U, K) e功 能: 设置系统时间 # Q. {7 w8 B( [; N1 h& D S
用 法: void settime(struct time *timep); ) \& r! e. ~$ ]* U3 m0 g
程序例: </P>2 w" ~ a: G6 k9 ~+ Q2 W5 o9 f4 h
<P><FONT color=#0000ff>#include <STDIO.H>
# \ l+ o6 E. m9 P# ?#include <DOS.H></FONT></P>
$ |: D, `1 d5 E9 G) A1 i<P><FONT color=#0000ff>int main(void)
4 m$ p6 |' P7 l' ?) Y/ T6 p{ ; B4 W. r' j, Y3 R: L& k
struct time t; </FONT></P>
m$ V: W ]# R2 w<P><FONT color=#0000ff>gettime(&t);
5 _. W x+ ?: Q3 ?3 [printf("The current minute is: %d\n", t.ti_min); 9 j8 J4 G: i+ G& X& V5 N, D$ b1 Q
printf("The current hour is: %d\n", t.ti_hour);
+ K; t$ n% z( Kprintf("The current hundredth of a second is: %d\n", t.ti_hund); . R, P% q! k- m: {- Q7 p% |0 r" Q+ B
printf("The current second is: %d\n", t.ti_sec); </FONT></P>
1 o* J( X3 a# L6 E2 S" U8 X+ ~6 L<P><FONT color=#0000ff>/* Add one to the minutes struct element and then call settime */
% }6 D5 s j4 j/ n( tt.ti_min++; 3 ^1 F' v3 E- \$ P( j9 P$ j. Q& M
settime(&t); </FONT></P>8 I1 }! j7 L: \; b# N2 j
<P><FONT color=#0000ff>return 0;
& p# a4 ^9 A4 f0 d} </FONT>" A: e$ i+ z8 |) ^( S0 j s
1 g0 ]9 }4 e% F2 @
</P>5 p8 e) ~; H, @3 W3 \5 ^* }/ ]
<P><FONT color=#ff0000>函数名: setusercharsize </FONT>
* B/ v4 I& D+ Z/ |5 _' f$ r功 能: 为矢量字体改变字符宽度和高度 1 L# Z9 A3 Q) H( P/ G8 u; s
用 法: void far setusercharsize(int multx, int dirx, int multy, int diry); 4 V& C1 J$ j x- j- @
程序例: </P>
1 ]# R: t# `8 t<P><FONT color=#0000ff>#include <GRAPHICS.H>
1 m! _, C8 P' s: u8 U5 X* a# w#include <STDLIB.H>
8 y, r) m; m# R* q#include <STDIO.H>8 R3 J" o4 Q' t2 F6 H1 Q# s
#include <CONIO.H></FONT></P>
6 m) _9 M; J# j1 k- I# ]4 |<P><FONT color=#0000ff>int main(void)
" t# G) L# o" |, b7 ^) N) W) g{
0 h; c* L# V# F. X- j- |8 e. @/* request autodetection */ # }" i1 m! A* ^* ~0 E
int gdriver = DETECT, gmode, errorcode; </FONT></P>
& B9 G/ w. E. g5 R2 g2 z3 g. L) H+ v$ c<P><FONT color=#0000ff>/* initialize graphics and local variables */ ) M5 K) K5 t# v$ ]
initgraph(&gdriver, &gmode, ""); </FONT></P>/ M4 ~0 T; u0 Y0 |
<P><FONT color=#0000ff>/* read result of initialization */
9 P+ O" A3 N- L9 Y2 c( j1 d; Q2 ierrorcode = graphresult();
! {! \3 q) P$ W: W Vif (errorcode != grOk) /* an error occurred */
" O- n6 R/ @1 q% D3 b* x k& F: [{ 1 l5 F% ?1 K, f
printf("Graphics error: %s\n", grapherrormsg(errorcode)); " ]6 `5 n. z% b" X
printf("Press any key to halt:");
6 I* i# C+ q5 @getch();
7 z& w3 T6 p# j* {. _exit(1); /* terminate with an error code */
+ t9 n$ R6 r9 Q1 E. O4 t} </FONT></P>4 @1 d$ W9 s, j0 _/ ^
<P><FONT color=#0000ff>/* select a text style */
_. ]: B7 X2 m, B+ X% ?/ o( Qsettextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); </FONT></P>4 q* H& e3 d5 o( m3 `5 L' z I
<P><FONT color=#0000ff>/* move to the text starting position */ 3 q2 }# t1 x6 C" x5 F. k1 t5 ~4 v
moveto(0, getmaxy() / 2); </FONT></P>
2 \+ Z4 t+ \+ t# A<P><FONT color=#0000ff>/* output some normal text */
" q0 h I2 f- R/ H9 c/ b* routtext("Norm "); </FONT></P>
! u% a6 n+ Q" b F<P><FONT color=#0000ff>/* make the text 1/3 the normal width */ - p z @0 s- A- \
setusercharsize(1, 3, 1, 1);
) e6 L; i8 D7 K0 b: A9 {outtext("Short "); </FONT></P>
" }! I" g W/ E5 V: ~2 r* k- |7 P<P><FONT color=#0000ff>/* make the text 3 times normal width */ 7 f+ }, A- T; M0 p
setusercharsize(3, 1, 1, 1);
( y ~1 W {2 B/ `2 _outtext("Wide"); </FONT></P>
$ L" K. e6 |2 F5 ?9 }; E<P><FONT color=#0000ff>/* clean up */ 9 d; |3 J) w) M" m7 T5 p
getch(); . H- x3 k/ r3 t7 x8 L! u2 K" G2 A; J
closegraph(); . z2 e$ p4 ^: O9 R0 s( g
return 0;
! G5 w& ^- A0 J+ P} </FONT>
6 n6 Q& |. i+ I- ^' S# \ B</P>4 V$ V' ]3 C+ M/ y6 h, z. d
<P><FONT color=#ff0000>函数名: setvbuf </FONT>
( X3 T/ T- z' _: H' f1 `6 ]4 n功 能: 把缓冲区与流相关 1 P1 W/ D0 _( b; e. W) B- H; x6 O
用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
% C/ O/ z9 d3 y& Z( r8 b+ v程序例: </P>
/ Z9 s( B$ Y5 A/ `6 L<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
! m# i6 t5 k1 F0 C# M( n& s n<P><FONT color=#0000ff>int main(void) ( G/ F6 q. I. N' q v
{
4 x+ a' t; a2 X0 F7 N0 uFILE *input, *output;
9 S) }- A- |$ \! w$ Ochar bufr[512]; </FONT></P>
8 Z' Y2 A! U$ O; A<P><FONT color=#0000ff>input = fopen("file.in", "r+b");
) x& S: D8 t0 G& houtput = fopen("file.out", "w"); </FONT></P>
b0 D# M% l. f8 S# b- ]5 g9 P<P><FONT color=#0000ff>/* set up input stream for minimal disk access, ; H- a/ s2 _- a8 P: N8 c$ R/ `1 A
using our own character buffer */
0 f0 S# b3 c. Vif (setvbuf(input, bufr, _IOFBF, 512) != 0)
1 M' {' R6 t+ {! ^# J$ \1 h b4 `& xprintf("failed to set up buffer for input file\n");
( g. P4 u- K0 P' m- O+ A2 _ ^. jelse ! r" o5 N( o' t$ v4 e( F% z& B6 i
printf("buffer set up for input file\n"); </FONT></P>
* m) m: T( `2 E# r- R5 E9 y<P><FONT color=#0000ff>/* set up output stream for line buffering using space that ) k- V# _5 X! y
will be obtained through an indirect call to malloc */ ) _1 y1 k$ b6 }2 X( K% f
if (setvbuf(output, NULL, _IOLBF, 132) != 0) 7 z! @* v1 Q2 Y" t
printf("failed to set up buffer for output file\n"); 4 g7 `( A1 p3 F0 R- [# ]
else 5 ]% W2 m7 M1 h$ K
printf("buffer set up for output file\n"); </FONT></P>3 }/ Y/ A. F: F" M ~" l7 m1 h
<P><FONT color=#0000ff>/* perform file I/O here */ </FONT></P>. {7 D! l- D0 p+ F6 L6 W
<P><FONT color=#0000ff>/* close files */
8 r; q" w0 a8 Z. Ofclose(input);
( H$ j% S4 H3 w$ Qfclose(output); 9 v6 F/ O7 m) a* H0 Q2 m; F* r) q
return 0; 6 E2 O6 ]/ e0 Z& t. {" P) ~3 Y$ `
} " N4 |4 ?! t6 g
</FONT>' n9 n$ v0 v/ b- l- d: g3 j
4 {$ Y. v3 v2 M( w$ t. Q& D2 F
</P>5 ?1 W* K0 h7 x7 {9 ?7 k. ]( b
<P><FONT color=#ff0000>函数名: setvect </FONT>
# c2 r. X, z3 w2 Y N0 @功 能: 设置中断矢量入口
6 X: [. @. r1 R f用 法: void setvect(int intr_num, void interrupt(*isr)());
% n, t) o- ]2 \" P2 G$ {程序例: </P>
d2 x- Q" \2 U<P><FONT color=#0000ff>/***NOTE:
0 u# \2 |) `" f6 t! A& mThis is an interrupt service routine. You can NOT compile this
& {8 [( Z6 F7 Nprogram with Test Stack Overflow turned on and get an executable
# K: v1 p1 E) Hfile which will operate correctly. */ </FONT></P>" C" \# K1 Y( o
<P><FONT color=#0000ff>#include <STDIO.H>
3 F- F/ W5 \' h) d#include <DOS.H>
" Z* h6 E" c3 o6 p3 k6 O8 r#include <CONIO.H></FONT></P># d# y# Q: C3 L4 H
<P><FONT color=#0000ff>#define INTR 0X1C /* The clock tick interrupt */ </FONT></P>
; L0 k& h3 Y) P- A<P><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>' X# G! S+ U; O; x( \1 j( K* F
<P><FONT color=#0000ff>int count=0; </FONT></P>
5 a1 s# f; N. l& e, H4 X<P><FONT color=#0000ff>void interrupt handler(void) 8 E' v, Y5 ]# K. \' h: l; K0 f/ F+ a
{ ; B8 o- X5 Q* b; \
/* increase the global counter */
' q& d# S& E% u6 e- Tcount++; </FONT></P>
# ?9 b8 v1 x2 J; ~) S) u( d<P><FONT color=#0000ff>/* call the old routine */
8 M4 Q: ]) j" b7 w- D9 Toldhandler();
, H+ X' V. l3 U2 J5 b} </FONT></P>
0 F4 i" _! Q; E' R7 \<P><FONT color=#0000ff>int main(void) # t, ^7 W: y7 S+ a
{
9 d F. i* e( k; W) z$ Z3 y/* save the old interrupt vector */ 5 t2 u D" G& I2 M/ T: K
oldhandler = getvect(INTR); </FONT></P>
" I: C% [ s t9 {0 y<P><FONT color=#0000ff>/* install the new interrupt handler */
( A) d! Y6 ~+ Q3 X) csetvect(INTR, handler); </FONT></P>
6 j+ b; Z5 k& ]1 z8 T- [) X4 V, g% w<P><FONT color=#0000ff>/* loop until the counter exceeds 20 */
6 U* H) w2 P) h3 I5 y$ kwhile (count < 20)
% o- `5 `2 W/ W7 G& Fprintf("count is %d\n",count); </FONT></P>5 E9 H- C$ l Y' U- j
<P><FONT color=#0000ff>/* reset the old interrupt handler */
0 ?- o. v( J8 Vsetvect(INTR, oldhandler); </FONT></P>
8 N& G% m& g+ }% K, A0 E<P><FONT color=#0000ff>return 0;
/ A5 [7 |( M! u' G1 x}
- D' \8 C/ L' R% P) x</FONT>
; V, {, i& r. e7 x9 ], I) t</P>
8 |1 l: k9 i1 n6 r9 Q8 G3 m" @8 r8 S2 N4 G<P><FONT color=#ff0000>函数名: setverify </FONT>: T8 \1 c- o: o/ T, @7 a" V/ T
功 能: 设置验证状态
- A! T( m2 I1 y5 y2 X用 法: void setverify(int value);
7 Z0 c& D+ A, W7 @: l8 }+ D" h程序例: </P>
4 A1 p* \' i* e3 X% V8 E% W; m<P><FONT color=#0000ff>#include <STDIO.H>
) {+ K3 D/ n: @- m/ h+ z: ?$ C#include <CONIO.H>) o- `1 [0 n: k" a
#include <DOS.H></FONT></P>& _- h5 V2 g6 ~1 F G2 ^ d9 }
<P><FONT color=#0000ff>int main(void)
8 p% K; H* M* T{
9 x8 | c7 @0 X+ ~7 C4 mint verify_flag; </FONT></P>
) i1 [ a# g- Z* B4 {7 B<P><FONT color=#0000ff>printf("Enter 0 to set verify flag off\n"); + I6 }* `* x9 s% L! }
printf("Enter 1 to set verify flag on\n"); </FONT></P>
) ?! S7 k+ z! S% T5 S<P><FONT color=#0000ff>verify_flag = getch() - 0; </FONT></P>
! {/ R+ F! p, e# l V, ^: I( @6 G2 Z, g<P><FONT color=#0000ff>setverify(verify_flag); </FONT></P>
+ _+ w+ Y) V+ j4 H- X4 ]<P><FONT color=#0000ff>if (getverify())
+ J9 j6 |3 v) G: {' i+ ?% Uprintf("DOS verify flag is on\n"); * F' ^ m8 R1 Q7 ^, H
else ( N+ R6 j( d0 v/ F. a5 ~! f. V
printf("DOS verify flag is off\n"); </FONT></P>
( v0 K! r2 j; n i<P><FONT color=#0000ff>return 0;
0 r+ Z8 Y, X |* N- b; c}
( z# @5 J2 H e b2 E( [( X<FONT color=#ff0000>! ~. Q( s" o# Y
</FONT></FONT></P>
" V, z3 [( V$ c: Q: n<P><FONT color=#ff0000>函数名: setviewport </FONT>4 y3 ]9 U! h3 |' }! M. L
功 能: 为图形输出设置当前视口 ; Y( I N- P$ A! ?
用 法: void far setviewport(int left, int top, int right,
6 a% x$ x: [- p9 Wint bottom, int clipflag);
- q9 d% f3 @- D9 u4 ^程序例: </P>: H' J9 h1 @( Y
<P><FONT color=#0000ff>#include <GRAPHICS.H>
; T; s6 y) P) e+ ~#include <STDLIB.H>
) ?9 Y3 g. p0 S1 s4 f#include <STDIO.H>
* P* A: [7 L6 y7 A. I#include <CONIO.H></FONT></P>3 S' S- j5 k5 `& d! y0 |
<P><FONT color=#0000ff>#define CLIP_ON 1 /* activates clipping in viewport */ </FONT></P>: b: Q! l( j( g! ^- \( u; w
<P><FONT color=#0000ff>int main(void)
5 a+ [! F2 H5 [2 p/ f$ S{ ) ]7 }2 J9 P' S! H& }! j8 u
/* request auto detection */ : h# B j- V8 t$ ?0 g
int gdriver = DETECT, gmode, errorcode; </FONT></P>
! _6 c6 n0 P( M! X5 v" {<P><FONT color=#0000ff>/* initialize graphics and local variables */
7 {8 r6 Z6 y5 m: ]6 W( X) \% o$ Oinitgraph(&gdriver, &gmode, ""); </FONT></P>
2 W! d. Q1 \! s: R; m: }& Q<P><FONT color=#0000ff>/* read result of initialization */
0 U- j( D* t9 S3 S; verrorcode = graphresult();
2 p# E7 K: K# sif (errorcode != grOk) /* an error occurred */ 4 H4 V/ M: ~& D/ Y, H9 Y G% _
{ 7 v1 d- i% { }; `: r. {: {' g
printf("Graphics error: %s\n", grapherrormsg(errorcode)); % n7 I2 J1 t; w1 z/ o
printf("Press any key to halt:"); & o. H4 c4 `( c5 d M) R
getch(); - u' b" l0 `* n9 `# h
exit(1); /* terminate with an error code */ h# s' \4 h( ^' b0 j
} </FONT></P>4 ~; V6 s0 d g8 V" s! g2 M
<P><FONT color=#0000ff>setcolor(getmaxcolor()); </FONT></P>
H0 [( N9 @* O( m: L# d<P><FONT color=#0000ff>/* message in default full-screen viewport */ ) L' ^9 {8 x$ [1 ~" Z
outtextxy(0, 0, "* <-- (0, 0) in default viewport"); </FONT></P>
$ W! V) a+ s- ~, u<P><FONT color=#0000ff>/* create a smaller viewport */ % `7 C" V6 y- g4 Q" X. _* W* e
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); </FONT></P>
3 l) p8 D2 G1 [<P><FONT color=#0000ff>/* display some text */
& h, @( i$ S3 C6 o5 d7 Kouttextxy(0, 0, "* <-- (0, 0) in smaller viewport"); </FONT></P>. ?9 {& v$ w+ E) b
<P><FONT color=#0000ff>/* clean up */
% q+ ?- L" `6 igetch(); & Y/ j' q8 I3 m6 f6 L4 K$ J. J
closegraph(); * Y% b7 c& H% W1 n
return 0; % o% t. c7 e, D, i9 E
}
& Z# j( {( d# C6 V' n' T8 c5 W. M</FONT>
2 O/ j8 U3 i, g7 m8 \( K+ t</P>
( A; f8 }) y8 g& b7 a<P><FONT color=#ff0000>函数名: setvisualpage </FONT>8 w a% ^7 T( n8 ?. C) q
功 能: 设置可见图形页号 2 B; b" G$ o8 x2 C
用 法: void far setvisualpage(int pagenum);
8 W s! b5 ~1 k: r程序例: </P>0 l8 [; Z' [6 g+ o8 Z
<P><FONT color=#0000ff>#include <GRAPHICS.H>
4 W K& `8 j* ]% E#include <STDLIB.H>" O) a$ ?5 D+ ~: X; ]2 w
#include <STDIO.H>; j) I) L3 a) f3 j/ e! R
#include <CONIO.H></FONT></P>- v% `' a ?7 c y8 }3 f8 G& k2 x
<P><FONT color=#0000ff>int main(void)
0 a, y8 E7 Y; Z{ . m; ~# t/ x: ^- A) b2 F9 q- S
/* select a driver and mode that supports */ ! y) d( K' k1 Y2 ^
/* multiple pages. */
; T4 C$ J' s* ^& `4 h- g2 zint gdriver = EGA, gmode = EGAHI, errorcode; 0 ?& Z4 d$ l' ^- s& |3 \
int x, y, ht; </FONT></P>& e" t% M+ ~' L! P
<P><FONT color=#0000ff>/* initialize graphics and local variables */ % C8 L% e" ^/ g- r% q
initgraph(&gdriver, &gmode, ""); </FONT></P>
2 N& b1 q1 r L5 U F# m<P><FONT color=#0000ff>/* read result of initialization */ 0 C& _8 w& ]0 V a0 j3 T8 u8 l+ v
errorcode = graphresult(); , |1 p% |1 c' C9 f$ F/ N! g
if (errorcode != grOk) /* an error occurred */ ( v X" e% N; ~; A8 ~; F
{
z/ |0 \+ \0 C$ {! Kprintf("Graphics error: %s\n", grapherrormsg(errorcode));
$ T" @+ \3 B# z/ e( U Uprintf("Press any key to halt:");
6 r1 Y* c2 G; M( [getch(); }8 @$ ~7 S5 F0 x Z
exit(1); /* terminate with an error code */
, V+ D9 r! x0 R2 V2 C} </FONT></P>0 _& m2 G2 G Z9 t, \2 X( B
<P><FONT color=#0000ff>x = getmaxx() / 2;
# U- J* m/ U7 O9 b g6 s/ Oy = getmaxy() / 2; 4 k j$ E" I+ C6 v5 M; w
ht = textheight("W"); </FONT></P>
& Y2 H9 e! O2 W3 f o1 f' B4 S<P><FONT color=#0000ff>/* select the off screen page for drawing */
: U0 x1 C7 u1 q# |! j6 h- r gsetactivepage(1); </FONT></P>
: K s8 i4 @+ C6 R<P><FONT color=#0000ff>/* draw a line on page #1 */ 0 j2 {! a$ S H& L( Q$ V
line(0, 0, getmaxx(), getmaxy()); </FONT></P>/ G5 q6 s" N( M6 ]8 Z
<P><FONT color=#0000ff>/* output a message on page #1 */
. i- W& L! M& O1 ]settextjustify(CENTER_TEXT, CENTER_TEXT); 7 q6 r8 `+ f) L
outtextxy(x, y, "This is page #1:"); $ p6 L8 `2 n& G; f! q
outtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>) w' w7 D1 Q2 ]" v
<P><FONT color=#0000ff>/* select drawing to page #0 */ . N Q$ X4 t: S! x' I
setactivepage(0); </FONT></P>
3 F8 a+ h1 s: F1 Q<P><FONT color=#0000ff>/* output a message on page #0 */ $ s* ?7 J& {: m8 C1 C, N7 `
outtextxy(x, y, "This is page #0."); ! d4 k. M, o! J$ Z
outtextxy(x, y+ht, "Press any key to view page #1:"); + J8 t9 ?* {' T- F: [2 d% I/ M& ^
getch(); </FONT></P>( n5 d% y7 w7 |
<P><FONT color=#0000ff>/* select page #1 as the visible page */
! {1 ]/ L- g8 ^$ N5 y( Asetvisualpage(1); </FONT></P>2 k0 M+ W" J4 i+ D0 b
<P><FONT color=#0000ff>/* clean up */ ' h }8 j) n% G5 t
getch(); 0 }$ v+ b. P; P: Y8 p' i2 B
closegraph(); * X+ U" K1 ~" x! B1 \
return 0; - P& e) W: C# V A' L" M- Z
} </FONT>/ ~6 ?' V- Y/ H
E9 a& l; U5 ^8 V</P>; { V2 m p$ x( Z2 t q y3 R# z
<P><FONT color=#ff0000>函数名: setwritemode </FONT>
5 Y: ~4 S% x8 N; I7 q7 a功 能: 设置图形方式下画线的输出模式 4 \2 k% H2 J: w6 q( s8 Z
用 法: void far setwritemode(int mode);
7 a/ I+ ~$ g" T ~" r5 ]程序例: </P>" w! N, _5 t- Z1 E
<P><FONT color=#0000ff>#include <GRAPHICS.H>
1 ^" t+ a# R6 X#include <STDLIB.H>
; V' E, X* L. e& ]' j#include <STDIO.H>& F3 J5 P, n' s8 `8 N
#include <CONIO.H></FONT></P>
. V( y4 k( D' V) u+ @<P><FONT color=#0000ff>int main()
$ C/ `- ^5 S( H7 Y4 F% `& ^' z{ 1 o- Y" E) _* P$ S0 R
/* request auto detection */
* J+ z' m3 W4 ?7 C$ v+ V8 `int gdriver = DETECT, gmode, errorcode; " |& r2 I5 Q2 H
int xmax, ymax; </FONT></P>0 b- d( H' k2 G7 F, z5 a
<P><FONT color=#0000ff>/* initialize graphics and local variables */ . t/ l5 v/ @# b
initgraph(&gdriver, &gmode, ""); </FONT></P>
% l/ `7 c7 ]3 E<P><FONT color=#0000ff>/* read result of initialization */
: w5 a1 _- W1 a2 lerrorcode = graphresult(); 5 g) i" e. a' n
if (errorcode != grOk) /* an error occurred */
, _) m4 P( c5 W8 L{ . I( G8 ^) D- i# p( Q- F: ?
printf("Graphics error: %s\n", grapherrormsg(errorcode));
9 Z& s; @( p( zprintf("Press any key to halt:"); ' K: [0 V9 ]" ]- n- g. ~% X& N) a
getch(); / O6 r$ i! {- {" ~
exit(1); /* terminate with an error code */ , L6 r8 \/ T' j) Q) w1 \
} </FONT></P>; s c% A S" y1 {. d
<P><FONT color=#0000ff>xmax = getmaxx(); 0 d: h! t1 Y$ l' I1 s0 i/ w
ymax = getmaxy(); </FONT></P>
* O& _* t3 ~5 T<P><FONT color=#0000ff>/* select XOR drawing mode */
9 [. { b' d$ ` }0 n# y5 z* Jsetwritemode(XOR_PUT); </FONT></P>$ C7 \" x( t' j0 t1 `+ k
<P><FONT color=#0000ff>/* draw a line */
1 q. a0 ~& z# E$ b- G8 Qline(0, 0, xmax, ymax); 1 q* P3 f' I7 H" M# {; o! a
getch(); </FONT></P>3 o0 R# ?/ i& {6 Z8 c5 I% c
<P><FONT color=#0000ff>/* erase the line by drawing over it */
' P4 f# m; [# t3 x0 nline(0, 0, xmax, ymax);
3 U" t" z7 U- e* E ggetch(); </FONT></P>% ^6 v3 ]- }3 O0 R3 w5 Y
<P><FONT color=#0000ff>/* select overwrite drawing mode */
" |( L) K- @) x8 F6 isetwritemode(COPY_PUT); </FONT></P>4 @7 |, s, {6 a) Y2 C3 `$ R: K5 ~
<P><FONT color=#0000ff>/* draw a line */ # k! [' v8 ^+ e
line(0, 0, xmax, ymax); </FONT></P>3 O% u1 U: @; b" U/ O
<P><FONT color=#0000ff>/* clean up */
- \4 e: L0 x* u0 w- Z+ \getch(); / g/ f0 G8 `* o- u: w' |0 o
closegraph(); - \! C% b% ]7 N0 t- `5 z+ A5 U8 U
return 0; $ ?4 c3 K M# ?$ Z3 |1 t
} ( _! b/ ~4 o3 `/ M: Z9 P
</FONT>( _3 t, J, E. w t. z
</P>" g- q7 z5 p1 N: @/ L
<P><FONT color=#ff0000>函数名: signal</FONT>
/ F6 Z- Y$ Y1 Y9 u! x2 b4 K功 能: 设置某一信号的对应动作
2 q- V4 k: @8 R7 ^, F用 法: int signal(int sig, sigfun fname);
& k0 \' F; P$ l$ l3 r8 S程序例: </P>& K+ l3 n& G( T
<P><FONT color=#0000ff>/* This example installs a signal handler routine for SIGFPE,
$ w' c. q; L$ W. acatches an integer overflow condition, makes an adjustment
# b0 n; _' T9 sto AX register, and returns. This example program MAY cause
- x9 s% L9 ^$ s$ t$ H) A+ h. Q" Ryour computer to crash, and will produce runtime errors
2 K7 x( F1 V, v! M/ ndepending on which memory model is used. 4 u+ B5 e; k; Z5 ?# d9 ]' f# C
*/ </FONT></P>, \; K" @- C& D0 f Z& P
<P><FONT color=#0000ff>#pragma inline / { ?( r7 Y& t+ a
#include <STDIO.H>
+ \& U, L5 j( D! Y! x8 l#include <SIGNAL.H></FONT></P>; q1 o+ @# L0 L. N
<P><FONT color=#0000ff>void Catcher(int sig, int type, int *reglist)
8 \/ [% `- ?( V& e{ 0 r2 U+ f5 {4 a4 q
printf("Caught it!\n");
: y. k+ a6 M$ t- v*(reglist + 8) = 3; /* make return AX = 3 */
' [) p' W: N8 `. T& m% u% V} </FONT></P>" ?0 J9 K! R- T) F# F
<P><FONT color=#0000ff>int main(void) $ ]' A( m/ W0 [; q' N2 M9 a1 @$ S
{ 7 @# b: D4 T D" O9 q
signal(SIGFPE, Catcher); 9 V4 u9 ]/ X' I! _/ q
asm mov ax,07FFFH /* AX = 32767 */ ( M; ^: J4 \: |
asm inc ax /* cause overflow */
6 f) B, M0 [! _asm into /* activate handler */ </FONT></P># \8 N7 {# G; j6 S/ }. p9 p# o
<P><FONT color=#0000ff>/* The handler set AX to 3 on return. If that hadn't happened,
* E. }# Q: n7 T5 athere would have been another exception when the next 'into'
3 Z ^' ~; K: X/ I- l5 M& K+ Y' f0 iwas executed after the 'dec' instruction. */ 2 m, N! x4 C' |1 b8 ^
asm dec ax /* no overflow now */
+ ?! N; F/ C$ Zasm into /* doesn't activate */ 0 {* p0 w, s/ a3 O4 @ y1 [
return 0; $ x5 q( }6 [# Y# n" Q! ?9 H' A
}
3 x0 _8 P- G0 ?: w% W' l G+ n3 Y/ _+ @! H$ B! l! N" B" q5 v
& o2 p: k9 u# Y
</FONT></P>1 G$ I" d. B) J' P8 U' ]
<P><FONT color=#ff0000>函数名: sin </FONT>1 G' I% I" r1 d
功 能: 正弦函数
4 x$ B! l" e! a) {' P& d/ c6 \( k用 法: double sin(double x);
4 i7 |7 l. \3 J程序例: </P>/ S$ ]% z) E- O' f- c @( S
<P><FONT color=#0000ff>#include <STDIO.H>: Q k! C, p7 c+ r2 f
#include <MATH.H></FONT></P>
5 m, a0 R8 Q6 }8 _4 W# w<P><FONT color=#0000ff>int main(void)
( B ~1 `, D" [$ M4 [$ I C t4 p- P& d{ 0 L% j) m8 v- V2 @* p' X: P
double result, x = 0.5; </FONT></P>4 z' }* |1 C6 M0 _" E" [- R( {2 C6 R) W
<P><FONT color=#0000ff>result = sin(x);
4 }; Z5 t4 ]. Y% h: gprintf("The sin() of %lf is %lf\n", x, result); / _* P5 B' j8 {
return 0;
# E! i. x, h! u9 Y9 L} * s2 a* e/ \; x% b- ^
" l) ]9 h: l6 D6 p, f
</FONT></P>
6 I# b9 _- ]. [<P><FONT color=#ff0000>函数名: sinh </FONT>
) g# X' n* h/ T' E功 能: 双曲正弦函数
7 v! r- B" G' ~用 法: double sinh(double x); " u7 }7 o) _, {8 i+ q2 A) g
程序例: </P>
% R; R0 M* s( G1 U% z" h<P><FONT color=#0000ff>#include <STDIO.H>
8 ^6 @2 ^9 c" U$ g/ X! S#include <MATH.H></FONT></P>
8 {. f6 T- y* q/ d \* _# `<P><FONT color=#0000ff>int main(void) : \" F! N8 Y# p0 a" K
{ " f+ k) S4 E j9 z
double result, x = 0.5; </FONT></P>
5 S: W" R& t. \" l( X2 J F! n<P><FONT color=#0000ff>result = sinh(x); 7 l7 l Z6 t: b& E" p
printf("The hyperbolic sin() of %lf is %lf\n", x, result);
( T: q' i" x# }return 0; % _& g. {) d0 q/ j5 T0 W1 j1 a( g
} : y8 E' c! }; @9 ~) ?6 H* w; O+ f% S) p( v
# Y% Q. ]. F2 q# b T6 V$ z% u9 m0 ^
7 `2 s6 F0 G5 Z5 i8 e7 |
</FONT></P>
7 U" L0 m& G2 s1 m! `5 L& ]0 }<P><FONT color=#ff0000>函数名: sleep </FONT>8 Z0 t9 J2 F& [6 z7 j; i
功 能: 执行挂起一段时间 ?+ @8 o: t8 O
用 法: unsigned sleep(unsigned seconds); + [& S- e$ f% W& Z4 b
程序例: </P>: d* r' C; s$ }6 o
<P><FONT color=#0000ff>#include <DOS.H>
- g. \( g! Q, L' H P3 C#include <STDIO.H></FONT></P>! T& O% t$ C! N1 L# C
<P><FONT color=#0000ff>int main(void)
1 w2 ^, A7 G, w; q+ e, p{ 0 p) A P) E1 ~1 W8 @* |; v0 o
int i; </FONT></P>; L" y; X2 J# M
<P><FONT color=#0000ff>for (i=1; i<5; i++) " m5 Q0 Y& Z, Q
{
: {0 i1 R' l! V$ g+ Uprintf("Sleeping for %d seconds\n", i); 6 e$ y0 z+ e' |+ Z; A6 m; u
sleep(i); + S' J8 ]" d+ U R) R
} 0 F z) s( s: H0 _
return 0;
3 d0 V% a$ b0 U I} ' P* g0 {' F6 P
% R0 ~3 z% |, g9 M% v4 l& h</FONT>
) _2 n- W) M0 _- r0 D</P>% M8 t6 p+ Y* a( e3 ]
<P><FONT color=#ff0000>函数名: sopen </FONT>
. y2 e% o" x( |% K/ [# B功 能: 打开一共享文件
) n/ \6 s# E9 y0 M用 法: int sopen(char *pathname, int access, int shflag, int permiss); 5 v. I% Y2 a X% V: M0 U
程序例: </P>
8 u; ]. R5 d( _! j! |( _6 a<P><FONT color=#0000ff>#include <IO.H>) r' m2 j& i$ L! A- m) f- |; ?
#include <FCNTL.H>6 h' i& g: m: X' |/ \
#include <SYS\STAT.H>
8 R( z. L- p0 C6 k# f#include <PROCESS.H>
6 e. `( A& H" l+ J( Y, [& d#include <SHARE.H>
' U; k. o4 Q5 Y#include <STDIO.H></FONT></P>' m( y3 I) f4 U: m
<P><FONT color=#0000ff>int main(void)
/ j9 c/ i+ g& m" T{
2 u; m0 `* r% x. i! ^int handle; % _, ^) l$ {! [% ]# L& j1 Z
int status; </FONT></P>0 [: e( \ x9 F k
<P><FONT color=#0000ff>handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD); </FONT></P>; z5 `* `. K# x0 i" V4 ~" ] F+ b
<P><FONT color=#0000ff>if (!handle) + R ~/ x+ w9 E# O2 a. {
{ 2 c/ k; ^3 h# g
printf("sopen failed\n"); , R3 m7 ]% E5 }& C3 _
exit(1); 6 c9 ?# L7 z, I" g1 R% h( E k3 I
} </FONT></P>
& A ]2 }$ o$ P! T4 q; a( R<P><FONT color=#0000ff>status = access("c:\\autoexec.bat", 6);
, q+ Q0 A5 U P* Lif (status == 0)
! ?- k3 T! _0 S* }$ pprintf("read/write access allowed\n");
+ N, d v+ e2 telse
, w( G# y! D" M* R8 B; _/ a& iprintf("read/write access not allowed\n"); </FONT></P>
N l$ }( @% e<P><FONT color=#0000ff>close(handle); . `* N9 ~# l( V7 a, ~& k8 [
return 0;
- ?9 ?3 n5 s! K9 E6 W* I} 3 h% q, D* l$ U4 I; h
$ [* }3 x. W# c; S! \- f4 ]9 E</FONT>1 e) f G' ~4 i0 h) H d# m F
</P>
7 [! l0 w+ y- p7 G% g<P><FONT color=#ff0000>函数名: sound </FONT>
# b( S' G% a! Q/ U7 F功 能: 以指定频率打开PC扬声器 % \' c( x) O2 G9 F0 W/ R
用 法: void sound(unsigned frequency); & F- l0 l1 Y& O9 S
程序例: </P>2 a( e- B) f, [
<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds.
' v4 _7 h4 e. E/ E" dYour PC may not be able to emit a 7-Hz tone. */
9 }% g9 Z1 y- I! _1 p5 Y: Y7 \#include <DOS.H></FONT></P>
9 @8 f' `8 X1 X ^<P><FONT color=#0000ff>int main(void)
N! J% @% b+ @- I1 s, C{ 3 X# s: F: F. N/ w5 @% A/ r! E
sound(7); & {7 ]4 x- m5 j; k2 h
delay(10000);
7 D" O* d% T$ Q- W2 j; _( B Dnosound(); . j, z Q& \6 f' W+ N( E
return 0; 8 c9 \! P% d/ _& O, |2 n
} </FONT>
( U% n1 E9 A4 @( s, d' G1 Y4 N$ z6 t2 G- \
3 n' Z* O& H( m% D; S' e
</P>
3 E" n2 V* e' {+ V+ d<P><FONT color=#ff0000>函数名: spawnl </FONT>3 ~. e, f0 ^! s2 S( R
功 能: 创建并运行子程序
6 ?7 w/ \! m1 ?6 O; a5 J! {用 法: int spawnl(int mode, char *pathname, char *arg0,
; G c! X% t* S! H6 p8 ?arg1, ... argn, NULL);
6 G5 S8 C+ Y' b( a程序例: </P>
+ g5 J, a) g) `' R. L' H/ z<P><FONT color=#0000ff>#include <PROCESS.H>
, i" F) q" `5 s t, v1 K. ^ J#include <STDIO.H>
1 d( s8 L8 W: C2 c#include <CONIO.H></FONT></P>
4 C+ I# ~( G5 |' H" @& a! |9 H5 ~<P><FONT color=#0000ff>int main(void) - p5 }( v# o, I9 \: V
{
& _6 V( I6 k* Eint result; </FONT></P>
3 j) o* I8 x: }( B( J<P><FONT color=#0000ff>clrscr();
9 J: U+ E6 \% ?, i4 f% l5 Vresult = spawnl(P_WAIT, "tcc.exe", NULL);
/ U. q5 s- G9 K+ Yif (result == -1)
% p8 E) `) ~' P3 r, t9 a; K{
/ M( J, N* A7 C4 o3 C( J% j& g$ p: bperror("Error from spawnl");
; \; k) @ A6 W: ^) c, Zexit(1); ! `! Q7 Q: c$ Y3 d
} $ [0 ?8 m& }1 G0 l V9 Q
return 0;
8 e* v1 n8 _' a} 0 Y/ N1 w. \& T1 G: ^
</FONT>7 X j: Y7 [% i q( C
</P>
7 Q& t+ n# ^7 c! k4 `<P><FONT color=#ff0000>函数名: spawnle </FONT>! Q0 T/ E, W# m/ Y9 g7 A" q
功 能: 创建并运行子程序 : M6 V4 |! P2 \4 R* y5 _
用 法: int spawnle(int mode, char *pathname, char *arg0,
. [( q. c) u9 ]: Narg1,..., argn, NULL); ; t. F+ g5 g' i# p* K* }. O
程序例: </P>
+ t& t5 N% i4 O$ a3 h i; I<P><FONT color=#0000ff>/* spawnle() example */ </FONT></P>, D7 t5 ?3 E" ~- v' f# r
<P><FONT color=#0000ff>#include <PROCESS.H>1 c/ Z0 ?' F+ V! F
#include <STDIO.H>- ^' z$ j& x" x
#include <CONIO.H></FONT></P>
9 {5 [& x6 z" Y/ M* P7 A9 i<P><FONT color=#0000ff>int main(void)
8 V: a% B( e7 s* a: g: @. i{ 1 \' ~) W0 b" P6 z. W7 D6 H
int result; </FONT></P>
* y, A g8 T3 {- {4 h+ t<P><FONT color=#0000ff>clrscr(); + V8 i' M% G1 t7 O/ C! @
result = spawnle(P_WAIT, "tcc.exe", NULL, NULL); 0 Q# F$ Z( S, ~- N6 ?" N% t
if (result == -1)
6 h( ~0 E' n# T% |; r: m1 t% j{
9 `0 l4 p( {% S0 Iperror("Error from spawnle"); ; P4 I" A+ d) i3 ~ b
exit(1);
7 _6 j. ~& E1 Q& l) H5 n: u0 p- t} 9 C5 i# d% R( f4 H1 E
return 0; . I {: O" M: c: G8 c) ` x% v
} 7 c7 m+ H o& ]# l' b0 Y/ M
! B6 D0 ]+ N: v T* H; t- L2 E0 d% `# I
</FONT></P>
8 i+ k3 V$ q7 l. X7 H: Q: _<P><FONT color=#ff0000>函数名: sprintf </FONT>7 h0 {# p) e h( {
功 能: 送格式化输出到字符串中 0 ?& v2 ^& ?8 i% y( g8 h" c
用 法: int sprintf(char *string, char *farmat [,argument,...]); + \; { X2 U1 I' N6 w
程序例: </P>2 I& `$ ]* U0 I8 l) q
<P><FONT color=#0000ff>#include <STDIO.H>7 K( `, S2 J, E! a" k9 d0 d
#include <MATH.H></FONT></P> ~5 J: U8 c: l* v: t
<P><FONT color=#0000ff>int main(void)
( M! D/ K: V( q& R* O `{ 6 w" E6 T1 m1 Z
char buffer[80]; </FONT></P> P+ _+ Z% E( k# N, D/ H5 p: I
<P><FONT color=#0000ff>sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
) m3 E8 ?% W# q0 i1 R9 H" _puts(buffer); , K- J; S2 K% i* }' p
return 0;
! V0 X; k5 s9 m/ N K}
Q% k3 E! U# D</FONT>
0 m: O& P- R4 |, G" o; Z</P>' L9 D+ m, }6 l5 k
<P><FONT color=#ff0000>函数名: sqrt </FONT>
& V/ G9 d, y5 w( E. Z功 能: 计算平方根 ' `4 R- Y5 J% F- L0 p& d, f
用 法: double sqrt(double x); : {# c( a7 Z& Q( M- r6 {+ C- P& H
程序例: </P>; C. |! ^' o4 E( l1 V. t
<P><FONT color=#0000ff>#include <MATH.H>2 S& z$ C- t% h! x
#include <STDIO.H></FONT></P>
% C3 u3 K! j7 w/ |) M3 r6 n [<P><FONT color=#0000ff>int main(void) * o5 ]/ v4 w) H1 p8 h
{
! L n7 U- K( Adouble x = 4.0, result; </FONT></P>0 N6 {3 F( \+ X# N5 T7 _0 h/ a0 W
<P><FONT color=#0000ff>result = sqrt(x);
/ E- W! Q) w1 {7 M. z" i2 Fprintf("The square root of %lf is %lf\n", x, result); 2 w. F' J- N+ d( R) G
return 0; $ H& Q6 d5 g& I. W; R
} 4 {) g1 z W' k9 n: Q4 a; E
</FONT></P>
9 d" ~3 C5 @9 b<P><FONT color=#ff0000>函数名: srand </FONT>1 J, Z8 h, l1 ^ l, h' R
功 能: 初始化随机数发生器
* n' ]' @0 c- m: g+ ?9 D! A用 法: void srand(unsigned seed); , |! w. g) m3 l/ o# X8 G& H @
程序例: </P>( z: q+ R2 a8 A; c
<P><FONT color=#0000ff>#include <STDLIB.H>( T- K4 I) v# m
#include <STDIO.H>& r% R! m: n: N6 G* `
#include <TIME.H></FONT></P>$ g5 Y3 |7 C2 p
<P><FONT color=#0000ff>int main(void) 4 Y+ N& Q* o' C- L, _$ X
{
g& T2 m0 O& {$ a+ O" ^" Dint i;
9 m* z( x9 ~5 n# Etime_t t; </FONT></P>
3 [" I) a w8 \$ W<P><FONT color=#0000ff>srand((unsigned) time(&t));
8 z4 r% x9 @: f! gprintf("Ten random numbers from 0 to 99\n\n");
6 r- a# s3 p& @' P& C. J6 _for(i=0; i<10; i++) 9 u: T( z, q: v" k/ J w" v
printf("%d\n", rand() % 100); ( {3 h& {" o X& t* w5 }
return 0;
# G- ~! \% n0 |' H3 S: b} , y8 W4 u) k' o& G
</FONT>& }* q" v# @$ y+ E# R
</P>" t+ j. Q8 [# y+ a
<P><FONT color=#ff0000>函数名: sscanf </FONT>' J& {) o$ K+ C
功 能: 执行从字符串中的格式化输入
6 J& @% D8 S4 k* b用 法: int sscanf(char *string, char *format[,argument,...]); 7 y% x4 g& x6 ]1 R
程序例: </P>
5 a. H/ K8 B8 C<P><FONT color=#0000ff>#include <STDIO.H>" q* p5 q+ _6 N3 g; n
#include <CONIO.H></FONT></P>( X- ?" [, P+ l8 Z% D0 A0 i b
<P><FONT color=#0000ff>int main(void)
# D* x( Q7 X; B/ y+ G! L{
" ]' _0 v7 ?2 E9 \) h2 H; [* kchar label[20]; ( n6 X% y7 S: @7 s2 d* M# B
char name[20]; . ^+ P' D( l9 [( V" [
int entries = 0;
) Q' _! r$ n% ]6 l( T2 Qint loop, age; ; T! g& a. Q; X, i" P
double salary; </FONT></P>0 v) q/ ^. u7 D4 o( h8 ~
<P><FONT color=#0000ff>struct Entry_struct ( o F( h% M, z2 X) b" z4 `. k" x, g
{
4 x5 Y# q8 m9 \! pchar name[20]; * \9 g' Y" {% K2 m& S' g3 I. T
int age; ( v6 _9 z2 T& r) V5 j( b2 G/ v; t
float salary;
, ]0 r7 c* @" n' V* j: A( D} entry[20]; </FONT></P>! b' q- M# f1 ^ Y7 K, Q: ~
<P><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
( D% Q e/ e$ _ x/ C( \7 aprintf("\n\nPlease enter a label for the chart: "); + l; R1 y G0 P7 [$ I% p- e+ M
scanf("%20s", label); 4 u* P i# I$ U# G1 l! L9 u2 _/ K
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>; A5 V. e% Z0 @3 \8 |6 y
<P><FONT color=#0000ff>/* Input number of entries as an integer */
" |% i6 m4 h9 y0 wprintf("How many entries will there be? (less than 20) ");
7 y# b5 ]/ d: I) E1 o7 tscanf("%d", &entries); 9 ^, w6 Z) O- Z- }: J6 \3 y
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
* Z' R: o# k d5 N9 e$ l<P><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ 8 z' n/ ^/ s+ F, `( i
for (loop=0;loop<ENTRIES;++LOOP) ) D" t, t; o3 j- B' X! g
{
0 v" ?- y( Y3 t* }% m* Pprintf("Entry %d\n", loop);
" O" u9 X, E4 ~8 F) uprintf(" Name : ");
& b. w: L4 ?/ T1 Fscanf("%[A-Za-z]", entry[loop].name);
% X7 }: Y4 B3 h X+ v3 @+ W4 a5 Pfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
" j1 L$ g7 [8 w' F<P><FONT color=#0000ff>/* input an age as an integer */ 8 P( X6 P$ \9 O: e1 T( j+ V& p* c
printf(" Age : "); _/ A* f( y5 u0 H
scanf("%d", &entry[loop].age); ' ` N' M& o9 o2 u3 w4 X( J
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
/ c% i: V9 o7 u# c5 b<P><FONT color=#0000ff>/* input a salary as a float */ 8 |9 `+ _! V. }+ E2 P" T
printf(" Salary : "); 8 L i% B- ^) Q# ^$ @2 l, B$ z
scanf("%f", &entry[loop].salary); & C& V7 z/ g% q" F/ U7 \
fflush(stdin); /* flush the input stream in case of bad input */ ; {4 s# Q1 C9 m8 L$ n! {
} </FONT></P>- l/ |7 x W$ ]
<P><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */ # }8 j0 X% E6 ^! f4 M+ `
printf("\nPlease enter your name, age and salary\n");
, K, ]1 D1 A6 [; I$ u; k0 A2 i8 @" Lscanf("%20s %d %lf", name, &age, &salary);
! |9 m. P6 t3 I# {0 Y9 A: W</FONT></P>8 _4 d/ V7 d' j4 y3 F% g
<P><FONT color=#0000ff>/* Print out the data that was input */ # s! `- \+ _8 K1 l
printf("\n\nTable %s\n",label);
/ S; A; B4 O2 I, Rprintf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
, y) {5 J. O6 Aprintf("-----------------------------------------------------\n"); 9 B' H+ ~9 e/ T9 c: q
for (loop=0;loop<ENTRIES;++LOOP) 0 G) A+ _& Z7 ]6 c0 f, K& c
printf("%4d | %-20s | %5d | %15.2lf\n",
% K$ q) |6 r0 J& R) a8 iloop + 1,
" _4 T0 v- W4 t, s- Bentry[loop].name,
& K2 f( S1 }8 B) F! p* Bentry[loop].age,
, M3 ^* z& j3 B* W7 a* Zentry[loop].salary); 7 h. R- j9 K4 `3 u
printf("-----------------------------------------------------\n");
# S; k; b; y u' O0 Z' t$ |return 0; , D" a; t! e0 W$ j' E: G8 K: u! H
} ( f" U3 O6 h1 N; R( W; S+ Z
, ~" o O: L0 A% W2 L
</FONT></P>6 J6 C. {5 j' d% `% @# K8 E& X M
<P><FONT color=#ff0000>函数名: stat </FONT>
, k& r, ~! \; E6 R功 能: 读取打开文件信息
; a8 ~3 P8 r# ]用 法: int stat(char *pathname, struct stat *buff);
5 ?2 G, z9 J3 G A7 Y" J7 T' t程序例: </P>
, V' q" T( E# F4 A/ F<P><FONT color=#0000ff>#include <SYS\STAT.H>3 E5 C% E' u: _+ c# o5 O) @: g& Q) U
#include <STDIO.H>
& w% e) t% G- D8 t; u! {#include <TIME.H></FONT></P>
p$ K; ], l0 y+ |+ P2 I<P><FONT color=#0000ff>#define FILENAME "TEST.$$$" </FONT></P>! `) \" s @) M: }8 a
<P><FONT color=#0000ff>int main(void) 7 Y; a& [" c0 c+ _
{ 0 G1 l/ Z: b7 s0 d4 E% o/ c1 J# Y! G
struct stat statbuf;
6 h* {' d7 A" U0 H* v! FFILE *stream; </FONT></P>
5 [2 T: k6 C$ G. I, ~1 Y<P><FONT color=#0000ff>/* open a file for update */
6 h& a: O D* }# u7 `/ l# Kif ((stream = fopen(FILENAME, "w+")) == NULL) . W) r3 m7 A5 b: E) I6 \6 M
{
( ?1 ]: s2 l! K q$ b, Qfprintf(stderr, "Cannot open output file.\n");
& D/ h) V5 I1 @return(1); 0 K2 x8 t- m. g/ W0 a
} </FONT></P>
6 T0 X6 p. m/ B# o<P><FONT color=#0000ff>/* get information about the file */ , Y' j3 N9 k6 x. Q
stat(FILENAME, &statbuf); </FONT></P>3 r' _* ~& S1 F: N% ^5 d
<P><FONT color=#0000ff>fclose(stream); </FONT></P>$ w k8 m4 E# O2 B: i a' s7 |' Q
<P><FONT color=#0000ff>/* display the information returned */ 1 ~1 K v% ^+ Y
if (statbuf.st_mode & S_IFCHR) 1 y1 H# N9 R; B! S
printf("Handle refers to a device.\n"); 4 B0 o7 B3 u& \7 h, d' a ~5 C
if (statbuf.st_mode & S_IFREG) ' P' O' J3 V6 I9 V) |; `
printf("Handle refers to an ordinary file.\n"); 3 T1 i7 c7 o& T
if (statbuf.st_mode & S_IREAD) 0 j* R: l# c: n5 X' }$ J" |$ X* C" u' c
printf("User has read permission on file.\n");
1 T$ j; Q2 f9 L9 {4 s5 \if (statbuf.st_mode & S_IWRITE)
$ u& u, H7 } |% [3 i4 Bprintf("User has write permission on file.\n"); </FONT></P>3 b( T1 k6 c' T U# p4 t' K
<P><FONT color=#0000ff>printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
6 h& e" o7 c4 H0 [* j) B8 Sprintf("Size of file in bytes: %ld\n", statbuf.st_size);
4 H: }# |5 F' Iprintf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
- v. C6 s4 G( S( Nreturn 0;
6 B" e/ R3 p N1 }0 d} + ^+ O+ |4 C7 q Y! X
" v4 M+ x3 A1 T; p
</FONT>* a5 b8 n, Q: [9 o
</P>
7 `/ [/ q7 S- c/ l$ ]$ a$ d<P><FONT color=#ff0000>函数名: _status87 </FONT>
/ X6 D8 ^9 ?! p* Z8 ?- x% N& n功 能: 取浮点状态
# q; [2 T% K& r }( G用 法: unsigned int _status87(void); - k _7 ?# O3 E
程序例: </P>
; D) [, G( X( ]* q2 |9 \<P><FONT color=#0000ff>#include <STDIO.H>: ]9 I) K( r7 }' X
#include <FLOAT.H></FONT></P>% w* B! V5 R' E5 y/ F
<P><FONT color=#0000ff>int main(void)
8 X, _8 y4 X; t3 t4 B8 B{ 1 o2 b+ |' J3 [ _2 }* h
float x; ( C; I `$ K ~& }0 f7 j: U
double y = 1.5e-100; </FONT></P>
+ F P" u C. ^$ V<P><FONT color=#0000ff>printf("Status 87 before error: %x\n", _status87()); </FONT></P>- I# S) `- t: ^, a# i' ^
<P><FONT color=#0000ff>x = y; /* <-- force an error to occur */
# ]8 l6 t2 U9 r( V; a) U1 E9 [y = x; </FONT></P>- m r% B# O2 ?
<P><FONT color=#0000ff>printf("Status 87 after error : %x\n", _status87()); 6 h! s5 f& q4 N& R
return 0;
4 g* ~: }' Z" M} - V- S% Q9 R3 }3 N5 n6 Y( ^
</FONT>
: D W+ g' O7 G7 T! i7 S# y</P>& y: w. \, s' S8 P- j
<P><FONT color=#ff0000>函数名: stime </FONT>% y, }% D' z2 B+ g' g$ a
功 能: 设置时间
% A6 |3 q& {/ T7 V6 Z用 法: int stime(long *tp);
; @8 }/ @# V3 ~7 f* w- B( S程序例: </P>- O5 A' D' p: d; z. O% |
<P><FONT color=#0000ff>#include <STDIO.H>
, R3 b6 @- {' ?" R; w' y; R( N7 J: x#include <TIME.H>( i% D5 s \- _( i
#include <DOS.H></FONT></P>& ~+ b' k* k0 T1 I
<P><FONT color=#0000ff>int main(void) ( |8 m' U8 U2 c2 E
{
. v) r, J3 d5 C+ R4 utime_t t;
" j. L9 n. W( h/ wstruct tm *area; </FONT></P>4 v6 ^0 j- J2 Y$ [' M
<P><FONT color=#0000ff>t = time(NULL);
% C2 D! e$ z+ _: L$ D5 uarea = localtime(&t);
9 i8 a$ b; T7 @9 A: zprintf("Number of seconds since 1/1/1970 is: %ld\n", t);
6 r# }+ `. N; S( Uprintf("Local time is: %s", asctime(area)); </FONT></P>
9 l, N+ @' ^' h4 p4 r* ^4 _<P><FONT color=#0000ff>t++; / y5 f/ B+ s! w# G
area = localtime(&t); 1 c# }* j7 {* E; R# x+ p' A5 ]
printf("Add a second: %s", asctime(area)); </FONT></P>$ |' S4 A- @5 W5 x
<P><FONT color=#0000ff>t += 60; 9 M1 A# F7 l! S9 ?! t; E" t
area = localtime(&t);
/ w1 Y9 C" Z" z: {2 Q8 u aprintf("Add a minute: %s", asctime(area)); </FONT></P>
7 N* g* F# I' N<P><FONT color=#0000ff>t += 3600;
1 {5 Y* Y P- z7 P! u; j2 Warea = localtime(&t);
" N$ M3 g( Z9 T) f4 bprintf("Add an hour: %s", asctime(area)); </FONT></P>
% j; I9 ?9 J, b5 q0 m<P><FONT color=#0000ff>t += 86400L; 6 f# o" A) L( p3 g L
area = localtime(&t); 5 U' E; i- n3 q6 X+ i% _
printf("Add a day: %s", asctime(area)); </FONT></P>4 Q* E. i* b9 @* k5 B
<P><FONT color=#0000ff>t += 2592000L;
/ U E9 n' ]; [) o6 B: {8 y+ xarea = localtime(&t); ) b1 _ E3 Z$ O' ~6 c! Z7 W9 f: Q
printf("Add a month: %s", asctime(area)); </FONT></P>
8 P6 v% q3 g1 z<P><FONT color=#0000ff>t += 31536000L;
8 d2 t+ M% a5 A2 yarea = localtime(&t); ; |4 z5 o B/ l) x
printf("Add a year: %s", asctime(area)); ( ?+ o/ q: [6 @, g- m7 E. S
return 0;
) g) J! u4 |6 \; a# V} </FONT>
3 E+ M5 d% K& H) z
- l) B5 H( q% X( b9 ?: E
4 w2 b4 c3 }6 [4 B8 p! _% q* {* k</P>
- R# ~% L$ n5 I1 o2 r9 t8 N. o3 q<P><FONT color=#ff0000>函数名: stpcpy </FONT>
7 h% r, L# g k, |& l功 能: 拷贝一个字符串到另一个 6 ~7 J9 d! G, Q3 M. l! q! m& p
用 法: char *stpcpy(char *destin, char *source);
; l5 P# R0 }2 A程序例: </P>
/ {& o/ o0 D- r<P><FONT color=#0000ff>#include <STDIO.H>
9 S9 o6 |' q. U8 J# e% v& T#include <STRING.H></FONT></P>
* d1 i# ~4 }1 O1 h& |& U0 @<P><FONT color=#0000ff>int main(void) , F% V- _& g( d# a k, {
{
* e$ ?5 c+ {6 qchar string[10]; ( V% C# i7 L" C! M! [' J1 }
char *str1 = "abcdefghi"; </FONT></P>
9 s. u% F3 q. x8 O! z/ E. T) @<P><FONT color=#0000ff>stpcpy(string, str1);
, w" O* d) o, {1 g3 F) b3 C) Qprintf("%s\n", string); T) X/ q" |' I- K7 G
return 0; ; ?- S. }7 ]. ~+ a$ @. ?
}
" M: T( K( Z' x: S, Q8 N! G" P2 N T' ~, V8 L$ w; P% F% X
</FONT>
8 ^- k4 j% r0 ~& b8 o, L" G3 S% \</P>
B+ i" M: w+ [<P><FONT color=#ff0000>函数名: strcat </FONT>
2 U& R+ p, d" ?% z功 能: 字符串拼接函数 ) Q0 O* F0 z+ Z3 i. |# U7 e# V
用 法: char *strcat(char *destin, char *source);
; F5 w" V( a6 n# r) Y* L# q程序例: </P>4 A2 h) O" Y: u
<P><FONT color=#0000ff>#include <STRING.H>& n$ ^7 i2 G" n, I9 e. }/ D
#include <STDIO.H></FONT></P>
, F ?1 x! w* @% e; {* o<P><FONT color=#0000ff>int main(void) ! c; Q: k( ` z9 H" Q" Z: g
{
$ j3 P! L5 w6 [7 G- Y& Uchar destination[25];
4 ^) u+ {/ h( ychar *blank = " ", *c = "C++", *Borland = "Borland"; </FONT></P>
# ~, i: F) r6 A1 F<P><FONT color=#0000ff>strcpy(destination, Borland); ) q* b7 O9 {" f$ D" s( o( U2 B3 ^
strcat(destination, blank);
0 n4 r0 T ?! m, v0 cstrcat(destination, c); </FONT></P>& l+ c; A/ E5 k; X3 \1 B1 H
<P><FONT color=#0000ff>printf("%s\n", destination); H/ e2 H+ h* O/ m% m7 h
return 0;
G8 j- C" Z+ J7 ?2 I, ?( f} * ]& q u7 b# r0 t9 n. i2 ~% f
& v; Q+ S# S7 @5 i, a: ]" T</FONT><FONT color=#ff0000>
& V+ l" g" D3 X! U! J</FONT></P>
3 r8 d2 E, {3 C1 v, V" c<P><FONT color=#ff0000>函数名: strchr </FONT>* |6 @/ ]" _1 a- i9 m1 u+ q
功 能: 在一个串中查找给定字符的第一个匹配之处\
4 g4 h! i0 E R用 法: char *strchr(char *str, char c); & n6 {' g6 ^9 _2 r2 \3 Q: {4 r% J
程序例: </P>9 j9 N; s: P/ ?4 ^' L
<P><FONT color=#0000ff>#include <STRING.H>
1 T: y$ W! q3 q) t0 N#include <STDIO.H></FONT></P>: u5 V: P; \5 T% e, C9 j
<P><FONT color=#0000ff>int main(void)
& O3 j* A9 I* e{ 4 B& _: |0 c; |
char string[15]; / H+ k) R+ d4 e3 \2 Y
char *ptr, c = 'r'; </FONT></P>
: g! Y4 Z: ^8 e1 u( r2 @<P><FONT color=#0000ff>strcpy(string, "This is a string");
7 u& `( M( H( t$ d: z$ z7 Z4 j. Mptr = strchr(string, c);
) @# e6 F3 a6 K4 xif (ptr) 1 l4 W# u6 M( u: E7 Q: J: W2 d
printf("The character %c is at position: %d\n", c, ptr-string);
2 ?' B/ ]" v% i9 {- k4 Celse $ k$ Y4 \2 i+ x2 f# P( y
printf("The character was not found\n");
( e. h; t/ g. p- F+ _; w4 Rreturn 0;
" [- a. o: N! Y( Z j} K0 |! S7 O: C
</FONT>
- z$ y" G$ P: k7 ^" D) M+ R1 a4 {0 ?( \+ m
</P>0 P8 G2 Q( N" h# b
<P><FONT color=#ff0000>函数名: strcmp</FONT>
; M1 |2 k, X. |7 C' a2 M5 s0 \功 能: 串比较
+ B9 Q; N7 I! O$ O2 | _用 法: int strcmp(char *str1, char *str2);
\+ t9 O$ s$ E) w程序例: </P>2 [1 t" b+ x( |7 W) ]' S! Q+ _
<P><FONT color=#0000ff>#include <STRING.H>
, z6 v/ L/ o% K+ }#include <STDIO.H></FONT></P>2 h D9 I/ P' k1 I+ d
<P><FONT color=#0000ff>int main(void)
5 J, _8 X( E8 X' W( I' d{
' S3 d1 J% @/ }3 V5 Qchar *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
0 y' y3 X; g s) _9 A- }" Nint ptr; </FONT></P>
) Q0 M( K0 ^% J5 ]0 G<P><FONT color=#0000ff>ptr = strcmp(buf2, buf1); U1 w/ d+ z8 O) K* N/ \
if (ptr > 0)
9 V, j7 C3 Q3 [, gprintf("buffer 2 is greater than buffer 1\n");
% O9 Y, J+ C% b5 v; [else
5 x0 Y! ^/ t" O3 J/ w$ U$ Dprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
6 l9 Z7 a1 b' E1 ?5 g<P><FONT color=#0000ff>ptr = strcmp(buf2, buf3); 9 G1 ~ v+ F) P; b
if (ptr > 0) ( a+ k: ^1 r! @' j: x$ a0 D8 V; f
printf("buffer 2 is greater than buffer 3\n");
s* Y! _/ V5 C; Z$ O! h0 ?else $ A; _: ~- y' K* A3 Z0 p# v! X2 R
printf("buffer 2 is less than buffer 3\n"); </FONT></P>
6 [3 ]) G' l$ {# c; G/ ^<P><FONT color=#0000ff>return 0;
T# c9 o, e0 b- P* Y* a}
, A- L+ q) J( E p: U5 A, b* j( I( o" C5 A' t2 j
# P( Q" M; l0 z" w; @
</FONT></P>, S; I, F; n2 \1 U5 n
<P><FONT color=#ff0000>函数名: strncmpi </FONT>
6 e$ v( R1 p. U! ]: B" L功 能: 将一个串中的一部分与另一个串比较, 不管大小写
& v# J. V+ `( _' _用 法: int strncmpi(char *str1, char *str2, unsigned maxlen); 8 r) T' E J9 R# q
程序例: </P>
4 Z8 F& u0 {) Z# F<P><FONT color=#0000ff>#include <STRING.H>8 p5 l4 v( o6 T
#include <STDIO.H></FONT></P>
w' C( y) C& _( U" `<P><FONT color=#0000ff>int main(void) 8 S) K1 @! U+ r5 Y& R' J+ g5 V$ s
{ : B2 O. a M: c m+ y. z4 z/ ?
char *buf1 = "BBB", *buf2 = "bbb";
! O* w2 v4 s, h& p; vint ptr; </FONT></P>
0 N* @+ `; _$ \ ^, H<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>
. ~. L, h7 ~* h+ ~<P><FONT color=#0000ff>if (ptr > 0)
5 K6 f# m# B$ u% \4 Y& {printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
7 X, j8 b5 P- Q& c<P><FONT color=#0000ff>if (ptr < 0)
& e5 C/ _! M$ nprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
9 B" E( L' k' W: G4 t<P><FONT color=#0000ff>if (ptr == 0)
0 E) Z* n# J9 [& Y* ^4 R! o7 A0 e7 nprintf("buffer 2 equals buffer 1\n"); </FONT></P>
1 c# l: Q( H# [( @0 n7 {<P><FONT color=#0000ff>return 0; # W, ?8 K+ P7 l# U/ m9 n4 `
}
6 p4 Q+ }; A8 C! k! X p0 v+ o/ H. V- ?1 Z! M- v6 B
</FONT>% [! b3 C9 ~1 L9 _! a9 P |
</P>, h6 G9 g2 s- I( Q, O* s" ?! M
<P><FONT color=#ff0000>函数名: strcpy </FONT>$ g+ F9 y3 D% c$ A9 I
功 能: 串拷贝 " d: o# [5 D4 q
用 法: char *strcpy(char *str1, char *str2);
0 C1 C: Q+ f! f. `程序例: </P>! P ~9 n) l' F* \
<P><FONT color=#0000ff>#include <STDIO.H>
9 D/ C2 t, K5 ~; }0 G2 p#include <STRING.H></FONT></P>
( f. K, ~" B+ @# t" I<P><FONT color=#0000ff>int main(void) 5 O1 K! z D* q- g/ e* |
{ ( J. Y5 H; {% R2 K3 H, s8 T3 A
char string[10]; % f! V0 @$ y, {; R8 B/ g
char *str1 = "abcdefghi"; </FONT></P>/ i+ \; u. N& }0 W* n. V
<P><FONT color=#0000ff>strcpy(string, str1);
4 N R9 Y D& \0 a4 j& Tprintf("%s\n", string); 5 Z) x" |' P2 b8 w/ U; E s0 R
return 0; 6 c) E) z9 J( p( T
} ( l, y6 {2 {5 h2 b0 o4 g ]
</FONT>+ C: P( w% |$ V
2 l# f/ V i, o/ V; \7 K
</P>& a& P' P. }4 h, O4 _, n4 N( P8 [
<P><FONT color=#ff0000>函数名: strcspn </FONT>
- Z$ y) x: C" S功 能: 在串中查找第一个给定字符集内容的段 0 _& v8 G# U6 p. Z+ P+ N9 o$ h! q
用 法: int strcspn(char *str1, char *str2); 0 s9 Z4 y# i! r0 ~
程序例: </P>) j4 T- h3 }0 Q, o k, v
<P><FONT color=#0000ff>#include <STDIO.H>* C: d3 p5 a; C
#include <STRING.H>2 U# |+ _- z0 A
#include <ALLOC.H></FONT></P>. x8 `7 l8 I) G* W1 b8 }8 p
<P><FONT color=#0000ff>int main(void)
; H# T8 A* Z& W |6 I4 Q6 b{ - V: q4 n0 w* i( G3 h: M+ r8 A
char *string1 = "1234567890"; " p6 i* A2 W2 M% ^$ g& ]) T1 W
char *string2 = "747DC8";
7 D' A# t) z) _7 A5 s% n( E7 m) F$ Eint length; </FONT></P>7 {( C! I, ]" ]
<P><FONT color=#0000ff>length = strcspn(string1, string2); 5 U: N7 R9 w9 R4 k0 g, X7 i4 s
printf("Character where strings intersect is at position %d\n", length); </FONT></P>
: l# l9 `- u& u8 l<P><FONT color=#0000ff>return 0;
1 s5 h! A% T7 m1 C7 j5 ^8 N} 5 ^/ L8 h" B n& W, g* ^
</FONT>
; g* J1 N3 a q" Y+ Z! d
' p' H. f6 U% X- G# |! f</P>& m& ^: J8 J# s* ?
<P><FONT color=#ff0000>函数名: strdup </FONT>
' B9 r" v1 E2 G6 ?7 g4 M: L/ m功 能: 将串拷贝到新建的位置处
- d8 q x1 }; @3 A用 法: char *strdup(char *str);
. n8 K- E% ^2 Y# M/ ~$ B程序例: </P>$ i- f/ p! J7 g: \2 P
<P><FONT color=#0000ff>#include <STDIO.H>
" ~( u" @8 O) j) s# I5 ^, d#include <STRING.H>
7 } M g8 N) w. ?#include <ALLOC.H></FONT></P>
4 l: f( S9 m& D9 j$ j<P><FONT color=#0000ff>int main(void) 2 P& c. p0 j- e9 c% T0 S
{ $ n' x5 |/ Y9 o# S- G4 H
char *dup_str, *string = "abcde"; </FONT></P>
/ ~6 D F7 b9 C2 `4 i<P><FONT color=#0000ff>dup_str = strdup(string); 2 e+ k7 S q9 ?! ]6 c
printf("%s\n", dup_str); 1 x# \( ]$ M2 O) S a8 A) I& _
free(dup_str); </FONT></P>
_ g$ I& f( c" e+ s q<P><FONT color=#0000ff>return 0;
2 x& h# T- |3 G9 s1 V} - ]9 O y5 U( I7 l2 B
. R' S3 U: [) E3 G
</FONT>; n. ]1 F- A3 P
</P>
. @% n! k* X2 ?2 T% E<P><FONT color=#ff0000>函数名: stricmp </FONT>: V4 D$ a" H/ y
功 能: 以大小写不敏感方式比较两个串
' m) T: _$ o6 z4 b9 b4 ^5 A& Q用 法: int stricmp(char *str1, char *str2);
1 l% _4 J! a3 b- N9 `! M$ B$ v4 ^6 |程序例: </P>0 r8 x9 |, u8 T ?. v" u# F& Q
<P><FONT color=#0000ff>#include <STRING.H>
, `6 d( q+ e: t#include <STDIO.H></FONT></P>% i ~/ v5 \- } r3 L
<P><FONT color=#0000ff>int main(void)
% t5 T+ T6 m Q9 ~/ F{ ) N! N; u( ]& G1 B/ H }: n% b
char *buf1 = "BBB", *buf2 = "bbb"; " n( I6 W+ ]% |& u o X
int ptr; </FONT></P>
+ A4 b5 z- i6 j<P><FONT color=#0000ff>ptr = stricmp(buf2, buf1); </FONT></P>% _$ b- a; ]8 k e' u# L
<P><FONT color=#0000ff>if (ptr > 0)
# |0 }$ C- b I S; cprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
- e- F. a9 e, V# i' z# f+ u6 F( n. X<P><FONT color=#0000ff>if (ptr < 0) ; j' m$ c6 X" C, {6 p# [
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
. r0 c4 m$ z6 b/ U* j" t<P><FONT color=#0000ff>if (ptr == 0) / T \0 J" _. H" E3 e
printf("buffer 2 equals buffer 1\n"); </FONT></P>: ^1 C/ w J+ q/ w
<P><FONT color=#0000ff>return 0;
7 h. b' G; ^5 T}
3 N j& r* P, f6 m</FONT>
2 X$ D5 l- L/ y4 l0 X3 `4 p</P>) g. u* R3 ^1 K* L. {/ k: m
<P><FONT color=#ff0000>函数名: strerror </FONT>
9 P: a+ U0 s- a7 R' i- c功 能: 返回指向错误信息字符串的指针 $ J( S9 c- L, t0 ~
用 法: char *strerror(int errnum); ( |- f5 L& I0 u% Q
程序例: </P>9 g; l( p4 r& s
<P><FONT color=#0000ff>#include <STDIO.H>
7 |: V$ l) C. u#include <ERRNO.H></FONT></P>1 d6 P' E3 e2 m& M: S
<P><FONT color=#0000ff>int main(void) ( o7 Q1 w, G2 A; R+ d; M
{
" w. ~* o( @, s2 kchar *buffer;
1 Y3 |! r% |5 h! [buffer = strerror(errno);
% ~; ~) S7 q- e1 U& p9 c- D' Yprintf("Error: %s\n", buffer); 6 Z( l! |; q- y( F: T% _8 T
return 0; 6 ^% D) T; w n# o, d
} : t* p; ~2 w% }9 @, k( C7 w
/ @. [0 I2 V/ L4 A+ D# e- _$ K
</FONT>
. Q) S2 H6 d( k' ^</P>% o$ ?3 ?. Q6 T) I$ S: ^
<P><FONT color=#ff0000>函数名: strcmpi </FONT>8 X* r* R! K0 P1 y
功 能: 将一个串与另一个比较, 不管大小写
M7 U% F) V s" t3 p* b, |' w用 法: int strcmpi(char *str1, char *str2);
: V5 E/ ]3 y% K' M4 N; Y; _程序例: </P>% v$ k; e4 P+ L* V- ?
<P><FONT color=#0000ff>#include <STRING.H>
9 e f8 c' S3 H- H#include <STDIO.H></FONT></P>$ V& y5 |) T9 j u1 Z; O% n
<P><FONT color=#0000ff>int main(void) ! ?! t1 g6 \1 C: v& U8 e; S
{ . H T) |- \% U: ^9 k
char *buf1 = "BBB", *buf2 = "bbb";
6 ?( j; K7 M9 T$ T5 iint ptr; </FONT></P>
/ o, F: P5 [( L$ [. E- t3 A<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>( [, J4 r9 m$ n1 J4 i# a
<P><FONT color=#0000ff>if (ptr > 0)
5 Z, @, K$ D* \1 lprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>* v2 Q( U2 Q- H. v8 C8 y( J
<P><FONT color=#0000ff>if (ptr < 0) + f; ]1 ~( n7 G/ m+ Z) Y
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
2 |" z& R1 Z/ ^" a<P><FONT color=#0000ff>if (ptr == 0) @1 C9 {) @! N! l8 H& n- D
printf("buffer 2 equals buffer 1\n"); </FONT></P>
! C# v; Y9 }5 ?3 _2 D<P><FONT color=#0000ff>return 0;
2 M! W' P1 S$ S. t}
@% M: ^4 |9 [0 ^( m& {</FONT>
+ a- j$ n: f R5 h: V: C5 o2 b$ g6 u' a4 Y
</P>4 g2 V! @4 B% ]6 {
<P><FONT color=#ff0000>函数名: strncmp </FONT>
; h/ \2 A, m6 @. ?" }' k# j% Z功 能: 串比较 5 [ V1 P9 V) O, f
用 法: int strncmp(char *str1, char *str2, int maxlen);
; H; o2 b' I9 E8 V5 ~' J程序例: </P>! v: B+ y- N: m0 ^' V2 H
<P><FONT color=#0000ff>#include <STRING.H>
0 _! V6 L: g; l/ n5 m% n9 T# _#include <STDIO.H></FONT></P>
& e4 `2 Q: m6 O: K9 k. R4 |' s<P><FONT color=#0000ff>int main(void) </FONT></P>
0 V" e! n$ K" H& I$ d) m$ @<P><FONT color=#0000ff>{
, c: E( r, |- ?/ `char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
5 f: j) {, F9 B; mint ptr; </FONT></P>$ a6 X) M$ i1 u
<P><FONT color=#0000ff>ptr = strncmp(buf2,buf1,3); B F9 a+ w& E2 l- M
if (ptr > 0)
8 s5 w O7 M1 `! }% T Uprintf("buffer 2 is greater than buffer 1\n"); ! A9 Y7 X+ E' B" |
else / o, G) Y! h$ w+ R6 X7 D; o
printf("buffer 2 is less than buffer 1\n"); </FONT></P>9 {$ ?0 [7 Q* Z# x! R% d+ J
<P><FONT color=#0000ff>ptr = strncmp(buf2,buf3,3); / Q% z9 v- n, K, W; d' D
if (ptr > 0) 4 O0 m( d, e2 L0 n$ @' s
printf("buffer 2 is greater than buffer 3\n");
0 \2 `* c$ x3 a( F4 R" xelse
5 q3 i* p1 I' }( r% F/ @0 eprintf("buffer 2 is less than buffer 3\n"); </FONT></P>' ~- Y. B5 @" o! T
<P><FONT color=#0000ff>return(0); & Y0 q; x' I7 a5 d; r( g
} / a' j4 M U2 \1 q9 |. d
* M7 Z/ l$ _/ \. i1 W</FONT></P>
5 `2 H: [3 N8 b5 o; ~1 _9 T9 Y! H<P><FONT color=#ff0000>函数名: strncmpi </FONT>
. y0 \: O8 m" T `( Q1 Z: k$ d功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写 6 q/ ]- M# h+ Z8 a; n, h" d; O( Z; J2 E! T
用 法: int strncmpi(char *str1, char *str2); ; g5 w; Z1 H* ?7 O
程序例: </P>
6 n' F7 h0 q/ D8 F1 m<P><FONT color=#0000ff>#include <STRING.H>+ m/ a, {0 l. u7 b$ {% w4 H, w# P, S
#include <STDIO.H></FONT></P>
% W+ G9 l X/ u. W: K. o<P><FONT color=#0000ff>int main(void) , A1 ]+ k2 a0 y! x# o
{
( r7 y7 Z& x- \: R u8 ?9 i$ Echar *buf1 = "BBBccc", *buf2 = "bbbccc";
, x0 L. K. A( l) lint ptr; </FONT></P>
$ B8 R0 d% O4 x' Q* @+ u8 j<P><FONT color=#0000ff>ptr = strncmpi(buf2,buf1,3); </FONT></P>
& I l7 h& i* X# Z) Q0 ^<P><FONT color=#0000ff>if (ptr > 0) . n" ?( [1 c' p
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>- E1 }- X9 Z6 }* n5 Y
<P><FONT color=#0000ff>if (ptr < 0)
* @7 ?# O( c7 rprintf("buffer 2 is less than buffer 1\n"); </FONT></P>5 O' U H+ K m- a, |7 e5 a
<P><FONT color=#0000ff>if (ptr == 0) . H8 j5 ]. S$ M/ S2 ? D, P
printf("buffer 2 equals buffer 1\n"); </FONT></P>0 x# O/ Z" h9 I' F. M
<P><FONT color=#0000ff>return 0; </FONT>, \6 a3 m" V# \7 a# |, k) v
}
. i+ E S* L! e
! Z. S* m2 P3 f( B</P> Y. {, i2 E6 K- i7 A, u/ K
<P><FONT color=#ff0000>函数名: strncpy </FONT>
/ j6 I+ S8 V5 Y* I功 能: 串拷贝 6 G+ s3 W: B! i- W; u1 r4 R
用 法: char *strncpy(char *destin, char *source, int maxlen);
7 S8 ?% M& A+ _程序例: </P>9 s1 j* J+ X" E. m0 C, P
<P><FONT color=#0000ff>#include <STDIO.H>
( S' G* u/ }0 p- C#include <STRING.H></FONT></P>: ]% @0 d- Q2 t, \2 D# Y
<P><FONT color=#0000ff>int main(void) 1 f8 P2 [' ]5 ]( w, {
{
7 Z. W( g5 l a$ ~/ e# R" E4 Wchar string[10];
4 w& x" {' ^5 v; G- _! e Rchar *str1 = "abcdefghi"; </FONT></P>
" Y( y- k4 q. \$ w2 z9 ^<P><FONT color=#0000ff>strncpy(string, str1, 3);
3 {" P( {0 ?5 A `! f/ m; J0 i. [6 Mstring[3] = '\0'; 2 B; e; [) f4 m |3 S4 J0 v+ r2 ^
printf("%s\n", string);
: d9 o; h/ Q, i- i' Ereturn 0; / X1 L [: M. I/ k
} ! x% _# {! Y$ S, Y% [
</FONT>
, X) ?. g, c; Q. I2 C2 u# G: \, _4 H</P>: L. }( |. ~( }) q4 Q) l, F
<P><FONT color=#ff0000>函数名: strnicmp </FONT>' W/ C& b& F- [; N% y5 A
功 能: 不注重大小写地比较两个串 + n0 z5 Z1 P l% ]! Q
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen); 6 T7 b- _6 d5 E+ w& ^
程序例: </P>5 z" a/ L) U# Q: q1 V* r
<P><FONT color=#0000ff>#include <STRING.H>' O$ \% s6 d+ G& N9 H+ T. ]
#include <STDIO.H></FONT></P>
" G! I! g, h; y<P><FONT color=#0000ff>int main(void) 1 O- w! W8 Y4 l
{
2 w5 z* c+ W* k+ Tchar *buf1 = "BBBccc", *buf2 = "bbbccc";
6 X: j3 V8 e' x. @- fint ptr; </FONT></P>
, V3 x/ k( g9 d6 S<P><FONT color=#0000ff>ptr = strnicmp(buf2, buf1, 3); </FONT></P>
1 Q1 b6 C3 _/ p% r<P><FONT color=#0000ff>if (ptr > 0)
+ A* Q1 g; W- K* T' V) `printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
K" o# w3 g' O3 s<P><FONT color=#0000ff>if (ptr < 0) 3 ~1 r9 Q- ^1 \9 X
printf("buffer 2 is less than buffer 1\n"); </FONT></P>2 N5 m2 M7 U7 L' E$ I
<P><FONT color=#0000ff>if (ptr == 0) ( i9 j h; S$ c3 G, {
printf("buffer 2 equals buffer 1\n"); </FONT></P>) {; [" {8 _' X* G
<P><FONT color=#0000ff>return 0;
* o2 X' O2 T2 c* a& |- \+ ?% h5 U} 2 R" q6 g0 _7 C# g4 a
( _! ]/ w: A1 ?4 ~</FONT>
) j! i4 s9 u$ u* W$ J+ A: R2 _</P>) v% K0 ]7 ^: z) \6 A4 S2 K
<P><FONT color=#ff0000>函数名: strnset </FONT>; q) S, S, |7 ~8 \1 s* K7 f
功 能: 将一个串中的所有字符都设为指定字符
0 ]+ t, L- ]) o Q" Z8 N, N用 法: char *strnset(char *str, char ch, unsigned n);
% d# g L- p! k! t3 H程序例: </P>
, K# G1 b" D! p, F% M/ W<P><FONT color=#0000ff>#include <STDIO.H>
/ ^; j8 A& j3 Z! l" E X1 \#include <STRING.H></FONT></P>, Y* B, h2 ]6 x& x
<P><FONT color=#0000ff>int main(void) ! o& i- g5 A' w2 g) U8 T$ D3 z
{
7 R s) s7 [8 {0 t' Y% P2 `char *string = "abcdefghijklmnopqrstuvwxyz"; # a; Y5 C" D0 x8 R: ?
char letter = 'x'; </FONT></P>
! f: W* g8 }9 g9 }; d<P><FONT color=#0000ff>printf("string before strnset: %s\n", string);
8 P- U+ w% n0 ]/ i- q; Ystrnset(string, letter, 13); ! b) p9 _0 F4 R3 o5 }
printf("string after strnset: %s\n", string); </FONT></P>/ O3 w* Q e' q0 g" W4 ~( E
<P><FONT color=#0000ff>return 0;
' m+ _3 d1 l3 _* ?8 h; Z0 u" ]}
* f6 H- ?1 [4 r</FONT>
( N+ f n' ]) q- V# q8 y</P>! ]: s3 a- M* X$ d. f) _3 w3 ]1 {
<P><FONT color=#ff0000>函数名: strpbrk </FONT>
0 Q! ^% U+ D( @% k- ^功 能: 在串中查找给定字符集中的字符
# K" ?2 l1 d4 ]$ n# g9 w用 法: char *strpbrk(char *str1, char *str2); $ O' E, ^% x* R/ a# M
程序例: </P>
( u6 r8 \& L" r6 F<P><FONT color=#0000ff>#include <STDIO.H>: U5 t9 }/ I3 U$ L2 E: P
#include <STRING.H></FONT></P>% @% s3 k- n5 g, z7 T
<P><FONT color=#0000ff>int main(void)
u* p4 _ z% d5 V K{ ; j! N" ^) q z% V w" b; \
char *string1 = "abcdefghijklmnopqrstuvwxyz";
1 a! I5 j9 m2 N& Bchar *string2 = "onm"; + P9 g/ R( ?2 W! a; r: q5 f
char *ptr; </FONT></P>/ a" w# n! U V& u2 k" o# N
<P><FONT color=#0000ff>ptr = strpbrk(string1, string2); </FONT></P>
$ M4 p( R- D" d" w( k2 |<P><FONT color=#0000ff>if (ptr) $ v( K, Y- Q+ ?" |
printf("strpbrk found first character: %c\n", *ptr);
) L( U+ R/ A- R# h. l" O) Kelse * ` r: H9 N4 V) [$ o/ R0 Y2 b
printf("strpbrk didn't find character in set\n"); </FONT></P>% z. a( c0 ^9 I1 c
<P><FONT color=#0000ff>return 0; 4 O! j# G) }/ C+ q: V
} ; T+ a$ B: }0 ?
" R# q& F1 E' n- f1 B+ V</FONT>7 [/ N, A; \8 X
</P># [* i* |" q+ Z2 `8 `
<P><FONT color=#ff0000>函数名: strrchr </FONT>/ j. u1 q F$ d2 y0 C5 s# k" o
功 能: 在串中查找指定字符的最后一个出现
% i! Y, G) q7 C. l- } U用 法: char *strrchr(char *str, char c); 2 Z5 d i+ n: C; b) s$ p! U
程序例: </P>: l: f3 h- ~, p* ?9 k
<P><FONT color=#0000ff>#include <STRING.H>
G# v0 u6 C) I; q' ]+ ?8 {* d#include <STDIO.H></FONT></P>
5 A, X0 @5 ?$ h8 ]# c<P><FONT color=#0000ff>int main(void)
# I9 b9 j. h# Y2 y, U+ H7 f- K{
& ?5 _9 q; N7 X' ?0 B4 v4 ]# ?9 ?char string[15]; 9 |. f M# ]) ?/ j' ]! k% X
char *ptr, c = 'r'; </FONT></P>% M I- h5 U! e0 ?$ e$ j
<P><FONT color=#0000ff>strcpy(string, "This is a string"); 4 O- x$ y/ f F
ptr = strrchr(string, c); + f% a- V. d% l1 b
if (ptr) ; l( G6 R: {0 {" l- Y
printf("The character %c is at position: %d\n", c, ptr-string); . ~/ _4 e: j: | Z8 _
else 2 E, X; B2 ?8 o8 L# g3 y$ \
printf("The character was not found\n"); ! o( N2 y2 _) V& y! x y
return 0;
; B) \: C& H# f} </FONT>
: ?: j q0 d, Q( s0 I! A) W0 y) H, @
$ K! j2 A8 T* s' M</P>2 V8 I" m) ^/ n: S" J
<P><FONT color=#ff0000>函数名: strrev </FONT>" T. y7 j) E$ r
功 能: 串倒转
( r; F' {! h" C0 I, ^用 法: char *strrev(char *str);
' m# ~# Y) N' A, k: s8 ^程序例: </P>. A8 H7 ]3 X( r3 V* ]8 C% C: {
<P><FONT color=#0000ff>#include <STRING.H>
' ^. @- z g5 O2 [7 X; b#include <STDIO.H></FONT></P># Y( N- h! v4 P6 P: Y
<P><FONT color=#0000ff>int main(void) 8 P% D1 S; v8 h; {& s0 P
{
4 F. [2 }3 c& y c# V, y& fchar *forward = "string"; </FONT></P># G5 H4 P" B R. F, x4 N
<P><FONT color=#0000ff>printf("Before strrev(): %s\n", forward); & u; R1 G: a) p+ @3 J
strrev(forward); 1 W& Q2 o G1 W5 V. I* \
printf("After strrev(): %s\n", forward); # }: C" x% [ N$ Q5 ?* u- e4 {4 v
return 0;
3 _6 i2 A% U/ {} </FONT>
( r/ j5 A; b# q' W% E. B$ D</P>
8 [& h( w0 p5 K+ d2 g% }<P><FONT color=#ff0000>函数名: strset </FONT>
$ q5 L9 c5 l2 O% X1 c7 O功 能: 将一个串中的所有字符都设为指定字符
$ C/ w8 N% F* w. o a用 法: char *strset(char *str, char c); ) o( q. d. c5 m- G
程序例: </P>0 n4 E: ^7 o, y h" I
<P><FONT color=#0000ff>#include <STDIO.H>
7 j* T- t6 i. a#include <STRING.H></FONT></P>
2 B3 j7 a: a/ ?6 { E<P><FONT color=#0000ff>int main(void) & h; A0 P f+ G. t, D- h
{ : M6 \& f+ C: \# j
char string[10] = "123456789"; * r9 `0 m! L% e' W
char symbol = 'c'; </FONT></P>: W3 d' }6 M; |8 k% w4 R
<P><FONT color=#0000ff>printf("Before strset(): %s\n", string); / Q* g9 Q$ @8 b7 {0 V) j
strset(string, symbol); # m) |* A5 \0 D1 Y
printf("After strset(): %s\n", string);
& c- Q% e1 [- @8 W+ Ireturn 0; ' e# C) K" f R; G
} 0 p# J# ~% i. T; T4 @
" J# \# c7 W1 h K</FONT>
9 z5 m, t! x" J1 s( ^</P>* I. b3 S; V# v
<P><FONT color=#ff0000>函数名: strspn </FONT>- b8 {# \) l$ m- ~
功 能: 在串中查找指定字符集的子集的第一次出现
+ B3 }, g1 Y9 n7 J* j" v用 法: int strspn(char *str1, char *str2);
# B9 f& U5 D3 [; Q/ B程序例: </P>8 R: F/ b) Z. S2 K
<P><FONT color=#0000ff>#include <STDIO.H>+ O& C% B2 c! v5 l4 K% k; N
#include <STRING.H>) s4 D l, l& U3 d- }
#include <ALLOC.H></FONT></P>
$ P: W. D* }- V9 c( A t<P><FONT color=#0000ff>int main(void) * ?; }; \3 z. U) Q1 M2 l
{ - z& j, [" ?& q
char *string1 = "1234567890";
2 r _. y2 o% `2 ~# W& `% Q4 w$ tchar *string2 = "123DC8"; & B5 L5 f+ [, W$ w+ C. W
int length; </FONT></P>
! \, O0 `. q3 U9 A' z<P><FONT color=#0000ff>length = strspn(string1, string2);
7 w9 Y$ }+ u" C0 ~% k) sprintf("Character where strings differ is at position %d\n", length); 4 J# i0 P5 V3 W0 U
return 0;
4 n4 [1 _) B% t& v9 K2 O& T}
+ R# I. x0 }: X. Q</FONT>; r3 u9 i( n* j5 }1 P+ _
</P>/ Z4 @. Y1 `: U3 @5 e5 r0 d
<P><FONT color=#ff0000>函数名: strstr </FONT>- Y8 n& ~! j; E+ k+ I5 E& k- D4 J
功 能: 在串中查找指定字符串的第一次出现 ' L9 M" V9 V/ z
用 法: char *strstr(char *str1, char *str2);
. L& V' L) {8 p" w程序例: </P>
" ~: E, L, o6 V) [+ c& f) O<P><FONT color=#0000ff>#include <STDIO.H>
. P6 \8 N2 v. c4 Y& @#include <STRING.H></FONT></P>
' A: Z$ H3 ^% d# U/ L+ j<P><FONT color=#0000ff>int main(void)
. Y! i; [" f, M1 d$ z' D{
3 q) S& X- F) Echar *str1 = "Borland International", *str2 = "nation", *ptr; </FONT></P>
+ r9 P( Y. e& y# D9 v/ ~<P><FONT color=#0000ff>ptr = strstr(str1, str2);
% N: v9 A: L. k- Y) p( N+ jprintf("The substring is: %s\n", ptr); : k8 @: P: F: V+ a9 Q* Q& c S' y
return 0; 4 i l2 G/ t+ V k2 X( M
} </FONT>
" z& k" e! ?, l# \0 }' C4 [
/ F J' x, q- F5 d- z, {: ^</P># v1 M0 B( t! U
<P><FONT color=#ff0000>函数名: strtod </FONT>
& y& v" g7 V7 j: ]功 能: 将字符串转换为double型值
2 r1 S5 _ r9 _- b7 `4 s+ P7 K: S用 法: double strtod(char *str, char **endptr);
# L% x/ e) B* J- v5 l6 Y9 V1 l1 w程序例: </P>; R' V' C* @# P% |! X3 J
<P><FONT color=#0000ff>#include <STDIO.H>) g- v$ g& e% p" W
#include <STDLIB.H></FONT></P>
9 K# Q" R. {) B* _6 n<P><FONT color=#0000ff>int main(void)
! S# W6 j% ?' h4 t{
$ D# W6 l/ _) b* _: fchar input[80], *endptr; * ]& s! f* B) U' x7 H
double value; </FONT></P>+ g7 Y) W5 X% l- k: B! F
<P><FONT color=#0000ff>printf("Enter a floating point number:"); 5 h8 C5 M6 m" c9 ^0 u$ B% z
gets(input);
, @6 V; E2 ?- a. w/ Uvalue = strtod(input, &endptr);
/ O7 D) T4 s, Z# W1 r' `# b1 Uprintf("The string is %s the number is %lf\n", input, value); 7 r1 @ q( r+ V7 U7 {2 Q' X
return 0;
' h+ Q; d- W8 u2 p! x1 P8 G} ; ^% B7 s% e- g1 c
</FONT>
+ [4 h7 ^$ d! T: {) a8 J) |
2 f$ u- N3 m6 |* p4 E4 [) s5 @</P>
8 `! J3 `; M! a Q+ p- X# d<P><FONT color=#ff0000>函数名: strtok </FONT>& Q I6 ^+ q, `
功 能: 查找由在第二个串中指定的分界符分隔开的单词 + K1 q+ x. \1 M, i% `( v
用 法: char *strtok(char *str1, char *str2);
) l, R8 r& ]1 o- ]8 y程序例: </P>' b/ p* i2 r- h9 Y% u4 k
<P><FONT color=#0000ff>#include <STRING.H>
# v' Z6 z4 h* u" H#include <STDIO.H></FONT></P>% {: k+ A( ^9 c
<P><FONT color=#0000ff>int main(void) % y, E" \) y: W" y0 I
{ / a- H- m" @6 i8 O* Y
char input[16] = "abc,d"; 4 s' n8 ^5 j: M. J1 e. c! Q, j
char *p; </FONT></P>2 }( ` h. A3 A- [( q
<P><FONT color=#0000ff>/* strtok places a NULL terminator
6 z9 C2 Q3 q* t8 q0 t) Kin front of the token, if found */
5 J9 V( R( Z* T6 h9 i+ ?p = strtok(input, ","); * ^0 a6 L' t! F. c
if (p) printf("%s\n", p); </FONT></P>
) B9 }/ O+ t b+ N" Q3 O5 h* l- u4 ~<P><FONT color=#0000ff>/* A second call to strtok using a NULL
1 K- |, K- X c: Q: w: \as the first parameter returns a pointer 9 f8 A' j6 s- u
to the character following the token */
. x5 A6 E8 p; E1 ~% C# @3 O& op = strtok(NULL, ","); - ^6 g/ ?- E Z1 }3 D) ]$ a
if (p) printf("%s\n", p); . n) d" V: y3 g8 ^2 L2 m" x: |
return 0; 4 y2 u7 U. ]7 X/ L4 k% p3 Z
} + D& o! V' G1 O- Y
* F ~8 ] I# i4 s
0 M: d# ~0 @1 N$ i, i7 G& ~7 v</FONT></P>
7 a/ z7 m& F2 F. N<P><FONT color=#ff0000>函数名: strtol </FONT>5 M. D4 L0 S6 @' F
功 能: 将串转换为长整数
2 x" o# y6 G, l. a: Z9 A# N2 s用 法: long strtol(char *str, char **endptr, int base);
5 k! Q4 N! m0 \/ Z7 [+ D程序例: </P># T1 }6 P) x9 O5 V* |* ^) e# M
<P><FONT color=#0000ff>#include <STDLIB.H>
, {, i4 I9 K; Z#include <STDIO.H></FONT></P>
, d) K6 e! `2 C) y<P><FONT color=#0000ff>int main(void)
) v% [! I4 }% p2 v; C{
0 r, M" ~- x' a6 i! O$ R4 achar *string = "87654321", *endptr;
( i3 |5 J* @0 J5 ?1 C# \long lnumber; </FONT></P>
: g) t/ o2 Y/ n! n, T$ N2 F<P><FONT color=#0000ff>/* strtol converts string to long integer */ % N% Q0 z0 T# L5 m* k
lnumber = strtol(string, &endptr, 10); ( n+ m' M' j- R/ [# g, z* R, A a
printf("string = %s long = %ld\n", string, lnumber); </FONT></P>* @9 g4 d4 u( I+ {
<P><FONT color=#0000ff>return 0;
u' ~3 A, @+ V& I' T. I9 X} </FONT>* ^, N X* b, m4 A* w ?) T2 O2 f4 Z
</P>0 V- F$ g1 ~* n6 `* f
<P><FONT color=#ff0000>函数名: strupr </FONT>
9 ]1 r0 V% V6 A4 ~9 H; k功 能: 将串中的小写字母转换为大写字母
: ~% J1 e& u: t/ o6 t用 法: char *strupr(char *str); 8 U- Q: T4 h9 C" {' l- U
程序例: </P>! Y2 s( [4 \/ {! k8 G+ b+ m
<P><FONT color=#0000ff>#include <STDIO.H>" U. _4 }- d) F: k) U
#include <STRING.H></FONT></P>+ T. q2 S$ B) n9 B5 m# d
<P><FONT color=#0000ff>int main(void) 6 m7 S# I( |/ D! I, d7 i
{
: [/ o3 ~9 j0 dchar *string = "abcdefghijklmnopqrstuvwxyz", *ptr; </FONT></P>0 i+ B0 |% C" c' P4 ? {. s) J. z
<P><FONT color=#0000ff>/* converts string to upper case characters */
* X% c) ?$ G$ L( Sptr = strupr(string);
# C( u+ }9 [8 Y; tprintf("%s\n", ptr);
% J9 |- E1 ?) Breturn 0; 9 G6 E2 a3 A0 O5 _
}
1 m: {$ _1 j6 F; X; u, J8 a
7 j4 k+ X7 Z a! |</FONT>
/ L1 w0 O+ x, o" O</P>5 `9 S, m7 _; L0 f$ z1 \
<P><FONT color=#ff0000>函数名: swab </FONT>: m' d e6 z. ~0 s$ a" O0 k$ ~
功 能: 交换字节 3 h" D7 W9 _2 q0 ?3 [
用 法: void swab (char *from, char *to, int nbytes); $ H' g4 U8 R; \* x3 w; m" t: h4 Y8 j
程序例: </P>- X4 r5 F9 i. A1 m8 M/ k& f: D$ m! N
<P><FONT color=#0000ff>#include <STDLIB.H>1 T0 o/ N; X8 G* H* W9 U
#include <STDIO.H>( ^% ^3 v h: ?! ], e. z; X' g
#include <STRING.H></FONT></P>
- G& |! l' C$ u5 Y, `6 ?<P><FONT color=#0000ff>char source[15] = "rFna koBlrna d";
( R5 B! e8 q9 O5 i" S; \% L9 ?char target[15]; </FONT></P>
$ \ j3 s. f6 r1 J' ~, O<P><FONT color=#0000ff>int main(void) , f, j% h# R* Q9 O1 z
{
# w6 b5 m5 g) W/ y: aswab(source, target, strlen(source)); 8 Z* [2 |" @; E/ X% A$ l8 Y! m
printf("This is target: %s\n", target); ! r: Q9 Z0 v' ?, Y, k
return 0; T+ W* X0 ]$ { e: z
}
5 F' C, z- m5 N- @ j</FONT>
& W% O8 r" s! g, ^ Q n. {- p; N5 Y) W$ ~
</P>
3 m/ Z3 E) [; z1 Z0 I" V! w+ n<P><FONT color=#ff0000>函数名: system </FONT>3 [6 c, e% T! q) ~8 y5 D
功 能: 发出一个DOS命令 3 n9 {$ |3 z/ F+ e
用 法: int system(char *command); 4 G" r- p9 v) a2 S7 w- a7 R- @
程序例: </P># x6 I( c Z& }. a& P
<P><FONT color=#0000ff>#include <STDLIB.H> O6 f" l% S F( a* H# _# Y
#include <STDIO.H></FONT></P>
: e2 |5 u4 @) `6 L* Z<P><FONT color=#0000ff>int main(void)
! u+ W% Z: G1 K: t/ S% ^{
; O+ C6 I6 J% b) X0 vprintf("About to spawn command.com and run a DOS command\n");
) w* h. a1 u9 O$ @, B' m8 j2 _system("dir"); & B: K, ^; D1 d* z* `2 n; [8 X
return 0; & F. M9 U! y9 F" `+ p2 M: z
} </FONT></P> |
zan
|