- 在线时间
- 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>函数名: qsort </FONT>
, Z$ w* k2 p8 o! C+ k功 能: 使用快速排序例程进行排序
. V! s6 ]* p3 g8 ?7 I; U) u用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
l+ w! `" ^3 N程序例: </P>
- _4 \ ^. P4 w* l+ Q% K8 X< ><FONT color=#0000ff>#include <STDIO.H>
( _! ?' J' y) [9 @3 L' b#include <STDLIB.H>
5 }9 U: i7 v5 K' V0 t8 q+ M: E#include <STRING.H></FONT></P>
6 I# A% y/ [; c6 x% s< ><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
) O* B. L& i7 @8 W- @& O8 r% v, G% e" A< ><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
- ^6 A; d8 |: J. h1 b, p</FONT></P>
$ ]0 L I' N4 Z( `0 o2 p< ><FONT color=#0000ff>int main(void)
. F( u7 O2 |6 P8 F6 Y8 J{
# y1 U. v% {9 Cint x; </FONT></P>
: V9 v& x" B' A% c; u! h< ><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
S, u) |$ `. g5 n* ]for (x = 0; x < 5; x++) ' a& p8 U5 ~" G0 I
printf("%s\n", list[x]);
5 t; O% {! Z1 o; w. y1 I! ^# _4 Dreturn 0; 8 f5 \" B; |& K2 `
} </FONT></P>
3 k6 V( U, q6 E/ o' `< ><FONT color=#0000ff>int sort_function( const void *a, const void *b)
1 o6 [: p4 H- p* S% A+ a{ 5 Q8 |; J. U/ Q2 z; }: y
return( strcmp(a,b) ); 7 d* R+ W! B7 ]1 G6 v
} </FONT>5 Y. ~4 X, m! W. N9 t5 |0 T2 b: H9 Y1 y- N
</P>! n0 |$ E0 C2 ]7 x7 R
9 O7 _5 _% p5 Z* S% o X" k: G" @* }
<p>1 @# f* |; x9 Q: r, E1 a6 b/ J- P2 n
< ><FONT color=#ff0000>函数名: qsort </FONT>! ^" v' ~/ @+ O/ c* ?0 H# H
功 能: 使用快速排序例程进行排序 6 b6 N! M8 w$ A
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
+ H8 R$ C6 p; r# z( ~6 f$ y程序例: </P>5 S1 b( h/ u0 |' E5 U
< ><FONT color=#0000ff>#include <STDIO.H>: S6 P4 h$ f4 `, v0 r: T
#include <STDLIB.H>
7 {1 S8 s) |' g2 T8 j- j$ H#include <STRING.H></FONT></P>
) q2 C6 Y6 Y2 |. x- o, w< ><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
4 U: [/ K4 \, n) L& M< ><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; # e( v, N: T# `5 ~, c S
</FONT></P>
' o" S5 Q. Z0 I7 B1 U' b4 H1 {< ><FONT color=#0000ff>int main(void) ' Q3 D1 l' s+ J- i! }
{
v. D" q5 b7 H d, a% a- o- Iint x; </FONT></P>; b; m1 H4 a- r' L: R/ R0 d
< ><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
5 s0 X8 c$ o X6 jfor (x = 0; x < 5; x++) 1 M4 R+ \. C: w
printf("%s\n", list[x]); / B+ o" B- V1 w, r# L8 N$ X1 O9 ^
return 0;
9 H5 R' b8 _% O0 a# s6 X3 M' s- f} </FONT></P>
& h h5 e W! ]! m. q* _8 `4 P< ><FONT color=#0000ff>int sort_function( const void *a, const void *b)
5 m( C. U. N8 `' r; g$ r: n+ z$ T0 C{ ' w! B' P+ j; \% R: c. h5 g
return( strcmp(a,b) );
6 ^& {5 o0 f- l8 I3 _ U% g0 W}</FONT></P># b2 ]' i9 J4 \8 `$ {/ ]( l
< ><FONT color=#0000ff>$ W- m5 t9 C1 s- l2 \3 x J
</FONT>$ t8 D; u% N& ^/ ~' k" d
</P>
3 J" l! F6 `/ p. O! _! \* {: y: s< ><FONT color=#ff0000>函数名: raise </FONT>
$ n) n7 j5 W- ]& M6 B2 a+ S* G功 能: 向正在执行的程序发送一个信号 . _3 K9 Z) | _2 v
用 法: int raise(int sig); 4 v: x k, r) ]& q( D, N
程序例: </P>
& x+ d" g2 Q2 v+ C% R: J< ><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
2 O, D& r, T: a/ J# g9 c< ><FONT color=#0000ff>int main(void)
$ U) `3 Q) U8 s W: T7 a{
8 \3 f0 |" [# pint a, b; </FONT></P>. k) w! c+ \7 ~& i P
< ><FONT color=#0000ff>a = 10; 4 K% u& D e0 {' J7 n
b = 0;
/ N M- p7 s- o6 ]+ pif (b == 0) . Z1 v9 E% U9 H! I
/* preempt divide by zero error */
0 Y1 L2 V: S2 e/ ?2 j- Draise(SIGFPE); ! ]: q, _7 ^2 y
a = a / b;
5 m# J- H: O3 _8 B% ]7 ]% G- preturn 0;
# Q* |4 n& r+ x4 a' A}
" {( A/ }" u" H3 j9 N9 P+ |</FONT>
) ]* J% x8 O9 U: ?* V, Q</P>* H! z+ T1 x1 b" H$ ]
< ><FONT color=#ff0000>函数名: rand </FONT>3 X: o9 T6 e. k3 E1 o4 y$ F- S
功 能: 随机数发生器
) ?8 ~# m5 S) k$ M6 n4 G用 法: void rand(void); 1 y5 u$ Y8 K/ c+ f. Q3 c
程序例: </P>
- t! q: n% V( c/ c) r R6 p< ><FONT color=#0000ff>#include <STDLIB.H>. U$ G. M' L, J$ ~; j2 a8 r
#include <STDIO.H></FONT></P>
7 H x) y! U/ y) ?/ x0 B- Z< ><FONT color=#0000ff>int main(void) 5 L. R0 p1 m% ~! j$ x
{ , {! s7 l* d: X/ Z* i! N) w
int i; </FONT></P>8 T% n, c5 w. M) H" U! Q+ k5 |
< ><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); " e' Y7 ~% H1 \/ G
for(i=0; i<10; i++) : o4 F, N7 i1 Q! @
printf("%d\n", rand() % 100); 4 Y3 a" v j" G- I
return 0;
% F9 N( z$ G+ q- W}
4 H6 U0 c7 D* C( M1 ^/ u& g4 w( v2 d w
</FONT></P>
% Q5 A5 K7 l# M( S, b< ><FONT color=#ff0000>函数名: randbrd </FONT>+ p9 s( b5 r. e0 o* m& Z- p4 a* P
功 能: 随机块读
1 m4 n0 w+ _! l& @7 f Y用 法: int randbrd(struct fcb *fcbptr, int reccnt);
1 A4 m) Z2 f9 r程序例: </P>! m4 E1 g. h) y& g+ ~% T3 f& O7 M
< ><FONT color=#0000ff>#include < ROCESS.H>
$ v$ u% r$ W1 ~& ~#include <STRING.H>
) [" v& H; D+ F2 J- [# ^# p; U& p: j#include <STDIO.H>
5 V: x. }) B( l6 x1 \/ v2 l7 {#include <DOS.H></FONT></P>
- A0 @! e" r9 H& K1 p, C3 r' g1 Z4 W< ><FONT color=#0000ff>int main(void)
' e! c/ _. G8 b" P( u8 |{ , M% E. P" Z: N$ d+ t
char far *save_dta;
* u( G* |( _4 w& `& A9 Uchar line[80], buffer[256];
& D: T# j: I7 m4 R: Mstruct fcb blk;
" z6 S& {' T' y$ }2 v; M9 hint i, result; </FONT></P>
1 I- C* c% v& S3 [3 _< ><FONT color=#0000ff>/* get user input file name for dta */
) i5 ~" l( p# Q! Mprintf("Enter drive and file name (no path - i.e. a:file.dat)\n");
% T+ M8 R* X) C) s' dgets(line); </FONT></P>
( _) R9 z9 H, W5 j: R8 U1 y' V% U: x( m< ><FONT color=#0000ff>/* put file name in fcb */ * z5 X# v# k1 W, ~7 |, Y* [
if (!parsfnm(line, &blk, 1))
4 O5 ?8 C I7 `) h# l) W5 }{ ; M! I0 `- |: R$ M
printf("Error in call to parsfnm\n"); 3 d( y$ l2 d+ h9 Q4 j7 Z
exit(1);
1 {# o5 x, V! y. W1 j) }6 p}
& J$ P* M) @4 p6 b) b* Aprintf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>0 j, f: e: Q$ e, H9 N( c
< ><FONT color=#0000ff>/* open file with DOS FCB open file */ ; z: W# ~$ U* ~, ]
bdosptr(0x0F, &blk, 0); </FONT></P>2 ?$ `/ J' z5 p8 |) D
<P><FONT color=#0000ff>/* save old dta, and set new one */ 7 t' ?4 {6 W3 L" M) k
save_dta = getdta();
9 |! k" B7 {" v- B- I j% r$ wsetdta(buffer); </FONT></P>0 ]+ `3 g. K3 a' {6 A B
<P><FONT color=#0000ff>/* set up info for the new dta */
i! V: d0 W% ?7 d) |1 Q5 v3 xblk.fcb_recsize = 128;
: G2 d& p6 ~- J, q* U w _. h" _blk.fcb_random = 0L; $ d( J, v+ H7 x6 M: }0 @
result = randbrd(&blk, 1); </FONT></P>! R8 Z" J, I, z. e( z/ @! s
<P><FONT color=#0000ff>/* check results from randbrd */
/ k! [4 X+ N5 K/ U: pif (!result)
3 F' l6 A& L! y: L. w) T5 aprintf("Read OK\n\n"); 3 s& ~1 Y+ j+ G; h- M
else
7 d* d$ o+ q. d; |) R{ # ?. Z4 {- m/ q+ C* c% R
perror("Error during read");
; g6 ~8 ^- A! Fexit(1); & S; I! j. O J! X- U; F' b
} </FONT></P>
: E+ k, |# b! F- `5 y6 o<P><FONT color=#0000ff>/* read in data from the new dta */
5 ~9 X. S, A5 j- y) ]( w0 xprintf("The first 128 characters are:\n");
! {' c, f8 e+ H2 H$ L# {- Tfor (i=0; i<128; i++)
; j( v" ?4 L$ @1 P5 @putchar(buffer); </FONT></P>$ R7 t& J1 Q# O r- Z
<P><FONT color=#0000ff>/* restore previous dta */
& d l. {' E$ D+ s+ Usetdta(save_dta); </FONT></P>4 o4 _' E0 q! `/ }- Y
<P><FONT color=#0000ff>return 0; * M, T0 z5 [2 A, V5 T1 O
} </FONT># Q/ `4 W2 [. ]7 `3 w
</P>
$ o3 @0 p( _2 O7 Q; C& T<P><FONT color=#ff0000>函数名: randbwr </FONT>
, G8 `9 H5 G- w P" v( f功 能: 随机块写
) m4 s$ @2 m$ B6 d7 q% I0 X8 |用 法: int randbwr(struct fcp *fcbptr, int reccnt);
" w4 }( c' h q程序例: </P>
; D: O& g$ H) L<P><FONT color=#0000ff>#include <PROCESS.H>% v0 ~, X/ X+ m7 ^% t7 N2 z
#include <STRING.H> N+ D8 f& f' d; K( a0 }! q1 B
#include <STDIO.H>$ C" H% N2 f. A
#include <DOS.H></FONT></P>
1 g. S. O$ r1 k U$ [, j1 `* x$ w. g<P><FONT color=#0000ff>int main(void)
( k+ H! L- C# j& i7 E9 D! W{ . T( X, C- K" g5 S8 A
char far *save_dta;
* R' h' X/ Z- F# \2 ?9 i wchar line[80];
) Q4 w8 S0 @4 M0 ]4 l( _char buffer[256] = "RANDBWR test!"; : Q- |; Q5 `- u3 J
struct fcb blk; $ t( F" l; z. ^( e
int result; </FONT></P>
8 n' o/ q$ X; T! g) |& k3 {<P><FONT color=#0000ff>/* get new file name from user */ 9 R- U b9 Y( E( r" e* J
printf("Enter a file name to create (no path - ie. a:file.dat\n");
( W1 A l/ Q8 h9 g; Z0 `gets(line); </FONT></P>, ?$ p- E( m9 h5 M$ o) _2 b1 C. f; L
<P><FONT color=#0000ff>/* parse the new file name to the dta */
# h( i- {& o) X* t8 H, aparsfnm(line,&blk,1);
5 K, @7 T5 K- t0 O- `printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
) Z& f% z" y; f6 d<P><FONT color=#0000ff>/* request DOS services to create file */
% n5 L3 w$ j2 A- z) H( _5 Eif (bdosptr(0x16, &blk, 0) == -1)
+ d4 l: H* j0 u5 H. w6 f- D, D8 j{ 8 k2 K% U8 \( B6 G! M
perror("Error creating file");
, s! A+ U) w0 G. O% l1 kexit(1);
/ F" J2 T" E- p( \) e} </FONT></P>/ W5 r; K4 A$ ?. |# H7 q& j H
<P><FONT color=#0000ff>/* save old dta and set new dta */ 4 ]3 x- l9 Z, V' U
save_dta = getdta();
: }8 A. L* I% z) Z% Psetdta(buffer); </FONT></P>8 s8 ^2 p7 B2 i. `- I( N- u/ Y
<P><FONT color=#0000ff>/* write new records */ 8 J' D, [6 V: u0 I8 X- M
blk.fcb_recsize = 256; ; H% {6 J; D% a2 P( a- d5 f6 j+ ^
blk.fcb_random = 0L;
0 _$ B/ l+ [4 W1 X+ qresult = randbwr(&blk, 1); </FONT></P>
h* x2 i: O$ x- c<P><FONT color=#0000ff>if (!result) - k6 u# s% Q" z8 O. S* J
printf("Write OK\n");
- Y6 c. v5 p; @/ e" [else
& G! d, ?) y: S* C; @{ ! T8 M1 y8 t* ?" k. U! Y8 e3 R: S) a( k
perror("Disk error");
, ? w5 l2 y4 `! d3 x3 Fexit(1); # Y8 e& g" l1 r- ~
} </FONT></P>+ Q6 A+ Y3 I: f, I; j* {& `
<P><FONT color=#0000ff>/* request DOS services to close the file */ 0 P$ M* I+ C5 T: o& y( Y
if (bdosptr(0x10, &blk, 0) == -1)
# g. d8 `1 R# _{
d) ^9 F; E* T* ]4 x6 Sperror("Error closing file"); - e( w" Y8 N: T. T% r3 r
exit(1); + C2 P6 Y1 I( _ }, E) w- |0 j X$ N
} </FONT></P>
' {2 X- b- ~3 y: }<P><FONT color=#0000ff>/* reset the old dta */ 3 i, a% u; g. y* J( ?% Y8 C8 F* G& W# p3 ?
setdta(save_dta); </FONT></P>
' f+ l. Y# c. o" q9 @<P><FONT color=#0000ff>return 0; 1 t& F" E+ }6 f/ J$ L' _
} + d. o2 h( Y& I9 e2 c2 X& f
</FONT>
9 ?% i7 ?+ H$ z</P>: y. h8 f3 `( D( a O
<P><FONT color=#ff0000>函数名: random </FONT>
! X! M' B1 o; `3 g. E6 i% M. {功 能: 随机数发生器 3 O4 r8 h, f; j' b! K
用 法: int random(int num);
: m: ]- S3 c) r程序例: </P>
" r* F4 O/ [- o" D$ p5 @# ?) s4 ^<P><FONT color=#0000ff>#include <STDLIB.H>
) }8 z( _2 ~. ^9 S. m9 ^#include <STDIO.H>5 L/ T/ Z8 p* t i% B& y M
#include <TIME.H></FONT></P>/ u, j3 H' q1 P# N
<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ 5 m4 G' D; F u3 O. a1 H! o9 Y
int main(void)
8 y0 u1 N% K& O0 _* ?- X4 W: q d/ S{
. M$ H" Q8 s2 b" [2 M$ Erandomize(); , c" X8 n# e6 ]
printf("Random number in the 0-99 range: %d\n", random (100)); ( A1 ]0 h+ t) B' W2 S
return 0; * \( P b$ R* ]! U, c
}
, |$ O* p; d, q. S( a/ ^</FONT>6 A& N0 N5 D) }; R8 }1 `
</P>
$ C: h+ b9 l6 s1 T7 w<P><FONT color=#ff0000>函数名: randomize </FONT>/ P# P4 ^% o! s4 _6 D7 f# Z
功 能: 初始化随机数发生器 $ D( n% c/ r: Z3 ?, H3 R+ b0 t7 z
用 法: void randomize(void); , P: z+ F1 a6 O( U; q9 y
程序例: </P>$ c/ O$ z) [+ o) x! b) r
<P><FONT color=#0000ff>#include <STDLIB.H>
$ x/ j. D0 V' ?5 M% _#include <STDIO.H>
8 r; y3 Y( H9 `+ |#include <TIME.H></FONT></P>
0 c. I, m4 m7 K& ^6 l<P><FONT color=#0000ff>int main(void)
1 U8 G. @8 Z7 M" d: A0 x+ Z{ 4 o- Y* {9 I \- X
int i; </FONT></P>0 }% f# J _: U$ G/ Z0 q0 ~- \# M& U% y
<P><FONT color=#0000ff>randomize(); - |4 C( |; F- E \ p @1 d
printf("Ten random numbers from 0 to 99\n\n");
0 Y& H6 i1 f: X2 Pfor(i=0; i<10; i++)
5 t8 Z0 ]8 Y+ N0 Xprintf("%d\n", rand() % 100);
/ `% d# P H7 O5 a9 ?" Nreturn 0;
4 E6 S Z3 k1 }}
8 A6 n2 F2 A* E/ H$ V# L' L</FONT>
: f4 s7 B* l# a. H, @5 H* p( ?</P>+ }' ^6 [5 j: F4 q% f; i8 _
<P><FONT color=#ff0000>函数名: read </FONT>
1 e. |* W1 }/ r1 @' t; @. u) ]功 能: 从文件中读
. n7 f" i# x( M用 法: int read(int handle, void *buf, int nbyte); # G4 W3 D( j! N9 r% a/ M* q9 U! i
程序例: </P>
, `4 t, S z( U( c# h5 D* I9 M<P><FONT color=#0000ff>#include <STDIO.H>* T0 o7 A3 M% f# C
#include <IO.H>
( W1 |# ~4 |: H0 ]5 s$ x$ o) V6 I5 m#include <ALLOC.H>+ B) e: o) i1 Z: g5 ~* h
#include <FCNTL.H>
% ?% D) z6 L l$ ~6 z5 {/ @#include <PROCESS.H>2 h" i( f+ l# f& Y4 `
#include <SYS\STAT.H></FONT></P>6 H( C9 y8 ^0 d5 k* S' g: }9 n8 b
<P><FONT color=#0000ff>int main(void) 1 `2 u7 p- j0 R- V1 S4 K/ E
{ " f" P9 G2 {6 n7 t( W
void *buf;
$ I; q2 {! w9 K7 P3 ~8 ~int handle, bytes; </FONT></P>0 h y6 Q. u; Z0 B7 B( k
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
" U# i6 x2 H& @5 \<P><FONT color=#0000ff>/* # E2 T) f" O! U) i3 ~
Looks for a file in the current directory named TEST.$$$ and attempts 5 j- Z3 w0 I, \) W9 z4 y
to read 10 bytes from it. To use this example you should create the
) H& q. m! U5 ofile TEST.$$$ ( j0 t! @' E# ^. r
*/ 5 N- B5 V7 D) \, G2 c
if ((handle =
4 h6 H/ S, X! u$ W/ d L; popen("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
9 @5 G8 K- @ {2 C1 J4 G{ ! @3 Z' }" O- z
printf("Error Opening File\n"); 5 Q5 s" B" T! B2 j1 n6 l3 X
exit(1); 6 |; p, B- f4 f- D( C! F4 s8 y
} </FONT></P>
: o$ E! t% a6 V) I8 @<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
0 t: u0 h/ q: I7 Nprintf("Read Failed.\n");
d5 {( r( P, nexit(1);
* G c0 B- C2 T- Q1 S$ L} * F6 x( w5 ?. D1 M" J
else { ' C" E; K8 F. ]2 t% `( Y
printf("Read: %d bytes read.\n", bytes); 1 J0 L2 G/ ~ R6 c7 [
}
9 t) I s% g+ o: _- e: X greturn 0; 3 C0 t' i: }0 f" s
} </FONT>* X& o6 r$ N3 |" V( B# S
8 k# v6 t) [3 U& e# J4 A8 L
</P>
: D+ I0 \* V, a+ m" j1 ^) I<P><FONT color=#ff0000>函数名: realloc </FONT>
1 c/ y7 f6 e" ~# s. O功 能: 重新分配主存 6 Y- x# ?1 S5 |3 X8 l
用 法: void *realloc(void *ptr, unsigned newsize); 2 u3 \- H9 u' d) Z" z$ o* B9 @
程序例: </P>
* a7 z! b3 b# o- Y/ t7 R<P><FONT color=#0000ff>#include <STDIO.H>8 t# e. ~# Z/ S3 m! n# x: `
#include <ALLOC.H>
, I7 i5 ]0 I1 i#include <STRING.H></FONT></P>
i$ n8 `/ W1 E, k: n& U<P><FONT color=#0000ff>int main(void) 9 t$ J+ |) l) y- I) K
{
; |0 H: Y4 W4 i+ Y& j9 O" S7 x) y8 _char *str; </FONT></P>
* E7 R) i5 y, Z) t1 S8 a<P><FONT color=#0000ff>/* allocate memory for string */
, z* H5 A1 H3 l6 m: E# s' jstr = malloc(10); </FONT></P>
8 Q6 e. X5 v/ b: {% y# A/ r/ \<P><FONT color=#0000ff>/* copy "Hello" into string */ 7 A" h2 b. v( R( W
strcpy(str, "Hello"); </FONT></P>3 H* P8 O+ X! V/ b/ |
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str);
9 {" H5 f( p- a; d5 W' @str = realloc(str, 20); ( E4 p2 x4 B& L& r% P- P- f8 w
printf("String is %s\n New address is %p\n", str, str); </FONT></P>
2 W' I' H* z( q: q<P><FONT color=#0000ff>/* free memory */
[. v( p, X- d( y. rfree(str); </FONT></P>' P& U# c8 U3 x7 m- c" _2 ^
<P><FONT color=#0000ff>return 0;
" v/ @5 m7 U2 j0 h/ h6 N}
- g9 H8 O( t$ t4 V9 o: L* ~</FONT>
8 p7 r! p$ \( ~1 D( p+ r</P>; _. D% y& X7 q& \8 |
<P><FONT color=#ff0000>函数名: rectangle </FONT>
9 T: N+ I2 H* D/ K9 i功 能: 画一个矩形 6 T1 z+ J* v9 E: ^8 e3 Y; L! \
用 法: void far rectangle(int left, int top, int right, int bottom); 4 Y' D# O; {" n# L. _, Z
程序例: </P>
; @7 O, g9 r) o ]! D- `3 D8 T<P><FONT color=#0000ff>#include <GRAPHICS.H>3 f% f) i: G6 u1 U( {/ m
#include <STDLIB.H>
x5 {$ d$ ]3 V9 {% X C#include <STDIO.H>3 j7 ^2 j6 z9 u5 L+ h
#include <CONIO.H></FONT></P>) b3 P- K3 p8 {
<P><FONT color=#0000ff>int main(void)
; S' [ B3 W* D. x0 k) E) Y{
- W1 [/ m: d( ]! q/* request auto detection */ ! D: A" x! k3 d
int gdriver = DETECT, gmode, errorcode;
+ N5 c7 I4 T. e" Oint left, top, right, bottom; </FONT></P>' E7 y7 Q5 p2 A9 x2 ~6 k
<P><FONT color=#0000ff>/* initialize graphics and local variables */ : ~6 K- ]) w: G# T
initgraph(&gdriver, &gmode, ""); </FONT></P>
" F3 c- F, Z5 e9 f' b; ?<P><FONT color=#0000ff>/* read result of initialization */ 7 v' k& G& }7 v/ D* s
errorcode = graphresult();
4 ~2 ]( e1 O2 E( Z. ?- Hif (errorcode != grOk) /* an error occurred */
& A! l0 w! m7 {% s0 w+ W5 _{ ! O2 p1 L F6 M! D, \" Z5 f
printf("Graphics error: %s\n", grapherrormsg(errorcode));
! \1 F% C6 z* B9 jprintf("Press any key to halt:"); ) {3 B' [6 D/ \4 L+ k, J
getch(); , l* ]: G$ P% Z
exit(1); /* terminate with an error code */ - p7 e/ \( I8 K2 a- {8 c
} </FONT></P>
1 V" ~5 P, }6 |* Y2 Q; [$ S9 {<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; & T& ~, Q3 o, i% W/ b0 \9 T
top = getmaxy() / 2 - 50;
$ ?; D3 n% n8 \! y6 Y d% x2 ?right = getmaxx() / 2 + 50; $ w' d: N! C1 S' x- h
bottom = getmaxy() / 2 + 50; </FONT></P>- Y7 }8 B6 \/ |
<P><FONT color=#0000ff>/* draw a rectangle */ % N, w6 j* f/ J7 P
rectangle(left,top,right,bottom); </FONT></P>
# I- l$ {) C- B* R7 M9 y<P><FONT color=#0000ff>/* clean up */ : @8 A) } S* G3 G
getch(); # N8 r/ l# h! ~" C" D
closegraph();
3 o4 C d! Q/ H& K+ r1 J, freturn 0; 8 o: k2 C: x/ j# U* Z( Y% k
}
. d0 P+ `& I6 z9 t</FONT>
5 c+ v! T% [/ K( d" s& R7 J</P>$ z6 S. ]: E" m9 `0 E0 P* C
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>
& P4 h3 q7 B- k& b" J功 能: 登录已连接进来的图形驱动程序代码 : Z7 L, S3 s/ A- L
用 法: int registerbgidriver(void(*driver)(void));
" O8 C! r) P: {程序例: </P>
! O$ a# Q& R- L( L<P><FONT color=#0000ff>#include <GRAPHICS.H>4 m G6 g' ~5 M. u: Y$ K% H# y
#include <STDLIB.H>
7 ^4 u" E f, u! Y! _#include <STDIO.H>
. t. N& k+ F& x, v#include <CONIO.H></FONT></P>
5 y' y* d2 r* z3 O: `<P><FONT color=#0000ff>int main(void)
% f. q9 V& w) \. L{
9 x3 V" Z9 L0 c- g7 h) @/ e- d/* request auto detection */
1 w' l4 ]/ W) m# ^int gdriver = DETECT, gmode, errorcode; </FONT></P>( S0 {5 a: k% U) j% R& L
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
+ H* K% G' z; | }. jerrorcode = registerbgidriver(EGAVGA_driver); </FONT></P>! W. f. m/ k# h1 d" N5 P
<P><FONT color=#0000ff>/* report any registration errors */
% A& z. {% `- `, sif (errorcode < 0)
7 }7 R! \+ F! t4 r3 q- J{
7 o! x* C! u: l! L' Eprintf("Graphics error: %s\n", grapherrormsg(errorcode)); l9 G3 g! T/ V X; I: n3 @3 P( `
printf("Press any key to halt:"); 7 A: a7 x3 i: u c t7 U8 q
getch(); # ^" T* s- i6 d4 `6 W; M
exit(1); /* terminate with an error code */
9 J7 \% M0 o1 J9 P" K- `. U} </FONT></P>/ I2 M, C$ h0 ^6 \/ d% Q
<P><FONT color=#0000ff>/* initialize graphics and local variables */
9 ~. q9 Z ~7 binitgraph(&gdriver, &gmode, ""); </FONT></P>
1 y2 B5 W* X' H1 n0 O- _- F9 l+ W<P><FONT color=#0000ff>/* read result of initialization */
& ?. N3 @. d* W- \/ R/ ^4 berrorcode = graphresult();
$ R7 I0 b* M/ i3 k+ @3 bif (errorcode != grOk) /* an error occurred */ 8 B9 g$ e2 u5 t# N' H' Q
{ / J6 V4 j9 H, }% E8 _
printf("Graphics error: %s\n", grapherrormsg(errorcode));
3 q9 q4 @7 j2 i! l& k/ Nprintf("Press any key to halt:");
& Z7 ?7 x9 w t N: Ugetch(); * j* r7 y* c" n7 r7 S v
exit(1); /* terminate with an error code */ / o- F$ }8 o1 \ P
} </FONT></P>, @; `$ _) s8 J X
<P><FONT color=#0000ff>/* draw a line */
1 w% A& p4 |. A0 d' |4 Kline(0, 0, getmaxx(), getmaxy()); </FONT></P>( B+ c# l( \: p: K! N
<P><FONT color=#0000ff>/* clean up */
9 z. M. C2 L* D# N* ]9 O! ggetch(); 2 q7 h6 [# f+ E/ d+ g
closegraph();
! X4 c' G$ j6 |return 0; 3 u# v4 |% e% E+ Z, G2 L j
}
' ^2 }. {3 h! T1 y) N3 \; `</FONT>
( }! n. J: t8 H. C7 o) S</P>
7 K( {( ]5 o2 F, e* K( f<P><FONT color=#ff0000>函数名: remove </FONT>
7 i1 R( h( p$ N! K5 G' `功 能: 删除一个文件 # G ?5 V+ j2 ~+ \
用 法: int remove(char *filename); : c6 e" g7 ?1 r ?6 h7 Q4 y9 t
程序例: </P>) O3 |6 W/ I' b# o6 V, _. Q* |) _# o
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>& A: ^6 K7 w% _9 j" Y- H# X; J
<P><FONT color=#0000ff>int main(void)
5 z* }" R9 a7 F2 g0 h" x6 v{
& }* H& v& j1 s9 F: I9 vchar file[80]; </FONT></P>
6 _* l6 k# {. C* f<P><FONT color=#0000ff>/* prompt for file name to delete */
; c: X/ y$ J3 }8 Q* s! Hprintf("File to delete: "); ' L# j. a3 V$ {7 @8 N, p$ F
gets(file); </FONT></P>3 G% x# W7 s' N6 |4 H7 w9 z+ }
<P><FONT color=#0000ff>/* delete the file */ 1 [0 @, f0 a( i
if (remove(file) == 0)
, \) G- g. p) Wprintf("Removed %s.\n",file); ( Z4 t& t0 M) `
else
# a( n* w+ v7 @/ [perror("remove"); </FONT></P>
7 o% f% I' I6 B5 T* ?" P<P><FONT color=#0000ff>return 0;
2 y* |& {' m6 G r}
; v( H4 R' M) H( Z
# f! N4 k7 l0 ~, y) L</FONT></P>. e' U- e. ~: g f
<P><FONT color=#ff0000>函数名: rename </FONT>
7 e4 o, d; _5 `4 T功 能: 重命名文件
1 c( k/ V: C7 s# O用 法: int rename(char *oldname, char *newname); ' @ `* R& a, _# L7 }# D6 K
程序例: </P>4 q. M9 ]! K2 h+ Y' {$ [$ j2 x
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
i, J% S6 B* O" L% i<P><FONT color=#0000ff>int main(void)
: I5 m- d' l3 J{
, D" @; x. x/ o2 z' U( bchar oldname[80], newname[80]; </FONT></P>* W- T! d7 L9 A+ n3 S) k
<P><FONT color=#0000ff>/* prompt for file to rename and new name */ ) r+ p' W$ M0 N/ O7 @: i3 c: T, W
printf("File to rename: "); 8 E( a' V* w9 }% w: b5 \% _
gets(oldname); 2 j' K" x3 c0 H# w
printf("New name: ");
, z/ K* Q7 r- I5 e' P+ ?gets(newname); </FONT></P>3 o3 y3 f; p* u( i: w3 L
<P><FONT color=#0000ff>/* Rename the file */ ! K/ }9 z( G5 {2 `3 f% T6 { e# |
if (rename(oldname, newname) == 0)
$ @) I6 w! G7 ^3 {& A9 Q/ Y1 _printf("Renamed %s to %s.\n", oldname, newname); : _! C" Z# a4 y( Q& _
else
- m/ U- e# T- Y+ C2 p! ~/ rperror("rename"); </FONT></P>, g D# J6 n5 ]
<P><FONT color=#0000ff>return 0;
' O* w0 c" p1 f9 v}
" `1 b( }; C V9 ?, u7 e7 k ~
9 q# W1 {' H f; h( k6 m</FONT></P>. S4 ~- e- `+ e) D
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
0 ]- O F2 \6 W0 g- z功 能: 将屏幕模式恢复为先前的imitgraph设置 * N) ^5 M( y5 E3 l( s j+ F# S
用 法: void far restorecrtmode(void);
! q' j# J) F$ o0 x7 b程序例: </P>
5 j- O1 f& I2 E2 j/ Z<P><FONT color=#0000ff>#include <GRAPHICS.H>2 @2 J4 U5 T% V& c, t; Z
#include <STDLIB.H>7 }& A3 u8 g: m$ D! [
#include <STDIO.H>) W* F5 u/ X. U. k' W0 I# t
#include <CONIO.H></FONT></P>2 A( a# z% a/ n7 z- ^) V) H+ Q
<P><FONT color=#0000ff>int main(void) c' ]8 y, p5 G/ y
{
8 j* T% D! k- S& k/* request auto detection */
# c) k% t& }( P9 N1 @" ^9 ]; Cint gdriver = DETECT, gmode, errorcode;
6 G" A, |# K+ f6 u1 ^! Z, ]4 }int x, y; </FONT></P>/ r- H( k/ A1 n0 l& g5 u
<P><FONT color=#0000ff>/* initialize graphics and local variables */ * Q. S3 O; _; N1 a/ ~1 d0 E' a- e# u
initgraph(&gdriver, &gmode, ""); </FONT></P>
9 k% c0 Y. B" s<P><FONT color=#0000ff>/* read result of initialization */
' o2 \) i: d! G% i+ G4 @. t" _. Y3 Qerrorcode = graphresult(); # _! f6 y- u) _1 [
if (errorcode != grOk) /* an error occurred */ ( W4 v6 D; `5 x h
{
; V( c- O* f& h: z8 aprintf("Graphics error: %s\n", grapherrormsg(errorcode)); & d7 d& J- q) b# V$ M2 b5 a
printf("Press any key to halt:"); : T( n" ?8 v# J3 K) e' a
getch(); 4 E6 O: \& U8 @2 F" E$ T
exit(1); /* terminate with an error code */ , m$ o5 `9 x% J0 x0 s( q" @, H
} </FONT></P>
# M1 P5 q' u/ ^8 _) e% Y<P><FONT color=#0000ff>x = getmaxx() / 2; + U& ~) G% O2 |4 R+ C8 B
y = getmaxy() / 2; </FONT></P>) Z( V" \0 k+ J! s5 E
<P><FONT color=#0000ff>/* output a message */
' V; _4 [# A; d7 ~- ^settextjustify(CENTER_TEXT, CENTER_TEXT); - R# p5 W) m1 ^; L
outtextxy(x, y, "Press any key to exit graphics:");
* b$ a" |/ G+ j' }getch(); </FONT></P>
\- j$ W/ {1 M5 V<P><FONT color=#0000ff>/* restore system to text mode */ - I& D% Y5 \( T: M' I
restorecrtmode(); p% k3 r- y+ L8 o& u
printf("We're now in text mode.\n"); 8 i; j8 D8 J; U/ L
printf("Press any key to return to graphics mode:");
1 I/ e8 i8 u3 O/ E3 fgetch(); </FONT></P>1 K+ U0 W& M B
<P><FONT color=#0000ff>/* return to graphics mode */ + r1 P3 F1 q" W9 s8 ?( U
setgraphmode(getgraphmode()); </FONT></P>. r' m1 K7 e. p# Z& b. Q
<P><FONT color=#0000ff>/* output a message */
w9 J2 F) D# S1 Bsettextjustify(CENTER_TEXT, CENTER_TEXT);
% }3 M( N1 v. U4 Bouttextxy(x, y, "We're back in graphics mode.");
* S* H# `' D# V, d' gouttextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>
1 T9 G- ?/ z9 @4 y' \ R1 u$ S" W<P><FONT color=#0000ff>/* clean up */
& p3 V' |( R7 K* ~% f* \! Egetch(); ' t0 i- ?5 K, B8 u
closegraph(); 0 y" _% o! B8 v V2 `
return 0;
0 z1 f# k% c E} ) x+ S2 D( o: K: Q
</FONT>
: ]/ V% k: M$ E, u2 V# y( O</P>
5 Y1 R5 G) f/ u2 s9 q<P><FONT color=#ff0000>函数名: rewind </FONT>8 u) F( O) X. V# S
功 能: 将文件指针重新指向一个流的开头
, d8 e7 ~/ S( ]; m用 法: int rewind(FILE *stream);
# X1 y2 E* s+ p7 w& u! @程序例: </P>
6 }( q" P+ w6 l k3 G/ D<P><FONT color=#0000ff>#include <STDIO.H>
, }2 F/ r6 A" Y6 t( k& O1 s* h#include <DIR.H></FONT></P>
- L4 [5 C3 `; ]$ y" V<P><FONT color=#0000ff>int main(void) 7 r. q/ j) |2 T1 V
{ 7 E0 j/ T8 q) ]( C" @* e, P
FILE *fp;
+ H8 V+ y. |) g } o7 K0 Nchar *fname = "TXXXXXX", *newname, first; </FONT></P>
% y! P' J/ w9 c# M$ Z5 m<P><FONT color=#0000ff>newname = mktemp(fname); $ J9 C; |2 O- f2 k* H% M: y9 k1 P
fp = fopen(newname,"w+"); 0 Y: i* G5 C0 o+ X* j
fprintf(fp,"abcdefghijklmnopqrstuvwxyz"); 1 r0 I% l0 A) c' }3 m
rewind(fp); , v! I# J( K8 C2 G
fscanf(fp,"%c",&first);
+ U3 a) D* S; x1 U6 lprintf("The first character is: %c\n",first); ) ^6 l. o$ o# m& b9 H3 N4 f4 T
fclose(fp); 0 H7 v( u% U0 ~ d# ?1 R
remove(newname); </FONT></P>
0 ]8 P1 ?; d2 [4 e& [<P><FONT color=#0000ff>return 0; 1 W4 h r/ s4 |! a0 k
}
& r. @3 v% c4 o* m9 P2 \- b. b* w</FONT>
6 q6 _$ B! `3 v5 q% @0 t) K</P>* x8 T2 ~3 |2 T
<P><FONT color=#ff0000>函数名: rmdir </FONT>& K3 _) g3 f5 D# O; Q
功 能: 删除DOS文件目录
& B5 {& o8 ~3 W1 L' {; [3 F用 法: int rmdir(char *stream);
' m+ Q+ c ?4 u$ u, u程序例: </P>9 q& r3 h, m/ l5 x
<P><FONT color=#0000ff>#include <STDIO.H>/ |1 U- Z9 t ^8 z/ |$ b
#include <CONIO.H>
: t. e ]' ~: `" Y* r#include <PROCESS.H>( q) M9 | b. I" h2 C. z
#include <DIR.H></FONT></P>% e: k& P$ I* f/ g
<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>
5 _$ p! T) l* ^! n. B<P><FONT color=#0000ff>int main(void) , _& R4 a/ Y$ O; p4 z4 V
{
; z* K/ {' ]3 g3 q' o6 g6 }int stat; </FONT></P>
0 ?! \" x5 q; ~7 a. F) @8 }& l+ S<P><FONT color=#0000ff>stat = mkdir(DIRNAME); : [) i+ m1 l# @( m% ^# O
if (!stat) / G6 p* i" U/ c8 N1 a2 r. [
printf("Directory created\n"); 8 ]: K' U% N& c& w. O# w2 x
else - T, `/ N1 T1 K1 s& L* P5 C
{
* D' R$ j3 `+ s# e: I; Fprintf("Unable to create directory\n"); 7 m& h1 c6 ]8 N9 @. V6 W3 M+ l Z! a
exit(1);
/ l% R3 l- ^# L+ H f6 c, [% K} </FONT></P> o$ w3 M* S1 ?) }
<P><FONT color=#0000ff>getch();
$ @$ F! d) @8 |% Rsystem("dir/p");
8 ?9 r1 l; x; e& u. Vgetch(); </FONT></P>
4 j0 s, [" @7 c' |6 |* R* T<P><FONT color=#0000ff>stat = rmdir(DIRNAME);
b* J. r' [& O0 A) L% iif (!stat) 1 D$ d# ?1 |# l( w. a2 S2 T/ T
printf("\nDirectory deleted\n");
5 l$ r$ m9 D6 l2 u/ @6 [else ' w" z+ a5 _/ u8 X6 P# y
{ 1 g M! |7 x1 t/ x; H" R. {
perror("\nUnable to delete directory\n");
; K! m6 s6 _+ e/ Y+ Aexit(1); ( J: O4 X. j! I( Q3 u# g
} </FONT></P>
- u. |: f- M5 {2 s9 L<P><FONT color=#0000ff>return 0; 8 a" Y. \# A6 p
} </FONT></P> |
zan
|