【程序59】# G* ^/ g( K/ R+ T' ]7 M4 `$ P
题目:画图,综合例子。
& x$ e' b) j8 U/ J$ M4 T- `; k1.程序分析:
- g! i) G; ^% R) [! z' @2.程序源代码:' j. t/ }' d. M+ T6 L0 o& u0 l% L
# define PAI 3.1415926
0 i# D8 ^- v$ s! s# define B 0.809
9 q8 }! O$ V ?- h1 M# include "graphics.h"; ~5 x5 P' p, g
#include "math.h"# R0 n4 R& y4 {1 @
main() Y- [1 I2 X- f/ B
{
6 d1 J% q" g9 k6 \4 X; d' a% Oint i,j,k,x0,y0,x,y,driver,mode;
: X% G8 ?7 g X+ nfloat a;
0 I$ T5 S( y ^) B% U$ Fdriver=CGA;mode=CGAC0;( \9 h6 `: S7 Q" T8 Q3 [
initgraph(&driver,&mode,"");
# w v" R% y; D" p7 y( i3 fsetcolor(3);, N- i6 g$ D! R
setbkcolor(GREEN);
6 ^" g H4 }/ r$ F) T# Jx0=150;y0=100;
% X( g& g- A. \circle(x0,y0,10);( @4 Y4 \2 w- v9 P
circle(x0,y0,20);, o) U/ w% f! K- I
circle(x0,y0,50);
! W" l# `5 I5 Pfor(i=0;i<16;i++)7 s. T6 Z3 s' g3 s
{
9 Y V! ?4 i# _. [ a=(2*PAI/16)*i;
6 h& k* p! |5 L! ] x=ceil(x0+48*cos(a));" a R* H2 U/ L
y=ceil(y0+48*sin(a)*B);9 [) b; ?! C* a7 {% O2 K
setcolor(2); line(x0,y0,x,y);} a" O* h; R+ d7 M2 N* o7 D
setcolor(3);circle(x0,y0,60);* d: `4 ?/ |9 C, C
/* Make 0 time normal size letters */
. u ?5 W: x- ]0 f# Csettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
}9 m3 n4 k, e$ d) _) d( ]outtextxy(10,170,"press a key");
, E4 t5 B, J% d! w) I! O! sgetch();! }( L+ H& ]4 U2 a
setfillstyle(HATCH_FILL,YELLOW);' a' s6 t/ `; N2 ]4 \. \& ?
floodfill(202,100,WHITE);
$ S8 Y2 F4 L$ q, f6 agetch();% O/ Q& Y( U: \$ `/ s' c5 {
for(k=0;k<=500;k++)
. c. Z9 P- m; \{0 x: T0 z( E" W0 w* f; ^9 U: o
setcolor(3);
6 _$ Z, Y+ Z' q5 G' S$ { for(i=0;i<=16;i++)4 y! f. k8 n7 I/ i8 M
{
9 Z8 a: K, i) ]( j6 F a=(2*PAI/16)*i+(2*PAI/180)*k;
7 j. X' w. Y/ p# C x=ceil(x0+48*cos(a));
% A9 }& s/ P4 S& [" T y=ceil(y0+48+sin(a)*B);
! W8 Q% j7 z u0 O/ y- e5 g setcolor(2); line(x0,y0,x,y);
/ U) [% A* t' i2 l }( {) |- D7 p6 I- K- D# ?/ e1 d5 l
for(j=1;j<=50;j++)
0 M! L7 p/ j, i# F p {
- d0 p5 s8 x* [+ A# \: u a=(2*PAI/16)*i+(2*PAI/180)*k-1;* E3 @) I8 i* h: j6 N
x=ceil(x0+48*cos(a));
- N# f$ R. l! M5 `: u, { y=ceil(y0+48*sin(a)*B);2 p# \6 Q G) i% W
line(x0,y0,x,y);& ~) }) b7 s/ v7 B
}
. T+ l1 u7 f" A% Z}
/ T+ J- U" \) v3 d& X7 J9 trestorecrtmode();
) O$ L& q, B( O c6 O# S* @}
【程序60】! |6 s7 V" w) `' O$ w, {7 N* ]) n
题目:画图,综合例子。 . U. C& v7 R, C# \- G2 u& [) [
1.程序分析:
- M; t" k1 {0 ^' [7 \2.程序源代码:& `! |" O; e4 T+ ]
#include "graphics.h"
0 `* h- A/ U- j( _#define LEFT 0' j- q6 r* o: J" \ j
#define TOP 0& ^& E( |3 w9 E! X6 Q8 h
#define RIGHT 639
( b' r$ F* n' _4 q" U2 k#define BOTTOM 479
8 ^2 Z' G1 |: `0 b6 j#define LINES 4009 g q5 v8 A* ~' u: C/ y
#define MAXCOLOR 15
, U) s- ~. V; U7 H5 p/ n8 Nmain()
K' H- }3 ]" B) h{
) m, x! I5 m$ ^' R/ H- Hint driver,mode,error;
" ^$ x' v- ~3 |) O1 Y% Sint x1,y1;) m+ O. k+ H7 U/ `8 ?5 S# v) W3 b
int x2,y2;/ ?0 F3 f# w$ z- T& m# b5 Q
int dx1,dy1,dx2,dy2,i=1;" c! M3 G5 i! j' f0 w3 @
int count=0;9 n$ L* m% Y- I2 P$ E
int color=0;/ u9 a' K L! ~
driver=VGA;
- |% B/ _; N# F* R; Lmode=VGAHI;5 D# G/ n- }, U0 B
initgraph(&driver,&mode,"");
4 U1 j' J& F, u$ ~" ox1=x2=y1=y2=10;
2 |5 V2 K! b) E0 d0 q( pdx1=dy1=2;
8 g/ p0 e1 n5 g3 o" I! Ydx2=dy2=3;( B3 k) d: S' X) y5 l; a
while(!kbhit())! K% T/ i9 C# S0 l( T$ n" E
{! i( m8 m% _4 I" {
line(x1,y1,x2,y2); S" p0 \9 S+ {2 ? u2 V8 p7 T
x1+=dx1;y1+=dy1;
' O A7 n3 ^2 g8 M1 Z" ]9 \ x2+=dx2;y2+dy2;
/ V; t# Y# u7 S) {, Q0 ^ if(x1<=LEFT||x1>=RIGHT)
7 C2 T3 L* \/ U. r. ]! E dx1=-dx1;/ P0 n7 j# S1 M z% o/ c" Y _7 P; e
if(y1<=TOP||y1>=BOTTOM)' P/ X; N2 X1 k
dy1=-dy1;$ |% Z* \( R' L2 B, o
if(x2<=LEFT||x2>=RIGHT)) y5 S1 h! e( \0 [' t
dx2=-dx2;; f- G7 X2 N. C+ A, o: h
if(y2<=TOP||y2>=BOTTOM)
1 b, i3 _7 H8 R( j: D" b0 } dy2=-dy2;
+ R; H7 f% l4 ^9 N) o# I3 v if(++count>LINES)" o- ]- k# A+ R6 P" R0 D( o. Q
{
6 C5 U' Q$ H+ \& `! d* F8 C! Z setcolor(color);! ^3 w) K, v2 _
color=(color>=MAXCOLOR)?0:++color;
& V: o+ L$ t! b: X6 z( } }
8 _% l$ G. p7 L2 ]8 R3 s+ ?% K% d}& Q i' S- r$ Y V
closegraph();
" I6 {# Z: W2 H/ L* [5 F: E}
6 _( P) K1 z4 R# f
【程序61】
6 ?, \5 S6 ^3 F7 b: E8 |题目:打印出杨辉三角形(要求打印出10行如下图) # f$ \+ s9 I' E( ]6 B& L
1.程序分析:
- z: [ W7 H$ z 1
* o" ?8 z) \. k/ Z' u$ C 1 1
3 @8 D# h# ?' P, U7 u0 ]+ Q 1 2 1
9 M) M4 u# I& C+ f7 p 1 3 3 18 X* W* v1 s: x! V7 K5 u
1 4 6 4 1
3 V9 `7 A0 X8 ] 1 5 10 10 5 1 ! G: k8 S+ p; p! W# m
2.程序源代码:* O1 Z9 x h' Z" Q/ W
main(): \0 G% y) ^8 k7 t. @
{int i,j;
' l: H: ]% o7 {int a[10][10];
O, P1 X% g' a( Tprintf("\n");4 a; A0 [. H8 F$ b
for(i=0;i<10;i++)( u9 o1 r$ ?* u3 C1 p
{a[0]=1;
/ C$ k1 O' a: i# H: F; S4 ~! Y C a=1;}0 G B8 q( h. U# C# {) j0 L8 C s! ~- a
for(i=2;i<10;i++)7 F+ F. h) ]0 B, u
for(j=1;j<i;j++)
" b: y7 `6 h5 U D7 ^ a[j]=a[i-1][j-1]+a[i-1][j];
4 Y" E3 K7 S2 Y# b- ~for(i=0;i<10;i++)/ ]4 F# z* w1 I4 J( w9 B6 a: d
{for(j=0;j<=i;j++)
I0 B7 J5 k* q% k' `( ` printf("%5d",a[j]);
2 Q/ E/ n! l2 G2 W4 J printf("\n");1 `# [7 B. ^ c" d7 Y. R" Y- d
}
( I! ~! H3 k4 Z# t}
9 }, Q" V" I* L8 c
【程序62】$ @. L, `1 Q% x7 I
题目:学习putpixel画点。( L5 [- q+ U; v' C$ c6 V9 e4 C
1.程序分析: ( {" w1 x& t7 K) q( g* @' L
2.程序源代码:! B/ T* ?3 X% @, r$ C
#include "stdio.h"
# K/ c& Q# C9 V) r9 _) g#include "graphics.h"7 N3 ^4 h$ X' |0 w
main()
0 ^% w5 J h) A) D& o9 u0 F3 k7 C{
. D t; f! i, r. R3 m f. J: `+ [int i,j,driver=VGA,mode=VGAHI;$ J/ T! n9 }9 F) y' y) y
initgraph(&driver,&mode,"");) r% q; m& G& ? \
setbkcolor(YELLOW);. `$ }4 |. p+ l# S' r7 J y
for(i=50;i<=230;i+=20)
4 e8 Y9 q2 _2 A, F- l4 j for(j=50;j<=230;j++)
b8 `, \$ I% H2 S putpixel(i,j,1);
5 I. x5 t4 z7 Q3 Cfor(j=50;j<=230;j+=20)8 {* F7 v% s p* l/ \! Q
for(i=50;i<=230;i++)
( J7 a. s( S/ z1 {! ^8 P4 k% ? putpixel(i,j,1);* ]5 d6 ]9 @7 m4 H" E
}
0 @' s. S% z! U1 N3 [【程序63】
+ A- w4 \/ S* v& m P; U题目:画椭圆ellipse
0 `- Q9 O" ]9 T! ?6 X: x1.程序分析:' y* a5 I: @; V1 q
2.程序源代码:+ w' j6 ?; M4 N" S; C3 d
#include "stdio.h"2 ^6 _% U0 Y8 r6 J
#include "graphics.h"0 y& Q& h5 J+ C% Y$ j9 a
#include "conio.h"; _# V, T; E+ J5 s1 ~( B( ~
main()# Z5 l0 r2 a5 Z$ r& X- y: W
{- N7 Z% E; q: q
int x=360,y=160,driver=VGA,mode=VGAHI;/ ?2 P, v2 P- _) k0 i; P0 V9 @
int num=20,i;
& |, K( a! d: B. V7 lint top,bottom;
8 l, w) V7 E% i, minitgraph(&driver,&mode,"");% a% D v* \& G g% g% C( |# U. h
top=y-30;
$ f2 C, ?0 a. E. obottom=y-30;& P7 W5 X, G) ]7 ?
for(i=0;i<num;i++)
* W; v5 n5 |. V& K& t, p2 P: S( j{
& \7 U( Q$ Y+ Rellipse(250,250,0,360,top,bottom);% p' ?! H3 v& X0 y
top-=5;# o' R' v. N2 f% c' b
bottom+=5;
# B2 V/ `+ e4 Q( X}
7 `$ j7 A& f# \& e. N2 |, m/ X" agetch();$ X7 K4 D2 x% T, A
}
【程序64】3 Q, H5 o6 g5 }( x" x$ W5 D1 X
题目:利用ellipse and rectangle 画图。7 o, e5 S, a* h! Z0 K% P" q2 @' G
1.程序分析:
# E4 A: Q* Y$ ?2.程序源代码:
( g2 Z( `/ u( [( z, Q#include "stdio.h"! T8 B3 F- F0 J0 d
#include "graphics.h"
1 B4 S+ n' J: `: \# f4 B#include "conio.h"
- s6 G( x9 ^! b" u/ gmain()
1 B* Y K$ v. u0 G{
$ Y* ~( I. U# Q1 g rint driver=VGA,mode=VGAHI;
; M* s+ U1 i- ]9 k Oint i,num=15,top=50;
! e( T7 i( h0 Sint left=20,right=50;" m4 s1 N* _6 t7 E e6 }
initgraph(&driver,&mode,"");
) Q* Q8 w2 J+ Ofor(i=0;i<num;i++)
) x4 A( S! U, n# E$ ~1 k{
" g( l9 z$ f" i& |) p3 sellipse(250,250,0,360,right,left);7 v7 X! w+ W) D+ L
ellipse(250,250,0,360,20,top);
8 j3 p' i6 R( B3 B1 d5 n5 ?/ P; \rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));1 P+ ?# R- }/ j3 O
right+=5;
+ z9 j B; ?- d: B% [! ?& Mleft+=5;
! N z) P( q8 utop+=10;1 X. O3 d7 ]) v) }) [
}
/ E4 _5 R* H& Sgetch();
/ |% S* O# e4 o3 q5 S4 p, a}
【程序65】
: h; G4 z# ?, r, j题目:一个最优美的图案。
9 X. I- p( I% D& U1.程序分析:0 H! F! F- C# }8 e& Y
2.程序源代码:; \' G: p) V1 B; L
#include "graphics.h"
5 O* B2 [, a0 r" M8 P4 c' A1 ]#include "math.h"
7 m3 a4 z5 O2 o+ Y+ d+ C/ K1 Q! L#include "dos.h"7 X/ V5 w# j7 J$ x7 K9 I: s
#include "conio.h"
: J: |4 P8 M$ d1 i- |+ l6 }% f#include "stdlib.h"
' B! P5 x5 J2 S9 c. q6 k#include "stdio.h"$ q2 d# G, z K8 `
#include "stdarg.h" ]- N! i2 d, Z0 r" w* P$ `: A
#define MAXPTS 158 e8 O' h+ C# o' V; K l
#define PI 3.14159262 i m' P, } D
struct PTS {
# a' Q2 q" s* A" b1 q. T/ d& { j- Lint x,y;# u" [4 ~4 s" C
}; s8 H% L+ l! f2 K6 [9 q2 g
double AspectRatio=0.85;
* C& y' O I3 tvoid LineToDemo(void) H5 m7 f, p- \8 U$ X/ d8 Z( c$ C
{
2 w+ X8 f% t( O, D5 `4 z, Fstruct viewporttype vp;5 j9 D$ ]+ \5 i3 X. Y9 \! m
struct PTS points[MAXPTS];
0 T( `, R# g8 i9 w9 | Xint i, j, h, w, xcenter, ycenter;/ i# @5 S, ?0 t' u
int radius, angle, step;! Z- F9 {2 N1 s! `# B9 T0 F% Q, W
double rads; e a1 o2 k8 e- H2 o2 O" u
printf(" MoveTo / LineTo Demonstration" );
. u, t1 ~" l+ [) J' ?0 Ogetviewsettings( &vp );9 w V0 D4 p( U U* P$ A( c- H2 U4 H7 J
h = vp.bottom - vp.top;) x3 f' k" U* T5 w% o0 v# u
w = vp.right - vp.left;+ k2 {4 t& ~& S2 r' V5 e
xcenter = w / 2; /* Determine the center of circle */
- g0 m( h5 q: yycenter = h / 2;
2 I) `1 N) r8 b" W' jradius = (h - 30) / (AspectRatio * 2);
* g( \) {+ ^ G' H( \3 `1 @step = 360 / MAXPTS; /* Determine # of increments */7 M& U3 E' d. U$ ^# n \
angle = 0; /* Begin at zero degrees */" a" }% X* p& e ]6 |+ s0 P
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
+ G1 D A$ ^7 {rads = (double)angle * PI / 180.0; /* Convert angle to radians */
* {/ @, m$ u0 c6 E$ A: l- g9 C* dpoints.x = xcenter + (int)( cos(rads) * radius ); m3 ^8 Y* _; D |. ]+ e( V
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );" j% _2 L. }' D' W
angle += step; /* Move to next increment */9 I0 X; P4 G- N# W
}
5 C/ Z$ ^5 m2 S. x# {7 icircle( xcenter, ycenter, radius ); /* Draw bounding circle */
( ^! J! ~" z3 `* vfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
2 U2 J: c9 J3 T |* \: m7 qfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */5 `/ d+ X8 O# E* k5 F6 I1 |
moveto(points.x, points.y); /* Move to beginning of cord */
9 S3 q8 B4 p8 V2 [1 F- clineto(points[j].x, points[j].y); /* Draw the cord */7 W( ]/ u9 A& N$ S# h
} } }1 k8 r7 q2 X" l2 ?2 Z3 N) c
main()
( w4 a7 K j+ ^) p{int driver,mode;
1 E, T) }2 }- \ Q. {- H6 ^driver=CGA;mode=CGAC0;
! ~1 d) q* U/ u6 S, Q" |7 ?8 Vinitgraph(&driver,&mode,"");# E: B- J9 I U. e( @
setcolor(3);
+ f: m7 d2 b8 ^ Isetbkcolor(GREEN);# a( |8 m7 a& d2 C5 ?6 U1 H8 K
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
9 ]& H" b l( j! o
【程序66】$ `3 b+ R, C$ V! Y1 `( `$ n/ N
题目:输入3个数a,b,c,按大小顺序输出。 ) S9 D3 c- U5 n
1.程序分析:利用指针方法。, q" [1 o6 i! k" J
2.程序源代码:/ S# t& W6 X/ s5 M8 A. a
/*pointer*/4 F- ]- F* o$ ~ L; O0 f
main()
2 h. i7 U( O/ i{* G0 x H$ h) _8 m5 ~0 u
int n1,n2,n3;
8 V3 L# B: K7 x: [0 gint *pointer1,*pointer2,*pointer3;7 W) ~0 N* J2 N5 @
printf("please input 3 number:n1,n2,n3:");/ d) |+ K& C( @
scanf("%d,%d,%d",&n1,&n2,&n3);
/ L4 z; U% T9 W v; ]1 j4 ppointer1=&n1;
~. j7 |5 C9 ]& Upointer2=&n2;6 Z3 a3 M7 \5 B K; l
pointer3=&n3; k3 d9 a1 \$ `5 I1 }6 b" x6 `
if(n1>n2) swap(pointer1,pointer2);
& I, |9 \2 I: Lif(n1>n3) swap(pointer1,pointer3);
% ]; V8 k( D. i# t" rif(n2>n3) swap(pointer2,pointer3);7 M* m( [3 ]% }* i
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
* R& g1 }# E2 n; q' B. `& g}5 w- `8 u( c9 }4 i$ n1 o* l
swap(p1,p2)
& v* I0 a5 @0 b: [; N, g& Mint *p1,*p2;
5 b3 j2 e2 T( E8 w: d$ R* h' a' A{int p;
# E% l6 p- K2 [ cp=*p1;*p1=*p2;*p2=p;% e: R- u! v/ x: O F Z/ E( T3 v
}3 S7 N" s7 s& A1 d+ `$ r" h