【程序59】4 [7 r- V" \; f
题目:画图,综合例子。
9 G% m; S) K# q% S1.程序分析:. V8 }* n% C# o5 M
2.程序源代码:
3 b1 j6 d2 l2 A- `8 a# define PAI 3.1415926
7 b p5 E* N& L, {& F$ R# G# define B 0.809* ~. {6 x0 ]- d9 a, \3 B) P
# include "graphics.h"- u2 k0 _- _) q( }
#include "math.h"
& e- R" b" a: w( i# X' i5 x3 Lmain()- d! w/ z9 Y4 Y( h s4 Q; M
{ `' y1 H+ L4 H5 e7 s
int i,j,k,x0,y0,x,y,driver,mode;
, |* \# n. E( z, o- ? [float a;
0 ^2 R8 L9 e: `1 ?) ]driver=CGA;mode=CGAC0;( Y+ |- Q' w( @+ W6 [# O
initgraph(&driver,&mode,"");
- ~: S/ C: M* N. z/ T. x+ osetcolor(3);
6 m, }7 j& k+ @' L; Bsetbkcolor(GREEN);
& S; y4 A6 B( lx0=150;y0=100;* j: ?9 T& ~2 x. c: A! G3 ?4 T
circle(x0,y0,10);
3 }$ G/ _. g( }3 C; O/ D2 Ucircle(x0,y0,20);
K7 o) ?! w+ Ccircle(x0,y0,50);
7 p. m# H: F+ V( C5 | T* T/ |* ]for(i=0;i<16;i++)4 s$ \: D1 ^+ e! S3 V \- |! a1 G
{
* y& t1 ~% M; l4 ?' H a=(2*PAI/16)*i;
; Z: u' z1 `* g x=ceil(x0+48*cos(a));# U. S/ Y/ p) w5 T
y=ceil(y0+48*sin(a)*B);
R1 N0 _4 `" N& q# e setcolor(2); line(x0,y0,x,y);}5 c4 i9 J1 V7 f& B% r
setcolor(3);circle(x0,y0,60);5 K. _; a& A1 N$ K. o4 C
/* Make 0 time normal size letters */( g3 W9 \2 z& [3 v/ \: |. c
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);1 u) q. x2 I- [5 ?
outtextxy(10,170,"press a key");
7 {+ X! |/ D( R N6 A. j& V/ M5 ~getch();
3 C9 Y1 i* {9 E, `' D# isetfillstyle(HATCH_FILL,YELLOW);
+ z; R/ C0 P; @- g' S% K) F0 Ifloodfill(202,100,WHITE);. J' \" c6 t/ i8 ?/ O% ]
getch();2 R9 ~1 |* r5 q
for(k=0;k<=500;k++)
* {* k* A6 t; k6 s, Y{
M, v$ C: G. S5 m7 H9 [ setcolor(3);
8 t) k+ D( b7 E2 R' v: F/ X7 {6 s0 ` for(i=0;i<=16;i++)
' E* H# E. i5 v* f, e {9 x$ _1 M- c# h5 }2 a3 C
a=(2*PAI/16)*i+(2*PAI/180)*k;# Z2 B! X4 h: c$ p( _' z
x=ceil(x0+48*cos(a));7 Q" w! ^0 U w/ ?3 W* n# |
y=ceil(y0+48+sin(a)*B);
, f# g/ c# Z# m; d1 ?: h setcolor(2); line(x0,y0,x,y);
, C, E% t/ W% B }
6 o2 \; h( L) G+ o: Z for(j=1;j<=50;j++)
+ ?$ s3 W; s# R {
4 M, |; b( B2 A a=(2*PAI/16)*i+(2*PAI/180)*k-1;$ [( R' X+ H( A. O0 Y! d
x=ceil(x0+48*cos(a));0 ^7 M7 q/ { r6 ^" c# K+ q$ l: c4 a
y=ceil(y0+48*sin(a)*B);
- P5 [* p" d/ k; a line(x0,y0,x,y);+ M# d' L/ h5 d+ {- ^! Z; I
}: V% |5 j9 M/ j
}
* w/ {; O! `, Y- [restorecrtmode();
7 j* g" ]: A' P; ?7 @}
【程序60】 d+ ^" Y- @# Y; O
题目:画图,综合例子。 ; Q& D" X$ ~. e$ l, x5 ^
1.程序分析:
% k/ Q; }7 _* h0 M2.程序源代码:
0 L3 L; W/ t$ I3 @! h$ i/ R/ L#include "graphics.h"
F. D0 J: G U( q7 S#define LEFT 06 ]/ l* z' {" T3 K* I2 r
#define TOP 0
& e1 H& L1 v7 `1 c#define RIGHT 639
1 J2 s+ Y9 Q R+ ^8 h9 B6 L; H#define BOTTOM 479
. |0 m2 j# e. s- G8 m5 h3 z8 J#define LINES 4002 u' b% V) A" p5 S) Z7 ?* u
#define MAXCOLOR 15/ N3 z) F' y* @ b y% H4 c/ V
main()6 u7 s: @- i$ {. A
{
5 [5 Y; q1 C! w7 A! d3 M' \int driver,mode,error;
\0 |+ s5 G B% S c3 [* c% Y; C1 Iint x1,y1;( F- h; j- T W; p! ]
int x2,y2;
I& v `0 f8 d5 N7 P( Rint dx1,dy1,dx2,dy2,i=1;
" N; s; }3 }7 \1 |& pint count=0;
1 J: Q9 I6 K2 ?+ h& ^int color=0;
7 D: d+ t8 R) ~& |1 a: [6 T# d* fdriver=VGA;4 O- |+ u- {2 y
mode=VGAHI;
! a* d- b# I" o5 ~initgraph(&driver,&mode,"");5 v: ^8 o; ]0 P, z; W
x1=x2=y1=y2=10;
/ b O h: u3 H3 n1 |2 l! udx1=dy1=2;
# E- K4 H, J- I) f4 cdx2=dy2=3;7 C$ J6 b. I- a) J' l
while(!kbhit())
& A# d8 b! g8 ~& F% Q{
: q! U; a* e0 ]1 h line(x1,y1,x2,y2);7 P! c/ @% d5 f z2 B# R( t5 i
x1+=dx1;y1+=dy1;: D. X, X8 a9 g& A0 \# x
x2+=dx2;y2+dy2;/ K; `0 Q7 R. h7 Z( Z; q$ Q5 s
if(x1<=LEFT||x1>=RIGHT)
; C$ `; ]1 ^$ b1 m. b$ {6 S dx1=-dx1;
* B% E" |+ M# `' l q# T9 B* B! }9 I) V if(y1<=TOP||y1>=BOTTOM)
3 w# c, f& E3 g4 N/ w k dy1=-dy1;
# b8 T. C2 T# b6 P0 b" Q; }' ` if(x2<=LEFT||x2>=RIGHT)( N" x4 h0 j" n3 f n2 ^. F1 k
dx2=-dx2;
4 i# J8 P! i# `' B, z8 n8 U9 p1 M if(y2<=TOP||y2>=BOTTOM)7 B6 y# l3 k5 G( ]5 {# s5 Y6 ?
dy2=-dy2;
0 _6 [: V/ {5 B% r, d2 T if(++count>LINES)$ e3 ^: q% g6 I* _5 s
{
* s% r, g6 R5 B" ], N. n! q/ B setcolor(color);
+ r# u+ z( @) X5 c color=(color>=MAXCOLOR)?0:++color;+ {, Y1 M( X2 a, ~7 U8 C; E( Y" [
}# ^1 R F8 v4 O9 u; t& Y7 L6 r; z f
}
( B: W, b7 p$ c4 {- V5 oclosegraph();# c$ L6 T( M8 ]0 m. W) R
}
# @% {% H1 I8 o
【程序61】
+ F+ [8 P; o6 t s3 M" U题目:打印出杨辉三角形(要求打印出10行如下图) 3 Z# W2 ^6 ~; b: n+ U4 V
1.程序分析:. { i3 s# k8 s, C
17 u4 K! r" t( d1 @
1 1
) ]& e- e. Z0 s 1 2 18 N8 M, Z, o+ g; w0 A* D1 v
1 3 3 1
" n' i4 ~% \! i' b0 e; T 1 4 6 4 1
" E( N" [7 d( d& y 1 5 10 10 5 1
; m" t- k1 p6 a0 O4 D) }( ~" `2.程序源代码:
* w8 M7 N+ u2 d0 N4 P; jmain()
* ]4 r% j1 m# A) m& W! A* q{int i,j; g! E" e; Z+ m9 n* ^" t) \, m8 c' n
int a[10][10];
. Y: x5 Y* n' G7 k6 g/ j2 p# \, _( Uprintf("\n");2 Q. f( U7 q% Q% d& W- `" J9 g9 J L
for(i=0;i<10;i++)) R, Y. r; q, N" C* P
{a[0]=1;+ {6 c' w: n! K
a=1;}
* Y9 G/ s" Z; F8 w" w9 q3 Afor(i=2;i<10;i++)
1 N a1 Y0 Z' C. K* i3 U for(j=1;j<i;j++)
1 l9 {' \1 v- ]8 X) l* B: w; g3 b a[j]=a[i-1][j-1]+a[i-1][j];
7 u" C% ]8 O9 R/ I, d( dfor(i=0;i<10;i++)2 y% L H" n1 W8 m" l0 Q
{for(j=0;j<=i;j++), N# S5 ]9 X5 e, A) k
printf("%5d",a[j]);
' K9 q- N `' | printf("\n");
! w$ j! S- Z2 ]5 E }1 W3 `; o1 t+ S7 x$ u$ R$ d+ R, s1 d* z8 x+ K
}
! \) v1 i( o8 N; ^
【程序62】
) A: h O, [9 o3 T9 T% U题目:学习putpixel画点。
8 D1 D& a$ O. u& T3 r6 }% ]1.程序分析: * X: i5 [: c5 M$ I! H/ n7 x/ n
2.程序源代码:
2 [1 d: L' ]4 ^) c0 t* X; I#include "stdio.h"# G! A. m& e6 k; B- p4 V/ m
#include "graphics.h"
4 d7 O5 Z+ l, [% c9 C9 M' x8 tmain()
/ V+ z- f; g* _{
, b3 X: g; |0 b5 ]4 ~* f7 L: gint i,j,driver=VGA,mode=VGAHI;) l7 h+ [4 u9 t, O6 c g! T
initgraph(&driver,&mode,"");
% q2 i V, B3 V4 C+ nsetbkcolor(YELLOW);
0 ]) q5 v" I2 T! s1 q" Tfor(i=50;i<=230;i+=20)3 v4 X5 t7 K# u$ R% E9 I
for(j=50;j<=230;j++)
( T C1 \* q l putpixel(i,j,1);
4 u i2 H' O& D5 j3 i0 d. Sfor(j=50;j<=230;j+=20), {- T, d8 o5 Y% K* {; a
for(i=50;i<=230;i++)" o9 o+ K: W% B: x4 W/ {$ g2 ~
putpixel(i,j,1); D4 h( n- U5 X+ f8 C1 n4 o2 g+ t" V
}
" ~0 Y6 v; B+ O ~【程序63】
% _& U0 O: s7 f题目:画椭圆ellipse
& G" s, q% o3 e4 B# k% h1 j1.程序分析:
" D" B3 u: f5 I2.程序源代码:
$ t* o/ K6 `: k. K0 v( i5 \8 _#include "stdio.h"
$ |+ S% {' g0 X#include "graphics.h"
- Y, \9 I) M8 c# E# ^0 i8 P% @8 T#include "conio.h"9 ~/ q$ r) T/ B9 d8 Q) L2 Q
main()
3 Q# D \& n0 o" [0 P2 x{
* Z( W# U( q5 {( Vint x=360,y=160,driver=VGA,mode=VGAHI;2 a& z- w2 D& g' O( j
int num=20,i;
3 x- `, J6 e; L/ iint top,bottom;
( S# t3 k$ ?& f1 T+ |3 g. d% {initgraph(&driver,&mode,"");
% P4 _, K2 x2 `, A: t) ?top=y-30;
0 e) e( k9 [: f/ v4 J# ebottom=y-30;7 M7 ]+ ?8 m; x$ N2 a, }
for(i=0;i<num;i++)2 ^' l. \" f4 l
{
6 B5 j9 R5 B' z# p* m* Dellipse(250,250,0,360,top,bottom);* E' ^8 i" I0 I
top-=5;
3 e" z! v4 \8 p+ ebottom+=5;: T9 b* j% c* W: T! f7 B& s; m
}) F# |/ F" K! g; k
getch();
( t X. H' b& `1 Q8 _2 y1 x# s}
【程序64】) D" y: l( v5 w o; v
题目:利用ellipse and rectangle 画图。( j5 s; ^+ Z3 f2 n
1.程序分析:
4 I' E& T# ]2 F: K5 c$ `* x2.程序源代码:: _1 B' j+ F, W0 G
#include "stdio.h"
$ M3 M3 O7 ?; |8 e' P( O#include "graphics.h"3 B$ @2 P* V9 Z+ O8 m2 H8 E& F
#include "conio.h"
3 e# ~( u8 z" \0 w: }+ @5 Tmain()* D5 Y* _! h/ |
{* y& m6 U. n1 @# i. ^
int driver=VGA,mode=VGAHI;
9 e) H- j0 R/ @4 p: K4 Y/ \int i,num=15,top=50;
/ v" W0 P. J% Y% _& oint left=20,right=50;: c" P+ L5 C6 O
initgraph(&driver,&mode,"");
- F1 n: I6 @2 ]& O0 n) \6 W& ]8 A; Cfor(i=0;i<num;i++)0 T2 s2 G9 A9 J: f: y4 E
{2 @5 O; y; w2 p
ellipse(250,250,0,360,right,left);
; o! f/ P8 r6 r7 D6 lellipse(250,250,0,360,20,top);
+ B7 B1 ]7 j2 O8 H9 v. trectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
. Q8 u7 ]6 N6 u9 l. ]right+=5;1 ^# z7 Y5 Q, h& [% |9 [) R9 z
left+=5;
# n% X6 L$ Z' r, [8 d0 M$ Rtop+=10;
$ h" [2 ~9 d9 t7 J ~5 Y2 B0 |. K' |}7 Q; Z4 s) M7 }+ ~1 y
getch();* M$ G' L( R5 g/ k9 |0 q, L- A) s) g, H
}
【程序65】
7 b* n; ?! I" P7 m; H4 G g4 |题目:一个最优美的图案。
5 T' K3 V: d- G: s7 l8 y" {- c1.程序分析:. ^3 w0 c7 @, ]5 e8 U% B
2.程序源代码:
4 J+ u: a3 V. \4 |& z#include "graphics.h"; d# \3 k# }4 r w/ h
#include "math.h"4 C# F' T( {% _4 |2 d, A- ?
#include "dos.h"# B7 }0 L4 t+ O6 W4 l" U
#include "conio.h"
5 d# p8 Q/ r# M6 |) r8 _#include "stdlib.h"" X n% Z& ^$ G% c+ g) k7 ]
#include "stdio.h") |# \% V8 r9 V+ E& P' F. P
#include "stdarg.h"4 i7 n6 P% X3 O9 u# E
#define MAXPTS 15
( |1 A, ] p7 X9 U a#define PI 3.1415926+ l' j4 w: V$ n+ `! |- V
struct PTS {; z& d8 }3 E5 X' E
int x,y;
- O0 A* N) W3 F9 \! h( L};' Z+ K' I+ G4 A% k8 V
double AspectRatio=0.85;
) N9 d" n4 d% \4 e; |) `; Y) Rvoid LineToDemo(void)
7 D Z% Q0 I5 ^{
( \$ ?) c2 F8 ]4 Z# @) L8 A3 Wstruct viewporttype vp;
) p9 D& W) j( D( |struct PTS points[MAXPTS];
( ~& \" t; w% \! R4 oint i, j, h, w, xcenter, ycenter;
( L* x/ e: W/ O, r! M9 }* y6 N# Zint radius, angle, step;# g9 {* H2 S A, B4 w$ [
double rads;
4 C0 e) w8 l1 c' J/ O4 dprintf(" MoveTo / LineTo Demonstration" );4 |! r' M& J6 ]3 H* E7 C9 s- H
getviewsettings( &vp );& }/ B# J/ S) M+ K3 A3 Y
h = vp.bottom - vp.top;2 n* w' n ]% _# G7 q, R
w = vp.right - vp.left;
. X& d/ {, X, mxcenter = w / 2; /* Determine the center of circle */, o X; i' A/ W
ycenter = h / 2;
* v' S5 R. |1 y* m$ C) G' uradius = (h - 30) / (AspectRatio * 2); }, H4 a( a; @' H% z( d8 T9 M! C
step = 360 / MAXPTS; /* Determine # of increments */
% p) x* h5 g, h B4 xangle = 0; /* Begin at zero degrees */
# E- L6 H% L: |2 f+ ]for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
- r1 f! f% |- C- Z. t! C6 C. Arads = (double)angle * PI / 180.0; /* Convert angle to radians */) m, B a/ y$ c4 d5 Z9 p; R
points.x = xcenter + (int)( cos(rads) * radius );* T' N% t6 ?* S+ D' H( K A
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
$ O: Z z% a3 V$ r Langle += step; /* Move to next increment */+ X6 g+ M2 M4 b2 ?/ |
}
9 p! ~5 n5 x- [. A6 P0 W. A4 V8 g: xcircle( xcenter, ycenter, radius ); /* Draw bounding circle */
, I/ w$ h- p1 Hfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
7 V1 ~; V. {+ r9 O. s A& o/ D2 qfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */( \! D- V5 i- O) h0 r( g
moveto(points.x, points.y); /* Move to beginning of cord */
9 v% Q6 E( N. a, x3 Z; D5 nlineto(points[j].x, points[j].y); /* Draw the cord */$ M" s- j% B" a B+ P2 h) k
} } }
6 a5 E+ Z# l& U- S# I5 D1 E/ _$ O kmain()0 s. g0 k$ |( m: d: ~6 {' |
{int driver,mode;9 }) x' A* g! V
driver=CGA;mode=CGAC0;
2 o2 c. k3 a6 M" r8 A0 Dinitgraph(&driver,&mode,"");$ |/ a0 s/ ^9 _' l& I0 a
setcolor(3);
. @+ ]. q3 [1 i4 C7 w6 ysetbkcolor(GREEN);+ A6 H, S9 X; g; R
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
5 `& g& t# o' s【程序66】( ^ y8 B& v. q1 Y' U0 G
题目:输入3个数a,b,c,按大小顺序输出。
+ X$ O( L3 i& N8 K2 B1.程序分析:利用指针方法。
# h) E8 G" ?; }2.程序源代码:
0 s$ A' m e j% z* o% a; }/*pointer*/5 G- G' v' T( G: n+ o" u1 u
main()/ H- a) I4 s" F9 a. {7 b0 o9 M" u0 ?
{
: [2 h9 t+ r# h: y9 q9 cint n1,n2,n3;
; o+ a3 t Y$ d+ R3 Y# mint *pointer1,*pointer2,*pointer3;
$ y. Y5 Q0 q/ L L6 n) ?printf("please input 3 number:n1,n2,n3:");; P# o" L$ b; Y4 D: _
scanf("%d,%d,%d",&n1,&n2,&n3);
8 i4 _, { G, f. x8 \pointer1=&n1;
" c7 N5 J% U1 o# ?pointer2=&n2;
0 W) b3 V2 i8 Q! ?4 d% Xpointer3=&n3;
$ @- T& u4 S5 `* l; x6 Jif(n1>n2) swap(pointer1,pointer2);: d8 I( ~- E8 h
if(n1>n3) swap(pointer1,pointer3);5 d9 i9 u. {5 M) j8 i- P
if(n2>n3) swap(pointer2,pointer3);+ A; L$ g0 g- B: q0 w8 V h. y
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);* g) D& A' C" O
}
: \+ [$ Q( P- Z, Q& o' {- Aswap(p1,p2), I: s4 T; x: g# q0 e9 L9 A
int *p1,*p2; V X, z* J% n' ]) T
{int p;
8 `5 ^: C; r' F$ I0 xp=*p1;*p1=*p2;*p2=p;
, X& ?; M1 h& E) e+ T- B}6 p* g. @; T7 }$ T. q- l& j$ [