【程序59】
( X; m S) W/ y题目:画图,综合例子。3 o2 |8 D1 |4 i% g" ]
1.程序分析:
) p5 u: Z4 L- s" X2.程序源代码:* t+ @/ P# p2 X: _
# define PAI 3.1415926
! e# }5 V" ~ }5 N# define B 0.8093 ]' |! o2 {; |$ z8 ], v- I
# include "graphics.h", }5 f7 }% L, E s
#include "math.h"
5 x! x7 K8 j5 lmain()3 j& p0 U1 p* X6 [) l
{" a8 G5 X" W, `, ?: q
int i,j,k,x0,y0,x,y,driver,mode;! @5 r+ @- k2 z1 B0 U& W" ? _
float a;
2 m6 K2 ~. w; R" X+ }% v8 rdriver=CGA;mode=CGAC0;
4 p2 A8 V* }( e% winitgraph(&driver,&mode,"");
x( \# i% y" ^' }1 i& E6 ssetcolor(3);
0 P" {# U% h0 x- Asetbkcolor(GREEN);
& @# g4 H) ?% K( Dx0=150;y0=100;, ?& r: L- F& e( m) W
circle(x0,y0,10);
/ |; f$ T+ U- t# W3 J0 W7 Z) f, v2 Ecircle(x0,y0,20);, R v* f0 h& b1 _
circle(x0,y0,50);
) s# I# v' d0 j8 i/ c7 Yfor(i=0;i<16;i++); D4 V* {, r+ S' z
{
1 B$ L" v. b1 [% J( c a=(2*PAI/16)*i;# z0 D4 E6 D- e0 _3 x$ ~
x=ceil(x0+48*cos(a));
( K% h" V- L/ A1 T& E' ~ y=ceil(y0+48*sin(a)*B);: m; a$ v/ ?& a
setcolor(2); line(x0,y0,x,y);}
* q/ [8 G$ ]: m6 w1 q, z! k; o, v2 Jsetcolor(3);circle(x0,y0,60);% q9 o$ Y4 n5 v* f) S
/* Make 0 time normal size letters */ S6 C4 M% Z" @4 Y- w! w0 R: L
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
3 s. w. z6 F% m) oouttextxy(10,170,"press a key");7 n5 _9 T2 b( s& | ]
getch();
% y% x3 @8 R7 E5 l& |setfillstyle(HATCH_FILL,YELLOW);" `0 y. }3 ~ V/ x* p3 T
floodfill(202,100,WHITE);
, q/ b' H, ?* }& d9 Rgetch();5 u- L- j5 Q' l
for(k=0;k<=500;k++) n" _" q, j: u) J v5 |2 |
{' k. N" F, v. Z, K: K9 a# Y: \5 S
setcolor(3);4 y P9 ]# L8 ?; N5 C$ r
for(i=0;i<=16;i++)
, ]% a. Z; n# U4 \# J, e" X {3 i+ V3 T0 Y" C" C
a=(2*PAI/16)*i+(2*PAI/180)*k;0 A7 N, O' ?( T5 |2 G7 y( u
x=ceil(x0+48*cos(a));
& a: r! R" n& q( W. C y=ceil(y0+48+sin(a)*B);
* Q5 O# S0 Q1 f7 M' K setcolor(2); line(x0,y0,x,y);6 [' w+ ~4 O; V+ A$ Y$ c+ _2 Q4 o
}* t: Y# R: i( x2 I" B0 K1 l
for(j=1;j<=50;j++)
8 L; l4 n2 P$ Z! r, C6 [1 Z3 ^# z6 O {3 F6 Y6 G& K8 t3 y2 J& X8 ^
a=(2*PAI/16)*i+(2*PAI/180)*k-1;) a4 H1 ^2 B, O/ m& d, y$ t
x=ceil(x0+48*cos(a));# u' e6 [9 W, m5 a7 U$ F
y=ceil(y0+48*sin(a)*B);3 {) X: P& S& j# O
line(x0,y0,x,y);% G8 w) Y" ?! j$ o
}
: ?5 F2 t* l' W! Z# S}
6 L9 C5 u& |8 r3 P. a; Y- R% crestorecrtmode();
& M( l7 s4 }" y}
【程序60】
0 n7 u& Y# g+ {$ F! q题目:画图,综合例子。 . M& @9 p, S, h4 j/ u
1.程序分析:
; F5 I1 [( ~; H: h2.程序源代码:1 b) y4 Q" R5 P5 R5 {, _2 w- a
#include "graphics.h"2 k9 r6 S$ r+ B3 B0 y
#define LEFT 0: m9 Q8 U6 w; H; \) L a0 g9 ]
#define TOP 00 Z8 c" H; t5 M
#define RIGHT 639% h+ j( z$ o; p' V: g
#define BOTTOM 479
! x5 \9 d& q" t0 [+ t9 M: d" ^#define LINES 400
: g' F ~+ w% s; q, J+ \6 }$ \0 H#define MAXCOLOR 15
: P- t% i& H* H& \% y; ^3 b2 _" P7 imain()
9 f2 \9 B' j+ U$ }& t{ M6 K5 X. z0 A5 m! V/ |0 m+ t) N
int driver,mode,error;+ j8 a* \- d* l; x, s
int x1,y1;- I: N# e: n! R9 r
int x2,y2;
; q# Y+ z3 D1 V) {; P! U# w/ cint dx1,dy1,dx2,dy2,i=1;) b4 F. g: s- I4 C( H1 O
int count=0;
0 `& b; E- c: d, o& [( @int color=0;
3 V7 L" y! h) t* T& x" jdriver=VGA;9 ?; w! f! Y" ~+ }3 U
mode=VGAHI;
9 u- j6 v* s2 h1 Sinitgraph(&driver,&mode,"");
! I# ?( y8 d& t8 b$ Px1=x2=y1=y2=10;9 W7 A3 J5 p d6 A; [+ }; E; E2 ]
dx1=dy1=2;4 r; U, |* E! K( Y: o$ F3 c) ~+ W( }4 t
dx2=dy2=3;$ Y( H7 w/ l/ H
while(!kbhit())
2 e$ R' w, l1 D) b9 E$ t{ Z. p( T* F# J' Z! { f* `3 N/ X
line(x1,y1,x2,y2);
W. k, s- r4 n' B x1+=dx1;y1+=dy1;
0 }& A; j: o) V x2+=dx2;y2+dy2;
$ x2 i' P9 M5 c& `( G( N/ r if(x1<=LEFT||x1>=RIGHT)0 C# k) P9 P9 h5 p" e
dx1=-dx1;
4 u: Y _+ ^3 ]( H) x _ if(y1<=TOP||y1>=BOTTOM)
1 d i% i+ }6 n, u% V: y; K dy1=-dy1;
' _; U% L, c8 L if(x2<=LEFT||x2>=RIGHT)7 f( j' v0 V' a/ B
dx2=-dx2;
0 ?: G+ J* e& \% f if(y2<=TOP||y2>=BOTTOM)3 h" J% R! F7 c* [7 G
dy2=-dy2;- h* o: q! M# J4 t+ r8 O
if(++count>LINES), ^$ K& L" l- f) l5 z4 m
{
; U& j, ]! ^3 @ setcolor(color);/ [/ _, p: o. y- ?8 W! H
color=(color>=MAXCOLOR)?0:++color;: S4 C* i& [3 f
}
- T. y3 f/ ^- t/ ?, S; w! ^$ Y4 q9 M1 I4 K}5 l3 j/ ~! _* |" j6 _3 Y
closegraph();4 H% \- e4 ?4 d! f1 i
}
V- c+ E$ G$ d【程序61】8 o; K. V, A1 H5 {: G
题目:打印出杨辉三角形(要求打印出10行如下图) ; P! L' G! g- C6 k; y& k* l3 b
1.程序分析:
7 s& O. L" @" X 1! w4 a: `: l+ X' V" P5 j6 U
1 11 o7 p9 p0 s5 X. e3 q9 \# [# N2 G
1 2 1" a, i6 p* V3 E1 K" O+ l
1 3 3 1
5 F( `( x- v& t1 J& Z" W6 o 1 4 6 4 1' V* y8 p. Q3 S4 O, k2 P
1 5 10 10 5 1 2 p4 N6 o3 I2 Z! E% p
2.程序源代码:
; I; {5 v7 k4 v+ B. I: |main()% ?1 R2 d) N1 }/ i7 {+ |
{int i,j;. H7 p7 W& I1 K# W6 Z
int a[10][10];% n5 \* _) j0 h
printf("\n");
9 n4 t7 P3 @4 T6 y1 z" q R" i. m: X- kfor(i=0;i<10;i++)
7 f' J: |) X( O {a[0]=1;
2 t+ ^0 e9 c& r0 u a=1;}* F9 U- G1 }( Z5 _ E5 L2 @
for(i=2;i<10;i++)- I0 B' F& \+ ~+ Z! m! @
for(j=1;j<i;j++)
. S1 L$ a* k- n4 D a[j]=a[i-1][j-1]+a[i-1][j];
% q0 ]3 W0 H0 o4 ~( {for(i=0;i<10;i++)& _" E4 \, Z$ o6 p
{for(j=0;j<=i;j++)' P: z7 {, n" [: s! }9 c
printf("%5d",a[j]);3 C( z3 o' y$ d% C8 J
printf("\n");
* [+ Y8 A/ A6 B8 S. E/ o }
7 C# B/ {& J- L: M" a}
0 U1 P" q3 V' }( _【程序62】
6 S* W6 A: B6 b7 X0 x, g; G题目:学习putpixel画点。0 t/ m* n- U) x; p1 `" _
1.程序分析: , D8 J2 D- G# [" |. D: F" _
2.程序源代码:
% P" [6 O5 K) @8 g- R; J#include "stdio.h"
3 {6 Q6 Q+ f5 K! _* c& H#include "graphics.h"; J/ t+ y4 G0 L+ }# S
main()
; Q9 E7 }3 B" r' @" F{' T4 z+ L1 u3 r
int i,j,driver=VGA,mode=VGAHI;
; y+ [8 P9 [8 `5 r+ S9 ^6 o7 `initgraph(&driver,&mode,"");4 ]. A+ S; n/ m/ u
setbkcolor(YELLOW);
- F0 a1 L0 B9 B- x4 A8 Cfor(i=50;i<=230;i+=20)7 n+ x0 _. k2 d; f4 V# }
for(j=50;j<=230;j++), R. v; N9 {: x A! M) k
putpixel(i,j,1);# H& F* o/ B4 c" K
for(j=50;j<=230;j+=20)/ N9 `' W5 B4 C- [( T3 c
for(i=50;i<=230;i++)
2 j0 E* z7 @9 m/ l putpixel(i,j,1);
; K4 |6 a/ n& l3 l* T G& i}
; r! r7 w+ M3 x0 R& w
【程序63】
, C3 b; C% Q$ P( D: X题目:画椭圆ellipse / ^/ r4 o0 j U9 T. v
1.程序分析:
( Y8 Q/ K- E$ v, h Z; T4 W0 {2.程序源代码:
( j! ^' c9 t- F/ z- t- _2 Q#include "stdio.h"
, h" s7 o, c- }#include "graphics.h"
# {( _3 r1 s# d6 h" ~#include "conio.h"
, P# P: _7 y. Z6 ^! Wmain() [6 e- k- f) O0 B, [* x
{
8 u5 w* G2 h" s8 g: u% aint x=360,y=160,driver=VGA,mode=VGAHI;
7 e2 n& ]+ X2 V) f, k2 H8 rint num=20,i;
- j5 ~; i* g; W8 t9 c7 Kint top,bottom;! V5 s( {6 E8 B" s1 P8 ]" s
initgraph(&driver,&mode,"");0 f9 T, Q& I& @: }5 N
top=y-30;% S# ?) T( P3 u! I4 j/ P+ \- E
bottom=y-30;
# s0 n" `4 N: t' J. d; Ffor(i=0;i<num;i++)
4 h. x; v3 w+ [! z: Z# a, M& ?{
' c. y* F' @- v E5 zellipse(250,250,0,360,top,bottom);( E& U% v' R2 p h5 I4 @
top-=5;
* g4 L& P9 x9 z' Y7 fbottom+=5;
& ^& m. s5 E( }1 u& N! ]; o}
, j' a5 `: X" a- sgetch();: H; U, Z+ \9 k: `( F
}
【程序64】0 u$ L1 T/ ~& e4 B* I3 a% \
题目:利用ellipse and rectangle 画图。
) [! b- g# v; e3 O( D. I3 e+ A1.程序分析:' `% p5 S5 f0 a4 F8 s7 a
2.程序源代码:# Z( C0 O3 I3 X2 |1 |" u
#include "stdio.h"" B: _3 @% n% a# s
#include "graphics.h": u3 D2 I3 k. C. q( E
#include "conio.h"
& U0 H [. H# W. F# W y" m/ |- Y% Umain()2 i) q( C3 {, R+ s; b
{
8 n7 Y: n0 x! {2 ?1 e4 Wint driver=VGA,mode=VGAHI;
& P L3 j* G8 zint i,num=15,top=50;" Q5 r$ J5 O7 e
int left=20,right=50;
. f; i/ C2 P/ K% oinitgraph(&driver,&mode,"");# m" s E ^, g x
for(i=0;i<num;i++)0 K+ J: E8 Q2 d. p1 v" f3 W/ ]
{: m _3 a& w* {# H# d! q
ellipse(250,250,0,360,right,left);5 T/ N9 J' v! p x, J: s0 ^% d
ellipse(250,250,0,360,20,top);
) `( z$ b- L; n3 T, ~% vrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
M f4 P' `6 g- ~right+=5;
4 o$ |: m1 z; E. o1 j. M4 Aleft+=5;
/ d ~: ^7 f+ \. e+ K T4 Y; Q* ^top+=10;3 J0 c2 g8 ~8 n* M2 Q, D
}
+ Q9 B" D; R+ e+ Ygetch();
+ [+ g% m5 z, R}
【程序65】0 b& J% F6 R; P' s& G( K5 J' Y- G: h
题目:一个最优美的图案。 / K# f" P5 q/ g
1.程序分析:4 U) g$ }- O* }; Q$ D
2.程序源代码:9 Y0 b' `* ]+ E; v+ V* l' g
#include "graphics.h"0 K* j; H; x4 ? K/ k8 E
#include "math.h" M1 {% f7 ]7 B& k! b) L9 X2 V
#include "dos.h") i9 E' ^1 k; y3 D1 p0 P: V, Z
#include "conio.h"7 L3 N! J# C" }( I- n8 O
#include "stdlib.h"
& i0 ?! X( b; K) L#include "stdio.h"- [0 c, d% @! j& y" \% b+ Y$ p) s B& h
#include "stdarg.h"3 c. e; C* U" J
#define MAXPTS 15
6 p8 O. X' }7 j t#define PI 3.1415926( I/ p5 r7 m& A% j2 n
struct PTS {
: H8 y/ Q8 v& A- N, y2 kint x,y;
3 I0 m7 w- X" u" L2 K8 d};; p" @6 Z; u) C! S4 E8 X2 o! [) [5 k# J- P
double AspectRatio=0.85;( x+ S6 u5 w# O+ J. f. h
void LineToDemo(void)# R$ q' `: }& x3 N1 s- M9 b
{4 h! E& F; ?! j- a
struct viewporttype vp;
* i. _) R+ B' j! j) N7 a N; _; nstruct PTS points[MAXPTS];
* J" R# `3 @3 sint i, j, h, w, xcenter, ycenter;
% g1 N# R: ?7 a7 L0 `1 J* O. Aint radius, angle, step;9 y5 W' G# r8 e/ c8 \
double rads;
$ Z6 t8 I2 s& O* e# s X( ~printf(" MoveTo / LineTo Demonstration" );. m) b2 p( l% w, A4 h O
getviewsettings( &vp );" q; s |. X( Y7 K# m: y$ Y
h = vp.bottom - vp.top;
/ M( o5 w4 s. [w = vp.right - vp.left;. {2 y* w+ @6 P
xcenter = w / 2; /* Determine the center of circle */
$ M3 u% [5 o: t% a+ l& d. Fycenter = h / 2;, B1 f3 |7 H. {# s! i
radius = (h - 30) / (AspectRatio * 2);
6 Q7 K+ K3 ?+ G# ~step = 360 / MAXPTS; /* Determine # of increments */
% w% J, [; o$ }6 _$ I! xangle = 0; /* Begin at zero degrees */
% f, ]0 b, P g7 ^! t. k' y' e% _for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */* j: p& U$ M( w, Q+ @. u$ U9 a8 c% b
rads = (double)angle * PI / 180.0; /* Convert angle to radians */8 o1 K7 W) x; S; l6 D2 d) F
points.x = xcenter + (int)( cos(rads) * radius );
% t! z1 K, j' Wpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );8 {. S6 y8 C8 `; \4 E
angle += step; /* Move to next increment */5 g) w, z- J8 S/ f
}
0 Y4 ~$ ]/ u# I) _4 \" ecircle( xcenter, ycenter, radius ); /* Draw bounding circle */: A6 r$ L2 T- o! s: g0 @, o" x
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */: g# W. X4 C, f; j
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */$ Y- q% M. S5 a/ G6 Y
moveto(points.x, points.y); /* Move to beginning of cord */
/ c* C: s1 c" Alineto(points[j].x, points[j].y); /* Draw the cord */
( }& `7 V3 d+ ^+ o+ Y4 T} } }$ u! C) S: V4 F `
main()
: c. x& D, L# O# Z4 s{int driver,mode;9 n+ [5 a' Z4 ~, s* c+ M6 ^8 S
driver=CGA;mode=CGAC0;
% u/ t& s& \: P. Y4 R2 tinitgraph(&driver,&mode,"");
# x9 m i/ C, p0 w' A* K# lsetcolor(3);
" n. {9 w0 K( i% o/ ] f5 c8 a& Ksetbkcolor(GREEN);
" w" o- g9 H4 a4 @% |LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
# D* [- D; h/ L【程序66】
, u/ T$ [8 o* p3 r$ b题目:输入3个数a,b,c,按大小顺序输出。 5 R# N% F) e+ }, h5 G
1.程序分析:利用指针方法。
$ c! P) f3 m( ^$ }, k4 ^2.程序源代码:4 K- o* _3 B( W: [& g0 o4 z
/*pointer*/, {" P& B% R6 G, x
main(); s7 U3 g: b! `- e
{
' n- H( g& f+ h$ w! D$ jint n1,n2,n3;- E4 G- L" F$ k) m2 H' M
int *pointer1,*pointer2,*pointer3;% P9 M k% Y0 W: g+ n
printf("please input 3 number:n1,n2,n3:");; d0 T6 Z# m9 D9 V5 s
scanf("%d,%d,%d",&n1,&n2,&n3);
4 p* s/ K0 m7 ypointer1=&n1;
+ W) ?8 i( B8 ^. dpointer2=&n2;9 g# z# h6 m" D% Q
pointer3=&n3;
8 H, ` y+ f% z8 d; Tif(n1>n2) swap(pointer1,pointer2);
9 L2 v: N& e# y9 Kif(n1>n3) swap(pointer1,pointer3);9 Y& ?% ~# B$ f0 M* ~+ H$ {! g# U i$ y
if(n2>n3) swap(pointer2,pointer3);( \; t( B. O, w% ?
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);! [- S: M3 a7 i, u: y K: H
}
% |3 N3 l, L9 r! C2 cswap(p1,p2), f$ w0 y2 Q- W7 n4 C
int *p1,*p2;
( `, ?0 o2 y4 G% _& ^% G+ \# _{int p;% D- L. y8 ^' n& [! E
p=*p1;*p1=*p2;*p2=p;) y0 L" H4 L- d- l
}' y4 X2 l& K" @# q/ m, e( d