- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
< ><FONT color=#ff0000>函数名: sbrk </FONT>
& L0 y; d( V: {, N' s }# P功 能: 改变数据段空间位置 / z7 z4 b$ ~0 Q( ?3 j
用 法: char *sbrk(int incr);
- N, M- }& ?9 Z: A4 }8 S* F& l/ m0 ]7 Y程序例: </P>
, n0 n% W: P0 J; r3 b7 r# b6 @8 X< ><FONT color=#0000ff>#include <STDIO.H>
. i! k+ Q% x. k/ F& c( p; _#include <ALLOC.H></FONT></P>6 U: I! k% O1 R8 s
< ><FONT color=#0000ff>int main(void) 6 K0 g1 T1 C, A! @. C- V2 i
{ {# f, x6 X; u5 h
printf("Changing allocation with sbrk()\n");
% G# _0 F) w" d! m' Uprintf("Before sbrk() call: %lu bytes free\n",
* A% u- E/ O! o3 w5 J2 c(unsigned long) coreleft()); ) {+ x1 K8 E3 L. J
sbrk(1000); 6 O! c: s9 L6 f- B* @
printf(" After sbrk() call: %lu bytes free\n", ) _1 u# H' E( ]- b% ^
(unsigned long) coreleft()); * {7 c; I5 l: Q
return 0;
* k) c3 H) w! |5 F# ^} 5 A5 ^( y4 x3 S3 e
. D3 C3 m+ b3 _
</FONT></P># [/ i2 u7 B" j
< ><FONT color=#ff0000>函数名: scanf </FONT>- Y7 s" L$ x- t5 c! b
功 能: 执行格式化输入 % q/ E) q) u9 T9 F7 B2 o* s
用 法: int scanf(char *format[,argument,...]);
. J( S9 I: A P6 O$ ~程序例: </P>+ e* o0 N* x2 j" Y+ O/ U
< ><FONT color=#0000ff>#include <STDIO.H>
6 J' {5 E4 {" r& U#include <CONIO.H></FONT></P>5 V5 i; B2 t5 V( N9 ]
< ><FONT color=#0000ff>int main(void)
; I9 S) [( Z. o _; Q{
: o0 T1 B' A% V+ l N* x/ o& gchar label[20];
4 G' C) } k F4 ]6 G1 t5 `char name[20]; 7 q2 D; E" {( E" g4 }+ [$ \7 ^
int entries = 0; . c; P/ c, R9 }" I
int loop, age;
8 D% `! t$ x) \3 k$ V3 gdouble salary; </FONT></P>, O5 ^; s# A. k6 _1 B( f
< ><FONT color=#0000ff>struct Entry_struct
" E% x o( |& B; K# p8 J* F" k{
2 `7 n$ l9 i: \$ ^4 p1 Tchar name[20]; : G" {4 R" X7 T- P3 `# W. f
int age;
s' J7 F% y3 }! P1 C! L: mfloat salary;
/ K" k: Q, |- I! a* s, ~& |} entry[20]; </FONT></P>- S' G+ C! I+ D7 \* c
< ><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
3 a- z, @: W+ X2 ~ g. @printf("\n\nPlease enter a label for the chart: "); 0 k" e, Y' u; u* C6 I3 m/ Q' T
scanf("%20s", label);
& k l/ \" O$ l- Yfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
/ H+ C- X2 g% B# X< ><FONT color=#0000ff>/* Input number of entries as an integer */
7 Y1 K/ N0 [: bprintf("How many entries will there be? (less than 20) "); ' W9 [+ p1 l+ c) a+ x8 L
scanf("%d", &entries);
+ t/ o9 A) k# ^. \; E- Kfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>3 z F# Q9 M% q3 b. A5 e
< ><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ 2 B, ?; n8 `1 \+ ~! w7 l
for (loop=0;loop<ENTRIES;++LOOP)
7 |' [/ |$ P! R {
1 m- ]% t4 M: C1 s! c7 d2 i, Eprintf("Entry %d\n", loop); 0 @6 ^) [% k. A
printf(" Name : "); ( E. H# ]# d: m6 Z
scanf("%[A-Za-z]", entry[loop].name);
1 v& w8 Y9 G, r* Ifflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>; \. |- ^8 k9 ?
< ><FONT color=#0000ff>/* input an age as an integer */ w% t& _- [$ I: F! w$ g- ^3 Q, R
printf(" Age : "); 2 ~. s+ [$ [! w( }
scanf("%d", &entry[loop].age);
& A/ K' r+ F& z+ Bfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
7 U' L( X) P7 \/ C< ><FONT color=#0000ff>/* input a salary as a float */
/ W; g5 l2 \4 L/ O( _printf(" Salary : "); + u. R& M6 F. _1 \
scanf("%f", &entry[loop].salary); ) O/ H2 X8 v6 h+ g4 F
fflush(stdin); /* flush the input stream in case of bad input */ 1 Q- g- F: G# U7 \/ s6 g" g6 F w
} </FONT></P>9 u) F, F' ~, x3 N* t
< ><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */
9 s5 n8 S l% Xprintf("\nPlease enter your name, age and salary\n"); # L( B. m; }! n$ n
scanf("%20s %d %lf", name, &age, &salary);
/ D8 U6 a0 r% l, N' m% ]4 i</FONT></P>
1 }# D/ k) G# n' G6 q7 g& M3 _< ><FONT color=#0000ff>/* Print out the data that was input */ , T. Y$ {/ [+ @( n
printf("\n\nTable %s\n",label);
& q: Q) S% s, s! Z' a1 B+ ^printf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
. l1 x0 [! {" s* e3 U2 Q) fprintf("-----------------------------------------------------\n"); Y, y5 J+ @' s0 A
for (loop=0;loop<ENTRIES;++LOOP) 8 |' O2 T" x3 v, ]$ T7 [6 P, n2 X
printf("%4d | %-20s | %5d | %15.2lf\n",
; [$ @. a' d( _loop + 1, 8 Y" V5 q! q5 @4 \% V- r" F) e! S: X
entry[loop].name, * F" p( s9 ~6 v5 a: R
entry[loop].age,
1 @1 z% d% D! G' }1 F2 _+ d3 fentry[loop].salary); & W( x& H% R! f0 N3 A/ b: W
printf("-----------------------------------------------------\n"); + u; d2 a# M0 G! |3 E7 k
return 0;
3 u8 U7 i# I. o. ^5 Q2 W6 |* a( S}
9 E0 S" h, c$ a$ e</FONT>! I: K0 W0 G0 {& \
</P>' c) g7 o' }6 ?/ x# ]! s
< ><FONT color=#ff0000>函数名: searchpath </FONT>
. T: o6 U7 |' Y. G* r& f/ Q5 }功 能: 搜索DOS路径 $ _% V: e/ {1 n7 `0 Z
用 法: char *searchpath(char *filename); 3 g" W6 C/ V& ?" `
程序例: </P># K- Z) g$ P" Y$ }4 }9 q: h2 C. c8 R
< ><FONT color=#0000ff>#include <STDIO.H>
3 z* Q1 w" ]: ?3 |#include <DIR.H></FONT></P>' j& M" n2 K) p; M( [4 ?3 O
< ><FONT color=#0000ff>int main(void) 9 |3 Y5 c% Z2 E& S0 U
{
' v6 E: e; H& [3 lchar *p; </FONT></P> p& u1 Z, l) U& G% S, B
< ><FONT color=#0000ff>/* Looks for TLINK and returns a pointer 0 x. p7 l" |7 p$ |& B6 ]* u+ d7 ~
to the path */ & v5 d+ b, A% `! T
p = searchpath("TLINK.EXE"); - ]" ^* Q1 i8 O' K/ F
printf("Search for TLINK.EXE : %s\n", p); </FONT></P>: M0 {; t+ e, R- x* s
< ><FONT color=#0000ff>/* Looks for non-existent file */ * I: S# d9 Q* W' P& k7 B4 J3 A
p = searchpath("NOTEXIST.FIL");
8 {2 q' \8 [4 F2 xprintf("Search for NOTEXIST.FIL : %s\n", p); </FONT></P>
) v W; a2 r% e2 ~, i+ N< ><FONT color=#0000ff>return 0;
3 X6 _8 K8 d6 R' }2 s% S( `} ! ^* ^7 X% D3 L. V. y$ X6 I' Y
; R( I7 g% P1 m$ C
</FONT></P>
# l+ \9 a9 q- I, i< ><FONT color=#ff0000>函数名: sector </FONT>
) y2 s7 I" d. V) k功 能: 画并填充椭圆扇区
; Y' Y A, l" P( J. E& Q9 J用 法: void far sector(int x, int y, int stangle, int endangle); # E+ {" d* ?+ Z
程序例: </P>4 [# N/ U8 w' {7 n. H" l: O6 t
< ><FONT color=#0000ff>#include <GRAPHICS.H>, a* o% l/ H* N% u9 J! u7 q
#include <STDLIB.H>& `; E% o4 z. Y: w/ w* Z4 x
#include <STDIO.H>1 K7 v0 _5 ~1 P& k0 P
#include <CONIO.H></FONT></P>
* P) e- i' ^% g2 q. b4 q< ><FONT color=#0000ff>int main(void)
% \( e8 `4 Y# H4 t% X9 V9 q{
/ T: r' G% G1 c3 y% u9 i1 ~" c0 G/* request auto detection */ 1 X6 W: b8 n8 |! o' `
int gdriver = DETECT, gmode, errorcode; 4 U# U! ]2 I. f( _' F
int midx, midy, i;
0 A; X% ]) U) I& \int stangle = 45, endangle = 135;
; ]- d3 j5 I& {/ hint xrad = 100, yrad = 50; </FONT></P>
' D" {* y Q& B3 v5 b< ><FONT color=#0000ff>/* initialize graphics and local variables */
O$ j0 V) {8 p0 ?$ _& w4 Pinitgraph(&gdriver, &gmode, ""); </FONT></P> A3 p! r# X! r, ]
< ><FONT color=#0000ff>/* read result of initialization */ 9 X5 Z: Q7 m: j+ G3 i3 M
errorcode = graphresult();
4 t* k4 d Y% L# rif (errorcode != grOk) /* an error occurred */
9 S' D8 F' j q8 X{ $ W! M+ ?" ?, w# k. ~
printf("Graphics error: %s\n", grapherrormsg(errorcode));
, \4 d2 y& f& w$ [printf(" ress any key to halt:");
# B( ]- S6 {. j1 X& wgetch();
- d- z, B l* D+ O8 ]# z% [! n6 \3 uexit(1); /* terminate with an error code */ - W. h$ c- S6 p% ^; ^) R2 ~
} </FONT></P>) h4 Z- M8 Z9 S3 [& k
< ><FONT color=#0000ff>midx = getmaxx() / 2;
a3 Z# L& e( r$ Z# }6 _8 Vmidy = getmaxy() / 2; </FONT></P>5 `+ x: y& [1 x$ n, ?5 v* _
< ><FONT color=#0000ff>/* loop through the fill patterns */ # W8 U. ~* ]' j# X7 r
for (i=EMPTY_FILL; i<USER_FILL; <br i++)> {
- N& E# I5 z! A" {0 F. K: E/* set the fill style */
/ v2 s" S) L0 r. Y5 H3 h; E6 \setfillstyle(i, getmaxcolor()); </FONT></P>/ }4 u7 ^/ a% m$ q& R0 A3 |, u
< ><FONT color=#0000ff>/* draw the sector slice */ 0 E8 j# z/ g$ X9 v: Z
sector(midx, midy, stangle, endangle, xrad, yrad); </FONT></P>3 A( _1 G# y3 R
< ><FONT color=#0000ff>getch(); 0 P& ?7 J, h3 Y) H* Y1 C5 a$ b% i
} </FONT></P>
; w6 ], w7 x7 t<P><FONT color=#0000ff>/* clean up */ 7 N9 V3 i9 C5 Z
closegraph(); & n, e3 Z; j. X2 {9 H5 n
return 0;
/ Q' a3 x2 r& V+ H% H! V0 r& W3 Y} , T& f3 X6 j& y+ T- e3 C
</FONT></P>: p" b* A! F4 v4 A! Y/ o4 T
<P><FONT color=#ff0000>函数名: segread </FONT>2 R7 B7 l. r4 {0 _1 v* s
功 能: 读段寄存器值
e4 R1 J8 z+ x% F3 z用 法: void segread(struct SREGS *segtbl); * @' ?3 E9 m- a7 K
程序例: </P>
3 S* B( V) D$ a8 Y, M# Q) _9 g- D<P><FONT color=#0000ff>#include <STDIO.H>
- w2 G2 z: R0 u0 E#include <DOS.H></FONT></P>4 a$ p1 C4 W+ I! [9 U% h& ~
<P><FONT color=#0000ff>int main(void)
# M0 |9 V2 _2 g{
: O j& X. g) B2 k5 xstruct SREGS segs; </FONT></P># }3 o h7 g; f
<P><FONT color=#0000ff>segread(&segs); , G/ X, o6 ^- O, i7 y7 u
printf("Current segment register settings\n\n"); ; t/ v# c" ?' \% m& z; p
printf("CS: %X DS: %X\n", segs.cs, segs.ds);
0 p8 z8 m U3 c0 wprintf("ES: %X SS: %X\n", segs.es, segs.ss); </FONT></P>
- H* D: N( a# \9 h<P><FONT color=#0000ff>return 0; - t9 c+ O! Y4 U, r0 [8 r
} </FONT>
% Y1 C' E+ [: P. |
7 A7 Q8 O* U" \& z</P># g* w' T+ t9 @) z4 t* d- T
<P><FONT color=#ff0000>函数名: setactivepage </FONT>& [, \& S/ z. \
功 能: 设置图形输出活动页
( ^& B# ^- U3 |: U/ {4 w用 法: void far setactivepage(int pagenum); 2 Y! j+ T# ?7 J0 |. d( `, x4 d
程序例: </P>
+ Y4 p [/ `; a+ E+ r2 Z, ?<P><FONT color=#0000ff>#include <GRAPHICS.H># n* }8 f' ^9 K
#include <STDLIB.H>
2 H# W: @9 C5 ]#include <STDIO.H>! b' Z: [6 P3 ^5 K* C/ R6 N
#include <CONIO.H></FONT></P>
* c3 m8 l f8 Z$ c' c<P><FONT color=#0000ff>int main(void) ; b5 ?6 h9 @4 q; Z- F6 \
{
' ?+ | {# u1 l/ X1 R7 B- b/* select a driver and mode that supports */ Q- c% ~* }4 [
/* multiple pages. */
6 n0 S/ ^( J" ]4 h+ \int gdriver = EGA, gmode = EGAHI, errorcode;
; L6 w& }! M6 S3 H+ k* \- `* _int x, y, ht; </FONT></P>+ N+ B9 U u1 L# H
<P><FONT color=#0000ff>/* initialize graphics and local variables */ $ |% l4 R! ^& E' w2 g
initgraph(&gdriver, &gmode, ""); </FONT></P>
) ]/ B D; z+ v1 \. ^5 k$ c<P><FONT color=#0000ff>/* read result of initialization */ ' l% v8 X! b# `6 i$ r
errorcode = graphresult(); 1 \9 C* C1 P+ m
if (errorcode != grOk) /* an error occurred */ " x# c5 g8 B, O; H5 ~
{ 6 Q2 R4 u2 Q- P& v1 x4 X
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 5 r6 Q) y/ k4 v
printf("Press any key to halt:"); 9 [ S6 P7 o4 N. r; e4 u' Q
getch(); + ^4 b4 {* P( J' t6 T
exit(1); /* terminate with an error code */
$ e @# q/ j: p* B0 w} </FONT></P>
* N+ ]: T$ ]7 q0 Q+ I<P><FONT color=#0000ff>x = getmaxx() / 2; 2 R+ P& G6 l% ?) T" v
y = getmaxy() / 2;
* S& g! O' O6 {/ e1 ^ht = textheight("W"); </FONT></P>
$ E6 s: s/ g4 j, O7 z/ \<P><FONT color=#0000ff>/* select the off screen page for drawing */ g( ?7 U) }) L2 a9 v' A9 Q* K, g
setactivepage(1); </FONT></P>
- Q0 i X$ q% z; h9 J) s0 T F<P><FONT color=#0000ff>/* draw a line on page #1 */
2 Q0 i+ \4 Q. z0 a' F4 Vline(0, 0, getmaxx(), getmaxy()); </FONT></P>
0 n( h( s7 q6 q<P><FONT color=#0000ff>/* output a message on page #1 */
/ v' E3 O( M# y0 v( a5 Osettextjustify(CENTER_TEXT, CENTER_TEXT); 6 C- K* I5 ~8 N
outtextxy(x, y, "This is page #1:");
) \2 @; @7 i x8 n6 m/ Gouttextxy(x, y+ht, "Press any key to halt:"); </FONT></P>1 E3 m4 w( S3 g) l$ Y8 @* ~
<P><FONT color=#0000ff>/* select drawing to page #0 */
8 z* ^" U3 |3 q1 rsetactivepage(0); </FONT></P>
3 y3 O8 a; i+ r<P><FONT color=#0000ff>/* output a message on page #0 */ + j( w9 u0 R5 T( h+ E! E; o* d1 L' N
outtextxy(x, y, "This is page #0."); 1 Y z j) T' X% l/ z* O0 r
outtextxy(x, y+ht, "Press any key to view page #1:"); 9 |$ m6 T; h5 G0 i8 W3 k, u
getch(); </FONT></P>
* u2 b. W3 @, s: r) l<P><FONT color=#0000ff>/* select page #1 as the visible page */ . f" o# b& y w/ B
setvisualpage(1); </FONT></P>
9 q7 H1 u) Q K, _0 v8 {. S- r+ @<P><FONT color=#0000ff>/* clean up */ % N+ k" p2 k" q! ~! C& x: \
getch();
3 z/ H) s- O0 X6 ]# n; Eclosegraph();
) k! a1 N8 ?* b/ K+ creturn 0; . y) p! q7 J! E$ G" _
} * o. e! i3 v9 N( R6 N5 s
</FONT>
3 U9 |# [- B6 }+ I4 i</P>6 R. ]1 j4 g7 i9 ?( Z! ~7 w
<P><FONT color=#ff0000>函数名: setallpallette </FONT>
* B3 J. D# M8 n! I* x/ ^' }0 s功 能: 按指定方式改变所有的调色板颜色 4 i- V) \# \6 _+ ~; r* G/ W! C
用 法: void far setallpallette(struct palette, far *pallette); % D& C9 _: D& {& H' E' _
程序例: </P>
' B* O, p7 q, E X2 ~! z<P><FONT color=#0000ff>#include <GRAPHICS.H>: n2 J8 c H/ M% N+ J) D% Y2 ^
#include <STDLIB.H>
4 o5 h1 n( f' z$ s; y( L5 ^#include <STDIO.H>
- k9 ~- j# P5 h/ V#include <CONIO.H></FONT></P>- }, H3 |7 r8 H! ]9 v
<P><FONT color=#0000ff>int main(void)
1 `/ S+ U0 p5 J$ ~! T' Y{ " v; D; t l* Z5 s1 J: s$ C# G
/* request auto detection */ # H* |# v+ i& K9 V0 K( ~/ F
int gdriver = DETECT, gmode, errorcode; 6 z1 x) I9 C, u N
struct palettetype pal; 9 L& ?: f0 a- q) r1 V
int color, maxcolor, ht;
, h- O3 h1 H sint y = 10; % r" ^% i+ ^8 [
char msg[80]; </FONT></P>
0 `, _% p8 l- j1 E' i<P><FONT color=#0000ff>/* initialize graphics and local variables */
3 z0 g: T1 l r" U. [1 a% vinitgraph(&gdriver, &gmode, ""); </FONT></P>1 H1 h6 s! _) }2 ^" w: q
<P><FONT color=#0000ff>/* read result of initialization */ : q+ v0 `9 Y) F. O9 R
errorcode = graphresult(); ! d4 i9 d$ |6 x6 ]& }! E
if (errorcode != grOk) /* an error occurred */ ; x. k: g ~" @( ]5 D2 V0 |
{
# g' M& [+ R7 H+ Oprintf("Graphics error: %s\n", grapherrormsg(errorcode));
4 W) z# }8 V" K$ P2 Q7 w9 Wprintf("Press any key to halt:");
+ I. c# [; A" G7 ogetch(); 8 f. |9 t& M9 Y5 k- E, \
exit(1); /* terminate with an error code */ . D t J9 F5 T( H
} </FONT></P>: i* c0 S& R3 z* N( r6 {
<P><FONT color=#0000ff>maxcolor = getmaxcolor();
+ b0 S6 X$ a+ H& l! y1 T# \ht = 2 * textheight("W"); </FONT></P>
' x/ h/ q5 F# q0 H7 W<P><FONT color=#0000ff>/* grab a copy of the palette */
; T4 e) G) H0 u9 p; Zgetpalette(&pal); </FONT></P>
e! y9 W6 l0 g+ w<P><FONT color=#0000ff>/* display the default palette colors */ ( K" `% V5 G$ p
for (color=1; color<=maxcolor; color++)
" G$ M/ A: ~; ]{ 1 w. n9 e8 J0 e) Q4 O% b3 Y) I
setcolor(color);
, b3 l: T0 ]9 T2 ?5 L3 ~sprintf(msg, "Color: %d", color); & b0 k: i6 U. r4 ?
outtextxy(1, y, msg);
`- i h9 ~% e) O+ O- S& My += ht; U2 m4 O E- o& l' Y4 F
} </FONT></P>6 ~ a; E* S! S( a9 E1 U8 A
<P><FONT color=#0000ff>/* wait for a key */ ) \3 z7 Z# F# o; ^2 M' ~
getch(); </FONT></P>! K9 D) |( {- j2 f2 |- j7 u
<P><FONT color=#0000ff>/* black out the colors one by one */
2 e- U9 Y$ U0 G. Q9 _) U0 [/ i4 xfor (color=1; color<=maxcolor; color++) & S. W& u# G3 m! c* @) S I x
{
3 U6 h% W' v6 J: y0 Qsetpalette(color, BLACK);
( ^; J9 W4 N* ^0 q; w3 q" Q; pgetch();
$ A2 q( W3 Q H6 L6 ~/ j F} </FONT></P>
$ L. I2 C, b# ^. G* B7 u5 H<P><FONT color=#0000ff>/* restore the palette colors */ 5 G" g" m2 r: p
setallpalette(&pal); </FONT></P>
`5 T. B) C+ W r4 V! r<P><FONT color=#0000ff>/* clean up */
' m% T% n f9 s4 ] ^1 H; R) `getch(); 7 U% e$ q. I5 W8 J# X1 z* z
closegraph();
3 Z3 d/ {: L- M" \3 B/ dreturn 0; 2 R" k& l* T2 f; n s N& k
} : u( l3 h% e/ u1 Q3 H* B& p
5 n, s8 M- g8 e' c0 s. Q</FONT></P>
5 ?% P3 J" o) L |5 L<P><FONT color=#ff0000>函数名: setaspectratio </FONT>
+ f/ S7 d6 ^& ?5 d6 |. E功 能: 设置图形纵横比
7 N7 Z( ^& y4 P T& p用 法: void far setaspectratio(int xasp, int yasp); 1 }0 b5 a. B! l- n* T U9 b
程序例: </P>
1 t* K+ |- ]( _6 k+ L4 Z<P><FONT color=#0000ff>#include <GRAPHICS.H>
! W, v8 o+ m8 @/ p#include <STDLIB.H>: E4 ]$ p- i, a1 S$ S8 s
#include <STDIO.H>
3 `7 a& |: n; T+ h#include <CONIO.H></FONT></P>2 L$ ?. K" Q2 Y! e/ E
<P><FONT color=#0000ff>int main(void)
' s! A7 \9 ~1 n{
- O" J. s# x/ F8 P/* request auto detection */
; f$ W1 o" @( [& wint gdriver = DETECT, gmode, errorcode; % p/ c- H! W! Q3 q
int xasp, yasp, midx, midy; </FONT></P>* c7 ?* s1 {+ |" t% H
<P><FONT color=#0000ff>/* initialize graphics and local variables */ , J: j0 y! q6 P. ?; k# }
initgraph(&gdriver, &gmode, ""); </FONT></P>4 e2 B# E ~. U4 g6 _6 Z. l
<P><FONT color=#0000ff>/* read result of initialization */
, e9 |8 B! ~3 zerrorcode = graphresult();
# T* Z2 H& V8 b4 g+ `' y+ vif (errorcode != grOk) /* an error occurred */
* j. l# |, K$ q, n{
. _# P2 ?& ^# K$ Wprintf("Graphics error: %s\n", grapherrormsg(errorcode)); % z' H" S) @4 D# M$ {; {
printf("Press any key to halt:"); 9 E3 C4 ?* d5 \7 f0 i0 ?
getch();
" K. v! {8 b# P5 I: Yexit(1); /* terminate with an error code */ + l, @$ c. q6 P
} </FONT></P>
- j# y. x7 h7 t$ ~; U<P><FONT color=#0000ff>midx = getmaxx() / 2; ' W* r+ O9 ^$ H5 d/ W0 j* a
midy = getmaxy() / 2;
, Q" b- c% N+ A2 v3 gsetcolor(getmaxcolor()); </FONT></P> G3 \$ i- u8 s' F# ^8 K
<P><FONT color=#0000ff>/* get current aspect ratio settings */
+ [: [- e i0 \; B- {getaspectratio(&xasp, &yasp); </FONT></P>& D& r& V$ M( d# Q# G7 \
<P><FONT color=#0000ff>/* draw normal circle */ 7 Q1 L' u' ]5 g. S
circle(midx, midy, 100); 9 G, @ \. X; c( A. [
getch(); </FONT></P>
+ B8 v0 b W0 J<P><FONT color=#0000ff>/* claer the screen */
; Y& U4 l" U0 Dcleardevice(); </FONT></P>
! Q( ~+ G& W. k! L5 b# W<P><FONT color=#0000ff>/* adjust the aspect for a wide circle */ 1 b4 a+ |' X" C5 A% g$ X
setaspectratio(xasp/2, yasp); . C. O# p- g& Y8 f" t) h
circle(midx, midy, 100); 6 Q$ R5 S( n. ?: }; h# Q0 c
getch(); </FONT></P>
p+ D4 b: f/ X9 a<P><FONT color=#0000ff>/* adjust the aspect for a narrow circle */
8 s1 C6 B1 s. M! u) x8 A# N0 Acleardevice(); ( e' {( ]2 k& ]+ |
setaspectratio(xasp, yasp/2);
2 w9 x- ~7 c2 o) q2 ]5 A5 Ucircle(midx, midy, 100); </FONT></P>
* C( Y" z5 Y! ?3 N% J( r& V5 S; V( F<P><FONT color=#0000ff>/* clean up */ % O' C! D- O* S$ u/ T
getch(); : j6 k3 I: C4 W# f4 G
closegraph();
) q$ x; G& ^0 E/ ~return 0; $ T6 v [2 x8 \7 D# q& H0 G
}
& t: h7 i1 f/ T: M! ?</FONT>
2 {7 T4 ~5 w, }- g5 Y</P>& p; w2 Q* M% Y7 M+ t4 u
<P><FONT color=#ff0000>函数名: setbkcolor </FONT>7 j( k; S$ J' t- ~) P* L7 U
功 能: 用调色板设置当前背景颜色 $ L$ z0 B2 c: _& g, |
用 法: void far setbkcolor(int color);
k- x Y. N6 h0 \2 I程序例: </P>* ^0 D7 w" j* \+ f+ u( y
<P><FONT color=#0000ff>#include <GRAPHICS.H># M- `2 P$ L2 \' [
#include <STDLIB.H>
c# }* W1 S6 E8 M& q9 r#include <STDIO.H>3 y' a; k( P# a: a
#include <CONIO.H></FONT></P>
' k: B3 c9 |3 K& l0 ~% J% D& |" ]<P><FONT color=#0000ff>int main(void) 4 R! b- t& E$ U& M- \" g- H
{ ! B, H( R6 @5 I0 q1 ~
/* select a driver and mode that supports */ * x3 M+ \0 P( e& B+ T
/* multiple background colors. */
; ~, p; ?% m* Oint gdriver = EGA, gmode = EGAHI, errorcode;
, l% C7 ~; q4 g: ]( K' g7 Lint bkcol, maxcolor, x, y; % D& O/ q3 c$ M
char msg[80]; </FONT></P>
" p1 Q/ M& n) e5 i0 p! A5 @2 K<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 d. {* R4 J! `7 s) U0 }8 q" v
initgraph(&gdriver, &gmode, ""); </FONT></P>
* l" q3 c) j9 N3 a9 b4 ]! I<P><FONT color=#0000ff>/* read result of initialization */ " k C/ p9 H5 y N' r2 t$ Y, n/ n4 m2 e
errorcode = graphresult(); % h x. P) W" x2 t' h
if (errorcode != grOk) /* an error occurred */
; r! X( Q' t: c) C3 W: g3 h{ 9 L" ~3 F" ~: K/ l
printf("Graphics error: %s\n", grapherrormsg(errorcode));
% b$ F0 D8 @6 o+ q) X) Tprintf("Press any key to halt:"); 2 `7 J' x' B9 D O+ @0 v8 \6 d( n
getch();
+ i) k( w& _& f7 z, cexit(1); /* terminate with an error code */
v, b* c+ Y' j, {1 N% h} </FONT></P>2 T: [1 Y; m5 `4 l- C7 e
<P><FONT color=#0000ff>/* maximum color index supported */ % \# D8 G3 g/ [/ V8 u# y' A9 l$ |
maxcolor = getmaxcolor(); </FONT></P>
# y: E0 |0 {' h: g0 ?<P><FONT color=#0000ff>/* for centering text messages */ & S% q, x2 |7 U! v! P0 [
settextjustify(CENTER_TEXT, CENTER_TEXT);
% M) p+ \1 r/ Z& Y' {8 Hx = getmaxx() / 2; 8 O a; b8 G% A4 i- W& c- Y# ^
y = getmaxy() / 2; </FONT></P>
. `1 }" [7 h& i7 f: ^, R8 Q<P><FONT color=#0000ff>/* loop through the available colors */
5 w$ j" R. e5 Y& Wfor (bkcol=0; bkcol<=maxcolor; bkcol++)
2 \* U, e2 |; h{ 2 r) F' }& g5 M( B
/* clear the screen */
( g* K B" d$ ccleardevice(); </FONT></P> j# V0 l# I4 B7 A9 U6 |
<P><FONT color=#0000ff>/* select a new background color */ % \% }! @/ E7 m2 W" \
setbkcolor(bkcol); </FONT></P>
0 W; i# k; ?9 Q4 C# p# x<P><FONT color=#0000ff>/* output a messsage */ * O' W8 Y' M3 [1 S* ]/ `
if (bkcol == WHITE)
3 q" T2 @2 w3 gsetcolor(EGA_BLUE);
, B$ I' H6 D. _) Y% Rsprintf(msg, "Background color: %d", bkcol); * O- Y# m3 ?; i
outtextxy(x, y, msg); 7 O" i$ I) T$ K! t" M' W: W" q
getch(); ( y' @. T: o. g5 n. e6 i
} </FONT></P>
) p2 T, ~: K2 j q- {<P><FONT color=#0000ff>/* clean up */ ' g7 z4 Y' F( [% @5 l' n: q4 p
closegraph();
: u+ n6 Y/ Y R" m# l; t9 _return 0; 2 P9 c* Y# _' h7 u; x
} </FONT>
7 D8 m# q$ J( b( b c% m& e
( c& ~/ f$ _5 W# O) Q</P>$ x3 [$ k" U, |+ H
<P><FONT color=#ff0000>函数名: setblock </FONT>
) Y& y+ H5 V6 \( G# h功 能: 修改先前已分配的DOS存储段大小
: @1 s7 u7 j0 L. ?+ K) N2 y7 Y' p用 法: int setblock(int seg, int newsize);
, s% G$ B( W7 I7 ~1 ^程序例: </P>
$ e e6 f+ v& u' m<P><FONT color=#0000ff>#include <DOS.H>8 \7 b* r2 j, |0 ]# x
#include <ALLOC.H>
0 ]9 C$ J! m) k1 t. n4 X#include <STDIO.H> z# {9 h+ \% }5 y* _# B
#include <STDLIB.H></FONT></P>
* \2 N3 `) V- P9 l; l<P><FONT color=#0000ff>int main(void)
( C$ D+ }' Z* c" {( {9 I5 y{ 0 N! `6 N( b$ _9 ]6 m) ]& ~
unsigned int size, segp; 1 T% g! }' a7 C4 H$ ^# H G7 Z3 K+ D2 T
int stat; </FONT></P>
8 W H3 `1 [" r! A+ s! ^: ]1 J" k Q# C7 H<P><FONT color=#0000ff>size = 64; /* (64 x 16) = 1024 bytes */
U( T q& e) R( I6 Ostat = allocmem(size, &segp);
+ x6 j! E* z5 o, t' \8 c* t4 Tif (stat == -1) ) h2 j% a# Q5 {0 }
printf("Allocated memory at segment: %X\n", segp); ' h5 M! d3 Q- Q8 n
else
1 ^! {$ E; _! O5 [{ 7 q1 i6 r) S; R0 ?' H
printf("Failed: maximum number of paragraphs available is %d\n", 0 U3 ]5 k( _8 D$ b: J9 l
stat); 3 N" h) t U1 n4 L' o) O% t# W& q
exit(1);
. `) w4 O, u7 m: G! a. q# B6 ~} </FONT></P>$ {2 W3 O+ ` }0 b# T, P
<P><FONT color=#0000ff>stat = setblock(segp, size * 2); . X: i/ b1 k9 h2 V
if (stat == -1) 0 [( c/ h) Y3 O+ B
printf("Expanded memory block at segment: %X\n", segp);
6 i; }2 @- Z: p7 C2 n+ ~% m3 p3 Z# s; O melse
; P. ^4 s A5 D$ B- iprintf("Failed: maximum number of paragraphs available is %d\n", # R2 e7 H f% Q6 K, P% j
stat); </FONT></P># n& `0 H% y/ d! E8 ]
<P><FONT color=#0000ff>freemem(segp); </FONT></P>- }6 O( z5 \! ~, z
<P><FONT color=#0000ff>return 0; 6 A. x1 O: z! ~& l6 V
}
& |% n% W, A2 u1 ?</FONT>
% v" X) n. G8 K7 n</P>" o( h$ x% ], ^/ ^5 M- }
<P><FONT color=#ff0000>函数名: setbuf </FONT>$ L |6 c, U+ w" B6 B6 ?% d% ]
功 能: 把缓冲区与流相联 ' H1 f% C8 x& c( z. u
用 法: void setbuf(FILE *steam, char *buf); 0 H( L$ D/ N4 y) J" O! w w
程序例: </P>6 h: x/ E. R/ W% } Y
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>2 s9 Q2 ]( z( c- f/ B
<P><FONT color=#0000ff>/* BUFSIZ is defined in stdio.h */ 3 I, n R* e; w
char outbuf[BUFSIZ]; </FONT></P>1 k, e; n. Y$ N9 M$ w
<P><FONT color=#0000ff>int main(void)
. F% F" b# V( m( E$ v/ b4 [# `{ 7 V I. M, h2 {( D. l' r
/* attach a buffer to the standard output stream */ 7 u' |% O8 B6 y
setbuf(stdout, outbuf); </FONT></P> b4 D/ y; W$ ?9 Z9 `! \
<P><FONT color=#0000ff>/* put some characters into the buffer */ 6 R8 k, k6 i8 W9 F E; L5 D% s
puts("This is a test of buffered output.\n\n"); # T. ]" _5 W1 k/ ?6 b3 G. G, F
puts("This output will go into outbuf\n"); 5 F! ]! c0 a4 }% l E
puts("and won't appear until the buffer\n");
% l3 H; A4 T( J1 H( J j+ Q6 E: xputs("fills up or we flush the stream.\n"); </FONT></P>" A U( \1 L8 K. R8 ?
<P><FONT color=#0000ff>/* flush the output buffer */ ( Q& s& r/ ]: J7 Q' m
fflush(stdout); </FONT></P>4 }" L& d6 [& ^" ~; p
<P><FONT color=#0000ff>return 0;
: W6 `6 T% Q3 a6 L# I* W+ v) K}
* Y; f( d3 }& |' C' h' L# `% e, K% [/ w</FONT>
, E7 Z0 t3 \0 l) c</P>2 e- W' B8 |! n' ~7 U) g
<P><FONT color=#ff0000>函数名: setcbrk</FONT> 7 E; {2 P8 i2 g/ s: R k
功 能: 设置Control-break 9 K8 Q$ E1 x& r, x+ s/ r
用 法: int setcbrk(int value); / O- `2 Y' f0 z( B2 P
程序例: </P>8 Z* l8 n% L% M4 r" B7 s8 n9 F: j
<P><FONT color=#0000ff>#include <DOS.H>& e- R% P/ t+ B6 D w
#include <CONIO.H>
4 S+ v' p# G5 a! Z/ _#include <STDIO.H></FONT></P>/ c1 w% c' \6 x5 h! q
<P><FONT color=#0000ff>int main(void)
; n# `& p% `8 }- B" m1 ]3 H% O{ ; O q+ d2 S b7 D2 L* |' g* n
int break_flag; </FONT></P>
1 x( T$ \$ n8 A<P><FONT color=#0000ff>printf("Enter 0 to turn control break off\n");
" l/ J: w% M6 W T& W! Hprintf("Enter 1 to turn control break on\n"); </FONT></P>
; m0 p- t9 p+ E3 M7 i0 Y<P><FONT color=#0000ff>break_flag = getch() - 0; </FONT></P>
4 X: ]8 O( h" X* K<P><FONT color=#0000ff>setcbrk(break_flag); </FONT></P>1 {" z* T# \' ~
<P><FONT color=#0000ff>if (getcbrk())
5 f$ h& H* i' _& p0 T" eprintf("Cntrl-brk flag is on\n"); : T9 y' s5 U7 w( I) c- [- n
else
: B. l; B3 T- W N) \% S3 gprintf("Cntrl-brk flag is off\n");
; \7 A) V( T$ z+ e: `) h' Q0 v% `return 0;
# B! L& h, ~2 v7 N} # O0 W$ E1 N. |) k. B
' ^8 t) T' t+ f4 |
</FONT>
5 L i( p o7 [1 `1 o& _</P>
9 x% E( z( j: |' ?<P><FONT color=#ff0000>函数名: setcolor</FONT> / N* u) h# V1 E# u5 |1 |6 K; g
功 能: 设置当前画线颜色 9 m1 H' u1 h" y0 ~. T7 p, W
用 法: void far setcolor(int color); , b$ O8 I4 d$ s* G
程序例: </P>3 N. }* ~/ \/ C, B8 D" R8 V; C+ O
<P><FONT color=#0000ff>#include <GRAPHICS.H>
* g3 n' h) T3 e a5 h# F#include <STDLIB.H>
! l2 [3 n; ~& ^& F; A0 R#include <STDIO.H>2 s" l7 U( }$ q b
#include <CONIO.H></FONT></P>3 O0 \! W; C0 ]$ g3 X: x, b
<P><FONT color=#0000ff>int main(void)
1 D, E7 t& U1 Z9 i3 `5 q e% o6 |{ # c$ j$ S( \2 p; U/ W. U
/* select a driver and mode that supports */ 7 g6 a3 z# U# m- h4 @* e: D
/* multiple drawing colors. */
! O( Q) G- y0 T$ H2 n$ G& C2 Nint gdriver = EGA, gmode = EGAHI, errorcode;
3 h# V6 A* y: z! N+ q' Qint color, maxcolor, x, y; ) Q$ d: `2 ?. r, R) u/ q
char msg[80]; </FONT></P>( R. l+ v4 d0 n2 O5 e" S3 ^3 y+ E) Q
<P><FONT color=#0000ff>/* initialize graphics and local variables */ - k* o- t6 x( B
initgraph(&gdriver, &gmode, ""); </FONT></P>- g& N' `% X0 z b1 `$ e
<P><FONT color=#0000ff>/* read result of initialization */ + M& y ~# ~7 M3 M y
errorcode = graphresult();
( E+ V2 T9 B' p3 G8 e; D6 j$ Bif (errorcode != grOk) /* an error occurred */
# U* G( { W# L- g& R# c! S5 E{
: ]* w( ~) J4 Rprintf("Graphics error: %s\n", grapherrormsg(errorcode));
0 M) C& s6 C2 _! q9 s2 {1 sprintf("Press any key to halt:"); : [/ L8 A. w D8 d! q
getch(); . v8 d0 Y4 {5 a4 `- e$ T9 Q& c
exit(1); /* terminate with an error code */
; [* l, B# n+ I+ Y/ ~% I- M$ g} </FONT></P>. w t! Q% I& h0 M% S
<P><FONT color=#0000ff>/* maximum color index supported */ ) P2 S2 h! J! Y! _
maxcolor = getmaxcolor(); </FONT></P>
1 h, E: H& v6 N$ n7 f9 G( V) n! t9 i<P><FONT color=#0000ff>/* for centering text messages */ 0 ]: E4 b4 C* V9 k' l
settextjustify(CENTER_TEXT, CENTER_TEXT); : f: K1 V; {+ M; P
x = getmaxx() / 2; ! V: K% ~' y5 U! H5 v; m. n) P
y = getmaxy() / 2; </FONT></P>
( O9 x0 G* [1 X" Z# ~<P><FONT color=#0000ff>/* loop through the available colors */
c5 i8 r% o0 g/ Rfor (color=1; color<=maxcolor; color++)
; _ h6 [1 E% v# C, a{
s6 g. ^% b2 _& ^/* clear the screen */
0 d1 Q7 _$ I4 w& |& ^0 W' n/ \$ }cleardevice(); </FONT></P>; Y' \' s( E1 L, r9 o0 P. C$ |
<P><FONT color=#0000ff>/* select a new background color */ / @/ n) d) l1 N0 }( h
setcolor(color); </FONT></P>
0 R7 q; ^2 b/ L/ Y! T8 s<P><FONT color=#0000ff>/* output a messsage */ ' z0 V, @; d, e4 @0 H9 G" u* p- d
sprintf(msg, "Color: %d", color); 3 ^. p0 {! I1 D: ]
outtextxy(x, y, msg);
$ K W: Q( o+ {& {# U) e( \getch();
; J) I { r" S" \( V2 N) D; B} </FONT></P>; j) k- T; y4 J# N: W
<P><FONT color=#0000ff>/* clean up */
+ X7 i1 z$ R* g8 @7 O+ eclosegraph(); 1 p. N, O' B. R' F; t
return 0; . \6 y( g9 q" D& t+ v
}
; U" D/ z# x: H3 C/ ^4 N9 L</FONT>, x( A7 r) K4 [* O% {/ B
</P>
3 n) r, \0 s7 J& X<P><FONT color=#ff0000>函数名: setdate </FONT> u# y/ {: S6 \8 N% c
功 能: 设置DOS日期 ! g/ K" p0 p3 A& p; Y/ k1 z
用 法: void setdate(struct date *dateblk); + E, I# N4 I y9 ]; v
程序例: </P>! P* ^ k7 |$ P* } z
<P><FONT color=#0000ff>#include <STDIO.H>
8 h8 ]7 x, l* b& y#include <PROCESS.H>& e' r+ c7 L! w5 r/ m a( D( l2 o! n
#include <DOS.H></FONT></P>' B2 y* g o% o. H$ a' k' ^
<P><FONT color=#0000ff>int main(void) 8 E5 G* P( ]9 z; b$ W3 P
{ & E) f: F" t; X+ Z
struct date reset; 7 u/ Z( F: A x& l6 ` F# ?/ R
struct date save_date; </FONT></P>
, ?; O# q4 d g4 p<P><FONT color=#0000ff>getdate(&save_date); p& \2 I% B+ j- g5 T* O
printf("Original date:\n"); 1 {6 j* e, `/ i
system("date"); </FONT></P>
/ [! T' R5 ^& R8 b. Q$ @1 R<P><FONT color=#0000ff>reset.da_year = 2001;
1 u& P: w" o8 r4 C! p2 v8 creset.da_day = 1;
8 W7 W1 w6 m! z# Rreset.da_mon = 1; ; I7 l5 n. o* D( y0 K' ^2 m. y. z
setdate(&reset); </FONT></P>
0 [% H+ d# b6 z4 ], X<P><FONT color=#0000ff>printf("Date after setting:\n"); ; |2 b- R0 p5 i+ K r0 Q
system("date"); </FONT></P>5 q* t( X6 ~* Q& u. Q: _) Q0 Y$ b
<P><FONT color=#0000ff>setdate(&save_date);
1 G4 P" \4 u) t6 J* e' W5 Yprintf("Back to original date:\n"); + ~1 V0 g9 P) g3 X+ z$ V" g5 T
system("date"); </FONT></P>3 H- u( b. E5 z) ~
<P><FONT color=#0000ff>return 0;
{ o( h% ]( |4 I3 h. \7 @} / t# _7 Z' Z- K, B6 }. @
</FONT>
8 Y; v `5 [! \5 m A, V% o</P>
# ~/ t' G) T/ @% U* u1 R<P><FONT color=#ff0000>函数名: setdisk </FONT>
( ~; M9 `" z, y/ z: s) c; k* z; P功 能: 设置当前磁盘驱动器 * Z/ D+ n+ c# X/ K+ t8 b
用 法: int setdisk(int drive);
+ q! N4 ~$ t& W! P程序例: </P>$ l% Y" n ~* w" Y' v
<P><FONT color=#0000ff>#include <STDIO.H>
9 ?2 w( Q% Y4 g, N2 `3 D6 X2 ~5 t8 J#include <DIR.H></FONT></P># N! ?" _! v- z6 Z8 p
<P><FONT color=#0000ff>int main(void)
8 z* b# r. i6 J/ T{
6 C H, b- C! Uint save, disk, disks; </FONT></P>: \/ I: j! Y% R
<P><FONT color=#0000ff>/* save original drive */
: a7 j0 B7 i2 ~4 ]+ ^save = getdisk(); </FONT></P>! h5 L A# O( W m4 J+ x
<P><FONT color=#0000ff>/* print number of logic drives */ 5 s' n( D: R3 ]2 x( Q& Q
disks = setdisk(save);
( Q% \) D2 ?( |printf("%d logical drives on the system\n\n", disks); </FONT></P>( k; Z8 K& r- C% j' y5 I
<P><FONT color=#0000ff>/* print the drive letters available */
- p: x4 [1 Y1 q8 ?! _6 x+ S+ Aprintf("Available drives:\n");
& f0 p1 g* ^. ifor (disk = 0;disk < 26;++disk)
% h! `! I, u' C: C9 b{
, R& l% }8 B: ~1 W6 J c6 T. M% zsetdisk(disk);
2 d. p0 T8 W, V, e7 tif (disk == getdisk())
* o0 d3 O1 I4 i, _ z! W( C, K$ Lprintf("%c: drive is available\n", disk + 'a'); , }- k3 o4 T7 y4 Y; _/ y5 ?( I
}
3 j& ?2 s# b( F! h2 e, {setdisk(save); </FONT></P>
# s5 Q' {$ D- d<P><FONT color=#0000ff>return 0; 1 N+ }) s: `6 A
} </FONT>$ Z, ]+ r7 n& p( G" v
' F" k. a I5 ~. C2 E8 J</P>8 W" n; o! o, j9 [8 N
<P><FONT color=#ff0000>函数名: setdta </FONT>
+ _; I5 I4 E( n% U: j% M功 能: 设置磁盘传输区地址
. n; e6 e( A; ?- G用 法: void setdta(char far *dta); ) t8 X- A& S7 p0 q4 W
程序例: </P>
8 ?7 c% p8 v1 {2 X4 b* D: P& b<P><FONT color=#0000ff>#include <PROCESS.H>% x$ k: I) x% f8 M% z; _
#include <STRING.H>, E R0 E8 G+ O) l
#include <STDIO.H>
. ?: x" o8 A7 A3 ^5 F#include <DOS.H></FONT></P>) U' o9 q) a1 y! ~6 `
<P><FONT color=#0000ff>int main(void) " c! f& U" G6 K! P
{ $ ~- N, ^/ J; {- e. U5 N: L
char line[80], far *save_dta;
/ _% Y' d: w1 n0 U8 Lchar buffer[256] = "SETDTA test!"; T K9 @# w% u3 \0 O
struct fcb blk;
3 w: [2 e7 g0 [5 B( uint result; </FONT></P>
. A5 `) b1 f+ a) ^6 y$ n& O9 i5 A$ z<P><FONT color=#0000ff>/* get new file name from user */ * D4 U' t! [- r# i
printf("Enter a file name to create:");
7 J1 F9 e9 w/ f, n3 Y5 {! ]gets(line); </FONT></P>5 J5 `2 ~( e3 b6 k4 y; O4 ?7 n
<P><FONT color=#0000ff>/* parse the new file name to the dta */
/ Y0 P. i( ~' [6 `+ I6 Uparsfnm(line, &blk, 1);
5 P8 \5 }6 ?; J7 m T0 yprintf("%d %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>' M/ D3 c; I4 M2 q! Y* P3 M- ^2 z+ d0 _
<P><FONT color=#0000ff>/* request DOS services to create file */
& l6 I( O8 O V" r4 zif (bdosptr(0x16, &blk, 0) == -1) . H* Z( @1 z+ ~( w* A7 Z# a
{
4 U# Y! f2 q' r( R& H Gperror("Error creating file");
2 W8 V1 F. x' jexit(1); 8 a. e# X* T' [
} </FONT></P>
/ c9 c& f" n6 M. D' p8 v( y<P><FONT color=#0000ff>/* save old dta and set new dta */ # @3 _0 @4 N, ]- E6 q! r
save_dta = getdta();
& Q# {: \0 u" _0 L3 P+ Xsetdta(buffer); </FONT></P>) e: Q' {. r$ V- w( o5 `/ i2 n' Y
<P><FONT color=#0000ff>/* write new records */ ! u) J8 N& Z' `% @0 `7 ]2 j
blk.fcb_recsize = 256;
( r" B3 \7 H' J: Kblk.fcb_random = 0L; % x8 K, T, y, W; J
result = randbwr(&blk, 1);
/ ?- n E% m+ iprintf("result = %d\n", result); </FONT></P>
6 ]3 H' M% s: e J. x* f<P><FONT color=#0000ff>if (!result) # `, s0 n: l( Q7 \$ S8 l3 T7 c! Q
printf("Write OK\n");
8 _1 `% |8 ^. I: celse
0 S; ~2 _& ]# c; Y{
, ^; O& Z" \: H( N5 zperror("Disk error");
! e; {2 a0 L" G0 J& i. P% d5 Qexit(1); ' L' R0 O$ v7 E, ]3 A, M Q* \
} </FONT></P>4 G6 s) t! W( f
<P><FONT color=#0000ff>/* request DOS services to close the file */ : i n% U7 z# \! F( }
if (bdosptr(0x10, &blk, 0) == -1)
5 A( C# p Q6 ~! q1 d{
+ H! E1 {* ~4 N! L h" k( I, vperror("Error closing file"); " T# Q8 z- K9 v5 X3 E
exit(1);
1 P0 |7 M% J, y1 H" z9 ~} </FONT></P>
( w& D3 Y8 v- B) z- T8 O5 s+ q<P><FONT color=#0000ff>/* reset the old dta */ 1 n9 P& v7 c# k x
setdta(save_dta);
. }: h0 Q. |- y- n* o! d9 G1 a" kreturn 0;
# C3 K% p( o i. g} ! Z, f- b1 L7 W$ ^9 b# |6 E& h
</FONT>2 U- m+ Y- G! r( G- \
</P>- N3 `& k$ |% }, v1 F$ f
<P><FONT color=#ff0000>函数名: setfillpattern </FONT>
+ P! i$ j9 `+ ^/ j8 K2 U功 能: 选择用户定义的填充模式 - M& X" d$ }/ \( R, B O2 b
用 法: void far setfillpattern(char far *upattern, int color); ' l5 F; Q$ U) A0 `; C, R
程序例: </P>' C9 Q; `" {7 Y; {5 k5 U1 t
<P><FONT color=#0000ff>#include <GRAPHICS.H>
$ Q8 e, k$ X' f. Q0 q; Z#include <STDLIB.H>
/ z4 g3 N8 S$ r3 b- s4 q#include <STDIO.H>+ X+ B1 ?! \7 q6 b0 I, W
#include <CONIO.H></FONT></P>8 D; i8 G/ z5 b/ d! w7 e! p0 m" {
<P><FONT color=#0000ff>int main(void) + }0 T/ e* Z. U2 |) w2 f4 W
{ / w. ~: v9 k5 G5 t1 n
/* request auto detection */ 4 Z, V. I. m9 e6 G
int gdriver = DETECT, gmode, errorcode;
0 l9 B/ H- X2 s/ `( k% Vint maxx, maxy; </FONT></P>
" N5 q c3 b% ?6 H% A+ r4 I1 P- d% X<P><FONT color=#0000ff>/* a user defined fill pattern */
- }. `1 X" r. ^0 L6 K6 r" T- achar pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; </FONT></P>9 \/ Y- q% v1 g6 U/ G4 W" l8 M2 W
<P><FONT color=#0000ff>/* initialize graphics and local variables */
. X1 n+ o5 D% y2 r4 {: binitgraph(&gdriver, &gmode, ""); </FONT></P>2 @* a( @+ P" S2 L3 r% R
<P><FONT color=#0000ff>/* read result of initialization */
! D: a: l, ~* q. Z% kerrorcode = graphresult(); ) i8 b& `: w; o) l/ T- ~+ a% K
if (errorcode != grOk) /* an error occurred */ 2 S$ `6 }; @- i, @
{ 4 o) A; W" `( q! Y* \7 p* g# {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
- N9 S9 f9 J( F4 n6 cprintf("Press any key to halt:"); ; P' K7 R. x5 g! ?2 E* c E
getch(); 0 p% \% h1 z) P' h
exit(1); /* terminate with an error code */
' J5 \$ Z3 K1 J# |} </FONT></P>
! s5 b1 W g. {<P><FONT color=#0000ff>maxx = getmaxx(); 8 w$ v: q- W5 U- g3 b! N4 y
maxy = getmaxy(); & e1 ^3 i" A; k F
setcolor(getmaxcolor()); </FONT></P>
! F t! e3 q# j4 D# p7 d6 s<P><FONT color=#0000ff>/* select a user defined fill pattern */
$ E" T9 K, M. G- qsetfillpattern(pattern, getmaxcolor()); </FONT></P>: c( _3 `( C) m/ v
<P><FONT color=#0000ff>/* fill the screen with the pattern */ ' e2 j) E$ R3 @/ Q8 g
bar(0, 0, maxx, maxy); </FONT></P>
; L6 N/ _7 C: @! D7 Q( b<P><FONT color=#0000ff>/* clean up */
0 u2 ?8 p# l* j# Tgetch(); . D, t" e3 F, b& t& p. r5 u( }; b- Z8 l
closegraph();
* {0 `) P" S4 [' j# Z# yreturn 0; # H) _' W" D4 O# F/ q* y
}
6 s* f; i3 Z7 D9 O# t</FONT>5 k6 L; k; g+ ^0 }
</P>
& I& F1 l! W+ T7 X; v6 o<P><FONT color=#ff0000>函数名: setfillstyle </FONT> W$ Q( T% q& w5 B( c
功 能: 设置填充模式和颜色
' d4 |. M, d% Z1 g5 s# z7 ~+ ?! q4 A: m用 法: void far setfillstyle(int pattern, int color);
! X2 T$ \$ G% W# V( y程序例: </P>; I& c; g+ V0 Y, w* h: J
<P><FONT color=#0000ff>#include <GRAPHICS.H>1 E L' |: H/ @; S+ p$ _2 j/ `
#include <STDLIB.H>) z1 P3 M4 e2 l7 z# q I# |
#include <STRING.H>
$ j# _2 E) I& }( w* {7 r# \; U#include <STDIO.H>/ f! G0 l# f3 M4 [6 A; |7 {
#include <CONIO.H></FONT></P>
: L, S' D. s# B/ J9 t% H<P><FONT color=#0000ff>/* the names of the fill styles supported */
0 }' V( x2 ^4 p; mchar *fname[] = { "EMPTY_FILL",
% J( N$ _: G/ d" w' t% I) ]% w: o( C"SOLID_FILL", % u$ j Q' f0 m; o$ ^5 `
"LINE_FILL", % p& R! p1 f1 {, D1 k% {% z& A3 t
"LTSLASH_FILL", $ r) r' Q* X- l" ?) z F) {# h" A
"SLASH_FILL", " p+ M0 X9 s' b9 G& X
"BKSLASH_FILL",
* U) O; Q" E6 }) I"LTBKSLASH_FILL",
; a. H+ W/ z6 J"HATCH_FILL",
, ~, |6 i+ z$ e9 k1 x/ y"XHATCH_FILL", 3 h/ G8 _. X$ f1 [
"INTERLEAVE_FILL", # m* g0 @ n# C+ u
"WIDE_DOT_FILL", ) O! d6 H% b3 V3 h" f; f# e
"CLOSE_DOT_FILL", ; N$ k0 R. \5 E; J/ |/ {
"USER_FILL"
1 }% a! g% A R: X* ?}; </FONT></P>
( F9 P, z$ y7 q6 O$ _; @5 ~& t( X<P><FONT color=#0000ff>int main(void)
1 i2 L, ^1 ]0 k+ B- L) J6 Q{ # S$ g7 m: Y7 s6 o+ r# z
/* request auto detection */ % |5 a! h7 z+ I0 D
int gdriver = DETECT, gmode, errorcode;
3 v7 F2 s3 s$ A) D7 N0 Xint style, midx, midy; ; a( l, Q, Y& k z3 L8 j" d3 w d' v
char stylestr[40]; </FONT></P>
Q) a0 Q$ A* ^' K0 |0 [<P><FONT color=#0000ff>/* initialize graphics and local variables */
* Y) d0 V9 T0 t# M# iinitgraph(&gdriver, &gmode, ""); </FONT></P>. b( [2 q2 n+ |% o4 [9 g
<P><FONT color=#0000ff>/* read result of initialization */
4 r! R. T# P! j7 U' n0 ^% Uerrorcode = graphresult(); * V% ?' b' Y2 P( U4 e! j
if (errorcode != grOk) /* an error occurred */ " N( X0 i6 v% K! [' \: R, u {) ^
{ 0 d8 |1 b! w# U' G7 T" ^! f+ Z" b
printf("Graphics error: %s\n", grapherrormsg(errorcode));
2 \; O* @% j/ ~+ ~4 p* [$ ^: zprintf("Press any key to halt:");
& Q" h$ v$ q! b0 n* u/ B; E& Z1 lgetch(); / i2 v, D$ L4 R. `/ D
exit(1); /* terminate with an error code */
, k% e+ d1 V. J; Z} </FONT></P>( t( H# y( _, Y: M
<P><FONT color=#0000ff>midx = getmaxx() / 2;
* ], e6 Q0 N; m- w6 Xmidy = getmaxy() / 2; </FONT></P>
: B# I) P! J u* E* e6 r. D. a1 X8 X<P><FONT color=#0000ff>for (style = EMPTY_FILL; style < USER_FILL; style++) 2 S+ R5 K! Y2 v k# i0 i& B
{ $ F8 F) X: p2 Z; V
/* select the fill style */ l9 e3 ^9 v( L4 c& g& w
setfillstyle(style, getmaxcolor()); </FONT></P>
5 G4 O' |! g; s u5 f<P><FONT color=#0000ff>/* convert style into a string */
/ j, Y* V: D, |3 C6 v0 Lstrcpy(stylestr, fname[style]); </FONT></P>
& u+ X% B' e# e5 h9 q G" w<P><FONT color=#0000ff>/* fill a bar */
- ^, n, J9 T; h$ g9 Wbar3d(0, 0, midx-10, midy, 0, 0); </FONT></P>
) Q; F- ^9 F9 }& {7 k1 a<P><FONT color=#0000ff>/* output a message */ / ]% Z7 Q1 C" |& L @+ A
outtextxy(midx, midy, stylestr); </FONT></P>
: ^6 Z7 f+ B& ~# z; M<P><FONT color=#0000ff>/* wait for a key */
6 [& ^1 v. F6 ^! N% g" Igetch();
0 U! i) n0 h- F' N$ Y9 g4 N6 Gcleardevice(); 3 p: ^& V- Y$ \8 \
} </FONT></P>+ g0 Q( i/ H0 U- k+ w
<P><FONT color=#0000ff>/* clean up */
1 {2 S# o4 \) x% @getch();
/ k6 M) Y9 c" A4 W% e- u0 Vclosegraph();
% N7 y$ ]& }( [0 G X! f5 Z' ereturn 0; 1 l) \! S2 t4 y. s+ B5 E' n
} </FONT>
- Z5 c* k8 y2 T
3 G( w1 S5 n# D6 W V; N5 w) K</P>
; A" h4 B* L' X( R) g V( j<P><FONT color=#ff0000>函数名: setftime </FONT>
0 X3 V, X" a z5 d' s$ K功 能: 设置文件日期和时间
) D: ^: @& [" f, ]- @用 法: int setftime(int handle, struct ftime *ftimep);
# R8 r, J; }3 \7 N程序例: </P>3 F" f) J( P6 y7 ^4 \
<P><FONT color=#0000ff>#include <STDIO.H>: A5 D6 T1 t F9 W( W/ q4 Z
#include <PROCESS.H>
O& d9 B( `* d* d7 }#include <FCNTL.H>! U2 t: V( w0 K4 D& f' @# m
#include <IO.H></FONT></P>1 b# f1 c) N F3 E
<P><FONT color=#0000ff>int main(void)
6 j, ]. K5 e5 t: R{
3 }; m- V5 Q0 Zstruct ftime filet; 9 [2 F4 l# g; z2 g$ [4 ]( f3 l
FILE *fp; </FONT></P>
$ F* k$ t* @& _: b<P><FONT color=#0000ff>if ((fp = fopen("TEST.$$$", "w")) == NULL) 3 d% t6 R) g- @# h( d4 \
{ 9 V) G9 R# [6 h) [! ]/ U
perror("Error:"); - X/ u2 C: i& P9 y' U& D
exit(1); + e' ?7 w' t: U5 \( j, G8 b* p3 S
} </FONT></P>' N" \: h! u% A& Z- l
<P><FONT color=#0000ff>fprintf(fp, "testing...\n"); </FONT></P>
! b! s3 V) x9 l" @) s5 W( p<P><FONT color=#0000ff>/* load ftime structure with new time and date */ 9 z: M7 `0 U8 ]# v% U( N" k
filet.ft_tsec = 1; 8 Z$ `+ F5 w6 J6 Y
filet.ft_min = 1; + c% [0 D8 A8 x' s
filet.ft_hour = 1;
. h# o' [. `' Q; M+ z) hfilet.ft_day = 1; % ^& G; ^/ m0 A+ G; `; R4 Y5 |3 b' v |
filet.ft_month = 1;
! V$ H u5 @" H$ k* |filet.ft_year = 21; </FONT></P>6 O) I* ]1 q% s% K5 C
<P><FONT color=#0000ff>/* show current directory for time and date */
& {3 x8 W( k0 K9 nsystem("dir TEST.$$$"); </FONT></P>, O- s9 J8 v0 p* x( v4 J
<P><FONT color=#0000ff>/* change the time and date stamp*/ ) T' H& ?$ B# P2 F: K: u( p0 p
setftime(fileno(fp), &filet); </FONT></P>
/ B: ^. R3 V9 A* n" u( j( F<P><FONT color=#0000ff>/* close and remove the temporary file */
- P6 p. E# e1 o E5 Qfclose(fp); </FONT></P>
6 @' C' q) D9 W; x% @1 }, Y/ c<P><FONT color=#0000ff>system("dir TEST.$$$"); </FONT></P>+ l' b5 ] V. l, L* e% }
<P><FONT color=#0000ff>unlink("TEST.$$$");
- m! G2 Y/ Y+ V2 x+ hreturn 0;
" I4 N P8 N3 k% h6 D} # Y' K5 X# }9 T4 S% L. D
</FONT>5 j% V8 k M2 j- ]- @: S
</P>+ `0 F: R/ t+ d* u/ s
<P><FONT color=#ff0000>函数名: setgraphbufsize </FONT>* l+ T7 f0 N4 B' ^
功 能: 改变内部图形缓冲区的大小 : a8 c5 i* d- f e2 j! O
用 法: unsigned far setgraphbufsize(unsigned bufsize); 2 t; M; d4 ^) E9 M; k
程序例: </P># P& k& ~3 Y1 d' R% x6 ?7 [/ B+ d
<P><FONT color=#0000ff>#include <GRAPHICS.H>5 n# s8 i0 x& B9 X* o4 Q# R* S# x7 ^
#include <STDLIB.H>
) `5 T4 v% o( q- g7 P& v; t#include <STDIO.H>
@) z$ F( [2 S0 R#include <CONIO.H></FONT></P>
4 E9 s& V9 I' b6 }<P><FONT color=#0000ff>#define BUFSIZE 1000 /* internal graphics buffer size */ </FONT></P>
" u) I7 l. F9 a( i, s<P><FONT color=#0000ff>int main(void)
* J8 c" t3 z2 F% |{ . n3 J$ M* O, o3 n
/* request auto detection */
, A$ x* d) x, E" l4 Sint gdriver = DETECT, gmode, errorcode;
& v: k$ Q/ P3 f# Q3 }int x, y, oldsize; / g$ A8 k4 R5 @9 o
char msg[80]; </FONT></P>
" o0 ~4 ^! ^6 E8 `% k: A: Y3 c<P><FONT color=#0000ff>/* set the size of the internal graphics buffer */ / b4 a$ f3 g- \9 q& _
/* before making a call to initgraph. */ 4 H2 T4 p8 W8 y; m* K! ?
oldsize = setgraphbufsize(BUFSIZE); </FONT></P>* F. ~8 Q$ ^* x- g+ L7 F
<P><FONT color=#0000ff>/* initialize graphics and local variables */
- L/ d" c. f" dinitgraph(&gdriver, &gmode, ""); </FONT></P>
; M9 p8 P' X1 M# {2 Y: p& w<P><FONT color=#0000ff>/* read result of initialization */ ; m; F2 M2 b# q+ @. F
errorcode = graphresult();
; O3 B+ @: t+ M5 a- i9 C, `if (errorcode != grOk) /* an error occurred */ * q1 H' A8 i4 e+ T+ k" }+ w
{ 3 U" e* }6 \" O8 p
printf("Graphics error: %s\n", grapherrormsg(errorcode));
& I" f0 n e/ Z2 t$ M4 X kprintf("Press any key to halt:");
2 L: @' @# Q+ t+ x0 }6 Ugetch();
2 L4 N9 J' D' W) R+ a1 pexit(1); /* terminate with an error code */
: E9 N& b b7 f. u* ?9 f$ h} </FONT></P>
4 h) T6 l1 l) [ o. B<P><FONT color=#0000ff>x = getmaxx() / 2; 7 H7 t$ V. Q7 s# Z9 U
y = getmaxy() / 2; </FONT></P>
- I# ~" T) l' M+ T2 H( [" C<P><FONT color=#0000ff>/* output some messages */ . }; [, {6 y$ w0 _/ T
sprintf(msg, "Graphics buffer size: %d", BUFSIZE);
/ o4 d, s6 q# g3 csettextjustify(CENTER_TEXT, CENTER_TEXT);
8 H1 z, O# z: s( b6 p6 c* y% C( Uouttextxy(x, y, msg);
8 K7 D" q" D% b( I. zsprintf(msg, "Old graphics buffer size: %d", oldsize); 2 y" d5 f: ^8 G/ A" R
outtextxy(x, y+textheight("W"), msg); </FONT></P>
6 ?4 l& z$ J! [+ m% O: z<P><FONT color=#0000ff>/* clean up */ $ a5 e2 C8 ^" t2 K% g3 n; v* ~
getch();
, N7 F4 {8 c6 W2 o8 q7 A* ~+ }closegraph();
7 u% @/ w$ o7 {2 `4 x3 N/ ]" x- N5 y6 ureturn 0;
1 [4 ~0 |/ _9 a& Q. ^}
# S8 h1 A) F2 ?% F
8 ?1 s' A$ H% ?: h- K
: l$ q! |! T0 ], M/ B</FONT></P>
; k; Q; S- S7 S R& H7 J% C<P><FONT color=#ff0000>函数名: setgraphmode </FONT>
0 C' f- ]& j+ v/ N8 k6 u功 能: 将系统设置成图形模式且清屏
, i1 F' M7 ~9 |; h! N用 法: void far setgraphmode(int mode); ) H8 u, k7 c$ \! w/ N, r" k5 [
程序例:
/ f; X: n! W% ~& |, u- d2 `% p
. g# M g( P h/ P; Q }# u<FONT color=#0000ff>#include <STDLIB.H>9 z( H8 \; y8 O, `' @! R$ W
#include <STDIO.H>& N' V) ]8 A) q4 U
#include <GRAPHICS.H>#include <CONIO.H></FONT></P>6 e4 `% B/ i& i5 l0 A
<P><FONT color=#0000ff>int main(void) : I9 @1 m) Y: X2 z+ t. P
{
; t, ]5 u" h# f3 N0 C/* request auto detection */
: w& r- z2 N( @9 F5 J0 b2 qint gdriver = DETECT, gmode, errorcode; V( I! @! ^; {( }2 P1 ]
int x, y; </FONT></P>! Y+ q: J) J% R* J8 g0 `' ?+ }- `
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 4 t3 G( q0 }% O8 m3 G) `, d
initgraph(&gdriver, &gmode, ""); </FONT></P>" |* f) s" H3 T3 t
<P><FONT color=#0000ff>/* read result of initialization */
$ A2 p$ V' O0 e. E! d! c z0 ?errorcode = graphresult(); ' c& a' H8 Y+ ^5 [, [; D$ W
if (errorcode != grOk) /* an error occurred */
: I8 Y$ b& O4 _( g1 ?8 N{
/ ^2 L1 z/ a1 B. _printf("Graphics error: %s\n", grapherrormsg(errorcode));
6 r. e0 o5 a* x1 L, z6 i3 zprintf("Press any key to halt:"); 3 G9 @* ? ?& Z7 x- I) |$ f& p
getch(); 7 J7 u% Y6 W1 P. `
exit(1); /* terminate with an error code */ 4 m. S; H/ C/ b( G
} </FONT></P>& _1 s' t. m$ s. M( @+ S) _% V
<P><FONT color=#0000ff>x = getmaxx() / 2;
4 ? _( P# x% ]* fy = getmaxy() / 2; </FONT></P>
0 X" m# A. p6 u& I: U+ v<P><FONT color=#0000ff>/* output a message */ 2 ?* o' n' S6 Y0 ~5 x1 Q, v) e- j. b
settextjustify(CENTER_TEXT, CENTER_TEXT); 9 N. \/ H% S8 k( Z
outtextxy(x, y, "Press any key to exit graphics:");
* L3 R% c9 ^& S$ V. {2 @, Sgetch(); </FONT></P>2 Q7 z/ B5 z' V6 O' g# r7 |" ~
<P><FONT color=#0000ff>/* restore system to text mode */
8 i( ` u' `4 ` j: D V/ J) rrestorecrtmode(); : I' b* `8 V# d% F+ p+ |
printf("We're now in text mode.\n");
. i; J; G _, B( {7 F6 r$ N1 hprintf("Press any key to return to graphics mode:"); . G( J8 R& w4 q; u+ a
getch(); </FONT></P>5 D6 q9 t& D$ k" d V5 F. C
<P><FONT color=#0000ff>/* return to graphics mode */ ; i; O- I( h) ^! F- L$ x
setgraphmode(getgraphmode()); </FONT></P> L' j+ C S6 _5 _0 |0 P. N" ^
<P><FONT color=#0000ff>/* output a message */ $ H/ v. i# r0 p9 v' y/ l! \
settextjustify(CENTER_TEXT, CENTER_TEXT); # t$ V, m/ `7 Q
outtextxy(x, y, "We're back in graphics mode."); * R' D% u+ i [8 x
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>. E1 X: V. i$ T0 k' W4 y- Q+ W
<P><FONT color=#0000ff>/* clean up */
* D2 c+ O- q# d6 X8 fgetch();
$ A' O3 }3 {+ L, l4 t( o$ F* A7 Z: fclosegraph();
8 O. R6 ^4 O5 d/ b$ N/ @* `8 Vreturn 0;
' c) v- ~4 M u$ O: q" I1 E}
" ~# p. c: E$ e2 R% i
5 @3 T* s% n6 G
; k& t, `+ A4 K* a</FONT><FONT color=#ff0000></FONT></P>6 l E! ~5 D8 T& g
<P><FONT color=#ff0000>函数名: setjmp </FONT>/ [- x5 |1 I b7 y
功 能: 非局部转移
' f4 U: g& f' r- Z7 F5 q" @用 法: int setjmp(jmp_buf env); : z4 e G* L# o. _2 H/ j' J' Y
程序例: </P>
1 p* h+ ]* y* d/ x<P><FONT color=#0000ff>#include <STDIO.H>
q. x, r: k; T, ]#include <PROCESS.H>
! M4 H; V2 Y, D/ T! p#include <SETJMP.H></FONT></P>! ~8 t: }( u: x
<P><FONT color=#0000ff>void subroutine(void); </FONT></P>
Q5 S( \$ B% n. a& c( L<P><FONT color=#0000ff>jmp_buf jumper; </FONT></P>
; } X0 F+ p b! ^9 a<P><FONT color=#0000ff>int main(void) & q5 u0 c0 h4 Z- p# o6 t+ d
{ # A, d8 x& Z# F9 W$ \9 }3 E0 ~
int value; </FONT></P>
: d8 F1 Q. S a- Q8 z<P><FONT color=#0000ff>value = setjmp(jumper); * i& i$ h; X& W2 v B: }
if (value != 0) 2 W* c( J/ I6 L! b- A+ U9 V& e1 U$ \- F
{
* y3 L* o, \1 j, a& `& Q6 Wprintf("Longjmp with value %d\n", value);
) |% \6 m' z+ _: R1 \exit(value);
" f% X7 e+ W. {1 t} + `, o; S! V% ^3 W! e
printf("About to call subroutine ... \n");
+ Q" D* Y: t/ \subroutine();
8 C8 h: A3 F. Y7 `1 [! Freturn 0;
8 w3 `5 ?2 Y: {4 {, k} </FONT></P>
% ^! e9 b6 e W% @' C5 L5 g<P><FONT color=#0000ff>void subroutine(void) $ x& |' P/ R2 ~4 K
{
) t1 p' v, n9 K% E/ L0 c$ hlongjmp(jumper,1);
) e& D5 e1 B1 W$ v. ] y}
9 x, C4 F M7 I( N W</FONT>9 ~# P+ [1 k7 h. A0 }3 O
</P>
7 {: ~+ F, L1 _ ?* ]7 }<P><FONT color=#ff0000>函数名: setlinestyle </FONT>
+ ?/ b6 y f8 H* h5 C功 能: 设置当前画线宽度和类型
! a( {& I, E8 [, @' v1 H用 法: void far setlinestyle(int linestype, unsigned upattern);
& G* \# Q9 Y% o程序例: </P>
- o& l: y' B: `4 w" |2 D; {<P><FONT color=#0000ff>#include <GRAPHICS.H>
3 A0 q. B% |1 Y5 B; Z: r- I#include <STDLIB.H>
k+ C. @5 W w# Z#include <STRING.H>
1 a+ D2 B7 m, U0 `" D$ Q. w#include <STDIO.H>" I$ l: g0 A& ?9 b% x
#include <CONIO.H></FONT></P>2 n# `, Q+ J& a6 F
<P><FONT color=#0000ff>/* the names of the line styles supported */ 1 a) f7 l$ r8 { d: X9 V
char *lname[] = { # O1 _5 g9 B( c8 o, D% E
"SOLID_LINE",
# \5 }( C- S" g5 e; S"DOTTED_LINE",
# F3 p1 c I; G"CENTER_LINE",
0 A1 q; ~5 ~2 O"DASHED_LINE", 3 E! T. S' d8 [( c: X8 V
"USERBIT_LINE" * Y3 h4 k9 c: z6 `
}; </FONT></P>
& y( f. i8 L0 ]3 P7 q<P><FONT color=#0000ff>int main(void) 9 B6 J4 ^* S! q5 b+ E
{
: }# G; _) v$ q, Q, ?3 P/* request auto detection */
" ^, x2 q+ i; iint gdriver = DETECT, gmode, errorcode; </FONT></P>9 r o' }. z5 b3 h
<P><FONT color=#0000ff>int style, midx, midy, userpat;
/ C0 P: B; G0 ?4 A- G: g5 V4 U Ichar stylestr[40]; </FONT></P>
, i( Z j Z: C& s<P><FONT color=#0000ff>/* initialize graphics and local variables */ 7 `4 x- W2 c1 X& \2 ~
initgraph(&gdriver, &gmode, ""); </FONT></P># L1 S( o1 a4 h+ W! s
<P><FONT color=#0000ff>/* read result of initialization */ ( _ [$ e4 `, t
errorcode = graphresult(); $ r: ~- p+ E2 r
if (errorcode != grOk) /* an error occurred */ 6 |. I0 Q0 }5 J3 K
{
5 |' Z* ^7 A1 m/ E; v" Iprintf("Graphics error: %s\n", grapherrormsg(errorcode));
' {3 O. T% C8 \printf("Press any key to halt:"); + S+ N4 p6 {: m* P3 E
getch();
8 t/ s Q2 ?, _7 m2 fexit(1); /* terminate with an error code */ ! k; v* I |9 R& A1 @1 P
} </FONT></P>" y$ P* [# X6 t$ j% a
<P><FONT color=#0000ff>midx = getmaxx() / 2;
* R% U0 X9 u7 L* U% {midy = getmaxy() / 2; </FONT></P>
+ f0 Z1 \5 _+ `<P><FONT color=#0000ff>/* a user defined line pattern */
?' w" V; W0 D' M" U2 C. _/* binary: "0000000000000001" */
. T8 L4 v, N/ luserpat = 1; </FONT></P>
7 x0 s% w5 m) C6 R: X" u<P><FONT color=#0000ff>for (style=SOLID_LINE; style<=USERBIT_LINE; style++)
+ u1 H8 Y: p1 {' v Q X4 T ^{ - q( r3 Z1 h1 N( k( U
/* select the line style */ ( m5 r4 C S+ ~# V/ E y
setlinestyle(style, userpat, 1); </FONT></P>
* O9 J4 Q: e( l6 b- j<P><FONT color=#0000ff>/* convert style into a string */ 0 W4 T0 x4 W9 R9 y" D
strcpy(stylestr, lname[style]); </FONT></P>( x* ]# V; [3 ^$ r% h0 d1 {) ~
<P><FONT color=#0000ff>/* draw a line */
3 b W* C% z2 t9 S4 L& g# Lline(0, 0, midx-10, midy); </FONT></P>4 L- U$ j, Y- L( w2 |
<P><FONT color=#0000ff>/* draw a rectangle */ , o5 H3 z2 b/ e9 d$ F1 k0 o
rectangle(0, 0, getmaxx(), getmaxy()); </FONT></P>
; n Q9 K8 X/ ?9 p6 K" F& x<P><FONT color=#0000ff>/* output a message */
" R0 ^0 q. b3 Q+ ?# S% Vouttextxy(midx, midy, stylestr); </FONT></P>& x1 k( Z u# K8 S, C0 w
<P><FONT color=#0000ff>/* wait for a key */ 7 o& M; C& E8 ?3 d9 g% N
getch();
# V/ C# Q) [* }: n7 m+ G# O Vcleardevice(); " I% c: K4 [) j( J1 _$ [
} </FONT></P>$ }1 a. o$ L/ L1 O; o: ]9 ~7 \
<P><FONT color=#0000ff>/* clean up */
2 g, K+ i) d; @: D5 E; @closegraph();
! a/ ?5 Z8 U- y" Sreturn 0;
: J K8 r; O9 ~ v! o% s* n} </FONT>
! y0 V- W$ a1 N
6 n5 L$ O( b: `0 }' `7 x" {1 ~4 R9 c0 y" P
</P>. ]# S* S! |; q* S* \6 j
<P><FONT color=#ff0000>函数名: setmem </FONT>* w% I- i0 e+ F# g$ u5 M
功 能: 存值到存储区 ! Z+ C0 ~( {. X- m; y! f0 a/ A9 f/ a% a
用 法: void setmem(void *addr, int len, char value); ) h! ~: [6 z& G$ H- z4 B% L* [
程序例: </P>
3 B8 L2 Q/ ^9 {6 Y1 A9 P<P><FONT color=#0000ff>#include <STDIO.H>
- _. V1 h4 H: u5 h#include <ALLOC.H>
9 ]3 Z0 J( P* S" j/ |#include <MEM.H></FONT></P>0 F4 y5 V; D8 m! Y" W% Z
<P><FONT color=#0000ff>int main(void) 2 e/ ~6 X7 v5 ]5 ^8 T6 \
{
$ u! C( ]3 F6 V! D5 j. l0 bchar *dest; </FONT></P>
/ W+ r' b: v+ }<P><FONT color=#0000ff>dest = calloc(21, sizeof(char)); 2 d& B3 Q/ g) Y5 s4 D$ l
setmem(dest, 20, 'c'); % l" ? z& v/ W) ^6 \' B
printf("%s\n", dest); </FONT></P> d( N; k4 G4 T2 [4 M8 S: A
<P><FONT color=#0000ff>return 0; & t: N" ~& W* v( {5 \
} </FONT>
$ L( J$ e, F* u8 z @; y( H( m9 b6 S( c7 g# I# ^
$ j5 V" L& V2 f/ _; L) A</P>
" H- g9 i: a4 ?4 _/ g0 _. W$ T3 G0 f<P><FONT color=#ff0000>函数名: setmode </FONT>
' C5 j T( x' f6 D2 \功 能: 设置打开文件方式
( u( W6 Q" o9 Y# r, O用 法: int setmode(int handle, unsigned mode); ; ^ q, c8 u* F2 g2 ~3 T
程序例: </P>
) u1 V4 L3 h% h3 v+ w0 Y* a4 \<P><FONT color=#0000ff>#include <STDIO.H>
* R& U& b% k; `: z#include <FCNTL.H>
A9 R2 S! I1 s& F2 W$ V#include <IO.H></FONT></P>
C5 \: X6 L; P" X/ N<P><FONT color=#0000ff>int main(void) ! u' K( ?+ E# A9 O( M
{
/ j8 @) b' x k) `int result; </FONT></P>
" P4 V, h S& B+ y: w3 z<P><FONT color=#0000ff>result = setmode(fileno(stdprn), O_TEXT);
# z2 ^3 E6 F3 e I P# W, P6 uif (result == -1) / `8 F. ?- C7 n# ~5 a5 F% U
perror("Mode not available\n");
/ ?# v8 a9 R* m {2 F0 m9 pelse
, ~) S' K) @! @$ m6 e/ `- {/ Rprintf("Mode successfully switched\n"); 5 |, y: g; F" a" L
return 0;
0 p1 X4 q. Z. u. `0 [+ {7 A' j5 _} ( x& q/ `' Y1 [2 B
8 i# G2 C# Q% x+ ]& ?& ^" p% w/ u; F</FONT>
7 g8 K- c+ ~, B) Y n! {</P>8 \, `) H1 `( n9 K
<P><FONT color=#ff0000>函数名: setpalette </FONT>0 F0 w* F3 x3 l2 X' p# R+ M
功 能: 改变调色板的颜色 U8 P( F* S9 }! O
用 法: void far setpalette(int index, int actural_color);
: W6 z+ ~: l8 v: c程序例: </P>
$ D+ l0 N' u/ `! ~<P><FONT color=#0000ff>#include <GRAPHICS.H>
) f# ?+ q8 H- n( D#include <STDLIB.H>
- a" G% D0 ~* C9 p#include <STDIO.H>. \. D" \/ b5 ?- l) C. k F
#include <CONIO.H></FONT></P>, d8 x9 }5 A; Z4 f3 {
<P><FONT color=#0000ff>int main(void)
; Q2 w8 k# }; k, w' W* \- Y{ 1 ^& N" o6 E; Z1 w0 `
/* request auto detection */ 1 h. r# I, h8 w- p, x& I: C
int gdriver = DETECT, gmode, errorcode; 6 G( K3 d) W7 F0 `- X3 o Z. u4 _
int color, maxcolor, ht;
* g9 Y4 t# h* jint y = 10;
: o1 P" m* B8 C: a; \2 echar msg[80]; </FONT></P>
4 e9 z" K; F/ D<P><FONT color=#0000ff>/* initialize graphics and local variables */
% N; [, J; g+ E5 J; C2 R/ _initgraph(&gdriver, &gmode, ""); </FONT></P>1 z @ q/ J) U7 d
<P><FONT color=#0000ff>/* read result of initialization */
; k& d7 h; z% v' O% I( }5 l& l, Ierrorcode = graphresult();
. r* Z! J; B/ D( ?! @8 Fif (errorcode != grOk) /* an error occurred */ 4 X2 m1 z+ m% h$ {7 d, f! ?
{
+ C8 j/ G: a9 J) w: Mprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ) \; x, ~3 u. |' [; A& W. A
printf("Press any key to halt:"); ( u% m7 m" e8 W; q) ~
getch();
5 E- r+ t! M: d" [3 ^+ j- i1 `8 N9 Sexit(1); /* terminate with an error code */
! {! j" z+ i# ~2 q. U6 k} </FONT></P>
# f7 u) t0 Z/ @% K3 s<P><FONT color=#0000ff>maxcolor = getmaxcolor(); / G/ T' ]5 d& p, Y
ht = 2 * textheight("W"); </FONT></P>0 C+ h3 Q. ]1 u4 A& |
<P><FONT color=#0000ff>/* display the default colors */ 8 s$ ?6 ~8 H! K- Q6 @# z
for (color=1; color<=maxcolor; color++) 0 `+ T& } c( C& Q
{
1 n( H% T2 ~! B# T* dsetcolor(color);
' L7 \5 c; j7 t5 p; Wsprintf(msg, "Color: %d", color); - ?1 |) i' L; W- |) I5 \ m, [& s
outtextxy(1, y, msg); 6 z# v; T7 l( A2 P) V; {9 B( K, [
y += ht; ! D) ~, P: r0 z8 M1 {) s
} </FONT></P>) D, q; K0 o( }) `8 A- D; l0 L0 C
<P><FONT color=#0000ff>/* wait for a key */
( o+ ]* ?6 y5 H! i$ d" |3 a3 Vgetch(); </FONT></P>- k/ e6 L; n5 C/ ?* g, J+ O7 `7 G
<P><FONT color=#0000ff>/* black out the colors one by one */ 9 |% e6 O8 T6 F5 B \3 n" R1 y
for (color=1; color<=maxcolor; color++) / \& x/ m, Y% o
{ 9 J9 n- R6 ?/ ?5 j5 f* y
setpalette(color, BLACK); . D: b6 N+ H X2 K1 u
getch();
* c+ {! o8 N$ ]4 T} </FONT></P>
) F. k+ y4 b6 c- t<P><FONT color=#0000ff>/* clean up */ . P9 R$ G7 R G3 `
closegraph(); 2 Z: K" q7 |. o
return 0; 3 y+ C7 q8 \: W5 ~
}
9 c) _0 P0 k' X2 g</FONT>
! V5 g3 E5 c' w: j</P>& I6 T6 X; D# N5 w# s( E
<P><FONT color=#ff0000>函数名: setrgbpalette </FONT>
: s R4 C1 u1 [3 A功 能: 定义IBM8514图形卡的颜色 ' B3 X m* J! g5 X
用 法: void far setrgbpalette(int colornum, int red, int green, int blue);
/ M5 g" `# L$ j ]5 x程序例: </P>2 H. k' w" a0 H( ^- W; W8 a
<P><FONT color=#0000ff>#include <GRAPHICS.H>$ x, Z" {4 P/ E: r" Z
#include <STDLIB.H>
5 u* p3 t1 s: F; P/ q' u5 u1 u& u#include <STDIO.H>, J' n( V3 C" w( {
#include <CONIO.H></FONT></P>) G; Y) X7 Z0 ^% U$ k h
<P><FONT color=#0000ff>int main(void)
3 H7 K( {% B( B% E9 W{ & B" f) [$ J0 ?( [8 {
/* select a driver and mode that supports the use */
, [3 n& D- z7 \& ]# d/* of the setrgbpalette function. */ ! Y0 e2 a+ B7 e0 [
int gdriver = VGA, gmode = VGAHI, errorcode;
2 k. |2 H1 C# x6 z9 a; Bstruct palettetype pal; & k L- e; u2 e+ ]' V( p
int i, ht, y, xmax; </FONT></P>
; n5 i, P3 Q$ K; e0 n<P><FONT color=#0000ff>/* initialize graphics and local variables */ 8 C: ~( w/ C4 D- Q+ g
initgraph(&gdriver, &gmode, ""); </FONT></P>1 i, ~2 i) l% W8 Y
<P><FONT color=#0000ff>/* read result of initialization */
# v5 h. w9 w `- i+ D Jerrorcode = graphresult(); 7 e+ D1 u7 A+ R! p s9 } T
if (errorcode != grOk) /* an error occurred */ " O0 X; u. z! `) g: G; g; T( _ e {
{ ; Z( |1 i% `4 E( _: d
printf("Graphics error: %s\n", grapherrormsg(errorcode));
4 @! G' q& l! |: Jprintf("Press any key to halt:"); 7 ~! u) x1 t" p9 }6 H$ s8 ?
getch();
! H% A* p2 S5 c7 N8 Qexit(1); /* terminate with an error code */
# |* d% c3 x( l: @+ d: s; \: u" Z} </FONT></P>
, O: o8 a3 c' K5 r5 [8 K: b1 \<P><FONT color=#0000ff>/* grab a copy of the palette */ ; d7 P: `+ e4 Q: M0 ]4 n
getpalette(&pal); </FONT></P>% l% ^3 R! b+ n. ?6 b& Q
<P><FONT color=#0000ff>/* create gray scale */ - Y" ?6 |+ J' O8 ?
for (i=0; i<PAL.SIZE; <br i++)> setrgbpalette(pal.colors, i*4, i*4, i*4); </FONT></P>6 A+ ^1 ?( U7 ^+ i& h
<P><FONT color=#0000ff>/* display the gray scale */
8 d+ g9 G4 v- b/ @* uht = getmaxy() / 16;
1 ^! J1 L; g0 G, I. k+ n$ Rxmax = getmaxx();
- N# ] E+ ~+ l s0 A: q fy = 0; . C: j2 e! F) O; q; ^ C0 p$ R
for (i=0; i<PAL.SIZE; <br i++)> { 7 d) W. M' s7 h4 f7 C
setfillstyle(SOLID_FILL, i);
# _" s1 Y. p8 K rbar(0, y, xmax, y+ht); + I4 v- @1 f, _5 y( y4 X( L+ H
y += ht;
/ X7 ^. G+ k, M. J5 }1 ^5 _} </FONT></P>
% @$ C0 A2 f+ H, D+ N<P><FONT color=#0000ff>/* clean up */
' N" y* A! ] Kgetch();
/ b& A4 u b- C9 b# vclosegraph(); 5 t1 g; _& T9 w# S* T9 ^
return 0;
& W P) l" s2 {! q} 6 A4 l- G0 z$ \$ h3 o! \
</FONT>
% s5 A* B/ C* C. Z. t+ k; {( W B1 ^) a8 T: ^+ [
</P> N- J5 s& v7 w8 c% b+ q" B
<P><FONT color=#ff0000>函数名: settextjustify </FONT>
( B) {. h! ~, J2 i% }" ` Q功 能: 为图形函数设置文本的对齐方式 3 Z- ~$ `1 i0 z3 s: g& F
用 法: void far settextjustify(int horiz, int vert);
( }& k! w2 [9 }程序例: </P> z8 }2 Y- U" f; X, N
<P><FONT color=#0000ff>#include <GRAPHICS.H>
: V: _+ i+ C3 \2 e: X4 q$ }#include <STDLIB.H>
5 k7 P5 M5 j; X4 Z1 f#include <STDIO.H>7 o' S- V9 B }' j
#include <CONIO.H></FONT></P>, t- S+ w8 [; j. j6 w
<P><FONT color=#0000ff>/* function prototype */ ! u: ]- J; \ a7 F- M# F5 R3 V
void xat(int x, int y); </FONT></P>
. {. A/ q* O+ \) P5 t; k<P><FONT color=#0000ff>/* horizontal text justification settings */ ) F# ^/ G+ Q, U
char *hjust[] = { "LEFT_TEXT",
' {3 V E# V1 Z8 P"CENTER_TEXT",
% u# ]/ u) `/ s4 g# i1 P"RIGHT_TEXT" 7 e3 F& U# H7 b
}; </FONT></P>" v4 K3 t: i5 }* x
<P><FONT color=#0000ff>/* vertical text justification settings */ 4 @# ]! Q9 x8 K, D
char *vjust[] = { "LEFT_TEXT", : Z" U W5 W7 p! L8 ?8 f
"CENTER_TEXT",
2 r! w3 M# p% y; e" u4 b"RIGHT_TEXT" & r7 R9 p, i+ E& @& k$ o
}; </FONT></P>
7 M7 [! n# q- B<P><FONT color=#0000ff>int main(void) ( z: x5 O4 h4 d! h0 r- c" L- |
{ 0 j; K2 I, E8 ]0 N1 R+ V
/* request auto detection */
" f, T8 J3 _* z- E- |int gdriver = DETECT, gmode, errorcode; . q" `+ O) q+ S' g
int midx, midy, hj, vj;
$ E2 @1 g0 C0 v! nchar msg[80]; </FONT></P>
/ _/ \ G) R# B& f5 x<P><FONT color=#0000ff>/* initialize graphics and local variables */
) | X! J5 o/ zinitgraph(&gdriver, &gmode, ""); </FONT></P>
- M1 g* I2 l3 t<P><FONT color=#0000ff>/* read result of initialization */
( s6 ^, H) I, T5 j d* ierrorcode = graphresult(); . v, W. X2 z! A
if (errorcode != grOk) /* an error occurred */ 9 _8 n( t9 [" J) X) G( j& H8 W
{ 5 U2 T/ N1 _* l- D- @( C
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ' c' W9 A5 B- g9 E
printf("Press any key to halt:"); ' [$ E4 y4 j7 e* e, ]$ C0 i( W
getch(); . v2 A! s# n5 ^8 ], T' N
exit(1); /* terminate with an error code */
* A5 T! A+ ]% {+ e4 Z' s" ]} </FONT></P>7 q6 E, a& O! e* u
<P><FONT color=#0000ff>midx = getmaxx() / 2; . m! g# c8 N, i" ?7 ^# z
midy = getmaxy() / 2; </FONT></P>
( L# ^& K! x. ^: g" U<P><FONT color=#0000ff>/* loop through text justifications */
' ]; ~! m) x/ K. x4 K% Ffor (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++)
( @2 P: M5 e& M' O8 Efor (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) ' i8 J* T c8 L
{
, }9 H) H) \( j% p; Z" K$ h H5 Ncleardevice();
' F$ ^! f3 v- F5 s! x8 f- O/* set the text justification */ 4 h' Y& e7 n4 k, I% [
settextjustify(hj, vj); </FONT></P> N/ r' w- m4 N& l2 x
<P><FONT color=#0000ff>/* create a message string */
6 Q4 c8 j4 q; y; F2 u5 Rsprintf(msg, "%s %s", hjust[hj], vjust[vj]); </FONT></P>9 T5 D- Q% x( x; B3 f" M
<P><FONT color=#0000ff>/* create cross hairs on the screen */ 9 y# F7 Y( c. h# ?" o- k- R
xat(midx, midy); </FONT></P>0 k) g# J2 n# C) A
<P><FONT color=#0000ff>/* output the message */ ! ~$ [6 Y$ z3 ]& q, u
outtextxy(midx, midy, msg); / G' x* y. d, W( r0 u6 I3 Z
getch(); " E1 D8 p* s. G! |
} </FONT></P>
2 A V6 _: h, S0 w# y2 z3 M; k0 d( R<P><FONT color=#0000ff>/* clean up */ % l+ P, F _# o& i
closegraph(); 2 r. q. `. W1 B0 ]: }
return 0;
% U; I5 q5 f' D2 j6 R0 |# a} </FONT></P>
" d$ `1 i! z+ q" {2 O+ k2 {- u<P><FONT color=#0000ff>/* draw an "x" at (x, y) */
4 d6 g+ `2 ?. `: ]void xat(int x, int y) 2 I1 y9 C0 ^# w( A
{ * N4 d6 O- G! j# D
line(x-4, y, x+4, y); / t, k z% E+ b* I1 X+ i
line(x, y-4, x, y+4);
3 j3 {2 S+ u2 m# i2 V} </FONT>
: m! _1 x3 j2 Z+ A* u) ~# q9 T y1 j' r5 y$ N$ d: u- a
</P>
, O0 M+ M$ @6 W2 V% E* f<P><FONT color=#ff0000>函数名: settextstyle </FONT> J+ u: p# K% Z0 [ a
功 能: 为图形输出设置当前的文本属性
% e; g+ s& N% C$ s/ N用 法: void far settextstyle (int font, int direction, char size); # q1 u2 o- ^1 I6 @$ P, b* t
程序例: </P>" A; z1 s5 t3 N
<P><FONT color=#0000ff>#include <GRAPHICS.H>
( F2 Z" V5 E- ^ l2 P u#include <STDLIB.H>
# Q' ^6 Z7 X6 `& l3 p#include <STDIO.H>% Q* N, u1 H( y# h) ^
#include <CONIO.H></FONT></P>
& ^1 q w8 w/ k1 r) ?5 q<P><FONT color=#0000ff>/* the names of the text styles supported */
; W. e/ f0 E2 O1 kchar *fname[] = { "DEFAULT font",
9 L, ]$ r% T3 r"TRIPLEX font", : k( n& S; \: t! i9 e+ v+ o
"SMALL font",
6 L' e0 d+ y- K) t" b"SANS SERIF font", 5 R# y: \/ |7 F
"GOTHIC font" # J y) f$ l( ]8 S
}; </FONT></P>
; |# I7 z4 T, ]" z<P><FONT color=#0000ff>int main(void)
( K% P+ u( b6 _) M, Q- G# Z{
' r0 o0 s7 Y* N# O. [- n' @/* request auto detection */
$ n9 i: K1 i, e* W- ?( [* U$ n/ w5 K; kint gdriver = DETECT, gmode, errorcode; ; A, ?$ x9 S, x( H; o, c$ W, ^
int style, midx, midy;
, F0 T% K: ~, L* x1 n6 T% cint size = 1; </FONT></P>
5 w. b4 s% F, I( f) X0 ?" B, w; X1 x<P><FONT color=#0000ff>/* initialize graphics and local variables */ 3 B" G+ B- W* b4 t! U3 m0 t9 J
initgraph(&gdriver, &gmode, ""); </FONT></P>2 P) I, _% y$ @2 i! a7 \' p( S
<P><FONT color=#0000ff>/* read result of initialization */
1 d4 c) h5 {6 l6 t, O) C: jerrorcode = graphresult();
/ C1 Y, R* | D) }# m: P: D: _if (errorcode != grOk) /* an error occurred */
& g! `& b, l1 ]6 P( b$ P{
- l* V0 y9 ?, N! B* Z! _printf("Graphics error: %s\n", grapherrormsg(errorcode));
. Q- B6 }3 c6 Z* x2 m, ?; vprintf("Press any key to halt:"); 9 l% k% b1 T) _" W' D1 @
getch();
* d7 Y b, V B/ |5 L+ q% v5 Vexit(1); /* terminate with an error code */ $ C1 ` [4 s* ]4 D
} </FONT></P>" r6 a. D2 G+ P/ x5 w+ H, g3 g; L
<P><FONT color=#0000ff>midx = getmaxx() / 2;
- a% B \8 H/ c, mmidy = getmaxy() / 2; </FONT></P>- J( P! E& B9 s8 F1 k) h
<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
, n" W, E. ?. X+ |$ I<P><FONT color=#0000ff>/* loop through the available text styles */ ; {% g3 _8 O6 K. q6 |5 K
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
0 K1 K' k" F5 ^3 \& b# D{
4 |% l+ ?) ~3 x* w3 p: i: @cleardevice();
9 x2 x4 }4 D4 `, f1 e% bif (style == TRIPLEX_FONT)
( x' z, I3 `' p9 o0 gsize = 4; </FONT></P>
5 h- |- [% f/ t8 p<P><FONT color=#0000ff>/* select the text style */ $ p; n% h p- o% j B
settextstyle(style, HORIZ_DIR, size); </FONT></P>
j/ c8 U, p% x' N) ]8 k; M( }<P><FONT color=#0000ff>/* output a message */ 0 B g7 G$ ]6 W# T4 M
outtextxy(midx, midy, fname[style]);
. [8 U; t; H6 N8 [, ogetch();
: e2 d/ [# m' R s5 Z. S5 j+ \} </FONT></P>
$ E$ }* |! E) L* d9 n" e3 H<P><FONT color=#0000ff>/* clean up */
! z/ c0 Z/ `4 |# c0 bclosegraph(); 2 b3 l k4 i* ^$ j9 U3 i: R
return 0;
$ T' C4 q4 C6 T' V} + Q7 o+ Z# b7 z. ~- x
</FONT>
+ H8 [( z8 b8 L</P>
9 f; R$ P$ C; v4 O. O<P><FONT color=#ff0000>函数名: settextstyle </FONT>
C# m( l% C$ {+ [* H# Z0 y* r9 `功 能: 为图形输出设置当前的文本属性
2 m* f7 r9 f/ z1 T( I! O* n用 法: void far settextstyle (int font, int direction, char size); f2 V& z1 P! p0 q' m- v- r
程序例: </P>
) |8 M4 [1 Y `7 Z0 @' Y<P><FONT color=#0000ff>#include <GRAPHICS.H>/ `7 D" C+ l6 N% ?$ z
#include <STDLIB.H>
/ r0 ?6 L0 y( c5 }/ u& T# O#include <STDIO.H>' ]2 E4 e8 ?6 |9 O( ^1 Q3 q1 M
#include <CONIO.H></FONT></P>
+ |' R# B3 Q" q% ]8 O<P><FONT color=#0000ff>/* the names of the text styles supported */
) d; q w9 P& H& E8 U- f4 ?6 Kchar *fname[] = { "DEFAULT font", 3 W2 Q+ s8 F& d9 I+ T
"TRIPLEX font",
9 A( z ?1 N/ x/ \. _"SMALL font", , Z8 g; |. h, M: S3 W
"SANS SERIF font",
- t; Y' w% a" g+ `9 r+ V, h"GOTHIC font"
* h8 C3 i! F9 x, k$ }}; </FONT></P>
' ~9 B" y" F' S1 f* z4 q<P><FONT color=#0000ff>int main(void) 5 k5 v; }) F9 t8 Q3 \# D$ D9 q
{ ! M: i; L( y+ B- E
/* request auto detection */ 2 w8 A8 C% u) J. K8 R
int gdriver = DETECT, gmode, errorcode; % k' {9 x' T5 | j8 B
int style, midx, midy;
0 P* [5 F1 a" L8 n' L. ]int size = 1; </FONT></P>6 | G" {& {6 t/ J
<P><FONT color=#0000ff>/* initialize graphics and local variables */ - b" Y7 T5 T5 T4 s
initgraph(&gdriver, &gmode, ""); </FONT></P>
* v7 `% d* Y6 U<P><FONT color=#0000ff>/* read result of initialization */ 1 e2 ? Q5 Z' `6 z4 Z& Y6 m, ^* \ Q
errorcode = graphresult(); - D) a' Q, c% J% k/ `7 Y
if (errorcode != grOk) /* an error occurred */
! g F2 Y/ V' h; ^/ z- L6 n{ - i. n" |8 g( j- B& T
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 4 \: ]4 K5 n( u( q+ R0 l( r7 n4 _
printf("Press any key to halt:"); ( E' M5 R6 O* A# _
getch(); ( s2 `# y2 n4 \& k' f
exit(1); /* terminate with an error code */
; S# w6 X1 g c! ~5 r} </FONT></P>6 L7 C1 M$ z4 i2 [, Y
<P><FONT color=#0000ff>midx = getmaxx() / 2;
9 v) t; ]; }1 v2 T$ x& _. U9 Umidy = getmaxy() / 2; </FONT></P>" K* o! Y* }& I$ l- Q, W, B
<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>) R9 k! P- s8 a& Z
<P><FONT color=#0000ff>/* loop through the available text styles */ & |# a6 p& F! D0 {9 C, h
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) ) d" w! K/ w- \2 \+ C
{
7 p' M( y2 u: b) ecleardevice();
& l/ g- H: e5 \. Gif (style == TRIPLEX_FONT)
0 v3 f3 I$ k8 [! ]" U- bsize = 4; </FONT></P>
2 Y/ p3 C( A+ r X<P><FONT color=#0000ff>/* select the text style */
5 o: G) s" G1 z$ |settextstyle(style, HORIZ_DIR, size); </FONT></P>
3 i1 t- L9 P$ ?. ^<P><FONT color=#0000ff>/* output a message */
# ?) m+ ?2 {9 \! L wouttextxy(midx, midy, fname[style]); 1 ]+ A! z' V3 k3 w* q H$ Z! q6 d( D
getch();
' h0 B$ y0 n, m5 R( }" D} </FONT></P>
' V. v! i& \) U4 X: }; M<P><FONT color=#0000ff>/* clean up */
$ L! u) O% X+ g# @( qclosegraph();
6 k+ W$ O, y0 d, V2 R, |+ ^( oreturn 0; ' @0 X6 }7 k& ?1 |
} </FONT>& g4 P$ S) d& e
& q( I! z/ {* g. N</P>0 S, \% u, J* k5 j# @
<P><FONT color=#ff0000>函数名: settime </FONT>
6 `" ?( u2 n+ }: y3 c& G0 V功 能: 设置系统时间
% S6 }" N2 D$ V# H: \用 法: void settime(struct time *timep); / h; u I# ^. R: a% h( r
程序例: </P>
; t% G9 L; Q( j9 |* q# `) U<P><FONT color=#0000ff>#include <STDIO.H>$ h1 d% x! K8 ?& _
#include <DOS.H></FONT></P>
7 d$ s2 i+ U+ h! Y0 P! y' F<P><FONT color=#0000ff>int main(void) 9 e. Z) z! Z4 L2 A% d2 }
{
! e- _7 `$ p/ ~! K- M' U7 Pstruct time t; </FONT></P>2 J! f" R1 ]3 B* f# Q( A
<P><FONT color=#0000ff>gettime(&t);
0 @& x2 ~7 o+ xprintf("The current minute is: %d\n", t.ti_min);
5 i+ E% l7 N, A) m$ H7 K8 `6 Hprintf("The current hour is: %d\n", t.ti_hour); - o" _1 }9 J0 F1 ?$ |* O( H% ^' S! V
printf("The current hundredth of a second is: %d\n", t.ti_hund);
f& s+ _8 X3 w4 S/ lprintf("The current second is: %d\n", t.ti_sec); </FONT></P>
T' r8 U; R+ V" G- P2 p& ]6 r<P><FONT color=#0000ff>/* Add one to the minutes struct element and then call settime */
( J! \5 B8 Q6 y5 l+ C0 }, g' ut.ti_min++;
1 T) J/ p, m% z; Z6 T- C L: H8 esettime(&t); </FONT></P>
% p8 v8 V% a$ V9 \$ J: y. x/ ?<P><FONT color=#0000ff>return 0; . ?# d* S' S$ ?6 @6 M( E
} </FONT>
7 W: ^4 o+ L: F/ l9 W5 K& p% r- t# Q" V. G
</P>
6 K( e" b4 ?* p/ h<P><FONT color=#ff0000>函数名: setusercharsize </FONT>
* U1 v" D. q: Y3 n0 o功 能: 为矢量字体改变字符宽度和高度
6 Q/ N, }5 t- j& ~( a8 U用 法: void far setusercharsize(int multx, int dirx, int multy, int diry);
' `7 l6 C1 V" [& h8 k, S4 X程序例: </P>
- J, ]: k8 M. n0 [( K<P><FONT color=#0000ff>#include <GRAPHICS.H>; {) l7 G) k( Q
#include <STDLIB.H>
0 y# _% P1 J+ @- ~) b#include <STDIO.H>6 I7 E0 `+ }( H/ F2 K4 |, m r
#include <CONIO.H></FONT></P>" ]6 J' ]" T$ u+ \. d& v" h1 U
<P><FONT color=#0000ff>int main(void)
; I9 v5 ~: t5 {( ? o: ~3 f{
! a' @* x; m$ ?/* request autodetection */ ; w# z" l, w v4 E
int gdriver = DETECT, gmode, errorcode; </FONT></P>
; _2 ?) U. J9 ^# f4 E5 O<P><FONT color=#0000ff>/* initialize graphics and local variables */
! k+ y8 q8 t7 |# f7 Y3 Y& }) B5 t0 iinitgraph(&gdriver, &gmode, ""); </FONT></P>5 Q2 W5 K1 f9 Z+ L4 S
<P><FONT color=#0000ff>/* read result of initialization */ 5 S& i, I5 R. R- D2 a# i
errorcode = graphresult();
# r+ `" x$ B/ Q5 ^if (errorcode != grOk) /* an error occurred */
- }8 m' A9 i7 O/ I. R{ 0 `4 R7 Y/ F) c( [9 e
printf("Graphics error: %s\n", grapherrormsg(errorcode));
$ X r; ]" M2 c% [# t4 q$ [printf("Press any key to halt:");
$ U# g% e2 w3 p1 K" T4 Lgetch(); 8 O/ J3 g- x- [. c1 b! B
exit(1); /* terminate with an error code */ / I8 h U4 }4 r6 x
} </FONT></P>
1 @2 e$ b! p5 r<P><FONT color=#0000ff>/* select a text style */ $ p9 q' |: a# ?+ c; q0 O& _ `
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); </FONT></P>
1 N. @" P% Y% i, _' M m6 L<P><FONT color=#0000ff>/* move to the text starting position */ % Z8 H9 G4 m7 P) i0 y
moveto(0, getmaxy() / 2); </FONT></P>2 }" J, h3 E) f& j+ C
<P><FONT color=#0000ff>/* output some normal text */
* v: D2 s* V) Y9 douttext("Norm "); </FONT></P>6 y3 {, H: F6 x/ |" `# O
<P><FONT color=#0000ff>/* make the text 1/3 the normal width */
3 o2 \# F4 R5 o! ^2 esetusercharsize(1, 3, 1, 1); / M* M. a/ B, l3 x$ ?; s2 ]& I
outtext("Short "); </FONT></P>- `$ W3 x; g1 s5 s4 w
<P><FONT color=#0000ff>/* make the text 3 times normal width */ , A% w" e: r4 x/ ^" q9 N# Z
setusercharsize(3, 1, 1, 1); / n" Z1 Z7 ?3 e, j3 ~& Q
outtext("Wide"); </FONT></P>
) J$ n. J! p# |; T2 ]- c4 e0 B! n<P><FONT color=#0000ff>/* clean up */ ' b: L6 E& U2 \6 x) W9 @. [- ~* t
getch(); + P5 f( E3 f1 I' G5 D
closegraph(); 5 D4 s" W' N) \8 _" z! d
return 0; 9 M; \ A$ x5 m& m7 q
} </FONT>
3 j7 i: J$ s' R' Y7 Y</P>" O4 X3 f* K2 g; T7 b, U) g& {
<P><FONT color=#ff0000>函数名: setvbuf </FONT>
C/ A+ u0 q4 v6 |0 Y& v ]功 能: 把缓冲区与流相关 5 r4 g1 h: ?. x! I$ D3 a
用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);
! P. W4 V# C* y$ P程序例: </P>
3 G3 I h0 A+ D2 T7 [$ O<P><FONT color=#0000ff>#include <STDIO.H></FONT></P># e8 h/ ]7 b$ A6 m7 B0 C! l
<P><FONT color=#0000ff>int main(void) 6 P) b/ P! v6 r! }: ?$ N
{
' A0 Z& ~4 L1 n2 uFILE *input, *output; * M- T9 v* C: D9 ]
char bufr[512]; </FONT></P>
2 v+ r4 \. s6 y* {4 s/ Q" a5 c1 X1 e' v<P><FONT color=#0000ff>input = fopen("file.in", "r+b"); & }1 V4 w ], E* G) R+ s0 K
output = fopen("file.out", "w"); </FONT></P>* \' V% f9 f1 o2 V* E6 T
<P><FONT color=#0000ff>/* set up input stream for minimal disk access,
' Q" S S& q1 qusing our own character buffer */
5 }5 G/ F) q3 _9 ?2 k6 v% Zif (setvbuf(input, bufr, _IOFBF, 512) != 0) * G3 ?/ q9 ~& \1 ~/ v$ F
printf("failed to set up buffer for input file\n");
; R! [# ^3 `+ i. Lelse }# G$ [6 J( r" n' p
printf("buffer set up for input file\n"); </FONT></P>
% v) I7 o% \) j, N<P><FONT color=#0000ff>/* set up output stream for line buffering using space that
/ h ^; X$ |9 P7 E/ q) dwill be obtained through an indirect call to malloc */
* _$ b% N& @+ y9 P( D! jif (setvbuf(output, NULL, _IOLBF, 132) != 0) 1 u& W/ |; ?( e- }
printf("failed to set up buffer for output file\n");
. ]2 s, W+ v0 v6 {: ^else + s* c+ F. o* O. [# K
printf("buffer set up for output file\n"); </FONT></P>
4 x5 L7 u. S: r. f<P><FONT color=#0000ff>/* perform file I/O here */ </FONT></P>
- q) i3 ^6 U! j, t6 Y3 B<P><FONT color=#0000ff>/* close files */
4 Z+ P4 \0 I9 `2 J$ yfclose(input);
f, b6 q4 b3 _% f0 D6 ?6 R" e% vfclose(output); , B8 M- H0 Z. E5 |
return 0;
6 U3 ^* s. }' t* i3 w9 M) S} 5 j# J' g8 C! }/ F5 R
</FONT>
9 V" Z, V6 x1 o2 X! t% t
9 Z+ z/ a# [2 s: j' }1 k& O</P>
3 O0 `& b! W" C# U2 w# V$ i. T<P><FONT color=#ff0000>函数名: setvect </FONT>% L& {( b/ X+ E" y- c. l D$ q4 S
功 能: 设置中断矢量入口 " L: C0 ^' {7 j" C# T
用 法: void setvect(int intr_num, void interrupt(*isr)());
4 `3 m2 {, ~6 j8 @+ E程序例: </P>9 t: i3 h9 E: A
<P><FONT color=#0000ff>/***NOTE:
% C2 x6 D j. n& {& f/ XThis is an interrupt service routine. You can NOT compile this Y4 }5 Z6 r5 K1 H& k! W
program with Test Stack Overflow turned on and get an executable
, u; Y- @' `7 e M7 s% ]* tfile which will operate correctly. */ </FONT></P>
! J( I) z7 r: s<P><FONT color=#0000ff>#include <STDIO.H>
) W# P, p- x, n7 q N# P. R#include <DOS.H>
1 C& ]1 R. |' e2 d+ i% y#include <CONIO.H></FONT></P>
; z/ V4 c2 J, o" G; j<P><FONT color=#0000ff>#define INTR 0X1C /* The clock tick interrupt */ </FONT></P>' }, l# m% o6 @ V# Z
<P><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>$ ^( A$ t6 g) ?2 N- q) n
<P><FONT color=#0000ff>int count=0; </FONT></P>/ O4 e9 z0 u, y3 F8 C$ i
<P><FONT color=#0000ff>void interrupt handler(void) 3 [5 j+ [ C: g4 H
{
4 l8 H; ?' ?* I5 E/* increase the global counter */ 0 A# [+ u- W. ~' ^% Z! M3 N
count++; </FONT></P>
$ ^# K+ ?- ^/ u% j: A& L<P><FONT color=#0000ff>/* call the old routine */
- X0 b( T1 o. ]7 v2 b7 Yoldhandler(); . I# L1 o( w. J7 q1 ^% x5 V* E
} </FONT></P>9 e8 u% W3 t. ]5 P8 G' L) v% F4 B
<P><FONT color=#0000ff>int main(void)
/ i: T8 B' f( P' ?; Q! E; p+ T" J8 r{ + E5 z( K( w6 @# {+ r- s: C2 ~
/* save the old interrupt vector */
- H9 k7 o1 l/ y- ~oldhandler = getvect(INTR); </FONT></P>/ W h$ T1 F0 W T& [( h* [
<P><FONT color=#0000ff>/* install the new interrupt handler */
8 O4 J4 T) z( \: s5 _' K9 J) Rsetvect(INTR, handler); </FONT></P>
: K# z# q) `) n! j<P><FONT color=#0000ff>/* loop until the counter exceeds 20 */
' F( D/ `5 b/ Q* Ewhile (count < 20) ( `9 E2 ~7 Y! o4 }4 w
printf("count is %d\n",count); </FONT></P>* g. n1 w1 `$ V% P9 ~! E, A
<P><FONT color=#0000ff>/* reset the old interrupt handler */ , P. Y# g: Q8 U& M+ ?
setvect(INTR, oldhandler); </FONT></P>
/ s) w! N" H, A; H<P><FONT color=#0000ff>return 0;
( i. V! _5 p+ X: b$ f- P# k}
% o9 C/ |' Y: i; l" n: z</FONT>
; o d) H1 b/ @, H</P>9 N2 ~4 {4 Q- a l" a, I( t
<P><FONT color=#ff0000>函数名: setverify </FONT>8 a' T' ] h5 w- [
功 能: 设置验证状态
0 V) V' k- X) J7 l3 t; s: L) Q用 法: void setverify(int value); ( r6 @1 y8 I: F5 [. O$ n
程序例: </P>& l( M; d$ Y% b
<P><FONT color=#0000ff>#include <STDIO.H>
. G* \$ V9 p" `#include <CONIO.H># H! A* B6 ^$ u# P
#include <DOS.H></FONT></P>) v* P& l3 a4 U* z
<P><FONT color=#0000ff>int main(void) $ d+ w; C T+ W o# H
{
; D1 W8 F! k* g" wint verify_flag; </FONT></P>
* Z" l" L0 R* I; p<P><FONT color=#0000ff>printf("Enter 0 to set verify flag off\n");
* z' o/ r( q) Z+ Sprintf("Enter 1 to set verify flag on\n"); </FONT></P>" p% ]5 }$ L0 A3 b, x# g
<P><FONT color=#0000ff>verify_flag = getch() - 0; </FONT></P>" Y9 R1 Z: f3 E1 N
<P><FONT color=#0000ff>setverify(verify_flag); </FONT></P>
: m& x3 n* D6 V6 R E3 [<P><FONT color=#0000ff>if (getverify()) 3 T* l3 e9 g0 B) z: T4 u
printf("DOS verify flag is on\n"); # R" x- v1 Z4 P* _: B1 j( T9 U
else
! B, P& f1 P/ B: X# y- Dprintf("DOS verify flag is off\n"); </FONT></P>
9 j( s8 B1 N3 r5 q: l$ Q<P><FONT color=#0000ff>return 0;
: M8 ^8 d8 ?6 d' C$ z0 Z} % G6 w2 w+ F) X' N4 F2 C8 Q0 I
<FONT color=#ff0000>
. O" j: }+ K* a0 q% z, h' \( T! O7 E</FONT></FONT></P>/ V$ H/ @( _1 |/ V4 v
<P><FONT color=#ff0000>函数名: setviewport </FONT>
) H4 Q" J& l- v功 能: 为图形输出设置当前视口
5 R1 e( D9 N& l. E& v8 C. G用 法: void far setviewport(int left, int top, int right, $ W, Q3 s7 ?8 h6 h; Q; I! n
int bottom, int clipflag); 4 N. r7 P+ u8 V( M2 M" H
程序例: </P>% n& j% O j2 [: T
<P><FONT color=#0000ff>#include <GRAPHICS.H>8 G. {! m! L- r7 d; S5 h
#include <STDLIB.H>
# t# d0 x: I. D& q5 ~6 z' Q% R0 V#include <STDIO.H>5 y9 j) }+ h& P
#include <CONIO.H></FONT></P>3 D& u) }8 T1 ?/ H! s
<P><FONT color=#0000ff>#define CLIP_ON 1 /* activates clipping in viewport */ </FONT></P>
" ~) r- l7 L. b<P><FONT color=#0000ff>int main(void)
1 E7 _ \5 ~! }{
7 g" s% b+ R$ D# Q& h2 w0 n/* request auto detection */
# W x9 ?( [6 B3 p7 i% b6 uint gdriver = DETECT, gmode, errorcode; </FONT></P>, l7 [4 {0 B8 D6 w! ~
<P><FONT color=#0000ff>/* initialize graphics and local variables */
) _1 ?7 k9 D! L" I+ T: I) j+ cinitgraph(&gdriver, &gmode, ""); </FONT></P>( P/ @+ Y. j: s1 k- ?! b
<P><FONT color=#0000ff>/* read result of initialization */ 2 A& O3 I1 ^* o J/ w
errorcode = graphresult();
; b1 _4 y5 k7 i& m y; y3 w* }; H9 qif (errorcode != grOk) /* an error occurred */
% l' i7 x N7 s% T! I{ 6 w. ~/ s, {& ~ s' `
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ) C5 Z% T% v( r
printf("Press any key to halt:"); ! f5 z$ {* p0 `4 `
getch();
" S( z8 w& w+ o# u; w# \- z+ aexit(1); /* terminate with an error code */ * K7 b# m$ f4 h" \$ U# W0 A
} </FONT></P>
( X5 e: w& o3 f' U<P><FONT color=#0000ff>setcolor(getmaxcolor()); </FONT></P>
6 `+ W9 M4 ^6 m<P><FONT color=#0000ff>/* message in default full-screen viewport */ 6 L; ^/ @% z& t$ k
outtextxy(0, 0, "* <-- (0, 0) in default viewport"); </FONT></P>/ T; [4 D6 I. C* O: _- m
<P><FONT color=#0000ff>/* create a smaller viewport */ / R- \( V, {* Z8 ?3 |
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); </FONT></P>
5 J) H0 v$ J$ n3 a1 J ^<P><FONT color=#0000ff>/* display some text */
% A, p- u, {5 d1 ~: J7 n3 L( r% Iouttextxy(0, 0, "* <-- (0, 0) in smaller viewport"); </FONT></P>% ?' P/ x% {) u- A! X$ J
<P><FONT color=#0000ff>/* clean up */ # i" I7 G* ~% u! h! @$ e! J! _9 U3 c
getch(); . f- ^' {4 x" ?; e/ @
closegraph();
3 y# r) q* x. a$ t4 Q+ i ereturn 0; 3 R. `) c8 m" i" Y, w
}
4 I. l% [% o0 y& Z8 y</FONT>
$ k' ]7 m& E }</P>
/ V4 z& ^6 x6 s$ [0 L6 h" [2 k9 }<P><FONT color=#ff0000>函数名: setvisualpage </FONT>! U5 z/ }8 ] Q/ w
功 能: 设置可见图形页号
4 d+ s! A. w( c; G. d Q% y用 法: void far setvisualpage(int pagenum);
3 X* X' o% e0 K0 }4 A程序例: </P>
n) g: x, O% K# U<P><FONT color=#0000ff>#include <GRAPHICS.H>* e9 @: t9 g4 z. O; y) c( e
#include <STDLIB.H>
2 y6 c$ Z/ P L+ ~#include <STDIO.H>
' k4 R: p- ^& Y% K+ m#include <CONIO.H></FONT></P>
8 P: x$ U. E! @# B7 @<P><FONT color=#0000ff>int main(void)
( i( A( Q: R0 _9 N) T- b3 ]{
' W5 ^- {) y' |/* select a driver and mode that supports */ 5 u' P8 R$ y. p, w9 ` z
/* multiple pages. */ - J! J/ Q9 @$ @- J0 ?3 n. R S
int gdriver = EGA, gmode = EGAHI, errorcode;
' t( p* { Y9 ~" ?int x, y, ht; </FONT></P>" j2 N) {: K% M+ l+ ?7 H
<P><FONT color=#0000ff>/* initialize graphics and local variables */
: p5 g- T0 m# R, \initgraph(&gdriver, &gmode, ""); </FONT></P>! d, }8 D- m; O; ^! [" i( I! O
<P><FONT color=#0000ff>/* read result of initialization */
& i0 m. y/ W% i0 }# [2 kerrorcode = graphresult(); 5 T' T, j9 C5 G3 ?
if (errorcode != grOk) /* an error occurred */ * o7 e& I& x4 R. I2 C! `1 V
{ & t4 K. y% M: T. P* Z$ A! l6 D8 s3 |: P
printf("Graphics error: %s\n", grapherrormsg(errorcode)); . S/ w5 \2 I7 p
printf("Press any key to halt:");
* V2 v! t- C' k) Fgetch();
: F, w6 ~4 {3 d/ h4 c9 \( R6 sexit(1); /* terminate with an error code */
3 x* x4 M3 i5 G% S0 G} </FONT></P>
; O' d" G8 R; R& c$ ^<P><FONT color=#0000ff>x = getmaxx() / 2; 4 k% z4 D$ L! q& j0 R+ y' |9 G$ R
y = getmaxy() / 2; 3 D" Z. U# M$ y9 Z4 i" `! Q, M$ a
ht = textheight("W"); </FONT></P>, H0 X; z3 T, c, M \' P9 G
<P><FONT color=#0000ff>/* select the off screen page for drawing */
) \* m) |7 X, D+ b( M7 Y" ysetactivepage(1); </FONT></P>
! i, k4 T. }8 s: D# e/ ^, W! J<P><FONT color=#0000ff>/* draw a line on page #1 */
/ t6 C7 W1 a$ yline(0, 0, getmaxx(), getmaxy()); </FONT></P>
- O0 M. \7 k ], K L) n1 G<P><FONT color=#0000ff>/* output a message on page #1 */ 0 y4 D) g. k6 P: M& L
settextjustify(CENTER_TEXT, CENTER_TEXT);
/ W: R+ M! C% @ a- E, }outtextxy(x, y, "This is page #1:");
K8 a8 O: z. X" p. iouttextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
' i0 K6 p( T# ]4 j7 R. o6 U<P><FONT color=#0000ff>/* select drawing to page #0 */ / L; `- U* }5 t5 ]6 C6 Y' O( C
setactivepage(0); </FONT></P>
, K. [; T6 T# u<P><FONT color=#0000ff>/* output a message on page #0 */
+ G9 r3 V- r! t! t0 A- v; oouttextxy(x, y, "This is page #0.");
4 R9 z4 u$ ^9 A2 R( }outtextxy(x, y+ht, "Press any key to view page #1:"); + z/ q7 }, u% n
getch(); </FONT></P>2 C( I4 U2 h% m) q" I- `' i" _
<P><FONT color=#0000ff>/* select page #1 as the visible page */
( a; \& O7 X! F; \setvisualpage(1); </FONT></P>* `/ v# b, q! S! U$ I6 j: J8 `
<P><FONT color=#0000ff>/* clean up */
0 h8 G. Q" _7 M1 v1 n6 n8 y* _7 ^ Ogetch(); / i2 C8 n, o* `( H
closegraph();
' g1 g8 ?; H" y* T- N' E9 p9 m- dreturn 0; . e. H+ R$ `7 `, I
} </FONT>- t4 {7 _/ d, i2 p: T8 \
. K4 m& Q/ q3 l; s</P>
- N# p0 t- a! a( j7 t7 Z<P><FONT color=#ff0000>函数名: setwritemode </FONT>
* F9 J4 w7 F+ p$ E. d功 能: 设置图形方式下画线的输出模式 9 I k2 C. e' E" [& i( A% f1 r+ X
用 法: void far setwritemode(int mode);
3 G0 D8 i2 h6 o. R- a程序例: </P>
( F h$ p0 S' q' l* W6 [<P><FONT color=#0000ff>#include <GRAPHICS.H>
. c" w" R) ^& g! c#include <STDLIB.H>7 A3 z6 t0 E8 H' \/ e8 W' y& O7 d
#include <STDIO.H>
" h% Z% n, d: z% O: z. F3 |% Z9 c#include <CONIO.H></FONT></P>
) w0 D$ C( F$ S+ n, q N<P><FONT color=#0000ff>int main() 6 U6 d$ v3 W( p: _$ d/ U6 X3 s
{ # v$ w" g. S/ [ Q$ }# Y& _
/* request auto detection */ : P. V5 N$ G) v' c' T# V! X
int gdriver = DETECT, gmode, errorcode; ; ]7 ^0 C2 e6 W. u* N6 _" G
int xmax, ymax; </FONT></P>1 C8 I0 ?7 w) R, E7 [" V
<P><FONT color=#0000ff>/* initialize graphics and local variables */ ( ~0 C( O' d- H% Y
initgraph(&gdriver, &gmode, ""); </FONT></P>
2 C3 `( f8 Y6 K<P><FONT color=#0000ff>/* read result of initialization */ & c. Q/ z7 w" p) ^9 J
errorcode = graphresult(); " F1 }" `% k# T% J) N2 I
if (errorcode != grOk) /* an error occurred */
+ X" ?8 n5 ~' E' j! y{
3 _, w- a/ w+ g- b' \& t; Z0 \printf("Graphics error: %s\n", grapherrormsg(errorcode));
8 D7 J3 m$ l) q+ E0 F$ Fprintf("Press any key to halt:"); 4 R1 x7 i1 v$ `$ k+ K7 N
getch();
- s6 A+ A7 m1 Y7 L) Q' U- m- X2 Q, ?exit(1); /* terminate with an error code */ ) W, H! n. W& l# m. j
} </FONT></P>: V9 _. L7 Y7 p: |, S
<P><FONT color=#0000ff>xmax = getmaxx();
2 t! V: g! Y: ~1 t+ n$ B: Wymax = getmaxy(); </FONT></P>, T! o+ o5 Q0 S
<P><FONT color=#0000ff>/* select XOR drawing mode */ " Y; T( Z: D/ Z9 d
setwritemode(XOR_PUT); </FONT></P>
9 r9 C, a/ s8 L$ K<P><FONT color=#0000ff>/* draw a line */
`4 F0 U- m2 h# lline(0, 0, xmax, ymax);
: H, O' Y0 a1 ~% } |; B7 _* i$ J$ Wgetch(); </FONT></P>
& l3 D: @$ B" ]% t2 z<P><FONT color=#0000ff>/* erase the line by drawing over it */
; _2 {, ?% o8 ]& ?+ M$ q9 n- Gline(0, 0, xmax, ymax);
; M+ y( i0 @( H! t6 @; d. H8 I9 dgetch(); </FONT></P>
2 B! O6 E: f: g+ R% a8 C( a, H<P><FONT color=#0000ff>/* select overwrite drawing mode */
o' Z5 z2 Q! E: E& I+ fsetwritemode(COPY_PUT); </FONT></P>
8 \* K. T. i$ Y7 N i<P><FONT color=#0000ff>/* draw a line */
+ P' D6 b: [9 w6 u7 J" U, Q9 h; R9 jline(0, 0, xmax, ymax); </FONT></P>
8 h, M1 N8 V( W: F/ e, [4 ^. Q& o<P><FONT color=#0000ff>/* clean up */
( f1 \5 _ y I1 i" A8 Dgetch(); 6 y+ F- g% O8 b$ n+ r
closegraph();
% y& z4 s, ]* ^0 Areturn 0; . S. u* ^# b$ n' u
} 5 ^6 y2 w, d) X
</FONT>" \; | w) l+ Y) j( G
</P>
! X* c: e7 I4 T4 H% N<P><FONT color=#ff0000>函数名: signal</FONT>
; U" e9 g* e9 s, H功 能: 设置某一信号的对应动作 z/ H: {1 R# @
用 法: int signal(int sig, sigfun fname);
: k- |" I9 r$ E) A2 @3 h, ?程序例: </P>; R0 X- e9 T& G: h9 S4 [* e
<P><FONT color=#0000ff>/* This example installs a signal handler routine for SIGFPE, : @, ~6 e( Y/ |9 g/ m
catches an integer overflow condition, makes an adjustment
+ a, s' m& [, g2 r3 B" \to AX register, and returns. This example program MAY cause ) |0 E' T) c8 V$ q# R
your computer to crash, and will produce runtime errors
" j+ D" P/ J2 Sdepending on which memory model is used. ( X5 _ f [' t
*/ </FONT></P>
6 H7 p3 a; U& S5 B<P><FONT color=#0000ff>#pragma inline
Y% N' D& N* Q+ ]#include <STDIO.H>3 v7 g: L I* a8 A& ]8 l- L
#include <SIGNAL.H></FONT></P>, a2 [+ J' u% T/ }
<P><FONT color=#0000ff>void Catcher(int sig, int type, int *reglist)
/ y m- s( G3 A: ]{
) Q1 [" g( B. x5 pprintf("Caught it!\n");
) `3 R7 e* h' s6 @0 v% m*(reglist + 8) = 3; /* make return AX = 3 */
1 E0 ?2 I! {* P( ]/ ~0 ~/ ^} </FONT></P>& t q v# O8 F5 V9 v
<P><FONT color=#0000ff>int main(void)
' c, `1 Q+ p4 Q& b6 n( k: ~7 w{
' h$ g B/ v; o: @6 X( u) Jsignal(SIGFPE, Catcher);
3 {" \% W3 W; l8 u) Hasm mov ax,07FFFH /* AX = 32767 */
! m9 j- F" W# lasm inc ax /* cause overflow */
% e% r F8 I" V( A- x O3 D' p- M; Rasm into /* activate handler */ </FONT></P>% T- Q5 a' p; v, t* K$ w
<P><FONT color=#0000ff>/* The handler set AX to 3 on return. If that hadn't happened,
6 g& x% E: i/ Q0 Q! N othere would have been another exception when the next 'into' " V& @* k; p. B; Y0 q, _
was executed after the 'dec' instruction. */ + k; Q/ M \! w: w5 _3 I5 |
asm dec ax /* no overflow now */
) f& D8 w( A3 ]! M9 R" Z# Fasm into /* doesn't activate */ ' d7 _* F: N4 T+ c+ K
return 0; 6 ~/ M& m, T1 C
}
: [0 ~0 ~. \5 E7 v
( O" f* {& d) A6 `, N; y t% H& \5 X
</FONT></P>2 i% c8 \- M7 ?/ r& P. t+ ^
<P><FONT color=#ff0000>函数名: sin </FONT>
5 e8 o2 i! n! k$ I4 n! b; {功 能: 正弦函数 . d; A/ Q9 @, U2 q" _; O6 b
用 法: double sin(double x);
1 E0 @, r) R0 P8 E2 n程序例: </P>
" u4 R) G1 u4 Y% [<P><FONT color=#0000ff>#include <STDIO.H>* x* O& Z# `; l+ r' N
#include <MATH.H></FONT></P>
: e+ L7 c: u6 X/ {8 }<P><FONT color=#0000ff>int main(void)
, z! U3 G4 v1 i" y2 y- w. T{
4 a4 w' V- |" _# E' w4 H( _double result, x = 0.5; </FONT></P>1 C; S/ O8 s+ l4 ~1 R
<P><FONT color=#0000ff>result = sin(x);
3 H: v0 x+ S( ~% |/ [+ r6 K2 @printf("The sin() of %lf is %lf\n", x, result);
4 q( @, ]: x% B0 [2 A* k- hreturn 0; ' l1 g2 |! e9 y' ?3 `
} + l7 M: u3 L% k h9 X9 k
7 G" N U- d; C</FONT></P>: F# x0 G) s2 J% y0 y' X4 [
<P><FONT color=#ff0000>函数名: sinh </FONT>6 Q5 Z9 |8 q. k# r& M
功 能: 双曲正弦函数 , U) u: r1 ^$ d9 {# ]
用 法: double sinh(double x);
! N. N4 t7 p: x4 X4 x程序例: </P>
; }0 x9 j" ~ k. `! J<P><FONT color=#0000ff>#include <STDIO.H>6 H0 {6 t& o2 l) `' y; `/ O
#include <MATH.H></FONT></P>
% z! X8 F) x5 |<P><FONT color=#0000ff>int main(void)
& L' A5 F. E* h& M0 D; N{
+ [ h: v; a$ @( d& Sdouble result, x = 0.5; </FONT></P>6 u9 z# n8 K! _3 d' m, \; S& f
<P><FONT color=#0000ff>result = sinh(x);
" U- n4 z: d$ N. G! _printf("The hyperbolic sin() of %lf is %lf\n", x, result); 0 ?) O/ @# i& p" J. w# @( S
return 0; T; q' @- G: t- G7 j
}
. p" p' z6 c# D2 a- ~4 p+ f0 L7 k: t# S8 [: `( J/ ^2 k
# R; d' b' Q) k8 Y/ K) k7 ~; C</FONT></P># Z3 s5 P P1 i9 l+ _8 k
<P><FONT color=#ff0000>函数名: sleep </FONT>
" Q# ~8 g6 M5 Y; p [, C* a; y功 能: 执行挂起一段时间 " v: K: R. `* g; f
用 法: unsigned sleep(unsigned seconds);
; w* p2 T4 c0 G. J1 N9 M( z程序例: </P>
1 y( ~+ C7 j }<P><FONT color=#0000ff>#include <DOS.H>
7 y' n, j# `' c) S#include <STDIO.H></FONT></P>
9 b7 F" d3 }3 l0 T" M% S* _: Y& |<P><FONT color=#0000ff>int main(void)
' F r& c" W! O! a9 N! k{ * e! r% ~* L }: t' u
int i; </FONT></P>
: V# D% j8 B [) U<P><FONT color=#0000ff>for (i=1; i<5; i++) $ e/ z3 Y1 f6 q! k* n4 H
{ & c% _7 G3 z4 n8 [! C
printf("Sleeping for %d seconds\n", i); ! E1 Y6 y$ h8 x, k" |9 y2 A0 Q" _' v2 |
sleep(i);
8 p! s7 B: j) `- j7 Q, S} 5 n. n: U2 Z. Y N3 E
return 0; + t+ R2 i% k) @8 r3 i4 T
} 2 c0 t1 p2 q8 m& a1 M5 l8 u
- {) b5 `8 m( R</FONT>* ~# A8 R6 h6 Y6 ^ n0 `0 p E3 d
</P>
$ U" }0 H; R/ e4 B<P><FONT color=#ff0000>函数名: sopen </FONT>
8 ~2 M- j3 X! B5 ]. a% A功 能: 打开一共享文件
% P" q& k5 K! R+ Q& l用 法: int sopen(char *pathname, int access, int shflag, int permiss);
. I- J! N1 A: w' [7 |0 B) }0 w程序例: </P>
) f- m. X3 r: ]2 d: n. x: R<P><FONT color=#0000ff>#include <IO.H>- o2 b( n6 [7 s0 ?2 Z; p; z
#include <FCNTL.H> M) G" K# f; J* ?; k( V! C
#include <SYS\STAT.H>9 Q1 V7 i* d9 Y; G& C
#include <PROCESS.H>
1 M2 F* s$ j$ |' e#include <SHARE.H>7 O$ K; U G% C$ \2 S5 z
#include <STDIO.H></FONT></P>
0 T- a6 }6 a8 n3 Q<P><FONT color=#0000ff>int main(void)
! ~6 J4 T0 ?" Y7 h8 s* w{
/ [8 L& c n- X% [8 E2 U# ]int handle;
& O" [ o1 I9 s; R/ Iint status; </FONT></P># [* C- b8 Z) c* H. W
<P><FONT color=#0000ff>handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD); </FONT></P>) @+ E) Y9 q, u" X H8 V
<P><FONT color=#0000ff>if (!handle)
, M* s- h4 c: u{
7 c% L) y! \8 }) Z8 k5 I, _7 A; T9 cprintf("sopen failed\n");
1 N2 N9 J7 [, g2 p: dexit(1);
$ e- v+ Q/ J2 p% E+ \ e) n} </FONT></P>. g8 E r. L- Y) G
<P><FONT color=#0000ff>status = access("c:\\autoexec.bat", 6);
' @9 y. L6 M* M; \if (status == 0)
, b7 W2 Q, a- z) Lprintf("read/write access allowed\n");
" |2 t1 ~( j# |6 f9 yelse
, k9 ^. Y( h8 p8 `printf("read/write access not allowed\n"); </FONT></P>8 i1 ?+ B1 U, m: F
<P><FONT color=#0000ff>close(handle);
: n0 k7 C$ E* [, _4 _return 0; ( n/ F7 Z3 Y( i& _7 o- l7 C+ e% ^
} / v9 N5 a( h1 [$ m7 A Y
# p5 S4 ?: O, A" G9 f0 }</FONT>
2 ]2 G/ z3 G- Z8 U8 C% ~, F/ T</P>- [" w% K5 Z, A. L& d, x0 T
<P><FONT color=#ff0000>函数名: sound </FONT>% X* f* v; d( `" K
功 能: 以指定频率打开PC扬声器 % _5 F E& i$ s! ]+ m W# I, o
用 法: void sound(unsigned frequency);
* Y, O2 K7 @( S4 j$ d程序例: </P>$ [4 j) v% E* ~4 |# U
<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds. $ ~& \/ O- N: C' c& D6 i1 ^
Your PC may not be able to emit a 7-Hz tone. */ + t H' B9 F# N4 |& z
#include <DOS.H></FONT></P>0 @: I, c4 V2 l6 B# f1 f
<P><FONT color=#0000ff>int main(void)
# w6 A( E, g& X1 Q! B1 G. Z{ , ?$ m8 N6 B9 u% Z0 A$ G3 p5 d
sound(7); & v. w8 D; j7 g, a$ K j
delay(10000); 9 f8 b& H' x4 G) E9 l7 e
nosound(); $ b3 L7 n2 p* e) m8 x& r
return 0; . U* N5 o; L( J* z4 H/ w9 I
} </FONT>
$ {% [8 Z, j% a" j( H& |
$ d7 E E: Q" c, z1 ?4 \- r3 d# j4 m9 Q; n8 V% ^
</P>
3 i2 J) q- w$ {% }) U<P><FONT color=#ff0000>函数名: spawnl </FONT>- R* s6 u6 g* e) g3 Y) q
功 能: 创建并运行子程序
* O; e4 r9 ~/ J: N- M. L& V, z用 法: int spawnl(int mode, char *pathname, char *arg0, ?" m, ]" K$ M( Y+ q3 K) M* l7 R2 l T
arg1, ... argn, NULL);
p$ m* @: C" p6 Q6 y8 I程序例: </P>6 Q8 r. b6 P" [" m
<P><FONT color=#0000ff>#include <PROCESS.H>0 v; S* U u5 i7 @
#include <STDIO.H>
" e" [, ]) Q, g j#include <CONIO.H></FONT></P>
* o# h3 V+ U1 E- |; i! a6 G# j( S<P><FONT color=#0000ff>int main(void)
. i B: G$ }8 F2 F: T& B# c( g{
2 F, D9 B5 g4 [7 l+ n- z/ bint result; </FONT></P>/ N5 p ~ v7 F/ [0 c0 c
<P><FONT color=#0000ff>clrscr(); 6 Y" {2 T/ F6 p4 u4 \' B* j
result = spawnl(P_WAIT, "tcc.exe", NULL); , G# m# e0 m% [/ I. Q, e2 X
if (result == -1) * H( q, ?1 s! g0 w. z% Z1 Q
{
* Y$ T5 s& Y+ d% g& Q$ a* }perror("Error from spawnl");
9 t0 N. }3 A- _ |6 `% Texit(1); + S# g6 }% a# _( J( H8 Q
} 0 M. [& d1 F* X/ F9 w. U
return 0; 3 h# u( J( u0 Q A
}
0 n: o2 V( F" Q$ q" B</FONT>2 J' i \4 ^% D2 d& f
</P>
4 {( v0 J y$ B& _6 T6 h$ S }<P><FONT color=#ff0000>函数名: spawnle </FONT>
/ D* [" D7 n. i3 E! w# o功 能: 创建并运行子程序
/ P% N3 x g; @4 Q* |用 法: int spawnle(int mode, char *pathname, char *arg0,
/ c. _: t J- O* p$ Larg1,..., argn, NULL);
2 o$ ?3 E3 f7 b0 L( h! A程序例: </P>5 `4 R7 h$ G" `" o
<P><FONT color=#0000ff>/* spawnle() example */ </FONT></P>
9 V- t( h7 b5 t. S<P><FONT color=#0000ff>#include <PROCESS.H>
1 }1 p2 @: M3 Y% {! W) ` K#include <STDIO.H>8 R5 v" ~4 p7 f' i7 n- h
#include <CONIO.H></FONT></P>$ j; Y- Z7 L, h" l
<P><FONT color=#0000ff>int main(void) 3 e5 {! T& H1 ]! \. L, z7 }
{
' }; ^% E/ h4 l+ \int result; </FONT></P>- h/ D; p% X7 K
<P><FONT color=#0000ff>clrscr(); $ N- J& T9 L! _' G$ O
result = spawnle(P_WAIT, "tcc.exe", NULL, NULL); 6 G( X9 O& e, h' b
if (result == -1)
" `/ H, J4 C+ v( M+ O7 f8 u{
; K# N/ h0 X _4 Z0 j6 m0 Cperror("Error from spawnle");
# w! v# v2 n6 Z' t9 V! O& hexit(1); ' j3 K5 ^: E; O( J" w, x
} ) P3 N! q T2 l
return 0; " P- }5 o8 ~, t
} 8 k9 p: P/ G- Z! A$ P
- P# h. t" t$ M: [# c' s
+ D/ K/ ^8 W0 c0 E; h- W+ Z</FONT></P>
5 R8 ?) {; I; j" |<P><FONT color=#ff0000>函数名: sprintf </FONT>
, ]7 E" B" b+ K; Z; {4 s+ r功 能: 送格式化输出到字符串中 ' |) g' { ]& l( B: o0 L3 U, \
用 法: int sprintf(char *string, char *farmat [,argument,...]); " n+ h" s# \/ m" d( P! ?$ `
程序例: </P>7 b1 E0 n8 N# Q0 E3 w: ^0 V+ U2 j
<P><FONT color=#0000ff>#include <STDIO.H>. j6 x/ F, @8 V0 Z5 v
#include <MATH.H></FONT></P>$ q% V. P6 k! S, A% |
<P><FONT color=#0000ff>int main(void) 8 w5 e) r7 F3 w# N* \9 h
{ - J: d- J/ i5 O. G
char buffer[80]; </FONT></P>. |. |; u# b# u8 |+ B& W
<P><FONT color=#0000ff>sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
; G! N5 R3 L P. w, \0 F6 uputs(buffer);
. V: \2 C0 I. v: X+ N0 N, u- s5 _return 0; ! J* E+ }& y" _1 K: L( A& I+ @! o
}
7 F+ _# o$ Y8 H) `7 b( t# h</FONT>
; G3 e' u q; ^</P>5 ~/ T: _; J4 p/ i) V/ o
<P><FONT color=#ff0000>函数名: sqrt </FONT>
9 \. r7 a2 U2 u# M功 能: 计算平方根
: x0 u1 I5 n5 T( p用 法: double sqrt(double x); 3 S% {$ r: U1 E! v5 u4 t3 i. p% W
程序例: </P>
; ]7 N/ Q r! e- k# X# J/ ?<P><FONT color=#0000ff>#include <MATH.H>* U- J5 u+ L9 z! h/ u5 k
#include <STDIO.H></FONT></P>
5 j8 r$ Y. z/ I1 X ^<P><FONT color=#0000ff>int main(void)
' Z# k3 m& S! S Q$ ~3 @{
3 T# V, \" o0 _1 j) ?3 M% Zdouble x = 4.0, result; </FONT></P># V/ P1 j/ A9 t; K" r: n: ~
<P><FONT color=#0000ff>result = sqrt(x);
* L9 r; i) e6 e# T/ M6 Yprintf("The square root of %lf is %lf\n", x, result); $ `: W' k& {4 w, d
return 0;
4 Z: n3 H" Q [# J, j}
/ d: _0 H, m; }. a7 Y- B1 S</FONT></P>
3 _6 k) ~; @0 q. i" j% D<P><FONT color=#ff0000>函数名: srand </FONT>: P1 C% [' w9 x" J6 }* y
功 能: 初始化随机数发生器 6 ?3 J' C3 Q, H0 R6 h. g/ e2 `
用 法: void srand(unsigned seed); ' f# }( t/ s/ F9 ]6 G
程序例: </P> {" f) A' T0 K( j/ ?" L
<P><FONT color=#0000ff>#include <STDLIB.H>' G- C3 C: `# F
#include <STDIO.H>6 g7 o9 M9 h+ B; |" ?
#include <TIME.H></FONT></P>( Z4 C: l& u( P
<P><FONT color=#0000ff>int main(void)
: X! }- b( R' B4 k{
8 k3 C) q* W" Oint i; ' M6 N' N# ~9 I9 r
time_t t; </FONT></P>' A' l! ]1 w8 x% k# B$ q
<P><FONT color=#0000ff>srand((unsigned) time(&t)); + U4 G% N( q' Q6 E1 c& B
printf("Ten random numbers from 0 to 99\n\n");
; V+ I- ]+ E' pfor(i=0; i<10; i++)
. n0 W! ?: B2 P& n4 \: `0 k% ~' Vprintf("%d\n", rand() % 100);
& G. P. [. e- V8 }# Xreturn 0;
3 d% t! A- x8 O+ B7 z}
- |( v4 [9 f$ V3 j</FONT>
' k: Z; y @$ |) t7 @! s' R</P>
+ J( p8 R A# D8 Q6 U0 b' o<P><FONT color=#ff0000>函数名: sscanf </FONT>
0 M: }' B: o& ]) Y( q' a; `: y7 s2 j功 能: 执行从字符串中的格式化输入
. \# y7 d% {( [6 c用 法: int sscanf(char *string, char *format[,argument,...]);
1 ~& Q3 S5 D, g, f程序例: </P>, O7 `" H- H- |( h% E
<P><FONT color=#0000ff>#include <STDIO.H>
1 c. o' G3 K2 j, C% W#include <CONIO.H></FONT></P>4 y6 R, S6 X1 e0 r1 F
<P><FONT color=#0000ff>int main(void)
5 a- \3 T0 x2 ^2 S{ + y* J7 v) }# K8 A' M
char label[20]; . f+ Q: g! _% M0 e& G. X9 I
char name[20]; ' X+ _) H. n: c1 Y4 E
int entries = 0; / n% r; Q) V9 W5 Y8 C0 x2 D E
int loop, age; / Q) s% Z( p9 W
double salary; </FONT></P>
7 U! ~9 {2 J( b6 q$ c% \! x<P><FONT color=#0000ff>struct Entry_struct 3 r5 C$ T) x7 b2 [+ e; t" o
{
/ @2 m# ?) V" T% echar name[20]; ! b2 L; y- m q; w1 Y9 V; C
int age; ; R1 N. c1 N8 E. y: |( R
float salary;
: y" t# k: J" b+ n} entry[20]; </FONT></P>5 f8 n0 {" R. U5 A
<P><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */ % Y6 m4 D+ F" j/ q- y
printf("\n\nPlease enter a label for the chart: ");
0 Q+ \: Z' f9 L2 B% V- Jscanf("%20s", label);
( @8 @9 X6 `2 e qfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
. ^& |, s2 R# m1 u% d4 A<P><FONT color=#0000ff>/* Input number of entries as an integer */ ; Z" O- U* |: L
printf("How many entries will there be? (less than 20) ");
) d ?" g* g$ O2 G( Tscanf("%d", &entries); * O& a+ N+ u) }7 [- K
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>. k- y4 r, z8 l/ E/ B
<P><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ p8 k2 `6 v* b) s: c6 `: j+ l
for (loop=0;loop<ENTRIES;++LOOP)
! u. ^% R9 X$ u5 [/ R+ o- r { - v% U5 u* ^0 f X" `+ z H
printf("Entry %d\n", loop); , B$ O( m v! F3 O, J3 p
printf(" Name : "); . _ |+ [7 x+ a7 [8 f& N( }
scanf("%[A-Za-z]", entry[loop].name);
9 A- ], G, I: I, I0 Wfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
; n, h2 ?% d9 S3 Q( B" s2 w; K0 ]<P><FONT color=#0000ff>/* input an age as an integer */ & t, g+ O' C/ N+ w; {. d0 b( F0 _
printf(" Age : "); ; d. X7 ^7 H8 c9 S0 d8 v& j
scanf("%d", &entry[loop].age);
' T: u2 W: s" Xfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>8 F" g% }4 }, @8 C& h! x) Q; x- F
<P><FONT color=#0000ff>/* input a salary as a float */ . }1 o# W% L8 H$ @
printf(" Salary : "); ) S4 R8 ?# H9 K1 B# ^ q
scanf("%f", &entry[loop].salary);
4 c+ C% u9 @& ~5 C" M/ ~fflush(stdin); /* flush the input stream in case of bad input */ ^5 ]+ ]! [) b: }( u2 o) f; V
} </FONT></P>/ E& s! _- S! A. Y3 l1 ]8 b
<P><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */ 2 P1 g* J+ q8 H4 e% q
printf("\nPlease enter your name, age and salary\n");
; S5 W3 Y6 _3 r' p. W4 `! ]scanf("%20s %d %lf", name, &age, &salary); & U0 n3 I# _+ |2 D& Y0 f) R3 o
</FONT></P>
6 o( I- y5 w& _/ Q9 A4 b* a# n<P><FONT color=#0000ff>/* Print out the data that was input */ 8 }. J* y9 b k8 B
printf("\n\nTable %s\n",label);
$ @* A" m) i2 o2 e# j: \! {printf("Compiled by %s age %d $%15.2lf\n", name, age, salary); o* m% I4 v7 H' O/ H
printf("-----------------------------------------------------\n"); $ ]. ~+ {1 w" L8 ` h
for (loop=0;loop<ENTRIES;++LOOP)
; L1 B# i7 _1 v6 g* } printf("%4d | %-20s | %5d | %15.2lf\n", 9 z* X1 q- a1 u6 M: m: u
loop + 1,
* ?1 Z8 Y9 K$ N! F( u% ^entry[loop].name, ; `/ |9 e C3 B
entry[loop].age, $ l V* G' @( t3 n
entry[loop].salary); . U3 w0 q, p" y6 ^9 c! i& R" ~ t
printf("-----------------------------------------------------\n"); 3 V9 T$ d) I( h, ]$ |% X
return 0; 9 M- e( k! Z8 @3 k" P
}
i% ]3 e. R1 c1 p
+ x& j0 F" n8 [3 Z! j</FONT></P>8 E9 \1 G1 E: O" d: L! g+ Y- v4 ]
<P><FONT color=#ff0000>函数名: stat </FONT>
0 V- i8 O; J0 d8 i1 [& b1 d功 能: 读取打开文件信息
# P+ m3 k% C' ^! \) W" l5 K- {; S' C4 `用 法: int stat(char *pathname, struct stat *buff);
) @. e4 A) M- @: }程序例: </P>1 n& A5 a8 C2 z) Q5 S2 Z
<P><FONT color=#0000ff>#include <SYS\STAT.H>/ c3 y! w# j. S$ p3 H0 t: E7 }" u
#include <STDIO.H>0 n0 R( b- }" P
#include <TIME.H></FONT></P>
( N9 p( [6 c' D( f<P><FONT color=#0000ff>#define FILENAME "TEST.$$$" </FONT></P>3 T! K0 C a' k
<P><FONT color=#0000ff>int main(void)
* g$ m% v6 S( \5 {" Z{ V1 B/ E7 R7 F! b; o9 Y
struct stat statbuf; . B3 U" N) Q! @0 Z5 Z
FILE *stream; </FONT></P>
6 T' ~3 d" I' ]1 [' ]<P><FONT color=#0000ff>/* open a file for update */
, N8 \. O- g% @& U% k% Pif ((stream = fopen(FILENAME, "w+")) == NULL)
' Y" B. U/ S, c{
$ B& Q( M, Q3 f* n* Tfprintf(stderr, "Cannot open output file.\n"); 7 \3 [* `$ Y6 H2 \" @1 o
return(1); ! }' l s, l& k
} </FONT></P>
7 G- T! H8 z/ P, D1 z o" I2 i<P><FONT color=#0000ff>/* get information about the file */
0 G. j4 ]0 P% z9 H7 Bstat(FILENAME, &statbuf); </FONT></P>3 T8 l( s! p; }# Q
<P><FONT color=#0000ff>fclose(stream); </FONT></P>
+ E$ a r) s# E<P><FONT color=#0000ff>/* display the information returned */ 5 d5 Z/ |5 v' v3 s* F- N) I2 {" L
if (statbuf.st_mode & S_IFCHR)
* ]" ~2 \: D5 U7 d! e: r" |printf("Handle refers to a device.\n");
) \8 J F5 j4 d5 X" C* ?# V4 w- Nif (statbuf.st_mode & S_IFREG)
6 n3 \1 `; ?, J( r5 Bprintf("Handle refers to an ordinary file.\n"); + N- C* i# w: \, h& E" l' f5 N k
if (statbuf.st_mode & S_IREAD) 2 K) n& I1 d+ h; h
printf("User has read permission on file.\n"); 5 g5 L) f* W4 [: [
if (statbuf.st_mode & S_IWRITE) * m$ q. @/ X6 T* b) p2 r; F/ f
printf("User has write permission on file.\n"); </FONT></P>
2 M. e6 Q! T. J5 T" d: P! H H<P><FONT color=#0000ff>printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
9 x$ z9 a9 y' x( Mprintf("Size of file in bytes: %ld\n", statbuf.st_size);
4 t) o( b2 s X9 K8 a( Vprintf("Time file last opened: %s\n", ctime(&statbuf.st_ctime)); 3 d" ], d2 {2 I# t* f
return 0; 9 b0 m2 Z( U. S7 ]
}
3 ~9 S( j# |6 H1 f; I/ `8 ~; b
</FONT>& ?3 V# K7 y4 v/ c$ N& R
</P>
* [7 u; T' Z3 V+ u: {) H<P><FONT color=#ff0000>函数名: _status87 </FONT>
0 h7 w/ Y" q C3 Z! r功 能: 取浮点状态
& W7 x0 H/ S" K+ P$ l t/ ~' u1 A4 L用 法: unsigned int _status87(void); $ w- |4 m8 K4 n' {7 I! B) g, [
程序例: </P>1 B- X4 x- b$ _; X% Y& n
<P><FONT color=#0000ff>#include <STDIO.H>
( L4 ^" | g6 ?8 ^7 t: ~#include <FLOAT.H></FONT></P>$ H! ~0 S3 [% Y0 m( ?, \7 ?6 j
<P><FONT color=#0000ff>int main(void)
( i8 @5 ^. v( l& A2 Y6 j" `6 [/ Q{ $ o* [2 ]$ j( e, d
float x; 3 r5 T: F/ ~; X. M1 w" x
double y = 1.5e-100; </FONT></P>
- ^+ M! {+ S2 q6 V9 z<P><FONT color=#0000ff>printf("Status 87 before error: %x\n", _status87()); </FONT></P>
z8 b3 L( A6 t" F<P><FONT color=#0000ff>x = y; /* <-- force an error to occur */ ! a. `5 g, X% n1 h7 Z0 _5 E* T4 O! F; c
y = x; </FONT></P>$ v2 d: \7 t) X; p/ ~
<P><FONT color=#0000ff>printf("Status 87 after error : %x\n", _status87());
: g9 {* v) b6 o+ Mreturn 0; : t5 S' s* q- ?
}
, t" @& H5 `6 Q# l4 Z, C8 m</FONT>
! \. B- E) D# ^5 F- _</P>8 a! `8 K4 }# g4 U" }3 P
<P><FONT color=#ff0000>函数名: stime </FONT>: o6 t, a$ r0 Z$ ?
功 能: 设置时间
0 X. Y) n; C. X# K用 法: int stime(long *tp);
7 W8 Y, u. P2 `, h+ i. W程序例: </P>* m! k1 ]2 o' H
<P><FONT color=#0000ff>#include <STDIO.H>
& m( R/ {( J4 L+ C0 [#include <TIME.H>
1 A% y. `1 T8 _# J# {#include <DOS.H></FONT></P>
" i1 b; e( U. H% E- H<P><FONT color=#0000ff>int main(void)
" g- v: `2 T8 h8 P{
N! P: V: {2 t+ Jtime_t t; $ {( e6 a2 d3 O7 I9 ]
struct tm *area; </FONT></P>" Z" L& c& C8 q6 E C
<P><FONT color=#0000ff>t = time(NULL);
6 y2 I/ m! ^) M* C% j6 O% Y5 harea = localtime(&t); ! s4 E5 z, k1 E ^) k
printf("Number of seconds since 1/1/1970 is: %ld\n", t);
0 K5 ~ K! M0 Bprintf("Local time is: %s", asctime(area)); </FONT></P>9 l) ]. j# ~2 J# X5 J' E
<P><FONT color=#0000ff>t++;
( T" @" ^! E& ]$ z: W1 j, O3 tarea = localtime(&t); 8 o" M" s! \% D/ B" M$ |" _
printf("Add a second: %s", asctime(area)); </FONT></P>
. ?; B" H9 K2 T2 |# Q<P><FONT color=#0000ff>t += 60;
3 U; D" D7 {1 X) S* ]area = localtime(&t); . I% S% @; k* l7 ^
printf("Add a minute: %s", asctime(area)); </FONT></P>" U: Y' x$ ?: Q3 d
<P><FONT color=#0000ff>t += 3600; * Q0 k# w# h6 [# E9 U: L
area = localtime(&t); 1 B' m' H: N: Y" y. q. \
printf("Add an hour: %s", asctime(area)); </FONT></P>- Z' L* T+ j. P
<P><FONT color=#0000ff>t += 86400L; ' H" C: j7 O' y; H, y. U
area = localtime(&t);
0 i, X, ?: O6 ]& i, X+ Zprintf("Add a day: %s", asctime(area)); </FONT></P>6 `$ f% Q/ w: J# n: X) r$ o
<P><FONT color=#0000ff>t += 2592000L;
& b5 o. U" D7 P. m$ I% tarea = localtime(&t);
# e* d4 I: V7 K; y. Y7 vprintf("Add a month: %s", asctime(area)); </FONT></P>; t. Y9 y8 } @; Y- Q
<P><FONT color=#0000ff>t += 31536000L; % ]8 A3 n! o3 `% t3 m# z" i1 a
area = localtime(&t);
- f! L3 e. Q; w. @0 pprintf("Add a year: %s", asctime(area));
" M- N6 B" k( p* vreturn 0; ) h! x6 w7 U: _& {' @+ v
} </FONT>
* s' q% `! q- g# w
" |, [4 s. d7 V- V6 o. d2 e9 T# }5 V4 P# ]+ c8 {
</P>
4 [9 |" r! c, T* r5 b9 }( e<P><FONT color=#ff0000>函数名: stpcpy </FONT>- m& a% G O8 ?4 ~
功 能: 拷贝一个字符串到另一个 ; ]: g) E0 p' ?7 A
用 法: char *stpcpy(char *destin, char *source); $ I ^) h% T# W- V( R3 i
程序例: </P>
0 M s! D5 c( W8 Y/ Q" ^! F; O<P><FONT color=#0000ff>#include <STDIO.H>
3 M; o: U/ e: e1 g' D+ @#include <STRING.H></FONT></P>
9 B, g) {7 F* g9 T% D<P><FONT color=#0000ff>int main(void) ' j1 d# ~4 ]7 ^5 d+ A
{ 3 v9 F$ f/ Q/ R m% l
char string[10]; ( R+ f$ Y" f2 }0 @( `/ a
char *str1 = "abcdefghi"; </FONT></P>4 s$ o& U8 X' K8 l( M
<P><FONT color=#0000ff>stpcpy(string, str1);
2 _! c+ a# x! x* J, }printf("%s\n", string); K$ \' |( u. Q3 c7 d
return 0;
7 O0 G4 J z4 C1 S' A: z}
& e7 c4 p0 ~0 {) ?- V9 g
* k& J- e6 P% I+ L2 r+ f</FONT>1 X4 _, v P6 @" I! |2 y9 c* P
</P>
+ S0 p! t, n$ |& B' X/ T<P><FONT color=#ff0000>函数名: strcat </FONT>
& H6 y& D- _- ^8 z7 \# L2 G& F: N功 能: 字符串拼接函数
( k k3 ~* X; u0 R& \' y用 法: char *strcat(char *destin, char *source);
/ o% S" l/ o$ j! O5 z程序例: </P>
" _+ @" `5 P- ?<P><FONT color=#0000ff>#include <STRING.H>
- c: f9 Q4 u: ^& J: E4 O$ M#include <STDIO.H></FONT></P>
3 |( s( i7 v8 j' e, \ Y) X<P><FONT color=#0000ff>int main(void)
' f! } H {9 x H{ 1 u3 p$ r/ u- B7 T0 Q: j1 h- i
char destination[25]; % y. j& [ r2 _% a
char *blank = " ", *c = "C++", *Borland = "Borland"; </FONT></P>
+ V2 Q- x9 r; P: u<P><FONT color=#0000ff>strcpy(destination, Borland); # G) K5 F6 j. ~4 s7 k: B) G
strcat(destination, blank);
- r& \- H/ z7 t1 t" y5 Z; pstrcat(destination, c); </FONT></P>
( x( ]6 M6 }% j4 v2 O<P><FONT color=#0000ff>printf("%s\n", destination);
$ [: F4 d0 P$ l5 Mreturn 0;
7 `6 c# N1 d$ u, i9 u7 R% |4 D3 c} . v }! h2 R, N5 M. O' z
2 b- [/ U5 y+ @</FONT><FONT color=#ff0000>
w5 ]' J; c: O+ H9 S" ]+ M& c0 b</FONT></P>% y# P7 d# H; R, @9 C4 f" @
<P><FONT color=#ff0000>函数名: strchr </FONT>* O2 h/ A$ \. P' E4 y9 g
功 能: 在一个串中查找给定字符的第一个匹配之处\
, G; ?# U, T; ~: H用 法: char *strchr(char *str, char c);
- t. {" ~# ?2 k5 g/ X( o程序例: </P>
- N2 M4 z/ f' o2 t' |<P><FONT color=#0000ff>#include <STRING.H>
" N/ s5 W4 j# w/ D2 `* h% D#include <STDIO.H></FONT></P>9 y, R: M/ J' n! w
<P><FONT color=#0000ff>int main(void) ! \7 a* Q0 I* x$ O% t8 ^
{ " C& u5 ]$ c2 Q9 ^
char string[15]; ; R3 J# W+ H. V# t0 e5 b+ |" C
char *ptr, c = 'r'; </FONT></P>2 K, Y! i+ W8 b6 s" G3 Y
<P><FONT color=#0000ff>strcpy(string, "This is a string");
# i1 R! J# a* \3 w+ d- y M9 [ptr = strchr(string, c); ) O( s- z- P. Y: A$ s
if (ptr)
" w7 v% u* N& t' O& L* Aprintf("The character %c is at position: %d\n", c, ptr-string); ; Q6 |( }/ {* k d) y9 ]
else z: R4 L4 ]/ u: \/ k
printf("The character was not found\n");
/ c. ]) P* d" H' Ereturn 0;
- O$ c( N& b! k. a} 7 N5 l/ c8 w7 W* Q
</FONT>; z! O: E3 x! V8 t8 D8 R
( Z* T8 ^+ `, s
</P>
3 G% ~6 p0 Z* z<P><FONT color=#ff0000>函数名: strcmp</FONT> 0 f/ X) w* Y) [# W
功 能: 串比较 1 N8 S6 r; q$ J4 w! ]+ n9 E
用 法: int strcmp(char *str1, char *str2);
4 L9 K; v5 k% o) d% j程序例: </P>: w3 g6 ^+ {8 C$ n" d
<P><FONT color=#0000ff>#include <STRING.H>3 J2 ~0 P2 g1 d
#include <STDIO.H></FONT></P> f3 \" A: l5 y" d1 y
<P><FONT color=#0000ff>int main(void)
) `1 Q4 g6 j1 ?; X1 k9 N9 B0 D& j7 w2 t{
6 ]: T* G$ [7 {5 }( ]4 |6 i( Echar *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
8 V( G) l9 c8 \ d6 [% Rint ptr; </FONT></P>( h! x1 R; ~' ~% Z
<P><FONT color=#0000ff>ptr = strcmp(buf2, buf1);
9 g$ }! G+ u: Kif (ptr > 0)
# |+ E. H2 N* \2 F7 aprintf("buffer 2 is greater than buffer 1\n");
4 }! r; v9 k4 G. J9 M U# r$ uelse / Y* [# h( M, l- u' `1 c& v
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
( T# j; p7 T1 ?7 K* z: u$ d<P><FONT color=#0000ff>ptr = strcmp(buf2, buf3);
0 X1 P+ d; @3 _ a% k2 i7 oif (ptr > 0)
7 H* l# _2 P/ I; f4 i8 A4 r7 Hprintf("buffer 2 is greater than buffer 3\n");
* x, \; [% `) D8 Gelse ; P% k1 b4 s4 P
printf("buffer 2 is less than buffer 3\n"); </FONT></P>7 p4 f( b8 j; W/ }0 u8 [
<P><FONT color=#0000ff>return 0;
% c" V. Y# M7 t, i- U2 n8 n} " k- _" A4 G# O$ e
& X4 \2 ?5 A: P2 ?( W6 P6 D5 P( q4 M f1 J, b- o3 E) q
</FONT></P>
4 ^; ?9 x) ~. P" a<P><FONT color=#ff0000>函数名: strncmpi </FONT>, M; Q9 D7 C* q% S, M6 N
功 能: 将一个串中的一部分与另一个串比较, 不管大小写 # @3 w7 k5 v o) N' H8 M3 A
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen); . Z6 l0 Q: ~8 {, |4 r9 O9 x, ?
程序例: </P>
' a/ C, N/ C" u; r) h' n& g<P><FONT color=#0000ff>#include <STRING.H>$ z" @5 ^% ]+ K; l4 k& {! E; a1 V
#include <STDIO.H></FONT></P>; S% c2 ]3 I7 O. T
<P><FONT color=#0000ff>int main(void)
. L- G+ Q1 H( u% n6 t{
$ t0 T4 j# k) z* X% P8 A; e7 Ochar *buf1 = "BBB", *buf2 = "bbb"; ( t9 a p1 u8 M2 S) P; L3 V) f
int ptr; </FONT></P>0 P& X5 u g) S+ B. ^ O
<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>. ]8 a5 K: _. l$ s8 m" d, j
<P><FONT color=#0000ff>if (ptr > 0)
7 Z! M3 n9 h& g( O7 Wprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>! x, ~$ D v$ E# j8 X
<P><FONT color=#0000ff>if (ptr < 0)
; t' V3 ~3 Q: Y/ Z0 P3 d- t5 fprintf("buffer 2 is less than buffer 1\n"); </FONT></P>4 T! Q1 ^' L, B ?- x" e6 U+ {
<P><FONT color=#0000ff>if (ptr == 0) % F7 _3 H3 S. O" K8 V
printf("buffer 2 equals buffer 1\n"); </FONT></P># z" D9 o0 y' ]" m0 i
<P><FONT color=#0000ff>return 0;
1 n( B: w' h8 f% @$ h}
O" c0 q9 h) s3 |6 N
) Z0 H4 _9 G, P* O1 U0 V B</FONT>8 P: p; c( F6 s$ R. g8 G8 u$ |
</P>
. N9 K% {, O, a8 N0 i) l4 E<P><FONT color=#ff0000>函数名: strcpy </FONT>) a6 F2 q3 O/ q2 p
功 能: 串拷贝 . P( ^- r4 [3 J+ X7 _6 a4 }
用 法: char *strcpy(char *str1, char *str2); ; h) a. u9 K# A' S3 n2 M
程序例: </P>' n( ` \6 Y1 \& S) j" P
<P><FONT color=#0000ff>#include <STDIO.H>
2 x- @. ^/ k+ E/ c6 r& y#include <STRING.H></FONT></P>
3 p" {" E5 C6 p+ P3 i S<P><FONT color=#0000ff>int main(void) 0 R- P0 ^% t! f0 X$ i
{
2 ]) `; x2 X" f+ _6 _& ?char string[10];
5 ?0 m+ P4 g0 lchar *str1 = "abcdefghi"; </FONT></P>
4 v' {2 `- q& n' N+ ]9 E5 U<P><FONT color=#0000ff>strcpy(string, str1); ( C" L$ d& g* E2 k; Z7 s' J
printf("%s\n", string);
+ l( E$ f, ]" [1 ?6 B( v" @return 0; , |! m# u( T1 T, o5 Y
} 9 I& K( W! {+ O
</FONT>+ g( v- M. }6 w
/ V& b) C8 V& @) L</P>3 @: p& g* H' P8 Z
<P><FONT color=#ff0000>函数名: strcspn </FONT>1 F2 w% V; p7 E6 {2 {: G( A
功 能: 在串中查找第一个给定字符集内容的段 : t8 w! q" N. C0 }, X5 h; I
用 法: int strcspn(char *str1, char *str2); 8 ^- i# [* I3 Q$ u) t
程序例: </P>
0 a: i8 }/ v# V3 y1 N8 c$ c<P><FONT color=#0000ff>#include <STDIO.H>9 b: \5 Y0 x+ X& O! J
#include <STRING.H># E! L! V+ z) M( l7 g+ E$ Q
#include <ALLOC.H></FONT></P>! z8 O% X. h. s/ i" d7 b c5 Z
<P><FONT color=#0000ff>int main(void)
7 j' A) N: D# ~; _) J) Y{ 9 k+ J) C+ `/ P' R& @
char *string1 = "1234567890";
, p `8 `& N2 |- _char *string2 = "747DC8"; * |6 f4 U) ?, ^8 J w1 Y
int length; </FONT></P>
5 Q9 ^/ {7 e) n) S<P><FONT color=#0000ff>length = strcspn(string1, string2); % a; `: j S2 P7 w7 J
printf("Character where strings intersect is at position %d\n", length); </FONT></P>3 O+ j! b* E6 T( X; T; d; D
<P><FONT color=#0000ff>return 0;
3 l9 G+ {6 i0 Z& F6 \* U4 M} ' E4 i! F6 c3 d3 L' [$ h
</FONT>) W: ^3 s' h# B% D. J1 c; @; \
8 E2 c/ Z; ]$ d4 z3 k4 R</P>! V6 _7 ~1 y: k& n& E
<P><FONT color=#ff0000>函数名: strdup </FONT>
* W# \* y( R0 }# J" L8 p: e* M功 能: 将串拷贝到新建的位置处 % r- z; V# |- l
用 法: char *strdup(char *str);
! R! h# J, L/ S# j* U程序例: </P>2 Q$ n# J* k" M6 D, v6 J! s
<P><FONT color=#0000ff>#include <STDIO.H>
m! e# ?4 R' W3 l+ s0 C |$ _#include <STRING.H>
( r+ L7 G7 r" o: c. m; H#include <ALLOC.H></FONT></P>* Y. [8 Y& h0 R9 |- c/ H, R9 K+ F1 a
<P><FONT color=#0000ff>int main(void) % L2 H4 t4 j) c9 D
{ & ^7 U# y8 f# ~( _" O, {: R8 d
char *dup_str, *string = "abcde"; </FONT></P>
4 p, D) W* `# B1 T2 y" y1 E" j<P><FONT color=#0000ff>dup_str = strdup(string);
' B3 M) c7 L/ hprintf("%s\n", dup_str);
# L% B" X: e8 |+ }- gfree(dup_str); </FONT></P>
( ~0 o" \: B- ]& d4 Q1 b; k<P><FONT color=#0000ff>return 0; 9 G, W$ O8 ]% v* v/ \# |6 C+ t# d
} ; I/ {% K; f+ s
8 }! n* S' a, {5 V. [' ?( s
</FONT>
K* u$ b" l ^</P>) F8 M6 Y) `# k
<P><FONT color=#ff0000>函数名: stricmp </FONT>
! b7 A* k1 r1 Q1 G' j u {7 V功 能: 以大小写不敏感方式比较两个串
+ z0 U3 P/ _" t6 |4 n/ `/ o用 法: int stricmp(char *str1, char *str2); % w: [6 @2 k- t: m
程序例: </P> O, k9 s! |1 L0 O- O; z
<P><FONT color=#0000ff>#include <STRING.H>9 o6 ^9 W K+ v. Y2 L9 w
#include <STDIO.H></FONT></P>3 j/ |3 F; {2 u, h Z
<P><FONT color=#0000ff>int main(void) 0 y. u, [. k- D! I# Q! m- ^ w' k: o
{ : z j9 {& ^8 I G: L
char *buf1 = "BBB", *buf2 = "bbb"; : E z' j) W, d% P% S
int ptr; </FONT></P>
8 u) n* k& b5 [5 S* U<P><FONT color=#0000ff>ptr = stricmp(buf2, buf1); </FONT></P>
" h$ y+ k% v4 c# S3 F1 K8 l1 m<P><FONT color=#0000ff>if (ptr > 0)
7 J( M3 A. O3 _printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
: t0 `" J$ W: ?" P<P><FONT color=#0000ff>if (ptr < 0)
7 r6 c) D" q, b; r$ B2 M# b. _printf("buffer 2 is less than buffer 1\n"); </FONT></P>
* `5 o& r' W4 O" n. ^<P><FONT color=#0000ff>if (ptr == 0)
6 F n7 N( I" F" d( D7 Tprintf("buffer 2 equals buffer 1\n"); </FONT></P>
- x( c% c; b7 K<P><FONT color=#0000ff>return 0; ; U) A) h% b( y) j* F
} . Z. M+ q4 |* i
</FONT>
( e) |: N( q5 j# G z; b7 r</P>
5 T! w' S0 \; V( l% f. U<P><FONT color=#ff0000>函数名: strerror </FONT>* e) f; d4 u! C/ ?5 S* [% w3 G3 @" v
功 能: 返回指向错误信息字符串的指针
5 b7 A! W( A% T* a f用 法: char *strerror(int errnum); 4 D3 b2 x; z, Q c
程序例: </P>
# ` \+ ?6 {! U( B$ _2 Q) w<P><FONT color=#0000ff>#include <STDIO.H>7 z1 H' }4 x3 s6 g
#include <ERRNO.H></FONT></P>2 H7 f4 A, A! G- J0 C! r
<P><FONT color=#0000ff>int main(void)
! z8 f" W0 J" ? j. A{
& m" m0 I" J) _! ~char *buffer;
5 n! W' r7 r1 S8 hbuffer = strerror(errno); 5 f: P9 l, {6 ?5 J. m( A3 F9 [" O
printf("Error: %s\n", buffer); / x7 X2 b8 I, r! Y: t$ F
return 0;
! y& _2 l C) b: P1 Y}
% @4 L- U! y4 t
# e3 ~: W2 M( Z+ O# t9 j) B& g</FONT>
* I) ? Q5 B0 X) K- ~* T</P>
* Q A% z D$ r& _8 |: p2 }0 B<P><FONT color=#ff0000>函数名: strcmpi </FONT>' q4 `( _; F- t/ a; O4 z, r, ?
功 能: 将一个串与另一个比较, 不管大小写 ) E+ c0 E' e8 M
用 法: int strcmpi(char *str1, char *str2); 4 q4 o/ y; R7 d6 Y- M' \1 T
程序例: </P>
, Z; W2 p: f1 c" P. e( K<P><FONT color=#0000ff>#include <STRING.H>
, |" R6 K2 }5 O* i) ^#include <STDIO.H></FONT></P>( h# I0 U6 f/ z3 w$ R- B
<P><FONT color=#0000ff>int main(void) 1 {. F2 \) j) @' B
{ 0 J. l2 t! M% @6 p+ @
char *buf1 = "BBB", *buf2 = "bbb"; + L& F* ~1 o. y
int ptr; </FONT></P>9 L4 [. p4 ]7 x; G# K
<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>
8 T/ K8 j0 U7 h. m* ^2 o$ }<P><FONT color=#0000ff>if (ptr > 0)
: K' X) }' O) a6 Dprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>
" }8 ^) M2 H! ^" J1 d<P><FONT color=#0000ff>if (ptr < 0)
* b4 |# g4 v1 L% [0 Nprintf("buffer 2 is less than buffer 1\n"); </FONT></P>- l" I( p6 a! Z. Y, m! M
<P><FONT color=#0000ff>if (ptr == 0) b8 J. }2 k# P/ T8 N ^3 L
printf("buffer 2 equals buffer 1\n"); </FONT></P>3 a( j& W2 W3 m" S: l( s
<P><FONT color=#0000ff>return 0;
" `, O# a4 ?0 ^2 c}
+ v* F1 m3 ~# |</FONT>
& ]- t$ |' F N! }; T0 f+ T
- N1 Q. c9 R) C3 A+ A2 z* b</P>" H' l0 r! F) b2 I4 t0 g
<P><FONT color=#ff0000>函数名: strncmp </FONT>* A5 e4 t- K* U& {
功 能: 串比较 ( j7 |9 {4 T5 H* v* B
用 法: int strncmp(char *str1, char *str2, int maxlen);
( o8 i$ H6 N3 Z4 [- g6 Q2 \程序例: </P>
/ F5 c6 P* \7 X4 A<P><FONT color=#0000ff>#include <STRING.H>
g' f/ b. {8 B* M) l( }+ u2 ^$ f#include <STDIO.H></FONT></P># o- F% ~% Y& |9 I
<P><FONT color=#0000ff>int main(void) </FONT></P>+ C: w8 {/ q! k" s
<P><FONT color=#0000ff>{ 9 X5 C3 q3 i a# L- S/ L
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; * B$ i$ e) U" U5 [$ ~$ Z$ u
int ptr; </FONT></P>
: K6 w/ c& h5 D6 B% U2 X<P><FONT color=#0000ff>ptr = strncmp(buf2,buf1,3);
/ _% g2 ]/ b+ M8 j& s( n% x" B7 Kif (ptr > 0)
& I+ B$ T8 R0 ~7 S6 f5 ~printf("buffer 2 is greater than buffer 1\n");
+ h. S( a( l0 Z1 h% {+ Telse $ }2 s" h; b% c, J. ?
printf("buffer 2 is less than buffer 1\n"); </FONT></P>2 G) m# Y$ d. {: P, N4 E: A" ?1 { }
<P><FONT color=#0000ff>ptr = strncmp(buf2,buf3,3);
) ?& D0 R: _, B/ b/ U4 yif (ptr > 0)
3 ^% z' r2 J, ~: F* @# Nprintf("buffer 2 is greater than buffer 3\n");
* S* D6 v0 j* m% @0 xelse
: p- M' T/ K0 @" s# b8 H# _printf("buffer 2 is less than buffer 3\n"); </FONT></P>
, ]4 t5 Q/ L8 m6 V7 ?% s<P><FONT color=#0000ff>return(0); v+ L" r" b% ^1 g' Y; ^
} 2 |, I# F& g1 S1 U9 z
+ O0 w" z4 ]& K
</FONT></P>% _" o9 P+ B/ ^+ t% ]! R* ^$ m
<P><FONT color=#ff0000>函数名: strncmpi </FONT>8 I2 j( q, }; C/ I
功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
]/ J- U& A" \用 法: int strncmpi(char *str1, char *str2); 6 V5 U" d" t6 [& n$ w% X+ ?, E. k
程序例: </P>
2 y# N$ x; \% ]. b% w+ }$ h/ n<P><FONT color=#0000ff>#include <STRING.H>* O$ |8 L- I6 }+ G7 O) ^
#include <STDIO.H></FONT></P>
, E$ i( u( H4 o1 }<P><FONT color=#0000ff>int main(void)
( g/ R4 H. X/ A, V# p D+ |{ ) t0 U" e7 A" n5 H! ]' C
char *buf1 = "BBBccc", *buf2 = "bbbccc";
6 V( k. S! ?9 @! U: X& o5 iint ptr; </FONT></P>
4 M9 F/ h& F8 h, P/ r# L<P><FONT color=#0000ff>ptr = strncmpi(buf2,buf1,3); </FONT></P>. ]* V: K- I; F8 Q
<P><FONT color=#0000ff>if (ptr > 0)
* }. }: G! l9 K* wprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>: v& M& _* R! Q- u
<P><FONT color=#0000ff>if (ptr < 0) / e7 g) ^: [& b+ W
printf("buffer 2 is less than buffer 1\n"); </FONT></P>
( I7 [. ]2 p( ?1 O: s& A<P><FONT color=#0000ff>if (ptr == 0) . o& _( G8 g+ ]
printf("buffer 2 equals buffer 1\n"); </FONT></P>
$ ~! N" }+ Q( I3 j+ l4 n8 h<P><FONT color=#0000ff>return 0; </FONT>
' S: v" K6 s8 N: m}
* }: h; j" N8 ~: \& I; n0 [8 E- i
" _! Y# }; c( {7 [</P>
) w5 E( h, t: h9 [* P- r' U5 i<P><FONT color=#ff0000>函数名: strncpy </FONT>
, @6 ?: |% ^6 Y3 r" U/ d功 能: 串拷贝 ( ~+ D5 @" h8 C$ |
用 法: char *strncpy(char *destin, char *source, int maxlen); 9 V6 f% {0 W& A7 e% ?
程序例: </P>& z# P' y, L0 R8 w# D
<P><FONT color=#0000ff>#include <STDIO.H>8 Q: f% e4 V# M/ J5 C2 R, t" t
#include <STRING.H></FONT></P>
- U9 z" E5 ~ r+ |0 K4 {<P><FONT color=#0000ff>int main(void) - z z; m5 O9 r/ D$ L0 Y
{ ) F; U6 \. c- s4 V; h+ _- P# \; R
char string[10]; + d- L! E) I; x+ Z; K
char *str1 = "abcdefghi"; </FONT></P>
, P8 o$ ?9 v. ?; ~1 o<P><FONT color=#0000ff>strncpy(string, str1, 3); . O% F( q6 w$ P5 c: m# c! ^
string[3] = '\0';
& q Y8 m* s& c, |8 ?& L, Aprintf("%s\n", string); # o j" p3 Z$ U8 b% s8 J! @
return 0; 5 R* z$ j, A0 V8 d; ^
}
+ Y; I, x, S& C4 c2 {</FONT>
# H) C" v& Z* w</P>6 A9 I( e, ^! z4 q7 T
<P><FONT color=#ff0000>函数名: strnicmp </FONT>
2 T/ T1 }& C: @4 L功 能: 不注重大小写地比较两个串 0 V1 D1 ], c% p; v3 h
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen); ; r, Q' U7 V% S6 M6 H. c5 |
程序例: </P>
" @5 J7 a4 ^: B) [1 g% t4 N<P><FONT color=#0000ff>#include <STRING.H>
) k$ k5 \9 B% S! ]5 ~#include <STDIO.H></FONT></P>. [8 J1 \: c% |' d$ w
<P><FONT color=#0000ff>int main(void)
+ B" D- A$ }1 ^" n' ~{ , [2 I7 O$ x# o. M6 p) l8 y
char *buf1 = "BBBccc", *buf2 = "bbbccc";
# S- Z3 h- S2 B7 u7 ?int ptr; </FONT></P>
/ x0 J0 G, c' W& _$ T: M<P><FONT color=#0000ff>ptr = strnicmp(buf2, buf1, 3); </FONT></P>
; u3 H0 A4 T$ {<P><FONT color=#0000ff>if (ptr > 0)
; T2 o' G- V) l% v, P9 Lprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>( {9 y* L( K& k' U c5 C
<P><FONT color=#0000ff>if (ptr < 0)
) g5 @0 h8 Z0 T" T/ A* ?printf("buffer 2 is less than buffer 1\n"); </FONT></P>
( |, q; F* k: F. Q1 R" ]7 V<P><FONT color=#0000ff>if (ptr == 0) ' }0 T" O3 ]5 \; v) g" H, _
printf("buffer 2 equals buffer 1\n"); </FONT></P>
5 U& C/ r D$ u% P<P><FONT color=#0000ff>return 0; + T+ x1 Z( d6 l( W) B' h
} # F& A/ [1 v0 D0 {. T
( K1 r5 ^0 X# w% p6 y$ w
</FONT>$ v" X% B" U$ l
</P>
' }+ J8 e6 b7 I) T9 {<P><FONT color=#ff0000>函数名: strnset </FONT>
' {' d! V! J: j2 u功 能: 将一个串中的所有字符都设为指定字符
6 G7 o9 \0 j% e用 法: char *strnset(char *str, char ch, unsigned n); 2 @) @! E. L/ L0 F8 s% V. N
程序例: </P>
; ^' M0 H& @: Y6 K<P><FONT color=#0000ff>#include <STDIO.H>
/ q6 h( z% N$ ]2 H5 l$ O/ Y' Z/ I#include <STRING.H></FONT></P>* }: w1 B" {0 T# y
<P><FONT color=#0000ff>int main(void) 4 F- b( T4 _0 C p7 b
{
1 O3 ]% o' A! ^4 Mchar *string = "abcdefghijklmnopqrstuvwxyz";
7 o. f9 _$ P0 z6 ^ }3 `char letter = 'x'; </FONT></P>/ B% u* v* u3 K5 Y, p
<P><FONT color=#0000ff>printf("string before strnset: %s\n", string); 9 _' P% `+ [7 L* N
strnset(string, letter, 13); ; b3 X9 l. v6 |. [, Z! p) U
printf("string after strnset: %s\n", string); </FONT></P>, B+ `' R+ _& ~0 \9 u
<P><FONT color=#0000ff>return 0; 8 R4 V3 J$ \$ r
}
7 k6 F: D8 m' a+ q+ J</FONT>
) n0 X( `) \; N* L$ ]6 D</P>
0 @9 `3 y% E' ^1 ?% A: F<P><FONT color=#ff0000>函数名: strpbrk </FONT>! H8 t2 [) c: } Q2 j- q
功 能: 在串中查找给定字符集中的字符
7 q: i2 M5 }# E用 法: char *strpbrk(char *str1, char *str2); 4 T) p* g5 m4 L$ ^9 R& e( K
程序例: </P>; D3 a) ?* M% ^) A* K
<P><FONT color=#0000ff>#include <STDIO.H>5 _$ j% E+ @ O0 y3 N7 ^) a, Y' Z
#include <STRING.H></FONT></P>3 i8 H- a6 ?3 B* R( z1 N
<P><FONT color=#0000ff>int main(void)
6 w! @3 D$ |, _; l! [{
! s- E. \) w$ y+ ]# g3 wchar *string1 = "abcdefghijklmnopqrstuvwxyz"; ) D4 M' [! [' n0 [4 }
char *string2 = "onm";
. @4 `0 p2 A; x0 i' v* o+ }char *ptr; </FONT></P>7 Z4 j `- V1 r% [. l# ]' e$ {
<P><FONT color=#0000ff>ptr = strpbrk(string1, string2); </FONT></P>' i4 a! S7 j. D$ J! r" \
<P><FONT color=#0000ff>if (ptr)
. m. M( I; e3 Q1 u" I/ x# I4 Cprintf("strpbrk found first character: %c\n", *ptr); 4 }4 l: ^$ p# h3 }% j# G
else 1 C \8 S/ Z! \* @" z7 P# a( ^! O
printf("strpbrk didn't find character in set\n"); </FONT></P>* o' N, R) c5 y. |
<P><FONT color=#0000ff>return 0; ( D# @& C$ K; U+ `$ I8 E& y
}
. l) N x3 p$ z: o( O1 |3 S- d- s+ v: H( G
</FONT>
% K! u6 H1 A* b4 U, d2 H5 _6 S" K& |</P>+ A& s1 O( p3 `0 P! N
<P><FONT color=#ff0000>函数名: strrchr </FONT>& a3 r T' A" Q, G( O4 h
功 能: 在串中查找指定字符的最后一个出现 # b3 `" l9 P9 G
用 法: char *strrchr(char *str, char c); $ d5 k2 p0 s+ }5 B
程序例: </P>8 |7 Y, c$ s- Q0 x$ p3 w# u9 `1 A
<P><FONT color=#0000ff>#include <STRING.H>
( V3 W# v! F# ~3 Q( r6 ~' }#include <STDIO.H></FONT></P>6 z/ T+ U# `$ B5 O
<P><FONT color=#0000ff>int main(void) 8 @5 @( h. ?: g$ d0 B, p
{
4 A% a' S) @% Zchar string[15]; 5 I- p7 `( `! C) p8 w9 Q2 c
char *ptr, c = 'r'; </FONT></P> e" W. x; ?3 a& R% V
<P><FONT color=#0000ff>strcpy(string, "This is a string"); : Z6 ^3 F/ k! ]
ptr = strrchr(string, c);
2 m+ A7 O% w9 Pif (ptr) * r1 `& Z. ^; T
printf("The character %c is at position: %d\n", c, ptr-string); 8 U- k0 l) F' W8 j; \
else
2 r a! e1 n# b rprintf("The character was not found\n");
1 E, Z9 n2 v7 h5 ~7 L5 Mreturn 0; ; L* s1 L1 y. o( i Y3 r
} </FONT>
9 c x: Y6 v& g: z: b1 R# b. K7 A% A$ w# {1 t9 a3 ?0 j% Z2 g0 a
% d5 o. i: a8 ^; R# o</P>" `; H. g) K: ^- y- G
<P><FONT color=#ff0000>函数名: strrev </FONT>
+ ]6 J ]# A: E, O& A1 z0 H功 能: 串倒转 6 w+ c5 y _% G. H1 l7 r
用 法: char *strrev(char *str);
% {' l& t! w1 z: u6 Z7 }! l程序例: </P>
; J* e% e1 \5 w8 [+ ^<P><FONT color=#0000ff>#include <STRING.H>
# J: }9 s. q% X, P#include <STDIO.H></FONT></P>; ?$ S' A" q, i9 p4 q
<P><FONT color=#0000ff>int main(void)
! q6 b$ T K9 H/ _" j/ P. q) D{
; U+ C+ W7 Q, C* ?' q3 e9 |4 }3 Echar *forward = "string"; </FONT></P>% }- w G$ n0 ^/ v1 ] k
<P><FONT color=#0000ff>printf("Before strrev(): %s\n", forward);
1 e& n6 h7 R2 ~& K0 [' }/ pstrrev(forward); - w3 |1 m5 \" f. z) h, Q" P
printf("After strrev(): %s\n", forward); W! h6 L N% g: d1 o& s6 ^6 F% v
return 0;
3 m [$ f( X3 r/ }" L} </FONT>
# w0 V; @* ]5 l. P% i5 ?% N</P>
5 B0 X; X4 k+ @4 a8 O<P><FONT color=#ff0000>函数名: strset </FONT>, |* R* O0 [- v; I3 b0 i
功 能: 将一个串中的所有字符都设为指定字符
: Z6 P l* C! f; P8 C: P5 P* r8 }用 法: char *strset(char *str, char c); ; H4 v n4 x9 G& f' H8 j* ~$ f" t: {
程序例: </P>, X7 R5 e/ M, v4 z, L
<P><FONT color=#0000ff>#include <STDIO.H>
& Y' r$ H' w1 h- E4 t# `+ w' z#include <STRING.H></FONT></P>
) k( n3 u) Q8 |& c+ a8 t<P><FONT color=#0000ff>int main(void) 4 o; S' {) l3 C8 }9 u# `' o4 Z
{ & B4 T% }! T+ u/ s' N0 D
char string[10] = "123456789";
2 R3 o3 o& n$ u" z6 {) h$ e: v( {char symbol = 'c'; </FONT></P>
8 R( N+ L! {2 k+ v! ]<P><FONT color=#0000ff>printf("Before strset(): %s\n", string); 5 @+ y* t0 b* [
strset(string, symbol); $ B- Q" k3 @& n* m$ s4 f
printf("After strset(): %s\n", string); ; @2 [0 S) h" D
return 0;
2 c: E( i& F6 ^}
8 Q1 n# x7 n4 p3 O1 d& j9 Q1 f& q
* ?1 q( q& b' q3 N5 M8 O9 w</FONT>' _( B: }0 m9 K5 @
</P>
8 R& P# Q4 ?% w5 F5 k<P><FONT color=#ff0000>函数名: strspn </FONT>" w# P5 u/ C3 F% I7 Q T
功 能: 在串中查找指定字符集的子集的第一次出现
+ U! B$ R& a4 p' R$ x8 n5 D: b用 法: int strspn(char *str1, char *str2); 0 m! A: X+ N& B5 B" ?
程序例: </P># w- `8 y; I6 X: J
<P><FONT color=#0000ff>#include <STDIO.H>( b; ^+ {! F5 a% P) E- }" f
#include <STRING.H>
/ J: D: V! X: T9 H/ V; [#include <ALLOC.H></FONT></P>
+ Z( x4 Y0 A: V8 W+ S2 P<P><FONT color=#0000ff>int main(void)
- Q; K& r1 J/ J9 I! t$ S: x{ ! T& o; L: W" E) x6 w V2 ]
char *string1 = "1234567890"; 6 H: m8 d7 h* H
char *string2 = "123DC8"; % L# x8 N6 @) I5 q! `2 V
int length; </FONT></P>
: k, b1 j9 C& T, n& h8 b3 P: y7 _; |! f<P><FONT color=#0000ff>length = strspn(string1, string2); - b6 |8 f5 o8 U* S$ r3 @/ e
printf("Character where strings differ is at position %d\n", length);
8 w2 e* C' j6 W' @3 V6 J0 Z1 ^: I; Mreturn 0;
4 f/ E' m; x: G- T/ b% ^$ Y" ^, n}
* `' E1 W) V0 I g- m1 K. T9 n</FONT>8 P1 `% Q0 s" P e8 L- i
</P>
L4 a0 s' V* n( i5 T<P><FONT color=#ff0000>函数名: strstr </FONT>' G/ l$ s3 \& m' y, Z
功 能: 在串中查找指定字符串的第一次出现
" `# B2 m9 O+ Q. ^6 J- P用 法: char *strstr(char *str1, char *str2); . R0 K, T) `2 a3 c+ u) x6 q7 f
程序例: </P>
) Q6 W; x; F; H# a0 s- H8 f' _<P><FONT color=#0000ff>#include <STDIO.H>; @1 {' v# w& }
#include <STRING.H></FONT></P>
" N" I, X1 ~, F5 j<P><FONT color=#0000ff>int main(void)
- p8 g0 U5 `5 p7 n; d9 q{ * u0 s- A- s: q' v
char *str1 = "Borland International", *str2 = "nation", *ptr; </FONT></P>5 w& ^7 K7 V; H' D P- Q
<P><FONT color=#0000ff>ptr = strstr(str1, str2);
5 e, C+ o c8 l, k/ Uprintf("The substring is: %s\n", ptr);
' g9 x- ~8 r2 }8 u9 L3 q" R) ~return 0; 8 Z9 [. F. ~& N' U" _
} </FONT>) r. U- W/ A: c# h$ }) Y
9 @3 }% L0 _5 p8 E" j6 v; v
</P>' ^& I5 g [( F. B+ v
<P><FONT color=#ff0000>函数名: strtod </FONT>* b9 L. M+ `8 @( a
功 能: 将字符串转换为double型值 % a- I* A, N3 q9 q( N* e/ O
用 法: double strtod(char *str, char **endptr); ; @; x7 R! h+ S2 ^/ n7 c
程序例: </P>6 O' D) J* ~ h- m, u
<P><FONT color=#0000ff>#include <STDIO.H>! U2 ?# F, \: i( u
#include <STDLIB.H></FONT></P>7 X2 R# U+ L( c2 h- F3 O" D7 ?
<P><FONT color=#0000ff>int main(void)
9 n0 ]# L5 j; W. b5 I5 ~" m$ S' U! m{ / \; d! |$ e6 ?0 d% z/ Z% N
char input[80], *endptr;
; {# P1 B4 f, d6 s5 W. o: b; tdouble value; </FONT></P>9 p5 A6 N8 [) H/ Z
<P><FONT color=#0000ff>printf("Enter a floating point number:"); ' h$ G/ `: x$ K: ]% I4 [- z2 N
gets(input); 0 t; g$ X" ^0 u) u. N0 s
value = strtod(input, &endptr);
. A; e: x* \; S& g6 o- L) q9 G$ Gprintf("The string is %s the number is %lf\n", input, value);
6 j8 \9 V4 G% Vreturn 0;
7 r5 V* W- _* V2 H" Q v}
- m$ L- O, R. R</FONT>, D" s. f5 e- A+ I
8 H4 v0 Z) O f9 I. V5 V% R2 B
</P>( [9 \8 @* p. t% l' ^" f8 ?% j
<P><FONT color=#ff0000>函数名: strtok </FONT>
' |# m7 Y% H9 U功 能: 查找由在第二个串中指定的分界符分隔开的单词 / w, I) y& e2 h+ [- I% o. I
用 法: char *strtok(char *str1, char *str2);
3 g4 _ X6 ^3 W程序例: </P>5 P. i$ A# s' h. j
<P><FONT color=#0000ff>#include <STRING.H>( |* @2 Z( k3 c
#include <STDIO.H></FONT></P>
2 o+ A& K% J. Z# @: g: B, ~) }<P><FONT color=#0000ff>int main(void) ; |% J6 ]0 C- D0 M8 n
{ & M' j+ i+ @, r+ ~- `3 |8 C) N
char input[16] = "abc,d"; % \. O! j! l$ d; {4 B! P2 u
char *p; </FONT></P>8 e; \! J% t0 O: l9 v
<P><FONT color=#0000ff>/* strtok places a NULL terminator
" K" n$ B" J1 J$ fin front of the token, if found */ 2 v; k1 P" g6 g+ h
p = strtok(input, ",");
$ d# O. z- u; ~# S4 Sif (p) printf("%s\n", p); </FONT></P>/ ^8 ?% B5 V% d4 l9 t! I! w# @
<P><FONT color=#0000ff>/* A second call to strtok using a NULL 6 L/ C! J6 F1 i0 `7 b3 g% Y
as the first parameter returns a pointer 9 f+ `# @* {9 [, l- P& B
to the character following the token */ 8 c" M/ s$ I5 t) \
p = strtok(NULL, ","); ( Q( J5 H! Z1 V8 Z
if (p) printf("%s\n", p); * {, S4 D5 i( I- Z
return 0; , N- F" x7 t* X5 s1 I& Y' p7 w
} " E* f. W) g- ?: [1 u2 L! p
' h& f) ]: w6 J4 u' V
8 F2 U/ Y4 k, }</FONT></P>
, l) }' ]" m4 X3 v( x<P><FONT color=#ff0000>函数名: strtol </FONT>
9 |. Q W: E% Z1 R5 I功 能: 将串转换为长整数
* }" {$ w: R% |9 n( I2 m( J用 法: long strtol(char *str, char **endptr, int base);
; C, v7 n" M8 I; q0 A. C7 A程序例: </P>
5 P. f- B7 R' y$ R<P><FONT color=#0000ff>#include <STDLIB.H>* U1 x* k3 E) [, b/ g3 d
#include <STDIO.H></FONT></P>7 T$ H/ }1 E+ w' T- c+ {
<P><FONT color=#0000ff>int main(void) # k; @* O. S5 V2 a1 n( P
{
* q0 u! \7 }. a: Q/ R" dchar *string = "87654321", *endptr; ( z8 X/ H. r5 z
long lnumber; </FONT></P>
3 u, ^3 k- |" [<P><FONT color=#0000ff>/* strtol converts string to long integer */
3 I1 r, U' Z: a; f; j! c7 B" flnumber = strtol(string, &endptr, 10);
& X% @7 d# Z- S: Sprintf("string = %s long = %ld\n", string, lnumber); </FONT></P>
( _8 W2 h! E6 ?# j<P><FONT color=#0000ff>return 0; % O' \; N" Q$ r& W) M" k
} </FONT>8 M% `- v/ c. p; d! R* I
</P>3 K9 }9 G) g; Q+ O2 Q
<P><FONT color=#ff0000>函数名: strupr </FONT>
& l c' J* p0 V4 T4 X功 能: 将串中的小写字母转换为大写字母 ' c: {, i) L7 f- b
用 法: char *strupr(char *str); + G% M, N; O, q# R6 j( j
程序例: </P>
9 ?% e6 p% @2 X5 h2 H, |<P><FONT color=#0000ff>#include <STDIO.H>9 P% Y- d( y" {; Y7 k9 h/ c: N. C
#include <STRING.H></FONT></P>
" t! i0 U: g9 B6 {' j" ~<P><FONT color=#0000ff>int main(void) 7 V) p% |1 g0 Q1 v. }& w
{ , M9 F6 V3 |" K
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; </FONT></P>
g. Y% P( ?* E9 ^<P><FONT color=#0000ff>/* converts string to upper case characters */
q9 `$ ?7 ^$ Y; ]' [! Hptr = strupr(string); " B% o- m8 Y, t; w# b- r
printf("%s\n", ptr); " ]+ O7 u& B4 e: t
return 0; # h: J) J7 W) A( T6 ?$ }
} & P! M+ j9 _1 l- Q" q9 Q
! ]1 C1 d a; q
</FONT>7 F. H0 ?0 q, `0 I- [! l
</P>( I. ?- X' L; f/ h1 G# b0 K' O
<P><FONT color=#ff0000>函数名: swab </FONT>) X% a- j" R; O
功 能: 交换字节
8 B" y5 |4 e" D用 法: void swab (char *from, char *to, int nbytes); $ j6 I5 `1 C; X8 R$ ~7 o8 M0 @
程序例: </P>4 f" @ }6 R3 H# V
<P><FONT color=#0000ff>#include <STDLIB.H>
, n& E4 P- v8 l# h8 D9 }3 n1 e#include <STDIO.H>$ w8 l$ g0 ? K8 l
#include <STRING.H></FONT></P>- n- a" g8 U7 z# \- N! \
<P><FONT color=#0000ff>char source[15] = "rFna koBlrna d"; - a! v! s: M9 k0 D8 K* l7 O
char target[15]; </FONT></P>
6 _% k1 I5 b+ |$ |<P><FONT color=#0000ff>int main(void) 6 {8 b$ M8 i( Y' C; `
{ ; Z6 C4 V7 R q5 ~$ t
swab(source, target, strlen(source)); ; H& @' {% V" ?' k
printf("This is target: %s\n", target); & J) e* r9 `2 h3 x: o
return 0; F$ w0 }' t: K
} ' z1 p% e0 V& K" b
</FONT>
, r1 p# a% @* e% i' E- {, n. O
! R( O! B% J9 E7 L+ ^. Z5 \</P>
' | ~& j6 [2 B$ N9 e<P><FONT color=#ff0000>函数名: system </FONT>
1 t1 t7 B8 C5 z' W& l6 P4 @4 p功 能: 发出一个DOS命令 7 Z( q0 {& \! f8 b3 T
用 法: int system(char *command); ; F1 X- S: J! j* n
程序例: </P>
4 G3 b9 J6 ~* f# ?3 _( l<P><FONT color=#0000ff>#include <STDLIB.H>
9 W# a" [* r+ m+ t, g#include <STDIO.H></FONT></P>( l9 J& D' [/ j1 j. Q* x
<P><FONT color=#0000ff>int main(void)
( j! L l! Q+ ^{ ) b, d8 n6 g4 K; }* a
printf("About to spawn command.com and run a DOS command\n");
9 \2 V$ O6 ^" U. \- K) x3 zsystem("dir"); & s3 x# L" o! m4 p. L4 @
return 0;
7 v% X2 G4 p( v. ?! c} </FONT></P> |
zan
|