QQ登录

只需要一步,快速开始

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

函数大全(q,r开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:55 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<><FONT color=#ff0000>函数名: qsort </FONT>
6 r6 @& K! k- i% F% f" U7 h功 能: 使用快速排序例程进行排序 ! ]+ N* s! W+ \8 R4 Z" u
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); 6 z% z2 i" q2 j, _3 {
程序例: </P>" H8 I% [1 F/ ]& J% @: h: l
<><FONT color=#0000ff>#include <STDIO.H>/ h* C+ h- E4 }& E8 O: Q
#include <STDLIB.H>" i8 O' @, S* q+ s
#include <STRING.H></FONT></P>
; O) V' E, `1 Q! C<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>5 Z) L9 V- M. J. y7 t- P. b
<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" };   U- j+ E: ]2 [8 F' W
</FONT></P>0 a5 a: c7 k1 \9 f/ t& g
<><FONT color=#0000ff>int main(void)
: ^# [$ p3 g2 t. g( ^% n{
) F3 e* e% H1 ]( xint x; </FONT></P>
& f, g: B+ K/ J6 n: M- r<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function); 2 L+ V9 U8 w: |; {
for (x = 0; x &lt; 5; x++)
. d6 F7 e) F; k4 y' ]" M2 Mprintf("%s\n", list[x]); ( V$ t0 Z6 a& p3 J9 W
return 0;
$ J: q4 ?+ Y) Y" Q} </FONT></P>
% o$ d& V" U3 f<><FONT color=#0000ff>int sort_function( const void *a, const void *b)
8 E$ N8 v$ M# D9 r{ ; r4 D% M# X% j- }- z- i; r
return( strcmp(a,b) ); / t$ d5 ?! M& a6 \
} </FONT>/ b; q1 b# [$ k; L+ M( I2 G
</P>
* ~+ _0 ]6 p: L0 w  y& P8 a( X1 N5 A, ]/ X9 i4 E

% K+ n7 A) w  B1 l9 W, A  a: ]<p>
+ S) ~' I0 r$ Q4 R# x3 S; r<><FONT color=#ff0000>函数名: qsort </FONT>) v' I+ A# a# i  H! A" Y; t9 T) m9 f
功 能: 使用快速排序例程进行排序
, r( f4 Y. l+ V! P% o& H" [用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); ; y- x, _. d1 Q5 \) U+ U! \
程序例: </P>
2 T# C5 [7 w% U" n<><FONT color=#0000ff>#include <STDIO.H>  A2 H1 e0 C2 F0 w
#include <STDLIB.H>5 x8 z. F3 T: W4 K9 U
#include <STRING.H></FONT></P>- O1 L9 s# J& b3 ]1 j
<><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
! }. ]& l  [+ ^) _$ O* r<><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; * u" x3 G0 Z, O; x% S+ H+ c
</FONT></P>
* p4 z6 @& `  y0 n- A<><FONT color=#0000ff>int main(void)
7 |# F: n, f* Y0 Z% C, e{ ' R( l  _3 X$ D" ~. F- K; Y6 L
int x; </FONT></P>, I9 X  s: X2 M( j# v2 o/ y
<><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
, @/ O7 e, ~- V6 Vfor (x = 0; x &lt; 5; x++)
* Y6 R: G4 z" }4 ]( Bprintf("%s\n", list[x]); & h( E% [( A! ~! p, m0 L/ |  Z
return 0;
- H! {& s0 A+ z3 M' z} </FONT></P>
7 @, s- K: p- @<><FONT color=#0000ff>int sort_function( const void *a, const void *b) $ g/ r! y  n# @% G
{
0 ?% A% |" L: ^return( strcmp(a,b) ); 6 [4 f; u5 i! Q  ?5 q+ x
}</FONT></P>8 r7 m2 T) B  S7 M+ R
<><FONT color=#0000ff>) ?$ t8 b$ H) y& _  m0 ^
</FONT>
. _- s! S) K4 }5 J; v</P>
* m# N+ v- U0 Z4 h- f  Z9 y<><FONT color=#ff0000>函数名: raise </FONT>
- g) U4 ~3 C# U5 f功 能: 向正在执行的程序发送一个信号 / k7 Q1 c$ u8 [; f2 @! \4 r/ l
用 法: int raise(int sig);
+ v$ S6 c2 O( A程序例: </P>+ T( I- s9 q. z
<><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
( F. ?! C# x# b% }1 ~4 s4 R6 ?( _<><FONT color=#0000ff>int main(void)
$ [- O. v/ H/ ^1 [3 p' n{
$ a" L! l, k& L# B; t6 x: Pint a, b; </FONT></P>
( P. q- v$ [' L& S<><FONT color=#0000ff>a = 10; 7 x8 q  K" H0 @, ~
b = 0;
3 E9 l. e+ S' N- `  N8 I0 Z2 sif (b == 0)
0 e6 T" c$ ~3 [3 v0 J* f' g/* preempt divide by zero error */
$ _2 }7 t0 r2 D" }- W* graise(SIGFPE);
8 E! m1 Z  Z! c8 p% j' F& ]1 sa = a / b;
. e& m7 r6 m* freturn 0; 3 Z+ [8 b1 s3 e* l
}
8 G' k* [  ^2 u( ~% }2 {, \6 Y</FONT>
! }2 X+ e, T8 S: l8 V& [</P>
3 [$ G( E0 P( H<><FONT color=#ff0000>函数名: rand </FONT>& m! M. }, l; v/ G1 u* O+ b: |: o
功 能: 随机数发生器 * ]7 K7 l- x* L( c
用 法: void rand(void);
3 _- e+ N' w* K! f1 e( m6 ]7 l$ z/ }程序例: </P>
, `9 L/ F. V- {+ r- s+ r. L<><FONT color=#0000ff>#include <STDLIB.H>
( a# D4 r6 c6 X  D. A  M% g#include <STDIO.H></FONT></P>0 }- Z2 R" L- {+ W
<><FONT color=#0000ff>int main(void)
3 g; \. m3 K' d{ & O" J3 _) o4 L& V$ n0 y
int i; </FONT></P>
! U4 D8 x9 r4 N4 A  L<><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n");
) j$ x2 H; G$ R# bfor(i=0; i&lt;10; i++)
4 c- x- F7 j, C; Nprintf("%d\n", rand() % 100); 2 |6 G! U, W- z+ k/ @" H, _
return 0;
+ f3 X; l# W" L  A0 K* N/ Q} ' G  H# s, O6 A; m( m- U
  X/ Y( ?+ V" C
</FONT></P>
: E5 S1 [/ C5 U<><FONT color=#ff0000>函数名: randbrd </FONT>
) |. d: U) p1 T2 u功 能: 随机块读 , W2 {5 }5 J0 D& X
用 法: int randbrd(struct fcb *fcbptr, int reccnt);
, m+ q, K' f$ U9 X. m& a) h7 d& U; Q程序例: </P>
" h8 I- S) |9 |<><FONT color=#0000ff>#include <ROCESS.H>
' L3 p" `: ?# I4 |" S* D#include <STRING.H>
: s  h, U( }8 u0 D+ W1 B#include <STDIO.H>
( h  Z8 }+ |" C% t. S# w* _4 r#include <DOS.H></FONT></P>/ ~0 u( w0 C6 z2 j& D0 |
<><FONT color=#0000ff>int main(void)
) u; H' k& N9 B: A{ $ P6 V0 U5 ^0 ]) b8 g
char far *save_dta; ) v( ~# F& B8 S, R0 a5 j7 q
char line[80], buffer[256]; ' V5 b+ c$ G+ c' E( b& j
struct fcb blk; 5 Z' ?$ v( r9 y# W
int i, result; </FONT></P>
* J. O6 e' ]0 `0 o# s! A% d8 X8 O<><FONT color=#0000ff>/* get user input file name for dta */ " o/ n! _0 Z8 Q3 u- u
printf("Enter drive and file name (no path - i.e. a:file.dat)\n"); : p- t3 L; U8 G9 T( y0 l, a5 X
gets(line); </FONT></P>
4 {. r0 v- q! r" V<><FONT color=#0000ff>/* put file name in fcb */
: A6 N" o( g& `; z8 T  M% Aif (!parsfnm(line, &amp;blk, 1)) # k$ a" R$ N7 T2 g' i2 u$ I
{
5 ]) J, K& j& D& Q$ |printf("Error in call to parsfnm\n");
1 k" O/ M' T) Y/ W1 J! hexit(1);
9 T# k( i2 |$ v/ D1 N/ y1 g} 3 O, N! J% W4 v' S( U7 h
printf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>; T- {; Z+ H" w! v
<><FONT color=#0000ff>/* open file with DOS FCB open file */
- }5 t) R' v# {! Z9 W0 R4 `. Fbdosptr(0x0F, &amp;blk, 0); </FONT></P>
2 o1 i1 I1 d- s<P><FONT color=#0000ff>/* save old dta, and set new one */
- G) [1 e' X% p. }% Z8 M) {0 ~save_dta = getdta();
2 L, `5 g5 P, h, y3 o0 Osetdta(buffer); </FONT></P>
" z; s( N8 B. q6 ]5 t<P><FONT color=#0000ff>/* set up info for the new dta */
# e4 Y7 U6 r9 T/ oblk.fcb_recsize = 128;
' G6 y& _5 j5 s0 wblk.fcb_random = 0L; 5 p/ Z. V  O3 X- t
result = randbrd(&amp;blk, 1); </FONT></P>* j" `2 V6 t( M* w1 m0 I' t
<P><FONT color=#0000ff>/* check results from randbrd */
6 x& E. u( B8 `. m% ^) dif (!result) ( U( z$ P& T. {
printf("Read OK\n\n"); 5 y7 t  ^7 J; j" A6 V' U" `/ M
else
0 b4 e# O. z# {( S  E  n1 E7 ~{
+ g% i" W- O8 v1 o  Nperror("Error during read");
' h! K8 N! ]. U# p- ?exit(1);
1 r. [3 B  \7 v0 o- r} </FONT></P>: b" f/ }8 l) B0 U9 o  h5 H
<P><FONT color=#0000ff>/* read in data from the new dta */
" k% y1 Z& C4 a) D7 n0 w" K5 Bprintf("The first 128 characters are:\n"); 9 x' \' X/ p7 w$ C5 T
for (i=0; i&lt;128; i++) 2 \! B2 }$ z# i: L* y: b
putchar(buffer); </FONT></P>
7 a7 `' I8 h$ H9 i1 U9 x<P><FONT color=#0000ff>/* restore previous dta */
' I6 }# r( Z) a/ x* Asetdta(save_dta); </FONT></P>
; O6 l4 m2 M0 |: L<P><FONT color=#0000ff>return 0;
% m- G7 Q+ p  d7 U' a7 O8 l* @} </FONT>& Z& \' r, p/ ?+ D; ~
</P>
% F1 {1 }2 g- X<P><FONT color=#ff0000>函数名: randbwr </FONT>
9 j) q/ R. }2 U6 Y# K功 能: 随机块写
( c3 g3 z! I& I用 法: int randbwr(struct fcp *fcbptr, int reccnt);
& Q+ h& `4 b. Z/ |- T% K9 ?" ~程序例: </P>' l/ w5 Y5 H8 T/ D  V
<P><FONT color=#0000ff>#include <PROCESS.H>: U' o# i3 w2 y0 C6 J! ~
#include <STRING.H>
6 R3 Q& p/ a7 @6 f% [#include <STDIO.H>/ x8 n( s' o: H, y' u3 S* Q3 n; s
#include <DOS.H></FONT></P>3 x; c* z! x1 m0 ^2 p
<P><FONT color=#0000ff>int main(void) & l, I/ _/ C* q- G
{
" X8 s* _  \- p( echar far *save_dta;
; i1 l3 r) w3 w, k4 y0 Fchar line[80]; 5 Q9 ~  G9 N) J( e# H
char buffer[256] = "RANDBWR test!";
* s& [3 N' n. _5 N' Hstruct fcb blk; + _2 f/ k6 v* m$ Y! u. {; n: ~
int result; </FONT></P>2 g! e: e% ]; ~. b+ b' ]5 c8 b
<P><FONT color=#0000ff>/* get new file name from user */
( e  i1 y& B6 @/ k6 Z0 W8 @printf("Enter a file name to create (no path - ie. a:file.dat\n"); 2 |# W( W7 q2 u/ t# c- Z
gets(line); </FONT></P>
3 L; U" p% ~+ i; h<P><FONT color=#0000ff>/* parse the new file name to the dta */
4 j& ]* N( ?$ t; Z+ L- v2 `parsfnm(line,&amp;blk,1);
  `0 n9 u. [, p: @printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
$ u. f- ~4 R5 j9 q/ U: N<P><FONT color=#0000ff>/* request DOS services to create file */ 9 S$ c% f- U# s5 Z8 F( T4 D4 d
if (bdosptr(0x16, &amp;blk, 0) == -1)
2 q7 I0 g0 ^! K8 D$ Y9 F( w) \) P7 s{
. x. F' f* A5 i/ j* V+ u7 \perror("Error creating file"); 3 \; I! y2 }( L
exit(1);
# y: m7 n4 A7 _4 t} </FONT></P>: R( K) D8 |0 K8 R2 N
<P><FONT color=#0000ff>/* save old dta and set new dta */ , |, ^6 Y1 e. S
save_dta = getdta();
, f. N+ c2 b) N+ k/ Osetdta(buffer); </FONT></P>' B: |4 {( x& k! M5 Z( G1 c
<P><FONT color=#0000ff>/* write new records */ 0 L: J# t- U3 z7 ?5 A7 |  m
blk.fcb_recsize = 256;
, P1 Q. r. u( i8 U# A7 V- hblk.fcb_random = 0L;
/ T1 i/ ^3 f, m: Z0 v; tresult = randbwr(&amp;blk, 1); </FONT></P>
2 \3 Z, l! M5 Z( {& n5 f<P><FONT color=#0000ff>if (!result)
6 V+ [) i7 |( I( h. i: _+ vprintf("Write OK\n");
# M# ~- ^* P6 W) n: P  S. Relse 9 r; J" o& y* B3 q& E
{ , @" y  ~3 P6 b
perror("Disk error"); 9 I8 R  E; O5 g  f( h, h' h& j
exit(1);
- r7 i' Q1 R# c1 q( v. h, e/ ]} </FONT></P>
& T. N+ {1 V& z, @& ?( O- w. A<P><FONT color=#0000ff>/* request DOS services to close the file */ 7 B, r/ T$ ^- }# h- B  o( i
if (bdosptr(0x10, &amp;blk, 0) == -1) , G' h- w1 ~% v) E, S8 q8 e" @! t
{ * {/ B# P; s, M* J
perror("Error closing file");
* b/ s' e( H- a+ a& F' pexit(1);
* }4 q, {( j( g- v+ g} </FONT></P>0 p5 [. _% d( E$ P% e: X
<P><FONT color=#0000ff>/* reset the old dta */ 1 D  L( ~3 o0 g& p
setdta(save_dta); </FONT></P>
5 z4 E2 D9 \# N: z) W4 O<P><FONT color=#0000ff>return 0; ) I% i; v3 J0 u. h; B
}
# j1 C5 A7 a3 A/ ]</FONT>% P1 Q) O. K( |* ?& `& X
</P>. T7 E( u( t3 D' M: ]
<P><FONT color=#ff0000>函数名: random </FONT>( k. @3 P8 L) o4 Y5 F
功 能: 随机数发生器 ( k6 w8 a$ b' u" u' C" ~
用 法: int random(int num);
; F. A- Q$ u# E: x程序例: </P>
0 J8 ~- s' r# _4 B5 b<P><FONT color=#0000ff>#include <STDLIB.H>
' S, M) U: \( K2 m7 E( A* @#include <STDIO.H>& z' k; F7 p- Q7 D6 h" R
#include <TIME.H></FONT></P>
" Y  i7 W! \5 r% U1 N<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */ ( h0 Y1 g( z3 @  {+ C
int main(void) 5 p" ^, E' r1 a% T; v1 ~0 g
{ 8 D0 J/ n2 P; w6 H- j
randomize();
; {) L3 d8 [4 wprintf("Random number in the 0-99 range: %d\n", random (100));
( P2 G7 g+ W8 \# Z* i! zreturn 0;
; }- q" b' h$ T# U/ q4 [}
* @7 a4 f' X* Y</FONT>
( B  ?* u9 c; w</P>
9 i' n. Y3 e1 N+ a+ |<P><FONT color=#ff0000>函数名: randomize </FONT>+ L/ b' u! J( [2 Q7 R1 A" ^3 k& O
功 能: 初始化随机数发生器
& M0 h8 k& Y: {6 f, d) U: s用 法: void randomize(void); ' [9 e/ P& ]& h4 V1 H
程序例: </P>
# l1 Z$ [' D: T. }0 M. r<P><FONT color=#0000ff>#include <STDLIB.H>; i" P! Q- _  A5 c/ F
#include <STDIO.H>
* H/ g+ \  Q) i; S9 e#include <TIME.H></FONT></P>. N: {* P$ N, J( b: @# j) R
<P><FONT color=#0000ff>int main(void) # [" J2 V6 H9 ]. D) x. y
{ / y2 o6 k; f' O' p2 P) `: a6 y
int i; </FONT></P>3 |8 Y- r6 I  J3 e, c# `
<P><FONT color=#0000ff>randomize();   P% _( W6 g- [- W$ \
printf("Ten random numbers from 0 to 99\n\n"); 1 n! u! @2 q+ {" y$ i
for(i=0; i&lt;10; i++)
" l  q+ w0 Z7 O: d" g8 Q5 gprintf("%d\n", rand() % 100);
6 ^7 E" v  `2 c& Z" Sreturn 0;
' y9 g- Z& q4 ?3 R# x6 D}
& m: [& q' D! F- v: p& }0 z" G+ M</FONT>
0 N  }$ v3 t" w9 Y</P>
8 _$ D! e! i  n' O6 J/ b" O1 N<P><FONT color=#ff0000>函数名: read </FONT>
5 W# b8 B. R* V3 Y- v" _& r功 能: 从文件中读
6 @6 v% y; m6 ~8 i# r; W用 法: int read(int handle, void *buf, int nbyte);
/ q/ c9 S+ N3 \' N# m程序例: </P>
7 c7 ~  x) j. n<P><FONT color=#0000ff>#include <STDIO.H>
9 m8 w' L" s# w+ o0 e#include <IO.H>
! h( i: ^* |6 |+ T5 \% z* w#include <ALLOC.H>$ L8 q( F4 {4 r& U
#include <FCNTL.H>
" y$ ]# z4 ]* O6 R/ C#include <PROCESS.H>
) x! V* m& |; x+ z) D* C#include <SYS\STAT.H></FONT></P>, G4 H' I* d2 {( k$ L$ i$ P
<P><FONT color=#0000ff>int main(void)
2 e! z, J: c% `( `0 {/ _{
$ t/ E" y- U; [+ |* _# kvoid *buf;
* z8 y: ~6 i1 z& oint handle, bytes; </FONT></P>) w* |+ b: q7 R& \# {3 L
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>. l) ~, E6 [1 a& `
<P><FONT color=#0000ff>/* : L! u7 g2 v( z% e+ d# v" q
Looks for a file in the current directory named TEST.$$$ and attempts
8 G% x0 q( A; U5 M- Yto read 10 bytes from it. To use this example you should create the $ a+ i6 g& i4 y
file TEST.$$$ 9 O  v8 ]# V* H, M! A1 A
*/ 7 a$ G9 J( }- c2 v- M' A# R- C# Q
if ((handle = # ~& q" `7 R0 x, U: k  d; h
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
, t+ S) Y/ |  S# f# d9 [) i& }$ Y{ + ^; r! o' F! t: o: U) f( a7 l
printf("Error Opening File\n");
2 j$ j) \# k- z# o; m/ t- aexit(1); 6 B/ k; x& g6 X$ U2 b
} </FONT></P>1 t; u4 z' @3 i5 k
<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) {
, Q+ z9 K( X- b" mprintf("Read Failed.\n"); . C2 w6 ^' @6 s, Q) W6 b9 w9 }7 s
exit(1); " ]* m4 [; X9 A4 m$ V
} 3 B; @2 f: ]4 S/ z( _1 d
else {
/ d6 @" f4 {) e5 o5 p0 b- Aprintf("Read: %d bytes read.\n", bytes); & T4 `0 n+ h2 ~5 |
} 1 \+ T: a% M( d( A& a. j* F  j
return 0;
+ T3 ~; r) R$ O3 A2 H, j( g$ l* m0 t} </FONT>- G; G3 x. w3 v1 V9 [' S& S
  B7 |0 t% f5 A9 w0 i
</P>' v# T6 b: a' C8 c8 \* j0 W
<P><FONT color=#ff0000>函数名: realloc </FONT>
! r% v$ `! i. Z$ _/ E4 O功 能: 重新分配主存   v; U, L2 d6 F. n+ @0 f: J
用 法: void *realloc(void *ptr, unsigned newsize); , f% P1 {) W/ [) {
程序例: </P>! L! B  Z/ J/ m2 T: u
<P><FONT color=#0000ff>#include <STDIO.H>
- K8 I4 s% Z" ~: ?. A5 g. l8 C#include <ALLOC.H>% b4 R. d* L5 s- H( G
#include <STRING.H></FONT></P># j& q; T' r1 p# u9 J8 D+ S
<P><FONT color=#0000ff>int main(void)
  S9 n' H7 n( }$ ]; \. N1 N{ ; `: A2 r/ u. p0 N- d8 y2 N
char *str; </FONT></P>
/ N; f; R% N* F( i: N' ^<P><FONT color=#0000ff>/* allocate memory for string */ 7 K2 G5 E& c+ C' ^& y0 S
str = malloc(10); </FONT></P>2 p9 H& Y1 i; [0 w
<P><FONT color=#0000ff>/* copy "Hello" into string */
8 U% n. @+ u. z: q8 Nstrcpy(str, "Hello"); </FONT></P>. g9 m+ f  D( Q# y' y
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); ; A/ ]0 n/ n, z& F; ?9 W' h
str = realloc(str, 20); . }- O( _  W! V5 i: ^, j
printf("String is %s\n New address is %p\n", str, str); </FONT></P>: O: j/ j$ B: b1 g: V
<P><FONT color=#0000ff>/* free memory */ . w5 }6 Q- E8 E: O* h
free(str); </FONT></P>
4 T0 V& b0 y* l$ O! I5 n<P><FONT color=#0000ff>return 0; & x2 u  L+ i( V2 W
}
/ S. U' [% C  ~; n, \$ m- K</FONT>1 l5 n: O( C8 G8 k0 O$ {
</P>( Q0 ^; o5 ?) M& o
<P><FONT color=#ff0000>函数名: rectangle </FONT>" ?5 ]! `# Y+ q* N$ m7 `! F6 y* C
功 能: 画一个矩形
1 H% z, g) i8 J- Z用 法: void far rectangle(int left, int top, int right, int bottom); ' w4 U6 q/ l7 ?* I
程序例: </P>
2 Q. b0 z1 c8 C+ B7 x2 h<P><FONT color=#0000ff>#include <GRAPHICS.H>0 B" v3 `( X4 ~6 G
#include <STDLIB.H>) b7 c* F! o! f+ N7 u
#include <STDIO.H>
) c/ Q. o! y) s) H7 u#include <CONIO.H></FONT></P>
7 P0 C  j4 Z' ]: Q0 n9 O* T/ q<P><FONT color=#0000ff>int main(void)
  m3 J% I1 H) ~' H2 o5 j0 g, d{
' g9 P6 F! Z+ s$ x" ^  J4 l/* request auto detection */
& l9 P: _  o5 Zint gdriver = DETECT, gmode, errorcode; 9 X( ]* V6 |- a
int left, top, right, bottom; </FONT></P>0 z' H& k9 p  s
<P><FONT color=#0000ff>/* initialize graphics and local variables */   O- M' c, v" {$ v4 c
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>! S7 z+ c+ N( L! W) i
<P><FONT color=#0000ff>/* read result of initialization */ ! b% F" r: u$ M! ?( K) w* L
errorcode = graphresult(); 5 F9 Q4 s! C. `3 e9 ~% s( L
if (errorcode != grOk) /* an error occurred */ / d8 u) `- L1 }% M: M" _
{ 7 W4 n9 f! k. y) m& P% ~' Z6 _
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 0 |9 s# N& N2 E8 F0 f' [
printf("Press any key to halt:");
3 N$ r" s7 u3 A* z2 I  t2 p& Lgetch(); ) z# W6 o. K" G- d7 F
exit(1); /* terminate with an error code */ ! R8 ~9 I4 r5 n3 H
} </FONT></P>
/ O- A+ R  [- O7 q8 C5 A4 p<P><FONT color=#0000ff>left = getmaxx() / 2 - 50; ! r" \1 B. F7 p$ |
top = getmaxy() / 2 - 50;
4 B6 `) X4 I' q! v2 p# |right = getmaxx() / 2 + 50;
, T. N8 L1 K, C3 ]bottom = getmaxy() / 2 + 50; </FONT></P>% g5 o5 v( F7 o
<P><FONT color=#0000ff>/* draw a rectangle */ ! ^: ?- H9 V1 ^- {% U0 U% r
rectangle(left,top,right,bottom); </FONT></P>
' |1 |# I- I1 K9 X( c2 q6 g) o2 U<P><FONT color=#0000ff>/* clean up */
& s' a8 l2 U: {3 E2 @% Xgetch();
( @. `1 k- _, r5 ~# Oclosegraph(); , T( X7 C4 [, O; J2 K. u8 ]5 e
return 0;
6 E( ]( U1 d/ l/ c! G. K}
- g. [' I& T, J: f' X1 g9 F- m</FONT>
; ]; c( `" m) {: O* x  r4 Y0 Z3 r</P>
. Z6 }( O" S, }: V. Q# @<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>% W3 u: G2 m5 I) b$ i& a- y! B
功 能: 登录已连接进来的图形驱动程序代码 . a$ \( P3 i* @6 R9 y
用 法: int registerbgidriver(void(*driver)(void));
# h$ n+ O" J9 h; ?) ~2 C2 _$ x程序例: </P>
, l  F. M/ N7 L, ^<P><FONT color=#0000ff>#include <GRAPHICS.H>
, I8 a0 g) M: W/ w/ w  }0 [6 {' r; B#include <STDLIB.H>
9 m' Y( ~. Z6 y+ w( h#include <STDIO.H>/ e1 ]5 C& V; U
#include <CONIO.H></FONT></P>& L# D5 {& T1 ^0 w7 N% G6 e
<P><FONT color=#0000ff>int main(void) 3 \! d" M2 G5 p
{ 2 O6 a5 n5 _2 B! E
/* request auto detection */
  {5 N, _1 u" g9 N0 M# V! Y9 Qint gdriver = DETECT, gmode, errorcode; </FONT></P>
: [' k2 m( o9 H3 U% s. Y  y<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */
4 b+ N9 ]8 P" ]3 eerrorcode = registerbgidriver(EGAVGA_driver); </FONT></P>
  O! Z, l$ J: W" n% i  Y7 h/ V<P><FONT color=#0000ff>/* report any registration errors */
* V/ W. o6 ]4 y% Y' a* D  iif (errorcode &lt; 0) 5 c" j2 F0 V# S; O$ N
{
" T( f: V- a* R, y4 iprintf("Graphics error: %s\n", grapherrormsg(errorcode));
2 |, n+ p9 ]; k- d" t) w. Eprintf("Press any key to halt:");
! c$ r2 ]. G+ D' E% tgetch();
5 O1 N  p4 ]! T7 E! M7 N) {exit(1); /* terminate with an error code */
. A+ r( X: \' M5 K, U$ P} </FONT></P>
$ A+ O7 d; [% H6 m" l: ?. t9 U<P><FONT color=#0000ff>/* initialize graphics and local variables */ " M  x- H6 j- q. X+ n3 G
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
- N" {7 s' l2 ?7 ?# M<P><FONT color=#0000ff>/* read result of initialization */ 8 G6 Q% k! L- {. F/ a( e
errorcode = graphresult(); 2 Z5 z9 z% g& a/ ], e
if (errorcode != grOk) /* an error occurred */ * {7 A! f9 T0 U. G. z5 d1 ~
{
& j: p6 A. f% q% P& G$ Sprintf("Graphics error: %s\n", grapherrormsg(errorcode));
, {  F6 x6 `) ^1 t1 p( gprintf("Press any key to halt:"); ( ^  l- n% p+ ?! b# E% a8 ?
getch(); ! ]1 E8 _1 c$ g
exit(1); /* terminate with an error code */
% Q1 k% q$ j! {2 \: w. z5 R8 n, t} </FONT></P>
; A. `/ R( N8 ^/ Q<P><FONT color=#0000ff>/* draw a line */
- l7 I- q* X  i# Fline(0, 0, getmaxx(), getmaxy()); </FONT></P>/ L" Y5 @5 }; v+ ^, }  a' O
<P><FONT color=#0000ff>/* clean up */
' C  O/ y1 `  ?; n; u, {% rgetch(); 2 `& x; Q# F; P% f. z
closegraph();
* C* C$ S9 Y9 `  `6 g% O$ Treturn 0;
: L- `8 B& O) {5 E2 e! N} : {0 F1 c3 f  P1 t
</FONT>; L+ F2 J. n; ]+ s/ p7 e& y
</P>. p* W- Q2 p% d0 ?% _' Q. G! u
<P><FONT color=#ff0000>函数名: remove </FONT>
  |" I3 a0 e( [$ x功 能: 删除一个文件 1 O, _0 A9 O$ m: R( y
用 法: int remove(char *filename);
! ]# P# J/ }* ?程序例: </P>
( F# Z( E* ^& @( S+ W, y: N<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
( ]" j: P! U' V/ N+ |7 m$ m<P><FONT color=#0000ff>int main(void) * q" N  P) o7 P; F6 p; s3 V
{ 3 ]/ v" a1 T& h+ e! X' q
char file[80]; </FONT></P>8 g6 g/ `4 p! y/ g
<P><FONT color=#0000ff>/* prompt for file name to delete */
9 J$ G" P5 C1 {- i( y  ~5 L  ^printf("File to delete: "); 2 T$ {; Q7 u8 {: K& c
gets(file); </FONT></P>6 U4 s" h, B0 V) F4 |' x
<P><FONT color=#0000ff>/* delete the file */
8 n) C9 o* Q0 v& X) V+ |if (remove(file) == 0) , j' M9 U& G0 K3 ]* L2 u# ]1 m
printf("Removed %s.\n",file);
4 p+ `4 r0 @5 a8 E5 y; welse
8 S3 @9 l1 M' H) ]+ Eperror("remove"); </FONT></P>
; y, m% m: B7 R<P><FONT color=#0000ff>return 0;
/ P/ {# j6 I  m1 A+ W, M* Q} 1 q6 i' F1 h# w; o

# I# T; Q9 C0 ]1 }* E1 V. g7 k$ _  B</FONT></P>
$ ]. \1 t! N4 s( ~3 e9 J<P><FONT color=#ff0000>函数名: rename </FONT>; I" w1 z# g% V( G* \
功 能: 重命名文件 ; R0 F8 w, a% a5 N4 d% u3 f
用 法: int rename(char *oldname, char *newname); 2 f' t/ v" e. Y' i
程序例: </P>
- t9 z2 n" w( Y<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
: T! p+ e, h9 G0 n$ I1 w0 ~9 h" n7 S- H<P><FONT color=#0000ff>int main(void) ( L, i9 f; T' y' Q& q  z( t- k2 ]9 z
{
/ ]+ d" r+ d! ~0 Zchar oldname[80], newname[80]; </FONT></P># g4 \' ^# u2 E0 r+ r
<P><FONT color=#0000ff>/* prompt for file to rename and new name */ - t& ]$ Q/ g- c  R5 v# l% X- c
printf("File to rename: ");
/ M) W% J7 r; k; q  o9 lgets(oldname); 0 ~( |% E4 X: Y1 q) C1 f
printf("New name: "); / Z/ e" n. m3 E+ n8 J1 I* K$ {
gets(newname); </FONT></P>+ d0 J+ j+ G2 x" _, t' M6 j
<P><FONT color=#0000ff>/* Rename the file */ # Q7 U6 C1 r1 e3 a4 d  R, x4 F* T
if (rename(oldname, newname) == 0) 6 Z# S6 L# F* \4 I$ b+ t# h0 {0 b
printf("Renamed %s to %s.\n", oldname, newname);
9 @8 V2 j$ U0 [2 e9 g( telse
! ?* i9 d6 R; _! Lperror("rename"); </FONT></P>9 J: R/ E4 _4 |8 E  K- X. P
<P><FONT color=#0000ff>return 0; : w. D6 R2 U1 F4 E( d$ N4 Q
}
* I$ k: P! C% g1 T- V0 P. U1 c1 M+ n0 c1 e
</FONT></P>
! ]" y  _; B: p- V9 ^0 ]<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>( w$ A, i! v0 J/ W. y) m
功 能: 将屏幕模式恢复为先前的imitgraph设置
' ]1 V, n- W8 A0 u! z0 G用 法: void far restorecrtmode(void); / ^6 U4 F! |) f$ `
程序例: </P>1 t- J, b, E4 V, M! U/ b8 Y
<P><FONT color=#0000ff>#include <GRAPHICS.H>
9 P& J  I( o9 @, x; z" O2 O#include <STDLIB.H>" P7 u7 g( W: x& k
#include <STDIO.H>
0 H. ~2 @$ U, W#include <CONIO.H></FONT></P>
8 z1 ?7 p% }: J3 A. Y2 P7 |<P><FONT color=#0000ff>int main(void)   y4 D/ I( K+ M# Q" `2 H- }
{ & r% p  y, B' l
/* request auto detection */ ) ?, L4 k* D( Q  s# u
int gdriver = DETECT, gmode, errorcode;
3 Z2 A+ T2 h3 [2 M8 y9 `3 w% {int x, y; </FONT></P>
( S. c  Q7 n8 {! L<P><FONT color=#0000ff>/* initialize graphics and local variables */ ; K  T- |# Q7 ~6 [
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
) `' V* s6 Q5 u7 C# s" V<P><FONT color=#0000ff>/* read result of initialization */
# m) _# P! ^. h9 Qerrorcode = graphresult(); ' p& K  X  C( l% X7 J5 ~$ C
if (errorcode != grOk) /* an error occurred */
* Z9 c+ L; R. b{
% }# ?) g2 Y3 F# Y2 f& Dprintf("Graphics error: %s\n", grapherrormsg(errorcode)); " J' e/ ]5 L! P* G. @  e  v2 j
printf("Press any key to halt:"); . R* b7 E, u3 ~  B: [9 j2 \0 t
getch(); 4 `: O1 r1 k- T" v* X  x8 {) F
exit(1); /* terminate with an error code */ 9 @+ T2 x. v+ B4 a* g! @
} </FONT></P>
) N$ c/ l0 Z, @4 f) H' A<P><FONT color=#0000ff>x = getmaxx() / 2; & K# q/ z+ F/ ?3 D- S: O; Q$ i
y = getmaxy() / 2; </FONT></P>
6 j+ A5 b" Y7 z& x/ S4 k8 M<P><FONT color=#0000ff>/* output a message */ 4 q/ G3 J% u! h
settextjustify(CENTER_TEXT, CENTER_TEXT);
- `* ?1 Q' s; b- I* B4 xouttextxy(x, y, "Press any key to exit graphics:");
/ f) ]$ j; }9 J9 S5 ^7 V( R( hgetch(); </FONT></P>
" v/ @6 W: t) ^  A. h9 D5 f1 [<P><FONT color=#0000ff>/* restore system to text mode */
. p% c6 N' E' Z  i. }& D( trestorecrtmode(); 5 w- u% `/ f, M1 ]" U( B# l2 M2 i
printf("We're now in text mode.\n");
; G# u" ?6 L0 ?printf("Press any key to return to graphics mode:");   H$ y; x, d" O" i0 e
getch(); </FONT></P>
, p  k. ~6 x8 j  \3 s7 W. C5 |<P><FONT color=#0000ff>/* return to graphics mode */ % Y& n* i% h3 ^4 S; ~& }( M2 E
setgraphmode(getgraphmode()); </FONT></P>  l; j. v: f+ n( G
<P><FONT color=#0000ff>/* output a message */
( W( K2 r/ i  M& y$ Q) E2 Hsettextjustify(CENTER_TEXT, CENTER_TEXT);
. r1 V& c/ V1 P% j- e% }/ }1 touttextxy(x, y, "We're back in graphics mode."); 3 ?2 R- I2 U  p; q7 t% v7 j, C; q
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>8 @2 L4 D2 d+ V1 J7 `
<P><FONT color=#0000ff>/* clean up */ / G% g# \* U; W
getch();
5 {* y$ W+ ^( n/ ]+ g9 jclosegraph();
3 O0 b+ f1 a' v! |7 k2 greturn 0; ! E, V! a8 q1 |9 z' Q
}
) ]8 a7 m  t+ B/ Y! a</FONT>
$ v6 U: `% d  a! P) [$ e( a</P>2 I: B$ Q; `! c) V) a9 Z
<P><FONT color=#ff0000>函数名: rewind </FONT>
* W# [9 i9 V. M7 L8 v功 能: 将文件指针重新指向一个流的开头 ( l' x' v, \0 _7 ?
用 法: int rewind(FILE *stream); , Q+ J. R8 [8 ^/ _8 `/ j7 @
程序例: </P>
, K8 e9 E: e& z% E: j/ W<P><FONT color=#0000ff>#include <STDIO.H>* Q5 ~+ p" m& U/ I5 b$ ]2 Q6 c  b
#include <DIR.H></FONT></P>
* s2 l) \) f8 O& x<P><FONT color=#0000ff>int main(void) / T; `/ Y+ S& f7 U  @
{ 9 k4 Y) w6 P" @1 v5 a: X1 \
FILE *fp;
' C: V8 i! W! x! F# \2 Z% W% G7 pchar *fname = "TXXXXXX", *newname, first; </FONT></P>
" x3 L1 m3 B1 M<P><FONT color=#0000ff>newname = mktemp(fname);
% h: \5 a4 D# B1 b& Z; ]5 Pfp = fopen(newname,"w+");
- P# I- N- B+ _9 c0 i& N! _fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
( f: A: l* c  V; G7 G# N+ |% M6 erewind(fp);
/ D8 Q) W. J+ f& @: C6 l! L! wfscanf(fp,"%c",&amp;first); 8 y! p! z1 W9 d; [# }6 q
printf("The first character is: %c\n",first); ' t* q# h/ r" O; Z
fclose(fp);
7 W1 K; q/ [0 p6 O% o0 L7 Premove(newname); </FONT></P>
0 W; _  j7 u$ D3 a4 n<P><FONT color=#0000ff>return 0; . K, ]3 d$ @  ?  N" x
} - I0 _; x9 Z/ b6 O) Y/ }
</FONT>
8 A  _) m# y5 E: H% `! H: K3 D/ J6 M</P>3 _: y) Q8 s# c
<P><FONT color=#ff0000>函数名: rmdir </FONT>
, e" w/ l0 |* q) [8 Z# O功 能: 删除DOS文件目录 1 }6 X9 ~+ N8 l6 K; D
用 法: int rmdir(char *stream); 9 Z4 L  a% l  U6 ~9 W" E
程序例: </P>1 T8 _; n3 Q: [! ]+ U  }1 ^( S
<P><FONT color=#0000ff>#include <STDIO.H>, E: Y* \8 r& F- t# d) H3 L# ?; k6 k' h
#include <CONIO.H>
2 I5 Y3 d) y& n! n, I' G#include <PROCESS.H>
* d' b! G# O, f' q, C& B8 c#include <DIR.H></FONT></P>0 }; a) ~- y1 Y' [
<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>- B, h  ]4 Z5 \+ z
<P><FONT color=#0000ff>int main(void) 9 t+ n/ f! H! Z* Y$ h
{
! t  W3 o6 O6 Q9 o2 `int stat; </FONT></P>1 |) p. I1 `7 E
<P><FONT color=#0000ff>stat = mkdir(DIRNAME); 3 D. e/ t' [: f/ f% `" {# t( {
if (!stat)
! E: S' W! v4 w7 w; Aprintf("Directory created\n"); " M% V1 U# u: i* s
else & o; E% h3 D8 a) a; N& ?
{
$ X* S( K2 c2 G6 M; |printf("Unable to create directory\n"); $ B. Z- Z% d$ o
exit(1);
( v" K( K4 L. T" t} </FONT></P>
( j) e- z( ^5 W1 v<P><FONT color=#0000ff>getch(); : |# v) _8 t& ?) N( C$ c" i
system("dir/p"); % O" H) e4 u  [. u! }3 ^6 R1 W
getch(); </FONT></P>
7 l$ B* Z4 V$ \) E' q<P><FONT color=#0000ff>stat = rmdir(DIRNAME); 6 M( L5 k# Z; d& J
if (!stat) 8 x/ z. g5 X) M
printf("\nDirectory deleted\n"); ( c# w: {, z. v
else % `2 B7 e: _5 X4 n, T- o
{ # o) c* X% _- @) C0 F/ M
perror("\nUnable to delete directory\n");
! D0 D3 g9 ~/ d; e& v' ?exit(1); 3 T& Y9 c% G1 K: T2 w
} </FONT></P>" i1 D& V7 K, K8 m
<P><FONT color=#0000ff>return 0; $ g  b7 }4 {6 ]
} </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-10-30 08:02 , Processed in 0.439755 second(s), 52 queries .

回顶部