【程序59】+ y: E( N+ }7 I3 |% W
题目:画图,综合例子。
- D7 o* Z9 b9 l/ l1.程序分析:' l" h6 \& L, v0 z/ P- E$ H
2.程序源代码: i& f% y) `" \. k
# define PAI 3.1415926. T# J. }- V1 |( a
# define B 0.809+ {' q( P+ J: A6 L/ o
# include "graphics.h", q, L1 X+ A/ Q' x$ V) O- S# Y
#include "math.h"
; z) A% L$ _4 H( a# Z( t" e. Imain()
+ c. l/ v0 g; d W9 D' T v" P{, ]/ X* ~/ u! R" |5 w. _
int i,j,k,x0,y0,x,y,driver,mode;
! e; I+ _/ a8 u; d5 Xfloat a; U0 c7 Y* J$ N
driver=CGA;mode=CGAC0;8 P* e0 {" S8 J% M5 g
initgraph(&driver,&mode,"");
- D/ C% N" d' T* x/ fsetcolor(3);# m5 E: a* M" p/ P$ E
setbkcolor(GREEN);, {: d' t! J5 ^/ i
x0=150;y0=100;. c' P" d: R, j3 @: U
circle(x0,y0,10);
( Q: I( l3 a, Gcircle(x0,y0,20);
& e3 K8 N4 U. [6 k* P0 s) `/ B& zcircle(x0,y0,50);
4 Z* M- w" T1 Dfor(i=0;i<16;i++)# l! `4 Y4 P/ a2 N. E
{
1 I+ d4 G! F% M2 z a=(2*PAI/16)*i;' H7 k6 b; _, }9 d6 x& p9 c
x=ceil(x0+48*cos(a));7 \ e: c2 s7 s
y=ceil(y0+48*sin(a)*B);
! s' v: P2 Q! R1 H9 y# M% \ setcolor(2); line(x0,y0,x,y);}
! X- a; ]' m9 T0 w8 e% @" isetcolor(3);circle(x0,y0,60);
3 W# P2 e/ U. ]/* Make 0 time normal size letters */
( |* d% N7 _1 \+ r- fsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
5 |3 U# e d- `- z4 m7 pouttextxy(10,170,"press a key");6 P8 L; d3 Y9 d( t7 Y8 l, x
getch();+ X; c2 D, Y/ L) o' {+ e
setfillstyle(HATCH_FILL,YELLOW);
" j7 `+ J- e1 j5 u2 b, v% ^floodfill(202,100,WHITE);: h. _+ i, R, K" `( _3 S$ D5 j
getch();
- I; m3 k$ p( M2 ?" T0 Zfor(k=0;k<=500;k++)
( f& a D3 D: m( G* _1 t{) f2 K& Q3 }! W6 k% Y/ y# ]: O3 c g2 h
setcolor(3);2 O1 G% x/ h1 R8 ?8 w4 Z
for(i=0;i<=16;i++)7 w, h# Y" s1 y3 d! C* A# T
{( i' [4 ]' q. z' p2 }+ h- u
a=(2*PAI/16)*i+(2*PAI/180)*k;. i8 V6 p- t# p) F
x=ceil(x0+48*cos(a));. U; M# e8 T" c2 q$ x
y=ceil(y0+48+sin(a)*B);
( `, Y+ ~, C/ b2 o+ W4 j3 _0 | setcolor(2); line(x0,y0,x,y);% C) y9 Q) d' x- [ n; N7 R' B) F% D+ Y; S
}
! c# @' d @: p8 w* D3 j( A for(j=1;j<=50;j++)$ U( X8 V( I& ^5 z
{# k6 u. R3 } `1 O/ c, d: a8 s
a=(2*PAI/16)*i+(2*PAI/180)*k-1;
; N- i4 d Q$ x7 A) [ x=ceil(x0+48*cos(a));; b5 ?7 e9 `0 t
y=ceil(y0+48*sin(a)*B);5 ?3 I/ r- W+ o& V' z
line(x0,y0,x,y);
" O4 g t" Q1 h! q* r }1 M5 C' F9 w+ a! ^: }& c
}& y5 d* C# X+ y) \
restorecrtmode();. k# a) h8 }6 e/ b7 Y
}
【程序60】& h( _0 e8 `/ f; L/ c: k
题目:画图,综合例子。
, h/ p0 ^5 x! S9 X* v2 |2 U) g' x1.程序分析:
; {3 V+ }% r Q9 V. |$ S% @. q: \2.程序源代码:" }' h9 n7 L! r( ]; E
#include "graphics.h"
3 I _% M. L8 K#define LEFT 0: @. n' N1 `" R- U" s' X" M
#define TOP 0+ u& a( w! |6 P8 b" M
#define RIGHT 639
1 M. Z, e! {: j$ G#define BOTTOM 479
- K/ I6 m* I. I! J( i' A% p#define LINES 400
- I$ M8 g, u0 h3 J: @4 t#define MAXCOLOR 15
' Q9 f* R+ i6 \! ^* Dmain(); z3 n4 U& S9 M5 V$ q6 g/ ]* u
{( K- A1 L. `- M1 s# z2 U, y$ R
int driver,mode,error;6 m2 T5 N0 S: u1 D0 J, Z2 E
int x1,y1;
- |' [0 ?, l, t6 W( G& |int x2,y2;' D" a5 H' R! D! Y" W
int dx1,dy1,dx2,dy2,i=1;6 D0 v( q! f5 [$ q9 h
int count=0;, Y- z, I0 z1 y3 l
int color=0;3 R7 s; K# q' D& F Z2 W! K
driver=VGA; c4 K, _# y) H9 Z* M, ?
mode=VGAHI;
9 `/ s! X! N. f3 H: G# A% einitgraph(&driver,&mode,"");
0 X% }+ A; c9 J0 @# ? N$ q! Kx1=x2=y1=y2=10;
^. e0 F! ^* ^0 K3 q7 @- Jdx1=dy1=2;% h! O1 R& [, a/ U" K
dx2=dy2=3;
; }& z; J( p1 X' ^7 Twhile(!kbhit())
9 n$ r* X0 O( ?: N{
1 g _6 I* @( ?; J2 l- f" ] line(x1,y1,x2,y2);6 _ a% W8 i; f0 m0 h+ E( C; M, S
x1+=dx1;y1+=dy1;
3 s' B6 A3 G7 I8 p a x2+=dx2;y2+dy2;6 S! E3 Z: J* ~: N4 I+ N
if(x1<=LEFT||x1>=RIGHT)
/ K# R" I( A5 _# f: G+ t dx1=-dx1;
4 r W, [2 A2 o6 o if(y1<=TOP||y1>=BOTTOM)
; l* \4 K' ]$ [$ V: M dy1=-dy1;
) n+ p" W5 d* S if(x2<=LEFT||x2>=RIGHT)
% f/ l8 ^" X! P$ T1 D$ p) F dx2=-dx2;
& n9 ?7 K5 g0 ^ if(y2<=TOP||y2>=BOTTOM)* B8 H9 n# L) c; n3 y& ?
dy2=-dy2;8 f- I+ L0 ~( @) h K# m& ]- m9 I; u
if(++count>LINES)
+ q8 E0 T- b& F0 p6 ]+ I# } {
* @, E# w2 [% O& d- N setcolor(color);
Y5 P; l( |% j/ S0 {& \ color=(color>=MAXCOLOR)?0:++color;1 h2 y4 D: {: y+ U- G7 q$ _; B
}
7 \2 Y1 D. V# W8 V F( s' c}4 i( k: N; n3 k0 J9 S5 G
closegraph();
' {9 v- y; X3 p$ q/ j}
3 ^) ?8 t2 ~) y( b) q【程序61】
- }' \. Z M; }: R- @8 ?2 b9 Y题目:打印出杨辉三角形(要求打印出10行如下图) : G9 {7 E7 d- L6 e1 J( @4 f$ b
1.程序分析:1 g" C/ P$ i& J& I! V
16 N& [# p; \3 h+ P- i
1 1: m7 C+ e+ d v; W. A- w( |/ o
1 2 1
. w! F9 ^8 Q k6 Q- K) R 1 3 3 10 S, y2 L# `; w
1 4 6 4 1
' i& _+ m0 _4 K, m; e 1 5 10 10 5 1 0 E& f4 g) A& `, `1 T. X# O5 g( o
2.程序源代码:6 Q! l& `. e8 [, v, R5 V
main()( X5 }+ L) K! L9 a& S4 S2 J
{int i,j;
5 m' L' C* `8 c2 ~$ i vint a[10][10];
3 _0 i; E3 J1 H7 }( e3 z9 Aprintf("\n");; m- ]; b; J" V5 i
for(i=0;i<10;i++)# I8 p: g0 b+ q
{a[0]=1;. `) }7 @. m0 m' ~
a=1;}
3 R$ G2 ~0 E2 f Ffor(i=2;i<10;i++)
9 c2 o. Z7 d4 L1 w for(j=1;j<i;j++)" \0 s/ Z) c7 l+ j1 g$ |# H8 U6 Z) K
a[j]=a[i-1][j-1]+a[i-1][j];
& O$ I. Z; H# J' T3 A- B' r4 kfor(i=0;i<10;i++)5 ?; } @7 ]: }5 U8 i
{for(j=0;j<=i;j++)3 J" E6 L& M3 m+ A1 u7 ^
printf("%5d",a[j]);7 W& u3 a. O, ~
printf("\n");8 U. T; |$ Q& f6 A/ V1 }! k
}
; Z) m4 y B9 U& S}
. C# s4 a7 I2 L: H3 I G
【程序62】
+ v+ n& L3 J' B- b题目:学习putpixel画点。$ J J% d9 g$ R, n
1.程序分析:
* v# a) u' r# X: H2.程序源代码:
) L# n2 _( v: ^& r#include "stdio.h"
" {) @1 t/ ? x$ E#include "graphics.h"
9 e$ O/ {7 g% O- Z$ q4 l. p; O" q0 R; pmain()
" L9 f' q, ~- \0 W! ^{- ]) d, o5 b. S9 f3 v! A
int i,j,driver=VGA,mode=VGAHI;
# {! u7 D! k5 Binitgraph(&driver,&mode,"");$ K9 O" C) R {3 C
setbkcolor(YELLOW);$ O9 _4 w# H) p/ J
for(i=50;i<=230;i+=20)
! s% a0 A* _, N& P# R4 Q$ ` for(j=50;j<=230;j++)
. Y) c; j6 E* P0 s% ` putpixel(i,j,1);
' e# s7 ?! X) ~for(j=50;j<=230;j+=20)
0 l: F J+ x! }- p! D for(i=50;i<=230;i++)
& s' {9 V" G# h) K, w putpixel(i,j,1);' b7 y. E- ] Z- Q5 l {
}
; h! G. q9 X0 J5 r( h9 ^! B
【程序63】
I- P# q7 o$ l/ B) c题目:画椭圆ellipse
y" z+ a4 u8 ?& f/ C; f1.程序分析:4 J$ @4 }" @3 w4 ?4 d
2.程序源代码:
% v1 q( G; q# a1 A#include "stdio.h"
& m. D K+ ]7 U/ n5 N# e7 H7 [#include "graphics.h"5 V& [/ P9 S! k
#include "conio.h"
) d3 @0 S% P" R* Emain()
+ e: G% y7 { d @+ C3 X$ L{
3 d$ o4 E; A) P, lint x=360,y=160,driver=VGA,mode=VGAHI;
( z9 B1 S8 T5 o$ f" rint num=20,i;
4 w6 l! A# ?! O/ I2 O& F. Hint top,bottom;
3 o% b9 h6 R& C+ Ginitgraph(&driver,&mode,"");
3 E4 n3 d7 Q4 g$ L7 A; J1 K: Ztop=y-30;8 M7 B6 }7 u) d/ X; ?7 l. h$ U
bottom=y-30; Q& T8 ]$ i H) ?7 v2 r
for(i=0;i<num;i++)
/ H$ M) u8 u+ G9 ]* ~{
$ T2 R0 t) ? @- V1 Gellipse(250,250,0,360,top,bottom);# ?0 z: ~: p2 i# M
top-=5;# Q, h$ \7 }+ E3 Y
bottom+=5;
/ h- }7 _9 P2 K}1 Z* |! @5 g0 P& g; @
getch();
. l# z, k! }2 q3 K0 B" w}
【程序64】
: }9 p; s, M) L' R& s5 t题目:利用ellipse and rectangle 画图。" R) _: P1 o5 X7 M
1.程序分析:- |, V5 S8 S+ Z4 c( g8 Q% H
2.程序源代码:
/ j, C% d1 ^ j#include "stdio.h"# C2 @5 x0 ~! s. P$ y) o
#include "graphics.h"# z. ^& E# g6 T+ H- l
#include "conio.h"& B! U! P$ j2 z Y) h9 ?+ ~
main()
* J$ M( M6 e0 p$ O+ W6 u5 N* a& m{& p4 r$ a8 T, o% M
int driver=VGA,mode=VGAHI;4 W; J6 A x- D1 n! A& n2 |: I
int i,num=15,top=50;! _ U) ~* B4 C) A
int left=20,right=50;! z4 E s& R3 J
initgraph(&driver,&mode,"");; X+ o' ?8 t) Z3 x* q5 J" g: u
for(i=0;i<num;i++)
m8 B$ g7 C: i+ d{
2 y( t: O4 D5 v, {; r* f; aellipse(250,250,0,360,right,left);
! E# Q3 d/ s, c9 I _! Rellipse(250,250,0,360,20,top);
- `: N, h' d) c1 b4 @) j0 S& }, Xrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
# S1 {# { v$ Z: gright+=5;
2 b* r( f: ]0 V ?# uleft+=5;
0 A, V8 C% d) ^8 N6 T1 {top+=10;# o0 N0 G3 t- i
}
7 L7 |' H% @8 W+ R1 b: Agetch();
; H* O% M4 z3 H9 ?; S}
【程序65】 t' z$ q" ~" A1 X8 q& S
题目:一个最优美的图案。
- j7 J( U; S- W; s) Y1.程序分析:
) `+ @! @: q0 [! T0 e2 k1 ~7 f2.程序源代码:
/ \! A6 A6 Z1 y* Y#include "graphics.h"7 W4 `# N! D6 L! |
#include "math.h"
$ c9 {$ z; O5 r8 `4 `#include "dos.h"
! @* L# h- Y! I#include "conio.h"$ S9 Y o+ F# R- P2 p
#include "stdlib.h", R$ X8 z# j1 G0 h6 F/ I. g/ [ K
#include "stdio.h"2 V/ ^; A& W4 Z9 |* A+ O5 b& {
#include "stdarg.h"
$ T# l. s9 f% K3 y: W2 [1 r+ D#define MAXPTS 15
& l6 [; G3 D( J5 _; `* U9 E#define PI 3.1415926
6 G# }2 o. }" S* f. u7 `, B& p5 Ustruct PTS {
7 S$ z8 j+ |- j& M3 e9 wint x,y;% N. o/ _- B$ I# ^' h5 o
};& _9 B$ T# H, K2 p
double AspectRatio=0.85;; p8 |# J* z) D, \
void LineToDemo(void)
- Q' P' s2 U0 |{
, q8 E! d( t' L* z' `# ^5 G* vstruct viewporttype vp;- \/ L% A# z6 j
struct PTS points[MAXPTS];$ a+ g% R: j7 Z1 Y% ~2 J( M
int i, j, h, w, xcenter, ycenter;) R% v( c. G# u: [7 l: g' o* K
int radius, angle, step;, |1 C0 B# l+ L! |" k
double rads;* S- @9 J: C3 t
printf(" MoveTo / LineTo Demonstration" );7 W9 r$ J4 u4 `# p
getviewsettings( &vp );; S5 \" C/ a$ j8 ]
h = vp.bottom - vp.top;
/ H; n `5 v$ d7 ^3 l% Iw = vp.right - vp.left;
; ^* T; I, I$ j7 xxcenter = w / 2; /* Determine the center of circle */2 m$ I* o! y& D3 L
ycenter = h / 2;
8 [% T- I: {! T. X7 zradius = (h - 30) / (AspectRatio * 2);5 o9 n; }4 o- B7 M' Y' h* a- I5 K- d
step = 360 / MAXPTS; /* Determine # of increments */
8 {" I3 R4 h5 ~; b/ I( rangle = 0; /* Begin at zero degrees */) t' z! F/ R9 K7 X
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */3 d+ h8 x, N& ], P
rads = (double)angle * PI / 180.0; /* Convert angle to radians */4 W' c" T* V: ?) K; L n
points.x = xcenter + (int)( cos(rads) * radius );
; q3 F' ]; X, ?9 Z- bpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );4 h1 B4 o! u0 X5 q/ I" L; J* h
angle += step; /* Move to next increment */! X$ Q" u, p# a( ^" c! C4 b
}
/ U7 t( X j! c7 E4 t5 ^( X- ]. t; Y6 J0 |circle( xcenter, ycenter, radius ); /* Draw bounding circle */+ m& q$ a/ g# C: p4 k
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */: o% |3 s" a9 R
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
# H5 |: ~; f, A! jmoveto(points.x, points.y); /* Move to beginning of cord */
# K) q7 ?3 Y- z8 O, C( Q- [lineto(points[j].x, points[j].y); /* Draw the cord */
1 c# x# A `8 y7 @2 }' {1 {} } }% t# C6 {/ m7 F( a ~; O0 p( [
main()
3 g- M) j# I o{int driver,mode;7 p# G8 \" [% S6 N
driver=CGA;mode=CGAC0;3 P u3 K5 @+ A2 c
initgraph(&driver,&mode,"");9 P5 u8 G7 L9 Y- P" [) W6 f
setcolor(3);
- G% D3 ^1 F' |7 G' c7 w' Gsetbkcolor(GREEN);
+ f/ H% _# t% C5 {5 d4 B. [LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
3 c6 |8 r4 F$ i& P( `& H- x【程序66】
V# [, x# r! X2 r9 D题目:输入3个数a,b,c,按大小顺序输出。 % B5 b- p" S8 a! l, O" v6 e6 u1 L
1.程序分析:利用指针方法。
! J; \7 e# N( G7 ?7 O" l* f2.程序源代码:
+ ^+ l( m1 i H' a+ ~/*pointer*/: W: a& `7 u: d, k
main()/ Z7 u: y$ u3 A6 B1 ?' Z
{" P1 V9 ?8 S) z' l* ^
int n1,n2,n3;
& E5 {- g3 C7 [/ H! {int *pointer1,*pointer2,*pointer3;$ E9 t% c# F; S4 n
printf("please input 3 number:n1,n2,n3:");
2 ^3 M7 y; Q2 Q* h6 t, Xscanf("%d,%d,%d",&n1,&n2,&n3);
3 w! p O- l/ z( O2 ppointer1=&n1;
: @1 S( u4 ]* d0 v0 b7 cpointer2=&n2;3 ]7 Q/ `' R) N: z
pointer3=&n3;' I5 w5 E/ j' ]8 A4 y4 t8 X: R
if(n1>n2) swap(pointer1,pointer2);# Y" A3 r% A6 `% I
if(n1>n3) swap(pointer1,pointer3);
# O. D: n2 c2 f* ]: N$ G8 ^/ Nif(n2>n3) swap(pointer2,pointer3);9 U" ]2 n7 y. [
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
* M- E* \+ T& F' J/ y/ @}
: p. Y& U! n2 {, X& pswap(p1,p2)
. _' V. d" A0 M' n. F8 r$ Rint *p1,*p2;* \3 N+ W. q C, E" u
{int p;2 s3 v) X+ U3 G0 B$ l) O
p=*p1;*p1=*p2;*p2=p;
; Z' X- Q5 [$ b5 U0 F}
7 k2 f) }& x: [8 g; `