EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
【程序50】 2 c j. r0 F6 D* V0 ^' f
题目:#include 的应用练习 @+ u# l! U; a
1.程序分析: ( Q% r q- N; ~/ y* ^
2.程序源代码: & K# z' z& y& P7 G
test.h 文件如下:
# `' C! E1 d' g#define LAG > + w4 j6 v: T3 j! r2 T
#define SMA <
# l0 H9 U+ R& A f#define EQ ==
4 X" D! |8 m$ r! O/ n. c0 s#include "test.h" /*一个新文件50.c,包含test.h*/ 2 r; y4 o+ w9 \8 ^: `1 ~, O# _
#include "stdio.h"
- v- k: Q* E o W$ u7 d U8 d+ }void main() 3 Y3 B8 M3 r5 m2 o3 V3 G! Y
{ int i=10; ; b( S( T' n7 N" I
int j=20;
0 f2 ]; _( }5 {7 }9 `$ Sif(i LAG j) % P$ _, V Z2 c# I
printf("\40: %d larger than %d \n",i,j); ( F' _4 w+ \( n% H
else if(i EQ j)
" Z: B. J4 R- B; |7 @) |printf("\40: %d equal to %d \n",i,j); / J. r1 h6 i) j, A4 B
else if(i SMA j) ! i/ b7 U+ e, E4 a
printf("\40:%d smaller than %d \n",i,j); # \% ^- z1 S! j! {- h0 T
else
6 t, q9 |! F Q0 q0 v$ `! C }, Kprintf("\40: No such value.\n"); & O; ]0 V* q4 }" q# x1 w1 w! W6 |+ s
}
( w: ~: N# M# E1 ^1 I" e7 N【程序51】 1 n* d& i8 B* D4 j0 j& `
题目:学习使用按位与 & 。 6 b# K4 `- R" D; c: T5 C8 Z, A
1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 4 R3 a% J4 G3 @/ c% A
2.程序源代码:
1 m k+ x( P' ]' F/ w) G$ _+ s( K#include "stdio.h"
, T7 r6 ~. t/ E4 D4 Jmain()
( p/ V6 }5 p i, k- h i D; i{ 9 w, e" T. y. z8 Z
int a,b;
& O9 b( L" H; D6 u: |a=077; & E( D/ X: H* T+ ?7 y( f
b=a&3; 4 {. \% E0 \) y' q
printf("\40: The a & b(decimal) is %d \n",b); 7 L8 g P; u( f/ y. D" O
b&=7; ) F0 I/ z1 \. p" o! \+ p
printf("\40: The a & b(decimal) is %d \n",b);
* N" M" H% ^! O3 O1 K- n} 9 B+ ^# D% m4 j1 x
==============================================================
5 X* r) T2 T" Y; g5 K3 b( r【程序52】
( ~- R$ F. E; P$ {, R题目:学习使用按位或 | 。
$ c' ~% x. H6 v3 R1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1 5 l/ ?* K; @% ?8 u. h
2.程序源代码: 9 |% i/ ]+ ~& Z1 G: Q4 B# c3 q c
#include "stdio.h" 7 Q" Q) ?9 \* e) g
main()
, ?% f8 ~' ]( N/ r# M8 `3 m) ^{
: A4 k/ z; e. S$ D) Tint a,b; $ ^' a2 X+ A0 Z1 d6 `6 D
a=077;
( a2 v/ _3 z4 ^. q0 Jb=a|3;
" K. D# Y7 `2 w" X. E! E. c5 jprintf("\40: The a & b(decimal) is %d \n",b);
- N: A: O7 L: V# T, W: Y' `: zb|=7; 3 Z* W( W) e+ l7 j7 P
printf("\40: The a & b(decimal) is %d \n",b); 1 _2 b8 t; p- s. R# a' p, Q
}
T& X0 g. w A$ f==============================================================
+ X4 X0 C B, z8 m6 r/ e t【程序53嵌入式信盈达企鹅要妖气呜呜吧久零纪要】 2 B: W& ^. m3 K3 w$ h: ?! h% b( C
题目:学习使用按位异或 ^ 。 9 K+ ]/ _, f; c j" U7 Q0 u
1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0 ' A1 C, N7 Y& W1 s2 h: W
2.程序源代码:
* o, d( R$ h" g$ P t#include "stdio.h"
6 y+ V% G! c6 T: v; w1 `main()
( g! ?0 `1 B* H) k* O{
" {1 i r+ M; j, C( Xint a,b; |/ I8 _3 @* H1 x5 W& ^) F
a=077;
9 [/ V; r, H5 q3 Y5 ^b=a^3; " Y7 L& e# }" x( X9 h5 F) e! ]
printf("\40: The a & b(decimal) is %d \n",b);
$ Q4 \7 d& [2 ^" P/ z% B( ub^=7; ! F2 i$ V g, m. h r. Z
printf("\40: The a & b(decimal) is %d \n",b);
a& Q }! ~& K}
, c2 {& O' l0 L============================================================== 7 m3 ~: J' o' c: w! |
【程序54】
* t# u4 T. x8 ]6 Q3 C题目:取一个整数a从右端开始的4~7位。
* k1 |) |8 d9 V; q程序分析:可以这样考虑:
: ]$ o- C7 o7 f1 X; a( s) y1 {(1)先使a右移4位。
3 t$ A7 ~, S. ^/ z% Y- |(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4) 4 D0 P. J3 c2 |' r+ ]8 |
(3)将上面二者进行&运算。
7 b$ R. m9 h, k: H9 S% v6 A/ {: g8 m2.程序源代码:
( l/ S% n7 x2 R+ Y# Lmain()
1 l% l+ d4 `5 G- h: r# j& |* I{ ; e2 z2 Q% V* l6 l4 E- Z4 e- f" ~
unsigned a,b,c,d;
0 a% U' N4 a# oscanf("%o",&a); 7 x/ c& [% a2 w( z3 }. B
b=a>>4;
( ~! L/ Z$ S& ?# b; Wc=~(~0<<4); : q0 D+ ^3 k G5 S% u/ D
d=b&c; : M1 i1 p7 G3 X9 u
printf("%o\n%o\n",a,d); ) K8 C4 v. r% P9 F
} 1 X7 o& _9 R0 J9 Y
==============================================================
% A% {2 j7 f2 V7 G2 K【程序55】
; t/ D- n& j N0 X# O( n题目:学习使用按位取反~。
- E5 G% ~7 l0 C y1.程序分析:~0=1; ~1=0;
$ u- _% O0 o( V7 G$ A/ `9 n) H/ V2.程序源代码:
6 p) |1 t* d7 v/ X5 o1 q#include "stdio.h" ) t9 H5 t2 _' M
main() + @( G2 ^+ h: O4 z
{ " m% a$ M8 t/ n
int a,b;
4 j6 j2 Q. [& K ta=234;
% A8 W3 i* d: v m$ _! Y) cb=~a;
: f2 S2 A' x& I# x3 q2 Fprintf("\40: The a's 1 complement(decimal) is %d \n",b); , E* v5 U. b" h' K" u8 x. R) [" e
a=~a; 7 S0 w' `& x% o% E8 @5 r; i
printf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
* ~* \$ P3 \% E) c} - Y" b/ F3 I/ l) ?& T
==============================================================
/ X: _3 }* n, D4 V- l% {' C【程序56】 & m% R$ z, I3 P
题目:画图,学用circle画圆形。 ; R) }1 t0 f' v6 _9 z4 O( n1 F
1.程序分析: ' {9 e' c5 M- s$ K' q
2.程序源代码: / A" B4 o. E! P1 R: g
/*circle*/
# T% E8 H4 N: I! h7 ^; @#include "graphics.h"
. p3 }% s0 E9 [# Bmain()
" ~# o8 Y" f7 u" R0 Z! S{int driver,mode,i; d5 o( m. b& n: q
float j=1,k=1;
& B$ X- f6 [1 N, \driver=VGA;mode=VGAHI;
* a$ W! T' a1 B5 r. ?, ^initgraph(&driver,&mode,"");
; Z2 Z( k0 l4 Tsetbkcolor(YELLOW);
+ y) F2 o4 w3 ^* h3 Jfor(i=0;i<=25;i++)
) t4 C/ @2 \( \ L{ 5 A! X* |" H7 x3 G. v/ C3 A
setcolor(8); * f/ n) u: U) W: I! ]+ I" }
circle(310,250,k);
5 O) I3 o' a+ h1 Nk=k+j; . T- }# w; ]# R! r9 M2 A
j=j+0.3;
. N# A! G/ x4 ~, W8 D} $ E6 i5 |' @. I9 F3 [/ [' G
}
8 B% P' M! p3 H! R& H2 f- h: n. |============================================================== 5 {- q- N. U. U0 H5 [6 b) y3 Q2 f
【程序57】
8 `) k& p! M, ~3 ^2 U- j题目:画图,学用line画直线。 : [# s8 b5 v: X2 M* M
1.程序分析: % l# B. y% K B
2.程序源代码: 2 n% V2 G% `: `+ R* \1 Z. w) i
#include "graphics.h" ! t5 m& x: G5 r
main()
" L. M. Z" j' [2 ?{int driver,mode,i;
4 o7 N V1 N" X# S* ~ s- nfloat x0,y0,y1,x1;
* m4 l! a; o/ p/ v) e+ Y! j$ rfloat j=12,k; % \) ?, Z' o$ X" z1 n. w( j! p
driver=VGA;mode=VGAHI;
: y- J3 M- M) X) ?, M$ dinitgraph(&driver,&mode,"");
* X+ n+ r: l \8 hsetbkcolor(GREEN); ; H4 a1 i1 Q9 K1 Q% k9 Y' D2 Q3 s
x0=263;y0=263;y1=275;x1=275; 5 a4 D+ }! R+ p; ?5 w
for(i=0;i<=18;i++)
0 f8 ?' Z- u' Z6 D{ / q. I: } j) d3 `- N1 N8 Z
setcolor(5);
* ~7 q# E" O( Z$ i+ B" Pline(x0,y0,x0,y1);
/ X! V$ ~1 }: t0 D! D) hx0=x0-5; " a# _* Q% U7 {; d- Z& w
y0=y0-5;
) w. H- O+ B+ Bx1=x1+5;
+ b( M2 f* d( H @: k/ By1=y1+5; 3 B1 g& r- _( q! [0 H& {
j=j+10; ( I8 Q" T( a1 ]' ?2 B" R
}
. ]3 K$ T7 W. v+ gx0=263;y1=275;y0=263;
) O: \ A/ N9 W+ @3 Qfor(i=0;i<=20;i++) u1 R* U9 i' }8 S" T( x
{
9 l6 n0 [. U- o' f4 A }, t% Hsetcolor(5); 4 X4 e2 W4 F9 @9 K
line(x0,y0,x0,y1);
4 t4 z0 D) z7 E3 W* d# J" Wx0=x0+5;
a8 Q4 ^* A$ z1 \y0=y0+5;
3 U# \$ }9 K* ?! x+ J: p; sy1=y1-5; 5 G$ X* c+ i# l+ I& R
}
/ h1 }& [. p* ?% j! b}
6 Z; O t. B7 R$ B0 s( }) D. c============================================================== , b2 \% N2 _, u# A/ y: g
【程序58】 ; F- D3 y% o2 H3 `7 @1 l% f2 J5 E3 e" V
题目:画图,学用rectangle画方形。 ( g) B+ ~) x/ j& }
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
9 v# n% A5 @4 q' e8 o& W# T2.程序源代码: 1 a, {) z/ ^5 g7 k5 O/ \8 P. k
#include "graphics.h" e& i8 w& J# D3 C
main() 0 k5 u1 l- C |8 c
{int x0,y0,y1,x1,driver,mode,i;
' h+ @) Y R3 X" v: D1 bdriver=VGA;mode=VGAHI; 8 U9 k0 P+ H. w3 s& z* r* M
initgraph(&driver,&mode,""); 7 {" T* u9 U4 M* W
setbkcolor(YELLOW); 8 U: Z! `1 H3 ?) u5 a% ?0 V
x0=263;y0=263;y1=275;x1=275; ! t9 U' b' s* g( Q* f' \+ s, ]
for(i=0;i<=18;i++)
- Y- O' J. y- S0 _, y% c4 r{
r, ?* b- i" \- T7 e) [setcolor(1); t2 l1 h5 Y9 c, l' I9 i
rectangle(x0,y0,x1,y1); ~( o- D5 L8 N2 @
x0=x0-5; - A& x# A8 ~- H# V
y0=y0-5;
6 B, B4 m$ ?7 H% Ax1=x1+5;
; @0 ^1 E- F4 oy1=y1+5;
( r% N$ N E( _6 }/ B} - e9 x$ {5 @, \/ I7 S# l3 L) ^5 ~
settextstyle(DEFAULT_FONT,HORIZ_DIR,2); 0 ~6 x+ J* A/ Y: _' ~+ ?* L- j
outtextxy(150,40,"How beautiful it is!");
6 h# t6 m e5 \6 Xline(130,60,480,60);
& U+ G' o$ h, V8 \" V! Psetcolor(2);
# Z/ w3 W% m& {% B3 M; j9 R9 B6 zcircle(269,269,137); ; Y/ A Y3 R3 n+ O
} & J& O$ O( y" M3 d! l
==============================================================
8 a" H( A( m' L; x( _" N, K【程序59】 + {, W6 }) G& x- _. C" Y
题目:画图,综合例子。
7 ~3 q% s8 M& u- v. c+ N$ b1.程序分析:
( V( e8 c3 S8 t$ s2.程序源代码:
( j) X+ a9 |, f+ f& ^7 T! U. K8 E# define PAI 3.1415926 0 H4 \/ ?3 `4 H+ _! x2 S
# define B 0.809
0 D( o5 f$ x# U y7 f3 W# include "graphics.h" & l, g; L" ? _4 b4 B' N
#include "math.h"
6 B% f, g5 x# v. u' fmain() : w7 K5 ~" |9 E% }+ Q
{
% \+ G7 ^9 U; w1 F4 Q) Z; H1 ~, B: Fint i,j,k,x0,y0,x,y,driver,mode; 6 q4 Q( \) f) m4 m/ Y5 b7 {
float a;
& w$ o! i, l" y# I' v' vdriver=CGA;mode=CGAC0;
8 Q$ L' a; ^( S* Yinitgraph(&driver,&mode,""); 0 u! A* L$ `( E
setcolor(3); : \( d% `. A% @
setbkcolor(GREEN);
. G- U% H! k' y* Z* {x0=150;y0=100; $ j) w3 E) j2 P- X$ Y# ?
circle(x0,y0,10);
, l5 u2 n" Z' X) W9 P, s' W. Xcircle(x0,y0,20); ( A9 y9 }, d4 j% X5 i3 y
circle(x0,y0,50); ; s: h* u! Y$ ~8 F
for(i=0;i<16;i++)
- Z, x2 T+ K8 J( z6 f% u( r7 h0 N{
% {; E& c5 O* @. Ba=(2*PAI/16)*i;
4 B# h! N0 @6 N4 rx=ceil(x0+48*cos(a));
2 S* a7 D5 l. u+ A' m" z' Sy=ceil(y0+48*sin(a)*B);
$ M& {7 W! r* o9 g/ Zsetcolor(2); line(x0,y0,x,y);} 1 F8 U+ d& [5 o: E6 }
setcolor(3);circle(x0,y0,60); $ K0 q0 s& G* }5 T0 [! F( z
/* Make 0 time normal size letters */
" g3 z& t6 a# A J0 Isettextstyle(DEFAULT_FONT,HORIZ_DIR,0); : s$ c9 `8 Q1 e; ^% r
outtextxy(10,170,"press a key");
7 V) L+ M, S, M( \5 B5 a. S; ugetch(); 7 o1 q8 h0 S! L3 Z3 O
setfillstyle(HATCH_FILL,YELLOW);
* ~. N$ s4 v# `* S; Jfloodfill(202,100,WHITE);
3 `: F- y5 N. i* l$ f7 T" Sgetch(); 5 j% G3 H* W/ b6 T; L
for(k=0;k<=500;k++)
3 f" I: P& G+ e' |: y( v: Z9 l0 ]{ ) r- Y+ s' p: Q1 S
setcolor(3); . F- f6 J- W1 F) r
for(i=0;i<=16;i++) 8 ^& C9 m" Z8 w2 G. |
{
) [; \4 L2 e2 x( v, I/ V9 Na=(2*PAI/16)*i+(2*PAI/180)*k;
4 q: A% Y: k/ m( K9 {x=ceil(x0+48*cos(a));
# P6 }; q! [. T' @y=ceil(y0+48+sin(a)*B);
5 B4 d* o' Y2 s1 Wsetcolor(2); line(x0,y0,x,y); ! [/ y8 P: N% A# r8 R/ w
} - P1 l% E8 Q" a# I) |0 I- A+ i7 x
for(j=1;j<=50;j++) : B( U% i2 ^7 q% v# ~
{
* c' p9 R3 i5 j) Aa=(2*PAI/16)*i+(2*PAI/180)*k-1;
9 v# i; x2 H, G4 Q( u1 ?& j) [x=ceil(x0+48*cos(a));
2 J3 x. s4 b8 c9 my=ceil(y0+48*sin(a)*B);
5 Q" |$ ~0 j4 d9 q! r9 j. x- Jline(x0,y0,x,y); # d, k1 G$ H. h
}
' s4 b9 N( j9 Y# d" q. ]* x} % |* e' a: T# U; ]- w7 u
restorecrtmode();
# O8 ]% D, J8 J2 C" H. ^8 {0 d}
" s& c6 Q6 o- h/ E==============================================================
8 x" d2 l6 r( w$ y1 ^【程序60】
% ~ t) g6 ^5 X; P) s$ |题目:画图,综合例子。
3 b6 o9 f9 p# G7 O5 h% Y$ s1.程序分析: * ]: P1 T. U% |9 c: @
2.程序源代码: 5 X- R. H% _) c2 d: q1 L: g, Z
#include "graphics.h" & P! `) n! I2 R5 }" V- G# }- E
#define LEFT 0 6 v7 p) S7 M. y
#define TOP 0
4 w2 q1 q0 {& j" \! F/ w o4 o* q5 x#define RIGHT 639 5 _* \6 t! C' f f3 `
#define BOTTOM 479 0 ~9 F5 v# L2 ^3 ]7 d8 |
#define LINES 400 0 L9 W0 z' l$ {( }; t! s' c
#define MAXCOLOR 15 : r4 a! a( l$ u
main() / U7 C( ]9 _: y4 D( {' m
{
' N) {) u4 [+ @2 E) {int driver,mode,error; ( h" J: U- B' I% n: {3 a! j
int x1,y1;
) b( x4 s4 e4 a! w$ o" N- J* u9 pint x2,y2;
* q& y' f* I* Z0 y* k( H0 _int dx1,dy1,dx2,dy2,i=1; 5 {) z) ~" y. u8 d
int count=0;
5 u) ]1 A4 T1 Wint color=0; 0 V# }' G3 a9 @& S0 D1 g
driver=VGA; ( [. A1 L" ^+ b2 D4 @* S! o' L1 `9 I
mode=VGAHI; $ Y9 H6 q4 W2 I: T) j
initgraph(&driver,&mode,"");
$ r; n0 t; U+ q; D' T7 @3 ^! nx1=x2=y1=y2=10;
' o2 ^& G- _) }* Z2 c. q. b* Fdx1=dy1=2; 2 t7 g+ P2 I4 Z9 a# j; k
dx2=dy2=3;
5 T+ j9 X; H2 A1 Dwhile(!kbhit())
* j5 e: h# P! J/ G5 R8 B4 Z* d{ 9 m( t1 f2 s' h/ q
line(x1,y1,x2,y2);
$ y% J, P, T. F; J |% d% ], Lx1+=dx1;y1+=dy1;
2 R9 g I7 O/ Z) qx2+=dx2;y2+dy2;
- j0 M! v1 \& B7 Z/ l7 _6 j$ C2 tif(x1<=LEFT||x1>=RIGHT) 7 t) }; N, a9 |! l+ n. B# k
dx1=-dx1;
% c+ b4 z! I) S- c, t G5 Lif(y1<=TOP||y1>=BOTTOM) * r b' A, D( e! u$ Q) z1 |2 p
dy1=-dy1;
4 l9 Y( Y+ g* j: H' q/ ]if(x2<=LEFT||x2>=RIGHT)
! O% U* o1 r, }9 W+ Bdx2=-dx2; ' v: y' A+ G( X) u- J0 G- }) Q4 F
if(y2<=TOP||y2>=BOTTOM) 7 V9 D& o! j; o. @4 d
dy2=-dy2;
; \4 V0 G/ a2 [if(++count>LINES) # a; y% C8 e- K0 ^# `
{ & V- C }( E0 m% D3 {4 C
setcolor(color);
2 h+ N M, |/ ]$ M/ Ecolor=(color>=MAXCOLOR)?0:++color;
' x A" e( r" C% v* ^: w1 t}
* l! H' m) d8 ~/ S% c' i$ A3 O0 X} $ k0 B3 l1 f$ N6 n; w
closegraph();
2 k5 V5 ]- t* G- [} 【程序61】
. d6 p2 ] C9 _7 p) e9 z$ O& _题目:打印出杨辉三角形(要求打印出10行如下图)
. B, p8 P, F @8 M& u. I0 V1.程序分析: . }1 T$ L) q, u1 L5 J
1
; x" V# z: P( t5 H% k% L$ g1 1
R* N$ B4 X, h S z W1 2 1 & [' V3 ^3 t# U! Y9 @, j
1 3 3 1 0 ^; ^+ K7 U i, x# W; l
1 4 6 4 1
' _5 @" g5 i' \/ o- A# H7 z/ k2 z1 5 10 10 5 1
) p# Q3 F" F8 U* r4 r2.程序源代码:
6 M7 ?! @$ ?0 d8 V$ emain()
- g4 p, p2 ^! n: q, _{int i,j; / G, ^. r; r4 P4 |2 H
int a[10][10]; ) O0 `( J( e2 o4 M r0 Z9 U
printf("\n");
# t z% x, Z6 P/ D8 |9 Y# qfor(i=0;i<10;i++) # Y3 x# x5 A" X. @/ A. X5 J
{a[0]=1;
; X& H8 h+ ]; T- V; M; O- V) La=1;}
3 C* r q* L7 l2 ^for(i=2;i<10;i++) % E- ~& Q/ U' ^/ e! O" G. N
for(j=1;j a[j]=a[i-1][j-1]+a[i-1][j]; : t0 b" v/ U& V/ o+ L0 M
for(i=0;i<10;i++)
3 ~( }2 G- M2 W3 M7 |' v8 a E- R{for(j=0;j<=i;j++)
$ y9 a& ^2 s/ f$ k7 h; d* `printf("]",a[j]);
8 ? e2 K" n( c0 g; Y4 A& zprintf("\n"); 3 K* @( {- W$ c) l( x/ {3 M b
}
" Y3 n+ U4 o' A* k}
6 O1 ?0 Z" K# j) H; O/ C9 V* ~$ j* a==============================================================
0 o( H( u' H8 l9 k4 M- _( E1 z【程序62】 8 V. H; ?# c/ ^ g7 P1 O4 @
题目:学习putpixel画点。 2 J" o# d2 m. j, A W0 c
1.程序分析: 9 |7 g' j$ i4 D2 R
2.程序源代码: ) z2 w0 r4 p, j& H& g+ g6 E
#include "stdio.h"
5 y) G' W& Z& P! @% m#include "graphics.h"
. Q2 M l, E* }+ i0 F' s. smain()
' ^' K- ]: Q" S, [3 D: {/ i5 q# o8 O* \$ E{ 9 V, c, E8 d4 e1 V; ~
int i,j,driver=VGA,mode=VGAHI; ) [! d7 J0 v+ e. p; |
initgraph(&driver,&mode,"");
, x, |) f' @6 v7 o$ n' Bsetbkcolor(YELLOW);
* v' W8 L5 c) h) Bfor(i=50;i<=230;i+=20)
, V8 {7 K* B( k" o8 T+ Xfor(j=50;j<=230;j++)
/ t; |2 C9 v* I& f! e8 N) Nputpixel(i,j,1); 5 m( D$ J0 l+ Z6 W% m1 h6 r$ {
for(j=50;j<=230;j+=20) . p* _0 }) g8 u6 y4 V
for(i=50;i<=230;i++)
9 n6 \4 @: g( e; }+ C9 cputpixel(i,j,1); . X! \: @' f( v7 L
} 5 N3 ~: S. x6 ]
============================================================== - B. n" V4 i% u
【程序63】
0 N# O' d6 g* v% b题目:画椭圆ellipse
6 s0 U3 b) H" g1.程序分析:
: E1 e; p& p# T0 W1 r6 E& Q2.程序源代码:
- X `/ U# I4 t) v- x#include "stdio.h"
7 {6 C. \. z" r% t6 I$ x#include "graphics.h" ' k5 Q$ @( {1 g% k
#include "conio.h" * J0 ~; `+ s1 G, ` G% A' c- R
main()
, B4 b; L8 c; @6 P9 B2 q4 j7 m' C{
. j& m( k0 i: e+ @+ T. A* gint x=360,y=160,driver=VGA,mode=VGAHI; ! c! ]8 L3 F( F& T) C0 Z" s
int num=20,i;
, X5 ^4 q$ _. j B# zint top,bottom; * o. B. L4 f$ q% t5 y8 I+ t5 P
initgraph(&driver,&mode,"");
; J6 x. N" @( {5 ?: R; w3 btop=y-30; + i0 t6 K! q: Y
bottom=y-30; " [ C& B! P6 t" P2 U$ G2 i0 i
for(i=0;i{
9 G; d x& U" o$ W, C9 K+ `ellipse(250,250,0,360,top,bottom); , C$ v! y1 p( F2 Q+ ` c
top-=5; 2 y' N! m8 {* [. `! T' m
bottom+=5;
( G/ A I+ z' @: {9 q" `}
- G8 ?3 @# @ D7 a& rgetch();
! s! C& \* u9 I) H}
: F* A2 r+ \4 q3 f: M. O+ e============================================================== & @2 C. J/ ]9 z9 z# S6 x
【程序64】 2 l, n/ w: @+ A
题目:利用ellipse and rectangle 画图。
! \1 j0 l W# @: x1 e [1.程序分析: 1 Z! z2 G1 e# Q5 O0 j
2.程序源代码:
, k% U1 q. g: g1 Y0 `#include "stdio.h"
5 g" k: |5 a4 u, G3 Y#include "graphics.h" / g4 I+ F1 j$ c6 A: y
#include "conio.h" 9 K3 D* ]5 `+ X& L& T% t
main() ' d6 \% d; g3 }/ ^) |* J, G6 }( i
{
$ O+ r5 o$ m+ M- r+ Aint driver=VGA,mode=VGAHI; ; G6 c+ A( M9 ^; b
int i,num=15,top=50; 1 p0 @1 m* x2 i D
int left=20,right=50; 9 `4 |0 k5 q1 ]5 s8 b: d3 X
initgraph(&driver,&mode,"");
# o7 y1 V8 S5 f D( ofor(i=0;i{ & `6 D) S, ?: y( _
ellipse(250,250,0,360,right,left); . ~2 @ S4 m; D( y" Y" x0 D
ellipse(250,250,0,360,20,top);
8 E' B6 J6 Z }. ^1 [/ {rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
( g; N8 y: `' {& G; _1 {7 `! Eright+=5;
/ |& M& J1 g- G5 g; rleft+=5;
. o _5 u! b* i. j$ D a' q0 ltop+=10;
, n5 E& l$ Q" F7 L. ]5 a7 {& u}
* `9 ?3 p" P' t7 ?getch(); 8 W( A' H, f7 B! ]: A# T+ I3 |
}
. f, d+ ]$ ^1 n==============================================================
: S3 C- T: R$ x% ?- a【程序65】 , A0 P# B, J2 R+ B0 K
题目:一个最优美的图案。 ' T# K+ Q. p2 B) T, R
1.程序分析: ' j$ V& Z* u* F2 u5 H$ ]0 V
2.程序源代码: & z+ \2 J# N6 c, Z6 X, Q
#include "graphics.h"
: ]1 Z, N' m D4 m9 g! l i#include "math.h"
5 k: A y* E: {) s' Z#include "dos.h" + A2 X) A6 ^+ f# _) _4 t
#include "conio.h" " D1 Z) k0 J; @# R; { A
#include "stdlib.h" 2 D0 e( W7 i* L; y3 D& S+ Y
#include "stdio.h" 3 H6 A6 S n% M4 x; c
#include "stdarg.h" ! k% g) f0 ]2 }
#define MAXPTS 15
# U9 N* z, ^- f9 \: [' ]* p! S#define PI 3.1415926
1 }6 I: _. u2 ]% Z* `1 {( ?9 sstruct PTS {
, ?( T& z p& @8 n1 jint x,y; : f, P H4 o; w/ H, S
}; + O. y: @# O$ f- n n( n
double AspectRatio=0.85; 1 `; W. A) Z, q% e& ?3 ?- s; w
void LineToDemo(void) - W5 ^! }5 {) H7 F& R' d: o
{ 8 O) n0 `. |4 ~8 R
struct viewporttype vp; 5 f6 a# d$ z- s% e5 N2 S, ~! e5 a1 h
struct PTS points[MAXPTS]; / ]: [7 d. H; I2 S, f- i1 d
int i, j, h, w, xcenter, ycenter; . _* c4 X% ~. S
int radius, angle, step; ; i' K( A# Q, l6 {( ^4 r: d. U- a5 h
double rads; " x, G& }3 j* g# m2 T$ u+ a# P! u! q9 M
printf(" MoveTo / LineTo Demonstration" ); 8 z9 D( A# P" G
getviewsettings( &vp );
: N1 f1 M* F3 nh = vp.bottom - vp.top; - f% Z- C6 K/ O0 d, B) N
w = vp.right - vp.left; 9 q* f$ C! ]3 F* a
xcenter = w / 2; /* Determine the center of circle */ $ ]- k& v3 d$ u0 z4 y5 ?
ycenter = h / 2;
" A1 }# C* z# P3 [5 yradius = (h - 30) / (AspectRatio * 2);
2 a: h9 c! i; C; u @+ @6 z- {step = 360 / MAXPTS; /* Determine # of increments */ 6 a4 X+ e" D5 `+ p6 y1 P0 N$ m
angle = 0; /* Begin at zero degrees */ & s9 x- Y/ m7 @7 C) o3 q4 A t
for( i=0 ; irads = (double)angle * PI / 180.0; /* Convert angle to radians */ ) l$ ~8 B' p; D+ t
points.x = xcenter + (int)( cos(rads) * radius ); * S N1 m. M# q! G4 y) N
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
. t1 n, `& A. v% Mangle += step; /* Move to next increment */
; N7 [' @( e6 I- K+ t4 \! m) _} ' x* r; @: A: V. F' H
circle( xcenter, ycenter, radius ); /* Draw bounding circle */ $ N. L L8 z- o/ ~# `4 J
for( i=0 ; ifor( j=i ; jmoveto(points.x, points.y); /* Move to beginning of cord */ ! x. D0 U! u: R& D
lineto(points[j].x, points[j].y); /* Draw the cord */ # T3 t# {0 Q: k* B: ?; r- Z* |
} } }
8 E( q, w3 M/ k, k* x& vmain() ! f) M E9 d/ `! v2 M( S8 s7 S0 z
{int driver,mode; $ s' Z' x$ q. m: P
driver=CGA;mode=CGAC0;
3 d- L. l3 q" r, r: ~initgraph(&driver,&mode,"");
2 j2 X. j1 O5 rsetcolor(3);
5 T5 U- C, p. s( \ u$ wsetbkcolor(GREEN); : i$ m8 p. G) T) M
LineToDemo();} & j' I3 A8 L$ \; Z- a& L ~% d
==============================================================
/ R4 E+ g* T1 l; F: i+ C# X( W! S【程序66】 0 ]8 ^# ]% O, l9 R5 ?+ y7 P ]
题目:输入3个数a,b,c,按大小顺序输出。
' Z7 y" K7 @5 d8 ^$ Z4 m1.程序分析:利用指针方法。
1 w/ w. t: r2 h2 z/ D! G5 ~! ^; ~+ ?2.程序源代码:
; K4 H! Y. [8 M8 b/*pointer*/ 9 @ A) O8 @8 g& ~7 @
main()
6 H$ B6 P/ m& k8 Z. `7 r{ 4 B( v' N4 G+ C' [# J0 Q2 c
int n1,n2,n3; 8 B% R9 A, l8 u, @- D
int *pointer1,*pointer2,*pointer3; : K: z- `/ F" b- h- T/ Z3 {/ C
printf("please input 3 number:n1,n2,n3:");
1 h9 x+ P6 @6 ~ S$ z' sscanf("%d,%d,%d",&n1,&n2,&n3); * r$ v+ P* N3 z" W" A. b
pointer1=&n1; " Y/ S# }* C$ D$ Z# X M% ~, L4 G/ m
pointer2=&n2;
2 I" `2 I$ h3 W# t# fpointer3=&n3; : x- ~/ ?9 B* i( ^4 E! N0 ^7 P
if(n1>n2) swap(pointer1,pointer2);
; i& ?% p" m6 Q+ Hif(n1>n3) swap(pointer1,pointer3); - T$ @. z8 T+ \/ K3 n
if(n2>n3) swap(pointer2,pointer3);
/ r9 V; d y9 \' Q2 d# e) Bprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3); * j4 I! H5 O1 W. I, f% }9 U
}
& a0 W" {1 Y# t6 ]; V' E/ s) cswap(p1,p2)
. x0 Y. ?0 E y' {1 _4 ~( o0 |int *p1,*p2;
" \! l1 B3 A* e- s{int p;
0 L" x9 ]; l, d1 b/ B* ~# kp=*p1;*p1=*p2;*p2=p;
3 T( ^$ G( [, Y}
+ P K3 Y# V) N! t$ ]6 c8 H5 A==============================================================
& A* @1 p' s9 ]/ L3 v* X【程序67】 # d% h4 E9 |! u8 k: {8 Z
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
: `& O4 U9 M/ J" i4 B7 `$ t {; G1.程序分析:谭浩强的书中答案有问题。
6 e7 f/ m; G4 O! @2.程序源代码: % l6 A7 P j& b: L
main() - y$ e; _( C" E0 O
{
# Y/ J+ y* U& x: F8 L+ kint number[10]; 4 r0 g: g% Y& U( V5 x# {: p
input(number);
" O; U, `- I1 X1 C; |, Lmax_min(number);
6 f& r- y; G- H7 soutput(number);
' Z) z/ u; u' ^4 g8 W1 A! p} & a4 y" U Z2 M$ o: ]* J5 j; H% g
input(number)
; l0 X6 M) B: ?- w0 Lint number[10];
$ f9 H4 B) s2 D- `" ~# W$ Y- R{int i; * `6 Q- G6 m* N0 B2 d
for(i=0;i<9;i++) " l# D3 ^( B) Z: Y5 G
scanf("%d,",&number);
" a6 O8 n4 k% W. Y3 F( Fscanf("%d",&number[9]);
6 ^& V# {1 S: w& u/ o- \5 d}
/ K( j) W, x1 c$ W3 Kmax_min(array)
! ?% C, T, v8 F+ X uint array[10]; 4 ~: w* ~" }, m+ @. b' E+ C5 k1 L# v
{int *max,*min,k,l;
* `+ }9 r: T2 { m" ?int *p,*arr_end;
8 q$ J7 l. n. B% @9 larr_end=array+10; 9 _. N+ e! K. R2 u/ F! v
max=min=array; 1 J3 r# D) P& I, l' ]1 i2 W0 x
for(p=array+1;p if(*p>*max) max=p; & R* K( @1 M9 b; b: W
else if(*p<*min) min=p;
& L+ j; t7 M/ _' P; X, Z9 kk=*max; 7 e, V4 ]5 A; ]* G, v, x
l=*min; 6 r4 J. y/ p1 H! W8 h1 J$ L
*p=array[0];array[0]=l;l=*p; 5 x4 x) H- D/ t: U& G
*p=array[9];array[9]=k;k=*p;
& O% C4 I6 ?; h* p1 C" y4 R D* X, Greturn;
4 ^! D$ y* \) {# { d} ' s9 F, n/ \" @4 {0 F
output(array) 1 g9 {) E1 g7 N+ p* `2 }
int array[10];
* P; Z# d: J% I7 x: k2 h{ int *p; 9 C7 U5 i$ W- t0 U9 G! _, P* I6 q
for(p=array;p printf("%d,",*p); 8 o( C8 s; Y2 { {5 v/ G
printf("%d\n",array[9]);
. z9 B# B( `5 h/ z} ! T8 f& E2 k! \4 M9 R r
============================================================== 7 g! f& O/ O& y6 q. Z% w% F
4 [# |0 [: E3 v" ?
|