|
【程序61】
8 W6 s$ L& t+ o( Z5 C- Z题目:打印出杨辉三角形(要求打印出10行如下图) 4 R1 Q: I- H8 O! ]: f U7 N
1.程序分析:5 a3 r- v8 v4 K2 a0 j; U
1
/ a- [9 S; l d! m 1 1: k- ^& V! e0 W' y: k4 p+ k
1 2 1. `8 }) D* m: j( R
1 3 3 1
3 M* ]" k- v! P* z) P- i 1 4 6 4 12 a7 q2 A4 f% h, a% L' u
1 5 10 10 5 1 : M& W4 @6 N' ^# V0 H. L2 x& N
2.程序源代码:
9 S* O5 D" E8 Q: m7 F9 imain()7 `: k8 A! s r: J
{int i,j;+ i; k, `# S, E$ O, p7 I9 Q6 x2 H
int a[10][10];7 u8 ^+ v4 k# T) k4 J# T
printf("\n");
" }7 m( V$ ^, c0 ~: kfor(i=0;i<10;i++)
5 _" K: i0 ]) ^( Y {a[0]=1;
- Q( I) X( P& J# |8 f a=1;}
* Y# v* R0 k* c9 ]3 k3 d5 vfor(i=2;i<10;i++)3 Q; _- p; D3 `4 G m: h3 }
for(j=1;j<i;j++)9 A1 Z+ r$ h M& V4 w' q6 i
a[j]=a[i-1][j-1]+a[i-1][j];
$ e2 A+ s# H: h R4 L6 k3 P: ]% yfor(i=0;i<10;i++)! x, V, ~3 M/ t; V. H, ]: c4 D
{for(j=0;j<=i;j++)
. S9 I w* L! n printf("%5d",a[j]);
; Q2 U9 z( Y. [7 `: ^0 w3 g printf("\n");8 ~: J; Y5 d: b6 L) E4 N- l: Q( p
}1 {8 f6 F' Z& s: I0 X# m
}% X, m0 q$ F6 Q* b& r
==============================================================2 G& X* N( p; H8 ?& o S5 ~
【程序62】+ _" S, `- ^5 s2 P' k
题目:学习putpixel画点。
5 ^6 k4 B0 M$ Q O8 Z1 v1.程序分析:
1 P/ T/ ?9 {7 s. K6 ?2.程序源代码:+ b( c- B# m2 x% @
#include "stdio.h"
# h6 m$ S$ x) _( p* o/ h# n3 {#include "graphics.h"
" G1 P9 @( S) Q3 z5 N* {* O7 k5 @main()
" Y4 d( W" p% J{
9 Q! ~/ t5 [5 { pint i,j,driver=VGA,mode=VGAHI;% O+ y" a( m, g. w; ]
initgraph(&driver,&mode,"");7 ?, B" {/ l# @1 _8 L5 c+ B, m
setbkcolor(YELLOW);- r' c6 t# W+ Q' ~4 d0 v& \% b: s
for(i=50;i<=230;i+=20)1 I* [% r9 n! I1 Z/ _
for(j=50;j<=230;j++)
* x# o K5 ?8 g v% X9 i+ _8 s# ^ putpixel(i,j,1);8 M# P, _8 b0 `4 {$ k; b
for(j=50;j<=230;j+=20)
9 u* i) r# A" w+ N' m' d for(i=50;i<=230;i++)1 z8 X. I3 X; W7 T/ {7 [6 K/ k. f
putpixel(i,j,1);6 Z6 a2 s) ?8 `; }. r n5 r
}$ K. i' O% @" X0 l7 q3 [
==============================================================5 J& G A; H: e. N6 c ~
【程序63】, m4 ]3 z1 V; P3 A1 j
题目:画椭圆ellipse 3 V$ U; ^: i i0 q# N3 W
1.程序分析:4 G6 `$ O7 Q) P6 a9 Q, L& x0 G! L |
2.程序源代码:
J3 n* R& ` j V$ T/ E#include "stdio.h"9 V2 Z0 w( T- H: A8 x4 p8 {
#include "graphics.h"
, _4 K6 h# n6 }" \0 [( p& q: A#include "conio.h"
+ _ c; }8 }6 Vmain()! m* b( P) |4 o& M$ E, N
{% e" E% B4 i* E3 \, m. W
int x=360,y=160,driver=VGA,mode=VGAHI;' e0 W1 r9 ?0 m2 D# ^1 t* `# S: F7 c
int num=20,i;
) W6 s5 W" ? X- g7 [int top,bottom;9 E5 i1 T2 K: [& L6 W
initgraph(&driver,&mode,"");: G# c0 a+ {2 z6 O0 v
top=y-30;
9 c4 v+ `4 t+ [( l( Mbottom=y-30;7 @ H, E) x1 E( Q, u; ]6 G
for(i=0;i<num;i++)9 \: v2 M0 p9 @( j& c! J2 [
{! k% O4 o( ^0 y& ~" |. L
ellipse(250,250,0,360,top,bottom);
) w$ E* B$ T ]9 P- ctop-=5;, X# d2 ^7 R( H9 ^8 V! S j. m
bottom+=5;
: X/ L9 C1 K$ H0 ]}, g# c+ M: ~" Q3 Q
getch();5 t( _7 w7 z, ]+ P( ~* f
}
y; N# l {1 A+ A==============================================================
, c9 A# V$ ~, T) H& ]【程序64】
, o4 u) _2 L( b5 U: s9 v题目:利用ellipse and rectangle 画图。
+ r3 B! A7 y R0 V7 }/ _& o7 k1.程序分析:
9 w7 y4 H. K/ [5 [0 z$ g8 H2.程序源代码:
8 w7 v% p* R% P, V) X" {0 S#include "stdio.h"
1 r3 f# G4 N' o1 S" Z#include "graphics.h"
) f7 k( F6 U3 P. F% t9 u7 I#include "conio.h"
- _& g* ]4 K( g1 Q3 o4 n/ L7 xmain()* d4 p/ t5 J8 O7 V @" ]* e9 v; a7 e
{# k% ?/ F/ J ?4 F$ x, b7 G/ U
int driver=VGA,mode=VGAHI;
4 _! j2 j+ _% A$ _, t7 D, eint i,num=15,top=50;
! F6 T8 i! S- X! G: C( o! L. J! Oint left=20,right=50;2 H) }) m$ }9 ]7 a! p/ |8 w
initgraph(&driver,&mode,"");
6 D4 l$ T1 b. W5 p7 W! P: K" Y* Zfor(i=0;i<num;i++)# N. E9 D r; W
{
% K. F. v9 |3 P- Y. K3 ]ellipse(250,250,0,360,right,left);
3 o. n& a% `/ X$ w4 |: ]ellipse(250,250,0,360,20,top);
, o. w- m4 w& \& z+ i; u+ |; Zrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));, U7 h h3 B$ J4 Z
right+=5;- q6 }3 g3 o/ k9 A' z3 v
left+=5;$ H! S+ b a; H# Z4 _- _% l5 _
top+=10;
) g: ]: U$ B! K5 U! v7 f# ^}- ~- s6 [5 \, Y/ K) |
getch();
c0 V! @: @4 @+ ~8 I$ q}
6 A. @2 a0 l4 L$ r0 m% }==============================================================
2 B* \7 q1 } ~" ]2 K+ D0 |% O. a! J【程序65】
% j. \- P5 f3 o7 \题目:一个最优美的图案。 3 m1 Q& I, T) c K* D8 Q$ F
1.程序分析:5 B+ E0 z5 s# v. D, j) q
2.程序源代码:4 N) Z( x9 y; w' E
#include "graphics.h"# y6 D" m" Z; x$ x& v
#include "math.h"
7 b3 `6 J4 N- t) z6 C#include "dos.h": l* [5 Y7 T# y" ?( y
#include "conio.h"; M- a* S: P1 g1 l
#include "stdlib.h"* z$ r9 r3 w: x
#include "stdio.h"5 o* ^ u% n2 ?6 a) h: C
#include "stdarg.h"& E/ Y' h; ^9 F: r5 o# m
#define MAXPTS 15 R% T& N3 ^4 y1 J/ F
#define PI 3.1415926; e$ c* z0 f6 z3 K7 X( t
struct PTS {
' A% y9 }. Y5 e- J3 d! Y/ ?int x,y;( ~/ n4 F) `0 P2 V! D9 l$ O! R
};
) E7 Z" e! @% r8 C* Y# B) q, idouble AspectRatio=0.85;1 f) R& h% V$ Q& [
void LineToDemo(void); G# W# y! H' z4 o e$ O
{7 {/ F# O* R* u/ u
struct viewporttype vp;, z! p+ x7 a J/ H
struct PTS points[MAXPTS];
/ X. G+ \' l1 a& w7 z5 `, ]1 Cint i, j, h, w, xcenter, ycenter;
/ }9 g4 ^- f- n9 tint radius, angle, step;
) B+ @* ?3 S! z5 @8 |double rads;
' q" o& {4 ^$ i9 C) H( A3 Pprintf(" MoveTo / LineTo Demonstration" );* M, B- k: o# S) S% j& N
getviewsettings( &vp );
" m; \# _9 D0 X6 ]" M, ?" bh = vp.bottom - vp.top;1 z7 l1 H: F; h! T, `% L
w = vp.right - vp.left;
$ w7 B m3 t2 e9 o; qxcenter = w / 2; /* Determine the center of circle */
6 P1 y* B5 Y0 C% }& `ycenter = h / 2;! Z9 a+ }+ b9 R4 g. t7 n" X/ n3 `
radius = (h - 30) / (AspectRatio * 2);
" S6 p, y5 X9 B% pstep = 360 / MAXPTS; /* Determine # of increments */
9 y: J3 c2 _2 W$ Eangle = 0; /* Begin at zero degrees */
t, `3 j; r( U) H/ Y/ X% _for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */# C6 s [" i2 g D! _% P, N6 S; [: e
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
! C6 g" B0 a' l- P1 W2 m( `/ J" B& lpoints.x = xcenter + (int)( cos(rads) * radius );
: [2 I6 ^$ ~. P- y( Epoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );- A, E' }; W! x7 I
angle += step; /* Move to next increment */0 ]$ ]5 ]7 t B& Q5 F, C
}
% A8 V8 F$ }3 v3 K0 N4 n" Jcircle( xcenter, ycenter, radius ); /* Draw bounding circle */" l; b9 @ j4 s, f1 U
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
( t) y7 q' ~, Y; j( |for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
, p4 S) K: {5 d3 b- t; e$ Kmoveto(points.x, points.y); /* Move to beginning of cord */
# g0 u6 d# Z( c- Zlineto(points[j].x, points[j].y); /* Draw the cord */9 Y2 n+ x5 O _0 W" r
} } }; Y$ a9 k+ ]1 R2 E1 m6 {4 Q* W
main()3 k$ C) I, u0 M$ c. l: W
{int driver,mode;8 l% X! r: h. m) M" Z* @9 k
driver=CGA;mode=CGAC0;
8 E" S4 e3 N: P& n0 N, Finitgraph(&driver,&mode,"");5 H4 D! N, x8 [- y
setcolor(3);
* s+ t/ P0 d! L! ~- V; Zsetbkcolor(GREEN);
9 W7 X( O$ E/ K% oLineToDemo();}
8 j/ i) B+ Z# e# X! t==============================================================7 v1 ~' r' ^' e7 n
【程序66】5 L: e) M8 x J
题目:输入3个数a,b,c,按大小顺序输出。 & K2 m7 q# q$ G( O
1.程序分析:利用指针方法。
7 d0 E$ M2 G/ N' k; S8 e q2.程序源代码:
+ k3 ?8 y2 n9 e$ V5 E' s- r/*pointer*/4 m! Z$ [& U5 {( i
main()7 g) n t7 @; ]3 h# T+ f1 |
{! _, o! z" L/ I* e
int n1,n2,n3;! K( j1 `/ Y/ ~; B5 m
int *pointer1,*pointer2,*pointer3;
% a6 z. X* A/ K3 b' Hprintf("please input 3 number:n1,n2,n3:");
, A: U, m% M9 ?- u; xscanf("%d,%d,%d",&n1,&n2,&n3);
, A% f5 P! J6 e7 rpointer1=&n1;1 z' V' H6 A% b/ P1 r
pointer2=&n2;
+ M- }7 ~- W3 h v; ] O4 Apointer3=&n3;
; w1 k. @; c! M: D% ^) Pif(n1>n2) swap(pointer1,pointer2);; A; |9 _8 D% c f8 `0 G# r$ C9 u0 @
if(n1>n3) swap(pointer1,pointer3);
% D: W* r/ }! X4 }0 `3 V) }/ pif(n2>n3) swap(pointer2,pointer3);; Q; x: d- o* m! k! l; u
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
0 h1 L! ?* {* S" b}
6 L; }8 Y0 R# e' i J8 o% Z& wswap(p1,p2)
0 v7 L* {7 p, xint *p1,*p2;& ? ]: _+ z4 D' j' Y
{int p;
4 d8 o! @+ w" i) o/ h& `* dp=*p1;*p1=*p2;*p2=p;6 r: F* D1 G4 U3 a0 k/ C, R7 C
}
( d- J. I+ y3 |7 D5 |2 _9 g0 w4 v2 ~==============================================================$ v; L9 {: [& T& S
【程序67】
6 r0 |( l! I ]+ t9 r题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
; E, M6 d3 O, ^+ G1.程序分析:谭浩强的书中答案有问题。
3 T* A$ |& O1 K! }2.程序源代码:2 t. c U# I! g5 e0 N5 n! d h
main()
/ }2 R" [2 R" V% d{
( o7 |- T4 B& A/ bint number[10];
% e, e5 [( R) C* J0 ^- J9 [& Ainput(number);: w! X+ E* @6 K) D
max_min(number);& i+ R# [3 V& X' P* r, o4 x, ]
output(number);: z' g" p' J; O" e
}, O) Y& f) f- W! N" u8 L% k
input(number)
/ M) b" b) Q/ Q- R2 w5 l6 ~4 [' ]int number[10];
! k% B8 w' i/ w3 T1 h) [1 r{int i;
5 P1 T3 k. _+ X; H6 N0 jfor(i=0;i<9;i++)2 _5 R$ ?- w% ]* t1 U
scanf("%d,",&number);
. ]4 c& L4 @# b! T- f scanf("%d",&number[9]);
& k- i/ w9 d. ]# a! O% i. K2 R}
4 N. X9 y$ v( J2 {max_min(array)
/ P$ F+ T& h* K: O0 E; oint array[10];& I2 f, a- L2 Y( j" l4 L5 c
{int *max,*min,k,l;
0 Y, y* b g! ~5 H, s. j# B% I- lint *p,*arr_end;
3 m! Z! H% G' u( e/ w7 g' [& farr_end=array+10;, A0 Y1 H2 W5 x. {( g4 l! i
max=min=array;! M6 F- e7 v7 g' m( J% ?; N
for(p=array+1;p<arr_end;p++)$ N& g. ^3 C: `$ j4 }
if(*p>*max) max=p;/ `- U' [+ q0 D2 b
else if(*p<*min) min=p;
4 {2 P+ h6 y; r/ ~! g k=*max; X5 _4 i3 Y8 I q) W( w
l=*min;
G) N. I, u8 h0 b *p=array[0];array[0]=l;l=*p;
/ M+ s% z3 U! @/ ?. o4 f9 A *p=array[9];array[9]=k;k=*p;0 H/ C# e, e) k% K+ r
return;7 U/ d- Q4 q6 o/ x4 a |8 |
}8 v0 g$ g+ K1 ]9 s1 k3 Z# O" d
output(array)
$ H$ f: n3 ^) S. eint array[10];
2 q% i! k& s4 m. s' K& }{ int *p;- ^$ a; y! X5 u# r& R4 F( Y& m
for(p=array;p<array+9;p++) x8 x& l6 d5 V- ?
printf("%d,",*p);
4 j& r3 _( \4 D" w# {printf("%d\n",array[9]);
/ D3 s$ v4 b1 z1 A1 ^}
; ?6 X% U! i$ F6 ^ ]0 u: ~==============================================================
$ S4 k( q1 K- R6 B9 W6 ^3 Z( ^【程序68】
" Z5 m1 D# u3 [题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
% E9 @; S: w6 U5 Q1.程序分析:
% H: r2 p$ z8 s1 j5 n2.程序源代码:
0 v" G- e' f+ C6 J7 X% n ]7 h2 s: c! \main()
: y5 A1 o0 N4 l{
0 {( Q9 C4 j1 @. r6 `. f6 N$ y7 \: Rint number[20],n,m,i;: t6 `* g% z. a9 X* O1 n$ L
printf("the total numbers is:");5 |8 }' F: O' N d
scanf("%d",&n);) G3 T% M$ p" n P0 G
printf("back m:");
- _3 {8 K- i7 ?9 _+ Qscanf("%d",&m);7 j* k) X5 R. l# W, k
for(i=0;i<n-1;i++)( ^: x3 E, y& q2 N. n) J( q
scanf("%d,",&number);
( B, N I. n3 @ z8 cscanf("%d",&number[n-1]);, u* o5 J1 H" t6 P3 P
move(number,n,m);* b' G5 Q6 h! D
for(i=0;i<n-1;i++)
/ @2 v# c9 i5 g& v0 W( y+ F9 H printf("%d,",number);
6 |, s# w( Q6 j2 A" {printf("%d",number[n-1]);
' Q Z/ g! B# F! y* e}
* ^1 X4 o6 L+ P5 T' F y0 Dmove(array,n,m)
5 t7 k0 U2 g, `int n,m,array[20];& `) H7 F1 ^0 n* x" V6 c
{
: K8 p% ]* a7 [6 p4 A$ D$ Gint *p,array_end;
; b$ U: X$ y4 _# a" i4 r, I; s7 Oarray_end=*(array+n-1);7 X e& o- Y/ p
for(p=array+n-1;p>array;p--)7 ^ T% K; [2 A' I& q0 b! H5 X
*p=*(p-1);
+ F. ], k2 V+ H/ W1 K2 {" o2 S# g& d *array=array_end;' g& D3 S7 H2 v; B
m--;$ A2 T! d A+ F+ h( `4 m
if(m>0) move(array,n,m);+ q1 j \. S. q
}
, e# X) r! K$ o, j/ C b==============================================================( Z: W2 a* V C$ S) Z$ e1 H7 j" k
【程序69】3 X/ r' Y/ t) F
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出
/ t0 L* S0 r0 d' w% i: z; M: Y8 ]% X& ? 圈子,问最后留下的是原来第几号的那位。# x9 Q+ C/ B6 C- k! X
1. 程序分析:
7 b3 w: [9 b4 G2 K; d2.程序源代码:4 U! `7 b, @9 n( C3 d
#define nmax 50/ O6 v5 x- `' i! `1 }, X' [5 C
main()% m; S/ s0 X: _/ S/ a5 z
{
; @, l7 b, ?$ N5 \int i,k,m,n,num[nmax],*p; B' S2 ^8 F9 L5 T/ ~
printf("please input the total of numbers:");0 g T& i |, K3 e0 {4 G
scanf("%d",&n);
/ D) r1 I8 ^2 V) d, Up=num;
* M' i2 M7 p7 n$ u+ L+ e7 @) Z+ Ffor(i=0;i<n;i++)6 U0 Z4 Z' M0 e5 I( V7 b
*(p+i)=i+1;
$ p8 j) x9 |: R5 f: ^8 B1 a. B i=0;
; K' \, J% m: T% V5 p k=0;: V$ F7 ^9 d$ I. d: \1 ~
m=0;
- `" C& z& d( W1 f! j while(m<n-1)8 K$ |( V- H. U6 Y& g3 t
{
/ a( C$ W& f' `, i5 R r if(*(p+i)!=0) k++;
1 l0 h) ?& C. J# c L5 K if(k==3)
% z1 e% K6 O' f { *(p+i)=0;0 f! ?+ m+ D" z
k=0;; I* p$ y" G2 C' f1 h c
m++;" A7 |# i- e: |) H: }) R
}
. v; o0 J8 {0 j+ x- di++;& V& l$ d9 d3 t+ c( t! q9 `1 n" g k
if(i==n) i=0;$ _5 b! `' [( Q+ f2 M
}
# V3 A4 x5 z; E% C+ \6 o9 K; ~8 Ewhile(*p==0) p++;+ g$ \( b: c H& D2 A% [) \
printf("%d is left\n",*p);
. r' \& k) ]( ^1 k o}1 {+ V+ Q; V2 J& m, G* ?2 _
==============================================================
9 K1 _9 s& |5 j7 [【程序70】
8 _) _9 [. X2 a' F# C题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
( j( S s S! K2 y9 C( J1.程序分析:# p) A2 W+ w* O$ Q
2.程序源代码:4 F' e5 ]1 C" ^9 g7 G& _
main()9 Y0 F2 x6 k, m3 f( }. @
{
0 M, A; |4 r2 X) w& Gint len;% ` M3 b7 ?& ]4 ^; } S( \
char *str[20];% K$ X, Z/ i s( t
printf("please input a string:\n");
7 P: {6 J+ z) h' j% ]% w5 mscanf("%s",str);' Q" N; f- ^9 `% ] w) K
len=length(str);3 a6 a( @2 P- `7 M" U6 w
printf("the string has %d characters.",len);! D% ^% ~ \8 L. T" K. T
}
% n% B, w9 X7 E! Y; j/ vlength(p)7 q& l/ @" o( I4 A" d6 ~
char *p;% u4 u3 c* ]. g) Q* v! `, L+ E; g
{
' x6 i; }4 \' p& F+ cint n;
& f, N( E- U5 A' Jn=0;/ ~2 x- m3 p6 ~3 c- u
while(*p!='\0')3 x$ ]7 ` d6 V/ O% F/ z, C. F" f- x
{: N: T5 F9 i: F! a2 a
n++;
, o% F0 W0 h* P4 s* o1 N7 v p++;
E0 o& Z3 b: F/ D" t}
% t3 U5 {( c9 D0 areturn n;
9 j) [* U1 j r L( t" s6 _% J6 \} |