【程序59】
$ ]/ D' E' t2 Z1 z( t- `" ]题目:画图,综合例子。+ P$ H9 I1 G7 m$ d
1.程序分析: W9 h* S! c1 {; V9 q: e- f
2.程序源代码:5 g6 @$ l3 T/ A6 D. v0 l
# define PAI 3.14159260 D! y4 Y0 Y; y: N
# define B 0.809
9 h" C4 L, d/ n5 B ]0 Z! D0 T# include "graphics.h"
) K# t7 _" ?5 `) F+ I; V#include "math.h"6 X& `0 {' R7 F7 Y4 e
main()
$ B+ O% [& K c7 A{" D! w1 ~5 {. h
int i,j,k,x0,y0,x,y,driver,mode;
8 m/ H# D: @- k, n6 E- T( X) Pfloat a;' ^2 x& v$ W2 K- l, v# }
driver=CGA;mode=CGAC0;# _: s ?% z. j9 x
initgraph(&driver,&mode,"");6 @2 i/ p- g2 N! }
setcolor(3);$ a! U$ l+ K( y: a! e1 n' R' H8 m
setbkcolor(GREEN);2 @- ` \% }) f% K, N5 c j
x0=150;y0=100;- Q. a" ^" r& `) |- W% w
circle(x0,y0,10);8 N; q( k* R7 t( d9 L8 X- ]
circle(x0,y0,20);
% H" i$ {$ M. Scircle(x0,y0,50);
! y8 V$ g: O, Afor(i=0;i<16;i++)
& ]/ t3 X' D3 D1 C{* D l/ F1 q5 U7 k) \! j! n* ?( ^
a=(2*PAI/16)*i;
. @0 P% ~& _% q x=ceil(x0+48*cos(a));8 r. y; E* P5 e( E
y=ceil(y0+48*sin(a)*B);
8 \$ r) W6 D/ r" ]. _5 ?$ l# u' g* F! E0 ~ setcolor(2); line(x0,y0,x,y);}
! J7 Q. }) ~5 f' C, _' P7 Msetcolor(3);circle(x0,y0,60);6 _ |5 ?8 k, Y. y' f: k
/* Make 0 time normal size letters */
) w1 Y/ ^0 R- k3 ?& d$ {settextstyle(DEFAULT_FONT,HORIZ_DIR,0);' n1 |5 {! g7 {( R3 z
outtextxy(10,170,"press a key");3 F3 C6 d' C1 Q
getch();
+ w+ o* r9 O3 q2 n$ \setfillstyle(HATCH_FILL,YELLOW);
6 K6 h0 v+ v8 A) ]; f$ qfloodfill(202,100,WHITE);7 t- H1 K. ?/ r8 g; b/ h# I; w
getch();
+ `" A% ?; J8 H# y. f9 n& ?for(k=0;k<=500;k++)
& i& D2 n' p" o& \, O3 K{
8 y: a7 n! v# T$ J$ Y9 R( ~ setcolor(3);3 i% l& P9 x+ @% p3 D6 r/ |
for(i=0;i<=16;i++)
4 `# A$ c- @6 d C7 V. } {2 o" \" V+ g2 t/ A- k( [
a=(2*PAI/16)*i+(2*PAI/180)*k;
5 B# t8 |$ {2 [% z6 d' j- ]5 Z x=ceil(x0+48*cos(a));
+ h$ v; p0 K* z. ]' N y=ceil(y0+48+sin(a)*B);
$ |( w" S* f6 p$ n# H setcolor(2); line(x0,y0,x,y);
g- _+ p6 R; E. k0 o. W }. J0 r9 x X; ]# O8 h
for(j=1;j<=50;j++)
* i/ z( ?( B/ i$ c+ h+ R {
6 B3 w, ]/ N3 ~! m% ^7 a a=(2*PAI/16)*i+(2*PAI/180)*k-1;
( `. l @2 c' d/ h3 N x=ceil(x0+48*cos(a));$ L+ K( ^1 y7 G% ~+ G }
y=ceil(y0+48*sin(a)*B);3 \( X, l" p6 C* f* a0 k/ m
line(x0,y0,x,y);
) l/ _0 Y; F6 Z& \! B }
: r3 T8 x9 }( L: p1 o}$ f. i: a) z5 T; d+ e
restorecrtmode();
" G# m! p z5 ~ |}
【程序60】
4 r: ^8 L9 t: j2 {题目:画图,综合例子。
$ k3 p) c0 Z2 }, H% ] g' ?4 L1.程序分析:
) \+ x; A H) ?# w* V& M2.程序源代码:, d9 r! t, ?" W8 L0 s' H9 a
#include "graphics.h"" V4 V$ [9 W2 A1 ] c8 r
#define LEFT 0+ k9 b9 Y, l/ n" S- J4 K
#define TOP 0* a: B3 W6 l3 j9 ~) p
#define RIGHT 639: ~, k+ E$ o+ N% ^, r: x
#define BOTTOM 479 ?9 N1 X! ~) g; \* ?
#define LINES 400( Q& H7 S" S6 L$ m, y- T$ m
#define MAXCOLOR 157 t* d$ y# J( n G9 F8 e, i
main()6 y6 h1 z( O; O/ C. ^2 P
{
' t; Y+ D! ^' s# V T. Tint driver,mode,error;, L- ?. J2 e$ |$ ]9 y- h% g9 p
int x1,y1;+ |5 u# i( }- p! K, W3 C9 K
int x2,y2;9 e. B6 k" e1 Y/ e9 n
int dx1,dy1,dx2,dy2,i=1;
+ J4 q" W, K3 k: cint count=0;& w0 o# K: O0 L; B8 V2 b5 k
int color=0;0 c% L( q; X$ t) }0 V3 \
driver=VGA;9 @8 `- Z1 C) ]. F4 C2 W, c+ t
mode=VGAHI;
9 r1 F/ O) b1 M6 N; A8 ~. A* w1 qinitgraph(&driver,&mode,"");- z+ X3 F8 l8 _$ t( l, t
x1=x2=y1=y2=10;( [2 K: \9 J5 r* u+ s
dx1=dy1=2;$ X! x. `1 o: F( E5 s) I
dx2=dy2=3;
' G) j/ b/ }7 r, }/ W8 Xwhile(!kbhit())
& D ]6 E6 i% s1 o{1 M) S. p' w4 y3 ?# D
line(x1,y1,x2,y2);
0 j* M# d6 ~& p; C# Z x1+=dx1;y1+=dy1;" o% n4 A$ U. v8 `5 s
x2+=dx2;y2+dy2;
r8 W+ f* Q! _" W6 [ if(x1<=LEFT||x1>=RIGHT)
/ K" G! m% g* Y: I+ d6 G dx1=-dx1;
( c" [9 A. k) D+ t3 o3 w. }1 c" P if(y1<=TOP||y1>=BOTTOM)3 q0 e7 }9 k& j8 z8 m* h5 [+ i; B" N
dy1=-dy1;* N9 v* n2 W. z( K
if(x2<=LEFT||x2>=RIGHT)
3 [7 x2 @$ v& J0 y) s/ Q9 O dx2=-dx2;
- [/ H) H, N" Y& e2 W if(y2<=TOP||y2>=BOTTOM)
9 i4 f+ D& h8 @) j h dy2=-dy2;
+ ~! p; k3 N1 c) W5 U u3 t/ I if(++count>LINES)* w+ ?) {% h( J* ]) ?( O
{; F0 ?" I1 @1 Y" \% e& E
setcolor(color);
( @( K0 i& t& n color=(color>=MAXCOLOR)?0:++color;
! c" z8 {+ H+ Y$ ] }8 E) Z. P: l# W% t
}
q0 P' `$ N/ E) ~closegraph();
% N6 N' F6 z3 @0 D% `}
! l j. R* s8 g; @7 Z
【程序61】
: v1 B7 p: y7 d' {% r$ |4 \8 U题目:打印出杨辉三角形(要求打印出10行如下图) N6 ~, t) P0 l( F# ?5 h
1.程序分析:
3 k0 E- k+ C, A, b2 W 1 O! R' C8 V. R- h1 I5 C4 D+ e
1 1
* d$ U3 v# L# N$ _2 ?# V 1 2 19 o6 J- s/ Z! }- T
1 3 3 1
' t3 T& Q% t% T4 `9 w% N4 g2 l 1 4 6 4 1
- S# E: `6 L0 v9 }( K4 h- | 1 5 10 10 5 1
' p% |) i+ @. Y! n. N; O8 X2.程序源代码:7 v$ Y0 c! T( Y# y
main()/ v. D4 d r6 ~1 ~0 G" F' K1 Y k. H
{int i,j;7 ^0 `1 s0 B: |3 q; Q
int a[10][10];2 P* m1 I0 i4 h8 F) _/ B6 ]0 E8 N) x
printf("\n");
! H) v4 L; B Ofor(i=0;i<10;i++)2 I$ n6 { j% f. ^+ a: A& q! a* e
{a[0]=1;
* m1 j$ `9 {& ^9 R) P7 _# l a=1;}
$ ]! L% X, g# H& k& Zfor(i=2;i<10;i++) F' v& p: x6 P C; D: p
for(j=1;j<i;j++)
& K& D# ?7 j, b: I9 @+ y a[j]=a[i-1][j-1]+a[i-1][j];
0 n d2 z' v5 \" cfor(i=0;i<10;i++)
( v5 b' e% @5 m I& T! D; W {for(j=0;j<=i;j++)
! a2 N' B5 ?2 g2 \! ?0 b printf("%5d",a[j]);
- z) |% B; t& D! h4 z" p printf("\n");; e$ O/ e6 ?& s$ B$ N5 b" F
}, n8 M$ W5 M( @/ O. _/ S' d! V9 a
}
\, t6 O: X* N, L H【程序62】
5 X/ F- f( j2 i- B. C x9 m题目:学习putpixel画点。
: d, u% [0 u% v- x( J9 e8 U1.程序分析: ! W6 s* F+ g2 u! M& }& R
2.程序源代码: j- Y- v9 D: b1 n; ~' Q `
#include "stdio.h": ?) y$ I( r. e% Q: I$ s# B% Y) i7 {
#include "graphics.h"+ }, _* J% x9 v" h9 r
main()
9 {5 I/ S/ o' L{" o* I# K z s& b+ ?% U
int i,j,driver=VGA,mode=VGAHI;
: _2 w% Q" g8 e/ x9 {8 linitgraph(&driver,&mode,"");
! `) M) g: l* @setbkcolor(YELLOW);
2 `) L9 n$ T1 ~9 n8 ^7 b% jfor(i=50;i<=230;i+=20)2 N% w* h l: x. [) N5 N" p
for(j=50;j<=230;j++)
% ^ \, V0 |% ?# Q) h9 C' P putpixel(i,j,1);
5 ~# ?6 k$ u' _3 jfor(j=50;j<=230;j+=20)
& y8 x6 \1 h' x, S: T. B! F for(i=50;i<=230;i++)3 d7 o8 Y" l$ R9 S6 e4 Q" u/ G( b3 V- z
putpixel(i,j,1);: s' `1 r9 B! M t# }5 e9 d. k! P* c
}
; G+ R& F$ _" ~- `- Y+ b( U
【程序63】
& l$ g6 K' l/ r9 {0 I题目:画椭圆ellipse
3 |" V; q; R2 d8 ?9 O1.程序分析:
; C( @8 ]1 [+ v7 t) n# q2.程序源代码:
0 p! e) k2 a) F8 ~) C#include "stdio.h") T3 x: s" _9 ?3 l
#include "graphics.h"# N- Z+ P6 {) }# N- l5 [
#include "conio.h"
4 k2 J+ t( q4 j' {+ smain()8 K Z7 k B+ V0 ^+ [
{
3 d$ t- C! E* H2 v: Jint x=360,y=160,driver=VGA,mode=VGAHI;
0 i9 L2 }# G$ wint num=20,i;+ O" e, l& V& Z9 u" I: i1 C6 J: k
int top,bottom;: |+ b6 T5 I* y. ?* K, p% O
initgraph(&driver,&mode,"");
) G1 e: Y+ h0 E2 vtop=y-30;
) w; a" N3 d& K0 \1 s3 ^8 ?- W8 xbottom=y-30;
7 [$ \5 O/ r9 lfor(i=0;i<num;i++)
7 j. n* k7 v4 Z. X- \5 l{
2 x2 x) A0 S, [& E0 h6 H$ Tellipse(250,250,0,360,top,bottom);
0 w$ f0 s o4 w. n, O$ Ftop-=5;3 m& L. `* K6 h c
bottom+=5;# t4 ~" @8 e& ~& Y% n
}
3 q1 r" R4 U8 T2 {7 o) Bgetch();: a3 D: d9 B* K1 |4 L/ p. o; d
}
【程序64】0 F2 d, _/ }- y" ?# p- U4 [7 k2 t( p7 O
题目:利用ellipse and rectangle 画图。
: R4 P/ {; _, q3 i8 [3 [: V) J1.程序分析:( f. W" L& h! N6 g5 t9 C6 Y4 L
2.程序源代码:
* B% ~ b: t- B4 x1 d#include "stdio.h"
4 Q* k) F! V+ u/ u. Y. U6 |#include "graphics.h"
( U, ~$ [8 _; V7 C% H, W1 P#include "conio.h"
( d- G" o0 b; K2 vmain()1 }( M7 R- P- F; A3 @, Z
{
- c7 f. g9 }5 N+ H' R3 S& dint driver=VGA,mode=VGAHI;& [' k" @/ ~! U7 v
int i,num=15,top=50;
+ b3 g7 b1 M1 \5 ], J9 D9 Qint left=20,right=50;0 }; I& N( f' H: F' g3 _0 |, h- b6 M6 r$ @
initgraph(&driver,&mode,"");
' V7 `! F9 ?6 n. p4 {, ~! y: {$ Zfor(i=0;i<num;i++), N5 f! f0 Q) C& R" S8 X
{
+ f, d# _% t6 v/ }! {+ T: k, ^0 l" z( v- Qellipse(250,250,0,360,right,left);) m. T% N& i4 ^* d' a
ellipse(250,250,0,360,20,top);
6 F/ U8 W. T: Rrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));1 | }+ Q3 D8 y( U
right+=5;5 B' g/ _4 h2 X/ l4 Q N0 F
left+=5;
3 j( }5 m0 ^6 Q( otop+=10;
, l' U) U$ `) p9 c2 N}2 \, `% [7 ], E6 j+ d% u" R
getch();; O$ P* T6 `9 q+ n; U
}
【程序65】4 J/ a9 ?/ j, c4 L! _/ { T1 c
题目:一个最优美的图案。
/ r' A$ @3 E- N4 X/ t, V; b1.程序分析:" Q, A! N1 q9 H" V5 a# w; `) R. C
2.程序源代码:, n9 [/ O6 {* l2 ?: r
#include "graphics.h"
: I9 a& H1 @; O# i& w" O' v#include "math.h"# s" P N+ i: N/ G! D8 g
#include "dos.h", W9 M" y/ D/ N( e3 V& Q. v1 S
#include "conio.h" t! f! x9 e9 m3 N% f& a
#include "stdlib.h") ]$ Q1 I a: R' l$ w. ?
#include "stdio.h") l* g5 d9 q& G4 m: G: d* W0 K
#include "stdarg.h"
/ q1 }. |8 [+ D3 f+ X#define MAXPTS 15
1 |. l$ B0 r' P" V#define PI 3.14159265 d: |" L+ {3 J8 ~7 `( O
struct PTS {
( i$ h6 C3 x' _9 d: S+ p2 bint x,y;
9 p2 m$ `! r; |* F1 H% d};) O. A" \$ f$ i3 M3 d
double AspectRatio=0.85;8 t9 G8 |* J0 r9 S5 j1 z* N
void LineToDemo(void)' s0 C' @: L; P: y; }
{
( I1 ^) ]9 u2 n2 M+ n4 ~4 W$ bstruct viewporttype vp;- y) C4 k1 E! `5 r
struct PTS points[MAXPTS];; E) w$ O5 r% }& }' Q3 O
int i, j, h, w, xcenter, ycenter;
. H h0 H1 |/ Q; f! ~* [9 w$ sint radius, angle, step;
. ]- [6 ~% N5 g5 ~double rads;- x" q9 `. D3 ?4 \5 c5 X$ H
printf(" MoveTo / LineTo Demonstration" );$ U& m6 o m% s
getviewsettings( &vp );9 ~% M4 Y: P- T# W) `) ?
h = vp.bottom - vp.top;. Q+ g6 d( c. @) g8 h. p
w = vp.right - vp.left;
) X. t2 P9 P2 c) Lxcenter = w / 2; /* Determine the center of circle */6 U8 ^- ]$ T, E! r( t
ycenter = h / 2;* p: [1 G7 E' I
radius = (h - 30) / (AspectRatio * 2);
! [+ v" K6 H' Z: ?step = 360 / MAXPTS; /* Determine # of increments */
! B3 d# P Q6 Rangle = 0; /* Begin at zero degrees */
0 {6 g( n- R+ m1 b8 Zfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */, R2 E, F; G) w$ l2 M
rads = (double)angle * PI / 180.0; /* Convert angle to radians */5 |( r2 S; [, D1 T) b1 G
points.x = xcenter + (int)( cos(rads) * radius );
& w4 p, c1 Q9 W- j. y) p+ h% J3 rpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );: w' u0 I+ Q1 ?) b4 u2 N& [$ k
angle += step; /* Move to next increment */
% V+ z# r/ k( j3 E}
) p( b% {) M+ N& K# u3 S' Zcircle( xcenter, ycenter, radius ); /* Draw bounding circle */1 r+ A& L& Q& F, s$ O
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */; }. e# Z. k# @9 z |4 {
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */% m4 h/ J* z0 A
moveto(points.x, points.y); /* Move to beginning of cord */
6 e! V. C/ v! J3 wlineto(points[j].x, points[j].y); /* Draw the cord */9 G- e9 J! K& e; `
} } }9 B3 v t S* Z( ~1 x9 m5 l, U0 T. t
main()$ w) H% P6 I5 L( e2 F1 i# D! G
{int driver,mode;7 ~ R' P) {0 U3 y' c
driver=CGA;mode=CGAC0;
1 b7 B- u. G6 x* _: Yinitgraph(&driver,&mode,"");
: X( E/ c) t# R1 O& Y% G. S; A1 Jsetcolor(3);
3 w+ N0 o& X8 `6 F, ^2 e9 X! Csetbkcolor(GREEN);: S: {$ ?# G+ V/ x9 U
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
$ v, h7 P N8 I
【程序66】; E: W F1 q* P( B& }
题目:输入3个数a,b,c,按大小顺序输出。
9 ?: K/ f* N3 [! \1.程序分析:利用指针方法。- B5 S( k& w8 ~& c/ @+ v$ r
2.程序源代码:
5 b& S$ h( {/ L! m3 z4 j- O9 [/*pointer*/
9 m l c8 t6 W5 @# gmain()
3 j c8 J* e6 g( S5 g) p4 z{
; Z/ p8 [& V' V/ [9 kint n1,n2,n3;
. q8 \5 Z+ m, q8 V5 a" @int *pointer1,*pointer2,*pointer3;
! w* j, _1 t8 P i7 S1 gprintf("please input 3 number:n1,n2,n3:");
+ W0 b" \9 B% u4 o; T3 wscanf("%d,%d,%d",&n1,&n2,&n3);
* R$ t. o5 t: o* Tpointer1=&n1;, j5 P) ]" I' }* S1 Q$ P& F4 }
pointer2=&n2;
9 J5 A( Z' D; E( K" kpointer3=&n3;0 I* W' p I) L/ }/ d$ D- d
if(n1>n2) swap(pointer1,pointer2); k- S8 A* C2 r4 d, s
if(n1>n3) swap(pointer1,pointer3);4 o: ?( [1 d+ }9 |8 ?; C
if(n2>n3) swap(pointer2,pointer3);
' ?. R& V( D: C! a" mprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);9 G! r7 Y) p& j
}
1 |7 T3 |3 a7 F- J8 Z* w' w1 Q" xswap(p1,p2)
5 A! }1 D. K9 G, b5 ~) B" kint *p1,*p2;) o. T2 N7 V% b
{int p;
{6 V( C/ R( t; U7 Z' Op=*p1;*p1=*p2;*p2=p;5 [ T c- ]2 _1 l: s. X$ K# U
}
- Z8 O! r4 w' l6 w# M