【程序59】. V& f1 a( y* z4 p
题目:画图,综合例子。
) y# w9 a6 M2 x" ~1.程序分析:
( q! t+ F" F6 u4 I- H1 v2.程序源代码:
/ p/ z5 y) L' T- u; g# define PAI 3.14159269 ~7 C% O D- L+ ?' @
# define B 0.809
# I6 B1 a$ D, c" K& t# include "graphics.h"
) P2 N$ z4 D2 y6 Z7 o/ i: `( h* |#include "math.h"* Y: W ]$ N( t% }, R9 p+ X
main()
5 S9 Q) l. o0 ], E2 p; ]1 O8 v' V7 `{4 T; Y- A# ~2 P: p; E
int i,j,k,x0,y0,x,y,driver,mode;
7 J4 L. p4 b8 R$ m9 o: \float a;
. y/ N7 y8 Q* v' A9 b3 T# f' Q) Cdriver=CGA;mode=CGAC0;
u. I J+ J3 ]initgraph(&driver,&mode,"");9 ~# J- A6 y, M; a5 |
setcolor(3);
! x1 a; ]" j3 m }1 }/ @( ~ D1 Csetbkcolor(GREEN);/ }1 R. G4 g8 O7 W/ p
x0=150;y0=100;7 D/ `# ?7 F E& q$ g0 O g
circle(x0,y0,10);
% Y0 T; N" j$ C, _circle(x0,y0,20);
3 q( g) v, N5 F) X, E7 i5 acircle(x0,y0,50);
0 f) V; [ v' s0 u+ kfor(i=0;i<16;i++)
/ a7 \" k \+ ^& Q7 W& ~{
# E5 u$ f4 N& q a=(2*PAI/16)*i;% z* Z) V5 |( q& m6 @
x=ceil(x0+48*cos(a));& D! ^: V. e; f
y=ceil(y0+48*sin(a)*B);3 F1 m+ H" p; ] |4 K
setcolor(2); line(x0,y0,x,y);}6 _8 F' I" D# i- q* U
setcolor(3);circle(x0,y0,60);* x/ F. E3 ~+ M4 F
/* Make 0 time normal size letters */
" _& ?, B" ]4 D6 R8 Qsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);6 R$ N6 `5 H+ N( K! N. M
outtextxy(10,170,"press a key");6 o8 Z& l U7 Q% l
getch(); y) }4 P6 _7 W! F8 r
setfillstyle(HATCH_FILL,YELLOW);
; C: Y4 c3 G& S4 P, ^floodfill(202,100,WHITE);5 Y. \; J, @6 S
getch();+ |" s# H2 [5 ^6 F; [5 @5 |) f
for(k=0;k<=500;k++)
8 q# E5 B( Y1 ]$ Z{
7 z( o2 N; ?4 r4 s$ q7 B setcolor(3);
$ k# ~! u0 ~ v! Y for(i=0;i<=16;i++)" {! f' K) Z+ g) ]: m, i, N4 J
{9 `0 D6 |: c8 r, ~; y, D
a=(2*PAI/16)*i+(2*PAI/180)*k;
: N: M# R5 w7 w1 n8 p! m x=ceil(x0+48*cos(a));
2 ?3 d4 b9 ]% j6 F y=ceil(y0+48+sin(a)*B);* a; l! G/ L0 {
setcolor(2); line(x0,y0,x,y);
+ z) D- E- Z$ N/ j& u }$ P+ |) m2 n1 i3 X9 C1 A
for(j=1;j<=50;j++)$ v! N3 ~& ]1 o* I3 g" ~% A3 ?
{
: `* Y! }$ { S: b1 C! Z+ y a=(2*PAI/16)*i+(2*PAI/180)*k-1;
& e- j/ w. |( h x=ceil(x0+48*cos(a));
, B7 F3 D1 }0 j$ [- v% ~% a y=ceil(y0+48*sin(a)*B);( F( |! ?9 d: ?; T! M3 h& s% Z
line(x0,y0,x,y);5 {+ `( O; p$ n+ J0 y, U
}
. \1 [/ H8 \: Z}
. f$ E# V4 v9 W6 k7 wrestorecrtmode();
* v3 H' Z4 _ `" [}
【程序60】
+ M6 n H, A$ q: n6 B! V8 @" `( {题目:画图,综合例子。 : |) k$ h! J6 _1 `3 Y
1.程序分析:
) {+ V1 S* ~4 y0 g0 P% ~2.程序源代码:; a" e. Z. g" B$ Z" \ J% n
#include "graphics.h"; u1 R! {( F2 V
#define LEFT 04 e+ m7 a9 u, Z6 v% A- w' q
#define TOP 0
: D; }4 _' o8 j: A; Y4 q( V$ |#define RIGHT 639
8 O5 R0 w5 |0 Z) S8 g! h. ?, l#define BOTTOM 479
6 x; a s2 J3 }- L#define LINES 400, O7 G( q- d. ?9 }9 S! d& Q
#define MAXCOLOR 15' g( J1 l" m4 K0 _$ o
main()) h( k) j" a" p* |: {) k/ i' C1 x; @
{" [% q2 p. t7 @- w8 t* g
int driver,mode,error;
2 N4 J( w2 Q9 xint x1,y1;
+ s1 ?: f* t" U; u! ?. nint x2,y2;4 F" }% S) y* t/ e) w# ?
int dx1,dy1,dx2,dy2,i=1;' ^2 p0 b$ O2 D6 d/ r+ i, _4 D! I
int count=0;
. {* ?: p2 C7 t+ C7 F; Aint color=0;
2 R5 N7 p; ?9 t% q. Zdriver=VGA;! k) [& P; J3 O8 l! f. o
mode=VGAHI;
# P/ A' ?% n3 a; f* S0 A \initgraph(&driver,&mode,"");* L x0 W" [3 R; j4 V
x1=x2=y1=y2=10; s. X% g. b/ g" h
dx1=dy1=2;
* A; f- A( v6 p. i2 F' c3 F) Odx2=dy2=3;0 R! I9 Q, }1 P4 `
while(!kbhit())
( }; G' }" L0 l) H{/ E; L! {$ J' r" o$ n+ l
line(x1,y1,x2,y2);- [2 v" F0 g% A: p$ @
x1+=dx1;y1+=dy1;
6 W# B9 g' V& |! ?; ?8 b x2+=dx2;y2+dy2;' I8 W6 ]# X- G
if(x1<=LEFT||x1>=RIGHT)
& F7 Z0 Y$ |6 c' g: H& A dx1=-dx1;# [% }: j, i% n, b
if(y1<=TOP||y1>=BOTTOM), P! b( N$ Y. k- O
dy1=-dy1;9 ]5 ^5 e1 k7 g% o6 M Q+ f2 I
if(x2<=LEFT||x2>=RIGHT)2 X7 B2 i6 @+ U& ~0 Z
dx2=-dx2;
9 T4 u- T) ?4 B" q if(y2<=TOP||y2>=BOTTOM)
/ c3 Z+ n3 R8 V dy2=-dy2;
- J* f1 P" {" e* X if(++count>LINES)$ V- }0 D3 ~( ^, p
{) s& V/ k' Z% I# d0 D
setcolor(color);
t# b% v: W9 R" q color=(color>=MAXCOLOR)?0:++color;$ i) |9 ?- b7 L
}( a6 A" |: G; Y' D9 L2 ?
}
" R: M) l" U% a: a. v" wclosegraph();
+ g* x* F% k9 e: p}
0 f7 d! ^3 R7 M【程序61】5 u; A: E9 q" A. S: g
题目:打印出杨辉三角形(要求打印出10行如下图) ! n, I( y8 E: `1 ~
1.程序分析: w3 T, r$ r; Y+ r7 k
1
% n& F5 x8 D8 Q# ~/ f* x 1 1
2 d" H/ Z- [- q* K& o2 `8 C7 t 1 2 1
( S7 c6 b: o* u& R' s+ q# r 1 3 3 1" C+ j' ~; s& [# @% H* c
1 4 6 4 1
& r+ }2 b3 }& Y( h2 m9 H; { 1 5 10 10 5 1
7 Z/ a4 V/ D/ J& ?( M2.程序源代码:
d# P4 c, Y% l) M* Q: hmain()( V) _- C7 ?, B' o( ` f# Q9 P
{int i,j;
$ G2 F- q2 n$ l5 M' |) G. O+ Iint a[10][10];
* j' S, _! f( `- A* z# J$ yprintf("\n");8 W/ z q9 F: i) B& B& p
for(i=0;i<10;i++)5 ]8 A( N ^4 F- B6 N5 F
{a[0]=1;5 R& }" Q- ?) i6 x `: K( L2 s: v/ P
a=1;}
4 x8 e1 E; a0 Xfor(i=2;i<10;i++)( }) F! ]' Y! c7 }( |4 E
for(j=1;j<i;j++)5 \3 r" h- i5 R4 T
a[j]=a[i-1][j-1]+a[i-1][j];8 ~! @" ~% b# R Q* n
for(i=0;i<10;i++)6 l% K, C+ W( F) B
{for(j=0;j<=i;j++)7 }% K7 W3 J* x- w3 N p; p
printf("%5d",a[j]);- B% k0 \4 Z2 r$ K- i" M/ \& \: v$ K' p
printf("\n");6 n; H& h. r2 p/ o9 h' g+ z1 m/ z
}; V& j6 d! K4 i3 q; C) R( e
}
2 Z) V% w* B0 e7 o; A' X! _7 u& A j【程序62】( P6 l1 a- i6 _9 C5 R
题目:学习putpixel画点。9 Q3 N; y% r( g& T) U6 b
1.程序分析:
: W: C* V# l) V1 {* W3 B, d2.程序源代码:; F9 q( W" n7 T4 i
#include "stdio.h"+ |6 [3 [$ ?6 p* v9 j r* N* r4 R8 j
#include "graphics.h"
. d0 q0 S. L. p! Q1 ymain()6 I# O- Y0 A, o% {, {
{; W. d# q4 |" h. i
int i,j,driver=VGA,mode=VGAHI;( ^+ N. Q! B- A9 n% y3 G
initgraph(&driver,&mode,"");( ?+ Q, j" R( v: }
setbkcolor(YELLOW);5 U# q1 W3 m# H; i* Q
for(i=50;i<=230;i+=20), m6 h( z7 V& p/ n0 E6 {6 w
for(j=50;j<=230;j++)
7 I2 m( C. a) [% @. |# l7 D3 j$ M5 E putpixel(i,j,1);& }" A8 z2 U: b* J: ?' A x6 D/ y- U2 [, Y
for(j=50;j<=230;j+=20)- t4 V. C' M, ]1 g. }4 {" u
for(i=50;i<=230;i++)
9 F1 M9 d5 B% T I putpixel(i,j,1);
" Z/ ~: W9 t! g% K}
/ n% W- c! k- a/ f
【程序63】" ]7 s6 N6 w" S( b$ \ c8 T: K2 X
题目:画椭圆ellipse
' T' G( n( L h4 h( D1.程序分析: \: ~ k `2 j( D; b( Y
2.程序源代码:1 Q- `( u& C( S, G k
#include "stdio.h"( w+ J1 R3 k/ R9 V5 I4 F
#include "graphics.h"4 z' Q' p$ J- Q$ X5 v( L! @# z8 d" g! i/ q
#include "conio.h"
+ }! e% W1 I- l7 kmain()
}- s7 W8 c' X5 H# K{
+ E9 h5 O0 Q( o0 y# Q* }int x=360,y=160,driver=VGA,mode=VGAHI;
1 Z. s' B6 O9 c. s$ Iint num=20,i;
. I+ E1 v- k- k) B" s- v- w3 tint top,bottom;# i. h$ ^% L3 p5 N5 _6 X3 h
initgraph(&driver,&mode,"");
1 M/ T; c1 z% I; v& |* _* u& k) e! vtop=y-30;
* B$ o% h5 @( p7 C: Z) s! }bottom=y-30;. X' T/ ?/ X) X; e9 }
for(i=0;i<num;i++)1 O' `1 ^5 U X" r
{0 H, y) ]' W+ x% X1 \" \: x2 d
ellipse(250,250,0,360,top,bottom);+ L4 s/ l4 q3 w, r
top-=5;% D* b- V6 A- P
bottom+=5;# j* z! e9 k; k
}
6 W, k( e# b0 V" }( U. Qgetch();+ ^. W V7 x$ C2 z3 G. A! p
}
【程序64】& {1 O: {! o* ]+ B, Q0 w4 R' \
题目:利用ellipse and rectangle 画图。
( }6 K3 x0 R! Z( l r1.程序分析:
$ P0 H( e" y+ j, M8 {. a/ K6 W0 u2.程序源代码:
1 K% M3 v) k$ a$ s* m#include "stdio.h"7 D) n2 d+ h* @% o* D
#include "graphics.h"5 m" p' X# s0 N. Q
#include "conio.h"! F; v! i+ X- t" i5 X
main()
9 B* R3 x \2 h' e{, l; s# g/ f/ Q! R& e9 F
int driver=VGA,mode=VGAHI;
" w7 J. f, s% N- S0 k3 Mint i,num=15,top=50;& v; ~+ n6 `) o0 T
int left=20,right=50;
+ k4 G. _; V% j8 K# T# \- L1 _initgraph(&driver,&mode,"");
" W+ I2 z' ]- F' R! Q: T. g- Kfor(i=0;i<num;i++)1 J; U( V6 B' [0 a! {
{, \9 r) Z# P) n' T
ellipse(250,250,0,360,right,left);
3 c" p4 V/ J6 o# y uellipse(250,250,0,360,20,top);6 A* d2 l6 _+ d- Y+ K
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));) z/ i( Y {' Y
right+=5;. s6 a: w/ U/ G% D8 k/ y# o
left+=5;
6 c }$ q4 @0 ^/ }9 A" r* E1 _top+=10;
# e8 u# m% l- Z0 T) C: S}1 _1 @0 u2 e d8 }( ~. k# X
getch();
. Z# `% N1 `9 r O0 z}
【程序65】
) d9 Z% x9 ?' }0 p, X* s' G题目:一个最优美的图案。
3 k& s$ s3 @4 Y9 B0 o! d1.程序分析:
d A% e4 I9 l7 l/ U2.程序源代码:+ v4 @+ Y0 G" N. S% `- M
#include "graphics.h"
4 `6 H( A( q0 C8 o; k" b# I#include "math.h") ?: m/ d% A& S. M2 R i$ V
#include "dos.h"& i6 K2 a( H' O7 m! r1 A1 f
#include "conio.h"( j; w: D" c: _3 x7 g
#include "stdlib.h"
% v% z5 N) P& M- R g+ w#include "stdio.h"& S, ~! M: s' x/ l& U; |/ e
#include "stdarg.h" w, X g" X( ~! y
#define MAXPTS 15) @) K7 x6 h3 w C) U8 G
#define PI 3.1415926 G& G8 F& _- ?1 D) T( z/ Y
struct PTS {
! _9 K1 P( y( o T) Vint x,y;
1 ?" z/ [: c& y. h# t};# |8 g+ R) d4 H5 z
double AspectRatio=0.85;
- V6 @8 d2 e. n3 i$ J, Y! V( qvoid LineToDemo(void); @4 i' J( _% m7 K) v1 n. b; Q7 d
{$ n8 I/ A8 ?. f3 K+ k9 d. F# c
struct viewporttype vp;7 v K* \6 e! B8 Z+ S
struct PTS points[MAXPTS];# L$ D- r2 @5 m
int i, j, h, w, xcenter, ycenter;
5 B5 `, u" M0 Z" Q9 lint radius, angle, step;
& i& k! ~ T5 t& E1 j0 Mdouble rads;
; E7 e4 m% E1 U1 s: o: Oprintf(" MoveTo / LineTo Demonstration" );
5 l7 y) t1 J7 e, hgetviewsettings( &vp );
5 A0 D% [. |1 A/ S. Fh = vp.bottom - vp.top;
, ~$ c- m8 @2 T; |2 `* D! iw = vp.right - vp.left;% z6 L J5 K1 f. @5 \. u5 I% N+ |
xcenter = w / 2; /* Determine the center of circle */
. f+ Y) D2 _4 pycenter = h / 2;; q3 ]; q* |7 A) ]9 n7 R2 j( Y6 B
radius = (h - 30) / (AspectRatio * 2);" P% z/ B R' O8 `; U/ ^0 l, j
step = 360 / MAXPTS; /* Determine # of increments */# ~( G" E- Q. j4 d
angle = 0; /* Begin at zero degrees */
: { o S: A' nfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
$ @ L3 e1 Q% v4 Srads = (double)angle * PI / 180.0; /* Convert angle to radians */
: M, q, M, ` C; Z! M( a* Fpoints.x = xcenter + (int)( cos(rads) * radius );
% a0 k. s3 [& c9 q% y3 e2 vpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );' D+ Z( [/ {: V0 |/ E
angle += step; /* Move to next increment */
* e( E3 Z |" K S( |, i}/ l- R0 d# b1 X6 i: B: U, s; J
circle( xcenter, ycenter, radius ); /* Draw bounding circle */2 r7 w2 n# u& q. |; R+ [
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
8 N9 `* I" a: S% i1 Bfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
7 J8 _# w; |* ^7 S1 `moveto(points.x, points.y); /* Move to beginning of cord *// q' a& ?- N! q9 X
lineto(points[j].x, points[j].y); /* Draw the cord */4 C, c; b: H$ B0 D, Y! o
} } }
; H' ]/ W9 t/ l/ umain()( [7 o6 ^0 b, c4 B
{int driver,mode;/ o K1 Y2 U& g; [) J' S
driver=CGA;mode=CGAC0;
3 \1 k' \3 z8 Hinitgraph(&driver,&mode,"");7 C, i# g2 N) Y4 e x5 f% b# W
setcolor(3);
$ w7 X* {# A3 w' rsetbkcolor(GREEN);( I+ d, Z2 y+ v$ _
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
4 U9 H4 j$ i3 y* Z+ T& n
【程序66】
4 D" @3 k* S/ p6 r* g B% N题目:输入3个数a,b,c,按大小顺序输出。 - `) n+ p1 U' h2 F" z
1.程序分析:利用指针方法。- u/ q- {# Z3 o. g* |7 [" u* p
2.程序源代码:
5 P5 A! T6 _0 ?* Z/ _/ G/*pointer*/% O1 O& x: Q8 [8 q; W
main()
7 s$ ?' t1 s$ L$ {{
( @6 J6 z: b1 X" F8 oint n1,n2,n3;
s7 }. }, F9 aint *pointer1,*pointer2,*pointer3;
) R7 J1 U8 c8 c& s" _printf("please input 3 number:n1,n2,n3:");/ J2 I9 \) a8 b" Z# {
scanf("%d,%d,%d",&n1,&n2,&n3);+ j B+ w# f" n) I8 W2 C
pointer1=&n1;! f! R' ~: s$ U( c: ^: C
pointer2=&n2;
+ k( I( ]) P) l* i7 ypointer3=&n3;
; n$ l2 S! H3 F; ^6 m; r4 X2 oif(n1>n2) swap(pointer1,pointer2);: ^4 I2 n8 X" b, T1 U! c
if(n1>n3) swap(pointer1,pointer3);5 c; x% `* d) Z! F6 @+ T
if(n2>n3) swap(pointer2,pointer3);
1 u/ c5 a" Y- j! h" Eprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);5 M: _. T1 ?* _
}# ^+ H2 o' v, j. o4 N
swap(p1,p2)
* b. a3 b* \: p" qint *p1,*p2;; l, w. V7 _" X( E/ T8 M
{int p;; h6 g& g2 z1 n, [& A/ S
p=*p1;*p1=*p2;*p2=p;
! x/ r+ t6 J- I; i}
7 k' d. q/ ^% p% i3 ~2 X* @4 L