数学建模社区-数学中国

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

作者: 韩冰    时间: 2004-10-4 02:58
标题: 函数大全(k开头)
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(k开头)</FONT></B></FONT>$ M4 V# w4 ]+ p" w: H! m; y) X
</P>
# p: n! t. x% O1 B! ~- m<><FONT color=#ff0000>函数名: kbhit </FONT># U& S- ^5 K: S# {! g: x" T
功 能: 检查当前按下的键
3 G8 r, ^' \: c6 ^1 P用 法: int kbhit(void);
# l$ B) b) |4 W. _; R$ b程序例: </P># I, b* o9 L# U9 D$ S1 T
<><FONT color=#0000ff>#include <CONIO.H></FONT></P>5 ~3 w* U& W, z4 q" `" \" U2 c
<><FONT color=#0000ff>int main(void) ' u+ N9 h* m/ t" L4 _  x! ~
{
- F- }- d4 p$ {8 q4 n- P: xcprintf("ress any key to continue:"); ) n  P1 n- l, v4 r# @( a
while (!kbhit()) /* do nothing */ ;
/ z% T  A, N" |8 M  Y- H" _! Z6 tcprintf("\r\nA key was pressed...\r\n");
7 f/ ~2 @- L: p/ a& C' A1 preturn 0; . \+ ^% O, m: j& X4 {+ K
} </FONT>; ~* G# W" `# Z) A5 `' V! x
</P>7 o4 d' T  n2 A$ m3 r7 V
<><FONT color=#ff0000>函数名: keep </FONT>
% W; U' c2 Z7 ~: G4 L  j3 I功 能: 退出并继续驻留
3 Q8 i! Q. S0 B2 ], |7 L用 法: void keep(int status, int size);
, ^* k" a$ L7 |( z3 q程序例: </P>
& X) ^: P+ K8 s& k<><FONT color=#0000ff>/***NOTE:
* o  L- K* f$ k/ x  xThis is an interrupt service routine. You
2 Z5 N# R( Z& P- x$ ?& g# Ccan NOT compile this program with Test % k) g) g) B$ U
Stack Overflow turned on and get an
- C+ r7 E& P7 c' K+ |! oexecutable file which will operate # @& w$ T" f) ^9 d
correctly. Due to the nature of this
) o0 r# Q6 `/ l. |function the formula used to compute
, x7 b: m. d1 D' Q, ethe number of paragraphs may not
1 S& }$ t6 \" k  ^+ D+ {necessarily work in all cases. Use with
! k8 f; W1 x/ j  p4 K: j. |6 C; v2 _care! Terminate Stay Resident (TSR)
* n+ J+ K& k9 ?" c- aprograms are complex and no other support
$ P  O! O  r- ]( b/ _4 r4 n' Ifor them is provided. Refer to the , R; c; p& D2 Z2 }  {2 y
MS-DOS technical documentation
( U, M! [+ ]3 b/ ]for more information. */ : P8 o* {4 f2 K8 O9 A
#include <DOS.H>
' q# O+ E0 I) d. n; l7 p/* The clock tick interrupt */ + O) G# z4 E' a& b
#define INTR 0x1C
5 d- Y& c3 Y! s% @! S9 ~/* Screen attribute (blue on grey) */ + P, k& }, L/ p. e
#define ATTR 0x7900 </FONT></P>2 ]2 p* Z0 O& d$ c0 [; f
<><FONT color=#0000ff>/* reduce heaplength and stacklength
3 ~3 e1 e+ O; v" z' S: X" cto make a smaller program in memory */ 7 Z7 _% c6 f5 S9 O! A+ g
extern unsigned _heaplen = 1024;
6 o7 K+ k2 P, e/ v3 {$ Oextern unsigned _stklen = 512; </FONT></P>
8 ~8 d/ h  _, C9 A8 G) k<><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>
5 N: y3 k" |& k& \5 V- @% P<><FONT color=#0000ff>void interrupt handler(void)
8 i, T* O& ]  _8 h! [0 }5 T{
# ?& S4 V) W% y2 n+ J1 eunsigned int (far *screen)[80]; 1 [+ t/ o) N- e' F
static int count; </FONT></P>
- i6 m* X6 y8 u' S! `<><FONT color=#0000ff>/* For a color screen the video memory - t) g/ D; \: R! q  [
is at B800:0000. For a monochrome
, l! l  T+ s' p1 W" wsystem use B000:000 */ & y. P4 ~/ Z5 m( `) ]; ?& v
screen = MK_FP(0xB800,0); </FONT></P>
3 `. q/ o2 K9 _- ]7 k5 {7 c<><FONT color=#0000ff>/* increase the counter and keep it
4 d- u6 j6 c" R' h# Awithin 0 to 9 */ 3 o, K: u( d4 Q2 h+ E
count++; 0 o; [3 F0 t2 E& y/ P9 f
count %= 10; </FONT></P>7 Q/ d& Y0 l* z# g
<><FONT color=#0000ff>/* put the number on the screen */ # l$ j$ j7 \0 e' G( S2 z
screen[0][79] = count + '0' + ATTR; </FONT></P>
3 C' L% T% F& C8 G% `) c<><FONT color=#0000ff>/* call the old interrupt handler */
7 {: t1 R# L5 l4 |oldhandler(); 7 I% s8 |, s1 g( W6 o* z( w
} </FONT></P>
, W7 l% }) I6 e" X2 m; e1 E<><FONT color=#0000ff>int main(void) / B( G  c: x  `9 K( S# `
{ </FONT></P>
3 H/ F1 b( Q* m' E<><FONT color=#0000ff>/* get the address of the current clock
! c# [( c1 N/ n9 ?tick interrupt */
( V) V( b# f% @- `, k* P8 ioldhandler = getvect(INTR); </FONT></P>; o! |% t- d8 [; R* r# F
<><FONT color=#0000ff>/* install the new interrupt handler */
9 ?4 o9 p+ }4 t* a: @. G+ r  ~setvect(INTR, handler); </FONT></P>: h- h: u- E' E0 J9 i
<><FONT color=#0000ff>/* _psp is the starting address of the
2 f5 ~7 M( C, I, ?! T/ J5 z/ @program in memory. The top of the stack
; E5 `- ]0 G7 j9 x6 u% V% T. wis the end of the program. Using _SS and 6 T. P) p1 e7 B9 z8 N) k
_SP together we can get the end of the
6 f/ _5 }- ~0 [stack. You may want to allow a bit of
( F# Z2 F# ~) p0 Esaftey space to insure that enough room
* a9 T0 Z# i8 Y% a* d& ^is being allocated ie:
& a# `' r( U* i, v' Z& b+ M(_SS + ((_SP + safety space)/16) - _psp) ; C- k8 @" H8 @1 z
*/ 5 |& F+ O. o: t& w5 ?1 ~; ?
keep(0, (_SS + (_SP/16) - _psp)); & ^* i% c; C9 L6 N6 @) U
return 0;
2 e2 U/ }+ R* f}
; m. |- h# B7 t( @' j9 F</FONT></P>
: i( z6 F4 P0 I, Y! O( f' h  ^: ^" u6 n. ]  L
# e! u5 S1 O7 s% P  j
<><FONT color=#ff0000>函数名: kbhit </FONT>
6 M, P- _7 o  u% u功 能: 检查当前按下的键 , J, f7 v* \% i, N0 c( F
用 法: int kbhit(void);
5 e9 ^4 \7 Q2 X0 N程序例: </P>
: C; Y6 E  H- Y. ~<><FONT color=#0000ff>#include <CONIO.H></FONT></P>* G) N4 e% p! D4 {/ w2 l) Y
<><FONT color=#0000ff>int main(void) ' Q% _5 P$ o+ s& |
{
# T3 D: l/ j1 r( ]# Fcprintf("ress any key to continue:");
& b4 c1 F: h1 ?; O6 r: twhile (!kbhit()) /* do nothing */ ;
# G0 C- f, `3 s8 a. ycprintf("\r\nA key was pressed...\r\n");
- ?& D9 ], |; G7 n& w# b4 Nreturn 0;
: a. w/ R1 C) `0 T} $ l/ e) u# a( l
4 W  C; ?9 u+ d

+ y1 U# o* g: Z2 g8 K8 L; l6 F7 X</FONT></P>
6 V- S+ g( q( S<><FONT color=#ff0000>函数名: keep </FONT><FONT color=#0000ff>
3 @) |, v/ X: Y" K' N7 n7 S<FONT color=#000000>功 能: 退出并继续驻留 % C6 g2 Y/ X8 j: K0 y
用 法: void keep(int status, int size); 6 O0 _: d1 h) M- r# I3 z7 y/ H
程序例: </FONT></FONT></P>: F; m2 j5 \1 v- d: |' T
<><FONT color=#0000ff>/***NOTE: ; X- S3 _: T( q& ^, b; ?3 S
This is an interrupt service routine. You
( V8 _2 d, n/ I: Lcan NOT compile this program with Test 4 e" I0 F; H9 l! b5 G
Stack Overflow turned on and get an
9 C& l( a! v1 I2 `: \; qexecutable file which will operate   ~1 U+ [8 {: L5 r, A  s
correctly. Due to the nature of this
4 m& y# Y  ~0 \& P& a" ^$ [1 ~function the formula used to compute ( P! E* Y8 `2 j; f! p
the number of paragraphs may not 9 K9 u: _( |+ ^! r2 ~) j
necessarily work in all cases. Use with
3 c* K' H8 J+ n6 }/ z. y# r# u9 t  Jcare! Terminate Stay Resident (TSR) ' c4 D( m3 j2 T% O8 e
programs are complex and no other support
7 H  B* F% x6 s8 gfor them is provided. Refer to the , |! c1 i2 z4 |; n; b$ S& t* Q
MS-DOS technical documentation
% n0 T1 W  n  V) n$ x1 Ofor more information. */ : F* y4 q$ S" s) f' I  R. q
#include <DOS.H>
- R" F. U) P7 H, P  E# `* p3 m& [/* The clock tick interrupt */
4 R8 A& o0 l* @' d/ c$ K#define INTR 0x1C
5 g9 a! W6 n$ g0 Q/* Screen attribute (blue on grey) */ ) S) P: d) M) m5 o1 x/ N( |
#define ATTR 0x7900 </FONT></P>- k4 T! m- @' z
<><FONT color=#0000ff>/* reduce heaplength and stacklength - D  n" I% z$ A( M! d. D1 p. }
to make a smaller program in memory */
! Y4 X: g9 \$ }% s( X; c6 |extern unsigned _heaplen = 1024;
$ N$ ^& a4 [4 d  j" W5 z# Qextern unsigned _stklen = 512; </FONT></P>
+ ~& w4 {2 y* @<><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>& h4 k& U" h1 E" X) ^$ y6 v. f7 X
<><FONT color=#0000ff>void interrupt handler(void) 2 g' {1 g1 Q4 D
{ 4 g1 g0 z$ B5 g! r2 W) l
unsigned int (far *screen)[80];
8 k+ w; ?0 V1 W: u- sstatic int count; </FONT></P>
. ^& E8 h: U# Y/ M5 U<><FONT color=#0000ff>/* For a color screen the video memory # \/ ^1 D! v8 G* m! n' u7 I
is at B800:0000. For a monochrome
7 k3 }4 o! J. v- j5 C0 r5 Bsystem use B000:000 */ ) i; L- V6 U1 h9 Z) U
screen = MK_FP(0xB800,0); </FONT></P>
) ]5 S5 o. n9 U<><FONT color=#0000ff>/* increase the counter and keep it
; |8 E2 A: p7 {within 0 to 9 */
; D# `5 {9 q# g& H  c9 n+ hcount++;
+ U  S! _/ b" }3 Ccount %= 10; </FONT></P>7 S2 T: p& W6 V7 W- Y4 l- p
<><FONT color=#0000ff>/* put the number on the screen */ ! B1 G# x3 i) b+ m
screen[0][79] = count + '0' + ATTR; </FONT></P>( ^2 m3 o8 T) A  U
<P><FONT color=#0000ff>/* call the old interrupt handler */ % h1 N% H2 c. A$ _5 ~" B: R
oldhandler(); + Z* `# f: o5 }6 B" c
} </FONT></P>+ y+ o# K' l, G% w" x
<P><FONT color=#0000ff>int main(void)
0 k- f& d! ^4 z9 h5 I; i9 ?{ </FONT></P># i6 `) b9 a7 ]' f) q
<P><FONT color=#0000ff>/* get the address of the current clock
) [! U' V: W# A. O  M; H* I8 O9 @tick interrupt */
9 G5 s+ T6 {1 {8 V% \7 L* Roldhandler = getvect(INTR); </FONT></P>' d- ]/ V: M) ?2 b9 J+ c
<P><FONT color=#0000ff>/* install the new interrupt handler */
6 s4 N  n( F1 t" Xsetvect(INTR, handler); </FONT></P># y  k8 p* P: D1 D; _; p3 d+ t
<P><FONT color=#0000ff>/* _psp is the starting address of the
& g' s+ h* o3 \. |program in memory. The top of the stack
) _. I# K2 X; T0 vis the end of the program. Using _SS and
1 d8 ]( H' V0 R_SP together we can get the end of the
' q2 j5 `' @5 G; d* y, G7 m' b# }stack. You may want to allow a bit of / V. Q) [- R9 L
saftey space to insure that enough room 3 ]* L9 m! s7 z( Y
is being allocated ie: ( _2 v7 r) ~' @  e
(_SS + ((_SP + safety space)/16) - _psp) * T  C# q: ~( q. A6 J; v
*/
4 E1 s1 s9 Z8 Fkeep(0, (_SS + (_SP/16) - _psp)); / Y' x. O5 }2 _1 R
return 0; 3 m6 Q$ H: E( n) y  f
}</FONT></P>




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