QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
- X# t7 f# B# {8 d% }4 X6 B功 能: 使用快速排序例程进行排序
* y0 ?1 u* h' Y  `  w; `用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); . s) g# r: |) f9 o
程序例: </P>2 I. C1 x8 ~. ?( Y
<><FONT color=#0000ff>#include <STDIO.H>
8 ~  |4 Y1 o; m' V#include <STDLIB.H>
6 S" }  P: }; K+ ?#include <STRING.H></FONT></P>
; r  [0 A' l# g) o. t5 v) y<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
" Z% M4 E: s5 D; c<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; ! s2 Z8 Y$ D  b( y# q: u
</FONT></P>
, j- C8 w9 D) Z: ~: I2 d<><FONT color=#0000ff>int main(void) - S  c8 G" X! Z0 ?* ~
{   ~' c" ]- H  B. S
int x; </FONT></P>- f/ {, t( [: G$ x8 z/ I0 W2 y
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
3 J8 n# P, S  Z& Afor (x = 0; x &lt; 5; x++) ! H1 r, E/ b: V2 ^& i3 d: w  K
printf("%s\n", list[x]); 7 \- M( v# K$ q, G8 d) S- J
return 0;
7 x9 P# ^8 c( q% N% g} </FONT></P>
$ B/ R* i( M6 a0 T7 M& O<><FONT color=#0000ff>int sort_function( const void *a, const void *b) ( [: g% q# L+ B
{ # v) R' u1 P; ^+ ?* T3 y0 p6 P
return( strcmp(a,b) );
7 t* F1 B) o1 D# W9 m} </FONT>
* X% L) X; M/ x# a; u</P>
' G4 A8 J8 H' V6 z; i# P5 j; O* X1 x9 j0 I4 u

1 S( D. g& H  A5 v0 f/ b<p>4 q9 b* O+ y: [
<><FONT color=#ff0000>函数名: qsort </FONT>
; j6 {* s4 ^! L功 能: 使用快速排序例程进行排序
5 P( I( b2 b6 |* u/ i! V( z用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); ! t$ F8 [% m, q: }& C2 g, L' F
程序例: </P>
) K0 ]; z5 @2 q- S7 Y  W<><FONT color=#0000ff>#include <STDIO.H>
& {& N6 Q& y3 [6 |+ _+ ~# h# l#include <STDLIB.H>
* z) Z2 m  C1 ?. e; R; y#include <STRING.H></FONT></P>) }9 N* J! N& h( h8 p5 r
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
( o( J( n) R- O# D/ ~<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
4 ^" j4 R* N& U9 ]0 y/ w</FONT></P>
1 G1 m5 s) s6 M1 N" e<><FONT color=#0000ff>int main(void) + n$ h$ n4 {6 ]
{ % S" V6 [$ ^0 G& ]
int x; </FONT></P>2 \& E. c' N; B! r. |2 T' {6 A
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
$ m0 J8 a' h: _; Y- ~for (x = 0; x &lt; 5; x++)
2 ~( o- r( s2 Y/ y; iprintf("%s\n", list[x]);
' y: d& y* [+ ireturn 0; " [& x" k" i( N/ D
} </FONT></P>$ F; \# D0 W% z
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
. p; p, P" M+ ?1 t4 Q6 t{
& k- z% J# z+ J0 Preturn( strcmp(a,b) );
1 @3 z0 ^1 t$ @6 d  u}</FONT></P>
; `0 i* K8 B5 J3 H3 Q7 R<><FONT color=#0000ff>
& K# p2 g. O+ F7 F</FONT># _) W8 C) j" t+ I# D
</P>, Y8 x7 @- R5 ~' W; |8 l. i* [
<><FONT color=#ff0000>函数名: raise </FONT>! E4 s: v! G) M7 D% y7 C" K1 r
功 能: 向正在执行的程序发送一个信号
) [' q$ d3 ?' p7 ]' H1 M  N, J用 法: int raise(int sig); , O3 D& f: R4 O) |5 Z+ x, R
程序例: </P>( {8 N. |$ a1 P! E! {
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
$ y) j0 m/ [- l<><FONT color=#0000ff>int main(void)
7 P1 ^/ \( n$ @2 S; g4 E  v0 E{
$ v  \0 e: X0 V* d8 \int a, b; </FONT></P>
6 l( `& X3 @; H<><FONT color=#0000ff>a = 10; , S# N, L4 s% J" X- H/ ~& m. f7 t
b = 0; 5 V1 ^5 B: C6 S6 B1 @& ~4 a* B  B# d" P
if (b == 0)
) `: F, x2 b$ y( H/* preempt divide by zero error */
; p8 p# V( U# w) N- B# sraise(SIGFPE);
/ s* C# Y% ^7 N4 j, B, ^a = a / b; / F: P# b% e* h, ], ~9 g" F
return 0; ! ~- J0 i4 u" `8 [/ V3 A
}
- \3 m; y+ m: @</FONT>
( K/ k/ }3 e/ q</P>
' i, Q* b2 N$ {) N$ H<><FONT color=#ff0000>函数名: rand </FONT>( p+ U2 ]' x9 k6 O) c
功 能: 随机数发生器 ' s/ d8 u. B9 Z  ^/ M7 J' n
用 法: void rand(void);
) C. K- v% e4 J/ m+ o, U- B程序例: </P>5 f) {0 K0 E6 c& t" U
<><FONT color=#0000ff>#include <STDLIB.H>
6 c1 z. n4 \+ B' r0 c, O#include <STDIO.H></FONT></P>
6 ^& l- P' E9 G% e# J<><FONT color=#0000ff>int main(void)
0 G2 V7 V0 F$ x$ W; a4 Y{
4 h. z7 L6 \: Q! ?6 E/ w" sint i; </FONT></P>; A' y) N8 Q: a# Q+ q" I
<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n");
. S0 m2 J$ e- D% W: _- afor(i=0; i&lt;10; i++)
6 R  e% K: ~) h* Q) ?7 nprintf("%d\n", rand() % 100); " {1 ~( |  a) T
return 0; 2 K9 O) p3 |7 {- G7 h  ?
} - J1 v; Q/ O5 n- l9 m. x, R

- G7 C# \! V# W$ i+ |" w</FONT></P>" R6 O- E3 P, I
<><FONT color=#ff0000>函数名: randbrd </FONT>
% M7 V2 V! [6 e' f+ B) S功 能: 随机块读
+ O1 T' ^* l9 y8 H, U用 法: int randbrd(struct fcb *fcbptr, int reccnt); 8 m, }; {3 z( H% F
程序例: </P>
  U$ s. y& t8 Y$ \<><FONT color=#0000ff>#include <ROCESS.H>
4 I  |# |# e7 P/ n2 p! x#include <STRING.H>0 p4 T3 Y) I0 p; X( a. X- l
#include <STDIO.H>
7 `* R2 h4 ^4 @  F#include <DOS.H></FONT></P>
3 {" _9 W( R& v9 T5 _8 h( g, a<><FONT color=#0000ff>int main(void)
; f( ]# Y. w" v5 N$ q; t! k{ ( c( l- [- s7 L! v
char far *save_dta;
( n' c- w- N3 q! P8 `: p. rchar line[80], buffer[256];
" R) X8 j# X9 w# R2 d1 K7 C, m- `struct fcb blk;
" @! m3 {7 F( e4 {; r, hint i, result; </FONT></P>
5 h. x* A( {! }% {2 Z0 y1 Z5 F! }6 e<><FONT color=#0000ff>/* get user input file name for dta */ ; C4 g  P# G: L- n% K! g
printf("Enter drive and file name (no path - i.e. a:file.dat)\n");
. ~: L/ L# w, E+ I' Sgets(line); </FONT></P>
# m5 D& V, N6 ]! @6 Z2 O* g$ f<><FONT color=#0000ff>/* put file name in fcb */ $ X1 B& E. S0 k7 M
if (!parsfnm(line, &amp;blk, 1))   ?  U! U) O$ X& c; x4 Z
{ ( C* n4 v* w5 c! ?3 ^  Y
printf("Error in call to parsfnm\n"); " h$ k. T% H- @3 ~! u1 ^: U: i
exit(1); 3 J9 W) m6 Z) ?" A
} - f$ M! |5 d; e& [6 j# b
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>* w3 O' u8 F* o2 b, ?
<><FONT color=#0000ff>/* open file with DOS FCB open file */ 7 Z: y  R, U: X
bdosptr(0x0F, &amp;blk, 0); </FONT></P>
# v8 Z7 q, F, _& T( |% a. k, ^<P><FONT color=#0000ff>/* save old dta, and set new one */ & w3 ?* o% U; j) r' I
save_dta = getdta();
) I0 I4 M/ e& Rsetdta(buffer); </FONT></P>
' Z5 x" Y; }3 u8 O( [<P><FONT color=#0000ff>/* set up info for the new dta */
# c$ Z/ i8 h3 g6 a' Q( v) z  W: ]blk.fcb_recsize = 128; ( f2 ?7 i7 I1 {
blk.fcb_random = 0L;
; s/ o2 i. C! X  g' O# l" q7 U# K+ Tresult = randbrd(&amp;blk, 1); </FONT></P>
" ^& q# ?4 @) |' X9 {3 M<P><FONT color=#0000ff>/* check results from randbrd */ * Y6 R/ N2 S8 n* ~
if (!result)
1 h: S- Z# j, Dprintf("Read OK\n\n");
: d9 W) y' c9 m5 g/ t" y# yelse
) a% o& P& D5 v& m2 ^) y6 R4 y{ ! V- a% H9 G% {; N
perror("Error during read"); ; d3 T8 g" m0 ~# v! S, J8 Q# C/ x! A
exit(1);
7 K9 I: X9 h) H) _} </FONT></P>
7 B( e0 G0 |# p0 I<P><FONT color=#0000ff>/* read in data from the new dta */
! R" I+ p0 M4 N5 `, V' W9 hprintf("The first 128 characters are:\n");
! ]' i9 |* r/ @1 rfor (i=0; i&lt;128; i++)
: A8 k1 K) l. o9 \: Yputchar(buffer); </FONT></P>
4 q) j' o% V/ m  V' U9 D<P><FONT color=#0000ff>/* restore previous dta */
1 A3 L6 }0 y& C/ I9 s  C+ ]- fsetdta(save_dta); </FONT></P>
. ~$ p* Z! f% r) w# k<P><FONT color=#0000ff>return 0; : }! x" J4 g  G% D+ _# ^
} </FONT>, z7 u; V" L" J. ?
</P>
! }# m0 }$ O8 R! G  P<P><FONT color=#ff0000>函数名: randbwr </FONT>, E$ O+ z1 ]. m+ w
功 能: 随机块写
/ ?+ J& g0 B" T% v用 法: int randbwr(struct fcp *fcbptr, int reccnt);
2 }! Q9 n3 Z5 d* H1 Q% z8 l程序例: </P>! C% ]% {) H' {5 T! \/ t7 d; U) n4 x
<P><FONT color=#0000ff>#include <PROCESS.H>. x5 J0 R) p' s# O
#include <STRING.H>
# V1 C8 n# G( Q: t; H3 y; z#include <STDIO.H>7 ?9 F4 R0 d) e5 I. o
#include <DOS.H></FONT></P>0 P- v- {8 A' E- i) D5 d
<P><FONT color=#0000ff>int main(void) ) w; |/ [# x; }9 D" m" j% T+ J
{
% q( L) i- s( q3 Bchar far *save_dta; : x3 G, l4 ]' J2 k: u/ F
char line[80]; : \* ^; e0 h6 g4 f# e
char buffer[256] = "RANDBWR test!";
# j! K% j8 F6 v3 I5 ^# [3 Wstruct fcb blk; ' R. L# _% k, k5 N2 p+ j
int result; </FONT></P># `3 O- _2 {6 F
<P><FONT color=#0000ff>/* get new file name from user */ + V4 u% s8 _  n- u
printf("Enter a file name to create (no path - ie. a:file.dat\n"); + k8 O* P3 a( E  W& A: u
gets(line); </FONT></P>
* {# L9 ]0 T, \2 h. i<P><FONT color=#0000ff>/* parse the new file name to the dta */
0 f3 B( `5 P0 Q% {; ~- jparsfnm(line,&amp;blk,1);
# J* R3 ~, W. w3 Y4 ~. h! Cprintf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
. X: v# B# T# S<P><FONT color=#0000ff>/* request DOS services to create file */
, {' U/ q9 i) _: A0 o* Bif (bdosptr(0x16, &amp;blk, 0) == -1) ! K* N2 m6 s: Y
{ # Z4 Y, y0 A' z- Z/ P5 K0 S
perror("Error creating file"); 3 f/ Q, k: M1 p- O4 U5 w
exit(1);
( @' G' B8 z; j: I" D} </FONT></P>! q. ?9 d  P3 u( I( U+ C
<P><FONT color=#0000ff>/* save old dta and set new dta */
8 k: s+ B. J9 n- F  W. r3 }save_dta = getdta(); 5 j2 i* Q0 U/ ~
setdta(buffer); </FONT></P>
# k8 q' t; I- `- g# a<P><FONT color=#0000ff>/* write new records */
* M  V$ m5 `# xblk.fcb_recsize = 256; $ n  B9 t, X0 |8 f# i3 n9 u
blk.fcb_random = 0L;
$ W( g6 b6 K+ fresult = randbwr(&amp;blk, 1); </FONT></P>
1 s1 R2 p( @- P) K<P><FONT color=#0000ff>if (!result) 2 x+ |& e" K( D6 L3 i
printf("Write OK\n"); / X+ X, U! L  n4 R1 U) E
else
# W+ |* V- b$ r4 v) ^{ - U% d  C% V  J/ b
perror("Disk error");
- ?0 L, B- o/ D, Rexit(1);
& B% O8 _# q  L} </FONT></P>
, ^6 g& C5 q: J" B# U: t<P><FONT color=#0000ff>/* request DOS services to close the file */
* g5 y* n  P, E! G3 D  zif (bdosptr(0x10, &amp;blk, 0) == -1) 3 X& `& P, O5 J+ W
{ + x4 t7 g, ~' u, p4 A0 g
perror("Error closing file"); 2 l/ K3 }! f( D+ A! v8 M4 }6 e) T
exit(1); 5 j6 h8 O" u5 g  M; ]5 ], H, E
} </FONT></P>9 x  z  R: i9 y) @% O1 b* R
<P><FONT color=#0000ff>/* reset the old dta */
% @" s7 E. h6 ysetdta(save_dta); </FONT></P>+ q% h; ~1 ^0 X2 O# Y, M6 v2 ^: |
<P><FONT color=#0000ff>return 0;
: x; W& l6 S( i0 _6 \} 3 \! W" @% o8 ]
</FONT>
2 M9 u% T: c. m- ]6 J! v</P>& Q2 K; W+ d' U* M
<P><FONT color=#ff0000>函数名: random </FONT>
- Q5 V- [9 K; j1 `( Y功 能: 随机数发生器   `  i3 s$ r# s: r4 S$ @
用 法: int random(int num);
3 k1 M5 w6 V( u5 D9 B9 L$ I6 s程序例: </P>* Z, n; w. R4 d6 }- g. W
<P><FONT color=#0000ff>#include <STDLIB.H>
* K( a3 q$ M* ~, C5 V) n1 V" s#include <STDIO.H>
/ d. |/ V5 X0 B% }3 `) B, c( T#include <TIME.H></FONT></P># w; A6 y  K. a: K
<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ % ^# \* B& H8 q
int main(void) & R3 y$ W6 d: ?! ?5 m; C
{
' v( A7 t# D8 v7 irandomize(); ) m0 \" a+ Y+ C) n4 p( M
printf("Random number in the 0-99 range: %d\n", random (100));
) s& E  ]0 I4 h: j9 {5 O/ ]return 0; * G0 m- r, O# E2 k* k- _
} ' f6 P. s( K$ L/ o4 \* `1 p
</FONT>0 J( J: u* Q8 R  |2 Z2 A
</P>
9 h* D' r) r3 o5 I8 A+ b9 R. Y, q2 o<P><FONT color=#ff0000>函数名: randomize </FONT>
% M* I0 L" H, Q, K功 能: 初始化随机数发生器 " e3 Q4 u! Y# u0 \" P
用 法: void randomize(void);
8 g7 `+ D% e+ ?" H程序例: </P>
. V+ j: @9 ^, g4 `<P><FONT color=#0000ff>#include <STDLIB.H>5 C; F4 T9 ~  N0 ]
#include <STDIO.H>+ L% T2 _/ B& K! B7 e' N
#include <TIME.H></FONT></P>) p+ \) ?. U: |8 r2 m1 P
<P><FONT color=#0000ff>int main(void)
  [) N8 ]- I2 p- y  y4 M, }/ `{
# W$ b9 v0 K3 R, p1 D1 d) A# tint i; </FONT></P>3 D$ _' i7 T* P) H  x( a; K2 Y
<P><FONT color=#0000ff>randomize();
" c# b7 ]# a, c' l, p, Oprintf("Ten random numbers from 0 to 99\n\n");
, C9 q+ C" M) Wfor(i=0; i&lt;10; i++)
# o: g7 N4 |+ `6 k# [& R3 _printf("%d\n", rand() % 100); - C* p- M+ z1 W- W# b( m- ?
return 0; ( x2 c/ w4 h9 \6 d' w# g' Z
} " A7 G8 I. k0 o" }
</FONT>3 r- v: o2 [4 Y
</P>
! a7 \* H0 N  h: n+ Q) Z( Y, k<P><FONT color=#ff0000>函数名: read </FONT>
% b( c6 {* S. p% f# D+ H" |4 u% f功 能: 从文件中读 0 q* o0 G$ a$ s; h
用 法: int read(int handle, void *buf, int nbyte); 0 S( z- I$ J3 T+ p1 _5 j
程序例: </P>' A3 v  {5 v+ V  {1 G& u: |
<P><FONT color=#0000ff>#include <STDIO.H>- \, V6 f& p* S+ ?+ {/ s: [
#include <IO.H>
( j! x/ ]+ Z& e9 L& b6 N) ^#include <ALLOC.H>$ |8 _  b/ E8 b9 p% ]
#include <FCNTL.H>
+ H) e% ]* {; }$ v#include <PROCESS.H>
( k8 x; h$ S& ^2 w  \#include <SYS\STAT.H></FONT></P>
0 A, L/ r3 j* _<P><FONT color=#0000ff>int main(void) , `; w0 h1 E: }5 ]
{ $ v' w- d4 \7 F2 D
void *buf;
7 a7 S3 I# v' a3 T# [int handle, bytes; </FONT></P>
5 _" n# X( \3 u6 \* J* f" }<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>0 q3 ^$ y- L- B3 t$ I
<P><FONT color=#0000ff>/*
* l1 q+ U# U  yLooks for a file in the current directory named TEST.$$$ and attempts
$ z1 m1 y; b  ^4 h: c0 c+ H* D; cto read 10 bytes from it. To use this example you should create the . w, l" P+ a1 \! n% W& B- H7 Z
file TEST.$$$
3 X5 X6 E; j3 e2 [9 j! _*/
) [9 ?/ U/ c+ q$ j' y3 P; Jif ((handle = 5 E. b1 O/ r) r; W0 k, R
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1) ) B+ m' A; z9 a9 g
{ / P) j) ?5 a  J5 V5 f' o
printf("Error Opening File\n");
" w4 F# t. L( E$ ]3 ]: lexit(1); # Z& \; A; I5 K% p) X! e( _) ~
} </FONT></P>
$ f  |& V' n, ^. L<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) { / W# X' N) ^% c4 u0 p
printf("Read Failed.\n");
. n- F5 s+ D7 j  A5 S* kexit(1); 5 L; j$ ^$ d. J  \0 v4 @$ l
}
+ Z6 o' y1 t! Jelse {
5 P0 ~2 p, T* Z1 ~& R) J  Qprintf("Read: %d bytes read.\n", bytes); ' e. m8 a/ O0 I' j% e2 f0 j- W4 y8 P3 Z
}
# K7 W0 W0 L4 J! i  ~" Mreturn 0; 4 W0 n2 ~! K9 b+ T6 E
} </FONT>
! Q) F$ N5 R! f0 l1 x
0 `3 _7 q3 K0 y8 Z# T( h0 m</P>' `* C: X2 j  w$ o" Q
<P><FONT color=#ff0000>函数名: realloc </FONT>* o- |% A  {5 u$ ?
功 能: 重新分配主存 1 h) \2 x& X) L4 g: O" d% ?
用 法: void *realloc(void *ptr, unsigned newsize); 1 l- M( ], @0 h* S9 R3 ]
程序例: </P>/ t# g3 P* O* J+ d
<P><FONT color=#0000ff>#include <STDIO.H>3 j8 l( o- P; `- h! f% W
#include <ALLOC.H>
% T% u8 z, q4 W% O#include <STRING.H></FONT></P>/ s2 }, @0 Z. r7 m* x$ f- `( c
<P><FONT color=#0000ff>int main(void) 7 E7 {# y3 [9 T. g, n9 {" ^
{
2 S! W" \0 `3 z. t* |& bchar *str; </FONT></P>
$ x) `$ Y) Q. }( t" u* c3 A<P><FONT color=#0000ff>/* allocate memory for string */
1 H7 o( c6 A; c+ u6 O% N5 N/ \+ _% sstr = malloc(10); </FONT></P>
. w$ O5 T$ ~6 S<P><FONT color=#0000ff>/* copy "Hello" into string */
" H/ w' G: y: i/ U6 wstrcpy(str, "Hello"); </FONT></P>
: W$ k; `# R# u/ k- ?) U<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str);
0 @8 f2 G4 e3 E, k* K% K' V# }str = realloc(str, 20); " D! T0 I( {" j( g. {4 V2 C7 R
printf("String is %s\n New address is %p\n", str, str); </FONT></P>4 Y+ d& e, F* o" \& i8 h1 \
<P><FONT color=#0000ff>/* free memory */ 0 r* ?- |% H# {8 S! |7 }; U2 R7 r2 f
free(str); </FONT></P>
6 ^. |/ c& i( o4 g# `$ l* ~( n/ D<P><FONT color=#0000ff>return 0;
  l6 @- e  Y& \1 o}
( a" {6 C4 a7 _9 r2 T$ M) _* I</FONT>( L- G9 y% k! B# X' ]+ ^
</P>
% L& M8 W4 ^4 H4 Y<P><FONT color=#ff0000>函数名: rectangle </FONT>
- L1 i' X# C& s" {$ k% k1 `% t7 D功 能: 画一个矩形 9 x- v" l( U' q4 e# h# U
用 法: void far rectangle(int left, int top, int right, int bottom);
& G* E. Z! B* S- h程序例: </P>
3 Y' @# e6 ]( ~0 i' T6 E- z* N<P><FONT color=#0000ff>#include <GRAPHICS.H>! L6 b9 p% P+ A6 D" O# |$ K8 W) l
#include <STDLIB.H>* C$ W( l* y4 e& G8 d' `
#include <STDIO.H>$ \8 n( W9 ^3 _0 X- [1 }2 p
#include <CONIO.H></FONT></P>
% }3 ]# P: B7 a: K& r$ }) p<P><FONT color=#0000ff>int main(void)
# F; _, `, \( ~4 @{
! A1 z1 Z6 e& H: \/ ?$ U/* request auto detection */
+ T( K, t% ~# D( [% b+ {- I6 Iint gdriver = DETECT, gmode, errorcode;
( `2 Z6 J( d* O9 h7 bint left, top, right, bottom; </FONT></P>
  d4 f! f- q3 ?0 L<P><FONT color=#0000ff>/* initialize graphics and local variables */
9 d  k2 y  z: h3 c1 q# Z4 ^+ Finitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>8 G: {% z% N5 k
<P><FONT color=#0000ff>/* read result of initialization */   ^* e2 J8 e( d& M* H( ?
errorcode = graphresult(); , ]- S) r1 B0 j+ z% a: z, J
if (errorcode != grOk) /* an error occurred */
  o0 j) F) _! R{
6 l5 P! g4 D) D, p1 G; `% Zprintf("Graphics error: %s\n", grapherrormsg(errorcode));
) e, x$ o7 y& V6 t+ ]printf("Press any key to halt:");
5 i' U/ i# d9 q5 m# w# h5 P1 vgetch();
/ x6 t+ ?* ^, |3 P1 t* I* O. mexit(1); /* terminate with an error code */ # u' g+ r0 O# @0 r/ g3 ?
} </FONT></P>* B) d5 a: s* W+ p
<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; - E% ?0 _8 k& e* @
top = getmaxy() / 2 - 50;
. f% G9 r8 G* _) b2 v" q7 J' bright = getmaxx() / 2 + 50;
2 X6 t, n5 S0 {4 W& mbottom = getmaxy() / 2 + 50; </FONT></P>4 Y# U, Q( S8 ?2 r/ I  n$ ^& u
<P><FONT color=#0000ff>/* draw a rectangle */ : S  B& }2 D1 h6 d4 F
rectangle(left,top,right,bottom); </FONT></P>
$ v% s. i6 o! z( K<P><FONT color=#0000ff>/* clean up */ + }. g4 `( r7 A6 v" M( u8 X$ K
getch(); $ X3 m  m# A: O3 o- U, ?
closegraph();
# V: ~$ j* f8 @  I; nreturn 0; 4 V  w) |( ^: m& E
} 1 J: f) k5 G; w/ X: t
</FONT>
/ U! @2 E3 e: N; V3 `1 _- h6 E</P>1 ^6 @, _5 N3 Q9 d# d& W
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>0 v  m$ G6 T! f; C
功 能: 登录已连接进来的图形驱动程序代码
$ ]1 B- H6 p# J: ]  [* W2 z用 法: int registerbgidriver(void(*driver)(void)); $ q. w6 l# M/ B1 T
程序例: </P>
8 F/ Z* j% i8 h<P><FONT color=#0000ff>#include <GRAPHICS.H>
! H$ B+ c6 w# U' W7 p. Q#include <STDLIB.H>+ {& [7 \+ B; ?/ @
#include <STDIO.H>
0 ?8 [1 |0 H) @( W# D2 g- X#include <CONIO.H></FONT></P>- t/ K3 Z; p2 u; [7 n
<P><FONT color=#0000ff>int main(void) 0 b  l; d( F9 V6 L$ K
{ ( ~5 k$ p. n3 \9 \# d9 Q8 ^
/* request auto detection */
5 ?4 P8 p$ L2 C. d; l) Y/ l# H. pint gdriver = DETECT, gmode, errorcode; </FONT></P>  h. |6 T" W* g" ]
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ 1 k5 z# X. D( b5 l' p+ D' S
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>* S& Q: s+ m5 [: a8 s* i
<P><FONT color=#0000ff>/* report any registration errors */
2 u2 q7 I' N+ h8 V% x0 Bif (errorcode &lt; 0) , p3 N" Z8 J) g1 Z4 E
{ $ l. O+ I# l1 N) c$ b
printf("Graphics error: %s\n", grapherrormsg(errorcode));
, t! Q% ^' B5 _6 t$ Q) a. Rprintf("Press any key to halt:");
, m) f( a! k6 `6 d! b. A/ ~getch(); " \1 R5 Y: e+ x6 K' u/ ^; E) H
exit(1); /* terminate with an error code */
6 M2 M% J+ ~) u% A' L8 A} </FONT></P>8 H' Y+ L# Y' G" ^6 W* s: W6 i
<P><FONT color=#0000ff>/* initialize graphics and local variables */
( B. n- V# \9 A+ A# @" Linitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>- B" |: S  t. T
<P><FONT color=#0000ff>/* read result of initialization */
* Q- M1 D7 F& T/ @% V0 rerrorcode = graphresult();
. @3 e: s1 d4 C3 i) D$ Y6 Tif (errorcode != grOk) /* an error occurred */ 2 g2 E- G% f7 _9 d" w. w  g" [- m
{ 0 S1 O1 k0 k% f$ x8 F
printf("Graphics error: %s\n", grapherrormsg(errorcode));
" V/ z3 v+ |" T$ e  z8 `1 ~printf("Press any key to halt:");
! q6 f) q' X, ?+ h+ P/ l5 i* k* Xgetch(); 6 A2 e8 X, }8 X/ l" s# o5 x- s
exit(1); /* terminate with an error code */ " i% L1 Z7 u  z5 N! _% V4 S/ l
} </FONT></P>
" T3 J. R! J0 k. [$ H8 I" s& L3 U<P><FONT color=#0000ff>/* draw a line */
2 l1 v$ Q# L. d6 i1 G# s" \6 zline(0, 0, getmaxx(), getmaxy()); </FONT></P>
- ?; R& ?0 J3 E& F# o<P><FONT color=#0000ff>/* clean up */ ) |4 t# {7 c, k8 J$ O) e
getch(); % s) Q2 _. P2 t2 B8 T, {2 F
closegraph(); " W$ d3 R- s9 g4 g8 K. W
return 0; ) f* o$ O; t3 T) H0 `
}
2 P& I' h6 E/ Q* O9 {</FONT>$ ^' `& p4 I/ X* F
</P>
( [1 d( X2 A3 A; {<P><FONT color=#ff0000>函数名: remove </FONT>
) o2 y/ D( ]  ]& b. Z功 能: 删除一个文件 1 [/ X5 Y7 r+ K$ j: y+ }* @3 `
用 法: int remove(char *filename); 9 ?& i2 p$ ?2 y2 ?* \
程序例: </P>
( d+ _9 o7 c# l) P5 I<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
: y  }" `; [6 v4 T9 \+ {. Z" r& F<P><FONT color=#0000ff>int main(void) 2 n. g- Y7 X9 G, `6 k* u- j4 y9 K+ G
{ # w; |/ u& S8 A5 g" a
char file[80]; </FONT></P>" _) \0 u* C; g& E7 c3 k
<P><FONT color=#0000ff>/* prompt for file name to delete */ 9 Z: {9 P& W# u+ b; j
printf("File to delete: ");
/ ^4 D& M9 \1 R9 e  R! K; j2 }0 ]gets(file); </FONT></P># c3 r0 ~/ D/ E) K0 K
<P><FONT color=#0000ff>/* delete the file */ 0 ?' i6 Y* x3 V% u
if (remove(file) == 0)
9 n& ~" y) Z3 H0 S0 ]) G3 Yprintf("Removed %s.\n",file);
: w+ K8 o' p. pelse
. Y! {1 x% n! _7 z& J# gperror("remove"); </FONT></P>
4 s: g/ Q7 m$ J* j; j2 N<P><FONT color=#0000ff>return 0;
  }3 g8 ]7 K5 I} 9 f0 J  `' a' \* F
. A' K4 e2 R$ m
</FONT></P>
- c- o# c' e0 y<P><FONT color=#ff0000>函数名: rename </FONT>
( o* d1 p3 M" D1 K! H9 g- O功 能: 重命名文件 & Y  R$ W: Q# X& N. h
用 法: int rename(char *oldname, char *newname);
' i9 d; [+ I1 [8 Z. E, g程序例: </P>
, j6 E. a. I0 u9 O! \" b<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>3 ]" \! G. v3 X  w( y
<P><FONT color=#0000ff>int main(void)
8 S! ^- m, Q/ ?' L2 v# Z% M1 y1 G{ . M0 ~/ o' M7 R  |
char oldname[80], newname[80]; </FONT></P>
, E. P) C" s& |- i0 }<P><FONT color=#0000ff>/* prompt for file to rename and new name */ + S, _8 }: |+ T" m
printf("File to rename: "); / A& C/ v5 o# R9 m9 M1 q/ d& M7 N7 ?1 t% i
gets(oldname); ( h* Q: F7 g4 B3 k/ ?+ O6 W
printf("New name: ");
: M: |$ z& H* o+ C$ i; igets(newname); </FONT></P>/ F6 d5 j3 P: I$ C/ p0 d
<P><FONT color=#0000ff>/* Rename the file */
$ L- W7 Z0 C- }5 Cif (rename(oldname, newname) == 0)
/ E8 Q' ]- E, s: y+ e( s( |printf("Renamed %s to %s.\n", oldname, newname); % M2 W1 }+ \0 y0 O+ x
else
* k- ^$ k' V0 ~/ Qperror("rename"); </FONT></P>
# J2 d( Q8 `& P0 N6 b* F' N<P><FONT color=#0000ff>return 0; 9 h. N8 m% |! ]% F  E% y8 W
} & D1 k, u9 e# T8 U8 S/ D
' V7 q& `% m$ V: x7 l0 A
</FONT></P>
' S  `, x( l6 z3 e+ w4 t<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
* r9 ]3 D& ^% @8 S" g: |功 能: 将屏幕模式恢复为先前的imitgraph设置 1 l! b' H; j5 d1 Y) y9 g: G. l; R9 o
用 法: void far restorecrtmode(void); , l3 P0 ?2 E0 r9 S
程序例: </P>
4 Q8 z& p% h9 E/ c4 K  I5 c<P><FONT color=#0000ff>#include <GRAPHICS.H>: Z. j& |0 d9 q" A, o
#include <STDLIB.H>+ q5 y2 c: ]3 c1 m0 L1 k0 d
#include <STDIO.H>, K( G. k' k& C8 x* d" Q+ _! Q0 c
#include <CONIO.H></FONT></P>
3 ^/ u8 r' J- l& X<P><FONT color=#0000ff>int main(void) 2 H+ F3 ]  k# H
{ - y7 N* Z: v1 f5 U
/* request auto detection */
  f% H6 c6 D/ J; rint gdriver = DETECT, gmode, errorcode; 0 T% i! s2 V  \. b  @  H
int x, y; </FONT></P>% J. P% Z2 ?) t! D3 F
<P><FONT color=#0000ff>/* initialize graphics and local variables */
2 k. Y# n+ @5 ?# z* L, rinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>& f& P% i1 a, n$ }; C
<P><FONT color=#0000ff>/* read result of initialization */   C6 y3 u" D' u4 ^( z  r
errorcode = graphresult();
+ V' G* H) M: ?# \+ p/ {if (errorcode != grOk) /* an error occurred */ . L; ?4 I4 f' s. z7 L4 ?
{ ) f: \8 \! i8 f* }; j
printf("Graphics error: %s\n", grapherrormsg(errorcode));
& s/ W' h5 E  l6 A5 L+ B- V& Jprintf("Press any key to halt:");
0 G' O% B0 J& _) I$ [  \getch(); , r% T5 X% V. S
exit(1); /* terminate with an error code */
  g: h0 A8 C! K4 o5 ]$ Q* D+ G} </FONT></P>
9 {& i* g& b! w9 J8 J& b0 H! g<P><FONT color=#0000ff>x = getmaxx() / 2;
, z+ }; L7 @1 a3 r2 I$ q8 y6 Zy = getmaxy() / 2; </FONT></P>% V  n5 c( W, b3 ]
<P><FONT color=#0000ff>/* output a message */ 1 {) X- Q4 t; f# ~; C4 r7 A  M$ W
settextjustify(CENTER_TEXT, CENTER_TEXT);
; v, |& p* |) O& ?  _4 V" mouttextxy(x, y, "Press any key to exit graphics:"); 7 `2 x7 \, v0 ^& m9 v+ x/ `
getch(); </FONT></P>
( B( C8 w( ]2 J6 q& Z<P><FONT color=#0000ff>/* restore system to text mode */
* _/ J# _' V. C$ ?  c0 n" lrestorecrtmode();
. x4 y! H9 n! i4 Gprintf("We're now in text mode.\n"); 2 P- m( C1 A+ R' o6 B
printf("Press any key to return to graphics mode:"); , O: H# i6 A  m  `, R! s
getch(); </FONT></P>% E5 F7 V% r5 z  t  v
<P><FONT color=#0000ff>/* return to graphics mode */
, r& }) D$ T& d. U9 K! ]setgraphmode(getgraphmode()); </FONT></P>
7 z5 Q( J4 b8 H$ M" }  _: g& R<P><FONT color=#0000ff>/* output a message */
- h9 J" {' p+ [% ~8 q: [' F, ^0 Xsettextjustify(CENTER_TEXT, CENTER_TEXT); 8 i) z& l/ U$ J  L9 W
outtextxy(x, y, "We're back in graphics mode.");
/ i+ N' l; S- A( X8 @0 @! ?outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>, J; `2 h) K0 p
<P><FONT color=#0000ff>/* clean up */
" u& B; R- b2 q, d; {" n# {$ Wgetch();
0 O. R& T; O! M' ?( ~) Z6 a2 {closegraph(); + R$ k( Q- n1 z6 I
return 0;
# D, y* ^' A% A. ~/ U# q}
0 T, R0 a/ o' M  Q</FONT>
) _6 ^) k' X( U4 d8 f</P>
' f) g3 H( U/ _4 M1 z6 D8 E* v<P><FONT color=#ff0000>函数名: rewind </FONT>1 p& @2 R. o( }, G
功 能: 将文件指针重新指向一个流的开头
. }* \! S' T% `; [' m8 t用 法: int rewind(FILE *stream); - t$ o$ D1 \# r2 c& h8 a0 z+ s
程序例: </P>
! I! m5 ~6 K2 i; |& r7 m<P><FONT color=#0000ff>#include <STDIO.H>
+ d) l: A, t$ g5 r; U; r$ O: F#include <DIR.H></FONT></P>
4 A- J' J" r, l3 |<P><FONT color=#0000ff>int main(void)
' J( N1 Y$ s3 K; Q# l4 G* A{ & U& i  }9 S2 N* R0 N' R  `
FILE *fp; ' y" A, m) ^3 Q4 ^) b( \$ I
char *fname = "TXXXXXX", *newname, first; </FONT></P>. O+ ^% R6 p! K
<P><FONT color=#0000ff>newname = mktemp(fname);   H/ ^6 R: d* A) Q8 H
fp = fopen(newname,"w+");
2 b& I" L6 l: L$ sfprintf(fp,"abcdefghijklmnopqrstuvwxyz");
1 j7 s$ ^/ P6 u" Q* Trewind(fp); 2 m+ S/ P' E  L$ d/ s
fscanf(fp,"%c",&amp;first);
8 o' I, {- b& v+ Jprintf("The first character is: %c\n",first);
( d) S2 Y% z0 M4 I' E$ afclose(fp);
9 b; V4 w, {0 m) H& _- i. |remove(newname); </FONT></P>
& R$ S+ `5 S. T6 L6 d<P><FONT color=#0000ff>return 0;
- Z9 ]' A2 Q* W0 P$ a} ) E9 G$ i& ^7 G
</FONT>5 S% I& \: T# d9 B
</P>/ L: D7 q" G' e6 i& d5 U. c( C
<P><FONT color=#ff0000>函数名: rmdir </FONT>, x. j3 J. y& n9 v0 Z
功 能: 删除DOS文件目录 , ^$ ~4 N$ g  T8 d- {
用 法: int rmdir(char *stream); & ]+ K: y0 p) T6 b$ _# _
程序例: </P>( @- w3 G4 b( n5 I! ?  K
<P><FONT color=#0000ff>#include <STDIO.H>* l/ s  X# k( F: U5 \/ {+ r: ?7 p
#include <CONIO.H>) L" n9 U* u. m8 [  v, l; u* c
#include <PROCESS.H>
+ l6 I7 ?0 d/ z+ ?: R$ F#include <DIR.H></FONT></P>- \/ K( O' ?+ P. M% v
<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>" C! r3 G# s2 v% ]. w
<P><FONT color=#0000ff>int main(void) ( {) x) U. y; s1 G% Z8 V7 l4 Z
{ 2 O7 C$ s* M  }) C: _- q  ?2 {/ R
int stat; </FONT></P>1 \6 B0 B: e! y5 q7 \4 ^) B
<P><FONT color=#0000ff>stat = mkdir(DIRNAME);
" F1 e4 F9 g7 c) |' b1 H5 w8 Xif (!stat) 6 v/ X1 m5 T4 l& F2 e
printf("Directory created\n");
; w8 L7 Y+ r0 T' k! Delse
- n3 B, a" w0 o! u9 t2 a{
. ?% Q) f! }3 U) hprintf("Unable to create directory\n"); 3 ?5 N: S# s- L$ E) `1 D0 s" t
exit(1); / u/ C% s, n7 c6 ]( ^. r
} </FONT></P>1 v  s9 V6 N4 \' n
<P><FONT color=#0000ff>getch(); 7 ~0 N# V8 x2 ?: v5 T6 H) Q5 R/ t
system("dir/p"); 4 ^. V8 M# C% n# C1 a( j
getch(); </FONT></P>
. n! N0 }. w5 V' @9 k4 c2 x<P><FONT color=#0000ff>stat = rmdir(DIRNAME); + Z. f2 K3 c. E" h0 W
if (!stat) + N1 }  Z! @, T' _+ O6 {/ _
printf("\nDirectory deleted\n");
( e% W( C2 n% R/ |  Gelse
$ P1 y8 M! v& i) e9 c! n{ , t3 m: o5 _5 u+ v( V  \, ]. C) [
perror("\nUnable to delete directory\n"); / }* L, G$ y; {& y1 R( x/ t5 f$ Y% S
exit(1);
4 ~. ?, ?' c. |8 r3 \$ C# C} </FONT></P>
0 p/ G. ?- ?$ v2 ?- d+ q<P><FONT color=#0000ff>return 0; & X; k6 o- W; [  ]: u, v
} </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-10-30 08:13 , Processed in 0.423880 second(s), 52 queries .

回顶部