数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 02:57
标题: 函数大全(m开头)
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(m开头)</FONT></B></FONT></P>1 I* U- M6 R: w* Q' `
< align=left><FONT color=#ff0000>main()主函数
7 m% y6 Y: g  n% j# M2 k</FONT>每一C 程序都 <B><FONT color=#000000>必须</FONT></B> 有一<FONT color=#ff0000><B>main()</B></FONT>函数, 可以根据自己的爱好把它放在程序的某
9 o- h7 z9 x+ l2 o4 m个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放 ( u! A8 B/ @7 H
在哪个地方, 以下几点说明都是适合的。 & W' G5 U$ P! G$ t( t
<FONT color=#000099>1. main() 参数 </FONT>
6 f) o, X) B1 ^- _2 O6 V% t# S在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。 / |7 W8 N) J5 F" |1 E$ ]  q
* argc: 整数, 为传给main()的命令行参数个数。 - E5 L0 `# M; S0 P
* argv: 字符串数组。
5 V3 Y4 e/ v5 q0 \1 W# t# T' S/ k$ \6 R在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0 + `4 ~0 [) O, z. z( C
以下的版本, argv[0]为空串("") 。
- }' U" b1 d: U! _  E5 Q3 e: gargv[1] 为在DOS命令行中执行程序名后的第一个字符串;
) q3 V7 a* m' largv[2] 为执行程序名后的第二个字符串; . R! Z2 P1 y! m* b
...
% f4 T- j) M% ^; D' G8 V; yargv[argc]为NULL。 3 o; [5 _) ]3 K0 `0 g+ ?/ A
*env: 安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符 9 `" c9 G# ~. M+ r3 m. F+ r1 @
串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C: + M+ R# b, n! Q: Y8 C
\TURBOC(对于PATH) 或YES(对于87)。 # N# S1 f; T5 p1 r
Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中 + d; O8 z( G( t; n; q- |
说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序
7 M5 x  T  C. d( ?& p的局部变量。
3 c' n! H1 k% B2 U. p* l' {4 |请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下
7 w$ T7 v" `' y2 ?1 h的例子:
' R$ ]( n( k5 ~<FONT color=#0000ff>main()
  X* d& ^+ |, O  `2 q6 \0 n! x3 N; amain(int argc) * b0 G! k5 I5 c
main(int argc, char *argv[]) : R& ?% p% s9 f
main(int argc, char *argv[], char *env[]) </FONT>2 T4 \! ^2 }! p, e" N' a/ F+ C
其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不 ' y$ H: B0 c6 ^( S' y
用argv[]的情况。 7 y, ?' F' w) l
以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数:
9 H0 ~4 z% B. R% q  ]* ]7 [( D1 Y<FONT color=#0000ff>/*program name EXAMPLE.EXE*/ . l+ t9 @- Q" P6 m/ Z  g% j# J
#include <STDIO.H># m' k- |1 y2 a! k# G# ~& P
#include <STDLIB.H>: t( [+ T; v$ B; Z! h. K) u/ f7 J
main(int argc, char *argv[], char *env[])
/ q8 y3 D2 i$ G8 {/ k) ?3 p{
. S, H2 M/ a! X  G; B5 C8 e, x* z, qint i;
* B/ l" i/ q4 T+ |) L: Hprintf("These are the %d command- line arguments passed to ; a. M# Z& g! I  M
main:\n\n", argc);
" F$ M. K: Q0 D; ~# M7 k' W2 ~for(i=0; i&lt;=argc; i++) 8 y& }# }- B, Y, M7 Z' d9 S
printf("argv[%d]:%s\n", i, argv);
2 Z! u; i6 t$ S& r4 v1 J! s, yprintf("\nThe environment string(s)on this system are:\n\n");
# S) c) j% d; C) P! yfor(i=0; env!=NULL; i++) / c8 ^1 K; @& c3 E7 n0 C
printf(" env[%d]:%s\n", i, env); ( p- u* B% U% ?% A( J* o
} </FONT># i, G5 l% a% q1 N0 A5 T
如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE: ! c4 k) t. m6 D6 c! m3 X
C:\example first_argument "argument with blanks" 3 4 "last but
5 S% P" w6 i2 c2 \8 g/ B- A' \. x  |one" stop! 6 m# k/ }. y' F5 }/ ^& a& M
注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument ) k) L9 L  s0 i8 E/ G( {
with blanks"和"Last but one")。
" d% P/ ]. [+ Y8 |" h0 N9 d8 x* P  v结果是这样的:
& J1 \2 {6 k) b4 u7 X/ G<FONT color=#0000ff>The value of argc is 7
! Z- e& S6 q5 g: b" {  A: c' a$ ZThese are the 7 command-linearguments passed to main:
2 g) }2 m5 i9 U* ~5 I* t+ K& x, B+ N2 Sargv[0]:C:\TURBO\EXAMPLE.EXE
( l! y$ e5 W& y' Margv[1]:first_argument ; h2 q5 X9 W* w2 g  e/ Z
argv[2]:argument with blanks
0 X2 g' f/ X3 I+ Z% xargv[3]:3 - o3 f" o" e" T: d  }5 j
argv[4]:4
) K  H* U! k, B5 D8 s, cargv[5]:last but one 2 {! C" q9 |3 l! s
argv[6]:stop!
( [* [3 u8 D+ o9 d' y9 I4 j8 s5 Nargv[7]NULL)
& O$ r* P) ]  E9 D& z7 RThe environment string(s) on this system are: : Y& T) z% `  \2 v0 B
env[0]: COMSPEC=C:\COMMAND.COM 2 _8 \& E- n! T- s7 p
env[1]: PROMPT=$P$G /*视具体设置而定*/
. b' M- \1 W! [: O, Q2 f, H3 A# \env[2]: PATH=C:\DOS;C:\TC /*视具体设置而定*/
) q' S) j8 u$ O% \</FONT>
1 h1 x/ r3 g% j  Z/ M* F# V应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包 : J' v. F" g5 D1 _2 @8 @1 f
括参数间的空格), 这是由DOS 限制的。 : a3 S8 F! X2 l( V: L
</P>
; n! ^3 b# @$ d$ E; R7 w( ?<><FONT color=#ff0000>函数名: matherr </FONT>9 g3 U. U+ ?! a* v
功 能: 用户可修改的数学错误处理程序
4 @  K& ~. p! s0 t用 法: int matherr(struct exception *e);
6 O8 R- o9 |0 `% K, W$ u+ @程序例: </P>  v3 G1 [  U8 _. l
<><FONT color=#0000ff>/* This is a user-defined matherr function that prevents
+ ?% [9 f7 d  l/ I2 f# C" w0 `any error messages from being printed. */ </FONT></P>
- `2 O% E, t  Y0 ^: n9 g<><FONT color=#0000ff>#include<MATH.H> </FONT></P>9 B  i5 K4 ~& t0 o- U
<><FONT color=#0000ff>int matherr(struct exception *a)
' @4 b2 f6 O( [/ G2 |6 Y0 H{ 4 R7 |0 L3 j, n( v
return 1;
- v( u6 d- V9 G* d}
6 ^' q+ J/ u% V% j</FONT>+ N* E  B5 J3 z: }" R

! X/ T$ D" N' t% C</P>0 \0 P' X8 ~) j8 P( }0 `* d
<><FONT color=#ff0000>函数名: memccpy </FONT>
, q8 x" |; Q6 u3 j+ `' W% @功 能: 从源source中拷贝n个字节到目标destin中 4 n) V" Q5 g% v0 ?5 c8 z8 ^
用 法: void *memccpy(void *destin, void *source, unsigned char ch, ( r5 T$ B5 q3 q; R
unsigned n); + E2 f$ L; K/ ]5 I
程序例: </P>+ Y4 b1 v% a2 P1 u8 v
<><FONT color=#0000ff>#include <STRING.H>! }. w, c8 k8 f5 @( `' K, `- [
#include <STDIO.H></FONT></P>
- u  n# o* O! w8 P1 y% \<><FONT color=#0000ff>int main(void)
8 y" D4 [4 q. `( M{
: j/ R4 @7 i4 R( F. kchar *src = "This is the source string"; ( a4 Y1 n  s: \5 y
char dest[50]; ! c9 q$ K3 h* S
char *ptr; </FONT></P>
; U8 y0 w. j6 z+ ^) {/ ]6 o<><FONT color=#0000ff>ptr = memccpy(dest, src, 'c', strlen(src)); </FONT></P>
7 f; t5 {" _8 g1 r! S- L$ w9 s<><FONT color=#0000ff>if (ptr) ' B& `9 |1 ]- T# M% c0 @' g
{ : G5 K. h: t/ t+ B
*ptr = '\0';
; m: @1 D' p; r# ^. j* w( i) \0 Eprintf("The character was found: %s\n", dest); / r2 U! I$ A/ ^8 C+ y+ q" U
}   S$ \6 R9 g& u0 u4 o' V1 K
else
# V* ]. S. a: Z8 ~6 t/ j) Hprintf("The character wasn't found\n"); 6 a! x7 A3 q* t
return 0;
) k- Y; C, x, v% m' ~$ q8 T} ! J7 W' u+ P. f- |8 S) i. g' W
</FONT>
4 n0 K& E" I* I! v& k</P>
9 p) x! b: t: g5 ^0 j5 N( M<><FONT color=#ff0000>函数名: malloc</FONT>
# I. ?6 C" \( ^; Z7 E+ b& a功 能: 内存分配函数 % l: Z! V% m- F5 I
用 法: void *malloc(unsigned size);
( ^0 j, b) ^* J程序例: </P>
  I$ w* w/ ~  T6 `<><FONT color=#0000ff>#include <STDIO.H>
& T4 P1 Y1 x. c8 v& t#include <STRING.H>
1 @  K1 k! _& U#include <ALLOC.H>" j; o: F. [' x8 a, ]
#include <ROCESS.H></FONT></P>+ t9 a! t7 C8 z8 e1 B
<><FONT color=#0000ff>int main(void)
1 _5 R# A+ ~) l" z, Q& ?{   x# N& r' \7 M4 b/ V% N* x# P
char *str; </FONT></P>
" P% h7 u! v& G6 L& {" C<><FONT color=#0000ff>/* allocate memory for string */ & T& P" N8 V! ^2 o6 u6 ]
/* This will generate an error when compiling */ ' i+ v5 x( {+ q8 `' {$ i$ z
/* with C++, use the new operator instead. */
& p, O% x- ]+ Xif ((str = malloc(10)) == NULL) 3 c4 n  i8 m0 u! ^0 t
{
4 k( c8 V3 e7 X2 H6 Aprintf("Not enough memory to allocate buffer\n"); * ~! {% {& a+ E9 v% s
exit(1); /* terminate program if out of memory */
) k) n' \) P5 x5 [* J+ P5 `} </FONT></P>- h; E; f* I1 @
<><FONT color=#0000ff>/* copy "Hello" into string */ 1 P* a" _0 g  G5 `, |5 w( J( v
strcpy(str, "Hello"); </FONT></P>
- b) [  p4 y; H: d* g<><FONT color=#0000ff>/* display string */ 2 {+ c; h2 i, H  ^9 R
printf("String is %s\n", str); </FONT></P>* Q( N; W4 x; A3 Q) i
<><FONT color=#0000ff>/* free memory */
  M6 a! t$ l# B: Dfree(str); </FONT></P>
* |& x" \  R, y<><FONT color=#0000ff>return 0;
- d/ m0 n6 B5 ?! D}
2 Y, l; u- L" }" U+ b3 h6 M2 A) y
. @' e, I3 f5 C! Z</FONT>! R4 g8 K4 L! d7 @1 j* Z) L
</P>
6 J' y- R5 l9 ^4 L/ I% `1 M<><FONT color=#ff0000>函数名: memchr</FONT> % E6 w2 T$ j/ I; Y6 \
功 能: 在数组的前n个字节中搜索字符 0 n8 f% `) R/ u9 G$ o: D# E
用 法: void *memchr(void *s, char ch, unsigned n);
; J4 M! p! _. t# `' a: G: f程序例: </P>
$ X- {9 c2 U  r$ x& g! F: g2 f<><FONT color=#0000ff>#include <STRING.H>/ G8 ]! I% T* ~8 {2 _* Y3 t  i
#include <STDIO.H></FONT></P>0 x( ?- ^8 x/ N0 Q' {( n
<><FONT color=#0000ff>int main(void) 2 t- R5 w9 L6 O) y3 s
{ ) a, r$ ~9 E9 m2 X. H& ^
char str[17];
7 c$ H; X9 ?7 vchar *ptr; </FONT></P>
, t8 S1 i6 V9 o5 K% V& G& E8 Q<><FONT color=#0000ff>strcpy(str, "This is a string");
9 M* L3 U' b% I9 Q, kptr = memchr(str, 'r', strlen(str)); / }7 G) t7 f4 d/ p4 }
if (ptr)
2 C' o' x2 g; l  ~6 D/ eprintf("The character 'r' is at position: %d\n", ptr - str); 6 T( r2 _. K. n- q! o  w+ t
else
# y. p; A( J* n3 {$ o' [printf("The character was not found\n"); / x4 c& S; w7 \/ r7 c6 Q
return 0;
% j+ _& ^/ U: O) u}
/ j/ T& m! E) g9 o- x, O. J</FONT></P>
( ~0 w$ P5 I! \" v& b<><FONT color=#ff0000>函数名: memcpy </FONT>
5 c+ M* [8 i! \: W功 能: 从源source中拷贝n个字节到目标destin中
. i+ x' B" ~; o用 法: void *memcpy(void *destin, void *source, unsigned n); $ d9 N- q1 j2 t/ L1 e$ P& q
程序例: </P>+ [" y0 |) l* o5 v. d
<><FONT color=#0000ff>#include <STDIO.H>
# P. m1 ~1 o7 @5 q#include <STRING.H>
7 k$ }% |% O1 u& |  C# N6 Eint main(void)
( ^# e) G7 d% ~& C{
& M: c$ r" M& \$ T  o5 cchar src[] = "******************************"; ( X6 [7 K4 Q9 o$ n! L: X8 o# h" p
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; 9 A" J6 S7 m2 N9 p9 t
char *ptr;   ]# o; `5 }4 ~  X1 a4 X& u8 K
printf("destination before memcpy: %s\n", dest);
0 X6 {, B% h9 {ptr = memcpy(dest, src, strlen(src));
+ q* S. m1 X7 i$ m7 J3 P7 S+ Mif (ptr)
3 K* R7 B. V; s0 P7 W, q  m, sprintf("destination after memcpy: %s\n", dest);
  V2 u( j/ Q: |1 Nelse
  e( h; d6 t/ k' }$ d  nprintf("memcpy failed\n");
4 x/ }9 b; R/ o6 o( Areturn 0; 2 G- T: N+ G/ C; J  o1 ], L8 H
}
3 u, e  P% K+ Y/ O, c</FONT>
" U& _3 _  {2 V2 \</P>
8 y/ H. y9 \5 H" C- q- W6 Q: B<><FONT color=#ff0000>函数名: memicmp </FONT>
. K! b; [/ Y* W功 能: 比较两个串s1和s2的前n个字节, 忽略大小写
) c# G6 K4 P- y, z. O用 法: int memicmp(void *s1, void *s2, unsigned n); ) d% L& v4 ?: z% V3 K
程序例: </P>- b# q) G- |3 O1 s+ Y
<><FONT color=#0000ff>#include <STDIO.H>
- C. O/ ?! x3 {, x7 @  _/ d#include <STRING.H></FONT></P>/ y+ n+ I  S* ~) b! b7 ]3 p
<><FONT color=#0000ff>int main(void)
0 }! [2 V5 }) e$ A{
- E# p& W7 F5 C- Q6 V3 G9 c, xchar *buf1 = "ABCDE123";
3 n& I, K9 p7 S$ @char *buf2 = "abcde456";
2 W% v4 U  S3 d- Y' q' Yint stat;
, b# l, o) l4 U, R7 B! u& b5 L, v- {' Cstat = memicmp(buf1, buf2, 5); 7 D2 z) p$ F9 }7 @7 P$ p
printf("The strings to position 5 are ");
  ?" b% a7 ^2 S/ ^if (stat) + G/ _) J0 y3 m  g; V4 `! Y
printf("not ");
$ U* }" ~) [7 \- ]! \: lprintf("the same\n"); , j' t  f. p) @# t
return 0;
+ b8 Q1 _/ Y5 y} , J) A/ i4 v" ^3 c4 A8 }4 O
</FONT>
% o( B& b) C0 S) L</P>/ N9 G  M$ {7 E3 U7 r
<><FONT color=#ff0000>函数名: memmove </FONT>
4 ?' ~8 H: q6 [' \6 N0 Z0 \! Z6 G功 能: 移动一块字节
6 ], s2 A. t( K% r用 法: void *memmove(void *destin, void *source, unsigned n); & }) `4 n% |2 i& K. {
程序例: </P>
5 z1 a! q1 Z  b2 U9 T+ W2 ?$ t<P><FONT color=#0000ff>#include <STRING.H>8 p6 R4 U2 i2 ^2 g* i
#include <STDIO.H></FONT></P>
! e- W# ^4 I, U) I2 T- T) @<P><FONT color=#0000ff>int main(void) : Y! S: C9 Q- G* L( F6 a
{ 3 \- `( A1 `1 n- h
char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
( k! J4 {& w& O! v( F* }* \char *src = "******************************";
$ I$ A2 ~- D% R- g4 U# Tprintf("destination prior to memmove: %s\n", dest); ) E4 }5 S' M: q, j- V$ d
memmove(dest, src, 26);
- m  U( H: d( f6 C. Bprintf("destination after memmove: %s\n", dest);
1 U' q: z; m+ |) K- O0 L0 Mreturn 0; % g" o" I# k2 y) b' [7 D3 |5 P3 d
}
( f- `  ?" z, V6 k  A3 D
& l( `* G2 ~# X  M8 u2 {) |</FONT>6 G! p! N7 e. M0 w
</P>$ h9 k* z8 [5 H1 C9 B
<P><FONT color=#ff0000>函数名: memset </FONT>. a/ q! G  |  g4 B
功 能: 设置s中的所有字节为ch, s数组的大小由n给定
5 \5 W' ^& P" p! D用 法: void *memset(void *s, char ch, unsigned n); / |# R" }+ V+ N2 e3 F9 C& X- A! W
程序例: </P>
" v+ u- p; ?6 u" a/ `: {<P><FONT color=#0000ff>#include <STRING.H>! \' g% Q- o! b
#include <STDIO.H># H/ j+ ?  k1 S1 C+ ?, w0 l
#include <MEM.H></FONT></P>8 F" T$ w* g& \4 k, D6 l5 Z' L: e9 a
<P><FONT color=#0000ff>int main(void)
- ?' p& ?. {7 |  k{
& j; F% E1 V8 q* N, lchar buffer[] = "Hello world\n"; </FONT></P>
7 t9 u. E# C7 p* n7 X3 V<P><FONT color=#0000ff>printf("Buffer before memset: %s\n", buffer);
$ @; P3 w8 S5 R% J$ g) Xmemset(buffer, '*', strlen(buffer) - 1);
" X( K% r4 {" Y0 bprintf("Buffer after memset: %s\n", buffer);
! y/ ~9 @+ i5 e* q- U# l8 Treturn 0; - |2 `8 I  i' I% \1 J5 F- z. V' ]* d2 r
} </FONT>
+ ]. |2 j' H. }9 q/ Q3 z5 |3 r$ c6 P2 i, M; i% o/ e. r8 Z
</P>
7 ?, F1 z' b4 `- ^( F3 P& `8 u2 E<P><FONT color=#ff0000>函数名: mkdir </FONT>. ^, U3 P0 x$ F; E; M- [
功 能: 建立一个目录 2 J4 w. X; s1 `  T
用 法: int mkdir(char *pathname);
4 F) _. f& t$ `8 h" ^% n程序例: </P>& ]8 _" i* `4 A1 B6 H+ d1 o$ a# ?% F. ~
<P><FONT color=#0000ff>#include <STDIO.H>6 |9 M2 \8 T9 G4 C/ d7 ?
#include <CONIO.H>
* l* p4 H& V4 q& K* [#include <PROCESS.H>
& E- i6 I$ D$ a#include <DIR.H></FONT></P># D+ Q% Y5 ^  f/ _3 f
<P><FONT color=#0000ff>int main(void)
, b% a) u( W% V{ 6 V* l( S3 x' Z+ ?; x. y
int status; </FONT></P>
! d% ?* L4 J  R8 {2 B4 n<P><FONT color=#0000ff>clrscr();
5 l/ X) v3 `7 S$ V: b0 ustatus = mkdir("asdfjklm");
1 m! B$ W4 H% f* u( K(!status) ? (printf("Directory created\n")) :
4 e0 S/ y9 T' r* K(printf("Unable to create directory\n")); </FONT></P>( K' L" J( V, V6 q; H4 ]
<P><FONT color=#0000ff>getch();
+ p; {, b" n( E6 h) M" _: _7 T+ a  ~; Esystem("dir");
! l- Z1 V) N' E5 v( d! xgetch(); </FONT></P>
3 Q% X6 M6 F; i3 E" ?. V; h: h<P><FONT color=#0000ff>status = rmdir("asdfjklm");
. j5 y" R+ R3 x5 |+ t(!status) ? (printf("Directory deleted\n")) : ; x# c# |- u8 W* D9 B9 ^3 l
(perror("Unable to delete directory")); </FONT></P>
( S, @8 k# T  ]/ x; K( g<P><FONT color=#0000ff>return 0;
0 Z' l. y; F; ~& y( L+ p$ B}
- B& d% M" S3 N& c" q+ S" k$ v( A% R, p7 u. [
</FONT>
1 \' O$ ?4 m' F# {) l3 }</P>
9 M6 f! q: e* h# G<P><FONT color=#ff0000>函数名: mktemp </FONT>2 F) m0 U# b# {. ^8 L, N/ y' A' P: X
功 能: 建立唯一的文件名   q% ?8 M0 W  m0 h# \. m
用 法: char *mktemp(char *template);
8 \/ [- I  P0 d1 ?程序例: </P>$ @3 b! o' N$ E# _
<P><FONT color=#0000ff>#include <DIR.H>
- p8 _! y9 G0 ^3 _% I- X( k#include <STDIO.H></FONT></P>
9 d: |: @) ?. D<P><FONT color=#0000ff>int main(void) - _  o4 d; o5 p( @5 D2 h8 z$ {
{
/ y: {- g  c3 K" N& |; S/* fname defines the template for the
4 ^( d0 I/ s/ Q6 M/ ]* Utemporary file. */ </FONT></P>7 ^" `: D% ]( m1 V# K
<P><FONT color=#0000ff>char *fname = "TXXXXXX", *ptr; </FONT></P>2 t/ b" \' j, y1 x& O* K
<P><FONT color=#0000ff>ptr = mktemp(fname); + o  H. K& ]' _) {3 S/ U, I
printf("%s\n",ptr); 2 c) I6 T% ]' H' I+ u7 d
return 0; # q; v- T/ l8 l. l7 {' Q  j
} - J% F. ^. c; ~, l. Q1 B
</FONT>
3 B, c" e- |" J" ^; @) s) F</P>  [  B9 c0 C8 r% r4 P( S
<P><FONT color=#ff0000>函数名: MK_FP </FONT>
( ]5 J. F2 Y5 k; _功 能: 设置一个远指针 2 g  x& o8 ?; Z9 f7 U' M
用 法: void far *MK_FP(unsigned seg, unsigned off); 1 q4 z9 z& w  F( w4 h8 r, z
程序例: </P>" W7 V* S: K7 w+ c) Z: k
<P><FONT color=#0000ff>#include <DOS.H># ?: c" r- `( P1 C  [4 Q0 x
#include <GRAPHICS.H></FONT></P>/ Q6 f& N, j1 N- h  Z
<P><FONT color=#0000ff>int main(void)
' j+ I& x7 H* B+ ]9 e{ 0 g5 I% n0 _# g, \
int gd, gm, i; 0 {# \' g  k$ j& o' |2 @$ h
unsigned int far *screen; </FONT></P>
# M! B& ^; Z/ p& M) C- X1 B$ _' |9 j<P><FONT color=#0000ff>detectgraph(&amp;gd, &amp;gm);
  t3 I& S  \) ?. z  ?7 Q) f' Kif (gd == HERCMONO)
' f  n( m3 J* j, k% E1 zscreen = MK_FP(0xB000, 0);
  t4 T/ G5 H1 k% K* C* aelse
$ b+ Z7 g1 _2 @7 \screen = MK_FP(0xB800, 0); + U4 T0 v! U, y: D
for (i=0; i&lt;26; i++) 2 p$ p  Q+ r7 L0 k7 d3 L( Y
screen = 0x0700 + ('a' + i);
7 U% |( q8 Y/ Preturn 0;
9 V+ U4 K' ]& J& P" I* V( l}
% z7 f  u1 K. A8 Y8 @" g  P' X</FONT>
0 f( W* }" ]* p6 a5 O) W* u* B, n</P>
* N4 g1 t4 ]9 d! P% p! X! U8 D- m<P><FONT color=#ff0000>函数名: modf </FONT>
/ ~6 O3 {- Q$ o) }功 能: 把数分为指数和尾数
) p8 R! B5 U& L) J8 z用 法: double modf(double value, double *iptr); 8 u) B: A8 ?- t) L
程序例: </P>0 N& A4 h: m; K- f! v8 }% C
<P><FONT color=#0000ff>#include <MATH.H>1 L1 }/ t8 B- B. H- c
#include <STDIO.H></FONT></P>
( v+ D1 W- o2 b+ b, \<P><FONT color=#0000ff>int main(void) 3 e0 x# Q: y& p/ q" e5 p. X
{ & f# f# F! X4 a5 V( W
double fraction, integer;
( _8 t- Q  S* Xdouble number = 100000.567; </FONT></P>
$ m0 q8 J$ s$ U+ {1 _$ v( F<P><FONT color=#0000ff>fraction = modf(number, &amp;integer); ) B7 s4 b- ?0 g( _. x& e
printf("The whole and fractional parts of %lf are %lf and %lf\n",
" g2 X# c  i* K2 H+ Inumber, integer, fraction); ; l3 l* f2 K$ q8 K: ~
return 0; ) z, R4 t  a! M# v- Q
} </FONT>
- Z; D5 r& F$ ~9 _# e; q1 s+ k( k4 d, T
</P>7 e1 ]% ]3 V( Y/ ~  a+ y
<P><FONT color=#ff0000>函数名: movedata </FONT>
% g, Z+ F4 N4 |* ]! n功 能: 拷贝字节 . c" {* s" _! z3 {. O
用 法: void movedata(int segsrc, int offsrc, int segdest,
4 ?. u0 C" }3 `+ M( Lint offdest, unsigned numbytes);
' t! C% c% h9 ?. L: V程序例: </P>
8 W3 y5 M/ Y! J% V/ Q<P><FONT color=#0000ff>#include <MEM.H></FONT></P>+ h% @$ p$ k8 g/ Y
<P><FONT color=#0000ff>#define MONO_BASE 0xB000 </FONT></P>7 Z- d- r2 g. j& Z1 k8 k5 T/ d
<P><FONT color=#0000ff>/* saves the contents of the monochrome screen in buffer */ 3 L9 |0 N) B. W
void save_mono_screen(char near *buffer)
0 N: C0 Q7 V* A4 R6 c  y6 i' V{ + Y) Q/ h8 ]: L' b) P0 Y' _
movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2); . m) G0 l- i" }  C( y
} </FONT></P>2 a0 S" }% h* W, l* m8 B
<P><FONT color=#0000ff>int main(void)
# Z% ~, F) w( u* |{
8 v; k+ R/ ^, v$ d& X' Nchar buf[80*25*2]; # I; G8 G- o( l4 `
save_mono_screen(buf);
1 L0 r9 Y* B! g) d$ ?8 f1 w}
9 z% P$ n) _5 M8 a+ `0 F" W</FONT>) t. J! y' P2 [  O+ }8 K  w
</P>
7 G5 M! @* w7 [# ]# Z7 p<P><FONT color=#ff0000>函数名: moverel </FONT>
9 U) e$ H0 H! |" q) o/ e功 能: 将当前位置(CP)移动一相对距离 & X- H- S6 `; q6 j0 U" U; w2 P
用 法: void far moverel(int dx, int dy); 2 z/ I9 [5 B1 G& K6 e
程序例: </P>( N; U' o  U1 `3 {$ r
<P><FONT color=#0000ff>#include <GRAPHICS.H>8 C, y- x4 j+ w7 s
#include <STDLIB.H>
9 e0 e6 I" _7 h2 G( d- x0 p% ]#include <STDIO.H>
7 J" {0 x3 `; f: m0 Q6 F1 W#include <CONIO.H></FONT></P>( j% \/ }$ ]6 u9 ~3 X
<P><FONT color=#0000ff>int main(void) 6 u( J5 r. C, S/ i% J( W' v3 C8 e
{ 1 d0 o( k2 O' `' U$ _* ^6 b
/* request auto detection */
; [. m- ^$ g4 W/ U! T. uint gdriver = DETECT, gmode, errorcode; * {9 S8 x7 l7 ^. F4 ?4 Z& s
char msg[80]; </FONT></P>
0 q) B- o: R  c<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 Q4 |1 v5 ]8 C% R: y3 j
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>7 F- \3 u" u- }) a# ^4 Z2 Y
<P><FONT color=#0000ff>/* read result of initialization */ 7 X/ G3 C+ a* D! Q/ @* g
errorcode = graphresult();
1 s! {4 S' I- q' U) Qif (errorcode != grOk) /* an error occurred */ 7 k( |% d3 q% `% q9 w
{ 7 P# N; R% y: B, z
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 2 K1 o% T& C  p, U& ]2 T* X* K! N5 f
printf("Press any key to halt:"); , R5 v, O$ t' r1 v6 [
getch();
, L1 Z  S8 W/ E7 Q* Eexit(1); /* terminate with an error code */
8 j" `5 R0 F  n9 ]) p} </FONT></P>6 @' y, v6 k" `+ t8 r/ H3 A1 s- o
<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */ + e3 v6 M0 @8 L8 M4 M9 u
moveto(20, 30); </FONT></P>
3 C5 C3 w5 G, D6 g$ m" s4 g<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
9 h2 w$ t% L6 @4 uputpixel(getx(), gety(), getmaxcolor()); </FONT></P>
4 v+ M$ N: \5 o- w; k<P><FONT color=#0000ff>/* create and output a message at (20, 30) */ & p' v; T4 ^9 l' b4 m3 M
sprintf(msg, " (%d, %d)", getx(), gety());
' l* z5 \: A0 U2 C1 fouttextxy(20, 30, msg); </FONT></P>
, s$ T, R3 n1 k  D<P><FONT color=#0000ff>/* move to a point a relative distance */ 9 h; N2 d9 p0 S# M' a  u3 M
/* away from the current value of C.P. */ 6 \3 ^! ^7 [% z
moverel(100, 100); </FONT></P>* {" M8 ^+ D0 M+ u5 Y! K
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
! \7 y, I0 Q$ s5 O! Yputpixel(getx(), gety(), getmaxcolor()); </FONT></P>
7 Q2 ~& a3 J. G, }: s<P><FONT color=#0000ff>/* create and output a message at C.P. */
( i6 H- R# \6 d5 Xsprintf(msg, " (%d, %d)", getx(), gety()); , w% k. _. |& _* ^# g1 d
outtext(msg); </FONT></P>" [/ C' x+ b; T9 C4 E# l
<P><FONT color=#0000ff>/* clean up */   e( @$ x/ P5 Q3 f+ r8 T2 N7 D
getch();
; \& {% Q6 K! Iclosegraph(); 1 j4 y1 R" s& K# j2 W, W
return 0; 5 m1 E* l( t8 W' O. S- Y. C- d' a
}
9 ^( G: g" t2 Q1 T3 H</FONT>
% m% z, ]9 n3 T) p</P>2 W- F  C  b  j5 U4 _! t; |7 w( }% U
<P><FONT color=#ff0000>函数名: movetext</FONT>
% X7 f' O2 ?; J1 F( W- N# c: v功 能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域 ) S& l- }* B4 A* c' u
用 法: int movetext(int left, int top, int right, int bottom, 6 H# P$ G$ z  ^' Q/ ^
int newleft, int newtop);
: _7 L8 X4 |, I* G6 ?! l2 z; _- `程序例: ' q+ t, ?# q' R  G- ]9 d& {
<FONT color=#0000ff>#include <CONIO.H>
9 f( K. n: o  Y" T" U1 k0 e$ r/ e#include <STRING.H></FONT></P>
7 V) ~4 j3 r9 X! o<P><FONT color=#0000ff>int main(void) ! |9 x( c9 U9 q4 ?
{
- |, t! Q. `$ T8 K  U5 lchar *str = "This is a test string"; </FONT></P>2 n( y) L; u' e8 ]/ {, n) a
<P><FONT color=#0000ff>clrscr();
. d: m' Z4 e$ d# @( I2 wcputs(str);
  i" c4 H; j9 v+ O6 Q* M: I6 xgetch(); </FONT></P>
0 \9 Z2 T3 ?# j2 ]' I# g0 O& E* y<P><FONT color=#0000ff>movetext(1, 1, strlen(str), 2, 10, 10);
( W5 K  T" L# f' v% v/ Ugetch(); </FONT></P>
+ n( I5 I& z9 }; U$ ~<P><FONT color=#0000ff>return 0; ) d/ p0 D# m7 Z) v
}
9 B+ ?1 N4 ?9 ~  e& x8 W9 _! u4 u8 ~9 Y/ _; {5 b) G& C4 o
</FONT></P>
7 _0 N8 V! |5 V" S5 M<P><FONT color=#ff0000>函数名: moveto</FONT>
) V! Y0 `" T2 q8 o- |7 i" Q功 能: 将CP移到(x, y) + F7 b* y4 O. R+ G5 Z4 z# D
用 法: void far moveto(int x, int y);
7 D( C/ Q+ M; X4 l3 j* k程序例: </P>
8 x$ k, i# z! b7 j# ?<P><FONT color=#0000ff>#include <GRAPHICS.H>
; k, G( F5 u% F; e, t" f2 m#include <STDLIB.H>
1 V) k9 R3 ^) v8 C: |+ `#include <STDIO.H>
' [8 _: f# l/ V  w1 I) L#include <CONIO.H></FONT></P>
1 M3 D  j' T3 \3 t8 t4 ~' J<P><FONT color=#0000ff>int main(void) & G' ~+ H" O' y0 m' @' R( N' O
{
  l, z8 U5 r) P/* request auto detection */ ; F( v  `+ O+ b, n% n
int gdriver = DETECT, gmode, errorcode;
0 l8 d4 s4 f  A6 {6 e  ~& s4 wchar msg[80]; </FONT></P>
) w1 r% @4 A6 t1 H, f<P><FONT color=#0000ff>/* initialize graphics and local variables */
0 \0 x3 x0 i  p* o8 Oinitgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
2 m  P. B, L) `" N# {<P><FONT color=#0000ff>/* read result of initialization */
; @! q" @8 a( a: ?4 X' b6 u# S# Herrorcode = graphresult(); 1 I. A- ?6 Q9 P& ^
if (errorcode != grOk) /* an error occurred */   I4 e% R$ e# c" |, i+ f3 T
{ % q: K* t: r& Z
printf("Graphics error: %s\n", grapherrormsg(errorcode));
: ?' M3 [3 _" S& aprintf("Press any key to halt:");
0 X" P6 X2 a' Zgetch();
5 c: c& v" G+ n+ a+ Hexit(1); /* terminate with an error code */
2 J# I& o) P$ Z" G/ Y} </FONT></P>
4 t8 a; V, G0 h<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */ / c' M8 s8 c" L1 n
moveto(20, 30); </FONT></P>+ I3 g0 O6 z  H; Y, ^' e4 c  @
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ 3 Z# I/ D* s% \; H' I
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>+ I0 c- E0 q& u$ g! c& O+ E
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */
5 J+ M: E+ U% }# r/ q  [2 h& \sprintf(msg, " (%d, %d)", getx(), gety());
3 P7 r" c% b3 M. Mouttextxy(20, 30, msg); </FONT></P>
+ }8 W1 R7 w) u7 x# @<P><FONT color=#0000ff>/* move to (100, 100) */
4 N* ]/ a, ^, c$ P$ M- Jmoveto(100, 100); </FONT></P>
& A3 G( y$ B& ~3 j<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
" c* v+ H; B( {% ~7 l' Nputpixel(getx(), gety(), getmaxcolor()); </FONT></P>
& g  S! F% E$ K  b8 I0 W, f2 r; q<P><FONT color=#0000ff>/* create and output a message at C.P. */ 9 `  o7 V3 |$ f* J. w' V
sprintf(msg, " (%d, %d)", getx(), gety()); ! k5 J& \! j1 Q0 m
outtext(msg); </FONT></P>7 U' T; M+ q' g
<P><FONT color=#0000ff>/* clean up */
1 X, x+ _! M% m! i% c( i$ Igetch(); ; D) [0 `/ z9 |( o# K
closegraph();
- e& M; ?: r( J5 D6 h+ D) [return 0; % J" O2 Q! @; t5 V* O6 [8 Y
} ! `/ m( J& N' e
</FONT>
. h* o0 y# L1 X5 S" t% ^</P>* I' s. M: S) i6 q
<P><FONT color=#ff0000>函数名: movemem </FONT>; a- ^; E1 k: D; t, i, [
功 能: 移动一块字节
) j! p* T( P1 v, L4 M3 K7 V用 法: void movemem(void *source, void *destin, unsigned len); ' ?6 h% m7 h' ^" \, X- c: R
程序例: </P>
, U1 B" L' v5 T! {4 i<P><FONT color=#0000ff>#include <MEM.H>' v& |4 t0 u; U" V
#include <ALLOC.H>- ~0 K, R7 W. e8 c  q% L
#include <STDIO.H>
6 R5 ^8 {2 D' z5 ~# |  o; E#include <STRING.H></FONT></P>+ C  z5 M3 p3 R# H* Z. }9 O
<P><FONT color=#0000ff>int main(void) 1 m$ i7 Z7 e/ S: \/ r' A
{ , O6 @+ b0 A- A9 O! y) _2 ^7 g
char *source = "Borland International"; 0 Y+ R0 w0 U$ y; N& u" T4 j) v7 S, W
char *destination; 3 s' ?. _8 J* ~
int length; </FONT></P>
" B9 S4 H$ m3 C7 ?<P><FONT color=#0000ff>length = strlen(source);
' X6 a9 |( t, J" U+ g6 Y6 ydestination = malloc(length + 1);
+ I: l& Y- x5 x# lmovmem(source,destination,length);
6 f# M% y* ?$ a0 q6 ^0 b9 Y9 rprintf("%s\n",destination); </FONT></P>$ t, x& J/ k1 y; M# {$ j
<P><FONT color=#0000ff>return 0;
4 h$ a* S* z* j' ]+ Z6 l) I6 t2 h}
: |* ]' L2 Q7 }
4 m5 J/ i) L  ^7 E</FONT></P>
/ n* O/ t/ r9 Q( |6 s<P><FONT color=#ff0000>函数名: normvideo </FONT>
$ e, _9 L6 e  f2 _$ R/ \9 R功 能: 选择正常亮度字符
6 q4 B/ ^/ J$ U+ W用 法: void normvideo(void);
, f9 l$ U2 y( \  U程序例: </P>
4 R) j7 T* P. F5 U  w( F4 {! w<P><FONT color=#0000ff>#include <CONIO.H></FONT></P>
3 d; F+ `  q! B3 b  V<P><FONT color=#0000ff>int main(void)
6 k1 p/ [$ b. U- M{ & G; w$ {! @0 K) J& q
normvideo(); 9 a& Y6 t" e0 H7 R. X1 |
cprintf("NORMAL Intensity Text\r\n");
% p3 d3 w: C  q" creturn 0;
1 K. i7 K( _+ |4 h2 M  x. d5 r}
- `5 x6 ?4 u+ y- ]( l+ o3 A. G/ u/ [2 f; z
</FONT></P>
, l) ?3 H0 p- t0 ]<P><FONT color=#ff0000>函数名: nosound</FONT>
& n$ J" D5 f0 b) F' O% X2 k3 T6 }: G功 能: 关闭PC扬声器
, x& U2 j" o* z; L  b  C0 m* L/ v$ P用 法: void nosound(void);
) @! M4 a' W5 N# q: {程序例: </P>- h/ `9 H' |" s( ?
<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. </FONT></P>: v  T1 ]+ v; T/ j  O; a2 T6 i
<P><FONT color=#0000ff>True story: 7 Hz is the resonant frequency of a chicken's skull cavity.
8 m0 R- L% b. L/ aThis was determined empirically in Australia, where a new factory 1 k) S) ~+ [6 k( ~7 E
generating 7-Hz tones was located too close to a chicken ranch: ; t# d- e: B0 t
When the factory started up, all the chickens died. </FONT></P>
; x( X* l: `5 ]<P><FONT color=#0000ff>Your PC may not be able to emit a 7-Hz tone. 2 E. H$ ]2 x0 G7 F7 C
*/ </FONT></P>* e+ S. _$ E& y" n# _
<P><FONT color=#0000ff>int main(void) ; v: F# P0 [8 ~: L8 f) Z
{ 4 [+ z+ U' ~8 ~/ C" F$ @* d) P
sound(7);
3 v( V# R  Z& h. W* Q* u- r* q2 [" vdelay(10000);
( E6 y& H9 Z- J$ O7 ^nosound(); : z  e) I* f: u! i: y: U7 w3 ^# X
}
" V2 ^- L$ H$ a) G, n$ S3 O</FONT></P>




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