【程序59】) V" Q& b. q( @6 @
题目:画图,综合例子。
8 d8 s( N5 p2 O2 ^9 B1.程序分析:0 w; ?, U/ R+ C0 p$ s2 P, ]
2.程序源代码:
. k ]' S/ [) T- e" P# t# define PAI 3.1415926
% R: C% R1 S* [- {& h, b% N- m# define B 0.809) |3 e/ f1 G# ~6 c9 J
# include "graphics.h"
1 `! ?) ], L0 y4 k* p! C- I#include "math.h"( S% y: g; e: o f/ ^7 `$ g
main()! r+ p/ Z9 o+ J8 D$ N3 z
{
9 }. o; v( | u% p& v$ d5 B% fint i,j,k,x0,y0,x,y,driver,mode;
# R G- w5 _* d% G/ Afloat a;
$ X7 u/ r2 @! P) J/ M# `driver=CGA;mode=CGAC0;; M5 T+ Q+ E' {0 {
initgraph(&driver,&mode,"");7 J& x& F7 t7 }# ~6 ?
setcolor(3);
j) Y& H! x) b% Xsetbkcolor(GREEN);1 |9 ~/ W! Y; {( R E7 N# p5 I/ |
x0=150;y0=100;( }8 x4 ?1 A6 K7 }& u0 r
circle(x0,y0,10);
! y$ o, i# u- E Ccircle(x0,y0,20);5 o _ d- ^) f. P: C3 `
circle(x0,y0,50);" }3 }3 P8 U; \
for(i=0;i<16;i++)
; a" {7 M3 w- X% S0 ^% o{
% v6 q1 H; m! I) ^2 M; g: Y a=(2*PAI/16)*i;
- m6 ^% `. B3 ]; `& V# b x=ceil(x0+48*cos(a));- F5 C) S' j2 z
y=ceil(y0+48*sin(a)*B);
0 h2 ^5 Q/ ^5 g% ~* B+ b2 u- G setcolor(2); line(x0,y0,x,y);}. E; p+ [5 N# B/ a# W( `: R
setcolor(3);circle(x0,y0,60);
8 R3 X$ {# X8 \; b- D- C/* Make 0 time normal size letters */% D$ z* n& S7 Y9 P# j! Y: p2 |
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
: }$ p# e! ]2 j: mouttextxy(10,170,"press a key");
/ E5 V* h$ O; {$ A' T4 G; \/ Dgetch();
3 f C2 g" i0 @+ Q7 |9 \( Gsetfillstyle(HATCH_FILL,YELLOW);
K; {. v' r! T7 [9 F; ufloodfill(202,100,WHITE);
$ N3 V4 g& G6 R# |/ _getch();3 q9 z# ^6 Y z
for(k=0;k<=500;k++)* ]7 p0 _& [( Q( M/ Z
{
L b( |/ S$ i3 Q% t1 A K# U setcolor(3);
2 e" {# v# k) a- \+ @ for(i=0;i<=16;i++)
; m# I/ D1 d9 l; u0 K {# L4 u1 S" |$ X2 X
a=(2*PAI/16)*i+(2*PAI/180)*k;0 f2 g) s3 |' p/ V, a4 H( U, D# S
x=ceil(x0+48*cos(a));6 B6 U: E; {# D/ P0 @# D7 \
y=ceil(y0+48+sin(a)*B);
/ k S1 {# N8 S6 _4 W setcolor(2); line(x0,y0,x,y);8 Q) ^ p1 {, R5 ^9 |3 {$ D# [
}
* f! S% G' p. T9 }1 r6 |( ] for(j=1;j<=50;j++)
; o# m6 j% s$ D& g* x+ h- U3 Q {
8 Z8 [# Q; L0 W+ ] a=(2*PAI/16)*i+(2*PAI/180)*k-1;! K6 D* D/ n1 X& h6 S
x=ceil(x0+48*cos(a));% r w4 I; S6 d8 s8 l3 ~. M
y=ceil(y0+48*sin(a)*B);0 T2 n, D/ U1 Y: H; t
line(x0,y0,x,y);
- b5 x7 D& j3 ?1 Z: Y6 M; w }
9 i8 m; R/ D+ Q" a% B8 w% `$ Y/ q}
# r+ z6 U; f( O: J: lrestorecrtmode();
9 M2 Q) G: q, ]( ^, z}
【程序60】, w9 W5 U6 v" D: ?5 W
题目:画图,综合例子。 - o; |$ c7 W+ H$ f# L8 C- w! Y
1.程序分析:
( V1 }3 ~3 [ L7 A' P' @* t, z2.程序源代码:+ p+ ^! ]9 K" L, L1 |& J
#include "graphics.h"- Q \+ M/ q1 o9 X
#define LEFT 0
& x" u: N$ u6 b; _( R p#define TOP 0' r: X$ I2 R4 \5 R& {% j( Q% A
#define RIGHT 639
+ x' _& X5 |% H" w2 w1 v3 h7 P8 N#define BOTTOM 479
3 k3 A) M2 ?' U/ [#define LINES 400
1 R, X9 @! v& O4 t& |$ [#define MAXCOLOR 15) e& B# q5 m+ w9 \6 O1 v+ p0 |& V
main()* u# c- k' Z2 ^- Q! V0 f Y
{
, g) ^, N+ Y6 a! l3 L Xint driver,mode,error;% u7 [9 S2 Z6 C! h: h
int x1,y1;" G' l d/ }/ V& [: M( y- q' i, Y& v
int x2,y2;# y) I! |. e; ^/ e) O
int dx1,dy1,dx2,dy2,i=1;2 |( t/ {; n9 D p( Q: o
int count=0;8 E8 q& d# r2 @& s- s" j2 q1 _
int color=0;
1 q$ O0 v1 \' L! R6 idriver=VGA;
' v! G8 S* a: M1 O: K6 mmode=VGAHI;
4 A |3 X) t" c, }4 ~$ v; Uinitgraph(&driver,&mode,"");
: u1 G6 i' g9 n! {- Zx1=x2=y1=y2=10;
' K; r; i! t8 Q1 q) pdx1=dy1=2;9 W* ?1 i" \/ E9 I
dx2=dy2=3;. a! S, h$ R8 G# l
while(!kbhit())
* M9 k( A, t0 q! B{, Y }6 d( w7 ?
line(x1,y1,x2,y2);1 r, [4 H9 I0 T; y7 h
x1+=dx1;y1+=dy1;+ S2 Q. B7 A7 I) {; T O. ~
x2+=dx2;y2+dy2;
5 U4 R# F( { `& h if(x1<=LEFT||x1>=RIGHT)
" f. ^, I6 _ f2 b$ @8 ? dx1=-dx1;
6 H: p" Z7 W" S if(y1<=TOP||y1>=BOTTOM)# U5 }: Z, H' h- u3 X3 R' B
dy1=-dy1;
) G2 c; l/ c+ k. M' j8 Q# b if(x2<=LEFT||x2>=RIGHT)
! z5 B2 _5 M& C! V dx2=-dx2;
" Z" z5 M# h) \( r/ Y' x5 B if(y2<=TOP||y2>=BOTTOM)
7 r( `6 H2 d5 b @, v dy2=-dy2;7 A c3 L* B- \
if(++count>LINES)
: G8 d4 J- E* A" G/ z& u {
) e- D1 U. Q/ K+ [. d7 V setcolor(color);8 R5 k, J/ `8 O4 X
color=(color>=MAXCOLOR)?0:++color;
' l" l5 k! f% v1 u+ i }' E" H% @7 E& D: r j4 u& A" T( t: U
}/ y9 P. z) Z4 ^ q
closegraph();
+ H. S$ |2 P: {}
6 y% H9 L! K3 V* V1 q6 j" ~【程序61】
' g& S7 }5 q# I% i题目:打印出杨辉三角形(要求打印出10行如下图)
& _& c. _- c6 J j( N8 p1.程序分析:) h" ?( s9 C/ a9 h
18 O! b0 r3 p* t) o: M( I
1 1
4 H: G% X5 P5 E1 C4 ^ 1 2 1! \& K) R" c. r
1 3 3 1, X. G- [* m( p
1 4 6 4 1
+ G6 f# H/ i2 J+ r 1 5 10 10 5 1 - V2 r! H7 n W# ?4 m5 y
2.程序源代码:6 K+ X6 N6 U0 \7 {
main()% f( O+ J: w3 s9 p
{int i,j;4 u/ ~0 u# A( x* S8 N: r q
int a[10][10];$ E6 Q4 `' N, w# K
printf("\n");
0 W4 l0 d6 P6 @for(i=0;i<10;i++)
% V7 j8 |- N8 i" g {a[0]=1;7 L* I& |' {) J- z' f
a=1;}
3 u& g# b, w3 v( x' K' c( E; zfor(i=2;i<10;i++). P5 @# e# {, x2 G
for(j=1;j<i;j++)8 o4 r& W% [1 |- w# J$ l
a[j]=a[i-1][j-1]+a[i-1][j];
# }" \4 N% P8 ]! R7 i# |2 tfor(i=0;i<10;i++)) h3 b4 D3 S" W
{for(j=0;j<=i;j++)
& G$ b4 l/ j7 j' H+ A( c* w8 C u( H printf("%5d",a[j]);
" F; c! t g8 a7 N* a, ` printf("\n");
0 Y6 k! P" z( R( h }
: q9 s% N$ V# h4 _}
7 |" o" y- |1 D1 e( i+ n& ]+ {
【程序62】: q0 v+ D% v0 ~7 P4 j# M
题目:学习putpixel画点。; G! g% z* k* I9 R, d5 @- }/ V
1.程序分析:
- f' u6 x8 C( J' b# l2 l0 @: R$ B2.程序源代码:
" n7 l3 e$ r+ }8 |#include "stdio.h"& ~1 A4 l7 e$ C% ^ Q) ?
#include "graphics.h"- X" r, e* r. j2 t* U' H
main()
, c6 @6 e( S9 [6 `( p; t; h! q* ^{# g2 }1 D4 B9 N
int i,j,driver=VGA,mode=VGAHI;* s$ {3 D( {2 ?
initgraph(&driver,&mode,"");0 P, u, K. M+ M5 ?3 q! l0 |& c
setbkcolor(YELLOW);$ _! z5 V7 r3 T) D; Z3 \# T- `
for(i=50;i<=230;i+=20)3 l; }: N, d0 z
for(j=50;j<=230;j++)
L( U7 T4 x- r7 g putpixel(i,j,1); K# f6 Y9 T+ y `
for(j=50;j<=230;j+=20); ?. c9 {- j9 Z( c# W/ S
for(i=50;i<=230;i++)3 F6 ` @( \% W6 R
putpixel(i,j,1);
! v" |9 W7 K- u7 R$ W' A: _3 ^}
% R9 u* O2 g% W9 j1 t$ D【程序63】
+ N! K% t$ W8 b5 K9 F2 @题目:画椭圆ellipse
9 k0 i" X' S5 f$ R$ H4 I2 ?1.程序分析:7 l* ]5 Y1 p z3 C/ m
2.程序源代码:+ N/ c" H ~, K% m7 W( ~
#include "stdio.h"
7 u; E- {: H2 a2 s' I+ z+ H: o' v3 ?#include "graphics.h"
2 Z9 O+ S# f) u5 Z/ S: O#include "conio.h"
3 B L# y( i4 jmain()' ^, ]' e! T7 o! Z# t
{6 Z! r1 g8 R. H) w/ b7 W; }& g
int x=360,y=160,driver=VGA,mode=VGAHI;
+ v1 U% |' r+ s: c. fint num=20,i;
2 P, j: H! t. U; xint top,bottom;# I' g+ q! u% y! i, v q, P8 G
initgraph(&driver,&mode,"");) `1 {8 [1 @+ h) c ^
top=y-30;
, ~ {/ v1 o8 o9 ^; j* ]: s3 sbottom=y-30;
1 T3 p$ A# U: h7 o6 U: f, F" lfor(i=0;i<num;i++)0 m! N8 j5 P, w4 j$ P: L- ?" B8 [0 ]
{' q1 @! [* h2 ~" u L9 T) G& Y. |& ~
ellipse(250,250,0,360,top,bottom);
% M0 L& E! z, s5 J3 Z. L Itop-=5;
0 @6 p. ~% A& e7 B! C C! ibottom+=5;( E5 k: R' l& ~ n, D: G
}
4 E+ u `* N" ?4 E) ]) R( d; F( {getch();' a3 e6 o8 U, k* D* _/ O
}
【程序64】
' c6 c0 \' W) u9 i% ~! r& ~题目:利用ellipse and rectangle 画图。
* B! @. C; {) n0 N1.程序分析:
) Z; Q2 {- z! N+ a5 Y' G, n2.程序源代码: G7 X$ ~" Z) r) Z) b, C! }
#include "stdio.h"9 g; ]! i9 P" S/ v
#include "graphics.h"% Q. M$ h6 {8 l) W5 F
#include "conio.h"% F/ W3 `2 N L
main()
* Y2 J; x$ V- L' ]1 e4 ^, y* E& Y1 h{6 g8 c: s9 D9 X7 E2 R' L$ F7 R
int driver=VGA,mode=VGAHI;) N/ |' R0 u) @/ L5 v
int i,num=15,top=50;% ^: k% |% R ?" T7 D6 \; g
int left=20,right=50;
; I0 L# M8 _: p- n" k, |initgraph(&driver,&mode,"");
* K/ |4 o$ \! B, N, _: Ofor(i=0;i<num;i++)
/ E# b. M/ `; l& V T5 J{5 s$ O! R. O' _; k
ellipse(250,250,0,360,right,left);- b) |4 `4 T. l; B" o
ellipse(250,250,0,360,20,top);1 w: ]) y4 F& _ h
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));( U v; A4 d0 C. q3 L- E% P
right+=5;
: ?. {/ ^( |! l [! B# {4 L u9 lleft+=5;4 z! @# s5 `3 d7 G& n3 `
top+=10;
# x; r+ O u8 v2 J5 ^$ a}& c- E3 G9 `7 @! j; q P
getch();2 w/ P! P1 a- w/ u7 T
}
【程序65】
" U; @6 R; r2 U6 w题目:一个最优美的图案。 / F9 U5 C; W2 v- b& J. }
1.程序分析:
' t6 E+ s+ P5 h# U4 q# N. @2.程序源代码:& R& Q$ G; G- ~
#include "graphics.h"4 H+ z" Y0 }- j9 s( F
#include "math.h"( p2 s) p* P7 A& ?. x
#include "dos.h"* G# G5 _3 Q; i5 _
#include "conio.h"
$ j8 z: C7 l8 ^, F( Z1 E* G$ @#include "stdlib.h"
+ [3 T2 D; `% t U$ L+ |#include "stdio.h"
4 ~: A+ ]: m a#include "stdarg.h"8 H1 z. P% r3 W# k- K3 P% Q
#define MAXPTS 15
( S% D- Z! R1 r! X/ x#define PI 3.14159268 _. K% x: o# w. z( U/ ^: I
struct PTS {
& r- x3 a( u0 Kint x,y;, N2 @; V3 U2 K! Z
};2 {6 z2 Q" v0 p0 T) ]
double AspectRatio=0.85;
7 s3 _# U# b$ |6 f5 ]void LineToDemo(void)
. D0 F) m8 F: M4 D- [+ k{
# }5 {/ _0 |0 ]( m! Vstruct viewporttype vp;7 g7 X. S. M, ~4 U( E: v _* s
struct PTS points[MAXPTS];
" p. h& F% `/ t! t. ], u& Qint i, j, h, w, xcenter, ycenter;, e: b; [5 ~2 w% ^: ]# r- W
int radius, angle, step;
' u" v' j: e" `; _) W8 jdouble rads;
- G3 O3 [, V: c+ u7 Q6 Dprintf(" MoveTo / LineTo Demonstration" );# B" O2 Z; S# G9 T8 |2 ~+ y8 v
getviewsettings( &vp );
5 Q# d' a/ C8 S0 W- Xh = vp.bottom - vp.top;
) n# G( ~8 l( n e' Qw = vp.right - vp.left;
0 o' E% p2 S% e0 u* _xcenter = w / 2; /* Determine the center of circle */
! Y7 C% b) L0 q) B. vycenter = h / 2;
# A) O5 ~6 E3 G. S7 Q& Q' Tradius = (h - 30) / (AspectRatio * 2);% B a9 W) u$ |# E9 h w
step = 360 / MAXPTS; /* Determine # of increments */
F9 i+ q! b+ `& Mangle = 0; /* Begin at zero degrees */- G9 d: }& _$ O; ^
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
% _8 \1 u- m# i8 x+ Irads = (double)angle * PI / 180.0; /* Convert angle to radians */( d; I/ E: B2 v0 s; {' \8 d3 Y8 J
points.x = xcenter + (int)( cos(rads) * radius );
' _$ o( P! {' ^' _0 [+ opoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
+ Y3 b5 a/ |( _: Q/ s4 Dangle += step; /* Move to next increment */
- ~8 W- { Y. e7 j7 f}. _& j) L. n6 O/ S! R% l
circle( xcenter, ycenter, radius ); /* Draw bounding circle */9 s0 R3 s; w* t* ~9 D: W6 \1 N! b
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */1 m, M. d$ h( c) n
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */8 X; m/ |) _0 e! W
moveto(points.x, points.y); /* Move to beginning of cord */! v; c, G$ w4 R8 X' h$ m; J
lineto(points[j].x, points[j].y); /* Draw the cord */. m+ w( v: o5 |
} } }
; O( W- W1 [- u2 Pmain()" [4 |2 `. Z' P( `8 I6 N
{int driver,mode;
9 L$ h) `' F* B/ w* q5 ?8 N; Xdriver=CGA;mode=CGAC0;
; B. g! v+ \" B$ d% A- ninitgraph(&driver,&mode,""); y8 | s8 R5 R- b" C" S
setcolor(3);
* u4 {: Q2 D/ k- j fsetbkcolor(GREEN);) I* ]% m; S8 u5 u* f! k
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
; i, f0 J8 R: B2 N' @& E$ H2 c c4 I【程序66】3 @- Q8 X( x5 m& w! C9 H
题目:输入3个数a,b,c,按大小顺序输出。 ) p$ e' @- U8 u8 w7 k
1.程序分析:利用指针方法。
) g! g/ _$ N4 p2.程序源代码:
* U6 s) g) G! d& w/*pointer*/! T* g5 Y0 x' @+ g1 _0 E
main()* C# J |' k( ~" M
{
/ Z2 q( a6 f0 x% J2 t" _9 Tint n1,n2,n3;
; A: ^/ k- s+ q$ E! xint *pointer1,*pointer2,*pointer3;% m4 p2 `( J3 A7 |, |3 A" Z
printf("please input 3 number:n1,n2,n3:");
8 _8 a! E3 d! W! A2 e6 Jscanf("%d,%d,%d",&n1,&n2,&n3);* H: ?* O' p6 `- f4 S
pointer1=&n1;
P) S% B- e1 i+ r4 S5 D L7 ?" ppointer2=&n2;
2 e* ^( Z: a) ~" ipointer3=&n3;: q T( v u0 S0 x" n' z {7 u0 ~3 e
if(n1>n2) swap(pointer1,pointer2);
9 N& U4 H3 f8 w8 E A1 O3 e8 Vif(n1>n3) swap(pointer1,pointer3);
5 V; g6 y8 E( s3 D6 nif(n2>n3) swap(pointer2,pointer3);
$ A" W. y, @. q/ [ Qprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
6 _" p/ ~# v. j; L" x7 B9 ^}
' g: P$ ]/ _" @. ?6 q Dswap(p1,p2)
! m/ ]2 e) [2 @1 x8 K% w" Vint *p1,*p2;
# S, w3 D# F5 t: n{int p;
5 K. @3 ?9 ~" u4 J' `p=*p1;*p1=*p2;*p2=p;! H8 B, z; q- K
}
. O# F1 t, B$ s2 s/ G2 F7 x4 L