数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 03:00
标题: 函数大全(h开头)
<><FONT color=#ff0000>函数名: harderr </FONT>: p' t, N! C& h! Y5 M
功 能: 建立一个硬件错误处理程序 ; r  O* E5 K4 a% T- O5 }7 t$ R
用 法: void harderr(int (*fptr)()); % L7 v, i2 C! S( n3 G8 o( y
程序例: $ o+ Q9 n1 {! o7 V# T" f
<FONT color=#0000ff>/*This program will trap disk errors and prompt . S7 M4 I  Q( y9 j$ p
the user for action. Try running it with no
# n# {  q/ `6 K# O/ wdisk in drive A: to invoke its functions.*/ </FONT></P>2 F, E1 U1 n, {! h
<><FONT color=#0000ff>#include <STDIO.H>- a% E. W4 W* n/ J" Q: Q4 G& B
#include <CONIO.H>
3 J; ]6 ?, e$ _* M4 _8 t& l#include <DOS.H>
" a9 G+ b9 s7 u#define IGNORE 0 7 x! C+ h6 F, t7 H
#define RETRY 1
" k" h4 O: r% s9 q' i#define ABORT 2   z, Q- Z) f/ E  z3 y2 \5 z6 T' E
int buf[500]; . Y7 `2 k1 r. u  Y5 ?( \
/*define the error messages for trapping disk problems*/ - U) r( V6 q) O1 H
static char *err_msg[] = {
5 Q( h4 w* l# g8 o. y; T"write protect", 7 W9 S2 N  w* K8 n
"unknown unit", + t9 a/ v# B4 X0 K  i# M  ^; k
"drive not ready", 8 W5 C7 O: y* A* i" D, D* o/ _
"unknown command", ' A5 b  O" k/ A; J$ ]
"data error (CRC)",
- X5 o3 {3 S1 v" X. e0 \"bad request",
( D2 h! {8 a5 G9 i$ o"seek error",
6 b7 z, S7 T1 r, D# t' @( M$ J"unknown media type", ; i9 e& q7 p8 h% L# [
"sector not found", : J; v0 \4 N  U! [/ d; M
"printer out of paper", / g  x- t7 I& {2 B
"write fault", ( ^1 b1 K# N8 x3 E; U8 i
"read fault",
' K1 p$ _: `1 u' z/ Q  N- R0 ^"general failure",
/ i6 A" ^0 N, x; ~1 `3 o"reserved", , L3 x- X+ H+ x
"reserved", , n0 t& }8 u5 f' U  A0 _
"invalid disk change"
: L0 b. |6 ~) n0 @- g1 Z( o5 R; ]}; </FONT></P>8 Q! b7 Q2 y* a( L' ?( G
<><FONT color=#0000ff>error_win(char *msg)
3 ~2 a: k8 D* c# c{
* h. W& M' X. M- Uint retval; </FONT></P>
& y+ b6 ^: Y1 h9 n3 u- H4 A( g; m<><FONT color=#0000ff>cputs(msg); </FONT></P>3 {9 U- n' A9 N- G$ x
<><FONT color=#0000ff>/*prompt for user to press a key to abort, retry, ignore*/ * b- M7 S8 E  Y7 e
while(1)
9 C; K; \$ Z! x2 d' k, L$ x{
* S- i. l9 H3 ]' q$ j' g2 dretval= getch();
2 t" Q" O) u2 [! {& `/ U: Uif (retval == 'a' || retval == 'A') ; [! l4 e8 s  v' K4 ]! `( ]
{ 3 P- R5 O# d9 R7 g6 \& M
retval = ABORT;
* v  ^; _. K" p9 s; F, C, `break; - m+ Y0 h1 {/ i" L: x: E- P
}
9 j' [  o- T: `0 \7 C/ d" z8 K8 vif (retval == 'r' || retval == 'R') 2 Y/ u" j4 E5 b1 X7 v5 N/ p
{
& {  Z: J( M, B% Tretval = RETRY; & D) \- R9 M, b+ [( ~8 t
break; / l" m; ~0 ^9 {5 S
}
" D* o4 f3 f1 \if (retval == 'i' || retval == 'I') 9 y% v/ ^7 t) Z% c4 h& V
{
! q8 \. `7 D3 n. Lretval = IGNORE;
- V. ]0 h; \7 z( Bbreak;
3 `7 ]1 l: d1 k. Y} 2 U+ |' n/ w) R9 @" k/ x3 W
} </FONT></P>
% U4 ?  n6 j  C0 O$ T/ e; x" B<><FONT color=#0000ff>return(retval);
, R( ]+ a0 I" |* \+ m} </FONT></P>
# ]* z  M. U, I6 K<><FONT color=#0000ff>/*pragma warn -par reduces warnings which occur
! D# T9 Y1 q- Z' I, q! U/ h3 udue to the non use of the parameters errval,
1 A4 e3 a' X) ~$ G1 p" ~% f8 ~bp and si to the handler.*/ , I, ?! e( R9 k9 }& P+ L
#pragma warn -par </FONT></P>
0 E+ |7 x3 X8 J3 s<><FONT color=#0000ff>int handler(int errval,int ax,int bp,int si) $ S5 f8 R- y3 G: \& n$ w, m
{
# P/ \2 h/ S4 T$ E& z( _static char msg[80]; 1 P! M& w  g" e- L1 ]: e
unsigned di; ) X% h* Z% E! i1 @8 q' W
int drive;
& ]/ e  u  f4 v$ wint errorno; ) i, D' [/ u' ?
di= _DI; 2 X9 T# s0 x% R9 ^6 g
/*if this is not a disk error then it was
) M1 J" c! ~# X. Yanother device having trouble*/ </FONT></P>2 x1 |; w6 s+ r9 b+ _
<><FONT color=#0000ff>if (ax &lt; 0) ( t( Y+ t/ C# U
{ ; B5 [7 G1 W. f( A& v
/* report the error */
  B. k1 r- G9 b0 Verror_win("Device error"); $ B. p- o, C- w6 K' J
/* and return to the program directly requesting abort */
3 k+ T4 |8 s6 e2 v: ~hardretn(ABORT); 3 ^5 C( k. }2 X& e
}
) d% k$ g8 \+ b9 d9 d% P$ `3 {/* otherwise it was a disk error */ : e8 w# W5 ^" Y6 ~( e
drive = ax &amp; 0x00FF; 2 Z. R0 E( |2 Q$ Y4 ], z
errorno = di &amp; 0x00FF; ! Y$ _. R7 D" H( n9 F5 F
/* report which error it was */
1 a; [* l6 `: Z0 Y$ ?+ o; n0 lsprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ", # O( e# y( r6 \$ \( k' E) b3 k
err_msg[errorno], 'A' + drive);
8 \  a' Q; y) P4 ]: N0 k/* ' ~' W1 Z; A% x; J7 a0 V
return to the program via dos interrupt 0x23 with abort, retry,
/ a$ u  r1 P( H0 H4 d1 `or ignore as input by the user. 8 I- ~0 p/ Y" J+ d6 W  f0 B$ m/ H6 |
*/ * E8 p% J9 q2 P
hardresume(error_win(msg)); 2 J- L9 o$ [. }5 N& C5 `
return ABORT;
( _; e' s0 {1 ]% Y# C" ]. n+ e2 z+ O; i} 5 D, c9 T0 p9 L, G% u" Q
#pragma warn +par </FONT></P>" N1 m& E0 m' o2 x0 X
<><FONT color=#0000ff>int main(void) 8 y6 l; o7 G* O: q9 G& U
{
6 M7 T6 v5 v; b  I; X/*
  D- G  K' E' ?5 ~install our handler on the hardware problem interrupt ( y4 _1 e5 j/ w' H0 B8 @9 N
*/ : x5 }1 f, J( r. u% |8 d' w: Y
harderr(handler);
& d& U+ T" ]) d) x7 K0 f% i8 `0 jclrscr();
( M* ?! X9 b6 i. hprintf("Make sure there is no disk in drive A:\n"); # j) W" G% I9 ?/ |; m+ d& H
printf("ress any key ....\n"); 7 O% e" l9 N6 q% [
getch(); 3 s; K5 k! V; p+ V
printf("Trying to access drive A:\n"); 6 W4 Z, R' l! K% u
printf("fopen returned %p\n",fopen("A:temp.dat", "w"));
2 a5 a( Y5 ~3 r3 K3 Q6 Freturn 0; & M5 u& S+ [' t# `9 m
}
' M# p3 D" ~1 i* @</FONT>5 |/ x8 T' X4 z1 t9 I4 j
</P>
* ^. K' s/ a/ d3 M<><FONT color=#ff0000>函数名: hardresume </FONT>
9 s0 S2 r2 W3 W- K& z0 j- P9 Y功 能: 硬件错误处理函数 - i1 |/ K& |3 N" H1 l
用 法: void hardresume(int rescode); 0 @$ y' Z! m0 q! a( r; T7 Y
程序例: 9 R$ }; b8 E3 r3 [+ w% R
</P>/ r/ n4 i! U  M# ]( R- q
<><FONT color=#0000ff>/* This program will trap disk errors and prompt the user for action. */ 8 \, P9 R% f+ w/ m2 j
/* Try running it with no disk in drive A: to invoke its functions */ </FONT></P># o( x: q* z! t( f
<><FONT color=#0000ff>#include <STDIO.H>
/ Q* I0 I) m" @9 c& f4 L#include <CONIO.H>
8 a6 W7 _7 y4 o& h8 i! v#include <DOS.H></FONT></P>3 |- W/ q( S6 e9 \2 X, d( P
<><FONT color=#0000ff>#define IGNORE 0 4 P8 o/ }- {- ]. x. ~' O3 T) o6 K) @
#define RETRY 1
1 y8 q4 }: S. h& q; Q3 f% _#define ABORT 2 </FONT></P>
, z  C* S5 Y% t3 n2 n<><FONT color=#0000ff>int buf[500]; </FONT></P>
& _  d  S( ]9 M: L, v<><FONT color=#0000ff>/* define the error messages for trapping disk problems */
" K  r5 m2 ~% n& n# Tstatic char *err_msg[] = { ! |- g) }. m- k9 s
"write protect", * K7 A& ^# w6 x* z0 u1 l; s
"unknown unit", 8 U: j8 s5 O4 f. W2 E8 Z
"drive not ready",
* w  ~3 L5 j; G2 I, j3 K6 I"unknown command", $ R. }% h% c  x+ Y0 ?$ }
"data error (CRC)",
2 p! B7 h/ y' C; {"bad request", : j0 d( ?& I& ~8 [" R$ `, L: y
"seek error",
$ \4 X" I3 H; X2 I+ S7 l4 }0 l"unknown media type",
* w. d( ^8 m% G0 z- o"sector not found",
: x& A3 c1 \0 O: z5 d* A- i" F3 l"printer out of paper",
7 ~  Z5 F3 F9 G$ j' H0 Z5 e"write fault", , N' H$ l/ E: Y0 ^6 t
"read fault",
, x+ H1 |- [# r+ e  I" t"general failure", 9 h4 u" H# Q/ A5 c, `4 e2 d
"reserved", / ~0 C$ G  [9 ]7 [* `& e
"reserved",
, x3 N( ~" ?( q. t"invalid disk change" ' U. R: @5 ?7 H; M: V; N
}; </FONT></P>! N0 Y6 z, t  k* d# w* I2 ?
<><FONT color=#0000ff>error_win(char *msg) . t' N; K5 H6 N( I
{
; e; v' d" {# e+ A( T7 `: X$ eint retval; </FONT></P>8 ]9 @: m& y# `( F0 b
<><FONT color=#0000ff>cputs(msg); </FONT></P>+ M& |( Y4 K: I" n
<><FONT color=#0000ff>/* prompt for user to press a key to abort, retry, ignore */ 2 _' B8 d3 `0 l/ l5 \7 C; q! h( U9 Z
while(1) , N4 g: N3 g; d3 m9 L' Z+ ?
{
! U$ T7 f% ~4 A/ Vretval= getch();
8 t, A5 S  r) M0 wif (retval == 'a' || retval == 'A') : \4 H$ K6 e! Y5 p1 j% `
{ / X9 F0 \4 {+ q: h( C4 I
retval = ABORT; . S+ ^6 S  W4 m0 T5 S& j+ i! {  M
break;
( z! D$ k# R9 E7 z}
* n6 X2 A8 s5 K: q2 y" Nif (retval == 'r' || retval == 'R')
/ G$ O6 l. \7 h# v{
. }, ?' Y& _# U/ R" V+ I( sretval = RETRY;
/ y3 u2 A1 E9 P2 M8 mbreak;
3 @& o7 ^5 V* v5 i' {}
  X  ^' Z2 U: s# y5 _if (retval == 'i' || retval == 'I')
3 f. w9 l& W0 V( P4 L$ }{ 3 ~0 Y. P7 n  H
retval = IGNORE; 5 h, b# w8 P/ @! _
break; 7 m5 c- N( u" M
} & }+ C0 r6 ^, _& A0 X8 J
} </FONT></P>
* |; Q8 N1 q; Z<><FONT color=#0000ff>return(retval); & t1 p3 ~6 O4 E* K9 z* ]/ U, [4 N
} </FONT></P>
# |4 t$ Q3 \, K5 M<><FONT color=#0000ff>/* pragma warn -par reduces warnings which occur due to the non use */
! R# d: @1 Q) L( B/* of the parameters errval, bp and si to the handler. */
6 y' y: F  r! p$ a5 W+ L4 g  f#pragma warn -par </FONT></P>
* j  e& R: C) _<><FONT color=#0000ff>int handler(int errval,int ax,int bp,int si)
; i+ E0 k  A( q5 w* a{
; ?8 }1 l8 l5 M. k: r. O4 Nstatic char msg[80];
6 E! E: n2 `+ q8 x/ V# w: ?( Wunsigned di; . v* y" g$ @: b9 g& e6 g  ]
int drive; 4 O+ I6 V" N+ s5 B, y9 L1 _6 a
int errorno; </FONT></P>
# x$ |8 C9 X3 ^* S+ @<><FONT color=#0000ff>di= _DI; + {3 M* t7 \/ V2 W. W0 f4 o$ a) f
/* if this is not a disk error then it was another device having trouble */ </FONT></P>2 q& T( v  c' a' S% o7 f6 X1 K
<><FONT color=#0000ff>if (ax &lt; 0)
  p9 L* R  J% n5 @: R{ ; t& J1 X$ k+ }. x! U
/* report the error */
4 P: P0 t: X7 L4 h! Derror_win("Device error");
: ]. H* h$ Y9 I; G; E. K- {1 u/* and return to the program directly
0 M1 n$ ^6 \' n& Orequesting abort */ : `0 h* Z# t  b
hardretn(ABORT); 3 ]5 p( L( e* e8 ?: y* C" x
} - i1 k. L: X( s1 m" u
/* otherwise it was a disk error */
- l- U7 y7 `3 ?( t9 qdrive = ax &amp; 0x00FF;
4 O. \5 N' R1 ~$ Gerrorno = di &amp; 0x00FF;
) s+ m' X1 q. D0 ~# v/* report which error it was */
: e, D7 ^7 P6 e$ D* usprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ",
5 g; O9 `; o' [1 perr_msg[errorno], 'A' + drive);
2 Z1 k1 v" ]# e5 V/* return to the program via dos interrupt 0x23 with abort, retry */
4 n0 f& ^2 e' W1 Z9 a0 K9 c. e. f5 h/* or ignore as input by the user. */ % e& y4 \3 L6 ]& ?
hardresume(error_win(msg)); : O6 A+ |* I3 L0 I2 P% w, E9 M
return ABORT; $ m1 V- }3 p  }* i$ [; \7 x! B
} $ c& L5 I3 E0 d5 G" n
#pragma warn +par </FONT></P>* r! i/ ^; t, R6 V3 d6 s
<><FONT color=#0000ff>int main(void)
* }& V9 q& h3 v! g5 Z{ 8 f3 t- d* n& ^6 Q
/* install our handler on the hardware problem interrupt */
+ m; J6 L) _' N2 s+ Oharderr(handler);
9 z* F2 Q$ \2 }5 pclrscr(); 0 H/ o0 t0 c. _& b  b
printf("Make sure there is no disk in drive A:\n"); 6 n8 R8 q. f- D; V
printf("ress any key ....\n");
5 K, G8 g) S7 c6 S' G/ I2 Tgetch();
) a* D( |5 |" Uprintf("Trying to access drive A:\n");
+ p! F- y8 N8 p& H$ r. a1 cprintf("fopen returned %p\n",fopen("A:temp.dat", "w"));
5 z$ L/ C  p. M/ ~" Xreturn 0; 6 P' a  ]5 Q' _# P* |) O# H2 b
}
) v' G) y/ G; ^/ X7 q</FONT>) X/ j' C& P" J, ~" x; k/ \
</P>9 w0 S! f7 k4 Y8 ^
<><FONT color=#ff0000>函数名: highvideo </FONT>
% n0 a- b1 A6 Y功 能: 选择高亮度文本字符
# d- |! X$ c: P6 {/ ^/ l: I用 法: void highvideo(void); # F  e: r# `4 Z8 _
程序例: </P>
# D9 a6 K- P3 ~) E  `# i<><FONT color=#0000ff>#include <CONIO.H></FONT></P>
+ U; W& y/ O. V2 D<><FONT color=#0000ff>int main(void)
5 {3 c4 {& L& j{ " Q* X# b2 K# K' [/ r) ~" Y
clrscr(); </FONT></P>
( ?/ D3 i* \( \7 T# @7 h5 g<P><FONT color=#0000ff>lowvideo();
9 p- d+ h8 y* Dcprintf("Low Intensity text\r\n");
1 t, T! y4 I, s- C3 ~highvideo();
6 M; z5 W/ E& ygotoxy(1,2); + u5 a! [( [( N) M
cprintf("High Intensity Text\r\n"); </FONT></P>, C' N) u3 |+ q8 v9 e. @
<P><FONT color=#0000ff>return 0; 0 r; u! G7 H$ P/ _: b, |5 U0 d
} 0 L* I. q5 P# P8 q8 k& s  I; ^
</FONT>
: ^- ?1 K( X( y- A</P>
) U' e% L' k8 D( O<P><FONT color=#ff0000>函数名: hypot </FONT>7 i$ P9 x6 J/ l  |# ~
功 能: 计算直角三角形的斜边长
% y) T$ N; |. Y( I0 _$ ~用 法: double hypot(double x, double y); ) p: q$ _8 p% x2 V
程序例: </P>8 r. [& f8 a1 D" ~8 @
<P><FONT color=#0000ff>#include <STDIO.H>  }% e' N9 z; ?8 o  N! c* k, K
#include <MATH.H></FONT></P>
" H' k  z1 h" w% `7 i<P><FONT color=#0000ff>int main(void) 1 U& i* P( U$ x3 x8 a
{ 7 _& y( J2 U1 T; T
double result; $ s  Q1 ?7 w; O2 d" Q' `4 {
double x = 3.0;
" b/ Q' q* a* J  D' A* Qdouble y = 4.0; </FONT></P>. o0 C' w, P, y# {
<P><FONT color=#0000ff>result = hypot(x, y); 4 b9 W0 H- t! I9 Q1 k
printf("The hypotenuse is: %lf\n", result); </FONT></P>
' ]; V+ q. f) C  {: A<P><FONT color=#0000ff>return 0;
" a( _; f! @' D& z3 t} </FONT>
( N% G; D& s  D+ K! W4 }$ k( R: s</P>




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