QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1798|回复: 0
打印 上一主题 下一主题

函数大全(q,r开头)

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>- y; R  M2 Q& T% u# ?/ u, X9 r8 [
功 能: 使用快速排序例程进行排序 # \8 Z0 r. g) F/ `" U' v
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
: T. u) l) L5 `& d7 [  }3 L程序例: </P>: o+ K) h" Z$ @, I* V/ ]5 U3 _
<><FONT color=#0000ff>#include <STDIO.H>
6 D% ?" j; F! s- u2 V#include <STDLIB.H>) P  \2 I  D2 a  O+ `6 {
#include <STRING.H></FONT></P>6 b  @, Q& r  Z; k9 E
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
' g. @9 f$ g* l" O<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
( q' W; F/ s5 h  W</FONT></P>
- b# M( g0 m/ [2 a; }/ w<><FONT color=#0000ff>int main(void) ! Z- n, ~, J5 T4 m8 y7 y
{ : U+ y0 l! t+ v1 H2 I
int x; </FONT></P>
  `/ h3 b; l, S) _9 l<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
7 G. \, ]/ c! s; z/ B2 p8 D" O3 ^for (x = 0; x &lt; 5; x++) : G# x) s, b* v  v1 c
printf("%s\n", list[x]);
% v5 Y! M$ o4 V* E% J5 _6 I7 Breturn 0; ; C; t: ?+ j6 ]
} </FONT></P>6 [; F' x6 B; s, q7 c
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
7 |4 t( \2 X. L{ / Y* x4 l# z+ `  z- l& i0 q
return( strcmp(a,b) );
6 x% ?2 G9 U/ ]- l( z7 _} </FONT>
% \  v' q( x  f</P>
4 z% W! x0 ~, A0 B4 w/ L  @1 e  d; [5 x
: s' s5 E. m. r$ L5 L
<p>
" `# e) w0 e7 P: @- t& g, j<><FONT color=#ff0000>函数名: qsort </FONT>) H8 a  o9 a( k7 R  w2 x
功 能: 使用快速排序例程进行排序 - }: o0 C3 E! @
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); * i' _1 d, @: R% A1 ^: w/ J: w/ p
程序例: </P>; v# w& P' [, z7 `+ f8 I
<><FONT color=#0000ff>#include <STDIO.H>/ L4 K; Y: t- h5 E4 w2 l+ b. R; ?
#include <STDLIB.H>
$ o4 r4 y6 E4 M" O. r% i6 N#include <STRING.H></FONT></P>
: M' b% f8 p" p2 J; m<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>7 e: k# [, H" U! L+ i' O: H1 X
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
* E1 J5 N6 z8 @3 X. @  K</FONT></P>  [7 O9 L. ?' {5 ~2 O- X0 Z
<><FONT color=#0000ff>int main(void)
' Z# i* E  ]* h. ~{
; r4 M, K% V3 `% y5 M( Iint x; </FONT></P>: ]1 A1 g9 O$ t3 O  ^0 ~
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); ( [. I- U0 O7 C. j
for (x = 0; x &lt; 5; x++)
, E( `6 D! I- S3 V  g% v' T- i' Uprintf("%s\n", list[x]);
2 |8 [0 b0 ~) q2 l  x! O; Areturn 0; 6 H: Z- s" ?: `
} </FONT></P>
0 V% `' u9 ^% @, ?- e<><FONT color=#0000ff>int sort_function( const void *a, const void *b) % ?0 @6 K. o" Q& @. P2 W
{ % q! D( B& s! B+ Y. J# v( R
return( strcmp(a,b) );
8 M) I7 i6 v* G) n}</FONT></P>
& p4 G: ~; S) P! a+ Z1 l<><FONT color=#0000ff>+ N9 I. |  A9 q4 y( c2 B
</FONT>/ N& |- Q9 h0 i, ]1 d
</P>
* e' q# E. T1 f) Y& s4 e6 ]<><FONT color=#ff0000>函数名: raise </FONT>6 e+ A/ f5 ]% L1 g
功 能: 向正在执行的程序发送一个信号
9 B& s9 Q/ W4 R* |用 法: int raise(int sig);
* _9 Q7 `) ]+ X! b. b1 l  Z程序例: </P>
4 _. @4 u, T' }/ I<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>$ w7 @0 Z& w4 T+ g' `- u3 [- g# W
<><FONT color=#0000ff>int main(void) : [, z4 y; F& U# w) b7 s
{
1 [8 S# S" [5 q& w) F. e. p* @" jint a, b; </FONT></P>
2 ], e+ c1 m' N( X# H3 F% Z<><FONT color=#0000ff>a = 10;
+ ?3 P+ M* Q8 y, Y7 I; r, Tb = 0;
  C5 Y$ E% `! {7 t0 Xif (b == 0)
+ F" |: T# l" \: l/* preempt divide by zero error */
# g( O, r1 ]) R, eraise(SIGFPE); : X! C" |( F% O
a = a / b;
" H3 |( @, j2 E/ H- v% n. ^1 preturn 0; 6 I- B2 g  V+ Q4 n
}
& G  m: T9 q3 Z0 T</FONT>
% E8 }4 P% j1 _8 Q8 }: Z</P>
- V# N5 n& [; F7 U<><FONT color=#ff0000>函数名: rand </FONT>
$ F8 U/ U9 }( e4 K( v功 能: 随机数发生器 1 W+ t' w6 ^7 T. J  }3 }3 a% U
用 法: void rand(void); - f0 F* O( _! {4 g1 }6 T: s
程序例: </P>
9 l/ \: O6 t9 ~) q: M. V<><FONT color=#0000ff>#include <STDLIB.H>) d* c) g: j  \! s) U7 v4 @
#include <STDIO.H></FONT></P>: D- J3 d# o; \8 W2 p' F. E2 k
<><FONT color=#0000ff>int main(void) * I) ?0 p' Y2 f2 t
{ 2 e9 G) s/ {; G. M& w5 H
int i; </FONT></P>
2 z. d( D2 c- `$ g- y<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); , R7 I4 ^$ W4 g6 B
for(i=0; i&lt;10; i++)
2 I! c( L% J& |/ P9 o% C1 a: uprintf("%d\n", rand() % 100); . W# @& A/ f: U  Y
return 0;
. o/ e- W, S+ g3 e}
( x  ^! D1 P2 h( N+ F8 A& k( w# D3 R- ^( r4 a- u2 c) S, B% S
</FONT></P>
& f- S3 }: K) e, q6 A<><FONT color=#ff0000>函数名: randbrd </FONT>
1 c9 W# i0 W8 b2 P0 c功 能: 随机块读 1 t0 B* W6 G' f% p) L! w8 i. P  U
用 法: int randbrd(struct fcb *fcbptr, int reccnt); ) ?! A9 ~6 \% ^
程序例: </P>
2 E6 r6 p  g$ u* S+ n<><FONT color=#0000ff>#include <ROCESS.H>, j. X) o# T6 N5 r+ Y" V1 @
#include <STRING.H>
/ m' [4 x$ [. g! [' A#include <STDIO.H>
2 n- C' N9 ?( X+ E! {& m& D3 K, ]#include <DOS.H></FONT></P>
8 Z6 g+ I' C. G' r( k! t<><FONT color=#0000ff>int main(void) - [. w1 p) Y% `. f+ ]3 T6 n
{
0 ^0 m  h- d* [char far *save_dta;
1 a; W) N7 Q( f8 c' j+ t" d; y1 Cchar line[80], buffer[256]; , P1 K  o; o6 N8 V7 I1 r+ Y# {" d
struct fcb blk;
& N' V% K. z9 B( l. M$ L, T$ s+ ^. sint i, result; </FONT></P>7 s. T1 \/ P3 \5 b- S# ^
<><FONT color=#0000ff>/* get user input file name for dta */
" H* x, P6 k5 T6 x% Yprintf("Enter drive and file name (no path - i.e. a:file.dat)\n");
1 B/ G- p3 I' d7 pgets(line); </FONT></P>
) N! ?( X2 Q2 y) E' ~# M<><FONT color=#0000ff>/* put file name in fcb */ $ S& W0 X1 a9 l- D
if (!parsfnm(line, &amp;blk, 1)) . ~: `1 v, e! X% H7 ^
{ . C+ [! S8 V! N& m' ~. ?& W
printf("Error in call to parsfnm\n");
* S6 z2 j. F2 _8 Jexit(1);
- M- _# W" u4 Z4 Z4 C& N+ L% H+ ^} + d, Q* M& |# @; p8 `
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>$ [  ~9 k: }- ~
<><FONT color=#0000ff>/* open file with DOS FCB open file */ 2 V1 }; h( c: S: B1 W# j1 i2 _! \
bdosptr(0x0F, &amp;blk, 0); </FONT></P>
4 G5 A, N9 Q7 B<P><FONT color=#0000ff>/* save old dta, and set new one */ ( }; b$ ~- Y3 |# d
save_dta = getdta(); ' w* _# m& D* v( D5 ]) I
setdta(buffer); </FONT></P>
% k3 @8 {, d, O8 b<P><FONT color=#0000ff>/* set up info for the new dta */ - G8 f2 k" y, r
blk.fcb_recsize = 128;
9 I1 ]4 a5 F, T' @( J( A) e+ X8 Kblk.fcb_random = 0L; : @/ y/ t" H) j
result = randbrd(&amp;blk, 1); </FONT></P>" F9 W6 ~, U0 K, y3 U' s
<P><FONT color=#0000ff>/* check results from randbrd */ 3 y; q2 P2 a' F3 W% \
if (!result)
- R3 S  m+ s- Q6 W; g3 S, @7 Dprintf("Read OK\n\n");
7 [" o0 L6 f' b/ S/ x- {- Y, melse
! b) W" o9 Y' I3 P) k{
2 J! X) J% ^! {! M! c" v) Eperror("Error during read"); : [6 A. K, G9 r
exit(1);
' x6 s6 W% p$ P" O- v9 Y} </FONT></P>
+ K  |3 I, w: L' @* q<P><FONT color=#0000ff>/* read in data from the new dta */ ' v8 d! c/ A+ X
printf("The first 128 characters are:\n"); " I* n/ Y4 C' v
for (i=0; i&lt;128; i++)
' [* t; V* z( u% p" I0 m6 K% X5 Rputchar(buffer); </FONT></P>2 [6 ?* d. r0 Z
<P><FONT color=#0000ff>/* restore previous dta */
+ W" m, j% b1 y8 msetdta(save_dta); </FONT></P>) i2 n0 E! t/ |* J: A. W8 ^
<P><FONT color=#0000ff>return 0; & j4 W3 o9 q! G0 P5 s1 J+ Y8 S9 K
} </FONT>) y6 k! ^6 P; E# P+ Z
</P>( Q3 ?2 b, k+ r' N
<P><FONT color=#ff0000>函数名: randbwr </FONT>
! Y( v  }6 ~8 Q, [功 能: 随机块写
* b, H  g5 I- p7 U用 法: int randbwr(struct fcp *fcbptr, int reccnt); # k5 f) l1 M" ~  `8 D& g4 \3 G2 g
程序例: </P>
  |4 q# C+ f+ j# K0 v: ]# Y2 Z<P><FONT color=#0000ff>#include <PROCESS.H>
7 o2 @0 e- ^2 `( [# v- T2 i#include <STRING.H>1 l: z) ]+ J! o
#include <STDIO.H>
  l' L" }$ i, W# m7 M7 A4 h#include <DOS.H></FONT></P>& l2 }4 s! g% t; Q. W. p
<P><FONT color=#0000ff>int main(void)
5 U. ?9 j* L9 i1 n$ y$ D+ R{ % ]3 p0 }8 o7 Q  F! g4 _
char far *save_dta; 4 S/ @0 m3 _: N
char line[80]; $ u! O3 X3 l/ t1 S# j" e6 m( ?  k
char buffer[256] = "RANDBWR test!";
! h% ~7 ]$ c( v2 bstruct fcb blk;
5 J- F8 R3 W4 L: ?5 s8 Qint result; </FONT></P>
% I1 w4 Y: s/ Q<P><FONT color=#0000ff>/* get new file name from user */
9 Y* J5 v, l9 m: Mprintf("Enter a file name to create (no path - ie. a:file.dat\n"); . y7 O+ P% U- V) `/ L' M
gets(line); </FONT></P>2 `% I' d; O% _, Y6 V
<P><FONT color=#0000ff>/* parse the new file name to the dta */ * C' L: ?9 x( j$ ]
parsfnm(line,&amp;blk,1);
6 q3 x* q2 {, p1 P( r1 qprintf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>( a% S6 Q  b$ S: h! ]" q
<P><FONT color=#0000ff>/* request DOS services to create file */ 5 H; q7 t( c$ {
if (bdosptr(0x16, &amp;blk, 0) == -1) 8 `% O- Y* l8 z# H
{
5 j* o4 b+ `6 @8 z* c, c  b& Lperror("Error creating file"); / ?$ k: R' `. c, \9 S" [
exit(1); 7 W. r7 G) U- g+ ?% F( u% h8 m3 h) H
} </FONT></P>
4 r6 c- m' e' i# o4 B) N/ y<P><FONT color=#0000ff>/* save old dta and set new dta */   X( H, n& _) k
save_dta = getdta(); - B7 c7 R) h6 r: T0 X" f
setdta(buffer); </FONT></P>
, e6 c# u7 ]$ J9 Y; d0 m4 m<P><FONT color=#0000ff>/* write new records */ - n, G$ r4 q9 N" b: X' k5 T
blk.fcb_recsize = 256;   `1 T  g2 J  t
blk.fcb_random = 0L;
$ c& z# g' d* l0 J6 s$ M) V2 c( Nresult = randbwr(&amp;blk, 1); </FONT></P>
0 j' G4 p( K$ d- ?<P><FONT color=#0000ff>if (!result)
: R$ R. E# Q8 h& J. L6 W; K: z) H6 Xprintf("Write OK\n");
/ a, r% c) I  J- g+ yelse
0 x% e. y  A" Q- ?{
3 f! z; |! x+ o( N3 gperror("Disk error");
& c2 z- o1 a% ?1 }: T9 _; V# }) rexit(1);
2 b3 z. N' {# |* G" M- \, P} </FONT></P>( M1 c" j: N) y# u- h
<P><FONT color=#0000ff>/* request DOS services to close the file */ $ [% ]  O+ R7 A* H0 I: P
if (bdosptr(0x10, &amp;blk, 0) == -1) 0 I& S; Z! W+ r9 t
{ 1 B# t1 N6 C: [$ `
perror("Error closing file"); - J$ J  T6 M- Y3 C1 y
exit(1); ( o* l% Q, f8 m. d1 h1 B
} </FONT></P>
  L9 Z5 n# U9 g5 r  p. b) T( ^<P><FONT color=#0000ff>/* reset the old dta */
3 n: _% D% ?' [+ G0 m8 e- c, Tsetdta(save_dta); </FONT></P># J" E& n7 ?; u' Z7 F3 f
<P><FONT color=#0000ff>return 0; 7 M5 j) `7 I3 t2 g
}
+ r, v9 N, P7 i</FONT>6 i9 w8 O9 z; y* [. x9 _
</P>3 ~# b2 w0 T5 i& L- `
<P><FONT color=#ff0000>函数名: random </FONT>% F* m6 I* h# K) C4 u
功 能: 随机数发生器 2 E, |* n9 q% |! u
用 法: int random(int num);
7 c4 s+ J% U7 G/ ^1 Y; H程序例: </P># o' S( ~/ a% Y# q( h' i
<P><FONT color=#0000ff>#include <STDLIB.H>, B3 Z, r* y- r7 W
#include <STDIO.H>
( Z$ l" I. }' [1 ~" k- v1 x#include <TIME.H></FONT></P>
* w  o3 c% m5 p<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
8 r; g( L! ~9 Fint main(void)
+ {+ r5 N* H& @/ x6 Q: M' X, x{ $ ]1 q9 ^2 n4 d  ^# W3 C: z
randomize();
/ L- a" _0 l1 }7 Y1 e! }- aprintf("Random number in the 0-99 range: %d\n", random (100));
) H" M7 p2 }/ R3 j9 q: |) S9 O5 Preturn 0;
( R8 M( `* I2 z/ Y! l} 3 z6 L0 l8 A/ C1 D8 V
</FONT>
2 l; }" u, R2 d$ b+ d& k4 B7 N0 ~</P>& ]  O* w( s. p3 w4 Q; i
<P><FONT color=#ff0000>函数名: randomize </FONT>
# i6 I" ~; G4 i: X1 z功 能: 初始化随机数发生器 9 z; e- T" V. |  J2 B- x
用 法: void randomize(void); ) ~" J! t6 [0 |7 r
程序例: </P>7 |1 N7 `. O9 ~0 Q) \/ E0 `, ]
<P><FONT color=#0000ff>#include <STDLIB.H>
& V/ c- E7 h) D6 a8 s#include <STDIO.H>
# m6 H' X) }6 @0 V* O6 f' y8 l#include <TIME.H></FONT></P>2 [% A% G% v) p5 O  M
<P><FONT color=#0000ff>int main(void) * h4 k  z- o) k$ l7 V; H( B
{
; S9 W- S1 d7 L0 d2 l; U' [' nint i; </FONT></P># d3 Q/ h; C& E1 V: x3 O
<P><FONT color=#0000ff>randomize();
8 G( _' ]; M: x& z6 ]printf("Ten random numbers from 0 to 99\n\n");
3 u; V' v( s; v& dfor(i=0; i&lt;10; i++) : w# i+ S, E+ g
printf("%d\n", rand() % 100); : [: S! w. J- N* L) k" ?) Y
return 0; ) h! x" M1 ^$ P. ?. Y0 @. c# j' v
}
' k  O, w& x- \  f</FONT>
/ e" Q+ e+ r; O  Z- d" Q</P>
  D8 q: h0 G4 w* V9 T2 f<P><FONT color=#ff0000>函数名: read </FONT>
. p) |7 U! `( ~* \' \) S' l功 能: 从文件中读
1 M4 v1 j( ]: c5 J  ?用 法: int read(int handle, void *buf, int nbyte); - }2 [9 J% ]1 s' u- F: n6 [: t+ ^# r
程序例: </P>
; u% E* W' A. L, T  ^<P><FONT color=#0000ff>#include <STDIO.H>
* \! r' @+ U7 l4 w& K* u8 {& B#include <IO.H>
, [: z0 B5 D8 g#include <ALLOC.H>
: |0 O, X6 q3 U#include <FCNTL.H>, [1 E5 \, o4 u
#include <PROCESS.H>' o0 f8 H6 _+ E2 `4 Z* }
#include <SYS\STAT.H></FONT></P>
5 n( R- M1 K5 H- e2 `<P><FONT color=#0000ff>int main(void)
6 L4 m) z2 a6 M{
2 O5 ~  N: D9 Q7 n8 Lvoid *buf; : ~  I$ b, w; ]+ p# e6 p
int handle, bytes; </FONT></P>" r. U  [" R& Y( p/ S' m1 P) ]
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>1 O$ g) M0 j* _; A$ x' ^
<P><FONT color=#0000ff>/* , ?# e# C# s8 e0 v/ O1 m& Q
Looks for a file in the current directory named TEST.$$$ and attempts
. _$ p# Q- l+ X4 g' b7 A# d& Gto read 10 bytes from it. To use this example you should create the
- X0 B8 ~7 O! pfile TEST.$$$
- c( T$ z- h" l" q  L*/
" C9 P* q' @- D; ^: \' Yif ((handle =
3 [" s9 l% K/ E6 u* copen("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
: V/ l; j2 C0 F2 |9 {5 b- P0 |' Z{
% m- Z" a* s8 bprintf("Error Opening File\n"); ! i" S% K0 A- q6 d' D/ z
exit(1); 2 x- y2 P) }) K) m
} </FONT></P>
7 m0 y1 {% L, ]& K2 _7 G; U/ `6 C" e# w<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
4 ], I( W9 i! b$ @2 }- Nprintf("Read Failed.\n"); 4 d$ @3 Z+ s3 T1 E5 f% u4 y
exit(1);
* b$ F3 t3 K/ {( U} 6 T7 Q4 L" x1 E# Y; U
else { + ]0 B- N! i! u; c. C0 w; T8 P2 B
printf("Read: %d bytes read.\n", bytes); ; X" F$ q3 B) H% E( r2 C
} , B8 D4 f& @! ^- `+ ?3 G' K2 P
return 0; . I8 z. N& |* y7 J
} </FONT>
& c& O( M1 f& {: k- R( R* G
* v: P7 D# e3 U" b" y! C1 A</P>$ G4 K" O# K7 B/ M: j$ F) Z
<P><FONT color=#ff0000>函数名: realloc </FONT>: R+ s% s0 o3 J1 s9 j5 D3 ]
功 能: 重新分配主存 $ n7 }* y- @6 F4 w3 Y5 l- G
用 法: void *realloc(void *ptr, unsigned newsize);
; C* k/ j5 ]4 D. T6 D  d" R) V程序例: </P>* j- F, T3 C' x" C" N! y
<P><FONT color=#0000ff>#include <STDIO.H>& K& U9 G2 k8 e3 s6 f2 g! s
#include <ALLOC.H>
# y4 E# m1 x5 u% f/ h#include <STRING.H></FONT></P>4 ]5 f: ^  r& u9 z/ ^. S& @
<P><FONT color=#0000ff>int main(void)
: M! }1 b9 F. Q$ _0 J' L{
! h: G( [& Y) O7 Z4 p9 ichar *str; </FONT></P>1 T% A- ~5 f/ w6 i1 E
<P><FONT color=#0000ff>/* allocate memory for string */
) j! X# M" j0 K" K1 Vstr = malloc(10); </FONT></P>3 o3 G# U) r, J# t% I" G; F) W3 W
<P><FONT color=#0000ff>/* copy "Hello" into string */
0 i' d  s0 C8 M9 jstrcpy(str, "Hello"); </FONT></P>
! [# Q) L' c, H1 o% D3 m<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); 8 |2 c6 |( k6 X) g
str = realloc(str, 20); . J$ x3 p) h3 d" g
printf("String is %s\n New address is %p\n", str, str); </FONT></P>
! ]9 @/ H/ ^/ V; A<P><FONT color=#0000ff>/* free memory */
6 r8 \8 K7 b1 [free(str); </FONT></P>
: f  E8 P  K8 C2 F<P><FONT color=#0000ff>return 0;
9 i8 W& }4 y  c3 W} + j; ~8 w5 ?* j$ e/ |% ?
</FONT>
( g$ ~1 k7 O, d9 p</P>
6 E0 [( j, x! [( T# x7 ~<P><FONT color=#ff0000>函数名: rectangle </FONT>/ ]7 N% m; C) s, i/ w
功 能: 画一个矩形
1 l9 `+ ?8 f/ R# _1 b  O/ R用 法: void far rectangle(int left, int top, int right, int bottom);
6 [! Y' C3 d5 @' O/ B$ X" n& V1 e程序例: </P>
$ \+ W5 T- A& s  ~<P><FONT color=#0000ff>#include <GRAPHICS.H>3 s/ c! V) s& C5 K# B# ]% [
#include <STDLIB.H>
" s# Y9 F4 S$ [1 j) q% W#include <STDIO.H>
4 p- d- ?' L3 f( V2 U2 H#include <CONIO.H></FONT></P>
' n. R& k2 r+ [2 n: {" y/ s<P><FONT color=#0000ff>int main(void)
7 u1 I2 s* _  Y{ 9 I' d, L$ B+ N$ X% B: X
/* request auto detection */ % {! w1 ]& ^1 E
int gdriver = DETECT, gmode, errorcode; $ U, V( E- z8 H- W
int left, top, right, bottom; </FONT></P>
6 w  e/ q* r# ~# B: q<P><FONT color=#0000ff>/* initialize graphics and local variables */
7 F" ]- B4 p. \" r, U& {8 Winitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
% U- E% G# h; `<P><FONT color=#0000ff>/* read result of initialization */ 9 e9 v% @; L" o: P5 e2 Q  x* Q& [5 ?
errorcode = graphresult();
. x- M/ Q! p1 _; sif (errorcode != grOk) /* an error occurred */ - R  n8 m6 ?0 a: F0 n4 _: U. x! Q. x
{   Z+ _' l8 D6 }, l/ G6 @) P
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 1 C6 D- B3 ?) ^( R! s( F
printf("Press any key to halt:"); ) m( q! P. }9 Y, o  A/ O$ W
getch();
8 Z) H1 _  G4 g  q" [exit(1); /* terminate with an error code */ + t1 b( S  v' h; Q8 B2 \! I7 h
} </FONT></P>
6 ?: p* t( l' i0 |<P><FONT color=#0000ff>left = getmaxx() / 2 - 50;
1 l2 n8 p- @! p; @! _/ s; @top = getmaxy() / 2 - 50; $ i# `* N$ D1 l8 k9 n- F
right = getmaxx() / 2 + 50;   O4 w$ B0 F/ q6 H+ o
bottom = getmaxy() / 2 + 50; </FONT></P>
1 v& Q, |9 F% U& G7 o<P><FONT color=#0000ff>/* draw a rectangle */
8 V2 ^" U) c1 [1 W/ S  c/ k2 J( g& srectangle(left,top,right,bottom); </FONT></P>; c9 G# T" d$ \1 F+ w
<P><FONT color=#0000ff>/* clean up */
! r* G3 k! y% x0 e8 g/ K% q1 A0 fgetch(); % ]+ x4 h( y5 f$ q- v9 g. S6 k
closegraph(); / i/ v1 r$ M$ y. F5 |7 `5 z# o
return 0; 2 v4 R- J( f3 R; u+ u6 B
}
, q9 V3 j9 R# \2 m9 H</FONT>
4 P- v9 T  ~1 R4 u</P>
+ a0 N  O2 q# Z( e6 t8 Y2 c<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>- x3 n+ s3 k( i7 g9 m- Y5 e4 |
功 能: 登录已连接进来的图形驱动程序代码
5 ?3 m6 y5 A* T用 法: int registerbgidriver(void(*driver)(void));
3 n& k9 t9 U2 l9 _+ k9 I. _程序例: </P>" ^2 q& N, R& P7 w: n
<P><FONT color=#0000ff>#include <GRAPHICS.H>
0 N. w/ m) G& ~" ^$ @#include <STDLIB.H>
& T' d" r  j* u7 \# Z#include <STDIO.H>
" @' z) F0 q# J2 J#include <CONIO.H></FONT></P>
# l) I( i3 {( o. V<P><FONT color=#0000ff>int main(void) ' @& M+ ]) L% \2 p3 h
{   f$ V* M3 Z  W3 P% r# w9 d
/* request auto detection */
. `4 J% }( M  Y- u/ @. A- Aint gdriver = DETECT, gmode, errorcode; </FONT></P>$ E) T- T/ A2 T5 [" v
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ . ~0 A' j$ x  M. ^) W% h
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>. f% B) ?5 ]! c. p( L7 p
<P><FONT color=#0000ff>/* report any registration errors */ 5 Y: k% o: |5 T' c" u1 V
if (errorcode &lt; 0) % t( p0 W. @( N4 e! W5 w8 T& l  V
{ % t$ @  k+ Z6 Q# c# d" U0 N
printf("Graphics error: %s\n", grapherrormsg(errorcode));
% o: G& v' F+ ?: R' Zprintf("Press any key to halt:"); 9 \3 s( H; N4 ]* _4 x( f$ P
getch(); # y* I/ W  }8 E1 b
exit(1); /* terminate with an error code */
  ]0 i3 Y4 B' E4 {- V; H7 g} </FONT></P>
/ K: E7 q- |" Z% W3 I  t<P><FONT color=#0000ff>/* initialize graphics and local variables */
0 w- R9 Q/ Q- v8 |initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
+ S8 N+ T& {9 J8 {<P><FONT color=#0000ff>/* read result of initialization */ # w' b) K( p! {' B; L6 s8 X: F
errorcode = graphresult(); 7 x9 K' A# b& l  d3 J5 x5 l
if (errorcode != grOk) /* an error occurred */
) Z/ z9 P' l9 ~  E0 L{ 8 @5 x' b7 O: m9 Y/ {1 _) a- \
printf("Graphics error: %s\n", grapherrormsg(errorcode));
1 n  K3 g% ~$ u/ Yprintf("Press any key to halt:");
- L+ p3 c/ p, ^  q+ @getch(); # c( z4 R& c" i6 |' z! }
exit(1); /* terminate with an error code */ . `1 M1 `5 z& }
} </FONT></P>1 M* \3 Y6 |! ?& n5 l4 q# u6 A8 \, Z5 n8 W
<P><FONT color=#0000ff>/* draw a line */
! ]1 \/ E9 K, ^# ~) Oline(0, 0, getmaxx(), getmaxy()); </FONT></P>( _* b' M, }* j
<P><FONT color=#0000ff>/* clean up */ ' p4 }' {" ?" K2 m& g
getch();
8 h! Y- D4 U2 I5 eclosegraph();
9 b! |' N: S* F: u, |& Y5 t2 W% Treturn 0;
! W+ h: p8 H) e3 o}
/ a( t  m1 \2 X( J% K$ Y</FONT>
# G" ]3 w) i# b) ?9 ?</P>
) F, O8 G9 D8 O<P><FONT color=#ff0000>函数名: remove </FONT>/ T3 M) ]' s$ G" V) V# H4 ]
功 能: 删除一个文件 $ _4 ]1 v( Q- v2 y% x
用 法: int remove(char *filename); 6 j) y3 l3 w1 o" Z
程序例: </P>0 r+ V! K( u0 k+ R- b! j
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>7 `4 G' ^+ `2 r, F0 p7 Y+ T2 g. }
<P><FONT color=#0000ff>int main(void) 1 C8 ?& F; n' H- t
{ 2 m" d9 N. h/ L8 t3 ~& J
char file[80]; </FONT></P>( m3 Q5 m. r" i  s
<P><FONT color=#0000ff>/* prompt for file name to delete */
# c+ i( [! e- A+ ]4 ]) }printf("File to delete: "); # I7 t6 L7 P( A* p0 f5 N: ^% O
gets(file); </FONT></P>( D0 I# W" a% `1 X/ W' o
<P><FONT color=#0000ff>/* delete the file */
1 W1 z; t  t8 @1 Lif (remove(file) == 0) 0 U; d. N6 ]$ I/ [
printf("Removed %s.\n",file); ) g9 m' {- m8 v
else
1 R" X( D9 W" ^4 n; c) yperror("remove"); </FONT></P>
0 {- w8 J) q" R1 E, h- J$ v<P><FONT color=#0000ff>return 0; ( m8 W, ~- S: g" q3 @$ ^% T3 ~
} 8 s! U5 _% ~& J) a! K/ d

+ P5 d' ]- p5 h' R: o- X( {6 h</FONT></P>
* _* T$ u5 b# w- `: Z<P><FONT color=#ff0000>函数名: rename </FONT>
: D: ^6 ^& H" B: E功 能: 重命名文件 9 D, k8 z5 L0 Y+ `: t7 F
用 法: int rename(char *oldname, char *newname); # n$ p8 N9 B4 S) [7 l1 I; t
程序例: </P>' @9 |) w7 J  y5 |3 z! A, I
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
: X- m/ l6 m- n( r  E2 H' ^# o<P><FONT color=#0000ff>int main(void)
3 o( t0 x( T3 j8 Q$ W' f4 q2 n{ ) j' i/ v; i, L  t! n# a
char oldname[80], newname[80]; </FONT></P>
1 v9 F- y4 \2 W9 a/ V1 t<P><FONT color=#0000ff>/* prompt for file to rename and new name */ ) \1 g" Q: ~5 r  S/ O- f7 j
printf("File to rename: "); % }; K% {* i! K. r0 o
gets(oldname);
! B' e9 ]% o+ `$ [& V) hprintf("New name: "); ; N# L: w& F7 m5 o" G) w2 m& `, r
gets(newname); </FONT></P>1 Z: U2 Q2 |6 i8 E! X
<P><FONT color=#0000ff>/* Rename the file */
7 c# K& Z2 [) j" v0 g4 x( e$ Z6 nif (rename(oldname, newname) == 0)
0 N4 E( x2 h" h! a. ?! {" ]- @printf("Renamed %s to %s.\n", oldname, newname);
* R  q& C6 |5 Z: z* o; Uelse
( G( x' j5 f, @perror("rename"); </FONT></P>4 m) f/ P8 R2 T, _) Q( y
<P><FONT color=#0000ff>return 0; ; A( s6 E& b+ b1 l' Q0 }
} 1 t% M* n( O. i4 V( K" a

" w; p- C$ R! T# P. q</FONT></P>  y- _; F: p+ `$ p1 M6 b4 R
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
" ^3 L( b+ k2 d2 J& S8 b$ n功 能: 将屏幕模式恢复为先前的imitgraph设置 , _$ M" F% g- C& P
用 法: void far restorecrtmode(void);
" H7 @: ?# N3 ]# z8 ]. G程序例: </P>$ G  }9 F& _( q$ ^, w) T/ o1 d
<P><FONT color=#0000ff>#include <GRAPHICS.H>' P3 b  \2 u) A% b2 H. o
#include <STDLIB.H>" N- _: e2 V- P$ Y! S$ |7 V
#include <STDIO.H>
: ^$ V& D2 p- l) L: @% M8 y  Z/ O% K#include <CONIO.H></FONT></P>
# `2 [& J7 U0 Z1 i+ C# s<P><FONT color=#0000ff>int main(void) - L6 |) g  P8 f" g1 d
{
( A8 U: v% w; d6 S6 w! o, ?/* request auto detection */
; D, B# ?- P, Z- f* z8 S! z; Cint gdriver = DETECT, gmode, errorcode; 8 w( a( t. d5 y! p- u0 {: t
int x, y; </FONT></P>8 J5 G7 F; n) T8 U. U
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 1 o7 K7 r3 P, S4 \. n% J: E# E
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
$ X! r- m3 M* n1 c! I<P><FONT color=#0000ff>/* read result of initialization */ ( _( D6 K( i. c: ^9 L5 P( [) F
errorcode = graphresult();
& \2 D( B- Q& |6 v+ B4 wif (errorcode != grOk) /* an error occurred */ 7 M( a7 A# L& e+ ^  q' `
{
4 }# C. ~8 N# m' f( Kprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ( J2 h) N+ G4 ~# c% l+ R
printf("Press any key to halt:");
) X- W8 f% b/ R/ E: ^getch();
$ F9 D3 @! d' c1 O% uexit(1); /* terminate with an error code */
- T3 Y) Z# I2 K5 [6 M} </FONT></P>: a, k" p$ g) |3 x5 x, Q7 W" k
<P><FONT color=#0000ff>x = getmaxx() / 2; % x6 Y& Q) `/ ]1 s+ Z5 F
y = getmaxy() / 2; </FONT></P>. J' s# X1 Z5 V* A
<P><FONT color=#0000ff>/* output a message */ # _) ?  w) S! D# R& O) c- Z
settextjustify(CENTER_TEXT, CENTER_TEXT);
' K9 ]- L. [0 G* \+ T: X6 [' w- U* Couttextxy(x, y, "Press any key to exit graphics:");
9 p3 @8 X+ ]8 h& P% Z; vgetch(); </FONT></P>
+ u) T1 h& v5 ?<P><FONT color=#0000ff>/* restore system to text mode */
2 ?* g9 h5 n0 j, L0 Brestorecrtmode();
4 @# O, r; @1 D& g3 ]printf("We're now in text mode.\n");
3 _. o; R6 [7 Z5 qprintf("Press any key to return to graphics mode:"); - \8 D1 O7 j! I7 W% X
getch(); </FONT></P>
- @& i/ i  @6 s$ w6 ?+ j3 v<P><FONT color=#0000ff>/* return to graphics mode */ 8 `  ~7 a; P& `' p( b; t
setgraphmode(getgraphmode()); </FONT></P>
9 y7 A5 m5 V' O8 f- W0 N<P><FONT color=#0000ff>/* output a message */ 1 t# d! J1 S9 Z: q+ A0 e  R5 F4 k+ a
settextjustify(CENTER_TEXT, CENTER_TEXT); : z% O8 a* U' n
outtextxy(x, y, "We're back in graphics mode.");
7 S5 W& {" x) v2 }3 \" `; wouttextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>
7 K* R. h% i) H7 A<P><FONT color=#0000ff>/* clean up */
" N1 b. o4 |; f( Agetch();
  m2 y, B8 o0 G8 Vclosegraph(); ) o1 ?! j9 ^, w" |  E- t- y- E! w
return 0; - J" e' k1 {. X
}
. S6 y3 r" k$ O4 _9 O3 B</FONT>, c( |6 }' V* J1 \9 u% h
</P>
$ H! B9 j# e7 j9 P9 B<P><FONT color=#ff0000>函数名: rewind </FONT>
( ^8 n) \" x+ e8 ]6 o6 `; d6 j功 能: 将文件指针重新指向一个流的开头 / l5 \* W2 M; H; j: ~* }: D5 h
用 法: int rewind(FILE *stream);
% x4 b+ N9 s) T( }程序例: </P>
' z: e6 F7 G0 o. k" X<P><FONT color=#0000ff>#include <STDIO.H>! f8 Y4 _3 k: \. f+ s
#include <DIR.H></FONT></P>
' I9 u. Z1 D4 M) U" m<P><FONT color=#0000ff>int main(void) 2 O7 T# \0 s6 }- A1 s2 T
{ & W8 \/ r0 `2 n% |$ p7 @; E
FILE *fp;
3 |8 N' {6 l/ l( W2 cchar *fname = "TXXXXXX", *newname, first; </FONT></P>1 }8 r' _% [5 g6 s% p1 a
<P><FONT color=#0000ff>newname = mktemp(fname);
- U# w# v0 x& P7 `5 Y3 h; Dfp = fopen(newname,"w+");
! d$ l1 I# d" A6 K; ffprintf(fp,"abcdefghijklmnopqrstuvwxyz");
5 u6 H: C: T) c* P7 R" q5 E, \; drewind(fp);
: A2 [. ~; J: O2 i8 p8 \fscanf(fp,"%c",&amp;first);
. }: M0 l% z  Q0 T$ H% Y3 M/ Fprintf("The first character is: %c\n",first);
/ C( E; N+ R% C3 `fclose(fp); & Y7 y; C" s  q% Q; F- Z; B' |
remove(newname); </FONT></P>
- r# |6 f8 j: t4 _! x  j5 n2 W<P><FONT color=#0000ff>return 0;
' d% [! T# m: }* {. v6 ?7 C} + I3 }" G) H2 B* b8 m
</FONT>
& `/ n3 B6 w& C3 m</P>
; T( m4 r9 j* C<P><FONT color=#ff0000>函数名: rmdir </FONT>
- M, Y7 f0 h( F. l9 f! X; n功 能: 删除DOS文件目录 # j6 S5 H5 B( y( W1 O
用 法: int rmdir(char *stream);
! n+ H* {  ?& W9 f! h, Z' G/ W" o程序例: </P>
: [2 D8 N9 \; l' t* ]<P><FONT color=#0000ff>#include <STDIO.H>6 }& r  m* g0 e( x; T+ \! l
#include <CONIO.H>5 N" d' V! |  e$ d0 V: X- Y
#include <PROCESS.H>0 T0 u- V5 r. H( b
#include <DIR.H></FONT></P>
+ N9 O6 r; R6 f1 K4 _6 }4 V/ B<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>
: c7 p- l) k; L; H<P><FONT color=#0000ff>int main(void)
, |* }1 U& m9 K5 [- B' n" u$ o9 L1 y{
- U3 l  B3 B8 R; Uint stat; </FONT></P>( G) E+ {4 n) O
<P><FONT color=#0000ff>stat = mkdir(DIRNAME); * l- k" e2 g4 g  d! }! ?5 y
if (!stat) - O% U- P5 v( w6 T8 _0 m
printf("Directory created\n");
: A7 u7 v4 p; o: x; jelse
  n2 o3 r8 D" J$ ]1 n5 ], A{ # @9 x( ^1 @( M' M3 l; v/ G
printf("Unable to create directory\n");
5 S3 |9 [6 p' i; p/ U( f& ^: y5 |exit(1); 6 o- Y' o2 b: V, P! L
} </FONT></P>  C* \5 L% Y; d* Z  ]
<P><FONT color=#0000ff>getch();
6 m2 K: c; l) qsystem("dir/p");
* }- K; g2 p, E  [# Cgetch(); </FONT></P>; O/ z/ C: p6 y% T0 |' d
<P><FONT color=#0000ff>stat = rmdir(DIRNAME);
# ^2 {3 t' f+ o3 O8 aif (!stat) 3 Q+ @: J0 I7 k& t
printf("\nDirectory deleted\n"); # I. J+ S; H# O2 y
else
4 W$ d& ]' f# I* X6 m+ _0 [# D{
; g$ R; f( n8 x3 F/ tperror("\nUnable to delete directory\n"); , ?' t9 Z; ?9 [' W
exit(1);
1 q; n( d# m0 ^- r1 Y} </FONT></P>7 M% y$ W5 T6 i. J
<P><FONT color=#0000ff>return 0;
" F( p: d  K1 p! B. V. d4 C5 J! f} </FONT></P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-20 09:55 , Processed in 0.354579 second(s), 52 queries .

回顶部