数学建模社区-数学中国

标题: 函数大全(q,r开头) [打印本页]

作者: 韩冰    时间: 2004-10-4 02:55
标题: 函数大全(q,r开头)
<><FONT color=#ff0000>函数名: qsort </FONT># I0 t  F' n2 K0 Y- ^& @% s: i
功 能: 使用快速排序例程进行排序
; b8 d8 P# ]3 x6 b用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
3 N# F8 a4 @, `6 `程序例: </P>) |& \. M. Q/ q# k( R+ U
<><FONT color=#0000ff>#include <STDIO.H>
9 x" r( N5 |3 ^" C: l! l#include <STDLIB.H>
& X* T) K& m8 I% M% z: v#include <STRING.H></FONT></P>( s" W( Z, m( Y) d/ n
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
( t1 A+ j& Y) G6 g1 t- {<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };
* V( P% r2 a& `0 u/ O7 O8 r</FONT></P>
7 k4 h1 J8 P! d" ~4 M<><FONT color=#0000ff>int main(void) 9 s( f. Z, b, P" x1 _' v. R
{ 7 z, k8 G+ U' d- q- h$ L6 n
int x; </FONT></P>
; W$ y5 A0 F" U6 N1 D# D8 T<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 0 C: k0 y$ @% ?3 x" o7 Z
for (x = 0; x &lt; 5; x++)
0 e) D. @% w6 I- N3 F! dprintf("%s\n", list[x]);
6 p, Y/ I- T, {+ e* G. Z/ A7 R9 freturn 0; # W. c* X: o  A1 Y1 v. D7 D
} </FONT></P>
9 }5 l0 a' ~0 I! v<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
) h8 q7 I7 s2 v2 V{ ( p8 R* P! @% y8 q) M
return( strcmp(a,b) );
, q/ u% Y. D( d# R# i1 F. \} </FONT>
* ~' N7 o2 [, Z/ v1 ~</P>" H' H6 K8 X! n" h, b

- s  E" S* O; h! ~: n  i
  M) E) @( U2 N( `4 T<p>! }4 R* b3 u4 L) e
<><FONT color=#ff0000>函数名: qsort </FONT>, N# s+ E+ x2 d5 |
功 能: 使用快速排序例程进行排序
1 z, J% U1 W: B: z. X5 E4 B用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); ! B+ m( X+ l# ^/ i1 I$ D$ L
程序例: </P>7 c7 k) v+ ~+ E  ^& H
<><FONT color=#0000ff>#include <STDIO.H>
% g# I' |$ y. ?0 u9 O* u, r8 `8 L#include <STDLIB.H>1 P, g- S( \5 Z+ y* q
#include <STRING.H></FONT></P>
& |. }9 n; C4 Z( b! w2 V. ^<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
# f8 a1 I) V, N5 |) I<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; - A  c% U1 w* @& D  g7 i
</FONT></P>* }2 z. ]- T/ i' L0 ~* i/ `$ j
<><FONT color=#0000ff>int main(void) ' ?% m* b+ @7 b/ W: x/ N* D  U
{ 4 @" M8 u- N; u- u
int x; </FONT></P>' C5 |# q8 H/ v- ?7 o8 |
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
5 V; M* o  f2 k, W  zfor (x = 0; x &lt; 5; x++) % H' H$ q2 ?0 J3 {
printf("%s\n", list[x]); : p5 \+ H! Q! w6 c+ j. R" y  P) L$ w
return 0;
8 `4 Q- f/ C: }  ^# V  q3 U} </FONT></P>
7 ~0 e/ D7 {9 E$ y- F<><FONT color=#0000ff>int sort_function( const void *a, const void *b) / T7 i2 L) j7 X; o. P/ K! D
{
. ^; g! o$ C6 O7 Q4 [: Kreturn( strcmp(a,b) );
! R# R0 f' Z- U7 L# b}</FONT></P>5 u0 f% m9 ?4 U! j
<><FONT color=#0000ff>
+ T8 ~; N* w) e</FONT>1 g# T$ G/ X: U% L
</P>3 q* g: n3 |; D- S/ g# }3 U
<><FONT color=#ff0000>函数名: raise </FONT>/ \2 W1 r0 F0 y: L3 a
功 能: 向正在执行的程序发送一个信号
# t5 I+ c% ~; Y" E. E$ e用 法: int raise(int sig); ; K6 i. M0 [; {
程序例: </P>  W- Y) n6 V1 H. Y! Q) Q; B
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
/ R6 V% Q' i! T& }" Q- P<><FONT color=#0000ff>int main(void)
2 I, q& F9 h$ j0 d( s5 u{ : W8 |1 O- V! Y% v3 O: e3 Y
int a, b; </FONT></P>2 O5 N3 `1 {- `
<><FONT color=#0000ff>a = 10; - B2 ?& _6 X' H$ ]. P6 Q5 A+ e5 q
b = 0; ) y9 c5 S1 K6 d. V
if (b == 0)
4 m: K7 i6 M, e9 V( B4 V3 Z/ W" U2 _/* preempt divide by zero error */
# C3 Y: Y: Q) u, H6 p, w& Wraise(SIGFPE); & }- b( }9 z, C
a = a / b; 5 v3 R, O8 ^* ~" h2 ?4 m
return 0;
9 [8 \: G9 y# B" z- g8 P} 5 a0 M$ h$ A5 P+ f
</FONT>
3 b4 ?. r+ s% |5 g% x' H</P>" p/ T# v. y0 r0 w: z5 w
<><FONT color=#ff0000>函数名: rand </FONT>- N2 n7 K' n9 ?3 t7 X
功 能: 随机数发生器 $ ^" Q( t% R/ n* d0 I$ i1 H
用 法: void rand(void);
8 z" e2 Z$ j# F/ F5 |# u程序例: </P>& g* B7 @  R% |/ u
<><FONT color=#0000ff>#include <STDLIB.H>" ^' e+ w6 p7 i
#include <STDIO.H></FONT></P>+ e" T# F* M' y
<><FONT color=#0000ff>int main(void)
3 y8 A) P( z5 R  R( P3 v! M% [' }{
% G( Q' t' K5 F" O/ l4 P7 ^, Sint i; </FONT></P>
  x$ |2 d% ^5 r<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); % \$ K* B( t# @; m
for(i=0; i&lt;10; i++) * e! ~6 b" X! g# n' R
printf("%d\n", rand() % 100); 1 Q2 o( q: M7 J  w" \  g* `$ ?
return 0;
( t4 E+ m6 O' m+ z3 [- m; Q}
2 p) a  ]1 B7 d8 S- g0 n
$ R$ `. ~$ D+ F</FONT></P>  N6 Z; r" k% g: X/ D
<><FONT color=#ff0000>函数名: randbrd </FONT>. j. f1 B6 B, U! x" s# K0 u
功 能: 随机块读 * t9 m1 T% w6 `0 H; j
用 法: int randbrd(struct fcb *fcbptr, int reccnt); - `* J5 f- p+ S) s
程序例: </P>' W2 ]% N( B7 K) y! T, g3 d' n- b; ?
<><FONT color=#0000ff>#include <ROCESS.H># u+ s: A2 m2 p, l, Z
#include <STRING.H>, ~; T( M* }! d0 A2 Y) E
#include <STDIO.H>- j2 ]+ r# y0 _4 _% z  v% ?2 {
#include <DOS.H></FONT></P>0 G/ k5 g3 s+ ]- v6 K4 M! P
<><FONT color=#0000ff>int main(void) $ c' @& ?, c  w6 k  h, B- J& P
{
# g  g9 M/ C$ l' }9 Gchar far *save_dta; 3 U3 a; v9 q' N1 h
char line[80], buffer[256]; 1 Z2 E7 \% n/ o/ V: S
struct fcb blk; 1 M( K, e9 ^9 [
int i, result; </FONT></P>% ~, b7 |* m& z$ B! g. z; }
<><FONT color=#0000ff>/* get user input file name for dta */
! L! K/ G, m& s- ?- w  |printf("Enter drive and file name (no path - i.e. a:file.dat)\n"); ( b& C+ R6 D2 ?
gets(line); </FONT></P>
8 W/ c' U5 l: s& K6 ~1 d<><FONT color=#0000ff>/* put file name in fcb */ 7 }# |* h9 j" t9 w% ?5 w9 W5 {8 t$ r
if (!parsfnm(line, &amp;blk, 1))
) L+ {) B; W& E& ~+ t: N- ~. I{
) c8 g' r3 J; c$ Z8 S; Kprintf("Error in call to parsfnm\n"); . s) U3 ?* g! C/ M
exit(1);
6 B# j/ o0 ]0 h, P6 N( L} " b1 [- i( W5 w1 K
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
, n$ w' e  o0 o$ Q! Z<><FONT color=#0000ff>/* open file with DOS FCB open file */ ) i$ l0 Q2 Y. r+ i
bdosptr(0x0F, &amp;blk, 0); </FONT></P>1 l( a. E' @  R+ m& D/ g, j8 ~
<P><FONT color=#0000ff>/* save old dta, and set new one */ / M" n7 E2 n' _8 u1 S* Q  |& W* X
save_dta = getdta();
% ?# Y5 a, t5 T' p" d2 Z1 e3 H+ `setdta(buffer); </FONT></P>
- V8 |2 ?4 B3 P8 r<P><FONT color=#0000ff>/* set up info for the new dta */
0 m! h2 N5 `" G+ v! L7 dblk.fcb_recsize = 128; 3 [6 e. V$ e' h  W0 _* X) K% N" c
blk.fcb_random = 0L; : X* l( W9 `- S$ p, M8 k& n& e8 ~! F
result = randbrd(&amp;blk, 1); </FONT></P>
1 P( T8 l- L. |# ?: |) D/ V% l<P><FONT color=#0000ff>/* check results from randbrd */
: r+ W7 {2 A* _8 j& Iif (!result) # T! x6 a+ j) a. Z  N% q- M: c
printf("Read OK\n\n");
- D( o8 _9 d$ n8 r- j7 j4 |8 selse
( ?+ ^, [- m4 u$ `# M{
. w7 H! A4 z9 \: h! z- J  q! h2 k5 X2 aperror("Error during read");
9 K2 b' C/ H9 t. cexit(1); + ^% y6 y" D& s5 R/ H
} </FONT></P># k. x4 e( I+ W5 F* D
<P><FONT color=#0000ff>/* read in data from the new dta */ . z9 H+ |7 k6 {2 C* L5 m
printf("The first 128 characters are:\n");
2 f  Q0 S; d9 ~for (i=0; i&lt;128; i++)
/ D# }- a/ u) @# n" Y; X5 U; Tputchar(buffer); </FONT></P>
  a. o1 `( L  Y<P><FONT color=#0000ff>/* restore previous dta */ $ u& y, y2 n) y
setdta(save_dta); </FONT></P>
7 [  J7 A8 E0 E2 N1 o6 Z<P><FONT color=#0000ff>return 0;
! A* T# f/ }, G1 J} </FONT>( z) K& {4 I" m  N6 p- t1 Q: ?
</P>- d2 c9 w" }; n- Q/ n4 q. Y
<P><FONT color=#ff0000>函数名: randbwr </FONT>
9 {$ f% I4 Q1 v1 r) k功 能: 随机块写 # b% x* W2 u  ^- r" U4 J! J
用 法: int randbwr(struct fcp *fcbptr, int reccnt);
: h$ z$ ^* i: H程序例: </P>0 k# N7 n! v8 p( f* k
<P><FONT color=#0000ff>#include <PROCESS.H>
* N1 Q( x* z+ k- c1 A7 h# K#include <STRING.H>
8 ]8 K; t5 L1 w% b% T8 U#include <STDIO.H>
2 f1 R; \* h4 l8 E% `#include <DOS.H></FONT></P>
; P: k' |; M/ y* [' [! j# m<P><FONT color=#0000ff>int main(void)   n' w2 g. T+ R1 n
{
3 T) w" H$ o: o( c' l: bchar far *save_dta;
. F- D$ Z3 W) _1 x+ `char line[80]; * S- E0 f' ]0 L0 f, A3 I! F; d+ n/ C
char buffer[256] = "RANDBWR test!"; 4 [7 u1 ^: U) ^" s2 u5 I5 x$ A
struct fcb blk;
" q' t9 @' i9 g; j. g# d  wint result; </FONT></P>( [' T5 z+ ]7 f, j- A; P  {
<P><FONT color=#0000ff>/* get new file name from user */ ! `1 x; n* Z" f, c' D* Q. t
printf("Enter a file name to create (no path - ie. a:file.dat\n");
* t# o* W0 _$ N- Dgets(line); </FONT></P>
4 c7 z6 B- \7 i7 Y* f<P><FONT color=#0000ff>/* parse the new file name to the dta */
' @- p* v2 I( M9 m+ n. q2 J  pparsfnm(line,&amp;blk,1); " C3 \+ t: T2 `/ v* k* A
printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
! r, y" _+ t$ i<P><FONT color=#0000ff>/* request DOS services to create file */ 7 Y) y$ l3 f  Y. I' L7 j$ L" }
if (bdosptr(0x16, &amp;blk, 0) == -1) ! @1 g! F$ W6 o# u
{ 1 q2 B$ |: b( I% q
perror("Error creating file"); % B6 C9 ^6 p7 n! w" K: }% U1 J& H6 F( J
exit(1);
" x+ C7 r  J# o2 y4 c9 x} </FONT></P>
  Q* f* Q$ d4 Q* j, G<P><FONT color=#0000ff>/* save old dta and set new dta */ ! k  Z$ f2 z/ t! E; T, C: ^( D1 @! s
save_dta = getdta(); ; y) a: |- E$ X" y7 s
setdta(buffer); </FONT></P>) M# X6 l6 X, R0 P0 `3 N
<P><FONT color=#0000ff>/* write new records */ % J. H$ l8 s9 R1 r
blk.fcb_recsize = 256;
4 s2 X# d  n- _6 eblk.fcb_random = 0L; 1 d( W# m( V- p1 j; V
result = randbwr(&amp;blk, 1); </FONT></P>* \5 \8 A7 O* f% L" o1 Q- v
<P><FONT color=#0000ff>if (!result)
" \" r. q5 A" S. o3 Xprintf("Write OK\n"); ; k9 B+ Z/ v, Q
else
, ]5 J( ^6 _' y4 V{
, ~4 L- N8 x& Y; R  Sperror("Disk error");
, i: g) |* {% ]9 P8 Q8 Hexit(1);
9 w/ a8 W& W% d: N( v- r" Z} </FONT></P>  {/ \- n0 w  ^' _: l
<P><FONT color=#0000ff>/* request DOS services to close the file */ : O" K7 r+ T! H2 Q5 U/ d* t
if (bdosptr(0x10, &amp;blk, 0) == -1) 4 n, h4 p. w2 m# I
{ ' q' g1 }8 o% b7 s+ Z( }8 U5 Q
perror("Error closing file"); # I  P0 i. l4 `
exit(1); $ ?  z& ?5 V+ A- |5 N
} </FONT></P>  i, ?- t6 [. _
<P><FONT color=#0000ff>/* reset the old dta */
. k. p, r  D2 ~7 k& M& ?' bsetdta(save_dta); </FONT></P>
$ ?' Y5 y  q& A+ T<P><FONT color=#0000ff>return 0;
, O' i6 M& S' }8 ?! C7 ?} * ]( o, A  u: J( D, g1 [( q* J
</FONT>! |! }& y& ^. H  Z4 q8 ~! q- X
</P>6 `# r! N2 G2 z1 J* e$ c
<P><FONT color=#ff0000>函数名: random </FONT>- W6 j- a$ r+ Y/ r' r( ~) ^
功 能: 随机数发生器
" U2 o5 D8 F# ^1 R$ v, h- O用 法: int random(int num); - P% o% L% |0 K
程序例: </P>9 K0 z2 {' q8 f! g& `
<P><FONT color=#0000ff>#include <STDLIB.H>
& F5 {0 i7 r2 `+ c#include <STDIO.H>
' m! t! C9 r6 @3 A7 t: U1 H#include <TIME.H></FONT></P>
3 N: ]! ^1 F4 H( A<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
. s& L" |. Q. ]4 h: Mint main(void) # b/ g* X' q! {" t# K, y
{
( K8 U9 i: p( I& q9 }6 o9 h* drandomize();
: s" G, ], d& n! C3 Hprintf("Random number in the 0-99 range: %d\n", random (100));
% f" F; P+ \# z; d* jreturn 0; 2 }" N2 V3 F% I( J
}
* W# U( j& v9 h0 A; K+ A5 B</FONT>9 p) N4 \* Y1 d: I5 U% U
</P>
7 r+ [2 u4 \% @% y- a# k4 u<P><FONT color=#ff0000>函数名: randomize </FONT>
: n2 N2 s( Q% u  a& ~. N功 能: 初始化随机数发生器 8 D9 {6 L: t& Y. y
用 法: void randomize(void); 1 L+ C7 K2 t  ~
程序例: </P>, v" Y0 m; x( {( v5 B& g% Q7 j
<P><FONT color=#0000ff>#include <STDLIB.H>
. N9 z2 i4 z  D6 L* ?#include <STDIO.H>
3 t0 p4 J8 r4 t* v4 ^#include <TIME.H></FONT></P>9 u* }: Z" Q8 F
<P><FONT color=#0000ff>int main(void) 1 D3 ?1 e1 M6 A- ~1 F1 k" F
{
) t; G8 @# \& \int i; </FONT></P>
- k, ?6 s0 {; b8 V+ Y( _<P><FONT color=#0000ff>randomize(); " V1 m- e1 w; p, w( g
printf("Ten random numbers from 0 to 99\n\n"); 4 m) n: d3 P6 B$ X, ?
for(i=0; i&lt;10; i++)
# }6 G, H# S1 V; S+ Y/ g5 r6 Nprintf("%d\n", rand() % 100);
3 r; S% z! x: P0 S$ k# zreturn 0;
- x0 n) \9 m6 c}
  q% `) `0 {3 v( b</FONT>. m9 q  C1 q6 V3 j9 k! R
</P>
2 {! _3 P/ [% S$ x, L# S<P><FONT color=#ff0000>函数名: read </FONT>
- v7 Y8 f/ s1 R4 `- M4 Q; [5 M功 能: 从文件中读
' w5 d. C2 y* V3 _& Z# d用 法: int read(int handle, void *buf, int nbyte); 6 Q) @: @  y5 l; ?: q1 O
程序例: </P>  a4 g  Z. D/ K3 K2 w5 K  B3 y7 L) u
<P><FONT color=#0000ff>#include <STDIO.H>
% _2 \& L+ M/ z6 x( j4 p* S#include <IO.H>
& T4 o5 j( _' Q#include <ALLOC.H>
2 Y, V. T: O5 L7 x. k#include <FCNTL.H>$ f( r+ M$ e8 E  ^  N5 {. c
#include <PROCESS.H>
9 D8 ]2 Z) _+ Z#include <SYS\STAT.H></FONT></P>% e& N& C: Y* |$ {7 k8 g
<P><FONT color=#0000ff>int main(void)
' s1 `& O# D( @{ 6 w; Z) D) ]" _6 }3 x1 q% B
void *buf; & `# E" ^: f# J
int handle, bytes; </FONT></P>
& S% A" ?; l) @% @" i/ h' k* @<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>  V* @% _# d) ^4 p$ K
<P><FONT color=#0000ff>/*
0 H- g; ]. i' }" f2 W- y+ aLooks for a file in the current directory named TEST.$$$ and attempts $ q, q7 K# A  C. @+ B$ a% s, E% [
to read 10 bytes from it. To use this example you should create the ! B. F7 c) o' @6 R; l1 E6 h4 M, D1 C6 j  K
file TEST.$$$
+ J  l4 c, r* J) z*/
( V2 f7 A- U8 g% Xif ((handle =
. @/ L$ P9 ~  k! Popen("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
2 z! N; Q/ ^, k- ?7 S" F: y{
  h& k# Y0 l; X* r1 W- E* gprintf("Error Opening File\n");
: r) _8 c6 C2 texit(1); * G; ?8 s+ Q: _' `$ B. O
} </FONT></P>
- s! Z2 g- e. E8 H' N<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
6 x$ j6 K  O, lprintf("Read Failed.\n"); 3 w; B; o1 W9 @" y
exit(1);
' a7 C2 G' e( F& e}
. _7 |& n# L6 o: x5 H' M# Jelse { 3 y4 Q6 n0 B" y- l' B& X) s
printf("Read: %d bytes read.\n", bytes); & q4 d" j( n+ T- E
}
/ Y, z% F' @; preturn 0;
& m) z, h8 s1 y% s4 R) W} </FONT>, C6 R( D% N  H' @! Y& a- [3 V: K
: z8 a) T6 o  W! z& v2 a% R
</P>8 u, Y, y" Z  V; h1 q
<P><FONT color=#ff0000>函数名: realloc </FONT>
7 f! `' z3 l, {9 K功 能: 重新分配主存
( ?+ v3 s6 {0 S用 法: void *realloc(void *ptr, unsigned newsize);
  r+ q- x3 ?9 S程序例: </P>
1 O2 B4 X+ F2 q! a$ ^3 E* R<P><FONT color=#0000ff>#include <STDIO.H>
4 R0 U4 N, q# L  v4 [#include <ALLOC.H>
8 l# f  k% h- D#include <STRING.H></FONT></P>4 ?$ C' E) M8 s9 I" Q
<P><FONT color=#0000ff>int main(void)
0 t( `, y9 L! s0 h{ ( ?$ Q, S; i/ c& P: t4 j
char *str; </FONT></P>
* q/ N/ V# A# s( h2 L<P><FONT color=#0000ff>/* allocate memory for string */ ) I6 \& l# ~- m  Q0 X5 l
str = malloc(10); </FONT></P>" p& j5 R1 G/ {, G' |- H: K
<P><FONT color=#0000ff>/* copy "Hello" into string */
, I' N& g& P/ l& c5 mstrcpy(str, "Hello"); </FONT></P>
% d0 M$ A2 w. |# R  |! B1 {<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); 7 y- Y( I( b' v. v! N
str = realloc(str, 20); 1 t/ R/ f; v2 f8 G
printf("String is %s\n New address is %p\n", str, str); </FONT></P>+ g! W; j+ E! A4 y
<P><FONT color=#0000ff>/* free memory */ " o% ]0 m5 t- K" J( u& N
free(str); </FONT></P>7 B0 ^5 ~2 A2 X1 F: W
<P><FONT color=#0000ff>return 0;
* J( c& r) G; f  O} & T/ K/ _' [& b& l- W/ b0 Y  e) ^
</FONT>0 o2 u; @' g6 O# M
</P>6 A5 ~- P+ B0 h) m1 h# Q
<P><FONT color=#ff0000>函数名: rectangle </FONT>
. t4 I# o# I7 v- q/ R8 m功 能: 画一个矩形
5 Y, D, c0 W1 o: B0 [/ x! J用 法: void far rectangle(int left, int top, int right, int bottom);
/ o& S1 Q! U$ q. w" G程序例: </P>4 c3 C6 ?8 f5 k. y# j8 p- c
<P><FONT color=#0000ff>#include <GRAPHICS.H>% H" j0 M& K6 |  A  X
#include <STDLIB.H>
6 x* i+ |; m0 t6 a#include <STDIO.H>
  w4 t) d5 O( ]. N#include <CONIO.H></FONT></P># w) o1 P/ C( T1 P/ r2 g* C
<P><FONT color=#0000ff>int main(void)
( M1 x- P2 Z; m( k{ % o! D# B5 ?$ i* `
/* request auto detection */
, h9 U8 t/ N/ \7 j/ Tint gdriver = DETECT, gmode, errorcode;
  }1 R) S6 R0 Wint left, top, right, bottom; </FONT></P>, e3 j4 f! {" S) S5 f( B+ ]: w: s
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 i/ O0 s; J9 u! @9 W3 ^
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
+ S8 ~$ Q" r, A4 L; h$ ~+ [/ Y<P><FONT color=#0000ff>/* read result of initialization */ 5 h6 p) X- r; H$ \. D# U* |2 o4 S
errorcode = graphresult(); 1 E. @) y! r9 g2 v7 q. b& x
if (errorcode != grOk) /* an error occurred */
- `; T7 R/ y# m( R4 A5 o{ $ [) w8 _' I8 ^$ B- B& W" g. M
printf("Graphics error: %s\n", grapherrormsg(errorcode));
2 _) [% U: T4 q* {3 N9 Z5 eprintf("Press any key to halt:");
: |4 M9 a- G& g* Agetch();
1 U) I6 i4 v( e% r. l6 uexit(1); /* terminate with an error code */
9 y8 n1 B# K* g( a8 @} </FONT></P>
' @# b! T5 U* T6 o4 a" y<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; , ^8 |7 f# o$ n, s8 o$ ]' v7 y
top = getmaxy() / 2 - 50; 6 u) w5 c& B) k# v! A
right = getmaxx() / 2 + 50;
: h- b$ v' N9 S0 n# cbottom = getmaxy() / 2 + 50; </FONT></P>7 ]8 O7 P  x0 y/ |4 Y
<P><FONT color=#0000ff>/* draw a rectangle */ 3 E3 A* z: @2 ~  y7 K
rectangle(left,top,right,bottom); </FONT></P>% i4 _* T8 Z) V- D# b% o
<P><FONT color=#0000ff>/* clean up */ & C% D4 Q2 o' |  V/ i
getch(); 9 k. ^& ], o* Z  v5 G, Z
closegraph();
: |  K( a! H+ N" }4 p7 r$ `return 0;
/ w  i: u+ Z* ?# W}
) K# X/ F( U. i4 I</FONT>. z/ a9 I$ b7 L' k# d9 ^( d8 q
</P>) [" {0 l  J5 J$ d( N& K0 G
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>. R# E1 u) ^) }6 c* L% y7 ~
功 能: 登录已连接进来的图形驱动程序代码
7 A( c& m8 L, }( g用 法: int registerbgidriver(void(*driver)(void));   E& i9 W6 ^) `) g# s+ B
程序例: </P>
. B+ P* t, [4 A# y$ N$ J  |<P><FONT color=#0000ff>#include <GRAPHICS.H>
  M" r. r( k9 I. G4 b#include <STDLIB.H>$ d2 m/ O1 J' y7 z9 k
#include <STDIO.H>
! |& T) N" o% C7 v1 _0 {#include <CONIO.H></FONT></P>& A7 {7 {+ z5 b$ T. l- Z
<P><FONT color=#0000ff>int main(void) ; Y$ J. y, ^% l8 R. v  z- ?/ T
{ / p/ O  Z6 E2 B8 S0 H' D* E
/* request auto detection */
1 b( H) j  |! c( e) [int gdriver = DETECT, gmode, errorcode; </FONT></P>* f* c: v9 @; P0 C0 ^
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
, v6 R5 J/ G9 @8 I8 ^errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>! a  v: k2 f* K* D9 b4 \" N
<P><FONT color=#0000ff>/* report any registration errors */ 8 ]: n2 J' m! [) ~3 V$ q! Q
if (errorcode &lt; 0)
0 s  ?: |* Z# v3 _' g; C& L{ - d9 e" R) i& }
printf("Graphics error: %s\n", grapherrormsg(errorcode));
% o* \  g, f3 N0 Hprintf("Press any key to halt:"); ) k0 j* `. u: M+ Q+ s3 u
getch(); 1 T% U6 x; G. j1 v# }! e
exit(1); /* terminate with an error code */
, ]/ u( y' O. X} </FONT></P>3 y6 P! j. I4 W
<P><FONT color=#0000ff>/* initialize graphics and local variables */ & \. |1 r: k/ k" b" ]( ?* r- k, q
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
" L4 N! a& J( `" f% H' c8 |/ u<P><FONT color=#0000ff>/* read result of initialization */
! |, a# ?1 P0 K7 N6 s5 X! aerrorcode = graphresult(); " h) q4 {1 Y% m# N4 d7 Z
if (errorcode != grOk) /* an error occurred */ $ V  y  G2 A) B" c: W
{ 1 v' g1 X! X4 E. D; n; X
printf("Graphics error: %s\n", grapherrormsg(errorcode));
* K  j, W- p& r6 Q" z1 f! n+ sprintf("Press any key to halt:");
3 E5 Q* [  [% Q3 {' rgetch();
8 L8 m- x% B+ K* Y; pexit(1); /* terminate with an error code */
$ ~0 Z3 I, p) [+ u, @} </FONT></P>
' X7 ^& e0 }2 ~1 x5 {+ f<P><FONT color=#0000ff>/* draw a line */ , c6 E5 J; z, G/ y5 z
line(0, 0, getmaxx(), getmaxy()); </FONT></P>
6 E# o& o  ^. s2 c- Q<P><FONT color=#0000ff>/* clean up */
. I: S' \# I( b8 F. c2 Pgetch();
# p; I" s0 k8 Nclosegraph();
9 ^3 f, B# t3 Areturn 0;
% y7 U' B* r$ u- @! S+ j- \}
9 @9 `, ^( z3 u5 s( D, U! a0 y</FONT>. B. _# A9 M+ h& d
</P>) B& A# F" Q8 q0 X) ~
<P><FONT color=#ff0000>函数名: remove </FONT>
, h  q6 n2 c2 l- [& r1 X- |. ~+ ^功 能: 删除一个文件 " s) \: B" A) [7 ^: h
用 法: int remove(char *filename);
' i; p/ U4 \( ~& S; P! F* |2 r; V, j程序例: </P>
2 @, l, G6 p# {$ N+ [& N<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
5 D% F- Q+ E! A3 M& l<P><FONT color=#0000ff>int main(void)
6 S$ P) I4 z) `( B  }1 r{ ( w* H2 @: O$ z! h4 e
char file[80]; </FONT></P>( d$ T' X4 X! D- @  C; w7 P/ u6 w
<P><FONT color=#0000ff>/* prompt for file name to delete */
  I7 K  ]3 j3 [# T) L5 Nprintf("File to delete: "); - e& m2 D, ?' T+ p
gets(file); </FONT></P>
+ G; G  }& d0 m: b- b7 g* h<P><FONT color=#0000ff>/* delete the file */ $ t! O4 e- H+ Y
if (remove(file) == 0)
' b) Z5 |1 F4 G0 n/ f& G& {printf("Removed %s.\n",file); 5 h! o) k& Q6 m- \- Y% h
else 0 q& M. q' t/ T3 S. k
perror("remove"); </FONT></P>
# _/ E8 Z- ]) l8 j& F) D8 d<P><FONT color=#0000ff>return 0; # F  W4 s( H# {
} 4 W3 s1 A1 ^" i4 F* e# X8 n

- a. x( Z7 n8 N& Z4 f3 Q</FONT></P>
; ?  I& ]$ H+ s3 S9 B<P><FONT color=#ff0000>函数名: rename </FONT>
( u/ d. ~4 t+ Z5 e8 R) V, @功 能: 重命名文件 9 C: V6 H# x- k( Y
用 法: int rename(char *oldname, char *newname);
7 i4 B2 f+ q( ?. w/ k) V' v2 f% r程序例: </P># O( f5 Q! b4 c( u$ Z
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>6 [7 L" j2 ]. y
<P><FONT color=#0000ff>int main(void)
& S( \9 i7 q- Y/ ~% d{ , T- _* e, V8 H8 d9 {1 R0 k. J
char oldname[80], newname[80]; </FONT></P>/ j: e$ @2 V3 V1 ~
<P><FONT color=#0000ff>/* prompt for file to rename and new name */ ( o- _9 f  A& o" {" w
printf("File to rename: ");
( V( ?/ f' `7 J. b/ D& |gets(oldname);
+ l4 r! u6 ]( F: I! aprintf("New name: ");
. E& I( j# d+ K/ \2 jgets(newname); </FONT></P>7 J# {/ m9 F4 w2 J- d/ N
<P><FONT color=#0000ff>/* Rename the file */
: K6 E. z0 b$ Z  c5 Zif (rename(oldname, newname) == 0) # ^8 y& _, K/ M. N" j) x5 P
printf("Renamed %s to %s.\n", oldname, newname);
6 @7 @; l. {7 u: n6 Jelse $ z7 Y9 Z$ [! o, o- j6 g
perror("rename"); </FONT></P>
& K% V7 u" R* A" S  e! Q6 l* ~( I<P><FONT color=#0000ff>return 0;
# l8 k) D9 Q8 w4 b/ ]" `7 U& |} . i3 w7 Y/ B4 R4 E9 Y
1 l  Y, W2 [  m7 E
</FONT></P>
: J8 T4 _- ?0 n/ C' N9 p<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>" O% v0 A9 ^7 |3 E' b. g# D
功 能: 将屏幕模式恢复为先前的imitgraph设置
  {2 @% F, E4 a( z2 N; p0 k- V用 法: void far restorecrtmode(void); ; c* H3 e  d, X
程序例: </P>
% a$ d, y6 X3 r0 z9 l<P><FONT color=#0000ff>#include <GRAPHICS.H>
9 w* ~. l  ^! e. L#include <STDLIB.H>
3 C$ T/ @7 u, F, T#include <STDIO.H>
4 h" v# H' V; Q#include <CONIO.H></FONT></P>
- C0 p5 z3 @, V( r: b$ A$ K<P><FONT color=#0000ff>int main(void)
; C; _2 A% d: S{ . u1 f8 `5 C+ ^: p+ F
/* request auto detection */ ) H1 k1 S4 M8 r  x( U0 |4 f. G! p
int gdriver = DETECT, gmode, errorcode; 0 K/ [1 `" K( ~
int x, y; </FONT></P>$ X9 A4 Z1 C: u9 @) @
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 o2 K  ^( c& Z5 R
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
8 G) Z. Z2 Y  T<P><FONT color=#0000ff>/* read result of initialization */ 7 T. }; y* S, `2 Z
errorcode = graphresult();
) b/ H0 \' E8 r  V$ U" oif (errorcode != grOk) /* an error occurred */
' `0 L: V# U/ [, ]6 X: ~{ ( C6 M6 Y; T! Y7 v' L: w7 x8 v
printf("Graphics error: %s\n", grapherrormsg(errorcode)); / B6 `. N, i& W/ Y& P
printf("Press any key to halt:");
$ L8 \, P: |, I  p& r3 |( `& hgetch(); 8 T% J( A" M" B
exit(1); /* terminate with an error code */
' h7 F9 {3 C+ a# Y4 _4 z5 v} </FONT></P>
% i4 u! I" O6 w+ u1 X. s8 j0 j0 e, {<P><FONT color=#0000ff>x = getmaxx() / 2; 7 r/ i, ~* g* o
y = getmaxy() / 2; </FONT></P>( w* t% k, @. W1 O0 ~; V
<P><FONT color=#0000ff>/* output a message */   x  [0 ^% @: _2 ~3 ^
settextjustify(CENTER_TEXT, CENTER_TEXT); 1 v9 T' A! K! J! j2 ^, M% N
outtextxy(x, y, "Press any key to exit graphics:"); # y6 ]7 p+ p6 V0 N& H
getch(); </FONT></P>8 k& }9 [5 l0 s+ j1 ~/ e9 u
<P><FONT color=#0000ff>/* restore system to text mode */
4 U9 I  r) N# `" @restorecrtmode(); # Y  c' U9 m, q/ ~* [/ ?2 j
printf("We're now in text mode.\n"); 4 L! i0 x" m/ f* M' o6 ?1 G5 A6 W/ y
printf("Press any key to return to graphics mode:");
$ i1 {1 s: m, d3 \- v) ygetch(); </FONT></P>( c6 o- n1 W, B% M' ]  h  E* c: [% \
<P><FONT color=#0000ff>/* return to graphics mode */
5 |$ G- k0 M- k+ ~  J) f+ G/ usetgraphmode(getgraphmode()); </FONT></P>" j8 _& q: z, p4 _1 C
<P><FONT color=#0000ff>/* output a message */ 9 i3 U6 W/ g. @9 K
settextjustify(CENTER_TEXT, CENTER_TEXT);
5 `% G# a8 h/ M7 Z' @6 \outtextxy(x, y, "We're back in graphics mode.");
+ b: O; M8 H# n! N6 I3 ?" y1 e7 }outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>
1 N' u( O* c+ _/ Z1 d! m/ C<P><FONT color=#0000ff>/* clean up */ 9 Z( G: d4 k7 z0 @$ Q" N7 o
getch();
  a6 U) ]0 K" f0 v3 H2 n; _closegraph(); ; q8 a8 M  v0 `
return 0;
5 t) c* }+ q* K! C: y9 }" _}
/ a" \+ S. U# T' P7 v: I</FONT>. b5 j! ~' L' ^9 ~
</P>
# T3 a7 E3 w& c9 m2 E<P><FONT color=#ff0000>函数名: rewind </FONT>; t" b" e2 A7 [; u5 D7 ]' |) v6 ~0 P
功 能: 将文件指针重新指向一个流的开头 / z! E& o4 S: q4 g% [# B, i
用 法: int rewind(FILE *stream); * S% k! P) w, u) E
程序例: </P>
: m, K0 w. W1 n0 R- C, R; z" A<P><FONT color=#0000ff>#include <STDIO.H>
* d  p7 r4 ^, ]0 [' s  ~#include <DIR.H></FONT></P>- }9 ^4 k% n, E
<P><FONT color=#0000ff>int main(void) + j6 ~3 m3 L; X! ~' ~8 c
{
) a" E/ C+ Y* O2 qFILE *fp;
% |# W! @! b; y/ echar *fname = "TXXXXXX", *newname, first; </FONT></P>
1 U' r9 z( [& Y( x% Y; C$ s<P><FONT color=#0000ff>newname = mktemp(fname); ) G" J# T* J6 ]" S
fp = fopen(newname,"w+"); ; ]/ a% H( F4 W
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
# z- c+ ~" S/ F( Trewind(fp); 5 d" K2 a. k7 J
fscanf(fp,"%c",&amp;first); 9 w  e0 z8 G' L( W
printf("The first character is: %c\n",first); 1 S) x* X( j% y; u
fclose(fp);
0 ]5 m# i( E7 b+ Uremove(newname); </FONT></P>
9 m$ A# b# e# k2 [, `<P><FONT color=#0000ff>return 0;
2 J% N. U3 _5 u4 r}
6 l2 r7 d- x, u' I</FONT>. ?8 y+ q; Q" ]3 ?
</P>
, E+ b  F( r! E' P  m4 u<P><FONT color=#ff0000>函数名: rmdir </FONT>  H. s  c. e. ~: C' ^) B( [
功 能: 删除DOS文件目录
7 ]. k* l4 `/ ^6 u& q2 Z用 法: int rmdir(char *stream);
2 T" o8 w3 N& u2 h程序例: </P>" E4 Z! U, y6 {9 {
<P><FONT color=#0000ff>#include <STDIO.H>+ }( c8 X$ x) I+ m6 Q3 }, N* ?0 D! {
#include <CONIO.H>, l5 {6 z/ ?+ X! M
#include <PROCESS.H>
1 x' ]% ^& z9 o& G, d) ^& n#include <DIR.H></FONT></P>
0 Z0 z. f2 a0 @$ r2 {<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>& A5 b* f6 v( l' D) L
<P><FONT color=#0000ff>int main(void) : m3 H) z! Y4 i; \, G1 c
{ . _4 W; o, H2 L3 W: q
int stat; </FONT></P>
) L! v% n$ q; `3 i8 h: @<P><FONT color=#0000ff>stat = mkdir(DIRNAME);
: I9 R& h3 s6 H, k; M+ y- Wif (!stat)
$ `: X- w" o0 Dprintf("Directory created\n");
  b: Y  r. G2 l& b, q+ J0 r& Kelse 4 Y3 k0 x4 G' @8 y5 J) _% \
{ . ~) s! D& \, _) ~3 {, X5 o" W& I7 @
printf("Unable to create directory\n"); 7 D, |# F: F; f
exit(1);
" m" [0 Q# Y% q, k; m% Q" \, `: C. _} </FONT></P>
- t  x! w) L3 r8 F" \<P><FONT color=#0000ff>getch();
4 u2 Z, n% R: Ksystem("dir/p");
" g' {% p  U+ F; U3 agetch(); </FONT></P>
; o& Z5 |2 O" N8 L! g' Y8 o<P><FONT color=#0000ff>stat = rmdir(DIRNAME);
. m; E& g; h9 ~; Eif (!stat) ( o( N; n2 |# Z& f/ Y; Y
printf("\nDirectory deleted\n"); 8 ], ]# S4 i' V0 C; \
else ) x) c8 x5 h) U# V& D9 t
{   ^5 \) X( `! U/ L9 K/ }6 |9 n+ [
perror("\nUnable to delete directory\n"); + F8 P* ]5 K% n" T% i, S+ W/ k* F  |
exit(1); / Y6 L+ C3 j. _0 g2 ^
} </FONT></P># K+ H/ O: S* H+ I% ]
<P><FONT color=#0000ff>return 0; ) d2 `7 X6 |( j' F
} </FONT></P>




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5