- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

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