【程序59】6 P1 F- C" t% i& m! b5 a9 o
题目:画图,综合例子。$ J, S8 f. h. B$ |; P1 X0 Y
1.程序分析:
: [ ^& M& m+ M8 t2.程序源代码:
+ j! C/ ]/ g0 K5 v8 C, {( K# define PAI 3.1415926
0 g1 e4 T/ d# x# define B 0.809* A) P7 I0 H; z& W& H1 r- i
# include "graphics.h"
% d, G; J! u. T* ^: X$ X# f#include "math.h"
3 v; p# j3 D: A/ m1 jmain()
/ _: C0 y0 c+ F( p# y6 P{$ j! Y! D- N9 m9 |! Y
int i,j,k,x0,y0,x,y,driver,mode;3 m7 @2 e+ s$ I+ m( D9 N
float a;
4 o9 q$ ~' w# U' ?8 |driver=CGA;mode=CGAC0;; y* i& G- Z9 r$ r% k8 W
initgraph(&driver,&mode,"");
) J: e, `5 Y9 B) {$ A. X8 Isetcolor(3);3 c) [5 J! {7 V4 |2 S, N% k4 l
setbkcolor(GREEN);% S# i0 N4 R5 G
x0=150;y0=100;
9 m0 [5 U. q( H& g$ ^. Pcircle(x0,y0,10);9 N7 |, S6 j8 I9 n: x
circle(x0,y0,20);
[) s4 s% r+ N3 j* B! l5 q8 xcircle(x0,y0,50);
' B" f* K8 a/ @2 _ _for(i=0;i<16;i++)
0 ` R6 c9 J' L0 @5 }{% e) p) ^* Z X
a=(2*PAI/16)*i;
7 a S& E! I% F$ J& C) ]! z/ b% b x=ceil(x0+48*cos(a));7 k- G$ W/ ?! S+ V
y=ceil(y0+48*sin(a)*B);
0 j7 r- Q' i, f1 I { setcolor(2); line(x0,y0,x,y);}- h$ t4 s; ? J( B
setcolor(3);circle(x0,y0,60);8 X" U8 W! B7 h; s
/* Make 0 time normal size letters */
: p0 F' T1 S& }: x* e6 q. _settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
- I9 }: e6 l+ M9 e9 Uouttextxy(10,170,"press a key");4 Q& O/ t& l2 C; E
getch();
! b k7 D8 u+ ?" C/ O- Ssetfillstyle(HATCH_FILL,YELLOW);' m' {( N* v& B* z8 A6 i& x# |7 J
floodfill(202,100,WHITE);/ |, U; q* q( ?& X8 z
getch();% F1 P7 j* w4 ~9 _" h2 Y
for(k=0;k<=500;k++)
: K+ B& I0 }' S1 ]% C4 J{
( ~% F6 N' K, ] setcolor(3);
: ?! {/ Q7 |3 B/ q7 b r for(i=0;i<=16;i++)
: ?. n4 ?( {2 l0 a {- }( g# {3 ?6 y5 y
a=(2*PAI/16)*i+(2*PAI/180)*k;, w9 w' N* v5 z- U7 x u
x=ceil(x0+48*cos(a));7 D+ f; ], ^! i( L( H8 j
y=ceil(y0+48+sin(a)*B);3 N# Z: |1 A: ]
setcolor(2); line(x0,y0,x,y);
5 w& N' \% v3 u( d }! g; v# {) O" T2 N+ i
for(j=1;j<=50;j++)
$ c4 ~ {# ?: o6 V; m3 R {( ~% u' C! i" }" y$ k
a=(2*PAI/16)*i+(2*PAI/180)*k-1;. ^7 {6 Z6 G/ g0 t1 n0 h- M
x=ceil(x0+48*cos(a));
$ M+ q/ b8 J: b( o" L y=ceil(y0+48*sin(a)*B);
6 g3 e8 w$ n5 H. F) i+ j& t line(x0,y0,x,y);$ L8 {4 }# F+ O
}
2 F/ q) R+ r8 @8 ?" @ J/ ^0 H}: V* \) \2 f2 f
restorecrtmode();4 W% n" P$ ?( f" R3 J2 b8 I6 I
}
【程序60】$ C& v( P3 Z1 h: j/ B3 Q9 t
题目:画图,综合例子。
: v& @, w. @" H) A( R1.程序分析: C( P+ k8 K( U: ?
2.程序源代码:
* _4 v3 Y/ R/ s ^/ a8 a#include "graphics.h"
0 O% c7 Z! x/ S4 g4 v#define LEFT 08 R, w" O; U$ D8 Q
#define TOP 0
9 Q4 j2 q" N7 N0 B2 }6 Q#define RIGHT 639- ]% V) n) U2 q( \8 ?6 F
#define BOTTOM 479
L' w6 Q4 ~6 |( N#define LINES 400" m/ k/ e. c- V3 {* L
#define MAXCOLOR 15( q6 f- Y, S, b9 p: [5 W
main()- A9 i) Y6 e/ \
{& N: J- _& P' p
int driver,mode,error;- @, g) X2 D% I) h( g2 W0 L6 |
int x1,y1;
6 _) E$ p0 W( ]2 T, o0 J3 O( }int x2,y2;
8 j) J; t3 G9 m; Fint dx1,dy1,dx2,dy2,i=1;, O. G4 r- d4 R' C& ^
int count=0;! @) K7 \# P9 Z& o
int color=0;
, r0 E' ~: a. r3 H$ m: t Sdriver=VGA;# E1 K; `" E8 F
mode=VGAHI;8 ~" c7 [' ?1 L1 j
initgraph(&driver,&mode,"");
) }& B" { j/ r/ q% r9 `& ix1=x2=y1=y2=10;
4 @$ F; b+ G- Z% O- L8 ldx1=dy1=2;
; V4 f) j( k& R3 k7 [$ \/ Gdx2=dy2=3;$ Q& W5 z* d. C1 c/ ~
while(!kbhit())
/ `0 j1 h# }0 ]6 ]{9 h" C+ a$ r- p1 q8 e
line(x1,y1,x2,y2);7 q. g6 W' C5 L- J, l p, @
x1+=dx1;y1+=dy1;
$ i* P7 S5 m# R/ Z S' _# G x2+=dx2;y2+dy2;3 ]) l* k( q0 I5 f
if(x1<=LEFT||x1>=RIGHT)
. `* K; _" y; c8 X3 j4 [ dx1=-dx1;/ L9 m3 [" ~, A) z8 K" N; A
if(y1<=TOP||y1>=BOTTOM)8 F- w1 h& q6 w0 n/ [
dy1=-dy1;
7 Z! {+ L% T2 k+ q: ~1 R0 u2 x7 ^ if(x2<=LEFT||x2>=RIGHT)
$ V1 F1 _8 [4 n) u. l0 E dx2=-dx2;
; `4 P5 ]: C1 ?) ~/ N+ k. T if(y2<=TOP||y2>=BOTTOM)9 j: A0 b8 Y; o1 N( ?
dy2=-dy2;7 T4 W8 D' e7 s7 j6 {2 Q
if(++count>LINES)! G& e" ?, J# U
{
. w( g1 ?/ o- `2 X6 p, r1 v/ V' W setcolor(color);4 w8 d6 v- _) g* h8 K- c: E6 {
color=(color>=MAXCOLOR)?0:++color;1 g+ \% K1 x0 b6 T7 G" N
}
: X N4 X2 G& n6 M}, T. |: q- S3 s2 [- X, k* E
closegraph();
0 B% G: B) o; b. [- R}
& X" s0 k5 d) }2 l1 ^, t8 N" A
【程序61】) A0 M4 w$ X4 X7 ]
题目:打印出杨辉三角形(要求打印出10行如下图)
( g2 o6 \- q7 c! u# t g) |1.程序分析:
. m+ E9 `% y" \. P 13 h' b. }; `5 w- H" G
1 1( o7 v/ Q# J' V( z% ?0 |
1 2 1, ~9 c! B3 { Y; d8 D. i
1 3 3 1, s) O- Q9 Q4 P8 Y7 E; m0 C3 R8 y
1 4 6 4 12 j; ?3 u; D9 ]$ [$ Q7 `$ a
1 5 10 10 5 1
2 C8 l! Q2 h8 V' t2 E5 S4 v4 b2.程序源代码: {; ^1 D* j) N5 B( }3 D( P. c
main()
; e& c) Y2 E" Q. ^7 _{int i,j;5 i# o q/ ~+ N/ x! i
int a[10][10];
, X# i# X* G% C: s* Vprintf("\n");/ l0 t6 h7 _! m: m9 j Z; |9 v+ f
for(i=0;i<10;i++)+ K- W8 |! y; C Z2 p) Y( }
{a[0]=1;6 i7 c ?1 w8 |! R$ i% G( @
a=1;}! ~0 [/ L' p! O1 F3 l
for(i=2;i<10;i++): n2 j- `" E& W e7 C4 Z
for(j=1;j<i;j++)( X% \ c6 n% a1 q
a[j]=a[i-1][j-1]+a[i-1][j];
' C6 J( r" H1 K2 r/ k/ ]0 d) [9 w3 ofor(i=0;i<10;i++)9 D, E! d0 s9 ?' i2 Y
{for(j=0;j<=i;j++)+ h% i* d" Q+ a
printf("%5d",a[j]);5 b5 n' \7 i' G% a% g( h D
printf("\n");: g' `( P$ k* W1 x8 p& `7 C1 J
}
5 @* |7 v5 S( [% \: ?}
Q; |7 }* W: f* p# @- @8 X) n' f
【程序62】
: j: |- x0 K, Y% C- L: K题目:学习putpixel画点。
: Y& s0 ~* ]3 C1.程序分析:
2 q$ \& a1 T0 a5 H) x2.程序源代码:
" J0 ^7 _; n; j4 @+ @6 q* L3 o#include "stdio.h"
) G9 t: x5 n0 W. J: P$ B#include "graphics.h"
7 |3 J5 ^# P" u1 q6 Hmain()+ @- \! h6 E! g' `
{
* \/ h$ Q! I; S. v( u+ ~/ pint i,j,driver=VGA,mode=VGAHI;- u: U3 O% o" e% d4 Q
initgraph(&driver,&mode,"");
9 {4 N" ?/ u/ q0 @6 j0 K6 asetbkcolor(YELLOW);9 d$ u, Q) o) ~/ b1 _& p( _1 g
for(i=50;i<=230;i+=20); l V$ t6 P0 C7 C# R4 B
for(j=50;j<=230;j++)1 o& N: d6 W6 ~# H
putpixel(i,j,1);
S1 V0 J% l( ^' o" Q5 j! lfor(j=50;j<=230;j+=20)
+ f4 _! z& S& A for(i=50;i<=230;i++)
# p# F1 o4 U4 A' A F% S T putpixel(i,j,1);& W+ Q7 n7 L( G, b" v
}
8 `/ C4 v) V# z" ?; y
【程序63】8 f' r8 F8 Z m! G7 ]- h
题目:画椭圆ellipse
! P" r- X: K' S6 A* }+ M) M/ h1.程序分析:
3 v1 [5 C. _3 h7 v2.程序源代码:
5 T% X( Y# P: y#include "stdio.h"' o) }! b) r L9 A/ m" P5 B
#include "graphics.h"
! N. A' g( d8 Q, u% d. i8 n U#include "conio.h"
9 Z' R% f! ^0 H1 b, Xmain()
1 M; z: I0 A* r{6 x5 p# e# F0 F4 N: a
int x=360,y=160,driver=VGA,mode=VGAHI;
# e: z/ W! ^* \- n2 A. j) eint num=20,i;# @' l8 ]" j0 l) d" y
int top,bottom;
6 O4 R' ?1 C; y% l0 n! ~initgraph(&driver,&mode,"");
/ }1 ~% _ D( Otop=y-30;8 H: j3 Q9 o1 Z+ A4 }: |+ w
bottom=y-30;
1 n# n6 i7 e" u! xfor(i=0;i<num;i++)
% q/ A5 V! U7 z7 f{2 b/ h# o7 W! r' n+ j6 X# c* ]& A
ellipse(250,250,0,360,top,bottom);9 | s7 l# N( U1 w# H# [
top-=5;- _/ |6 @7 E' }- t3 J
bottom+=5;2 h% [# w- K1 P' q3 L) Z
}8 |0 [0 K1 d$ M0 T
getch();
8 B0 J( L7 |1 Z' N }" E}
【程序64】* S# l2 K+ i5 Q0 P: A% H# p0 c
题目:利用ellipse and rectangle 画图。
6 i5 _' |( a# H$ g& d' E' N1.程序分析:
2 K% R5 v% z2 ^& f9 n, g! _2.程序源代码:
1 `2 d" Z# A! `; V+ U#include "stdio.h"
! ^. v3 D6 s: P& \% a2 Z#include "graphics.h"% K0 l# K# N) Z5 }: _% O
#include "conio.h"' [; S2 R# l" U5 V7 D
main()
: x9 O$ w% Q E& q1 m{
4 d$ n' @. \- Y6 f, v1 Mint driver=VGA,mode=VGAHI;# S1 y7 F% K% n' R% d
int i,num=15,top=50;8 f( D8 x6 X, ?. R n& r
int left=20,right=50;0 o/ O$ b9 P, T B+ v
initgraph(&driver,&mode,"");# p o1 q4 @( ], d( U# n& ]
for(i=0;i<num;i++)2 S# {& h Y* N' T0 h
{
( @! x0 Y! J) n" N8 d6 p' D) Zellipse(250,250,0,360,right,left);; g" w/ g8 ~5 k# V$ Y
ellipse(250,250,0,360,20,top);
& y+ ^( L. |- q: Frectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
" q4 ?4 O6 G5 ?! L @right+=5;
' [) E' g2 F( i' N8 K) j! xleft+=5;
- s+ ~6 S, f! s2 {* s8 V% U9 Ktop+=10;+ M R" b/ ^$ g+ f6 x! O# r/ p
}( t) G/ h' g0 B/ a5 L
getch();% m( e& S4 P5 p8 M$ E% }+ R
}
【程序65】
! `4 a6 o0 |0 }! _题目:一个最优美的图案。
# B" k* d7 \" I& l! l5 V; P1.程序分析:
! o/ E; h: F2 E' L- j) ]( h2.程序源代码:3 W6 C$ Z; r* ~& p
#include "graphics.h"- e+ K1 ?0 E- {2 G8 Q7 j {
#include "math.h"
, k0 s# E" a: S. c#include "dos.h"
/ X* E- _, W$ B; U5 x3 Q5 I: q#include "conio.h"# z5 E! Y0 S- V+ X2 c! G5 v3 Q
#include "stdlib.h"4 a) ?" T9 }: b w( v7 f; d, M
#include "stdio.h"
1 P7 B: W! t4 B7 j' t [! F#include "stdarg.h"- ?- V( G/ c3 j z3 f+ |- y5 @
#define MAXPTS 15" x9 G# k* ~, P% C3 }* |
#define PI 3.1415926+ z% `- u N& o, X; h1 N( K Y1 E
struct PTS {
) C% B% B+ S2 u" q* lint x,y;& v9 k8 z y3 l" m: f9 g$ ?* s. p
};7 H. o* b9 R6 Q
double AspectRatio=0.85;+ y/ `/ Z& N, M* _/ {. a% z9 {( o
void LineToDemo(void)/ y. {* C1 v. t( }# D# t! e
{
# d8 ?* t. P# I0 j! ^) Y. }1 Wstruct viewporttype vp;
8 T$ p& H ^" ~& ~! _struct PTS points[MAXPTS];: F% a" B" k8 q! ?% n8 u
int i, j, h, w, xcenter, ycenter;
/ T: ]+ J& X* X' E* iint radius, angle, step;
( H6 O+ \, ]9 S! G6 B8 a* Z/ @double rads;
' m" O$ t4 p2 A/ _5 t' Lprintf(" MoveTo / LineTo Demonstration" );9 u" W6 C; E% G9 n/ U6 k
getviewsettings( &vp );0 `8 d/ g9 n- S V1 @2 L. W
h = vp.bottom - vp.top;
! c9 ]: y4 l- }/ v5 J9 F. Lw = vp.right - vp.left;2 S0 E/ ^" a% H: V6 \' V* G6 J
xcenter = w / 2; /* Determine the center of circle */
, m+ ~1 }4 {; ]5 d3 e! R. Q }+ Vycenter = h / 2;! y6 n9 E; T$ {6 H
radius = (h - 30) / (AspectRatio * 2);
# ^( |* P' |- ^2 U+ l0 nstep = 360 / MAXPTS; /* Determine # of increments */
$ F- b. R* s4 t! V Tangle = 0; /* Begin at zero degrees */
% p. K+ Z/ a& C/ p2 [for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
: B% M- X" N6 m* `; Q/ _rads = (double)angle * PI / 180.0; /* Convert angle to radians */
; F# q' Q* U. F1 _* R* O& E7 t! ^points.x = xcenter + (int)( cos(rads) * radius );
, z$ Q. b6 [) M" D: spoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
% A( U9 D7 o: v1 _* @, Z$ k/ sangle += step; /* Move to next increment */& E4 U: |, Y- M' R7 P7 W& ^6 V
}' _/ d- {* h+ A' U6 X( N
circle( xcenter, ycenter, radius ); /* Draw bounding circle */. `* z! \1 ]1 E! L `
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
* i( q. h% p% qfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */! t3 h$ ~% r4 j9 x+ N" j' B
moveto(points.x, points.y); /* Move to beginning of cord */
) v6 x' K2 V3 \. |8 Z. Plineto(points[j].x, points[j].y); /* Draw the cord */
& u+ z% q) k7 S6 c6 \1 w6 j# v} } }2 D' @" Q0 [6 \7 z# {& r, x+ W1 ~
main()5 O/ M( x' H' V# N* n) _
{int driver,mode;1 D- x* Y( Y0 { w: `
driver=CGA;mode=CGAC0;
' K" k% G% N4 B1 R( E, I5 V1 N) r9 @1 Oinitgraph(&driver,&mode,"");
$ D) Y5 w! p& F: G1 ]setcolor(3);: e9 t5 T7 _9 D2 @& U8 m# b% O- f
setbkcolor(GREEN);
3 ^: T$ w* Y* A; {9 lLineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
/ _% i" x V0 t$ O/ i. z7 G3 T【程序66】# v0 k% U) V |6 @6 | G5 ]8 H
题目:输入3个数a,b,c,按大小顺序输出。
- Y/ a: n, N) w$ D/ \; @9 r; |1.程序分析:利用指针方法。
1 V! a! w% G5 k* o; k, \2.程序源代码:# ]% l% v) _% c' n
/*pointer*/
6 C7 e! Y2 d$ E, bmain()0 i( I2 L$ Q/ f0 @; U5 d
{
/ w k/ ^" w, L9 d7 _; }int n1,n2,n3;1 u5 q- h B: ]6 L, q1 ^8 v
int *pointer1,*pointer2,*pointer3;& [% u0 v* m4 L8 c7 w
printf("please input 3 number:n1,n2,n3:");
* z& A1 I0 Y4 v; Kscanf("%d,%d,%d",&n1,&n2,&n3);
6 K! H- A1 ~" i0 X Y& W" ypointer1=&n1;. b5 s7 a, H& V+ S+ k) D( E
pointer2=&n2;4 H( Q; ~+ N" N
pointer3=&n3;# b/ @6 u8 v5 j, h9 y$ [$ c8 q% {( p
if(n1>n2) swap(pointer1,pointer2);& f, Z% }: O0 a" `7 j! H: D
if(n1>n3) swap(pointer1,pointer3);8 ~$ [! H7 c. z
if(n2>n3) swap(pointer2,pointer3);
8 G( f% N7 ~4 q- T; Zprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
0 d$ r$ M8 s' |( z f( v0 I}
2 Y7 N7 B( Q& G2 o- @5 fswap(p1,p2)9 J1 }9 e- @ b4 ?; z8 _9 b
int *p1,*p2;
, c, y8 k9 B% V. W+ o/ m. z& d* r{int p;7 r- u/ M6 O. Y @" i
p=*p1;*p1=*p2;*p2=p;, |( F' u' N) E4 v) q
}* r" v1 h: L& k# U8 v