【程序59】$ R, g/ m. V8 B% F6 \' v* |
题目:画图,综合例子。3 T% x' t( o: i% c) h% V2 e, j; g
1.程序分析:
6 H7 R; ?" c% w2.程序源代码:/ t5 k: i+ D2 V: s
# define PAI 3.1415926
) }; z! Z d! ^1 H- |# define B 0.809( |3 i% L% d$ w1 \6 t
# include "graphics.h": h0 t! i# v7 J0 S: B7 H% u
#include "math.h"3 y; P9 U% S; Y$ g3 B
main()
, A2 K! p: |: a- p{
8 N( x- `5 {, s5 Sint i,j,k,x0,y0,x,y,driver,mode;& B. q+ C# U5 E6 C# h/ A+ m3 |
float a;4 p/ W7 I9 [4 ?1 i! f# Q
driver=CGA;mode=CGAC0;8 j) m' J9 A! x6 Z& q8 J
initgraph(&driver,&mode,"");
- v" ]. q& |' v( A9 Gsetcolor(3);
' O+ ^6 D. [! a4 O c4 ?# S6 asetbkcolor(GREEN);
) s% l' N& ], h/ u* v# z5 ux0=150;y0=100;
+ }' _: z4 r/ G& j$ l( ecircle(x0,y0,10);
& d2 }1 S; g: [3 ?7 t9 ?: h5 ?8 w) B Hcircle(x0,y0,20);
# _/ R% `% I0 e, W4 [circle(x0,y0,50);" p6 ^$ v) J3 q* n1 D9 P, w0 o$ u1 r
for(i=0;i<16;i++)0 U8 I5 A" k! h5 l
{
" w0 K q/ i. C$ J6 I+ L. Y! n a=(2*PAI/16)*i;
$ F8 [! F; ^1 V: i q5 M" M# Q7 J x=ceil(x0+48*cos(a));5 c. M5 e$ n' Y
y=ceil(y0+48*sin(a)*B);
5 G1 i* K# p' h. ~/ |# v setcolor(2); line(x0,y0,x,y);}
K% ]3 T7 g! q% `setcolor(3);circle(x0,y0,60);8 {4 e- q, a% d) \+ G7 d+ ~. u
/* Make 0 time normal size letters */
8 F9 \& Q9 f& x, g) U" Esettextstyle(DEFAULT_FONT,HORIZ_DIR,0);7 [4 U# }; F, p# x2 a2 e0 C5 N
outtextxy(10,170,"press a key");
$ z5 v$ [9 O8 p& D2 g. L0 O6 Rgetch();
1 q; J) |; J+ U: u; i' p5 g" J; f1 Psetfillstyle(HATCH_FILL,YELLOW);
* p r' f6 z# v5 s0 R" Z' Y- Efloodfill(202,100,WHITE);
- _$ b6 x$ F. b( @) e: fgetch();1 |+ [! P8 ^! V6 F: A
for(k=0;k<=500;k++)
* _4 [0 H. w# w1 k- X' v8 Y{
8 q9 u- C+ G v4 o0 p ^ setcolor(3);! E' f5 W/ K. S. q: O: \
for(i=0;i<=16;i++)
8 B _2 I1 q4 v/ o3 K. J {
# X' b5 G4 I0 Z a=(2*PAI/16)*i+(2*PAI/180)*k;* V2 U! V2 k6 D$ V' l: Y
x=ceil(x0+48*cos(a));
$ c9 P# D* ?4 j9 a3 k/ x y=ceil(y0+48+sin(a)*B);
; b1 t' X8 S r2 E' ]+ f- P% z5 ] setcolor(2); line(x0,y0,x,y);" C% w' L s7 @
}8 a7 u2 Y2 ]% a5 D
for(j=1;j<=50;j++)
- J1 u2 y: x2 s2 @ {7 M4 z- B/ l/ c) n q2 J
a=(2*PAI/16)*i+(2*PAI/180)*k-1;: s4 ~! T2 L, D: @: v6 [
x=ceil(x0+48*cos(a));
' f, |# K( p1 e1 T. H9 _0 b. g* v4 q y=ceil(y0+48*sin(a)*B);
4 X/ ?" w. a( K0 r) N2 \" v line(x0,y0,x,y);$ ]0 `9 u2 S( u3 N: _/ d' B) ^- x9 Z( ?
}
- |# I+ Q7 i& j8 `# V' S$ h}
3 Y# W) F) L l8 s+ D$ e. Erestorecrtmode();* R/ o: a) e2 p' d( t- ^$ Y, _
}
【程序60】2 W& q' Q0 W4 e1 [$ `
题目:画图,综合例子。 $ g- x+ c) S' L. ] K9 I9 p+ Z
1.程序分析:" P& y& K5 j, |
2.程序源代码:
P3 T7 w5 g9 ~#include "graphics.h"
- a) I) w4 n/ u# E#define LEFT 0
2 l0 J ^) i1 z( }: \; o$ f9 k2 s9 Z! U1 d#define TOP 0% a6 f% n9 x+ X5 T/ e
#define RIGHT 6399 l. V/ I& G% w7 c* _' g7 Z
#define BOTTOM 479# }3 W# j" }3 Y6 |! A) n7 K& L
#define LINES 400
' O4 H- ]3 e! Q$ L/ ]#define MAXCOLOR 156 X0 `. v7 t# y2 U/ g0 _3 g5 ~9 q
main()
7 x# V* I+ q* L. y; u{
8 G# p7 i- J; F9 A8 z- }! m: K% ?5 aint driver,mode,error;( |* ~- L4 K% n, N0 N7 I
int x1,y1;
+ C" x! s3 @* x6 ^' \; ]int x2,y2;
9 a( S/ I. v; q/ Gint dx1,dy1,dx2,dy2,i=1;" E- @* l+ v! ]5 _9 @! d
int count=0;" _$ n# A3 E8 M1 c
int color=0;
: z* P) P, R- b+ gdriver=VGA;/ O/ j( i2 z7 z( y
mode=VGAHI;( e% _+ M3 d3 H9 _! W5 X) W( W
initgraph(&driver,&mode,"");
1 B4 @# x& U# \* z* }' Ax1=x2=y1=y2=10;
) }! B. Y* k @! B5 G4 Pdx1=dy1=2;
. O, x& K3 I, q: x# Z+ S8 pdx2=dy2=3;. a, x7 M. z" o& J
while(!kbhit())
$ ~8 g/ S8 ^* a' S s( ~& d/ {4 M. F{% g% D% }; [$ E
line(x1,y1,x2,y2);
; y: Z: @# {( Z7 S: D. f; J x1+=dx1;y1+=dy1;
4 O8 y, m& k. i9 b/ t x2+=dx2;y2+dy2;
+ f- S% V7 Y2 {) a& }# B if(x1<=LEFT||x1>=RIGHT)' }- x% p# J K
dx1=-dx1;
; e9 K# W+ H) J, _9 U if(y1<=TOP||y1>=BOTTOM)5 E2 I, t! |& ~/ M3 o
dy1=-dy1; o9 m( Y+ J) q! D N) s: a( T
if(x2<=LEFT||x2>=RIGHT)
5 i1 S! r$ B$ V( U+ Y dx2=-dx2;2 z$ r0 P: s5 K8 y
if(y2<=TOP||y2>=BOTTOM)
- B9 H0 C+ x; v( W" m2 m5 X4 y8 f( q% m dy2=-dy2;$ O: E- e* r; a$ X& w/ k
if(++count>LINES)% f2 P1 Z0 K; g! m
{
3 |& a+ P, u% B4 O5 P setcolor(color);+ v8 p B" g1 c
color=(color>=MAXCOLOR)?0:++color;7 ]3 m1 s- a( A0 V& d Z- S( u) d
}; \: ?6 q+ J3 G. d% a( O
}7 Q9 w) j. D! ~% X6 u& D
closegraph();+ K- s) f2 m/ |8 D. ^" p- \
}
Z3 r+ A: Q" U! M
【程序61】
" R& D1 {# N4 P" }9 k; T" E题目:打印出杨辉三角形(要求打印出10行如下图)
/ R& j( H: J( ]& W' h) m1.程序分析:
B: b( I+ |9 Q 1
. t; l. [: U& U) v 1 1
% ~& X( V7 W" G0 E2 O 1 2 19 G# V! u$ k5 G+ G+ ]" o6 Z5 G
1 3 3 1
+ V- Y" j# n3 z( V" P& Y" g" O 1 4 6 4 1& R$ _1 O4 s" U! h& \; y5 y
1 5 10 10 5 1 / E( q& p5 p' ?1 L$ O- x3 f A
2.程序源代码:4 P; E+ R, q* y. Y: O& M9 X
main()! U1 d0 F' m7 X) @6 \8 j* ^
{int i,j;
* u8 W; r {. w* _* n% Gint a[10][10];/ B* U4 H) C4 J# @
printf("\n");; X$ p" u# y% i6 [2 Q
for(i=0;i<10;i++)
' a* \# e9 L( F3 v# L& r K {a[0]=1;
4 h) T. d& z6 Z5 G3 J( ^% P& l a=1;}$ u/ U. z9 }/ A+ w
for(i=2;i<10;i++)
+ _2 S$ V1 ^9 @! x: i, |3 A for(j=1;j<i;j++)$ n1 ~' H* r0 Q
a[j]=a[i-1][j-1]+a[i-1][j];
1 s& J# N5 Q: j& ?for(i=0;i<10;i++)
6 H3 S p; q/ v7 W0 Z6 P/ ^* M {for(j=0;j<=i;j++)( L, |6 |- `) B8 Q2 Z5 N" P
printf("%5d",a[j]);
, d: L3 \3 `. q printf("\n");
$ n( U8 I5 Q# {/ \& E5 @/ V }! d9 L7 C8 R8 g( L5 r- f z2 s; b
}
- B! h. |& U* [5 d" f【程序62】2 S2 m$ u/ J. x7 s4 }7 q( s
题目:学习putpixel画点。
- h! }/ a: y9 L- W- L$ I0 K1.程序分析:
: w7 O$ z& l! G9 M2.程序源代码:# P3 R+ C0 ~: T1 v* o
#include "stdio.h"; ~, m, K4 Q3 r1 |
#include "graphics.h"
/ `' ]3 ?, y. ymain()) j: m" g; b2 b- K+ r$ \
{0 A3 }$ i; r: U, |9 N2 k1 d
int i,j,driver=VGA,mode=VGAHI;
3 z5 @% x' L1 m( J* d1 }initgraph(&driver,&mode,"");
3 V. Z9 t: j4 F, l1 g& `6 w! Esetbkcolor(YELLOW);
) Z( O1 D% S& W& y% B4 l. x9 lfor(i=50;i<=230;i+=20)
; T6 n" U% A, \' m) H$ g for(j=50;j<=230;j++)
: c/ r5 c" f+ t/ w5 J putpixel(i,j,1);
+ H1 E6 S* p+ W. H* ^for(j=50;j<=230;j+=20)
( @/ C$ k! R+ C h9 u+ z for(i=50;i<=230;i++)
# r3 n/ c$ @5 c( Z+ a3 }. X, y# w% a putpixel(i,j,1);
" k/ V8 o" ?) U2 Y6 J}
& j6 G' |# s' ^2 J
【程序63】
" p4 f5 T; s2 E. E题目:画椭圆ellipse ) I3 q- z2 r) t/ ]0 D k2 v: H; P
1.程序分析:
! K$ J Q2 c4 A3 k% }2.程序源代码:9 [0 i, H" N: X0 y2 ]0 t4 a
#include "stdio.h"( d3 `4 O6 ^2 [5 { V
#include "graphics.h"
4 E, G7 y( p" m0 Y/ {# V! ~( A& `- z#include "conio.h"+ K, r N n& Z! I6 P6 z5 b7 n
main()$ F+ L$ W# |, w/ w3 r
{! e* ]# T5 d8 j1 c" O# ?5 q' ]
int x=360,y=160,driver=VGA,mode=VGAHI;
x/ @8 y" f4 J' {( iint num=20,i;
2 V: \+ E# t3 W# Mint top,bottom;
9 D/ i" J5 j8 R- M9 y$ \8 L. [# ^6 e! linitgraph(&driver,&mode,"");6 s' f; U( P- B8 Z/ ~& A
top=y-30;: ?: @" i. q1 }4 s9 A) r+ o* E: P
bottom=y-30;
: @* j# h$ N& k+ g2 Efor(i=0;i<num;i++)0 F4 `2 n+ b5 K O* v1 f
{) d9 g0 Z: T6 `+ ]$ d2 A$ D
ellipse(250,250,0,360,top,bottom);& D0 l8 f% o; }$ Y
top-=5;; ?. T4 a, r' P' w$ W4 @* \% v' s3 e
bottom+=5;
4 z; l1 u. ]6 U; w9 G- O}; u' l: b% s8 U
getch();
# J$ x& W& h7 d N6 ?) j}
【程序64】5 N8 \" u+ ^% |1 A. \. L
题目:利用ellipse and rectangle 画图。
7 V6 ?8 e9 y% H1 H; z1.程序分析:
! o8 ?1 b! q C9 a2.程序源代码:- C$ q( B" `: G ?: m# G1 ]2 b% o
#include "stdio.h"
$ n1 b/ }% }$ q/ g0 b#include "graphics.h"9 R0 f- F: f" q+ X
#include "conio.h"6 M1 r* _4 E1 G$ p
main()
8 d3 f2 h% o' d0 v7 G; [0 [{
3 g$ A8 [/ A P2 L; b& n. aint driver=VGA,mode=VGAHI;. L! X5 _1 o+ [
int i,num=15,top=50;0 B9 Z! G* M& \% K6 K( f$ b
int left=20,right=50;
. c$ @* o6 \2 Y3 C7 a Iinitgraph(&driver,&mode,"");- \3 _# C+ w* ^6 p; O$ L- \( Z
for(i=0;i<num;i++)
+ E+ r1 t6 i# n; f$ N2 R2 G, Q+ h* N% j{
/ s6 n! t* r9 l* [ R) R6 vellipse(250,250,0,360,right,left);
0 e' Z- l ~* R1 z5 vellipse(250,250,0,360,20,top);8 U& H* W6 ?" ]+ T% x, P' D
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
8 ]4 j+ C5 R: b* U) O! g# ^right+=5;
8 k l- X2 ^: L$ x5 [$ {/ `left+=5;
% c- P4 H# ?) J- utop+=10;! J0 ^% V+ J2 w5 C8 f3 O! |/ Z5 j
}% R9 R& o0 |0 ~/ `
getch();5 U U5 @: I- I& u6 @2 P
}
【程序65】7 }+ j. {0 I% a6 F" T
题目:一个最优美的图案。 ! T2 e2 M# B9 @1 b+ V/ Q( S4 m
1.程序分析:( q7 o1 |: O" u7 W( g
2.程序源代码:( N" i4 q/ n$ q c" T
#include "graphics.h", J( V+ B3 P* B2 G" A+ P# M. r+ R. m
#include "math.h"6 `3 K0 n* Y$ a$ Z( ~5 I
#include "dos.h"3 U: h* d+ J: G/ j
#include "conio.h"! J8 g* x6 r [: h
#include "stdlib.h". A) t# V* x5 s. O$ W7 C/ v% p
#include "stdio.h"- b) D. ?1 a+ [7 b
#include "stdarg.h"
5 J4 i0 n$ W- v2 @#define MAXPTS 152 K+ j) ]8 w2 }) ~2 U" I
#define PI 3.1415926
& D+ S/ j( Z7 hstruct PTS {
7 Y& d" N% R" i' F: x( c+ Z6 e+ _int x,y;
9 W1 ] I" n, v8 L' Y, D* m9 w3 l};
; B, ]9 i7 ]1 H. A; w3 Ddouble AspectRatio=0.85;
4 _; W9 v: M1 M& Z, N( Vvoid LineToDemo(void)- ^+ g" Q9 y, [2 C2 e
{+ l2 e0 G6 j( h
struct viewporttype vp;" q0 m& B5 c; R" a% S0 L5 y+ |
struct PTS points[MAXPTS];; L+ [+ U, K& ]4 P" S
int i, j, h, w, xcenter, ycenter;
a8 J; @6 H# g4 z/ T& ?int radius, angle, step;! ^5 t$ ]1 h/ E) T1 D
double rads;
2 i# r ~; j8 O* bprintf(" MoveTo / LineTo Demonstration" );
6 l$ V8 z- F- ^0 H# [1 H6 ogetviewsettings( &vp );
* p8 r' b+ d5 Jh = vp.bottom - vp.top;
3 @( i; Z, M6 Q- ?" t iw = vp.right - vp.left;
4 m# n8 ]0 M4 q: }; D) ~7 u* Fxcenter = w / 2; /* Determine the center of circle */
5 `0 C5 R6 u) T/ G0 eycenter = h / 2;+ x" v7 G# I1 S8 S
radius = (h - 30) / (AspectRatio * 2);- o" |( @* I; j7 H
step = 360 / MAXPTS; /* Determine # of increments */
1 Y/ ?- H* M) i, `, K9 Qangle = 0; /* Begin at zero degrees */
/ f# Z! \: ?0 o2 W8 ~5 Nfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
) m* l4 w& T" x: G% Zrads = (double)angle * PI / 180.0; /* Convert angle to radians */' P0 l6 s/ y! B& n6 ?! f1 e% ^
points.x = xcenter + (int)( cos(rads) * radius );
; a2 p+ k' h' Y s hpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );& b" ^- O( `% G5 o! C' b$ A
angle += step; /* Move to next increment */5 I4 Y( V# n6 }. o, L3 v
}" `; P9 w5 \" W$ a& w- l: K& i
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
1 l% F/ s5 M6 l9 ~; Tfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */, i6 F; B7 w: |2 [3 M
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */, h! ^3 C" I! U
moveto(points.x, points.y); /* Move to beginning of cord */- r7 p) g$ P' a. S4 [
lineto(points[j].x, points[j].y); /* Draw the cord */* m, w& o. P+ C' k
} } }
- J8 Q$ ^- f; }0 J8 wmain()
/ m. }, k% a6 ~& X: v{int driver,mode;
5 N. c j$ ]! L0 S" Mdriver=CGA;mode=CGAC0;) C* }, U# w. c: m) u- B1 _9 U5 v$ L
initgraph(&driver,&mode,"");3 l, d" k$ \2 `
setcolor(3);
4 y" r/ h$ I' {, ?+ zsetbkcolor(GREEN);
y- l7 p! z% e/ R" f, L" |* iLineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
! W8 R2 `; O3 Z$ ]
【程序66】; E. ~! z6 [6 x+ S! U) g* o: r( p, F, J
题目:输入3个数a,b,c,按大小顺序输出。
5 _$ e6 v% S2 A* k- m4 m# o8 ?1.程序分析:利用指针方法。- F4 P; o& n# l. K2 C! E) H0 ~
2.程序源代码:5 I1 n7 L* ]- T% e
/*pointer*/. {1 B; u& e: _1 \% [9 ~: S
main()" h0 C* l% c! M
{% \& n3 ^. E7 Y. ^
int n1,n2,n3;: [6 y! G5 S5 }; E: n, C- @
int *pointer1,*pointer2,*pointer3;
# w* i$ E* n5 p# ?. Z6 aprintf("please input 3 number:n1,n2,n3:");! J2 l; M. j' Q8 A3 ]- w
scanf("%d,%d,%d",&n1,&n2,&n3);
, [- ]* f: _, v; B Opointer1=&n1;& V4 n* m5 T9 s+ q
pointer2=&n2;' K( v% v. R( O" K' A/ J
pointer3=&n3;3 U/ F2 \+ `9 f" N6 g3 a
if(n1>n2) swap(pointer1,pointer2);2 q$ Y+ H+ a' y1 q" j# ?
if(n1>n3) swap(pointer1,pointer3);
1 e0 {6 Z' [9 `4 p: |if(n2>n3) swap(pointer2,pointer3);
$ _/ C1 S* u- cprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);9 o$ d+ n) N; K9 f. T. N3 I
}
* ^ Y# ?* U [swap(p1,p2)
, u0 u9 ?6 l% w: o4 t, U7 Fint *p1,*p2;
+ o# @6 b* ^5 W3 y3 y8 y{int p;
1 s2 I$ a( y4 _! F& b4 U# Vp=*p1;*p1=*p2;*p2=p;; ?9 o( {, T. }- P
}$ m. c; A% W. o$ X