QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
4 T! R- T' t/ c$ N0 P" O. B" p功 能: 使用快速排序例程进行排序 , T8 I' ]0 v0 t, g5 x. g+ _, I
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
' w/ D" y. `! i: x9 C) T; d  c$ G7 q+ @程序例: </P>
& q6 c# c0 Y- P* G. }<><FONT color=#0000ff>#include <STDIO.H>
4 ?( |! Y9 j, \8 T2 V#include <STDLIB.H>
3 M' g: g1 ~0 f. Z  b/ ^! g1 ~. F#include <STRING.H></FONT></P>
0 D8 |5 N& H* @! a) z( d<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>+ N: u6 p$ i2 \
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; / j) B! M' T- y3 G
</FONT></P>) P! Z: E7 a# l) z% q; k4 w1 U3 B# @
<><FONT color=#0000ff>int main(void)
' a1 k/ g/ o$ z' W0 }{ 5 K+ n+ h' x$ O( Q4 l
int x; </FONT></P>
5 v, @+ `5 r" Q. o8 q<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
1 X4 D, }* C# w9 c! _- Sfor (x = 0; x &lt; 5; x++) $ k' B9 I5 B7 `# c! b1 N
printf("%s\n", list[x]);
, ~' r7 ?* s- s" b1 v9 Treturn 0; " C8 s" Y0 p. t1 m4 D
} </FONT></P>1 P" H4 Y3 e; y) N; u
<><FONT color=#0000ff>int sort_function( const void *a, const void *b) 5 }3 L8 h" o1 L5 }
{
% D* y& v! E9 g. l# P- Kreturn( strcmp(a,b) );
1 o/ z/ L6 g* a$ {3 j1 V# A} </FONT>
5 L$ u- _0 [: T2 o</P># L  p& z# b8 e% j8 c) F

) K7 e7 D2 k9 [
% g6 D2 M/ t" R( V# t5 L5 P<p>
3 S8 [6 p! h/ O: F1 P( E, z- k+ j<><FONT color=#ff0000>函数名: qsort </FONT>. x7 T0 @2 @1 j/ b8 @  B- [
功 能: 使用快速排序例程进行排序 ! `! B, E/ ?" _) {- R2 z( O
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); 5 p: G5 V8 F+ B, I7 K) g
程序例: </P>
: b5 u" d$ s7 X4 N0 t& M! s<><FONT color=#0000ff>#include <STDIO.H>
8 C6 T# V$ G3 U5 }% Y#include <STDLIB.H>1 }  d% A# i2 ]$ R/ E1 T6 P7 q8 W
#include <STRING.H></FONT></P>
* h; ]* P; [, Q# ^0 {  J9 i<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
3 W3 ~& g; Y: t<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
7 S+ q6 |6 x: {' {* O; M- ?</FONT></P>1 [6 N- z7 }- b
<><FONT color=#0000ff>int main(void) 5 H1 r3 P6 L# \4 |- \
{ 9 K$ V6 Y/ R# x
int x; </FONT></P>+ M2 P3 ]8 F3 z. t3 {0 _$ t+ @% |
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 4 F" F: E  ~+ ?1 S0 p7 A+ U
for (x = 0; x &lt; 5; x++) ( C0 o# b: g4 E. B+ ^
printf("%s\n", list[x]);
4 N- _$ v3 y! w' m. k" L: U0 kreturn 0; ; C% f% E; x7 O0 @
} </FONT></P>" @& R' m- G; _7 c$ x
<><FONT color=#0000ff>int sort_function( const void *a, const void *b) + r4 t: q$ {9 [+ p' P' o! W' ^
{
! O- G' h. q7 Q, {, Qreturn( strcmp(a,b) );
* A! T, c2 R' @}</FONT></P>! b% q2 L, R3 @9 Y) C
<><FONT color=#0000ff>
" g: V$ I9 {4 v* z2 u</FONT>! T# T& i) A' \; ~( F$ \
</P>: @1 l+ u# ~6 P6 a' l) _
<><FONT color=#ff0000>函数名: raise </FONT>
1 v+ W- j# u& k7 f* G# w8 Q功 能: 向正在执行的程序发送一个信号
$ L8 \% s5 v4 T6 u用 法: int raise(int sig); - n2 W/ l8 f' S4 K& \6 B
程序例: </P>
) ~+ U9 Z  ?* [( d% R& g# o6 }<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
5 w- U. d3 ^0 w" }<><FONT color=#0000ff>int main(void)
- `3 Q6 P3 \; ]+ c7 `{ ! q  s% q1 G8 b, X8 g8 P6 E  ?, O
int a, b; </FONT></P>0 L9 }7 n1 C$ x3 y2 T1 [
<><FONT color=#0000ff>a = 10;
( g# K$ p% l1 v+ S0 G7 vb = 0;
6 Y8 R) |6 J2 O# ]' f0 @) mif (b == 0)
1 h- S; V( [1 W5 p+ H, C/* preempt divide by zero error */ $ p4 @) p" Y! Y4 F, v# N7 ?  X
raise(SIGFPE);   j  u& ]6 k, A0 z
a = a / b; 4 y, w; S1 n. n  t$ _
return 0;
2 |6 n4 G8 @7 U1 \2 n, `9 |9 R} 5 F9 Q7 p/ u' P% y7 d; U2 S
</FONT>
( Q1 \8 O# w& m  r, T: }. d</P>+ Y! S8 q+ X: r
<><FONT color=#ff0000>函数名: rand </FONT>3 E7 @% y4 d+ J' u9 y
功 能: 随机数发生器 ' q2 r7 w: e5 m) y
用 法: void rand(void); / a0 v1 O) K6 z( H% \( k
程序例: </P>
5 Q0 B, J" _5 x5 u2 _# d1 ~<><FONT color=#0000ff>#include <STDLIB.H>2 N' m4 P- d3 M' Y7 a8 ~* e
#include <STDIO.H></FONT></P>7 K2 f3 F! j* E# B0 M7 D# }  W3 a
<><FONT color=#0000ff>int main(void)
* f# M2 i, J, t! {  Z1 E4 v{ # W  [8 T4 b  W
int i; </FONT></P>( V" l5 |  p4 V: Y3 l9 a
<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n");
9 R+ B. L. U* gfor(i=0; i&lt;10; i++) + h+ }7 R0 ]$ G9 K( ~. m8 e2 Q! a
printf("%d\n", rand() % 100); 3 A3 w& M* V! x- C
return 0;
) j( r' K3 }2 z* A! j. z! A% _3 j}
) h; T3 `, m6 R1 b& Z
5 t$ d/ a) e5 |8 M8 O1 I- M</FONT></P>: m1 Y6 S0 J6 O- ^; z% `
<><FONT color=#ff0000>函数名: randbrd </FONT>( U1 A+ e0 s8 X
功 能: 随机块读
* `0 @) D3 ~7 P8 A# p用 法: int randbrd(struct fcb *fcbptr, int reccnt); 8 q, j, w" [0 r  z
程序例: </P>$ `$ z9 j" `* N3 ~( o
<><FONT color=#0000ff>#include <ROCESS.H>3 N5 J, D0 \% x7 W0 s  I) P" e
#include <STRING.H>6 U: S5 K& R" ]) p) G0 c1 c% z
#include <STDIO.H>
4 s. _/ j1 Y  P- e6 M- L- a* W#include <DOS.H></FONT></P>
- m( y" i8 ]4 r# X  ^5 @2 l+ |<><FONT color=#0000ff>int main(void)
7 Y8 w/ t/ ?5 u) T. _/ |4 w{ 1 D+ i# S0 U& x+ t
char far *save_dta;
( x) |- g6 _* p. |0 H0 Achar line[80], buffer[256];
. O" H9 n& P' q5 w$ H( ~1 Fstruct fcb blk;
% N% H" L) w5 Pint i, result; </FONT></P>. i8 T- h1 p% f2 F( {2 Z" |
<><FONT color=#0000ff>/* get user input file name for dta */
2 N' K, M6 k$ Z3 l9 z( j! Z2 G+ T. `; Gprintf("Enter drive and file name (no path - i.e. a:file.dat)\n"); # H, `# m% Z9 k
gets(line); </FONT></P>% |; F6 N% e5 }
<><FONT color=#0000ff>/* put file name in fcb */ 9 c; m* s; d. n2 c0 z6 x, I2 W
if (!parsfnm(line, &amp;blk, 1))
; c8 H! H9 J- f+ Y+ ?{ 6 P3 A, P" G" N8 ]# Q, I
printf("Error in call to parsfnm\n");
+ S! W+ d8 g! b' `/ Eexit(1); ; t* K  _; m( Z/ d3 X
}
. Y4 m' h4 K, s$ Jprintf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>& K$ }8 W" S! A2 L) t( ]6 Q
<><FONT color=#0000ff>/* open file with DOS FCB open file */ 6 T6 q0 n7 B/ Z6 O+ W+ @7 ^5 v
bdosptr(0x0F, &amp;blk, 0); </FONT></P>& f# p, H+ |8 ~
<P><FONT color=#0000ff>/* save old dta, and set new one */
" a" O0 j* X/ I7 Y# ^save_dta = getdta(); 8 [1 U/ h  v- a
setdta(buffer); </FONT></P>
: C3 H# l5 [7 i0 p<P><FONT color=#0000ff>/* set up info for the new dta */ # {7 e& V# t0 ~. Z9 O2 K# S' x
blk.fcb_recsize = 128;
5 q5 ?4 }, [0 w; Qblk.fcb_random = 0L;
: x# K+ _2 z0 J/ ]* b8 tresult = randbrd(&amp;blk, 1); </FONT></P>6 y0 f, U; k9 V: ?
<P><FONT color=#0000ff>/* check results from randbrd */
9 R' f/ V- t( p; E/ y1 }if (!result)
* h* s& C% B  r0 bprintf("Read OK\n\n");
6 N% p' ^- @2 ielse & w6 _0 I4 ~; e# ~) E
{ ! ]8 ~% F* N9 Y! l# _1 Z
perror("Error during read"); 5 v! b5 F' O! w! ^
exit(1); 4 i- v: Y; ?  T; A+ O
} </FONT></P>+ b7 Q3 Q% d  ?  U# p: _! y, R
<P><FONT color=#0000ff>/* read in data from the new dta */ ; P' n) i* T% W& Q$ S1 g, v& t
printf("The first 128 characters are:\n");
7 Q, I6 H$ a+ v6 K0 w$ a0 s: ~for (i=0; i&lt;128; i++) 7 v* t- [. N" g9 ~8 Q4 h. e6 P
putchar(buffer); </FONT></P>" o% f2 ^; \( v4 c. F4 ~2 ^
<P><FONT color=#0000ff>/* restore previous dta */
" P# J+ o- D2 D2 u. Z: d; Isetdta(save_dta); </FONT></P>- Z. X- K. Q) _. @' x
<P><FONT color=#0000ff>return 0;
, U8 D( i+ D+ ^' I} </FONT>
' p) n  h* `; Q</P>
0 Q3 a* L8 W) v1 m- k2 x9 ~: Q<P><FONT color=#ff0000>函数名: randbwr </FONT>
3 w. L! \" p' H7 D% m功 能: 随机块写 7 p' F( l3 O) c3 v, F
用 法: int randbwr(struct fcp *fcbptr, int reccnt); , y. P) U0 J+ k' D$ H
程序例: </P>6 ^7 h5 L0 t* R& `% U/ I- `
<P><FONT color=#0000ff>#include <PROCESS.H>
3 k0 j: Z& a. D! p* I9 S#include <STRING.H>, {3 j% V1 D$ K3 H3 z, [
#include <STDIO.H>( \0 e9 A4 C# R6 Y8 |
#include <DOS.H></FONT></P>* a$ \: S& P% l5 G" Z  z) v
<P><FONT color=#0000ff>int main(void) % ^: t5 v% R3 }/ `
{
2 b8 P7 r! [0 b3 h  z1 Xchar far *save_dta; 4 S) F" g  }& [. T; `( |, |
char line[80]; % ^' P' j* s# O& p! ^
char buffer[256] = "RANDBWR test!"; ( q8 j6 W: c) E. T  o
struct fcb blk; 8 v" X' ]3 M) ]0 N' q! z
int result; </FONT></P>( \  F4 K5 q7 y( m
<P><FONT color=#0000ff>/* get new file name from user */ 9 g% ^& P0 K5 c& R$ r" L5 B
printf("Enter a file name to create (no path - ie. a:file.dat\n");
+ k' D5 S' P% ]gets(line); </FONT></P>
; K) f$ W3 t+ p* W  N6 V! b1 {<P><FONT color=#0000ff>/* parse the new file name to the dta */
& j6 }) @8 ]6 X' Jparsfnm(line,&amp;blk,1); ' u; L! L/ N# g4 [$ {
printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>9 x4 Y' S3 ?3 F' Z3 U4 c4 ]
<P><FONT color=#0000ff>/* request DOS services to create file */
# T. A( z$ ^( W5 |" G8 Mif (bdosptr(0x16, &amp;blk, 0) == -1)
1 |- }( o- q+ C5 W, [' V{
) J9 z. u+ ?( t3 I8 Dperror("Error creating file"); 3 b, V2 q* v# b5 b; E/ u  m
exit(1); * K- U- d9 g! x4 z% ?' O& [. n1 e
} </FONT></P>  ]; Q0 i0 ~% w( L: V4 {8 k& [
<P><FONT color=#0000ff>/* save old dta and set new dta */
7 E: S! X: K1 h. I# asave_dta = getdta(); 5 g) |9 t1 T  o( w; o; t9 c
setdta(buffer); </FONT></P>
* ^: y) p! E1 _0 M+ \) T- z<P><FONT color=#0000ff>/* write new records */
( L& o, x/ ^- q4 t9 E- Y7 v1 @! v# Yblk.fcb_recsize = 256; " s0 N" I( B! T9 M" B
blk.fcb_random = 0L; ' y. y) {* y4 h6 B" [0 ~% V
result = randbwr(&amp;blk, 1); </FONT></P>
% {7 Y1 s; k5 e<P><FONT color=#0000ff>if (!result) ) D7 Q) {$ q" g+ q3 q
printf("Write OK\n");
6 @( j: m  w, D7 i! y, d& r$ z! oelse % \- R+ k- [: |5 {
{
( U' r$ N0 c" _2 ~. I) \2 Q1 n, xperror("Disk error"); : ?/ V, e& M$ {0 |" n+ ~
exit(1);
! e1 ]* c4 Z" ^2 k  b} </FONT></P>
' N, r8 h" W! F5 q) c6 H1 z<P><FONT color=#0000ff>/* request DOS services to close the file */
/ r( @# Y1 z5 kif (bdosptr(0x10, &amp;blk, 0) == -1) " j- \5 q7 r) c; l4 I9 a" G
{ # n' a& @" }1 q/ Z# G
perror("Error closing file");
' [% Y$ R$ S# B- M, dexit(1); & _# o' y. T* w
} </FONT></P>& w7 e2 O7 I8 m+ |" O0 T7 e
<P><FONT color=#0000ff>/* reset the old dta */
" \+ G0 F" ~' {0 G/ _0 g3 hsetdta(save_dta); </FONT></P>
" W+ o0 k. G3 w5 {# A% M# ^<P><FONT color=#0000ff>return 0; . L! }1 Z# E1 a
} 7 x- j' _3 C& s3 r0 S( L3 v
</FONT>% V8 P8 e* \  s( X( e
</P>
6 K' r& u( p1 X<P><FONT color=#ff0000>函数名: random </FONT>
; z" M4 k) F3 P( w  m  t功 能: 随机数发生器
9 S' \# s7 @' H* H用 法: int random(int num); 7 `* k% e+ K% L! [% {2 ?1 L6 I
程序例: </P>
% u9 r) R! H" ^! v- n<P><FONT color=#0000ff>#include <STDLIB.H>* S- e+ u" `: p( }
#include <STDIO.H>
2 v' x8 O1 Y+ r  ~9 @* z% ?#include <TIME.H></FONT></P>
9 a& }3 c5 B2 b  Q) U<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ 7 \: t. |0 A) c- C
int main(void)
  @# Z: V) u/ {7 ]2 T" @- S2 J{ . j9 {! p2 I+ X1 v' |* s  L
randomize();
1 f! [5 O9 ^3 u( Aprintf("Random number in the 0-99 range: %d\n", random (100));
, G( e8 L! s- K) U3 \return 0; # h: G; j: f# B; _: j: G
} 2 w5 B% i! C6 i- b2 A1 W# Z& B
</FONT>
! ?  X. i8 w* J- P( ~: j</P>
- s8 a, K4 u6 s! r! G<P><FONT color=#ff0000>函数名: randomize </FONT>4 j  A5 c" \" P. H4 y
功 能: 初始化随机数发生器
7 A$ x' {1 A' ^用 法: void randomize(void); ) i! {* F5 F, y4 |
程序例: </P>
" H9 h1 _' n2 \<P><FONT color=#0000ff>#include <STDLIB.H>  B( [5 v: x+ |' \
#include <STDIO.H>
, n! v5 u! R) x3 G- m) X#include <TIME.H></FONT></P>
2 H1 G1 t( f+ S! h<P><FONT color=#0000ff>int main(void)
2 C4 f# v$ e3 z9 w) e" S{
6 }; w9 `# K5 [7 s2 \  Yint i; </FONT></P>
# b- i- m: R9 D+ V% z<P><FONT color=#0000ff>randomize(); ( y! A: A# G, T5 f; ~4 c7 W" L' v
printf("Ten random numbers from 0 to 99\n\n");
. o' p8 y3 k! q! J: cfor(i=0; i&lt;10; i++) - @, p: y( j# ]8 s# X1 H
printf("%d\n", rand() % 100); ( |1 O2 J3 o( W$ @. _  s& |$ j
return 0; 6 K- k; W7 ]5 }: p8 z7 ^& V1 ]6 I/ e2 ]
}
) I; }8 \6 B5 l, B5 D6 E  _" I1 w</FONT>3 q* W0 v; g4 X: W
</P>
, ~. N: g0 G  g+ i* e7 B6 V<P><FONT color=#ff0000>函数名: read </FONT>
8 V: P7 i9 z$ T" T$ X2 N, v功 能: 从文件中读 ! L; o0 U5 P  x- F, x* q6 h' ?
用 法: int read(int handle, void *buf, int nbyte); 7 {, D  g* s6 ~% {, m
程序例: </P>9 J/ U; `& e; F
<P><FONT color=#0000ff>#include <STDIO.H>
/ x% c: A( R" d0 S#include <IO.H>3 j, n+ p  f& C6 j" i
#include <ALLOC.H>. v& l! d3 s$ K# y* d8 o
#include <FCNTL.H>
1 C% y7 m+ I- s#include <PROCESS.H>
$ M! b* x3 V8 _# v' `#include <SYS\STAT.H></FONT></P>
) m0 A' Z. f3 E<P><FONT color=#0000ff>int main(void) " V* W4 z4 E; H8 D$ o
{ 4 p+ U. ~7 [+ `
void *buf;
8 V2 C5 B, m- r; X: }int handle, bytes; </FONT></P>1 Q$ I. A2 D2 \' b% r
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
: B. R* k+ }! f7 |% O5 D<P><FONT color=#0000ff>/* 6 M, |, ]2 q* w( t: u# L: I
Looks for a file in the current directory named TEST.$$$ and attempts
0 @3 m2 E3 K3 cto read 10 bytes from it. To use this example you should create the 2 k3 B, p4 l( o( d( o9 [9 J0 V
file TEST.$$$ 0 v3 N* S# n5 a: o+ Y
*/ : R5 h, M. J" d
if ((handle = 9 `$ d0 C1 O0 n( D! Y
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
9 c( x$ C! R  w{ & q- J4 H: ]4 d- z* s9 V
printf("Error Opening File\n"); 8 P) q9 I0 u, J  y, ~1 Q' S8 W
exit(1); 1 F: W7 L1 b6 B* J6 G$ f* l$ F- {
} </FONT></P>: `: _# d" a$ y) p
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) { . ?4 a( M1 Q5 S8 B: ?
printf("Read Failed.\n"); / f: k- b4 K; o; _5 ]
exit(1); * m# T0 b* n+ O! ?9 j- m6 H: F/ F6 O( \
} 2 v, \" \* w1 D1 D6 D5 f# M0 _
else { 6 \( ?6 p3 X/ F4 u
printf("Read: %d bytes read.\n", bytes); $ t% J+ U& v4 ]# ]7 p, d5 S
}
1 ~! @/ w$ x! A# d# F- Lreturn 0; % d# g% _- ~3 b- A( y
} </FONT>) G! Z- ^, K+ E2 P
7 S% r+ Z& d/ T9 H1 w
</P>
' I5 C* A" t# \& W; R% g<P><FONT color=#ff0000>函数名: realloc </FONT>
  Y- z, Y6 `+ M' h, S) c9 g功 能: 重新分配主存 ' s9 p* r/ I6 m, W. V
用 法: void *realloc(void *ptr, unsigned newsize);
$ a) M- v2 ~8 Q" r" k6 |程序例: </P>
$ i; c+ h/ l4 d/ j<P><FONT color=#0000ff>#include <STDIO.H>
1 U' W" A4 Z$ |% ~$ o& U#include <ALLOC.H>0 g8 Q. K% R  `/ S1 x! w% B+ V+ l7 w
#include <STRING.H></FONT></P>
1 z4 e; ?. x% F<P><FONT color=#0000ff>int main(void)   U  d% Z& C% s5 D" ^9 x
{
1 F5 }9 L$ g0 j6 K) F8 B3 D2 C( achar *str; </FONT></P>
0 i) j$ F2 K6 i1 }  @' q# `0 O<P><FONT color=#0000ff>/* allocate memory for string */
- `8 t9 H- }* h- L9 wstr = malloc(10); </FONT></P>
; s9 q; }7 `) Q, r; X/ j) g<P><FONT color=#0000ff>/* copy "Hello" into string */   }' }9 Z4 F9 U: L( J
strcpy(str, "Hello"); </FONT></P>" d' ?5 G$ A2 D+ r1 v
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str);
# f! G  Y" U$ z# C$ c) C$ g4 e, \str = realloc(str, 20); 5 l2 @8 F  C9 c) ~" c: Z4 J. d; h
printf("String is %s\n New address is %p\n", str, str); </FONT></P>+ l, H- l0 v: Z4 Q$ n$ i4 p
<P><FONT color=#0000ff>/* free memory */
9 L6 q& ~& \9 q6 Pfree(str); </FONT></P>
) Y% H, R. C( w, E) G" f% Q<P><FONT color=#0000ff>return 0; : y7 u9 q# B+ @$ n2 f% c
} % ^' y" T, p- ^4 L/ O
</FONT>/ X2 s: H. L% l( G0 R5 V! S
</P>$ |4 p6 \+ u; h  j5 T- R9 L' m0 T( T
<P><FONT color=#ff0000>函数名: rectangle </FONT>* g7 @/ P2 I  \: e8 T' D6 @5 [
功 能: 画一个矩形   [7 Q& ~: B/ m2 L
用 法: void far rectangle(int left, int top, int right, int bottom);
$ ^  ?, Q9 Q4 u' J: G# F( e程序例: </P>! \% r; c# l: E* f* T4 o
<P><FONT color=#0000ff>#include <GRAPHICS.H>) @. }; i; \" [1 W$ t
#include <STDLIB.H>
  M7 Z! |4 z; ~7 ]" X#include <STDIO.H>
) N1 @" a0 |5 i#include <CONIO.H></FONT></P>, l! Q" f! J. c) D. t" o
<P><FONT color=#0000ff>int main(void)
8 N% w' n+ [. t* T  T; J$ v8 j0 X{
! _8 K4 t  C* V4 @/* request auto detection */
( n) }$ a; C- {; i0 {1 A2 ~int gdriver = DETECT, gmode, errorcode;
# g8 l" n6 E- v, T3 Kint left, top, right, bottom; </FONT></P>6 `% v9 P. e  l* g2 Y' i
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ( h6 L& x9 J) K6 m
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
: p8 ^, A1 I$ C( m2 c4 j<P><FONT color=#0000ff>/* read result of initialization */
" N( L3 x' A: f: Y7 J4 cerrorcode = graphresult(); . z) F) `" G% h: Q/ R
if (errorcode != grOk) /* an error occurred */
# J3 l( E, F6 e$ F1 g{ ( ?; o0 m  q2 R. ~9 }! C
printf("Graphics error: %s\n", grapherrormsg(errorcode)); & x# y9 \4 U" M9 I# v- {* Z
printf("Press any key to halt:"); 3 S# V% ^9 ~4 [6 ?! R
getch(); 7 j) _* {% @6 O, S
exit(1); /* terminate with an error code */
3 }3 E8 @: ^  T4 {- _} </FONT></P># I3 c. `- ~, G$ A1 W
<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; % S: g- e# Y% h1 J4 r3 _
top = getmaxy() / 2 - 50;
& x, z# L+ N3 p5 p& Wright = getmaxx() / 2 + 50; , [% L+ A3 d5 S( {/ v9 z! A: E  `/ I
bottom = getmaxy() / 2 + 50; </FONT></P>
7 o- q% y4 `; Y* ]$ D( F3 K<P><FONT color=#0000ff>/* draw a rectangle */ 1 P# }! A1 ?5 Y# ~& V# h
rectangle(left,top,right,bottom); </FONT></P>
% h  ?* y% N4 X. W3 L4 h* W$ u<P><FONT color=#0000ff>/* clean up */
  u4 @) c3 `) v0 E; `7 [getch();
- x) u; R: g* k1 Q8 Z, S: R2 w0 Wclosegraph(); 5 M* |! L6 @+ P0 u; m6 H, D. n
return 0;
& B7 O& h# f# A9 J4 b. s5 o, Y1 r}
4 ^+ g" o% d: o. v</FONT>
3 S5 M* s! g, d" C; f; v</P>
' ?  l: F/ ]" `  z. s<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>
- i# J0 X0 G4 `5 O功 能: 登录已连接进来的图形驱动程序代码
- K5 M5 o  a8 W! S& L用 法: int registerbgidriver(void(*driver)(void));
( `) W7 h( q. O2 F/ ]: Q2 C程序例: </P>+ G# G. \% p0 r& w5 m4 T, N9 _$ h
<P><FONT color=#0000ff>#include <GRAPHICS.H>
6 K: E, A3 o* P- l2 E( H+ Q0 n( q#include <STDLIB.H>0 _# G6 Y: x, S) {4 }
#include <STDIO.H>
7 @1 a) W0 z) p) h' Q1 J#include <CONIO.H></FONT></P>) o; s2 M! o3 i& b
<P><FONT color=#0000ff>int main(void)
: }" z+ G2 x+ U7 @6 h" o{
: j5 h2 N" K" A6 N! f. H$ S/* request auto detection */
; L8 b  L3 {6 C4 f' z6 r) rint gdriver = DETECT, gmode, errorcode; </FONT></P>
6 g9 d7 Y9 P, i# l8 n: i% i. y<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
* S. S% a/ ^+ K- b, e) R5 t0 [errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>) a' B: b3 c/ B
<P><FONT color=#0000ff>/* report any registration errors */
  z' O9 y, W8 k3 hif (errorcode &lt; 0) & X, \1 z! B- r: P2 k
{ 4 z3 m/ q% ~. n/ I
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 2 G  x) e; ]/ {' g& F$ P
printf("Press any key to halt:");
0 r, m, L+ d3 S) R+ E" _getch();
9 Q4 `* n( n" ~1 aexit(1); /* terminate with an error code */
; g+ q- Y  o  G( i' r} </FONT></P>" D2 P' i5 z6 s1 z, s: z4 ~; j
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 8 V% P# X+ m( _: v' H1 Z3 Z- E
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
- M6 K( Q! v  j" `) Y<P><FONT color=#0000ff>/* read result of initialization */ + x5 N/ y* [9 }
errorcode = graphresult();
( Z( ]2 j- n$ c8 g, f3 iif (errorcode != grOk) /* an error occurred */
4 p& @( m' p- J; ^' x) ~{ # [& j1 {6 D" V9 C9 c. P
printf("Graphics error: %s\n", grapherrormsg(errorcode));
! c- T. L- X* k# I& H8 D& Zprintf("Press any key to halt:");
2 Z' x. s' {- g% ~- e# @8 lgetch();
9 S" f8 c! c' E2 pexit(1); /* terminate with an error code */ # H$ C5 w5 F5 ~2 y% T
} </FONT></P>2 P( @6 I$ _6 {6 A: d
<P><FONT color=#0000ff>/* draw a line */
9 Q% M1 x8 X( D9 v+ ?line(0, 0, getmaxx(), getmaxy()); </FONT></P>' E; {8 g, V9 |0 l/ Q# Z+ @
<P><FONT color=#0000ff>/* clean up */
9 {+ U& s+ Q* M! agetch();
- @1 V+ M' g! e* a8 S. |; yclosegraph();
) c$ q2 H8 S/ A6 R8 ~return 0;
1 O/ J6 ]4 x( M/ h8 y} 4 X+ a+ j! y+ w$ o
</FONT>7 O' ?& [$ C* ~4 o9 T9 U0 S, `
</P>4 Q, b/ ]$ c! p* b" D  A7 ?$ N0 ?$ m
<P><FONT color=#ff0000>函数名: remove </FONT>
4 |. _  x$ @0 @8 j% x功 能: 删除一个文件
) {- v) F6 |  u6 e用 法: int remove(char *filename); - A6 R" |4 v# _# I3 p
程序例: </P>: J# Z- \3 C2 `- v: s9 D5 Q6 Y
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>1 Z8 \& s$ z1 c! B1 M
<P><FONT color=#0000ff>int main(void) ) P, ]% p9 W( a4 Q/ x$ h
{
% w  p! z/ z, r3 kchar file[80]; </FONT></P>
: }' w7 w7 P- K" X<P><FONT color=#0000ff>/* prompt for file name to delete */
3 h4 {5 ?) G$ u: }) A( xprintf("File to delete: ");
" `3 x- i$ Y, i+ l4 G7 \6 Y' vgets(file); </FONT></P>
7 d- b% x8 P8 Y2 F/ y% j# ]<P><FONT color=#0000ff>/* delete the file */
1 N( N  z' T( R$ G5 a8 Cif (remove(file) == 0)
8 H6 {7 J# f2 r* I6 h% O; v6 Vprintf("Removed %s.\n",file);
0 o7 U) U* m# Z0 a" Zelse 7 f6 t4 q6 m5 o( j; E0 _# ~9 F! U
perror("remove"); </FONT></P>
( b. X2 F+ ~. {% O# W8 y) p2 X<P><FONT color=#0000ff>return 0;   _7 W2 o* E, u  @6 ^3 {. C, t. ~  _+ F
}
2 K' u# R5 c4 e# k2 N- b
0 [$ @' z$ c0 @/ |) G( ~$ Z</FONT></P>
3 i( p1 l% C. m' F<P><FONT color=#ff0000>函数名: rename </FONT>
: ?; \& h; |* _# D0 U6 U, v, v. y功 能: 重命名文件 4 M& z$ B( H) z
用 法: int rename(char *oldname, char *newname); ; i  W; }* @8 M8 z% S' g! o0 T
程序例: </P>
" j% k, [- q2 E, E! P<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>, e$ ?+ c1 w  A' }  e* {
<P><FONT color=#0000ff>int main(void) ! X% ]/ `. g2 H8 L% ~  c& Q
{ ' n% W% f2 Y8 ^( |: v
char oldname[80], newname[80]; </FONT></P>2 v# `0 Q) n/ ^. ^, l5 A: h; G
<P><FONT color=#0000ff>/* prompt for file to rename and new name */ * N6 Q% \8 j7 T
printf("File to rename: "); % R; [* L2 d' ^/ q& |
gets(oldname);
3 J6 }& m" X9 r& bprintf("New name: "); 1 w+ {$ i( y0 T0 d' F. |
gets(newname); </FONT></P>4 M* \# |( N7 m! j& q# P0 E
<P><FONT color=#0000ff>/* Rename the file */
' v7 c* m; d3 R4 a! k# Wif (rename(oldname, newname) == 0) & y' ]; I, `7 ]+ y# \2 N
printf("Renamed %s to %s.\n", oldname, newname); 6 A" u4 K; `) H
else ; N  d" E9 X7 e5 X+ M' b0 q
perror("rename"); </FONT></P>! u0 @8 d0 O% }: [
<P><FONT color=#0000ff>return 0;
1 `/ W5 J5 a* z, C# [- S}
' F/ d5 L8 K8 m  x4 L( @+ r7 W3 a- n$ z, m$ B' s
</FONT></P>& B: B  F+ r6 g0 ^; n$ A
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>4 ^; Y; `8 Y  l7 j
功 能: 将屏幕模式恢复为先前的imitgraph设置
" r7 U  q9 m7 H2 Y用 法: void far restorecrtmode(void); $ l7 j: @& O- d7 o
程序例: </P>& e  ~2 O9 e6 b9 }) w  V
<P><FONT color=#0000ff>#include <GRAPHICS.H>
0 u% {7 N( G: i, m#include <STDLIB.H>3 L+ D( S  H1 f9 l* u, I" {# ?
#include <STDIO.H>( y: ^1 Q* `  d
#include <CONIO.H></FONT></P>+ q4 N: l; n: i: l
<P><FONT color=#0000ff>int main(void) ) g' _& U/ B) _7 {
{
, {) j% O) f6 W/* request auto detection */
) m* V; d; h/ G$ `6 Xint gdriver = DETECT, gmode, errorcode; & U! a3 Y  n/ H
int x, y; </FONT></P>
% V3 G# A# d1 }6 s1 j! H! L<P><FONT color=#0000ff>/* initialize graphics and local variables */ * Q0 C' T% g9 g  U) ~2 R  }
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>( A3 J( _3 n* w3 C
<P><FONT color=#0000ff>/* read result of initialization */ 9 M4 M4 o  O5 b
errorcode = graphresult(); + ~( j& H: H7 u5 _4 J: {
if (errorcode != grOk) /* an error occurred */ " G/ T* K# C: L* X/ \
{
/ L5 h4 O. n" Cprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 3 |+ S, Y/ Z; w: M  l
printf("Press any key to halt:"); 9 C8 v- K9 g0 J. @0 {& O7 N
getch();
5 T" E3 H. p. v, |  V7 z) S- v# Uexit(1); /* terminate with an error code */
( |; i1 A7 O# v& {} </FONT></P>
; W# f) K% z9 u* R# C8 V  T<P><FONT color=#0000ff>x = getmaxx() / 2;
: }7 N1 j1 y4 V; Fy = getmaxy() / 2; </FONT></P>
+ a+ _4 L0 p4 J+ a<P><FONT color=#0000ff>/* output a message */
9 r: i9 t6 J1 V' \' k8 F$ @settextjustify(CENTER_TEXT, CENTER_TEXT); , l" D: T  A" J, O' e
outtextxy(x, y, "Press any key to exit graphics:");   {( Q0 p4 t! b/ E& v' G: c: a
getch(); </FONT></P>
, y4 V) q' ]; W& |6 T4 }) S<P><FONT color=#0000ff>/* restore system to text mode */
( i* U3 E( |. q' ~! j0 u% ~, Nrestorecrtmode();
  _; p6 ~4 c0 t( S3 U4 Oprintf("We're now in text mode.\n");
0 p8 z6 C  |' Y; Q& r! gprintf("Press any key to return to graphics mode:"); ! K  T. [& k6 d7 W. N! s% }8 g: i
getch(); </FONT></P>
. B8 `* O8 F0 N5 e0 {! r! e<P><FONT color=#0000ff>/* return to graphics mode */
' t2 k+ |8 b$ u6 H- Gsetgraphmode(getgraphmode()); </FONT></P>" |" [0 E0 p) T, s0 G( @" t. ~
<P><FONT color=#0000ff>/* output a message */ . q6 V! D5 F& }5 L2 Z$ q* T
settextjustify(CENTER_TEXT, CENTER_TEXT);
; z4 w# u5 G& couttextxy(x, y, "We're back in graphics mode.");
  F9 I/ ~  u7 kouttextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>% R; }+ x' W$ w! M3 G- Q
<P><FONT color=#0000ff>/* clean up */ + U: J& }& F1 m; o7 a3 J* I! j$ y7 E. k
getch(); : T. L- q. v( a- m2 c' j
closegraph();
* n7 o0 o( r" ]0 y( f7 wreturn 0;
7 H2 h: c( j0 I+ J}
  l% k( T4 k2 _: c</FONT>4 w1 }5 I1 e& o2 }  K1 B: t) \1 F8 E
</P>6 Z8 b( Z5 p" L0 G" i3 Y
<P><FONT color=#ff0000>函数名: rewind </FONT>/ K: `( d0 W; n% k
功 能: 将文件指针重新指向一个流的开头
4 F; h% U  S0 A, X9 l7 w5 |用 法: int rewind(FILE *stream);
0 @' h# h) i! X# d2 }程序例: </P>
6 N2 a; z1 T1 W  p<P><FONT color=#0000ff>#include <STDIO.H>/ w$ T" q: h6 T; B# V  e4 r5 b
#include <DIR.H></FONT></P>
7 B# \# `' }8 j; k& I4 |<P><FONT color=#0000ff>int main(void)
7 i5 e+ b6 T4 j, O7 `{
6 s" G/ p0 O; b& P* [FILE *fp;
8 L, e( d" N& N5 s/ C; X1 fchar *fname = "TXXXXXX", *newname, first; </FONT></P>4 Y1 J1 D' u! K' O' d2 i
<P><FONT color=#0000ff>newname = mktemp(fname); ! w! f. n* F* r; h" M, J
fp = fopen(newname,"w+");
& G) V& j$ x' \1 ?2 D* @fprintf(fp,"abcdefghijklmnopqrstuvwxyz"); 0 M3 [; G3 ^# V! c
rewind(fp); / f. E; o: ~7 u- @8 l8 I
fscanf(fp,"%c",&amp;first); : s2 ?- ^% V* H2 W9 J7 l7 i
printf("The first character is: %c\n",first); 6 ]& Z- F5 ]4 K. `4 D) m" Q
fclose(fp); 3 \1 j/ E, z% C7 j7 S, ^; H) N. \% y" X" ?
remove(newname); </FONT></P>1 g) G" g3 D3 o
<P><FONT color=#0000ff>return 0; - G; a, ^6 q# Y4 q7 w
} $ H& I2 }7 t; D# x
</FONT>2 X2 d) \" u1 o' ?" u4 q8 O5 L) h
</P>5 X8 t/ A& i6 E- s8 @  k' V4 r. p, l
<P><FONT color=#ff0000>函数名: rmdir </FONT>9 S/ z. q  R5 P* h
功 能: 删除DOS文件目录
. e& S$ S! {5 K" u2 N用 法: int rmdir(char *stream);
$ g6 C+ i( X; ?$ x8 {程序例: </P>
/ C" E/ M/ {3 N5 e- A2 m- s<P><FONT color=#0000ff>#include <STDIO.H>, i$ Q- ^2 X; E$ r1 h& N$ Q
#include <CONIO.H>+ o) [9 n8 t5 p& i
#include <PROCESS.H>
/ C4 a. X( r" A/ `#include <DIR.H></FONT></P>
2 v. |9 ^" n, @, L: P( G2 q% w" |5 G: C<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>% @! I0 T& H0 o3 w
<P><FONT color=#0000ff>int main(void) 6 U5 B0 }; m( y! f  k. V0 c
{
& {! s# L( w5 U" m% E/ s+ }int stat; </FONT></P>
4 m, M9 E) J+ p2 o4 Q7 D<P><FONT color=#0000ff>stat = mkdir(DIRNAME); " w0 C# G# Y+ v4 L7 X) h
if (!stat) + P: C6 R* P9 s0 }
printf("Directory created\n");
9 A" C4 I5 H- Xelse
% D6 M- P# E) Q4 l! o- e{
+ o6 G: r1 \  R" Q: f# uprintf("Unable to create directory\n"); 5 t+ R6 D- }! F3 z$ s" u2 Q
exit(1); 1 M: I, @; {9 ?4 i
} </FONT></P>8 }! R: @& O5 E- i% H/ g
<P><FONT color=#0000ff>getch(); 3 V% U# m% M; k  Z7 s
system("dir/p"); 0 b2 H2 u  g- n! }- S6 O
getch(); </FONT></P>* K# Y9 P" M6 K8 G% U" b- c
<P><FONT color=#0000ff>stat = rmdir(DIRNAME); # L+ G1 ?' o% `, s
if (!stat) 2 {1 K0 S: J! J( ?1 P7 q, U9 Y
printf("\nDirectory deleted\n"); , U- L$ o( z# T: U+ w0 _3 c
else
: J9 D$ L" G2 m5 @  z3 o{ ' Y* }3 A, ?( ?4 Z& p6 ^# G
perror("\nUnable to delete directory\n");
6 X- ^, z( Y3 _! x6 ^exit(1);
$ J1 G- r9 Q- l" D4 U8 @} </FONT></P>
+ z4 {; F4 i8 j  K( f$ a<P><FONT color=#0000ff>return 0; - R, `, z/ ~/ _! l
} </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-6-11 07:26 , Processed in 0.412833 second(s), 52 queries .

回顶部