【程序59】8 B* A2 ^& _ Z, G$ Y, i* f* D
题目:画图,综合例子。; _: `0 `$ p) h$ {& w7 v% b, d4 M. k
1.程序分析:' H1 R6 ?9 E$ b; U# y- A- B
2.程序源代码:
; K( y% C* l$ Q% d1 j" D# K# define PAI 3.1415926; L+ h, H2 U8 b0 ?' |
# define B 0.809
6 `+ }: c. B2 e+ j" y# include "graphics.h"! ~8 m ?: y: Q4 I K& N
#include "math.h"8 A* S: N0 c, U* T+ q5 @1 Q8 \$ i* r- A
main()
3 G' h0 T* W+ y% r% d- F{3 o. [" ~! F/ l! n1 i2 I$ g
int i,j,k,x0,y0,x,y,driver,mode;( k- P9 M, U7 M. \
float a;- c( \- u6 Q1 B
driver=CGA;mode=CGAC0;9 _1 e0 L1 j+ R# ^
initgraph(&driver,&mode,"");
M! p# n* q" @$ W. ^/ h# I1 O" Ysetcolor(3);
# o6 F9 U* u' Z S! u+ H2 hsetbkcolor(GREEN);# X4 p! U3 l, n3 ?0 K
x0=150;y0=100;
& O: @9 X X, I& m" Hcircle(x0,y0,10);
- |3 M$ b7 N, b( scircle(x0,y0,20);
8 j/ \$ Z& f, v/ h( [/ \circle(x0,y0,50);6 Y3 m. E$ h2 ]! z( G( `! j
for(i=0;i<16;i++)
; Q- E, ~2 J9 Q7 J2 C{- C+ K% t* i* G- Q
a=(2*PAI/16)*i;
& o% X# V5 P$ R" c0 H w& v x=ceil(x0+48*cos(a));! p& A! Y0 M( g. c0 a- F; J: `
y=ceil(y0+48*sin(a)*B);
, x! e7 u" K& r0 T5 Q setcolor(2); line(x0,y0,x,y);}
Z+ a. ~! w6 msetcolor(3);circle(x0,y0,60);
4 K, m0 S* V& X/* Make 0 time normal size letters */
4 M3 A$ W a. Y5 Jsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);3 \. e7 L5 H2 L2 v" q9 q( X
outtextxy(10,170,"press a key");4 c) L( m2 B( E$ {! Y, N; q7 {
getch();! w" m: ?# v/ {3 J9 m1 \5 ]0 j" ~
setfillstyle(HATCH_FILL,YELLOW);
0 p+ F3 H% @6 c4 vfloodfill(202,100,WHITE);
8 N* K# b) t! L. t" d. ~* Ggetch();
+ T& G' Z# K: ]7 N" C( _/ Efor(k=0;k<=500;k++)
' o2 W: F+ K( J, `$ T, d! v1 q6 ~{
. w. P: U- i% ~. @: s. a( ~6 Q( R setcolor(3);3 @: w; k2 ]* V' \
for(i=0;i<=16;i++); v0 c* [/ o# p: I& U. \
{
U! ~1 l6 J: e$ T; g. l# B. W a=(2*PAI/16)*i+(2*PAI/180)*k;: } m7 S( Y9 w7 U# Y! k
x=ceil(x0+48*cos(a));
. ~% u' j6 b6 o+ [ y=ceil(y0+48+sin(a)*B);
1 q0 u9 k+ n1 r) N' E" j6 c setcolor(2); line(x0,y0,x,y);' W) u3 B/ m9 j% }
}7 r8 ^" @ ~! x/ `4 A# g
for(j=1;j<=50;j++)) i9 X; o% C4 F' z/ A
{* r4 n( F# J- {
a=(2*PAI/16)*i+(2*PAI/180)*k-1; L' p, O: b! I2 s0 q4 y
x=ceil(x0+48*cos(a));- j4 a1 c; d C: p5 Z y7 R7 `1 a
y=ceil(y0+48*sin(a)*B);; p& y- M$ |! Z
line(x0,y0,x,y);
! X" t& m9 j9 L g) C }+ s' U/ ^5 g, ]3 n
}% m, _- u& }- V7 E1 p, H
restorecrtmode();* L T7 \. S1 W5 b1 f' X- Q
}
【程序60】
+ @6 i p8 ^2 F" M0 [题目:画图,综合例子。
- o. Z- n. c$ N. n2 }0 L1.程序分析:
7 m7 Y) e$ f: y' O8 |7 {+ f2 Z; N2.程序源代码:; L; @" w" ?6 T6 x2 z; D( X
#include "graphics.h" T8 G9 V9 E3 \ Q2 j
#define LEFT 0 x9 c/ C; m5 [3 V# @
#define TOP 0
: b. ?9 h$ _7 t& a& R#define RIGHT 639
7 s5 N( k2 `2 |+ K: n, B0 O2 M#define BOTTOM 479) }7 W5 I) P7 H# v' {, F
#define LINES 4005 J9 i- f j3 y9 Q8 }6 g7 s
#define MAXCOLOR 15
1 | Y, y4 k) Bmain()
4 h9 l! m! V5 g6 K% s: R{
6 |' G& }3 @" cint driver,mode,error;8 \( ^( }2 }+ ~' ?+ L) L" O
int x1,y1;
/ D% {9 u6 D% v0 sint x2,y2;
0 G T. ?3 l3 W" f3 a7 M! h9 vint dx1,dy1,dx2,dy2,i=1;
( e- W5 e2 j4 m Iint count=0;4 n7 j% I: M' b: }
int color=0;6 ~$ ^8 t5 e3 E
driver=VGA;
2 \+ b2 R4 G- z( I0 ^5 Smode=VGAHI; ~. K V* [4 W8 e
initgraph(&driver,&mode,"");
7 a, @# @ y: I" f4 lx1=x2=y1=y2=10;
/ U* y K% q {9 X# _8 Kdx1=dy1=2;6 ^2 z; J( @8 X5 O
dx2=dy2=3;1 m; u! G$ Y' o/ |' h0 c* v1 p
while(!kbhit())
* ~: K7 Y8 L; Z! }{
0 u# K0 V9 e$ l& O line(x1,y1,x2,y2);
5 k* a3 p* N' H$ o; V2 L3 M" @. } x1+=dx1;y1+=dy1;
1 `' B9 Q2 @" q( q1 i5 P" P x2+=dx2;y2+dy2;
5 P% U0 e( T& a4 [ if(x1<=LEFT||x1>=RIGHT)
% P+ P6 {. D/ T+ y, @8 e& x dx1=-dx1;- r, o+ \- H5 A7 u- g) V: Q3 z
if(y1<=TOP||y1>=BOTTOM)$ C6 }4 M' i" H- h
dy1=-dy1;' B$ o/ A" A4 y, U. @$ j6 J- ]9 P3 o# r
if(x2<=LEFT||x2>=RIGHT)
( z1 M. Q) _* o$ t/ I! y dx2=-dx2;
! }# G5 r1 ~# Y: d5 u2 o- g' H if(y2<=TOP||y2>=BOTTOM)+ t7 u4 R/ p9 v2 ]
dy2=-dy2;
0 w6 G! ?% [4 E( e( p* [; M if(++count>LINES)
! }! [! |5 k* U m {
5 G3 R( @ ~ A setcolor(color);
- x8 p, S# j- `. N+ o& |' [. b color=(color>=MAXCOLOR)?0:++color;
! z' M: ]: U. X. h* l9 | }. c3 {$ z% a2 E* H( f( a( K C
}
4 K. C( U$ z3 Y+ R& s* iclosegraph();5 n+ L+ s6 _; k( o7 W# s( }5 y
}
1 h9 ]& c9 N, C( u; b( }
【程序61】7 G+ Z; e1 B# ?/ E- f4 N
题目:打印出杨辉三角形(要求打印出10行如下图)
' P. b3 P c9 Q1.程序分析:6 W3 X( W4 f: q/ T
1- b8 C0 z1 u5 U% m9 ~7 S
1 1/ O: I# Z* e% l4 `, I. w, I
1 2 1! i$ n0 I0 v+ y# l0 v5 V
1 3 3 1
7 i, ~6 t+ W% {6 Q6 {3 N 1 4 6 4 1
8 ? Z! ] U) b 1 5 10 10 5 1 / _5 i' |: E. f, N5 v
2.程序源代码:
8 f7 Y: W( @9 _" C/ F( Z$ Gmain()
1 Y% X7 y1 N- a$ a0 E, B" q{int i,j;
( B, Z# o. _8 q. i: Sint a[10][10];& c+ \) u8 o( f7 p; D
printf("\n");
' v2 F* _2 I0 S B9 _for(i=0;i<10;i++)3 {; U* T( d$ C2 f& h9 {
{a[0]=1;$ b7 u0 W" d3 \/ D* k( D
a=1;}6 x* d9 V- I% x- ]2 W
for(i=2;i<10;i++)2 Z* C8 v( W5 p
for(j=1;j<i;j++)8 A8 r6 \- E1 S
a[j]=a[i-1][j-1]+a[i-1][j];
~ }4 X; ?4 Q3 q7 ?" Wfor(i=0;i<10;i++)
3 h6 U$ o, L- t0 N {for(j=0;j<=i;j++)
' d4 x% H' ^! Q printf("%5d",a[j]);7 k3 Z$ A5 p0 `# f( g1 p0 C% |
printf("\n");% J- o N q- ]1 K! Q* y) h6 }
}
$ a* V- f4 B& |0 q# U3 }' p}
9 h/ X1 L+ e+ k+ @- w# Y9 u* v0 m【程序62】
/ S$ i* a; h( m" o# ?# j& [题目:学习putpixel画点。
5 T }- w' w' S+ H* I1.程序分析: 9 d- d% H# p0 K" S. Y
2.程序源代码:
2 o% a; j9 V9 E( j; S9 K& C#include "stdio.h"1 O0 _% w7 D! p1 y5 z
#include "graphics.h"
: }6 m" F, h# }main()" u2 j' l5 \9 x# G8 E Y# T2 \; z9 p
{
1 J$ O1 G8 y+ ^6 t% e2 ` vint i,j,driver=VGA,mode=VGAHI;6 T9 o" {+ e" I. N% N7 n! `
initgraph(&driver,&mode,"");
2 X, A$ {. T1 o% S' ~( f3 rsetbkcolor(YELLOW);2 D% f0 \* s' r8 ]
for(i=50;i<=230;i+=20): a7 O/ z: q8 E& Y) C, Q
for(j=50;j<=230;j++)
& r% w$ X$ t8 |* f3 b putpixel(i,j,1);0 M6 o, V+ g- B0 K
for(j=50;j<=230;j+=20)# \# ?8 d+ ~, ^: b. G9 ]& I' B' r
for(i=50;i<=230;i++)
% }; H- E1 V L putpixel(i,j,1);- T$ a" @- N- [1 l( w
}
! b. V E% S+ @) j# C: I0 a【程序63】4 s$ o. M. b4 Q$ d
题目:画椭圆ellipse 4 g0 X+ i4 k$ p, i5 ~' ^
1.程序分析:7 J5 e0 k0 X8 ]+ m z
2.程序源代码:
& R/ ^5 ]; T2 F" n0 u g! Z#include "stdio.h"
; i) V2 D* l6 A8 K#include "graphics.h"! s% O9 K* P% D( F4 y
#include "conio.h"
! b4 X3 Y/ m+ ]% I1 A9 Ymain()0 h2 k! ~: e6 G6 Y6 {4 Y6 |
{
' n+ z) k% l) Wint x=360,y=160,driver=VGA,mode=VGAHI;2 }; |0 H7 O: g( r. X
int num=20,i;
: J+ R; ~9 P' E ^int top,bottom;
5 r2 g) V; \( T; ?initgraph(&driver,&mode,"");/ F" y) `- x* g0 O$ v% a v3 l3 z3 H
top=y-30;
" c0 j6 G8 k6 B2 C$ Z" Lbottom=y-30;$ c) ]6 J3 v7 h7 x9 _1 a g6 o4 u
for(i=0;i<num;i++)# P" U# b) ^/ s' q e9 Y7 ]8 m
{
% O$ I9 \% W0 ]5 uellipse(250,250,0,360,top,bottom);9 ?1 g9 G* T. Z c. [4 A3 \ O% c
top-=5;; z5 w& Z5 m/ u
bottom+=5;
0 \/ y+ T5 w: s% p}
& Q3 ~8 B& u! ^. a, X2 r: |3 Cgetch();
4 P: g" Q- p7 \, V}
【程序64】
! M T9 u) i0 F1 P题目:利用ellipse and rectangle 画图。" R8 z( a2 ~& x5 b
1.程序分析:" m, j: O( D" X, J4 d/ R3 Z
2.程序源代码:
3 b- G+ K$ y; l/ L#include "stdio.h"
4 u2 k3 U7 L6 q' e0 k* K#include "graphics.h") H' w. C6 l3 Q0 j
#include "conio.h"# W: u6 d8 B2 `
main() I- |: P# v4 u: a8 e% c
{
3 F7 _( n, U' i* J3 c6 sint driver=VGA,mode=VGAHI;1 \1 @* T6 J4 f1 f
int i,num=15,top=50;7 Q% R3 p% e7 ], T) c% T+ D
int left=20,right=50;
4 S7 I; i K$ Dinitgraph(&driver,&mode,"");6 @' ~5 z4 x2 |. c
for(i=0;i<num;i++)$ w; @; H" b7 `. f0 V1 @
{( w% k) F; T2 V; S) b8 Q8 F
ellipse(250,250,0,360,right,left);$ t0 ]6 }* d- X7 f/ u' M% ?$ d
ellipse(250,250,0,360,20,top);
2 J! i$ A/ P8 s& w9 [) `rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
# ^% u( b4 o6 L+ a1 mright+=5;
9 j& |! S, Q/ u* w! K, U2 Cleft+=5;
/ `- A! N1 p3 q, t: I. ctop+=10;* z( M+ ~9 y" L$ m
}2 B( f$ K) G" |7 d4 e: U
getch();) z5 j2 T6 ~( i$ C$ a
}
【程序65】
& A$ j" h) Y: q3 t/ {$ A h% ~: C题目:一个最优美的图案。 * P) o: E' \- M5 J- j+ d1 d) x
1.程序分析:
, O# Y; x6 ?+ X+ j& ^2.程序源代码:
, b. O! h; _4 }4 E#include "graphics.h", p( z! d4 |2 q
#include "math.h"
: g8 i u# P- e/ @& K3 _#include "dos.h"+ I9 A8 O) P0 B
#include "conio.h"0 ^6 |: b+ w7 R# j; {
#include "stdlib.h"
. s6 b% V8 G2 \; v7 ^+ G+ z) r#include "stdio.h"9 X+ }& Z( b5 d& }6 }" S/ e
#include "stdarg.h"0 O6 J7 Y' H; s( q4 |' ]. G
#define MAXPTS 15. S5 s- T6 p6 \ U9 f2 \
#define PI 3.14159266 K: I1 [% W: k; A
struct PTS {7 g w; f+ T, Z9 Y9 r% |# }; {
int x,y;
% Z( d3 h! e1 N4 M" t1 [ H( c};
3 @( E5 _) |1 Ndouble AspectRatio=0.85;
: K( L1 O4 U9 X Rvoid LineToDemo(void)0 R; _* _, e' H4 @; J5 r
{; J; d6 m- Y( W: E! C
struct viewporttype vp;
' ^/ p# b# g. Y3 G% g+ gstruct PTS points[MAXPTS];0 E2 a2 a; K* `
int i, j, h, w, xcenter, ycenter;
/ i4 L) S! N6 X# _ pint radius, angle, step;- M: B, X# j5 p( m
double rads;5 C) ~% f9 p3 w0 b7 c1 T- N, Z
printf(" MoveTo / LineTo Demonstration" );
4 e9 L/ f$ J$ ]4 \# e% Hgetviewsettings( &vp );
, i& }% m) U: R ]h = vp.bottom - vp.top;+ V; l f9 c, k% l% g3 A
w = vp.right - vp.left;
1 Z8 [3 O; l( u* m1 n8 a/ v4 |xcenter = w / 2; /* Determine the center of circle */
! N/ f) B ~( E! M1 `4 mycenter = h / 2;/ l: h7 M; ?5 _
radius = (h - 30) / (AspectRatio * 2);
7 i: {+ H. ?5 A( {0 }$ lstep = 360 / MAXPTS; /* Determine # of increments */2 W3 a8 @ ~. A. r( ]: `9 U) E* e
angle = 0; /* Begin at zero degrees */% k6 R' b) p u* @% X. R4 L3 ~! u7 w
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */ j Q- r1 e H7 g5 n) {$ C" p
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
' u* ]& L' ~5 O; q, fpoints.x = xcenter + (int)( cos(rads) * radius );
/ Q6 k6 v7 C7 j9 S5 `# opoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
) ~& D. W9 P/ L* F" Gangle += step; /* Move to next increment */
/ ?% y7 U; }8 s) O" p. y" b! a}
% `6 {8 A; Z( B/ e- v* _circle( xcenter, ycenter, radius ); /* Draw bounding circle */
( x! c* v/ F7 j6 |! Pfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
, k! W+ z/ r% K2 w( P: a+ P8 V* \( jfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
4 b' H6 N7 e' \( H5 x7 ]- ^8 pmoveto(points.x, points.y); /* Move to beginning of cord */
r; ^# ]9 F3 h6 }lineto(points[j].x, points[j].y); /* Draw the cord */
. \0 m- G( U; m6 F} } }+ U7 P- V+ S. s, ~
main()7 ]" b* J1 R$ ~+ x
{int driver,mode;3 [' Q6 Z+ J8 x. Y3 F/ ?+ R
driver=CGA;mode=CGAC0;' L8 i9 u# e0 {! k& w2 m
initgraph(&driver,&mode,"");- y) G& ?; `# k M. W2 f. n
setcolor(3);
4 F0 g/ X* @3 i9 P! C/ U: zsetbkcolor(GREEN);5 L. q+ L. s6 Z6 R: n7 y
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
& z8 P( ^. @& c7 v2 I【程序66】$ B7 `' h0 _1 ~
题目:输入3个数a,b,c,按大小顺序输出。
; ^/ Q* a& w5 V# L; z* j$ V1.程序分析:利用指针方法。' n+ C% {- U6 c; l
2.程序源代码:
! l9 k" ~/ a' Y8 f* h, u$ l/*pointer*// ]) [/ P+ u; n( Q5 ~
main()
; X3 N' P7 r2 d* ]: i{
4 Q# R8 q- ~( G& D1 qint n1,n2,n3;
8 K- G3 ?3 A, i' h3 Z& bint *pointer1,*pointer2,*pointer3;( g. O, o/ p) D4 P7 q0 {( ~: h
printf("please input 3 number:n1,n2,n3:");7 b6 T ^& k. y3 z% e8 T6 S2 G
scanf("%d,%d,%d",&n1,&n2,&n3);7 V/ t* g& q6 j7 n& }+ V3 m& z, z
pointer1=&n1;' ^) n' i0 S' e
pointer2=&n2;
$ t- s3 q% B$ w/ ~3 c+ _) y( spointer3=&n3;# f% S8 T. F( v2 c) j4 p
if(n1>n2) swap(pointer1,pointer2);$ F5 H$ n b" H
if(n1>n3) swap(pointer1,pointer3);
u: k, ^0 _* K; r* xif(n2>n3) swap(pointer2,pointer3);2 [ U5 \" I1 T) o% [9 T
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);8 W$ @. v! b3 v
}
4 X, i0 R9 p* ]9 b6 }# tswap(p1,p2)' W- u: U$ e9 N1 x. m9 a: v
int *p1,*p2;3 o- t" s' l. Y$ l; P
{int p;
; f; g }2 z4 Q0 L" Mp=*p1;*p1=*p2;*p2=p;
" | t* l# ?8 B3 [}
2 h' F9 }+ t$ J& F