【程序59】$ M- I! d' F. J7 W& a' O% e3 p
题目:画图,综合例子。6 e& U5 c4 ~4 w _2 [
1.程序分析:7 n& U, M1 R- I' s. j' [- K7 h6 R
2.程序源代码:0 }/ b3 Q, Z7 C8 m9 l
# define PAI 3.1415926
! M: r; X4 E6 w# define B 0.809
& o; n E3 n7 h$ x/ f' t# include "graphics.h"" D( D& r8 v n
#include "math.h"1 }3 I8 B2 k z$ O
main()% X9 g9 @0 e5 ~
{! U. ~9 w. t# e, k3 Y, m* g
int i,j,k,x0,y0,x,y,driver,mode;
, k8 O4 t. F/ o" ]0 b3 A5 \ Pfloat a;
- X2 A: W- G: c, ]5 ]driver=CGA;mode=CGAC0;( ]3 z/ Q" A: {! @: U
initgraph(&driver,&mode,"");
# k! F& L5 J# g- M3 n, asetcolor(3);
. z: t6 a! T3 d5 t) j' H- ?+ J4 esetbkcolor(GREEN);$ O- g/ _- F' Q- k2 e) @' o
x0=150;y0=100;
& C. t" q6 r! ?, o$ i: Qcircle(x0,y0,10);6 y% h# ~/ r8 T6 a6 k
circle(x0,y0,20);; [4 d8 w7 K( f+ \6 z0 f+ {( d
circle(x0,y0,50);- ]. \$ Z3 U$ o: s9 z- y" y
for(i=0;i<16;i++)
- ^* j! X' ^& H2 i1 R{
& `* O z! @; w) u a=(2*PAI/16)*i;
2 E: q! {5 O/ ] ]* G# V x=ceil(x0+48*cos(a));
4 x4 y) x+ X8 z: T! v y=ceil(y0+48*sin(a)*B);5 S- H# W9 [. \4 m! {: \" L- w* d
setcolor(2); line(x0,y0,x,y);}
1 b+ y, }/ X; s, e9 Hsetcolor(3);circle(x0,y0,60);/ a$ K3 p* d' R( l- a
/* Make 0 time normal size letters */9 a6 ^: l. R* W# @0 F0 i8 `0 k+ f
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);( g' {5 a7 o9 w5 o
outtextxy(10,170,"press a key");
& |! r; Q- u/ l, E; S( ngetch();
$ f. E2 |. y- ]+ z: b' xsetfillstyle(HATCH_FILL,YELLOW);) C2 `6 [1 q- |9 @
floodfill(202,100,WHITE);8 U3 P0 z! Z3 A; L% o P& K
getch();
. u8 B7 j* Z, x9 e1 Tfor(k=0;k<=500;k++)( w2 Q$ K ]( J6 w
{: Y" v, E. l" @; B
setcolor(3);, a) T+ R- k- u* D1 I% Z
for(i=0;i<=16;i++)
7 c9 `+ J& [' ?/ L {1 v; c. G' J$ V# ?6 ?6 _1 u2 |7 K
a=(2*PAI/16)*i+(2*PAI/180)*k;
* g" f$ Y' k# C% } x=ceil(x0+48*cos(a));; o+ w( W. A/ x
y=ceil(y0+48+sin(a)*B);
+ X9 K6 r8 O( P$ F setcolor(2); line(x0,y0,x,y);4 ]/ f2 I. [) i9 t
}0 J( B) X) \, A* U
for(j=1;j<=50;j++)& Z/ r6 J7 W2 e' c
{
c. V+ e- _8 ], s6 a a=(2*PAI/16)*i+(2*PAI/180)*k-1;
& |/ _5 \2 L. b5 a* K x=ceil(x0+48*cos(a));
' G' r* K+ y" |- y2 v$ }* E y=ceil(y0+48*sin(a)*B);
( W5 l/ D" Z6 ^# a line(x0,y0,x,y);
' D' v2 U/ M( S1 t; n; ?$ c6 I/ j6 o }
- \4 w* y+ i+ k: b5 L: r}
" }! L( r, m7 T. T% N' \4 u. _) Srestorecrtmode();
$ d" ?0 y7 q6 D1 M" J}
【程序60】
/ _( ~/ c& m4 I: }9 Q: B题目:画图,综合例子。
8 ~" ?: b4 ^1 S1.程序分析:3 n( G8 _8 ^- m: Q
2.程序源代码:
2 f) X& l7 f. } [#include "graphics.h"
+ C- r+ l. x2 u- d b ^) J, p#define LEFT 0
H1 `- `1 c- M0 W2 Y( w1 O#define TOP 06 A# U+ @% a ? ?
#define RIGHT 6393 @( B. y4 k, y s
#define BOTTOM 479+ U9 R$ M# c* a, Q
#define LINES 400
4 ~5 x# h- A; u* _; K! a#define MAXCOLOR 15
1 |5 Z; p( t3 S* ~0 C) Omain()$ u- J" |; n1 D) y3 f
{
0 p2 i3 Q8 }( w$ j6 G) _) cint driver,mode,error;& j- {% h1 R1 U& `5 M
int x1,y1;! o- J! b5 O! _1 J
int x2,y2;
4 P) i# f+ ]7 U5 Dint dx1,dy1,dx2,dy2,i=1;
# ~: A C+ Q- [int count=0;: j' c3 S2 k. b4 u
int color=0;* {, |5 q5 g# ~& ` [
driver=VGA;% P9 C, J9 m6 c% D8 S
mode=VGAHI;$ Q0 s% Y% ^% ?9 S6 h3 C7 P
initgraph(&driver,&mode,"");3 ]+ N+ y# f+ v( m* G% S5 o9 c
x1=x2=y1=y2=10;! X2 M- M5 j5 S Z5 i0 N
dx1=dy1=2;/ C4 O6 Z+ T4 G4 ?! s8 p- N
dx2=dy2=3;, A9 o6 q& T4 |. x9 q
while(!kbhit())8 i5 H+ i4 a! o: D. z+ t( k
{
9 x$ H; o) q3 @ line(x1,y1,x2,y2);& D0 v" ]- u8 {7 c4 o( Y3 C4 [
x1+=dx1;y1+=dy1;4 e, b, y( O1 V# S$ i5 d
x2+=dx2;y2+dy2;
8 `( Z0 E1 A; R! Q8 {2 |7 V, N if(x1<=LEFT||x1>=RIGHT)
2 g3 p( d, A8 t, Q- T dx1=-dx1;
q2 b0 `, ~# n% I$ t; f if(y1<=TOP||y1>=BOTTOM)
, ?7 A# e0 z _) A dy1=-dy1;
7 b) Q) K4 ^# A" s if(x2<=LEFT||x2>=RIGHT)
; N; ~3 F/ \1 \: U dx2=-dx2;3 @6 |7 ^ @9 d+ s7 C0 X' B# \8 ?# W
if(y2<=TOP||y2>=BOTTOM)
; g2 c& r, R- H o dy2=-dy2;
2 A1 j% M/ O9 G+ m. D0 B h if(++count>LINES)# i8 e$ E: R g$ O4 C
{' T D! K6 }5 L7 p( y' [
setcolor(color);5 t3 {8 @$ m" O$ g9 M0 w
color=(color>=MAXCOLOR)?0:++color;
, t& N# J7 z; n/ J }
2 c) S1 W; k, }% J" ~}
: r$ P& H/ r H& h# A4 L+ o" g8 lclosegraph();; i9 V W, \6 j( e' I0 ?: v* u
}
9 V. m! ^! j) L$ h. T4 r* {$ x
【程序61】
, G% O$ v5 [' c- m题目:打印出杨辉三角形(要求打印出10行如下图) " E; E3 W0 {4 a% M$ P: B
1.程序分析:8 C/ Y8 b# `" Z- h3 a( V- g
15 U* A' L- P" a6 m/ L4 q2 V- p
1 1% V+ u$ z" f* K( ?# R
1 2 1
* Q% \) f5 e, D8 Y& E7 D 1 3 3 1. R+ _: |8 V( `7 z& r
1 4 6 4 14 f- ~- ]7 L o+ P; I# C3 K
1 5 10 10 5 1
0 o6 u8 c+ E1 U2.程序源代码:
- t- O: s/ _4 J9 q+ [main()
5 k* h: r0 E7 ?$ I- O* {$ T{int i,j;( \: y0 L9 T4 Z3 S; y) }' m
int a[10][10];/ u7 W5 E1 x6 t
printf("\n");+ w. s3 Z B% e* X+ @
for(i=0;i<10;i++)
' G2 M# @2 W) S1 M5 k8 [( L {a[0]=1;' r) @) w+ J: e, \
a=1;}
# E" z& ~0 A2 e6 sfor(i=2;i<10;i++)
9 [) Z' [( @3 B) a for(j=1;j<i;j++)
7 e7 O6 V* B: A7 M6 y a[j]=a[i-1][j-1]+a[i-1][j];
$ X% X7 E' O+ c% b/ y, hfor(i=0;i<10;i++)
5 _1 O# z4 b; ` {for(j=0;j<=i;j++)1 y: c+ H2 k# \1 n
printf("%5d",a[j]);9 L3 l6 N" w v3 I* c( W
printf("\n");9 w% H2 H; r& i. W/ D- l; S
}0 O& Q. u) |# ]) F7 F" o
}
# v" w. U- W: H. H8 r# E
【程序62】
! I: ` e6 Q2 F: m5 Z: I* R! P4 ]题目:学习putpixel画点。* \$ ], b5 A6 w9 z
1.程序分析:
( G* a0 j+ K. c1 |! u- J4 Y2.程序源代码:4 ]& W, v) A- H* |. j) y* w
#include "stdio.h"9 b5 [$ m% x% [ m
#include "graphics.h"; m4 S" o# }! W+ \' C
main()
' W- V4 u& w+ x; s/ q2 P{
w: H$ F& ?( Sint i,j,driver=VGA,mode=VGAHI;) |3 ~9 [' L U( t- t( l m4 k. R8 K
initgraph(&driver,&mode,"");) \& A! K3 N6 r# H# L& P, P
setbkcolor(YELLOW);5 m3 G; p7 ?/ r9 h) {& O1 Q
for(i=50;i<=230;i+=20)) K( Y2 o3 L/ R3 K4 N/ ]1 [! D O' [
for(j=50;j<=230;j++)
& R# x" d' l% m8 k2 L3 C; e putpixel(i,j,1);/ v( E+ G5 `9 a3 O
for(j=50;j<=230;j+=20)
$ n# x) r. ^* ~" T for(i=50;i<=230;i++)
1 b; @' Y$ K: Q3 [; U! M! Y putpixel(i,j,1);* n# X+ e U6 D
}
0 \* {" x! `. T6 F n9 a: _$ L【程序63】
4 H8 i) W( N; f5 J4 ? i题目:画椭圆ellipse ' o& M. a. c4 N/ |) r/ ]8 P
1.程序分析:
! j* }" C2 g* P: {- r2.程序源代码:' q5 p; d# }, [; `
#include "stdio.h"
& ^+ |& \" D! D#include "graphics.h"; d# |0 P: }/ Z5 Y7 P
#include "conio.h"
4 `( ~1 R2 M" Q" L! imain()
1 A% ?4 k9 l- Q{: k0 M! a( O0 X
int x=360,y=160,driver=VGA,mode=VGAHI;
- p- ~' K& I7 W! Z3 k1 Pint num=20,i;
0 g# F) J( x& Z: Z( Q/ l& hint top,bottom;, T/ h& e2 r2 T- ~5 M; _0 g- T
initgraph(&driver,&mode,"");& f2 [- x; [% U( `' S) X4 Y
top=y-30;
: [$ k/ C, q! |- V. l2 rbottom=y-30;
* i5 B) q& \) b( O1 L; ^7 ]) afor(i=0;i<num;i++)" U+ |% M) R+ p5 N) Y, u
{
; f1 O: U' H" ]+ r3 h$ R2 e: ~7 fellipse(250,250,0,360,top,bottom);
1 |: F; v, |" a# xtop-=5;
( a+ R2 |7 Q' F+ g" ]bottom+=5;1 J( k/ p+ f. h* ?5 Y
}
, D# C: b" L+ b! ?; [$ wgetch();
/ _8 Z4 G$ h$ A5 g0 b4 F" X7 Y}
【程序64】. \6 U5 ^/ u* m
题目:利用ellipse and rectangle 画图。
, O& a% l6 j, B) A+ g1.程序分析:
; w% S4 ]: j B% K4 I* W2.程序源代码:
' E8 K8 x. _2 G& K* n- V" N+ ~#include "stdio.h"7 W$ C# Q# _. ~) X
#include "graphics.h"
% q* W: l4 ?. x0 G. r#include "conio.h"
, {: o* | N; f6 A$ O) H fmain()3 Q3 z* H, G- b# v4 a
{% w6 d3 u& B: v- v1 r- t
int driver=VGA,mode=VGAHI;
7 ^. y% K9 Q$ E( Y7 ~int i,num=15,top=50;
0 \# a2 d3 D# p+ B7 N8 oint left=20,right=50;
: @4 e2 N0 s2 p8 u5 N% r( `# \initgraph(&driver,&mode,"");0 c# V. ^' I0 q+ S6 Q, C4 T
for(i=0;i<num;i++)
' B1 ~- t3 U/ J0 J{5 o$ o8 M( @& u
ellipse(250,250,0,360,right,left);
6 K" v+ ^/ U8 a g- S3 s4 D- Uellipse(250,250,0,360,20,top);/ v! N* o5 @* }9 t: p: h8 l
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
8 J6 I5 [' D! k ]" ], c6 T+ jright+=5;
! q! M9 ^' e; q- A# z! a& u9 {7 qleft+=5;- `6 F9 J% ^1 ?7 K! g; ~- J: m7 Y
top+=10;
$ |. e0 B, x; @* o}
) [; r: t/ x a) ~* Kgetch();! E; H7 ]5 J) Y u+ j7 T( @
}
【程序65】" {9 u! \- t8 y; U. `" s
题目:一个最优美的图案。
7 S) w, a1 h8 ~2 s( X6 |% D. x1.程序分析:5 J& D& A' D& N* J
2.程序源代码:
# Q- c6 f0 a. _6 u#include "graphics.h"
5 q6 m& O! ?3 u* E7 q#include "math.h"
; r: v, K- N* J5 f' x3 @#include "dos.h"
, Y7 o0 l- i& B, R& @#include "conio.h"1 g T$ H3 s& G" S0 R
#include "stdlib.h"1 L+ x2 B) s$ k
#include "stdio.h"
. ^7 ~+ \/ V4 {2 t& }) T; ^" r#include "stdarg.h"
" g* r! i. L$ s. v A#define MAXPTS 153 R. n, c) x' j9 C N' W. ^
#define PI 3.14159266 u1 y4 h* a! B
struct PTS {
% _8 s7 R# ^- L- ?int x,y;
# @& o9 i) p, B};2 f9 M) K g3 C# m! T# W) x1 A
double AspectRatio=0.85;, a2 ?8 w/ S9 S! Y! Q
void LineToDemo(void)
. K+ e# Z& r# v8 A{5 A0 o; A' b7 L$ B+ S, f w7 e
struct viewporttype vp;
3 C$ Z+ R+ D3 xstruct PTS points[MAXPTS];
X6 H. D+ b2 [+ g$ Hint i, j, h, w, xcenter, ycenter;
) z4 ?! Z- n0 ?int radius, angle, step;
2 T r% [3 p0 D& v& I# Sdouble rads;
1 J- G# ^. K4 u$ G* Wprintf(" MoveTo / LineTo Demonstration" );
! s( _8 o1 j0 F `* w/ Y2 O& tgetviewsettings( &vp );
5 A! M% K& u. p" w% _h = vp.bottom - vp.top;" i; E+ g( S7 i0 S$ n, g! b
w = vp.right - vp.left;. t I) M- |4 e4 s9 ]
xcenter = w / 2; /* Determine the center of circle */. N- W+ S8 L2 ?7 R& {
ycenter = h / 2;
0 q) Z0 [6 r( R2 ?2 Kradius = (h - 30) / (AspectRatio * 2); o% J$ U8 Y0 p8 p% V
step = 360 / MAXPTS; /* Determine # of increments */
& a! ^8 v9 X- M8 b- u. ^angle = 0; /* Begin at zero degrees */; b; h, e( F& a3 p
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */) \$ G/ B# o# K! Y3 @; V: C3 T
rads = (double)angle * PI / 180.0; /* Convert angle to radians */8 R7 z$ f, N: l
points.x = xcenter + (int)( cos(rads) * radius );
/ z/ f! b- ^0 m; E/ `0 ~points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
% Z: |( z! q1 O0 v: k/ Y& T' @angle += step; /* Move to next increment */0 K" ?, L% ~4 V" s( N M, @3 x) i
}" k, U! J6 a* O6 r/ ` c2 I. u, W
circle( xcenter, ycenter, radius ); /* Draw bounding circle */0 t4 i2 l8 u% `% Y: c
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */9 d% C& P! P( h ^
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */* D% c1 Y0 @4 T" a
moveto(points.x, points.y); /* Move to beginning of cord */+ u Q0 }* f G) p; q" u t
lineto(points[j].x, points[j].y); /* Draw the cord */
0 _+ k0 e9 j. \2 k: {$ e} } }
" ~& o2 f- i) _5 M: x6 Jmain()
4 _+ L5 q1 Y# ? M' ?0 j4 v8 F{int driver,mode;' z' b8 D. T* H! o3 }2 q1 e, v
driver=CGA;mode=CGAC0;6 A4 r6 X3 v0 T
initgraph(&driver,&mode,"");
! o& u! Z5 m; X% w8 d* z( Vsetcolor(3);
' R4 f' Q' x# z2 d4 p I! csetbkcolor(GREEN);5 h; Q) s; N+ c
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
]9 U" q$ v% s2 P4 }/ s' E
【程序66】
% h. @0 C& x5 t- r; _# |9 R题目:输入3个数a,b,c,按大小顺序输出。
( N& a* K2 x6 ~, f1.程序分析:利用指针方法。% _. u! }. } ~3 w! {8 B1 F B
2.程序源代码:3 X& s* |8 J) E2 Z" M
/*pointer*/
6 J% z4 M, a" w7 Fmain()
& Q# Y* K+ q% f) L{3 }- u1 P3 F4 i' ?. X" ~$ T
int n1,n2,n3;
+ a1 x, t( z9 y* X6 F. z/ q& k0 d! O% Zint *pointer1,*pointer2,*pointer3;
+ \! O; S ?; l9 Vprintf("please input 3 number:n1,n2,n3:");! q7 P+ _# \* O& k: Q
scanf("%d,%d,%d",&n1,&n2,&n3);) i/ M- g# N6 C5 a* e
pointer1=&n1;
( W5 n0 B& P% K# epointer2=&n2;
1 @0 @4 E7 M8 x8 _% ^. Lpointer3=&n3;
# M' j% H" @0 h2 U% }6 w7 y+ ]if(n1>n2) swap(pointer1,pointer2);: q1 Q2 {3 M/ \, `' i5 v4 X7 O
if(n1>n3) swap(pointer1,pointer3);& k" v0 k V1 k6 S1 l a) r
if(n2>n3) swap(pointer2,pointer3);
- l7 V: D* l; o B) z1 ]7 h rprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);7 O$ Y: i q$ V0 }0 O9 @
}
2 H/ L& V/ R( n9 w& Oswap(p1,p2)6 z7 W" D. i! u. `" H8 N) A4 ?
int *p1,*p2;3 r1 y6 K( r |) F) u. M, q
{int p;
- L) O+ g, R6 tp=*p1;*p1=*p2;*p2=p;
! B7 y" P5 R8 L5 z}/ |1 t1 L% C# d* ]6 _