QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
3 l; m% e' \9 i9 `功 能: 使用快速排序例程进行排序 , Q1 ?. s/ Q, V; L  s" A3 B$ d0 ?7 W
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());   V# f3 L' R4 g9 m, j
程序例: </P>/ B6 z, k2 [; L& A# [& A4 z3 D4 o
<><FONT color=#0000ff>#include <STDIO.H>7 j* K! C0 o  Q( L& P: t
#include <STDLIB.H>3 I' }1 N1 A6 X4 x" \
#include <STRING.H></FONT></P># c+ ~3 Z1 X+ N) o
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
3 C7 s% P% ]" b" x<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
- v1 i5 F; P4 [</FONT></P>
  }5 L! O/ a4 o5 y# w<><FONT color=#0000ff>int main(void) ( }' R0 v! P3 B$ ~" `
{
  d, L# o  F! \6 ?int x; </FONT></P>: m' P6 T0 \6 R) c9 u
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
1 D* v' x; ]. I) J, E% ^: W' efor (x = 0; x &lt; 5; x++) & K3 l* Z+ Y" T$ ^- @
printf("%s\n", list[x]); 3 m8 T) L8 v! v* Y: V' {5 P
return 0;
1 ?$ j0 l! d6 k. M} </FONT></P>
0 h$ \% G/ B. L. [  H9 E# e<><FONT color=#0000ff>int sort_function( const void *a, const void *b) / N$ h' `1 L8 B: i
{ % x. h8 B" X9 c: |( W, {9 l
return( strcmp(a,b) ); ) ]  Z6 [& l& B7 Y. b
} </FONT>8 W+ S$ L5 o7 `+ |) {. u7 E& [" b
</P>5 x- i! k( S7 }& P( s: a2 p% y2 m

6 x* p  E! [4 j6 ?6 C! G. Y: R$ X9 n7 V+ [% L4 z2 u
<p>
4 |9 Z: N$ U$ D1 _<><FONT color=#ff0000>函数名: qsort </FONT>
" a6 S1 W, @; n# W功 能: 使用快速排序例程进行排序
% [7 h, U" N9 `! T1 S用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); : T8 x0 r' E3 m* J! @
程序例: </P>
1 X6 |6 y7 b* H" e' a; f4 \<><FONT color=#0000ff>#include <STDIO.H>1 {1 A& t/ t9 `6 W  E5 b7 b
#include <STDLIB.H>
+ t* |$ S9 O2 `3 p#include <STRING.H></FONT></P>
* `% J; R/ k3 J" G$ r<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>" E2 k/ F6 u" H1 @* {
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
: x% G" p* D0 p2 [4 R1 h</FONT></P>
# y9 e7 S, B; I+ R4 ?+ T+ K( t<><FONT color=#0000ff>int main(void)
7 H6 `+ B& }! I& t{
; p1 ^+ ?7 p, ]0 a) B% Sint x; </FONT></P>  ~" V- L# a2 G$ }
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
: Y9 K) s/ w% k! Q$ J: Y7 Wfor (x = 0; x &lt; 5; x++) ! W9 t0 Z6 ~& v; |1 K9 f! v  H% T) V
printf("%s\n", list[x]); 7 Z6 m4 u  h) ]* I& q) ]5 q
return 0; 8 l1 e1 f; A) B" B
} </FONT></P>2 r4 X. a* W7 b9 y. k$ d  {
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
' W. s3 c8 Z0 q. l! z" v7 m{
$ W5 R! J4 U- z9 t1 b1 w0 {( Areturn( strcmp(a,b) );
& l/ R) v3 A6 K) e3 C( K}</FONT></P>9 j, `3 g# t8 b0 \/ R
<><FONT color=#0000ff>
* a" b+ U6 \5 v: m</FONT>- D2 D7 f% @5 @& g1 A2 A" Q
</P>& k1 j. X- `, ~
<><FONT color=#ff0000>函数名: raise </FONT>
# ~1 B0 K* K8 W4 [7 ~功 能: 向正在执行的程序发送一个信号
# g' b- J3 P  p用 法: int raise(int sig);
$ X4 G3 u) N& f7 @6 F, T: x程序例: </P>
( i; h; X8 Z2 p9 K% U4 p6 C# j<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
+ x" c  |2 t" C5 ~& @<><FONT color=#0000ff>int main(void)
0 n8 p  [# V# \! E# A. E{
% _5 Y) x; d9 A" m" Oint a, b; </FONT></P>6 q5 ]* T1 Z( t
<><FONT color=#0000ff>a = 10;
+ T$ ^$ T/ W' e& B$ Yb = 0; % N: R" |3 p1 m/ f& s) I% L
if (b == 0)
5 j4 y  Q) b* A. z" |/* preempt divide by zero error */ / s1 z- K9 w" f: L2 S
raise(SIGFPE);
/ T9 H0 x/ P4 l5 o6 L5 Na = a / b;   @1 @0 a5 e0 R3 a; G! b* a
return 0; * u' N: o7 o, ~. Z7 ]
} * o$ r- z- D3 ?9 |7 T% q% j
</FONT>5 X5 m& J0 b/ B: m$ i" n
</P>
- \# v' x$ }, n1 I# _5 ~& b<><FONT color=#ff0000>函数名: rand </FONT>
/ I# M. _6 s& @/ {( h) R功 能: 随机数发生器 / H) C; v  i, {" J( z1 Q1 g/ f
用 法: void rand(void);   n6 l6 `3 P. H/ ^" X" W! n
程序例: </P>
9 M- |- _  `: H: r<><FONT color=#0000ff>#include <STDLIB.H>+ A$ H9 {! J8 C! s
#include <STDIO.H></FONT></P>
) q3 m  A+ U0 o9 [6 V6 Z0 a<><FONT color=#0000ff>int main(void) 0 k, k, g. U) t, R6 P
{ 6 m! }( F; f% @* K
int i; </FONT></P>
0 c9 w6 x% j8 w( k4 ~<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); - b7 v- E, O+ C
for(i=0; i&lt;10; i++)
0 I4 L4 C. [+ E8 o& @. aprintf("%d\n", rand() % 100); 5 i, ?) X7 |& a. _
return 0; . ^( S/ B5 _5 ~! H& ]+ V
}
8 D7 e. I: c- n5 n9 i# B! G  M" X7 U) V8 b$ X) _
</FONT></P>
; s+ T! h: W" U% x+ a<><FONT color=#ff0000>函数名: randbrd </FONT>0 ^  ]+ f  J/ Y. k7 P  v) Y" M
功 能: 随机块读 9 S  X. E/ Y6 G( _6 W9 g
用 法: int randbrd(struct fcb *fcbptr, int reccnt); + Y% ~/ y' Q& j
程序例: </P>
5 f8 I4 g: b6 n# b4 l: ]1 o) A<><FONT color=#0000ff>#include <ROCESS.H>
6 D# u4 `! N+ Q7 U# Y#include <STRING.H>
7 w4 ]" X* y% N  P& a#include <STDIO.H>0 G& U2 _: r0 ?& `  r  h. C
#include <DOS.H></FONT></P>9 F" b3 h$ C: n2 s1 r0 n
<><FONT color=#0000ff>int main(void) 0 [: M+ J( f8 x0 l6 o7 N& K3 D
{ . A+ K: g( a5 F& H5 k
char far *save_dta;
$ K9 H4 ?- _/ a7 @: @: Achar line[80], buffer[256];
5 s" Y8 J, S, R5 s6 {1 Istruct fcb blk; $ |6 D, @( [' [, D% _% Q
int i, result; </FONT></P>
. K$ w# W) `  D5 j5 y  P<><FONT color=#0000ff>/* get user input file name for dta */
! d% e; p! ]2 y, C) ?& jprintf("Enter drive and file name (no path - i.e. a:file.dat)\n");
2 h; E% C" D2 Y, n; \gets(line); </FONT></P>
& N8 g( J$ b% F4 E1 [" }<><FONT color=#0000ff>/* put file name in fcb */ 0 m: W. y* u. }$ Q: q8 G' u9 `
if (!parsfnm(line, &amp;blk, 1)) + |: k, a9 o+ Z* ~' l$ X* K! S
{   }+ F1 c" m8 p" }6 N
printf("Error in call to parsfnm\n");
" U* }' R" X: F4 }, e0 Pexit(1); 2 p7 D4 X$ d2 W
}
. o8 a2 v; \' D& b5 S" \& m1 Gprintf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
  [, D8 H) a, Y* K" u9 y8 n  }<><FONT color=#0000ff>/* open file with DOS FCB open file */
$ {! Z6 x) N7 j+ pbdosptr(0x0F, &amp;blk, 0); </FONT></P>
$ c) D, _# n( q<P><FONT color=#0000ff>/* save old dta, and set new one */
& H/ p- @" J* L( q4 zsave_dta = getdta(); 7 f! y4 I/ K1 D& G7 x; A
setdta(buffer); </FONT></P>
. ^$ x( \8 L, ^. f4 [<P><FONT color=#0000ff>/* set up info for the new dta */
: r4 T. ]' m6 t* M8 \2 q0 X# `3 |blk.fcb_recsize = 128;
$ J- Z/ `5 Z: }blk.fcb_random = 0L;
4 t# S1 }; M( R6 b# J$ tresult = randbrd(&amp;blk, 1); </FONT></P>; m) {8 l1 p% E* i% P- Q) v3 @
<P><FONT color=#0000ff>/* check results from randbrd */ * B* @) j# D( X2 Y' \5 L. Q
if (!result) 8 T3 _- a7 y. T: [2 R
printf("Read OK\n\n"); ( @  Z* w4 W* t7 t
else
5 Q" ~/ u( v5 E7 {" ^, ]{   m! [! d' r2 \& ~& N" J
perror("Error during read"); . J! o+ ]4 I7 Y# i, @
exit(1);
; C3 x) L& A  S} </FONT></P>
1 L$ ^6 w; r" x8 D<P><FONT color=#0000ff>/* read in data from the new dta */ ( t- W, S- B! o! @
printf("The first 128 characters are:\n"); ( n0 Y  C  e# \, q7 _2 \4 ]
for (i=0; i&lt;128; i++) 6 p( Q7 D/ f9 J$ U
putchar(buffer); </FONT></P>. c+ t% A2 Z/ D5 y, J4 x
<P><FONT color=#0000ff>/* restore previous dta */ & P0 X% B# c; X& d, }8 x% E
setdta(save_dta); </FONT></P>' ]# n5 d+ W( F+ s! Y" U, R5 @  M  g9 Y
<P><FONT color=#0000ff>return 0; * O: Z# y* w$ h- d) r7 A
} </FONT>' v/ ?( n$ v& S" I4 i" o
</P>) a, W7 b" D* i7 f! l
<P><FONT color=#ff0000>函数名: randbwr </FONT>6 Q4 c1 w( l* Q; ?
功 能: 随机块写 6 k: {' R$ i! r
用 法: int randbwr(struct fcp *fcbptr, int reccnt);
7 x% w6 i4 Q$ l& s程序例: </P>
# o, d$ N. x. Y8 V, \/ L<P><FONT color=#0000ff>#include <PROCESS.H>
: q% ~+ l3 o$ U; K" S- v4 \  q. D! h#include <STRING.H>' c9 X1 [, `( D: u, b* j! e
#include <STDIO.H>
* P$ T+ C+ \- q% z2 x#include <DOS.H></FONT></P>- A7 Z. [8 ]0 D$ {8 X3 _" k" w1 w: Q
<P><FONT color=#0000ff>int main(void)
2 v, r5 M- @1 @2 e5 g/ |{
5 n$ R" t: M8 u- R" S+ {char far *save_dta; 2 U( q8 H8 [/ B0 L
char line[80];
, ~3 L& k+ X" r( m$ {char buffer[256] = "RANDBWR test!"; ( \* o8 b- f% y; F
struct fcb blk; 7 o. [, J# w' [( y5 B) ]; B
int result; </FONT></P>' w9 q2 l% U2 H& S2 u' e
<P><FONT color=#0000ff>/* get new file name from user */ 7 S, @( |- m" A& i
printf("Enter a file name to create (no path - ie. a:file.dat\n");
9 x( l( l- \/ m: Ogets(line); </FONT></P>
9 h  {5 t+ _; B% X7 J5 d4 R<P><FONT color=#0000ff>/* parse the new file name to the dta */
- K/ H% k+ Q" g4 m! Y- Zparsfnm(line,&amp;blk,1);
+ p4 Y- I7 p% }2 Yprintf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>, E. ]5 p: C) {/ \2 h; O! _! W- _
<P><FONT color=#0000ff>/* request DOS services to create file */
; G# m. W, r5 i; ]6 k- F' Wif (bdosptr(0x16, &amp;blk, 0) == -1)
* h5 M. m+ ^7 ?9 W$ c. U{
( w, ~3 R% C3 w. C" U8 Xperror("Error creating file");
& F9 D/ k9 Z% `exit(1); - @, y7 r, `0 k
} </FONT></P>) U) D, T9 }3 n2 a
<P><FONT color=#0000ff>/* save old dta and set new dta */ # `( L( [8 D1 C! e& L; j
save_dta = getdta(); : c6 Z7 H% i( z# S5 M: j
setdta(buffer); </FONT></P>
5 j+ J& }. `; T. `<P><FONT color=#0000ff>/* write new records */ : u7 ]& Y7 Q0 v; x. h& u9 y
blk.fcb_recsize = 256;
. {* S# [9 [( N, zblk.fcb_random = 0L;
) B) Q" K( @: Qresult = randbwr(&amp;blk, 1); </FONT></P>
' ~6 }0 Y* g# r9 q<P><FONT color=#0000ff>if (!result) # f8 L4 p  j8 ?+ }
printf("Write OK\n"); 7 X( b. l0 @: O" q$ f9 _9 z" [& N
else ' X( l. w2 f+ u
{
8 w4 g& e. {! v( T( u5 ~perror("Disk error");
" N5 z3 J" I) ^exit(1); 3 J( n+ G0 O9 b5 l! X1 v4 Z
} </FONT></P>
/ V- E; ^/ L: \4 A- W: ]<P><FONT color=#0000ff>/* request DOS services to close the file */ + z" L9 \& I9 I1 {( [
if (bdosptr(0x10, &amp;blk, 0) == -1)
" M, J9 G7 }+ W: s- s5 d{ # E& h/ l6 j6 D: [+ Q
perror("Error closing file"); 5 {7 L* f# d1 O% r9 M# Q% P
exit(1);
+ ]  o) H6 t7 _0 T; J; _# [% U} </FONT></P>; a: d8 K* Z, s3 _" e+ j. [
<P><FONT color=#0000ff>/* reset the old dta */ 5 W: [1 v* _1 A3 k. l8 H
setdta(save_dta); </FONT></P>
9 `9 y0 [/ L, K7 r! _7 S/ e<P><FONT color=#0000ff>return 0;
8 [# S( r1 w* Q, o}
; S3 h! q3 v0 J. D3 Y0 p* K" m</FONT>( k8 y5 l  ~4 K
</P>: I5 L; _- q, J5 `+ q
<P><FONT color=#ff0000>函数名: random </FONT>/ V$ G) n. R5 r, B
功 能: 随机数发生器
. R: n/ l  ]7 ^用 法: int random(int num);
; m+ Z$ `' V- h2 v程序例: </P>
7 K+ ^% p% G. F3 b! ^  Q<P><FONT color=#0000ff>#include <STDLIB.H>
8 s- P/ L( C6 n#include <STDIO.H>* e; E& C1 I) s% |
#include <TIME.H></FONT></P>; j" e% J/ S4 i) p8 u' D$ V# j
<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
+ r) _$ J1 X8 r7 k1 d6 Kint main(void) ' c( T' c# s- u% `# U5 b
{
. ]( {: m$ }8 M# o5 w3 Crandomize(); 4 p2 a) z/ y- Q5 q6 Y
printf("Random number in the 0-99 range: %d\n", random (100)); $ A( y) C4 {% K# D+ z, n9 `
return 0;   L+ a7 H6 r& |4 S5 Z; D5 U
} ; i$ \: V- ~3 x! j; P
</FONT>
& n: O. u3 C% d8 H5 F* T$ i</P>
0 W9 z* s/ o6 ^: ^<P><FONT color=#ff0000>函数名: randomize </FONT>( Y0 c4 u2 U. z) J( b' J8 u  A
功 能: 初始化随机数发生器   y. Z+ d1 S8 x7 a. n
用 法: void randomize(void); % G; R# O4 x1 e; l5 L9 Y5 j, V: I
程序例: </P>
# N! _, r& R) g<P><FONT color=#0000ff>#include <STDLIB.H>) j' ]  H/ B$ P
#include <STDIO.H>
( H6 e% N, Q5 t! r#include <TIME.H></FONT></P>
# a8 \) Z4 ]1 {6 W( \<P><FONT color=#0000ff>int main(void) - p8 F% D/ b! V0 T4 l* R; ~
{
- r% |( U  D: r/ y0 l3 Z( m+ iint i; </FONT></P>( m3 @# z7 @$ T2 z& y* X- L
<P><FONT color=#0000ff>randomize(); 1 f$ b8 k( [7 Q) i0 Q  `3 F, F* E0 O
printf("Ten random numbers from 0 to 99\n\n"); 6 q6 a* I8 V0 a& u" }6 i
for(i=0; i&lt;10; i++)
: r& s+ C$ x5 ]. J" k0 W9 \, dprintf("%d\n", rand() % 100);
* K  ?# o% F4 u3 Lreturn 0; 6 D* [& A8 M0 |% y5 k8 i
}
; b) K0 {( Y4 b" i- p) P# }</FONT>
2 H# Z5 {( l3 b3 X1 a+ I8 F9 T</P>" R& ^$ I3 [- k- L
<P><FONT color=#ff0000>函数名: read </FONT>
% n1 f0 i( V0 |, Q* f: Z- {功 能: 从文件中读
9 E* m0 ], P! b' S* a% o8 K用 法: int read(int handle, void *buf, int nbyte); 1 `0 }6 E# g1 f2 j, a% }/ w
程序例: </P>
% |( S  v6 o; Q1 ]% }<P><FONT color=#0000ff>#include <STDIO.H>* N& U& D! B1 i+ o/ x. ]0 A1 M
#include <IO.H>
$ u1 V  @0 K7 T. l) A( \#include <ALLOC.H>, d! T% t8 G1 \) v! M
#include <FCNTL.H>
/ C: W- ~$ `1 K$ \* K#include <PROCESS.H>
) c% _5 I6 h3 `#include <SYS\STAT.H></FONT></P>+ W% R/ b' u- k! W3 [3 P
<P><FONT color=#0000ff>int main(void) 4 s) ^5 d& p& ~3 X- K
{
! M5 H6 y) `1 U2 z  b, `# w- k' Q0 Tvoid *buf;
- a8 i+ }' q, A! ~5 ~' t& \int handle, bytes; </FONT></P>
/ b, I1 f% i' l<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
7 ^( S* n, p1 F0 i<P><FONT color=#0000ff>/*
& Y2 P8 E: t- K2 L: F% n7 \1 RLooks for a file in the current directory named TEST.$$$ and attempts
! I5 T8 B% o5 w, Y: s- |to read 10 bytes from it. To use this example you should create the
* }6 k3 t: Z1 _# _# _) g, ]$ efile TEST.$$$ - D: `1 t  Q$ o
*/ 0 M1 L2 S- E  f& v
if ((handle =   x( L$ ~3 ?/ h- W8 P: S4 {
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1) $ e- Y2 N; F# Y# E  b4 P3 R
{ 0 O+ n* z4 h- o( e
printf("Error Opening File\n");
* T7 [0 T5 d& d* iexit(1);
9 j: I: o4 @! E+ X( A+ C# C- k0 E# ]} </FONT></P>: z6 W7 K9 ]4 d- }% ^8 K
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
  ?* V( t$ Y0 R8 d  h8 H5 iprintf("Read Failed.\n"); : M( X% k3 Q2 l3 f+ n
exit(1);
, L: r1 R; w% I2 h) u} ) s1 M. v/ i( u2 A
else {
3 A$ V  {, b% b6 Z& |- f! |printf("Read: %d bytes read.\n", bytes);
9 M, B2 r. y: Z}
. a+ X+ s6 h+ Y( K; {return 0; / Q) D" I3 F8 ~$ P: B& Q) g; Q/ N
} </FONT>
5 {- @, j% ^. U/ ?0 y2 S/ D
1 S* p+ B6 i! n0 p: k' t</P>
9 N( R( n' v" B+ g5 M7 g<P><FONT color=#ff0000>函数名: realloc </FONT>
- p; ^. u5 g  I! A! w功 能: 重新分配主存 8 s" a' ]  `5 g
用 法: void *realloc(void *ptr, unsigned newsize);
% W1 A; `9 c8 @' [: i3 A程序例: </P>
4 q% H3 q0 T  q1 o: ~+ Y0 J) H% C<P><FONT color=#0000ff>#include <STDIO.H>) o: s) e( F" B% K# e) i1 d% \
#include <ALLOC.H>
- w! W3 K( F2 S1 f1 R#include <STRING.H></FONT></P>
4 n2 T6 P  b4 v2 p2 p% K0 K<P><FONT color=#0000ff>int main(void)
* r3 n' B( P( d' x5 P2 g2 b{
$ u6 J& I' z" lchar *str; </FONT></P>1 z4 l1 I7 n1 U2 [# c
<P><FONT color=#0000ff>/* allocate memory for string */
+ c- l; ^" _6 N! V( G1 v4 zstr = malloc(10); </FONT></P>
& E8 T2 ~% T5 J$ G8 r: C<P><FONT color=#0000ff>/* copy "Hello" into string */ / ^0 [$ ?4 X/ ], f: v
strcpy(str, "Hello"); </FONT></P>6 q- h* N. @& h+ Y1 Z
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str);
# b& r5 K$ K( b" Z7 bstr = realloc(str, 20); 5 r' u% F& h8 ]5 `8 f: Y
printf("String is %s\n New address is %p\n", str, str); </FONT></P>
7 n% x" r7 ~5 S) Y( l4 v* H) n<P><FONT color=#0000ff>/* free memory */ % v$ P# R5 ^- p( }8 n0 C$ P, i
free(str); </FONT></P>
9 x4 d  A5 M0 h<P><FONT color=#0000ff>return 0; : ^2 V3 n8 y1 }) W) P
}
) [2 i  l- n5 ^8 Z# ~0 }1 l</FONT>
$ P! H$ `+ I! J( t</P>6 j  p+ ~2 i" H- q; U9 ?7 R
<P><FONT color=#ff0000>函数名: rectangle </FONT>
* G( Q0 a/ o' v# v3 S功 能: 画一个矩形 : K  ]/ s* W9 L) d
用 法: void far rectangle(int left, int top, int right, int bottom); : b4 j8 u, b, A6 R; L
程序例: </P>
1 F( ?- ], a/ i# }+ F: H<P><FONT color=#0000ff>#include <GRAPHICS.H>
. N9 A4 J( f8 K! Q0 h5 R" ]#include <STDLIB.H>) u. K/ x$ n# P& j
#include <STDIO.H>2 E5 v! U& X6 @0 t& Y( _; q
#include <CONIO.H></FONT></P>
" z- d- y2 I' M<P><FONT color=#0000ff>int main(void)   L2 t+ p3 Y, f6 I( @& U
{
1 M6 \( P& f) O2 A: w: x/* request auto detection */
% }1 o) g9 X: R- X9 }) J# x8 Vint gdriver = DETECT, gmode, errorcode; + O- D  V& Z- q, T! ~) i
int left, top, right, bottom; </FONT></P>
" o* |0 t1 J3 E  L+ t& R<P><FONT color=#0000ff>/* initialize graphics and local variables */
! O: J  N0 J, y) g" {  binitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P># ]7 F# h1 R& A" y
<P><FONT color=#0000ff>/* read result of initialization */ ) O. W5 b4 |6 B/ o3 h
errorcode = graphresult();
4 ?! D$ q: H1 ~' d/ N$ nif (errorcode != grOk) /* an error occurred */ 5 d5 W3 [) P5 Y* L( F4 k2 A
{ . f! j5 o0 r. m
printf("Graphics error: %s\n", grapherrormsg(errorcode)); . d" m3 N+ Q7 W% o* U
printf("Press any key to halt:");
3 ~7 C% ~, A. |4 }$ u% Jgetch();
  m  i( _, ?4 \! _, Uexit(1); /* terminate with an error code */ + h# ?& k+ |* ~9 o
} </FONT></P>
" t# A2 r) l7 B8 j+ x& r<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; 8 P2 `2 B4 P/ G
top = getmaxy() / 2 - 50;
4 C$ w3 t+ _0 }4 Kright = getmaxx() / 2 + 50;
& N! x& r; Q8 @bottom = getmaxy() / 2 + 50; </FONT></P>
+ {, {1 L' [/ c+ j. W+ r! g<P><FONT color=#0000ff>/* draw a rectangle */
  G, a& m7 C+ ~" X6 e6 x$ `' `4 nrectangle(left,top,right,bottom); </FONT></P>' K- `4 G& g$ H  X
<P><FONT color=#0000ff>/* clean up */
7 M0 @5 h0 Y( O7 |/ }/ A# N  Z2 vgetch();
( V3 o# _$ o6 r4 n2 \$ eclosegraph(); : Q$ Q& i8 p5 C( \" |: B
return 0; 9 p  n" e" G( Q9 A: R
} : H8 B* o5 t, j. ]" }
</FONT>3 [9 D; ~) m' F. I0 ^
</P>
7 S9 O0 N$ Q/ B2 M' J" X# Q<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>8 Y! V+ q) a8 b. J
功 能: 登录已连接进来的图形驱动程序代码
' o- d0 s! Y$ o8 ]用 法: int registerbgidriver(void(*driver)(void)); ' G" V: ^" X- A* A1 i
程序例: </P>+ U- T) e: `4 d
<P><FONT color=#0000ff>#include <GRAPHICS.H>
9 c/ R6 w' O6 S' ?2 Z; ^- L& H#include <STDLIB.H>" y* v$ w8 h" i* r# y
#include <STDIO.H>
- e& n, ?9 _; \8 x& X( H, ?, K' n#include <CONIO.H></FONT></P>7 f6 }: x' S( e3 Y/ ~# ^& j9 v; y
<P><FONT color=#0000ff>int main(void) / h0 E4 K7 q, |5 f( ^* [/ ^5 e7 p" f/ A
{ , M$ H* i, m" S3 f7 P
/* request auto detection */ & w( e4 h3 L; [/ H; l* N& x) l7 E3 |
int gdriver = DETECT, gmode, errorcode; </FONT></P>
9 _  e: N" r/ c4 ^1 u$ q<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ 5 Z0 Z5 W7 M' |# z* M) G- A
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>
( g' Y; l  l+ Q# _2 p  j<P><FONT color=#0000ff>/* report any registration errors */ ! r& M# h" Z' a
if (errorcode &lt; 0)
% B3 H9 Q& q  M{
2 ]" Q- Z& r: Q5 w" R. bprintf("Graphics error: %s\n", grapherrormsg(errorcode)); % x$ \2 a1 l1 Z2 A2 q1 s
printf("Press any key to halt:");
0 D2 G, U4 R0 ^8 u4 T) t6 w5 I2 s) xgetch();
( H& D) Q8 n; Lexit(1); /* terminate with an error code */ 6 X8 a, S/ {* d) o! k: y8 q
} </FONT></P>2 m, ~2 t' ~4 N+ P& G* Q9 M
<P><FONT color=#0000ff>/* initialize graphics and local variables */
9 L# C' M) I6 k& _' j1 y3 }7 zinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>$ R8 D# S, L& K
<P><FONT color=#0000ff>/* read result of initialization */
6 L7 K2 m9 c; X2 J' p4 yerrorcode = graphresult(); 9 V1 _) p( c. S$ u: l3 T- k
if (errorcode != grOk) /* an error occurred */
2 y( @, v* `1 h{
6 Z/ N% s/ s: kprintf("Graphics error: %s\n", grapherrormsg(errorcode));
8 ]' l, F* z+ V. p9 d+ Dprintf("Press any key to halt:");
* l: p8 X, q  h3 Pgetch(); 3 M) U/ e+ I9 ]5 x6 e' B
exit(1); /* terminate with an error code */ : o2 r. z  j6 E( a- B9 A. ^5 Q  d8 [
} </FONT></P>
) u# P- L: i+ E( [: \$ n2 K<P><FONT color=#0000ff>/* draw a line */
; ~' h* x% H2 U1 t' c" G4 Bline(0, 0, getmaxx(), getmaxy()); </FONT></P>
% p0 t/ W( Z+ J" ^! f1 \<P><FONT color=#0000ff>/* clean up */ 8 G# N; G' D3 Z7 ^0 o6 w
getch(); 8 d6 _! K% I+ \' W4 v* ~' l
closegraph();
" U0 e- s! x" a/ o# [$ Breturn 0;
& A; n3 K9 g' d: G7 ?6 S, y7 f- y} 0 C3 m5 q0 U$ O  C3 t
</FONT>
8 Y/ E' L. p- W+ T; q; }& [</P>
0 d, y( J: d- c  D8 @<P><FONT color=#ff0000>函数名: remove </FONT>5 e5 o& w/ s/ H
功 能: 删除一个文件 9 Y+ A6 n9 l4 i: e
用 法: int remove(char *filename); 4 @- A/ q! V' t8 A8 X
程序例: </P>
( T! a1 r* @' q6 B+ y( D4 n<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>! l5 d) S. h# O% r  Z
<P><FONT color=#0000ff>int main(void)
3 Q; M, m! v! {) K9 m2 i9 O% {{
! N3 N5 m' C( j) }" F& hchar file[80]; </FONT></P>
. y  y; s4 J7 X& B/ _<P><FONT color=#0000ff>/* prompt for file name to delete */
7 B7 J, n2 l, W! y' dprintf("File to delete: "); 0 ~5 m% a  ~! G. V, E
gets(file); </FONT></P>+ H, ]# [9 g4 y1 K  z
<P><FONT color=#0000ff>/* delete the file */ ' z- n) W- {4 t  }, E- N
if (remove(file) == 0)
+ I5 ]% t, `" N# N7 aprintf("Removed %s.\n",file); ' |/ `' u; S& s7 F6 a  }! A
else
4 q. M' h; K6 I: j. T7 D2 tperror("remove"); </FONT></P>
" B( x  X5 w3 }# u* u, K<P><FONT color=#0000ff>return 0;
8 Q% A; p1 J% m1 R2 a} % g3 K5 o5 B- L+ ~
1 P3 c' ~; U/ T8 D3 Z5 L* ^
</FONT></P>
! O& D1 J0 u- J" T0 u. `) b<P><FONT color=#ff0000>函数名: rename </FONT>0 y; ]8 |. \4 E. C
功 能: 重命名文件 6 e  u( }6 H" }3 A7 B, S
用 法: int rename(char *oldname, char *newname);
& J+ v& m- C& m: g  ]程序例: </P>
  ^; g8 m" ^2 A4 q4 ~9 l! p+ V* ]<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>" \% J- k1 _' I2 B. N' X- P
<P><FONT color=#0000ff>int main(void) % H! I, L$ c8 N7 B3 L' J0 E3 ^$ w
{
; x" k0 n" X: N) Gchar oldname[80], newname[80]; </FONT></P>
- i: l' q& i- Z/ u) b. c$ |) G3 _<P><FONT color=#0000ff>/* prompt for file to rename and new name */
0 w! T5 ]2 C4 B4 nprintf("File to rename: ");
: Q  b1 P+ g: Z1 agets(oldname); 1 Y) q1 g6 J0 U8 z
printf("New name: ");
1 c! l& p4 P! F5 g& k7 q) ^gets(newname); </FONT></P>
4 u: `! q9 h& L3 C8 q<P><FONT color=#0000ff>/* Rename the file */
9 x2 i+ C2 `7 i# S3 f3 i$ |if (rename(oldname, newname) == 0)
0 z/ S: d7 e6 {7 T3 L: Eprintf("Renamed %s to %s.\n", oldname, newname); * Q+ n' l$ M( i& @) \
else # e1 e0 j; D3 y  ]2 _
perror("rename"); </FONT></P>
0 D& c' r: F) B# ~<P><FONT color=#0000ff>return 0;
# H5 ~; x/ u+ I+ i9 m2 ^3 o4 h1 s} ; W. w+ [5 }: n6 m$ C! W

  j$ P4 g# S- `0 f</FONT></P>5 n# d% c" |& ^, g/ F3 o
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
, f/ [4 Z/ w' r# @; m- r; j功 能: 将屏幕模式恢复为先前的imitgraph设置
  q& i- v4 T: F用 法: void far restorecrtmode(void);   m4 Q+ B- l: c! A5 O) [' T: ~
程序例: </P>2 T- R* a5 {' d! t
<P><FONT color=#0000ff>#include <GRAPHICS.H>5 S; e) |6 u4 {
#include <STDLIB.H>
% r+ y, y& m2 q3 x6 \& ], O#include <STDIO.H>9 W1 t  E1 N$ g+ V
#include <CONIO.H></FONT></P>
) N1 \3 v: ^' N2 f+ V<P><FONT color=#0000ff>int main(void)
) G0 i/ n  j9 ^  z$ m$ ~$ g# J0 f* l{ ; F' u9 B5 o: F* d3 c- J9 [
/* request auto detection */ $ d1 j# b9 U; i$ x0 R& i
int gdriver = DETECT, gmode, errorcode; 2 D+ J2 b& @& B+ v3 B7 Z' L, u
int x, y; </FONT></P>$ H- C$ N$ u1 y. {0 T: g3 f% n
<P><FONT color=#0000ff>/* initialize graphics and local variables */
- o  X6 S5 B  B/ Iinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
5 C( K% E3 N+ {$ K: T2 O<P><FONT color=#0000ff>/* read result of initialization */
+ Q% I/ D+ T: J$ q$ Kerrorcode = graphresult();
; |% Y, Y0 E, I* @if (errorcode != grOk) /* an error occurred */
1 S; [! H  Y* o- x* E{ 1 i" ]3 d1 F9 ]0 }0 m; u7 A( s
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 3 d& K' _8 w# h) D6 W' U
printf("Press any key to halt:"); / {( e; r% M% P" m8 L
getch();
2 G4 O; w3 O9 F$ N+ ^& y6 Eexit(1); /* terminate with an error code */
. D! s* b* y1 }% |} </FONT></P>$ T1 h) i) k3 J
<P><FONT color=#0000ff>x = getmaxx() / 2;
5 e  A$ P$ Q4 F7 l; ^y = getmaxy() / 2; </FONT></P>" P  b7 q. _; g! h( @" o/ F
<P><FONT color=#0000ff>/* output a message */
3 r  s: J* T6 y* i* B/ [/ t% Tsettextjustify(CENTER_TEXT, CENTER_TEXT);
% \4 {+ Y( }' Aouttextxy(x, y, "Press any key to exit graphics:"); * Z5 \" W  y& r
getch(); </FONT></P>0 p# J; A* x& S2 a  b9 p
<P><FONT color=#0000ff>/* restore system to text mode */ ' \/ w5 C! P" c: C# ]" K
restorecrtmode();
6 d5 Q" W+ N# Yprintf("We're now in text mode.\n");
, `, {! r( e, _0 l) oprintf("Press any key to return to graphics mode:");
5 o6 @7 g8 Z: {' w/ w2 \& }1 M. Z  xgetch(); </FONT></P>
8 @- Q& w+ O/ A* t& U! D<P><FONT color=#0000ff>/* return to graphics mode */ 2 d, `9 B5 \2 H: [( w, l
setgraphmode(getgraphmode()); </FONT></P>
9 ^+ i4 s9 P9 m1 ?<P><FONT color=#0000ff>/* output a message */ 3 X2 P0 U4 m! n& w# k
settextjustify(CENTER_TEXT, CENTER_TEXT); 8 `: j2 z; ]; I& q
outtextxy(x, y, "We're back in graphics mode."); $ e, [4 _' X6 X6 j; G
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>2 R  R8 D7 n( v- Q& e
<P><FONT color=#0000ff>/* clean up */
: {( F2 X) |+ h) Wgetch();
5 r. ^' T4 Q8 L3 B0 J8 Vclosegraph(); 1 r( W" b" v' D$ ]% [0 I; W
return 0;
0 R4 F4 q0 q& c1 V- J# N. C}
) E; F; c/ e0 j' r/ H/ k* a</FONT>" n* U! C/ N+ t; Y6 e3 c
</P>
& Q" P7 j  Z* G! ^& e  s<P><FONT color=#ff0000>函数名: rewind </FONT>
/ B# u; p' k; f; x功 能: 将文件指针重新指向一个流的开头 5 J! Y. Q- U- V, c; J  x9 F" w+ ^7 {7 K( ~
用 法: int rewind(FILE *stream); ! z$ z" v  ?0 F! ]4 x2 V
程序例: </P>
" L! \4 ~& c9 L; i' Z; C; x<P><FONT color=#0000ff>#include <STDIO.H>9 t2 D1 `* n1 }7 P
#include <DIR.H></FONT></P>- W: E0 r8 [, R5 \
<P><FONT color=#0000ff>int main(void)
2 c8 h7 a) j2 p6 a9 ?# m{
' j, r( P1 n! y( V& K& kFILE *fp; 0 c# w8 `7 v8 x6 G4 M; t
char *fname = "TXXXXXX", *newname, first; </FONT></P>1 z1 ?6 y5 I- Z4 u& l% H; t" |0 ^
<P><FONT color=#0000ff>newname = mktemp(fname);
: |5 W+ `9 x/ B0 s" V7 d2 W  Sfp = fopen(newname,"w+");
2 f. Z' S( ?6 @% Bfprintf(fp,"abcdefghijklmnopqrstuvwxyz"); 1 k; s4 F" m2 E  E: s
rewind(fp);
5 N" ~8 W- T# E3 _$ H- P6 Dfscanf(fp,"%c",&amp;first); 2 {% t3 S9 q6 {  Y1 q+ r7 k2 U
printf("The first character is: %c\n",first); + T' @) ]6 R4 b  b7 b6 C' h
fclose(fp);
' R" y; o4 W; f" H- [remove(newname); </FONT></P>
+ J4 U- ]; k  a+ g/ c' R  M<P><FONT color=#0000ff>return 0; " O! O/ E8 B4 W
}
' k) i) P7 |: a/ d' Y8 Z: s# ^</FONT>! h* a/ x/ ?; ^! a9 j  |& [
</P>
( T2 Y, p4 P3 _. H% Y<P><FONT color=#ff0000>函数名: rmdir </FONT>: u; R' Y# _- Y2 a8 F
功 能: 删除DOS文件目录
; C. Y7 C& |7 B  A用 法: int rmdir(char *stream); 1 V. n1 N- m" ?
程序例: </P>
% v+ @$ Z  {" Q1 @<P><FONT color=#0000ff>#include <STDIO.H>
# ~5 y  V+ C8 y1 f5 @, j9 j#include <CONIO.H>
, u- N! L: j, R# T#include <PROCESS.H>
% g9 b! G7 ~) F# d/ B3 _#include <DIR.H></FONT></P>
. X( p- S* B5 B1 x" N0 }<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>
+ a$ u( J6 W/ |9 C8 M! Y<P><FONT color=#0000ff>int main(void) # N8 {$ P5 Y: [6 R. `' C2 K: f
{ 4 f7 V& c1 l3 y; u! M4 j
int stat; </FONT></P>
7 i- F8 U# O1 {: B+ s3 Z<P><FONT color=#0000ff>stat = mkdir(DIRNAME); 3 I1 P# k% L. O# b( O- J
if (!stat) 5 _2 s3 t5 q. ~6 a6 v4 P6 X
printf("Directory created\n");
2 O' r* e3 K+ U# f& lelse % k0 J& M! N  p* f/ J, o8 @
{
% G7 A) }( J; x0 ~printf("Unable to create directory\n"); # E' Q4 A/ `. w/ q) o
exit(1);
0 G" h& w. x- e( D, t+ Y% V} </FONT></P>; x& H, K! t5 U0 Y/ @7 ~
<P><FONT color=#0000ff>getch(); * n, p+ N7 O- n! _. X& e
system("dir/p"); 7 O: }. O; T: q0 ?
getch(); </FONT></P>+ s# Q$ P0 j- i& ?
<P><FONT color=#0000ff>stat = rmdir(DIRNAME); 0 F$ o7 K5 x; q, u( I' q1 Y5 l: z: i
if (!stat)
% K7 Y. h  G. {) H& ]printf("\nDirectory deleted\n"); 6 W6 c' X8 m- G8 c/ X7 ]$ U5 ~. [
else
. F3 v4 h; B6 I$ w{ $ `3 D; U& X8 C0 }: J. F$ n4 N
perror("\nUnable to delete directory\n"); , t+ ^6 Z. e1 l' Q
exit(1);
& ]! Y1 Q# U2 [/ C/ G) u# G7 h} </FONT></P>
" B& [5 _- D: \0 a% f<P><FONT color=#0000ff>return 0; 3 c7 O7 q1 {# D
} </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-22 03:15 , Processed in 0.430889 second(s), 52 queries .

回顶部