|
【程序61】
" F' m( ]+ d1 F0 `- `题目:打印出杨辉三角形(要求打印出10行如下图) 3 \! x- i5 @& ?6 H0 E
1.程序分析:- y ?7 ?2 Y% T
1- Q4 V6 ] ?7 ?2 R: q
1 1
. y! d* v8 D# W. y" K 1 2 1
( Z% A. g6 p _ 1 3 3 15 V8 a6 a3 y$ }
1 4 6 4 11 ~% |2 o, [2 S
1 5 10 10 5 1 1 Y4 E( q; v: h: M
2.程序源代码:9 B: _/ l& V5 i5 J& W0 N
main()
& j) S, v$ M- T! m{int i,j;
9 w, ]" T( I$ ^+ `/ ~2 Fint a[10][10];
& d; s r: c' c( G/ ~ @- H" Vprintf("\n");
( |1 H8 ~! m2 _/ Pfor(i=0;i<10;i++)
9 r, q( o# q8 h9 Z1 S/ u5 p. x {a[0]=1;
$ f2 r, B4 a5 y7 n) v/ U a=1;}. ]1 o# L) _. j' s3 f. G
for(i=2;i<10;i++)
9 y: Q3 D) T% x& k& R W( f4 e m% W for(j=1;j<i;j++)
8 s6 Y# }. C/ k- j a[j]=a[i-1][j-1]+a[i-1][j];
# P% u3 V) C) u9 y4 h0 @# h. pfor(i=0;i<10;i++)5 X; ^) U% C6 P, k) I# R- z
{for(j=0;j<=i;j++): i; q; ]. D6 l9 x, v7 k" T
printf("%5d",a[j]);9 D$ c: v% |- v& n4 u. R3 ~3 J
printf("\n");% `7 ~3 G+ l' L9 j
}' _* S8 _ r$ r# W' E$ A
}
/ [; x0 Z7 u6 l8 O==============================================================7 P3 b& L: s& f* G) N
【程序62】
! `- z1 B8 |. Y3 C9 s. G题目:学习putpixel画点。: _6 j B5 _- ?, `5 f
1.程序分析:
7 J6 Z% l. u$ v ?2.程序源代码:
1 C$ `% ? P( K6 s( j+ E( `#include "stdio.h"
}- \8 y: {- y#include "graphics.h"
$ Y/ H7 R! |4 B. Smain()
G- A$ t* D; p! K7 s, ~{, B5 T$ |. d1 z# u7 H4 v
int i,j,driver=VGA,mode=VGAHI;6 a8 R* h2 x$ m: z
initgraph(&driver,&mode,""); l* _( |1 A: f& A: [: m
setbkcolor(YELLOW);) [; E0 `2 t- W
for(i=50;i<=230;i+=20)1 A" V7 X- }, X l' t! k
for(j=50;j<=230;j++)1 \$ R$ Z# l, q# `# v9 g; r
putpixel(i,j,1);4 f0 c5 n i6 ]4 S) u! O9 [; P' W
for(j=50;j<=230;j+=20)1 _% k# ?; N5 O' F1 N
for(i=50;i<=230;i++)" T; G: ~/ B& k; y: G) b
putpixel(i,j,1);4 F6 Z' p$ J7 b3 t5 b, a
}: c+ W0 X% o T6 q
==============================================================2 f( z+ S; D, t1 ^0 t, @* q& x
【程序63】6 c/ }8 R3 {, j" m, u' y
题目:画椭圆ellipse * c7 _1 |' Y$ R" L6 x5 H
1.程序分析:
, I. l9 ~0 t! X* F4 h2.程序源代码:
5 l# K( Q i* y: T7 O& d7 r/ @+ l' v#include "stdio.h"6 A3 j3 G/ H' N' B
#include "graphics.h"
$ d% _9 d" m. Z+ X$ p1 s. Y+ P# n#include "conio.h"
7 u1 h+ `2 W2 n& b( imain()
2 C9 v8 l, X& E* K, O{* O3 m; C3 Z+ g9 K8 O
int x=360,y=160,driver=VGA,mode=VGAHI;
4 E8 z4 Q6 H1 u% _int num=20,i;$ I* k1 v' m5 H" p/ h# b
int top,bottom;
7 Y+ h" @1 j% s4 W8 k+ r, Q2 w8 }initgraph(&driver,&mode,"");& z0 T1 J$ E/ A! V W
top=y-30;
6 ^1 l I* t; C& {7 s' sbottom=y-30;: j6 L& {4 ?2 B6 i$ ?+ @. ?; ^3 t3 W5 d
for(i=0;i<num;i++)
D& k- W7 [5 ?( ~{
# C0 P/ A$ B+ N6 T6 j( A9 `1 rellipse(250,250,0,360,top,bottom);$ e# x- R* [) Y* A
top-=5;# t3 o' \: ^- L7 t- E- f& W: \
bottom+=5;
9 x) u2 g9 |% `3 S) _}# I6 f* i# P, z8 U# i5 L! _6 X) ^
getch();
$ `# ?$ T# I5 y) g( B9 t: T* t" z}8 g6 s/ h2 m i8 K) P
==============================================================
# S0 G2 e$ o1 h% b! K7 X. U* H4 b【程序64】
" h$ Z$ o/ M9 @题目:利用ellipse and rectangle 画图。
5 `- R; q' |. E5 ~$ {2 H6 q- H1.程序分析:
+ e% O8 G8 m8 ]+ u7 n2.程序源代码:
7 k9 D5 D7 K9 ]. F& f% Y& x) x#include "stdio.h"
5 S/ |% U/ l+ ]1 S: x3 b#include "graphics.h"
4 f; r7 _, Y; E0 P, I/ a% i9 J#include "conio.h"
% V) s, m7 ~& V8 vmain()
4 g8 i, n$ Z6 K& G- w O; T! U3 a{
: a. B3 W; A- k b" Jint driver=VGA,mode=VGAHI;8 O) y# O1 u' Q" B' F, P$ o
int i,num=15,top=50;
( n8 _/ i3 o2 a! Jint left=20,right=50;
}/ P8 `* U* g0 k: ]initgraph(&driver,&mode,"");
/ ~, u. H; n5 Cfor(i=0;i<num;i++)
0 G3 @* X t1 T' x6 \5 O3 i{; l: P# h/ T$ c7 [
ellipse(250,250,0,360,right,left);
% x2 |6 N4 g4 L; R3 m, D" Lellipse(250,250,0,360,20,top);: o5 F* H1 K X0 [4 F4 W( R
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));" h( q7 g) I* S; l, k b J) P6 S
right+=5;2 f& D, b$ n* U! F5 G) ~! O& h
left+=5;
% x: U2 `- {$ k) q/ k- s: r( s' Dtop+=10;
% B" u& n7 x3 O8 W: p}' l! K) M% j3 e7 u8 t+ k1 U" p
getch();/ W8 s, B0 a, V$ i
}: i8 ?' ~5 j: `% U
==============================================================7 q8 M4 }" C2 S) f6 F
【程序65】) A! h. T8 m( Q$ q
题目:一个最优美的图案。 6 h- o% I+ P2 L6 @
1.程序分析:
$ i# t5 E( B, D5 u2.程序源代码:$ s$ e4 w/ z" ?4 R1 N6 r
#include "graphics.h"
3 r6 k# ]( e6 X" Q% H c#include "math.h"
0 O! c. t+ b5 t1 j5 j% g#include "dos.h"* o# m) _, ~$ x1 d0 V5 k( ^) M
#include "conio.h"
$ s I' R; b8 |4 }3 f#include "stdlib.h"
x2 b# R' E) K( u7 o! x#include "stdio.h"8 _9 e) I0 z, n
#include "stdarg.h", q0 d! _ a! I. p; s$ p& i
#define MAXPTS 156 Y) h8 `/ a- |3 P! b3 }; T
#define PI 3.1415926
% a, ?7 X/ ?. c1 estruct PTS {( i/ ?" j' C4 h+ w! `
int x,y;3 D- H2 a" V3 H
};) _0 y& ]" B8 f! O# n) i0 J/ p
double AspectRatio=0.85;
" ^1 f- _. b. h% nvoid LineToDemo(void)' b _$ v; F# q% [$ d
{
7 Q$ z$ m& u: E: C# U Z5 q" \ q! ~/ e8 Bstruct viewporttype vp;! m7 _, V; b' N2 f
struct PTS points[MAXPTS];
4 i& ?, X9 X, C: @! w. j+ rint i, j, h, w, xcenter, ycenter;% A- x9 S! }1 T$ f4 @. _
int radius, angle, step;8 i: M" R6 P# g6 }6 ^) |% ]& ^ G
double rads;
$ C+ K/ Y5 D9 ~, N; c; {3 Aprintf(" MoveTo / LineTo Demonstration" );( u* ` S9 M E3 S5 d1 F; p
getviewsettings( &vp );
& c4 } O" W; ^% w; Ih = vp.bottom - vp.top;$ u" U* i# E% t, [* I$ o
w = vp.right - vp.left;
- t- M# F* ?8 O1 Uxcenter = w / 2; /* Determine the center of circle */
1 ?& A: ^9 B* c3 Bycenter = h / 2;3 \, h5 j9 H$ b* ^
radius = (h - 30) / (AspectRatio * 2);
% ]4 R8 x3 H% I- ?, l9 ~+ P; Y. cstep = 360 / MAXPTS; /* Determine # of increments *// `" z! O. K _0 R) a
angle = 0; /* Begin at zero degrees */
. e# \' C+ c; k' Tfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */7 H {7 N& h7 j4 v/ ~& B
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
# @3 s) m; I; p7 c/ D& o) ?) epoints.x = xcenter + (int)( cos(rads) * radius );" b" l: k; u; G' \& g
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );1 X% m; N; O- G* A
angle += step; /* Move to next increment */+ q3 k6 o% r- f- r8 p$ N
}4 }' x( G; [: ]* D/ P
circle( xcenter, ycenter, radius ); /* Draw bounding circle */
. {6 i8 T2 k: c3 xfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
# T3 C# D2 U' |& B5 u5 Rfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
/ E7 H: S+ k; N! W8 S9 r. dmoveto(points.x, points.y); /* Move to beginning of cord */
q1 J' ]4 \' r M8 b% nlineto(points[j].x, points[j].y); /* Draw the cord */2 a7 t+ A4 o8 F3 p b4 S4 }
} } }0 z- w1 x% n& U) J. c$ r5 `
main()
4 _. O6 o1 @! R# s6 D- x{int driver,mode;- j! w. B$ ^8 q8 U" U# R* a$ q& K
driver=CGA;mode=CGAC0;8 g1 c* b0 N% Z- n- z" T- _9 k$ z
initgraph(&driver,&mode,"");
' t6 c+ Z. V9 Asetcolor(3);' U9 r% }& \' b+ q; G, W5 J
setbkcolor(GREEN);
+ {9 R9 @6 `: }' Q( E, q2 zLineToDemo();}/ b8 k* q2 l& [1 G( X/ d1 f
==============================================================
& W @5 K/ q. I$ S2 ]" u$ S【程序66】
0 ]: P! Z4 O. b( u% {7 r题目:输入3个数a,b,c,按大小顺序输出。 : `- V: g. o, U d& y! h
1.程序分析:利用指针方法。; Y- U6 c" s3 ^1 K7 [1 g
2.程序源代码:
6 Q8 P- K! s- E6 s$ q/*pointer*/, T& ~. F3 L& a( h: ?" ~4 e6 X! @
main()7 B o" b0 k, J& Q1 O3 m4 Z2 X
{
: h; @6 B+ `# w0 Yint n1,n2,n3;
: [; o9 u, M* ` I* E; N: t% c+ nint *pointer1,*pointer2,*pointer3;
& j+ |1 F _0 x: L/ t7 l! Bprintf("please input 3 number:n1,n2,n3:");
D/ @' H" O# }! w; \* ~! I3 vscanf("%d,%d,%d",&n1,&n2,&n3);$ N4 r/ I3 y% W
pointer1=&n1;9 \% w0 u2 |7 w. c8 K" H
pointer2=&n2;- m0 C& n* W' f& a" j4 x3 K
pointer3=&n3;- Y0 s( g: i, b5 u
if(n1>n2) swap(pointer1,pointer2);3 M( { Q) C+ o/ B% L
if(n1>n3) swap(pointer1,pointer3);+ n7 d" Q. X/ x
if(n2>n3) swap(pointer2,pointer3);
5 I; _/ ]! |0 D* c, o8 H8 yprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);4 w" _# ^) H/ k: P7 `7 m8 ]
}
3 }) p ^6 D3 m$ A- \swap(p1,p2). ?$ ^0 `1 ^- e1 }/ ^
int *p1,*p2;: v/ Y2 X7 l) ^" V: r. W* k
{int p;
7 @7 \9 T$ c% _, A+ _p=*p1;*p1=*p2;*p2=p;
9 j& C" [6 S* W9 s) i4 c" ], o}* ]/ P3 t9 {& v% u8 k3 A; u# N+ N3 x
==============================================================
4 M. j7 g% l4 w8 e【程序67】' \$ ]1 `& h5 `
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。, x4 L8 j$ @' s ~
1.程序分析:谭浩强的书中答案有问题。
" m9 N& n0 }0 ?4 x6 Z& O9 M- o( e8 L2.程序源代码:
9 H4 @0 h6 ^* i9 u- qmain()% U' x: I6 G& b A+ V8 S. `# L+ K
{3 q! }; e9 s; W. s/ b7 R
int number[10];& R& K$ C) I! f7 K! ]
input(number);* s$ r6 P% r! r0 k; U
max_min(number);8 y* L/ d. {& _0 U
output(number);4 U/ }% i1 V! r5 Y
}
! P/ `- ~" J6 l2 U( j6 Sinput(number)0 }# E5 j6 F: V; E! b2 |" k
int number[10];" L+ w2 P6 k5 f: l- u
{int i;
6 [/ h$ Q o" I* p( b7 j' [for(i=0;i<9;i++)4 b3 c' A- k6 l# e2 u) J. i/ w
scanf("%d,",&number);' |4 w( q$ {) o& t3 k$ k: W9 ]! d
scanf("%d",&number[9]);
) b: y, I9 K6 J( g}
9 h) d8 W! X4 I7 emax_min(array)8 U8 G8 k$ Y& S% \/ ?: F A
int array[10];
) X0 \% s7 d* _1 Z3 Y1 i) W# b{int *max,*min,k,l;4 u: D, W2 @; {
int *p,*arr_end;* L6 l( s" q" r
arr_end=array+10;3 U3 ?9 L+ i( V2 R2 ^ ^
max=min=array;
" e4 ~& n7 A+ j2 D- q5 `for(p=array+1;p<arr_end;p++)# u/ C! l! p( q! |
if(*p>*max) max=p;3 f+ { }. ~% i+ u: G, H
else if(*p<*min) min=p;
0 a4 a8 X- i; U k=*max;
. R# @7 W* V* A7 a! I l=*min;, R6 U+ [1 Q. O, d1 g* k
*p=array[0];array[0]=l;l=*p;% b, Z5 q: Z9 d7 t
*p=array[9];array[9]=k;k=*p;; x! q. ? c. N9 M; @0 Z
return;
- |1 s; V6 Z p5 |* n: J}$ W5 V4 g3 [4 A0 m0 N5 T+ _9 K
output(array)
- S- ?5 R" f& R! v oint array[10];
7 i$ O9 ~/ p6 u4 ^{ int *p; K' v& I2 G( ~6 i
for(p=array;p<array+9;p++)
! y/ J% `% |% Y) U; n printf("%d,",*p);) j, y) ]2 u( y! O+ F
printf("%d\n",array[9]);
: l" F+ Q: z E- B# U}, ~ P0 t" Q, ^8 y; z
==============================================================" K* j! o0 m& q: d" j
【程序68】
5 C2 w/ X2 r9 v% L) ?* M$ X题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
% E6 E/ N7 p1 ?' r1.程序分析:
% i2 _+ x! A- m$ E5 w9 [& n2.程序源代码:
i! |0 A# g1 {0 n5 W* c# {+ S- R) Amain()
; i" w9 F g3 T6 X. d' N3 R9 M) q{
1 u# S8 t/ s9 F/ b2 s* Uint number[20],n,m,i;
2 n/ Y( }0 F+ G: h4 z( `+ \ qprintf("the total numbers is:");; L& k2 m* S& f( ?2 V8 z7 h2 Y
scanf("%d",&n); V0 ^( ^+ f# b4 Z
printf("back m:");9 x, \6 Q2 l2 b" B7 K0 {! d/ D
scanf("%d",&m);: h' A% c" {2 D$ g* n
for(i=0;i<n-1;i++)
$ Y' Q A% ?. H8 x/ N4 Y scanf("%d,",&number);, K" u8 Z1 ~# N0 Z; u! k
scanf("%d",&number[n-1]);
) K- _1 P% a+ Pmove(number,n,m);
}$ o+ M9 X. V' O8 p- X. ?' Q4 ~for(i=0;i<n-1;i++)
4 `8 H8 q+ p6 T* ^# H+ h printf("%d,",number);
. S; P0 S5 }8 u6 ~printf("%d",number[n-1]);4 j, S+ @: u; f0 W$ d; ?
}
. h5 V5 V+ ]# R# nmove(array,n,m)
5 _3 p: A; i1 C X8 Gint n,m,array[20];
1 |5 f0 W/ l# \4 ^/ p) F+ x# J{' \& ~5 e* f2 C- _0 f2 T2 ^) f
int *p,array_end;
* o+ m& S1 D6 n3 _array_end=*(array+n-1);$ M2 V! h& c. a
for(p=array+n-1;p>array;p--)
! A- F0 I2 d' m8 x9 L8 P" c- h *p=*(p-1);& ~( y; C# v1 R! Q8 D: M
*array=array_end;' U, f( Q$ }$ s0 I% I; r
m--;
& N" K7 @1 {+ S7 y if(m>0) move(array,n,m); _5 k5 |" y! k( n2 u0 f
}
8 v' A, K- U5 o* D. }==============================================================$ [$ J0 |# t: ?
【程序69】
; Q7 N* } T0 N& c题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出
/ D5 d8 q* z* _7 B3 v$ Y, X' G, m 圈子,问最后留下的是原来第几号的那位。0 J1 M a" N* a9 i
1. 程序分析:
- z! k! I9 ~, Z/ N4 n) P2.程序源代码:
3 j% |% J# @8 I3 {#define nmax 508 g+ _/ }, D1 |6 _0 y
main()
- g. C# g) k! o4 o/ n) F{
4 R" A" J( Q% mint i,k,m,n,num[nmax],*p;
, Q. l* W; p$ G% k: ?8 Z' Aprintf("please input the total of numbers:");
! q5 Q/ G) @& p' T& l! Lscanf("%d",&n);
; e; N8 w' C' v7 Y& O! }p=num;6 z6 Q! k. ?3 R! b
for(i=0;i<n;i++)8 D8 n' I% W) [9 Y6 X/ s
*(p+i)=i+1;5 S: U% h" I7 t8 _* c W
i=0;; w9 P; ]0 W0 o K7 H7 @. [ o6 g
k=0; \/ V, u% E0 d) d3 c6 G
m=0;
% O: y( B0 P8 i C7 Y while(m<n-1)
$ s# ]* A- R4 p. Y5 `1 e. p# c {
2 u% C+ y$ Z o/ d8 X if(*(p+i)!=0) k++;
" H7 d7 {& v9 E* q3 S0 l9 Z1 y4 b if(k==3)$ ]2 g5 f/ h3 d. }9 }6 y
{ *(p+i)=0;0 L! A) J, q3 T& i
k=0;4 g3 ~2 f$ M2 D
m++;
% D+ b' ]/ x( D( n# g, U- Q g( d }
$ Z, Q, w0 a( S& Di++;
5 \; K" \' M" ]9 l, k6 W, lif(i==n) i=0;4 t4 ]; m2 ~9 {! ^1 s. E
}
8 n* Y7 F: I8 m1 |8 y3 mwhile(*p==0) p++;& l, c0 ~; s. k1 g# S x5 e
printf("%d is left\n",*p);0 l- |& _/ ?4 D) O# W& k
}+ ~5 {; @; K3 o( i# {6 z5 v
==============================================================( x8 }4 H2 W6 H! `8 @! U
【程序70】! Y2 t; d1 G& h! g4 a: L" f! c
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
1 b3 X* P3 r% ^; `% _1.程序分析:
% a+ [& t4 }2 d! o9 S2.程序源代码:
% R# a8 X8 |# `& Jmain()
& @( [0 C; ~6 |0 o. @9 E6 s{: O- L+ @- s* U+ n/ u5 X! ?+ \
int len;
5 ^* i: Q( @ P3 Lchar *str[20];
4 G- |5 f* A6 {4 K: Sprintf("please input a string:\n");
( s8 @( b: ~ I. N) T; I+ tscanf("%s",str);: @4 J) D% s- O) [' q5 z6 S
len=length(str);$ N- H6 z) }- ]
printf("the string has %d characters.",len);; ]! M/ A( s. o) `
}
2 @* ^7 w/ J) j% X$ ^0 s S" Vlength(p)
% C# {; g. [- z( s) P2 uchar *p;
. `3 }- b4 q' f2 k% H{2 c; ~( `- O( p( o/ r
int n;
& |0 s( |2 f9 l" W7 Fn=0;
3 G! r0 U c3 O8 y$ ^+ q" Owhile(*p!='\0')( ^# k ?7 `6 A \& n* d9 A4 G6 Q4 U
{
5 y9 X- C! d: W( W* I, f n++;* Z, o5 }/ N7 V5 {
p++;+ ~ S% J, b; G E" ^- C+ l( |6 d7 Q; K
}
% u& k1 |: }8 i. e0 mreturn n;
+ d! o5 I/ D7 V6 X7 a! k$ r} |