【程序59】/ [; l+ M' J3 ^$ \9 w
题目:画图,综合例子。* m/ n% Z% o- L) J8 x
1.程序分析:
: y Y! d* h* i g1 `2 y2.程序源代码:8 G- v2 u3 u: P J
# define PAI 3.1415926& u% Q/ ?" b7 M: L
# define B 0.809
( ~- h0 i) z& I* R* m# include "graphics.h"9 t2 f7 B* ~* P8 |9 A
#include "math.h"
: n! j4 D0 g+ G) _main()9 r9 K- S8 K) x" a2 m& F6 |
{3 K' t/ A0 V5 U, a6 T$ Y
int i,j,k,x0,y0,x,y,driver,mode;% `" O) o4 K% L6 F# O
float a; N, `0 Q' D3 M9 n4 E
driver=CGA;mode=CGAC0;, [8 L0 [3 Y7 h) o
initgraph(&driver,&mode,"");/ K& c( C7 I% N8 a/ A
setcolor(3);# B/ P( l3 E# N! ^9 X& w: }/ r
setbkcolor(GREEN);9 e: O2 @9 s7 W' l% u
x0=150;y0=100;
3 q+ z, r( C9 o# j3 ]8 F2 A3 ]circle(x0,y0,10);
4 Q2 W* M, k8 m4 f- a- Jcircle(x0,y0,20);
( H; S u8 |6 H0 x4 d. I: Rcircle(x0,y0,50);3 V- w; c7 i. N- @2 A
for(i=0;i<16;i++)2 o( \' ~0 c) t/ e4 c1 a4 y2 D- g0 U
{
/ _# p+ ^' d/ b1 a1 ^- @6 V" E a=(2*PAI/16)*i;" g1 i0 R3 [. E& O2 q( H5 |
x=ceil(x0+48*cos(a));
6 l7 S: e' I( h# @ y=ceil(y0+48*sin(a)*B);0 h; g; }( t" [7 T
setcolor(2); line(x0,y0,x,y);}
( J1 A" U( \/ E% u+ asetcolor(3);circle(x0,y0,60);# Q3 n, I+ a! C; P: b5 C
/* Make 0 time normal size letters */) f$ Q7 ~+ f% b3 F) K3 L
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);3 Q4 M7 W4 ?1 S: B
outtextxy(10,170,"press a key");
+ P9 D$ b2 c+ F7 z6 bgetch();4 }2 d/ B O& \3 G- }. q! o
setfillstyle(HATCH_FILL,YELLOW);& d( l8 g S2 b! C2 J& t7 p# @: }
floodfill(202,100,WHITE);4 ]7 N# l3 G4 U1 H. e: y3 P0 z
getch();
% ^% m/ V! o6 i0 N3 ifor(k=0;k<=500;k++)
8 w7 r+ Y' N( }. @{: B$ z* p! b& r5 `, \$ n" B1 y
setcolor(3);
. v E1 C1 I" b7 b2 N9 ?% T- X for(i=0;i<=16;i++)
' @5 \6 d" v. I6 Z( `: f- Z0 o {
8 Y' Y) p' Z9 L a=(2*PAI/16)*i+(2*PAI/180)*k;
& q7 H M5 W- L, Y" V x=ceil(x0+48*cos(a));1 K! u7 C* e, t4 T+ a$ m
y=ceil(y0+48+sin(a)*B);
- `1 ?- t' E2 P2 Y+ Q setcolor(2); line(x0,y0,x,y);
5 b2 m P' U2 E: ] t7 t }% [/ h% J! u" _
for(j=1;j<=50;j++)
- h9 P" y' k, D% A2 [* z. {. |9 T {
0 e/ X8 K" a: E# w a=(2*PAI/16)*i+(2*PAI/180)*k-1;
1 |4 c9 l# N* T9 T9 ~# E x=ceil(x0+48*cos(a));5 M+ ^7 C, M x6 M( ^
y=ceil(y0+48*sin(a)*B);
3 Q: q* I/ E5 K' n line(x0,y0,x,y);2 [. W' L1 }6 ?
}
* s+ M9 Q( B5 u8 G( c8 ?}. d- W: J- i; L4 T7 R7 x
restorecrtmode();
' G0 N) k+ c" s2 ]}
【程序60】 `0 ^" {8 _5 q* H* r
题目:画图,综合例子。 , M& ?2 s7 c3 W2 w. _& _
1.程序分析:8 J, R1 M' K; V* Y0 C/ W' t
2.程序源代码:
$ x) k, f0 ]6 ]* J/ T8 g#include "graphics.h"0 ^; T* u0 m! d! m+ o
#define LEFT 0" ^5 |5 }8 Y8 Q S
#define TOP 0
9 `( L( ]+ U! Z3 h2 j. z8 ?#define RIGHT 639" V0 e4 X7 C6 e5 u" W9 n/ E6 i
#define BOTTOM 479
0 e) }) y" k# h( u! n- j: d, S#define LINES 400
' v" U, ]" C1 _+ g) i' F- s#define MAXCOLOR 15
+ G4 ^0 l' s( B, [main()7 U4 O P+ B, Z3 Z# X! S
{$ z0 o) u, c7 p
int driver,mode,error;
( {. J: d [8 \9 h7 ?+ Jint x1,y1;
% d# u/ I; r! h( f \int x2,y2;6 Z, h) E" \9 W3 s4 I3 p
int dx1,dy1,dx2,dy2,i=1;
1 L% c. N' Y6 T; B, f, y J8 y" Jint count=0;8 i' \3 Y c6 _* \
int color=0;, Q/ l) ^9 [9 Y5 U4 t8 V4 E0 E
driver=VGA;
) ~1 K+ `/ a$ }. mmode=VGAHI;( U# ?2 t; k$ `5 Q8 q- E8 A
initgraph(&driver,&mode,"");+ o7 S' k. F6 U% h4 Y/ M
x1=x2=y1=y2=10;
5 D$ ]: ^7 A- W3 C. G! t& N- {dx1=dy1=2;4 r, c, Y o. S7 n; ?3 \0 ]. a1 N! f
dx2=dy2=3;
7 v, v. |% r; fwhile(!kbhit())7 R4 \" i8 Z o
{
8 n# x$ c) H0 m5 L: y line(x1,y1,x2,y2);
8 k ]+ J4 }! I; Q x1+=dx1;y1+=dy1;
1 k* k9 o% `/ b3 @1 M+ r" M( e x2+=dx2;y2+dy2;
0 F- l7 D+ m5 h. H: A/ o if(x1<=LEFT||x1>=RIGHT)
" P/ h& C* ~- S dx1=-dx1;
* y: |7 _, F# [( r8 ^ if(y1<=TOP||y1>=BOTTOM)8 a2 h( V! F: f0 _ o
dy1=-dy1;4 N9 C3 S; S8 f
if(x2<=LEFT||x2>=RIGHT)9 b) Y; K8 c, U2 G/ H# u8 E/ l
dx2=-dx2;% O4 U+ F( m3 d, q( i
if(y2<=TOP||y2>=BOTTOM)
0 _9 q( N+ I# H( H1 L! m" p dy2=-dy2;8 L9 P: ?& f4 L( u: W. ?4 f
if(++count>LINES)" c) B; I6 k4 J2 d$ y1 Q
{" @' ^# m& z" ?% T, C
setcolor(color);
- ~* r5 b* G% o# `1 K" z, B9 Y color=(color>=MAXCOLOR)?0:++color;1 o5 Z/ l; e) o& J+ B2 S2 ~' V7 |
}
/ g0 d3 H' [0 c, c* O7 S; |}- ^+ H* W0 l5 {& t
closegraph();/ k. {$ K3 }( M4 V) B4 V$ a; p- b
}
- t/ V, R3 O8 N' R4 k8 `* e【程序61】# r# s) X: `- B. D# e ^! U
题目:打印出杨辉三角形(要求打印出10行如下图) ( ?/ s$ _3 V. ]5 @) Z& ]
1.程序分析:* G. O! C8 z% R
1
5 N0 n, a' K+ X7 J; o 1 1; I3 v* A1 N. T$ C. b
1 2 1& g7 M! a0 h ?! k, q8 s
1 3 3 1
( u( s9 c6 ]$ {" b7 z0 M 1 4 6 4 1
* P; a; B2 \, `/ n6 ~+ O 1 5 10 10 5 1 6 c( W1 i: P$ g
2.程序源代码:, u+ G( n3 }/ @
main()
" G4 J8 \. K. {& }& d{int i,j;+ [3 y* G& T' O
int a[10][10];) \3 D, P7 ], I# L
printf("\n");8 g" A; k# Z2 _& l8 P; C+ Y
for(i=0;i<10;i++)
8 D8 @, ~( Y7 S' E; R1 e( n3 [ {a[0]=1;% g5 \- B3 U$ ~3 i# M; h
a=1;}9 \, p3 D3 L) |3 A7 A: }
for(i=2;i<10;i++)
0 k+ {/ L! e# j$ W. B7 x7 |8 ^ for(j=1;j<i;j++)- ?; O5 ~8 D: z9 c
a[j]=a[i-1][j-1]+a[i-1][j];
- J6 J. O7 |# g0 V# gfor(i=0;i<10;i++)
9 m: T2 L# o" J5 v8 A {for(j=0;j<=i;j++)
& v1 \, ?" \2 h) I5 r* h printf("%5d",a[j]);
1 d7 r2 L6 |" C6 E- d printf("\n");: H' O( D( t" L7 V
}# }. Q& L- X1 ^7 x. c
}
, k: t, N& j& N+ p$ O
【程序62】
* A- m" i& w3 u# o题目:学习putpixel画点。
. T8 g2 ~, Q M$ L( D* Z3 P1.程序分析:
; T6 u9 M" A" y2 {+ F4 C2.程序源代码:6 }! P+ [; `- R. C) |9 M0 a
#include "stdio.h"
/ o0 l8 x [, w9 i; n! T#include "graphics.h"
$ p3 d3 e9 {% M, Zmain()
' H" W( K/ h/ S) K8 n: Q{/ P) W! q3 H! \, F
int i,j,driver=VGA,mode=VGAHI;
8 ~% H8 h! N) o$ Ninitgraph(&driver,&mode,"");
! L9 X" ? Z- _5 gsetbkcolor(YELLOW);
) E/ x1 G( E+ h) u, B. n4 cfor(i=50;i<=230;i+=20), a4 G) L- J: y8 a$ ~, a
for(j=50;j<=230;j++)* A7 n9 K! t6 C+ z0 A4 k9 F
putpixel(i,j,1);$ Z6 {# x! s2 M( K0 n+ _
for(j=50;j<=230;j+=20)5 O/ U6 J3 D8 s1 x2 `
for(i=50;i<=230;i++)
+ J) w$ F2 y1 J; l0 K8 ] putpixel(i,j,1);
- D8 _ p% y+ S$ P}
3 D4 o8 e3 o$ E4 C8 a8 _$ o M【程序63】# \6 U* {# B& i- Q
题目:画椭圆ellipse v9 C3 @! M4 i+ v- k6 G- }
1.程序分析:
) H8 C0 S& d& v2.程序源代码:
- F$ o# D) u. u+ Y' _# _" U#include "stdio.h"
! L2 u+ A) X6 G#include "graphics.h"# j p4 c z x% Z0 y
#include "conio.h"
9 D7 `" h: [& \8 ~& h8 H' cmain()
5 i6 F; o$ O, J) n{
2 }9 K/ {$ C8 ?+ L8 E8 G$ h- R2 `int x=360,y=160,driver=VGA,mode=VGAHI;
; d Q+ s' B0 }/ sint num=20,i;
% E: [9 q I1 c5 }+ _7 i; ~int top,bottom;
" c6 ?+ u5 m$ _initgraph(&driver,&mode,"");( V0 q* `. Q) C5 N: Q- x. L
top=y-30;" v3 a) e5 g7 @
bottom=y-30;
$ n B& T0 B* G- afor(i=0;i<num;i++)2 {% k. \$ F. N& X" S6 h1 f# }
{' G6 |/ R6 r- }! y' [- b
ellipse(250,250,0,360,top,bottom);- w, `: |7 s; M: \/ B& I* g$ g
top-=5;& `9 p0 V7 A( U' c5 R* ~1 M& y
bottom+=5;
' N1 r2 q# E- q2 u}+ @9 m' i3 Y* E, b$ C
getch();
) M7 V' [- `" O, J( [- z}
【程序64】4 ~% s9 c9 W/ T0 n, }
题目:利用ellipse and rectangle 画图。3 g3 r2 r& M3 J
1.程序分析:
% F3 X1 q2 s' p# v5 N q9 `' `2.程序源代码:
; u! {' b5 @6 A+ k0 r#include "stdio.h"" U) Q, C4 `) l3 U& k
#include "graphics.h"6 Y* W2 M$ Y; I! N9 Q" j+ C
#include "conio.h"
/ u, C* e w' I- b% u6 ^main()6 \+ G) y# I: f+ b( n) B% w. u3 m
{4 J1 A8 l+ D) r# B# X
int driver=VGA,mode=VGAHI;& {- o. B' i. e3 ?
int i,num=15,top=50;, T% J1 S& j2 K1 b& f
int left=20,right=50;
+ p* s: F( T5 o e8 ^initgraph(&driver,&mode,"");
' F. A q w# Wfor(i=0;i<num;i++)7 f( R0 ^: L' K, H+ L$ Q
{
( j9 i% k: j; p: t; x) Cellipse(250,250,0,360,right,left);) P& `& [" N0 B
ellipse(250,250,0,360,20,top);
4 \: i8 v3 D4 j: R5 r* U4 _: brectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
1 z$ {; P- p d" u/ vright+=5;6 h$ i2 U: q! \& T+ s$ e& i: ]
left+=5;
+ O. R+ j! |5 v# \8 ~& dtop+=10;- N* P# A0 z2 W7 ?* t8 V3 f2 Y
}
6 Q! q- }4 f1 Agetch();
6 u, _2 V, _5 _) J Y4 }}
【程序65】 {, S! ?3 p3 E5 z* u
题目:一个最优美的图案。 $ ~% B) `' O% J
1.程序分析:
5 j# {5 q% @- ^2.程序源代码:1 ^; c: X* ?9 M6 n% Q
#include "graphics.h"8 W* ~0 l1 G* T0 b% b& Y6 ]. a
#include "math.h"
6 { q) D- u& n) w; Q/ i#include "dos.h"1 N9 ^. n9 N/ g# o# c
#include "conio.h"; ]/ {& @# g% h. U$ k; J
#include "stdlib.h"
5 a& f+ L ^4 m7 M- B& Z#include "stdio.h"6 Q* Z6 f4 [- t4 l
#include "stdarg.h"/ V1 n* V R, z6 I
#define MAXPTS 150 L( W0 C2 y, Y6 z5 m& q. }
#define PI 3.1415926
4 I( L( x" F2 ostruct PTS {
! A w" N3 T9 s. nint x,y;
* X6 n! ~5 e. @+ Q};' X3 A% I( E4 |) _
double AspectRatio=0.85; o8 E9 u9 |9 F- R
void LineToDemo(void)
# K9 x# L" a% V. M+ L{
" \- g" T, D: M$ L+ _7 m8 ?struct viewporttype vp;
2 f' T! E i9 t. Gstruct PTS points[MAXPTS];/ o' y8 }) O6 E. V- @& S# U
int i, j, h, w, xcenter, ycenter;
& C5 A4 {3 v) {+ Yint radius, angle, step; ?! ^7 A/ h, a
double rads;
1 W" o# k% j) R* [: |8 {printf(" MoveTo / LineTo Demonstration" );/ R1 c4 Q6 w( v
getviewsettings( &vp );7 T5 {. c5 D: e k* `7 v
h = vp.bottom - vp.top;
* U1 v, D j! x1 n) N- j5 }w = vp.right - vp.left;. N% Z7 I; K3 W4 p+ ?7 Y% b; q
xcenter = w / 2; /* Determine the center of circle */
3 I7 l( G/ o! L$ Sycenter = h / 2;9 q; M& ]. G, W) Z9 P
radius = (h - 30) / (AspectRatio * 2);
Z! }0 |4 K4 c% v2 a# G4 T astep = 360 / MAXPTS; /* Determine # of increments */4 d) U- N1 E1 A' D2 n
angle = 0; /* Begin at zero degrees */
- ? c i9 r" u A5 a- r/ z8 Sfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
' s& l$ ?9 ~+ a* Brads = (double)angle * PI / 180.0; /* Convert angle to radians */8 q' k# [$ }' e( [. V, g4 ]2 v6 r
points.x = xcenter + (int)( cos(rads) * radius );, m3 @6 i4 `) n2 j5 N
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );4 B- m2 O9 ]8 L
angle += step; /* Move to next increment */( v% x% a5 j) r. i* H& L: l! j
}: `' u* i8 D$ N1 r5 m
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
6 Z, }1 h; [% z: Wfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */3 P# o6 R9 P4 [6 [/ m" }
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
# m3 f' L6 p' ^" U2 H- Imoveto(points.x, points.y); /* Move to beginning of cord */6 c" \8 M9 z' x
lineto(points[j].x, points[j].y); /* Draw the cord */1 u* }/ _0 @/ q! m
} } }
Z$ C$ V4 D3 V/ A" \4 i) lmain()
/ Z6 u; l N4 P: i) G7 z{int driver,mode;
. \+ m1 S4 z. J% C. \driver=CGA;mode=CGAC0;% }6 `* g. J5 \. H( s: c k! p
initgraph(&driver,&mode,"");
* w4 m7 d* B$ s( msetcolor(3);( ]! K8 b$ N7 Q
setbkcolor(GREEN);
& N W$ b" l; `5 T. Q2 ~4 ]LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
3 s s/ } I$ a J& \
【程序66】2 l+ m- `: V4 | M$ q! f
题目:输入3个数a,b,c,按大小顺序输出。
% E4 M0 f( w1 E5 I1.程序分析:利用指针方法。
# O! Z- ?8 n, b8 G: F( t, N2.程序源代码:; S6 e/ w5 A$ b5 }
/*pointer*/2 I! f: X0 g% B+ _# Y! {8 ?
main()
- q$ n0 T$ ~ o9 G{
0 p# w! o" a1 X6 uint n1,n2,n3;# J# R0 B. t& L m: X0 H) O
int *pointer1,*pointer2,*pointer3;
+ `1 k S2 O- M# e* ~: }printf("please input 3 number:n1,n2,n3:");: Q3 u3 C, ?2 Y! C3 Q$ Y
scanf("%d,%d,%d",&n1,&n2,&n3);/ R6 S) ^! f1 P$ D. T1 E4 y
pointer1=&n1;
3 f7 Q& l4 u5 N2 T0 A' [& Ypointer2=&n2;
7 V- U7 e+ v ypointer3=&n3;
* b9 Y& P9 m1 R, Tif(n1>n2) swap(pointer1,pointer2);' D; n; T) S- B! [* B2 Q$ H
if(n1>n3) swap(pointer1,pointer3);
* x% a' g, R5 d$ E( `' V" _if(n2>n3) swap(pointer2,pointer3);
% s/ p' G' u3 R! H( i" Hprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
- r; D* H, G. o$ }}- \- n* ^9 Q& f3 ?# ~
swap(p1,p2)
; Y5 c) J- }; K" `int *p1,*p2;7 `# k5 u/ J6 _8 p9 q8 P6 [
{int p;
# C/ n2 [1 ? y, P; Qp=*p1;*p1=*p2;*p2=p;
6 W `* G- K# [ O+ S3 V}2 N6 @) G/ j5 l5 ^ t: e6 l6 u