|
【程序61】
7 {+ ^: O. \' z: u+ t题目:打印出杨辉三角形(要求打印出10行如下图) " ^7 P1 {& j) s, R2 @3 [* T, ]" H/ u7 }& W
1.程序分析:9 D3 E( a1 w* i/ L7 ?
1
' C( T6 o- x& q* d 1 1/ P8 {' C( r0 M- n h- K+ n' u* y
1 2 1
. R( |) G7 s) e3 G0 M5 p3 v" |. q) y |: i 1 3 3 1. e! T! p9 ?+ z& u4 b4 K
1 4 6 4 14 y" S6 F" ?1 i8 d8 L1 ?# U
1 5 10 10 5 1 ; o: \+ d; m6 o5 w. Z4 \6 I
2.程序源代码:
" n/ I9 B h! Q8 J. mmain() m2 Q0 Z4 |! y' k8 k( E( k
{int i,j;! H- h5 A5 H$ f, M* Q# c
int a[10][10];6 O+ E. L/ @6 C+ S* z
printf("\n");
% w$ f- O9 n2 |for(i=0;i<10;i++)
9 ?$ l8 P! H% g+ e4 e4 \% u4 K* L8 { {a[0]=1;, M/ n& X1 T' `( [0 [
a=1;}( ]- X5 ?+ E: f) a& j, l6 Q
for(i=2;i<10;i++)! Q) n- ^$ D! W" \
for(j=1;j<i;j++)# l! x; j/ V: T6 T, k+ U
a[j]=a[i-1][j-1]+a[i-1][j];
) g% d6 @" W& m$ [1 X2 Ifor(i=0;i<10;i++): p( v( d2 A! o" W
{for(j=0;j<=i;j++)) W+ V& }$ |2 C$ P0 n! V) Z
printf("%5d",a[j]);, u( M6 P) d; G
printf("\n");# S7 x2 N w9 _7 I4 j& _# L% N
}
1 U4 [% u4 P$ Z2 W. n% d3 f* i}
6 w( c, G3 N# _- T; S7 f==============================================================$ v$ \- f+ ^# v1 a
【程序62】" \! f2 P. h P! z. h9 ~9 j& x7 t
题目:学习putpixel画点。/ J p, H, p+ X, Q; b
1.程序分析:
0 ~' C$ c& }9 Y- }8 O) M: d2.程序源代码:
4 I% b; l8 F8 F8 M/ P: q#include "stdio.h"; r+ v' s4 H w
#include "graphics.h"
) Z1 n; b* t; g' B1 o) G. Lmain()+ P5 Z7 V/ a. w
{0 Z, y6 C/ ] y" C, n5 h+ M
int i,j,driver=VGA,mode=VGAHI;
; B! P4 K) U' ]6 Y( E; k; e0 ainitgraph(&driver,&mode,"");
2 K# T0 F& e4 c( C! M9 csetbkcolor(YELLOW);
$ C1 e1 n9 E. W. Ifor(i=50;i<=230;i+=20)$ N% s4 ^9 J# \6 r3 y
for(j=50;j<=230;j++)
# c3 H; @. p. @% P8 L- z }! l6 k putpixel(i,j,1);7 [( z: @% [1 n# G1 \$ n" g/ j
for(j=50;j<=230;j+=20)
! E& v7 P+ d& ^6 P for(i=50;i<=230;i++)$ l9 T; Q# h- b0 h. [ T \- g
putpixel(i,j,1);2 x5 A4 |" A# v. N: Y" Q9 q7 A8 }
}& [0 m4 Q" O3 j6 ^1 i/ ]5 g
==============================================================
) J; o9 ^2 a6 O【程序63】
0 ?# B) `- ^8 }+ C题目:画椭圆ellipse
+ \8 j# n6 _- T! t# e- e1.程序分析:
! o, D4 p- c% }5 t( w- y2.程序源代码:( a& B9 k% r5 Z0 i, Q
#include "stdio.h"
: H) E& r5 Z+ K8 [9 G+ F* ~- |#include "graphics.h"
4 l" [. ]8 n, a3 a* C6 r8 n#include "conio.h"; t) E" l$ I; F9 j6 O# Z f& d
main()
5 o4 R6 n9 D- L; }$ b, w5 Z% `{& H' @8 [ O- x
int x=360,y=160,driver=VGA,mode=VGAHI;
- b7 O- L* q. L6 K$ Y; _int num=20,i;7 `5 F% W' e. u/ ~
int top,bottom;
( T+ i; N3 I# N8 c0 hinitgraph(&driver,&mode,"");
! v7 e* [4 \( z$ p$ ]9 J* Otop=y-30;
9 X5 t, L- P( N8 V' G/ |% Abottom=y-30;
# z- {. Z9 U G% {" ofor(i=0;i<num;i++)
- _/ \% _' _7 i4 d: I: I) S f{( G" E9 H4 ~: T1 a$ S
ellipse(250,250,0,360,top,bottom);
: o, _8 g0 r+ q+ i% W) O) _0 Ftop-=5;& _% a1 _2 D; {
bottom+=5;
' |! ?' T9 [' f% T' X1 K# v$ {} j$ M# |# W) w1 h+ p% G# m
getch();
0 M) R. F2 s* H% e8 ~" ?& G}
% @; a' w$ h$ s2 s==============================================================
( j# h4 w( V3 [8 |8 j【程序64】
8 Z& K3 z5 N$ @: C2 E9 Q% v1 n6 j题目:利用ellipse and rectangle 画图。7 ^/ @& x- a2 V6 z; L9 H1 o
1.程序分析:
* J: b. [, t5 t- q' y2.程序源代码:
& V# g% h" y e: I; E6 \#include "stdio.h"5 q7 w0 n7 i8 D: ^" c
#include "graphics.h"
) X2 s+ J* Y7 @# _* g2 @# e, T#include "conio.h"
+ S b3 e, _5 O0 E8 a& X& b& g) x1 fmain()- V3 I+ a2 `% m3 X
{" j, A6 |3 t1 p1 }" x- |- f; W+ p
int driver=VGA,mode=VGAHI;
1 b, ^) A& E1 T9 x+ Z: nint i,num=15,top=50;+ _2 V' t8 X, }$ j6 w+ J, Z+ p s& V$ d
int left=20,right=50;
/ w9 A" @/ e" i$ @) Sinitgraph(&driver,&mode,"");, p! Q4 n7 J( j: Y7 y
for(i=0;i<num;i++)
' F: P5 T8 K2 w{ c5 ?& x9 l |/ T2 Q1 `
ellipse(250,250,0,360,right,left);
; y; @ ~& o' Q, U1 m- |6 Kellipse(250,250,0,360,20,top);
1 m; V* g! W' P) T& s1 Nrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
! p. q# w G/ Xright+=5;8 g5 g3 T* g2 M1 H0 Q+ N* U
left+=5;
5 h. J( N, u! [top+=10;5 g8 j3 ]( {4 t* _9 j; ?9 j0 q3 ]
}8 `5 t3 `( A8 O" Y
getch();
' R3 M( X7 m1 O/ O9 W- i}
* v4 L2 u+ q, \5 P% Y==============================================================
8 a2 H# d2 [4 q8 q" x7 s【程序65】9 n/ }9 f j! x, }7 m
题目:一个最优美的图案。 ( \1 _1 V2 t/ b' k2 O5 |
1.程序分析:
4 V2 y. \0 B' F! V2.程序源代码:. R* t# C6 W; ] k
#include "graphics.h"- f& u# I7 M, A5 a: \( H$ F
#include "math.h"# w0 W2 m/ Y/ u$ \: I
#include "dos.h"
8 e9 z& ]# Y( r8 k# t* s' e1 W#include "conio.h"; @3 `+ G; X: J/ G4 M* K
#include "stdlib.h"
" t! O1 y9 I5 Y* `2 T. ^#include "stdio.h"
/ J5 d0 i" @ W D. e* g' J6 G; Q#include "stdarg.h": j. [" X" h K d4 i/ X4 x
#define MAXPTS 15
; q2 o! N+ |0 @6 S6 j" C4 {#define PI 3.1415926
5 E3 ?6 e p, k% B% ]8 p5 s# @struct PTS {9 u+ n% D1 j; P! C
int x,y;
; |/ F6 j/ y) P+ d};7 @3 [8 c1 }$ L2 h7 P
double AspectRatio=0.85;
4 U4 o, k- ?! g5 s! Nvoid LineToDemo(void)" h% r2 O) i& D
{
) v) ~( U0 P/ B' i' {8 wstruct viewporttype vp; j" T/ u0 ^) P8 k
struct PTS points[MAXPTS];
7 |3 a8 N* s$ [1 b& P+ q _4 @int i, j, h, w, xcenter, ycenter;
) L& [0 A' f# ^: d1 Q$ k- @int radius, angle, step;5 c- X) M+ M5 ~( k* k2 q1 Q
double rads;
+ e0 A6 Q6 q( E$ X4 gprintf(" MoveTo / LineTo Demonstration" );
1 w1 h M8 I8 R* o/ U1 m Cgetviewsettings( &vp );# Y/ {8 |7 {- q6 t+ \6 N
h = vp.bottom - vp.top;
$ B4 W: X: M5 G) X* I3 dw = vp.right - vp.left;- `$ |. i! ?; S
xcenter = w / 2; /* Determine the center of circle *// g; ?, G& v* |
ycenter = h / 2;6 Q/ r* B' b8 O) }
radius = (h - 30) / (AspectRatio * 2);
, y- V4 }( d7 F1 \step = 360 / MAXPTS; /* Determine # of increments */5 z! Y8 A! G' y- s' P9 i' I0 A2 N
angle = 0; /* Begin at zero degrees */! r- |0 M$ @. j' C
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */4 A1 G& K5 _. W7 U
rads = (double)angle * PI / 180.0; /* Convert angle to radians */4 ]' Q4 ^" a: j! {# w, N* P
points.x = xcenter + (int)( cos(rads) * radius );
% {5 I9 X7 ]% s" O7 x! a5 P4 k$ Kpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
# `; ?+ f; g* l: f& c' B' tangle += step; /* Move to next increment */6 u/ w* Z8 H# K$ h
}6 p8 R4 ^9 \- Y
circle( xcenter, ycenter, radius ); /* Draw bounding circle */) }7 b1 n/ j! W- e. \
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */5 y" F( Z" U/ ~: a: N
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
% ?9 ?8 y0 F8 imoveto(points.x, points.y); /* Move to beginning of cord */
7 k( f) }. h; Jlineto(points[j].x, points[j].y); /* Draw the cord */4 V# d3 p8 c# ^8 ]- n k' G, |8 t
} } }
. H* T- U8 w/ Y0 ? wmain()6 V9 P0 [( i" T4 m* p D
{int driver,mode;
) B4 C9 H# H( `# g7 xdriver=CGA;mode=CGAC0;
4 c+ {5 v) v( winitgraph(&driver,&mode,"");$ H8 k K$ j/ ]# d$ b( [ y
setcolor(3);+ l4 |8 ^ k$ }- ~( }* @. P0 ~
setbkcolor(GREEN);
% d" i, q' a) h8 O9 G% h! X1 vLineToDemo();}4 }- w/ R3 G- l: k6 I! @
==============================================================7 H3 D" x, \, w" k5 C
【程序66】" u, W0 {: L) z
题目:输入3个数a,b,c,按大小顺序输出。
4 p# O! O; n! H' I/ p5 X2 x- ]1.程序分析:利用指针方法。1 V! S- ]" g$ D% j& `) `6 I( h; ~
2.程序源代码:
- |) `7 b1 T# A: \" k$ e1 P- x/*pointer*/
# p( c+ {' `$ R. R( M. J6 hmain()
1 h5 r. s1 t* S- U{; R1 s# ?& v$ n. d. G8 m
int n1,n2,n3;0 w1 R& Q4 v+ Z! G5 A6 q
int *pointer1,*pointer2,*pointer3;
; M6 y' K# z' M6 j- @printf("please input 3 number:n1,n2,n3:");
- j( H2 y T pscanf("%d,%d,%d",&n1,&n2,&n3);, _% ~1 f; Q% T0 G2 V
pointer1=&n1;& p; _" c' w8 b4 c# A* ^+ _
pointer2=&n2;' ]- `$ K+ `/ |. e* N
pointer3=&n3;
" j* |' D$ |! y8 k1 [$ tif(n1>n2) swap(pointer1,pointer2);) G0 Y# d# B# [: S& P8 w% f
if(n1>n3) swap(pointer1,pointer3);# h H6 y" x6 T4 H) \
if(n2>n3) swap(pointer2,pointer3);
! H# h% O2 E0 W1 s" r0 p& t, Hprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);+ `% t Q3 K" N/ W9 E: f9 Z% ^2 C
}
. p0 A; k5 w2 M% A- Fswap(p1,p2)! ?3 D+ ?$ m4 b: ]: C0 `) P
int *p1,*p2;
0 @- ?) K* v1 q% J3 H{int p;
X/ c# y! S9 Z7 ~5 wp=*p1;*p1=*p2;*p2=p;
$ O8 z7 z5 U$ ]7 u}( Q+ T% u+ S( @. s
==============================================================2 ^ U0 ~8 m) b1 O/ r+ a" r* v$ I
【程序67】
: u* W6 b7 V! S题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。7 F* P' a) `/ n0 V3 |% w: H- P
1.程序分析:谭浩强的书中答案有问题。
' A N( [3 N2 v5 U/ z2.程序源代码:
5 G# W, \3 R8 Amain()2 z5 D2 o; w+ E
{
4 _ ]/ b0 u4 D. I' V; Cint number[10];
& j, ^! |% ^# U h5 einput(number);8 @5 N% E& O0 h& v
max_min(number);/ q \! B- p" p c
output(number);) O( U1 E+ F0 `2 q" O* m
}$ g6 r2 Z& B- |+ g& q
input(number)) b& |- T. @, b9 s2 x( r+ e
int number[10];$ X& B4 E( V) G$ {% H1 o
{int i;8 O T+ ^( Q5 m! ^9 w l& _. _
for(i=0;i<9;i++)
/ s) T6 G1 f. E" Z2 g6 `3 }/ W scanf("%d,",&number);. s. q$ ?4 E( l3 ]6 f+ d8 y
scanf("%d",&number[9]);, T& _( S5 j* J& q/ I
}6 h% c! Y7 {% ?' G, c
max_min(array)* a3 Y' Z, B9 V0 `) V4 b3 J1 e/ O; Z
int array[10];. c; m. v" _- m0 h
{int *max,*min,k,l;7 ~5 l% a7 B* b2 P" s
int *p,*arr_end; B& b0 L% I" U9 g3 E0 k& F
arr_end=array+10;
: c& W# e2 Q0 j$ y$ y- `) A ?7 pmax=min=array;, Q7 v" s3 i2 c- n( Z
for(p=array+1;p<arr_end;p++)2 N4 P- U) B$ S3 S2 w: }, r- _% H
if(*p>*max) max=p;
: m# R3 p4 Z- E/ \ else if(*p<*min) min=p;) b6 a( ^+ G3 |: W
k=*max;( o9 n" j9 o2 l) L) O
l=*min;
6 x* _- B3 M9 A7 O# H$ U) y; x *p=array[0];array[0]=l;l=*p;
# X- t T' f' \; t/ X4 G. J *p=array[9];array[9]=k;k=*p;% `( N% ]' N( F0 T1 }' F3 B
return;5 x. ]7 H6 R, ~4 P# c7 y" ~" R: [
}
0 j. Z, r0 G1 \6 _* U( Loutput(array)% C4 F8 E! ?$ V0 V, m
int array[10];
; q' B) n! F% f Y: c4 w9 C{ int *p;
0 x8 Z& D/ u; z2 A! n% S5 E3 r2 `for(p=array;p<array+9;p++)+ h0 H1 @# _7 P+ F
printf("%d,",*p);
^% \( }' }& vprintf("%d\n",array[9]);
$ _* h# T! d* D0 J4 I0 C9 }1 W}
' N3 F9 e5 S$ ~) A5 t. C==============================================================
* S3 h' n2 V7 S9 _) A( f0 g- X【程序68】/ j2 H2 e/ v; W" A( `7 u* |
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
! B0 C5 }: K9 }* D) J1 {- ?1.程序分析:
" k6 O; I+ L: ^2.程序源代码: M6 J4 ?& R' y/ J
main()
, z' a; z! a/ Q6 r" B{% a {6 N/ ]% e* o
int number[20],n,m,i;
9 T' H5 \. M- e* \5 S* P5 bprintf("the total numbers is:");$ u1 I- W+ q; M
scanf("%d",&n);
- f/ g) g0 [) s3 ]printf("back m:");2 @' |7 _* D! D4 }' M6 e
scanf("%d",&m);/ B1 {$ Q1 z4 ^4 Z+ q' v' S
for(i=0;i<n-1;i++)
* @, n! f' H) q S+ p8 ]; y scanf("%d,",&number);
! C9 B. a* W, Q: S" ^scanf("%d",&number[n-1]); i0 q6 j) z* v2 B* A
move(number,n,m);
! q$ F8 |/ N* E7 Wfor(i=0;i<n-1;i++)
$ [& f8 h. j1 a) \. o) h printf("%d,",number);
8 I* i9 z: a9 X4 G; o4 gprintf("%d",number[n-1]);( U3 ^' K2 I4 Z
}, S4 P7 u) U: T
move(array,n,m)
; W1 F. Y; k) B! V; y* {int n,m,array[20];4 U0 s& j4 m# L: w! v# X: s
{0 S$ U8 y- g; t. v* c
int *p,array_end;
- @5 W. r; ]( Uarray_end=*(array+n-1);7 v7 ?# |3 d* [4 E
for(p=array+n-1;p>array;p--)
4 y/ h3 v3 c; v9 T% X1 t) m$ Y4 n *p=*(p-1);& }; X+ D/ M7 B5 n" i
*array=array_end;8 J7 p+ R0 G8 @# f3 j
m--;# A2 {* C0 |" o. |% c
if(m>0) move(array,n,m);
6 E5 V. w! [: v}
$ i( C* m* g5 ?: j6 P2 F==============================================================) k) R9 `$ g d# i/ M
【程序69】. f+ k/ g9 {% x9 V- q- q
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出! g) U. R. F& y, ]1 U: g1 c' y+ e
圈子,问最后留下的是原来第几号的那位。+ |* Y) M8 m6 B/ D& Y% `9 \# n
1. 程序分析:. k6 t% B7 k; e1 S4 V
2.程序源代码:
- B7 s& s2 Q- ]" p- J T# _#define nmax 50& m$ U0 L8 m2 Z" V- A
main()
' t$ K: v; k& J R{
3 B7 b* g3 C& M4 ^2 K4 L, H) Lint i,k,m,n,num[nmax],*p;
% Y1 ~8 S5 K; z6 K7 Eprintf("please input the total of numbers:");
/ S2 u) v) {6 s9 u9 r& t xscanf("%d",&n);8 X& X: N/ F! h0 K1 r
p=num;
. `' I) c' {0 Y% gfor(i=0;i<n;i++)
4 A! j% ?" A3 H1 S7 t& b *(p+i)=i+1;
* _+ Z' t( _! ^ `+ U* U. c0 m i=0;- A" i! Z& k9 j5 c( ~
k=0;7 E, Z& Y; ~8 h. N/ N9 ^
m=0;
p5 ~8 F: P6 W& ~2 K& X$ Q( o while(m<n-1)
- v) {$ L) [" I2 D6 X' r$ G {
8 @* P) N% V* Q9 T# r if(*(p+i)!=0) k++;
- y8 l! G6 `3 \/ }7 T if(k==3)
; Y) V5 G4 l: }; A { *(p+i)=0;
6 ?' y* z* d" s k=0;
# u/ V( Y4 E+ n+ W$ a0 q+ b m++;
3 u- f8 h* T4 r3 W; V! z }" v. L3 q* x' Z w5 p
i++;; l s Y9 p- i# N3 |8 _ \* Q
if(i==n) i=0;
9 C7 b4 T% q7 X1 H! C}
. s4 `; J: `2 Jwhile(*p==0) p++;
9 P* e; o8 `1 d/ W/ b D" B8 ?printf("%d is left\n",*p);
5 N N, B2 F1 l& o# h0 Z}
- P- n: i- h% T* V==============================================================; h# E7 e1 [) V
【程序70】
, X$ I& y% A2 y) [0 X0 x9 S题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
; P4 v$ n6 `' n: K$ p1.程序分析:
7 B8 s X& _; P l4 R2.程序源代码:
& k9 S7 X0 T1 Q: K* cmain()0 }+ p, @" D/ k. H9 x# U) P1 q7 p* G
{
7 q- |+ k) }7 {' Xint len;
5 z. E* z# y+ D1 h' o9 |6 _char *str[20];! y c' t* _1 t: S$ f' x ~6 ~' u
printf("please input a string:\n");
5 H; n# p! q+ P9 D3 D( Gscanf("%s",str);0 b! U# [3 a, _' _8 j
len=length(str);6 ?# c: k* {4 w" w3 [# g
printf("the string has %d characters.",len);" h5 ?) S0 X" ?7 U5 \4 U! e
}8 q$ T/ f/ ~: G+ T- i
length(p)
, O" J0 l0 u0 _, Y. \1 P& W( Vchar *p;
6 J0 d0 C% j6 h{
4 ?$ t5 c& z& Q6 W6 cint n;+ N" w, J7 ~% G: H0 O7 n
n=0;
5 ]9 y4 O6 r/ x0 Q( B. E Qwhile(*p!='\0') k# m9 q+ b+ z6 w% x) c! a
{
8 E* \( i5 O* e6 R3 ~/ K n++;
* e! Y( p1 K f0 E$ Z p++;
$ j. x$ p+ u4 q6 @* X0 I5 s}
_% u) P3 j# zreturn n;
4 D7 N& g- a* J* T' B9 L1 E& V} |