- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(m开头)</FONT></B></FONT></P>2 _! @- C9 R U% W @
< align=left><FONT color=#ff0000>main()主函数
1 x% T+ X' u1 K; }</FONT>每一C 程序都 <B><FONT color=#000000>必须</FONT></B> 有一<FONT color=#ff0000><B>main()</B></FONT>函数, 可以根据自己的爱好把它放在程序的某
1 y P9 h' u5 h6 O个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放
( T# s, A& r& h: @' ]在哪个地方, 以下几点说明都是适合的。
9 i4 N* a9 z8 ?<FONT color=#000099>1. main() 参数 </FONT>5 u+ V4 ]& Q& C7 y& K0 W5 u
在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。 1 L" `2 r7 B" F
* argc: 整数, 为传给main()的命令行参数个数。
! p' P2 `, g7 u( e1 g+ n K" M, M* argv: 字符串数组。 ( H' D; h: s; R0 j; P; U
在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0 . y+ j% I4 ?) m) q
以下的版本, argv[0]为空串("") 。
5 y# f$ Z/ F# D4 w# v/ F8 eargv[1] 为在DOS命令行中执行程序名后的第一个字符串;
0 ?0 D5 L8 d. R/ |9 t0 y+ j6 largv[2] 为执行程序名后的第二个字符串; 7 m3 \* u/ e& F3 `" i) ~" r
... , d- x+ W" I3 ^
argv[argc]为NULL。
7 @* q" v/ h+ I7 G, L! D3 |- v6 v*env: 安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符 / r. e- Z: j! N" @
串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C:
# a8 n4 ~- {3 v0 t; F: p p\TURBOC(对于PATH) 或YES(对于87)。 ; V- ]3 J+ a% b' L5 N
Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中 ) R4 Q! N; z* x
说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序
4 e# K1 n! X) A7 t. \( T5 R的局部变量。 : Z/ x% J# B2 o4 Q) b
请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下 ; v% {# r+ q1 C! G
的例子:
. a L$ @' L# u+ g+ E<FONT color=#0000ff>main() ( f8 D. c( f8 n" o( M! T
main(int argc)
. t1 s, ^# p5 Zmain(int argc, char *argv[]) ) b( u1 ?6 F1 @9 e) N, h- i
main(int argc, char *argv[], char *env[]) </FONT>) r' d3 R8 T R' M( e' c* \# l
其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不 / u3 w0 \# W! u' `6 M( Q m
用argv[]的情况。 7 i6 r: p& _; C1 P. @. j6 O
以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数: # ?1 d- C8 m3 w8 O, d( m) ]) M
<FONT color=#0000ff>/*program name EXAMPLE.EXE*/ $ u* j/ R, ?; g7 m, V4 M/ K
#include <STDIO.H>$ D6 \& y; U6 A- r
#include <STDLIB.H>
0 h: }" T6 |0 S- C* Fmain(int argc, char *argv[], char *env[])
: f! \ `6 S) w{ ( H/ T+ g* e+ H# r8 p5 A& u/ a
int i;
7 U# B; K3 V/ P: ]" Y2 w1 U2 C2 ^6 j+ _printf("These are the %d command- line arguments passed to 5 O9 \0 A0 ?# t# ]8 K* |* ]# G
main:\n\n", argc); " i d e! [+ N4 T- K! N. L
for(i=0; i<=argc; i++)
; @$ L5 m9 V8 K, E9 eprintf("argv[%d]:%s\n", i, argv); 6 Z3 h$ N# [/ {1 f9 r0 v! r
printf("\nThe environment string(s)on this system are:\n\n");
: y# Y% a2 z. s4 ufor(i=0; env!=NULL; i++) 6 q. f/ f. v: X% V7 K; e
printf(" env[%d]:%s\n", i, env);
0 k2 J! U9 @/ o" H6 p" s} </FONT>; I3 w7 R% P) |. ^
如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE: " R1 t/ i6 I( e, w$ A
C:\example first_argument "argument with blanks" 3 4 "last but 9 L/ N* M0 j: g8 b, s# e
one" stop! J( R* Y9 @6 P s5 f8 w) P6 S
注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument 3 k2 V5 \9 x# J& h2 c d$ q
with blanks"和"Last but one")。 ; m5 a! `; f" ~9 L+ H3 O
结果是这样的:
# n: C% i- O6 S- G, M+ m<FONT color=#0000ff>The value of argc is 7
( i( b9 x) [# e( T9 n5 D$ lThese are the 7 command-linearguments passed to main: ' i4 R/ j' b; k3 C5 Y0 }
argv[0]:C:\TURBO\EXAMPLE.EXE $ {; O: J3 _* D
argv[1]:first_argument P K* e) u2 i! v; U6 ~+ @( r+ W
argv[2]:argument with blanks 8 u( p6 \- }5 i
argv[3]:3
8 L9 O0 S) T8 y) pargv[4]:4
! O' `! n) E+ O* gargv[5]:last but one 8 f2 {$ J7 j& q @# B$ D% c
argv[6]:stop!
9 o" p/ \6 N8 O9 gargv[7] NULL)
) r' `5 A( S. K3 o+ QThe environment string(s) on this system are: , G# Q8 q& N" @+ a( |8 D
env[0]: COMSPEC=C:\COMMAND.COM 2 z5 b6 c; |# v- s, c' N
env[1]: PROMPT=$P$G /*视具体设置而定*/ ( a* }: R% a- r3 J' c. |
env[2]: PATH=C:\DOS;C:\TC /*视具体设置而定*/ 5 p o/ p) d. r2 S) T* L' G
</FONT>
# Z2 H. f4 q& c+ ^应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包
' M4 P$ j- `1 F' [) E括参数间的空格), 这是由DOS 限制的。
: V$ ^$ c6 J0 b' {6 {6 | H; ^</P>
1 A1 y. p6 D3 m5 Y" ]& V" @6 t< ><FONT color=#ff0000>函数名: matherr </FONT>
/ [; ~& b- Z3 a; E0 z) _8 L6 |1 U功 能: 用户可修改的数学错误处理程序 6 c1 b+ N P! |; u; w
用 法: int matherr(struct exception *e);
( n1 m( u( H! t8 e; o) i+ e3 @, Y程序例: </P>
: }; K7 g8 `4 o$ B2 e< ><FONT color=#0000ff>/* This is a user-defined matherr function that prevents 5 Y! d8 l' Z6 } a: t, O
any error messages from being printed. */ </FONT></P>: y: B1 _6 ]3 j% ~: {. z
< ><FONT color=#0000ff>#include<MATH.H> </FONT></P>- l9 ]) I. }. u* y% {# V% i$ p( q2 W
< ><FONT color=#0000ff>int matherr(struct exception *a)
2 u1 B4 @( b1 v% t& F, Y, M{
. t% l4 {; c- m' ~return 1;
) N& o/ k1 C; ^1 _/ o8 `, U, X/ z}
& {% @" s$ u1 ^6 {( z4 P2 G5 @</FONT>% |1 t: V# T# A- e% D
N. P2 `8 ~, O; m1 b% ?</P>' A% ], Y5 f& ?. Q
< ><FONT color=#ff0000>函数名: memccpy </FONT>
6 }" g9 Y$ s" N8 |功 能: 从源source中拷贝n个字节到目标destin中 + j9 C3 _0 h( U3 n1 H3 I# z% P0 ~
用 法: void *memccpy(void *destin, void *source, unsigned char ch,
4 p6 S+ b9 C' k& F% R9 r/ i+ Dunsigned n);
0 \7 ^9 ^7 j4 V/ m程序例: </P>: C% d$ j- B# A# m
< ><FONT color=#0000ff>#include <STRING.H> z* F6 D& \4 y# q
#include <STDIO.H></FONT></P>6 F6 x% b7 t* j) i. |# u0 G1 M
< ><FONT color=#0000ff>int main(void) / E$ z; _/ C8 E4 i
{
) Y+ a; Y; B: J; u" a. F q- Qchar *src = "This is the source string";
( n7 i$ [5 D8 ]$ L2 [. o1 @9 echar dest[50];
* }; f& M5 L' J; @2 [char *ptr; </FONT></P>2 l# \2 t3 L9 ?5 T4 W
< ><FONT color=#0000ff>ptr = memccpy(dest, src, 'c', strlen(src)); </FONT></P>
: x* j- E# R j# \6 W< ><FONT color=#0000ff>if (ptr)
6 @4 U" M4 }2 o! U4 b- f1 }{ & H/ D4 U* {+ g! n# u+ y$ L
*ptr = '\0';
& h+ R! b4 N# l* Rprintf("The character was found: %s\n", dest); 0 \7 G2 x! V" {, D% E2 T6 T$ R! S
} $ R6 [$ u6 ?$ N' s
else
) ^% [5 \: V# X2 Y& k0 zprintf("The character wasn't found\n");
! V3 [( D6 n% g5 @return 0; * {6 e( G! Y4 K" D! b E/ H
}
- R& q$ j6 `* G$ l. E2 W9 ]</FONT>
, c5 Z0 ]5 A9 R+ [; v% N# h! ^7 X</P>
% E ?) s! `# [< ><FONT color=#ff0000>函数名: malloc</FONT> ( m0 w8 u! T8 Q) w3 |( }4 w% i' [
功 能: 内存分配函数
5 O- }7 P4 ?) N$ s用 法: void *malloc(unsigned size);
% x3 x9 c4 b; g程序例: </P>7 L) c: _& J8 i: |
< ><FONT color=#0000ff>#include <STDIO.H>6 v T1 j2 R+ X. |, H
#include <STRING.H> w( Z$ X1 l) p& }
#include <ALLOC.H>
! W) J" B% Y0 E#include < ROCESS.H></FONT></P>! @/ U: ?3 v2 m6 U
< ><FONT color=#0000ff>int main(void)
3 d1 }( f8 [2 j' v6 S: R{
0 B1 }! N2 n, C8 M" Q* h- r. G& p6 F) zchar *str; </FONT></P>
) y% g' N! b# o! _! A) a* `< ><FONT color=#0000ff>/* allocate memory for string */ " L# u1 m$ i0 S
/* This will generate an error when compiling */ 2 F, C5 Z6 j& P* o0 r' L6 [
/* with C++, use the new operator instead. */ ; _& U7 p4 ]: n$ \4 s- E; T% D
if ((str = malloc(10)) == NULL) - L' ?: r$ M3 |
{
7 r3 ]" G5 W. P, c/ ?printf("Not enough memory to allocate buffer\n"); ; ~+ P9 B0 K8 f' V2 R
exit(1); /* terminate program if out of memory */
& ?" k- }, _; r$ W: m6 ]( S} </FONT></P># Y+ ?# y8 k* o" {# v9 S8 {# b6 x
< ><FONT color=#0000ff>/* copy "Hello" into string */ - N7 {: ?% a$ D$ u9 b4 J4 V' f
strcpy(str, "Hello"); </FONT></P>
; m! H9 n1 O' X< ><FONT color=#0000ff>/* display string */ $ |1 _$ d6 u3 {% ^ b9 g6 v
printf("String is %s\n", str); </FONT></P>
) @' b d/ h/ c" Z< ><FONT color=#0000ff>/* free memory */ 1 H% U* i0 C& W. t
free(str); </FONT></P>
5 F! A! g$ o. c1 R# R, N& A< ><FONT color=#0000ff>return 0; * S w: B# G/ o7 ~. Y( g
} + o1 |7 c3 D( I6 e/ n( |
8 j- U' b# |& X
</FONT>
4 M$ B2 o$ H6 L2 k2 _$ z</P>
, B. r! k' g" G4 p! n< ><FONT color=#ff0000>函数名: memchr</FONT> ( |1 D/ G6 H! e. O
功 能: 在数组的前n个字节中搜索字符 * r5 J' j; }9 M7 N$ b z+ v
用 法: void *memchr(void *s, char ch, unsigned n); 6 o( H, X4 B$ b) G2 a! w3 [" Y8 z5 U
程序例: </P>7 P6 c1 L+ \" W. j P0 d
< ><FONT color=#0000ff>#include <STRING.H>
5 q# P8 P$ [; E+ H3 |0 L#include <STDIO.H></FONT></P>- u% A1 ~; v2 Q' M) T* J( |
< ><FONT color=#0000ff>int main(void)
& U5 j. U( D; Y" a/ ]# F! v{ ' K4 d" ?: f* o+ B9 C8 w
char str[17];
4 n$ F4 q+ `0 S" hchar *ptr; </FONT></P>* M1 y8 w# n3 q y& u1 Y8 N1 Z' E
< ><FONT color=#0000ff>strcpy(str, "This is a string"); 1 H; g4 q5 U3 I1 u* J9 s
ptr = memchr(str, 'r', strlen(str));
' j* e2 Q6 \- Z5 E' kif (ptr)
# ]( ]) x! U8 b7 wprintf("The character 'r' is at position: %d\n", ptr - str); # ], }' u; o8 ?) ~
else
% p$ `$ g% d) H5 w2 O1 w7 Eprintf("The character was not found\n"); & j" y) X# @/ N7 |
return 0; / K8 ]6 E" x. S, g9 d' v5 u
} 0 a8 F9 W0 ]) D3 f7 U* y1 _
</FONT></P># X3 K9 t5 O- i' i* G8 y
< ><FONT color=#ff0000>函数名: memcpy </FONT>! F% k4 K- r8 i3 U
功 能: 从源source中拷贝n个字节到目标destin中
4 P) N0 a9 y) {( {; j4 S( ]用 法: void *memcpy(void *destin, void *source, unsigned n); ' V2 g/ U* J7 x( {+ X
程序例: </P>
( t' Z/ l" \% b3 M& ?< ><FONT color=#0000ff>#include <STDIO.H>
; J3 w! y, [& k$ g# a#include <STRING.H>" }' ?1 ^' {: p# P
int main(void) 5 W1 U( e) o) F9 V% `
{ , C5 A- t: [/ O& A1 }- {
char src[] = "******************************";
8 ~4 ~4 p5 y7 H" Hchar dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; 2 n: E. D v2 {( S6 H! n1 w; z
char *ptr;
( s+ D% q: ^8 R: M( V( k dprintf("destination before memcpy: %s\n", dest);
2 T1 P Y9 @4 Optr = memcpy(dest, src, strlen(src));
( L& F; ]8 h$ y9 Lif (ptr) : U% ?; e3 {1 `: N
printf("destination after memcpy: %s\n", dest); " `) [1 H: p1 [, K5 ]
else : r! j; A7 j7 q" \; Q$ r
printf("memcpy failed\n");
% W7 I( d3 C+ `, u3 H+ H6 |return 0;
E9 M; x; \3 M# \& \}
" u$ `0 t+ g$ L& z0 }& X/ u</FONT>' o l8 x5 R8 F- t8 P" i
</P>$ T0 k7 R K9 f$ a! |* M
< ><FONT color=#ff0000>函数名: memicmp </FONT>' A {: ~- P. C( q+ s
功 能: 比较两个串s1和s2的前n个字节, 忽略大小写
' y$ t. P) ~ \2 e8 z% F用 法: int memicmp(void *s1, void *s2, unsigned n);
: o4 j N. `1 B3 Z \( X( H! u程序例: </P>1 }. x0 O# p' x+ u- X% c
< ><FONT color=#0000ff>#include <STDIO.H># G# w& }- D K3 v. ~$ Z
#include <STRING.H></FONT></P>
, \3 b! K1 t: l$ m, O: K< ><FONT color=#0000ff>int main(void) + e4 q' R( J8 D0 G ~2 h
{ i1 a3 ^. d \
char *buf1 = "ABCDE123";
) V$ |" T$ d4 Dchar *buf2 = "abcde456"; 1 x4 t. T) z3 L5 {* [' C1 X
int stat;
, _' k, c$ a& K; A5 a2 i4 Nstat = memicmp(buf1, buf2, 5);
* \. D7 J `6 S8 N' pprintf("The strings to position 5 are "); ; q9 O& }) X X
if (stat) " w6 J6 F$ k4 G5 C/ b7 n/ k
printf("not "); " r$ \: }- s( K* Y0 i
printf("the same\n"); ' d# p6 V( x% g) |' [
return 0; " J' G3 h2 p6 N; _8 Z
}
4 w/ w4 `! ~& Y; w3 h; C) i</FONT>* x$ W. i( p3 S+ s' G( d. V
</P>
# o/ a( U9 U6 g. [7 F! z< ><FONT color=#ff0000>函数名: memmove </FONT>
7 @; \' L) a% a0 B功 能: 移动一块字节 $ Y& T+ W! T" Z, T3 n
用 法: void *memmove(void *destin, void *source, unsigned n); 9 n f- }2 Y0 m/ l
程序例: </P>
B! |" Q: o( C" ?<P><FONT color=#0000ff>#include <STRING.H>- k+ A# d( ^, o% z) F
#include <STDIO.H></FONT></P>
" x3 j' B; X9 c<P><FONT color=#0000ff>int main(void)
8 _" T5 Z+ N2 q- Y5 {6 R{
7 n; ~, s( e5 v9 ?char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
" V5 d4 @( E; cchar *src = "******************************";
6 |- R" r) k2 U1 C. M) @printf("destination prior to memmove: %s\n", dest); & o7 C% P" \* ?; j( b" [
memmove(dest, src, 26);
+ ]; a' r. v; [3 `* b6 zprintf("destination after memmove: %s\n", dest);
3 ~* ^/ M4 n" W7 Kreturn 0;
7 j1 L2 l7 c( u0 c" A: Y# L}
3 G( o" o$ Q2 T9 ^- \; h! b5 e% h1 M5 W# _
</FONT>
" N! L i) j) w0 n% E/ a! s</P>: X9 X! \3 u1 [5 B0 \& j
<P><FONT color=#ff0000>函数名: memset </FONT>
4 k1 U9 g' v1 P+ p功 能: 设置s中的所有字节为ch, s数组的大小由n给定
0 Q6 X4 A! x5 U& N W7 M" ~. s用 法: void *memset(void *s, char ch, unsigned n); 9 v6 y* |2 {2 y& K
程序例: </P>
$ K( E( d# ]# O<P><FONT color=#0000ff>#include <STRING.H>
9 ?+ F' v( T2 c1 H/ c z#include <STDIO.H>
; n& C+ a7 i; V6 @#include <MEM.H></FONT></P>
% z7 F# W! [7 k, s5 i<P><FONT color=#0000ff>int main(void) ' t; ~ l* V/ w% u, U4 p
{ 1 D& u% Q3 r5 S9 N4 M
char buffer[] = "Hello world\n"; </FONT></P>
$ q( U1 y' S y: ~ Q+ r3 X* e<P><FONT color=#0000ff>printf("Buffer before memset: %s\n", buffer); ) U! ?* f/ v h
memset(buffer, '*', strlen(buffer) - 1); C/ p' J! H1 d4 t
printf("Buffer after memset: %s\n", buffer);
+ R& ^- z! J6 h$ sreturn 0;
4 j; S0 G+ o! E3 X} </FONT>
- m& H$ t9 p* F& k) F) D
, t4 _) T7 i0 ]) Q" N</P>
v$ ~2 M( p+ c<P><FONT color=#ff0000>函数名: mkdir </FONT>+ o2 ?3 I! x8 F3 |( G
功 能: 建立一个目录 & B. G+ _/ G5 N
用 法: int mkdir(char *pathname); ; S, c9 E; {4 ^( t u
程序例: </P>
! m8 Z! P ?% b5 n' L# R8 ?9 K6 [<P><FONT color=#0000ff>#include <STDIO.H>
- {. y8 i% n: d( w" z9 E% A/ y#include <CONIO.H>! j/ e6 d2 i$ Z) ?% M" ]" _) l
#include <PROCESS.H>
! x; P6 d9 j( o#include <DIR.H></FONT></P>
6 e1 \8 a7 M0 m; S<P><FONT color=#0000ff>int main(void)
1 ~7 N e5 P/ j9 \; `+ w* u{
0 D6 v! M9 L7 n! H- T7 m. Kint status; </FONT></P># G% L' V8 \! ?. j% J
<P><FONT color=#0000ff>clrscr(); ( o9 \, ~3 L5 }+ v* j! | e% G
status = mkdir("asdfjklm");
. {) h% C7 q& P$ R# @) U# r( ^(!status) ? (printf("Directory created\n")) : ' w0 q( B' \9 S
(printf("Unable to create directory\n")); </FONT></P>
+ b' x3 A7 M" b2 W/ q& v+ [<P><FONT color=#0000ff>getch();
, v7 ~3 b% q* ~- l# l: J. Rsystem("dir"); ; w1 `& A7 v6 H) G4 P$ }" ^9 w1 ?
getch(); </FONT></P>% U/ l" w+ J* q2 D0 h* [
<P><FONT color=#0000ff>status = rmdir("asdfjklm");
* o; {3 y0 q9 e: F+ L(!status) ? (printf("Directory deleted\n")) : 9 B3 ]$ O) ~9 T+ R3 a
(perror("Unable to delete directory")); </FONT></P>
5 u, j& y( P1 l' G) y<P><FONT color=#0000ff>return 0; & {4 I2 s0 S+ `! N, g' m* F
} 9 U0 K3 P+ @- U, p9 B, y7 j. B
) }8 P8 f* o+ k- l7 X) w
</FONT>* e% \' e# X4 e9 c |9 V9 t- Z
</P>
- c; |4 o0 S5 y3 ~1 F7 K5 K4 A0 v$ ]<P><FONT color=#ff0000>函数名: mktemp </FONT>
; _' r% \$ Y7 B2 K# x: Y( D功 能: 建立唯一的文件名 # T; w2 ^0 E2 \* v9 k% N
用 法: char *mktemp(char *template);
9 K" A3 S9 e" Z1 b& W H程序例: </P>
5 I7 o7 f" t0 q! G. P% m<P><FONT color=#0000ff>#include <DIR.H>
w0 K5 q8 P- r- I#include <STDIO.H></FONT></P> r, m/ d% g8 V( q
<P><FONT color=#0000ff>int main(void) 3 U! W% Z: B! W1 c4 g4 w% H
{
# g( d* w) z) H2 j3 Z! l2 d; _( ^0 }/* fname defines the template for the 6 t1 A( i2 ^ C9 Y# C2 V) p3 i: }2 a
temporary file. */ </FONT></P>
- T6 @$ X- M( {( K<P><FONT color=#0000ff>char *fname = "TXXXXXX", *ptr; </FONT></P>9 n9 W+ l' w: O2 c3 E1 g0 W
<P><FONT color=#0000ff>ptr = mktemp(fname); 3 e% E" {* ~4 F7 k0 u {7 j- d
printf("%s\n",ptr);
$ t; v% R8 J- B! m" }( Nreturn 0; ! Q1 v" x) Z# G9 r8 U' a
}
8 s3 Y; O4 y6 x$ E2 D6 v5 E+ x</FONT>) s4 e! E/ ^5 G6 M) ? t
</P>
$ |& R/ Z0 C0 l5 _4 e7 S0 B<P><FONT color=#ff0000>函数名: MK_FP </FONT>5 N; C, |$ c3 w% B. O
功 能: 设置一个远指针 1 i: ^$ X, \ R
用 法: void far *MK_FP(unsigned seg, unsigned off);
7 [( ~3 W3 F4 I0 [+ y程序例: </P>
3 ^4 d# M) Z6 I% z* L: b<P><FONT color=#0000ff>#include <DOS.H>
9 e$ N5 L, J* c, y# k0 L; o1 F#include <GRAPHICS.H></FONT></P>1 B* e+ Y' s) s& q) Y
<P><FONT color=#0000ff>int main(void)
- G. x4 ?0 Q# \% G8 D{
' t1 }' I6 n8 [/ R* gint gd, gm, i;
5 y" Q3 J4 Y& k* P( W$ zunsigned int far *screen; </FONT></P>6 W! n l, ^ L% `; w9 C
<P><FONT color=#0000ff>detectgraph(&gd, &gm);
1 m. v2 ^" I. Lif (gd == HERCMONO) : P6 x% V) M2 l* q/ M
screen = MK_FP(0xB000, 0);
; B6 L7 n9 o( R6 r% W+ oelse - l8 v) S) I7 `. h) P/ b
screen = MK_FP(0xB800, 0);
& e4 F* A" z! w/ n$ m0 |! Afor (i=0; i<26; i++)
% g$ Y: u4 n* ~% ?+ ^5 {screen = 0x0700 + ('a' + i);
' k; V( N- I- n/ z1 u5 ?return 0;
+ T$ B4 E e& U) \} 5 }- O: a4 F. P4 i& m
</FONT>
7 G4 N' g( x1 k6 O9 e e</P>* @# D( a% c! t& Z
<P><FONT color=#ff0000>函数名: modf </FONT>7 Q6 @) e5 v' o3 M
功 能: 把数分为指数和尾数 ; A2 E0 I' o0 W, W# h
用 法: double modf(double value, double *iptr);
" c0 \. K- ]; O, ^$ w6 m) H程序例: </P>
& |5 C. G. S5 O0 m<P><FONT color=#0000ff>#include <MATH.H>
7 k, m9 a$ |1 y, Z#include <STDIO.H></FONT></P>6 e, S& Z1 |+ n! R! r$ Z0 X
<P><FONT color=#0000ff>int main(void)
1 o h$ G% y# |. _{
! @8 d3 U3 j6 R6 D1 Edouble fraction, integer;
4 s( K& W: X! Tdouble number = 100000.567; </FONT></P>
) ]. C; y$ h$ z& i- w! A' V ^<P><FONT color=#0000ff>fraction = modf(number, &integer);
& |6 L% O1 `* N* y- F o: xprintf("The whole and fractional parts of %lf are %lf and %lf\n",
" A0 l* C$ n1 i: @5 `2 I% j6 H5 hnumber, integer, fraction);
! O! }- y- o4 W' g1 n# ureturn 0; . I' f7 b8 S% @4 f
} </FONT>
: ^! L0 E! j" W. ^- E3 o5 }! A8 v/ A- f2 p3 C
</P>
+ S7 y! _9 m. E: l5 Z" P8 j9 E<P><FONT color=#ff0000>函数名: movedata </FONT>
+ _% x$ K$ Z3 U5 S" R0 u% N功 能: 拷贝字节 - Q& X) G3 T9 ~
用 法: void movedata(int segsrc, int offsrc, int segdest,
: B* T" c& `9 G7 yint offdest, unsigned numbytes); % Z6 c) U: G7 Z/ U
程序例: </P># ^* P( Z# V$ q, i M
<P><FONT color=#0000ff>#include <MEM.H></FONT></P>
: e9 L. ?1 e0 `( h# m2 g, j<P><FONT color=#0000ff>#define MONO_BASE 0xB000 </FONT></P>+ j9 ]6 C# s# m9 s7 _4 \
<P><FONT color=#0000ff>/* saves the contents of the monochrome screen in buffer */
7 l9 o* x& H1 {% N% D$ J* K6 E3 n evoid save_mono_screen(char near *buffer) . z/ f: o' R/ H
{ : W3 K+ X7 v+ e1 L2 `, o
movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2); 5 n: {# E _5 K2 u% H
} </FONT></P>
: D4 y2 |, p2 m<P><FONT color=#0000ff>int main(void) ; _, r' P! B3 [4 p* [5 J
{
0 U+ s- l$ X5 h8 r6 ]+ ochar buf[80*25*2]; 3 t3 h' [5 X' E! r
save_mono_screen(buf); ' _! q5 E# l( S- m4 ~; f, b8 ]9 L1 P
}
; E: F0 L9 q! y0 J* b4 J1 H, E</FONT>7 ~+ L6 E! x- {$ S) J
</P>
3 o* r: I* G' S1 c* g<P><FONT color=#ff0000>函数名: moverel </FONT>" q5 V3 {0 T0 d: l8 H$ L8 ]
功 能: 将当前位置(CP)移动一相对距离
- l8 r- z: A9 Z5 u6 o用 法: void far moverel(int dx, int dy); & ]/ _4 `' e- ^9 h+ m
程序例: </P>
4 x9 m, C2 I# L+ h: O<P><FONT color=#0000ff>#include <GRAPHICS.H>" P( |) T$ I) R9 C* D, q# ~. e
#include <STDLIB.H>
3 r0 L( m+ h+ n9 q8 \0 N! }#include <STDIO.H>
x7 O7 d+ t6 Q#include <CONIO.H></FONT></P>( Y' [! _0 R3 q' c: p8 Z
<P><FONT color=#0000ff>int main(void) 6 G) L/ B, r0 ]8 q% L. u0 {* E5 z) f
{
- Y) G ~& x/ Y: `2 e/* request auto detection */
/ C* ], J+ R: ]& g* zint gdriver = DETECT, gmode, errorcode; 6 x$ C3 I4 z" O2 e
char msg[80]; </FONT></P>+ K% ^) W7 e4 @- h# d
<P><FONT color=#0000ff>/* initialize graphics and local variables */ % ]! W" D' U r3 h" |5 J2 @
initgraph(&gdriver, &gmode, ""); </FONT></P>& t# Q: B9 O( I. }! D [
<P><FONT color=#0000ff>/* read result of initialization */ / }& _6 q( Z3 `' z1 K" w0 Q$ k$ W
errorcode = graphresult(); f; k2 x" C: N' b5 \
if (errorcode != grOk) /* an error occurred */
* L3 |8 E# v5 A3 F3 J5 `7 G+ L1 {9 a4 R{ * ?4 H5 m) H' ^
printf("Graphics error: %s\n", grapherrormsg(errorcode));
0 t I3 O' B, ~) Y" M$ _2 ` Kprintf("Press any key to halt:");
) X( Y" |1 w& wgetch(); . p) E ~1 s( K0 T- T# Y
exit(1); /* terminate with an error code */
5 y( Q9 Q+ ]+ F" m `} </FONT></P>
; Y c! s# h+ y+ L7 i7 L<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */
3 d" [7 C& m3 r/ N# ^& G3 y# {# Tmoveto(20, 30); </FONT></P>
$ Z* @* b+ u/ o4 {0 p<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
6 ^6 ? c/ g: V& E& n7 \$ c- `putpixel(getx(), gety(), getmaxcolor()); </FONT></P>- L6 i% q, K" ` h( h
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */
; I; G {' s9 m ]; L% Z* Ssprintf(msg, " (%d, %d)", getx(), gety());
' w) b( G7 d. E% R: Q) Q0 t$ v+ jouttextxy(20, 30, msg); </FONT></P>
) w4 W6 t& L) N+ b0 x6 _9 N8 p<P><FONT color=#0000ff>/* move to a point a relative distance */ # h) V* U& R M" C& E
/* away from the current value of C.P. */
( M/ s& X' M" [/ ?2 ~moverel(100, 100); </FONT></P>9 q8 A6 p3 L8 Q3 w) p) b5 ]
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
% O( r5 ` ^/ n' X f3 F7 M4 Tputpixel(getx(), gety(), getmaxcolor()); </FONT></P>+ g+ K) Z3 l4 v8 k- ]
<P><FONT color=#0000ff>/* create and output a message at C.P. */ . i2 t: h' G, K; T( F3 p, t9 K0 d
sprintf(msg, " (%d, %d)", getx(), gety()); # V# U5 u/ {- K4 V) e: F( {
outtext(msg); </FONT></P>: {, G6 a4 w+ [2 v1 Z
<P><FONT color=#0000ff>/* clean up */
7 H5 a2 i" w3 n# Ggetch(); ' n3 J/ p3 ?& E3 k
closegraph(); $ `8 g5 P4 n$ b1 ?- b
return 0;
9 Q' x7 U. Z; ]* d( E}
. ~9 S7 H/ U1 T. j/ G</FONT>- `& b# s: K/ H4 J! K1 Y
</P>6 t2 X( `9 Z( Z1 @( {* r+ p7 G# L
<P><FONT color=#ff0000>函数名: movetext</FONT> 5 P+ H: q) A3 z0 O/ ~
功 能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域
" {9 X: d: g9 U8 P |用 法: int movetext(int left, int top, int right, int bottom,
/ I0 I8 I; T* f/ Jint newleft, int newtop); 0 e S. D3 k2 @2 w
程序例: ) b9 @1 O) ~+ N6 p, {; v- ~% m
<FONT color=#0000ff>#include <CONIO.H>& Y' _2 ~4 ^8 G' ~- ]; J
#include <STRING.H></FONT></P>: X+ P1 ], C& Q3 ^6 o. c
<P><FONT color=#0000ff>int main(void)
( e+ I9 d' v }6 j6 C0 g{ & ^6 u% g# n1 n( D
char *str = "This is a test string"; </FONT></P>
( c: A) v& ^4 O. c' Q; w1 g! Y<P><FONT color=#0000ff>clrscr(); 8 K6 G7 e1 \: s6 h/ q, N1 d) n: z. y
cputs(str); & d! h. [# d7 ?9 A8 e! d3 G4 y
getch(); </FONT></P>
/ y+ \) b. a6 A8 C7 [<P><FONT color=#0000ff>movetext(1, 1, strlen(str), 2, 10, 10); 9 V9 B8 P2 T6 D5 x
getch(); </FONT></P>
# g# S" f/ f2 ^0 `<P><FONT color=#0000ff>return 0;
( v3 W; T5 {* H+ v/ ]}
/ q2 w9 e" M2 {
% k0 {. m4 }, r</FONT></P># P( a d( J5 H4 ?8 g: f: V, p
<P><FONT color=#ff0000>函数名: moveto</FONT>
3 H+ w4 Y6 b! D功 能: 将CP移到(x, y)
* `: j& Z. S2 |用 法: void far moveto(int x, int y);
4 y0 s% S) D6 P) c% @. D( M程序例: </P>( H& N9 n, l1 b
<P><FONT color=#0000ff>#include <GRAPHICS.H>' N+ v) D! t$ h! E i* X
#include <STDLIB.H>$ ?5 P4 t3 p/ [0 |9 W. Y
#include <STDIO.H>
8 t- T3 d0 I( n7 l$ t5 j n#include <CONIO.H></FONT></P>
1 y7 _% ?) m# Y9 V @- C3 f: t<P><FONT color=#0000ff>int main(void) # e( P+ ~3 s) h8 }2 j
{ ( m! c0 f! G9 g
/* request auto detection */ - J) d! U) d5 z( G- }& b" d$ k+ j
int gdriver = DETECT, gmode, errorcode;
- H* g! |5 W/ f1 ?" n6 Jchar msg[80]; </FONT></P>
8 L- E' w1 b8 o- y9 D2 k2 D<P><FONT color=#0000ff>/* initialize graphics and local variables */ & C- v" G3 g5 m' Z% {
initgraph(&gdriver, &gmode, ""); </FONT></P>8 x) W7 D; r' W0 [/ E/ f
<P><FONT color=#0000ff>/* read result of initialization */ # E+ p9 s7 L! H
errorcode = graphresult();
* @3 r* X! u2 M; } o5 O/ O sif (errorcode != grOk) /* an error occurred */ ) A7 ?8 t# P' F. k& o
{
, z! C5 {; j0 K. jprintf("Graphics error: %s\n", grapherrormsg(errorcode)); & g1 g1 l5 n; R4 G! G& k# D
printf("Press any key to halt:"); , s( E2 m+ l- p/ p# Z% M. f2 P
getch();
( `6 Y" K" R0 Y$ cexit(1); /* terminate with an error code */
0 ~% U# L8 N1 F1 i+ k5 k3 }; D* J} </FONT></P>, _& F2 }2 m6 R T
<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */ " y% f6 l& k; W& f
moveto(20, 30); </FONT></P>2 ]' ?6 o8 L/ X( Q7 n" m) V D [
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
( y4 t" p* w" L) H5 Z# `& kputpixel(getx(), gety(), getmaxcolor()); </FONT></P>4 f$ e# U$ E( Y) I% x z4 F
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */
& B$ `3 n" u" Y. Z! dsprintf(msg, " (%d, %d)", getx(), gety());
* K" m1 j: k2 S) G- touttextxy(20, 30, msg); </FONT></P>
' ]: l) z: @7 s% d) _. Y7 f3 ^<P><FONT color=#0000ff>/* move to (100, 100) */
0 W8 I# K2 H$ Q5 S/ f. Xmoveto(100, 100); </FONT></P> z- S+ R" W& \# x* |) x3 R0 @( ^' d
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ : x4 {' g7 W/ z+ I) A9 p5 U
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>
k: U9 l' w6 {9 o9 c+ r<P><FONT color=#0000ff>/* create and output a message at C.P. */
, ?) D0 _+ ?6 D* W1 w9 Z- }# `sprintf(msg, " (%d, %d)", getx(), gety()); 3 ~: [' b4 e2 p$ {) r, Q" B9 w
outtext(msg); </FONT></P>' g8 U" U, b2 h' c( c7 S
<P><FONT color=#0000ff>/* clean up */
, I6 M. j. c6 A5 X( [7 Igetch(); Z+ D% Y( c4 @- e3 e" C
closegraph(); & w" O. [# s! b O) }* T
return 0;
( y- p. _( i P6 ]} ) Y4 P( ^( f; k Q$ q8 f
</FONT>/ n% X: n' @( c! g+ q4 E: X. D
</P>- e7 ]6 l3 o: }# |/ P; U
<P><FONT color=#ff0000>函数名: movemem </FONT>
5 ^9 a' N$ i/ v" G- ]3 y1 C功 能: 移动一块字节
- R* ]$ r0 G, F# P用 法: void movemem(void *source, void *destin, unsigned len); & P$ P9 x8 c) h( f
程序例: </P>
- |0 u2 f' F7 x, }<P><FONT color=#0000ff>#include <MEM.H>
. G, f/ N8 `; D$ m# s7 @* {#include <ALLOC.H>4 R, T: s4 v) u; K
#include <STDIO.H>, E# j: s, I# |! f- @3 \
#include <STRING.H></FONT></P>
# L9 G; o E( u" x- D<P><FONT color=#0000ff>int main(void)
; K7 e7 ?: _' k; v4 e+ _; l% x{ " w# ~/ Y, K4 f( B- U
char *source = "Borland International"; , {9 `0 v! W9 Q- V
char *destination;
0 F- k3 f; M6 K+ X6 ?5 B$ N; zint length; </FONT></P>, b' L8 A: f. p1 O) {) H* n
<P><FONT color=#0000ff>length = strlen(source); + ]. r2 s+ ~) `! {8 F- a
destination = malloc(length + 1); . E- q& A. w8 n- [
movmem(source,destination,length); 8 | z' t0 f( W {3 D" _1 n
printf("%s\n",destination); </FONT></P>4 {3 r- r3 j: l' j& y4 ~8 G3 t
<P><FONT color=#0000ff>return 0;
% K8 d+ L4 ]* G5 C: o% f}
! w: D) g- D9 E2 n5 k
0 `; O! k0 T7 Q8 r8 D</FONT></P> e9 V; R8 g% ?) i# P9 t
<P><FONT color=#ff0000>函数名: normvideo </FONT>
7 v7 g5 Z; j9 j% D+ k功 能: 选择正常亮度字符
$ L. P2 \* Y! p4 N( R* y# V+ L用 法: void normvideo(void);
3 d N9 y& w/ K4 G程序例: </P>
* }. ^8 I, B# c5 E8 b& ?<P><FONT color=#0000ff>#include <CONIO.H></FONT></P>" f) x. x6 s* k; T' j0 {
<P><FONT color=#0000ff>int main(void) ( Q* }( t5 |7 F' P2 ~
{ : A4 Z4 W6 N: R: I- B
normvideo();
1 x2 f) R! X6 w# Icprintf("NORMAL Intensity Text\r\n"); * E, J/ t% U2 c2 d @
return 0;
8 Q& w, p1 {8 J4 k1 @! G} 4 ~" A' E" _ T% F8 g
/ _# G1 p, j$ F</FONT></P>
+ Y, M5 {+ P1 n8 j8 U1 q( h<P><FONT color=#ff0000>函数名: nosound</FONT> : j- n- l4 u& k# |$ j' P9 P
功 能: 关闭PC扬声器
3 S1 P8 n1 R! z) m用 法: void nosound(void);
4 l0 K4 x! M- N% H/ t程序例: </P>
" s* k# }4 h7 |( t! L<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. </FONT></P>
3 I% Y" T8 R8 q4 F. P2 C& j5 O0 {6 {<P><FONT color=#0000ff>True story: 7 Hz is the resonant frequency of a chicken's skull cavity.
# }* j8 r; P. H' eThis was determined empirically in Australia, where a new factory
$ A- P* H( E0 n2 \" B7 _generating 7-Hz tones was located too close to a chicken ranch: # m* _% `4 e6 P, K# h/ ]7 K' U! R
When the factory started up, all the chickens died. </FONT></P>) G. t8 w$ x) m+ x
<P><FONT color=#0000ff>Your PC may not be able to emit a 7-Hz tone.
- D ?4 O8 s6 t*/ </FONT></P>7 k, d* x. n+ V, k) @5 l6 A9 f5 T
<P><FONT color=#0000ff>int main(void) % ~! Z; o: M: u6 K: p) Q
{
; Y* T: G; M! l) D8 |sound(7);
, ~7 K; B( B4 f; }. Vdelay(10000);
5 v( w0 m" A* i6 Q# nnosound();
0 k7 F. L+ ]% h}
3 p* B! Z# G) Q</FONT></P> |
zan
|