【程序59】0 l3 V3 k! q" T9 Y
题目:画图,综合例子。
+ X5 d, W( Y0 Y1 z; `9 ]7 h& K: {1.程序分析:5 Q x @8 Y8 V* A7 _# L; R
2.程序源代码:
/ b6 K7 U: p' o. X* Y& I# define PAI 3.1415926+ m! @ A6 o! _) t4 o1 u, r' R3 i
# define B 0.809
, X* E& N+ k' l9 _* u# include "graphics.h"
3 \/ e% H/ k( C#include "math.h"1 z+ l0 F; l. X7 {
main()
( T, X% V2 c9 Z" b' B7 K{
! c! t! I% l, v* V9 E0 aint i,j,k,x0,y0,x,y,driver,mode;& _3 @% B" \6 P& N
float a;
; b# {2 I) u$ \1 Kdriver=CGA;mode=CGAC0;8 r: `: q3 ^! i/ j- v4 R
initgraph(&driver,&mode,"");4 K1 `0 E1 w! k7 G- u
setcolor(3);/ s/ z: W6 c) f5 r
setbkcolor(GREEN);. K* Y- w% _- m4 G K3 Z
x0=150;y0=100;# b7 P8 T2 d6 v- v4 o0 |$ I% G
circle(x0,y0,10);1 @% U8 {: @, C
circle(x0,y0,20);+ I' ~% V, x4 G% O) C, Y
circle(x0,y0,50);/ H* L! {$ {& y: N" g; ?
for(i=0;i<16;i++)
* R$ ?/ a4 J1 ?/ x2 `{) ~- p; D, n) a) v3 {" F
a=(2*PAI/16)*i;3 W' n" F2 j5 m. H$ ]2 ^" l
x=ceil(x0+48*cos(a));) K0 e# C$ m3 R7 {( N
y=ceil(y0+48*sin(a)*B);
5 ^' y+ I9 s; J: w. d. U; I1 O, x setcolor(2); line(x0,y0,x,y);}
0 e; e0 C4 \' x, Esetcolor(3);circle(x0,y0,60);
4 ~$ C& d1 z% j0 c' Y$ E B/* Make 0 time normal size letters */
7 k9 z. r u( k* ^$ X( z! P& Zsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);7 o2 j6 y: m& {+ n- V
outtextxy(10,170,"press a key");
* M' |, N" Y/ D8 @getch();
/ ~% k- O! y4 c6 C6 Asetfillstyle(HATCH_FILL,YELLOW);' }- d1 r( e& Q& Z
floodfill(202,100,WHITE);
" K* I: m: C* ]# P. X! D$ F; i0 d! a* Rgetch();4 H& R- e7 y" E* q2 \- r+ K
for(k=0;k<=500;k++)
4 N; T! _$ o" c" d9 t" ]{
2 T6 W* v. N; b. }# P6 [' i7 f$ v8 y setcolor(3);
: Y5 q5 Y& k# J2 Y' Q4 S) o% c/ F for(i=0;i<=16;i++)
3 N6 O- A. b: @7 r+ _( ~- ^! j8 w {- E d6 y9 p# f+ D! q: e/ Z" a
a=(2*PAI/16)*i+(2*PAI/180)*k;
& ~! C3 k8 @4 c x=ceil(x0+48*cos(a));
* V) c! {6 F) ~: D2 u1 {* ~ y=ceil(y0+48+sin(a)*B);
; L1 [7 j) e* d9 g setcolor(2); line(x0,y0,x,y);
+ h, b* Q, a5 y, k* y2 Y- { }! q- c8 W4 U% ]2 ^( M
for(j=1;j<=50;j++)
" M# H( p( I. e$ j. C# H; `1 H {
& l0 b6 h- x1 ]9 ]) B6 w/ Y! b a=(2*PAI/16)*i+(2*PAI/180)*k-1;
@2 c( }8 e6 `" W x=ceil(x0+48*cos(a));- e5 t V" Y6 t3 r8 P5 i E3 Y
y=ceil(y0+48*sin(a)*B);
0 Q: a# u9 k& g; f+ H; Z6 ? line(x0,y0,x,y);
% G7 [3 A* s; `% n! G }" F3 _1 G# a. J8 e9 G) N) i
}" t- L7 |) A. m
restorecrtmode();8 S$ Y' U) R5 m0 q, ^
}
【程序60】
, l) G& \1 ?) n/ A( z1 J3 m2 ]题目:画图,综合例子。
! v. {! R5 H# B1.程序分析:% ]7 j, `9 }$ ^+ I* i
2.程序源代码:
) \3 S' }( t6 @; \+ e#include "graphics.h"
0 Q% m, d. R2 w3 `7 \- G: k3 }0 J#define LEFT 0) J3 |( V8 S8 m4 x
#define TOP 0
; f; E! A5 W+ [#define RIGHT 639! P ]7 q$ W7 `2 f9 _
#define BOTTOM 479/ O* m E4 Q6 q* n
#define LINES 400" L! ]* i( O. P3 N: {
#define MAXCOLOR 15
1 l' [- j2 N+ ~3 ?7 |3 Z( U% x' zmain()6 H( H! A/ I8 k* L5 f/ B8 G1 J
{
, y- ~2 ?- h$ N9 b3 j" j* tint driver,mode,error;
' N; h- c( ?% C7 ^: m6 Q" O# R/ cint x1,y1;5 l& p c, w4 z3 A( `1 y
int x2,y2;
) v0 S }1 q! w( O* X0 Zint dx1,dy1,dx2,dy2,i=1;7 P% y/ @5 H) g! B
int count=0;
# }1 U3 _% x3 h$ J) R% s; [ mint color=0;8 i* {: b# r" d/ ~: r- r/ B/ E4 S
driver=VGA;1 S% W2 z4 c/ `5 ~
mode=VGAHI;
; x( B% S+ L" Kinitgraph(&driver,&mode,"");
8 h4 M+ c/ L7 ^) Kx1=x2=y1=y2=10;
/ Y: X; Y/ B$ j: ~dx1=dy1=2;
" B7 K8 U* R# n# _: ?dx2=dy2=3;
6 Q. w$ p" ~0 t$ G0 A7 Twhile(!kbhit())
$ i+ P7 K! ~5 b{
/ G, m7 q& t9 n/ F, T# C' }) ` line(x1,y1,x2,y2);
$ Y4 p4 C+ g: S7 \ x1+=dx1;y1+=dy1;
0 a L: @5 p& ? O: l2 q x2+=dx2;y2+dy2;
" x+ @, G4 t) f8 a5 u if(x1<=LEFT||x1>=RIGHT)
( r: y' Y0 W0 v dx1=-dx1;
0 P" } @1 [0 X. w( l# b4 j if(y1<=TOP||y1>=BOTTOM)
; x4 W5 K/ M" L2 J! m3 w dy1=-dy1;
" ~+ z9 }" v+ K( Y# X1 O$ _: n if(x2<=LEFT||x2>=RIGHT)
7 H* L6 A; K7 T3 ~; Q+ r3 \: g) G dx2=-dx2;& g. P' D1 P4 }6 I# k: e
if(y2<=TOP||y2>=BOTTOM): B& o& p2 O6 ^& d' q
dy2=-dy2;" [# I* Y+ U7 }5 K( }- e$ |
if(++count>LINES)
5 C4 p- p( t. x {
7 ~. r8 K5 P, U6 o. G4 S/ b setcolor(color);+ i9 C4 I( J3 c$ \& ~
color=(color>=MAXCOLOR)?0:++color;- g0 R9 X! Z& l, [) J5 [) x
}
, u* q& s2 a0 Q}
. b1 y* k; f* @8 o$ ^& ~* Mclosegraph();
9 q! Y- z0 f. p7 I* W6 U5 N+ D}
" J3 G" p" Q) M* J" n8 E: c8 W4 R3 C
【程序61】 x( K3 K0 W7 z4 ]7 ?
题目:打印出杨辉三角形(要求打印出10行如下图) v9 x- D+ \' s4 d7 ~+ K7 y+ o$ S
1.程序分析:
. w0 E+ Q1 Z j) a+ N* ] 18 Q+ Y8 L$ ^: C6 s
1 1. c$ T4 y# n* S! J) |8 n( q
1 2 1
% Y2 r: ~( H/ ?& i9 t6 w* @( }4 d 1 3 3 1
+ l) M7 [: v# p$ g) W! ?: @ 1 4 6 4 1. ]/ L7 j( Z" M& C% ~
1 5 10 10 5 1 ( k" a/ t+ t9 J2 |& y- Z3 D
2.程序源代码:, T, ^/ l5 w/ e3 b9 E
main()% c4 E+ ]5 e+ b$ b9 `3 O, _; l
{int i,j;1 K0 ?# ^8 m) k7 F& E0 E" n
int a[10][10];
1 r0 Z1 y4 ~ V/ Hprintf("\n");
1 M! o( G- Z! ?' g" Q$ ffor(i=0;i<10;i++)
8 h5 |* p- V7 H; _ {a[0]=1;2 @; ~9 [ x8 h' d: G* b
a=1;}! `' J7 o& u) R$ _) i+ X
for(i=2;i<10;i++)
7 Y1 y' q) E5 {# L for(j=1;j<i;j++)
7 [; ~+ @$ R* Z a[j]=a[i-1][j-1]+a[i-1][j];
& u6 J7 U3 X0 rfor(i=0;i<10;i++)
1 t5 ?' O% W4 d( R. T0 @ {for(j=0;j<=i;j++)0 ~. V7 O, i+ Q4 G" ^" e: O6 f
printf("%5d",a[j]);( f+ \3 j# d. r7 f2 A- q
printf("\n");. f( `3 d+ [ j! z2 s% S" G
}
, J, m0 _5 s$ [3 ]7 t6 B1 d/ k}
7 W$ u7 s* e; d+ o
【程序62】
& K% y+ \, r* W, A: {题目:学习putpixel画点。
) v+ G* d4 T a' e1.程序分析:
3 A; R K1 ~; n$ K8 G2.程序源代码:
" @8 p8 l8 L& V0 N. q#include "stdio.h"
4 I' E9 R! k: R- I8 p4 d#include "graphics.h"
3 z: X* _) G) I; }main()$ E f& Q; j6 N5 p
{, b( P9 r7 L0 s/ M t; Q& K
int i,j,driver=VGA,mode=VGAHI;( u' @4 B/ t/ c# b
initgraph(&driver,&mode,"");- m) D" n) ~% q! x( O6 Y) f% ] T
setbkcolor(YELLOW);) k; H: c0 j" S3 k
for(i=50;i<=230;i+=20)! `5 ?& V+ y, Q/ y4 p
for(j=50;j<=230;j++)
% g4 p7 ?. U& |3 m5 v; n5 n8 F putpixel(i,j,1);
/ u) l% j" k! ^1 O- x* j0 W4 ~for(j=50;j<=230;j+=20)
1 ]& C4 ~, o* G; v9 r0 B y% X for(i=50;i<=230;i++)
3 r! ^& U4 s& u. N5 {' Z putpixel(i,j,1);
4 Z) p+ n, v' P9 {+ J+ d0 Z}
6 p0 C t' ~2 d. S【程序63】
" J# g7 \, {- S1 [# y题目:画椭圆ellipse / w7 t9 E9 d7 U, l+ i& O% h( R
1.程序分析:6 b' z5 t7 `+ k4 E# O0 V9 M
2.程序源代码:
* p! U$ `- f( Y/ Z3 G* [' n#include "stdio.h"
' `- f) z" `0 ?2 s: }7 b- G#include "graphics.h"/ J4 h/ H: A D+ c4 G
#include "conio.h"' e' q5 `! C" Q5 Z
main()" D( W5 N' |7 P" U' C
{& L: e8 N& o7 K% S
int x=360,y=160,driver=VGA,mode=VGAHI;9 A. G# d4 q4 ]8 O* U
int num=20,i;3 z# @/ j L) N' }7 C. \
int top,bottom;
7 D6 f5 i9 v; `2 yinitgraph(&driver,&mode,"");8 Z( v3 o8 d' n2 ~
top=y-30;' C6 K8 Q( j% C5 p7 E, |( j
bottom=y-30;
; w4 @' j) x' X0 k- xfor(i=0;i<num;i++)' Q& Y9 i- T" v% ^) K
{, ~3 q8 `2 O# d3 Z
ellipse(250,250,0,360,top,bottom);( y0 ~6 N. h; F
top-=5;
- l; a" h# M1 u0 H' mbottom+=5;5 g0 R% O% h2 P- S4 H
}# L) n) Q; p( d( r3 s, _- r7 t5 D
getch();
- ^" B- Q% V7 t Q4 \: d}
【程序64】
0 F2 J/ @6 q$ s" }题目:利用ellipse and rectangle 画图。8 P; Z' _% O) y3 R
1.程序分析:- e) ]& g3 ?- v$ ~
2.程序源代码:
0 S. U/ n5 X* a9 |8 Y; I) f4 x7 O#include "stdio.h"0 L* s! v R! N# @2 h5 B
#include "graphics.h"
8 y% `* G% F8 W/ Z# ~6 N; e0 P#include "conio.h"1 y5 S# D6 }) R6 e0 D x o% ^* v
main()
6 M# j+ Q* E! {9 W4 Z5 l; R{
, G& v; U8 n' r8 d5 C3 v O5 A3 Uint driver=VGA,mode=VGAHI;' s, A0 \( `7 w9 V; ?
int i,num=15,top=50;
7 `8 P* V% z' e7 z K2 Y+ yint left=20,right=50;
3 [3 R% _, t0 H5 ~9 q4 vinitgraph(&driver,&mode,"");
; d" D! @# F2 {+ T0 K4 z8 _for(i=0;i<num;i++)/ X& L) C3 l# A# K! @
{
+ r) W* @( \# y: k) H4 n. C6 k1 X( Iellipse(250,250,0,360,right,left);" k8 v- {& z n$ c9 k! P9 A
ellipse(250,250,0,360,20,top);
- x% v9 H J h9 Z+ srectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));9 Q3 c6 x- z' u+ |4 ~7 ~
right+=5;. c8 O4 a/ |/ ~* i
left+=5;. B& B' C$ }4 r8 |
top+=10;5 t3 y8 F/ ?0 G* J R1 S7 L- J
}7 s: S3 o- d) f; }
getch();
5 H" Z6 c+ W. R- b}
【程序65】
6 X# v" U9 T) [1 }+ J题目:一个最优美的图案。 ) n8 h- j5 }" y, h% ~/ L
1.程序分析:+ s: d7 t0 G+ \" D- S5 o0 X
2.程序源代码:* M& d& \% _) T/ ]: ^. C0 w
#include "graphics.h"
. m9 l/ m$ r# [3 Y9 I! H" C#include "math.h"! u ^ p, m' t3 h7 `: b
#include "dos.h"% ~. U1 g& z _+ u+ k
#include "conio.h"
% W6 f2 V1 \9 V( c+ @#include "stdlib.h"6 v. a4 j0 t1 l3 U2 z
#include "stdio.h") F _4 v# U0 v0 h# }- N: u
#include "stdarg.h"
5 z/ B9 n2 W! r& S6 { ]8 V1 ]#define MAXPTS 15
. O$ B3 S# d, c' c( b# k# g, ?8 \#define PI 3.1415926
+ i2 u9 D3 D# ~$ L$ ?* M5 kstruct PTS {+ u& z5 c8 Z' Y- T
int x,y;
+ W( c) M/ K# C8 W4 A};
( n! f- M7 F$ w- C6 kdouble AspectRatio=0.85;
n# w6 _. G+ H& E. ]5 ~ `6 qvoid LineToDemo(void)
3 P& x* g& Z c2 H{
. i, M+ e/ y7 g7 ?# R4 Cstruct viewporttype vp;
: i! ~7 g3 f3 c: |3 Jstruct PTS points[MAXPTS];
7 h. K3 O4 R( l3 X9 h F5 X1 Uint i, j, h, w, xcenter, ycenter;
+ m: h8 G+ U4 K( j, z! U, hint radius, angle, step;! t" ~, G& a4 ?; ^
double rads;" r& Z& I" w9 c( m& a; E1 o8 K
printf(" MoveTo / LineTo Demonstration" );2 ]* u) u+ _% `7 L
getviewsettings( &vp );
# I; G- c. l- B& w9 w# Eh = vp.bottom - vp.top;
7 p4 x( [$ R6 C# O0 h/ P" Aw = vp.right - vp.left;' d" P. v1 M+ Y
xcenter = w / 2; /* Determine the center of circle */
, R) j# j7 l1 I _ycenter = h / 2;
3 k! h/ f5 m- j( d* O6 tradius = (h - 30) / (AspectRatio * 2);
2 n. X* e1 ]3 Y: i* {4 g8 ?step = 360 / MAXPTS; /* Determine # of increments */
% r3 K% g$ Z. s: |angle = 0; /* Begin at zero degrees */
. u. l. o e; I9 e5 {for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */8 Y# p3 Y) M# W& H5 r$ L5 ?! u
rads = (double)angle * PI / 180.0; /* Convert angle to radians */" v V" H, Q( \1 y. V) o
points.x = xcenter + (int)( cos(rads) * radius );
) I4 p- [' s+ }/ e" vpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );$ R9 m; B6 p/ }) _, x! @6 A2 W# W, _
angle += step; /* Move to next increment */
; b2 k7 b- q$ F- S( x}" R/ C5 X/ Q; x- V& g. R$ }
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
& i1 n& P5 Y5 n% K* v) [7 T. \ x% Hfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
& A4 n- R, c' k5 p Yfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */. Y5 F! t8 {( {8 y' e
moveto(points.x, points.y); /* Move to beginning of cord */1 e8 F) _9 V+ D4 T7 N
lineto(points[j].x, points[j].y); /* Draw the cord */
1 ]6 ?7 x! c/ B( z3 S# }} } }
( Z2 }4 Q1 E5 O2 j4 j$ Pmain()' w: a4 ^7 S! O$ \( H; x
{int driver,mode;
6 ]# l( i& G+ m& w; J: ^! J) e) ndriver=CGA;mode=CGAC0;
5 ]9 v) @2 [" a/ o4 M: \initgraph(&driver,&mode,"");4 R! l4 m. a- P- d, f, i
setcolor(3);
6 Q9 c! Y# B( f) ~& c3 \% psetbkcolor(GREEN);
& e2 U9 p! B0 SLineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
; }- F0 E% A+ K" m; d4 s
【程序66】
0 [! N7 _$ o1 W- e* G题目:输入3个数a,b,c,按大小顺序输出。
3 A' \! Z* `6 A6 b1 S* j6 P1.程序分析:利用指针方法。0 V$ Z# G# M; a3 l
2.程序源代码:$ n6 i- A( ]5 r- G9 Z5 R4 w( K
/*pointer*/; w# w# ?/ E9 K" o% o! l5 L& `( q- U
main()
8 { ~2 y3 _ T! F% z) v/ A3 o{% I' _: k3 u$ j* u
int n1,n2,n3;- l l1 h! C5 g9 ?7 L
int *pointer1,*pointer2,*pointer3;' C9 _3 l, ^) j6 P6 ]
printf("please input 3 number:n1,n2,n3:");
; B2 e5 i+ T1 ^; @* B2 Zscanf("%d,%d,%d",&n1,&n2,&n3);/ s" T$ F T8 ~1 U
pointer1=&n1;- |. b, n8 J B& V
pointer2=&n2;
3 i# I8 \ t( ^4 Z, [% L- P/ b6 N. d3 H8 @pointer3=&n3;: S) R: ~, ^! D4 ~ j3 j% \1 C' Q
if(n1>n2) swap(pointer1,pointer2);$ L. p5 @! U, J' J* _- T
if(n1>n3) swap(pointer1,pointer3);
5 r$ d* [4 M$ f& ?if(n2>n3) swap(pointer2,pointer3);
, }* c$ u7 Z$ Q& G+ ~printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
# K5 ^+ F( l# X! E}* p: ~: ]& Q7 J$ A) h
swap(p1,p2)
; \8 I) M& N0 H z, V; X# f, P- }' kint *p1,*p2;
( U+ s8 Q# v6 h/ w6 v! h l{int p;4 i, w$ H6 X; ~, p# d4 k
p=*p1;*p1=*p2;*p2=p;
4 q* D8 G8 j+ V, }1 J}9 _" M- f* x0 J5 O" S, l