【程序59】6 d) J; O7 z2 g) {
题目:画图,综合例子。
+ L2 |/ U a: p5 |/ e" Y. w) V1.程序分析:
0 w9 L$ g+ f" k1 A2.程序源代码:
/ L: W4 N# O. f" u# t- N3 _# define PAI 3.14159268 `4 _7 T }9 D* h2 K/ f: U( R& I$ _
# define B 0.8095 F. S/ e3 H, \3 s3 V* Z2 p; P
# include "graphics.h"
' w3 |$ `6 ]2 d9 G% H) \#include "math.h"
; }3 ^0 F% g9 T3 }/ Q+ fmain()
+ O, P$ J8 e0 w" _' t+ y6 G{2 E- Y8 L* C5 u2 ^
int i,j,k,x0,y0,x,y,driver,mode;
) Y$ u, T/ |9 B; m3 Bfloat a;) y+ k+ ~- ~' m' J3 w
driver=CGA;mode=CGAC0;
0 V, C2 g2 l. ]6 o$ Yinitgraph(&driver,&mode,"");
9 N# d( p) E6 @! a$ T$ R; d+ p5 usetcolor(3);
1 ]2 O+ ]) S# A9 Wsetbkcolor(GREEN);; s/ e/ z% V0 v- \. z) S$ G2 @ q
x0=150;y0=100;
2 W- r' y2 R/ o; Acircle(x0,y0,10);; N% w; ^, R% H0 W. _( H$ A3 T
circle(x0,y0,20);( q4 E2 j! ?9 y/ p
circle(x0,y0,50);
! U' A4 I+ j8 l2 V nfor(i=0;i<16;i++)
/ h$ p4 n2 Q6 ?0 L, B6 G: o2 P{
G0 g8 g# \! n( o a=(2*PAI/16)*i;- Y" O' r$ j1 z3 x3 j
x=ceil(x0+48*cos(a));* E, `2 y7 `9 r* ]
y=ceil(y0+48*sin(a)*B);& w! Z! Z% H! v" a& T; A
setcolor(2); line(x0,y0,x,y);}
6 t4 t2 [$ j* a+ F2 q5 X6 fsetcolor(3);circle(x0,y0,60);
i8 M1 L4 P5 p1 B$ p# H/* Make 0 time normal size letters */
! y" `7 z/ J. @settextstyle(DEFAULT_FONT,HORIZ_DIR,0);5 K B8 Q! `4 \5 g. `9 M0 h8 W
outtextxy(10,170,"press a key");
- y: W( ] v/ ]2 zgetch();) W5 U" m/ u5 D3 n
setfillstyle(HATCH_FILL,YELLOW);' v$ k9 y6 o. ]& _, C* D
floodfill(202,100,WHITE);6 b7 k+ Y- d2 W. w" V \- Y
getch();
2 J2 N# e3 f; G( | J0 e6 q! Vfor(k=0;k<=500;k++)
4 K( O& y! j; y; {" h; O{
0 P, m7 L* _: d$ r; u- \& S setcolor(3);
" C+ [5 |+ P7 u7 S for(i=0;i<=16;i++)# [% j6 s2 g( l$ n
{
9 K4 H: Z" l" b3 J! S a=(2*PAI/16)*i+(2*PAI/180)*k;
_" S. F" j* A4 Q x=ceil(x0+48*cos(a));, A- r- a- ^3 D7 s7 s" k3 S' E
y=ceil(y0+48+sin(a)*B);, B4 p* ~. V0 q/ R' F5 m2 O& J! w
setcolor(2); line(x0,y0,x,y);* F% b# S$ l+ a# _+ `) k* K
}
9 ~( h: l% |2 q. k7 W for(j=1;j<=50;j++), \4 \) z) e# j/ W! y& s# q3 y
{
4 s- r- j7 `3 M2 ~9 @8 } a=(2*PAI/16)*i+(2*PAI/180)*k-1;1 s& j) A. {; ]7 Q8 L0 ]
x=ceil(x0+48*cos(a));
& u% o3 Q L; M/ a+ X3 G1 k1 p- r# I y=ceil(y0+48*sin(a)*B);4 R5 E$ |3 D" I( `$ i; W
line(x0,y0,x,y);
) r6 N: P; W5 _2 X4 ~! | } o0 q) G; ^; e( ?! X
}1 _: }6 H: f, F7 J" B* w- {, d
restorecrtmode();
- g! W. X/ ~" ?, Q0 \/ V$ u K}
【程序60】
( [: y% F+ a% M! f) ] T7 V( r# c题目:画图,综合例子。
, @1 C+ W) e8 `* I1.程序分析:
* [3 Y j" X" h; |( j# ~; }1 ~2.程序源代码:+ ?8 Q$ k( ^ c6 Z7 Q' k4 q* W& j A
#include "graphics.h"" q5 c \0 f L% [( k- c, \" ?: O
#define LEFT 0: r4 P! V7 t$ A7 M; I* j
#define TOP 0
6 B) O1 ~( ?6 h# n" l#define RIGHT 6393 e! T3 P/ g$ D4 G/ m; ^
#define BOTTOM 479
' V1 ~% x5 }. D# {" c8 P& F#define LINES 400
8 A4 T/ x0 M2 U2 F9 M#define MAXCOLOR 15- P( t# W& k1 f {, Z+ G
main()' s6 g' g2 H# L' D: h: Q5 c
{
& H; R9 d* T* a+ Sint driver,mode,error;0 s/ X. e% a" X5 {
int x1,y1;# c7 f J, C2 W
int x2,y2;
# P4 J) B! g# G* @int dx1,dy1,dx2,dy2,i=1;
+ |; @6 w9 [' O; ?int count=0;% |3 k) z9 ^9 F* K$ X
int color=0;
" L# `3 H- Z' N8 G8 |& a( Mdriver=VGA;
1 I8 t# h& h) {/ K* ?; jmode=VGAHI;) P1 b, o' a4 U7 f# a
initgraph(&driver,&mode,"");- n% ?8 J. |% c+ P/ D! a
x1=x2=y1=y2=10;
6 h# J2 i: X e6 b' k* j& j7 v6 \dx1=dy1=2;
! z& A3 Y7 |+ z$ ?7 n) X7 @dx2=dy2=3;
" o \0 x9 s% ~: cwhile(!kbhit())
/ e9 M9 u4 Y' f{$ Z) f1 i, {/ G$ O. I) C
line(x1,y1,x2,y2);
) \- d1 x' p9 E m x1+=dx1;y1+=dy1;
* {9 [2 d1 I/ z& m! Y x2+=dx2;y2+dy2;' y) s. j% f# t7 N
if(x1<=LEFT||x1>=RIGHT)0 w5 V- p) H, w7 ]$ ^
dx1=-dx1;
8 E* e# `( [. {. o- A if(y1<=TOP||y1>=BOTTOM)* ]* V- ?+ H5 K9 h: m! Q! t( ^
dy1=-dy1;5 ?" I( `, M& }2 S3 V
if(x2<=LEFT||x2>=RIGHT); p, w2 V7 b( o- A: ?2 y
dx2=-dx2;
" v- F& k0 B) U R+ q( y* Y if(y2<=TOP||y2>=BOTTOM)( |. L! J5 Z- z" }# J' h
dy2=-dy2;) U o; F# u0 A: z8 v
if(++count>LINES)2 K2 l$ @- h) T2 g
{
1 v8 p# f# S0 t% `6 f% N setcolor(color);
- q- J8 C& i% G/ W9 h! Q color=(color>=MAXCOLOR)?0:++color;- q) t/ u" Y* Q
}
# k$ w" @$ B, n0 w! v, ]) ?}% b; b; b3 N" u, E( z7 k2 q6 h. y
closegraph();3 L( ^9 t: k1 G; y$ m
}
3 O# U+ j, R6 x0 a' t+ W# Y8 T【程序61】
) p# D8 ?% T. O O! \题目:打印出杨辉三角形(要求打印出10行如下图) 8 D1 o; l! x7 z0 m
1.程序分析:
" L" L3 A* K' Y7 @% j 13 } U1 h. l3 T: M, C- F
1 1: E7 ~; m4 J9 H" ~6 o: N
1 2 1
P8 f p L' b+ N 1 3 3 1
* e8 P. O5 N2 Y5 ] 1 4 6 4 1
7 ?3 P2 g; ^* V8 |( s& y; R 1 5 10 10 5 1 ; t5 V0 s4 L- j4 v) k
2.程序源代码:1 c( u7 C5 H3 Y F6 {" X
main()
( n y7 `( J% C$ V{int i,j;, ~! h* N# M: d) \& w
int a[10][10];
+ q5 Q3 [2 W2 G. ~printf("\n");
# H+ N" {4 ^3 ]' m" g4 O( G( K' wfor(i=0;i<10;i++)# i: B" E( B8 S0 ~& X2 t: N
{a[0]=1;
' ~$ h8 V0 P0 X! s. _ a=1;}
3 }8 \ ^6 T4 _for(i=2;i<10;i++)
# J& L: u1 ^- o0 _0 U for(j=1;j<i;j++)
P3 O3 a; `+ T9 K a[j]=a[i-1][j-1]+a[i-1][j];. m0 [3 Z+ j& i* Q3 j5 H$ I7 _8 V
for(i=0;i<10;i++)
2 m% |7 X1 |& v! l7 X: ?& V! l {for(j=0;j<=i;j++)
? ]: T. G" j4 ] a6 h, @; Y printf("%5d",a[j]);. f7 ?9 S& N A, w1 i0 l% G. V2 W
printf("\n");
5 m4 }) b8 Z; Q. M/ ]0 U }: } W' r9 L, M! ^% f
}
% Z4 w7 I/ \+ q8 a
【程序62】
7 y; p7 O0 X! M, n题目:学习putpixel画点。
& R# M( X4 @. v9 n1.程序分析:
0 t+ E" G9 x0 R* `! h2.程序源代码:
9 @& r3 A" k0 F( ~8 W9 v0 W/ ~9 E#include "stdio.h"3 N' p# \% B: b" m
#include "graphics.h"/ `6 [4 M J) G3 ^0 L) h
main()
3 g" ~) t$ w0 J" h) ?6 P+ F+ p{
0 ]8 p$ g3 o% G& K L/ Bint i,j,driver=VGA,mode=VGAHI;
/ _) K2 V! u6 A9 F2 B& s/ @initgraph(&driver,&mode,"");3 P! Q+ o5 v4 U2 i1 @
setbkcolor(YELLOW);1 _5 l6 E1 i9 }+ |4 B D$ R
for(i=50;i<=230;i+=20) O/ L5 ^% N1 p! _" n
for(j=50;j<=230;j++)" t" R H! ^! o8 ^0 A, r. \
putpixel(i,j,1); X- ?- W' j4 K
for(j=50;j<=230;j+=20)
9 D! s# k- K, Y0 R O2 @/ N* J for(i=50;i<=230;i++)3 L r$ W, I: i: Z" T
putpixel(i,j,1);8 V7 q) J$ `0 V9 ]% C0 N: A
}
3 x* r9 x& R8 k9 g9 { s
【程序63】
9 _+ ]! F. m1 ?1 {题目:画椭圆ellipse 6 u) p1 U# ]4 p- ^. N( o% c
1.程序分析:9 B/ d- x; J6 @) a% a D6 R
2.程序源代码:: |; H2 p+ y3 @) Z
#include "stdio.h"* n/ z5 B! B& T2 ^6 o7 b3 q
#include "graphics.h"7 B( z1 z6 _- ]
#include "conio.h"
6 e# r8 Y7 l( B' Emain()% G: ?# d/ M1 N& X# _; `9 X$ f! Z
{0 g' d7 R( R0 i1 Z( @; S2 f
int x=360,y=160,driver=VGA,mode=VGAHI;
3 g+ q8 F& L8 E1 iint num=20,i; Y0 p$ C4 Q) B" k6 c; Q
int top,bottom;, z; k; Q: l1 p' W) ]
initgraph(&driver,&mode,"");
5 q/ {7 R6 A ?# T/ u& Rtop=y-30; [, q" O3 N- q2 r, Y
bottom=y-30;
# k! Z# J* A" R$ W3 ~0 E# `for(i=0;i<num;i++)4 v8 E5 X' ]7 O6 e! p- N
{
* `9 ]; i0 i& G8 @4 M- R% tellipse(250,250,0,360,top,bottom);
% F& V! O% A6 M R( f0 y+ Z0 n) k! Ptop-=5;. }( S! E; B; p; M5 b2 C' o
bottom+=5;
0 a" Q' a, w$ a1 R6 M}
h5 \4 {6 X9 \1 I2 W7 }getch();
9 ]5 }0 I7 ~0 D% p7 m) [$ Y+ f}
【程序64】5 K, [0 T! ]" L$ @- P
题目:利用ellipse and rectangle 画图。
* Y1 m, l% t x8 g o( u$ Z1.程序分析:5 P: B! I/ \) n' ~7 E
2.程序源代码:
, G' e9 \4 ], J" q1 M! H4 C0 k {#include "stdio.h"
; ^* _6 K- K9 W#include "graphics.h"* e' _2 @6 A; p% j m
#include "conio.h"
' Q" Q" \; d8 Z f' O- a/ cmain()1 y/ ^& L# c2 i# X: A& A
{
$ P- K( ]# w4 X/ f) _int driver=VGA,mode=VGAHI;
. l/ H T0 l. `3 D0 aint i,num=15,top=50;* G5 P7 \. A$ b c2 ]
int left=20,right=50;6 G7 @2 V1 f: n6 p3 G
initgraph(&driver,&mode,"");
5 c4 X7 Q: m) J- b9 gfor(i=0;i<num;i++)
0 g5 C3 q1 B/ h+ ^' `{
: K! y w# L; C% A& Zellipse(250,250,0,360,right,left); W5 y2 y% t$ D7 z" x8 r s
ellipse(250,250,0,360,20,top);
Q. v% f; _' s6 t4 krectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));6 U! A+ s, Z" S, M+ M) K# R6 P
right+=5;& V( \% f' g. Y. L
left+=5;
- @7 n) Z, Z& j$ Rtop+=10;. w' A! m# @) i' k0 }5 R) ^& m
}' d. }0 P& h- y _; X
getch();
. [: P% W4 a% B' y3 e$ H. `( R}
【程序65】
+ @9 S. @! g6 l O$ f" K0 h$ M" p题目:一个最优美的图案。 $ R! s5 p1 D% O" _
1.程序分析:7 P3 S" {/ {5 ~( X+ f, ~, }, k, x0 i
2.程序源代码:
& X- r2 B4 V% w#include "graphics.h"
. s, b- p4 E+ v& \1 s( g$ S, {; V#include "math.h"
$ F7 c! y- g. \) k* b#include "dos.h"
1 [* A. }- x1 @6 y$ ?' a! n0 D#include "conio.h"
& y6 k. h7 U4 P) H% L: H#include "stdlib.h" I1 `- c9 N) C1 Y) ?9 _+ M
#include "stdio.h"
$ x5 R- G+ M5 T8 F+ D#include "stdarg.h"% p( J$ Z9 }% [
#define MAXPTS 154 ~7 I& ?! g+ p. C
#define PI 3.1415926
6 A2 S/ J, i+ Z, q( X( Vstruct PTS {
7 b0 O' w, L7 D' Z0 U/ c Gint x,y;
9 |* ~9 z1 ?5 v0 Y. R};
$ H& d3 \( m$ |; L5 x9 Edouble AspectRatio=0.85;: ]2 ]8 @* \ s/ V4 X
void LineToDemo(void)
4 d9 R5 l; H" k! `2 Y+ U! h{
" o: Z* J' k3 |# x/ u; h, Z% U# U) T8 xstruct viewporttype vp;
5 m) g e: p& s. X( Xstruct PTS points[MAXPTS];" t8 W6 L3 s$ ]+ s1 Z
int i, j, h, w, xcenter, ycenter;
0 Q% O& c( f) W+ Jint radius, angle, step;
* q9 c" A8 U* L7 X( z" }double rads;
l+ ^ W1 \; uprintf(" MoveTo / LineTo Demonstration" );6 t- n1 H7 u3 T: ?9 t$ S9 b
getviewsettings( &vp );
" `& [5 m5 @0 W& dh = vp.bottom - vp.top;
9 r/ \/ ]: a* Ow = vp.right - vp.left;
; R) G1 o/ k! q& Y; f' t- M: }+ k( Sxcenter = w / 2; /* Determine the center of circle */
0 A7 e& `9 K6 B1 ~+ e8 fycenter = h / 2;) E! X2 F1 x# B% x
radius = (h - 30) / (AspectRatio * 2);% o0 e4 k5 l% m* v% z" F( ?
step = 360 / MAXPTS; /* Determine # of increments */
( }- e8 h# G7 c8 Qangle = 0; /* Begin at zero degrees */
3 a- c6 S4 m5 l6 {( Q3 Gfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
/ ]) n- a \; [* d1 V4 krads = (double)angle * PI / 180.0; /* Convert angle to radians */
, r- s3 `2 M+ b5 v' N" K. M/ Ypoints.x = xcenter + (int)( cos(rads) * radius );# j+ K m0 r0 p0 N* y: D
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );1 e7 e, N5 }+ _7 U: J
angle += step; /* Move to next increment */5 J* f6 \7 S& ^ s
}
5 S2 E& B% p) m$ `) pcircle( xcenter, ycenter, radius ); /* Draw bounding circle */
& u8 P. U7 b8 C0 H$ o3 _for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
# X0 y+ w& W% c7 D6 x8 a* E4 ~) |for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
% E+ G% ]- s8 n( ]7 b1 d; Wmoveto(points.x, points.y); /* Move to beginning of cord */* z4 [3 B6 |5 {4 o
lineto(points[j].x, points[j].y); /* Draw the cord */
( z6 D. I# h7 ? u2 K! Q} } }- C* u8 J3 d4 Z/ {4 v: |5 x0 B
main(), D1 f: k2 h1 C i& W3 W+ M
{int driver,mode;
0 L* W. ^$ I3 z. C k: ]' ldriver=CGA;mode=CGAC0;: R5 a) q& ]; @/ F; Z; f
initgraph(&driver,&mode,"");
3 J3 T2 w. Y! I$ z5 I, Jsetcolor(3);
@6 R& `" S4 \; x2 V* isetbkcolor(GREEN);
1 x m9 ?9 q1 wLineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
" }% t; B6 v+ a8 c( m# F+ I【程序66】, L" p2 l7 s6 M+ r; H, {3 i
题目:输入3个数a,b,c,按大小顺序输出。 5 O3 Y1 a1 y# v
1.程序分析:利用指针方法。
; C% {5 v5 g$ i7 h! K5 C: N& M" c0 o2.程序源代码:! Y+ M6 O/ K3 S( R$ g; d1 y
/*pointer*/6 V' h3 B7 l9 K$ P0 [
main()
! C: f8 d$ y' x& O: T6 z/ ]{
3 c! L! p" P0 K7 N% B+ `int n1,n2,n3;
' P/ W( F4 G; R$ k7 Sint *pointer1,*pointer2,*pointer3;
1 B/ n0 t# [5 W/ p2 o; j" |) Lprintf("please input 3 number:n1,n2,n3:");
1 O1 y+ l) S- d" i; e' k- lscanf("%d,%d,%d",&n1,&n2,&n3);1 V: T9 o) N; t7 c) o
pointer1=&n1;
2 Y0 I7 O( ^) }. b1 O3 mpointer2=&n2;
0 |& S% d3 ?6 R7 A0 @pointer3=&n3;4 Y( d1 e8 O$ l( C0 ?+ x' C
if(n1>n2) swap(pointer1,pointer2);" S" Q% p d5 A
if(n1>n3) swap(pointer1,pointer3);' n! F" z6 T L0 b: k% P1 L6 P) E6 \
if(n2>n3) swap(pointer2,pointer3);" t- s& D$ `. A' Z% _6 D" P1 L4 M
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);' N7 {% S4 N& y5 A" D2 t7 Z
}4 O& f$ n% P/ [: t
swap(p1,p2)
% G7 X, ` o! v8 p4 y: X3 P" |! Pint *p1,*p2;
1 ^. w3 T, Z! w0 c' |/ H# m4 ~$ ]{int p;
0 }3 _6 m9 f- d: L5 Np=*p1;*p1=*p2;*p2=p;
- I+ {2 G! }; l" J1 c4 R/ w6 T* t}
1 {! m. |0 x& _1 b