QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
2 C0 C9 B( _# H& j1 K$ f功 能: 使用快速排序例程进行排序
% P3 J/ l5 q/ M! G用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
# }$ k* R1 x! q3 l程序例: </P>' ~/ a. q. f; ]4 d1 r  t" q5 m
<><FONT color=#0000ff>#include <STDIO.H>9 T5 H$ b) W. B+ i
#include <STDLIB.H>
+ B$ \) h- O( S#include <STRING.H></FONT></P>3 `" O; Y8 r5 v/ u- s' x
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>; Q) [9 _9 q9 D- M: Z7 t% \
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
, j# z; G1 p* x- T* z; c. ~3 I</FONT></P>2 ^  t( D1 g4 n# o. q9 v
<><FONT color=#0000ff>int main(void)
# G3 t3 z' h, P4 D. Q( I" x3 [$ |{
9 V/ W# t& v/ @% d# iint x; </FONT></P>5 h& E6 p- {! D+ g9 G4 q: M& x9 G9 H
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); $ j: z' J0 U' W4 A  p
for (x = 0; x &lt; 5; x++) , y+ p6 T/ {6 B1 B) W$ ^
printf("%s\n", list[x]); - t) _; |1 p' K' J8 Y
return 0; ( X) z1 f( q4 y" t6 l
} </FONT></P>& H7 m$ E; h! \
<><FONT color=#0000ff>int sort_function( const void *a, const void *b) 5 j1 a9 j% }+ D, m- d5 ?
{
; l0 t0 t8 r. {& V5 N) ureturn( strcmp(a,b) );
0 L8 }) T6 X) C} </FONT>+ {; i4 W, ]0 R1 k! R6 r) V/ p( i
</P>- C3 g$ T; ]. r3 u* D2 l) \9 T
- Y: |+ t8 v2 o/ {/ E1 _( ]/ m

" }" D' }; f$ _( r" z<p>6 p/ H1 G# ~7 U# t  ~% A2 b
<><FONT color=#ff0000>函数名: qsort </FONT>. }$ B5 z) W1 U* H+ z
功 能: 使用快速排序例程进行排序 & P4 |' u: U, U  d; h2 y( d
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
/ L/ Y6 U6 [) U9 G4 Z3 R! _: ?% l程序例: </P>
' ^/ N6 x1 x& K( P  e/ h4 l9 @$ [<><FONT color=#0000ff>#include <STDIO.H>
' E& H1 Y2 g# _3 N% a  i#include <STDLIB.H>6 ^5 c4 M* Y) n, C, a
#include <STRING.H></FONT></P>
* P$ N& ~, N4 p& l4 O# r<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
7 e* N$ X9 t8 p: z* W<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
* r2 L2 b! f' ~1 }; v</FONT></P>
8 @7 X1 D# ]8 F9 F5 v: n0 Y8 t<><FONT color=#0000ff>int main(void)
9 Y/ N9 r+ q# `. m- Z{ # @  [  L" h0 f; ^" ]5 f
int x; </FONT></P>, ^! f9 J. ~5 d
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
$ G# b* t, A6 h, ]6 Q2 W0 ~for (x = 0; x &lt; 5; x++)
% X  J/ H; }! d, mprintf("%s\n", list[x]);
7 G$ {1 j, M+ i% Freturn 0;
2 q* Q. [# }! h: m- t6 _) f} </FONT></P>
6 N4 [: Q- o6 D; r+ g+ b7 h' N+ K<><FONT color=#0000ff>int sort_function( const void *a, const void *b) , c+ r1 p4 N. ^* O! R8 M( \; u
{
3 e. g- c; ~. ~return( strcmp(a,b) );
9 S/ _* o0 d! t7 r# L}</FONT></P>
% l# j: n5 t0 ?* Q2 W& n<><FONT color=#0000ff>
' E" w  M0 \: B: l2 W+ K! a* }7 a</FONT>1 {2 T. \* f( N; Q) G2 G* o
</P>
& V; ~3 e# v4 [0 ]* J* Q' b<><FONT color=#ff0000>函数名: raise </FONT>! L3 t) y4 j0 w* C4 m
功 能: 向正在执行的程序发送一个信号 7 K% K# X! G% ^9 M6 E
用 法: int raise(int sig); " Q. [% {0 x5 N8 G% v
程序例: </P>/ Z6 {7 n, Z$ g3 z, B- I" J
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
# m0 |: W$ M$ n. X% }<><FONT color=#0000ff>int main(void) 6 O1 \- q* q6 ^8 V9 o  m/ K$ H0 N
{ / g; R9 T7 @; ?* Y% T  G0 k9 E; T7 U
int a, b; </FONT></P>* T! @; P5 W. [- P# [; s
<><FONT color=#0000ff>a = 10;
, t* u. Q, ?4 A5 e/ w1 z3 ^1 [b = 0;
8 f/ R/ X$ \/ L" t- o0 G0 ^! vif (b == 0) - N, s  K1 Y2 I  [0 [- n5 C, B
/* preempt divide by zero error */ ' u+ w; h/ T6 i5 W! g2 _! A* a
raise(SIGFPE);
+ u2 J$ P: D. Y) h8 h: m8 s; Oa = a / b; , \5 K4 n7 J1 F
return 0;
% Q. Q+ y; s1 U, H) K0 F} 5 G+ V/ x2 B0 `6 ?
</FONT># u" k5 t% P  {/ a, }
</P>
! {6 Q* B' [* U$ @<><FONT color=#ff0000>函数名: rand </FONT>
; B+ d5 d  ]8 P5 \4 F功 能: 随机数发生器 6 ~, l  H7 |! w- q( n
用 法: void rand(void); 7 t- j1 w& g' O# q' P
程序例: </P>
- I& e% Z$ d7 S<><FONT color=#0000ff>#include <STDLIB.H>' X5 Y/ j% T+ R' h
#include <STDIO.H></FONT></P>
! f3 w7 I0 K7 x<><FONT color=#0000ff>int main(void) ! f  [2 |. r& |4 \: Y* f
{ * \2 a' F. J4 V3 l+ I$ Q
int i; </FONT></P>
# E6 M3 ~8 @3 v1 F" B) y<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); + t* O7 {/ P/ J7 d
for(i=0; i&lt;10; i++) ; o2 o( I1 F& D
printf("%d\n", rand() % 100); + d+ T: C! \0 F: t7 y& X4 \
return 0; * Z. T# N. f( Y4 M% D
}
# a! F9 a/ W8 g+ h
+ J& P6 B! z1 h: Y: d</FONT></P>! m% m5 i) U8 F& l5 M
<><FONT color=#ff0000>函数名: randbrd </FONT>% S8 ?* t) ?6 W) ]! [1 H
功 能: 随机块读
- {- C; x9 V+ N' a' i6 c用 法: int randbrd(struct fcb *fcbptr, int reccnt);
; \1 o/ E7 b6 p9 i程序例: </P>2 L% t8 g/ a- h' N$ \  d; ]7 l: z( B
<><FONT color=#0000ff>#include <ROCESS.H>
7 [- C5 z  w: _' a$ J4 R#include <STRING.H>
1 y  [" H  N, s  H#include <STDIO.H>
& r7 q- b, G, z. t  k#include <DOS.H></FONT></P>3 }. ^! ]8 F9 G4 U" E
<><FONT color=#0000ff>int main(void) 8 f5 d1 i" a0 c1 m+ @0 ~* c$ V
{ % v3 G- |  T: H5 O6 W* L
char far *save_dta;
: H0 e! }, G1 }6 L6 z$ D7 O- lchar line[80], buffer[256]; 1 X8 ^6 C$ X7 \$ X0 g6 U- W
struct fcb blk;
- `5 h2 [. s. y" n# o5 dint i, result; </FONT></P>$ F3 E( P! I% `6 e. q, }& {
<><FONT color=#0000ff>/* get user input file name for dta */
/ C2 ~$ h" V% m% I- c2 Cprintf("Enter drive and file name (no path - i.e. a:file.dat)\n");
& W& e6 |1 j4 P, }4 l; Ugets(line); </FONT></P>. p# L1 C. N* u8 z3 P
<><FONT color=#0000ff>/* put file name in fcb */
: v9 A2 G) `1 Q3 x! p1 aif (!parsfnm(line, &amp;blk, 1)) ( k0 \1 a" X# }6 r% h, u2 r! M( z+ J
{
3 y5 I# l  Y9 o9 ~* M6 cprintf("Error in call to parsfnm\n");
6 i) e+ ^4 U1 Q2 c+ B3 S) Z% Q, |, xexit(1);
/ B, C  u5 ^/ N} : E3 ~1 S- C; S$ ]- b- B
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
9 b& U" K2 b( ?- ^<><FONT color=#0000ff>/* open file with DOS FCB open file */ 6 k1 f& U1 d& G- n
bdosptr(0x0F, &amp;blk, 0); </FONT></P>
% w) J' L$ N( a<P><FONT color=#0000ff>/* save old dta, and set new one */ 3 j" H7 V' ]% W9 E
save_dta = getdta();
) |- D! A% P& M; ]% @setdta(buffer); </FONT></P>* J5 b8 i! ?6 M+ s0 a; l2 _
<P><FONT color=#0000ff>/* set up info for the new dta */
; V( z  N6 T8 }# j' F) h6 Qblk.fcb_recsize = 128;
0 w, {: [" @& ?; e% I2 [. o( ]& nblk.fcb_random = 0L; 8 F6 Q& i1 y- e4 Z9 z: h
result = randbrd(&amp;blk, 1); </FONT></P>" J7 P; u2 C3 O/ \  s# y8 i5 G
<P><FONT color=#0000ff>/* check results from randbrd */ $ a; E; E* s8 l: f2 l+ N" f6 M
if (!result) 8 K' q; _8 H2 U3 ~2 j3 `
printf("Read OK\n\n"); * Y% n+ G  A0 t+ T% W. q3 |; ~
else
4 e; o1 z! }- s: }0 [{ 4 D% i% o* k; I
perror("Error during read"); 6 \8 _. m* S) @7 B2 H+ r) @
exit(1);
! C% `, w7 ~0 L+ g} </FONT></P># `3 A5 p/ {0 w2 _
<P><FONT color=#0000ff>/* read in data from the new dta */ ) {; j7 ~- E% o& E# c
printf("The first 128 characters are:\n");
; A8 X; q4 ~. I5 C8 Mfor (i=0; i&lt;128; i++) ( e; O8 x/ B+ U3 p7 r
putchar(buffer); </FONT></P>
+ O, i5 `! R: T  z! r/ G' f( Z<P><FONT color=#0000ff>/* restore previous dta */
* n8 X. H  D, y# ]4 h3 U1 Q$ M& _setdta(save_dta); </FONT></P>
4 ^7 F! i3 L1 B$ x% h9 V<P><FONT color=#0000ff>return 0;
, Y. c9 Z) f0 @, w2 z} </FONT>; E; _" g7 o$ _3 d, C
</P>
/ v5 Z) c9 u6 D/ k<P><FONT color=#ff0000>函数名: randbwr </FONT>
+ l2 T1 ~/ j8 }" r5 E8 L功 能: 随机块写 2 S$ f9 @4 q; Y9 c
用 法: int randbwr(struct fcp *fcbptr, int reccnt);
" n4 Y# r9 u, [4 X" K( n* x7 m程序例: </P>
/ Q& d% [' ]* x) T: H1 e<P><FONT color=#0000ff>#include <PROCESS.H>; o1 G- R* [2 E) w
#include <STRING.H>0 V* I& X6 R, E3 p# n* L
#include <STDIO.H>" J1 s9 `( ]# }6 H) F
#include <DOS.H></FONT></P>
6 T' J- n9 f4 \( R4 u6 B( j<P><FONT color=#0000ff>int main(void) 4 n- [# \4 v* @* n2 ?
{
6 R! Q  P- P: vchar far *save_dta;
5 e! o: X" Y1 p- hchar line[80]; 8 T# g! j2 K' b
char buffer[256] = "RANDBWR test!";
/ z& V+ p! C, x5 {$ V& r: Q) L" Xstruct fcb blk; / D8 F9 t: O" c* g% x; k
int result; </FONT></P>5 \# p& S5 |6 s! i2 e$ p' R8 Y) T
<P><FONT color=#0000ff>/* get new file name from user */
7 R7 d1 _7 ^! e' n: p) y' L( x. k- E; Vprintf("Enter a file name to create (no path - ie. a:file.dat\n"); 4 N0 A1 i8 _  n/ z8 [
gets(line); </FONT></P>
* [2 p: c# h; {) u, N5 b<P><FONT color=#0000ff>/* parse the new file name to the dta */
( j" V! k. D8 J- A' q4 n! a& m- eparsfnm(line,&amp;blk,1);
: W; i) Y" ?3 W* \: H! ]printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
' U9 \' R/ x4 j$ u  ]<P><FONT color=#0000ff>/* request DOS services to create file */
" l( ?3 X8 R, E7 I9 _if (bdosptr(0x16, &amp;blk, 0) == -1)
5 M  i( i  X9 w0 q; B5 Y3 J% P{
6 _: c8 M0 r, B! ^perror("Error creating file");
" r+ U% @8 |9 ]- N/ {7 P+ `. mexit(1);
9 X; B9 ?' r$ g$ F, y1 K" a} </FONT></P>
' H2 T# i! H4 p3 W<P><FONT color=#0000ff>/* save old dta and set new dta */ 8 {1 F7 X/ h; C6 E9 v- Q
save_dta = getdta(); & x' m( g# L1 f8 J! M
setdta(buffer); </FONT></P>
. |' w( }3 O0 x# h/ F1 I<P><FONT color=#0000ff>/* write new records */
: j# G) p% B# [blk.fcb_recsize = 256;
  |! f+ v2 w% c1 ?) |blk.fcb_random = 0L; % R0 V% D* |" p( i* l0 ]8 T3 J
result = randbwr(&amp;blk, 1); </FONT></P>
0 R& A  f  Q$ _% N$ p2 K<P><FONT color=#0000ff>if (!result)
8 q4 ~' x% R. ]( J% w% ?printf("Write OK\n"); 9 [$ e. ?4 ~& @
else
! `7 u+ h; P8 C% I7 ^9 V% K{
% D! H, D; k  z8 T: Q$ t; operror("Disk error");
' _! W- U  {) \9 Z  v' `exit(1); * r0 A5 \9 ~+ A5 ]. z7 q
} </FONT></P>
6 Z, e3 i* f1 N<P><FONT color=#0000ff>/* request DOS services to close the file */ * E6 y# J+ A; z1 _6 N) Z
if (bdosptr(0x10, &amp;blk, 0) == -1) % F" |; m; W8 }
{ 9 w+ F7 o6 g2 T1 k( `, t
perror("Error closing file");
9 d# J2 m% g8 iexit(1);
( r9 a" z  |2 n" k} </FONT></P>% A2 M$ [7 c8 G4 C
<P><FONT color=#0000ff>/* reset the old dta */
( V5 S% `' A9 {$ A. r( Usetdta(save_dta); </FONT></P>; G! [. T' ]5 v: h
<P><FONT color=#0000ff>return 0;
' R$ }' n, [- _- _- f}
0 ]# d" ?' \; P0 z</FONT>% ~- M6 i7 k' f
</P>
' @# x3 ~9 l; V<P><FONT color=#ff0000>函数名: random </FONT>& [: |0 ~1 e8 P( i+ v# E
功 能: 随机数发生器
5 Q5 A- b8 `/ A6 k7 A: M用 法: int random(int num);
" Z% h$ g/ A- i; T4 _) T- V程序例: </P>* @2 z: B% }0 w- |; @7 D
<P><FONT color=#0000ff>#include <STDLIB.H>
8 a- u9 {) \0 B( P#include <STDIO.H>
3 F: Y$ a6 l2 V, v+ {#include <TIME.H></FONT></P>" [  j5 f- L$ {6 A9 S  m
<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
+ q" ]5 `, L6 a2 c- P$ x3 [int main(void)
9 J% n7 ^* D/ j+ v. I{ 5 W: E5 Y! J3 j) j0 e2 t
randomize(); 3 `/ K3 ?3 T' Z; x
printf("Random number in the 0-99 range: %d\n", random (100));
5 g3 J3 K4 x. P  e( |( mreturn 0; . R( @# p+ H9 D
}
" K9 e% ?. y! ?" @* H. {+ Y</FONT>
" R' I& W" M, v9 _1 L</P>
" J) u' n, P; F! @7 a8 _; j<P><FONT color=#ff0000>函数名: randomize </FONT>
8 L& w, p* W2 F- W功 能: 初始化随机数发生器 $ `2 O' B) X- t4 O, W! A, M
用 法: void randomize(void);
4 h  J! |8 c) D6 m* ]* i; r7 ^程序例: </P>1 n# @0 |9 a1 |9 \  ~3 h
<P><FONT color=#0000ff>#include <STDLIB.H>- [6 T8 C7 s# [) Y
#include <STDIO.H>' s, k$ M, _: k- O. h' O
#include <TIME.H></FONT></P>1 @* D% J' E4 P' E5 b( }
<P><FONT color=#0000ff>int main(void) 6 _' @, y6 w! u8 x+ Y6 Q( N
{
9 E2 n0 U  D" u; P9 F6 \int i; </FONT></P>
: f  [. V# X9 r4 _<P><FONT color=#0000ff>randomize();
0 V! \: N) L' \0 n$ k; ~8 Qprintf("Ten random numbers from 0 to 99\n\n");
5 L$ y, ^* t" b7 d7 P- [for(i=0; i&lt;10; i++)
) h: A  ?- n8 w9 R2 ^" Zprintf("%d\n", rand() % 100); 7 V/ d" }; Z, Q6 J. {8 ?
return 0; : {/ {3 `# B; ~" v+ b! q
} 7 M$ p. u: P/ n
</FONT>
+ P+ A) K" m8 u% \% ^( H</P>
9 s* L! [$ a, f/ [+ Q<P><FONT color=#ff0000>函数名: read </FONT>
, @4 x! `% \3 j0 e3 b4 b功 能: 从文件中读 ' F5 P  a( m" z% ^- V# E
用 法: int read(int handle, void *buf, int nbyte); : \# e8 f, L2 `9 C2 B: O
程序例: </P>: T5 ^& \. T1 C' _
<P><FONT color=#0000ff>#include <STDIO.H>
- B9 M' x# [# V3 b#include <IO.H>7 j0 m- B6 H- J0 {
#include <ALLOC.H>2 B* j0 T+ g4 o/ A5 p
#include <FCNTL.H>
1 ?# W; t: \' e3 n3 k$ N#include <PROCESS.H>
7 c" {+ w! ?# c# Q" N  f#include <SYS\STAT.H></FONT></P>, y! G5 A2 b% M5 I
<P><FONT color=#0000ff>int main(void)
- Q/ O* r, Y8 R  V. d" H4 X{
2 A0 w+ R0 A' s4 Q+ n) V: Jvoid *buf; 2 H, |! m  Z; `0 l! r, f
int handle, bytes; </FONT></P>
0 r$ a' K( T/ q$ F0 u5 T/ z<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
2 h$ M7 I. F- `- n2 y( u" h& H<P><FONT color=#0000ff>/* 7 X' h: {+ ~: g! e- K5 l: ^7 ]
Looks for a file in the current directory named TEST.$$$ and attempts & _4 v% b+ a1 T6 T1 X4 _$ r
to read 10 bytes from it. To use this example you should create the
# g5 D6 c( H. b/ p' G5 L) Sfile TEST.$$$
4 S5 }6 j! K( x*/
. q+ F" d. h& m- o& |if ((handle =
7 Q0 w$ s4 @! e. dopen("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
+ k# I8 O6 X  D$ o3 r{
7 k9 Z5 G" N# J0 G  `4 Lprintf("Error Opening File\n"); 2 s7 u- _  H! E
exit(1); 7 f; p4 |! ?3 A5 g6 h$ D
} </FONT></P>0 x4 [3 y: d- _; w# H& f& a
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
) f0 a1 B1 K" l' Q1 s" Fprintf("Read Failed.\n");
  d/ W" p! p; l  [' w$ Oexit(1); : s$ @4 ]2 V- i0 X" l
} 5 o# _3 j2 H/ p: b+ K
else { 8 t% r! @: \' s% d# X' W* L9 B
printf("Read: %d bytes read.\n", bytes); 2 }3 ~. k- m- _4 t$ F" V
} . g2 W! x( T) X
return 0;
( s: K5 S% ~/ p# [6 g} </FONT>; O1 ]3 L( v' }
7 M3 o: L- n5 o+ Z% G+ e+ {: `
</P>6 A/ A8 v$ W! q) R. a6 B
<P><FONT color=#ff0000>函数名: realloc </FONT>3 b0 t) o) a, w9 H! x. u, f
功 能: 重新分配主存
* R& T0 k* O2 v# n# \* P# H用 法: void *realloc(void *ptr, unsigned newsize); 0 t( g0 |$ Y2 L! H* |  M
程序例: </P>  `5 M8 T  J2 F  W* E5 |
<P><FONT color=#0000ff>#include <STDIO.H>
. K) |% h# w% p0 v% \+ {#include <ALLOC.H>/ R3 |* ?/ ]; B  L# @
#include <STRING.H></FONT></P>+ J$ ^9 h/ A2 ]  j' |
<P><FONT color=#0000ff>int main(void)
, k6 j4 q1 J5 i% n: Y" a{
( `5 |  b3 e% A$ W6 r- vchar *str; </FONT></P>
1 ^+ Z9 p2 y& z& v6 J4 s<P><FONT color=#0000ff>/* allocate memory for string */ * X) l: A2 w. ?
str = malloc(10); </FONT></P>- E& N0 D2 n( r6 W; c2 Y
<P><FONT color=#0000ff>/* copy "Hello" into string */ ' `3 T. n/ @0 Q
strcpy(str, "Hello"); </FONT></P>
" u! ~; W! W# y/ F& |4 `<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str);
) Z; M& L2 j# H- y; e/ Pstr = realloc(str, 20);
: x/ Y* F' F4 j: M2 p; e, i6 dprintf("String is %s\n New address is %p\n", str, str); </FONT></P>. z4 O6 J: Z( X8 S- l  Y
<P><FONT color=#0000ff>/* free memory */
0 u6 e2 y6 T6 Y% tfree(str); </FONT></P>
9 x) n/ F& G% i1 U8 h& k/ H<P><FONT color=#0000ff>return 0;
& G) }+ m; y2 E' m8 ]} ( \8 Z5 q6 P: R) O3 M9 Q7 B
</FONT>% B0 S6 ?, K+ K* b
</P>4 K! i& z% y- w
<P><FONT color=#ff0000>函数名: rectangle </FONT>2 U6 U% O: b3 q7 D5 E
功 能: 画一个矩形
0 ?( F( _5 h" @2 c3 ^用 法: void far rectangle(int left, int top, int right, int bottom); : C, Y) m, r% X" u8 z9 U
程序例: </P>
& _- a  E$ h0 b' c5 J* @# w<P><FONT color=#0000ff>#include <GRAPHICS.H>1 H$ q2 r3 h. R
#include <STDLIB.H>' c# P6 s1 l5 e2 i0 V$ H3 v
#include <STDIO.H>
" o4 q2 f: L: v#include <CONIO.H></FONT></P>
0 u/ C# e( H. F& W<P><FONT color=#0000ff>int main(void) $ G) ]% y* G3 `8 w+ W- f
{ 3 U0 ^$ n7 ~2 O2 ^& H( N/ B
/* request auto detection */
# p7 E0 w1 O  R4 \( Z6 y3 M% Uint gdriver = DETECT, gmode, errorcode;
7 |$ I8 b! c) F4 e. A1 a$ Hint left, top, right, bottom; </FONT></P>
& t1 \9 ~! J1 c& Q<P><FONT color=#0000ff>/* initialize graphics and local variables */
7 ~7 b# b% S( F5 x# o$ finitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
( g# Y; F* S% \9 @6 l<P><FONT color=#0000ff>/* read result of initialization */
3 z/ n1 y* ?( F5 t. R6 ^errorcode = graphresult(); ) r/ b  v. A  m' w4 R. n
if (errorcode != grOk) /* an error occurred */
/ w. n: l; n0 V" G{ $ J& _, q) R9 o$ v& @1 i
printf("Graphics error: %s\n", grapherrormsg(errorcode));
6 ?2 u; G3 ^0 l0 T1 R; q9 _* nprintf("Press any key to halt:"); % D7 ~1 X/ C' U0 w' L: W
getch();
/ _4 h% `) n4 Yexit(1); /* terminate with an error code */
9 B% i: u0 T8 x' O" H" S} </FONT></P>
3 i/ |' T+ p5 p<P><FONT color=#0000ff>left = getmaxx() / 2 - 50;   x- J* V- U1 ]) u: D/ p
top = getmaxy() / 2 - 50;   s- E- Q5 E6 p
right = getmaxx() / 2 + 50;
" P/ _: I4 R; b. ^% d7 g- Ibottom = getmaxy() / 2 + 50; </FONT></P>
( J$ ]9 @) x& O! M<P><FONT color=#0000ff>/* draw a rectangle */
3 r' w5 T2 p; |9 ^8 u8 F9 C% m0 Brectangle(left,top,right,bottom); </FONT></P>, K1 \) l8 `9 n
<P><FONT color=#0000ff>/* clean up */ 1 W2 D0 z5 W" Y$ C
getch(); " `& c. c: }% g
closegraph();
6 t2 u2 \3 l$ ireturn 0;
$ O6 P! k" }  }}
8 q, q* ~3 q6 F; o/ @& L</FONT>
% _6 m4 }4 N. e  p4 G8 U</P>0 e0 l' v; Y& M3 N
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>1 |8 U) I( U  @; `% w! y% W
功 能: 登录已连接进来的图形驱动程序代码 5 E$ Z6 M3 ?3 c
用 法: int registerbgidriver(void(*driver)(void));
& G' d; n$ R) K* p程序例: </P>
8 P( o1 q( P4 v: K: x<P><FONT color=#0000ff>#include <GRAPHICS.H>7 v& q8 w/ K7 o# x6 I! R
#include <STDLIB.H>3 [  a" O3 K* Z, x: w1 S% z" n
#include <STDIO.H>4 s* q$ K5 `: t& g
#include <CONIO.H></FONT></P>1 x) `6 @6 A: f4 g
<P><FONT color=#0000ff>int main(void) $ v# a- w2 f" Q& Q9 ?2 C5 w
{ * @% ]- m/ g' q6 `7 K
/* request auto detection */
: Q9 E2 z- Q2 {' W  d+ ^int gdriver = DETECT, gmode, errorcode; </FONT></P># U% P( {. j% T7 N
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
3 M0 j* Z$ i+ P# J" Merrorcode = registerbgidriver(EGAVGA_driver); </FONT></P>
% a* a! ]  C- X! c: f8 `! }* J<P><FONT color=#0000ff>/* report any registration errors */
' p7 f8 X3 S6 Pif (errorcode &lt; 0)
" C) a+ `0 G: p{
, c$ k  V9 X8 v3 k+ H+ Gprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 S$ c4 u: Y) ~
printf("Press any key to halt:"); 4 z' c8 t1 R3 d' a0 A+ H6 |) z/ {
getch(); 7 Q+ l0 J7 [( s/ Z/ H
exit(1); /* terminate with an error code */
7 x1 j  _0 I& O7 _( W5 M} </FONT></P>
8 M: w9 c. J) d" a. V<P><FONT color=#0000ff>/* initialize graphics and local variables */ ) o$ e0 w7 k8 t8 w. D1 c
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
* e, t/ Z/ H5 c3 m- v<P><FONT color=#0000ff>/* read result of initialization */ 6 d, m3 v. v+ p$ J' j. v( s2 c! i6 k
errorcode = graphresult(); ; m( b# i* A. ~0 n: J. t  b2 e; S
if (errorcode != grOk) /* an error occurred */
6 a! e9 V; N7 f/ n  Y& \3 U{
/ }3 [  k, R" u% e& xprintf("Graphics error: %s\n", grapherrormsg(errorcode));
) x  L. T  g7 ]( tprintf("Press any key to halt:"); 8 _8 X1 q8 ~+ j' o  R& Y0 M- u
getch();
; Z% d& y/ i( }exit(1); /* terminate with an error code */ 4 e4 ~$ S$ x9 o1 V; \# v
} </FONT></P>
# I* m" U0 ~5 O! i0 ]: c<P><FONT color=#0000ff>/* draw a line */
8 z/ K  \; W& i5 b) [# Lline(0, 0, getmaxx(), getmaxy()); </FONT></P>7 v# e$ Q% s- f: t) E
<P><FONT color=#0000ff>/* clean up */ 3 J! d( g( Z  x  D5 E
getch(); - `2 h. h" e# \6 v: {
closegraph(); 0 ^! k  t; e% J) Z
return 0;
( Z! B; g% l- H1 Y# v}
  `6 ~- {2 o" H</FONT>
& d+ a& h5 M9 i. {' C* w</P>; W' X- f- Q: N( O, K
<P><FONT color=#ff0000>函数名: remove </FONT>
# Q; p0 E! o) g: ]功 能: 删除一个文件
/ n& O/ z  v  m1 L" z6 S& N, S! R6 ?& V用 法: int remove(char *filename);
0 c& R( ~- E1 m- t/ U3 \程序例: </P>
  H; H4 i; S8 c2 U<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>9 X, I1 E( x7 y! f9 ~( d& P4 X: `
<P><FONT color=#0000ff>int main(void)   Q& N- ]2 P+ O- V
{
& [& O: l# U8 [& Kchar file[80]; </FONT></P>& G; g0 x) N# d6 n+ g1 r
<P><FONT color=#0000ff>/* prompt for file name to delete */
* l) x/ A4 E; ?8 q' w- wprintf("File to delete: ");
/ h+ O; D* P0 W# ~. B: I% u* cgets(file); </FONT></P>  J8 l. h, [2 |% k2 P
<P><FONT color=#0000ff>/* delete the file */
; J) R% w+ R4 M8 `if (remove(file) == 0)   L- D) ^* R6 @' v) a2 e! x
printf("Removed %s.\n",file);
% x/ G: O) k5 z' W8 p5 ]8 I1 Felse % P4 E. U) T) e
perror("remove"); </FONT></P>
+ {: \* [  U8 i! X% {0 X! J<P><FONT color=#0000ff>return 0; * P' @7 Q5 q: K; B! k3 r7 N* }6 A* M4 }$ X
}
8 e* h# q7 }! y) h4 m, J% \9 S# G( @! g5 s) p' C. b
</FONT></P>& U  \3 J1 f2 q, X  ?  I
<P><FONT color=#ff0000>函数名: rename </FONT>
3 S3 Y; S- u+ B9 D/ `功 能: 重命名文件
) _6 p) F$ z: H  _  J+ }+ d  A用 法: int rename(char *oldname, char *newname);
  f& F& j- N. t7 W2 L程序例: </P>, B* {/ Z! T5 F6 u1 n2 L, `$ ~9 d
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>) Z" J3 D) g/ `# R4 ?) B
<P><FONT color=#0000ff>int main(void)
" ~4 k0 j8 f4 F0 n! a{
' A! v+ c1 g% H& W/ Qchar oldname[80], newname[80]; </FONT></P>
! T5 T: v2 P  z5 k<P><FONT color=#0000ff>/* prompt for file to rename and new name */
5 O/ Z& J" H. W) |: cprintf("File to rename: ");
; |) s& F( M) o4 w+ Fgets(oldname);
5 I3 ?4 q* ^$ q1 k/ y: aprintf("New name: "); . q; R; J5 Z, b+ {5 Y
gets(newname); </FONT></P>
$ @/ L+ n  t/ j0 }1 ~* ^<P><FONT color=#0000ff>/* Rename the file */ / t' Q5 O. v, q" c; d0 E
if (rename(oldname, newname) == 0)
; O& J/ Y" D# |# Pprintf("Renamed %s to %s.\n", oldname, newname); ! }; c7 d4 n- h* ~. B- p" f
else 9 i% r: s) H$ J1 _3 z. Q
perror("rename"); </FONT></P>
; I1 P5 Q5 l' m% h5 Z6 M( q) e<P><FONT color=#0000ff>return 0; - y+ O2 M; G: F# k) n
} 0 v' Y7 M# d- n9 w+ W$ ^, J. r
6 Q+ a/ _+ m2 f& f% h$ c" h' `
</FONT></P>
1 W+ j6 `) }) t: ?0 P$ l" S<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>6 \( X* D5 E/ I0 y
功 能: 将屏幕模式恢复为先前的imitgraph设置
5 R3 K& h* W* h; `' L* m  M( T用 法: void far restorecrtmode(void); # {6 X5 W  v" `2 t, e8 ^5 j: [, T
程序例: </P>
) M3 K; H' ^; {) W! b3 I- z8 X$ f<P><FONT color=#0000ff>#include <GRAPHICS.H>
& r8 n  }! x, W& q" V- F: Q# C' N" P#include <STDLIB.H>
0 ?/ L8 H  v! @2 w3 L#include <STDIO.H>5 Y) w) [7 t% b* z1 ~5 p9 W
#include <CONIO.H></FONT></P>& m. m( q, \& q' r
<P><FONT color=#0000ff>int main(void)
( L" Z  G+ j  Y1 V1 l7 F* `% D' X{ 7 k+ ]# @4 N( R) O+ e& k( i; a) c/ V
/* request auto detection */
* S: D1 }: T- K! I% _: e# [int gdriver = DETECT, gmode, errorcode;
# F3 \" E% V9 I' wint x, y; </FONT></P>, q4 X! {( n7 Y& p% x
<P><FONT color=#0000ff>/* initialize graphics and local variables */
0 Q* A# x9 V5 i3 }# Winitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>9 S2 _. W, [: A' ?) q2 [% l
<P><FONT color=#0000ff>/* read result of initialization */
/ o3 R9 a6 f) w* Aerrorcode = graphresult();
! h$ z5 w7 p2 f: @if (errorcode != grOk) /* an error occurred */
2 `$ F. F2 b2 }% b$ a2 J" ~" `{ " T4 M4 w: h, W6 ^
printf("Graphics error: %s\n", grapherrormsg(errorcode)); & N8 i" h/ }4 A7 T/ w* k" }4 V& D
printf("Press any key to halt:"); / a& V) F2 v1 ~, ]3 B
getch();
. d7 G; Q1 A$ A* z' i! Jexit(1); /* terminate with an error code */
4 F2 T! m: ]1 F' c2 D} </FONT></P>
4 x+ k. B. P$ P0 n9 W; I<P><FONT color=#0000ff>x = getmaxx() / 2; : i' Z9 E' c) I! n5 N- E/ [$ p9 F2 Z
y = getmaxy() / 2; </FONT></P>0 O$ d, c- l) a, X
<P><FONT color=#0000ff>/* output a message */ & N' c, g8 I% B
settextjustify(CENTER_TEXT, CENTER_TEXT);
6 O& e3 `. M/ r# z4 f" \outtextxy(x, y, "Press any key to exit graphics:"); ( U9 ?* B$ l# v. y+ N6 \; R( J3 u  X
getch(); </FONT></P>2 U/ ?& K& }; u8 k7 y
<P><FONT color=#0000ff>/* restore system to text mode */
3 g7 Y5 [3 r1 Irestorecrtmode(); ' r( b( f5 i/ }1 B1 f7 N
printf("We're now in text mode.\n"); % G( D, V. p" m- c6 p1 f
printf("Press any key to return to graphics mode:");
( w) y8 x4 z* F- t, n4 N1 tgetch(); </FONT></P>
; P( v! {( a! v* r<P><FONT color=#0000ff>/* return to graphics mode */ % A6 t. U: v& ~% S
setgraphmode(getgraphmode()); </FONT></P>0 y1 D2 b( C" _' X$ z
<P><FONT color=#0000ff>/* output a message */ * R# A8 _2 q" b' }# i
settextjustify(CENTER_TEXT, CENTER_TEXT); ! p# v4 O8 {, N
outtextxy(x, y, "We're back in graphics mode."); $ O% w+ z7 \) K) i
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>3 n1 e) U: t7 o
<P><FONT color=#0000ff>/* clean up */
( |4 W4 c% q5 Ugetch();
( I4 E; E  ]' y; Bclosegraph(); 9 v" I2 v5 Q& k$ p7 `
return 0;
6 n$ I1 g* k( S  e; K}
( @/ F" b( [7 X. f  f</FONT>5 U2 Y% w. K& q1 [) t
</P>
/ C- g$ U9 B) U<P><FONT color=#ff0000>函数名: rewind </FONT>6 g( }: G4 k% {2 @! K( C) l( O+ C
功 能: 将文件指针重新指向一个流的开头
8 u% \. m$ l3 D$ @3 R# R9 ?! _用 法: int rewind(FILE *stream);
* H( f0 O$ A) [1 a程序例: </P>9 ]% M4 h! ], R& l3 Y* Q: G3 k+ M
<P><FONT color=#0000ff>#include <STDIO.H>7 \3 ~- `) K9 [* [1 n" }
#include <DIR.H></FONT></P>
0 w* W$ _2 d( {# }<P><FONT color=#0000ff>int main(void) " I  Z8 Z" \- r
{ 0 M. O( _( N: _) l  R# ]' c
FILE *fp;
- n8 i3 t3 M2 G6 [, ~3 Gchar *fname = "TXXXXXX", *newname, first; </FONT></P>$ y9 Z: F, I& b- ?
<P><FONT color=#0000ff>newname = mktemp(fname);
8 m" K1 S$ P' E1 ]; U  Nfp = fopen(newname,"w+");   g! z: G8 [2 g# Q- c
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
# n1 }% z" M2 @4 J* [9 W9 hrewind(fp);
, C: K/ N4 z: {fscanf(fp,"%c",&amp;first);
: B: i1 N& z. X8 I, Z9 V$ hprintf("The first character is: %c\n",first); 4 c7 i3 F1 t3 c' x0 @. U" ?
fclose(fp);
; [" Q  Q" Y! {3 k- S$ {remove(newname); </FONT></P>9 Q9 o0 |) w" ?5 h2 v; a! @, e
<P><FONT color=#0000ff>return 0;
* R5 O8 k; Z5 A& N3 l% Z5 g} 6 v" l1 u( {, L7 s" M
</FONT>
8 p6 \' \9 q- s& I</P>
1 K0 R7 C6 P4 S! W5 |- y<P><FONT color=#ff0000>函数名: rmdir </FONT>
$ o/ D2 z  l  ^+ Y功 能: 删除DOS文件目录
4 r9 c# E0 z) m; Z; h. ^) \' {% S用 法: int rmdir(char *stream); * U" @8 z2 J  T3 w; g# a
程序例: </P>9 u/ i6 i2 @* t
<P><FONT color=#0000ff>#include <STDIO.H>' B+ U. ^; \) k8 `+ u
#include <CONIO.H>% c% a9 R" F/ ~8 C+ j5 o
#include <PROCESS.H>' l0 l* ]2 Q( d3 z, K$ X
#include <DIR.H></FONT></P>
- E! K0 j5 J3 G" ~3 K" W3 J( O<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>: H4 q3 m; a6 t+ m% |6 d2 P
<P><FONT color=#0000ff>int main(void)
. _  Z0 L: b* n{
: u$ F- T6 B+ nint stat; </FONT></P>6 V- ~/ i1 a% `
<P><FONT color=#0000ff>stat = mkdir(DIRNAME); 3 S! o4 D4 u3 F& I: W* i. Z- Q) i+ _. H
if (!stat)
9 D3 V  W) ?" v8 y! Yprintf("Directory created\n");
8 |2 V7 P6 V+ N8 i+ E# H0 g0 q4 @else * _* q( ]4 E. s8 Q- O
{ ; P% n" @# ~& I2 j* J
printf("Unable to create directory\n");
, R4 H9 Q  @" {exit(1);
' R: P2 O; X3 G( t$ N& K- W$ k} </FONT></P>1 L9 G$ g5 m, r0 v- ?
<P><FONT color=#0000ff>getch(); 2 a9 ^" e9 p0 t& ~( M( p* j: c$ O. a
system("dir/p");
0 n* v+ p1 ]. Egetch(); </FONT></P>3 c: ?1 o0 D$ W% C3 o5 |
<P><FONT color=#0000ff>stat = rmdir(DIRNAME);
) {. ?" D" J; _+ Cif (!stat)
6 N7 Q3 e. U  Qprintf("\nDirectory deleted\n"); 3 k7 }" k; x- X: e: m; r* _9 n
else
% @: @0 h( h% b) i, K- \{ 6 B8 ^' m8 I$ w; j" P
perror("\nUnable to delete directory\n"); 4 h) f7 y9 Y7 ]
exit(1);
2 h! }: D6 `2 `! E, g9 G} </FONT></P>
1 I  ^2 X4 l; V# g<P><FONT color=#0000ff>return 0; # V  X( T$ A# q5 a- C5 P
} </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-13 05:11 , Processed in 0.380215 second(s), 51 queries .

回顶部