数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 02:57
标题: 函数大全(m开头)
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(m开头)</FONT></B></FONT></P>
' v, }6 u, q) F# ?( J< align=left><FONT color=#ff0000>main()主函数; A% Q- _/ k9 m: F& W+ _
</FONT>每一C 程序都 <B><FONT color=#000000>必须</FONT></B> 有一<FONT color=#ff0000><B>main()</B></FONT>函数, 可以根据自己的爱好把它放在程序的某
4 l+ G1 D6 W. s! q; c: l个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放 : s' |( {4 w; O7 A$ ~  x
在哪个地方, 以下几点说明都是适合的。 # _( _5 v' C4 ?6 j4 e' X3 u
<FONT color=#000099>1. main() 参数 </FONT>
8 N" Y- O. e# l$ _在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。
8 M" P" b; Y2 m# ]* argc: 整数, 为传给main()的命令行参数个数。   s4 I; g4 f' Y* t; T+ e$ m
* argv: 字符串数组。
1 ]1 }5 {, X, h在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0
6 c0 K( e8 t/ K. o) `以下的版本, argv[0]为空串("") 。 9 ^! n2 \: K  n& Y
argv[1] 为在DOS命令行中执行程序名后的第一个字符串; ' W+ U+ D- ], w. \
argv[2] 为执行程序名后的第二个字符串; 6 }; m# J3 m6 s2 Z' e& f! t. i
...
- M5 U& H+ X2 Z+ nargv[argc]为NULL。
' v) H; t* s$ j5 g3 @*env: 安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符
3 y+ j( b5 s, n0 y$ c串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C: * C( r$ K8 ]* ?% s
\TURBOC(对于PATH) 或YES(对于87)。
; n4 J$ d, i* U6 g4 t3 g2 zTurbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中 , I$ L% d2 O- T  v
说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序 ) I- C8 N+ n3 f8 E; k! B
的局部变量。
" K: p6 ^  p# @8 G& v0 L2 O( U: L请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下 % [" [* K( b9 K. R1 H5 G
的例子:
; S$ U, @+ ]# H<FONT color=#0000ff>main() " j4 ]" K/ [1 K& \" E0 U
main(int argc) 8 b, @. i& X5 }+ {
main(int argc, char *argv[])
3 F/ D3 \, u6 e& i/ [main(int argc, char *argv[], char *env[]) </FONT>* F% L2 y6 a/ I6 e! c
其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不 8 A6 Z$ V: X1 w! x3 Q
用argv[]的情况。
# o  n9 [' {6 D5 C; ~+ s以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数: & q2 S0 m8 F2 Z5 V. L; h: o
<FONT color=#0000ff>/*program name EXAMPLE.EXE*/
! ]! c/ S  u; t! g( C1 P#include <STDIO.H>+ b/ |# k5 h* c/ U5 j
#include <STDLIB.H>6 H2 T  Y( O# [
main(int argc, char *argv[], char *env[]) 7 Y- E; E- |; D! x+ \5 y' K
{
( S6 }1 h) N5 o  Y7 Q3 Lint i; $ }; O9 v$ i" X
printf("These are the %d command- line arguments passed to 3 s( ~3 U/ d# N$ x) I
main:\n\n", argc); ( p0 k2 h! Z  F8 Q6 U
for(i=0; i&lt;=argc; i++) 4 V0 I# w  |# a. {, z4 W1 x' n
printf("argv[%d]:%s\n", i, argv); + J7 i* k$ Q0 x' B5 Z, V
printf("\nThe environment string(s)on this system are:\n\n");
7 x6 L: z) k6 ~for(i=0; env!=NULL; i++) ) j7 O: }' D. J
printf(" env[%d]:%s\n", i, env); : X) M$ p& s7 \0 M0 A! Y. c
} </FONT>
4 I: N. Y/ Y2 ~( q5 G# W如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE: / L" L3 Z4 W% N) D5 j4 `( W
C:\example first_argument "argument with blanks" 3 4 "last but 0 U2 B0 Y" b4 ~7 |
one" stop!
3 F1 ]7 C$ b( t# {注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument " |0 y1 Y+ T/ C+ l1 G
with blanks"和"Last but one")。
. _$ q2 A/ x. w8 ]: p结果是这样的:
0 Z! d; f, N0 V+ l6 P+ J7 \- p. V& T0 k<FONT color=#0000ff>The value of argc is 7 4 Q2 P1 C7 e. p) T
These are the 7 command-linearguments passed to main:
! S/ D2 G5 `$ c& y" q9 t- Sargv[0]:C:\TURBO\EXAMPLE.EXE % K+ [( C/ p$ ]( o& l) @+ I, L- |
argv[1]:first_argument ) ^) x0 d6 o$ d; `9 W) F7 {; R
argv[2]:argument with blanks   @) U; a( i* M% H
argv[3]:3 4 I3 v* J6 w* t8 L' l* ^
argv[4]:4
9 j9 _! ?9 s  x- ?3 V! V1 Vargv[5]:last but one
, q, J8 b1 u0 eargv[6]:stop!
6 z, S0 s, n( V  s5 n7 Y( zargv[7]NULL) # X! U/ U5 g* m
The environment string(s) on this system are:
( |" Q. A+ {! i0 K7 a' F  m# |env[0]: COMSPEC=C:\COMMAND.COM
# t3 |& W3 v% M; _  m1 {env[1]: PROMPT=$P$G /*视具体设置而定*/ ' {1 r: w9 W0 u
env[2]: PATH=C:\DOS;C:\TC /*视具体设置而定*/ % A( |$ q/ \' s$ O/ e3 D
</FONT>: g- y) j; o6 J. C2 ^
应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包 , ], u4 I( i2 `. t
括参数间的空格), 这是由DOS 限制的。
( {6 E, t6 D6 |( S8 U& T0 ]</P>
0 l, z4 M2 L% N/ h$ J0 O<><FONT color=#ff0000>函数名: matherr </FONT>
# T. k6 V. u4 D$ z4 C5 H# K2 Z功 能: 用户可修改的数学错误处理程序 - U6 A9 @' l4 v
用 法: int matherr(struct exception *e); 1 z1 M" f* W2 ]/ \+ T, b1 U
程序例: </P>8 H; q) K0 T6 i6 K( t+ @& v$ F
<><FONT color=#0000ff>/* This is a user-defined matherr function that prevents
4 A1 ~, _0 h! M0 b" j* R! nany error messages from being printed. */ </FONT></P>3 d, Z. y( E, R3 U8 b/ m
<><FONT color=#0000ff>#include<MATH.H> </FONT></P>
2 o+ c1 M3 i5 h6 j7 @7 L<><FONT color=#0000ff>int matherr(struct exception *a)
& F* a: }. i# z{
; ^: G, s# o" z) ?5 M8 Hreturn 1;
7 ~; l' O8 R! K}
% K8 p6 i: N1 l6 k+ ~" M+ E</FONT>
* Q) @; _1 L, C/ A, F2 ?
1 h. O  \# {6 k5 K</P>
% Q% a/ w/ x7 \$ G+ g3 N<><FONT color=#ff0000>函数名: memccpy </FONT># Q& w8 O, K% N* @
功 能: 从源source中拷贝n个字节到目标destin中   I/ B! `0 d' K! b9 d. s- o
用 法: void *memccpy(void *destin, void *source, unsigned char ch,
5 D' w1 [; V" i2 r* l% Qunsigned n); 1 e: k4 e8 d. @' h+ G/ i# V
程序例: </P>4 G' F+ `4 a3 j6 `4 k8 C" l
<><FONT color=#0000ff>#include <STRING.H>
! B8 j. G0 B1 d/ h& }  m! ^#include <STDIO.H></FONT></P>
1 `2 r- E4 o, A6 I<><FONT color=#0000ff>int main(void)
8 B0 j' q" x) U$ w& f{
& R$ ~, w# s" B, K) n1 x! y: Lchar *src = "This is the source string";
' b: I5 i0 p+ F0 O7 @4 G# Q; ochar dest[50]; 9 p" d3 E4 K8 A6 g1 U' P
char *ptr; </FONT></P>
8 Q4 U8 y' }  x( e<><FONT color=#0000ff>ptr = memccpy(dest, src, 'c', strlen(src)); </FONT></P>
: k! P* X2 N3 h  h3 B- A<><FONT color=#0000ff>if (ptr)
3 x3 i$ ~, D  j5 a8 ]{ 2 w$ l, S) x: `2 W9 {; F: A' D& a; t0 a+ I
*ptr = '\0';
2 ]( r9 P  g7 z  xprintf("The character was found: %s\n", dest);
# \# M6 J) L" ?7 W  `}
& @/ b8 R! a, G2 `else " a' C- U/ Z+ ^$ g% ]( m1 }
printf("The character wasn't found\n");
! d- x; j& V4 y" e! a- |( Breturn 0; 1 o. H) k  C4 v  d; R. c; {
}
3 `: v$ u7 L' `+ w</FONT>5 `# I# L8 G* d: b. c
</P>
6 k9 |3 A$ j& M8 W# R0 K<><FONT color=#ff0000>函数名: malloc</FONT>
$ ]7 B2 k4 U/ K7 b7 `+ Y# w功 能: 内存分配函数
  S( ]  n1 a( Y. Q用 法: void *malloc(unsigned size);
+ K( s2 c4 ?" i2 v/ \8 ~7 Z程序例: </P>
* h6 E4 W' S8 x  c+ H<><FONT color=#0000ff>#include <STDIO.H>; U( W' {' |% o0 W
#include <STRING.H>
* X; X0 M. l( W6 d  S2 X#include <ALLOC.H>
) t; e( E* u6 @+ P#include <ROCESS.H></FONT></P>
) X( U" Z6 [# E<><FONT color=#0000ff>int main(void)
2 t( ^, d1 {5 w' p5 D+ P9 ], @{
0 T7 e  R% e( ?, cchar *str; </FONT></P>6 c* r4 H! t) d6 S# d
<><FONT color=#0000ff>/* allocate memory for string */ 2 R! e2 P: E3 b& x, c% \5 v, d
/* This will generate an error when compiling */ # [( I5 Y4 l& M' q
/* with C++, use the new operator instead. */
8 y& g3 u/ X! p) y3 Yif ((str = malloc(10)) == NULL)
7 T. d2 a/ K+ m1 E{
5 J& F! `( S# sprintf("Not enough memory to allocate buffer\n"); # j+ ~8 J9 V+ p
exit(1); /* terminate program if out of memory */ , m4 b6 o9 D9 [; U+ H- w& G
} </FONT></P>0 X  ?+ ~1 q5 u4 z9 J: |
<><FONT color=#0000ff>/* copy "Hello" into string */ - t% l& N: m9 H" _) D( S/ }  G
strcpy(str, "Hello"); </FONT></P>$ J/ W7 A: Y* }: C1 q2 t
<><FONT color=#0000ff>/* display string */
/ F! [$ c% e1 z) \7 T" P. Sprintf("String is %s\n", str); </FONT></P>1 E1 J) Q: ^& p6 f
<><FONT color=#0000ff>/* free memory */ + B- @% U5 Y9 q8 l: ~# T
free(str); </FONT></P>+ w+ k/ O+ V7 R3 e& l0 p$ c. i
<><FONT color=#0000ff>return 0;
7 c: T9 ]0 v6 ~+ D7 I8 C+ A( B} 3 M6 C" Z+ Z" ^/ J1 z

5 }; G: t; R8 c8 n# ^3 o+ C% z</FONT>- \. _* l  a# e  p% s
</P>) p4 V! G7 c  t
<><FONT color=#ff0000>函数名: memchr</FONT>
1 m& A$ n5 E' D9 r# ^5 ~) T0 [功 能: 在数组的前n个字节中搜索字符 % w0 K3 F9 \9 }
用 法: void *memchr(void *s, char ch, unsigned n); " e* Y1 g; H6 G, O: ^, d1 I
程序例: </P>2 m! R* N: A% F8 q
<><FONT color=#0000ff>#include <STRING.H>
- g8 Q. Y& ]% r" q% H#include <STDIO.H></FONT></P>
. i  u2 F1 U# H: Y0 y- F( S* ?<><FONT color=#0000ff>int main(void)
( s% h8 v& a  M{ 7 D) A0 R! A/ J# I( l2 A- H
char str[17];   d$ r( k. F6 @! U6 ?/ ]0 Z
char *ptr; </FONT></P>1 S: D" ]* G  g3 J
<><FONT color=#0000ff>strcpy(str, "This is a string"); * @) u# }1 p; z2 g0 w1 d4 t
ptr = memchr(str, 'r', strlen(str)); 8 j4 o! y+ O& ]! ]7 ?% Y- A$ a
if (ptr)
  X' u( K# @$ D* Iprintf("The character 'r' is at position: %d\n", ptr - str);
$ d- \+ J6 E5 V! B0 belse
6 b6 C  L9 \" w( G8 Cprintf("The character was not found\n"); ! g3 U8 f4 U& @: w9 j
return 0; ! R5 g- Q( e1 v( e1 ]  v8 k
}
0 O* F4 t# N7 F( e+ K</FONT></P>7 X5 _8 t  }. c, _' r! J1 M
<><FONT color=#ff0000>函数名: memcpy </FONT>
* B4 r7 e$ k  R  g* G, ]2 g功 能: 从源source中拷贝n个字节到目标destin中
& _  ^% q0 b: _6 S$ q用 法: void *memcpy(void *destin, void *source, unsigned n);
6 d! e0 u' F1 L$ v5 L/ |. l* z. q5 N程序例: </P>
* {# ?& D+ h1 B* r5 A( T<><FONT color=#0000ff>#include <STDIO.H>, f* ?6 a( Z4 `& ]. X" U7 j
#include <STRING.H>) G8 @3 P) X, }/ n5 I7 Y
int main(void) 4 N9 t( n! u- e0 l" |3 B
{ 8 b' `! B4 f8 F+ r9 j
char src[] = "******************************"; 5 ^+ f! S( `% X  P
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; / e  y. A' V  F- S" b/ G, A
char *ptr;
$ W2 D/ G4 p/ D7 Y' C* Qprintf("destination before memcpy: %s\n", dest);
) i* k& t) b6 Bptr = memcpy(dest, src, strlen(src));
; M2 E% U2 q& M: {$ z9 j8 u" D+ ]& Vif (ptr)
0 B/ }( v' R! z" k% h5 ~+ Q* _0 [1 {printf("destination after memcpy: %s\n", dest); . H0 d) R* B4 r2 e
else   e6 I, ?# y: a" _4 k- ^) J
printf("memcpy failed\n");
" ]0 C, e7 o* I! }return 0;
: \1 t# c7 ?% G7 F: O8 Y/ Q5 F} / r2 w- I5 K. D, M
</FONT>
+ ^" r& l$ q3 p: g4 [6 i. |$ R7 r/ h</P>7 y' V9 E! B9 I
<><FONT color=#ff0000>函数名: memicmp </FONT>2 R1 h8 D8 U. J+ f6 E& x1 |
功 能: 比较两个串s1和s2的前n个字节, 忽略大小写
$ l5 P/ P, w6 j1 K, ~3 y) F用 法: int memicmp(void *s1, void *s2, unsigned n); # Y3 C) A9 D4 |% v7 m$ P
程序例: </P>
# c4 R, _% W; Q6 }* \6 c1 I" t2 v<><FONT color=#0000ff>#include <STDIO.H>- Y1 k. J2 ]0 u7 u9 Q
#include <STRING.H></FONT></P>
6 S) A8 H0 R$ R. H4 a4 o<><FONT color=#0000ff>int main(void)
* o( J8 e- `4 P4 X' N{ 7 x( I% x, Q& C1 a; w7 u
char *buf1 = "ABCDE123"; % F/ C# b+ z( V" u# }: M
char *buf2 = "abcde456";
& N- V) y8 g& W1 `9 B0 ~- lint stat; ! g" J9 m9 J* s- E" N4 S+ C1 k
stat = memicmp(buf1, buf2, 5); * Y8 h$ c  L3 G7 ?# M7 O" \2 z
printf("The strings to position 5 are ");
4 f# N: D7 H" Vif (stat) 7 T, Z/ N, |7 l+ q; v1 V' `: ]
printf("not "); & o5 I0 e7 k3 A) I- M1 A1 A6 `5 ]
printf("the same\n");   G* k% v0 f6 a' f0 Q& A- t1 Z
return 0;
/ ?0 g. ?" t+ M, q6 O/ F& C} * g, ^0 H0 s% X
</FONT>
1 u: i+ w" [2 K9 X- |</P>
- m, @, G# h/ g/ b% h. M' \( o<><FONT color=#ff0000>函数名: memmove </FONT>& N% J$ Y- l% I; t& ?) q7 A4 Q% a
功 能: 移动一块字节 % a3 H+ p/ F% z$ B7 W: v
用 法: void *memmove(void *destin, void *source, unsigned n);
% [( U7 m% V0 q8 C程序例: </P>
& F& `: _7 A6 ^+ P0 Q<P><FONT color=#0000ff>#include <STRING.H>
" \( `8 }0 G) S: e6 l8 D3 ]#include <STDIO.H></FONT></P>$ {( p$ V4 M" ]- P  q; T
<P><FONT color=#0000ff>int main(void) ( W+ r$ E1 m6 |( A# o4 o
{
. \$ Q& [. V) I) `char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
+ D" L2 L3 y6 x3 g# |char *src = "******************************"; * b. u- w" S  Q1 H
printf("destination prior to memmove: %s\n", dest);
, `; ^" y' y' P% F, xmemmove(dest, src, 26); ! F* q5 Q  U! R# t; B
printf("destination after memmove: %s\n", dest);
  B: X( R4 m/ c, X) u4 U8 {* ^return 0;
/ _& [' m) A( [+ r' O3 j} % c* Y; J- C  O3 Q) O5 a
! R0 j. U/ V3 O6 Q
</FONT>
% I4 v2 ?5 j6 B/ B% _& _</P>
& c! k- k: `1 h- P- K& J$ b<P><FONT color=#ff0000>函数名: memset </FONT>3 m5 g' R* j" K9 V2 @9 \% l
功 能: 设置s中的所有字节为ch, s数组的大小由n给定
8 I0 F& a9 `; a9 d# ]2 K* W用 法: void *memset(void *s, char ch, unsigned n);
1 J/ l6 o1 A& G7 X: d程序例: </P>4 G8 k$ G2 E4 E1 j1 V) e; {# q
<P><FONT color=#0000ff>#include <STRING.H># J) L! j% W2 u5 a) [1 g  I: y
#include <STDIO.H>
2 [1 q- y; \2 h' i- t' ^. i#include <MEM.H></FONT></P>
3 F5 h8 E3 C  p2 b<P><FONT color=#0000ff>int main(void) 2 a4 }! L& Q# y0 m- _
{
8 A; g$ ^4 f3 O$ tchar buffer[] = "Hello world\n"; </FONT></P>0 F# N# O5 H' k" S3 |4 G; Q* B
<P><FONT color=#0000ff>printf("Buffer before memset: %s\n", buffer);
' b, t4 X0 N% {& s0 O7 r, o- Q& o7 qmemset(buffer, '*', strlen(buffer) - 1);
" U3 P8 {( {3 K! z! z( ^printf("Buffer after memset: %s\n", buffer); 7 h( Q4 c! f  \
return 0;
1 m# I  |( S9 H  Q# J5 {} </FONT>
/ f. M/ R1 P  F8 p; y9 S
  a9 Y, z# C  w4 A8 i</P>
* d; _/ ^0 q3 |. H2 y8 o<P><FONT color=#ff0000>函数名: mkdir </FONT>
: ]) M9 B& L6 w+ X5 N" [( v  t8 f功 能: 建立一个目录
' m6 ?% f# a% c: i9 `用 法: int mkdir(char *pathname); 0 A% E% H) w2 X; V
程序例: </P>: `! U6 H! R9 d; \
<P><FONT color=#0000ff>#include <STDIO.H>
# w  X/ @9 S3 e, B1 @# L#include <CONIO.H>$ K- X3 J1 Z3 ^  E
#include <PROCESS.H>
2 H& m# r) g" d; g: W$ Q#include <DIR.H></FONT></P>
  O+ ~- q3 V" p- V<P><FONT color=#0000ff>int main(void)
3 z& p8 a7 |9 n8 A# `{ 7 X- p1 B/ |2 u7 _0 q% y
int status; </FONT></P>
/ R/ `* j. Y8 _! V- P<P><FONT color=#0000ff>clrscr(); , ]0 g4 h- K: b3 ?
status = mkdir("asdfjklm");
5 r- J! c3 A' A$ C(!status) ? (printf("Directory created\n")) :
3 H( R6 G5 E0 U6 U(printf("Unable to create directory\n")); </FONT></P>
% d0 s( f, P+ j* ?<P><FONT color=#0000ff>getch();
+ C6 p" e7 Q! Qsystem("dir");
  W9 ?4 C1 E* p" _* q  h. Cgetch(); </FONT></P>) i. e3 c2 S6 l- I8 X! h: \; |
<P><FONT color=#0000ff>status = rmdir("asdfjklm");
, \! ]: @1 Z1 n( c9 a1 m(!status) ? (printf("Directory deleted\n")) :
; X" d$ K  `+ l, L(perror("Unable to delete directory")); </FONT></P>
, A% n/ K3 s/ c8 C2 t  t- Y<P><FONT color=#0000ff>return 0;
3 \. t7 X3 w% X} : G* f( I7 l1 A9 t6 I* {2 i
! B. \3 |3 M* u/ n' f
</FONT>
+ ~" e) Z/ {: y1 v% i</P>2 X9 F1 a  i# G. x
<P><FONT color=#ff0000>函数名: mktemp </FONT>
+ k$ l4 f4 [  @/ `  [2 G" X功 能: 建立唯一的文件名 6 ]7 x, r  n! L, c
用 法: char *mktemp(char *template);
& V. t9 l  D( u程序例: </P>
3 O/ ?' B5 b7 a: ^; f<P><FONT color=#0000ff>#include <DIR.H>
) W% U' M' j5 k0 V# ^1 U#include <STDIO.H></FONT></P>/ @6 ^) p% f, B& x: Q1 V
<P><FONT color=#0000ff>int main(void)
3 U: @3 E$ o+ {3 }2 k{
  t0 D) K2 W( e3 n' q2 l/* fname defines the template for the & }& [( x9 C+ ?) j1 q( ~
temporary file. */ </FONT></P>! f* P& F$ z+ N8 x4 C
<P><FONT color=#0000ff>char *fname = "TXXXXXX", *ptr; </FONT></P>
/ h6 z& s7 F2 s0 T1 C% Q<P><FONT color=#0000ff>ptr = mktemp(fname); " b: o2 O1 [! C4 X
printf("%s\n",ptr);   c! v3 D" C% `, l: {9 f: X8 h. E; G
return 0;
/ \" b+ c  i7 j}
3 V' h4 C2 O+ }" @7 @+ B</FONT>1 a: B+ w& ^& _, P& I; d
</P>1 [8 h5 z4 M6 M+ H) o1 U
<P><FONT color=#ff0000>函数名: MK_FP </FONT>
& |/ i; Y+ n! ]$ h1 I+ M功 能: 设置一个远指针   g, K% D7 P, S6 {5 }
用 法: void far *MK_FP(unsigned seg, unsigned off); 7 t5 s1 A% F% D% G
程序例: </P>) n4 J% A5 }: E
<P><FONT color=#0000ff>#include <DOS.H>
0 Q9 J, N) y- I  k#include <GRAPHICS.H></FONT></P>
) F- z; D, u: E<P><FONT color=#0000ff>int main(void) 4 k) m! z" ^' g& j& n
{ " b! ]! Q5 Q5 ~
int gd, gm, i; # \7 x, p6 [, x" J- t, r
unsigned int far *screen; </FONT></P>
% H( b; K) g. g; {3 |7 C. ?<P><FONT color=#0000ff>detectgraph(&amp;gd, &amp;gm);   a* X. T+ A6 E2 S# o: Y% F4 q3 W
if (gd == HERCMONO)
8 w% g# m7 X2 K* w1 Bscreen = MK_FP(0xB000, 0); 3 j# e  Z' A! p5 x0 A) s! M
else
* ]9 @* X/ c5 W0 q. t9 e( z* ^! {  ascreen = MK_FP(0xB800, 0); 6 ?8 n* w8 i% `
for (i=0; i&lt;26; i++)
. u! h0 _) Q" [! Pscreen = 0x0700 + ('a' + i); + W! T' f0 C' }7 b2 Y
return 0; 1 i4 S+ u' t. B  s& c5 w1 E
}
9 [7 `: ?2 l) E1 z3 }) f3 J</FONT>
) c" S3 T7 i$ I# A  k$ K9 I* T; S</P>
3 J' {6 g& q( W- z* s' e  c  O<P><FONT color=#ff0000>函数名: modf </FONT>
7 w5 B6 i9 M: H+ O& i: Q功 能: 把数分为指数和尾数
2 \' o( J: s* C6 q4 s1 e# c用 法: double modf(double value, double *iptr);
  ?! ?9 {( g5 c( I6 m程序例: </P>$ z  i' s  H( S0 |! H% z3 f
<P><FONT color=#0000ff>#include <MATH.H>; @2 l/ x. @0 f2 j& \
#include <STDIO.H></FONT></P>7 n4 u- U7 d: {/ U/ @$ n* g; d
<P><FONT color=#0000ff>int main(void) ; H; G; M& V- m0 p4 L
{ - e; F% q  f/ x# L
double fraction, integer;
$ B* ?% I3 c# e) idouble number = 100000.567; </FONT></P>9 }, \1 w" r# y6 |
<P><FONT color=#0000ff>fraction = modf(number, &amp;integer); 8 u1 @8 `& v( W! V9 L
printf("The whole and fractional parts of %lf are %lf and %lf\n", 1 G5 D8 m$ P' v0 B: M4 m4 e9 I
number, integer, fraction);
6 M5 [7 f, f: U8 k  p  Hreturn 0; , n8 U" A! G! U" h4 m
} </FONT>
" S. d9 T( J3 N' X7 T
0 a) x! w4 h% b- E6 B1 S: G</P>
' [* M$ u# J7 I2 C9 d, b' h<P><FONT color=#ff0000>函数名: movedata </FONT>5 c, N& {, s; w  c
功 能: 拷贝字节 5 V* R% J- m+ x6 G
用 法: void movedata(int segsrc, int offsrc, int segdest, , M; U- _( c0 I3 a$ H( t
int offdest, unsigned numbytes);
" ]9 I6 ]7 i# h- f) X程序例: </P>
% z+ G' v" t) h) Q% g<P><FONT color=#0000ff>#include <MEM.H></FONT></P>
6 z$ G2 l* X6 f% C+ K9 k1 i<P><FONT color=#0000ff>#define MONO_BASE 0xB000 </FONT></P>
0 w( C( V$ U! i: ?/ p) `<P><FONT color=#0000ff>/* saves the contents of the monochrome screen in buffer */ * c$ S0 b  e3 ~2 Z
void save_mono_screen(char near *buffer) & b7 T- }4 W' B! }- I" T* k
{
' T( k% r' w9 h# wmovedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2);
6 p- P3 R% F+ R% q6 |2 K' F} </FONT></P>
7 v6 L7 ~4 L% O. h+ [  X, A<P><FONT color=#0000ff>int main(void) / N9 b( t8 \* d! u2 A* W& d0 k3 V
{ % \. ?8 \5 ~% P
char buf[80*25*2];
9 N( i8 G0 ~' f' ?- Qsave_mono_screen(buf);
* A( A" _* n! P; ^6 @3 o+ \} / n3 Z( g: B0 W6 {, `4 f  S( i4 u; }1 @
</FONT>' {7 J! O. r6 _# s1 |
</P>; X* m% d: T: W) c
<P><FONT color=#ff0000>函数名: moverel </FONT>+ X+ y9 a) u' Q
功 能: 将当前位置(CP)移动一相对距离
4 q3 m3 n+ i: z( n用 法: void far moverel(int dx, int dy); 5 D9 E3 ^& F6 ~
程序例: </P>$ z  d2 ~8 [3 D% l/ S7 K& k
<P><FONT color=#0000ff>#include <GRAPHICS.H>
/ F1 ~+ `7 o, U- w+ o% y#include <STDLIB.H>
  K- Q7 F0 X, w1 A#include <STDIO.H>7 j% H' E. a) T4 W7 k2 I- _8 W
#include <CONIO.H></FONT></P>* k% K# J# G& i- R5 [! a! o
<P><FONT color=#0000ff>int main(void)
9 g2 A2 b1 s1 e9 E* ]6 L{ & r! ?2 j( I) w, z) K% j$ Q
/* request auto detection */ , F$ }* o! @- i1 T+ Y
int gdriver = DETECT, gmode, errorcode;
8 ^4 H8 L' D0 t* P& p7 H4 X0 [char msg[80]; </FONT></P>
4 h8 E5 W% W6 D/ ^' ^1 ^<P><FONT color=#0000ff>/* initialize graphics and local variables */ . O# o3 M+ G2 d& f1 }8 \9 x
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>: e7 z: E* B; q# C: `! K: o& J
<P><FONT color=#0000ff>/* read result of initialization */
7 m4 ~* {) ^% I7 J" R4 ]1 Perrorcode = graphresult(); . N# J' v% ]/ j- z- |, k
if (errorcode != grOk) /* an error occurred */ * m  w! b/ ^# T$ N7 y6 e) ~  P
{ ) c! T/ M9 c' _- Q
printf("Graphics error: %s\n", grapherrormsg(errorcode));
; g8 `6 `4 j$ c- t+ lprintf("Press any key to halt:"); ) r& s0 e3 n$ ^5 N% |7 E: N3 y( P
getch(); - E2 O- Q" S/ f) }$ m/ g) k" L
exit(1); /* terminate with an error code */ " h# N% v$ w. z5 Q( N) `2 Z' ]
} </FONT></P>
& i- [  H( H9 g1 j2 J<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */   N! P- d7 w, S4 v5 p% t4 V
moveto(20, 30); </FONT></P>& h  R3 t8 k- g. d' B
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ 6 A( @+ G8 I3 h8 J! i5 r( r
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>1 _- d. x9 ~! i$ v- ]% y6 q
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */
1 |# [8 h- k( V* I7 \# msprintf(msg, " (%d, %d)", getx(), gety()); ) A% i+ R* t8 b) r5 c
outtextxy(20, 30, msg); </FONT></P>% Y8 D! O9 Z1 O" Z
<P><FONT color=#0000ff>/* move to a point a relative distance */ ! m3 |, o; N$ |* M
/* away from the current value of C.P. */ 2 _& B, Q  c6 }+ l
moverel(100, 100); </FONT></P>
9 Q" Y  `8 m& `  \0 `9 G$ P<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ , I0 x  p# Q% `- W8 m* `1 X% b
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>- S/ ^( J' F* \& e4 \
<P><FONT color=#0000ff>/* create and output a message at C.P. */
0 `( U) ]) T9 O5 n. Msprintf(msg, " (%d, %d)", getx(), gety()); + D. w& ]2 l% i% \
outtext(msg); </FONT></P>2 U3 X8 X8 L" U# d7 [
<P><FONT color=#0000ff>/* clean up */ # U/ W' w& Q% i5 U7 W* v
getch();   D0 C9 @8 X5 b7 s7 q
closegraph();
* d9 Q& Q1 E/ }2 Lreturn 0;
: F$ r$ m, u% }' X1 P+ C' b( U4 D/ a6 i} 3 V& M( r" x7 M8 ^4 Z$ ?; e3 R
</FONT>4 P2 a& J) \# e1 C  R, C
</P>: W9 U7 x; |. Y% f  h. t0 H# }0 P
<P><FONT color=#ff0000>函数名: movetext</FONT>
% u; q! U8 d; J功 能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域 8 X) `5 S0 E* T
用 法: int movetext(int left, int top, int right, int bottom, + B6 b2 {5 U" ^% @$ }8 @
int newleft, int newtop);
( t5 u5 D; E; j% E+ E. L' v5 L: V* b程序例:
) I2 M, `1 ^0 b  q! z8 K<FONT color=#0000ff>#include <CONIO.H>! `! v2 v# h; q$ e7 E
#include <STRING.H></FONT></P>( P0 x8 c5 I3 u. a4 I9 e% N9 ^( [9 g
<P><FONT color=#0000ff>int main(void)
& S9 D/ H: O; V6 \1 q! ?{
: u9 Y' J, \- p6 Zchar *str = "This is a test string"; </FONT></P>
& R* Q1 A8 ^9 d/ w<P><FONT color=#0000ff>clrscr(); + H' n# L: i' F
cputs(str);
# e# s9 c# ~/ _; V2 x  Z" ]) \1 S3 fgetch(); </FONT></P>
1 w. q& q  [0 {! \* _- H<P><FONT color=#0000ff>movetext(1, 1, strlen(str), 2, 10, 10);
/ B6 J4 H) Y' }" [3 c& u: |. u. d5 Cgetch(); </FONT></P>0 g9 J4 g- h" z
<P><FONT color=#0000ff>return 0;
9 Y! e% Z: x/ G1 e! k5 ~1 ~}
! S$ H. m/ Y  o! n3 C5 W
  V$ X" D( p4 y5 D</FONT></P>
6 h0 e& H; ?; M6 R3 t- E  f0 h- ^<P><FONT color=#ff0000>函数名: moveto</FONT>
/ K' Y' S0 r3 Y) b( v功 能: 将CP移到(x, y) 0 t2 s9 r. a" {/ C
用 法: void far moveto(int x, int y);
$ M% e9 e( I8 v. c程序例: </P>
; `8 W! J' K* V4 h4 v, N<P><FONT color=#0000ff>#include <GRAPHICS.H>
( o% ~) ?1 M3 ?3 R# A3 Z/ e#include <STDLIB.H>
% Q0 u9 s8 _( G1 x0 }1 F. d: ^#include <STDIO.H>* v! t; i4 d$ D4 T% l
#include <CONIO.H></FONT></P>
( X9 c( Z4 |) X' e" K: o7 F<P><FONT color=#0000ff>int main(void)
8 n0 c1 f" U5 H. ]1 j3 P% }1 r{ 7 \7 j; B* x5 _, }( {- J
/* request auto detection */
$ K9 h& Q2 \9 ?1 V) C; v% `int gdriver = DETECT, gmode, errorcode;
2 `* y5 E8 k6 L+ u$ Nchar msg[80]; </FONT></P>
$ S6 d1 L; S' @% n/ T4 }<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 u5 \' I7 C  ^" b+ n( c: `6 ^
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>' D7 r: ~0 p4 P$ ~
<P><FONT color=#0000ff>/* read result of initialization */
( `7 _# i5 @( E4 @' |& ^/ K6 herrorcode = graphresult();
2 {3 ^, M2 E; yif (errorcode != grOk) /* an error occurred */ ) `6 `* s; A% a9 H: w
{
/ A9 F# C+ V+ u& X0 Kprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 2 L$ z3 Y% B: @+ n8 C0 r
printf("Press any key to halt:");
( j6 ^: ?) k) E4 Egetch(); 1 j5 c7 X8 D! a
exit(1); /* terminate with an error code */ 4 r. W7 p# C* q& k7 E
} </FONT></P>
2 N9 H( K/ U( k# |6 h' c1 ^) w( |<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */ ( e' e. t' V8 h' }
moveto(20, 30); </FONT></P>
7 n( k/ o/ t# a4 b; ~<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
" [# Z3 ~  }1 z5 \: Gputpixel(getx(), gety(), getmaxcolor()); </FONT></P>& ~( n% ]: C2 ?
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */ , k. w' D. X  F, u$ \, D" \* e- B  e
sprintf(msg, " (%d, %d)", getx(), gety()); * v# n" V1 X+ `5 `+ r. C; A
outtextxy(20, 30, msg); </FONT></P>+ Y& L( i, K$ K- P& E( c3 n* A9 N
<P><FONT color=#0000ff>/* move to (100, 100) */ 3 p' u8 `* [; G, I  s( U& y/ U
moveto(100, 100); </FONT></P>7 L: M& u  E- u4 x: ^$ a; v/ o
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
  e1 _2 }5 i) c# |( B. s1 Tputpixel(getx(), gety(), getmaxcolor()); </FONT></P>
- l7 H0 S+ f0 J# B7 e<P><FONT color=#0000ff>/* create and output a message at C.P. */
6 [* h/ d% S6 w2 [! qsprintf(msg, " (%d, %d)", getx(), gety()); + ~1 j. R5 p1 Q- I5 e5 k! {9 Z) B8 ?
outtext(msg); </FONT></P>( r5 A2 s% C6 u- i" r
<P><FONT color=#0000ff>/* clean up */
6 s# ~( [4 Q3 ]& v. L; K6 jgetch();
( P6 x: [- N) V1 i# I8 Qclosegraph();
  m; j" A+ x, y$ breturn 0; $ u  ?/ F" {& D' a4 b
}
+ }2 m& I/ S" c" C</FONT>
5 w( v7 g% X* V</P>% |0 U0 }- S* s/ P$ B8 X
<P><FONT color=#ff0000>函数名: movemem </FONT>; F: v" B2 n( U' I/ d
功 能: 移动一块字节 7 b! p; i9 z: ], b
用 法: void movemem(void *source, void *destin, unsigned len);
' |  l& x3 M7 R2 A+ Y* s程序例: </P>
9 K: f" N, }$ S6 k<P><FONT color=#0000ff>#include <MEM.H>
3 E& m! l( T1 g7 m#include <ALLOC.H>  }' N4 |# u, i6 z
#include <STDIO.H>
6 U! F; t* m4 V/ @/ Y#include <STRING.H></FONT></P>3 R% a+ o# B' `9 {2 `' q- X
<P><FONT color=#0000ff>int main(void) 3 h0 k) p! f, G" p! w$ V) o4 U
{ 0 J: E, c3 e+ X! X3 m
char *source = "Borland International"; ! f7 X  |5 D; I' ]* X: Y
char *destination;
* h' x# |* }# v- \8 G; hint length; </FONT></P>) @# r, y0 I" D9 c: ]
<P><FONT color=#0000ff>length = strlen(source);
0 N8 t) X( }: b, e0 C1 Mdestination = malloc(length + 1);
! r" a* c  R0 s# Cmovmem(source,destination,length); 3 |, Q4 r# x8 A9 |* ?
printf("%s\n",destination); </FONT></P>
6 Q% l3 Y* ~: r$ g  q& D<P><FONT color=#0000ff>return 0; 3 k3 C3 d* `2 _; l5 F
} / L+ P' r# w1 o7 z
, x! F) m! Q% Y" C& ?
</FONT></P>* O* C7 V, y) J9 W" V2 d
<P><FONT color=#ff0000>函数名: normvideo </FONT>& F( I8 F& s( X- a  ]( W
功 能: 选择正常亮度字符
. [9 G2 H% D' R1 n8 l2 L用 法: void normvideo(void); * [. Y1 y$ w9 y& Z- Z' j& g
程序例: </P>
+ O  m# P! r6 M5 U" c<P><FONT color=#0000ff>#include <CONIO.H></FONT></P>
* }9 K+ _% q" m7 s3 f5 t  v<P><FONT color=#0000ff>int main(void) / ^6 q1 j1 z2 X  ?" W( n) m
{ + t9 Q' @# S$ R) q& ~
normvideo(); : q+ w3 j1 i( m
cprintf("NORMAL Intensity Text\r\n");
4 d) L9 V7 |3 u! I6 \% S' j- T! nreturn 0;
% j* w7 v4 t* J2 T4 B* P! Z} 1 }3 M1 u( U$ ?9 R& R
/ e& n  R( k; H2 Y; \; p/ l
</FONT></P># R# |2 [" y% w2 R. I
<P><FONT color=#ff0000>函数名: nosound</FONT> 3 y  V; F; L! \2 ^
功 能: 关闭PC扬声器 # x  t2 l  W0 ^/ P5 i: @
用 法: void nosound(void); % U% R+ R$ O; C, B$ C! o% K
程序例: </P>
( |; Y1 g9 T( K2 D8 T<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. </FONT></P>
. t3 N. b$ M+ J8 Q6 g& ]<P><FONT color=#0000ff>True story: 7 Hz is the resonant frequency of a chicken's skull cavity. ) W, R5 j# _9 X! ?5 d" F1 ^6 P. d
This was determined empirically in Australia, where a new factory $ x- a9 S, z: h' q4 l- G/ u
generating 7-Hz tones was located too close to a chicken ranch:
! c7 \9 L$ i5 j  q8 nWhen the factory started up, all the chickens died. </FONT></P>6 j/ U; K* S; v  {1 n: r6 Q
<P><FONT color=#0000ff>Your PC may not be able to emit a 7-Hz tone.
7 T: @7 F( _. Q9 j8 f0 {, w*/ </FONT></P>/ I5 j" z9 g6 w% B' d
<P><FONT color=#0000ff>int main(void)
9 j& h- e6 u* N{
% w0 h$ K$ S+ x7 e! |! nsound(7);
; F1 f8 E! B# p" }% x$ M8 I# Gdelay(10000); 2 K. w% I8 J% ?$ G0 f9 g. I
nosound(); 9 V! r  b7 E' }/ y2 B, m  q
}
& ~4 u0 ~8 b+ ?</FONT></P>




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