QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |正序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>, k5 Y& k; O  C' e
功 能: 使用快速排序例程进行排序 3 W: r+ i; W/ ]  S. A; V
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); $ s& Q' Y: f+ |; ~4 q4 E
程序例: </P>1 D6 r3 I/ k) {5 A1 g8 U' x# H3 G
<><FONT color=#0000ff>#include <STDIO.H>  g- E- ?" W: E+ P2 f6 j/ F+ q' I$ A5 w
#include <STDLIB.H>  q0 m7 y* e- U$ J: R/ ]
#include <STRING.H></FONT></P>
3 g6 N2 h1 j0 j( T1 q4 L" M<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>* I3 u8 ^/ ~+ M( f7 R! N
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; $ d8 z6 u: ~2 w9 [* H9 A& q
</FONT></P>
! S9 M7 M& B) U4 Z<><FONT color=#0000ff>int main(void)
8 h/ N, V3 f. @  n{ # E/ I4 X: m; e! b. o; J
int x; </FONT></P>
% {/ m6 z3 C& ]& E! P& ^$ {<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 0 I9 N5 M9 _+ h) a: M* s
for (x = 0; x &lt; 5; x++) ) j. i* q+ a( Y* @
printf("%s\n", list[x]);
( r& Q7 F$ `4 j+ I7 m4 Ereturn 0;
7 v+ _% F3 h$ n1 G& P9 d+ c} </FONT></P>
9 q3 t* t9 ^( I, {4 n/ |<><FONT color=#0000ff>int sort_function( const void *a, const void *b) & o5 z1 x+ U: r: i1 z, S
{
! F( l0 P0 x1 e+ b0 t# oreturn( strcmp(a,b) ); 4 @5 d8 u) P/ y0 M( T
} </FONT>
: A" n& K/ Y% u. G' L+ q</P>
$ N9 v/ t9 a4 u: C0 E$ v1 K5 }+ E" M: `  W/ O5 m8 \  J% p
0 @8 w4 H( x0 [6 J& M
<p>2 g! K3 f$ `9 u( D5 `! z8 i) ~+ j
<><FONT color=#ff0000>函数名: qsort </FONT>
" e$ ~/ h7 u/ w, ]! ?, k功 能: 使用快速排序例程进行排序
0 n% E% c7 Y# _  U* k5 `7 u用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); ! U( c& i0 P3 q1 [4 h& ^
程序例: </P>
" S& Y# u: Z, j- Q# w5 n6 d) s<><FONT color=#0000ff>#include <STDIO.H>4 P. f' t6 p! k0 b/ f* R  ?
#include <STDLIB.H>
8 b' L1 T% j' D/ v( [/ W3 l1 s# R#include <STRING.H></FONT></P>% f9 ?0 `% _8 o
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>+ O- F  j+ S  E# N" A
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; 7 N; r# p& @3 F% B
</FONT></P>
7 t7 y( L* n" \1 `<><FONT color=#0000ff>int main(void) # M1 F; W6 k+ J* p# K2 s3 @& }! t
{
7 R2 h- y6 @  Z$ {1 v$ E; V8 Pint x; </FONT></P>
0 m5 x0 \. x" ^1 d0 N4 ^6 ~<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); & R+ t0 C# C+ W  F1 p. F7 e
for (x = 0; x &lt; 5; x++)
" r- u( M3 Y4 o+ Y; N7 L6 ?printf("%s\n", list[x]);
- f5 l: e1 j5 ^* `: Areturn 0; ( C! {) o- t. Y8 u" F5 i) j$ B
} </FONT></P>) _1 r% S+ W* p* k3 O2 U; a
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
* Y5 V" m/ |. C( o* d{ 6 V6 R! r0 x" u7 O" c
return( strcmp(a,b) ); * x2 n, A* G% h* f) D- u# }
}</FONT></P>
2 t9 ]* Y% m0 B6 O  G0 Q4 ?3 J<><FONT color=#0000ff>% j. Z; M4 u% M$ L% b0 e6 j% D
</FONT>
: X$ i- U" X' g% |</P>6 g& h. B: E5 O
<><FONT color=#ff0000>函数名: raise </FONT>
- O, ]( A( |. p3 ~' L. A功 能: 向正在执行的程序发送一个信号 2 Q- x! M7 b+ k0 L- B* P
用 法: int raise(int sig);
; ]* y. L% d& v# W程序例: </P>  {& M: m& n# q3 _' @- J. F7 C
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
- d  n9 y" D4 d' n* e$ h0 v<><FONT color=#0000ff>int main(void)
9 k& e8 I! a# j& H2 [+ }3 A{ - s' X# S  U8 S2 m: U3 Y$ ]; E
int a, b; </FONT></P>$ h+ j: ?5 D. u" j" z8 L
<><FONT color=#0000ff>a = 10; 4 G$ ^" ~" M. _; ]! x8 y% |
b = 0;
( E: Q$ P; H% o8 f+ }# g% g# y" c5 Lif (b == 0)
3 Q3 o2 F) b1 u) G% T% n& j/* preempt divide by zero error */ - ]5 K1 J7 U/ Y
raise(SIGFPE);
0 S) t, Q* J# Oa = a / b;
- e8 f+ S; e$ @# L! @) E" wreturn 0;
1 O. H" r! \5 Z: {. V$ v# G* ]. y} 2 d, r) b; c4 m
</FONT>' Y! V1 b  e, t; x) W! G4 V5 i# ]
</P>
( T* j! L8 S* l* \( k7 n* B1 F& C<><FONT color=#ff0000>函数名: rand </FONT>
# j& l( K) f8 \+ J+ N功 能: 随机数发生器 7 X: k& s, J" b8 C- s  g& \9 h
用 法: void rand(void); , V" K- c  A: I: T4 L
程序例: </P>
" S: s  L5 n) q5 y<><FONT color=#0000ff>#include <STDLIB.H>+ u/ O/ M9 a$ i9 N% {
#include <STDIO.H></FONT></P>
/ P& A9 ]! ^. `, G) z, U<><FONT color=#0000ff>int main(void)
* O; r7 w, `" V9 B3 K  E$ t# u{
- Z9 {. t  u  W" ^0 sint i; </FONT></P>
' {9 U2 V, T8 t% O" i<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); / p0 Q" u# }% h( a) L  z: m& T, f
for(i=0; i&lt;10; i++) ( T1 T! z2 b( V- w1 f
printf("%d\n", rand() % 100); ) e7 s( q% H& W. N8 ]) R; |- c8 m
return 0;
7 |, h1 g: {+ L% `}
) Q" g2 p) e) B6 n: B. m7 E% V; d; @4 p- p
</FONT></P>
+ d$ E8 ]2 j: w( l<><FONT color=#ff0000>函数名: randbrd </FONT>- S4 y" I9 R: w& B/ |6 O$ F  n
功 能: 随机块读
0 p  C3 s$ n* G用 法: int randbrd(struct fcb *fcbptr, int reccnt);
  n9 I8 `# U8 }/ S程序例: </P>* N; z8 X. Z: N6 W6 }3 a
<><FONT color=#0000ff>#include <ROCESS.H>
4 Y- R0 j/ ]$ t' O- J2 `#include <STRING.H>
" U4 I' W9 `3 F5 L3 `#include <STDIO.H>  r5 [9 i" N9 g( |- h/ E
#include <DOS.H></FONT></P>3 @* C) `/ H. e1 p  o/ v4 ^
<><FONT color=#0000ff>int main(void) 7 c3 n' g% |% s
{
& u/ Q  z: j$ d! Q" Jchar far *save_dta;
) l. _, {4 _. m7 X3 P- tchar line[80], buffer[256];
1 [. a+ ~7 @0 g& N' z! Wstruct fcb blk;
, f0 x  W! A6 `9 P6 I. H; v! Oint i, result; </FONT></P>
  \# E* E5 f+ D1 R: k<><FONT color=#0000ff>/* get user input file name for dta */ & |$ o9 x, v7 b1 q  e
printf("Enter drive and file name (no path - i.e. a:file.dat)\n");
' d  e! N0 E7 W/ c- v1 c/ K0 Ngets(line); </FONT></P>2 R% o2 b, j( T2 b! d: }$ m! s
<><FONT color=#0000ff>/* put file name in fcb */
" T8 s3 ?9 [; @if (!parsfnm(line, &amp;blk, 1)) 2 e( V  \; H# Z; {6 |
{
# u: ?) z# `1 ]* B% W( b' Aprintf("Error in call to parsfnm\n");
2 `, s! a2 L& q( xexit(1);
% F" Y) j9 r" e}
6 c# ~& X- }. U/ T0 l% B& Oprintf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>) _8 v, {/ K4 \- ^
<><FONT color=#0000ff>/* open file with DOS FCB open file */ : c3 O' R  O8 b* |& m
bdosptr(0x0F, &amp;blk, 0); </FONT></P>+ h" r4 `  a4 v6 w1 x6 p" ?& u4 c
<P><FONT color=#0000ff>/* save old dta, and set new one */
" i; C6 l# N7 O9 T3 x& Rsave_dta = getdta(); ' ?" E, _6 J3 P
setdta(buffer); </FONT></P>
. M, s  I8 B9 O% o. Q+ c<P><FONT color=#0000ff>/* set up info for the new dta */ ( _0 z% b- M& Z- S/ b+ Z/ W- D: I4 R
blk.fcb_recsize = 128; 8 A' Y* m7 |5 K' M9 S4 P
blk.fcb_random = 0L;
9 C! s. J0 e3 w/ i: x4 U8 V7 \3 Sresult = randbrd(&amp;blk, 1); </FONT></P>
* H+ K- G$ V3 }: @$ M- \$ F& s0 H<P><FONT color=#0000ff>/* check results from randbrd */ $ F" _- N2 h8 f6 w" k- j
if (!result) : O4 ?  X/ b4 k  t7 F& W8 Y
printf("Read OK\n\n"); ; Z* i; E8 z0 s8 ]" R; z/ t" I! e
else
$ v- V$ ?4 Y# G( M9 F: M{
7 t, e4 e' }* a& d4 S8 Zperror("Error during read");
8 {; `; B( w- b* fexit(1);
1 C3 H) y2 }/ @- J9 J& K% [- U} </FONT></P>
' d$ E! X6 n1 A6 T  f) y- I/ s<P><FONT color=#0000ff>/* read in data from the new dta */
* E/ a! v* l9 i# \8 q) F- t4 {printf("The first 128 characters are:\n");
& V( F$ Q& w  Qfor (i=0; i&lt;128; i++)
: s1 r2 G5 i, s! bputchar(buffer); </FONT></P>1 |  ^$ T1 V2 N/ c# b" v1 z
<P><FONT color=#0000ff>/* restore previous dta */
! b' C0 D% p# H4 X( a# a0 h9 o; |1 Ksetdta(save_dta); </FONT></P>4 c: A: c" f& _& K5 C5 H3 Z
<P><FONT color=#0000ff>return 0; " s9 v( e& ~  a; N4 \; i. O! U
} </FONT>
' \  U* h- L1 m3 i& W2 a4 E</P>
9 `( `1 {7 ^/ A* N! C  @. f<P><FONT color=#ff0000>函数名: randbwr </FONT>" A+ m) Y! z; q  p8 K' ?
功 能: 随机块写
, E- I: _" P3 M+ i1 b用 法: int randbwr(struct fcp *fcbptr, int reccnt);
' @7 P/ M2 Q; ]( E* w! M/ t, ]6 f程序例: </P>$ x: T6 G. d& q% K+ o6 _' |" z. `' y
<P><FONT color=#0000ff>#include <PROCESS.H>
- v  Q( l  E3 }' J#include <STRING.H>0 w8 t1 A4 N$ s6 U. r
#include <STDIO.H>
7 M$ K& n% x: H#include <DOS.H></FONT></P>
9 L/ B. W+ J; M( B<P><FONT color=#0000ff>int main(void)
* m5 f2 |  M/ x( B+ q{ : R2 g5 }" U0 N3 U$ z3 W
char far *save_dta; # _( b+ T5 {/ g: E
char line[80];
1 }9 z8 V2 j" @/ schar buffer[256] = "RANDBWR test!";
- a$ \; V. m: g* V+ O3 s1 f+ istruct fcb blk; ' w1 }2 S; o# X
int result; </FONT></P>
, U( A8 ]* a4 R! y6 o1 m<P><FONT color=#0000ff>/* get new file name from user */
, }5 X- S5 a& y( r5 }& i! xprintf("Enter a file name to create (no path - ie. a:file.dat\n"); + A9 O2 \# u% K1 s7 Y% i* r
gets(line); </FONT></P>
% \# W0 }: P9 R% z( O8 k<P><FONT color=#0000ff>/* parse the new file name to the dta */ 4 }% x, W8 L% j, B
parsfnm(line,&amp;blk,1);
9 e, b6 e' W. Z' ^4 C6 P/ l* qprintf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>) x( n# _1 ?: T' G- {. M
<P><FONT color=#0000ff>/* request DOS services to create file */
" \! r4 n) T1 [& `8 nif (bdosptr(0x16, &amp;blk, 0) == -1)
$ n$ Z' X( R9 I. ~) g2 H: t{ & j$ y5 C  l3 v& t2 ^- X* Y
perror("Error creating file");
+ P/ B9 |( i( z$ w7 ^exit(1); $ f3 k% J6 p5 J* F7 j
} </FONT></P>
2 {: w- ^; d. `9 R+ r<P><FONT color=#0000ff>/* save old dta and set new dta */
; ?  t, k' g5 Q: \save_dta = getdta(); . I/ C6 t9 X8 o8 [2 S# ]
setdta(buffer); </FONT></P>; S% u1 U8 ~6 W  f5 y! L3 j7 F4 N
<P><FONT color=#0000ff>/* write new records */
! n. D' h& N  Q& T, F. \% n& _blk.fcb_recsize = 256; 6 h' M; _! W( `4 h0 T1 a- j& B  ^
blk.fcb_random = 0L;
- R* }3 l3 L; [, M1 q( ]" y$ presult = randbwr(&amp;blk, 1); </FONT></P>
7 O( c5 T7 h/ L: ?7 o& l' n7 \! R<P><FONT color=#0000ff>if (!result)
+ G/ [& a' H) d& }; sprintf("Write OK\n"); 1 E* z% n, X: j3 c$ @* `
else
7 }, b( l' q. _) F* U: l( l7 p% p{
0 P, M1 V3 t4 D5 Sperror("Disk error");
' T( |1 U4 D5 U& T; M0 |exit(1);
4 E3 F& f: W; T6 x* K* b5 U( P} </FONT></P>
( i. ^: a! n. s+ e' _<P><FONT color=#0000ff>/* request DOS services to close the file */
. P  H  I) ?6 M! @! _( Y- g2 Rif (bdosptr(0x10, &amp;blk, 0) == -1)
' P% x( M+ o9 E+ Y5 l, P. S* ]* K{
, O, Z* d$ Y4 I1 |  [perror("Error closing file");
6 Y+ c& f. t& dexit(1); # b* q9 U# k8 D" f
} </FONT></P>* s& X( @4 C, e0 K& Z- P" o
<P><FONT color=#0000ff>/* reset the old dta */   t/ s8 ^* }" f# \. r7 P5 L
setdta(save_dta); </FONT></P>$ N$ s* m" N( `0 W( m7 `
<P><FONT color=#0000ff>return 0; % C$ V" |* O% |5 @$ X
}
3 T) U7 O0 m8 b  X/ Z" H7 L</FONT>2 ?$ `1 O9 _- p* L) k
</P>
  ~! d9 i$ X7 d) j2 G<P><FONT color=#ff0000>函数名: random </FONT>9 h' L5 C; Q+ y2 t
功 能: 随机数发生器
4 C# N+ ?% R( O6 {, g用 法: int random(int num);
8 K$ S# q  \$ v, w; I程序例: </P>1 w1 w# L' M5 \
<P><FONT color=#0000ff>#include <STDLIB.H>; V# i4 t, M3 u
#include <STDIO.H>/ ^- E- p9 P5 v1 R  X+ |+ }; O
#include <TIME.H></FONT></P>
, I* Q+ P: D" U* M<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
/ @" b8 U- h' G& U2 u- t! zint main(void)
% @9 ~; a8 S( r$ y5 u9 w. Y{ # V- g& t* I; {1 O6 e  o' B" ]
randomize(); : e$ {' X  L1 ^& w
printf("Random number in the 0-99 range: %d\n", random (100));
3 a  s$ t: H# K0 M+ kreturn 0; ; w* W5 E+ ^1 l) t) D5 a* C
} . d9 E. x7 ~: v8 q$ [* W
</FONT># T% ?: V3 v3 ~) H" N0 N& ]' Q
</P>
5 E# a" t9 o. {<P><FONT color=#ff0000>函数名: randomize </FONT>/ n( `' T4 z) \! o1 b  r6 K
功 能: 初始化随机数发生器 6 u: b7 b# P; I, p
用 法: void randomize(void); & A6 L2 w" D# m$ n
程序例: </P>- H; Q1 N# d, O
<P><FONT color=#0000ff>#include <STDLIB.H>
, m. G, a8 w+ {# I#include <STDIO.H>
, u* U5 R/ x; i! H. h4 u* X#include <TIME.H></FONT></P>9 f2 O% p& Y2 w( G4 J
<P><FONT color=#0000ff>int main(void) - W9 y9 s( \7 @2 e9 |
{ 3 g. ~' U% C, c) ]- K$ R
int i; </FONT></P>* C6 ^) f2 n! ]" g7 L4 U
<P><FONT color=#0000ff>randomize(); - X! U1 N$ m7 A
printf("Ten random numbers from 0 to 99\n\n"); % G% ~( i( ]5 p; J
for(i=0; i&lt;10; i++)
* F' `- |$ s$ v* `+ k" Cprintf("%d\n", rand() % 100); 0 k% W" F9 |4 h' p
return 0;
9 s6 x, v( ]  d7 V) s  b& L8 a& r}
) w5 K; Q6 k/ x0 p, W/ _* K' V</FONT>
/ ?( a! d% W  V# f7 L</P>8 d- X) b% B+ g: U; z1 u
<P><FONT color=#ff0000>函数名: read </FONT>+ J5 ~$ ?" z% I
功 能: 从文件中读
  M6 b7 f1 ]5 N9 S用 法: int read(int handle, void *buf, int nbyte);
/ E. j3 j0 y& f5 i/ Y8 y7 V% G程序例: </P>0 C/ {! A8 N0 t  c6 b+ J
<P><FONT color=#0000ff>#include <STDIO.H>1 Z3 w6 j% X% A, W
#include <IO.H>/ |% t" k+ ^( F/ z. \& k
#include <ALLOC.H>4 R$ u% n( y' g% z
#include <FCNTL.H># b9 h$ F* R1 L- K
#include <PROCESS.H>
: G* w2 g8 w. l" L) Z: D  A) r#include <SYS\STAT.H></FONT></P>
7 r% N$ x# H1 ?- N! r4 Q<P><FONT color=#0000ff>int main(void)
: |  }* o) l1 e* w) ^# `{ ! Q: X' Q5 _( D- w
void *buf; $ x3 _9 W1 j. H7 \. ~6 `0 g
int handle, bytes; </FONT></P>; N" u8 o& U% m
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
9 a4 v, D/ B+ a  ^# Y) s<P><FONT color=#0000ff>/* 0 H5 r5 v1 J( i. |
Looks for a file in the current directory named TEST.$$$ and attempts ( ]: c' S6 g9 R- E4 |% b+ c
to read 10 bytes from it. To use this example you should create the : M; m1 N+ A& \
file TEST.$$$ . H/ p5 k6 D5 A9 u) T
*/
* X. C" ^5 t# ?3 `$ Fif ((handle = 9 {4 P' O7 l$ w) b& I3 D3 G
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
/ a' B7 X/ X4 Q8 @$ i6 f{ + A: p4 A0 H1 _1 l/ T
printf("Error Opening File\n"); , h( v. a  {) }% ?
exit(1); , m, g1 W1 m' m) b3 j' Z# x
} </FONT></P>3 ]' l% O1 m6 I$ H2 I% j
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) { # F! u. h' h5 s. i, E- D
printf("Read Failed.\n");
$ c' x3 D* o. g; t- ~5 ]# v8 [( xexit(1); " |! h1 F8 O. E( Y( E1 u) A
} , @  y7 m$ q/ i8 S* t4 `5 w
else {
2 M: m8 G  g0 F9 c5 Fprintf("Read: %d bytes read.\n", bytes);
. G) @2 @3 |  h% p1 c6 |} $ G$ V" d$ s* }6 Q
return 0; : {0 ]/ J! Z+ ]
} </FONT>0 ]2 o- x& {* R; b5 @5 {# w# x4 ^

" O2 l) Y3 I+ ?</P>
0 W  u& c" x! V( Z/ [8 m<P><FONT color=#ff0000>函数名: realloc </FONT>
! O* d, t9 @2 t6 j功 能: 重新分配主存
/ D' Z# [# R& a. |; g用 法: void *realloc(void *ptr, unsigned newsize);
) x  d6 C5 K6 X3 [  H程序例: </P>3 o! O/ r+ H4 }! ~) y- D
<P><FONT color=#0000ff>#include <STDIO.H>
  V- }( V8 N2 R8 D/ g#include <ALLOC.H>) W8 ?/ [9 R3 a, x- ^) \+ B) G7 @
#include <STRING.H></FONT></P>
: t% |5 t4 F/ V% ?0 V* h<P><FONT color=#0000ff>int main(void) : Z% d& m! a9 M- d+ w6 R
{ % K0 X$ T+ @4 w7 i7 C" ?8 ?
char *str; </FONT></P>6 \$ X' G% o( W% y7 c
<P><FONT color=#0000ff>/* allocate memory for string */ 2 [! O* o6 B. V8 S  I9 E- u
str = malloc(10); </FONT></P>/ p- y, O8 P1 T2 q+ }
<P><FONT color=#0000ff>/* copy "Hello" into string */ : ~) F4 t" x5 O% E0 n/ X0 ^
strcpy(str, "Hello"); </FONT></P>3 m7 X- n  S. D
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); ' l% Q* R/ O. A4 j! J8 A
str = realloc(str, 20);
! |2 C# H- {2 z) o$ B4 M0 jprintf("String is %s\n New address is %p\n", str, str); </FONT></P>3 f6 i" J( n8 u# N% g+ z
<P><FONT color=#0000ff>/* free memory */ , m% b0 Y: G: ]) h
free(str); </FONT></P>
7 V/ ^0 X: \6 s6 d$ a: ^<P><FONT color=#0000ff>return 0;
- @, e, k& N1 O' w" N} * C% }4 B6 d0 M
</FONT>$ I4 B: N. |( `! {; m+ q7 g
</P>$ k! e; U* L- V8 m
<P><FONT color=#ff0000>函数名: rectangle </FONT>
4 [0 C2 ?" i) G0 B功 能: 画一个矩形 " B3 M! n5 t% W
用 法: void far rectangle(int left, int top, int right, int bottom);
" Q6 ]8 x# `! ]$ f程序例: </P>
2 \7 A& L- Z2 v8 {& t: f<P><FONT color=#0000ff>#include <GRAPHICS.H>
7 M' m: x; J! c$ L' e$ }' c* [  ^) t0 C- @#include <STDLIB.H>
" Y8 `( W5 Q% ~/ f; K% p6 d: O; J#include <STDIO.H>
. g- B+ u3 d3 p$ @#include <CONIO.H></FONT></P>( ^$ h! K2 o# i  @
<P><FONT color=#0000ff>int main(void) ) b! U5 K% C. f7 X4 P( y" q
{
8 Q' t3 [2 @$ H, M! z0 L0 f2 D" T# C/* request auto detection */
- P" k0 @% ?# y* N  X1 F* Dint gdriver = DETECT, gmode, errorcode;
) K$ f+ P% `  }% d' D& Y: z9 v& M1 uint left, top, right, bottom; </FONT></P>
/ T4 v* c* n0 a/ O4 [3 S5 O3 Q<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 d. i/ _5 h( b' ]- G; u5 d
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
6 P/ H4 o' ?3 a7 O$ ]<P><FONT color=#0000ff>/* read result of initialization */ 9 q2 h2 |2 l2 B0 s% j4 n
errorcode = graphresult();
  O; Z) Q6 X0 K6 m, u' U; F, dif (errorcode != grOk) /* an error occurred */ ) |, b; e( E, x5 ]( l" ?7 L
{
( B' \* @7 h% o7 I: V# P( I1 Uprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 Y; U4 K( T6 d7 G
printf("Press any key to halt:"); ( v  R6 k/ E, b( W8 N1 g5 Z2 R
getch();
9 k. L# h! S1 n! p0 m2 Wexit(1); /* terminate with an error code */ 2 T( e0 s4 |: F+ [
} </FONT></P>
# o+ y, B% M  Y3 U<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; & h2 ~; f3 y. t/ s/ z  q
top = getmaxy() / 2 - 50;
' G, }3 e! t- jright = getmaxx() / 2 + 50; + h: |$ ]$ N- Z
bottom = getmaxy() / 2 + 50; </FONT></P>! _- ^, `6 ]# N5 H' z2 `2 }
<P><FONT color=#0000ff>/* draw a rectangle */ 0 v6 X% V5 I% X) ]
rectangle(left,top,right,bottom); </FONT></P>
& R# q- f# {0 p# J( k- |<P><FONT color=#0000ff>/* clean up */
9 V8 y. O* V$ D& u8 q% Kgetch();
/ h( e& T) {5 N* Lclosegraph();
) M% d! r) P$ O5 Greturn 0;
& K) e/ e7 V# o* t' f}
4 \7 a* T! ~  G$ `$ q</FONT>
* \9 O, s! X1 v: k2 X* o) e</P>( P$ @: ?" }: j% l* R
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>: R- L0 [4 Z5 _/ p
功 能: 登录已连接进来的图形驱动程序代码
" n9 E# T) z2 g用 法: int registerbgidriver(void(*driver)(void));
* A' t' z; x+ i' @程序例: </P>5 X" ~. M) _# e' z, J3 \7 M" Y& I# v7 R
<P><FONT color=#0000ff>#include <GRAPHICS.H>0 i' {& E+ B7 @
#include <STDLIB.H>
: a% R) Z; a3 r# t" N. k5 k/ e#include <STDIO.H>8 Q9 y1 @3 L  a' c! K* |! x8 _
#include <CONIO.H></FONT></P>
$ u+ c# E- z! ?<P><FONT color=#0000ff>int main(void)
, X( j" Z9 \0 R4 E& S{ 2 c) m" y/ ?0 ^
/* request auto detection */ 9 c  y' z# N! D3 D: ~1 N0 N
int gdriver = DETECT, gmode, errorcode; </FONT></P>6 ]& d6 E. l& V5 _' N. J2 k
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ 1 H8 n3 ~2 I. `  ]! K
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>
+ R* m9 Y, G: [# |. j* i2 k% H5 g<P><FONT color=#0000ff>/* report any registration errors */ 3 v9 M* x5 h/ ~9 {* @& h, Z
if (errorcode &lt; 0)
/ `2 W. S: h9 h! s9 I! Q' q! z. O/ s{
* z0 a0 w- d( Y1 V# vprintf("Graphics error: %s\n", grapherrormsg(errorcode)); : f4 U, B: C# g& s0 b6 U  A
printf("Press any key to halt:"); & ^, C& J% x! V/ t* [+ w7 v/ u* Z; E
getch(); 2 @+ j* R9 ^: q% [
exit(1); /* terminate with an error code */ $ ^% ?9 L- ?' J* _6 M
} </FONT></P>2 l8 C! y4 B( a
<P><FONT color=#0000ff>/* initialize graphics and local variables */ * o/ W4 f* N" f4 o" [# A6 F+ g
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
' l& s: y# G3 W% b1 }5 n' r<P><FONT color=#0000ff>/* read result of initialization */ & k3 q3 N: j7 X, z
errorcode = graphresult();
( P6 g- F! ?+ Bif (errorcode != grOk) /* an error occurred */ ( ?4 s) H: Z5 T* e- X9 ]) z; r4 G
{ 7 u% `+ T& R: C
printf("Graphics error: %s\n", grapherrormsg(errorcode)); % W6 F  C- B9 ~7 ^/ n' s
printf("Press any key to halt:");
7 q- a( a& \0 O+ R# ygetch();
2 G  [+ b! m, O% g; l* zexit(1); /* terminate with an error code */
. m6 _* u6 T/ g* ]4 q. C' j+ o$ V} </FONT></P>; L% c* K2 D! a3 \& v( d
<P><FONT color=#0000ff>/* draw a line */ ' V2 ^) H9 r" s( c$ B) M
line(0, 0, getmaxx(), getmaxy()); </FONT></P>
  A! }. V# d" u0 N, P<P><FONT color=#0000ff>/* clean up */ & r$ A/ |- I! Y* y# x7 p
getch(); 4 z, C! g( ?7 w$ i6 k
closegraph(); * n( K$ y; ?9 K+ ^! Q
return 0;
& U7 V' [# p8 n7 o. y9 o) E! X}
# E; n( @( r' |6 D9 |% {</FONT>
0 l4 y. Z8 ^/ N% x* V' Q8 q</P>
5 n7 B, u$ _: A! y/ I<P><FONT color=#ff0000>函数名: remove </FONT>6 Y9 h. ?3 W( ]9 ?
功 能: 删除一个文件 + }" n. i" L  @% i
用 法: int remove(char *filename); ( ~! s5 o7 I8 G  h  \
程序例: </P>8 b, ]  b: U$ b+ b) o4 U
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
2 @1 ?; x2 N! Q9 t$ e1 C. T# t& h<P><FONT color=#0000ff>int main(void)
9 g3 P! \1 r, p; `2 e7 J6 C{
0 g* V/ ~: h( kchar file[80]; </FONT></P>3 k  c0 Y: C3 _7 e9 ^3 V
<P><FONT color=#0000ff>/* prompt for file name to delete */ 9 u* B- Y, C$ z; j2 p- N- q
printf("File to delete: ");
/ M( U, H& O! O0 e9 ?' U9 c$ }4 Bgets(file); </FONT></P>9 a* k& i3 C2 F+ v: a
<P><FONT color=#0000ff>/* delete the file */
$ R+ o/ Q* Z3 P/ e( a6 vif (remove(file) == 0) 8 r. R/ b0 I$ w( l$ I: K0 n  b
printf("Removed %s.\n",file); ) @6 g) @+ L8 G
else 6 w2 V" b3 l- a" C
perror("remove"); </FONT></P>
% L- C6 q, b" f) p<P><FONT color=#0000ff>return 0;
; i- ]1 D  M2 P2 S} 4 `$ y- n0 Z& V0 O) {, S  m  z
: I1 f. ?$ h! a; h
</FONT></P>. q8 n+ H8 H4 d- G# q; S+ ~$ C5 n
<P><FONT color=#ff0000>函数名: rename </FONT>6 p$ S; ~/ q6 o# e
功 能: 重命名文件 , K+ E" p# w1 l  Z8 g: Y/ Y
用 法: int rename(char *oldname, char *newname); ( {$ a) H  o/ c; A
程序例: </P>, l. F! R$ ~6 z
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>3 q! M/ J! k' g3 q( h
<P><FONT color=#0000ff>int main(void) + ^0 L, T' c$ c7 K
{ 9 n+ y! X; L! l. x& O3 K# u3 e
char oldname[80], newname[80]; </FONT></P>' z2 G; z! u0 g3 f! H# }, b! ]
<P><FONT color=#0000ff>/* prompt for file to rename and new name */ 1 M+ C" e* T4 n$ `% q. o6 Y0 B+ a
printf("File to rename: ");
: x9 b$ f& e1 Q2 Cgets(oldname); ; w2 C) f- ~4 J/ m' c0 [6 w
printf("New name: ");
/ _  t7 p; A+ F0 r; Zgets(newname); </FONT></P>
5 \) ]0 e* _& C9 T* |2 [<P><FONT color=#0000ff>/* Rename the file */ & C* q2 a% @( ^& W) P0 k
if (rename(oldname, newname) == 0) ' ?# P. |. T8 K; \# d7 J: n
printf("Renamed %s to %s.\n", oldname, newname); , z- D4 j+ ]9 f+ l- u
else ) O9 a+ J4 A4 _5 }: o( V
perror("rename"); </FONT></P>+ ]- U4 U2 c! x( Z
<P><FONT color=#0000ff>return 0; 2 @; P0 O2 i7 b( _% Y5 \1 r
} ) h6 z5 e  A0 g6 \( m* c' g% W5 t

# @, T. J8 I0 I</FONT></P>3 M5 c' H% V; J% E- P" A
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>
: l/ I5 g' y  ?# ^  T功 能: 将屏幕模式恢复为先前的imitgraph设置   l  ?! Y6 \# y6 f2 F9 k
用 法: void far restorecrtmode(void);
2 s2 r  t  f& d! [0 k4 l1 j程序例: </P>) Z* |: F6 ^1 r
<P><FONT color=#0000ff>#include <GRAPHICS.H>
: _: P4 ]7 t+ F. V#include <STDLIB.H>5 h5 D9 H" m/ o1 [
#include <STDIO.H>
% s- ~3 o, ~, l6 c  Y/ j! ?; q+ N#include <CONIO.H></FONT></P>
7 N; s# P* C9 P. ^<P><FONT color=#0000ff>int main(void)
0 |( i. v+ u* L1 K+ d5 r5 l& j8 z: T{
$ P' m6 w/ m* W6 q. K, K5 s8 E/* request auto detection */
# K- N3 i9 C/ V+ |0 A' e" Fint gdriver = DETECT, gmode, errorcode; 9 n! m  Z% B9 h
int x, y; </FONT></P>
. ~3 `. N8 x0 F: z0 X; K. ?/ O$ p; I8 F<P><FONT color=#0000ff>/* initialize graphics and local variables */ $ E" N6 r# w2 v. P
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
* H& c* E# L" Y" |' [<P><FONT color=#0000ff>/* read result of initialization */
- R1 ~1 |1 o/ o. N3 s- k! yerrorcode = graphresult(); # W$ i2 q6 L* c  A- s3 ]
if (errorcode != grOk) /* an error occurred */
$ Z2 z- p4 Z& @) O7 g  @{ # u2 d* X3 J9 u6 n: T* h7 F7 d
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ! G% H) w" G5 q' s
printf("Press any key to halt:");
) R# f9 g) |) G9 a* Sgetch(); 5 F% b# B- Q9 [- @* M# }9 N
exit(1); /* terminate with an error code */ 1 N3 N5 _4 J. t
} </FONT></P>$ j, E* D* g1 U; x; ~7 ]- x% _2 K
<P><FONT color=#0000ff>x = getmaxx() / 2;
2 q/ X8 @" N2 t# h3 sy = getmaxy() / 2; </FONT></P>! H: U5 A- _: C, F) E
<P><FONT color=#0000ff>/* output a message */
6 C5 ]+ Z* B$ z7 \. L' J% Esettextjustify(CENTER_TEXT, CENTER_TEXT); , X! u7 ^: ], k( u. b- ?9 d9 U
outtextxy(x, y, "Press any key to exit graphics:");
6 i! D( Z4 V: c% f3 q0 L/ S: Jgetch(); </FONT></P>
' W/ t% U+ C: F4 G3 ?  k<P><FONT color=#0000ff>/* restore system to text mode */ , }. v: X  t9 R- _/ x
restorecrtmode();
7 p: o% l' ^; Z; Q; V2 P3 b3 R3 gprintf("We're now in text mode.\n");
" J7 w7 I8 J, F+ [0 ]printf("Press any key to return to graphics mode:");
* |- H* @8 c7 d& e$ kgetch(); </FONT></P>4 k% x  E9 \7 [, f
<P><FONT color=#0000ff>/* return to graphics mode */ . R* k' _" H8 N, `, p% K
setgraphmode(getgraphmode()); </FONT></P>
" t* v: C% W+ I/ a. Q5 ?8 Y<P><FONT color=#0000ff>/* output a message */
: c& [1 u/ X- E6 t- H7 Z: Q( W5 Hsettextjustify(CENTER_TEXT, CENTER_TEXT);
- c. D  d9 L1 w7 w" Uouttextxy(x, y, "We're back in graphics mode."); 6 F' M( C- ^& s( Y
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>  }' L% k3 M* u1 r9 b7 c3 {
<P><FONT color=#0000ff>/* clean up */
" k/ a. c7 Y2 Zgetch(); 8 R' i( n2 T% c! q0 n+ {
closegraph();
* y3 q/ q. B/ ]return 0;
4 \  @8 |1 S2 c} & \4 ^( _5 J8 G; H* J
</FONT>0 e0 E/ |2 w; X
</P>
8 U' l) v* D  h. {<P><FONT color=#ff0000>函数名: rewind </FONT>
9 \& }* q- W  C7 m! I3 q' z) P功 能: 将文件指针重新指向一个流的开头
6 V$ E9 y( p7 h8 N用 法: int rewind(FILE *stream);
/ a! l* l) s. d; j程序例: </P>! Z; x, ]" m3 W" ~' j9 L8 f
<P><FONT color=#0000ff>#include <STDIO.H>& o; Q0 x3 ?7 p  {
#include <DIR.H></FONT></P>
' K9 J$ H/ n/ u8 g<P><FONT color=#0000ff>int main(void) 1 h0 K8 n4 X9 p0 \
{ 9 u, y9 {/ K5 J/ z- M
FILE *fp;
+ }' M2 b* c7 _& Y9 a5 dchar *fname = "TXXXXXX", *newname, first; </FONT></P>
# J* m& r) j) h<P><FONT color=#0000ff>newname = mktemp(fname);
% p2 J4 Y0 u4 h: s8 t% d5 X$ yfp = fopen(newname,"w+");
" H8 L# L( t+ bfprintf(fp,"abcdefghijklmnopqrstuvwxyz");
' X2 x+ s" }( `( J9 p4 G2 h7 Hrewind(fp);
6 G3 d9 R! ~1 Ufscanf(fp,"%c",&amp;first); 8 ^( H( y( ^( Q7 \6 P" \: @8 K. b5 L
printf("The first character is: %c\n",first);
+ ]1 @+ N9 X" a" U; S  D& {& Ffclose(fp); 9 r* U5 T. @3 C0 R, j2 J5 n
remove(newname); </FONT></P>
6 {& a8 C% D& H8 }<P><FONT color=#0000ff>return 0; 8 Z8 D. u( U, v- l' ]; Q- d, j
} 8 n  d* n( a* \# t
</FONT>& M) a2 s  t9 v" P/ e
</P>
1 t! `8 O( E8 B- c' T# g0 m<P><FONT color=#ff0000>函数名: rmdir </FONT>! F2 c5 {" w8 K/ Q9 U
功 能: 删除DOS文件目录
- d7 y( K6 g$ ^1 {, e% {! q" z7 L用 法: int rmdir(char *stream);
+ U- Z; f. r6 m; I+ @, l程序例: </P>( U1 T% @8 x3 b, h& u6 l3 y9 A
<P><FONT color=#0000ff>#include <STDIO.H>( R& [: k0 y/ l- Z- z& U
#include <CONIO.H>6 S, G1 L! ]& f! M, X9 Y, S
#include <PROCESS.H>  l2 \0 m! u5 Z  N/ W: _$ ]
#include <DIR.H></FONT></P>
' ?0 l8 x' X% U- _( V<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>0 E8 Z) K% E  n9 ~1 \- U0 w
<P><FONT color=#0000ff>int main(void) ; j3 D: Z; O- G; Z) V, O
{
, X# g( R! E3 c: @. |" Q" Y, zint stat; </FONT></P>
$ l# V  z  o. ~<P><FONT color=#0000ff>stat = mkdir(DIRNAME);
1 W( I2 D1 X  N9 R1 {if (!stat)
- g: O& k  C8 n4 X( ]) [printf("Directory created\n");
/ V4 y! I9 }' `6 @" J' }& relse
# v- X+ E  h' e- V! L' Q8 S8 Q{
3 b" I7 b" Z9 ?, B. l' J- \printf("Unable to create directory\n"); 6 k$ D# G9 N5 Y
exit(1);
+ P0 K. F2 K0 W; d2 w4 T2 ?+ @! p} </FONT></P>. i9 p9 x& Q  y$ I$ Y* l& I: u/ n
<P><FONT color=#0000ff>getch(); 7 [% E6 S' j% M4 T0 S
system("dir/p"); # {! Z, Z3 ^- ^
getch(); </FONT></P>
7 r# A/ w  d4 X! W: o" p<P><FONT color=#0000ff>stat = rmdir(DIRNAME); 6 R  R4 r1 S8 x2 ^
if (!stat)
% T' {7 R1 s. O8 w1 V. ]printf("\nDirectory deleted\n");
( T% t9 |, f. g  a. ]: |+ }else
: F; T: J# e+ G1 r; r{ 6 F% E8 u; J9 p6 l6 Q2 G$ k
perror("\nUnable to delete directory\n");
+ h! d8 a, ]; Zexit(1);
/ N! F7 m- w( A* u} </FONT></P>' H/ h) _- e. @
<P><FONT color=#0000ff>return 0;
- M4 F7 e7 j5 W5 n} </FONT></P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

fastpost qq
收缩
  • 电话咨询

  • 04714969085

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-10-31 23:42 , Processed in 0.250188 second(s), 52 queries .

回顶部