数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 02:58
标题: 函数大全(k开头)
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(k开头)</FONT></B></FONT>
" J" {0 J5 l0 {$ F/ W* ?* a+ c. r1 Q</P>
' i) }* ^1 a+ m# V5 q6 D<><FONT color=#ff0000>函数名: kbhit </FONT>9 c8 `8 h, Y+ B' f8 ?7 [
功 能: 检查当前按下的键 1 t$ _, J( l. w) J. R! U1 I/ i4 Q
用 法: int kbhit(void);
1 k! [  ^! X/ P程序例: </P>
' J" j, @  V# @7 t- D6 R3 a  \7 E<><FONT color=#0000ff>#include <CONIO.H></FONT></P>4 t; P  |5 f% s$ P9 t1 b6 W! i
<><FONT color=#0000ff>int main(void)
2 |- C! P2 p/ f: ]3 ]{
3 R' R7 V3 Y# N( r7 _% {; Hcprintf("ress any key to continue:"); 3 S4 w4 ?( c. a- t" q% q8 t. O
while (!kbhit()) /* do nothing */ ; / _% O4 K5 n7 C
cprintf("\r\nA key was pressed...\r\n"); / C' Z4 [3 Y( `
return 0;
7 I6 l! o# W# O7 e3 P  Y1 O} </FONT>
" O( J! e/ k9 ?4 ]$ w</P>
7 {% J  k; @$ u. R<><FONT color=#ff0000>函数名: keep </FONT>( ]4 L, O, f4 W# r, N1 Y9 b
功 能: 退出并继续驻留 ) v# g: C. O* M; x( E1 |
用 法: void keep(int status, int size); ' \$ e. F# W; v. `+ ]5 [4 I5 a
程序例: </P>" K4 [8 D2 q, w7 E' W
<><FONT color=#0000ff>/***NOTE: " q- f! l/ J. q5 y# }& b
This is an interrupt service routine. You
$ S. z6 S! M1 r8 \+ ~& Gcan NOT compile this program with Test : y" Y4 J4 u0 r+ J, q$ l: c% i3 l6 S
Stack Overflow turned on and get an ; y3 H$ L8 \+ y) Q
executable file which will operate % y8 M; K' c, I; t
correctly. Due to the nature of this
, l( v9 ?4 M/ \4 |; c: [+ sfunction the formula used to compute 2 c! F' {% L$ R. O) P; w  C: I: _
the number of paragraphs may not
; X6 J# h( X) K6 w& \$ hnecessarily work in all cases. Use with 1 w% J3 l* O/ d1 c8 C% H
care! Terminate Stay Resident (TSR)
. d1 X* v/ J8 X8 o7 Nprograms are complex and no other support 3 M1 H9 @7 X* L; L6 z% ]
for them is provided. Refer to the
  b5 _& g0 ~& Z7 {MS-DOS technical documentation
" _# P3 K) y; u5 dfor more information. */
" @+ v* o) {/ b( v! q- ]; ]#include <DOS.H>
0 Z& x2 X% \/ Q  U4 G) ~0 R/* The clock tick interrupt */ / t) a3 J& C- k( d+ E
#define INTR 0x1C + ~/ {1 B+ @; z  r$ o' k0 s! {
/* Screen attribute (blue on grey) */
, w) t+ a6 i3 @5 m2 t  l#define ATTR 0x7900 </FONT></P>2 ]7 @: F* y2 B- T' _
<><FONT color=#0000ff>/* reduce heaplength and stacklength
9 {& E8 N# R( {" T" @7 ito make a smaller program in memory */ ( `+ m4 M2 ^( G. S& l* H% ]
extern unsigned _heaplen = 1024; ! c& T/ E1 j: _/ U2 ~7 ^
extern unsigned _stklen = 512; </FONT></P>
+ A& q- B8 X  p' K- i2 d# g/ V9 I<><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>; R$ [& ?' ?( m( H4 o; l3 {
<><FONT color=#0000ff>void interrupt handler(void) 0 S" }4 ^- E8 }
{
; _7 V1 V3 W8 u4 K5 d# junsigned int (far *screen)[80]; ! U4 p! e& c* _) J, Z5 l" d* d/ P
static int count; </FONT></P>
+ L, a; C$ t: S  \<><FONT color=#0000ff>/* For a color screen the video memory
2 [" K* M, Z/ L8 U! U, [is at B800:0000. For a monochrome & Z; K+ Z$ u6 ~! `
system use B000:000 */ ( [% n& g) s9 E) y
screen = MK_FP(0xB800,0); </FONT></P>4 b7 {' n& w; S. I% H/ S: X! R, e
<><FONT color=#0000ff>/* increase the counter and keep it # F+ d& w. L$ N1 ~9 @  j0 c5 @; G+ x
within 0 to 9 */
/ h. @  k0 g5 @* p  f$ y/ ncount++; ) o% s: i3 d6 e
count %= 10; </FONT></P>
6 q4 f# H: H+ n<><FONT color=#0000ff>/* put the number on the screen */ $ j1 H3 r* j  H0 ^& @/ ^$ {* ]
screen[0][79] = count + '0' + ATTR; </FONT></P>
! D4 b) u3 `6 o8 Z) U<><FONT color=#0000ff>/* call the old interrupt handler */
3 W8 @3 z4 Y( P* Xoldhandler(); 4 y! a& Y, {* D, Y" L" Z; m* M
} </FONT></P>
* w8 J2 M4 H7 v0 B! c" O; ]<><FONT color=#0000ff>int main(void)
9 |! v: v) F  e  [' l/ @{ </FONT></P>
" i* c! V8 S7 o! O/ P. k1 C5 X. t<><FONT color=#0000ff>/* get the address of the current clock ; @  s0 m8 V5 h1 r- u7 b
tick interrupt */ 5 }2 S) o3 b: S
oldhandler = getvect(INTR); </FONT></P>
$ w8 z* v% d. O4 W" l<><FONT color=#0000ff>/* install the new interrupt handler */
, u  d, U7 S( e& [" R# G3 [setvect(INTR, handler); </FONT></P>
5 h) c0 B% J' @$ @<><FONT color=#0000ff>/* _psp is the starting address of the % J' t4 g  a" E& B
program in memory. The top of the stack : K$ z+ \/ T: j
is the end of the program. Using _SS and
0 D/ f2 k0 j8 l# D9 {_SP together we can get the end of the
/ n; F) z* d8 x! U, h5 H- [2 X0 s. ystack. You may want to allow a bit of
4 D0 v$ {; J, O0 t* x1 psaftey space to insure that enough room
1 p1 f* i6 w' M5 x& R4 Lis being allocated ie:
9 c) {. ?1 @5 _9 ~, U2 g( x1 E. y(_SS + ((_SP + safety space)/16) - _psp) : i% _& y3 ^' V# u5 Y, r9 ]
*/
( v! J& G8 K3 T% pkeep(0, (_SS + (_SP/16) - _psp)); ; E1 {+ X, _7 U, [
return 0; ( `& n4 O# H! u  k6 g
}& ~& J, V: M- ^( b/ Z8 n* J$ g' p/ O
</FONT></P>
, G! G! p# J( `) h: U, |0 V
: z. v6 b1 k- {! a5 K
. O/ ^* p/ t' ?6 L) ^<><FONT color=#ff0000>函数名: kbhit </FONT>" M5 _) `7 h( j8 u
功 能: 检查当前按下的键 ' \( ?3 C3 t* X5 N1 l, ^( y$ N% u  V; Z+ L
用 法: int kbhit(void);
. w3 S/ S0 \) O7 P) z: T& n; _/ F程序例: </P>
( j$ Q5 x; K8 S% t9 B# K- g<><FONT color=#0000ff>#include <CONIO.H></FONT></P>
' G! h( W( S9 E- \* |2 w<><FONT color=#0000ff>int main(void) 0 [; T" H1 M5 ]' j  M0 \1 `
{   V+ b7 i0 G& w1 v
cprintf("ress any key to continue:"); 7 ^+ J, w% h# G  I$ e
while (!kbhit()) /* do nothing */ ; 7 N- l/ ?6 U; }! y$ ~5 X% \# ]! P
cprintf("\r\nA key was pressed...\r\n");
/ l3 o9 F3 E( p. r! o5 breturn 0; * T2 P( ]2 X  j  A# D
} 4 d" Q/ {, F% U9 B1 H- [3 `

1 q/ H" [" R8 [( L2 O! U" X: G" \; A3 p. e. J# W# r/ L7 t( W0 G
</FONT></P>
# J3 \# q9 n$ N2 z" Q( K; O/ g4 G  ]<><FONT color=#ff0000>函数名: keep </FONT><FONT color=#0000ff>
# R5 k& ~+ a- u9 A, w( @<FONT color=#000000>功 能: 退出并继续驻留 4 R- G. O3 g, N' K
用 法: void keep(int status, int size);
! r5 W4 H* r7 g7 d/ A: K程序例: </FONT></FONT></P>
" Q2 X( c6 P* L$ n( i& r- C5 x<><FONT color=#0000ff>/***NOTE:
4 h# X9 f% m5 S0 SThis is an interrupt service routine. You
3 _  ]' ?5 s8 A9 m/ l  W" E9 M4 O2 Scan NOT compile this program with Test : V( ^1 a8 s' t0 I
Stack Overflow turned on and get an
- e9 J3 L4 W; g: W# E- Gexecutable file which will operate
0 w$ X9 N8 [. q: N4 H3 Gcorrectly. Due to the nature of this
, w: e* M" r; Q" z; Kfunction the formula used to compute 8 |/ a5 ^+ r& I) M! G
the number of paragraphs may not 2 a5 C3 X0 S) M% B% y) G( T% C4 p
necessarily work in all cases. Use with " R+ V7 v# M2 d/ R+ J0 ?' l
care! Terminate Stay Resident (TSR) * v  ~- S' Q! |5 h) d2 \  p+ d
programs are complex and no other support $ J& `/ r( \7 T' z* |6 N! H
for them is provided. Refer to the ( H! H; p6 r5 ?: W8 t+ T, N/ {5 S) a( i
MS-DOS technical documentation
: F& Y& C" G. D: l" yfor more information. */ # Q( k1 _2 N1 r! x4 ?. t% h
#include <DOS.H>3 i, K% Z- ?& \6 F
/* The clock tick interrupt */
" }; e8 @/ h7 }#define INTR 0x1C
. i5 ]2 `5 H: }! ?7 j( E8 G# U' L( U* t/* Screen attribute (blue on grey) */ 7 a) V$ `$ h. B8 D+ w% `0 y2 p
#define ATTR 0x7900 </FONT></P>
; K$ U$ H7 v4 `# q<><FONT color=#0000ff>/* reduce heaplength and stacklength 1 G0 x0 {4 ?3 I9 V- N) G, @6 S3 D/ j6 K
to make a smaller program in memory */
) ]1 B; A2 ~! }1 [0 F! W9 ]extern unsigned _heaplen = 1024; - l# x+ u8 j' _- @2 p) S5 ], g) @& X  ~
extern unsigned _stklen = 512; </FONT></P>
9 k3 t6 k( a: R" F% c; ~' n" I<><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>
& L- z, @& k7 |<><FONT color=#0000ff>void interrupt handler(void)
4 ~; p/ s5 B* p5 d' r  |! g' x{ 9 t  O4 n: |. A( L5 h8 |
unsigned int (far *screen)[80]; : O4 D$ n+ }* [% y# g0 A* |
static int count; </FONT></P>
- B3 R7 b# l: P3 A/ T9 E<><FONT color=#0000ff>/* For a color screen the video memory : ]: o% L6 J4 w, R+ [( l' r
is at B800:0000. For a monochrome 8 P- k3 ?( z/ Y2 Y: A' |  D
system use B000:000 */ & ]4 j6 ^1 W" o- X* U4 n, g
screen = MK_FP(0xB800,0); </FONT></P>/ {8 w; h) J( n( w1 x: E
<><FONT color=#0000ff>/* increase the counter and keep it
8 \* f- d' ]0 B( f8 ?: Ewithin 0 to 9 */ ! F5 Q* M3 p. D3 j5 w: H
count++;
" Q% V5 T* N9 x( N" L$ E: Hcount %= 10; </FONT></P>5 B/ F6 D$ x% J" d+ i, u" X
<><FONT color=#0000ff>/* put the number on the screen */ 2 j0 w2 U$ _. d8 m; }
screen[0][79] = count + '0' + ATTR; </FONT></P>
4 A+ H/ D& k& f, J1 [; y; z* x<P><FONT color=#0000ff>/* call the old interrupt handler */ / ]1 a% f; I2 s4 H' t7 ]
oldhandler(); 0 L7 d" J2 }& ~' q1 V) ~3 N, }. I$ i
} </FONT></P>' W  Q% Q- S! z8 a' C& J+ c0 _4 v
<P><FONT color=#0000ff>int main(void)
3 N2 z# K$ }# S1 D1 g" T  }3 ?{ </FONT></P>
4 f+ G2 t2 ^, h<P><FONT color=#0000ff>/* get the address of the current clock # P- w4 C6 {3 M7 ^8 z$ Y
tick interrupt */ 8 l  J) M( H1 E% g1 ?; h
oldhandler = getvect(INTR); </FONT></P>4 |; z) N( Z! R0 _
<P><FONT color=#0000ff>/* install the new interrupt handler */
* K5 M& c2 {# g; p- m6 esetvect(INTR, handler); </FONT></P>
: ~, Z/ u4 _( a7 F<P><FONT color=#0000ff>/* _psp is the starting address of the
5 K0 U# ]  ?) D$ M$ a6 \program in memory. The top of the stack . ~% t) |, j: H( {
is the end of the program. Using _SS and ( G8 T3 _( O$ m
_SP together we can get the end of the - R- k6 d  P* E2 N3 M; H+ J3 I& a
stack. You may want to allow a bit of
4 S- u4 z" \" {: Jsaftey space to insure that enough room ! ?7 V) P  Y( _3 `! u
is being allocated ie: 3 H8 Q5 y+ Q) F+ g0 Z  J' c, u, u
(_SS + ((_SP + safety space)/16) - _psp)
) x1 G6 T8 _+ I7 c4 l+ T+ F*/ ! j6 E0 e3 x4 o* h
keep(0, (_SS + (_SP/16) - _psp));
6 q4 A$ Y( ~! ]8 _9 Ureturn 0;
  h, V9 @# T$ o1 ]- T- J}</FONT></P>




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