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