【程序59】
+ q4 X1 X7 i4 M! M* J题目:画图,综合例子。
% j. K3 {* e* Q. ~8 M1.程序分析:: U+ z& i$ j6 g% ^. X
2.程序源代码:
+ y& k" k1 t, H- Q: v" {# define PAI 3.1415926, z9 O$ o, E) \0 v' o0 [
# define B 0.809
C8 J/ I, F1 P& R# include "graphics.h"
0 l# _# P. M. W' k8 U: Z#include "math.h"$ s+ [( X% v& e% F% {) v' {( ^. K
main()
( p6 r' m! E/ r R6 \{
6 Z0 c' D- @8 T5 l/ q# dint i,j,k,x0,y0,x,y,driver,mode;
f' w" U0 m5 c# z1 i4 }7 ^# vfloat a;
a/ m" {+ F2 K# idriver=CGA;mode=CGAC0;
- I2 k( a! @7 p; r- binitgraph(&driver,&mode,"");
3 F! p6 b1 h( l; E7 Xsetcolor(3);
( ~* I1 s+ F, }/ b) D4 a3 isetbkcolor(GREEN);7 J, ?' _, y9 b5 Z3 o
x0=150;y0=100;
$ M3 U' Y, _0 _& g, ~4 j; mcircle(x0,y0,10);
# b: g9 s9 @0 J- E! tcircle(x0,y0,20);6 t+ ]' Q( z* k: i/ b
circle(x0,y0,50);
, i2 n( t/ n4 `- E7 |! r7 Pfor(i=0;i<16;i++)
) @2 M9 l, |" p( o{4 V8 s9 @# q- M% {" \+ k, n0 \
a=(2*PAI/16)*i;% s: e0 I" T& O5 Q. O+ w
x=ceil(x0+48*cos(a));
/ v3 l+ D2 y# p- Y y=ceil(y0+48*sin(a)*B);8 Y0 j8 C% O5 |( C* x
setcolor(2); line(x0,y0,x,y);}: n/ c0 q- r6 j# r5 r
setcolor(3);circle(x0,y0,60);
& x4 G0 L3 v+ @/* Make 0 time normal size letters */
' ]% M: c0 s5 Zsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
, ]: h! p" ^# Q3 Z2 ?: `- j3 L! {outtextxy(10,170,"press a key");* M* q3 {! k/ g8 J( ^7 U. Z
getch();
0 G; F% i: h6 Ysetfillstyle(HATCH_FILL,YELLOW);1 y/ U0 z- [* `$ U8 m% t
floodfill(202,100,WHITE);
* i. X3 ]8 I( ?! I5 R# {: a" ugetch();
+ d6 N: R0 C* x' S8 Yfor(k=0;k<=500;k++)
" p1 q2 F2 o4 F; p) c{
( t) Z2 R+ p+ D- r/ m) R% J( A j setcolor(3);
# H7 o3 E5 D: ? for(i=0;i<=16;i++)
7 B: t9 `7 L4 w+ g8 z8 s {
% {5 o5 L a# k, _3 V a=(2*PAI/16)*i+(2*PAI/180)*k;
4 i& p+ |. n; J4 z$ K% V x=ceil(x0+48*cos(a));
, M8 D' y$ T5 Q# C9 R, O y=ceil(y0+48+sin(a)*B);0 g8 D6 j+ b1 [5 {) `
setcolor(2); line(x0,y0,x,y);
8 `; B( H3 Y, }2 b5 G( e5 U }% B7 `% t. B: j7 V( ~
for(j=1;j<=50;j++)0 ?! J" J+ [. Y" n ?. x2 ]
{2 g( g8 b' R2 ~% U: ]
a=(2*PAI/16)*i+(2*PAI/180)*k-1;+ B# g5 w; o. j3 H& X0 T
x=ceil(x0+48*cos(a));# p8 Z- o Q1 |7 @
y=ceil(y0+48*sin(a)*B);8 g6 [; d. X0 X* T
line(x0,y0,x,y);
/ B! x/ V. u1 J; s' L5 C( W% I) x' q }
& @) C1 D/ H" o/ A: _$ J/ S; V6 D/ A}! S+ _! z" X3 f3 _+ X0 ]/ q
restorecrtmode();1 U) q4 g* |8 C1 n+ T
}
【程序60】
* J% y/ Q8 Y S: w题目:画图,综合例子。 / o3 `% j; q! B9 q0 f3 Z
1.程序分析:) X& y6 ^/ [! ~/ r7 ^
2.程序源代码:
( ~ v4 u* ~9 [. X* s ^ X9 g/ y#include "graphics.h"# e8 Z) y$ G% [8 g" g
#define LEFT 0& }* w/ G& m* o, L" n7 {) h
#define TOP 0
/ x' Q# x) ?( O$ T$ f#define RIGHT 639
: f) X. m+ Y! O2 {+ F#define BOTTOM 479
7 f7 h0 Z; K) n! ^" k. H#define LINES 400$ k( Z, _) j; h h/ G
#define MAXCOLOR 15 e/ j0 b4 @' y* [ h7 D% \& T
main()- i' K8 a5 X& S1 Y) b
{
. Y3 M/ z4 R4 Zint driver,mode,error;0 E' N" k, b. n# Z% r# @
int x1,y1;
1 L, U) b1 v3 |7 O5 w) _int x2,y2;
! @- S& T* V6 Z) e; ?( n- g& S- b- gint dx1,dy1,dx2,dy2,i=1;
' E$ h8 J5 [4 A |+ {; Bint count=0;% r' q8 _( h8 f' a! I+ @
int color=0;
' N. Z7 G X: odriver=VGA;/ N% w$ F# w1 U* L$ x& c
mode=VGAHI;
1 w8 {! ^* P% Z/ W5 h- d$ Finitgraph(&driver,&mode,"");' Q2 [ C* p# l n5 f
x1=x2=y1=y2=10;! ]& J9 F/ v: Y: O9 G7 O5 C/ O- Y
dx1=dy1=2;3 q3 i$ E1 M8 ~4 W2 b: s
dx2=dy2=3;
5 Y( f4 e) U: l! Q7 cwhile(!kbhit())
7 m4 X* I( I6 `$ N) E{. @+ G7 K) g; E, z- n2 H
line(x1,y1,x2,y2);. o* V( O$ W7 g; O- v' `
x1+=dx1;y1+=dy1;
5 ]8 i8 f; Q5 m G5 C$ k$ y x2+=dx2;y2+dy2;
0 i6 v; `! N: ?! _& _ ] if(x1<=LEFT||x1>=RIGHT)2 p" b% `/ S( _5 ~8 k
dx1=-dx1;
3 s$ h2 a( n0 b, s) i9 b+ E8 |2 G if(y1<=TOP||y1>=BOTTOM)/ F# `: g" S9 `% {/ N+ U
dy1=-dy1;
* b- I$ B; ^8 V4 `. A( O; A/ K if(x2<=LEFT||x2>=RIGHT)
2 a% p! r% ?+ @% @1 ` dx2=-dx2;6 H4 f2 Z. `% O* R& Y
if(y2<=TOP||y2>=BOTTOM)- r: F0 J0 @4 X0 g I
dy2=-dy2;7 w( X7 r8 I' b$ K: m
if(++count>LINES)
" h- T3 W% Y5 c. s {4 e" D& |' K6 P% \, i# |0 q0 n h
setcolor(color);
7 F* k, ?3 y, L& W3 a$ h* M color=(color>=MAXCOLOR)?0:++color;
1 e8 T- F- b5 B) F% I" e# } }
' G& y' f+ {& ]1 q% @+ u {}
- A+ B+ l5 r$ `/ `closegraph();9 {/ _1 M* k; ?! c- o$ }5 D
}
7 }! W6 H7 X' {6 |% X* g$ [【程序61】8 M5 \4 p2 l2 k
题目:打印出杨辉三角形(要求打印出10行如下图) & o' J% R$ o$ C0 z i
1.程序分析:1 U! }: M V! T8 q! w% l
1
+ ?9 E9 T: n! |2 k! k 1 1
( ?& H! Q2 f9 r; ]. A 1 2 18 g$ B9 F9 J9 y a- ^: F
1 3 3 1
$ Y1 B7 a5 C' J f/ l 1 4 6 4 1
i; N9 l3 @' i% M; y% E 1 5 10 10 5 1
% D+ x# u, W2 P( T# w2.程序源代码:6 I. O, E- R' M/ }+ w* F1 f
main()
& O" G5 D4 A1 t1 \2 X{int i,j;
5 Q4 r; K# S- V( @8 Oint a[10][10];% o) d9 r2 h/ U/ S2 ~3 ~* n( E* R
printf("\n");
# j N# o# a+ S& E2 X; Bfor(i=0;i<10;i++)0 L, Y2 C& ]; j
{a[0]=1;( H% E* M4 S3 w3 X* a4 y3 c- y
a=1;}
. t% P2 C6 J4 u$ g6 [for(i=2;i<10;i++)1 k+ U3 h5 h6 f, k; T. E
for(j=1;j<i;j++)
& h6 T' l; ?0 }6 m+ P a[j]=a[i-1][j-1]+a[i-1][j];
l2 G; \8 {" ^1 T! cfor(i=0;i<10;i++)' P, W0 ^( M+ C
{for(j=0;j<=i;j++)
3 |/ ]1 v7 R5 Y* J! K0 w printf("%5d",a[j]);
0 u, b4 ~6 Z6 u printf("\n");
$ J% G( x* H3 M/ H; V' a }
, j0 p/ y+ N4 m# b! Z}
2 C4 a5 K! Q! I9 R5 V, E
【程序62】( H2 \; I' D! i
题目:学习putpixel画点。
- }4 ?; N$ H3 u; w4 l$ u8 e1.程序分析:
9 A$ O) L; z4 L5 z% w8 T- S2.程序源代码:, T3 v0 u% R/ }
#include "stdio.h"
2 h$ K- n; k0 H! Q' W4 p3 u4 p#include "graphics.h"
& S: Y: s! M9 y" D) s: m- i4 n( Umain(). F( U4 h8 m# [7 q' G6 q e" L
{
' C7 D) ?2 M* j# m% Z. E$ D$ {int i,j,driver=VGA,mode=VGAHI;
1 O2 r1 u& v! V) v1 binitgraph(&driver,&mode,"");
. v: p$ B! }3 ~' S7 ~2 Y a7 ssetbkcolor(YELLOW);
# d2 O8 D$ O3 x3 I- X* _; G+ yfor(i=50;i<=230;i+=20)- s; B9 K3 ]6 b+ A7 r
for(j=50;j<=230;j++)1 v+ C B) m/ o3 R/ n
putpixel(i,j,1);
: W$ `. p6 v# Z* \6 s- h0 ~4 \' }for(j=50;j<=230;j+=20)) i5 `( }% G4 _' _0 Z3 H
for(i=50;i<=230;i++)4 U3 V# g: z% D
putpixel(i,j,1);
2 F! v& F8 \8 d( L1 [ |! H' f}
- i7 q1 b9 v# j( r- l7 ^
【程序63】
6 [1 H% g5 A8 W- c% ^8 ?- O, Z% {题目:画椭圆ellipse + b% M; T+ p- r$ Y7 [5 O# C
1.程序分析:
. O M+ S; y" c, r( a2.程序源代码:
! ^1 i& g" V# {0 j#include "stdio.h"- h2 f* j" ^# I; x# m3 `
#include "graphics.h"
3 J' M4 f- c4 _1 u#include "conio.h"
' a/ \7 d9 H- g1 j) ~9 \% Y# R0 lmain()$ G7 G9 _6 ]; R6 b' ]' J. C
{: F7 n3 H0 t. Z5 K% `3 \# m, P6 c
int x=360,y=160,driver=VGA,mode=VGAHI;
* ?9 z5 |& @$ o- Q+ s _5 Yint num=20,i;
, l$ @8 s$ X6 ~, _& Q" yint top,bottom;- c& d1 [: C6 x( q
initgraph(&driver,&mode,"");
! _" _. j) T4 I" I- v, ]$ Ttop=y-30;
8 b, g, g; l7 f5 i( r/ h5 r. E7 [bottom=y-30; d; ]6 @- O" Y4 A' B. }' a( _
for(i=0;i<num;i++)
* s; O9 N! K, ^6 P$ P" R$ W) q# r{% `: ]* ]! c2 d& U4 c
ellipse(250,250,0,360,top,bottom);
7 e$ s6 w- |6 w: e3 D+ jtop-=5;
( g* p- F4 X. b5 S! D! F% [4 C- ?bottom+=5;# L' d+ w" b. o h2 ^# t: g: H
}
% v+ `' v' }7 Qgetch();8 l, j% `8 {- J5 K7 v$ P
}
【程序64】
: W' W9 c# d/ a! R题目:利用ellipse and rectangle 画图。
% ^6 g/ Y: G2 \' V' K. r% J/ w1.程序分析:
+ x2 g8 a0 j0 v# b2.程序源代码:
7 v: @, [9 C6 c# f6 G- \ `#include "stdio.h"( g4 l5 i: r' A1 f+ J5 c9 Z
#include "graphics.h"1 f: D) [" B) Y2 q+ ?9 W
#include "conio.h"* I+ W2 T7 Y8 T
main()
+ V9 U0 r' U" ^{! W9 x6 K2 H2 U$ c, q& [
int driver=VGA,mode=VGAHI;# B& n P: h4 B5 K
int i,num=15,top=50; ^4 v8 M3 z: I" x/ v" M& R
int left=20,right=50;( _ Q" t/ h9 t) P) g. D/ W
initgraph(&driver,&mode,"");" B! j9 ^- m8 a8 b$ f
for(i=0;i<num;i++)
- Y# r* C# w; ]: Z* F# a5 A3 |{
, o) t) `* X1 [+ Uellipse(250,250,0,360,right,left);
2 R0 w; p3 u3 ]* N5 ^" t. [* s" fellipse(250,250,0,360,20,top);8 s7 y5 r+ b9 g3 N; S1 M
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));& O. ^- k( o; R! O) r H
right+=5;% E) B+ a% m9 X' o% q/ R. l6 h( G
left+=5;0 Q0 M0 k0 E& M; ?' Z3 A
top+=10;
1 D! o6 M) n/ P3 O% H+ g}
7 { t/ l5 D8 G4 L/ T* ~getch();, I: @3 |8 w1 l2 a2 @
}
【程序65】# {. U& |. @# f
题目:一个最优美的图案。
3 w, U4 O' [8 k5 w" D t1.程序分析:
9 A. u2 R! H( M8 i2.程序源代码:
2 B0 e) _& M" v' J% F& Y# w5 N#include "graphics.h"( I1 }2 J" u$ u
#include "math.h"
) n7 z' I% g: L a( J#include "dos.h"$ T5 v- a# U1 S9 J
#include "conio.h") |9 q9 h. _! @: O) Z
#include "stdlib.h" `, ?. k/ g. D. c3 b. W
#include "stdio.h"
6 W! t: }- j* d0 R#include "stdarg.h"
+ N; P9 B9 I, n' w O/ x#define MAXPTS 15
+ l# F- M+ b( R( y6 _. ?% O#define PI 3.1415926$ ?* l2 O0 R& M. O4 Z9 i# j
struct PTS {$ p3 |$ E' G$ N! u4 y7 u
int x,y;+ A t6 L; Y6 I8 i p+ V
};" C& |+ s- Q: A' v# r8 ~& d
double AspectRatio=0.85;9 O( I4 T9 ?- ^0 v
void LineToDemo(void)
0 P" J* r: s( ]: K0 W{& C* H- y1 { h, P+ L
struct viewporttype vp;+ @( D3 C. h% X- ~; E
struct PTS points[MAXPTS];
, j% {: b% c: e4 O* hint i, j, h, w, xcenter, ycenter;* l6 q1 h3 S, ?0 G) {+ Y3 d1 m' y
int radius, angle, step;
, |8 A' m% I1 t6 }6 rdouble rads;
/ }. K: P: N% S5 Z3 {) Rprintf(" MoveTo / LineTo Demonstration" );
k. {. v7 k0 I+ B* o3 sgetviewsettings( &vp );2 v6 k* l& h/ o0 f
h = vp.bottom - vp.top; ?$ w, H) G0 \0 T% ^3 p3 s- t
w = vp.right - vp.left;$ E" t# n# Q3 q' ?( {) x7 L
xcenter = w / 2; /* Determine the center of circle */
1 t6 w) ?* L( rycenter = h / 2;
- t x1 x- X0 P) O8 u- H/ T6 R" @radius = (h - 30) / (AspectRatio * 2);
- i$ C6 O7 e4 \" e9 ^5 h* Zstep = 360 / MAXPTS; /* Determine # of increments */# Y& f8 ^0 B/ y& M9 T4 ~
angle = 0; /* Begin at zero degrees */0 j5 D, {& Z) p, b$ L4 s/ {" i
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */0 t* I9 e8 }+ u% X( r& r- W! A
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
# n% b# E% t+ |9 w& s Npoints.x = xcenter + (int)( cos(rads) * radius );( G* p) K0 H9 L0 p0 D
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );. S; @. P7 F" w
angle += step; /* Move to next increment */
2 E; ^4 f( G# U# U/ [% g" `2 h8 @}$ J1 a' g: U$ D; j3 S8 ~" t
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
* ?) {! B. ?# T1 v# Ufor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */! r0 V0 G- ^0 ?( D& d' Y
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */0 [5 S% p7 n. U% |4 g1 d& [; R8 p
moveto(points.x, points.y); /* Move to beginning of cord */
: V# N# P/ `/ ^$ K' `! A2 v ?# {1 Ylineto(points[j].x, points[j].y); /* Draw the cord */
; i) M. i. c* J2 h9 @} } }
+ m! V2 e7 j m4 u# p K# amain()0 f1 y# ?' l# a3 S" z* ?* i$ b
{int driver,mode;+ g. X5 y. [3 p: T3 C. s
driver=CGA;mode=CGAC0;8 W1 Y5 p4 ^" u( Z2 G9 j
initgraph(&driver,&mode,"");
4 D% W5 @6 ]$ e8 Msetcolor(3);
2 }+ n; `9 A7 K% Vsetbkcolor(GREEN);1 y) _" o" z" _/ {# A
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
, U: w9 F7 v* Q
【程序66】
* C( n. I5 a: Q, U8 |+ W6 Q H题目:输入3个数a,b,c,按大小顺序输出。
( M9 l" R! p7 a1.程序分析:利用指针方法。
' G& d0 S. n+ r, Q2.程序源代码:
2 w0 o1 t* h, E! m6 b( o E/*pointer*/7 m3 m$ y5 A7 A# `/ W
main()! ` Y) _" N& Z
{& \. @7 t% C( A* }! n4 ~9 t$ N
int n1,n2,n3;
2 I# r2 q2 V: Y2 v5 t8 |" |/ t) Oint *pointer1,*pointer2,*pointer3;
7 m/ e: L+ B5 q9 H% Nprintf("please input 3 number:n1,n2,n3:");3 s7 G t' p+ ~- R
scanf("%d,%d,%d",&n1,&n2,&n3);
z' s- Y: l4 y Z2 \3 apointer1=&n1;2 o1 W: o6 A+ [% q/ j9 T
pointer2=&n2;
3 v6 |! x* b2 x! S, u+ Zpointer3=&n3;
. [9 ]# z8 s- _, ]! aif(n1>n2) swap(pointer1,pointer2);) P, `# a A" k9 ]* n6 @, u9 {
if(n1>n3) swap(pointer1,pointer3);
) Y( n9 f9 Z6 t( G( Sif(n2>n3) swap(pointer2,pointer3);8 _) o8 v o: K1 p
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
8 q o6 O0 ?" ]7 w0 Z1 q, D}; s& Q; n( J3 ]% t' E% v
swap(p1,p2)& t6 M, ?2 l, I/ k9 c" |
int *p1,*p2;
+ d& `& W n7 P9 ]9 A+ ^9 D }{int p;1 M( t8 D4 j% l
p=*p1;*p1=*p2;*p2=p;
5 x3 a" |+ Y+ U# _. h, t; Y6 ?}
G& [) u9 D# v* q; B