QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
) j, s, F2 ]: [7 q功 能: 使用快速排序例程进行排序   ]" T& m% P" ^1 Q' F7 U
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
# Y! ~4 N# S4 X2 Z* Z! H3 S) g& s7 ]程序例: </P>
' ~1 D% {: @/ D! B7 b6 m4 b<><FONT color=#0000ff>#include <STDIO.H>
9 R/ T+ g( \: F1 P* A% |#include <STDLIB.H>) ]% K- f  V9 g0 e2 l+ A/ C4 }  f
#include <STRING.H></FONT></P>& X: e1 B$ d& v' v
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>% @$ p. ^6 U$ ^3 p% }0 }1 N
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
. P2 d, o* {3 w% z( N</FONT></P>1 I& y; m6 P  H8 q# e( Z
<><FONT color=#0000ff>int main(void)
" `1 @) x6 |. z; a2 m{
2 |6 X6 n* }- ?7 @/ G, iint x; </FONT></P>2 o5 m# v$ r# t* N# \
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
5 p5 ^3 H, _& s' T6 ifor (x = 0; x &lt; 5; x++) & X- D8 F9 Q8 g
printf("%s\n", list[x]);
$ `1 ~; Q4 u* q  F  z( o+ C" I! J3 a; Wreturn 0;
8 ^1 B, D' l3 }3 M  p} </FONT></P>6 o# m2 a5 t3 `* K9 {
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
0 t# B: ^4 F5 f4 a9 N6 z{
3 _  x" v4 F3 v6 T7 ?return( strcmp(a,b) );
$ {' ?. D! ], e  m. y8 P} </FONT>
( ?) y) {+ i( p2 n9 M</P>
1 O# [3 C9 `+ o; Q! z5 x& b2 C2 ]. t

# W  v1 B. p* ~  z6 D<p># r: l; j7 x; M# t. H
<><FONT color=#ff0000>函数名: qsort </FONT>
  p& G: ?4 k# u0 F9 p4 `6 G2 M功 能: 使用快速排序例程进行排序
) \+ P1 N5 l" d1 X3 @4 n( Z用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); 7 y6 c4 Z) i" v, A4 }7 K
程序例: </P>& V/ x2 ~/ [4 u# |7 O) M! ~
<><FONT color=#0000ff>#include <STDIO.H>4 Y8 d# F2 J9 m
#include <STDLIB.H>
6 T3 V5 p* |; L8 o) e* a, C2 P#include <STRING.H></FONT></P>( H0 M& p$ K4 z- Z8 k! C* G
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
4 T3 g( R* T) x<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; ! G  v8 j7 |" D$ p: g3 `+ E
</FONT></P># x8 U; w8 |4 n4 p9 Y) S6 t
<><FONT color=#0000ff>int main(void)
' l6 |' n) o) d) K! A{ 9 B4 R/ ]. C$ m' A# X+ v
int x; </FONT></P>
) R2 F; m, _" S$ N: I<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 3 X/ d8 M  e; i" W/ e8 m1 j
for (x = 0; x &lt; 5; x++) 1 }7 F1 N; V3 U& z& _5 L& b; ]
printf("%s\n", list[x]); " R6 x) z* E- O9 X0 l% u
return 0; . m9 R% P$ g7 E' B1 N) Z+ s5 o
} </FONT></P>
4 |" Z2 o' {2 |<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
, Y' S) D" ]6 I2 a0 v& O, [: b{ * Y8 d) t5 z0 X  b" r. x; a$ v. d
return( strcmp(a,b) ); ) ]" k/ o* u0 i! u; ~
}</FONT></P>
5 r. T* y" A! T/ m! L<><FONT color=#0000ff>
+ [, j# w% o7 b$ L9 c" @</FONT>
. q" O+ L) C8 p" u0 l9 v4 y</P>
+ P: H0 S/ a3 W/ U$ h$ s$ `<><FONT color=#ff0000>函数名: raise </FONT>8 h' M' d4 \( K& E) X7 h- A2 v0 X
功 能: 向正在执行的程序发送一个信号
0 J5 R& x0 U  d: Y' s用 法: int raise(int sig); . c) |) ^! X  r! ]2 Y
程序例: </P>) z! e  l5 f* X2 _, h! ]7 ^
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>, J$ O2 @. ~, x, a! {9 }
<><FONT color=#0000ff>int main(void)
# `& n# ^; b, v2 l0 P{
' k; v$ \3 u& t# X9 nint a, b; </FONT></P>
5 _9 v6 A8 g) ?# ?( @. C<><FONT color=#0000ff>a = 10; ( G6 w1 e9 x  g; p, x
b = 0;
, Q- G4 q" ~5 X/ e! yif (b == 0)
# Z$ X9 O) g+ _/* preempt divide by zero error */
, D6 T9 w. F, s" \8 braise(SIGFPE);
) |1 p6 J; X3 ]7 x" Q/ i# [9 a2 Ra = a / b;
* S$ z( ~* ~" s7 n6 G9 L* yreturn 0; * B# m  S1 u& G2 ]) u
} 3 h! C- \' [8 x7 x) {& x: K
</FONT>
3 c# w* A5 t. X</P>
5 k% I0 P  m& l+ q, u6 q) o% w<><FONT color=#ff0000>函数名: rand </FONT>
) O: W& C4 {) t# G, V! p: X功 能: 随机数发生器 5 y, P8 X& z9 I4 x% F$ B: K8 }
用 法: void rand(void);
) }) Y' Z* d4 r程序例: </P>
# x8 x7 N& z+ X4 h<><FONT color=#0000ff>#include <STDLIB.H>. P0 ~2 p* B2 p. E. u# Y
#include <STDIO.H></FONT></P>  G2 o; c' j* t
<><FONT color=#0000ff>int main(void) * G7 ^6 Q$ p0 F" L$ K; f' ?- y( K
{
2 Q1 e: G- I) t4 [0 w* ]int i; </FONT></P>
3 }& O5 W! P' g/ |<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n");
+ A# [* r6 m2 c, g/ l4 Ufor(i=0; i&lt;10; i++) / G8 A+ G0 c1 @' H
printf("%d\n", rand() % 100);
2 Q  o6 A, C  t- X; N# F- Xreturn 0; 1 V+ @1 O) ?5 x' T3 q: z
} 1 |5 W- B! S7 ]& D( O3 w5 m8 K
1 ~0 n1 \9 e% G: |
</FONT></P>
+ E; u& K  i9 a! Q5 n, P5 s0 a<><FONT color=#ff0000>函数名: randbrd </FONT>
' {0 q0 m$ s3 d8 X" T% m( T% z功 能: 随机块读 ' G) M. D1 V0 ^  B' p6 d, _
用 法: int randbrd(struct fcb *fcbptr, int reccnt);
; `* @7 w( b# x4 U程序例: </P>! {' Z! X- C& J2 z) T4 V
<><FONT color=#0000ff>#include <ROCESS.H>
4 T& D( L. S2 B, e* [9 m. Z  v- y#include <STRING.H>  `& i: {6 r, E' v  }6 j& M' }
#include <STDIO.H>
+ J' D( |4 J% t$ M9 ^" G  E& U#include <DOS.H></FONT></P>3 h7 U1 ~5 O9 B; Z- J1 }
<><FONT color=#0000ff>int main(void) 3 h1 R4 s* [) a0 I  w0 w4 n' L
{
8 h# B% `# d, _char far *save_dta; - Q' j  p$ D4 M( U& m3 s
char line[80], buffer[256]; 6 ^6 |& V3 D8 g# {/ q6 n
struct fcb blk;
5 e6 @2 t* t" I6 {, {int i, result; </FONT></P>
$ h/ Z2 i0 [5 S" c) }/ s0 U1 L<><FONT color=#0000ff>/* get user input file name for dta */
; o8 m  Q! M/ w* G4 f/ {printf("Enter drive and file name (no path - i.e. a:file.dat)\n"); $ O4 K/ L, C, z4 X
gets(line); </FONT></P>: _+ I% m4 z; F4 D% j/ j
<><FONT color=#0000ff>/* put file name in fcb */
, d3 l2 b1 w: jif (!parsfnm(line, &amp;blk, 1)) 3 ]4 U% u) m5 M, k6 r% x* L
{ : N) d& F5 D8 ^3 D$ ~
printf("Error in call to parsfnm\n"); , p( D/ s: i/ l1 T2 n& `8 r
exit(1); / x1 E8 o/ z1 D9 h6 l* q8 C
}
5 R* K: @3 I+ o* s" J( y6 z2 }printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
. @% e7 x: f0 I/ k/ g$ E<><FONT color=#0000ff>/* open file with DOS FCB open file */ , @' X+ q) L( @& t& ?: K
bdosptr(0x0F, &amp;blk, 0); </FONT></P>
7 z, X. ?7 Z  r, J% u' Q, v! K' j<P><FONT color=#0000ff>/* save old dta, and set new one */ ' \$ `+ }7 |" W6 H$ G  ~  _  h
save_dta = getdta(); , O: D6 Z5 J6 {, o, |0 q
setdta(buffer); </FONT></P>
2 N! @6 y% N* G' X7 s% l<P><FONT color=#0000ff>/* set up info for the new dta */
& g# V& `# j5 {blk.fcb_recsize = 128;
* T) C' M: O$ O* B# Pblk.fcb_random = 0L; 3 g+ _; h4 a" X+ k/ q" H) f( i5 \
result = randbrd(&amp;blk, 1); </FONT></P>9 c/ s1 H9 H6 J% O( `3 Z$ V! |, l
<P><FONT color=#0000ff>/* check results from randbrd */ 6 f1 t" i( o( g0 Q# |$ S
if (!result)
- ?4 V2 Z9 q. \' ]% tprintf("Read OK\n\n"); & e$ L8 I$ M, @, K& m6 p* r' v
else / V" E% k, x) v' L: u
{
4 X& E) {. ]1 Hperror("Error during read");
; W5 q" c, \& _9 ^: |exit(1); - w! n7 F6 J0 E) P! G$ l* C( f
} </FONT></P>
0 \- z9 v" M: @4 f1 A4 I$ |" B<P><FONT color=#0000ff>/* read in data from the new dta */ 4 [* z/ @: d% u6 [* Q
printf("The first 128 characters are:\n");
- F, G) Z3 ]" f! S3 _  m* Mfor (i=0; i&lt;128; i++) . Q4 N! b$ o9 w9 n- ^
putchar(buffer); </FONT></P>
; z1 [3 }$ N8 c/ `<P><FONT color=#0000ff>/* restore previous dta */ * v; i" D& y  |) q) G; j! |8 @7 t
setdta(save_dta); </FONT></P># p. f/ w& ~. }* @7 K8 M7 U
<P><FONT color=#0000ff>return 0;
% I+ r% N2 W2 E( g) [/ ^} </FONT>. \: ]/ l; q$ {( `8 }
</P>
9 H$ I6 Z& W: o- d3 l% `6 n<P><FONT color=#ff0000>函数名: randbwr </FONT>
9 x% p0 s, N( z! I功 能: 随机块写 8 w! O" ~# L, _# F! g: e
用 法: int randbwr(struct fcp *fcbptr, int reccnt); 9 f+ Z* u% i' \- f! v
程序例: </P>! w1 p* Q! z" Q
<P><FONT color=#0000ff>#include <PROCESS.H>
* x: X* g! C) r+ g  G#include <STRING.H>
: X8 k7 f; M; W# i) A( a! u. ~1 M  k#include <STDIO.H>' X  }0 W) M/ N0 j* U) q
#include <DOS.H></FONT></P>
: V$ ?; s' Q% q<P><FONT color=#0000ff>int main(void)   Y- i; Y' {# ]
{ ; K0 |: F, j4 p/ r0 H
char far *save_dta;
( m: I% Q8 D6 O+ y4 b5 R9 nchar line[80]; 6 K: }9 L4 }/ ~0 M/ N2 F0 r+ I
char buffer[256] = "RANDBWR test!";
4 F" e: c3 B  e  }6 ?3 P0 _% ]struct fcb blk; , D! E+ ]& v; z
int result; </FONT></P>( V: Z+ @1 }% U
<P><FONT color=#0000ff>/* get new file name from user */
' O( T7 }, Z/ K- D: n! M9 kprintf("Enter a file name to create (no path - ie. a:file.dat\n");
3 I8 C/ {2 U3 J& [8 R$ C& u" U' h/ bgets(line); </FONT></P>
; o2 X8 |7 j( T. X<P><FONT color=#0000ff>/* parse the new file name to the dta */ 5 g. Y7 C: [5 w9 v- [$ Y7 p
parsfnm(line,&amp;blk,1);
; R& i3 }$ E8 h. U. h2 m, cprintf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
2 I/ k# i$ ~1 E6 V+ n+ Y<P><FONT color=#0000ff>/* request DOS services to create file */
+ @0 Z8 R& T" \if (bdosptr(0x16, &amp;blk, 0) == -1)
' f$ l0 r1 [7 z2 w+ X: p/ ?0 A{
) ^! u* S  j0 w4 jperror("Error creating file");
8 s$ a/ X" ~& V6 hexit(1); : s* }* U  k+ H4 T
} </FONT></P>
) @* l7 W; P+ p) l5 P) B0 K<P><FONT color=#0000ff>/* save old dta and set new dta */ ' r: V, N: A( v
save_dta = getdta(); ; t1 _' q6 v* u4 ]4 C4 f  r0 n
setdta(buffer); </FONT></P>
1 ?8 |& R, J$ t- _2 }/ E<P><FONT color=#0000ff>/* write new records */
0 w9 X+ y" ?, \/ W6 ?1 Y$ bblk.fcb_recsize = 256;
1 o) N' {/ W0 H: A6 M! R2 bblk.fcb_random = 0L;
+ J7 t. i3 A, L" b- }# m  _result = randbwr(&amp;blk, 1); </FONT></P>+ x3 s3 o: `  w. d
<P><FONT color=#0000ff>if (!result) . B  E7 t; t" |7 F% D3 x) ?1 ]
printf("Write OK\n"); 4 V4 X* u: z5 B9 K% D6 u
else
6 L0 L- i# E# @$ X6 }{ " \! U3 v& [7 @! N- h0 h+ G
perror("Disk error"); 4 [1 S' T& @6 j% {. Y, R) X/ [
exit(1);
" `- A% a6 }1 P4 B, H4 Q} </FONT></P>+ v$ N, g8 u3 R, Q9 c' @1 T. ]
<P><FONT color=#0000ff>/* request DOS services to close the file */
2 s# A3 M4 @1 pif (bdosptr(0x10, &amp;blk, 0) == -1) , ?3 A2 [0 J! h) }# l/ _
{
0 V5 i) o2 c+ o' ^( F2 {perror("Error closing file");
) r9 I; I- E4 H' _# Dexit(1);
7 w% @7 E& S* a* e) O} </FONT></P>
% |! n8 e# S; P. [4 h& U<P><FONT color=#0000ff>/* reset the old dta */ 4 D0 _2 v" L  d. |& r3 ~! Q
setdta(save_dta); </FONT></P>  F! p) g8 z& _! t9 |/ ]2 J
<P><FONT color=#0000ff>return 0; % p5 @; l0 w" `" p8 t. u! p
}
& {& m4 r& `" k3 F$ a* P' ~</FONT>
1 m8 [6 ~: [5 v# r8 x) F, A: g8 [</P>8 f' T: E7 q! J
<P><FONT color=#ff0000>函数名: random </FONT>
* Y3 W' V# B/ Z! d功 能: 随机数发生器
+ W0 c- t8 q6 N1 |) R3 e! I$ y7 M用 法: int random(int num); 9 s$ l/ u2 y% G
程序例: </P>
1 E# _. @6 B$ `8 i<P><FONT color=#0000ff>#include <STDLIB.H>
0 S; H/ q6 I$ v/ ~! s: A7 f#include <STDIO.H>3 h+ z1 |+ o& u* }$ j
#include <TIME.H></FONT></P>
1 v9 s8 Q/ @6 }) @7 Q7 [' _<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ ; ~6 G* K1 G3 P* I9 }1 V- H
int main(void)
  ?+ Z5 y) d" c8 c1 `2 ~2 c" q6 i{
; [: _6 k1 v5 x, G' srandomize(); & P$ t6 U' l+ g* k4 |
printf("Random number in the 0-99 range: %d\n", random (100)); 6 M' P3 l" E3 z, L7 J
return 0;
$ Y2 `  l5 l9 j* {, p} $ b6 F/ N5 v3 O% R: R
</FONT>
! }+ P- ~) j" u8 D9 S6 ?( ~</P># w: L/ z5 ?% u/ R6 ?
<P><FONT color=#ff0000>函数名: randomize </FONT>
8 b. W: O0 A8 t功 能: 初始化随机数发生器
5 m5 |& L: C* s( l用 法: void randomize(void);
) D1 a6 d7 u- {, G( B程序例: </P>. x+ c) i. C9 G+ R9 ~! D% p7 x
<P><FONT color=#0000ff>#include <STDLIB.H>
" i* L- `0 |- s: I& A#include <STDIO.H># R7 o, B0 n! m/ K% }7 D
#include <TIME.H></FONT></P>
# \9 Q, Q3 w! |! ]% G8 q<P><FONT color=#0000ff>int main(void)
) g# P9 \, X: i) b- `/ E- r{
. j* D7 W) s# J5 o- E2 z$ _int i; </FONT></P>
  |" e7 }( p- e  |- ~2 o. P<P><FONT color=#0000ff>randomize();
4 z" x% f: W) }3 H. kprintf("Ten random numbers from 0 to 99\n\n");
+ g0 K, B0 A0 U* M" K! gfor(i=0; i&lt;10; i++)
' j% R+ }# U' W+ d- z3 D! g; yprintf("%d\n", rand() % 100); 3 N/ Z' [% ~, q
return 0;
: {2 n5 [: F0 Y" H} 6 K/ E: Z/ {- l& J6 m4 A; J
</FONT>9 e* M; S9 D# k& `3 G) g6 F, A
</P>$ J$ U) E7 S+ z" v# u# c6 o4 }
<P><FONT color=#ff0000>函数名: read </FONT>, x+ c" d3 f% t3 V/ @
功 能: 从文件中读
4 ?" A' d* E5 w2 T/ t用 法: int read(int handle, void *buf, int nbyte); ! r. G, o" y9 Q( C
程序例: </P>
" A; p5 E# n: X7 l<P><FONT color=#0000ff>#include <STDIO.H>
7 O$ ], a, y1 Y3 a#include <IO.H>  U' A3 }+ M' o2 m
#include <ALLOC.H>
5 \0 v: H+ ~; A: K1 I#include <FCNTL.H>
2 F) m" z+ K+ i3 _' b" j+ T#include <PROCESS.H># [" t/ I* I7 R% u/ Y0 H4 X
#include <SYS\STAT.H></FONT></P>& i, X/ V. G4 Y4 N9 j) v1 S
<P><FONT color=#0000ff>int main(void)
3 }8 i$ s5 r& e" Z% ]{
4 Z" r  C% t% M. I% y% Ovoid *buf;   B/ r( k! u; Q* U
int handle, bytes; </FONT></P>' Q( g8 L8 \9 D0 U% t* t# a, n
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>, f2 x) V1 u+ N- [, N
<P><FONT color=#0000ff>/*
, Z) \9 _# K" f' pLooks for a file in the current directory named TEST.$$$ and attempts
  D; N% f6 l) E0 Y8 fto read 10 bytes from it. To use this example you should create the + z3 h* D4 I* x( o( J+ m# m
file TEST.$$$
, O9 K( h1 g8 m& \*/
  K+ O4 h% w: {+ Z$ g" ]if ((handle =
, ^3 p! M2 k/ p2 D/ Fopen("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1) 7 f" H, f2 x$ C
{ ( A# Q/ s( f) J, x. e+ Y! D  |
printf("Error Opening File\n"); / [3 P" u! [- I( ^5 x
exit(1); # d; u$ g& R# X
} </FONT></P>0 I1 {0 z  X+ X( F
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
# t- q9 i+ M; B( m0 `1 X) B" tprintf("Read Failed.\n");
3 ?& V) ]5 u* _$ ]' Q  qexit(1); 9 U6 h! L6 Y, o
}
: P+ r$ N& J9 Helse { 6 v$ R# ^1 e8 f/ f( B
printf("Read: %d bytes read.\n", bytes); & w( }* W  e4 c) t( [; I  I' Y5 U
} * N3 h0 P6 t: J* C5 ]
return 0;
! W, W/ o! |- N' f: E- C3 H8 E} </FONT>
4 M5 X/ I! t. F0 b' {$ i: j3 Y1 x: A
% W' l$ U/ O2 Z</P>
  e3 f5 j1 l* m+ }" E9 x; G: x<P><FONT color=#ff0000>函数名: realloc </FONT>7 ]1 ?* q0 I* e: h4 B& n& g
功 能: 重新分配主存
) [6 j4 x+ j6 @' Q% q; Y$ g0 P# c  d用 法: void *realloc(void *ptr, unsigned newsize); 5 r3 H/ n7 v; z. n2 j
程序例: </P>* a# C9 |- i2 o5 }
<P><FONT color=#0000ff>#include <STDIO.H>
& |, ~! \( Y+ K9 e. R/ |" \#include <ALLOC.H>
5 |6 t" Q" T% `( V; B# p#include <STRING.H></FONT></P>2 W- G1 P$ V, ^6 }3 d
<P><FONT color=#0000ff>int main(void)
% q+ _3 _2 V1 b; n{
# s) \; `+ Y5 y% f2 hchar *str; </FONT></P>
+ i' y3 J* n) T5 j' \2 U. m: v<P><FONT color=#0000ff>/* allocate memory for string */
" B1 S5 R9 K: ^) L, ]/ h8 Jstr = malloc(10); </FONT></P>
3 ~6 k% P' t7 B5 f<P><FONT color=#0000ff>/* copy "Hello" into string */ 4 l: f$ q8 i% i% U8 T+ u$ O
strcpy(str, "Hello"); </FONT></P>. q7 q+ C7 R+ o
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str);
5 p2 {/ y0 w, U7 Xstr = realloc(str, 20); 2 J2 p7 n+ k) r. p5 l+ B+ k$ g; U
printf("String is %s\n New address is %p\n", str, str); </FONT></P>6 C! P/ w; P9 u( y
<P><FONT color=#0000ff>/* free memory */
% z# `+ y8 g# ^3 o7 R: W+ r# _free(str); </FONT></P>' Z. K# F+ q8 y" B3 v( A/ r
<P><FONT color=#0000ff>return 0;
8 L! u0 d; h2 O+ Y5 C7 N; }4 Y- I}
! i6 y; c" A6 P$ M</FONT>2 D- B5 x& u/ E& n7 s8 ~2 i
</P>& S. o, }7 t5 R/ a7 R% X
<P><FONT color=#ff0000>函数名: rectangle </FONT>  w" ^$ O  E+ ]. Y6 Y
功 能: 画一个矩形
0 r& p7 A5 N/ N/ Y( z: L用 法: void far rectangle(int left, int top, int right, int bottom);
" u+ f3 p& o  Z3 g& C/ \程序例: </P>8 I9 q5 w+ j' q5 n
<P><FONT color=#0000ff>#include <GRAPHICS.H>4 H3 `( H! f: }2 P$ b* u% D
#include <STDLIB.H>& b# E; {0 v2 q# p; R3 ~( r  n5 i. j; K
#include <STDIO.H>
& }% l. }. Y/ p1 l1 v#include <CONIO.H></FONT></P>
. @; Q. V5 o. E) h2 [7 K1 g  E<P><FONT color=#0000ff>int main(void)
$ u2 t$ B3 y: e( G# F  u{
$ \1 P0 f, b0 {0 V5 G1 v/* request auto detection */ ; |% h1 v$ Z: L2 x; v- U' V$ t; _
int gdriver = DETECT, gmode, errorcode; 4 U5 V) A8 {$ [% x) V! w" e" V! Z5 R$ i
int left, top, right, bottom; </FONT></P>
/ |3 I# {  {6 ?9 j+ Y2 Z<P><FONT color=#0000ff>/* initialize graphics and local variables */
. c/ ~% W8 N% i, W* g/ w$ j0 xinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>  p( J# {% k/ t3 _
<P><FONT color=#0000ff>/* read result of initialization */ 4 l4 ]( M, E9 \* Y
errorcode = graphresult();
4 q- i1 w& I! f. L# m# v. i8 B( Kif (errorcode != grOk) /* an error occurred */ 9 T; U' D3 s3 M' H( g& h8 W
{
) |4 u- b( \: D2 bprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ' A: `8 F5 ~5 h
printf("Press any key to halt:");
% R1 j% T" C. k; q; |" ^4 o7 Igetch(); ! O/ V2 r& E* P8 n
exit(1); /* terminate with an error code */
! [1 ]: j8 t' X! ]) B% R0 E} </FONT></P>
: ^+ I& X6 `5 V& M% V<P><FONT color=#0000ff>left = getmaxx() / 2 - 50;
; G, I2 u# D' h% ?( W- ?( g8 N1 ftop = getmaxy() / 2 - 50; 3 `1 Z! Q) ~' \; x/ \
right = getmaxx() / 2 + 50;
( a" a5 [( K+ B- L- z$ v3 }bottom = getmaxy() / 2 + 50; </FONT></P>) W+ B) ?0 ~% i* n% ]+ K
<P><FONT color=#0000ff>/* draw a rectangle */   C$ Q3 N- O3 j
rectangle(left,top,right,bottom); </FONT></P>& F, x1 B# {" L% m  @. N
<P><FONT color=#0000ff>/* clean up */
5 P: J- F: t% N1 T9 ygetch(); % s9 c* p. ^/ @' \
closegraph();
8 O; Y6 w) R. i1 ]6 Lreturn 0; 5 z7 @! w. t1 x" u& h
}
8 Q0 W: F0 v* C! X# m' n; E) O1 Z</FONT>& U: R- T8 c' S- B  D
</P>
/ S8 V$ P$ i. u5 k<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>
( b  L8 x; e# ?+ b功 能: 登录已连接进来的图形驱动程序代码 0 G% x8 j3 F' u# A4 x. i- n  B0 ?7 W
用 法: int registerbgidriver(void(*driver)(void));
* \/ h/ z1 }) |' K8 G+ M1 ]程序例: </P>' Y( m7 y! S8 T2 n
<P><FONT color=#0000ff>#include <GRAPHICS.H>
0 m1 N; _- y6 Q( n8 s8 u#include <STDLIB.H>: E! `: @8 E2 v' B* v
#include <STDIO.H>
1 X- b* r0 a/ W- B: R( n0 N* L#include <CONIO.H></FONT></P>. H6 t& A3 M7 s2 D) L  l0 E
<P><FONT color=#0000ff>int main(void) - T/ p* @+ [2 k1 w* h
{
8 e. o5 f$ N5 I' q9 K/* request auto detection */ % w; u. u- [: s3 e/ B  S" p$ a
int gdriver = DETECT, gmode, errorcode; </FONT></P>
& G( N, u$ Z% w* k; Q4 H+ r* j<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ & P: M/ G  B9 Z" N* M. i
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>5 n- k5 n# M" k: w& S
<P><FONT color=#0000ff>/* report any registration errors */ & o1 `  G! }" R8 z" f7 _$ b* t( E
if (errorcode &lt; 0)
% o+ U+ G/ ~' s4 _- M" @$ X, L{ 7 U, {& D* `! F; N3 w8 [
printf("Graphics error: %s\n", grapherrormsg(errorcode));
7 J5 t  K, r  t  pprintf("Press any key to halt:");
, p" x- i% ~4 L- M3 p6 u, ~getch(); 3 k3 c. `" @: X; T
exit(1); /* terminate with an error code */ 7 q0 m0 x" W. C5 `
} </FONT></P>, ~6 c5 S! O; ~7 P7 F1 C
<P><FONT color=#0000ff>/* initialize graphics and local variables */
; C, w" @+ K& _2 Rinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
' I5 ?* u. d' `0 e- t$ A<P><FONT color=#0000ff>/* read result of initialization */ ( v3 y8 a0 C5 l
errorcode = graphresult();
. n& ]! J# |- ?/ r8 ?if (errorcode != grOk) /* an error occurred */ # m& s: U7 y. e' [6 x& A
{
+ D6 J& o" [& g8 Z+ vprintf("Graphics error: %s\n", grapherrormsg(errorcode)); + f: J2 v' s' K( }, c1 M7 M* W
printf("Press any key to halt:"); , Q5 U3 b( u  l0 I& U" e7 g
getch();
1 j3 W) i  o: z" h( `exit(1); /* terminate with an error code */
4 n3 |7 N. P8 J' J5 a} </FONT></P>+ d2 G- R' |1 H/ s
<P><FONT color=#0000ff>/* draw a line */ ( g9 v6 Z8 {2 p
line(0, 0, getmaxx(), getmaxy()); </FONT></P>) i# p0 |5 Q2 {. X
<P><FONT color=#0000ff>/* clean up */ / Q- J8 j  Z. X
getch();
: b$ X5 [! Y! H/ E6 H1 ~6 a( s+ wclosegraph(); $ `: N+ N$ Q+ n+ e
return 0; 3 Q, K& d& ?) H& _0 K; D0 O
}
$ r( o6 Z! T! ?" {; x. c</FONT>
; r( }/ D" }' Y3 q* F; ^</P>9 U$ `/ D, f1 ^
<P><FONT color=#ff0000>函数名: remove </FONT>! V( ]3 q' x3 I% L- Q% u6 f
功 能: 删除一个文件
; h4 r6 p" M' y% Q+ i: X4 _用 法: int remove(char *filename);
! |4 d' N( O: H5 v3 V程序例: </P>0 f$ X1 Q7 o' q9 s* h
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
9 F8 P. @, i  d4 i2 m<P><FONT color=#0000ff>int main(void)
9 i( k3 d, u: m5 w{ 3 h8 P7 j1 {7 H# ]" ^3 h
char file[80]; </FONT></P>" |4 o/ N+ @+ S% F, d, D2 J
<P><FONT color=#0000ff>/* prompt for file name to delete */
4 J# p" {, D2 l% \0 g: e3 p* Eprintf("File to delete: ");
, _1 ]2 o6 ^. E/ R  g. Tgets(file); </FONT></P>* U6 C, A. {3 R) [( b
<P><FONT color=#0000ff>/* delete the file */
% e  n1 j; N" R( ]if (remove(file) == 0)
1 L7 ]/ U8 l3 h- gprintf("Removed %s.\n",file); 2 g$ g; w& i; Q+ e
else
) |3 O: L8 a7 t: D1 ]5 D. ^perror("remove"); </FONT></P>2 w4 j# D9 T* N3 M  S
<P><FONT color=#0000ff>return 0;
$ E$ N& o8 S" e4 a* x4 X} ( Z. P2 X( D6 `" G# F* r) W% m0 K
2 f8 d' x% o" `0 n
</FONT></P>% h7 E4 x2 U$ g/ d$ U5 F9 i9 u' u/ O
<P><FONT color=#ff0000>函数名: rename </FONT>
4 N9 y3 W0 U# J  E3 n; L+ g. \功 能: 重命名文件 6 m, Q) b+ k9 Y+ ]. O
用 法: int rename(char *oldname, char *newname); & h7 N( g4 W! |% o7 q  p
程序例: </P>
7 L$ a1 E5 {0 F5 c& f) T9 |<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>9 O/ b% k. e  e  u' y( m8 H" s
<P><FONT color=#0000ff>int main(void)
$ }/ B$ j6 D" n+ t$ n{ : u# q0 x  W5 v* o
char oldname[80], newname[80]; </FONT></P>
- E! [7 @3 }) O1 X# V: l<P><FONT color=#0000ff>/* prompt for file to rename and new name */ 6 |1 Z# B- V3 V' j. T6 C8 l
printf("File to rename: ");
* N. ~$ `# P  ngets(oldname); 3 K, d% C6 I7 u
printf("New name: ");
$ s9 N; \" |& i2 H: O+ j& `8 v/ [gets(newname); </FONT></P>4 |: ]# P! J3 x/ R: g0 y
<P><FONT color=#0000ff>/* Rename the file */
6 g' m1 K3 I" W5 {8 bif (rename(oldname, newname) == 0) ) v+ m/ S% Q3 _' g$ V% @) R
printf("Renamed %s to %s.\n", oldname, newname); ) w7 C& {( \4 v% k, \. k( E
else
9 Y) O: ]# s2 }' aperror("rename"); </FONT></P>0 p; \7 b6 x& I* X: j1 I
<P><FONT color=#0000ff>return 0; 6 i. ^1 B) _6 U
} / U8 f) S( |( E
8 Q6 ?7 _1 t: c0 M4 Z9 o# Y
</FONT></P>8 J& B. ^& r/ ~8 N
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
8 S- ^3 Z/ P/ Q4 A5 d: A功 能: 将屏幕模式恢复为先前的imitgraph设置 8 y& b6 L. d' Y2 c2 D5 T
用 法: void far restorecrtmode(void); : ]; [3 i( x. `* K
程序例: </P>$ l0 v, B2 F/ q6 `. d
<P><FONT color=#0000ff>#include <GRAPHICS.H>
  u! h4 d2 d2 M9 k6 ^1 C" J#include <STDLIB.H>4 G: }5 G% v" b+ K: c; E
#include <STDIO.H>! j6 C+ h+ {7 N. A6 {! I
#include <CONIO.H></FONT></P>
; o$ y5 X' y# K( m9 s& _7 J<P><FONT color=#0000ff>int main(void) * _1 n% C( s% C. b, H- t* d$ B  `1 s; n
{
0 Y# |4 s$ ^0 e5 Q/* request auto detection */ " W$ {3 Q# ?2 H. `6 D/ z1 b! Z$ C
int gdriver = DETECT, gmode, errorcode;
5 O, s0 W! S3 g+ i7 s# X# m9 ]' m  p' _int x, y; </FONT></P>* B: _4 |/ U, L3 E0 j- K9 S
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ( n: G/ R' S; N
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
/ K  R3 n# X" y& [$ R  n+ [<P><FONT color=#0000ff>/* read result of initialization */
& z# w8 e) p3 s' Q# h" B7 l6 Terrorcode = graphresult();
# B; `0 e3 T9 I1 p0 |& ]4 Uif (errorcode != grOk) /* an error occurred */
8 h: Y1 I) W' k. p{
9 y! ]7 q2 k5 R4 n# b% {  Vprintf("Graphics error: %s\n", grapherrormsg(errorcode));
8 f" E! l- z* Q' C1 Xprintf("Press any key to halt:");
4 p& ^5 }/ d+ }9 Q% p5 ]getch();
( h! `2 M/ d1 [8 O5 l/ A! m4 Uexit(1); /* terminate with an error code */
, ?- M6 R' j0 K/ Z: G! A} </FONT></P>4 c# E1 Q, G3 T9 s5 e& i
<P><FONT color=#0000ff>x = getmaxx() / 2;
$ v' Q/ l2 [; B) ?9 Cy = getmaxy() / 2; </FONT></P>! \! ^: C* i1 a7 v9 p" s( F/ W7 P
<P><FONT color=#0000ff>/* output a message */
0 [3 t5 g) i( _! f! Ysettextjustify(CENTER_TEXT, CENTER_TEXT);
3 s" e7 Y  Q2 X& d9 S4 d; _& C% Youttextxy(x, y, "Press any key to exit graphics:"); , w+ k) V5 u/ ~
getch(); </FONT></P>
1 X$ H" M6 }0 r& A; d& }! t<P><FONT color=#0000ff>/* restore system to text mode */ * R* E. l) c) S) F7 q6 @
restorecrtmode();
7 L$ ^% v2 A" K7 R/ Uprintf("We're now in text mode.\n"); ; e$ K$ G5 \$ }& p
printf("Press any key to return to graphics mode:");   J, j( P: Z3 i; \2 c
getch(); </FONT></P>
7 d* t$ N* h6 d3 D<P><FONT color=#0000ff>/* return to graphics mode */ ! V  c4 t  S2 w( {: N3 \# k) R" W
setgraphmode(getgraphmode()); </FONT></P>
- _3 P; ^! K6 K<P><FONT color=#0000ff>/* output a message */
2 }0 T$ j2 q2 ?" U+ Q2 k* Ysettextjustify(CENTER_TEXT, CENTER_TEXT);
, z. @" i" N3 ]6 }) q( b; souttextxy(x, y, "We're back in graphics mode."); ' E- S) b, j  b3 a: J$ `& T& }
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>' t' B$ q: D/ k# H# [. z4 s) S
<P><FONT color=#0000ff>/* clean up */
5 M" U. L, c4 O  Jgetch(); 2 m; ^  s5 F9 h) ]! d
closegraph();
8 l' d1 m; v! f7 ~! O! \# ]0 Ureturn 0; 9 c: b5 e. _0 s+ ]( }; M6 k: W
}
0 I/ [' c& |& d* }3 f</FONT>
) E7 o# z2 M% x5 x/ d% z</P>! ?9 P; w5 C8 p5 ]  N" J% }
<P><FONT color=#ff0000>函数名: rewind </FONT>7 D8 t! T+ {/ B0 ?; ?
功 能: 将文件指针重新指向一个流的开头
/ s; m, m( N2 x; }2 \8 ^- w用 法: int rewind(FILE *stream); & c- F. R. @5 a" H3 s
程序例: </P>( r6 u  k" d) z" u( z7 z2 [* k. O
<P><FONT color=#0000ff>#include <STDIO.H>  R3 S8 x" ^' b' z: g3 }3 N
#include <DIR.H></FONT></P>1 p  x* y) y1 V7 j  t5 O* a
<P><FONT color=#0000ff>int main(void)
! \  N4 |5 B) A. E{ # J4 m+ @9 E  ~# N" }
FILE *fp;
, L& f! T! f# L' nchar *fname = "TXXXXXX", *newname, first; </FONT></P>
8 L8 s( G4 {/ e" f7 x: K<P><FONT color=#0000ff>newname = mktemp(fname);
! ]/ v, `/ c4 U4 I. Xfp = fopen(newname,"w+"); " J, O9 {; T' n7 d
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
5 _% I1 ]# n! L, y) irewind(fp);
; R$ u+ x  C( C4 `5 X7 [fscanf(fp,"%c",&amp;first);
% V1 R' x9 e) b/ L! xprintf("The first character is: %c\n",first);
: h1 y; L8 D  }; z+ ?fclose(fp);   Z% L3 e3 m9 |' t) N, |/ N; t
remove(newname); </FONT></P>2 }- L- U4 C+ c+ O0 B: H3 A+ Q
<P><FONT color=#0000ff>return 0; 3 k3 E9 d" d5 W' Z  T3 o% i" B, z
}
& r9 [: L/ c0 \+ j; D5 y</FONT>
- b7 a! @$ {/ ~- B& x</P>
/ k. t. {; X! y7 t9 z. B# N<P><FONT color=#ff0000>函数名: rmdir </FONT>
* A( W  i' f; L% j9 H% c功 能: 删除DOS文件目录 ' g+ n% O8 B& f6 Q: ~
用 法: int rmdir(char *stream); 3 P2 R0 o. k  T' a* T
程序例: </P># Z7 H; J5 b: S7 L: _0 A( x, L: T
<P><FONT color=#0000ff>#include <STDIO.H>
; c8 V4 T& V. L! _3 B5 k#include <CONIO.H>
& D$ u' O' P* ]% \% c9 u* E6 H#include <PROCESS.H>' x: ^& d" J% e  N' }
#include <DIR.H></FONT></P>' d' X1 J, a" O- q& o5 h
<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>
% B( Q: i+ z6 Y; g9 X<P><FONT color=#0000ff>int main(void) 4 x: D$ f  o( }/ N4 n0 b
{ ) P/ k+ h$ ?: b' O  _
int stat; </FONT></P>0 w9 c( N$ K5 A1 ^# B5 W. n) _: i
<P><FONT color=#0000ff>stat = mkdir(DIRNAME);
0 Y8 z+ Z2 i6 Lif (!stat) 8 z. z* E' k* u
printf("Directory created\n"); 6 M- E: ~/ a8 ?5 V! f; `
else ( w9 a, F8 m5 @0 b
{ - Z2 ~& S" A! s( ?+ `
printf("Unable to create directory\n");
' G; C. @% W- ~' H- N2 ~$ i9 iexit(1); / J% e* M2 e/ K/ H1 b" u
} </FONT></P>
/ P7 E/ J, Z9 y7 A( g7 k% C<P><FONT color=#0000ff>getch(); # \7 n9 ~1 z. h( L
system("dir/p"); 9 \6 K2 B7 i, u9 G; p% h3 v+ j
getch(); </FONT></P>& \& ~! N3 ~. z1 ?( u
<P><FONT color=#0000ff>stat = rmdir(DIRNAME); & g9 J5 |3 _+ I/ _8 Z
if (!stat)
9 ?& u1 r7 Y$ o$ {; kprintf("\nDirectory deleted\n");
% }- m& ^' F& F0 kelse
) F( F2 S; w7 H9 a. k! B$ g+ _) ?{
" ]) t: w" U9 o2 h3 G1 Sperror("\nUnable to delete directory\n");
; l+ ~8 z5 D: z5 v8 ]+ Eexit(1); ' y5 a2 I( x8 C) d2 h! M
} </FONT></P>
; R8 |; u: y  O* E+ W$ J5 D) x<P><FONT color=#0000ff>return 0;
. E$ _( x9 B& i} </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 05:08 , Processed in 0.619245 second(s), 52 queries .

回顶部