【程序59】% D: o% l% S3 j t9 X
题目:画图,综合例子。, t6 y0 O. O+ g
1.程序分析:
: Z% H" v( P( \8 J2.程序源代码:2 U3 S% h* a" \* J _; }
# define PAI 3.1415926
5 J$ Z3 g& d8 }# define B 0.809
' f( A- j8 ~0 E1 Q/ p* q# include "graphics.h"* o8 x# [+ c9 c) |0 t
#include "math.h"3 y+ D( k' h+ p9 Z
main()
; U, Z4 R" p, ?3 H{, i$ a( s5 y2 D6 o" c
int i,j,k,x0,y0,x,y,driver,mode; r- Z0 T1 ^% u# q: L
float a;% g8 ?# f9 I. x4 x
driver=CGA;mode=CGAC0;
6 j9 @$ a' ]4 P5 Y7 z$ H& tinitgraph(&driver,&mode,"");/ k- {: F! \; _) G8 T
setcolor(3);
) a3 X: h, |8 r# z4 h- Asetbkcolor(GREEN);
A5 W* `* A, Z3 I& yx0=150;y0=100;" v' [) j V( C0 b( R/ F: c
circle(x0,y0,10);: ?, N C3 K4 I! T5 t% |) V; `( X
circle(x0,y0,20);3 t) O8 \, z$ u' k+ w
circle(x0,y0,50);
& o0 f k4 H9 b1 g1 Zfor(i=0;i<16;i++)
1 [' u4 c# C! ^. I7 v{# ]: U: |2 B2 F$ u. L7 p3 @
a=(2*PAI/16)*i;
% }% d/ I2 I" e# [ x=ceil(x0+48*cos(a));- N9 z% P- w4 q& q& ?
y=ceil(y0+48*sin(a)*B);3 B$ T9 H/ P& i( ~$ f7 v* u7 Q
setcolor(2); line(x0,y0,x,y);}& G' p' r# Q% e: j4 U/ R( ]8 k
setcolor(3);circle(x0,y0,60);& t3 \/ K5 s% m0 n2 c
/* Make 0 time normal size letters */
# H1 h, E i0 P1 J+ b- \3 B/ Msettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
# \4 W/ h( |$ l7 J- e# }" D J: Houttextxy(10,170,"press a key");
3 p- z }4 }2 W& Y" R- jgetch();4 r' N# n) B# L6 R1 F# k2 ~* a
setfillstyle(HATCH_FILL,YELLOW);
' I2 a* Z, `! ^; W r3 {; Ffloodfill(202,100,WHITE);
4 v8 W& W$ H# q d; ]getch();' G$ K! p& T; X6 I4 o3 V% o
for(k=0;k<=500;k++)* [" I1 `0 e" N, a5 y3 g( P9 k
{
( U9 T$ B$ N i- J: c0 A setcolor(3);
% S+ ?. |% @) t6 W/ M" } for(i=0;i<=16;i++)
, _* O- C! k e+ `. y4 ? {; K# f. E4 S; M- r5 S
a=(2*PAI/16)*i+(2*PAI/180)*k;$ i0 C2 z9 E8 B) u, o
x=ceil(x0+48*cos(a));
4 B' J& l9 Z! N4 {; x6 b- } y=ceil(y0+48+sin(a)*B);
9 S' k& N2 S" i. }! o, t setcolor(2); line(x0,y0,x,y);
: g& O G2 c; r; ]% h% n( @& K/ i: c }
& Z/ U2 _3 d4 ?. i for(j=1;j<=50;j++)3 H0 e1 C7 ~0 ~: _
{; C2 X/ T/ N5 e
a=(2*PAI/16)*i+(2*PAI/180)*k-1;9 F7 n9 n) E- J) _7 ^# }
x=ceil(x0+48*cos(a));
/ {2 N7 a: k6 y y=ceil(y0+48*sin(a)*B);
q3 w: t" u! `- u1 p line(x0,y0,x,y); {* G, c2 L0 k: j8 b' f. v3 p
}8 T* K/ f. e) p; D, w! @
}" [3 q0 W1 h5 V; \
restorecrtmode();
/ g4 s6 M: O4 R2 Y: M}
【程序60】+ T5 Y- e K! [
题目:画图,综合例子。 0 e3 b2 O+ {$ m+ ^% y; X1 o
1.程序分析:
, e) `0 b4 i% Z1 j2 {8 k2.程序源代码:
$ \8 m) s! p! N# Q O# l#include "graphics.h". ?/ ~" B) H! ?$ V2 E+ `( t0 z
#define LEFT 0
- {7 s$ F8 G8 H; G* s#define TOP 09 I [1 ]5 j6 P5 {
#define RIGHT 639. _5 m2 v0 D f" d
#define BOTTOM 479, D3 d! i* ~4 r
#define LINES 400* i& L2 u2 j0 U# S9 x
#define MAXCOLOR 15
2 _* k4 [: X4 o3 b1 _$ u- Vmain()9 Y: D1 |. P+ C, V& r$ S8 u
{
! V. l2 b- i2 p$ }" y! K2 { mint driver,mode,error;
( E" r; v: ~+ D: Uint x1,y1;
: ~" w: l% i! o! l) Wint x2,y2;4 C1 x3 n$ l8 |4 T- q, R
int dx1,dy1,dx2,dy2,i=1;
) Q; `: A/ G8 k6 ^% ~% X4 D. mint count=0;
, k+ O2 X! u2 C; F8 |" L3 ]int color=0;
/ S1 g' M. O4 ^# q4 k# Vdriver=VGA;
" y" e: d6 X+ t% i/ a Rmode=VGAHI;, w' i% v; x, o$ e9 {, ]6 g
initgraph(&driver,&mode,"");
# H4 J# I% p, s: l9 j3 bx1=x2=y1=y2=10;8 G0 Z1 H4 U+ @7 C! ^' U
dx1=dy1=2;
; L% d; z6 i, E% Vdx2=dy2=3;0 K( d1 D) x* d/ W
while(!kbhit())* T+ ^2 I/ P& l2 i
{
) x! v2 V( a2 ? line(x1,y1,x2,y2);0 }& Y( \* U, W5 M
x1+=dx1;y1+=dy1;2 b$ c' J5 l! z# u; i) e
x2+=dx2;y2+dy2;
8 C7 {4 T* p' P; O% x/ b+ P. `" W2 E if(x1<=LEFT||x1>=RIGHT)% _2 c- s9 N$ a9 a& Y
dx1=-dx1;( W; Y: u: W. I/ |- Q
if(y1<=TOP||y1>=BOTTOM)3 {& q j6 v! @/ j" I7 V% g
dy1=-dy1;* Y, U' _. J# [0 q$ y; N4 P* h
if(x2<=LEFT||x2>=RIGHT)6 w! y) w- _$ q, ?. a8 G2 W
dx2=-dx2;9 a" D( J3 J9 m! \+ F
if(y2<=TOP||y2>=BOTTOM)
! m& k3 m& U5 R: x2 e9 w dy2=-dy2;2 j3 M; s8 _$ V/ t9 N0 r% _. k/ f
if(++count>LINES)6 P1 v% @; [& d
{4 L4 t; {9 ]: }
setcolor(color);
4 `: ^# v5 I: Z5 y color=(color>=MAXCOLOR)?0:++color;& C8 C( M4 p' n( `$ i# ~0 _2 x
}7 n4 F$ c- E& T8 \
}) H6 P* _$ G8 |3 Q. Z2 m/ c. O
closegraph();
' G; F% m+ |8 @ P; v}
4 O2 _, U W+ v( v0 t, e& O1 ~
【程序61】: X. {1 K" z `
题目:打印出杨辉三角形(要求打印出10行如下图) - |& h3 }. p. c8 P+ C8 g' H
1.程序分析:4 D( X- _( W$ {, J% t
1
+ E/ R: z* X$ x$ ? 1 10 G# r0 Y; q9 Z' N
1 2 1( y d" H0 Q: D, E
1 3 3 1
- {( n- K9 S& q+ {' x# Z 1 4 6 4 12 d4 h6 a. I* j
1 5 10 10 5 1 0 j$ `* Z# K1 E u! u! }' X
2.程序源代码:: F9 c k- I6 g u: ~$ h
main()
8 g6 J8 h( z+ f2 C& d9 Y{int i,j;9 i- Q+ @ I3 {7 K. Y( q
int a[10][10];' T3 B1 l, K/ q" K( q# Z$ L5 S
printf("\n");) A( r" ]% b9 k0 f9 f7 \$ Q
for(i=0;i<10;i++)! X( k9 `5 p* e; o$ f# j1 x) {4 D
{a[0]=1;
3 L1 `% b7 j, F$ F1 L/ T: B/ P a=1;}
$ Q, T& x5 H# A0 o7 O& ?" ofor(i=2;i<10;i++)
! d6 J9 W: z6 c$ g5 G* _ for(j=1;j<i;j++)- m" L3 c. \5 N5 N+ @$ N0 \5 |
a[j]=a[i-1][j-1]+a[i-1][j];
. s; Z% ~2 J& E9 u3 lfor(i=0;i<10;i++)* F8 Q3 Y0 \2 h1 L9 H) \
{for(j=0;j<=i;j++)
0 P K8 N: G+ V: f3 O" L printf("%5d",a[j]);* B; |& n, ]: z/ m7 z& o
printf("\n");1 n. O! ~! n" V) y' ?4 O% I' {
}
$ T' }+ z) G- |! T}
" x3 [# s/ d% b$ \+ y【程序62】
) T6 K$ f1 ]- s6 N+ v/ c. K) N; R题目:学习putpixel画点。. a3 h0 O$ a0 S/ x
1.程序分析:
; w5 Y. b; o& D* T2 I2.程序源代码:( E4 a4 s q) _. c7 T4 U- u+ ^# G
#include "stdio.h"
* ~8 z6 u2 p3 i4 ^( s: `' x: w% m#include "graphics.h"; G+ w9 f# `. S' k8 G1 o
main()
9 r) {# g2 E8 G" A{2 {: k# O: y4 x: U) ?+ x
int i,j,driver=VGA,mode=VGAHI;; x( R3 z, P4 F. H9 o, k' A1 X& {* r
initgraph(&driver,&mode,"");$ p& m$ g/ ?! N" {
setbkcolor(YELLOW);
2 V# l( M5 N3 N8 N, Kfor(i=50;i<=230;i+=20)
0 K3 j R9 Q; i3 x% J+ Z; u for(j=50;j<=230;j++)) i! ]7 k# ^4 E' e I
putpixel(i,j,1);
8 S' P2 E. u6 `5 bfor(j=50;j<=230;j+=20)1 m# d# l- e, y1 I7 L
for(i=50;i<=230;i++)
" |; U/ P! m8 ^4 ~ putpixel(i,j,1);# P- \3 V' X/ L" a; Z
}
" b2 q& O1 P% J, B
【程序63】
4 I+ I8 n0 F9 X题目:画椭圆ellipse
, u b7 n& O% \5 d+ W1 ?1.程序分析:
1 Y' n* o& V3 X- S. W2.程序源代码:# d5 ?. a7 @5 Y3 h$ s( [
#include "stdio.h"6 X# ]" z, [- I6 p& p* H
#include "graphics.h"# M# M+ H" R1 Q& k8 E8 M% O
#include "conio.h"
& } v' ?7 y3 C' Z9 U/ W. N$ k! Imain()0 R& U4 W s: Z. l. I& l$ A# r
{
0 i" Z1 L5 h6 r2 X1 o6 xint x=360,y=160,driver=VGA,mode=VGAHI;
, K; I' p! V' Iint num=20,i;" _. q" _8 d9 `3 j3 \# I( M1 F$ p! f
int top,bottom;& l8 [+ x$ j5 e* ?
initgraph(&driver,&mode,"");
/ L* G! u, Z7 K ctop=y-30;) C" n- ]# @5 L' ?3 u! @
bottom=y-30;
- A# J- \9 q) |" ~for(i=0;i<num;i++)
3 j1 l5 [6 B S- u* {" i2 ]/ j{, F7 k s* z4 o7 A: K' l9 R5 t& E. m
ellipse(250,250,0,360,top,bottom);! e4 T8 z. U8 b6 f
top-=5; A' O+ g2 x& X2 U; U0 M! E; i N
bottom+=5;
" o9 G/ ~) V1 ?. E3 t}4 e! w( m5 k1 H
getch();+ Z* D. k% C/ U, z4 r5 ?' Z! X- s
}
【程序64】4 @) |" f6 q! A* q9 m3 y3 f/ h
题目:利用ellipse and rectangle 画图。
: [6 Z. L5 f9 @ ]% n1.程序分析:* Y0 O ?8 j2 h }& J
2.程序源代码:' F3 J( ?1 k, g8 g6 t9 p: y
#include "stdio.h"
! V8 g1 H% E/ c7 n- N6 c#include "graphics.h"
1 T0 I6 f- r7 q9 O#include "conio.h"
4 j& W. w, b P5 B' o& gmain()0 |7 e5 G2 C( |) G8 M* u$ j
{
7 q8 ~0 x. w* ~/ o1 e' ?& R% ^/ gint driver=VGA,mode=VGAHI;- c, r& f8 U6 T7 D, ~6 E) [
int i,num=15,top=50;% E4 f- v% @- O7 N5 A. n n0 e
int left=20,right=50;3 t+ T6 b* ^ ~, [, Z: H# W
initgraph(&driver,&mode,"");- z# \" {7 V7 a6 u
for(i=0;i<num;i++)
5 w& G3 o9 K8 M- |* p2 M{' B, H3 g$ m/ _, q( y" Y
ellipse(250,250,0,360,right,left);
" L' l/ I& A: Y& [5 |ellipse(250,250,0,360,20,top);
$ v) H/ w7 k3 E; krectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
; w* Y& n- K' L7 m; dright+=5;
; _$ Q! ?4 A) z+ j' _left+=5;
) ]3 c; n. N# B# {" P3 k, Ltop+=10;
5 O. [5 d0 {' F8 P- ]}9 M1 e+ ^5 T+ z) b/ L) v |
getch();
! Q$ r. d: q' q2 \5 j' x}
【程序65】, _& z) y! s' [& F) ?' ^
题目:一个最优美的图案。
+ Z2 S3 E/ }, Q0 t. ]& Q1.程序分析:
6 q# O4 i8 A1 p, E4 c* r& r2.程序源代码:
f V0 m. L# k& N" ] L5 T+ Y1 m#include "graphics.h"
l( k1 c5 n8 U#include "math.h"
7 [: V1 G5 n$ Z( D _- X4 v7 m#include "dos.h"1 ?! _, J* }8 _% \
#include "conio.h"
6 z F* `, ?( V8 z#include "stdlib.h"
) {7 ? p2 [+ u# P#include "stdio.h"
" h! z* k% h: X$ k: V9 ?; H3 S$ ~! m#include "stdarg.h" {& N1 ?5 l/ O; h, G; V
#define MAXPTS 151 Z5 h3 e: ~5 V/ D$ ^3 x5 W5 }
#define PI 3.1415926. A1 X2 G) W9 ^4 R
struct PTS {7 Y% X5 ~* |1 B6 I" j! \+ Z: k9 g
int x,y;* }2 V7 k& z& ^# N/ J
};2 ^. a1 T2 i9 C! a- G9 v, y
double AspectRatio=0.85;5 e3 D$ [2 v+ \
void LineToDemo(void)
7 v2 k! A9 {7 p{: `8 l8 V7 F8 Y0 q
struct viewporttype vp;- [$ y+ R2 W( P- Z
struct PTS points[MAXPTS];; X h8 X: |9 X8 o: X
int i, j, h, w, xcenter, ycenter;
; ^# q% J8 m5 m) m' x3 Hint radius, angle, step;4 `! U b- p/ V3 u9 ]5 H+ Y
double rads;
& K! H" G M. V$ H: o$ ^7 e/ m3 uprintf(" MoveTo / LineTo Demonstration" );
: ? [0 P5 [) a& t1 s7 o0 Agetviewsettings( &vp );
9 A' c" |9 H1 ?: D: t* o+ Wh = vp.bottom - vp.top;
$ E6 w) N: s6 |0 d# aw = vp.right - vp.left;* o: u/ L1 v/ G7 w$ V
xcenter = w / 2; /* Determine the center of circle */
; n' c9 p. b/ X. T/ c0 dycenter = h / 2;
( o% U- P7 |! \; S. @radius = (h - 30) / (AspectRatio * 2);3 c: R. |, A& ]' J! f+ H$ N1 m3 V
step = 360 / MAXPTS; /* Determine # of increments */
. ~" ~- `7 h3 h8 ?* f% Q0 Eangle = 0; /* Begin at zero degrees */ c* q" x: Z' T. p
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */# ?( F) }0 a& L, D" @
rads = (double)angle * PI / 180.0; /* Convert angle to radians */! a, M4 t- m( z. P$ m' M
points.x = xcenter + (int)( cos(rads) * radius );- M* P. D7 V% b2 v; O7 v8 x9 W, e
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
, X# f. ^ o- l# ^angle += step; /* Move to next increment */+ l1 l/ ]4 h3 } O" l
}# k) G! _/ j' Q, E# s
circle( xcenter, ycenter, radius ); /* Draw bounding circle */5 A9 _5 C7 g# C
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
3 u- `! L: m% o2 tfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */2 ]) L+ I8 b6 E/ A( @
moveto(points.x, points.y); /* Move to beginning of cord */
+ T6 z1 A0 I+ j, M- _lineto(points[j].x, points[j].y); /* Draw the cord */
3 Q- x- ]/ M! [: I G} } }
, R( O; }0 _" ~) tmain()) M8 l( p0 \1 J7 l& i' Z
{int driver,mode;
: d1 o. f+ B8 L, b8 M/ }driver=CGA;mode=CGAC0;, c6 d! f, k% s( L$ U( P6 q& V
initgraph(&driver,&mode,"");/ K$ Q! }, z$ V. G
setcolor(3);: L6 Q! t1 g1 e" W% ^: T! \
setbkcolor(GREEN);* {1 @- |% C# V+ C; K" R _7 T
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
& b. e/ x! E& u1 S9 Q' D$ m
【程序66】
0 N* I) g: i( c# G/ h: @4 r3 H题目:输入3个数a,b,c,按大小顺序输出。 9 T; N, F% W( g8 y0 ?
1.程序分析:利用指针方法。+ c8 E- q1 Z3 }" O6 F! |4 l$ r
2.程序源代码:$ x# H+ V5 T% T+ T! S* J
/*pointer*/
$ [2 z4 A3 O3 K& ], v% H/ Tmain()& A" S# t- J1 J- g* P
{
1 H1 F7 A, T, l6 e) vint n1,n2,n3;
5 M2 G, s% O+ z/ }$ u( {int *pointer1,*pointer2,*pointer3;3 N U: w3 h1 A$ T- S' I2 `
printf("please input 3 number:n1,n2,n3:");/ |. D0 h" e9 W$ ~ F' C
scanf("%d,%d,%d",&n1,&n2,&n3);* z, H5 A- d+ O* G* P1 e" \8 n6 O
pointer1=&n1;
# Y% w7 ]. J' D0 D: Y0 dpointer2=&n2;
5 J! O# W$ `' Q5 a: _; tpointer3=&n3; i" i) f; Q& ^$ o
if(n1>n2) swap(pointer1,pointer2);3 r# Q/ d+ j! T: f7 ?1 E" J3 Z
if(n1>n3) swap(pointer1,pointer3);& m% A0 s2 ~* o& y
if(n2>n3) swap(pointer2,pointer3);
1 V$ O) F2 ~3 ?* c o* p3 Sprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);1 D# D6 \; j" b( m2 k& L! i" H
}1 K. q. s H+ d/ u" S1 y5 X: n
swap(p1,p2)
5 Q3 ?, _7 h: `4 r6 C2 q. l$ H! Xint *p1,*p2;& N0 p' G* x. ^# w L
{int p;
4 `8 M$ |! C$ {5 r: `4 [' Rp=*p1;*p1=*p2;*p2=p;
4 v' i/ v# |* [& X}
( q4 a* r) @2 D! p