EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
【程序50】 % u3 s7 r$ ^7 o
题目:#include 的应用练习 4 ?0 ]1 n. W2 ]( l3 g+ [3 w H+ F) b
1.程序分析:
7 z9 M) K* W/ S# m2.程序源代码: # s' J6 O6 Q7 ?; s* E
test.h 文件如下: 4 v8 `# s4 \* l) ~% w7 x: |
#define LAG > & O# g8 r U K& O- b) D# H# I
#define SMA <
8 ~4 n2 y4 N1 H+ x; H) {#define EQ == ; [0 ^# |" v6 p3 C3 F
#include "test.h" /*一个新文件50.c,包含test.h*/ 1 M* ]. R: K2 _/ H
#include "stdio.h" 5 z% N# [# }) Y# E, k( G5 V e) x
void main()
% a1 b0 Z7 e5 D{ int i=10;
L) P. K1 T- F+ }" wint j=20;
6 G- d! N3 J% P# m* j& o8 y. |$ ~if(i LAG j) `5 C5 X7 I. ]: r
printf("\40: %d larger than %d \n",i,j);
: w% N( t. u5 felse if(i EQ j) 3 H: W' }" f& m$ P
printf("\40: %d equal to %d \n",i,j); ) U* d+ H3 [5 X) z" T L9 J
else if(i SMA j) " v3 P2 \" S) X- u- F
printf("\40:%d smaller than %d \n",i,j);
; {& h3 ]/ N; U4 Velse
" q& m R7 U$ O( T. Gprintf("\40: No such value.\n");
2 U5 c d4 _3 D$ l- T& {}
7 l0 P9 }; l- a o【程序51】 9 B$ N" E$ a5 w% K& Y
题目:学习使用按位与 & 。
! Y' E# v2 ^& h, ^1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
9 C; _5 K9 ]) N8 a5 }7 ~/ x7 N2.程序源代码:
* T1 f8 D+ S1 G5 N7 @* A#include "stdio.h"
g. L4 h c. I3 f9 H* ~main() ! @; ?" R$ Y8 u
{
7 `) r" r& `& s' w! Uint a,b; 3 [2 n+ j1 q9 t2 c; o- c7 J" a, D) d
a=077;
# _+ h9 D1 T5 g, V* I3 Pb=a&3;
5 R- `, ~, _$ D2 s K: Zprintf("\40: The a & b(decimal) is %d \n",b); ! O2 a$ t% B. O5 `4 R, w7 k
b&=7;
. s! {3 F e% T$ ~: m0 C6 y8 [3 y2 Yprintf("\40: The a & b(decimal) is %d \n",b); ! L7 R e9 w9 y( e2 F
}
1 A* ^3 v" y9 V9 K& q============================================================== * S# h, _" M! ~: N- V( c
【程序52】 5 F* X; _- {( W# ~. E$ J
题目:学习使用按位或 | 。
* s4 ~5 ?, @0 W( B1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
# w, S4 Z b/ r2.程序源代码: & a3 c" N$ q) b4 _
#include "stdio.h"
9 W) L0 }: g& ] n/ dmain()
9 c) t; C) S8 ?: C, W" J7 l3 ~* W{ * W6 Q- j, Q& `. f: r0 w" {" P
int a,b; - I8 ~ t" |: [; `. J" M6 t5 w
a=077; 6 G3 O' d$ v/ \2 h; Y" H/ i
b=a|3;
8 r: W" V& p" M6 X! H% xprintf("\40: The a & b(decimal) is %d \n",b);
" M* L9 t, }9 Y$ Ab|=7;
: O( T$ h: ^7 J8 _% Uprintf("\40: The a & b(decimal) is %d \n",b); 4 r$ Z0 H. m2 K9 \
} |! e$ d! C2 ]5 n* y
============================================================== 3 u/ \: J/ N- t+ @2 n" |
【程序53嵌入式信盈达企鹅要妖气呜呜吧久零纪要】
1 ^" o3 P# F: H( ~题目:学习使用按位异或 ^ 。
5 ~$ {% Z+ w* u8 p1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
; N0 @& d, D0 w4 N2.程序源代码:
) j: r9 |6 b( ~* | x8 }3 A#include "stdio.h"
( W! d& I* k9 g- hmain()
/ P7 `5 F9 D) [7 `$ r! ]{
7 w3 M. ?: U4 d1 o0 Fint a,b; 4 P; |% Z9 I( c) U' U* N! \2 v
a=077;
% w& L' D2 n! Qb=a^3; / R, w! S5 Q" t& J9 O, ]: o- H
printf("\40: The a & b(decimal) is %d \n",b);
: u# n5 p, E/ Q1 zb^=7;
: \9 b1 n3 B" V% ~printf("\40: The a & b(decimal) is %d \n",b);
/ C2 i8 p+ w' V% P3 w# Z! N2 _. k}
- z& q; `# b' o: A2 I* u============================================================== 0 w$ b9 R6 s5 A" t( e% c
【程序54】
. n. y8 E) A9 D题目:取一个整数a从右端开始的4~7位。
, ~) l! q# n& c4 o" n程序分析:可以这样考虑: ! \& g$ R! e* P
(1)先使a右移4位。 ( z! `' c2 H- q/ ^7 D4 |
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
9 `& Y, e- l7 n! T* f1 C(3)将上面二者进行&运算。
& [/ D6 Q3 k, }! V. P" r9 R2.程序源代码: 5 \: d$ b# A& h9 f7 L/ K
main() . O) D7 r4 h7 P# E2 u# B E: z+ L
{
+ d$ a$ \8 r a" M5 C0 w1 Tunsigned a,b,c,d; " J/ x6 y! r" b0 U- u, q2 h& P( U
scanf("%o",&a);
% H1 L: ^$ {3 g2 A- Db=a>>4; 8 k: e# B7 u- l9 y) }0 i
c=~(~0<<4);
) L7 ^0 U, x8 M$ t) id=b&c; ( v% G/ n2 C, v& n# ]
printf("%o\n%o\n",a,d);
0 ]* Y# u% ?1 F% w}
g4 y& k3 P* u==============================================================
/ M" f+ e D9 t4 _4 I【程序55】
5 F- G; `' K1 |$ W3 j' V题目:学习使用按位取反~。
9 {! E# Y+ p' V+ G# J1.程序分析:~0=1; ~1=0; 1 Z5 E9 g! D5 V: i
2.程序源代码: * [, B+ o/ x6 k& w
#include "stdio.h"
9 g( o$ @! U" G+ z( r4 W+ x0 Vmain() # f" J/ \3 ^6 w5 E2 c5 c% U" a
{ $ \" @* z$ a! M
int a,b; : m( A$ k9 ^0 Z) w( W
a=234; % T5 ~6 @* v' Y5 e/ w
b=~a;
& Y3 [: _* ]7 g+ {, G. dprintf("\40: The a's 1 complement(decimal) is %d \n",b); 3 g0 t/ W- @* b5 d) i+ a7 ^( B
a=~a;
9 A% t6 a5 s" v' f* ]& `printf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
( J) v2 |% Z& j* [* ~* t}
k3 z$ C8 f: {8 e6 }9 u4 S==============================================================
6 c8 g0 C% c G. x【程序56】
) W& s9 N& P9 w9 _' J4 w. A ~题目:画图,学用circle画圆形。 % w0 \* x0 |3 F& r5 s. |# [$ G
1.程序分析: u6 h& j6 ?( ^+ y2 Z5 {! k" z( H8 G
2.程序源代码:
0 Y6 q" l7 E; f5 D3 J; q4 Y/*circle*/
W1 U6 k! a5 \" {4 s2 c& Q#include "graphics.h" 3 O- O; C, F6 t
main()
; b2 v+ C7 p+ G4 b6 _{int driver,mode,i;
% j8 `/ |* Y# ^0 y w0 xfloat j=1,k=1;
; s3 {3 ^; j& I5 {* T, Hdriver=VGA;mode=VGAHI; 7 K' ?" h) ~1 m4 ?2 q R5 r
initgraph(&driver,&mode,""); ' p5 b9 p% ~- Z6 o. o- ^
setbkcolor(YELLOW); Y1 _# T' \4 c9 I
for(i=0;i<=25;i++)
: }9 K b" v! @' {& i7 s{
8 S+ A6 e5 L0 k2 J4 m* Tsetcolor(8); ! Q7 U( ]# K% g" b7 B
circle(310,250,k);
4 T) E2 @3 O7 i& Yk=k+j;
8 f! r6 `- B- b! d. W9 K7 I% Jj=j+0.3; 7 |5 U# y ~# r" s
} ' N; i9 y# `' \
}
4 R" `6 L$ D9 A$ z============================================================== 2 ^: i9 g3 D) J* p3 v c9 Z
【程序57】 * i" F) U2 A; Y( i) ~3 q
题目:画图,学用line画直线。 ) o2 b! U) z5 n5 V! k
1.程序分析: & @7 u' R. z$ }9 |- G
2.程序源代码:
1 p5 y+ s: A$ ~8 ]#include "graphics.h"
- E4 h5 N& W# ]" V8 p$ i+ Z0 Fmain()
6 z/ @8 E) @# z, @ ~ P8 [{int driver,mode,i;
}8 d7 y% ]# t. k( ofloat x0,y0,y1,x1; 1 |$ X9 O) i0 ]# |
float j=12,k;
' u6 j2 z. ~4 J8 S6 P! B# U. X1 G$ }driver=VGA;mode=VGAHI;
) X8 \- n( ~) J; |: R& G) f1 w; i9 Binitgraph(&driver,&mode,"");
8 k! v, L" g- Ysetbkcolor(GREEN); - O7 U- M1 U0 G. _ p7 O& N5 q) _ f
x0=263;y0=263;y1=275;x1=275; P) q# m% a5 J- m2 d7 B
for(i=0;i<=18;i++) 5 E- g* c3 {: z+ P4 x
{
+ l1 n Y1 G8 `( jsetcolor(5); , }1 m* D$ |+ z6 c
line(x0,y0,x0,y1); - ^- H6 U4 h- O D
x0=x0-5; ' ?3 ?5 p( q- w; o6 c! X4 ?
y0=y0-5;
1 h) r3 F4 `- b2 c, f5 l9 C6 Px1=x1+5; 4 B: d0 l* u9 ^! _2 ]# [
y1=y1+5; , `& q: J0 Q. i! N# \8 Y
j=j+10;
0 H/ ]. m% y+ t9 P- _2 F} 0 _, r1 t; |* e1 G4 g" C+ ]/ \0 q
x0=263;y1=275;y0=263;
& m: ~; q( d1 {. Y% b: Qfor(i=0;i<=20;i++) ; v3 C. K, W; ^! M/ K
{ % U- S/ G/ Y4 i/ d
setcolor(5);
?8 X8 X' A/ sline(x0,y0,x0,y1);
/ l( }$ R1 p2 f* l" {. J6 Sx0=x0+5;
' V, t; j2 P0 q0 w9 R+ U1 Yy0=y0+5;
" c9 L( k! A c) @* \y1=y1-5;
8 n' m& B8 w+ X. J6 O} 0 h* U" b# |+ X2 v+ P
} 5 t# G9 _& E2 b
============================================================== 4 N- \# _! }# F+ V' S1 _/ W
【程序58】 8 {6 R% f) p) k# v
题目:画图,学用rectangle画方形。 ) D2 m+ `) G, U: ]4 Q
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
1 T) O2 y& L1 A2.程序源代码:
' k- q+ _3 q: d8 Q; u#include "graphics.h"
, M0 d- T3 } j# L2 E! a( s( V9 ?main() 5 m/ D" Z5 h; C) j8 Q$ u1 P5 A
{int x0,y0,y1,x1,driver,mode,i;
' A* }0 A5 u G7 fdriver=VGA;mode=VGAHI; 8 e0 e) d0 A7 l' J% {0 j5 ~6 O
initgraph(&driver,&mode,"");
9 L6 i9 l7 D/ w/ w* D! Dsetbkcolor(YELLOW); " _4 _. P' Y l: A
x0=263;y0=263;y1=275;x1=275; % j% \. g& ?8 p' j+ [8 R9 n
for(i=0;i<=18;i++)
+ p. J* x2 _- Z0 U6 l" V{
+ D5 h/ V- f/ F: F& \: }" Z0 Jsetcolor(1); : r: Y0 M I! m$ }1 [( Z& r2 o$ l
rectangle(x0,y0,x1,y1); 5 Q( ]& t; b# T( X
x0=x0-5;
& g. S# I3 n; o- `y0=y0-5;
( e5 m' i( e: `, Mx1=x1+5;
% j! b4 Y ?8 gy1=y1+5;
' ^7 m5 v! h7 q6 U' t# s}
4 U, b5 l3 L1 }4 h% {5 Ysettextstyle(DEFAULT_FONT,HORIZ_DIR,2);
. U- v5 {6 r/ L2 O+ ?- c" [8 xouttextxy(150,40,"How beautiful it is!"); . I- \* G* M: |7 l- y* Q1 h
line(130,60,480,60);
% G8 F2 H! P, R" bsetcolor(2); Y/ r& `8 c8 V1 s' j! z
circle(269,269,137); $ H9 L8 o5 g3 r6 J+ j; D4 W
}
4 V4 o! A# `3 ]5 n0 V1 g==============================================================
: v4 l; a4 G! R) l2 l1 @+ W3 ~【程序59】
; S9 m1 J' k f. \: y% ~ w) \题目:画图,综合例子。
0 ^' _* R7 v3 _) c. ?7 r& A1.程序分析:
* v( s- E- z g" a) ]2.程序源代码: / O# p- V, _* G, N3 h2 U5 u
# define PAI 3.1415926 % x5 X$ N+ E& N
# define B 0.809
3 {7 Y6 e& k9 r* j3 i# include "graphics.h" 9 |, k# U: ~* d& z/ M
#include "math.h" $ B& m7 h k v: T+ O% e: z/ }
main() ( s' ]5 m6 y) b( D/ U( e( I! A
{
; [6 ~/ I5 n6 \2 j/ ^9 ?% D4 Hint i,j,k,x0,y0,x,y,driver,mode; 9 h7 d% c3 ~" @' z2 q/ x
float a; ( ~% W; C; z3 e7 I
driver=CGA;mode=CGAC0; 2 c- \4 Z" S7 d) ]) ]
initgraph(&driver,&mode,"");
' V. Y6 B' q& m3 Msetcolor(3);
" @/ ?# g# B- m# zsetbkcolor(GREEN); U" c: A8 q$ [3 r, [
x0=150;y0=100; 1 W+ T, w6 h( Z+ r* W- v
circle(x0,y0,10); 3 Y. Z# L: v8 c! Y1 Q7 W
circle(x0,y0,20); . }$ ?- F4 J. q" a+ u; @# z, S/ K
circle(x0,y0,50); 6 G% q6 i ~; k7 Y: `$ L9 [
for(i=0;i<16;i++)
' r) j" b+ N6 B! n{
4 ]3 o! w( q+ V6 \ Z" ma=(2*PAI/16)*i; 2 H) e/ |1 `: `3 V8 c4 b
x=ceil(x0+48*cos(a)); 4 q& R8 P' z0 _6 {4 z
y=ceil(y0+48*sin(a)*B);
M& k/ ^' F3 @5 hsetcolor(2); line(x0,y0,x,y);}
( _2 @/ N: V/ G. E+ y$ H$ E* D% Gsetcolor(3);circle(x0,y0,60);
_( O3 r) A- y" h2 b- f/* Make 0 time normal size letters */
j5 j7 g! ]1 R5 m- ^4 w4 psettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
& q3 B V, G. H1 Z. F! ?9 ]2 l4 W$ |outtextxy(10,170,"press a key");
, ^) v" E8 t u6 \3 ygetch();
R! ~; v+ p9 T8 w* o. Bsetfillstyle(HATCH_FILL,YELLOW);
/ m! @! x/ c* gfloodfill(202,100,WHITE);
# e2 Q2 `; z) J/ K5 H4 ogetch(); 4 P7 M5 C! J# v b; l
for(k=0;k<=500;k++) # M! e3 |( I- M( V
{
# O) r! L2 U+ h$ tsetcolor(3); 2 e9 K8 r' }( y' i# l R2 Z
for(i=0;i<=16;i++) ) `5 Z. l! r0 s, }# G6 o# C r
{
6 K& H9 L( T/ R1 B4 f- Xa=(2*PAI/16)*i+(2*PAI/180)*k;
2 p: m' t, I: r; U9 j, J8 M6 U. f" @x=ceil(x0+48*cos(a));
! [8 S5 n, T ^( l4 X; z( sy=ceil(y0+48+sin(a)*B);
4 a% j0 v8 x3 v& asetcolor(2); line(x0,y0,x,y);
( m) z$ O$ j% Z} 9 C! { F. P' F# X7 e- x( {! I
for(j=1;j<=50;j++) # V7 g+ ] I* S7 c
{ - W5 l# e7 J0 b. C5 h, p
a=(2*PAI/16)*i+(2*PAI/180)*k-1; 7 y8 m1 j! K& W) b' U) w
x=ceil(x0+48*cos(a)); 3 Z6 G i* x5 k6 c4 S3 g2 @' C7 E
y=ceil(y0+48*sin(a)*B); ; b# F* `7 B! w9 W& o# _1 s
line(x0,y0,x,y);
! X: d2 w; m( y" |; ^2 z4 M}
% D2 `$ I0 s4 {- n# [& {" x}
- i3 M5 w& u2 Trestorecrtmode(); ; X) ?9 I; u: r" s
}
6 z! j1 A0 D9 M7 E============================================================== 2 y1 ]- B' c2 h1 F* n
【程序60】
1 h8 I- n8 c6 r/ I P) U( C题目:画图,综合例子。 / A: E+ t% e0 @4 W
1.程序分析: $ I9 g5 K+ F) w: `
2.程序源代码:
) G- f0 _* j0 R, _#include "graphics.h"
4 K+ f- H8 b' l#define LEFT 0
1 K- Q/ \7 B" d- j( ^#define TOP 0 ; h* K) G- B B
#define RIGHT 639 " O M$ |0 o( }1 X
#define BOTTOM 479
/ X4 i7 E3 V4 r& J2 n#define LINES 400
- i+ D6 n+ F9 B* I! d#define MAXCOLOR 15
1 @) R3 ^4 ^; [main()
4 {" v" B4 T/ Q/ s y7 |0 T{
6 A! W$ A+ O/ I$ xint driver,mode,error;
k3 n) S( `8 eint x1,y1;
% \& H6 C" h* i9 t5 G% e% Jint x2,y2; , f& w x# j, `3 m! J8 x
int dx1,dy1,dx2,dy2,i=1;
( ?& g/ z# L$ ~int count=0;
2 ^8 y' N2 X2 \+ z' w2 c( y0 Eint color=0;
c2 Z6 P, E8 T0 Ydriver=VGA;
) q8 H* M6 s( I9 Qmode=VGAHI; & @$ x# c3 ^2 F3 p. S% L
initgraph(&driver,&mode,"");
. s# o! B& U' z9 |x1=x2=y1=y2=10; 1 B- f. V* O1 u9 E
dx1=dy1=2; o: K# x& ]/ \( T+ i
dx2=dy2=3; - J& ~0 C1 P7 \% i m. U2 g1 D! Y
while(!kbhit()) 7 g/ t! b" N3 W# L ?; E! I
{
0 p" D- x; ]7 C1 K6 ^line(x1,y1,x2,y2);
* x9 z; W* Y, | I6 C7 Z- r9 r8 M- rx1+=dx1;y1+=dy1;
5 F4 Y* t) O4 _3 H2 w$ ]3 a+ L$ i7 ex2+=dx2;y2+dy2;
- S, G; h8 c( |! fif(x1<=LEFT||x1>=RIGHT) 6 ^9 k. ^$ O6 k& r: E1 _$ R9 b- b
dx1=-dx1; 1 X) i t, y8 g9 n
if(y1<=TOP||y1>=BOTTOM) ; Y9 p A5 p1 \( ~; [4 H
dy1=-dy1;
- C6 W) P& Q- }( e$ Tif(x2<=LEFT||x2>=RIGHT)
& c0 A* m. ]) j, W6 ?dx2=-dx2;
- }1 f6 c9 I3 J& W3 ]if(y2<=TOP||y2>=BOTTOM) |3 M9 z8 |8 t7 Z. |( n/ f
dy2=-dy2;
3 k5 m+ A e8 F9 m- v- E- W( vif(++count>LINES)
$ Q4 z' H1 B# K3 {# h{ ; y$ p @8 A4 `3 t
setcolor(color); ) V+ T$ F% E. z
color=(color>=MAXCOLOR)?0:++color; & |/ k. f. y& c5 k; Z: ]
}
* Z3 H# s1 q+ L- x* d+ }) Y}
) S/ \* h! l2 v2 ?8 gclosegraph(); + ?. i8 i! k6 D0 T' s) L R, `( n2 Q
} 【程序61】 ! f) V! x& P0 ~" K9 V4 ^9 t
题目:打印出杨辉三角形(要求打印出10行如下图)
5 t# r# _+ a5 {: }1.程序分析:
. t1 `. z: E$ C8 T6 q L6 o" S8 u1 3 s, e$ c! x q. a# y4 j
1 1 7 E/ R) Q2 x# X
1 2 1 ! i& o1 [2 N0 M6 |! ^
1 3 3 1 , x5 H9 M8 C$ d
1 4 6 4 1
* L& ^% H8 f6 a1 5 10 10 5 1 + k$ Z7 l0 f. _% O1 @1 P' d+ x3 j3 ~
2.程序源代码:
6 h/ C4 R' B5 j, U. umain() 3 n' h7 ?+ P3 {" j
{int i,j; $ r4 |" w7 r) ]; K. f# x
int a[10][10]; `, N: } i# t* @3 `8 z* ^
printf("\n"); % ^7 e- C3 a1 `! J- ~" X* ?+ Y
for(i=0;i<10;i++) 7 S1 E/ J5 ` ~+ }" F* z- o
{a[0]=1; 9 k- p. F+ Q4 P8 f6 }& l: C8 A' U s
a=1;}
) |. a% u( {3 kfor(i=2;i<10;i++)
# N( ~9 L. |) Xfor(j=1;j a[j]=a[i-1][j-1]+a[i-1][j];
8 [+ ]% u% S* v) z+ @- Ifor(i=0;i<10;i++) . z3 R2 R% W: L$ j- M i
{for(j=0;j<=i;j++)
' ?; T' r0 I+ m0 T' P) Iprintf("]",a[j]); 9 J, U& r. R `8 |+ ^
printf("\n"); ! k5 r2 s0 ]2 b% A* F# h* d* P
} * U1 ^3 I3 t' m; o
}
! D( e; y# r0 k$ r8 K: K& o==============================================================
% E& j8 U) T- F$ F) B4 z【程序62】
7 o# D" g1 x2 m2 n" H3 ]题目:学习putpixel画点。 8 o& }5 C+ G3 M
1.程序分析:
" b1 M! D E5 E; K. _2.程序源代码: : Q( i! t7 V' Q e/ L- k
#include "stdio.h" ( B5 \: e8 U: g% h- m
#include "graphics.h" 7 ^/ F1 ?& W; A7 q: ]
main() 5 U6 D7 n: n9 S! A0 E
{
; x' J# x$ Q7 b3 c7 Q0 _6 f( q; [+ W, _5 ]int i,j,driver=VGA,mode=VGAHI;
( M6 ~. q) ]5 a5 B& |. r( E0 ^& Rinitgraph(&driver,&mode,"");
! x4 X6 G/ t! }! k* k& q3 `" B. csetbkcolor(YELLOW);
% Z. U) [3 `% ]5 U+ `# ]" |- ffor(i=50;i<=230;i+=20) . i" x* E% Q7 h5 P- y! c2 M6 G
for(j=50;j<=230;j++)
) S- j: g+ s5 A) F2 d, eputpixel(i,j,1); 9 j0 h# a; V9 j1 k9 v9 H3 H
for(j=50;j<=230;j+=20)
) ~9 ^$ K/ \& `1 ^. g2 t4 D2 vfor(i=50;i<=230;i++) , C4 o% ?2 P! t0 r
putpixel(i,j,1); ' C" \$ r, h; z, `2 }8 K
}
/ u- k" t t& f) b% ]' U, b==============================================================
) o8 Z6 w6 B7 Z; X【程序63】
2 M* {& v0 q6 [9 Y3 V6 `( G: g5 u9 G1 P题目:画椭圆ellipse
2 C" b7 c! V' `! d8 `0 o/ q1.程序分析: ' B0 n2 b- M* P4 K# V# j
2.程序源代码:
; Q0 k" I5 {2 F0 {. w9 Q8 B1 ?/ W#include "stdio.h"
* x4 {/ p/ `0 n1 X#include "graphics.h" 0 ]3 ~. H9 i! Y3 z) C: l% o
#include "conio.h" % w4 D8 ^, f4 O7 W9 P
main() + |- C4 r c/ P2 N( _
{ * f: t/ D' Z% U- z) ]6 D
int x=360,y=160,driver=VGA,mode=VGAHI;
% u* R0 f& n/ Fint num=20,i; 4 V4 g% ^ ?* E) M; T* [
int top,bottom; 7 O, l& U- `, O
initgraph(&driver,&mode,""); # ?# P: [: q( g0 R, M
top=y-30;
- n5 G1 n r, ibottom=y-30;
5 w" q2 W6 H v/ cfor(i=0;i{
7 C$ I2 V6 m) M3 `+ h& J9 ~# Lellipse(250,250,0,360,top,bottom); ( x- t, `% u0 U- ?0 [6 ~( ]; M1 K$ l8 i
top-=5; ' R' u4 s1 h1 Y/ ~$ m4 ]# u
bottom+=5;
: c/ Z1 R6 E+ o2 b8 b} / |# f, w# Q! h% D# R
getch();
4 h+ z& Q5 M( m! k& c) }/ T} . M/ B" O" X8 V/ Y9 l, L# g
============================================================== " Q2 }' h, l6 k; ?6 h/ J% C
【程序64】 * f- }! v, S4 Q( F
题目:利用ellipse and rectangle 画图。
" M+ H0 _6 \9 E- J7 o8 ]1.程序分析:
- j$ g1 r+ {, y" |/ \/ L2.程序源代码:
# s& r7 h6 c# M, F" j/ \# ~' @( z#include "stdio.h" 6 D* M6 ~- g/ ~1 l! K
#include "graphics.h"
; t' o' c% D* T% C' |* E#include "conio.h" - m& }. U0 e2 G/ z+ o
main()
9 o/ k) ?5 N: i2 r' J( T! E8 J! M4 c{ % P J& y: ]6 ^ _% M+ h
int driver=VGA,mode=VGAHI; ! z$ w+ b2 N! z( `; H
int i,num=15,top=50; 0 E& `0 w& c" O# @" J
int left=20,right=50; , K- y# U$ k2 U4 Q: \' P; L) a
initgraph(&driver,&mode,""); . \, ^ K7 {7 s2 O' v
for(i=0;i{ ( z( P/ F$ P. s, J9 _2 N* ~8 _
ellipse(250,250,0,360,right,left); # R* A+ R s0 b- ] d+ ?
ellipse(250,250,0,360,20,top);
+ L) J+ c2 X Jrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2)); $ ~( N9 l$ A G! s
right+=5;
) L, F7 s/ f+ D$ D7 C C- _0 vleft+=5;
' e% J3 `1 y" Jtop+=10;
! F# Y& {. b- E/ c}
' y6 Q5 u$ L+ R, j- o7 H H; R; `getch();
_- I6 u, C/ M3 a$ D}
% X# l0 X. { {. ^! \4 v==============================================================
- k$ X6 r: H1 N+ y( A+ V【程序65】
, v$ W; U* \) b* Z$ o4 c+ r3 y& N/ G题目:一个最优美的图案。
; h& Y/ x, K# T: v, `. n1.程序分析:
3 ]3 T: X# ~) K! q2.程序源代码: $ T" X8 A! ?$ S- }3 T
#include "graphics.h"
( S7 ~1 \1 L, i" i# K2 k#include "math.h" ( Q+ J# D+ v8 f. P/ q1 l0 H6 {
#include "dos.h"
! K; l' X5 N* d% a#include "conio.h"
7 m' M& A9 n- i( }#include "stdlib.h"
' B. H8 v& }6 i6 Z8 I& M- M7 }#include "stdio.h" ! n& p! p2 b: [& w( `
#include "stdarg.h" " p* t6 p( H- i
#define MAXPTS 15
* u6 i# }# i' x( K) @" A#define PI 3.1415926 / I# J7 Q: V$ j, M" s& c/ \8 ^0 X
struct PTS { $ o2 I2 Q3 ?' d* L
int x,y;
/ I, k" _7 B; k" y}; 1 [ M# l% V, _+ {) [ G
double AspectRatio=0.85;
# T6 b8 j/ O0 M- C avoid LineToDemo(void) / w! [" ] D* s; f/ \; M# p
{ # l& L9 E6 v. D1 H( E$ u
struct viewporttype vp; " J) ~% c7 t% h7 D) P
struct PTS points[MAXPTS]; 9 n4 l1 Q! R3 z1 R: V5 j# q
int i, j, h, w, xcenter, ycenter;
- Y9 v1 B; c2 o' ]" Z5 qint radius, angle, step; ' { c0 c9 @5 i4 [. g& H
double rads;
0 q8 Q% s t5 E; \- Y$ Vprintf(" MoveTo / LineTo Demonstration" );
* A+ a+ \6 u. L! Dgetviewsettings( &vp ); # ?7 k3 [ ^! X! U$ B! }
h = vp.bottom - vp.top;
! c- v- c7 t; e3 x1 ^w = vp.right - vp.left;
/ \5 z9 @% B4 [' C$ A& a% mxcenter = w / 2; /* Determine the center of circle */ 3 j6 u2 U" H* x0 L+ `. h/ J
ycenter = h / 2; . k& V$ |* E& j$ \4 a$ d& E
radius = (h - 30) / (AspectRatio * 2); $ k6 Q4 d( c* m( L
step = 360 / MAXPTS; /* Determine # of increments */ & }( ^) s' m- U- ]$ S; ?$ \
angle = 0; /* Begin at zero degrees */
1 T# d9 X* n7 j2 L+ L0 @% k& v% j' Tfor( i=0 ; irads = (double)angle * PI / 180.0; /* Convert angle to radians */
; m0 t3 Y/ Q1 X8 Cpoints.x = xcenter + (int)( cos(rads) * radius ); / J' v# J! }3 |! T& f
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
: X7 o7 T. ]! H1 ^- Dangle += step; /* Move to next increment */
6 I' `4 {" `. s4 P4 y) Q}
8 x9 a- e$ A) B6 o" Fcircle( xcenter, ycenter, radius ); /* Draw bounding circle */
% J4 f+ F# g, J: wfor( i=0 ; ifor( j=i ; jmoveto(points.x, points.y); /* Move to beginning of cord */ / T- x% Z% ?& t
lineto(points[j].x, points[j].y); /* Draw the cord */ , |3 J- K6 D' z) v: w0 z6 x. A: n' u
} } } 3 `5 T2 H! k7 w- D6 j+ p
main()
! j- N+ S0 Z, h; n( r& V{int driver,mode;
6 \5 o% S" j* y P2 Q& B! Cdriver=CGA;mode=CGAC0; " g; M! ]# s: G3 E
initgraph(&driver,&mode,""); / o. |0 C; `# P) t6 n& Y
setcolor(3);
+ G+ W6 O& \5 r8 Fsetbkcolor(GREEN);
( w% C- ?- a( l9 Y' VLineToDemo();}
/ U3 O7 v' s: |2 M2 d) a( c2 n============================================================== . ?7 `9 T4 M- i7 S0 m% @' \$ }! a
【程序66】
% N* c, ?* x: I4 u% M' c. j题目:输入3个数a,b,c,按大小顺序输出。 / e5 }& u# E/ ]$ u
1.程序分析:利用指针方法。
0 `$ r8 t0 Z" v/ e u& ~2.程序源代码: M3 D! K( n. v" w
/*pointer*/ " Y& A! `- h* D# Q" A9 n
main() # O# j: d1 ~! s) M* q/ d. \2 j
{ + K1 J9 X" C6 t1 ]
int n1,n2,n3; 0 Y( a, X# ^2 N* l
int *pointer1,*pointer2,*pointer3; , m% Z+ S. D# y5 |/ @0 a! W
printf("please input 3 number:n1,n2,n3:");
4 X, @% Y; Q- W. ^1 o6 hscanf("%d,%d,%d",&n1,&n2,&n3); 6 ?7 N8 t! [0 _0 }/ Q$ K
pointer1=&n1;
+ E% U" y$ Q! Apointer2=&n2;
% Z5 I# }$ K/ Z1 g4 m. Upointer3=&n3; - W c) I, ?$ f5 F
if(n1>n2) swap(pointer1,pointer2);
" d7 k* Q# u- b7 u$ h: m7 Sif(n1>n3) swap(pointer1,pointer3);
3 n" a7 n( b" c" f R4 xif(n2>n3) swap(pointer2,pointer3);
( \0 }+ Y+ ?! c9 Y6 @" w4 `4 `printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
# M. E S/ h- I" u3 Y}
& T1 l) w/ ~/ _) k3 y# q) [swap(p1,p2)
+ ]- z$ ^% s3 }1 L9 C; g0 dint *p1,*p2; 0 Q, S1 c0 W1 M
{int p;
6 T, I9 i; T# v3 N5 @" Z0 [p=*p1;*p1=*p2;*p2=p; : R% P9 U# j3 R( Z
}
' L2 c6 z! }- b) j$ L============================================================== % }! U3 p/ n' X5 i! T4 {
【程序67】 & ]& x; Y5 {; u% g5 K( z
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 ' j0 ?0 E1 r! R( Z! d
1.程序分析:谭浩强的书中答案有问题。 " l2 a2 ?* H% s2 Y2 E
2.程序源代码:
3 }1 c9 z/ E- K7 u Umain()
/ K; g2 c0 n' C$ B% R{
" O: G/ k0 Q0 W* m/ F# B6 yint number[10]; ) H% u5 w. {' [$ H
input(number);
1 B$ n# r( D j5 smax_min(number); 7 Z# j# I8 X' Q5 u1 B+ [) t1 y. ]
output(number);
; |/ l. S- U+ J- A4 m} ) o2 [6 G# N7 U6 P( t2 ^
input(number) 8 Y( Z5 `1 h7 z( Q- S2 ~
int number[10];
8 s" [5 ~6 h; h! w# H6 v! R4 S$ [{int i; + ~4 o$ f( I# R8 {$ e/ g
for(i=0;i<9;i++) 3 a( _, r* Z& ]- O# q$ z( ^5 @
scanf("%d,",&number); . I1 m |- t; o6 m0 {4 ^
scanf("%d",&number[9]);
3 a x8 ~2 w3 v0 a2 S}
& _8 q+ l; i7 J0 d7 Z4 ^' pmax_min(array) 4 R. B! |: P( s' t
int array[10]; - Z2 A; X3 ~' k- I. B% e
{int *max,*min,k,l; ' P. E7 B8 } _, X
int *p,*arr_end; & n' b/ `8 e8 V6 R: M0 V) j
arr_end=array+10;
1 g) ?9 G3 _5 ?- q- Smax=min=array; : s l5 |! _; q3 y
for(p=array+1;p if(*p>*max) max=p; 5 [* m7 S% N) ^6 P9 l8 V4 W T' ^
else if(*p<*min) min=p;
1 b5 ^/ \' I) F6 Z" u4 \k=*max;
2 s: l5 E- m; E0 l3 dl=*min; P% b/ z, W- ?0 |: [. ^5 e
*p=array[0];array[0]=l;l=*p; $ w3 W; `7 q. ]# D$ V4 q8 A
*p=array[9];array[9]=k;k=*p; . P& e0 J) J. H) p
return;
g6 B! g* m7 M1 a}
5 f$ H: n1 ?4 N+ H3 Routput(array)
3 `+ b: \" f9 E6 L* S: Sint array[10];
% J' N3 b) G! j) j{ int *p;
8 X- R7 l8 d/ wfor(p=array;p printf("%d,",*p);
# r2 y: y+ O+ D8 D9 l0 {printf("%d\n",array[9]);
) e5 c( ~, M0 e8 v1 A6 R} # o6 w, e* V# |3 \
==============================================================
8 \! r: w1 q' k) z
. h% U0 J* A$ l; D: t$ f |