- 在线时间
- 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>" i1 f8 t% V3 q& h/ c
< align=left><FONT color=#ff0000>main()主函数/ o# g$ w1 f: y
</FONT>每一C 程序都 <B><FONT color=#000000>必须</FONT></B> 有一<FONT color=#ff0000><B>main()</B></FONT>函数, 可以根据自己的爱好把它放在程序的某 % A6 P" U6 G0 ` j
个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放 % m/ E* c' |2 s/ Q
在哪个地方, 以下几点说明都是适合的。 % v( {6 Q9 d; O& N
<FONT color=#000099>1. main() 参数 </FONT>
" l3 H& t! ]+ @1 C' N) x, E在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。 e: I9 H' ^5 C
* argc: 整数, 为传给main()的命令行参数个数。 ' l" ]* Q' q' L) k
* argv: 字符串数组。
7 D% U+ P% O6 F% w0 K1 r$ h7 |2 N在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0 - g0 K5 T' d, h5 M& I* Y
以下的版本, argv[0]为空串("") 。
& A' Q, S% I9 Q5 z9 `argv[1] 为在DOS命令行中执行程序名后的第一个字符串;
) v2 H' D- e/ S# L* x j2 n# Rargv[2] 为执行程序名后的第二个字符串; 3 W2 b7 ~* }3 L$ I5 b
... 2 h# y: P& w+ |
argv[argc]为NULL。 ! S% n7 ~; k" f% S* x) _
*env: 安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符 3 _" b q. e, v# U
串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C: ( j; G& f3 O( H' `" t
\TURBOC(对于PATH) 或YES(对于87)。
6 ]3 x4 t/ g% `6 nTurbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中 3 {$ U' v" X# q `
说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序 ; |4 l( @2 [! \4 J% R
的局部变量。 . b1 @4 ~/ {' f0 s) a
请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下
' m0 P# U6 j, l% a( w* i的例子:
8 m! e5 ]# }9 i- W8 h<FONT color=#0000ff>main()
) s* f4 o+ G! I5 z( smain(int argc) / W% O& X, c0 I0 [! h# ^2 s1 j/ D$ [
main(int argc, char *argv[])
; X, q" `. o* w- Kmain(int argc, char *argv[], char *env[]) </FONT>; i- @6 F9 t4 \
其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不
. I; n: D: `- R用argv[]的情况。 & x* g1 U% k$ v" a4 S7 U0 _
以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数: + C) V( U$ S3 B4 v* a
<FONT color=#0000ff>/*program name EXAMPLE.EXE*/ 9 f; P( V! _) p
#include <STDIO.H>: A. B$ F% l3 J, S
#include <STDLIB.H>( y6 C! x5 W* Q c5 {, P+ z
main(int argc, char *argv[], char *env[])
' h, M4 P- F1 j! t2 n' X+ O{
6 z6 X K I8 }% w* Iint i;
. ?+ d; J# d! }1 w$ Aprintf("These are the %d command- line arguments passed to
' R& x- f" Y7 Y& Cmain:\n\n", argc); ) ~( U1 P* V5 P& [8 G+ J; ^% R
for(i=0; i<=argc; i++)
% W2 s* y3 }' Z" t3 J6 L" R' nprintf("argv[%d]:%s\n", i, argv);
0 i7 G7 p k/ M2 dprintf("\nThe environment string(s)on this system are:\n\n");
. S* v; y: Y+ O l4 }3 Nfor(i=0; env!=NULL; i++)
; v+ n/ ]; h/ \1 X+ `printf(" env[%d]:%s\n", i, env); . _) T# W! B) k6 `4 K Y
} </FONT>; d8 k" \% d% t c6 K7 }' b
如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE: 0 ]" \" U3 u8 e; T: O
C:\example first_argument "argument with blanks" 3 4 "last but
- r/ r. B# i# X K2 h# Vone" stop! ) G# O+ R+ j3 h" V7 f' G
注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument
1 B: i' T1 p+ _ \with blanks"和"Last but one")。 " N8 O. X( G- @, P$ c
结果是这样的: " _* q2 K# P) P5 N2 C
<FONT color=#0000ff>The value of argc is 7
2 N2 f2 \- U @3 ?6 pThese are the 7 command-linearguments passed to main: 7 d5 e# a* D( t1 ^, t8 a
argv[0]:C:\TURBO\EXAMPLE.EXE : ~: [: W# v% j7 F
argv[1]:first_argument 9 V1 A8 O+ s% [2 u
argv[2]:argument with blanks
9 S' \5 ~ O6 A8 Z9 m# Gargv[3]:3
9 ^. e1 P4 n. w' M$ P- sargv[4]:4
2 b; |' ~0 i+ sargv[5]:last but one
' z4 T+ r c* c) L) Q2 a. z/ _argv[6]:stop! ) M$ z# z+ G) J1 {, }9 `$ L
argv[7] NULL) 0 p, r7 u3 u3 F& s
The environment string(s) on this system are: ) r& d3 a. m3 l. O6 N
env[0]: COMSPEC=C:\COMMAND.COM % |2 y5 O: l) Y6 n
env[1]: PROMPT=$P$G /*视具体设置而定*/ & @. `7 x @2 D& r/ n
env[2]: PATH=C:\DOS;C:\TC /*视具体设置而定*/ . n% N3 z: V3 L2 e* L% L; \/ e
</FONT>
6 k- b+ u( I2 Z# X8 ]应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包
6 w6 Y$ b8 O. m4 g; g/ ]# j3 h) n1 [括参数间的空格), 这是由DOS 限制的。 * T. H; v9 X5 Q. @
</P>$ H" J) I" o- f: q$ r
< ><FONT color=#ff0000>函数名: matherr </FONT>
7 J! x X8 v/ M9 i7 b7 `8 o功 能: 用户可修改的数学错误处理程序 0 F9 D& b, L3 E! B. P0 }
用 法: int matherr(struct exception *e);
2 t& [& w9 x% g: q4 \程序例: </P>+ Y9 r8 a! c; l5 H- Q. D
< ><FONT color=#0000ff>/* This is a user-defined matherr function that prevents $ G! A) A9 Q& C
any error messages from being printed. */ </FONT></P>1 U& F" C9 N- p% @/ A4 S! O4 }
< ><FONT color=#0000ff>#include<MATH.H> </FONT></P>
, K0 d, C; L9 C- z$ _+ c9 n< ><FONT color=#0000ff>int matherr(struct exception *a)
: x7 C x7 b$ e+ f1 d8 |{
7 `- K7 ^3 N, v# ~! y, E/ o! n9 G% Q. W1 }return 1;
2 `: \/ G [' |# O& v. j} 6 f: W% J4 q" G" `, u
</FONT>! C7 h7 p9 X, {: H6 G; O( ]1 d1 i8 C
$ w& j' H# x+ y0 b1 V7 K& |6 A</P>% A- L7 b# I* u; I( X8 r
< ><FONT color=#ff0000>函数名: memccpy </FONT>8 k4 P6 ~2 b' Z1 P# R5 ~/ ]
功 能: 从源source中拷贝n个字节到目标destin中
9 @; U- f+ Z" D& s; Y8 f2 A用 法: void *memccpy(void *destin, void *source, unsigned char ch,
! B( {& r, d* v8 S: A" t6 Yunsigned n); 9 R" J- `# E+ m& M" z A
程序例: </P>: M6 ~$ O. K. x2 v$ J; d
< ><FONT color=#0000ff>#include <STRING.H>
, W Y- @6 I2 {3 U D. b7 e#include <STDIO.H></FONT></P>; `; U2 I# J/ @0 b9 c9 X
< ><FONT color=#0000ff>int main(void) " X* j( n$ `# Z. J; J
{
7 `& Q8 W" _ P& k' bchar *src = "This is the source string";
) f: A" b, q6 Z9 v! }char dest[50];
/ `# k. N# t) U$ Lchar *ptr; </FONT></P>" m5 U, T' g" y
< ><FONT color=#0000ff>ptr = memccpy(dest, src, 'c', strlen(src)); </FONT></P>
4 s( h. a. p9 k& m! J8 q- i< ><FONT color=#0000ff>if (ptr) : {3 a# N* Y/ T1 }. q0 E
{ % x' R& \5 L# E
*ptr = '\0'; + S. Q, L3 a8 S
printf("The character was found: %s\n", dest); * E( J8 L' R" h$ {9 y, Z
}
5 W% V# }8 s! ~6 Telse 6 [( S; T, b" @1 F @* B" g8 O3 H0 W
printf("The character wasn't found\n");
: z0 `& F8 F8 ]2 |3 _return 0; 2 [8 ?( x4 i$ R5 G! O3 |+ i6 w/ M
}
, @( s4 L9 @9 g/ ]) ?7 `</FONT>3 S) x* y( w- p# w( D# |- K
</P>
6 k) E7 H- ~$ z8 u# F4 f< ><FONT color=#ff0000>函数名: malloc</FONT>
% d2 U3 q4 \" ]2 p功 能: 内存分配函数 ; c2 E2 o% y% d/ |0 H
用 法: void *malloc(unsigned size); * y4 U& x( v% N A
程序例: </P>
# ?) S+ Y7 Y: `$ R# p< ><FONT color=#0000ff>#include <STDIO.H>
* h4 _) e. V0 j5 u$ X#include <STRING.H>
. Y$ ]* l" U4 l#include <ALLOC.H>
3 U: G+ z$ w0 h: B" f#include < ROCESS.H></FONT></P>; j, r% |1 C- i* g ~/ a$ C/ F
< ><FONT color=#0000ff>int main(void)
: b: J/ c( w6 P7 U. Y{
8 _8 ^: i$ l' h/ V Zchar *str; </FONT></P>
) g' W/ h! z: h3 L< ><FONT color=#0000ff>/* allocate memory for string */
4 A% y, V1 W1 U! p0 d/* This will generate an error when compiling */
! {8 T3 j" m! d3 x! U2 h/* with C++, use the new operator instead. */ / H; e: j8 o) [8 ^+ ~
if ((str = malloc(10)) == NULL) ( `# v$ C/ I d) b
{ ! N& ^& R# P6 J
printf("Not enough memory to allocate buffer\n");
5 N' [8 k4 Z5 A8 Nexit(1); /* terminate program if out of memory */
0 n! d8 R; y9 z( I} </FONT></P>: F" z5 b& o& \& S9 L
< ><FONT color=#0000ff>/* copy "Hello" into string */
6 q+ K0 a% N; o% c, d# Cstrcpy(str, "Hello"); </FONT></P>
# d- X& ~- D+ u+ k0 m: v: I" b* ?9 ^/ a< ><FONT color=#0000ff>/* display string */ 1 w$ ] `( d7 T. `/ q5 V! g! [
printf("String is %s\n", str); </FONT></P>
% ~+ N _( z8 ?< ><FONT color=#0000ff>/* free memory */
5 J8 R6 [5 i9 w; _3 _% pfree(str); </FONT></P>* s) J& @6 i3 R
< ><FONT color=#0000ff>return 0; $ N0 z1 \- U! Q( z% b6 T
}
4 B4 h- u: d1 d* ]/ w$ ]7 }, v* v* d- |6 ^5 M
</FONT>
8 h& A5 U( t' d</P>
% ~/ q( f8 N5 z0 \< ><FONT color=#ff0000>函数名: memchr</FONT> `% q, s+ H1 M) t8 v5 C2 a
功 能: 在数组的前n个字节中搜索字符
2 U8 @' X3 n! X用 法: void *memchr(void *s, char ch, unsigned n); & @8 f7 {; s" v* c S
程序例: </P>
9 B0 O9 u( N9 e* ]' ]: e" J- {< ><FONT color=#0000ff>#include <STRING.H>
( E' s( M) Q) H6 T' ?; Z#include <STDIO.H></FONT></P>
0 O: A" ] [3 n' R< ><FONT color=#0000ff>int main(void) 5 {: f8 ]# w1 b) O+ C A
{ - y: W; w: \ w
char str[17]; ! i/ m ~, i1 l; M r
char *ptr; </FONT></P>
) M# p6 [! h7 v# @( L< ><FONT color=#0000ff>strcpy(str, "This is a string");
+ }7 C+ j! g/ o: D9 @3 @; L; l" Q, {ptr = memchr(str, 'r', strlen(str));
; |+ o' c) Q, Y. _if (ptr)
n( w* |8 E, ]$ w/ qprintf("The character 'r' is at position: %d\n", ptr - str); ; a" A+ _4 f0 P9 |4 |! h
else
% B/ A3 ]6 Y, o; C* gprintf("The character was not found\n"); 5 K3 z% U- L8 U
return 0;
1 G0 N: R* m2 T+ w} , s- E' I# B$ X5 _
</FONT></P>5 q7 \' q/ P6 z8 ^6 L
< ><FONT color=#ff0000>函数名: memcpy </FONT>: k8 f$ Y1 |9 a
功 能: 从源source中拷贝n个字节到目标destin中
, O& Y; \! T0 a, z. {1 ?. Q用 法: void *memcpy(void *destin, void *source, unsigned n);
' x7 i% h9 _) k g: ]/ z1 f程序例: </P>0 v0 c5 n& Q5 P
< ><FONT color=#0000ff>#include <STDIO.H>& S3 L5 _ _5 W- f
#include <STRING.H>4 h5 x& O. ?) v7 Q9 e" b4 K0 M; E! `6 _
int main(void) 7 z( ]) Y5 h e! \/ h
{
2 N2 L7 u) f; A Wchar src[] = "******************************"; $ w9 o l Y' @4 O% |- n- Q+ O
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; & e+ b' g% X4 }) @: H
char *ptr; - d# I7 h1 K7 q) n: T
printf("destination before memcpy: %s\n", dest); : e& S7 q6 K: [) Z* H
ptr = memcpy(dest, src, strlen(src));
( h/ t. j- W$ p1 B" k4 eif (ptr) + n+ Q: u8 Q" S: i
printf("destination after memcpy: %s\n", dest);
( ~) W/ L" S/ _! A$ m# _else ) H( {3 f/ X9 k6 C- A- M- Z" ]
printf("memcpy failed\n");
/ [5 N% R% U' ~; u, Q4 H. Ireturn 0; 5 A3 u1 Y, ^' Z) ]8 g
} 6 z6 K! Z; ]. g5 B6 A- Q! Y
</FONT>6 h6 n+ B( |% j1 y: p
</P>
# J* _2 }1 z. y7 u+ b< ><FONT color=#ff0000>函数名: memicmp </FONT>. S: {# r1 H7 @
功 能: 比较两个串s1和s2的前n个字节, 忽略大小写
( T/ i( e! T; h: K* F用 法: int memicmp(void *s1, void *s2, unsigned n);
$ I' B- |2 t+ M3 D程序例: </P>, v6 B x1 K5 a( F, k! c; w
< ><FONT color=#0000ff>#include <STDIO.H>
, `8 Q& Z9 ]. N3 b; q#include <STRING.H></FONT></P>
; Q2 l, x% _; e8 o< ><FONT color=#0000ff>int main(void) & }! j- g' s# Z7 x1 w' r3 K
{ 5 |9 C* j# g% I/ b* v
char *buf1 = "ABCDE123";
% `7 I3 l7 u, ~: p) {# k( _1 H6 Gchar *buf2 = "abcde456";
; Q% M( g2 _. O. A0 r7 u/ x0 Pint stat;
3 c' x. ~3 |2 R1 H9 Ystat = memicmp(buf1, buf2, 5);
+ T, r0 V) ?: k& o+ zprintf("The strings to position 5 are "); . h9 i1 W% e1 Q
if (stat) * w6 _! _1 q* q0 A
printf("not ");
+ S+ T3 c9 m% l) xprintf("the same\n"); + X% ~4 y' }3 p! }
return 0; * F' r; ^' @& Z" s( \0 g w- ]
}
. t9 \" r# ]1 V7 q. y3 R4 X9 i: S</FONT>
2 E; m* {+ z: Y! Z</P>
# o" R" U# {% D6 O2 W0 y F- N< ><FONT color=#ff0000>函数名: memmove </FONT># s% |. C% f! M& h: m5 Q$ b, k
功 能: 移动一块字节 2 I& u0 z' }' t, P H
用 法: void *memmove(void *destin, void *source, unsigned n);
% j S2 P ?/ o程序例: </P>
4 o0 T7 r( U. p) M<P><FONT color=#0000ff>#include <STRING.H>
d8 @. D# g; ]8 g8 v#include <STDIO.H></FONT></P>
" v, v7 I" M, m6 \0 W5 F<P><FONT color=#0000ff>int main(void) 9 j- [( j1 Y2 e) x
{ 5 n, p$ C0 ~4 K: D( _
char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
3 B9 |/ G% J0 h, V& d. K8 tchar *src = "******************************"; . T! o% l. V% F# i& w$ {: O# ^( w) |9 F
printf("destination prior to memmove: %s\n", dest);
* P* }( S) s2 Bmemmove(dest, src, 26); 9 r' n6 L6 k: a, f$ I( O# k% h, t: l
printf("destination after memmove: %s\n", dest);
: X: E! e" m% X r& a9 yreturn 0;
+ g5 |! F" w" m i6 M} ! {# T. d" ^, s6 O- E. i
! K, \ q3 B5 D, U0 _0 ^& Q
</FONT>% c$ {" {& E+ T7 U6 } X' i
</P>( I) T x. S! e9 H9 O# ]5 \- V6 I% N
<P><FONT color=#ff0000>函数名: memset </FONT>
& q4 l+ g3 G1 U功 能: 设置s中的所有字节为ch, s数组的大小由n给定
4 Q& {3 g1 Z4 E4 Z T. N% Y5 g用 法: void *memset(void *s, char ch, unsigned n);
! A3 t+ L- r" R2 C2 A9 f% Z$ B9 {% r程序例: </P>
$ v0 R4 b! v' }& O( v1 K# ?<P><FONT color=#0000ff>#include <STRING.H>
- b' t: M p. u L6 P: t, K#include <STDIO.H>% O: B% c7 D5 C
#include <MEM.H></FONT></P>; D9 ]( g# a( ]
<P><FONT color=#0000ff>int main(void) 9 U8 [: Y1 [7 x- p4 D7 U3 A R
{
7 |1 p; N8 e. _0 u* G1 z0 echar buffer[] = "Hello world\n"; </FONT></P>
j) ~! h& A9 E; P" b' U<P><FONT color=#0000ff>printf("Buffer before memset: %s\n", buffer); 6 q! {% B8 j4 s: Z1 m0 n9 P3 }
memset(buffer, '*', strlen(buffer) - 1); B. ?1 L K+ Z* I: q
printf("Buffer after memset: %s\n", buffer);
$ s9 X; ]6 i" D+ yreturn 0; " G. x5 R2 o; _! }" G3 [
} </FONT>$ X5 }+ l4 h4 W- @/ U
/ F0 b" f9 ~3 v$ [. V
</P>7 D9 X H& r! q0 D5 e8 [
<P><FONT color=#ff0000>函数名: mkdir </FONT>
6 F1 s h2 |! h: G% I& X6 M功 能: 建立一个目录
( o: [. }$ f0 v+ \用 法: int mkdir(char *pathname); ; i, u9 p$ L, g$ f% ~
程序例: </P>" y3 D' M n" E
<P><FONT color=#0000ff>#include <STDIO.H>+ A9 W" |2 k8 S
#include <CONIO.H>) G+ P& U1 b7 l
#include <PROCESS.H>
! |4 g. J. F; ?, D* ^4 e5 z, C#include <DIR.H></FONT></P>! u0 Y3 f2 D4 n3 u3 u, [" o9 {
<P><FONT color=#0000ff>int main(void)
& h( e( |) y* o, |) T8 {7 J{
% y" K- M3 Z7 Q, Q% C- I; u8 n9 K ~% Qint status; </FONT></P>
# D' K2 K5 u, {8 z' W1 b& Y+ `2 \<P><FONT color=#0000ff>clrscr(); 7 T- g- e& J5 O1 f# U& ?
status = mkdir("asdfjklm"); 0 c% \! W) m) U
(!status) ? (printf("Directory created\n")) : % T+ b$ }: X: g% b, U
(printf("Unable to create directory\n")); </FONT></P>
( M1 t/ h: N! U% z3 S<P><FONT color=#0000ff>getch(); / K; T+ E% _9 q7 N- o
system("dir");
. h" b# O- w. l! }/ P& ogetch(); </FONT></P>% u) T1 u/ w+ J4 |3 {8 f' f
<P><FONT color=#0000ff>status = rmdir("asdfjklm"); & o! J8 w$ q* b: g. @
(!status) ? (printf("Directory deleted\n")) : ' r0 Q- i8 o9 `. r0 v6 I+ \
(perror("Unable to delete directory")); </FONT></P>
8 @* f2 }2 O. s2 k& I9 W<P><FONT color=#0000ff>return 0;
1 }; ^# h" ]9 `/ W}
7 H+ z. H; \8 M1 Q( r- b8 S. |/ ~, R8 @9 c8 G* G2 [/ ~+ v
</FONT>
9 t& j5 h# y- b5 u2 }, `</P>) e/ s$ e! G) v3 H7 c2 `) e
<P><FONT color=#ff0000>函数名: mktemp </FONT>
& }9 w6 B% i) ~9 X0 U功 能: 建立唯一的文件名
/ R7 N1 H6 I5 m& u8 c* g z0 b用 法: char *mktemp(char *template);
# V c. m+ N, A( l; G! X- G- n程序例: </P>
0 G3 D: ]7 S8 X+ t! \8 {<P><FONT color=#0000ff>#include <DIR.H>
4 e' g6 ~# H, ~#include <STDIO.H></FONT></P>
7 Z& Y* u9 ]0 ?& C' a& B<P><FONT color=#0000ff>int main(void) 8 t0 @- s3 e$ W5 x/ k: K( u
{ @& r: p, o9 X8 {9 ?
/* fname defines the template for the 3 n& J, W) ^' M
temporary file. */ </FONT></P>
8 Q. h q( J6 `. q, b- D<P><FONT color=#0000ff>char *fname = "TXXXXXX", *ptr; </FONT></P>; ]; o( x& M0 C% Q8 s3 e
<P><FONT color=#0000ff>ptr = mktemp(fname);
) E* H/ I+ {/ }+ A" N5 tprintf("%s\n",ptr);
' Y' n" m: R {" K! ^ m4 nreturn 0; . j. C: M. S4 t0 T' P6 Y
} 1 X Z4 U- x4 x5 b7 S
</FONT>* ~' I! M* T$ B( n' M5 a+ C
</P># T5 D4 r6 U `& x5 l1 }% K
<P><FONT color=#ff0000>函数名: MK_FP </FONT>9 w$ G1 I3 x! Z% v/ K, O
功 能: 设置一个远指针 4 F1 a, c+ b8 q4 c
用 法: void far *MK_FP(unsigned seg, unsigned off);
3 }- m7 t" {" ~5 n; o程序例: </P>/ F) X/ T6 Z# R# l/ h3 a# r2 B
<P><FONT color=#0000ff>#include <DOS.H>/ l& h. b# F4 e4 y) `
#include <GRAPHICS.H></FONT></P># F! d. n' a* n5 n0 ?% W
<P><FONT color=#0000ff>int main(void)
6 ?; K9 l8 I% |3 t{
# c) |* r# |5 u' \ Q0 sint gd, gm, i;
8 C7 \$ W1 y; t/ gunsigned int far *screen; </FONT></P>4 T) [7 d/ {2 X1 ~4 e4 e
<P><FONT color=#0000ff>detectgraph(&gd, &gm); 1 x6 V( f+ ~- p$ @' G! V
if (gd == HERCMONO) 8 O0 u# g0 w$ V. m; U8 F4 j
screen = MK_FP(0xB000, 0); 0 ~" H. K1 k3 P9 Z* n( P
else ' o3 W5 H* y* ], t: I$ f: _' Z( d
screen = MK_FP(0xB800, 0); 0 Z/ r9 b t% h+ t# Z
for (i=0; i<26; i++)
( F% r t' \ S( e# }) h- Yscreen = 0x0700 + ('a' + i); 9 v. {4 |* v: [( V$ x
return 0; - ]/ a W; h0 V; N- l: n/ @
}
9 L1 ^$ b! S* F* J6 s</FONT>& S7 `- g" I4 b9 }
</P>
; ~4 u% Q1 i4 O+ ]+ K<P><FONT color=#ff0000>函数名: modf </FONT>7 O7 V! G9 ]7 x- ]* v3 R# K
功 能: 把数分为指数和尾数
3 W# O$ p" L: ~8 M" n$ P0 F: o3 ?0 K用 法: double modf(double value, double *iptr); ! J5 F! n. M3 P8 @& p+ M3 r9 O* z
程序例: </P>7 r* a( e4 ?1 T7 w' B: x
<P><FONT color=#0000ff>#include <MATH.H>0 A8 x7 j& i' _/ D# E% s# F- }
#include <STDIO.H></FONT></P>
# ]0 V9 j0 b0 T( Z<P><FONT color=#0000ff>int main(void) 2 i" I ?3 ?1 S, O
{
3 \- p# o, h; n7 ~% e% p3 g# cdouble fraction, integer;
- p( Q1 T) K- K, \# o: E+ idouble number = 100000.567; </FONT></P>0 O }3 a' P8 T: m
<P><FONT color=#0000ff>fraction = modf(number, &integer); 1 d# F% m7 h& z& O, B% \- \
printf("The whole and fractional parts of %lf are %lf and %lf\n", . q0 g1 C& r# V7 N7 a
number, integer, fraction); / K/ f: ^ ]- K6 W B( F5 Y
return 0;
: M- t; m5 P: h; B& T} </FONT>
X) g V: T/ D# D2 V. d. T! ~5 g
& v* @' U+ b6 z</P>
7 ?5 J0 m* g7 f5 ~) T7 U# v. G; P4 B<P><FONT color=#ff0000>函数名: movedata </FONT>( K& V3 l; ~+ }! f: H! H. ?. l
功 能: 拷贝字节
# V$ C* v( i3 Y6 A用 法: void movedata(int segsrc, int offsrc, int segdest,
8 t# _# G$ p) E, C7 y8 F% z% dint offdest, unsigned numbytes);
& G, ^, t% X: `6 x' _; [& W程序例: </P>
# \5 @3 I. C9 H; e) p: C& R& Y<P><FONT color=#0000ff>#include <MEM.H></FONT></P>
$ T8 N, c" Y3 k<P><FONT color=#0000ff>#define MONO_BASE 0xB000 </FONT></P>0 o) z# b% h9 p6 z
<P><FONT color=#0000ff>/* saves the contents of the monochrome screen in buffer */
: h$ j$ l0 D. H9 @7 ?4 E: Hvoid save_mono_screen(char near *buffer) 6 T2 f2 P+ _% a7 a9 m# w3 E/ Y
{ # t D' Q \+ i- @- J3 r/ T: m% \* Q0 p
movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2); 0 F: d. r' C9 X) V4 r1 e! ?+ l
} </FONT></P>. J3 W" j J9 o
<P><FONT color=#0000ff>int main(void) 9 c& r. t2 o9 H1 G( ^
{ 3 c# p" z) @/ F( S) C$ a
char buf[80*25*2]; 9 ^: @" o$ F& _9 Z
save_mono_screen(buf); 8 w Z; l/ x- O0 P, T( A
} 8 ]& e9 ?) l" z& v6 \/ D* g; J4 U; p
</FONT>1 y$ a' V+ E" @- B4 y
</P>9 _0 G! f k. X4 [
<P><FONT color=#ff0000>函数名: moverel </FONT>1 e, C( d0 \% l* \
功 能: 将当前位置(CP)移动一相对距离 + q; P6 N3 f2 ^
用 法: void far moverel(int dx, int dy); V+ U9 |3 R- {% |
程序例: </P>6 F/ S6 Z7 j; q! ~" k9 P
<P><FONT color=#0000ff>#include <GRAPHICS.H>
# l% n' N @0 ?3 k$ z#include <STDLIB.H>
: g7 [+ \8 o% G& s! s#include <STDIO.H>/ G+ [1 v$ b' x
#include <CONIO.H></FONT></P>
( U+ Y4 O. H" K! Y( N. O7 }<P><FONT color=#0000ff>int main(void) : z+ ~" R+ e) H5 M# z* q* U
{
, ?8 y+ u4 F. Z- {/* request auto detection */ * P) K' ^% q, k9 t9 |
int gdriver = DETECT, gmode, errorcode; - K9 c; H) F$ \! o( S
char msg[80]; </FONT></P>
/ a% E( |3 g/ w% e- q4 L<P><FONT color=#0000ff>/* initialize graphics and local variables */ 8 p) r4 D! a0 O, L! c; k
initgraph(&gdriver, &gmode, ""); </FONT></P>
: a2 l$ a ~5 j7 K. C$ h. `5 l! B<P><FONT color=#0000ff>/* read result of initialization */ $ w# G1 a. R P4 P7 Z: w8 F
errorcode = graphresult(); ! v8 O8 {( P1 G$ D2 C
if (errorcode != grOk) /* an error occurred */
& X; E2 y1 y, m$ b7 J{
' J/ p. T6 p$ {& b3 M5 m* Iprintf("Graphics error: %s\n", grapherrormsg(errorcode)); h v6 M+ m6 x W, v" l/ ?0 }9 y) m
printf("Press any key to halt:");
1 F4 D7 n) C2 P2 C' W) vgetch(); 2 J8 ^9 C+ j+ ^: a4 c. v9 [* x
exit(1); /* terminate with an error code */
' r* @* e9 M+ }3 Y6 A5 w$ `- [' o+ e} </FONT></P>
. o9 i/ B% S, y" ^ B<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */
# L$ Z) y5 q3 V: r9 ]+ r! w" d# pmoveto(20, 30); </FONT></P>0 S5 W [) C3 c. h4 q, m& _
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ 6 @) e" o. I2 F# [2 N5 i
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>+ |3 D0 c6 ^7 n3 Q
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */ : }2 D1 @ `/ L$ G- j7 i6 }7 g: C
sprintf(msg, " (%d, %d)", getx(), gety());
. \, x! h9 `+ r! `* [& d9 j* douttextxy(20, 30, msg); </FONT></P>
# }- z5 b" G& Y6 K. M2 m<P><FONT color=#0000ff>/* move to a point a relative distance */
z# x& `0 a X3 n8 d1 J/* away from the current value of C.P. */
- ] z# t2 r n7 v) t* Nmoverel(100, 100); </FONT></P>1 U+ L6 @" Z8 _, D
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ 8 T6 J4 J2 w) |) h- G
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>
9 `# T+ H9 b ~0 X8 v<P><FONT color=#0000ff>/* create and output a message at C.P. */
2 Y5 r1 ?' R% |! _, j$ N# bsprintf(msg, " (%d, %d)", getx(), gety()); 0 D& m, e+ g4 l! [) S
outtext(msg); </FONT></P>2 A- R6 t* b! u
<P><FONT color=#0000ff>/* clean up */ , G) G( ^ c2 T. k9 M
getch(); ; F. v, r, s5 s8 L* [ x3 U
closegraph();
/ G0 h6 |! r1 ~+ Ereturn 0;
9 T- G4 Y a: H7 e- {2 c7 ~/ b; K: {} 0 ^( |" s. c- W' S s
</FONT>
2 x+ z, A- a3 t. m8 T</P>7 V; D6 t4 h, S; t2 Z: H( L k
<P><FONT color=#ff0000>函数名: movetext</FONT>
# t c% x1 V" X0 W6 n: C P功 能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域 * T$ g* n3 v' E9 K0 G2 L
用 法: int movetext(int left, int top, int right, int bottom, ; c8 }/ ~0 N" A1 f% ~ W5 e" u5 J
int newleft, int newtop); 4 s6 Z4 H' Z/ N6 v. d3 ~8 a4 [ o7 c
程序例:
6 x) W! m: P9 \" {* Y% T! ~' z$ \<FONT color=#0000ff>#include <CONIO.H>; ?: o% Y! S4 ?5 N3 C/ ~* X
#include <STRING.H></FONT></P>
! G# i6 F8 P' R# e) {<P><FONT color=#0000ff>int main(void) 8 g; Z# n( f( l
{ , P/ }! \2 G7 }7 N0 R! H
char *str = "This is a test string"; </FONT></P># {; A+ z. v3 @. b
<P><FONT color=#0000ff>clrscr(); , ?- K2 x8 `- {0 ^; t) G& C
cputs(str); 0 F* o) i% {+ l4 A x* f" L
getch(); </FONT></P>$ b5 R) Q: B5 ]3 h
<P><FONT color=#0000ff>movetext(1, 1, strlen(str), 2, 10, 10);
! ?, G) c. I+ _6 r0 C. P' d9 Xgetch(); </FONT></P>; c( @ p8 u' H) N% N3 @2 W
<P><FONT color=#0000ff>return 0; 2 K, f- e/ w3 s" z* e' k n; `6 ~
}
% t0 _+ P. R: l- n5 f8 e# R4 {+ z/ n' g5 o( [' c9 \, ]4 x* b& h
</FONT></P>
. h! @2 @2 D! ?<P><FONT color=#ff0000>函数名: moveto</FONT> 2 \% o; Y& E5 a3 U8 ~, r6 t
功 能: 将CP移到(x, y)
\8 w' o2 {: |8 A: o4 S% B用 法: void far moveto(int x, int y); 6 n7 \& j" o# `3 F# H
程序例: </P>
$ f1 K& _" b! n; M" W<P><FONT color=#0000ff>#include <GRAPHICS.H>
. q2 V4 S3 x! j/ Y/ a, m) Q* ?, a2 D#include <STDLIB.H>: Y* m8 F2 t- f
#include <STDIO.H>9 o9 a. W( O4 j% T$ [
#include <CONIO.H></FONT></P>7 x- Y& U8 Q/ Y/ [ Y
<P><FONT color=#0000ff>int main(void)
+ q9 l4 T8 p& C" R{ + }6 ~, ~/ n, N6 F* ?5 ?) y
/* request auto detection */
* ?6 p4 ~2 @* w) f7 ]. L9 J. V' ^( pint gdriver = DETECT, gmode, errorcode;
% ~) r# Y+ P6 u" c, P0 }char msg[80]; </FONT></P>
2 P2 ]! C: X, u4 Q<P><FONT color=#0000ff>/* initialize graphics and local variables */ . B H. v0 C. O
initgraph(&gdriver, &gmode, ""); </FONT></P>
; \% ?" u& o' v6 q3 K7 A9 k% ^<P><FONT color=#0000ff>/* read result of initialization */
- T& }3 g. N( K- Eerrorcode = graphresult(); $ `8 x) `; l+ j
if (errorcode != grOk) /* an error occurred */
6 `! M; }2 o/ E& I$ n/ O8 |% p- o# N{
5 t2 w6 x: }9 x" }: A% u5 iprintf("Graphics error: %s\n", grapherrormsg(errorcode));
$ ~) A+ K- W& D9 L) v7 r( eprintf("Press any key to halt:");
3 Y' \1 A' `- m' T/ ggetch();
+ V, \* ^, }* @# f/ p& \exit(1); /* terminate with an error code */ + [, ]# U5 B$ l! r1 S
} </FONT></P> F- d% j2 o' e& t3 D8 X# S/ z
<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */
! ?& W% v, J& l) c, I9 ?" t# |$ ymoveto(20, 30); </FONT></P>; ^( _; H1 @$ m2 [; x4 `) e
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ + k. _7 @/ D, _' o+ `0 w
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>
4 {4 ~4 W" l: U5 M<P><FONT color=#0000ff>/* create and output a message at (20, 30) */ 3 K7 M0 }2 ~, j7 w7 N& O3 C+ |
sprintf(msg, " (%d, %d)", getx(), gety()); , W2 B7 G6 u5 W# X- {
outtextxy(20, 30, msg); </FONT></P>9 B' y. V/ W2 t! K
<P><FONT color=#0000ff>/* move to (100, 100) */ * }; n5 o# s2 J! J
moveto(100, 100); </FONT></P>1 G. w- E, c }; t! [
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ + s! v5 Y/ T. S2 g0 y: L; {
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>. }0 H+ @5 |- ?, c! g: c0 c. L% k
<P><FONT color=#0000ff>/* create and output a message at C.P. */ 0 y8 m- ]7 s( `8 H4 D! N: v; O
sprintf(msg, " (%d, %d)", getx(), gety());
. u* g8 O' ~! f3 Pouttext(msg); </FONT></P> G; L' X! _% K1 j# V
<P><FONT color=#0000ff>/* clean up */ 7 H% ?) D# I7 {9 z
getch();
' \' y K. T3 {- aclosegraph(); % z+ m8 x) H5 f. }. ~1 b9 N
return 0;
$ x# B; E4 R I' a/ R}
5 A+ n% S, n/ h6 n</FONT>
/ N! A5 K- }6 G$ q; x9 N</P>
, ]8 w0 `% Y% c$ s& k) Y- ?<P><FONT color=#ff0000>函数名: movemem </FONT>+ H7 b2 r @& D$ ]: h) Z
功 能: 移动一块字节 : d% e" h3 p# q$ b
用 法: void movemem(void *source, void *destin, unsigned len); ! f! ?) b- ?0 F5 \# @! x# A7 v! L
程序例: </P>
; b; E) G5 z+ \<P><FONT color=#0000ff>#include <MEM.H>1 d A& g P6 J ~/ q7 l
#include <ALLOC.H>
1 d0 H! b7 R4 D! {! o2 _#include <STDIO.H>( R2 ]* W# {0 B" ]
#include <STRING.H></FONT></P>$ l( B, n2 A0 c1 n$ U9 F/ h
<P><FONT color=#0000ff>int main(void) ) }2 W" H9 t# P; D1 T
{ + ~) |* p, ^# a+ Y. \: F3 _
char *source = "Borland International"; ) U) A# z0 U2 e/ M, ~! V6 R
char *destination;
+ g! T# B* |; [int length; </FONT></P>
: K. a! Z" N; O! d/ B3 \ f! ~<P><FONT color=#0000ff>length = strlen(source); 3 A5 G7 k/ E( M8 m% A' G
destination = malloc(length + 1);
7 K6 V Z9 D+ c w- I: Pmovmem(source,destination,length);
2 ?, l& i6 a+ J; X" gprintf("%s\n",destination); </FONT></P>
8 H5 g3 T4 ^# R( o8 y+ d$ u4 m<P><FONT color=#0000ff>return 0; : g: _6 |" j2 c5 @* f$ F6 a
} . o' A1 }/ v( h
# ? L8 Q# a6 g. [
</FONT></P>9 L/ ^; r' |' \* d/ Q
<P><FONT color=#ff0000>函数名: normvideo </FONT>& q" R3 {6 y( g* _! ?
功 能: 选择正常亮度字符 D" X* {# x0 n
用 法: void normvideo(void);
7 l2 n/ N" y$ Z2 Y程序例: </P>/ C, j! w4 e: \) P/ Z
<P><FONT color=#0000ff>#include <CONIO.H></FONT></P>, I$ l+ U P& b* f0 {& s
<P><FONT color=#0000ff>int main(void) $ k \2 f7 r( l/ `9 N& j: m
{ . t' M0 ^( ]# ]4 z5 ^
normvideo();
8 b# L1 q) I9 P0 p S& bcprintf("NORMAL Intensity Text\r\n"); 3 O+ d( [. {( E" g) y9 k8 u, q; y
return 0;
0 X* ]+ ]4 ]) {5 Z, `0 i}
& w H! e( t w1 X- Z
9 m' S# x: C( L) I4 u6 z</FONT></P>4 F5 }* H- E, `- Z$ h% S2 @
<P><FONT color=#ff0000>函数名: nosound</FONT> 5 X3 O& ~9 b ]$ J0 q/ q" }8 [& }- Y' n
功 能: 关闭PC扬声器
' y- S% f0 }0 E4 z用 法: void nosound(void); # k [, O) ?1 H$ Y7 H; H
程序例: </P>( Y8 j( Y0 Q' n5 ~8 o1 a ~
<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. </FONT></P>
7 b* g: L( j; t: M9 u3 D<P><FONT color=#0000ff>True story: 7 Hz is the resonant frequency of a chicken's skull cavity. & a8 T* ^( C: {+ U+ q
This was determined empirically in Australia, where a new factory 4 |( j o% z: s4 Y/ l+ [: z. }
generating 7-Hz tones was located too close to a chicken ranch: 2 K; B/ r* i9 E/ O
When the factory started up, all the chickens died. </FONT></P>
) ~6 \ Q J7 @9 g& E<P><FONT color=#0000ff>Your PC may not be able to emit a 7-Hz tone.
/ M h/ B3 R2 u {+ ?8 R4 R*/ </FONT></P>
: u$ {2 H- `% h f<P><FONT color=#0000ff>int main(void) ! K/ o2 v2 }1 X4 p! h5 H) }. Y6 x
{ 0 _3 c4 _5 w, b+ T( Q
sound(7);
) X9 V' L3 Q: h3 K" t# Udelay(10000);
% _) O7 l. L# R( C F! _nosound();
; y3 p I/ c0 B( V1 }; u6 f; y}
9 q( f' U4 ^3 g% u( G! S' M- L</FONT></P> |
zan
|