数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 03:00
标题: 函数大全(h开头)
<><FONT color=#ff0000>函数名: harderr </FONT>
- i2 z$ Y1 E3 w) G功 能: 建立一个硬件错误处理程序 " r+ ^7 S3 w( ?, o# u
用 法: void harderr(int (*fptr)());
- B& }+ g/ B! ~, K- V程序例:
: @3 F4 ^7 n0 t/ w' I% S" i<FONT color=#0000ff>/*This program will trap disk errors and prompt
6 j8 G% E' H0 h0 ?& N# Mthe user for action. Try running it with no
5 Q' _" L. ?3 c1 s; N7 R1 M  p! [disk in drive A: to invoke its functions.*/ </FONT></P>
# F1 g6 Z* J/ n# E( _' n: g<><FONT color=#0000ff>#include <STDIO.H>
) H/ E1 r1 T2 X/ P#include <CONIO.H>
6 b/ b' P; R# K* e, o! b3 N#include <DOS.H>+ v9 a; }7 A; {& o8 d
#define IGNORE 0
  C6 S* L* B, H) V; l8 _% T  g#define RETRY 1 $ Z) Y& S* K3 v/ k" E# p2 |
#define ABORT 2
: w! M" B! J+ ~+ [, D. lint buf[500]; 6 f/ c0 f/ S& z
/*define the error messages for trapping disk problems*/ 3 V& q4 L" s; A3 U$ F
static char *err_msg[] = { 8 O2 h; V- N* j' f& ~0 q  q
"write protect",
% ]) u8 f# ?' N/ e* R"unknown unit", ( K6 z2 |9 K6 n. w/ U' H
"drive not ready", : _& |- m; i% G# T" K
"unknown command", * L, m% {! m) l
"data error (CRC)",
8 `+ X5 j- S4 T"bad request", : f+ c3 t* L% i. Q9 i
"seek error", 5 F  E: P- g- T$ P
"unknown media type",
" ~6 q: C/ e# Q"sector not found",
7 a  g; ^# q% m( Y2 D8 G. a"printer out of paper",
: E6 D/ a" D! r/ A5 }. x2 a2 [! k"write fault", / Q! ^* J3 g8 c7 I# b
"read fault", $ _) K) Q4 o$ M8 B, e, ^
"general failure",
& A" D; u( A. w+ y3 A5 w# Q"reserved",
% K' ?( Y: u7 w2 ["reserved",
2 K0 e  `8 C3 G# K"invalid disk change" 8 v/ ^. N& W1 Y% q3 H* q! E; R7 a
}; </FONT></P>
% f  j/ |8 O# u<><FONT color=#0000ff>error_win(char *msg)
# m' E- v/ a+ s( }; k{   @! T$ b8 m2 b, t; m$ H- D
int retval; </FONT></P>& H/ L$ [  @- J
<><FONT color=#0000ff>cputs(msg); </FONT></P>
9 @" n% c& d! X<><FONT color=#0000ff>/*prompt for user to press a key to abort, retry, ignore*/ # x4 Z; E5 d; W/ j' |" s
while(1) # q' g& h: K; }0 y
{
, b. |4 X" b5 _, }' N: u3 M0 zretval= getch();
, c; |2 I; A& {! s& {, g5 Hif (retval == 'a' || retval == 'A')
' o2 s6 V$ M& Y3 P{
+ ]" D% N! L* P" sretval = ABORT;   x& t2 h+ F+ V) Q
break;
4 l: P$ R, [, q4 g) D}
/ }! n+ n3 M3 @9 R, A4 j! w1 iif (retval == 'r' || retval == 'R') / K  T! Z3 u! [" V9 u  T
{
/ E9 ^4 m' a, T; i, F# L# z0 _retval = RETRY;
9 v% s# e, I( ^5 Qbreak;
6 {" K% ~* d, T: L2 x} * r8 ]7 @0 g& D& d
if (retval == 'i' || retval == 'I') 9 b) S+ N9 x0 _" ^% B
{
# L1 d" f5 V: J: v$ y4 P% x( Yretval = IGNORE; 2 ?/ C1 ?, z3 |/ `9 F* M2 ^
break;
) s% p. a, |- D8 V# c}
" W+ k9 S* o- t8 g7 g} </FONT></P>
* m  G# x8 u7 o<><FONT color=#0000ff>return(retval);
. x) D* X6 v! R3 H# D' k; x} </FONT></P>
' E6 r. B- [5 ?: E<><FONT color=#0000ff>/*pragma warn -par reduces warnings which occur * m! ]* c4 i4 A' v; A: o  F
due to the non use of the parameters errval,
# D, ?+ k& b& v9 W* o9 |bp and si to the handler.*/
2 ?+ G' }2 Y* D3 |4 x2 b#pragma warn -par </FONT></P>
" _8 B2 c  }! L# h( X; J5 @7 s' k- O<><FONT color=#0000ff>int handler(int errval,int ax,int bp,int si)
) Z8 T$ U$ G: S{ 2 P* k" k- t& _/ F2 t3 V3 F
static char msg[80];
: p3 B4 l: T7 C7 V+ tunsigned di; + \# P$ R( ?) X) d: f& x& v) l+ R( S  F
int drive;
, J4 j; p2 r9 a' O) d; }* S0 Zint errorno; : c# K" ~1 r# y5 r: _- Z8 w3 K
di= _DI;
! _8 ^1 }" R! G& `7 r9 K" t/*if this is not a disk error then it was
8 w- F) c# x% ^- @% R+ h4 _another device having trouble*/ </FONT></P>( b& B, x, s  F. `! V' c3 }
<><FONT color=#0000ff>if (ax &lt; 0)
  N2 O! c% y: a{ . W4 |6 o, a' f1 \$ S" }
/* report the error */ - g. g1 _4 G& s/ z
error_win("Device error");
2 S6 w( [2 b0 j# f/* and return to the program directly requesting abort */ 5 m8 Z7 p! G- Z0 C
hardretn(ABORT); ! w, C2 ~- o) l9 ]
}
6 W5 h$ `: ?8 C9 m/ q; Q* E, L  y# P4 M/* otherwise it was a disk error */
, P3 O7 m/ J5 J/ n6 b  ~, @7 kdrive = ax &amp; 0x00FF;
% z, j  ?: [$ W, {! T4 r. }  y' ~errorno = di &amp; 0x00FF;
% {9 a2 V2 h+ P2 V  V$ a/* report which error it was */
1 ?% \, n  E) D+ @, Z; zsprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ", 5 m2 ~) w4 n6 N- J: }) @
err_msg[errorno], 'A' + drive);
' V' D# j# H5 }/ n/*
/ p, C5 a) d; P* k! O! ereturn to the program via dos interrupt 0x23 with abort, retry,
$ Q0 }" E" M$ B( ]1 Tor ignore as input by the user. ' G: @1 H: U3 |! A) ]; s
*/ ; H, R6 m& c9 j6 j# I- A
hardresume(error_win(msg));
3 ^1 y' p/ _* ?! ~& @  l7 N' Ureturn ABORT;
" c& d  I; Y. q0 V8 G} 4 x5 m+ A% j3 G$ F7 c
#pragma warn +par </FONT></P>( F* S& c* \& X
<><FONT color=#0000ff>int main(void) 8 x# N& T% t9 ^; U. J2 `
{ 9 h0 @2 `" k/ r4 {' L0 \9 H  Y
/*
  u- g) s3 o6 Ainstall our handler on the hardware problem interrupt 8 R, ^4 d9 S# M
*/
% `/ g3 F9 p7 p: |# Vharderr(handler);
3 Z! c; G0 Q" N1 A" t' n( N6 a8 Jclrscr();
2 j7 b) P5 e( q7 Zprintf("Make sure there is no disk in drive A:\n");
9 W4 q3 U3 g7 C# W" g) [* d7 yprintf("ress any key ....\n");
6 N' C" l& Y6 {; J4 igetch(); ; w, u, f7 ]0 Y8 H' V0 P/ I0 d
printf("Trying to access drive A:\n"); 1 R# f* g3 o- I" ?+ t0 O  S* B
printf("fopen returned %p\n",fopen("A:temp.dat", "w"));
1 P6 A! ?: ]+ }return 0; + Q+ F" g% z) J# S' a# g6 v8 M
} $ W/ }( j( M1 L
</FONT>1 l$ Y/ k8 p4 c( v
</P>1 d, |& Y7 K" u; \( @2 }4 i
<><FONT color=#ff0000>函数名: hardresume </FONT>
( r! t$ h2 K( n# j功 能: 硬件错误处理函数 / b" A; k3 B/ w7 v1 j  k( n
用 法: void hardresume(int rescode); ) A% t; Y. {. }9 u
程序例:
) A* U$ A$ p1 ?4 R</P>, M, f1 q/ ]5 O6 r
<><FONT color=#0000ff>/* This program will trap disk errors and prompt the user for action. */ $ N( o% u$ @5 i- s! g
/* Try running it with no disk in drive A: to invoke its functions */ </FONT></P>5 T3 _8 n+ A" E
<><FONT color=#0000ff>#include <STDIO.H>
/ R/ o/ M6 [- D4 G& n! X7 K#include <CONIO.H>
! X/ d6 q3 }: M! Y6 D#include <DOS.H></FONT></P>
/ ^8 }( v! b5 @<><FONT color=#0000ff>#define IGNORE 0 - \+ \# V/ o$ V) W  ]3 L
#define RETRY 1 9 m0 y+ q' ^; @' D' f* ~: h
#define ABORT 2 </FONT></P>; F: h  p8 }8 T" X' E+ P4 s
<><FONT color=#0000ff>int buf[500]; </FONT></P>
( e4 i" F* v3 J<><FONT color=#0000ff>/* define the error messages for trapping disk problems */
* [2 |9 B# N' }2 t7 |static char *err_msg[] = { 6 {& t' t$ y5 Q$ i3 w4 K, E
"write protect", , t/ B% n1 q  ^# Q; ~
"unknown unit", 9 y* d# A6 U0 K) a. ?  u
"drive not ready",
, r! z2 G) B9 z( k"unknown command",   d' {% u, N* e$ l2 _6 u. A! p
"data error (CRC)", : p* ]# K' a! `6 p+ H- N
"bad request",
, I! b  Q% W9 b& i/ q  G; {"seek error", ! A9 Q* x/ a& O$ ]' F4 v6 b
"unknown media type",
& W0 w3 h" G0 x5 b3 N"sector not found",
6 w( i% B3 A! V/ R' e) ?6 ^"printer out of paper",
3 h0 X5 r3 h* L$ K4 ~"write fault", ) Z' p5 b4 H* ]+ O$ Y; p# X4 \
"read fault", 0 Q" e1 ?8 v6 @" t7 W0 u  [
"general failure",
- x: V  b  G1 i3 e& u; n"reserved", " q9 N& f* H/ ^9 ^
"reserved", % g0 M' N% k; o9 f0 Z3 w: X
"invalid disk change" ) K  e3 i" h+ P! Z! Z/ @
}; </FONT></P>2 S. L* u5 t, b5 b! l' s3 S1 Q
<><FONT color=#0000ff>error_win(char *msg) . n3 ?7 O6 J' W
{
+ y7 _) E: T* X! T/ gint retval; </FONT></P>
/ t$ U' F4 `* R' ]<><FONT color=#0000ff>cputs(msg); </FONT></P>
) q% K" |: c* d0 q+ A) ~: A<><FONT color=#0000ff>/* prompt for user to press a key to abort, retry, ignore */
. W$ t* j. N& H7 a0 W+ Mwhile(1)
7 [3 X* u/ A- G) y& Q' k1 q' I% v{ 7 w% z: t; ~. Q# Y, F" z6 \% c2 [, {
retval= getch();
0 c9 \8 ~) m/ k6 D4 @8 m% [if (retval == 'a' || retval == 'A')
0 u+ m/ `' o, q) u2 t' A4 y{
' G# w; c; S0 t) d2 F) t$ Sretval = ABORT; + c( ^5 m& t& @4 D% D" X0 M2 q
break;
, o) ^" v5 H% [, b2 h7 L( O}
- B% t( h3 s* Z$ l7 ~if (retval == 'r' || retval == 'R')
3 r0 x; f/ ^: N  Y% g{
" p( G0 g7 q9 B+ _) b: Gretval = RETRY;
% `" g0 T. o, r' L# d5 y% P0 _* Ibreak; # g* [& j. B7 N
}
  E. u. Z" v# E" `' `# t% aif (retval == 'i' || retval == 'I')
: y0 Y7 J+ i: T{
5 ~. V; \( ^  c$ X9 q2 ~retval = IGNORE; ( D9 |. E; E% i0 d) X
break;
$ Y3 [& \; R2 E2 Q- g' J2 T/ G* k8 f} 0 P7 n$ ]# T' L/ Q
} </FONT></P>/ [# F+ n& j) q  @1 O  V0 a& M5 Q
<><FONT color=#0000ff>return(retval);
- u) f+ y( h2 ?8 c3 T$ R. B6 G} </FONT></P>
/ A: m/ N3 P, R, q. `<><FONT color=#0000ff>/* pragma warn -par reduces warnings which occur due to the non use */ 3 i9 n. B0 Q! R/ U/ p6 \
/* of the parameters errval, bp and si to the handler. */ 0 w% p' T4 {- U3 ~1 f4 z; A3 w
#pragma warn -par </FONT></P>1 c* I/ }" m, S- V  I5 n5 b. P
<><FONT color=#0000ff>int handler(int errval,int ax,int bp,int si) - ~+ L" A! d6 Z3 R) D9 l# S
{ & ]4 v3 X0 U) o8 X$ ~5 K7 s, r
static char msg[80]; : W8 Z$ {4 _0 X$ s
unsigned di;
6 X' ?6 n; H) n, dint drive;
4 }- f$ |. A# q3 t0 Iint errorno; </FONT></P># r+ E9 ]7 l/ ?  Z1 O' ~  l
<><FONT color=#0000ff>di= _DI;
/ X! W7 A4 d3 M& z- ?) m/* if this is not a disk error then it was another device having trouble */ </FONT></P>$ u# j1 l  Q! F9 o+ C
<><FONT color=#0000ff>if (ax &lt; 0)
% H" |9 W) M  R% e& z{ . D  A' N- K9 p! C3 f6 i
/* report the error */ ( @8 E5 q3 l4 g3 E5 ]3 f+ D' n' r
error_win("Device error"); * Y$ s6 f& R3 P+ a
/* and return to the program directly
3 T( v# ?( K, A- Y. X! }requesting abort */
+ ]/ @+ q  c1 U$ thardretn(ABORT);
( V) y8 T: M5 i5 j4 ~6 O} 4 P/ |( w2 |; B" X* C6 T  {
/* otherwise it was a disk error */ 4 [) M5 O$ I' a% |
drive = ax &amp; 0x00FF;
& d' x! X6 K) h8 z" Kerrorno = di &amp; 0x00FF;
! ~1 x- h* \% P0 O* H5 H/* report which error it was */ . P/ U7 b% z4 D$ @. c( K2 o
sprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ",
3 [. [0 @4 v# `4 M( y" herr_msg[errorno], 'A' + drive);
. h% ^) j5 `" \5 a/* return to the program via dos interrupt 0x23 with abort, retry */
& P/ A) }+ v# j) X$ ?/* or ignore as input by the user. */ * S4 N& ]2 a: q+ ?, M
hardresume(error_win(msg)); 4 s! W! g5 M& z+ E/ k
return ABORT;
3 X  |( Y; }  M5 X: f- w4 N/ Q} : c& @* X: k6 b) r) O
#pragma warn +par </FONT></P>( x2 Q3 M( j! v2 A
<><FONT color=#0000ff>int main(void) - |+ _/ e* i* H9 B
{ ) E; E- S1 C# e2 r% U& k
/* install our handler on the hardware problem interrupt */ 0 a8 y& {5 [3 l7 A! [3 R7 T
harderr(handler); # F+ c5 _7 @: W" \: r
clrscr(); $ E9 a" _/ N+ W
printf("Make sure there is no disk in drive A:\n");
3 U" C. t) R% e+ y! dprintf("ress any key ....\n"); ' s1 d) C6 ?" }: M5 G
getch();
3 {! y( T2 a" Pprintf("Trying to access drive A:\n"); ( ?$ E  c0 n6 D5 a
printf("fopen returned %p\n",fopen("A:temp.dat", "w"));
) f' K2 }5 G$ [# ^: }% kreturn 0;
) a9 [: d. u( g0 M- @}
" \0 R. _/ {, ]1 G$ F</FONT>
+ c7 s/ \% |- j2 D' X4 ~$ ~: L) X</P>
- \' Q8 [/ z: w7 r: J2 S' \<><FONT color=#ff0000>函数名: highvideo </FONT>
* F4 ^) G- |0 `6 }" b功 能: 选择高亮度文本字符 0 M# ~& |& K% T& [
用 法: void highvideo(void);
) V6 K7 I0 \) f: c5 y- h. R1 W程序例: </P>
- l- o6 b/ \7 q# s) Y& E<><FONT color=#0000ff>#include <CONIO.H></FONT></P>
- o/ ~7 x/ R" J<><FONT color=#0000ff>int main(void) 9 f5 C8 Q) r1 u
{ 6 r6 i$ R% E: ?4 U1 E) m' h8 P
clrscr(); </FONT></P>
& q  z, _8 V0 s9 P- [<P><FONT color=#0000ff>lowvideo(); ( y; g0 A2 h/ {, [( d! U
cprintf("Low Intensity text\r\n");
; p* V/ Y) {, Mhighvideo();
% H& Y, v; {; O& tgotoxy(1,2); 0 a6 W7 R5 R4 y" ]! z0 S7 G
cprintf("High Intensity Text\r\n"); </FONT></P>
" F0 l# S# e9 ]- Y  u" c<P><FONT color=#0000ff>return 0;
/ L3 {% ^3 Z' W} ' Y2 f" {0 N: e* `, s
</FONT>) u5 `+ u# H7 Q( H  H/ u9 ^
</P>4 y6 A1 U% a+ I  [- \" I
<P><FONT color=#ff0000>函数名: hypot </FONT>  ]5 s: J. Q# R: x
功 能: 计算直角三角形的斜边长 + ?9 \/ W1 {1 z& g- ^9 `
用 法: double hypot(double x, double y);
+ `! C# X" x) Z7 }( E. |程序例: </P>2 e5 c' y8 c2 x0 V5 T
<P><FONT color=#0000ff>#include <STDIO.H>
2 [% I8 J0 e" ]) V8 e4 M#include <MATH.H></FONT></P>
" ~. y* X0 d  c: z: x. e<P><FONT color=#0000ff>int main(void) " y! v* I: p7 }8 B2 S2 Y1 T0 o: u% e
{
7 W+ }; m8 n: E4 I  L! D) Sdouble result;
, }8 e) v/ f3 G- j  F2 v# Q& udouble x = 3.0;
8 N- ^! ], K* w9 g4 Y5 T# e0 B& ?double y = 4.0; </FONT></P>
$ T5 S& \3 T' ^/ O/ n2 m0 q0 @<P><FONT color=#0000ff>result = hypot(x, y);
& N$ [' `, {4 U9 J5 kprintf("The hypotenuse is: %lf\n", result); </FONT></P>
5 w% ~- f3 ^5 p. y6 v# {4 H<P><FONT color=#0000ff>return 0;
# z) y- F- _) Z: _- V* ^} </FONT>0 o8 e# n9 g& o: i$ W. I! `
</P>




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