数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 02:58
标题: 函数大全(k开头)
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(k开头)</FONT></B></FONT>
/ @- Q- W" M- X$ Q</P>
; H  B" \' q; _, H<><FONT color=#ff0000>函数名: kbhit </FONT>+ Q8 H0 G; j$ u# _: z
功 能: 检查当前按下的键
. X9 c" v& M" b: N. {  J0 L用 法: int kbhit(void);
) m, x$ q+ O; V& E9 o4 |4 t程序例: </P>2 l; S* b% `1 k, d1 h5 E) Q
<><FONT color=#0000ff>#include <CONIO.H></FONT></P>  H8 T/ b  l8 l$ t/ F) y
<><FONT color=#0000ff>int main(void) 7 `! T. e! J) l) |
{
* v7 a7 `5 y& n; ?7 acprintf("ress any key to continue:"); # [: S% W! {4 R3 P# x
while (!kbhit()) /* do nothing */ ; 5 l  Z9 j9 w5 a" E
cprintf("\r\nA key was pressed...\r\n");
$ ]4 {2 B  B7 \: y9 p. I/ d. S; `- P9 a- preturn 0;
# u4 f$ s0 }; w0 v} </FONT>
) q6 V0 s1 g$ K& Y2 i% J</P>  q) c. k2 ]  _- e  a& X& L1 E
<><FONT color=#ff0000>函数名: keep </FONT>* G0 `1 t  u3 j5 ?( S) w
功 能: 退出并继续驻留
! B7 D6 Z% [: c0 x" C; Y' s用 法: void keep(int status, int size);
- b4 I' I/ }8 G6 e程序例: </P>! o- v/ E. e7 F3 s" |2 Q
<><FONT color=#0000ff>/***NOTE: - A' E, {) j' S3 c. s
This is an interrupt service routine. You
1 p' s7 f# Y8 D6 i, Ocan NOT compile this program with Test 5 V0 _! h4 Z5 O% P- n: W
Stack Overflow turned on and get an ( i) d5 ]5 p" c3 j; n* `
executable file which will operate 0 ?! L& P$ T/ V. [* t
correctly. Due to the nature of this 4 h1 J( \0 [; Q3 @( ?
function the formula used to compute 0 l& p$ \! l% I9 q
the number of paragraphs may not * G( L- K- t' c9 Q" Z8 r* |6 A
necessarily work in all cases. Use with
  N. u9 z9 ^! t6 ?7 w# ~* n  c0 l" ]- Zcare! Terminate Stay Resident (TSR)
# A% W. Q9 }& ^4 a' w, d- F7 Iprograms are complex and no other support - a, s; i5 l( g# k6 c1 E
for them is provided. Refer to the
9 R$ d& p3 o$ D* a  KMS-DOS technical documentation - u  I- H$ ~6 S6 M
for more information. */
! r  C* K8 e" T. Q+ ]" A: P#include <DOS.H>7 n5 ~, V; W! A8 g4 B6 t
/* The clock tick interrupt */
- Q( r: U% l9 s! I4 f* G% `9 A( @#define INTR 0x1C
& x7 t( _5 a; Q$ G/* Screen attribute (blue on grey) */ 5 G: _5 y) |( q
#define ATTR 0x7900 </FONT></P>* S! v+ R: f  J+ L# B6 V. \' P
<><FONT color=#0000ff>/* reduce heaplength and stacklength 6 n. z+ M% `# n8 ]  C! S7 b
to make a smaller program in memory */
0 D# g/ h9 |; D6 P# V4 T( q* mextern unsigned _heaplen = 1024; 9 f: i; X, y3 V7 J& I, D
extern unsigned _stklen = 512; </FONT></P>/ h- y7 _; y9 C: x! c
<><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>- c) j& G3 L& T0 p: K. T
<><FONT color=#0000ff>void interrupt handler(void) 0 Z; r4 i7 ~9 k
{
) Q# C% O5 l  a6 Y  S! _  Xunsigned int (far *screen)[80];
) o# \$ c5 T" J" ]) o! Lstatic int count; </FONT></P>
( O& w) x7 B' T# A<><FONT color=#0000ff>/* For a color screen the video memory 6 |! c. J4 r, V& M# W$ w
is at B800:0000. For a monochrome
$ E+ p" [4 o$ K5 Z0 ~: u% E; e5 D9 o2 ~system use B000:000 */ ! v! H6 T2 ]6 I, k/ @2 P4 y' l5 _
screen = MK_FP(0xB800,0); </FONT></P>! m- \3 F  X" Y8 X& a
<><FONT color=#0000ff>/* increase the counter and keep it ( S$ s; ]2 N- h0 J4 }
within 0 to 9 */
' h. ?% b- w7 y. gcount++;
* `8 j/ _' O, W" \9 Bcount %= 10; </FONT></P>  C0 @1 H9 y! @
<><FONT color=#0000ff>/* put the number on the screen */
3 ^7 h6 d+ W" u# D" lscreen[0][79] = count + '0' + ATTR; </FONT></P>
! `  L4 D% r7 _" K7 `( k8 `: l<><FONT color=#0000ff>/* call the old interrupt handler */ 3 P/ S) A7 C0 `6 c
oldhandler();
2 E( b; e% w( g! n} </FONT></P>5 ~4 D& Y7 }/ v9 Q  K3 B# v+ l
<><FONT color=#0000ff>int main(void) 7 N4 w; D' |3 D8 n0 e! ~: @
{ </FONT></P>8 v, @! J. a3 H2 k% L2 [! h/ v
<><FONT color=#0000ff>/* get the address of the current clock
1 B- ~& q4 [! E) [6 ?tick interrupt */ + R7 f+ J1 n/ J8 E
oldhandler = getvect(INTR); </FONT></P>
  D3 ?5 D! H5 g5 Y$ p<><FONT color=#0000ff>/* install the new interrupt handler */ ( I$ v9 C" X" n) o
setvect(INTR, handler); </FONT></P>
# W# m2 f( A; Q<><FONT color=#0000ff>/* _psp is the starting address of the
$ L/ A8 s& k& zprogram in memory. The top of the stack
4 G$ k0 I- H! ois the end of the program. Using _SS and . H8 U, u& ^! |- b+ g2 R9 H4 Y
_SP together we can get the end of the
* l* Z, T8 A- d8 i. Sstack. You may want to allow a bit of 1 {. V* p, U* P' Z+ G( t
saftey space to insure that enough room - }: u4 o7 S- R5 P' N
is being allocated ie: 1 E9 e/ q& J2 R( j) {% [9 Z, H
(_SS + ((_SP + safety space)/16) - _psp) . F/ |) O0 `, I& [+ l3 j
*/
3 Q4 C6 ]7 l! q! t3 mkeep(0, (_SS + (_SP/16) - _psp));
2 h  z8 E$ j& I; Z& Oreturn 0; ) w  \) S9 L6 G# ]8 Y6 h
}
- y& c- o& s( i$ _4 Q0 R/ `</FONT></P>% G$ ?- S& l5 G! u

& o$ P+ n! q  J' P* p% e& W7 t
% Q* t: y7 P! _9 t<><FONT color=#ff0000>函数名: kbhit </FONT>
1 m& i6 O; o. H# c功 能: 检查当前按下的键 9 K: J% y) x/ Y$ Z- R
用 法: int kbhit(void);
/ L, k! u5 B- ^  J5 u程序例: </P>9 G! N1 h+ x3 o& K% ?' R
<><FONT color=#0000ff>#include <CONIO.H></FONT></P>
* U9 u. N- _$ i/ {! E- D<><FONT color=#0000ff>int main(void)
- j4 r* d  U" Z$ }6 h6 s( C. X{
5 ]% `/ l8 d, y# M6 rcprintf("ress any key to continue:"); , g* a) s2 P4 t! r
while (!kbhit()) /* do nothing */ ; 9 m9 |: q9 j% ~7 Y  k0 o6 f% A, K
cprintf("\r\nA key was pressed...\r\n"); 2 \3 |: Q5 `# o% g/ P7 J6 O, l% Q
return 0;
% U+ w  m6 Q0 `3 s, F3 @}
: @/ A6 L8 @3 t) K3 T6 x
. E; T/ v7 U" |. B" ^/ M) F' M: I) @! Q& O& K
</FONT></P>8 P% W9 m" R. _. H$ x, u
<><FONT color=#ff0000>函数名: keep </FONT><FONT color=#0000ff>" t1 ~1 [, M. L& x$ E3 k  F
<FONT color=#000000>功 能: 退出并继续驻留
  Z2 i& H1 [& i2 v/ y2 F" K: R1 p  G/ e用 法: void keep(int status, int size); 6 a7 T; E/ m5 S" u
程序例: </FONT></FONT></P>
. e2 ^. b, G! }<><FONT color=#0000ff>/***NOTE: 0 q; `; S# z: H! K% _) G! ~! K
This is an interrupt service routine. You & e& o, q" @0 {% r+ N4 V
can NOT compile this program with Test # ~! Q' C. f  A1 ]9 z
Stack Overflow turned on and get an
! j7 {4 P" u6 X$ A5 Y" ]# x8 B. lexecutable file which will operate % B! |& A- w9 a! Q, X4 R
correctly. Due to the nature of this , v. J9 {& ?/ T+ p4 c, p* L
function the formula used to compute
+ Q) L, h1 I, T" F0 Dthe number of paragraphs may not
* g3 i, Q2 K) |, ynecessarily work in all cases. Use with
" O3 ~0 a& Q/ N: A2 a7 J5 ucare! Terminate Stay Resident (TSR)
+ d3 r, r) K+ ^# a) fprograms are complex and no other support
- ~+ v; S9 J$ \$ Z$ ~. ffor them is provided. Refer to the
) j4 J5 Q- o  R' d4 C# JMS-DOS technical documentation 8 a1 n  g( t5 w/ k0 \! G8 P! N
for more information. */ 6 S; e- C9 ?/ r% w
#include <DOS.H>, e6 o( }0 o" c4 A8 X' a4 Y, n
/* The clock tick interrupt */
& u; l0 b( K+ b) d3 C* W) m#define INTR 0x1C
4 a1 e$ R( f1 e! |$ y. R/* Screen attribute (blue on grey) */ $ ~, Q, y' O5 @
#define ATTR 0x7900 </FONT></P>  J7 }# c( A, o( [. W# U" j7 ?
<><FONT color=#0000ff>/* reduce heaplength and stacklength
3 q; c  M8 ]  x7 w0 ~to make a smaller program in memory */
  {' k9 o6 S  F- [: `extern unsigned _heaplen = 1024; 7 E, ^% U9 T3 ?- n7 y$ G$ k7 }. T
extern unsigned _stklen = 512; </FONT></P>
" t+ z# E# }- A% e% n  i8 g3 Y<><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>
, C, `0 n) Z  r7 `2 \4 H' N. Z<><FONT color=#0000ff>void interrupt handler(void)
. X/ ~8 A6 B. o% _{ + E5 g7 E" c& f. n
unsigned int (far *screen)[80]; 5 v3 B) {0 t; @
static int count; </FONT></P>
1 i. ]3 d& U3 Q% b<><FONT color=#0000ff>/* For a color screen the video memory * _0 Z0 c) P9 e3 ]1 s% {& d
is at B800:0000. For a monochrome ) ~8 E6 K" T( T% ~2 T
system use B000:000 */
$ M( ]& T4 @2 e( n2 U7 \: Mscreen = MK_FP(0xB800,0); </FONT></P>6 y3 d* S( ^, z- c$ _/ V
<><FONT color=#0000ff>/* increase the counter and keep it
$ H5 H+ i! Y2 R8 t$ o$ [within 0 to 9 */
( C! H, [2 L6 W" r( ?count++;
* H7 b9 C/ ^5 qcount %= 10; </FONT></P>( j! t. T' S! l! i
<><FONT color=#0000ff>/* put the number on the screen */
  [- I$ L. T! T. Q3 sscreen[0][79] = count + '0' + ATTR; </FONT></P>
8 j1 h4 y+ k& L+ i$ }7 _<P><FONT color=#0000ff>/* call the old interrupt handler */ ! I) \0 X# X  l( k9 ?7 N
oldhandler();
3 l1 H: L+ `# r6 ]% @6 b} </FONT></P>' z3 @6 x8 L) ]  V' o2 M$ Y
<P><FONT color=#0000ff>int main(void) " h" r' L: Z9 Y1 y, f
{ </FONT></P>
* s' @' P7 f) P8 q<P><FONT color=#0000ff>/* get the address of the current clock 2 H  A- c2 n" r, g" w3 g
tick interrupt */
& \$ ~" M3 K7 r2 w$ n- ~* G0 c6 Ooldhandler = getvect(INTR); </FONT></P>
# {$ K8 s5 W1 u$ T  H. K<P><FONT color=#0000ff>/* install the new interrupt handler */ 8 p: n5 k4 }: p$ ]% e
setvect(INTR, handler); </FONT></P>% _8 ?7 O* c" M; _
<P><FONT color=#0000ff>/* _psp is the starting address of the
, n, H7 T. ~' T0 {program in memory. The top of the stack
" j& X- q6 F$ G8 kis the end of the program. Using _SS and
- L) s! W/ C5 I! b* |/ j_SP together we can get the end of the
6 \! |- s2 H7 K; O% Y% ^stack. You may want to allow a bit of 4 f: H0 a- o, G4 C
saftey space to insure that enough room 6 T5 T# [- S7 _3 Q$ M
is being allocated ie: 4 A5 [) C$ g& Y, R& a* ~) ~7 a1 a
(_SS + ((_SP + safety space)/16) - _psp)
: c% ?9 t# G( g" u3 a9 w9 a*/ - e1 z  l: P. L2 L
keep(0, (_SS + (_SP/16) - _psp)); 2 {2 O; I9 c+ w
return 0;
. r' C0 r/ r, |}</FONT></P>




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