QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
/ w* R$ t& Y, k) y4 p功 能: 使用快速排序例程进行排序
1 B: f( Q' D1 f. a9 u7 p用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
7 S8 B" M3 u9 p% _  |: P) b- l: l8 L程序例: </P>7 c. w5 ^$ H4 h+ @
<><FONT color=#0000ff>#include <STDIO.H>/ m5 _& l, n) ~6 @0 l3 y3 ?' D
#include <STDLIB.H>) K/ ?$ g2 ?8 T& `: G5 A
#include <STRING.H></FONT></P>% A  n' M& H, U7 D; Q
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>* Z+ D/ ~' `  x. Q. E; i4 Q
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
9 o) e' p' k/ F4 f8 i: b8 C</FONT></P>
, G) _/ x: y; o$ c6 @; G$ o<><FONT color=#0000ff>int main(void) * S0 ]! J/ V  [% D$ I, D
{ . R6 t8 c. p5 }1 O
int x; </FONT></P>
, i( c* _- u: I' j+ |( Q; ?' O<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 0 F, R) D( Y7 R2 b
for (x = 0; x &lt; 5; x++)
, S% J) h5 B4 Mprintf("%s\n", list[x]);
+ M$ t( i0 P# |; k$ ?return 0;
' Q6 r0 _) @1 s5 J: d6 j/ W7 O} </FONT></P>
8 M$ `% p+ T& V7 t8 C) s  [* [<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
1 X7 a0 j& G! I' B: y* y+ d" q{ " j% J$ Q; _5 P4 d: S# C
return( strcmp(a,b) );
0 t0 A; G! G" Y2 K7 I} </FONT>5 i; x* x6 K8 }- y1 B& v
</P>
  I' z1 _( a( P# }
$ [/ |' H% |: r3 a4 S; t
  w, V6 o9 f7 F. S: D<p>- n, Y! C' k! b: Z$ \
<><FONT color=#ff0000>函数名: qsort </FONT>
  @! `2 r. g/ X$ T$ I. h功 能: 使用快速排序例程进行排序
- Y; t! g/ @" }: I6 v  N% ]用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); ( |& M/ K; v  V2 `$ Q% ^
程序例: </P>
3 O& j5 x1 j0 q; O5 n<><FONT color=#0000ff>#include <STDIO.H>: ~8 G1 C% M9 U% m+ X' W5 d
#include <STDLIB.H>
! ~, r0 ]$ z+ s5 G5 R6 k2 s/ _9 ~#include <STRING.H></FONT></P>- @, F; ~1 p% F9 i# N3 J
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
$ L# B; [% n& o% B! {<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; 9 l: \! P. n5 k9 z
</FONT></P>4 U; x* j% d+ q7 V3 T% z
<><FONT color=#0000ff>int main(void)
3 o3 D  M. ]# q4 Z& G: c' z{ 1 Y  t! B) w" w- o( j  c3 w
int x; </FONT></P>
4 S% p: H" i& N0 C1 r9 D* Q8 j<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 6 Z' C4 G( m$ ?/ p7 ?8 V
for (x = 0; x &lt; 5; x++)
  ^6 Q  B$ A* g6 ?# A, D$ hprintf("%s\n", list[x]);
) T0 s. w/ Y: J% Z0 B; K; Vreturn 0; " l. ^# i! u. j' z' L$ X; I: R
} </FONT></P>& ^. s. U" P( a8 v
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
- o8 o. \. g  @. F: ?3 g8 Z- l{ * ]# x+ \  `6 H% M9 T! S( p. a
return( strcmp(a,b) ); * `7 M+ w9 A9 N: A3 N& k
}</FONT></P>
  n1 F1 u0 G* M( l' h7 D% V3 l<><FONT color=#0000ff>7 W6 Y, i/ w" o3 _+ ~
</FONT>  E+ f" S! B* R
</P>+ M' N( |  a# w% Z
<><FONT color=#ff0000>函数名: raise </FONT>% Y& G  [0 |# Q& J+ n
功 能: 向正在执行的程序发送一个信号
6 {+ e7 w/ h) L用 法: int raise(int sig);
8 [  X1 P5 A4 ^1 X. _: v$ l  u程序例: </P>3 ]7 j  g+ I# H2 q$ K
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
! b% g# s9 r" o* q<><FONT color=#0000ff>int main(void) $ ^% c  k# q1 z" H& e( ^
{ . B' I  e+ W, j2 O
int a, b; </FONT></P>
3 L6 T+ E( x" M% `' O$ \<><FONT color=#0000ff>a = 10;
0 ]4 U. e4 a0 O; R1 {b = 0;
! m" |7 L7 F* S' @if (b == 0)
8 H2 a! M1 O: p- ]/* preempt divide by zero error */
5 H. W/ @; P4 B: U" Eraise(SIGFPE); ! f) C6 g  ~" F# c
a = a / b; # P# K- Q& A2 P; r) ?4 D
return 0; . L* k9 |" j( b6 I1 y4 P* d
}
6 G+ V, \# `) \6 B% ]6 @</FONT>, U5 K4 W; c2 t# T5 {
</P>
" t1 ^2 \1 k. Z; |<><FONT color=#ff0000>函数名: rand </FONT>
* m0 N2 R" D, p: G$ V; K5 G1 a1 W3 x功 能: 随机数发生器
/ v& q; ~  G% k* |) O7 l8 O! Z% T8 w/ c# v用 法: void rand(void);
9 ]( W1 u; j: {  N* n2 l程序例: </P>, z0 B- N) b/ ~& {6 i
<><FONT color=#0000ff>#include <STDLIB.H>( @8 M& ?4 j1 c
#include <STDIO.H></FONT></P>
# R, n" w. E& p. U" Z8 ]" }<><FONT color=#0000ff>int main(void)
3 `7 D7 N' P3 e{ 3 c1 h; c; Z6 v7 d) L1 X  r- g: o
int i; </FONT></P>
# Z. v0 w) S5 @3 h! k% e) _2 m3 ~<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); 7 j2 X- |  _3 d6 ~
for(i=0; i&lt;10; i++) $ _9 i# L" l+ K# j. g9 `1 `
printf("%d\n", rand() % 100);
" V, O2 j) q4 oreturn 0;
6 R2 H4 g2 P" ^: k}
" z0 i. U2 \1 I( x
4 d: f8 {& W" i! M</FONT></P>" }- R. v9 }! Q( t
<><FONT color=#ff0000>函数名: randbrd </FONT># a0 }  o, Q# Y5 `& N, \! m) L5 r
功 能: 随机块读 $ A! A% M* U) y$ w
用 法: int randbrd(struct fcb *fcbptr, int reccnt); 2 x, ?! n. V& V8 q# S1 o1 K
程序例: </P>( @$ K8 a7 x7 y6 W( K+ X6 u% t8 K0 x
<><FONT color=#0000ff>#include <ROCESS.H>
: {+ [% l& ^/ ~% A0 `8 W; e* n#include <STRING.H>
9 `; W  y  [6 J$ b9 o#include <STDIO.H>
7 A( A$ c: B  ~/ i#include <DOS.H></FONT></P>
) v4 M' [( q6 I0 D+ d<><FONT color=#0000ff>int main(void) 8 n  J1 {7 D; R- s" p0 }+ F
{
' Q; r) i8 @; K' w# Rchar far *save_dta;
+ e7 `2 O* I" z5 p! o7 }" O& Pchar line[80], buffer[256]; ( c8 U" Y3 N9 S) B; g: ~$ i) X4 t
struct fcb blk; / W/ y4 M3 d. p2 v' a; [7 R
int i, result; </FONT></P>) t9 b: `* r9 I7 _3 u# O4 i
<><FONT color=#0000ff>/* get user input file name for dta */ 9 _' R9 ~4 n+ P& F$ a7 Z7 m
printf("Enter drive and file name (no path - i.e. a:file.dat)\n");
3 g  X$ a8 G! Y) H4 Z  u; ]gets(line); </FONT></P>' V0 L- V6 n2 e5 ?# _
<><FONT color=#0000ff>/* put file name in fcb */ ( g. C4 @3 Q* Q$ ^
if (!parsfnm(line, &amp;blk, 1))
  g$ e  P4 T8 }4 J( P{ 8 O% c; _# r5 }8 f8 ?# x0 a2 u
printf("Error in call to parsfnm\n"); 6 s* \" R  u  ~' v5 [1 C1 `# Z
exit(1); ; P& M7 ?9 ?; s/ L6 Y+ j* i
} 5 u, W: @! f5 f
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>7 Z! @7 |" f" S$ s  V
<><FONT color=#0000ff>/* open file with DOS FCB open file */ # H4 a; J( h  m% c3 R# @
bdosptr(0x0F, &amp;blk, 0); </FONT></P>; S( J0 X8 m6 T% \: {# R% u
<P><FONT color=#0000ff>/* save old dta, and set new one */
  a! ]% X; O% I; Z: v5 Esave_dta = getdta(); % V5 b+ z# ~- |! K/ l0 a
setdta(buffer); </FONT></P>
  y2 g4 g. p3 R+ y7 Y* i: f; B8 W<P><FONT color=#0000ff>/* set up info for the new dta */
  q6 w/ C) ?* `blk.fcb_recsize = 128; - Q7 a! X: y. i  O  V; m6 J$ {
blk.fcb_random = 0L; 5 ~' k& b8 E% v5 Z
result = randbrd(&amp;blk, 1); </FONT></P>+ p3 W0 k: R: }; N8 y
<P><FONT color=#0000ff>/* check results from randbrd */
- o& i3 u/ F5 _& Sif (!result)
: \7 u/ F9 z9 l: o1 I3 Tprintf("Read OK\n\n");
9 l' w9 O4 v2 |- x6 G( j7 B  gelse , H, B0 l; V3 k# Y9 M
{
5 l& B/ Q, y3 A$ O, {; @4 e/ fperror("Error during read");
. ~7 s' D" X* z& P: s+ Wexit(1);
% a! m) q9 U- H5 U: u2 X} </FONT></P>
- ]/ q1 W3 g3 |+ D1 v<P><FONT color=#0000ff>/* read in data from the new dta */ 2 ^3 {% m9 e, [1 N5 j# s, V
printf("The first 128 characters are:\n");
' \. b2 X4 e& y2 Z  {( Efor (i=0; i&lt;128; i++) 1 b: d9 T3 H( {: g
putchar(buffer); </FONT></P>- ]/ _' R: @- u" z
<P><FONT color=#0000ff>/* restore previous dta */
2 Z/ c: Z; R9 Ssetdta(save_dta); </FONT></P>0 C* T4 b3 _3 Z
<P><FONT color=#0000ff>return 0;
! i* L8 E1 G) l( q- ?6 l} </FONT>
* M- V; O+ D+ p  R1 x# Y</P>; I4 m% m  B' x0 t
<P><FONT color=#ff0000>函数名: randbwr </FONT>
, `+ P, y6 ]# W" ?* F9 E# {功 能: 随机块写
. |) \. I0 t/ q+ I+ O5 p8 B4 n& P0 _% Z用 法: int randbwr(struct fcp *fcbptr, int reccnt);
3 G8 x+ r5 U" o; }; z6 A' A9 f0 p程序例: </P>
8 E8 t' g8 W) e0 p4 P<P><FONT color=#0000ff>#include <PROCESS.H>
' G/ J) e  c* s#include <STRING.H>
8 {7 j0 K* ?+ S5 r- h  T% l#include <STDIO.H>% |4 B: w; z3 O5 Q9 D
#include <DOS.H></FONT></P>2 p$ U% Q# {# m; N
<P><FONT color=#0000ff>int main(void) 1 R) _7 Q4 a' U/ ]7 z
{ ; [( `, R' ]' q9 [+ {( `
char far *save_dta;
  {: }. Q# ?, I% _. m, Jchar line[80];
8 U  |% [% u$ S$ Uchar buffer[256] = "RANDBWR test!";
: k1 ^: @. j$ V* `! Nstruct fcb blk; 1 W! R+ l: N5 j4 i
int result; </FONT></P>
6 }+ }1 z& o# f- A9 o% h<P><FONT color=#0000ff>/* get new file name from user */
* x9 ?* c& [1 v  T7 S8 h0 H  s) sprintf("Enter a file name to create (no path - ie. a:file.dat\n"); - t) W* V$ }' P
gets(line); </FONT></P>
/ F1 f7 S7 D: h( w$ @0 \  Y<P><FONT color=#0000ff>/* parse the new file name to the dta */ : {, o0 p7 Q) }% |) @. r
parsfnm(line,&amp;blk,1); 0 v9 j& C3 n3 c& S
printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
) V' M. o6 Q$ q4 Q4 j+ R<P><FONT color=#0000ff>/* request DOS services to create file */
. ?: S. [' U: U! S" E+ |if (bdosptr(0x16, &amp;blk, 0) == -1)
4 b( _1 X( \5 \5 E; ?0 u{ & U1 [" B5 m" [$ n3 D
perror("Error creating file");
- s- |8 h0 \' w% y2 K3 oexit(1); 5 f4 f4 z6 w" [2 N4 z! f
} </FONT></P>
# b8 ?2 h& ^7 a8 [; e5 y* A# v<P><FONT color=#0000ff>/* save old dta and set new dta */ * p5 b4 j+ b( H3 y
save_dta = getdta(); 5 N% ]+ _" }$ e) B
setdta(buffer); </FONT></P>
' k- ~; L& |  q! F9 f" f<P><FONT color=#0000ff>/* write new records */ ( V% j5 ]7 h* ^. ?4 B
blk.fcb_recsize = 256;
3 _+ _* Y% A; w0 Ublk.fcb_random = 0L;
, [/ P: M) @+ n& D0 i+ sresult = randbwr(&amp;blk, 1); </FONT></P>  c( c4 k% g1 o" {& }' b2 P
<P><FONT color=#0000ff>if (!result) ' `- R! {. e4 S
printf("Write OK\n"); , y* o/ k- p. N; m$ z3 i
else 5 ~) D- W5 y2 {5 L
{ / J5 i4 }% \' I
perror("Disk error");
4 A: e* c5 P6 A0 @% sexit(1); ! u. ~- x8 C1 H0 Y8 m
} </FONT></P>& u% L- s9 \" d
<P><FONT color=#0000ff>/* request DOS services to close the file */ * `% p0 Q2 |1 Z1 r/ P2 S$ R
if (bdosptr(0x10, &amp;blk, 0) == -1)
( t, F. E; }# A( i& i{ 4 z1 p( `, h  ?' M
perror("Error closing file");
) D' y8 H% ]. R0 W$ [! g7 o2 jexit(1); # j3 l$ f9 L" `4 K
} </FONT></P>
; |; {  k/ l; h& c3 k( D<P><FONT color=#0000ff>/* reset the old dta */
8 i9 I5 k6 @0 W6 l* Vsetdta(save_dta); </FONT></P>
" q6 U; J% G3 J- D<P><FONT color=#0000ff>return 0; . \0 H/ M, s  S; u* V. y! z% ?4 ~3 s
}
# p7 j5 {/ Q" T5 U5 \" |9 k</FONT>
3 G. ^/ L4 F2 `3 Y2 l5 V</P>
! |2 S5 H( D4 @3 O5 ]+ {2 i  j<P><FONT color=#ff0000>函数名: random </FONT>
* L# ~* p) D3 N# h功 能: 随机数发生器 ! M' R) Q% y* M
用 法: int random(int num);
2 D7 W; o8 M4 {7 P, M程序例: </P>
% ~; z+ h& _" E. j8 T<P><FONT color=#0000ff>#include <STDLIB.H>$ S1 a% O1 w) n  R! {( h
#include <STDIO.H>
. ~) u7 ^% r: h' _$ n- W  ~9 A#include <TIME.H></FONT></P># Y( s; P' k0 d2 M6 X5 N" K
<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ : ^% u: f, F$ G8 c
int main(void)
' ]  d$ C9 F+ \' \; ~/ d) h: w+ z8 i{   p* V" \$ o9 I# h: Q% w& C3 A
randomize(); - y* E3 o1 X, i$ v, K  A6 W7 _
printf("Random number in the 0-99 range: %d\n", random (100)); , m* R; k. {; e6 ]! m
return 0;
- M$ w( Z0 E3 k  w& [5 S( f} & c4 T, K; n- I
</FONT>% b/ H1 e0 s) `5 X' f& I/ \  W
</P>/ z( z: k! U: t# o# W7 X
<P><FONT color=#ff0000>函数名: randomize </FONT>
! L$ r3 c: D/ }, Y9 ?! Q! u功 能: 初始化随机数发生器
7 `& P9 k" W6 G6 N% {# i+ q用 法: void randomize(void);
* j( [+ E$ Y8 x7 S" r. Y9 r0 L程序例: </P>
7 L. L, k. U; f( v# `" w5 t<P><FONT color=#0000ff>#include <STDLIB.H>& n  i2 D9 T! V5 u, q
#include <STDIO.H>. _8 l* h2 M4 G3 o
#include <TIME.H></FONT></P>
2 r. w2 n5 @" y# m<P><FONT color=#0000ff>int main(void)
6 s9 f- l! t& B! c{ 8 v# Y- u( q" ]) V8 |
int i; </FONT></P>
& p3 A1 c, X# i& N<P><FONT color=#0000ff>randomize(); 9 T0 P( u( b6 r" S- S! U$ o
printf("Ten random numbers from 0 to 99\n\n"); % ^8 Y* M5 T% j: O
for(i=0; i&lt;10; i++) ) ^* o3 S; L8 b. t" z
printf("%d\n", rand() % 100);
: H$ n) p* P: e& }2 r( o/ ]return 0; 0 `4 _- x9 X* V  e$ @
}
% `9 B) J* u9 i( u</FONT>
! g( j" O3 {8 Z, C$ i: @! e</P>
5 X8 B: L4 y1 ~* Y" R<P><FONT color=#ff0000>函数名: read </FONT>' ~/ ?& H+ [3 N
功 能: 从文件中读 $ x" [/ _8 N3 [: n* Z& k" n
用 法: int read(int handle, void *buf, int nbyte); 3 j3 U. L. d) E/ I) [/ |
程序例: </P>
6 c2 }$ |7 _4 A# |" X5 c0 G<P><FONT color=#0000ff>#include <STDIO.H>
% f! j2 I4 Z6 W#include <IO.H>8 d' [" f- a) C
#include <ALLOC.H>( E, G& i2 ~% l% c
#include <FCNTL.H>
6 X! ^4 b! U1 h: R! s#include <PROCESS.H>
& n" d0 j$ Z! E0 p5 K, O* g#include <SYS\STAT.H></FONT></P>
/ I4 {. Y) z) o) I3 p0 W<P><FONT color=#0000ff>int main(void)
( x& ?: i* S; @$ Z, @$ p) I{ & C$ \  ?; c# E$ t
void *buf; 2 A# M0 P9 r& Q
int handle, bytes; </FONT></P>1 o% [! d' C  G4 E1 y% ^7 `
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>  w! @) U4 E+ t% l8 G, J5 H" q
<P><FONT color=#0000ff>/*
  C& ?$ R6 N8 Y; [' z- |- QLooks for a file in the current directory named TEST.$$$ and attempts
& _  B2 `0 N  q' F1 j. C& Bto read 10 bytes from it. To use this example you should create the ! ~3 W) P: b* U* L5 x
file TEST.$$$
7 V; l- q0 v! o, }! F& M8 u*/ # \# ]% b4 A+ N: u7 I$ G* s9 h
if ((handle = ; B9 k1 o* u# C& P( M/ t' }/ C& f7 ?
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
9 ~6 n, P0 N2 c, U" z+ S{ / w) U3 Y7 @+ S5 }* z6 G
printf("Error Opening File\n");
' @7 I5 ~  {' B6 r& c0 L1 E% nexit(1); ; @! Y4 F9 V7 |1 e4 ^% V
} </FONT></P>2 u4 \; l) T3 @5 @& B
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) { , {8 a7 A! ?; `. t' k' \
printf("Read Failed.\n");
4 g) g  Y7 d; P* Z& W3 B* J. }" Jexit(1);
5 ~+ |+ n. ?% z4 c+ S# c& Z}
* j+ H9 Q/ v9 A" ]$ \else { # ]; p1 Y1 U5 I  q- d1 h+ z
printf("Read: %d bytes read.\n", bytes);
8 M. L/ Q$ \3 @/ o( B3 s}
; {1 k- b6 S2 V& a3 @, ureturn 0;
" S  v% R  J1 h3 c; f8 y" `( {} </FONT>
1 G5 h. X# L, ~4 W5 H' z# ~6 a' v7 \6 s
</P>
8 K. l9 |: m% d0 a<P><FONT color=#ff0000>函数名: realloc </FONT>
8 S+ K/ o4 S7 H3 a& Y功 能: 重新分配主存 / _6 X7 y/ Y5 x# l# u
用 法: void *realloc(void *ptr, unsigned newsize);
4 ?2 _# J: q% c1 o; s程序例: </P>5 o4 n0 j) I( z
<P><FONT color=#0000ff>#include <STDIO.H>/ n9 X/ w1 H+ L8 U! R
#include <ALLOC.H>
! r7 Q7 ]4 U8 ]- F#include <STRING.H></FONT></P>
3 i7 D. R/ _4 b! i: G' Z* ^<P><FONT color=#0000ff>int main(void)
4 A0 Z4 j% q0 A6 R( A{ + x; I2 ^. m  ^8 W/ G
char *str; </FONT></P>
7 p  D8 T8 X! y$ K+ Q# n<P><FONT color=#0000ff>/* allocate memory for string */ * g: t5 j" i+ K  p
str = malloc(10); </FONT></P>' i' T+ n5 Y7 s
<P><FONT color=#0000ff>/* copy "Hello" into string */ $ d8 n/ I3 j# L
strcpy(str, "Hello"); </FONT></P>
+ @3 A6 v0 m8 T, w  u3 E& S<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); ( {' l, O7 |6 s+ E( b4 W
str = realloc(str, 20); " F7 g; C5 j8 ?4 Y4 G
printf("String is %s\n New address is %p\n", str, str); </FONT></P>6 L& Q1 v/ O0 k, h$ ?
<P><FONT color=#0000ff>/* free memory */ 2 i& x- s( ~1 I7 o3 ~4 n% S
free(str); </FONT></P>
, n8 N2 O: }9 D- D3 b: u& t, a- _<P><FONT color=#0000ff>return 0; 3 q$ `: u- l: q' T3 Y- R
} 4 g( k6 [  Y1 S6 b3 n4 Z
</FONT>
/ T- d0 W; `0 J  _3 [& Y* T: ?</P>) Z# N. h. r6 F. x- w" M
<P><FONT color=#ff0000>函数名: rectangle </FONT>
, W, c: O2 L6 E0 F功 能: 画一个矩形
; Z2 m0 a0 t  }5 h+ Y; b2 K用 法: void far rectangle(int left, int top, int right, int bottom);
! J. n, u; E! @5 u6 c$ y7 i程序例: </P>8 U% g- ^6 e4 ^- P) Y- t1 g. x
<P><FONT color=#0000ff>#include <GRAPHICS.H>
5 i9 D: b* v% H% L#include <STDLIB.H>/ t1 G2 T3 N1 j7 r1 L7 Q
#include <STDIO.H>
1 D! O$ |* p1 ~& j#include <CONIO.H></FONT></P>
$ K1 m: N+ G1 L( R) B  w9 B<P><FONT color=#0000ff>int main(void)
7 S7 ]5 g  X4 V, M) o0 @& x9 a) \{
( W. ~9 T4 ]8 y2 r/ T/* request auto detection */ " q& Y' B$ H0 R* ?
int gdriver = DETECT, gmode, errorcode; & D! R8 [! P( Q! {! J
int left, top, right, bottom; </FONT></P>
: ?2 V) Y! b% x( x7 Q/ Q<P><FONT color=#0000ff>/* initialize graphics and local variables */ 7 w0 k- F9 T+ Y
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>1 w3 U0 @3 U4 K" y" ~& E: l
<P><FONT color=#0000ff>/* read result of initialization */
: P" v! w3 c6 D% c& j. [errorcode = graphresult(); # [" M6 r- P. b" E" y
if (errorcode != grOk) /* an error occurred */ # Y! e. {- Y! q7 T4 y; F
{
& W1 O  O8 J( B) ?9 q* dprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 9 u6 J" v1 `! l+ z
printf("Press any key to halt:");
/ G3 c2 D) D# l) N, Fgetch();
% g2 n6 a1 F' F0 ?; Z5 o" t. vexit(1); /* terminate with an error code */
* i$ H  D" t( A/ G4 Y/ ^} </FONT></P>
/ m6 o) b' n! K' V; ]0 N5 @% G5 e<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; # R4 s; u% M, v5 y/ [! R
top = getmaxy() / 2 - 50; : |! _9 B- o! k8 H- `, @
right = getmaxx() / 2 + 50;
" C0 Z# E% j$ o4 H# w- I- X* Wbottom = getmaxy() / 2 + 50; </FONT></P>! t4 J% O4 p6 @! ?
<P><FONT color=#0000ff>/* draw a rectangle */ & R" k8 n) [* a+ z$ N* k
rectangle(left,top,right,bottom); </FONT></P>
% p* M) \% Z! E9 j' l. J- ]<P><FONT color=#0000ff>/* clean up */ 6 P7 D4 P! y9 Y# A3 Z
getch(); + l: o% T% S: I1 z( Z2 g; H( U" s
closegraph();
( ^8 f- V. \1 o8 E0 {3 Y. @return 0; * B! a$ j3 \8 _$ {/ G: k( E0 r
}
7 a! V8 F, [; w8 W5 B9 U8 ]$ {</FONT>
8 }/ X/ l3 _7 `  A</P>
( A, z( b1 q* K* V<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>* c- |7 g2 c& l5 M
功 能: 登录已连接进来的图形驱动程序代码
& X% T8 [, p0 e6 |& M2 m2 h7 z: v: J用 法: int registerbgidriver(void(*driver)(void)); : ?2 n4 z* }7 ~! o, u
程序例: </P>* k5 b+ Z, Z/ @% c, w
<P><FONT color=#0000ff>#include <GRAPHICS.H>5 r; l" o+ {; Y$ F: d9 q
#include <STDLIB.H>
3 o: N3 Q( g% L: C9 \/ k8 L$ G. U#include <STDIO.H># h' g8 O) S) U1 N
#include <CONIO.H></FONT></P>
7 X8 K( W3 `3 W) ]/ m3 \% m4 \9 K<P><FONT color=#0000ff>int main(void)
4 p" {! R/ U6 B6 ?: O* I{
) ^2 _7 x9 b7 @1 r* s" x/* request auto detection */ * Y) Y: a- J& n% G- M. s
int gdriver = DETECT, gmode, errorcode; </FONT></P>1 T* N4 F. v8 }& ?; x
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ 0 S; y* e' n2 v7 A: p. m2 X
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>0 m  s3 T6 m" J7 Z0 h
<P><FONT color=#0000ff>/* report any registration errors */ * [+ y- |0 }9 t# D: B8 T
if (errorcode &lt; 0)
2 A8 b+ a, H& f6 B{ # H6 r" z) L* B3 f% d
printf("Graphics error: %s\n", grapherrormsg(errorcode));
2 I* b7 ?* h! {$ w. _printf("Press any key to halt:");
# i& G5 a4 A9 k  L% J; kgetch();
% D% v2 r6 C$ o5 R2 dexit(1); /* terminate with an error code */
- ]2 j1 y& V! H, d} </FONT></P>
7 C! @9 k, F" U- s$ m2 ^" [<P><FONT color=#0000ff>/* initialize graphics and local variables */   ?8 Q" L+ a  n6 n1 R! O, D
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
: ^* [) P: i9 _0 s1 z<P><FONT color=#0000ff>/* read result of initialization */ ) E9 B" l' r1 f- e! j. f/ F& r
errorcode = graphresult();
& |0 W+ }) \( ^if (errorcode != grOk) /* an error occurred */
# G6 D" f1 r, S# m; p$ _. x{
0 }9 |& Z1 F4 a6 r: r$ A( Xprintf("Graphics error: %s\n", grapherrormsg(errorcode)); $ t- H; L* |- }; I
printf("Press any key to halt:"); - f" G+ J; R% i
getch();
) u. e! {3 G9 k( u) }9 m, ^exit(1); /* terminate with an error code */
5 z3 H/ Z; m; D} </FONT></P>
) |- n( C) U7 n1 C5 X: K<P><FONT color=#0000ff>/* draw a line */ 0 w# ^$ c* A# V6 I& t
line(0, 0, getmaxx(), getmaxy()); </FONT></P>8 P8 R; l4 }$ w9 p0 Y
<P><FONT color=#0000ff>/* clean up */
% G7 ?1 Z" g8 k- c# i- egetch(); 9 o# ^4 U( j  |, R( v( v3 }
closegraph(); 7 Q' b. j4 H/ W: f4 i  [6 T
return 0;
) d- s/ [) e& |6 N9 L0 T}   N) ^$ [0 X, h7 v' O6 c" ?
</FONT>
1 F  m% W/ ~& p: G8 a7 b</P>, z0 x4 O: }9 k0 b& P: u  H$ d" Q
<P><FONT color=#ff0000>函数名: remove </FONT>
# M& j& d% X: k8 W" `, n& I功 能: 删除一个文件 + t* p, R- j" v; t1 t9 M: `  C/ c
用 法: int remove(char *filename);
" ^7 P8 k  K, m/ H) r4 M程序例: </P>; [# P" W: j. |* k7 p- n6 t# ^9 @  N# x6 V
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P># `0 m& y4 t8 Y0 L9 W) t
<P><FONT color=#0000ff>int main(void) - {! W! y% Z& U9 Q. s1 B  t+ \, ?
{ ' H7 A: d# A* ]/ I  L
char file[80]; </FONT></P>
8 b6 q% p" b& A( h* U<P><FONT color=#0000ff>/* prompt for file name to delete */ * R! f5 z3 X' l$ _4 p8 j# J
printf("File to delete: ");
2 @- Z8 M; F$ p: P' Ggets(file); </FONT></P>5 W9 E- M* r4 r* E; g* B5 Z
<P><FONT color=#0000ff>/* delete the file */ ! V0 J( v$ f! x0 n7 X1 [9 s: [2 z
if (remove(file) == 0)
! x' {, @) S4 X( q! i' Vprintf("Removed %s.\n",file);
! @( y9 c5 E- M6 c) X2 delse
' v- S/ r0 V7 N3 b) G, \2 \+ }perror("remove"); </FONT></P>5 M; B$ N2 S8 d# z% V
<P><FONT color=#0000ff>return 0;
4 r, L$ b7 X. o* L: w}
: d7 E9 N6 q6 T: O9 a: }- K, b* U4 o# a
</FONT></P>; o* K- H% C2 ?" Y
<P><FONT color=#ff0000>函数名: rename </FONT>7 T- n1 P1 I, H6 e9 V4 P& c2 {
功 能: 重命名文件 8 F  F; i( ?. q' c
用 法: int rename(char *oldname, char *newname);
: e* V! w9 ^+ `# e$ y. h7 X程序例: </P>' B; [% t# H, z8 L- X6 r5 f3 ~+ E
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>, z  J0 z( K5 a! H+ C6 }4 Q3 K8 g
<P><FONT color=#0000ff>int main(void) & \. o5 N5 d7 o/ Q
{
! O1 A3 Z, c0 o9 ychar oldname[80], newname[80]; </FONT></P>& A# x! x" I2 j1 ^( ?5 N( c& d' C, S
<P><FONT color=#0000ff>/* prompt for file to rename and new name */
9 P' `8 R& X! H- B' uprintf("File to rename: "); 5 l, r; U; c- P8 S3 Q
gets(oldname);
+ c: y6 ?; @! U8 u8 E% c8 Qprintf("New name: ");
& q1 {& b  Q- A( ^5 U' pgets(newname); </FONT></P>6 x0 D3 ?) _/ ?: b
<P><FONT color=#0000ff>/* Rename the file */
3 B& t; R) ~1 S" Z  G* d% l) X* ?. M; vif (rename(oldname, newname) == 0)
1 U  e- w- n/ hprintf("Renamed %s to %s.\n", oldname, newname);
7 N* D7 l. ~  S, r* e( D2 Eelse : q6 l' d9 W6 y' p. N1 o. ?
perror("rename"); </FONT></P>
' h6 M* ?# [3 c" ]<P><FONT color=#0000ff>return 0;
. x5 c' Y& N- g} 5 G3 E# j/ _+ S/ C3 K3 t, |& {: ]/ n

* ~1 G* C' [& ?' ^  |, J/ Z</FONT></P>. ^0 }# H+ t* Y& R" V5 n# t/ \, k6 ]
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
/ ]) }! p8 G5 C, S功 能: 将屏幕模式恢复为先前的imitgraph设置 0 i# P5 d) |$ w+ g
用 法: void far restorecrtmode(void);
$ v' n: C9 w9 O4 G2 I# S程序例: </P>
) O$ Q3 S+ S# @9 G+ q<P><FONT color=#0000ff>#include <GRAPHICS.H>7 \" z& Q# ~9 \/ }# i. W
#include <STDLIB.H>
' q( @) @+ a( l#include <STDIO.H>
9 K' I8 v! y. G% n! z#include <CONIO.H></FONT></P>: C3 E0 k+ [, L& b6 |1 `0 K) w
<P><FONT color=#0000ff>int main(void) % s+ S* Y! D8 h* Q) F8 }/ ?
{
+ S, |8 b# f2 a, t+ V/* request auto detection */
, o7 q& Z: K; X$ K  y' bint gdriver = DETECT, gmode, errorcode;
$ h9 N# o; r. d. E3 a8 I- Dint x, y; </FONT></P>
) q. u. V- L  Q, ~& t# ]) L/ Y<P><FONT color=#0000ff>/* initialize graphics and local variables */
9 d* _' Y  V, W8 O4 q2 t, p3 einitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
" K, n' P2 l, P. y<P><FONT color=#0000ff>/* read result of initialization */
, Y5 U' m* F8 W5 p) y) gerrorcode = graphresult();
  }* y1 s" a: Iif (errorcode != grOk) /* an error occurred */
2 |  x5 \( b. n' f4 w{
8 a" x6 C9 O6 \printf("Graphics error: %s\n", grapherrormsg(errorcode));
4 v" y: k: I( Z& eprintf("Press any key to halt:"); ' f; \0 p, k# w% h' s" Z& }
getch();
- R6 N! S# H9 }8 ~& @6 T5 V7 Wexit(1); /* terminate with an error code */
5 r' \( b0 E0 e} </FONT></P>. O$ [5 {7 s1 i) R
<P><FONT color=#0000ff>x = getmaxx() / 2;
5 J; o+ _) f) D+ o1 K% t' [y = getmaxy() / 2; </FONT></P>
  x, x6 r" v2 ^- n1 X5 E) i<P><FONT color=#0000ff>/* output a message */ 5 _0 u: Z, ^4 a: p  J% w- E
settextjustify(CENTER_TEXT, CENTER_TEXT);   m3 g! k1 w3 f$ D
outtextxy(x, y, "Press any key to exit graphics:");
5 C- m1 N3 s) p0 J0 m: w0 R% [getch(); </FONT></P>
5 C, y% J7 o) [% E) G9 x<P><FONT color=#0000ff>/* restore system to text mode */
7 a' v+ I  p; }: Xrestorecrtmode();
" _  Q* X( K" \# u# r5 F# l  Zprintf("We're now in text mode.\n");
* m# e. ^) q$ I. fprintf("Press any key to return to graphics mode:"); ! B+ c" N3 B, t2 Q6 M9 f3 ]
getch(); </FONT></P>
0 [* l  |: M9 l; v6 _# F1 v<P><FONT color=#0000ff>/* return to graphics mode */ " A) ~- I  K4 a7 q, W# G2 ]
setgraphmode(getgraphmode()); </FONT></P>! Z7 I1 S8 N" ^7 p# Y6 ~
<P><FONT color=#0000ff>/* output a message */ * @' ?( `2 N6 m+ \. y( J: N
settextjustify(CENTER_TEXT, CENTER_TEXT);
; C1 Q! _& [) B" ?3 youttextxy(x, y, "We're back in graphics mode.");
* p$ Z) d* G& d; Bouttextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P># {, @# \, I: w4 v1 W
<P><FONT color=#0000ff>/* clean up */ 5 Y2 \( U% T9 F( a0 b" }0 V1 V
getch();
: f$ w* S$ w7 a2 p# I$ ^, E" u5 |4 Kclosegraph();
& L0 V. M( E# }return 0; ( o/ `2 q6 [1 o
} 5 X" `) w  n9 Q
</FONT>
' D" g; n+ H* M) c" i0 P; n, V9 N</P>% p  S, m/ Q- j: _$ U8 z
<P><FONT color=#ff0000>函数名: rewind </FONT>
; Q) D) q. N/ m/ ~# H功 能: 将文件指针重新指向一个流的开头 % U/ H" i/ E! }
用 法: int rewind(FILE *stream); * x- @9 J- H8 J" d+ n; |
程序例: </P>
+ m: [6 g( P& f4 ~) e<P><FONT color=#0000ff>#include <STDIO.H>
1 z8 O; u9 s7 a9 c0 q#include <DIR.H></FONT></P>
7 {% x; X' c2 ?! a! o( Y<P><FONT color=#0000ff>int main(void) ( ~4 D& E. I3 k8 |# o& j& |6 e3 t( ?
{ # a/ V' H! y! D4 U! N
FILE *fp;
. F4 \2 C& s* ~0 Schar *fname = "TXXXXXX", *newname, first; </FONT></P>; M- q. }: F! L) K! Z
<P><FONT color=#0000ff>newname = mktemp(fname); ) B% K% S- y( H
fp = fopen(newname,"w+"); 7 F, S/ z2 w$ u4 w
fprintf(fp,"abcdefghijklmnopqrstuvwxyz"); 2 U% b# o4 U  t' b" S- G/ E
rewind(fp);
: N; r/ j/ a* [fscanf(fp,"%c",&amp;first); 2 [5 `. J$ |: n% y( q
printf("The first character is: %c\n",first); 4 u4 y, b# O( j5 f' S+ H" G
fclose(fp);
. r* D6 @  h. w: B' nremove(newname); </FONT></P># E1 w+ B! [- }3 }5 @
<P><FONT color=#0000ff>return 0; ( G! m! L/ J2 d( M8 ^2 U* v
} 9 x8 ~8 a5 }% l% g9 H. y/ t4 U+ E
</FONT>, ]; t  F  K+ c& S1 M) t
</P>
( ~; H' N6 V1 |! y8 C<P><FONT color=#ff0000>函数名: rmdir </FONT>* z3 i  [( ~4 b0 s- t3 D
功 能: 删除DOS文件目录 % P; v" e% Y' @4 E: V- G
用 法: int rmdir(char *stream);
' q9 q% T1 S! \0 x程序例: </P>( b1 x) H8 @$ \: {3 e
<P><FONT color=#0000ff>#include <STDIO.H>
* U+ Y- S; X. H% e, I# ?#include <CONIO.H>! Z& @! A  C+ t+ D9 A4 k
#include <PROCESS.H>
! g9 a6 O/ X/ P/ i! L#include <DIR.H></FONT></P>
3 U$ }2 n7 p8 D, V<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>% U4 b+ f" t# w0 u0 U" e! K9 F6 x4 e
<P><FONT color=#0000ff>int main(void) " [6 f/ _- ]5 S$ U- |* H6 D
{ + F0 p5 n, e% v7 ]2 o; w4 R
int stat; </FONT></P>8 C$ [- F2 g, F) W! z- {1 B4 k) A
<P><FONT color=#0000ff>stat = mkdir(DIRNAME); . _; u, K( L/ F# p8 R  _. X2 _5 E
if (!stat)
. K5 [5 S! Q! r& h5 I8 Eprintf("Directory created\n");
# O" X3 G" M. q6 U; `; nelse
2 a" Y5 J( n# h+ q{
+ N% B4 r3 i/ C' E; rprintf("Unable to create directory\n");
' e9 C* I" l1 ]4 m9 C, {exit(1);
% q1 @; K7 E1 ], q: i* [8 E} </FONT></P>
6 ?6 b1 S. A! D0 Y, i2 X8 o- O<P><FONT color=#0000ff>getch();
. J6 R+ \2 |3 lsystem("dir/p"); ' w. ], r  D9 f- g/ D- o( z# S. G
getch(); </FONT></P>
: S5 W) q6 i: M. _+ A" E<P><FONT color=#0000ff>stat = rmdir(DIRNAME); * w+ T% J  H8 `, O+ Z' f
if (!stat) - ^, {3 Z* n/ l1 ^. D
printf("\nDirectory deleted\n"); 6 Y) P( h3 h, h5 y6 @9 W
else
# r! U! G6 q. q! q$ u1 S& P. V{ ' e' _: ?% t, ~; T( P
perror("\nUnable to delete directory\n");
; E$ E+ ~: N0 S: G( O6 mexit(1); ( {9 |, A, s' l5 E
} </FONT></P>
; G, A$ @' T  G& H4 N<P><FONT color=#0000ff>return 0;
' M. R; f5 [5 ^* P$ p5 y6 {- [} </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-7-21 01:38 , Processed in 0.793649 second(s), 51 queries .

回顶部