【程序59】
* i0 b6 w: T3 Q6 f% w y/ C+ m8 e题目:画图,综合例子。/ I0 A/ d% ^+ Q
1.程序分析:
; ?; d, r( ]7 a: E3 t6 z4 G2.程序源代码:
- P4 v0 T5 @$ t; ^# define PAI 3.1415926/ \4 z$ g: z; ~; f, {
# define B 0.8093 k/ V; M$ d% ?- r
# include "graphics.h"6 [. B2 w2 [. c* F U
#include "math.h"
8 A9 b; v% R6 J: q& z1 y4 P, d8 h& imain(); k! y- N9 R( P4 S7 f) k
{4 B% |4 T( s5 E T! t& P3 n
int i,j,k,x0,y0,x,y,driver,mode;
$ ^/ {6 |- b1 h7 L' sfloat a;6 w/ T( C/ O2 D) \( c4 a
driver=CGA;mode=CGAC0;- q( b) h; o& m; _- N
initgraph(&driver,&mode,"");
6 s3 m9 ?% Q2 t$ L- A1 O) rsetcolor(3);: V" R' j1 ~# \6 d% C/ r
setbkcolor(GREEN);1 Q/ A8 p. ^. G& S% }7 a0 Y
x0=150;y0=100;
& c( F+ @: Y! m( |" dcircle(x0,y0,10);0 r! A2 r/ }6 t U
circle(x0,y0,20);
! g; ?/ k ? g( M: O. a9 S1 V$ {circle(x0,y0,50);
& N' [: d3 p* tfor(i=0;i<16;i++)
1 ~1 e. c* E% f( ?5 t$ z{
6 i6 F6 V% b- c4 G a=(2*PAI/16)*i;1 m6 c v4 e( f8 G0 p
x=ceil(x0+48*cos(a));) C) r% H/ F# R1 W4 m0 Q! ^
y=ceil(y0+48*sin(a)*B);" z+ E- a/ O% E8 S8 S9 A9 [
setcolor(2); line(x0,y0,x,y);}8 X5 p8 N3 d, d4 S5 F
setcolor(3);circle(x0,y0,60);
. t# @: Z/ p j- ~7 B: d/* Make 0 time normal size letters */
) _0 [( P3 T# Xsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);' |" C q* A" p$ R8 V$ ^! R
outtextxy(10,170,"press a key");: n; C: _1 s4 k4 o9 B) G) Z
getch();" q( [/ a! D& J1 X: `) I
setfillstyle(HATCH_FILL,YELLOW);
6 A" w$ a7 t r9 s0 S) A0 L& Rfloodfill(202,100,WHITE);' a& w, L6 P' m7 Z
getch();
" C. f0 ~$ t7 o+ Tfor(k=0;k<=500;k++)
: }: o, W& C0 A D0 ]" _6 d X6 [{
# C0 A; k4 q0 l; J! J! o! b setcolor(3);% ~" k& U( L; q' V& |& ?0 ^
for(i=0;i<=16;i++)
8 v7 Y, v2 G) S {
; r5 A# Q! X6 }( L" ~3 E a=(2*PAI/16)*i+(2*PAI/180)*k;
) r8 x3 q! ]; V7 C. J7 H8 F x=ceil(x0+48*cos(a));
. t4 `2 n# ]4 p1 R ] y=ceil(y0+48+sin(a)*B);
, G2 b* ], ~: F z. h k setcolor(2); line(x0,y0,x,y);
) G4 q* F6 I! H+ r0 f }. Q1 p4 K+ M/ ^2 h7 f( {* @8 F
for(j=1;j<=50;j++)
8 _! |9 i+ U8 k4 g {7 h$ ^/ q n5 |) R
a=(2*PAI/16)*i+(2*PAI/180)*k-1;# |2 B+ @5 \+ d
x=ceil(x0+48*cos(a));8 Y; ?" B" o$ E0 n$ ]( ~, E$ s) t
y=ceil(y0+48*sin(a)*B);
1 J" G+ I4 \& E! B; T% }6 V, X C3 g line(x0,y0,x,y);
2 }3 S' ]% {& r/ e1 C }
" h X4 q# m! n7 O9 F. \}, o) K% S9 A: `& H- i @
restorecrtmode();' E# [6 {4 j) M) \5 L: V
}
【程序60】
% N/ \% [# x) V/ _) M+ O& M7 n( w: k题目:画图,综合例子。 , i) o; t( T2 ]; M; e
1.程序分析:, F4 D( t4 P. E3 p! A. @
2.程序源代码:, ?" S0 b* q+ t% r9 V6 b
#include "graphics.h"
* u) Q) C( n( R/ G. m) i2 ^#define LEFT 07 F! o* D' C) O8 S& |) C
#define TOP 0
5 V& O% p! t, B& y#define RIGHT 639
2 e$ M, H% z. H1 K) {4 w: U#define BOTTOM 479
( u9 H* {- c& C' F" l* M4 k2 `. O#define LINES 400
3 ?: r# C8 B1 F4 h# N* k#define MAXCOLOR 152 ~% h! S' O- g! ~& J
main()/ ?9 r/ L8 S$ [- M; I
{
c: L, H/ B+ ~. j$ x, z6 ~int driver,mode,error;% ]6 ]1 W+ S" D* D; Q7 D
int x1,y1;9 I; Z* N! B! S3 Z
int x2,y2;" c( m2 ]1 L+ l" _" y4 b" @. P
int dx1,dy1,dx2,dy2,i=1;
" U& F2 V. d; U3 e! @int count=0;
# R( E4 n! Z, ?" W, g1 hint color=0;
8 `* |* r0 Z, j$ C, y8 j1 z8 F, idriver=VGA;& G* N8 w6 w/ h4 s1 @+ W( V1 {
mode=VGAHI;
6 z; a r4 {* w( `7 ]initgraph(&driver,&mode,"");
2 w- m- J" b B! zx1=x2=y1=y2=10;
( S2 B' T' W5 ~" e1 l$ Z6 E4 Idx1=dy1=2;
% w+ J; E- X& V* [; Ldx2=dy2=3;- n& [5 l L0 _, v+ T
while(!kbhit())4 l. u6 ]# F* b% F5 e4 r( a
{
n* q; R0 b) u) G/ s line(x1,y1,x2,y2);
1 e( X! z! @: r/ O G x1+=dx1;y1+=dy1;- f8 h5 w4 s" C
x2+=dx2;y2+dy2;
# J8 k( a; Z% Z; ~5 h if(x1<=LEFT||x1>=RIGHT)
0 N1 N+ o' ~4 D- Z9 {1 @- A T dx1=-dx1;- a5 a: x$ W. K+ M. e w- r4 @
if(y1<=TOP||y1>=BOTTOM)8 C+ B3 \/ g4 V% w7 l6 Y5 o J
dy1=-dy1;
- A) w$ A; K, ]' w% Y if(x2<=LEFT||x2>=RIGHT)( B# ?0 l" _. d( s) t0 p# w: B
dx2=-dx2;
; Z1 R: v7 @% x5 ]6 ^( w* D if(y2<=TOP||y2>=BOTTOM)/ K% l) h% B$ j+ d; M0 O
dy2=-dy2;4 Q3 i1 M" h2 h- T- k
if(++count>LINES)
2 A1 C! Z ]# K7 ?: x$ ~ {# j2 F6 b3 n9 R$ e) j
setcolor(color);8 W4 P. K! i' O( \
color=(color>=MAXCOLOR)?0:++color;( ]/ w2 w& T4 S. x* }
}+ I k/ [* F3 ^# H# c( I
}3 a: Y X. J& j7 M; ]+ ~
closegraph();# g! a0 ~2 b$ O$ V5 M
}
) G; H9 ]3 p# _0 [
【程序61】
4 q; [2 r3 C+ m. b题目:打印出杨辉三角形(要求打印出10行如下图) ( b* u) F. `9 ^. `0 p. s% E# _ F
1.程序分析:, S, P' s5 d4 \/ G: r# c( f$ C
14 h$ v" Y5 n" Z3 ^* E
1 1- ]: {! c, k8 x( O. a. R/ e* a" d8 Y
1 2 1# I2 M. F* q* [) T" `" X
1 3 3 1
U E2 H7 h* u7 U6 N+ J( E 1 4 6 4 1
$ I5 Y, u! W ?8 V9 |4 h 1 5 10 10 5 1
2 n6 c& ?- X% G/ n* v" [2.程序源代码:
* Q- ^# }2 @3 p- Amain()
- ?. X6 ~$ h6 e+ a9 p2 a# T. d{int i,j;
' s! E/ N8 R; m, u5 \int a[10][10];+ v; M. |5 v5 @
printf("\n");
0 A( v6 `+ q$ H! {- X* d! `for(i=0;i<10;i++)
0 O, Y) q; f! X) k% j {a[0]=1;
1 b$ u4 ~ L# _4 w a=1;}$ n& Q E. U+ D/ k Z
for(i=2;i<10;i++)
% a" e. q+ v/ s4 v! _; u7 L5 X for(j=1;j<i;j++)
- W. [6 L6 V) r S* { a[j]=a[i-1][j-1]+a[i-1][j];. i* ?( J7 V* y9 }; |0 D" W" j5 o+ ~
for(i=0;i<10;i++)
( x6 v5 i' T" H) ?2 V# y0 O5 s+ r) e5 ^ {for(j=0;j<=i;j++)
+ d/ U/ n! s4 B0 o printf("%5d",a[j]);
4 }- O5 T) M6 H0 z; w printf("\n");3 o% n3 J& c* k$ u) M* M1 K$ W& z
}
. c5 C( e7 r/ _4 n% c* E- i}
$ e: `! {% B% |; X
【程序62】& Q0 W6 A$ {6 v3 H
题目:学习putpixel画点。
9 h0 z; Y+ [* W( n: Z5 I( L1.程序分析:
/ ?( {- J, b a* k* u0 a; Y2.程序源代码:, F$ u0 {* ?0 x$ E( `
#include "stdio.h"; m: y) w: T$ d9 H! W$ F3 P" v
#include "graphics.h"
L% [1 m% d" | Bmain()! G& K9 N0 p1 V n, ~
{
- F2 f. q/ N! Aint i,j,driver=VGA,mode=VGAHI;, {9 z4 X0 ^' N% V" H& T& m
initgraph(&driver,&mode,"");, T& w! ^* A G' T
setbkcolor(YELLOW);
9 v0 ?) f5 v4 h$ r6 \, Z, G1 Lfor(i=50;i<=230;i+=20), l) o; ~# c8 s( `
for(j=50;j<=230;j++)
" k. |% } p; Z2 g putpixel(i,j,1);1 r/ J) P! t5 A/ q* `, d
for(j=50;j<=230;j+=20)% ?) i3 B" ~2 m& h J. Y) u
for(i=50;i<=230;i++)
2 ~# x! d) c+ Z' Y+ J/ [* ^ putpixel(i,j,1);
4 ~* v1 x& Q+ T3 [7 }" u) ~7 G}
4 L, R* v6 j. b- H; l1 Q! W
【程序63】
# O/ ~8 s5 \% x0 P题目:画椭圆ellipse - k* e, w# ?6 V5 f5 A# w2 y( N
1.程序分析:5 y# y# h" V1 t0 t
2.程序源代码:) Q8 ]$ F) G7 J% W9 T+ ]4 ^! U
#include "stdio.h"7 |0 j0 P" J/ F' {2 w4 h
#include "graphics.h"
2 `9 Y7 h& p- I) G: d#include "conio.h") j6 y" O% z- T2 p) X% ^/ E' d
main()! X$ e7 F2 s! p& I: j- a
{, B* u/ e9 L1 t2 e3 z
int x=360,y=160,driver=VGA,mode=VGAHI;
% e5 p* ?+ E$ m# zint num=20,i;
+ @' Z! _" n7 Vint top,bottom;
3 Z8 i: F1 {+ X# o, Q8 v+ Uinitgraph(&driver,&mode,"");' r5 g" E( s9 x
top=y-30;
) t( M1 K/ d2 Z& L7 {/ D- J2 nbottom=y-30;% n) n+ f6 v. ^3 f
for(i=0;i<num;i++)
3 X$ d* I, e* c{3 O4 }( P* Q$ g! t7 D- {$ ^7 [
ellipse(250,250,0,360,top,bottom);! a6 O2 m' ^. @8 N( m! O
top-=5;
# @ i* i: P( A; L' W3 Bbottom+=5; \5 `2 L9 e6 ]' a
}
9 z* P1 r4 f3 A5 V8 X& j5 xgetch();
2 g2 x! B; g0 E# }$ ^% P( s2 c# T5 I}
【程序64】
$ w; r; D/ X o' F6 Y! W题目:利用ellipse and rectangle 画图。
# G3 f& s8 I9 U: x1.程序分析:
& W ~; r$ `* K3 {+ i' S c: H! p8 @2.程序源代码:6 p7 L5 }& s+ }' i# U. [9 h3 y
#include "stdio.h"
* [+ u/ A$ I4 \, {#include "graphics.h"
8 c8 q+ s; q5 V4 t#include "conio.h"
, T' F0 d' \1 c5 imain(). s" o) D+ l4 x3 u
{5 h' U0 D/ D( z; _# e9 A
int driver=VGA,mode=VGAHI;
1 r. t1 }$ L Sint i,num=15,top=50;
: a- ^) f4 |& T1 s- g. O; sint left=20,right=50;
7 f* x4 E7 e2 c% k- Einitgraph(&driver,&mode,"");6 V+ [0 O2 w& k& \
for(i=0;i<num;i++)5 O9 e: `; `' q4 G9 L
{
?8 J4 O* C; Z6 G1 q7 O4 qellipse(250,250,0,360,right,left);
, `; \1 T1 i/ {& Lellipse(250,250,0,360,20,top);4 e$ H( I% `% F% ?- X% b% X- t
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));$ \- N; N/ [: E9 c* }6 c
right+=5; k j% T* @( M8 G
left+=5;2 q, I/ D5 @3 Q+ L( l
top+=10;2 W8 E7 j" a6 O0 t8 @
}
9 ?7 o* d Q3 C$ n: o: E2 Qgetch();
+ U$ J- X8 Q* j. ]$ D6 l6 ~8 s}
【程序65】
7 i" }+ N4 L E1 d! M6 [% ?" L9 t题目:一个最优美的图案。 " x9 {& c9 o) j4 L) l9 [' C1 j" ^
1.程序分析:% V/ D+ E/ C+ y1 p5 h/ e
2.程序源代码:$ }( [+ N) l. e2 B' p3 h
#include "graphics.h"
/ L4 b9 E9 B! C+ B; ~% ~% x1 T) v#include "math.h"7 V! ^& K$ H) o0 x: o: Q/ A
#include "dos.h"/ r6 k: O1 {2 z L, m, G8 j
#include "conio.h"
' C- f4 C5 r0 ?! w) s& a( s#include "stdlib.h"5 f$ v0 K# x& B `+ j* s b
#include "stdio.h"6 Q* B+ N5 L$ N
#include "stdarg.h"7 v- w) ^( O' J: E" Z
#define MAXPTS 15, D& N+ v" H8 a; R
#define PI 3.1415926+ T% {- g3 _8 [8 T1 E2 ~- h" k3 n
struct PTS {
2 j! r1 y0 {3 Q# Eint x,y;% s7 n( e @. v3 M; O# g$ v+ o
};
2 Q6 A1 [6 V+ ^; W5 I3 S/ Kdouble AspectRatio=0.85;( f9 x! G- Q* \1 }. j$ z! |
void LineToDemo(void)
! g' g9 {! z6 l8 m{
1 d: x9 Q( l! Sstruct viewporttype vp;
# O' N9 H: x5 D' Astruct PTS points[MAXPTS];
8 M" f3 w+ j$ }1 J& nint i, j, h, w, xcenter, ycenter;
1 s6 F6 a8 v. M# e1 i( p* n! g1 U9 @int radius, angle, step;2 ?% q, W. d) B, O" G+ R& E
double rads;4 `" Z' v! J( S$ V5 ]( d$ \
printf(" MoveTo / LineTo Demonstration" );. D8 |& u% O) s+ {3 D" y+ A
getviewsettings( &vp );# c/ v( C7 h; A4 L; W6 T4 s; X+ i1 ~
h = vp.bottom - vp.top;
+ c. }! ~: ?6 ^. c- {9 b& kw = vp.right - vp.left;
/ X' @8 Y7 H& a3 _, K8 {0 x5 xxcenter = w / 2; /* Determine the center of circle */, C& w" v; E& [6 c8 r u
ycenter = h / 2;
& `+ t3 V9 _) G# j7 w! Eradius = (h - 30) / (AspectRatio * 2);
0 B2 a6 @$ G& Lstep = 360 / MAXPTS; /* Determine # of increments */; X" [( w. h1 z! O( s
angle = 0; /* Begin at zero degrees */
( z) I* A7 ]& P0 s$ X0 Q; pfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */1 T, k8 l x: F5 v S8 x
rads = (double)angle * PI / 180.0; /* Convert angle to radians */8 m! T% n0 B' b+ q2 g8 |5 c% w
points.x = xcenter + (int)( cos(rads) * radius );5 [" S# C7 t# L# {3 o! u
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );( D6 ?( D2 X/ I. A% d8 [
angle += step; /* Move to next increment */
" [( L8 ]- f, u2 o4 Z' V- h- C}
8 X. P& `8 H* {7 r1 Rcircle( xcenter, ycenter, radius ); /* Draw bounding circle */
# T }) n9 Z! }1 p5 d& ifor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */2 d& a' W9 C: i1 k" m; `% [, h
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
- x: H# w u# M) O4 gmoveto(points.x, points.y); /* Move to beginning of cord */
: W' _- S3 T9 A+ elineto(points[j].x, points[j].y); /* Draw the cord */
- B. s9 R5 ^, I8 |4 y- h5 v} } }
- ~$ G* _1 G. P' W8 }* xmain() V5 g! Y+ s4 s2 e0 |
{int driver,mode;
. k' Q, @4 O ?% u4 `3 ], jdriver=CGA;mode=CGAC0;4 N* ^5 C+ z" [! }
initgraph(&driver,&mode,"");
! n5 m7 @; }7 o' J" Isetcolor(3);/ I# S0 l3 e+ H$ q+ n, f. {. L
setbkcolor(GREEN);
% l8 o: y. F! V9 yLineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
6 B3 } p# s0 I: R. G; P【程序66】6 o5 J* M9 l( r
题目:输入3个数a,b,c,按大小顺序输出。
# N8 V3 `8 j9 ]9 v/ s l s1.程序分析:利用指针方法。
; t+ N4 x5 t! H$ A+ }6 V2.程序源代码:9 ?$ N# r- _* `3 E" s- D
/*pointer*/6 }1 D- O. [7 @& }9 n$ k
main()+ a5 R+ ]/ o3 \- ^9 [$ }7 c
{
- U6 U; \8 @ k; p( ]4 Q; C' _int n1,n2,n3;
1 P' j9 r9 Q. f0 t" rint *pointer1,*pointer2,*pointer3;* Z, d2 |. P% K. n3 z
printf("please input 3 number:n1,n2,n3:");4 U# F6 ~! p/ B, A, i$ [
scanf("%d,%d,%d",&n1,&n2,&n3);
1 [. ^6 }; x3 Q& w: q# d7 P$ i3 epointer1=&n1;$ ~+ d" |) H9 g
pointer2=&n2;
7 @( z o: v! z! c7 @ P/ Vpointer3=&n3;* o2 X) L( T. S* {9 n5 w0 y. C
if(n1>n2) swap(pointer1,pointer2);: z$ C! X5 h' n. y$ h- x5 v
if(n1>n3) swap(pointer1,pointer3);9 |# S+ a. w) o, {4 }
if(n2>n3) swap(pointer2,pointer3);; P5 T" Z9 U. c# |9 d
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
' y! L4 d0 x* d! g$ D7 d, k}
# ]; I5 F& E+ R5 Aswap(p1,p2)3 w5 R# F3 { @; D0 A, d* S
int *p1,*p2;; j1 ? Z! f" t; ^. [
{int p;7 ~2 `; K" x2 I+ D5 y" Z6 `9 [* H
p=*p1;*p1=*p2;*p2=p;
7 R) ?( N3 ~$ `, @$ V8 _6 ^}
5 ^) S5 t. q# C5 E5 i+ T7 O+ s+ K- c