【程序59】
$ |, C: K1 y' u5 j: X7 v题目:画图,综合例子。
5 Q! l3 B) P: x, o& Q1.程序分析:5 g/ x" F) k/ v1 S" }" {
2.程序源代码:
2 M; f0 G' J' ]. S# define PAI 3.1415926
" ^' n# U4 \5 U4 q! Z$ i- |% ^# |+ g# define B 0.809
: v/ R. b- R/ L5 y7 w- s# include "graphics.h"7 C* }; Y: y$ @" {
#include "math.h"8 l5 Y, o8 T7 A, F' l( [
main()& j: |6 n3 ?, L& P
{
6 \& R+ K% R* x1 P, mint i,j,k,x0,y0,x,y,driver,mode;
* Z6 H4 [" M# x A8 C1 U" v1 jfloat a;
+ F; _$ d2 u# ~3 \ L8 ^/ Cdriver=CGA;mode=CGAC0;
! U, N+ h" n& X( \5 _5 E' oinitgraph(&driver,&mode,"");
' C r" O. I5 m& | }setcolor(3);/ ]2 ?; C. s& I2 x
setbkcolor(GREEN);
: H4 | I0 `1 h& o7 W4 j+ v- u" tx0=150;y0=100;) j2 Y4 F& y t, K
circle(x0,y0,10);2 ]# A7 @) F1 v& m \
circle(x0,y0,20);
- x$ _0 Y/ v8 ocircle(x0,y0,50);: Z- ?4 z0 r0 F' t k2 {
for(i=0;i<16;i++): E, k3 F9 i' w& \9 x5 ^
{3 ~' G9 B+ e. D" S5 m
a=(2*PAI/16)*i;
# [4 D. o% P2 [+ u x=ceil(x0+48*cos(a));7 c( @& Q, P7 k7 F3 q
y=ceil(y0+48*sin(a)*B);, l2 P& x8 t( L& {. u
setcolor(2); line(x0,y0,x,y);}
& v. _: P; ~% }7 A) [setcolor(3);circle(x0,y0,60);
$ Y6 ]) B8 A1 }( F% b8 r/* Make 0 time normal size letters *// @1 c$ L# j, G& \( c, H9 d
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);* U7 v m5 n- p* M8 U& x. G$ {* G
outtextxy(10,170,"press a key");
: r$ f) y1 k. u) j1 qgetch();
& l# Q4 X# i# _0 P/ |2 Vsetfillstyle(HATCH_FILL,YELLOW);
) a- E5 F( b/ D4 r6 B& }floodfill(202,100,WHITE);
" ?' h3 n6 T, [& C4 G% Vgetch();% |) p" J; Z2 A+ T2 _
for(k=0;k<=500;k++)
9 z- w0 L, o, f) M8 d6 c$ S{+ ^% ~: a/ g: d+ A, A
setcolor(3);+ v" D" T b% z% n b" v
for(i=0;i<=16;i++)
8 E+ p% W1 D: ^ {
* ]* {: I8 {( v6 c: L" n a=(2*PAI/16)*i+(2*PAI/180)*k;
0 N, s' a4 E% t& O5 L x=ceil(x0+48*cos(a));
5 o r) E$ O0 P: m: Y7 w y=ceil(y0+48+sin(a)*B);( V3 P( M# F: E2 Q. |
setcolor(2); line(x0,y0,x,y);) l O! G& l2 [* J7 |) G2 V
}
. I+ n* ^! R/ D! a1 L. X: ^ for(j=1;j<=50;j++)- x }( m& G* z2 t
{% Q# m$ C! s' d% t
a=(2*PAI/16)*i+(2*PAI/180)*k-1;
. Y9 {6 U: z @ h5 ^ x=ceil(x0+48*cos(a));
. x5 A" Q& `1 _& [; S1 J' L" h/ o8 l y=ceil(y0+48*sin(a)*B);5 _/ f- w" n* X2 `+ x6 ?
line(x0,y0,x,y);9 G2 L+ D a3 I7 l
}
$ u1 Z' e: g7 o9 n1 C/ z$ W7 d}
. [# f: n, j$ `4 h/ t* j2 @4 Zrestorecrtmode();. v9 P( S: }6 g0 d) t% k: F% c$ j
}
【程序60】
& V1 ]7 B! F1 d, E) M) c) {6 {题目:画图,综合例子。
6 v0 Q: J' J, U, W# e; U h2 o1.程序分析:; g! Y" f) ^0 s _# I
2.程序源代码:
& J+ V* q+ c1 Z" T$ d3 z) B3 ~#include "graphics.h"5 ^9 t. N# ?& V2 Q$ Y& @! a) ]0 Z6 ?
#define LEFT 0
% \0 f2 C+ |, _& a0 g9 o6 Z+ F6 O#define TOP 02 i/ N& u5 Z z; g0 j8 r$ N
#define RIGHT 639
8 L: q& m7 r" l9 N1 r/ Z/ u#define BOTTOM 479
# A1 i6 _2 m: o% r; C) ~7 U#define LINES 4000 q1 V! R! r8 s8 N. D
#define MAXCOLOR 15; |7 M) l" @$ o( V8 z7 e0 v
main()
# ^, `3 y9 _$ z$ V{
Z: P: M5 _2 `# d' Rint driver,mode,error;$ B" s9 z: S# ^" f
int x1,y1;# z# ^' \ p' _3 `6 \( T
int x2,y2;8 O! u- I7 T+ m8 m5 c N
int dx1,dy1,dx2,dy2,i=1;
. T4 z) |! \1 o* tint count=0;# |) }+ v1 y& p8 V
int color=0;
. [7 o6 v9 M0 A4 kdriver=VGA;
8 Z' b7 U3 g9 ]0 `% \# ^2 Mmode=VGAHI;# L# a% D% p4 s: g7 f
initgraph(&driver,&mode,"");
& M. c, k W0 H7 I1 S3 ?1 {3 Jx1=x2=y1=y2=10;# d0 E. c. q$ ~/ f3 y) x" }
dx1=dy1=2;
5 e! B5 a* G6 M. w1 _7 Q: Udx2=dy2=3;2 j* ]0 m- D- v* F/ {3 h
while(!kbhit()); [$ h( c: f3 ^. V" Y1 T
{: o, {) [9 r; @/ C/ ~
line(x1,y1,x2,y2);3 ]' b4 i5 v# T, M
x1+=dx1;y1+=dy1;
H7 Z: [# v: ]: i9 u x2+=dx2;y2+dy2;
" c8 ?/ |# F) f; N0 C' o- j if(x1<=LEFT||x1>=RIGHT)
3 j0 F. e/ G( d1 c dx1=-dx1;& G L" E# R! Y) ], i* {
if(y1<=TOP||y1>=BOTTOM)
$ G* m' u( S0 [ dy1=-dy1;
2 i6 I9 {; J' @5 c' x3 m# O$ Q if(x2<=LEFT||x2>=RIGHT)% U. l1 u7 J8 [1 M1 s1 t
dx2=-dx2;6 e. f" i: S" d& `( E
if(y2<=TOP||y2>=BOTTOM)+ q3 |* t, [" {. R, f
dy2=-dy2;, U. h n& k1 E
if(++count>LINES)
3 `( I3 g$ {- l {1 Y3 t _; L; i4 r3 E+ n9 L
setcolor(color);
' k! N8 A( _: ^# s# @/ K9 j# n8 f: ^ color=(color>=MAXCOLOR)?0:++color;
+ ~3 K- g" ^; @2 E2 M }+ U( f- o' b% Q8 L6 `$ z
}- [3 k. F, ?2 u
closegraph();
e) |% Z0 N4 Z, t}
# n9 @% D. P: n+ w【程序61】
' O2 w# ^ S8 S) W5 R, Q, c题目:打印出杨辉三角形(要求打印出10行如下图)
& x% g5 q3 |) A, ?1.程序分析:
]* c3 I6 h- T/ b' l 1. a" f# j" R3 {3 T- ~0 `
1 1( l$ W U8 m. [2 A+ m
1 2 1+ a3 B; ?5 c& R K
1 3 3 1
?. F7 N4 m# X9 X 1 4 6 4 1
. _) z9 u; h; r) u+ f7 L, A; f 1 5 10 10 5 1 1 h$ w' R7 H: E* ^& A5 w2 T
2.程序源代码:
1 K& m& g* M$ S F# vmain()
( c. Y. l- ^5 w* n. X{int i,j;
& n; _ l! a$ Xint a[10][10];
4 R1 w7 r5 |9 kprintf("\n");
9 O. t$ n+ W3 U: tfor(i=0;i<10;i++)
2 Y/ N- ?/ B' R1 k9 q+ s1 z {a[0]=1;
- I$ L5 _+ j9 a/ n8 [ a=1;}; w" | P9 [2 b
for(i=2;i<10;i++)* u. |- v2 `- h
for(j=1;j<i;j++)
L) \# k/ Q: X9 d9 F' h0 W/ S a[j]=a[i-1][j-1]+a[i-1][j];* ]7 H6 K1 x/ M2 n/ c2 I7 S
for(i=0;i<10;i++)
3 W) v0 a6 w1 z1 S1 @, u' v' Z {for(j=0;j<=i;j++)
2 M! L4 I6 M! Z0 i% y( p8 J$ W printf("%5d",a[j]);
, @% q' z) ^* E6 K/ S5 L+ Y% X printf("\n");8 W. @) J) D# t- _( r* D
}
2 _3 }( u0 H9 U}
7 R# Z0 b$ M% m1 k: N6 l
【程序62】0 i, ^4 x8 \5 B$ I
题目:学习putpixel画点。( v }* Q) h( S2 H- R) x! K
1.程序分析:
( R$ f$ [. R ^, r9 `. ~8 ~2.程序源代码:: A8 U5 O" r* M2 t4 P& r3 L# ^/ P
#include "stdio.h"' \; j$ W- B: A
#include "graphics.h"* j4 V7 I d, }9 Z; k
main()
. Y" c2 j) B3 E% f{
$ e/ | l: @% E6 zint i,j,driver=VGA,mode=VGAHI;8 q d* W; S( i7 l: ^3 V" O
initgraph(&driver,&mode,"");
) }- D1 e# o, }) P* U% f' S& Rsetbkcolor(YELLOW);
# T$ c0 P& a2 Q" |for(i=50;i<=230;i+=20)9 p) G& ?. g% V L
for(j=50;j<=230;j++)
/ Q$ @8 C; o" O# U putpixel(i,j,1);
! Y/ T E" t8 e; @# kfor(j=50;j<=230;j+=20)
) C* h% K' d; }& G for(i=50;i<=230;i++)0 U( v: v; s' l. J! ]* ^0 V
putpixel(i,j,1);# {/ w" c; c, c, c$ n& Q" v. t* s
}
! s, i9 P, o) x$ F
【程序63】* l: g- s' f' B
题目:画椭圆ellipse
5 O5 n4 a4 _5 w k8 _1.程序分析:. M5 R' }9 H! g0 x8 r0 H: J
2.程序源代码:/ f# n, V& a" ?, Z8 c: m- S( K1 d9 l
#include "stdio.h"
/ _7 o# u) f! ?2 K! s! `( `#include "graphics.h"
: h' B* \5 m7 `8 d( P% M#include "conio.h"7 G' h3 C* I& c7 c( c& E+ O
main()$ t0 i! S6 Y: N" ?
{
1 n9 a3 f9 ~" m4 ~; C8 cint x=360,y=160,driver=VGA,mode=VGAHI;
. Y* Y. W# k1 [# E+ oint num=20,i;8 L+ f- `. E) j1 Z
int top,bottom;" z# `/ D- X; E+ x @7 K& C- }" i
initgraph(&driver,&mode,"");! P5 a% w6 V* V' e" F( S5 M, g
top=y-30;
! a! y. d2 Y7 f$ X# i! o4 U" ?! Qbottom=y-30;) O6 E# X: @# W5 m- {6 A
for(i=0;i<num;i++)8 `8 F. I+ i8 f/ M% c( R
{
! Z T x: c" z8 J) D% iellipse(250,250,0,360,top,bottom);
4 `/ w+ A, `) n7 e3 }4 i: ]# Dtop-=5;
* L* d; i1 V' S* B: w4 v1 _1 tbottom+=5;
. D" [0 m& o# [7 v& I}
4 }: X3 _8 H5 Rgetch();
/ ` v [: a$ V2 g- B6 ]2 y" n}
【程序64】+ j% E) B) d5 Q# {' B! V1 a
题目:利用ellipse and rectangle 画图。/ q( X" ?$ ^5 Z* w
1.程序分析:! c8 ]& C( f$ c0 M" M% v2 ~
2.程序源代码:2 J6 ?/ ?5 ~. J' o
#include "stdio.h"7 k& x9 k! I0 c; h
#include "graphics.h". B- |. k K; f: s
#include "conio.h"( k. y+ _" Q# B4 J: }& s
main()2 e! A# w5 H8 m7 i) b v b. j
{
5 H1 b: T6 y8 K1 A* ]int driver=VGA,mode=VGAHI;1 W+ f% R- P+ }$ B# E; b2 z
int i,num=15,top=50;( h% t0 M0 _% q! B C) o. g9 \
int left=20,right=50;, O3 r* r0 H. R+ S$ I1 {" ?
initgraph(&driver,&mode,"");
3 g8 ~. Z$ H0 @4 pfor(i=0;i<num;i++)0 S' ?+ m3 T7 @ Y% L6 M5 U
{
! g& w T/ W% Rellipse(250,250,0,360,right,left);
" J$ c: l+ w2 v+ E6 m3 @) xellipse(250,250,0,360,20,top);8 [+ i) V- y" J. _: a( i7 ~
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));, ?0 C3 u/ N7 t
right+=5;
9 O0 s4 p# V" e1 O+ y- Fleft+=5;3 d- ?! R1 j! ^( u
top+=10;, U0 w: s7 A( ~" q% `0 w
}. U, H6 t# n2 S
getch();
/ p6 B5 i& ?) d4 S( Q! m, J7 r4 \}
【程序65】
& J9 C8 E+ L5 p- E! i题目:一个最优美的图案。
2 ]* X; p/ e3 E1.程序分析:4 q3 K. J4 [$ B+ I
2.程序源代码:
7 v2 m, B d3 C3 S3 a8 j F) W#include "graphics.h"
! \6 n7 `; ^; j( L8 ]$ Z8 M#include "math.h"
- c5 J# ?. h3 k1 q#include "dos.h"
$ Y ]' f/ v' [" s6 N E$ v5 F#include "conio.h"- R# n7 j9 F: W" E5 b* n
#include "stdlib.h"
, b2 A! ?5 a+ o6 m2 P% F#include "stdio.h"6 m$ J* s! o, N' m& |# {; X% g/ p
#include "stdarg.h"
t. t5 O0 C Q2 p8 F#define MAXPTS 15* U! F! [+ Q6 ]* g1 F) y, I0 G. \
#define PI 3.1415926: r. R, `& r: S) A
struct PTS {, x3 o! }& E# p4 F4 y' m
int x,y;
1 p- O+ r1 a, ^6 _. O6 ]};
6 F, ^$ [1 b k1 _* O( y) e" A$ Hdouble AspectRatio=0.85;
# f* [* v. S0 {2 W! y4 W+ N8 ^void LineToDemo(void)" N3 P0 z! z$ }0 W
{: I4 M- }+ i7 X; ? w; G
struct viewporttype vp;
' o/ P' F. M) b! h, K7 m3 L* xstruct PTS points[MAXPTS];
' _8 |! w; O5 {% v- i: I4 n! m2 Gint i, j, h, w, xcenter, ycenter;
: L: y, i+ i0 w% }' J. X7 [; s9 f+ {; pint radius, angle, step;; p* z# y: {; @+ a& r
double rads;4 B5 q% ]5 S" V4 C, S9 ^
printf(" MoveTo / LineTo Demonstration" );2 `! C: F# d0 L1 g8 ]1 X& {& `
getviewsettings( &vp );
/ Z6 Z5 R5 a5 R& U/ ^3 L# @h = vp.bottom - vp.top;$ N' _8 [/ o/ f: Y4 P
w = vp.right - vp.left;( ]+ L! V. w1 ^
xcenter = w / 2; /* Determine the center of circle */
( _, @, ~6 F' U" Z. {( \; O8 _ycenter = h / 2;
0 s, _ ?! g' [: s0 o7 h3 hradius = (h - 30) / (AspectRatio * 2);
8 {3 R* O5 w% o! Q, [1 M* C* d ostep = 360 / MAXPTS; /* Determine # of increments */4 r% D0 A6 ?' N+ R- E: X+ I
angle = 0; /* Begin at zero degrees */
* e. |" c8 h* c# P, Lfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */6 m c* `! g0 b' N0 C/ r6 Z
rads = (double)angle * PI / 180.0; /* Convert angle to radians */$ \# W1 Q6 ^& B, n' P8 e, z' U
points.x = xcenter + (int)( cos(rads) * radius ); f" r- W) L, w p
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );) T# ^0 t4 Z. V; ?$ R1 E. }
angle += step; /* Move to next increment */
p( W! T- F/ g: g2 \! F1 G}1 o8 G5 X4 l- c
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
% J+ s5 {" t: C# O0 M5 D# Vfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */ n1 U' m/ h$ U) A
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
+ m4 z5 `2 C$ Z5 gmoveto(points.x, points.y); /* Move to beginning of cord */
" Y r8 C" Y# u$ Llineto(points[j].x, points[j].y); /* Draw the cord */
D" V$ o& o/ _4 S' m} } }- x) x: z Y" T/ h. |9 ^/ `
main()+ W3 g$ X& n. I* [& [
{int driver,mode;
8 w9 A' b8 N- t$ x/ }driver=CGA;mode=CGAC0;
8 [6 x: A% {, @- tinitgraph(&driver,&mode,"");
- u- K1 g1 q7 R5 h) z$ n% {- D# hsetcolor(3);
+ _1 x+ w3 w% Z- Gsetbkcolor(GREEN);5 L. u# k9 p$ V
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
a7 Z# k+ g; C& g/ X- J【程序66】
4 S' u4 S" q8 ` ~题目:输入3个数a,b,c,按大小顺序输出。
/ a; T0 c+ ^/ q7 Y6 n s' |; d; t1.程序分析:利用指针方法。
8 T" a1 W" H9 f! a- }5 ]! A2.程序源代码:
- A2 p- c0 L4 G: M* D/*pointer*/ }0 }0 C. c% O. t$ i; i' x
main()
3 d$ j/ g: T5 l# \% c j0 c0 a{6 {5 Q9 q+ g4 {2 L( M
int n1,n2,n3;- \; n! l* D" i( r% |( l3 e
int *pointer1,*pointer2,*pointer3;) q- R0 X4 W* R+ @/ Y
printf("please input 3 number:n1,n2,n3:");. A5 \+ f5 n# g- [. [/ N: W, h
scanf("%d,%d,%d",&n1,&n2,&n3);$ r2 Z( E W6 P# p, e/ Y
pointer1=&n1;4 f% p2 }$ r6 i& D( b5 \. {
pointer2=&n2;
6 h! g4 [+ O# V A. rpointer3=&n3;$ N5 j3 v! b. A, b
if(n1>n2) swap(pointer1,pointer2);
; x! G" }! O3 g- r$ m% vif(n1>n3) swap(pointer1,pointer3);! d | d4 v0 H* o
if(n2>n3) swap(pointer2,pointer3); R0 f) ?* t- Y$ e/ Y/ H- x
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);% z7 L3 _- f1 N% {3 O
}
8 X' S' [ K! ~+ sswap(p1,p2)
% C% d9 ^$ B' M0 u' }int *p1,*p2;
. n% Y* K$ q8 x1 S) M8 w{int p;( j \6 K& U5 V' B9 I" E
p=*p1;*p1=*p2;*p2=p;/ d! ^$ t" L" B* ?4 Z' X" Q
}( I& S' e$ A/ r