【程序59】3 M8 {$ k' a- q% @1 E1 [7 {
题目:画图,综合例子。
5 l$ T% _, z' R; e- H. s" U( |' v1.程序分析:: C' O$ m; R; g, r3 _
2.程序源代码:
; V+ S0 A- B; T1 y. F+ a: Y5 Y$ s# define PAI 3.1415926# Y. F$ G* K# K, M+ }
# define B 0.809
2 T0 ~- ?, ]7 V p3 c# include "graphics.h"
& c- m6 N; i* o v#include "math.h"
5 E/ Q( j% y- a! v2 Emain()
6 T) N' B0 v5 H/ S) y9 K{
5 S5 j. ?! T9 [6 q' A/ E) ?; uint i,j,k,x0,y0,x,y,driver,mode;
' E! f6 q8 O0 o1 n' h$ o3 s0 u1 K2 T+ [float a;
) y5 b/ O# D. |" e3 J( D- Gdriver=CGA;mode=CGAC0;
, |4 |6 j- A1 t& w+ O7 Binitgraph(&driver,&mode,"");
7 u9 i0 E N5 r, b) N% vsetcolor(3);. d9 _/ {; H- s7 l+ h g2 c4 i
setbkcolor(GREEN);
4 ^8 U) K4 r: I& w6 G* wx0=150;y0=100;
2 |2 V- h4 F1 Ncircle(x0,y0,10);/ R/ z$ ~2 ?; z( }( W
circle(x0,y0,20);% E7 Q0 i* E0 Q# c& b
circle(x0,y0,50);7 a; A- G+ N# l2 t2 @ V
for(i=0;i<16;i++)7 q5 O" j+ G% b: _! L5 y- S1 ` ~5 ~5 j
{" y8 u+ t3 m5 |5 b0 c- r/ P
a=(2*PAI/16)*i;
9 l5 w I6 V( T x=ceil(x0+48*cos(a));
2 c) z [3 D# m y=ceil(y0+48*sin(a)*B);3 F9 b2 E% F( M1 u
setcolor(2); line(x0,y0,x,y);}
8 N M. F: U s- K' d6 C: n2 c( n0 Isetcolor(3);circle(x0,y0,60);( z0 D( |% ~5 P/ n; k$ i
/* Make 0 time normal size letters */
* W: j& T1 T; U- vsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
1 x9 K4 B% O5 ~$ O' xouttextxy(10,170,"press a key");5 t4 J- \. q, a. [6 o% }) c
getch();9 q# ?+ e: w, x1 z
setfillstyle(HATCH_FILL,YELLOW);# K, X5 J: b7 ~$ y+ j
floodfill(202,100,WHITE); y/ c' o9 k; T! T @
getch();
5 `9 e' u" j# {9 J- y8 h Q: gfor(k=0;k<=500;k++)# h0 i1 P/ r8 C) I
{1 }5 H1 g8 }0 h0 S( m3 ]5 m
setcolor(3);
" c" h1 i5 D8 }' e for(i=0;i<=16;i++)* E" C9 {) @8 e: r; {+ L
{
8 V/ b7 \- m& ]8 l a=(2*PAI/16)*i+(2*PAI/180)*k;( R3 F* ^" L/ q- v
x=ceil(x0+48*cos(a));
3 j* O) j7 u [$ `) Y% q y=ceil(y0+48+sin(a)*B);
5 ?6 e9 A0 d5 }; ~* x setcolor(2); line(x0,y0,x,y);
" x& {$ k5 K" Z+ r5 f }+ V8 Y& Z+ o2 t5 g, q% }+ z
for(j=1;j<=50;j++)/ N- W& Q; b# ]: a; P* F. d
{1 u3 |6 z2 X6 z4 w6 p- e
a=(2*PAI/16)*i+(2*PAI/180)*k-1;
}( C, \) y3 u2 r/ T x=ceil(x0+48*cos(a));
9 l R4 I' l4 {2 f! e y=ceil(y0+48*sin(a)*B);3 F0 C# u, Y r! \0 H8 o
line(x0,y0,x,y);8 M# B b0 S8 }9 U2 q0 `: z* N1 {
}
! l; ]% Q- b; s! P; B6 w% g$ F" @# o}4 C7 b, X* B3 r
restorecrtmode();
+ i$ Z! E8 t8 d- a; d}
【程序60】; A/ f+ p9 k3 r. \3 F3 I8 m7 ?% w/ }
题目:画图,综合例子。 2 ?/ t. X( Q9 K) U3 u6 X0 A
1.程序分析:/ m1 l5 R* Q7 a: i4 m( J
2.程序源代码:
6 U* j, I) y5 `7 s' b#include "graphics.h"2 U1 G/ }9 n, B; f( d
#define LEFT 0( V! {/ `2 u' M7 N' t$ ^
#define TOP 02 l* y- }3 h' p3 n/ ?+ ]# [1 u7 H
#define RIGHT 639
8 U( M: O/ S, d4 D, G#define BOTTOM 479
# Z+ N6 V+ l& y/ [3 ]/ R: ]#define LINES 400# |. C% i" n: U/ G1 y
#define MAXCOLOR 15
% I, j4 _' D' I% vmain()
9 U2 Y5 \' {: J0 N{
' @; O' U) ?. e: B6 s/ j# ]6 H8 E7 fint driver,mode,error;
/ u5 M" Y/ F5 [" C0 tint x1,y1;
0 T$ t# l' x/ \6 B5 j) O5 ]int x2,y2;
9 g/ t) E6 `2 m2 D) b" t" jint dx1,dy1,dx2,dy2,i=1;
$ Y' n: a9 X9 }int count=0;9 F n3 s( ]6 A- j, e# F) \
int color=0;
' R5 g8 [' l( p" s* kdriver=VGA;
& O& G8 w: l ]$ }5 |mode=VGAHI;" r) c" E3 B6 t& o/ y- X
initgraph(&driver,&mode,"");
5 Q2 w1 i4 n3 I1 f3 O& [8 U- [ z Lx1=x2=y1=y2=10;
8 J8 s2 I' f: K$ Udx1=dy1=2;2 k2 H5 h2 _; e5 W; G. X
dx2=dy2=3;# ?# g4 X$ Z8 y% U
while(!kbhit())
5 {$ }+ j# h3 ?; c; s, t, d, x{, @: G. S$ |# X7 S% v7 _
line(x1,y1,x2,y2);) `5 L8 Y5 p! t# R( [; Y) ] G
x1+=dx1;y1+=dy1;4 i7 A' d* c8 h* R' p% j
x2+=dx2;y2+dy2;; J; _- s. W! S0 M0 K
if(x1<=LEFT||x1>=RIGHT)
5 E3 @7 A$ x2 n9 l dx1=-dx1;
* _$ m& Y+ c1 ~: U6 `- q% d if(y1<=TOP||y1>=BOTTOM)/ N2 i w5 }) k( @! [# f: A3 e5 f
dy1=-dy1;/ i, [" \3 G* }0 n
if(x2<=LEFT||x2>=RIGHT)
( ?0 W0 k) ~5 o. Y% [2 }$ ^ dx2=-dx2;
1 k" M" f; r# ?# Z8 w | if(y2<=TOP||y2>=BOTTOM)
) o3 _8 Z5 ^# R$ L0 L2 t6 H) O dy2=-dy2;5 z9 O( h4 C& V5 E+ T8 w4 m M
if(++count>LINES)
$ c! }$ j7 S3 w {
/ ]% b$ q! v _1 y; g setcolor(color);
; N. O- S1 t0 ]3 { color=(color>=MAXCOLOR)?0:++color;
. c7 n4 P& h6 I0 ^1 c6 \) J7 S; N }7 n! H( m/ t% x4 P u" X
}
$ S4 M' Y( W6 Hclosegraph();
+ ?& H: I0 W, o) `}
, y, ?- I" K8 K$ p2 U9 h9 ~
【程序61】
: C2 d9 F- ^9 u" p: T题目:打印出杨辉三角形(要求打印出10行如下图) $ T" F8 r+ W3 U+ _7 i
1.程序分析:, f1 F$ E Q" y: R$ R _9 x
1- x0 X! n& l: Y" o5 c3 l+ J5 Q
1 1) i$ L4 r: F, `
1 2 1/ O# V( J7 @3 p1 o/ u4 _, ?
1 3 3 1
: y3 o8 J2 h) ` 1 4 6 4 1% z% E1 O: y: ]
1 5 10 10 5 1 : j' U; {% F% e" A: }1 b
2.程序源代码:
6 {" n; X% w8 C+ a0 M& vmain()4 m r8 T# }* [; Z3 [
{int i,j;
, D( _: G3 J* x' }7 |) hint a[10][10];
9 e# o& X4 \+ F i0 _! Xprintf("\n");
: c' n& O3 w; r' B% N4 Wfor(i=0;i<10;i++)9 c) [% R9 u" e- ~+ a H) _
{a[0]=1;. c* C& X( F- H) X# e8 R C
a=1;}
4 O; _, M6 t: {, W" r$ wfor(i=2;i<10;i++)
3 P8 N# U% ~! i for(j=1;j<i;j++)' _) u- d/ t- A; t7 C
a[j]=a[i-1][j-1]+a[i-1][j];+ J5 a- T+ z5 B0 P
for(i=0;i<10;i++)
4 o; F7 f) f* \, `$ ~8 q. N {for(j=0;j<=i;j++)" N5 i6 N" L* F k& `
printf("%5d",a[j]);
' [% d" Y( Q Z' ~, w" J+ @ printf("\n");
! U9 n- i( M# ]. O4 T }6 U1 o1 Q' b+ x. W4 u
}
, Y+ A% k/ V2 U( Q+ F" |
【程序62】, Q1 N+ |5 H$ p" [6 E: Y& ]1 i- t
题目:学习putpixel画点。
5 {4 J& x6 N, M1 a+ `( {1.程序分析:
' E; P( b0 k% s2.程序源代码:' f# t* J% v0 Q9 c2 {; _8 Y
#include "stdio.h": r7 o3 c0 N. D
#include "graphics.h"8 |5 @7 ~# Y% Y8 T. C! s/ E9 X
main()
$ _5 H5 Q. M% z8 L+ ^' Q q" t" ~{% j" u8 M: J. }4 D
int i,j,driver=VGA,mode=VGAHI;6 ^9 {8 T2 f5 D% C
initgraph(&driver,&mode,"");& R: n9 I: w5 l" P
setbkcolor(YELLOW);6 R- g5 O4 Y& A: E' w+ d
for(i=50;i<=230;i+=20)" r( \" z% c+ o& C
for(j=50;j<=230;j++)
8 {2 c! d) n* `4 j5 n- k putpixel(i,j,1);
6 J; w0 w! _- \' _* s, k" d* Xfor(j=50;j<=230;j+=20)
. L2 o5 o% V3 |3 ^) O- n7 _: J for(i=50;i<=230;i++)% }5 j; Q7 T* s3 [ I" ^
putpixel(i,j,1);4 _3 j( c5 V7 }+ j! D! p8 H
}
w4 o* \5 y6 W1 y! N【程序63】 S, F! V# u* n. s4 J( F9 k/ t
题目:画椭圆ellipse # p1 {& m+ R7 w1 L5 j
1.程序分析:
" p3 {, R2 ]0 y. e- T' ^5 N2.程序源代码:# B2 o0 G8 \; \& Q8 l) |+ F& |. v
#include "stdio.h"
0 d; O$ P' }9 u$ \# ~7 s* w% G N9 Z#include "graphics.h"
" d& Q" Y1 m; M#include "conio.h"
7 A6 z* q1 I; b- w! @: Tmain()
8 r: B$ m7 s- ?0 f{
, [$ H& E* J' A6 hint x=360,y=160,driver=VGA,mode=VGAHI;
" E$ M& V @3 C7 p W, Rint num=20,i;
' [* p+ T, {3 j1 d* b- ~. kint top,bottom;- z1 x4 s: S2 A' t; _
initgraph(&driver,&mode,"");
6 a/ K8 ?% J4 J8 E! F; ntop=y-30;3 N& R/ L- D4 a, n# E
bottom=y-30;/ f8 ?% e9 L: l; V( S/ X
for(i=0;i<num;i++)
5 W1 i0 _2 C4 M4 \{) u- A4 {2 B/ t0 D1 P( _
ellipse(250,250,0,360,top,bottom);1 F+ u& W/ X9 i* P2 H' t9 |: E
top-=5;
: O$ g! D) K( C) P" I" D1 X$ Sbottom+=5;
5 x$ v1 S4 i, f0 b3 V}2 q' k4 E- x: P7 _5 n. I) Q
getch();8 P h6 ~' r) P4 r
}
【程序64】9 h* K6 d9 q) c/ G: L8 _
题目:利用ellipse and rectangle 画图。
- C7 h8 U# E/ P, }1 V# Z7 J1.程序分析:
& R& l% t# Q. t, M, z2.程序源代码:- B& f9 z) X! Q
#include "stdio.h"
* Q" B! w' I; l4 j4 v6 A#include "graphics.h" F4 H) q6 ~* Q+ |' j
#include "conio.h"0 F: S! T2 p- n. M# M; ]# P
main()9 c4 i- a' P% b {4 ?7 y2 O/ b' D
{
8 B5 z' i& K8 h9 M2 r+ S, b& E/ J7 @. _int driver=VGA,mode=VGAHI;
; U+ e) V; W7 D+ tint i,num=15,top=50;
+ M, P2 X! x- }* ^9 G+ _ Rint left=20,right=50;
+ n! p. \4 _5 u" i) l9 f2 F: O9 |initgraph(&driver,&mode,"");2 L5 c+ o# Q' E7 O8 T$ D' A
for(i=0;i<num;i++)
4 H9 Q1 D: n! ~! R: p6 _5 O{
, m) b+ U( p$ {. R5 jellipse(250,250,0,360,right,left);% O4 r1 v( y: Y9 I' {+ H3 c; P
ellipse(250,250,0,360,20,top);& c% M+ |/ b+ @% W
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
; ]" A) z% I! U# z0 |right+=5;
1 k# v/ W, |0 L3 M9 h( A% _. a; yleft+=5;# d8 J. I: V& ]# b: S
top+=10;. [& _1 f; W0 c# O, L
}, ]$ }3 b6 R- S4 s) I% `
getch();0 i( ^& N* u" O' I
}
【程序65】
6 ^* v- n E! m/ t) n* W题目:一个最优美的图案。
5 a+ T% i! n! _9 g8 j) j" R& O5 d1.程序分析:( K% U/ A v" A) n/ _
2.程序源代码:8 H$ G v y$ u$ K, R4 K, M9 t
#include "graphics.h"" p* \; \( Q6 P5 r: c1 ~+ [
#include "math.h"# z" q1 A; T5 L/ C/ U% p8 L
#include "dos.h"
3 {6 C* u* D- S$ U#include "conio.h"
' k1 M: `0 O/ U* c#include "stdlib.h"& m) I" {( P7 v5 s0 ` e
#include "stdio.h"
. `& Z' K2 m+ L) i#include "stdarg.h"
* r+ p' G2 b# s' O* a9 [#define MAXPTS 15
s1 U* n7 _; w" E' o% G#define PI 3.1415926
: F8 m6 T! \& l4 m4 C) istruct PTS {
( @" Z7 O; w1 N$ B" Jint x,y;1 f: X* @8 {% f& l
};
) y- H7 F3 A8 D. A3 h% `double AspectRatio=0.85;
0 f7 L Z6 g1 W/ o4 L; E- Mvoid LineToDemo(void), [# q/ z% T* |, }; ~6 |
{: \: c# @! U5 h7 J# t8 F( y1 ^+ k
struct viewporttype vp;, T1 [/ E! o- J. p5 z/ I
struct PTS points[MAXPTS];% Y2 g, i: h1 a
int i, j, h, w, xcenter, ycenter;
$ N* R# e3 o& Z* Mint radius, angle, step;
- F4 C. P0 b I6 Sdouble rads;3 V: E( K+ }) _6 K* {: D+ k5 F
printf(" MoveTo / LineTo Demonstration" );
' @) m( ]! r+ T* ^$ ngetviewsettings( &vp );( S0 E% t$ k! C( b
h = vp.bottom - vp.top;
9 ] D7 _% c8 Ew = vp.right - vp.left;
0 c' c7 j( a+ g8 r8 D5 {) Rxcenter = w / 2; /* Determine the center of circle */
: v$ r9 J* } \. A. J o/ b# _ycenter = h / 2;9 l6 t4 h$ O p9 R1 Z
radius = (h - 30) / (AspectRatio * 2);
* @& Z5 N- y0 ^! s; Estep = 360 / MAXPTS; /* Determine # of increments */
: u8 A" B3 N langle = 0; /* Begin at zero degrees */6 n, R X" G& { Y. f0 a/ f
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts *// f, m; n) V: L8 h v6 R
rads = (double)angle * PI / 180.0; /* Convert angle to radians */1 h2 g2 ^# C) a0 t
points.x = xcenter + (int)( cos(rads) * radius );8 P5 I% \, ~2 A) k2 q1 R; G
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
a) J0 [$ S( }8 ^. E; hangle += step; /* Move to next increment */
+ U6 [5 J9 [$ C2 u}
+ z& u+ W" A+ y1 V; k; m; t3 ocircle( xcenter, ycenter, radius ); /* Draw bounding circle */
8 p! o3 r; ~+ n3 }for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */, i1 k, h% J$ g- O! X% ]: ~" ?' w
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
1 B" A9 q$ ~+ J6 mmoveto(points.x, points.y); /* Move to beginning of cord */
5 e, W9 Z) ]# Glineto(points[j].x, points[j].y); /* Draw the cord */7 Z! Y* P! k( {$ y- ]
} } }
3 V: J$ z6 c" N7 h, fmain()
! J [4 g' P0 V# U6 E o{int driver,mode;) R5 @* D3 q) f t4 n
driver=CGA;mode=CGAC0;
5 }8 U# g/ s, w& Z) Minitgraph(&driver,&mode,"");
. {( o5 K7 |% O G8 q. Asetcolor(3);" `' g8 z% Q& t0 l, G- A
setbkcolor(GREEN);: [4 {' D. m0 `
LineToDemo();}
关于图形的程序最好在纯dos下运行,c的图形函数有可能和windows冲突,出现非法操作
; J$ ~# V) ^; u3 G【程序66】
* z6 g+ q4 L8 `题目:输入3个数a,b,c,按大小顺序输出。 : ^- X& n3 C: k
1.程序分析:利用指针方法。
1 r( B+ Z P" R: J' l; G" M2.程序源代码:
" a. T* P, S9 O" W3 W) b( n/*pointer*/' `+ |9 u# g7 @+ u, _
main()
9 u4 ]2 \3 m+ h# Y{
- o) @4 Z3 q4 bint n1,n2,n3;
5 l& C" K r9 d) `int *pointer1,*pointer2,*pointer3;: M( A/ t5 R" C! C( d
printf("please input 3 number:n1,n2,n3:");
R0 w6 a* w6 w2 escanf("%d,%d,%d",&n1,&n2,&n3);' ]3 }4 @! w7 S7 U+ Y1 l
pointer1=&n1;7 P( k, U8 s V5 Z
pointer2=&n2;& `) _& |1 B7 I8 c0 @1 |) ]* ~
pointer3=&n3;
* Z1 [1 V# n8 b' {" M1 aif(n1>n2) swap(pointer1,pointer2);3 D% M0 L9 T4 N0 v
if(n1>n3) swap(pointer1,pointer3);
4 N2 F4 o- f/ R3 iif(n2>n3) swap(pointer2,pointer3);
# y- L! L" D% u& ~4 E& K1 ]. Rprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);2 E! C" Q1 Z2 a( }
}
6 x( P" E! I- F% {4 f: pswap(p1,p2)
0 U: F9 N+ p3 ^7 ]) `3 J) M1 G/ `int *p1,*p2;
2 _5 G% h8 o1 z2 X& G) i v{int p; T- g F" @; [- o9 i3 x% t
p=*p1;*p1=*p2;*p2=p;1 k% e' x3 X8 y$ C: |: z7 m
}
8 v/ }! ^/ ~: Q$ q9 b/ o