QQ登录

只需要一步,快速开始

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

函数大全(m开头)

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2004-10-4 02:57 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(m开头)</FONT></B></FONT></P>& ?. U0 v/ m# @) _  m, K  e
< align=left><FONT color=#ff0000>main()主函数6 |8 z6 G9 k4 u0 f+ C# B8 n1 _
</FONT>每一C 程序都 <B><FONT color=#000000>必须</FONT></B> 有一<FONT color=#ff0000><B>main()</B></FONT>函数, 可以根据自己的爱好把它放在程序的某 * I4 N: Q2 ?( Q# G: P7 P8 A/ ]1 T
个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放
2 H6 e$ U! |) ?; }7 f2 h& K4 }7 |在哪个地方, 以下几点说明都是适合的。 ' ?& \+ ?: m1 \  H# I- E1 h9 Z( Y
<FONT color=#000099>1. main() 参数 </FONT># e) I# b/ P  ?8 u% A/ {
在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。
7 y; E- R( f) J3 H! ^- j* argc: 整数, 为传给main()的命令行参数个数。 ( t% T/ G# V3 C/ Z: U' o3 [0 f
* argv: 字符串数组。 1 q% ~- W* g$ }5 |0 O) M' o; D& d
在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0 + g$ D0 q9 E' Q
以下的版本, argv[0]为空串("") 。
( Z+ Z# e$ k) o0 c( fargv[1] 为在DOS命令行中执行程序名后的第一个字符串; & F- |+ B6 c# [. o3 g9 i* |7 R$ R3 Y
argv[2] 为执行程序名后的第二个字符串;
4 W# U! I$ F6 k* n0 v  V...
0 z$ `3 j7 p. [8 ~$ pargv[argc]为NULL。
( I+ J( X6 T; x1 g* G*env: 安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符 : x2 s' K0 T7 l
串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C:
( U) Y# t9 ^: X/ W- o- O" _\TURBOC(对于PATH) 或YES(对于87)。
; P+ I9 D. N8 q+ X7 N1 R+ vTurbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中
* h% W% q/ v4 [* ~$ q说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序   Y: R  d5 U. G5 T1 p
的局部变量。
) y7 V9 L+ C. r6 s/ I$ M! u0 q请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下
$ P5 ]7 z! |3 ?的例子: 0 n$ c; Z& x/ i& H3 X( U
<FONT color=#0000ff>main()
& l5 E" w9 f' M( I+ Y# l' e0 O/ q: ymain(int argc)
6 |8 u1 e2 l7 Y) ?main(int argc, char *argv[])
3 s9 f. F! ?8 \+ Q1 t+ R* ]0 jmain(int argc, char *argv[], char *env[]) </FONT>5 L6 _. N* G4 s  a# ~
其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不 . v* _, l  W/ [
用argv[]的情况。
2 u8 y, `) s4 t0 s以下提供一样例程序EXAMPLE.EXE, 演示如何在main()函数中使用三个参数:
  ^; _$ D6 m9 w* ^* W5 x" j- B/ |  g+ r<FONT color=#0000ff>/*program name EXAMPLE.EXE*/ ( E/ Q$ _3 g3 x7 ~# g
#include <STDIO.H>  S* _0 j& z6 Q
#include <STDLIB.H>' i. ^8 G. S7 s! w7 X* ?* Y% q
main(int argc, char *argv[], char *env[]) 6 W6 T6 O, Y4 N# ~1 w% Q
{
" u& l7 f+ q: o" V0 }int i;
& X. H2 t9 f6 ~5 U5 y9 lprintf("These are the %d command- line arguments passed to 0 J2 H* l; T7 K* \  A
main:\n\n", argc);   M" j$ C6 i& X" b, w. M, N* [4 M
for(i=0; i&lt;=argc; i++) 2 ?# W* r7 N+ K3 C
printf("argv[%d]:%s\n", i, argv); : `* B8 B* B& A' X, \4 f
printf("\nThe environment string(s)on this system are:\n\n"); ; ~; C3 i5 V) Z2 x& @# D+ ~- h
for(i=0; env!=NULL; i++) 3 ~5 R* Z# `! |" v
printf(" env[%d]:%s\n", i, env);
2 P+ I/ z: v, b3 P. k: \} </FONT>2 s1 ]3 K6 o1 \) D: P( D
如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE: & A% [9 k6 A. u6 ?9 Z$ b7 S. L; R
C:\example first_argument "argument with blanks" 3 4 "last but
9 h# t8 z. D* `, c6 `one" stop!
7 B1 r; V: _" U) b" G: ]) b注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument 4 E- x* p4 p% a) n/ V% a" D
with blanks"和"Last but one")。 * Y2 }6 s6 p; v
结果是这样的:
' w2 H2 t( G' d5 A. {5 k<FONT color=#0000ff>The value of argc is 7 + ]$ }3 S4 R; L. S- |
These are the 7 command-linearguments passed to main: 4 i& v$ e6 }9 F+ ^# f
argv[0]:C:\TURBO\EXAMPLE.EXE
8 q4 Z2 X. I2 U! C* W0 n: r6 sargv[1]:first_argument
# h4 C2 R2 [! Iargv[2]:argument with blanks
4 C$ S3 B. |+ }7 ~9 nargv[3]:3 * \3 j* X( W3 ]) v
argv[4]:4
% g( `& O( m6 S( s1 i0 hargv[5]:last but one
* h% n# r+ J6 Y6 y' Zargv[6]:stop!
9 u  ]( Y/ Q# s! t" W4 [* `argv[7]NULL)
8 \% a, d, x" T2 H1 s! z! A+ GThe environment string(s) on this system are:
+ _! N9 b1 C2 @  Senv[0]: COMSPEC=C:\COMMAND.COM
, y1 p" C2 m( Z8 Z" Uenv[1]: PROMPT=$P$G /*视具体设置而定*/ 7 V7 ]  g& ~2 `! y4 j+ k+ _
env[2]: PATH=C:\DOS;C:\TC /*视具体设置而定*/
$ K/ u. k6 `& {# q8 Z& i</FONT>
7 A" U7 K+ b8 x  {1 @应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包 - ?' r! ?+ V& j  e. k
括参数间的空格), 这是由DOS 限制的。 # K, d: t% H* L2 `. Z' L/ J
</P>: g* @# ~6 O2 q' O' A% E
<><FONT color=#ff0000>函数名: matherr </FONT>
7 G5 `( r) I) ]! q) n功 能: 用户可修改的数学错误处理程序 0 Y7 c3 B: _# @* X  m, Z3 m- |; X
用 法: int matherr(struct exception *e);
4 x) l8 f  @  m2 I1 q程序例: </P>
, ?# L( }$ C" v8 N. i7 i5 ]<><FONT color=#0000ff>/* This is a user-defined matherr function that prevents
0 M3 h, W) S& q) T, |any error messages from being printed. */ </FONT></P>* _4 d; N/ X# T: x. F
<><FONT color=#0000ff>#include<MATH.H> </FONT></P>" u2 t: {  ?3 j
<><FONT color=#0000ff>int matherr(struct exception *a) , I" B8 Z2 t3 P# E
{ - ~; W/ L) t+ U, H6 J+ S
return 1; 0 D$ z8 Q' b$ B
}
0 B- A3 `. ~/ c+ o9 J- ~7 o</FONT>
; p  [9 }( j$ `) d/ i+ T/ S% N6 ~0 z6 i4 p( X
</P>& f( d2 P$ _* j5 @0 x
<><FONT color=#ff0000>函数名: memccpy </FONT>
& [! m) R& C" V6 t$ x功 能: 从源source中拷贝n个字节到目标destin中
( C; i+ c# Q. E用 法: void *memccpy(void *destin, void *source, unsigned char ch,
- a2 o/ W2 |- ]8 w1 ^unsigned n); 5 `3 y. x+ Z% P
程序例: </P>
4 ~- F9 |+ ?( O6 z: B<><FONT color=#0000ff>#include <STRING.H>
9 L9 I# l2 D! i. c# q5 R  ^# `#include <STDIO.H></FONT></P>
0 s5 ]# B2 R" A& q+ T<><FONT color=#0000ff>int main(void)
7 A0 U( n( }6 O  T! A{
* h4 B+ U6 v- C( ichar *src = "This is the source string";
% [) `% j0 l7 J0 D  v; {char dest[50];
) s4 w; A4 V  t. C+ gchar *ptr; </FONT></P>8 a+ T  {  ^9 K6 P* @( V* c
<><FONT color=#0000ff>ptr = memccpy(dest, src, 'c', strlen(src)); </FONT></P>, h7 e! i8 l* w6 V" M; @% ?
<><FONT color=#0000ff>if (ptr) % f: Q' }) |7 ]4 F8 @+ i) J$ e
{
/ J5 N7 n7 ~7 v*ptr = '\0'; 0 S& c0 g. _/ h8 g! W1 r$ i
printf("The character was found: %s\n", dest); 5 S& T$ d2 h2 n( E: M/ k
} 7 |* C$ K2 O- u% j! d
else
1 V5 I2 y2 i! q2 qprintf("The character wasn't found\n"); ! X! s5 N2 {$ U! d9 C% h/ P
return 0;
1 K5 l0 s  b# E  x  Z, O} . z! Q" p' a6 q
</FONT>
/ G; J# u: e6 ~4 ?3 }8 S</P>
- U3 o1 G- h/ ?9 _+ z& ]  I<><FONT color=#ff0000>函数名: malloc</FONT> & T2 N& i1 f8 ?# c( p! P
功 能: 内存分配函数   r9 x; P" b$ a) h- a5 j% P9 s
用 法: void *malloc(unsigned size);
6 z" _' o& {2 J( B+ ~5 w程序例: </P>/ I" @% n) d7 Q. r0 k- f( c
<><FONT color=#0000ff>#include <STDIO.H>. Q+ F3 h# w  |8 P" t+ @, h7 H
#include <STRING.H>: U4 B9 x* z* y
#include <ALLOC.H>
+ }( w* c# @/ E#include <ROCESS.H></FONT></P>
; I- I  q$ D2 ?; K, R! @/ w<><FONT color=#0000ff>int main(void) - N; ^1 @, j# j* }
{
* y) k/ w8 z4 P  i4 @" Cchar *str; </FONT></P>3 [- [2 s0 w" x( s9 F* ~
<><FONT color=#0000ff>/* allocate memory for string */
( w1 c; B; @' B$ _; _/* This will generate an error when compiling */ 2 z9 _# M8 L7 p  c+ T/ R/ P
/* with C++, use the new operator instead. */ + m: ~# y, }5 q
if ((str = malloc(10)) == NULL) ! m/ J( \, ?# C" F+ H: T6 ]& R- r
{
+ e  |8 ]" q+ U& H) |; D9 dprintf("Not enough memory to allocate buffer\n"); * U% ^; s- [$ ?3 F* u
exit(1); /* terminate program if out of memory */
/ l) f: r. d0 @! ?' @} </FONT></P>
: s/ l( q) X  `5 a. n<><FONT color=#0000ff>/* copy "Hello" into string */ 9 Z$ H% K8 A9 E+ q7 B
strcpy(str, "Hello"); </FONT></P>: z& r6 x4 ?' B% V
<><FONT color=#0000ff>/* display string */ 9 F. \2 x5 f  y0 a. B
printf("String is %s\n", str); </FONT></P>
$ b$ ~1 n0 V" k( C1 A<><FONT color=#0000ff>/* free memory */
6 G4 \. {: ^4 \$ tfree(str); </FONT></P># R. N* X: M; p8 c0 ~5 l4 E
<><FONT color=#0000ff>return 0; : t5 c6 U  a7 z. t  z
}
$ D% @0 X. w& N0 ?4 t8 c! R/ x- ]* Y0 \) I
</FONT>
3 G; W* |2 Z8 B( F( f; A. T" r</P>
9 j# m* _6 z$ b7 t% O+ Z4 t<><FONT color=#ff0000>函数名: memchr</FONT>
& y: \% P/ t/ C功 能: 在数组的前n个字节中搜索字符   J$ V: k$ \' w
用 法: void *memchr(void *s, char ch, unsigned n);
# ~. U2 k, I. {# y程序例: </P>5 W) I, \6 ?7 u8 L- Q
<><FONT color=#0000ff>#include <STRING.H>
+ K3 c; {: c3 P+ x# {#include <STDIO.H></FONT></P>
) L7 \* |" O+ G, C  \( K1 K, a<><FONT color=#0000ff>int main(void) " u1 Q8 b7 r% R0 p8 p
{ : a5 ^" Y/ [/ F! l) d. I/ {: w
char str[17];
/ K7 j# b5 D- _7 rchar *ptr; </FONT></P>
0 z# s/ P6 N, M1 O2 }9 h- w<><FONT color=#0000ff>strcpy(str, "This is a string");
8 |5 }5 P# R7 |+ X5 q7 z1 x* u1 {ptr = memchr(str, 'r', strlen(str));
' R* R! `8 e5 Lif (ptr)
, `3 J8 v3 _7 x/ v: gprintf("The character 'r' is at position: %d\n", ptr - str); ) \, d% i5 k: Z/ `" y" `( w) v' m
else , R2 x" o6 n- A; g% O2 d1 l$ }- M
printf("The character was not found\n"); ( S5 Z) ~' S9 A
return 0;
9 Z$ ^8 e  k. S9 h* N& r+ f} 6 j+ d0 a  N1 L4 }
</FONT></P>
# o2 ?+ W7 g# ^& }/ G9 C" V3 ]<><FONT color=#ff0000>函数名: memcpy </FONT>
/ d' J& _* o3 n) M$ v  `功 能: 从源source中拷贝n个字节到目标destin中
) R  }6 s; D* ?) r1 n0 o; ]用 法: void *memcpy(void *destin, void *source, unsigned n);
9 e- ?! _" z. l' p程序例: </P>
; s6 H) J, @9 e  r2 k& c, p1 E<><FONT color=#0000ff>#include <STDIO.H>, r! M4 b3 }$ ?1 p0 \
#include <STRING.H>
- U8 o. G$ k8 p) f5 L) Hint main(void)
' A7 \; i( a1 @7 W4 f{
" F* ~. C+ ?" @* u! e( b% }char src[] = "******************************";
/ P9 t9 X4 q- bchar dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; 8 X/ X, m/ j" L
char *ptr; . o' N1 p; h. @1 [. n
printf("destination before memcpy: %s\n", dest); 2 {3 J; \1 w  S" E' O! B
ptr = memcpy(dest, src, strlen(src));
" g; k* S5 n  T" G5 P/ e( b' K/ Xif (ptr) / {- j: z+ `- R
printf("destination after memcpy: %s\n", dest); ) g; j. ], Q4 R7 z6 G. U# l
else 9 a; A: l7 k: ]
printf("memcpy failed\n");
) I  l1 M" u. ^5 p% w5 k' ureturn 0;
+ Y: C: ^7 x; L} ( E; {$ f" Y1 p. [
</FONT>
1 I5 X3 k5 E8 U5 X5 P9 C: x</P>
* k7 \, o+ h/ @6 Y: G) m6 W<><FONT color=#ff0000>函数名: memicmp </FONT>
- O0 l" n( X! [功 能: 比较两个串s1和s2的前n个字节, 忽略大小写
! ~4 q* u( M; S: d用 法: int memicmp(void *s1, void *s2, unsigned n);
7 L! J) ]4 V: i8 Y$ w# r程序例: </P>
3 x+ s7 K( c" ~4 X! [' [<><FONT color=#0000ff>#include <STDIO.H>% @" e  q0 O8 n) U2 k1 x- s. h
#include <STRING.H></FONT></P>
/ `7 p3 k" [/ f<><FONT color=#0000ff>int main(void) " p/ r+ f& n3 ~( j
{
% j. R* S5 U2 V/ w! j/ Y3 W  zchar *buf1 = "ABCDE123";
3 n" C8 ?  ^7 r! ?/ U5 zchar *buf2 = "abcde456";
$ \% p+ }) L" B" q9 M/ Bint stat;
! {$ ?  W$ o% Kstat = memicmp(buf1, buf2, 5); 4 q# u1 Y9 d9 t2 \8 M' l! S- \4 s
printf("The strings to position 5 are "); 0 R# n  @  w( q. z
if (stat) 4 l5 V5 u- S) e5 ~/ m. X' x
printf("not ");
$ p2 w) }. D% h4 r& G# Y/ Nprintf("the same\n"); # N6 L: U3 Y2 z. J! p
return 0;
$ `, x0 I$ N( \# G}
4 w  i) |6 J0 b1 b9 ?& m</FONT>9 `# v3 b# A8 U2 U
</P>
) t  j3 ~5 Y1 P# j- M6 S<><FONT color=#ff0000>函数名: memmove </FONT>  v; e6 V+ _5 m3 c/ l$ u. ~4 B
功 能: 移动一块字节
$ F. E  k/ l" o( o, p用 法: void *memmove(void *destin, void *source, unsigned n); % @/ `8 f% c# M- c1 G$ V
程序例: </P>0 p5 t* U% M( Y, S; Z- ?
<P><FONT color=#0000ff>#include <STRING.H>
  d2 w3 I# H8 n+ U- [#include <STDIO.H></FONT></P>1 W4 E  K8 c+ ?$ `( {
<P><FONT color=#0000ff>int main(void) ) x0 N. t! ~3 l( M( j* g# f( x
{
5 [6 F' q9 K2 N. mchar *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
2 q6 _2 t$ [$ Vchar *src = "******************************";
) g) r( N  B: z" zprintf("destination prior to memmove: %s\n", dest);
& ~! n2 Q0 d$ ?1 jmemmove(dest, src, 26); - r; @9 I0 A( w" p! l: c, r8 \
printf("destination after memmove: %s\n", dest); 9 K' i, ~7 j8 o( p8 V" _1 p
return 0; 4 _0 J* X/ d/ z8 q0 |7 f# h
} 8 o! A% {; T, f- o* D

3 F, x4 Y, }1 K1 g: d* W  }. s; m</FONT>
# m6 ~# j( ^9 u2 |</P>
+ r8 f3 R, V/ M- ?+ D<P><FONT color=#ff0000>函数名: memset </FONT>
8 g+ ?/ r! g3 i; Z' K功 能: 设置s中的所有字节为ch, s数组的大小由n给定 0 f+ U' r  B( ], J1 i4 {' `
用 法: void *memset(void *s, char ch, unsigned n); ' E  p2 f+ _# \4 C
程序例: </P>& G" l: b. }! z$ s8 k8 }; _
<P><FONT color=#0000ff>#include <STRING.H>
; S2 \) _" R  D& r#include <STDIO.H>
# T6 m5 J+ s1 N3 r3 D' p#include <MEM.H></FONT></P>! E6 ?  @  d. m- b
<P><FONT color=#0000ff>int main(void)
( _6 Z! g: |- B: y{ 5 b5 H7 ^( }& \1 e: f
char buffer[] = "Hello world\n"; </FONT></P>
# \3 o! k; a. Q<P><FONT color=#0000ff>printf("Buffer before memset: %s\n", buffer); - r6 k# b# o9 g, e0 a7 y8 ?
memset(buffer, '*', strlen(buffer) - 1); 6 \7 ~' r8 `8 I# K# q, X
printf("Buffer after memset: %s\n", buffer);
  Q- c5 `9 s1 f; @2 v7 N; Ireturn 0; 9 q* `* M) Y' o/ L$ e2 ?9 ?4 G
} </FONT>
9 o- \- h  K& d. x8 c& X( ]3 G0 ?6 T4 _. w
</P>
/ H$ T2 D% z' w; b( [<P><FONT color=#ff0000>函数名: mkdir </FONT>
: R6 O( ~& O! N/ \! j功 能: 建立一个目录
3 K% v  G( t, z% i7 @1 }用 法: int mkdir(char *pathname); ' }+ }% F- R0 E; |: [$ n6 P
程序例: </P>! R# ]2 u& E: M7 X) u9 ?: C! z
<P><FONT color=#0000ff>#include <STDIO.H>
' J% e) k- m3 h#include <CONIO.H>' i2 n7 W& q5 v
#include <PROCESS.H>
' Y8 e% [/ o0 e7 K- [#include <DIR.H></FONT></P>" C. W/ k" b) p' U8 t
<P><FONT color=#0000ff>int main(void) ) |3 ~( W0 q' u* S# K+ p$ z% q2 _
{
8 m6 {2 l6 z$ Z+ w" B1 ?int status; </FONT></P>
5 X4 n7 v$ f# j& w- ?<P><FONT color=#0000ff>clrscr();
( |$ G% [6 J8 I3 }; r. bstatus = mkdir("asdfjklm"); ( H4 ]8 `8 P7 N
(!status) ? (printf("Directory created\n")) : & p2 j8 E, b3 Q' P; |6 D; G
(printf("Unable to create directory\n")); </FONT></P>' Q. ]% A. i5 q: A, F
<P><FONT color=#0000ff>getch();
) g9 z% [. \1 b/ k8 t0 }9 G; M* Asystem("dir"); ; K' ~5 }& [6 o& U; u1 _0 V
getch(); </FONT></P>
- H" E% ^" c6 L- u# W5 V( T; x<P><FONT color=#0000ff>status = rmdir("asdfjklm");
/ a, B  @- b" C- p/ J+ ^(!status) ? (printf("Directory deleted\n")) :
) T. h: w% v. P0 A- C! u(perror("Unable to delete directory")); </FONT></P>
! B6 g3 F# I8 J* o( t9 H<P><FONT color=#0000ff>return 0; + E9 `! e& V% Z: c. D
} ! {- S  T6 M" z* {: @$ A9 l7 z
* s& {, \  C% L& T  [3 W9 ~
</FONT>
* R+ i7 _, H2 y$ e/ O5 _</P>
+ I. [2 `1 E- C+ Y) l7 @<P><FONT color=#ff0000>函数名: mktemp </FONT>3 ~3 h6 M1 T5 O! W7 V# ~1 Z# P
功 能: 建立唯一的文件名
4 M  T5 i/ y; ?2 _7 t; c用 法: char *mktemp(char *template); 2 E, J7 Q, @* Y" z0 z# E
程序例: </P>
* J' |3 k/ m, w; H- C<P><FONT color=#0000ff>#include <DIR.H>
1 M: q2 G) i) K#include <STDIO.H></FONT></P>
0 c# y2 O7 i0 j4 [! U: x<P><FONT color=#0000ff>int main(void)   E" X' \6 Y* y9 [3 }
{ ( J; f* y0 f' N! t
/* fname defines the template for the
! F1 G+ F- k  O3 U6 P) k) c. K, B) Ntemporary file. */ </FONT></P>
5 q2 J0 z0 ?! b' @- Q: ~<P><FONT color=#0000ff>char *fname = "TXXXXXX", *ptr; </FONT></P>" C: c9 Y' A. @, D
<P><FONT color=#0000ff>ptr = mktemp(fname);
! Q8 W8 y9 A% S0 O& rprintf("%s\n",ptr); % |8 o( P! O2 O3 Y. H5 u* E$ e
return 0; ) b( Y1 a: ~' E" y* v& _
}
* C- C9 K, p& j$ y</FONT>
) l( j% {( b0 ~( m" ^! u2 d- X</P>) n- _2 f8 J0 }& ?: _
<P><FONT color=#ff0000>函数名: MK_FP </FONT>% t* L( v& y; Y! X4 I% B: b
功 能: 设置一个远指针 ; c) M: I3 ?& g: K% h9 W+ a  a, Q) E6 Y
用 法: void far *MK_FP(unsigned seg, unsigned off); # H' F) {: Y7 k( e* t6 ~0 ~7 w; d5 |
程序例: </P>
+ ]3 V5 J2 d+ F2 O! b& x<P><FONT color=#0000ff>#include <DOS.H>  x# S* Y' t! G( T6 B
#include <GRAPHICS.H></FONT></P>" u; k7 X8 O+ V% ~
<P><FONT color=#0000ff>int main(void) . b! D3 l' u) q/ P
{
4 i1 B" C5 v' ]  x9 |3 Rint gd, gm, i;
' @" L" i) i; Q; D  E  ^" `+ bunsigned int far *screen; </FONT></P>
: o: z/ v" N7 k) }* `& h8 l/ c- D<P><FONT color=#0000ff>detectgraph(&amp;gd, &amp;gm); 4 T9 _$ A0 \/ U# e  w
if (gd == HERCMONO) " \& v6 }6 w( _4 l! p) w3 L# B
screen = MK_FP(0xB000, 0); 3 w/ j- w  U) w: I5 x# @" N
else + `$ z  o& l% {- T5 C
screen = MK_FP(0xB800, 0); - f1 l9 v* m) ?* [: {1 q  ]1 ]
for (i=0; i&lt;26; i++)
' Z8 U0 `! [7 Qscreen = 0x0700 + ('a' + i); & k' K: R; p( K! n4 v
return 0;
) V* Q8 }) |: u5 `# ?8 ?7 P}
. w/ V5 u+ y9 q6 D5 n' `</FONT>3 V! |4 c5 [8 A& [* a' o6 V
</P>
  Z4 O) k+ g' `8 P, {9 _<P><FONT color=#ff0000>函数名: modf </FONT>
0 f0 m# |: K" O功 能: 把数分为指数和尾数
! y$ k6 E2 F2 i7 C) c% ?7 L用 法: double modf(double value, double *iptr); 0 E" T# O2 t0 C2 J: E
程序例: </P>9 a, o. S9 F( X" T' J& E
<P><FONT color=#0000ff>#include <MATH.H>
% q1 d" r& k* O% j$ a#include <STDIO.H></FONT></P>
; s6 y$ j, z" @" ]. o0 s8 h<P><FONT color=#0000ff>int main(void)
( h* S/ Q1 k8 ~: R4 k$ Z3 i{ & }7 Q$ [4 Y* ~( A; v7 V& ]% A- B* H6 u
double fraction, integer; 6 N! m" n! d, m' ^' m. t
double number = 100000.567; </FONT></P>+ c3 L# x2 F3 t5 o- f
<P><FONT color=#0000ff>fraction = modf(number, &amp;integer); 8 C% q* n9 f/ E2 R3 y
printf("The whole and fractional parts of %lf are %lf and %lf\n", ; Y; G: U9 J* j% X+ r- Y
number, integer, fraction); , u4 Q: u& B$ [0 g; E9 `
return 0; ( W& ]. y: V, K5 X; V. [
} </FONT>
  Z. d5 x. ]) x5 F4 o. n8 ^0 G& P9 j& S! d
</P>
9 n: o( d7 s, M0 M/ K  D! \6 X& f<P><FONT color=#ff0000>函数名: movedata </FONT>, \3 i8 I2 h- s
功 能: 拷贝字节
0 H% p- ~$ _" ~$ @. q% x, L* ?3 U用 法: void movedata(int segsrc, int offsrc, int segdest,
9 G9 y6 b7 n& k0 x0 Pint offdest, unsigned numbytes);
% I3 L8 w0 a( _3 g$ b! `& b程序例: </P>* N  o3 d( Y6 U2 R" e4 @. C7 G' P
<P><FONT color=#0000ff>#include <MEM.H></FONT></P>
5 E, h# A: Y& H+ b3 R5 P<P><FONT color=#0000ff>#define MONO_BASE 0xB000 </FONT></P>* p" B0 ^0 \5 q1 M! u) }3 @
<P><FONT color=#0000ff>/* saves the contents of the monochrome screen in buffer */
7 N& r$ Z0 P  \4 v. N* hvoid save_mono_screen(char near *buffer)
4 w* p, \: M: [; r8 Y{ 9 r+ l  b, W7 \/ b, l
movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2);
, H- ?' ]& j3 W} </FONT></P>
4 }  x+ C9 u/ R$ I, @6 Z1 [0 @/ O<P><FONT color=#0000ff>int main(void) # e% M! X( l# A  R1 H% s
{ ) A8 i) W4 c; O
char buf[80*25*2]; 4 `/ o$ H. h: D% m: }. a$ ?
save_mono_screen(buf); $ u! x9 T* `1 f( A! M! N. _
}
; o+ l5 J, V4 I1 w3 |) v</FONT>
: o: W$ [( c0 [1 i</P>
. Q6 U/ F  f+ Q<P><FONT color=#ff0000>函数名: moverel </FONT>( T3 ]8 k  G7 q" m( s  R! G
功 能: 将当前位置(CP)移动一相对距离 0 U. U) @5 {  E! e4 m+ z% s8 Q& U7 O
用 法: void far moverel(int dx, int dy); ( h' V6 ?, c. p2 y) V7 U2 i
程序例: </P>
8 _" M( M* D3 ?# B0 j5 B<P><FONT color=#0000ff>#include <GRAPHICS.H>
. {6 `( l& r/ j* }% p#include <STDLIB.H>
$ R8 [. [2 D9 d& H% _6 y8 S/ I. b#include <STDIO.H>& R# p% K' f$ J3 Q& p4 z: m, w8 J0 T
#include <CONIO.H></FONT></P>+ z2 t" ^+ w, @7 c# y
<P><FONT color=#0000ff>int main(void)
+ a* r* @6 D6 V5 U{
3 p4 M  f. d' Q% x/* request auto detection */
/ }5 I) D5 Q! E, @+ b4 N/ Pint gdriver = DETECT, gmode, errorcode; ) a+ ]( Y8 M& K3 `
char msg[80]; </FONT></P>+ V) D! Z0 a5 @
<P><FONT color=#0000ff>/* initialize graphics and local variables */ " F1 Z: }2 f. }9 C5 ]
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
- |/ s  `7 c* Z* D2 h7 p6 m' W- m<P><FONT color=#0000ff>/* read result of initialization */ 0 v" b: E) z& u" ^* D# X
errorcode = graphresult();
; G1 R* b9 Q; x* \if (errorcode != grOk) /* an error occurred */
/ ^/ R+ g+ I' N2 K, c{
, n9 P1 y- ]8 B2 I% e$ G% U  Eprintf("Graphics error: %s\n", grapherrormsg(errorcode));
+ E: z4 {, s  e1 o+ i7 P7 Gprintf("Press any key to halt:");
, m2 d2 o1 {9 w: {getch();
. ^/ Y7 m4 T# P" nexit(1); /* terminate with an error code */
/ Q" b5 }3 r# S9 c- R} </FONT></P>/ P, Z% `" N: m9 J# n: `
<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */
& h! c7 m$ T5 f9 X1 b! Amoveto(20, 30); </FONT></P>3 n8 t4 D" |" ~
<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ + j3 r' `3 h  o1 M7 P
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>% s; b' H0 @1 x4 k- A# p
<P><FONT color=#0000ff>/* create and output a message at (20, 30) */
# |' |% Y0 k& Z2 {6 n5 G6 dsprintf(msg, " (%d, %d)", getx(), gety());
! p6 {0 m  C5 o; e  Zouttextxy(20, 30, msg); </FONT></P>
7 V  {  n" V+ Y) {& h- }, G& B! E( r<P><FONT color=#0000ff>/* move to a point a relative distance */ 1 t7 r4 \" j1 k" P% M
/* away from the current value of C.P. */ 2 B8 a7 ?4 }! i, A3 M6 l
moverel(100, 100); </FONT></P>
* [( V4 s, w/ }% I<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ - ]! J' y- D" s, h0 h8 R
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>9 S3 T; v$ F0 B* R( `! L: N
<P><FONT color=#0000ff>/* create and output a message at C.P. */
1 g' C% I) Q/ J5 |" t5 u& hsprintf(msg, " (%d, %d)", getx(), gety()); 0 E2 B: V: W1 n
outtext(msg); </FONT></P>: a. j1 E  G+ R$ w
<P><FONT color=#0000ff>/* clean up */
  p! b! O! k3 D/ Kgetch();
3 |0 @1 O# A& b% Z) Gclosegraph();
# C( h. l5 K% ~0 o  r6 Freturn 0; ; C7 L5 s$ y  ^5 O9 m
}   n% }" b* |" h$ l! f
</FONT>* F7 U) }/ \$ ~' j* E
</P>
8 _8 n+ N$ ^$ Q1 R0 p3 B  ^0 E3 k5 g<P><FONT color=#ff0000>函数名: movetext</FONT> $ P& H0 Y( i5 ~+ E5 N% d6 e- [' w
功 能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域   F8 V. Z3 U9 z0 C- [4 D" k& O4 E, S. E" N
用 法: int movetext(int left, int top, int right, int bottom,
/ {( s. M' V7 y# m( X) ~- y/ ~" Eint newleft, int newtop); $ e' j8 W4 ~* f. I  A& `
程序例:
2 _9 Y3 a& A1 ^1 h<FONT color=#0000ff>#include <CONIO.H>* ^9 n1 c/ H  S, V" M
#include <STRING.H></FONT></P>5 O0 P1 L& {: H  D
<P><FONT color=#0000ff>int main(void)
* n* ?& `& O) r/ J' Q& d( g{ ) a2 S2 S  x) y& Q% R& N% o7 S" j' ?7 z
char *str = "This is a test string"; </FONT></P>
- H! L; o1 A$ F& ?) _# z<P><FONT color=#0000ff>clrscr(); ' j) `7 |! t5 Z! e6 a3 g+ ]
cputs(str);
5 p0 i+ j3 Y( @getch(); </FONT></P>" K8 O# a# w. G8 O7 d& ^: [# W
<P><FONT color=#0000ff>movetext(1, 1, strlen(str), 2, 10, 10); 7 o: J9 H- j3 M) g1 x
getch(); </FONT></P>
! H" t" F$ e+ f. B0 L<P><FONT color=#0000ff>return 0; 7 D+ A  d, m$ O$ [
}
! A3 u0 g; J1 F6 o8 A' x* M: N9 ~) T4 A0 Q5 `8 q# @; `" t
</FONT></P>
# {* X  J3 c# S<P><FONT color=#ff0000>函数名: moveto</FONT>
' h" F& b5 l* |功 能: 将CP移到(x, y)
2 G' d2 Q2 R7 e; U用 法: void far moveto(int x, int y);
* |2 \: V7 T1 X' D0 f程序例: </P>( f0 [4 n( V  \4 z) ^4 l- d
<P><FONT color=#0000ff>#include <GRAPHICS.H>+ B. `/ ~: t4 g, Q
#include <STDLIB.H>
6 C, r' D, L* T4 z#include <STDIO.H>
' l& y9 ^4 {. v& Q6 n* F6 Q  q/ W#include <CONIO.H></FONT></P>3 a/ i( s8 ^9 P2 y& X* C
<P><FONT color=#0000ff>int main(void) : A+ X& V  k- i! n6 x8 d+ s
{ * c$ A- K# M- u/ ^( }
/* request auto detection */
+ L: n/ I9 N4 M% y$ Vint gdriver = DETECT, gmode, errorcode;
7 c$ u9 J, ^, p9 Nchar msg[80]; </FONT></P>/ \* n1 v5 B1 X' {
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 7 E% O& b1 ^* q$ S# l: K" l  V
initgraph(&amp;gdriver, &amp;gmode, ""); </FONT></P>
7 ~5 h8 _0 O! V: D) o<P><FONT color=#0000ff>/* read result of initialization */
+ r4 o+ m* q: u! Y; Jerrorcode = graphresult(); & K& a7 K9 j7 g! o- I2 q! S3 \
if (errorcode != grOk) /* an error occurred */ , j! g- n4 j! C7 z1 `
{
' x6 r% c3 j" [: t: z6 J3 U( X9 Rprintf("Graphics error: %s\n", grapherrormsg(errorcode));
$ D0 A  L# m4 K1 \3 ^3 Uprintf("Press any key to halt:"); $ {. h2 W* X9 l3 b7 k0 E' o
getch(); " C! g; O5 y% D0 b: W3 [! |' @
exit(1); /* terminate with an error code */ / T; U$ F7 Z) d- r& [
} </FONT></P>5 g2 F( L$ `* c2 ~5 X7 m
<P><FONT color=#0000ff>/* move the C.P. to location (20, 30) */ $ |1 H/ U2 d: T4 c+ g+ ~
moveto(20, 30); </FONT></P>
% c6 P  m# S7 @7 p7 p( o7 j5 h<P><FONT color=#0000ff>/* plot a pixel at the C.P. */ & ?2 b% B' z# @/ _! F$ g
putpixel(getx(), gety(), getmaxcolor()); </FONT></P>
- x0 z4 |6 g! [; G) Q" o6 i' n$ P<P><FONT color=#0000ff>/* create and output a message at (20, 30) */ ( i+ ?6 I4 I9 f0 Q( E
sprintf(msg, " (%d, %d)", getx(), gety()); - h, K* S+ E, f' ?6 \0 c
outtextxy(20, 30, msg); </FONT></P>9 `. v8 a- P8 ~3 s8 L) M% Z( m
<P><FONT color=#0000ff>/* move to (100, 100) */
: y7 Y1 o! L; G7 h& Hmoveto(100, 100); </FONT></P>
7 x3 S* V. U$ e<P><FONT color=#0000ff>/* plot a pixel at the C.P. */
9 J+ ]6 [- i% w' H  wputpixel(getx(), gety(), getmaxcolor()); </FONT></P>
' ^6 p9 c5 g! w$ E# ~<P><FONT color=#0000ff>/* create and output a message at C.P. */ : r; m* x& d+ @5 U) L# F  Y
sprintf(msg, " (%d, %d)", getx(), gety()); ) T: c- d) ^4 I$ R" G9 c3 j  O% k1 g$ c
outtext(msg); </FONT></P>* U, O" g% X( s) v' f% ~' R3 I
<P><FONT color=#0000ff>/* clean up */
8 n8 d; _+ h1 r" ~getch(); ; x& e. ?8 D) s* I  t
closegraph(); + R! Z- _8 [+ S$ D2 _
return 0; - h2 c; H& z1 r# f% |% \
} # x" [- |3 q1 J9 ?
</FONT>
# ~2 r! y: P; H, E  Q</P>- H* T4 B$ F# q6 t0 w
<P><FONT color=#ff0000>函数名: movemem </FONT>  w- Z6 B! O; w+ X
功 能: 移动一块字节 1 V6 J9 `2 T% P7 ^2 B( U
用 法: void movemem(void *source, void *destin, unsigned len); 6 I0 z; P! l, \8 Z
程序例: </P>' m6 l: Q7 N& _9 P5 B
<P><FONT color=#0000ff>#include <MEM.H>
) d5 R2 M/ ]* i#include <ALLOC.H>
4 K/ o1 C: C/ s) W#include <STDIO.H>, c/ n9 x& u4 w- X4 {, b' ^4 g6 ~
#include <STRING.H></FONT></P>
) y$ Q5 |+ H. O: N( r9 T<P><FONT color=#0000ff>int main(void)
6 x$ r6 U2 `' z  F0 E5 _+ M{
) d# b% r3 y7 Q* y9 ?char *source = "Borland International"; ) ^. \9 r* S; q2 l8 ?9 j+ T! c$ p
char *destination;
& o3 w. M- h1 z; n$ r) S, H6 K+ gint length; </FONT></P>" u. M9 B* {; [0 o8 |( Q
<P><FONT color=#0000ff>length = strlen(source);
1 y+ Z( }# L( h# ndestination = malloc(length + 1);   `. S( m& S8 s, U2 i
movmem(source,destination,length);
/ H/ F: E/ m0 Q, r) m( Jprintf("%s\n",destination); </FONT></P>/ B# Q6 R# l: `7 [5 g7 F
<P><FONT color=#0000ff>return 0; % x; |4 |0 W3 @# B2 y3 ?
} + j; s7 M- {4 l0 O; K# J
2 p" n+ q% N$ ~9 H+ c' U2 |7 ]
</FONT></P>" j3 Q1 P3 u, q, @4 F
<P><FONT color=#ff0000>函数名: normvideo </FONT>9 U6 A/ e5 _; a
功 能: 选择正常亮度字符
' V$ J# r4 v/ B8 v; q用 法: void normvideo(void);
5 |; O2 R& B7 M& d0 M& v; ?程序例: </P>2 w7 n0 y; Q0 l0 v+ {2 G0 h  N
<P><FONT color=#0000ff>#include <CONIO.H></FONT></P>2 @! Z. k2 L% a) W: O6 v
<P><FONT color=#0000ff>int main(void) 6 r) t3 h4 }8 U2 Q
{
, e  A2 G- f* {' bnormvideo();
* F5 K: Z5 T8 e+ o9 M. m4 _cprintf("NORMAL Intensity Text\r\n");
2 ~# F$ u# e$ Ureturn 0; " w# ]; g. d* a6 z6 \* B% N2 e
} # J* {+ d4 k) _

' T4 e' f) T9 y</FONT></P>
  q1 Z3 y) b* x* E- W<P><FONT color=#ff0000>函数名: nosound</FONT> * d7 y' J$ {2 ]; x- U! ^
功 能: 关闭PC扬声器 9 M3 g- ^: D' E4 e2 ?( `! K
用 法: void nosound(void); 0 m* Z4 ]+ ]5 [+ w
程序例: </P>. e: B& L, W0 Z
<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. </FONT></P>! I' c+ i: n/ r
<P><FONT color=#0000ff>True story: 7 Hz is the resonant frequency of a chicken's skull cavity.
( j$ ^7 w) j" v# G7 Y7 ^This was determined empirically in Australia, where a new factory ; w2 C2 E8 L) a8 P$ i6 j& ^1 A
generating 7-Hz tones was located too close to a chicken ranch:
/ r1 P2 X; H; U  [" q' m/ j+ `When the factory started up, all the chickens died. </FONT></P>) S9 J2 n7 i; J! C+ \
<P><FONT color=#0000ff>Your PC may not be able to emit a 7-Hz tone. 7 B. K1 e) ~8 E) ?/ y
*/ </FONT></P>
9 ?& b$ G. x" t<P><FONT color=#0000ff>int main(void) * A' ?) O. Y" ?7 b/ V0 |5 K* R( d
{
3 a* X& X% \" p7 c; h0 \sound(7);
9 U+ F: M- O* i/ E) N3 r0 Tdelay(10000); 8 |- Q" d# m. g/ V
nosound(); ) n/ w/ B, }- Y# r' K
}" w; B3 Y' T5 {: [2 R- l) z
</FONT></P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-20 09:56 , Processed in 0.475164 second(s), 52 queries .

回顶部