QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>9 ~# ^: F2 ~) J  P0 _5 h
功 能: 使用快速排序例程进行排序
; h/ {& n' J. |" Y用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); : M  _/ k- r' S4 u3 v. r3 a+ b
程序例: </P>0 W( u( O$ o) |9 \* k+ @. L
<><FONT color=#0000ff>#include <STDIO.H>) `$ j4 F( ?7 c$ E
#include <STDLIB.H>
. U" M2 S  h" A5 V#include <STRING.H></FONT></P>) j' ?; t$ I; R/ w8 ?. K
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>. ?1 C) |9 A9 x, {2 q
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; . Y9 x0 F3 e# [7 C- t
</FONT></P>! l$ t5 C4 k/ p' Y5 t& d+ r; h
<><FONT color=#0000ff>int main(void)
( Q9 [6 y1 A! @! m; T% g" y2 d{
5 f6 b! j& W5 U% Jint x; </FONT></P>
  D2 |  Q9 |. J, O' Q: s% t1 j<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
, e( [: p0 t( Q9 X5 ufor (x = 0; x &lt; 5; x++) 3 w) {$ r2 J( |, }6 e( p' W
printf("%s\n", list[x]); / T8 O- F* H" _3 h# r
return 0; / f2 `2 i. i; V9 m4 ?6 m
} </FONT></P>
& c& {8 D- h4 W4 ^5 V8 ~; c- S% W<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
9 c% X# L/ \4 M  j% ~% F{ ' u4 U) v" W. Y- B3 X+ F1 |$ }
return( strcmp(a,b) );
8 d$ D" R2 k7 z' c} </FONT># X( V' q' \; E$ g1 |! g; N  W
</P>
' H6 P4 Y0 E5 t+ N
" P* [, y- W, R
6 [# f1 g, K1 Y6 }<p>
7 f, C5 }5 P" X! v" S1 P<><FONT color=#ff0000>函数名: qsort </FONT>
0 R# I0 H6 j8 h' z" r, H" Y功 能: 使用快速排序例程进行排序 2 B2 D7 x6 t3 l1 J& @- o- `2 J0 L
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
; K* r) s3 H/ g' U- E& j9 \% r! S6 x程序例: </P>
  q, }  k' b5 j& V; B9 J<><FONT color=#0000ff>#include <STDIO.H>4 t- u2 K$ [* q; h5 [0 U
#include <STDLIB.H>1 Y9 R3 m( z2 [- K# z
#include <STRING.H></FONT></P>
6 r& q* I$ j9 h) ]+ z" u<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>9 v4 a$ A* ~) C3 ?/ X, Z2 e# _/ M$ s3 q
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
# g! k! ?" L) U4 u2 B% q* C1 W8 F</FONT></P>
9 A1 d! }3 S3 V% V! E3 y4 J<><FONT color=#0000ff>int main(void) % l# d8 V* }3 R4 v# N1 W
{
+ k+ t/ Q) c2 w6 N. Pint x; </FONT></P>6 f! ^( [, Q  Q# e8 M3 R
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); ; S3 d: m& A- P2 V$ X: [
for (x = 0; x &lt; 5; x++)
: n9 i/ ~7 J7 wprintf("%s\n", list[x]);
+ T. Y; ]8 O/ [5 v, rreturn 0; : Y; e5 ~( T4 s
} </FONT></P>% I3 |( t& q3 G$ @$ H
<><FONT color=#0000ff>int sort_function( const void *a, const void *b) ( u# t( j5 C# x1 h  v' v4 I
{ # R% U0 F/ K' _/ q! n
return( strcmp(a,b) );
# ^; U9 T/ }% {; y# s& e6 N}</FONT></P>9 X) s8 f. W: `" ^3 G' o4 c
<><FONT color=#0000ff>5 y5 U3 T+ U$ g! T1 T
</FONT>$ k/ F, ^; g- v: ]9 J
</P>
, p1 h1 x  v9 I4 y- j<><FONT color=#ff0000>函数名: raise </FONT>
& K0 {& g1 q. a) k  P* Q/ K& P8 ]+ }功 能: 向正在执行的程序发送一个信号 5 w4 A2 r0 |% {( O, [# v* n
用 法: int raise(int sig);
7 \4 l5 t* z" `: I$ r' ^程序例: </P>
/ t4 [* t5 M  f0 |2 J7 \0 `' ~<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
/ D2 d: |4 \# S* L<><FONT color=#0000ff>int main(void)
, k0 p* V* Z2 C' F{
# Q" A# q8 k  E! W  zint a, b; </FONT></P>5 G3 l- @! N' {( ]" L( G- @
<><FONT color=#0000ff>a = 10;
: x$ R3 ]5 K: W# Y2 ^( o) Kb = 0;
* a1 g2 [8 p3 {; N% N( Mif (b == 0) 4 p2 H% d4 h9 |/ x. R
/* preempt divide by zero error */ - i3 L4 Z. U7 K
raise(SIGFPE); * U% o/ S- L  g& ?5 G8 d
a = a / b; , t* C, e! l& V( f" _) Z
return 0; 4 H9 h: @( ~- s3 w$ Z$ V8 V" t
} & Q5 S& L6 ?7 K2 `  Z- \0 E& A- Q- b
</FONT>
5 O8 A+ |1 q: d) G: _1 [# F9 O</P>& {0 }7 X# x# v: f, G
<><FONT color=#ff0000>函数名: rand </FONT>- Q1 [% ]$ R0 y+ y$ D
功 能: 随机数发生器 5 n" ^: [. y8 t* ^3 I$ D
用 法: void rand(void); 5 a- c$ c  {* f* b3 F
程序例: </P>  M9 m1 j3 Z7 p# v
<><FONT color=#0000ff>#include <STDLIB.H>
$ P6 X/ m  i8 p6 }) ^, Q  Z#include <STDIO.H></FONT></P>
) s, H* k) f, L9 r$ |1 p! L4 \. l<><FONT color=#0000ff>int main(void) . D) R6 r2 p3 {% |. d( t
{
/ a: b- W1 J3 Z( p& z3 x4 {1 Tint i; </FONT></P># V3 N2 Z( y9 W% @
<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n");
4 t: c- i  b) n; yfor(i=0; i&lt;10; i++)   _' H2 [% F" x  m2 z$ w6 \- L
printf("%d\n", rand() % 100); $ }& s5 ]; p9 ]( n
return 0; + N2 Z6 A  S$ d- Y: B/ U
} . X5 v9 ^; m* ~) }% U7 J

0 m+ P$ `0 i4 q</FONT></P>
+ M5 a& J  b4 a$ }! t<><FONT color=#ff0000>函数名: randbrd </FONT>0 l9 @$ J% q: B- n) L% E
功 能: 随机块读
3 o0 a3 ?7 e0 Y; ]6 B, G# \& |) V用 法: int randbrd(struct fcb *fcbptr, int reccnt);
- ~+ e' {. P& O( P( X3 Z0 b程序例: </P>( K9 W, i" q( ]- i; ~
<><FONT color=#0000ff>#include <ROCESS.H>
; u4 R% u  t2 q$ q  j9 v! I# H#include <STRING.H>
3 L' c) o. P/ E* Y/ n5 k#include <STDIO.H>  r/ X# n7 G; ~5 j: H3 @
#include <DOS.H></FONT></P>
1 ?  ~4 [/ j: X9 {/ w% q3 L9 ~  M% H, r<><FONT color=#0000ff>int main(void) / X" I/ l3 J' R  A; K
{ : {1 I0 l: T  s& |6 q4 q
char far *save_dta; % d& |( v5 D) r" n* F& z/ Q! W
char line[80], buffer[256];
( Z- K: i+ Q$ T. S6 f; u" j/ a  wstruct fcb blk; 1 D$ Z3 n" ]7 y" {6 D
int i, result; </FONT></P>) j( o1 M8 F6 b5 X0 d
<><FONT color=#0000ff>/* get user input file name for dta */
% F- b. F8 [1 b1 w1 o8 z% Oprintf("Enter drive and file name (no path - i.e. a:file.dat)\n");
7 f- T' M6 l# [* ygets(line); </FONT></P>
* g5 O2 C+ n7 K* }6 D; B! y<><FONT color=#0000ff>/* put file name in fcb */
6 e5 C, P: U  u: V) Bif (!parsfnm(line, &amp;blk, 1))
8 K, g7 u. n/ C4 i6 o0 o{
( m: x  h* e$ Nprintf("Error in call to parsfnm\n");
  N2 k( h% L8 {7 l+ T$ J% ~8 Nexit(1); . w/ T8 C1 Q! o3 l8 P
}
7 S1 Q, |1 z  s$ r5 G* Z1 ^& h  t# Gprintf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
( e3 v4 t. z1 l+ O<><FONT color=#0000ff>/* open file with DOS FCB open file */
# \% B: D# s7 u: R+ ebdosptr(0x0F, &amp;blk, 0); </FONT></P>4 J3 f3 W/ Z; U1 ~
<P><FONT color=#0000ff>/* save old dta, and set new one */ ! @0 D; E9 D2 N. Y6 d: J7 Z
save_dta = getdta(); 4 D7 e& Q2 T) c! P/ M( C% A8 E
setdta(buffer); </FONT></P>
. \6 f9 e+ a) w/ P. n# d0 d<P><FONT color=#0000ff>/* set up info for the new dta */
. q, i2 [3 s! Y1 g( y, v' nblk.fcb_recsize = 128; $ x, i. W/ Z: N' |% U1 m
blk.fcb_random = 0L; " N3 _& K1 g$ ?8 T, u
result = randbrd(&amp;blk, 1); </FONT></P>6 k7 j7 _' m; x# |5 ^
<P><FONT color=#0000ff>/* check results from randbrd */ - Q, I  V" x* L( q
if (!result) " Y& C% Z+ q5 i( v: M
printf("Read OK\n\n"); 0 p) }  C" V8 o5 L3 N0 q1 L- @+ u
else 9 F# ~/ v  {/ T+ I
{
. V2 B. K$ c  Wperror("Error during read");
" V' B" N9 q* N8 p/ Hexit(1); ; s7 `4 d7 k+ B( R( J; P% U
} </FONT></P>/ i( j! O" j, ?& X1 f3 V( Q
<P><FONT color=#0000ff>/* read in data from the new dta */ + s; \6 f0 F# b7 o$ c
printf("The first 128 characters are:\n"); 3 q9 G! c2 c+ N/ |9 F/ m2 S
for (i=0; i&lt;128; i++)   h; I$ u# a) P0 z4 h6 M& P- q4 S
putchar(buffer); </FONT></P>1 Y8 I) e/ o! a) s5 U  w
<P><FONT color=#0000ff>/* restore previous dta */   q3 T5 S- Z- }8 j- w* |" x
setdta(save_dta); </FONT></P>
: g% S8 U) `' l$ E<P><FONT color=#0000ff>return 0;
3 f7 q- Q' g2 _/ P} </FONT>& e/ N# M4 Y. Z% j  E2 S
</P>
7 s! b+ Q( J; W/ Y2 H5 V; S<P><FONT color=#ff0000>函数名: randbwr </FONT>
  L9 p2 I7 @4 l2 h$ W6 t功 能: 随机块写   v- I; k/ e$ q4 b8 J1 |
用 法: int randbwr(struct fcp *fcbptr, int reccnt); 7 u0 n" c& m* t7 h+ B( _
程序例: </P>& f! O) o) _3 T! W; R! e$ }
<P><FONT color=#0000ff>#include <PROCESS.H>
% [% K" I, f6 S1 `7 X( ?' G2 w#include <STRING.H>
9 F4 ~" h: S8 v: N/ I#include <STDIO.H>
3 h" k/ `1 F. U#include <DOS.H></FONT></P>
5 D  B8 y! H, C" D9 \<P><FONT color=#0000ff>int main(void)
! k; S* O$ ]& S{
" L. h5 u" [& M8 |3 ~char far *save_dta; . I) D: }, w/ Y6 z! ]+ j$ Z
char line[80];
1 y# W, R) @" Z+ Q3 fchar buffer[256] = "RANDBWR test!"; ) \& t. j, v+ p- Q
struct fcb blk; 0 _0 m6 N) F" V, V
int result; </FONT></P>5 X7 K: t2 ?8 E. Z# F+ V8 I
<P><FONT color=#0000ff>/* get new file name from user */ % o- \, t; d# N6 _# T3 E
printf("Enter a file name to create (no path - ie. a:file.dat\n");
7 _! j& v: t3 R; v. N% i5 q2 f- r. W6 n: zgets(line); </FONT></P>2 w% I/ Q+ D1 H7 b* h
<P><FONT color=#0000ff>/* parse the new file name to the dta */ ' T: h- l  t/ V* x/ n% v
parsfnm(line,&amp;blk,1);
8 A3 H8 M2 `  s$ r7 h% S, dprintf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
6 }6 F* C! h' l& S+ q1 x( G<P><FONT color=#0000ff>/* request DOS services to create file */ $ @% b8 f* X( K" J8 w
if (bdosptr(0x16, &amp;blk, 0) == -1) $ \- }( u) F( i% c. r' w3 w: A* k
{ : F2 x' Y: L0 f7 u! X$ d
perror("Error creating file"); 1 M6 U) q4 T) U: `9 Y' r* r. G
exit(1);
3 D, Y6 _! S/ y6 M} </FONT></P>
2 P) v( J" h+ s& \<P><FONT color=#0000ff>/* save old dta and set new dta */
, o2 m- J  ]' g$ R6 Ysave_dta = getdta(); 3 J# N  q' m* Q( r2 n9 E
setdta(buffer); </FONT></P>
5 b6 _& y/ e+ q/ _2 ?<P><FONT color=#0000ff>/* write new records */ ! \0 t, A1 ~1 }+ r; C/ y
blk.fcb_recsize = 256;
9 D! V- H! `# [( C) Wblk.fcb_random = 0L; 3 A$ g( v  [( I
result = randbwr(&amp;blk, 1); </FONT></P>7 w, y! b- f  m
<P><FONT color=#0000ff>if (!result)
; z8 _! v/ G4 r, b" H8 gprintf("Write OK\n"); - y4 L3 j1 A7 g; j  o
else
' J/ A. M& e1 T5 N/ D, ^{ 6 {' @$ ]7 ~, |) a# q1 O4 t: L
perror("Disk error"); , g2 f( U: h" y. ^8 D
exit(1); , j  e0 y) f  R" b& Z
} </FONT></P>; R* y! L3 B% v
<P><FONT color=#0000ff>/* request DOS services to close the file */
) ]6 n6 V% {6 F3 R+ dif (bdosptr(0x10, &amp;blk, 0) == -1) . u7 u; ]- I, d( J4 S( [( j/ @# I
{
9 E& p4 {, j7 z" Uperror("Error closing file");
) v* E! M( h( w0 m* h) Sexit(1);
" E. A! X" a$ |0 r} </FONT></P>" z8 Q) [! Z& O# |3 r
<P><FONT color=#0000ff>/* reset the old dta */
. d+ T( |5 C3 S7 V1 u& f7 p1 ?setdta(save_dta); </FONT></P>. [+ W- _+ l$ E( W, M5 Z& {
<P><FONT color=#0000ff>return 0; " r2 a3 M% |( ?. t7 A+ {
} 5 J" T0 I( J, \) u; j, A# M
</FONT>
3 z/ \( M( a$ }- I</P>
& P8 |  C0 h' p7 l' J+ }<P><FONT color=#ff0000>函数名: random </FONT>- f% Y  K9 y$ S( ~8 u& Q( i" N7 [
功 能: 随机数发生器 7 p1 d0 c, m7 G% @8 p
用 法: int random(int num);
- M" G% h7 k4 r9 h) S, }程序例: </P>6 O. W/ |2 v; W! ]( @0 M3 ~
<P><FONT color=#0000ff>#include <STDLIB.H>
  i" T; m+ B2 s) N( q7 {; _) W6 m. s9 Z#include <STDIO.H>' d: x- B& r- J8 {! _! L
#include <TIME.H></FONT></P>
) m( M' K, B( r<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
1 h( \7 [8 d/ o1 d- iint main(void) $ e$ o. v; k# M% _' l' l! n+ ?* r
{
3 y, S2 ?% R, J- A. Zrandomize();
7 y0 W4 n' f' @printf("Random number in the 0-99 range: %d\n", random (100));
7 _% Y, |- O7 b7 K! ^return 0; % h6 q  O# {% \0 V2 d0 x/ |
} 5 D. i( q( Y# o' x. g; n2 k
</FONT>
) o8 z# J5 l) a1 d& U</P>
, y7 l+ Y) x: C# F<P><FONT color=#ff0000>函数名: randomize </FONT>
- Q( d. ^7 W1 `. b* o9 e. j4 _! n功 能: 初始化随机数发生器 / f+ l# _4 i/ }% A; N$ i& A* a
用 法: void randomize(void); + B2 t+ D6 \$ A
程序例: </P>
* `6 V% b* {& F3 S4 j' ~<P><FONT color=#0000ff>#include <STDLIB.H>
/ Y3 H- Q* f9 H$ N" E$ b! z1 ~#include <STDIO.H>1 Q) W- m1 H: F  w% a! t# C& z
#include <TIME.H></FONT></P>8 H* z' V0 S  R; j
<P><FONT color=#0000ff>int main(void) 7 u1 [  ^' Y/ w! Q- z5 @# q
{
$ P; y8 ?3 K* lint i; </FONT></P>, @9 G9 w0 ]5 {6 N, I/ u$ [* r
<P><FONT color=#0000ff>randomize();
" D% ~0 f( O8 D, ^: Y5 I7 Qprintf("Ten random numbers from 0 to 99\n\n"); 2 c; M3 P2 o3 F$ d
for(i=0; i&lt;10; i++)
; S! a) i; i$ i( Kprintf("%d\n", rand() % 100);
2 u8 A4 L% N, }- K$ i! p% Xreturn 0;
# w7 N4 c' O- q' \- P7 x} ! M: K( [5 x0 ^4 W: A+ S
</FONT>
: e' F- G8 h* i/ P' ~0 U% ^</P>
5 D, G/ N! B! g$ X<P><FONT color=#ff0000>函数名: read </FONT>
9 M- d* C0 _+ p& [6 G. v5 L功 能: 从文件中读
- y5 P: w7 b7 a/ a' F' I用 法: int read(int handle, void *buf, int nbyte);
2 k( i2 e. ]# m程序例: </P>
% l6 O4 }& P2 i! w<P><FONT color=#0000ff>#include <STDIO.H>
" h% U: W+ Z% z' W1 W  A0 c' l#include <IO.H>$ h8 M9 s( D( }' }4 \
#include <ALLOC.H>
3 G6 d- T3 A6 O- Y3 @" M#include <FCNTL.H>
; `$ B4 e' j3 [# F& h% y5 Y#include <PROCESS.H>
7 {& \. d1 ~7 N; J% h#include <SYS\STAT.H></FONT></P># M) o, b+ r/ \6 [8 V
<P><FONT color=#0000ff>int main(void)
# p5 }3 k2 Z' L9 e{ 6 M* j6 B9 X: d& U
void *buf; & d$ A$ f( [5 m2 s" C3 G8 i
int handle, bytes; </FONT></P>, m4 a7 M# ]3 o6 L4 _2 S
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
1 y/ o' d# n' s' W<P><FONT color=#0000ff>/* ! h, t/ F8 K4 R& V9 O
Looks for a file in the current directory named TEST.$$$ and attempts + |* {- z" W/ Z$ l$ z. ]
to read 10 bytes from it. To use this example you should create the ) Q( b: ~, J8 ?4 f' h( L
file TEST.$$$
, {1 U# Y" J9 N5 {, n( h" M*/
' d6 M8 z" s$ [, N. W0 sif ((handle = / A( Z" b# L. ^/ Z/ y/ r
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1) 5 G$ M7 [7 b# ^* @& o
{
' C* w7 {: @/ Q6 [( ^& P' E: Jprintf("Error Opening File\n"); 9 z2 I, e$ O, p" j* U" ^
exit(1); 0 A) e0 }0 `; h0 ~
} </FONT></P>
7 g. w: x0 e! p/ v7 {1 f  t<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
0 O; {$ F% J1 ?" ?4 S( ~( Sprintf("Read Failed.\n");
9 P6 j) V: p. qexit(1); 7 s& ^: o1 o# f0 U) L. d
} " _) G3 n- p5 l. p  Z, j
else {
8 Q2 i# j+ A3 [3 O( u" Rprintf("Read: %d bytes read.\n", bytes);
6 U: R* X4 r8 P3 p$ L}
% j7 e& L7 A; `0 P+ xreturn 0; + @% E4 w  g  @$ X, F+ m4 R
} </FONT>
- ?8 i" N3 e+ E0 {- j5 W0 i6 ?+ Y1 m; g6 B' a7 ]& p% Q
</P>
8 Z. u9 d0 v3 o, T" V' |/ s4 U1 _<P><FONT color=#ff0000>函数名: realloc </FONT>
; t1 h- ]% |& X4 j% q功 能: 重新分配主存
  S. B; l. Z4 {* l9 a; U1 f用 法: void *realloc(void *ptr, unsigned newsize);
5 E) c! j  D; X程序例: </P>6 [; h* o% C, j4 V
<P><FONT color=#0000ff>#include <STDIO.H>
- y( X# l3 F5 G% l0 x' v#include <ALLOC.H>
# [, |: i# u' t$ b6 R# r* O#include <STRING.H></FONT></P>8 S% }* y/ q: ^3 b
<P><FONT color=#0000ff>int main(void) ! K- K+ j' U$ e8 c
{ 5 @7 A' S5 {" D9 L1 \' ~' k
char *str; </FONT></P>2 \; g) ~* S+ j
<P><FONT color=#0000ff>/* allocate memory for string */
' h) ]: u. @' U/ [% Z, }str = malloc(10); </FONT></P>
* E( c6 i9 R- x3 z6 q<P><FONT color=#0000ff>/* copy "Hello" into string */ 3 V  t" f: k1 x0 T" h
strcpy(str, "Hello"); </FONT></P>
' h& A8 }2 }/ `: V! r5 j" v( B<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); , \! ?% z1 m5 A+ h  T
str = realloc(str, 20);
( Q  \2 d) P, yprintf("String is %s\n New address is %p\n", str, str); </FONT></P>
1 h0 X% |. A4 U  @+ i& W<P><FONT color=#0000ff>/* free memory */
4 S/ D9 ^: @; T$ j) V8 Gfree(str); </FONT></P>4 n6 P+ |( v  D% {+ }
<P><FONT color=#0000ff>return 0; : K2 b! c* n* B% o! F1 o
}
( G7 F5 Y, J6 v8 F' k1 M</FONT>- L/ z; H% c; [, {8 W  ]
</P>9 e: O7 p; E8 c* Z1 t' a
<P><FONT color=#ff0000>函数名: rectangle </FONT>1 Z- b9 H) _$ R# N8 z: m' k
功 能: 画一个矩形
: B+ v8 m3 t, r2 @; O用 法: void far rectangle(int left, int top, int right, int bottom);
  q0 S5 r9 M; s3 V3 m程序例: </P>4 O( O* I- J* t# `
<P><FONT color=#0000ff>#include <GRAPHICS.H>  K# s3 d& }; _5 h. |8 y: Y4 g
#include <STDLIB.H>
$ V& K9 v. i* Z) t5 a#include <STDIO.H>* j! b) j' Z# S' o; x4 v
#include <CONIO.H></FONT></P>2 G* q4 r( Y$ |2 Q
<P><FONT color=#0000ff>int main(void) 9 B' Y* p1 U& {6 `
{ 5 x* C% w8 d: x; s* }2 F6 w3 d
/* request auto detection */ / R6 k  d( w# c6 _) ~$ z8 S
int gdriver = DETECT, gmode, errorcode;
! N. d8 H/ |: E+ O  Kint left, top, right, bottom; </FONT></P>" o, ^0 [) s6 W9 i
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 ^; {  p- _1 D6 R
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P># r6 d( u& Q9 B! R
<P><FONT color=#0000ff>/* read result of initialization */ 0 T$ P) U, n1 p' x/ n5 j; \
errorcode = graphresult(); 3 m  k$ M# [4 z0 F) z3 X+ ^  H* U
if (errorcode != grOk) /* an error occurred */
+ R- l: D: c) o{ , a) S' X3 U& K& \& d# Q0 L
printf("Graphics error: %s\n", grapherrormsg(errorcode));   S  n9 e& z0 h& R+ L
printf("Press any key to halt:");
+ U9 n+ D/ |* c2 V+ v' N, kgetch();
: u6 w3 b: ]3 J' M  @: V  J3 @exit(1); /* terminate with an error code */
% j# E2 F  A8 Z2 X3 z  ^} </FONT></P>
  Y2 g4 ~% r7 s<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; % k7 S5 C* Z2 O4 X, Y! B
top = getmaxy() / 2 - 50; 2 [; i4 V. j6 I' x/ c
right = getmaxx() / 2 + 50; 6 ~* ]& k0 P# \+ m' a
bottom = getmaxy() / 2 + 50; </FONT></P>
% d3 J: Q: t& E7 A, H, T1 {1 A<P><FONT color=#0000ff>/* draw a rectangle */
; M: B* ?0 i7 L4 l/ R+ _rectangle(left,top,right,bottom); </FONT></P>* Z" q! b4 @5 T: Z
<P><FONT color=#0000ff>/* clean up */
* A# q  u: m  R. ]7 Ogetch();   y" f8 ~' p7 J4 T0 p/ ]
closegraph();
0 p+ W7 j) N& l* h4 C% p8 Y; ^return 0; ' D2 p, F7 q5 a6 [4 v
}
. J7 R' e; A. v) ^7 e0 ]</FONT>
: b8 ?/ ^# V" {/ H7 p5 U' I+ M</P>: M! H& p& M$ w  m
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>
; }; C6 Y% i; A8 C1 @功 能: 登录已连接进来的图形驱动程序代码
: z; C; g/ H9 K' ]. J/ C* `用 法: int registerbgidriver(void(*driver)(void)); 5 ]  k! v1 v  u
程序例: </P># W# q  C; }0 s  `: q
<P><FONT color=#0000ff>#include <GRAPHICS.H>
. j' Z" t1 @4 s. b#include <STDLIB.H>' O5 j, A# n- G) \8 D
#include <STDIO.H>
" o# H: Y& p6 h& c' H#include <CONIO.H></FONT></P>* ?! i* O9 s+ @/ g% ?
<P><FONT color=#0000ff>int main(void)
5 I& l& t; L3 R" ]: ]0 B2 ^{
" }' Z9 f2 B6 e6 O/ o4 J: i' A/* request auto detection */ % w/ x2 H( U% R6 B* p
int gdriver = DETECT, gmode, errorcode; </FONT></P>
' A5 E' ]' W% i# w$ h% r8 m9 o<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
, a3 D% P" Z8 X2 G. H: werrorcode = registerbgidriver(EGAVGA_driver); </FONT></P>
- m) R3 y" z, h2 x8 S8 c$ T- o<P><FONT color=#0000ff>/* report any registration errors */
9 X, R' g3 i/ A: c+ R: @if (errorcode &lt; 0) ( P+ h" D. v+ S+ u
{ 7 s$ G9 h) j& X+ O9 z, S; A
printf("Graphics error: %s\n", grapherrormsg(errorcode));
  O+ G9 G; h3 pprintf("Press any key to halt:"); ! X$ b  l) v' {  y
getch(); # \) \1 D  z! h% m
exit(1); /* terminate with an error code */ % B: r( ~! `+ l# r4 u& a6 b
} </FONT></P>0 |" r2 z0 [$ F# a9 X, K' }
<P><FONT color=#0000ff>/* initialize graphics and local variables */
: V% m! \" {7 M7 B2 `* ~! j" a4 Qinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>6 W9 p9 h! {0 U7 D2 h8 z& x6 Y
<P><FONT color=#0000ff>/* read result of initialization */
2 ]! @; i/ y- [" s, e( yerrorcode = graphresult();
& r/ r1 B- l9 O2 ]3 i( u% g; a! hif (errorcode != grOk) /* an error occurred */
2 n2 F/ f+ j: I  o. {{ 8 N4 E! j8 c: C% t6 f% e
printf("Graphics error: %s\n", grapherrormsg(errorcode));
, x; V; s7 Z7 `: N7 P8 }# qprintf("Press any key to halt:"); 6 ]( ], A2 M; x, g) R
getch();
& H/ w$ o5 }- O7 I( h6 A% ~exit(1); /* terminate with an error code */
) J' ~, n2 C! `+ H} </FONT></P>
4 |! e8 v- K2 V$ ^! c; B, D6 Q<P><FONT color=#0000ff>/* draw a line */
1 \! L2 Z. G4 Eline(0, 0, getmaxx(), getmaxy()); </FONT></P>
! R! g, n8 s" J& t  o$ e<P><FONT color=#0000ff>/* clean up */
9 ^1 W' C- [+ z) g9 N, B! u) ^( W; R- xgetch();
+ P+ _; J3 O- b( R+ jclosegraph(); 3 P% F+ }, F4 B( K* O* ]. R
return 0;
* @# G5 A  }, w4 `, L} 6 o3 j  V9 H1 }; p9 Y
</FONT>
+ m$ J4 z2 b8 S& Q; r. V: S</P>
* q- Q) Q$ H3 p0 ?' ~$ O# P<P><FONT color=#ff0000>函数名: remove </FONT>
0 c4 U7 ?1 ~, s- J& v: |; n功 能: 删除一个文件
1 Y  g- V; ?) a3 [用 法: int remove(char *filename);
% T+ @( p# m# i( \  `, V程序例: </P>% L6 |# I% h5 G) N; [
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
( _% M6 P/ O. K8 ~4 s: S<P><FONT color=#0000ff>int main(void)
/ @9 b) _: s# t2 `; |{ ' ?; N4 V( d" y* Y/ g" O* c$ f
char file[80]; </FONT></P>
! [4 Z2 _$ X4 s  k/ L/ r<P><FONT color=#0000ff>/* prompt for file name to delete */
! {6 d5 ?4 [+ M4 K% e! Dprintf("File to delete: "); : f$ i( h( g& t
gets(file); </FONT></P>: B# y9 W- G% p2 u; X  x9 `( L
<P><FONT color=#0000ff>/* delete the file */ , G- I2 U# V& d1 n; F  `
if (remove(file) == 0) 5 i$ t8 i6 C. ]0 s$ p8 z4 H7 i3 T
printf("Removed %s.\n",file);
0 q. X3 G# m* S3 N0 x" L2 helse
$ z# f" F* S& Z# rperror("remove"); </FONT></P>7 O9 C$ U9 m2 M: C+ l) U/ I
<P><FONT color=#0000ff>return 0;
7 [3 N" L) T" {( Q/ F" _} 1 B* t2 b, H2 N) f- i1 E
2 O+ w/ A' X/ s
</FONT></P>1 E/ J2 b8 w; K7 G
<P><FONT color=#ff0000>函数名: rename </FONT>
2 v+ p2 P: y/ l* A, }功 能: 重命名文件 # [; I$ a+ s. k/ f* E
用 法: int rename(char *oldname, char *newname); 5 E# ^" L( Q9 c( U4 H
程序例: </P>. r  u; H, G% u$ f5 y) y& h! a$ N; T
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
& f/ V; W) P( k/ O$ }<P><FONT color=#0000ff>int main(void) * s1 U  _; l& Q
{
+ n) r+ _4 ]) D0 g" L( \9 g" y3 \( ]char oldname[80], newname[80]; </FONT></P>  j" a+ u. I/ E. f( i) [9 u7 r( P
<P><FONT color=#0000ff>/* prompt for file to rename and new name */
. m' R6 S, F4 p2 x. h9 Q4 {* p& x  vprintf("File to rename: "); : O3 N  O/ f: P. U' o3 V
gets(oldname);
* _/ h) f/ _4 v% [printf("New name: ");   F& J+ [1 F  k/ U
gets(newname); </FONT></P>( {* U2 J! r0 s' y, q) p
<P><FONT color=#0000ff>/* Rename the file */
1 E) N! V, K. W& I5 T* x# eif (rename(oldname, newname) == 0) 5 u9 ]. [& Z: ?' g9 a$ z8 {  {
printf("Renamed %s to %s.\n", oldname, newname);
( C1 X. n( l9 d$ _8 u% nelse
# n* |1 s. n% G- _perror("rename"); </FONT></P>' [" k8 `: N( @% }/ N% |& U) q
<P><FONT color=#0000ff>return 0;
' X/ V4 A* u+ v$ r+ B, N+ Z5 S}
) [8 E. ~9 u7 ?- m$ M3 v2 _
) I7 X& f, S5 F: q5 t2 f8 l+ N</FONT></P>
1 M# |$ m+ B# g' g<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>7 `$ D, ?$ i! B4 f8 F4 z' T2 j
功 能: 将屏幕模式恢复为先前的imitgraph设置 : L! J' [0 d- H6 R. g
用 法: void far restorecrtmode(void);
6 a. C  ^- f$ b* V5 C: E9 y& B程序例: </P>: W+ |" ?1 W- u1 f$ Z
<P><FONT color=#0000ff>#include <GRAPHICS.H>
% k" h( F( _; v: r) n9 P1 D2 l#include <STDLIB.H>
$ ]$ V1 L6 }3 y6 n#include <STDIO.H>
" P# Z: T& k* P# b$ w+ w#include <CONIO.H></FONT></P>
- P* b* |3 R9 J/ N% A  c* r: L<P><FONT color=#0000ff>int main(void) 1 I5 |! w5 U3 c* B5 y/ R+ @
{ ' X0 i  B; T5 i
/* request auto detection */
' q4 h6 @1 F5 D4 J5 _int gdriver = DETECT, gmode, errorcode;
( n$ X7 w4 a+ Q( S5 hint x, y; </FONT></P>
# \9 u6 Z& \$ ]. C; Q<P><FONT color=#0000ff>/* initialize graphics and local variables */ 9 v6 [( K2 Q% g5 O! p" Y& J0 k
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
6 a9 S# v5 M" q0 T' q<P><FONT color=#0000ff>/* read result of initialization */   B/ v: t0 u8 p+ f7 Q0 I8 ~
errorcode = graphresult(); $ J4 _: A! B5 h
if (errorcode != grOk) /* an error occurred */
5 |4 P. y8 g: e& G1 s8 m% U{ $ ~3 f' W$ ~6 h. w- X) @
printf("Graphics error: %s\n", grapherrormsg(errorcode)); # V* @" K3 ~8 ?2 j5 H. ]
printf("Press any key to halt:"); 3 e6 k) V* E& [: @6 I
getch(); 4 d, F9 j6 u& f& q6 z. U) a
exit(1); /* terminate with an error code */
$ s3 _6 r4 z$ L4 K} </FONT></P>% d8 f. o8 i: L# a
<P><FONT color=#0000ff>x = getmaxx() / 2; 5 g0 X/ V" L5 h4 U5 u
y = getmaxy() / 2; </FONT></P>
7 A# N2 g  I! `( F$ o* y  U<P><FONT color=#0000ff>/* output a message */ 6 @/ z- [) m! {! }
settextjustify(CENTER_TEXT, CENTER_TEXT); 2 A  }. Z: O( i7 G$ Z6 m
outtextxy(x, y, "Press any key to exit graphics:");
7 N2 Z$ v8 W! h8 Agetch(); </FONT></P>
4 \& o* _( j) }<P><FONT color=#0000ff>/* restore system to text mode */
5 x* }9 v" N' |$ @8 Srestorecrtmode(); , j: H$ i/ \0 Z2 ~0 x3 V/ t
printf("We're now in text mode.\n"); ( b  g' q. A+ s9 H, h! m
printf("Press any key to return to graphics mode:"); 8 R! Q& [; D+ D* u' B
getch(); </FONT></P>6 L# M- R" b- _# I) ?6 n
<P><FONT color=#0000ff>/* return to graphics mode */ $ F3 ^: b7 `" q; C, e; u: |
setgraphmode(getgraphmode()); </FONT></P>3 E; p( w, I) L1 Z7 o/ H
<P><FONT color=#0000ff>/* output a message */ 2 b( ]( P. S# s% w5 \# i
settextjustify(CENTER_TEXT, CENTER_TEXT);
$ _; g, n% V6 b. xouttextxy(x, y, "We're back in graphics mode.");
$ J$ W5 z* Z% Y- _outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>- E+ J% U) n+ [7 Q# I* a$ u: h
<P><FONT color=#0000ff>/* clean up */
9 u6 A& F, O$ S) ]+ n- _getch();
- w2 e. a% v( ]/ v( ]closegraph();
% ?# |+ @( ^8 x. Kreturn 0;
* {' f# }" R# y4 f& ]}
0 l4 A7 c, g, ~  J5 j% g( k</FONT>
( Z; S  H/ d% B5 x" g</P>  m4 u+ P, {/ z  A" L& G  z
<P><FONT color=#ff0000>函数名: rewind </FONT>: s0 a" k' w6 F
功 能: 将文件指针重新指向一个流的开头
6 _' k" F8 o/ d用 法: int rewind(FILE *stream);
9 P% T, H' ?1 Q4 S0 _3 H7 W程序例: </P>) \" S- x' Y' t' A4 |' U, z& J
<P><FONT color=#0000ff>#include <STDIO.H>
2 K- q* F) b7 A: F; [' d#include <DIR.H></FONT></P>
7 O. T6 ~) j& c! P<P><FONT color=#0000ff>int main(void)
1 l+ L, C$ o; G/ k{
. @* g& ?+ @+ \$ {7 I' H! BFILE *fp; + p3 M( P6 @: V0 W2 x
char *fname = "TXXXXXX", *newname, first; </FONT></P>
4 G! u4 t8 e% f/ D( x8 W3 i' }<P><FONT color=#0000ff>newname = mktemp(fname);
/ f6 ~5 r8 \6 V7 {3 h- r! l7 }fp = fopen(newname,"w+"); 0 i1 l9 t2 W9 k2 I8 b1 A0 X* i
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
9 T' y/ f2 t1 J5 @: hrewind(fp); 8 M3 \" ?1 }. F& x( J/ @
fscanf(fp,"%c",&amp;first);
9 _  P+ [8 h: F3 n, kprintf("The first character is: %c\n",first); , ]+ v2 m1 G3 O
fclose(fp);
$ ~3 ~4 q! f# \( L& R1 _remove(newname); </FONT></P>
+ U5 ^4 C' Z7 S: C<P><FONT color=#0000ff>return 0;
6 A; g% _/ f  V; x. o& G) m$ V2 j$ u}
3 z, ?! E) q. y6 R" u! f( A+ Y</FONT>
5 K: a+ a4 V( l, i</P>3 _3 i3 ~$ F- j; _' g# ?( B/ L# f
<P><FONT color=#ff0000>函数名: rmdir </FONT>
$ P6 K+ C9 {8 ?  Y/ z8 W功 能: 删除DOS文件目录
8 D8 {9 u% ^2 s$ G/ E; [用 法: int rmdir(char *stream);
& O4 y. U# {5 @3 \+ s3 F* ^) V程序例: </P>! z9 N% l: _; L0 _$ T$ I, f! }
<P><FONT color=#0000ff>#include <STDIO.H>
3 M0 k6 L! m6 l, ]#include <CONIO.H>
9 S( Z$ s8 m. q- K$ Y9 ^3 p. r#include <PROCESS.H>) Z9 {4 @; m4 t( o: M* h. C
#include <DIR.H></FONT></P>
7 f, x- a" @* }/ c8 l3 m) u, T$ T/ `<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>2 d, t2 {! j) c( l8 Q- w
<P><FONT color=#0000ff>int main(void) ; `( W4 ^* M, p! w+ F. e# W, T3 _
{ 5 Q1 S+ l  A% P" @$ q
int stat; </FONT></P>
7 D* [9 ^6 g- i6 I<P><FONT color=#0000ff>stat = mkdir(DIRNAME); 6 z  z/ z% `4 u" j$ T' S
if (!stat)
! E" C  s2 k: {& r# Tprintf("Directory created\n");
) I9 g( |+ U- l  n, Gelse
; R- m1 B/ G( A* \8 {! C) F{ - L9 t3 n! n) q2 y; m1 Y7 x3 _" P
printf("Unable to create directory\n");
7 ^9 P0 W. H" kexit(1); * z7 X' J" P3 @7 q
} </FONT></P>
* s$ N2 Q& w; k' G, m) [<P><FONT color=#0000ff>getch();
$ S2 b# l1 z1 U  k% U* Bsystem("dir/p"); 3 ^# c7 u$ T+ u! D' X5 X7 ^! g0 [
getch(); </FONT></P>
1 g$ {: A7 |1 H" D. h4 e9 U8 l<P><FONT color=#0000ff>stat = rmdir(DIRNAME);
3 ^9 }& C1 ?+ J8 D  X4 l+ Pif (!stat) $ L2 e, q: M) R: V* N
printf("\nDirectory deleted\n"); $ h; Z' h3 Y9 w9 b
else - C, n2 W. F' V0 a. [( }
{ . d0 Y2 |+ i0 I
perror("\nUnable to delete directory\n"); - U1 V8 d, E7 B4 N# w1 D) A: n7 d4 J
exit(1); 2 ]( @, G  q) e- ^) I1 }
} </FONT></P>
7 a; Y# y) O! y$ h0 `, |' L<P><FONT color=#0000ff>return 0; ! \( k2 o4 q1 P  W/ f; L% x! }
} </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 13:31 , Processed in 0.438467 second(s), 57 queries .

回顶部