QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
  U9 b# B* j/ t. h! o( w# ]" Z功 能: 使用快速排序例程进行排序
. Q2 h! ~! e4 |! ^用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
1 A2 x# \+ t  O1 I程序例: </P>  x2 D. }1 l, R" b
<><FONT color=#0000ff>#include <STDIO.H>' s" r/ W& R* s( n" L
#include <STDLIB.H>
: Z: h' K  w9 Y0 y7 c4 L# d( l#include <STRING.H></FONT></P>* y: G# D# I7 ~$ b
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>5 [* R1 m* ^' I
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
* O) P# Q7 [4 |$ X& {5 y+ i% Z</FONT></P>1 w% x) T4 g" v+ K4 `
<><FONT color=#0000ff>int main(void) : J+ W+ Q) z2 {# J, a6 r
{ ! }& y% _2 T1 v! |
int x; </FONT></P>
) B# E: W0 g5 x" A<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); ( m4 o" v; r" N5 Z, U" _
for (x = 0; x &lt; 5; x++) 7 O' P% `9 W( j7 [, g. i# D( c
printf("%s\n", list[x]);
* w5 @& P4 v4 g9 T# u4 v7 M) qreturn 0; + F& c$ f/ Z7 @& x! M( c
} </FONT></P>' K+ I; V/ g* u* [% j' z7 \0 z* O* [
<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
# I( b, k5 e+ `' ^{ 1 t/ N) ^) `" \: X$ r
return( strcmp(a,b) ); 3 W# |" M$ b. z2 h" g1 s) R
} </FONT>
# n) V! n: H5 f- k' \</P>
, \2 u% u; Y0 R. l' I
, w( F( F' [! u5 \
/ h- q/ L) R2 @  V<p>; x& M7 R( S/ R1 y) W% B, K+ \
<><FONT color=#ff0000>函数名: qsort </FONT>
9 @! K8 u1 e1 e& F5 q3 j, E功 能: 使用快速排序例程进行排序 ) q; Q- Z6 O3 ?* j7 L
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); " ]% j  m2 |  ^
程序例: </P>% N* b" d* d# {
<><FONT color=#0000ff>#include <STDIO.H>
7 e$ X% |% x  e#include <STDLIB.H>
; f2 @9 d( _" Z6 U- W#include <STRING.H></FONT></P>
/ ~5 C; `# T; b, w<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>! a  F) N# X3 _0 W0 U* F$ J
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; . |; m3 K  Z- m3 w" ?0 F+ U7 L
</FONT></P>: I  _9 J0 A4 @: ]6 R7 I1 _
<><FONT color=#0000ff>int main(void)
' n1 t+ s, D& }{
- r# X! M+ T* |- G$ q* V- fint x; </FONT></P>3 @1 k+ Y# C2 T# g( a
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 2 y1 r! P( u6 n3 {# W" R
for (x = 0; x &lt; 5; x++)
: @" }. y1 g1 H# p) jprintf("%s\n", list[x]); 6 m4 @% A( c) k! m# T- ~8 k  X6 C
return 0;
% R- |9 d! X1 r, Y, `$ `} </FONT></P>
8 G: C9 y/ `, ~<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
9 ?" {' G( u4 o; m3 c$ ]" x{ * F1 w& ^! |# o/ \
return( strcmp(a,b) );
* H& G- }& \+ a5 T, |) @1 d* W4 H}</FONT></P>& |5 a3 k: R" N; k  c" Q/ Q% ~: I/ U
<><FONT color=#0000ff>0 n$ L! I9 s8 q& Q! @+ z
</FONT>/ ]3 v9 \% x/ S2 U; V+ y; j7 d. a
</P>
( _3 S8 T& N6 J# T* y0 d! t, t<><FONT color=#ff0000>函数名: raise </FONT>+ j4 U/ h- J) W
功 能: 向正在执行的程序发送一个信号 5 _  j* V) U: j/ h
用 法: int raise(int sig); * z) {0 [" k8 T3 U2 _% Z& [" \4 y
程序例: </P>4 m) I' H: C6 l4 K* m2 t
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P># H2 U( D' d' Z! D3 ^1 i& E  v
<><FONT color=#0000ff>int main(void) 9 ]6 I$ ~% g* l& f
{ 5 i6 [# l, g/ T% C6 u# R
int a, b; </FONT></P>
8 n1 ]* s1 O% q6 C<><FONT color=#0000ff>a = 10;
% j' H8 \% e/ {- @b = 0;
  M) m, {7 O  Cif (b == 0) 0 U" S0 g& C. G* [6 W3 g
/* preempt divide by zero error */ ! @9 f9 ?2 i9 Z5 q$ z6 {5 k' Y& M
raise(SIGFPE);
& J* [" Q# I: V) @( Y2 T, I2 Ma = a / b;
3 B' A9 O: K2 f9 j8 C4 A/ M3 yreturn 0; # v# X+ O4 P9 j) `9 W
}
% _4 [& H' ~- ?4 M; o8 p4 g</FONT>1 @* r  [) D& a. S# D, E& G. t5 Q
</P>) l" m2 h$ \) N
<><FONT color=#ff0000>函数名: rand </FONT>
3 ^& B5 u# Z5 \1 D& \& x! x功 能: 随机数发生器 8 h3 `7 n+ U' _6 }* @! b% k
用 法: void rand(void);
- ]1 j' G+ W! I9 e0 ?6 @3 [. Y程序例: </P>1 V: P( ]: _8 h( C; ?
<><FONT color=#0000ff>#include <STDLIB.H>
" f' f6 a9 J3 y( H3 }#include <STDIO.H></FONT></P>
( O1 y" o1 e/ Z: U# w+ E$ ^5 _<><FONT color=#0000ff>int main(void) 0 Z- [% r) c1 ^" ?
{
2 K, l! T) p% r4 Y! t  f3 zint i; </FONT></P>
; J. I$ E, t0 V! `, s4 _<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n");   r. _  h$ {9 E7 S3 ^
for(i=0; i&lt;10; i++) ; X1 f! S5 o- ?; p5 n$ f) C
printf("%d\n", rand() % 100); + P# M" F7 L4 H2 o
return 0;
! V- q# m/ q) X} & V, \6 E7 d7 A- ]) j5 e6 k

4 y- S; u" b8 J+ T* X</FONT></P>
5 X* m5 ^9 Y7 f( I2 s) i<><FONT color=#ff0000>函数名: randbrd </FONT>
( S1 c" ?8 x2 R) g+ R功 能: 随机块读
4 T% s) A; a  `( s用 法: int randbrd(struct fcb *fcbptr, int reccnt); & W0 M4 ^) h* Z! x' v
程序例: </P>6 \$ s4 T8 X: T; n* `9 m+ j; q
<><FONT color=#0000ff>#include <ROCESS.H>. w  D2 P! g( d  C1 l) e: K
#include <STRING.H>
$ ^  Q7 Q/ O( _! _#include <STDIO.H>6 l2 Y; M/ h; i/ K/ ]3 ]
#include <DOS.H></FONT></P>
5 t% Z# [) l6 J7 Z3 K<><FONT color=#0000ff>int main(void) - ~9 k3 `1 F# Y8 m0 i
{
2 a# u) C( Y! |* R, Ochar far *save_dta;
4 e' i( _) x7 ochar line[80], buffer[256];
( a4 y( x3 l7 b8 Y: z' d6 gstruct fcb blk;
) j8 l2 J' u* w; u$ v' N5 cint i, result; </FONT></P>, u. L8 m! V: D" U) C( V
<><FONT color=#0000ff>/* get user input file name for dta */
4 r% ^( H( S% Lprintf("Enter drive and file name (no path - i.e. a:file.dat)\n"); - L* i0 Z9 q! `6 o
gets(line); </FONT></P>
, S: t  J# f8 i3 N# @5 U<><FONT color=#0000ff>/* put file name in fcb */ 2 z% J+ j4 t2 h9 Y& b; L4 i$ V! _
if (!parsfnm(line, &amp;blk, 1))
8 o/ u5 ?% b# a5 D: K{
: k, D  B9 X% k, Sprintf("Error in call to parsfnm\n"); # F4 X) |! C* ~- M+ O
exit(1); ) Z8 S" j; J# {0 o: V8 f( O
} ' f$ E& l9 [" Q& `& n9 ?- X  K; j
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
! e, m: A* l( t1 J. y" \# }<><FONT color=#0000ff>/* open file with DOS FCB open file */ / h# J8 H% I  m% B
bdosptr(0x0F, &amp;blk, 0); </FONT></P>
, o, w/ e4 j, K% V4 q3 g<P><FONT color=#0000ff>/* save old dta, and set new one */ $ B# N" L. V( w1 Q. ^9 J
save_dta = getdta();
2 t% h; `; o$ Z! l$ C  esetdta(buffer); </FONT></P>4 Z1 \, i9 z" U# p& E) U' u
<P><FONT color=#0000ff>/* set up info for the new dta */
- K2 o7 H& u& @' ^$ v7 @) A# Dblk.fcb_recsize = 128;
, {  `, y& _' z$ B% i. v% Hblk.fcb_random = 0L;
5 |# g% \/ f2 t5 j2 v" y# rresult = randbrd(&amp;blk, 1); </FONT></P>. X& z* I( ]: S* y& n: A4 U! e
<P><FONT color=#0000ff>/* check results from randbrd */ ' U" d& E; j4 x% R: Y
if (!result)
' Z. S1 `" z0 e% y" d+ |2 k- oprintf("Read OK\n\n"); $ h$ G- ^6 T( U
else   |- j8 A! [0 T
{ $ @# s, C# ^) l3 C8 e
perror("Error during read");
6 b; G2 v1 F5 x4 ^' texit(1);
1 R' C7 Y; ^  W! c3 ?} </FONT></P>
+ m2 u% `. }. Q" \2 J<P><FONT color=#0000ff>/* read in data from the new dta */
8 h1 j  V, V8 fprintf("The first 128 characters are:\n"); / [6 x/ ~: X* {
for (i=0; i&lt;128; i++)
  D! h8 u% y" S% f' O+ B- m9 sputchar(buffer); </FONT></P>3 {; a8 o2 |6 [! l
<P><FONT color=#0000ff>/* restore previous dta */ 8 D4 S4 V8 ~4 P, A" H9 J& B' k2 e
setdta(save_dta); </FONT></P>3 e+ B) z. z# X4 {4 x+ G% C0 C
<P><FONT color=#0000ff>return 0; & U, ~4 F. O) U/ m4 F
} </FONT>( |' b+ v" N) t2 w2 n0 ?
</P>
) b. }  ^% y3 ?! V9 ?0 u& R$ v: W' `<P><FONT color=#ff0000>函数名: randbwr </FONT>3 s0 W0 b: l" X5 E. L$ W
功 能: 随机块写 ' E4 d7 t( i( f8 M% S
用 法: int randbwr(struct fcp *fcbptr, int reccnt); 0 S& {6 ~$ R+ Q4 o( I& ]
程序例: </P>
5 }3 S$ v" D; Z6 M<P><FONT color=#0000ff>#include <PROCESS.H>
( D) ^" c% A, r. J/ D; K/ y#include <STRING.H>
; B/ }* g; R; j#include <STDIO.H>' e% m1 N8 @! j' F
#include <DOS.H></FONT></P>
' |4 X/ M. `" `9 z8 t3 c) w<P><FONT color=#0000ff>int main(void)
9 \5 I2 `5 A+ \" k! t3 U, d{
) Y6 ^6 Y3 r7 k% q- ]. P/ @# k# zchar far *save_dta;
2 k' ^6 i& [- x& `+ {2 hchar line[80];
, u9 M. \, O2 R( r8 ?char buffer[256] = "RANDBWR test!"; 8 |. m$ H: r% i) i' ~" {
struct fcb blk; : @# m" F  I  V$ G" I
int result; </FONT></P>
5 e+ N7 g0 S5 ~- C  H<P><FONT color=#0000ff>/* get new file name from user */ ! d& t: }# i5 G" Q
printf("Enter a file name to create (no path - ie. a:file.dat\n");
( [$ Y$ b9 p1 R' z. J6 ogets(line); </FONT></P>
# u7 Z' D& ]$ G: r* C<P><FONT color=#0000ff>/* parse the new file name to the dta */ % ^( Y3 o. A& x
parsfnm(line,&amp;blk,1); ( f& `- V$ R$ K- w3 T6 y# ]9 v
printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>% |' y+ @+ f) v( ]  `
<P><FONT color=#0000ff>/* request DOS services to create file */
, E* \7 z$ E" |! T" Wif (bdosptr(0x16, &amp;blk, 0) == -1)
5 _5 k9 U0 A! }! y. c1 t4 j1 K{
. m. z: b" p0 O: Sperror("Error creating file");
0 x7 \. v( T1 T* h$ ~exit(1);
3 U3 J6 [% O, _' Z- q} </FONT></P>0 \( S3 O' ^5 I) {
<P><FONT color=#0000ff>/* save old dta and set new dta */ 5 k% S# a, S9 ^9 ]2 e* Q; i
save_dta = getdta();
) Z( e) Z' `! n9 L4 m4 c. h; ^setdta(buffer); </FONT></P>, v; C) z" J7 Z3 L9 p5 N% D
<P><FONT color=#0000ff>/* write new records */ % c' e* ]0 W8 U! t
blk.fcb_recsize = 256;
& L& U( [3 x* ~# R  cblk.fcb_random = 0L; : `2 U! T- O1 d; h
result = randbwr(&amp;blk, 1); </FONT></P>; a: \9 |, S6 z4 G
<P><FONT color=#0000ff>if (!result)
- _! v' l$ k& v: P$ h* K: x3 j' bprintf("Write OK\n"); # e; l- B8 a$ ]. [2 b! B
else 7 c+ L) R1 ]( e
{ 7 j3 r. u% k2 L; V
perror("Disk error"); & S  A' c$ ~  h0 v2 _+ t: T$ z# Q
exit(1); 6 M- q7 t: }. [
} </FONT></P>
5 H$ P) o5 o. l  W' F<P><FONT color=#0000ff>/* request DOS services to close the file */
* z: L' G8 ]  B9 ?9 j% Gif (bdosptr(0x10, &amp;blk, 0) == -1) % k  m- j  w' U( m/ R% V
{ : u1 ~8 S9 Z: F# E1 ^; [/ {
perror("Error closing file");
7 a4 b" n5 C1 U. k% [+ B2 O# Wexit(1); $ \& ^; ?, A) k  M! y
} </FONT></P>. w8 {- E  e/ Z6 r
<P><FONT color=#0000ff>/* reset the old dta */ 5 `) ?+ R% ]+ l( H
setdta(save_dta); </FONT></P>
( P' [5 |# ~1 Y( \( r  n<P><FONT color=#0000ff>return 0;
. [6 _- W, J' [' K6 R# }$ |4 C}
, X: j8 Y( y/ W! ?</FONT>( ], g8 Z! Y  [# o
</P>
" B- t6 Q$ l% V3 O/ {. M<P><FONT color=#ff0000>函数名: random </FONT>0 T2 `* H$ u1 K
功 能: 随机数发生器 9 @7 F# y& b: W$ r+ S
用 法: int random(int num);
  {2 a+ V  N8 R1 _8 c程序例: </P>4 l$ A5 r; q0 i
<P><FONT color=#0000ff>#include <STDLIB.H>7 {- W3 b+ o2 k1 J8 j- ]! }3 V. I
#include <STDIO.H>
( J3 K* ~- c4 w/ Q/ K/ ?) A#include <TIME.H></FONT></P>
3 C1 z3 a) k$ o& m% Q3 F<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ 7 S9 m/ J! p2 \6 H3 {8 q
int main(void) 0 O& {- N/ J! j$ }% m
{
3 U! ~& ^' N  b, T3 W6 }+ X# [randomize();
) f2 X5 ?) g- b9 j/ |$ Aprintf("Random number in the 0-99 range: %d\n", random (100));
9 Z+ P. r5 D( V  Y( ?, J2 Xreturn 0; 1 a; V* o" I% V2 k: Y: p
}
  g8 ^: [& |4 o, g  P</FONT>
* e. E' v% z2 O1 l/ j% M</P>
' [( K- ?0 k" Q<P><FONT color=#ff0000>函数名: randomize </FONT>
( ]* }8 f9 p( i6 w/ z  }& x功 能: 初始化随机数发生器 7 S2 p, Q, I) V, {
用 法: void randomize(void); ; ^# i* S  w% l% s# b( k: O
程序例: </P>
# r) S8 b. {* @6 F4 ~% ~) L: e<P><FONT color=#0000ff>#include <STDLIB.H>, @% {' l/ i% J$ J
#include <STDIO.H>
  b( }* x' z! l7 g* u( }#include <TIME.H></FONT></P>
! U3 `9 X) S, |  p) ~" {# u/ g<P><FONT color=#0000ff>int main(void)
2 V4 c' G% N5 L. G6 I% T- s{ 5 [8 p( F! W8 x6 l
int i; </FONT></P>
- z# P7 z; p5 z4 ]<P><FONT color=#0000ff>randomize(); 4 W9 `4 M# w3 ?
printf("Ten random numbers from 0 to 99\n\n");
+ @8 Y' E5 V) L  G; pfor(i=0; i&lt;10; i++)
4 l( J: ~8 d% U- }9 a; w3 kprintf("%d\n", rand() % 100); 9 u: b7 X/ H+ t9 {( ?3 {, q
return 0; ; w* D8 S4 C4 R+ p, ~. {
}
$ y5 P7 ]2 X! ~! ^</FONT>5 m% d. y- ^5 H9 g. Q6 C& ^* e
</P>) J% w$ x0 B( a
<P><FONT color=#ff0000>函数名: read </FONT>
: q4 U7 n0 q- O* r% k5 K功 能: 从文件中读 7 m! L; \* q+ @5 M4 c+ H  l
用 法: int read(int handle, void *buf, int nbyte);
; z& R$ S+ v4 M) F程序例: </P>
8 W0 x( W- ~+ t! ^4 T, B<P><FONT color=#0000ff>#include <STDIO.H>5 j; z& j8 M) R( U! i
#include <IO.H>4 P& q+ v4 i1 k- {! G' c' Y
#include <ALLOC.H>- T% \$ i9 Q: j4 G# z1 a
#include <FCNTL.H>
- J0 J: {6 l3 @+ ~2 K#include <PROCESS.H>! y/ j6 f* u  ]# c. A& y: x
#include <SYS\STAT.H></FONT></P>
3 U& D9 O" ^  x2 ^* L9 F<P><FONT color=#0000ff>int main(void) 6 G8 m+ h" t' X; I1 m
{
9 S0 A* ^1 r5 u$ Avoid *buf; 8 F2 H; F) I) q  E& {5 Q, Z
int handle, bytes; </FONT></P>
( L# _' ~7 G8 W# s4 `4 ~, x4 a. l6 |<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>! Y& o- q. f, D/ x$ S5 t
<P><FONT color=#0000ff>/* 6 j  x! i) z  I! e
Looks for a file in the current directory named TEST.$$$ and attempts
& e% F+ s8 c( s' A* `3 e7 P1 h9 ?to read 10 bytes from it. To use this example you should create the , j1 T" s  S' j; V: U. f6 ^
file TEST.$$$   j2 p8 P0 G1 @( x) L4 m+ ~
*/
4 t$ {* s& w" Q. x2 K6 }if ((handle =   Z" N4 b5 u! u* K+ J9 D) O
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1) 9 |7 U( m8 @+ c* n  [
{
- j- m7 ?2 R6 F& v0 [: c: n* nprintf("Error Opening File\n");
, G/ m- E* O8 M0 ^exit(1); 9 F* q/ b4 M0 e* y5 C
} </FONT></P>
: H/ M8 y2 R$ F: I<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) { & C  Y( Y" o0 x" C) c' {# t
printf("Read Failed.\n"); 0 O1 i* `: c9 p2 l9 A8 _. E' U* K9 S
exit(1);
' }: y  {& Z! g7 n* |& o  H; U0 K} 5 Z0 B6 C" t6 Q" ]$ [
else {
% N: S; P( C' D, J+ @% q, P/ ?printf("Read: %d bytes read.\n", bytes);
! j: p# V: q" b- M) _+ I} 9 ~* e: k/ M: N# o0 g
return 0;
5 ^; f- i+ F9 Z6 z9 c7 f& l5 |' d# S} </FONT>
+ L8 p! z& l! f* C% b' B' f  o6 p( \3 U4 P3 A) o( H
</P>
- i+ U2 j6 |/ h0 `* d<P><FONT color=#ff0000>函数名: realloc </FONT>
- {; G  l1 B. j. j# [4 `功 能: 重新分配主存 % q4 n( s2 ^6 G( N9 _) E, k. }
用 法: void *realloc(void *ptr, unsigned newsize);
% _: ]7 w6 Q" a% A程序例: </P>6 u# }' x+ h0 I! ^) M/ X
<P><FONT color=#0000ff>#include <STDIO.H>
4 P2 O  g  {- g& _9 ^. Z% Q#include <ALLOC.H>
9 |' x" j' b' Z7 Q#include <STRING.H></FONT></P>
8 o5 C, i# E3 j- ?% q" R<P><FONT color=#0000ff>int main(void)
+ q: f5 u0 n$ T4 b* Y{ 3 x$ V7 h- X& R
char *str; </FONT></P>
4 W/ H5 i/ X2 L3 d, `<P><FONT color=#0000ff>/* allocate memory for string */ + v% S- ?+ [2 r/ Q3 d- O  U
str = malloc(10); </FONT></P>& l3 v* k; [# P, E! I( n( y# g# Z
<P><FONT color=#0000ff>/* copy "Hello" into string */
8 T5 P6 ?& z0 e8 v# y! U( w. Bstrcpy(str, "Hello"); </FONT></P>
; @3 M/ @7 a% H+ V& [* R" {<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); - E9 Q1 m  E+ x8 n  z: y2 p0 F
str = realloc(str, 20);
/ A9 l- F& n% Jprintf("String is %s\n New address is %p\n", str, str); </FONT></P>
1 W6 @0 a* a8 [& E- C<P><FONT color=#0000ff>/* free memory */
' Q8 s) t% S+ |) }free(str); </FONT></P>
; V- i( o* d; C* d/ j. A<P><FONT color=#0000ff>return 0; 6 e8 B9 N! R* w
} # I( ?9 D8 Y- c/ b
</FONT>
6 j7 ]9 q0 C) F! t) N: S</P>
$ Z4 X% d: a7 ~, N! U/ K<P><FONT color=#ff0000>函数名: rectangle </FONT>
7 l6 t5 u* Z9 v+ p- `功 能: 画一个矩形
  C$ m# r  b6 f- W3 L+ \5 u- v2 X用 法: void far rectangle(int left, int top, int right, int bottom);   F% N+ _4 N3 q' G4 N2 o& F
程序例: </P>
4 A) q' t, a& H% ]- b/ f  t<P><FONT color=#0000ff>#include <GRAPHICS.H>3 o. g& j$ X! v/ l3 }  l" a
#include <STDLIB.H>
# y% w! ?  n8 o#include <STDIO.H>+ L/ u2 A: K% K0 J
#include <CONIO.H></FONT></P>
+ B: @1 j& V2 n% q' M& L<P><FONT color=#0000ff>int main(void) ; L$ {$ q1 C; q6 @
{ + _1 K$ l+ v( u
/* request auto detection */
; v+ H+ K/ u3 g6 Oint gdriver = DETECT, gmode, errorcode;
  y# {/ q! ~+ r& Lint left, top, right, bottom; </FONT></P>
* O: U3 `) E5 ]: u5 S& K<P><FONT color=#0000ff>/* initialize graphics and local variables */ $ T0 ~% H3 I3 E$ d; g: O
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
5 X8 H5 R1 n/ j0 S4 R- Y' H<P><FONT color=#0000ff>/* read result of initialization */ ( I3 c. x" V5 E6 l6 y; d) V4 e1 r) z  x
errorcode = graphresult(); . h3 ]- }* m7 b* g" r5 M
if (errorcode != grOk) /* an error occurred */ & l, c; J: N- X0 H; L6 m' N
{ 3 M' X! B4 _$ T! ]+ [
printf("Graphics error: %s\n", grapherrormsg(errorcode));
6 D/ ]) r( G2 b) mprintf("Press any key to halt:");
" m- P& X7 J( g6 \getch();
$ E6 c  ?1 r8 k. m6 a( ~6 Vexit(1); /* terminate with an error code */
9 r% y8 E* z( x( l  A' C6 Q( A} </FONT></P>
' K, ]3 M) X- j/ s1 J5 u& g7 x7 ]<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; 2 B* a1 P- }/ |( ]
top = getmaxy() / 2 - 50;
/ w; Y* A4 ^, s" P# z7 Fright = getmaxx() / 2 + 50; ' ^- s8 {2 V- R4 x; G# W
bottom = getmaxy() / 2 + 50; </FONT></P>
: c; B, [- o" K3 j6 C; M<P><FONT color=#0000ff>/* draw a rectangle */
4 ~- E8 R/ P5 N- _: Grectangle(left,top,right,bottom); </FONT></P>
0 f/ n: J0 h4 P% j! r' g<P><FONT color=#0000ff>/* clean up */ 5 x2 A4 c% J& T8 c
getch(); 1 a& D  g. X7 e9 V; h0 t
closegraph(); 3 J' `/ t8 [, [8 S0 z& w
return 0; 8 m" n  k, E" O" f. H/ @# R0 {* j( b: Y
}
- z& l; P4 N% r! T; }8 O</FONT>
  j2 C6 Z' u' c5 Y( V/ k5 L</P>
: ~/ T1 Q( R. t$ \* J0 j! w- H<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>
8 T8 ^1 X, K* ~* q功 能: 登录已连接进来的图形驱动程序代码
/ w2 n* t' J% A. ]' V用 法: int registerbgidriver(void(*driver)(void)); ) {. S; `$ D" M) E
程序例: </P>- Z9 I" k6 _5 Y/ T5 p  \, I6 \
<P><FONT color=#0000ff>#include <GRAPHICS.H>
" O* S( I6 }6 D#include <STDLIB.H>
9 F5 _- t. V; |5 x& Z5 \( R; o#include <STDIO.H>/ h6 H: l7 n. O  A, ^) v
#include <CONIO.H></FONT></P>
& e. r+ j" \% C$ L<P><FONT color=#0000ff>int main(void) : ^5 z  _, p+ L. x# q2 J
{
  E6 S7 j% {# u- p7 ^. F/* request auto detection */ " }, [2 L; f' h3 x- \% W7 ?& Y
int gdriver = DETECT, gmode, errorcode; </FONT></P>
& k9 ?7 k7 n. {( p1 h6 o% Y<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
# z8 x) h0 b. k8 b) h( i. v; Jerrorcode = registerbgidriver(EGAVGA_driver); </FONT></P>
8 f' K! b% q, g/ P0 {; z<P><FONT color=#0000ff>/* report any registration errors */ 0 |9 i3 z( @2 X  v! y
if (errorcode &lt; 0)
% ]! N/ ?; s4 G! Z' s& e, F; R{ * q3 t& p1 ~/ i# r2 ?  R# }) ^8 d) k
printf("Graphics error: %s\n", grapherrormsg(errorcode));
1 n: u! F1 ^0 R' Mprintf("Press any key to halt:");
* f8 M( f# ^4 O% F% Ogetch(); ! M, I, u* }9 S0 V; Y
exit(1); /* terminate with an error code */
9 B7 X: Z( ]. B! O$ L- @# `8 ?} </FONT></P>9 \. e! h' c/ v$ F6 h- Q
<P><FONT color=#0000ff>/* initialize graphics and local variables */
% A+ e# q. m- vinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>% w' ]2 y+ q1 J/ ]$ H! S
<P><FONT color=#0000ff>/* read result of initialization */
; F- F2 r1 }# s0 ~errorcode = graphresult(); . c& W4 ]8 _, n8 z
if (errorcode != grOk) /* an error occurred */
" x1 W0 a* R! y' n1 k, [{
8 ~  O1 ]$ x. A, `' \) |+ Aprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ( G' P, L( B) b7 |3 [
printf("Press any key to halt:");
/ [: O* L& B5 l% f! Xgetch();
* m: c  a. v: p* pexit(1); /* terminate with an error code */
2 B6 e8 d% u1 H! D, s} </FONT></P>
0 w2 j( m- D8 c- T# |9 z2 |<P><FONT color=#0000ff>/* draw a line */ % A2 V) h5 \$ X1 S) q& `8 j
line(0, 0, getmaxx(), getmaxy()); </FONT></P>, \; [7 H3 _2 j* A( d" ]
<P><FONT color=#0000ff>/* clean up */
" b+ q% R# l) fgetch(); 0 N# ]* b' I3 J4 j. N4 y
closegraph(); 1 s. `; G0 S* v% G# W
return 0;
# D. d0 z3 m$ ^6 I* _}   G: e: Z2 \+ W# e' @
</FONT>" e; _( |! U* l5 H9 S- h) f
</P>" }% j- J) p- ^4 n$ m- L8 q0 t
<P><FONT color=#ff0000>函数名: remove </FONT>! w6 D' n7 ^6 \2 Y% @3 ~+ S
功 能: 删除一个文件 " ?2 G# \0 c8 k- d7 o
用 法: int remove(char *filename); / j6 l4 }, v" U, c: F/ v1 j0 Y! Y. l
程序例: </P>  O( q' e" c3 o# N# n% ?" i
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
3 w. L0 R: |  c3 ~; Q+ S9 P<P><FONT color=#0000ff>int main(void)
; m- a& ]3 A. `) T0 B( e{ . I/ N/ B5 g% b- b* o
char file[80]; </FONT></P>
7 Q5 \" V4 _: I+ Y2 o2 U<P><FONT color=#0000ff>/* prompt for file name to delete */ , P( B7 }7 f1 G0 o
printf("File to delete: ");
. g: D2 B( k" @+ Rgets(file); </FONT></P>
# p* O( H  Q9 i7 f/ k<P><FONT color=#0000ff>/* delete the file */ , w/ u$ ]1 _% w6 F" `, \  J
if (remove(file) == 0)
+ W7 h2 q) f5 `0 R  l( Xprintf("Removed %s.\n",file);
% X% I  m  L* P9 l8 {% Y# Y  N: Q1 _% helse
3 L/ S+ A/ P  h9 _perror("remove"); </FONT></P>
6 e2 A+ T. `9 k# Y<P><FONT color=#0000ff>return 0;
. [/ H3 E/ t7 X: i- W}
" h9 a' K( I& `
1 i9 I0 P. N& d4 P) w</FONT></P>. X' i9 [3 m" l3 @2 \8 L5 n
<P><FONT color=#ff0000>函数名: rename </FONT>
2 ]( x: P: c  A3 `# M6 y3 }功 能: 重命名文件
- |0 r+ U2 i6 j, [% O% G用 法: int rename(char *oldname, char *newname); 9 H( E, q9 u6 o1 h5 a
程序例: </P>
/ L2 c* |4 @9 R4 e" U<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>2 B# ]" `' x/ q% w
<P><FONT color=#0000ff>int main(void)
* ]5 [% w) T4 i3 p/ p( Z2 N* p8 e{
: k" I5 m0 X' gchar oldname[80], newname[80]; </FONT></P>
8 {+ d1 r# X: n8 P<P><FONT color=#0000ff>/* prompt for file to rename and new name */ # j/ @( v+ ~$ j: w# D
printf("File to rename: "); - M7 H) n3 z' y
gets(oldname); # \3 @6 ~; {+ G6 H9 N
printf("New name: ");
4 u3 Q2 m0 M4 Z9 X/ @; ?gets(newname); </FONT></P>% X" @0 D# I4 q
<P><FONT color=#0000ff>/* Rename the file */
( n: W% f3 |) {$ b5 n$ [if (rename(oldname, newname) == 0)
4 z% C9 F# M1 E$ Vprintf("Renamed %s to %s.\n", oldname, newname);
. k" m" k* E) velse % B, O9 g+ _! R
perror("rename"); </FONT></P>/ ]3 Q7 O9 I$ I7 W$ @" I' x
<P><FONT color=#0000ff>return 0; ! ^! k$ v/ y6 f5 C+ t. a2 h$ ], C
}
. Q) \' L5 W8 h  ?$ |4 N5 o. s; A( b( J/ h1 l
</FONT></P>
, X/ q; A/ [5 r! _4 h( I" ]<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>8 f2 r( m7 E" W& I; N* @
功 能: 将屏幕模式恢复为先前的imitgraph设置
, J0 T) T7 F* T1 V用 法: void far restorecrtmode(void);
4 \, T* j: @! [程序例: </P>9 l+ H, R. g# W' g4 a9 x
<P><FONT color=#0000ff>#include <GRAPHICS.H>; b. L( d% F; f5 R5 }- r
#include <STDLIB.H>
. g6 q5 l9 @( h+ l#include <STDIO.H>; E) k! H) U7 h
#include <CONIO.H></FONT></P>. i6 F/ i. A/ {/ z( }# K
<P><FONT color=#0000ff>int main(void)
5 A( Z/ |4 H& T{   F, c) ^7 ~5 N5 q% t
/* request auto detection */
7 ^( T1 e4 m  e6 }int gdriver = DETECT, gmode, errorcode; , d5 |# C# P; y9 s! e& n1 l2 H
int x, y; </FONT></P>6 f. K# S, ?4 s
<P><FONT color=#0000ff>/* initialize graphics and local variables */
) t5 j) f$ P, I' D: w/ ainitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
% b: G8 i1 k4 p6 z2 j' B; e<P><FONT color=#0000ff>/* read result of initialization */ . C( ?3 P3 T8 t, k0 r" U
errorcode = graphresult();
# ]1 Z0 w% g, m- }# V; ]if (errorcode != grOk) /* an error occurred */
5 h& K# a! ~4 [- I6 h$ b{ . U% E$ }( ]7 [: v( j, E
printf("Graphics error: %s\n", grapherrormsg(errorcode));
5 p5 b  c3 a' L5 p' a0 g! Vprintf("Press any key to halt:"); 2 R# V9 ]# c; v' H) D; p& \+ Y+ K
getch();
- l* e  }; e2 _4 f+ H! Cexit(1); /* terminate with an error code */
+ f* G+ J: q8 a& {) B1 P8 Z} </FONT></P>7 x+ Q+ ~- o; [4 h8 D3 y+ {* \
<P><FONT color=#0000ff>x = getmaxx() / 2; 7 K; }/ s7 P' T$ M/ x
y = getmaxy() / 2; </FONT></P>
( @4 l7 ]! B; t% a; t/ w! G<P><FONT color=#0000ff>/* output a message */
* |4 }9 k5 U1 N9 w9 Isettextjustify(CENTER_TEXT, CENTER_TEXT);
2 y: |' }: g7 {6 W. u4 ]outtextxy(x, y, "Press any key to exit graphics:");
. A- b. ~! H; }/ @getch(); </FONT></P>
/ }+ j, y" c9 g# p* S<P><FONT color=#0000ff>/* restore system to text mode */ 2 [" F# y0 W3 t: P  m
restorecrtmode();
- {1 l+ Y! Z5 d* J0 wprintf("We're now in text mode.\n");
: g2 i. `4 ?3 b7 _6 Nprintf("Press any key to return to graphics mode:");
3 a. Y9 T. G; j; n' j  Egetch(); </FONT></P>
- I, [# w2 {! ?6 L6 u% m/ }, R<P><FONT color=#0000ff>/* return to graphics mode */
; I- P/ s" Y! T& [% Osetgraphmode(getgraphmode()); </FONT></P>9 F% [. f8 b1 q1 V! s, A" c
<P><FONT color=#0000ff>/* output a message */ # i: @0 t# @8 n/ d" e; _. M' _3 Y9 D
settextjustify(CENTER_TEXT, CENTER_TEXT); ' G$ L) v8 I: M6 o* }4 E' W9 f' P
outtextxy(x, y, "We're back in graphics mode."); / D  j8 L- x$ E2 h
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>" j- s- G7 x6 T! U" I
<P><FONT color=#0000ff>/* clean up */
9 ]+ P4 Z. T+ jgetch();
2 d) ]/ J$ G( X/ [" q. I7 l4 e- h% Fclosegraph();
/ q7 _% `& v: A* M8 }return 0;   g/ }9 M7 R: d' O" T/ e
}
/ h( k, ]: B; z! Q& [+ [& ?</FONT>* p/ y" P) j8 C! D+ o  |
</P>8 ]- }& C4 ]* e4 D9 Y& K
<P><FONT color=#ff0000>函数名: rewind </FONT>
, v# ]/ s. W( ]. x2 a) p功 能: 将文件指针重新指向一个流的开头 + E) q; f9 {' x. R
用 法: int rewind(FILE *stream);
* i: s! o7 o4 @4 s$ R+ L8 U程序例: </P>& ]! j- m" @/ Q' n) D' Y
<P><FONT color=#0000ff>#include <STDIO.H>  I& g- X1 @2 _" y6 x7 @4 G
#include <DIR.H></FONT></P>
6 Z" s9 n5 }( r$ b# L<P><FONT color=#0000ff>int main(void)
+ {! [( @& g% ]! \) q( {{
+ M1 V& k8 }" W" Z5 d3 wFILE *fp; + |" U+ S$ G9 |: Z* B7 O& `, a
char *fname = "TXXXXXX", *newname, first; </FONT></P>( y. L5 r) w. n1 V' t/ x
<P><FONT color=#0000ff>newname = mktemp(fname); $ [; r( _# ^" I+ e8 \
fp = fopen(newname,"w+");
% e6 @; ~8 Q8 `  |' @2 E! i- L3 efprintf(fp,"abcdefghijklmnopqrstuvwxyz");
* G) _/ C" \2 ^$ F& V8 ~  g  ~rewind(fp);
: p5 u* q" Y: Wfscanf(fp,"%c",&amp;first);
2 Y# w4 q& ~% z8 M! s! a2 hprintf("The first character is: %c\n",first); ' `9 k$ U) P0 L0 x( @
fclose(fp); 3 u* o2 }9 R! z/ {9 a" b; P
remove(newname); </FONT></P>. r. q! |  P, h' b, }, ~" C
<P><FONT color=#0000ff>return 0; / L% p( X) Q1 @7 {( i
}
1 i, d( T  _/ @2 t' C) L3 A</FONT>
  f8 w! t3 b; `3 Z+ S- w</P>3 h& S1 ]: k# q5 t, I
<P><FONT color=#ff0000>函数名: rmdir </FONT>
6 {- h" U$ i* _/ H  a0 o$ x功 能: 删除DOS文件目录
! d/ V6 W* Y  Z. B) t" K! F4 a用 法: int rmdir(char *stream); 5 O5 E  e) y- k9 e
程序例: </P>
. ^% D, z/ A/ \( u1 I<P><FONT color=#0000ff>#include <STDIO.H>7 P! W- L) k% a7 X! `
#include <CONIO.H>) r1 A5 a- k$ E6 |( |) H, \
#include <PROCESS.H># h& j6 G2 Z* Y4 E) t
#include <DIR.H></FONT></P>
1 Q8 b! @' @$ i' Q* a3 L: |' |  h; ^+ k<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>
6 t3 l) S+ C- j- D' S<P><FONT color=#0000ff>int main(void) . h2 ]! V6 ~, N( |" |6 m' J2 v
{ 0 Q( X; b" F2 l" Q  A
int stat; </FONT></P>. {* z; A. E0 j! U' E7 j
<P><FONT color=#0000ff>stat = mkdir(DIRNAME);
  z3 i8 U0 n) \8 d- Sif (!stat) - \" M- l- p. l/ w% f
printf("Directory created\n"); + c' n2 |7 g4 ?/ b' F" x5 m+ i
else 1 m2 _" r) Y& U1 A. \# v
{ * _+ X- ]( P% U) d- _/ H
printf("Unable to create directory\n");
+ m4 }$ P4 c" n) C3 w8 z, k, @exit(1); + E) I7 h6 T8 Z6 u" Y6 Q6 s8 D
} </FONT></P>
, O6 R8 p# h9 m3 W' x<P><FONT color=#0000ff>getch();
- c! s) R5 _1 P3 @system("dir/p");
1 N9 N+ g5 G1 }; _; ygetch(); </FONT></P>
9 S. f( ^% }  r) e  R<P><FONT color=#0000ff>stat = rmdir(DIRNAME);   ~  R% Z; j7 F
if (!stat)
: ^( s# m5 R  n* P( Qprintf("\nDirectory deleted\n");
8 N' O7 V1 h$ P+ q7 h1 Welse
: y+ @1 J* h3 |) O{ ( @( m  Z% x; |' s
perror("\nUnable to delete directory\n"); , [. w& c8 V, y4 j" u
exit(1);
% x; N; ^$ }$ \- w6 [3 s& z} </FONT></P>$ f* E) D$ q, @2 X
<P><FONT color=#0000ff>return 0;
: B5 J1 g" V9 S  h( X% Q} </FONT></P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

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

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

蒙公网安备 15010502000194号

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

GMT+8, 2026-4-20 11:49 , Processed in 0.423050 second(s), 52 queries .

回顶部