|
【程序61】
$ m; m7 Q/ H5 K" S3 U" P题目:打印出杨辉三角形(要求打印出10行如下图)
/ v, b" m+ _4 D1 M1.程序分析:
8 j9 c" t, u) [/ g; r+ Q' T 1
7 [; k& B5 V) a; N/ W$ t 1 1& J: I( Y, e8 T \7 S4 Q
1 2 1$ \% d7 \7 c- C! b
1 3 3 1
9 l3 G: N. Z2 E5 Z# E7 `7 K* w( _ 1 4 6 4 1
) f+ G. }( C, b$ |# V/ H 1 5 10 10 5 1 2 U( n" ]9 e% V2 K
2.程序源代码:' i5 j6 k' b. c% Q7 l8 b* `+ q
main()
. d$ ?; l( P0 ^# r{int i,j;, j% u3 q" t: K+ z' T5 R
int a[10][10];
- J" i/ Y1 J% D/ U7 aprintf("\n");
# P9 q, ]% Q# r6 O; T& Efor(i=0;i<10;i++)- Y( D% N* k" S( [! w. ]
{a[0]=1;
7 Q' J9 ]( W. b( b a=1;}
. w- ~3 V3 S" ~for(i=2;i<10;i++)6 b; d6 v" `/ {9 A! h$ s
for(j=1;j<i;j++)
$ D% ?3 H3 m c+ `' Y: D a[j]=a[i-1][j-1]+a[i-1][j];
; g. ?- Q0 T) T% Nfor(i=0;i<10;i++)5 ^) Q/ g1 u( _% g' Q; G }- T# h
{for(j=0;j<=i;j++)( T! G9 k8 v" o9 h; ?: F7 l
printf("%5d",a[j]);
' [, H0 U+ @9 B6 z& n printf("\n");- g# w. S% u6 H/ C
}
; {: A# g; V: i/ b1 [1 H1 f2 w}; t! e6 R) c; Z4 ?
==============================================================6 y8 g" l6 ^% ]) J
【程序62】
) A! Z: T8 O+ w题目:学习putpixel画点。
! \7 ], i R/ L$ o9 W" F5 d1.程序分析:
9 u6 E/ l N- }# S4 T1 f2.程序源代码:
- g, p Q4 v9 R% U6 j0 C5 M#include "stdio.h"
! I: f: r0 c8 K#include "graphics.h"
9 z/ O3 _( {; j( E& k: T- i) Ymain()- X& A" B4 Z7 ^: ^
{
M2 ?" K" @' q5 ~$ n1 bint i,j,driver=VGA,mode=VGAHI;/ t9 t( ?( X5 M% K! y b
initgraph(&driver,&mode,"");2 }7 t. ~8 Q9 p6 I% I1 d, l: d
setbkcolor(YELLOW);
, o5 {7 F( F/ D+ _3 N) G' {/ Ofor(i=50;i<=230;i+=20)$ s" k! r( v' [7 e$ |8 P
for(j=50;j<=230;j++), k8 R2 K) \+ t3 |
putpixel(i,j,1);9 {, b w+ F% Y/ ^9 A4 E7 o' L* b
for(j=50;j<=230;j+=20)
2 B4 ^- @8 H/ N/ r, H4 c for(i=50;i<=230;i++). G: ~& J7 c4 P: b3 B
putpixel(i,j,1);: |5 f# I- O4 V* d, x, d1 K4 R
}* L6 k E# l' m4 I
==============================================================9 ~% G* D# S% U. w, w6 u- T# C
【程序63】8 ^! o3 m" w! d! l5 M
题目:画椭圆ellipse
& K; S% Q% S* ]1 ] @& Q3 y2 Q1.程序分析:
% i. E6 r; T: o& [" J2.程序源代码:, ]6 \' ]8 m1 _6 G7 s1 U7 u
#include "stdio.h"( w3 R& D3 r3 u$ N5 y' a
#include "graphics.h"1 J* z( R* Q; P0 S5 _3 K
#include "conio.h"
2 _2 w, j0 ` L; K H! l, l4 Smain()6 w$ M/ H: m' U x
{
% Z; H% P2 U, Sint x=360,y=160,driver=VGA,mode=VGAHI;0 B9 l( f5 W+ L$ ~
int num=20,i;
& D9 m/ H; H& ~( ?8 q* Sint top,bottom;' s% w7 @. H- u7 i$ E+ j
initgraph(&driver,&mode,"");$ [* h4 v# c7 C2 M7 Z! ]1 q
top=y-30;
2 u/ R; n6 O1 @" m2 t7 cbottom=y-30;
q" D$ A( j6 `6 V5 V7 Z+ U/ Tfor(i=0;i<num;i++)
# D9 t2 E* z' _: ~: k6 i{; Y, r+ l5 ] C# |# }# i5 a X+ U5 t9 ^
ellipse(250,250,0,360,top,bottom);/ Q: \& T/ b6 V+ E
top-=5;( w) u% s0 ~4 ~, |: P
bottom+=5;2 w* g. k% W6 ~5 [7 D' B
}
8 O1 L9 f* |3 a2 g! a6 B; jgetch();
' [9 L# H# U1 @7 K}
% v: f9 D3 I! d==============================================================) [1 L e Y( j4 j# [' ]% f
【程序64】
o1 C& _2 u% e6 b, Y题目:利用ellipse and rectangle 画图。) u8 r( z6 Y) i& C
1.程序分析:& Y' ` y, o6 S( `
2.程序源代码:
U; h% F+ r0 T* `- V* G#include "stdio.h"
3 s! C+ K" d; L' Y5 L- M g#include "graphics.h"1 ~) Z' R2 Y/ K, }
#include "conio.h"7 V& A4 I' _8 ~& j
main() X) l$ a" c5 d8 c9 {8 b
{* E" O8 T! Y' e3 t- V M* D
int driver=VGA,mode=VGAHI;
5 ~* E/ @" c# ^7 v; o$ V, X3 G% }int i,num=15,top=50;$ J% B) u) g; R" i; s
int left=20,right=50;) z1 Z* Z# G! z. f9 R+ ^
initgraph(&driver,&mode,"");
7 U* @: l' q' d8 t/ Pfor(i=0;i<num;i++)1 s8 I, s' b; X r' f( z
{
7 X8 @: G$ t7 Z0 V3 M: Pellipse(250,250,0,360,right,left);
# u- h& N3 [# Mellipse(250,250,0,360,20,top);9 Q8 N0 F8 j- o; ?
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));7 e: ]2 B8 ~! z4 T( G- E7 d
right+=5;
7 @3 i8 p4 ?9 J( ~left+=5;
3 z( W6 Z8 z* d* \4 |3 Gtop+=10;
8 {( e. d3 K/ E5 P- U}: ~! y# a7 O/ V h1 x9 c/ r4 r0 N# l
getch();! t w& i% w0 J3 N2 o1 b$ K( u7 l
}+ ^ d! i1 F, D+ q+ i) {" V7 `
==============================================================* ~$ }) f- S8 a: Z; }
【程序65】
0 E: W. v" E5 I2 u; F/ j# m题目:一个最优美的图案。
% A- @1 g& x9 y9 h" _: U1.程序分析:( z' w; [1 H; ~- ]8 O, j
2.程序源代码:
$ V5 Y; a$ D! r! K* q#include "graphics.h"
H2 }/ h! K& Q2 x# q7 T% ?" W#include "math.h", ^2 Y% M- r, |
#include "dos.h"
, H& D2 B: z. P5 N8 `' g1 z& W1 m#include "conio.h", @& ?- J m/ l, T9 l
#include "stdlib.h"0 g; X) f% [/ ?9 S9 O; p$ g
#include "stdio.h"( ~7 |( ?/ r7 O4 J
#include "stdarg.h"
, C' `3 S3 [6 _5 {$ I- P$ ~#define MAXPTS 15
5 W9 a; A T# `$ @( Y5 h#define PI 3.1415926
$ }6 S; e/ P" h, e6 s& g6 ~struct PTS {) i! T3 F. O( K4 Q# { A& L
int x,y;
/ K0 S4 E# p1 ~2 K};) ]" J+ j! a# T- |6 y: h7 p4 P" o
double AspectRatio=0.85;
9 ?" j; h# k4 z( ]4 ovoid LineToDemo(void)3 f ?% s# i9 F: t+ b6 B; s! `# T* c' [
{
+ ]# \( w. J" W" p, qstruct viewporttype vp;
: j$ L9 x$ I8 T$ H% |! zstruct PTS points[MAXPTS];
% M4 S3 \+ o* ]# L- B, S7 d5 v5 \int i, j, h, w, xcenter, ycenter;
, R# e5 }( ?5 I9 Oint radius, angle, step;
5 V3 @7 Y( M- d; @" Udouble rads;
7 b" |9 i$ I( |# J! pprintf(" MoveTo / LineTo Demonstration" );
+ I% K5 _7 i1 T! z: h7 P* r. Tgetviewsettings( &vp );
- l9 j' h2 x% X2 E3 N& i7 xh = vp.bottom - vp.top;; W, S& S1 U4 d" }8 e" l$ Z
w = vp.right - vp.left;3 h1 c' k* k0 K
xcenter = w / 2; /* Determine the center of circle */
0 B8 Q& K& l! Pycenter = h / 2;, B! b" E* I4 D, L: B' {
radius = (h - 30) / (AspectRatio * 2);* U, a0 d5 w3 |5 s
step = 360 / MAXPTS; /* Determine # of increments */; Y1 c+ w0 z8 T! ?: o
angle = 0; /* Begin at zero degrees */
# S4 H: _; c+ Q0 J0 L9 ~7 Z5 F. sfor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */
' G+ j' w) o+ K& rrads = (double)angle * PI / 180.0; /* Convert angle to radians */
' Y% F. }; I# ~! \! C1 Fpoints.x = xcenter + (int)( cos(rads) * radius );
1 V$ Q6 |/ c3 V( P4 wpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
3 R4 Q/ _. p: Q1 ~) C, n6 mangle += step; /* Move to next increment */
7 {2 T- l( E0 K: o}
6 } K/ b% ?' v& S% Gcircle( xcenter, ycenter, radius ); /* Draw bounding circle */
5 y% R, l# `# a) F8 o9 f% p; Gfor( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */2 x* U2 s) I% H' X$ m- I+ k& l
for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */, _+ ]+ C# e4 H2 p7 X
moveto(points.x, points.y); /* Move to beginning of cord */& e# Q: q9 |( z/ L3 T. f: C; m' i) K( O
lineto(points[j].x, points[j].y); /* Draw the cord */" { o' L$ v$ S( i% \9 e
} } }
4 W) `8 i A1 J5 L6 |main()6 C1 J# d" _% y: T) l% M
{int driver,mode;5 V6 Q6 o& P0 B7 E
driver=CGA;mode=CGAC0;
: u* b9 b- m" J6 L8 k! ginitgraph(&driver,&mode,"");- R9 m8 {8 _# a, u
setcolor(3);
& e+ [* R7 x+ c3 w: Tsetbkcolor(GREEN);- a& ^8 E; q, i: g I
LineToDemo();}
, B+ y- V' P8 |. v- L4 Q V% C4 v==============================================================
5 d* ?0 p: g" v: k+ S4 G; N【程序66】# z' }2 g! }. G8 b
题目:输入3个数a,b,c,按大小顺序输出。 0 Z; H$ O) E5 V J
1.程序分析:利用指针方法。
9 o# o/ r& b2 i2 J6 `6 l1 H2.程序源代码:
4 L. f7 A7 @- g) b, x3 f/*pointer*/
3 Q* {! N3 }: z7 n) w& F2 Amain()
3 B1 c- x( |# [{/ }5 `* @) p4 h M& F# @2 R" t
int n1,n2,n3;, i& Y" Q- ?* G7 u9 K4 c# p; T7 G# r
int *pointer1,*pointer2,*pointer3;
/ _9 O, i5 ^, ?4 Xprintf("please input 3 number:n1,n2,n3:");
% A& Y3 d: j) l$ A# tscanf("%d,%d,%d",&n1,&n2,&n3);1 E+ G5 b0 q, f+ J# _
pointer1=&n1;% T1 J3 o/ g! U; j4 b, R8 X
pointer2=&n2;4 s8 H% v" f' q( a, R# X
pointer3=&n3;
, i( J& z+ C7 mif(n1>n2) swap(pointer1,pointer2);4 Q' V P: j4 Z2 B9 \5 g y
if(n1>n3) swap(pointer1,pointer3);
& s/ M$ k% i' M3 \0 y0 ]& e4 A1 ?if(n2>n3) swap(pointer2,pointer3);& `5 `8 O w/ A. Y
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);" Y9 s2 h4 k0 |, g4 N# p* n
}7 N- K. _- T1 f. v
swap(p1,p2)
1 O9 ?* e7 ?, V5 k: [8 s4 zint *p1,*p2;
+ y+ o5 G0 \9 b: ?- p9 K1 G7 L b1 ^{int p;
% h/ K9 w5 a- I% V! v3 [2 }4 x& H1 h* Hp=*p1;*p1=*p2;*p2=p;/ _2 J( r4 Q3 q2 d6 E4 Z5 \' V
}7 ]) ^& y# G2 m4 s
==============================================================% |. u7 R' o/ i8 J4 S, H
【程序67】
' u$ S: b, O b题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。, ?; N1 } Q' o" }* |) G8 b
1.程序分析:谭浩强的书中答案有问题。 & F" [: s0 Y8 p9 D/ J9 E& ^
2.程序源代码:- g9 g: Y) p9 | C4 w0 o
main(). N4 |9 Y! c2 d' W. d& @( k) o
{
4 S; v ] m S: Z- Xint number[10];$ u' c, R% C1 d& m
input(number);+ S6 ~* p4 @3 T. @: c4 g! {+ n5 G
max_min(number);' ?( z5 a/ S8 [ b
output(number);8 S6 e; d, k3 X4 D& j9 {: h2 y
}
2 d* n1 p! f7 f# r1 {+ d& d) rinput(number). u( B" X& ]% d A! o+ c
int number[10];
% {/ M9 F' d3 g# d% }4 ^{int i;3 Y% l) c$ I/ z( @; H+ [& M
for(i=0;i<9;i++)0 K$ r- W5 Q5 t8 E! ^
scanf("%d,",&number);
: D3 I! A! H/ D1 E" ~ scanf("%d",&number[9]);! I& u7 y% m* N: ^3 h1 r) R
}
. Z+ e2 C3 Y G2 N, N3 R# l& r" ?max_min(array)& x" h/ l/ H+ \- Z; Y
int array[10];
# F3 ~$ ~' l2 j9 c/ L6 U{int *max,*min,k,l;
- Y' m8 z; ]6 ?3 W: {; d; Iint *p,*arr_end;4 n! v( `1 A* X, G
arr_end=array+10;1 t# ^" b6 A- |/ @
max=min=array;3 M' \) l" W' r# i4 j @+ ?
for(p=array+1;p<arr_end;p++)
/ G6 c* C# L0 N" y& ^ if(*p>*max) max=p;4 O8 L- i/ f0 T8 f# _$ g N
else if(*p<*min) min=p;, z7 C; [0 C: p8 X$ d' S! v
k=*max; U/ r7 K9 K+ R- `0 D) v4 m2 `
l=*min;& j7 ]8 f5 a- r+ a" v1 u
*p=array[0];array[0]=l;l=*p;) U+ f0 [% K% C) |7 l' {
*p=array[9];array[9]=k;k=*p;
! p- {: \. M2 s" u9 k' h2 w! Q return;, s) L# T- L' J! Y$ K
}
2 v7 c( i M% e. X+ Woutput(array)
?2 @1 \ b( mint array[10];0 g" s1 j# Q* s, r. a6 u4 Q
{ int *p;( x3 d+ H( _1 x- G9 N; c4 p1 d) N
for(p=array;p<array+9;p++)
2 J% ?" e0 t, Q8 B printf("%d,",*p);7 k% b! F8 I- D6 @, M2 E" H
printf("%d\n",array[9]);4 _" ?: A6 ^& `' `1 D
} N! H7 O# `. }( Y) c
==============================================================
/ W1 {6 q5 Y8 z( U9 k5 y【程序68】" s# o1 \ ]9 T' S/ i
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数+ x( o7 q6 Z3 r
1.程序分析:8 D. i! V' I, q7 F
2.程序源代码:
( U3 `8 ~, c7 T3 b: Jmain(): {8 v6 E i; P3 o# Q
{
* ?4 w8 d+ Y9 k$ J$ S6 L2 ^int number[20],n,m,i;% D j# ]; C# W: e
printf("the total numbers is:");
4 ^# \" y8 `2 a" S# z6 Lscanf("%d",&n);
) J3 {% C$ O' z. k6 d. \printf("back m:");
O+ N: x* ~9 X8 `3 g" \+ |scanf("%d",&m);
$ x/ `: ^7 u4 O3 R8 x$ F$ l Gfor(i=0;i<n-1;i++)
/ u; f9 E5 h0 H scanf("%d,",&number);- t: |( ^- f o8 s7 \) S
scanf("%d",&number[n-1]);
( o) w% Q) v# imove(number,n,m);. E r+ O% S& Y6 Q1 Q- \, C$ _
for(i=0;i<n-1;i++)
9 J& W3 g2 I, Y printf("%d,",number);8 b3 `& K+ v) S6 L
printf("%d",number[n-1]);
3 g- N; H) P( g( s1 {2 [: A}
6 j, O- ^) f! Y" ]( Pmove(array,n,m)
/ o0 Y" O& q0 E6 t ^: nint n,m,array[20];
% j) Y& L: a" ?{
5 L" z1 n& K4 l kint *p,array_end;
7 U2 n: Y2 N0 @- m/ Q \array_end=*(array+n-1);' A+ K& `6 m% e; }0 l
for(p=array+n-1;p>array;p--)* \2 Y4 t# T* o# `, Z( e
*p=*(p-1);1 \2 W6 g# K+ {6 k
*array=array_end;
/ k' X* P. I! e9 P# Q m--;
1 ?8 l I: h6 l i if(m>0) move(array,n,m); [; W8 v1 ?0 G. O" ?$ @
}
% h! O! r5 W" }$ m==============================================================
: X+ K h4 W- z t) B0 h7 r【程序69】7 R9 M- J' M0 {# l$ j
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出; \( @& y6 m' S' i
圈子,问最后留下的是原来第几号的那位。
0 o; D/ j1 }6 X3 a- @3 @5 b& J9 {) T1. 程序分析:2 l* V5 H1 U/ {. ?
2.程序源代码:
2 Z0 H7 w; ]6 C2 a5 l# q) Q#define nmax 509 X/ {6 M: @9 Q# R
main()2 {: M" z1 |8 m" l: o- Y- H; @
{
4 {; {% |9 r! x% \3 M$ u9 y7 Tint i,k,m,n,num[nmax],*p;) r# X* |2 n* X: X9 i; B5 E' G
printf("please input the total of numbers:");
) |( W q0 M7 B u4 [scanf("%d",&n);
) N9 q0 t1 C, zp=num;
% Z o. k) i& }# [- \2 B# ~for(i=0;i<n;i++)
8 _! |$ j) [" K& b2 [4 b4 p, w* [( ?# c *(p+i)=i+1;
' T! n# E" f, E2 d7 \ i=0;3 E2 E% Q( G4 D! e8 a
k=0;' _1 a' q/ N0 t% b6 ?
m=0;( n, k2 B% }, N
while(m<n-1)
; i2 S4 D/ f o( ~* ] {- w2 F* ?, h' _9 u( r6 K7 ?" y/ G/ r6 l
if(*(p+i)!=0) k++;% H4 `) q- u' K/ D, a r
if(k==3)
- Q' l8 e! x' `: o { *(p+i)=0;7 {: a9 ]8 g. ~
k=0;
' ^0 X a! x+ p m++;
% c5 P% \# s' U I }: J+ H4 W. \) K5 r- `1 T/ J# Q1 t
i++;% A2 K) S) y$ c4 ]
if(i==n) i=0;
4 I$ k# X( B! p9 K}
8 D- }1 t3 Y$ c3 c! twhile(*p==0) p++;2 k8 d- G V1 S5 m+ Y& z
printf("%d is left\n",*p);# h: O! ]& _0 H' s, y9 E
}! r: m; e# g* B9 o
==============================================================
6 f7 _& J3 v/ z$ N: @3 r【程序70】
# C2 \$ _: ^- H/ Z; w题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 2 U1 w) v9 X" c8 c2 a1 {
1.程序分析:" c. k5 z/ _6 E7 |9 m
2.程序源代码:9 v6 }7 ]" Z, @, k5 _# C$ i
main()
* I5 g& x$ f% @# {. _! K) k+ c4 L{# }; X- o9 l0 a1 _. c! N
int len;2 f5 B$ g* |/ z+ C- I4 Q5 ]5 Y
char *str[20];
5 T4 l( E }; Eprintf("please input a string:\n");
5 p( Z5 k' b' @/ y% iscanf("%s",str);; b: D5 Q5 f' |, P r
len=length(str);
+ u$ k+ t8 \ d& E+ j0 p! yprintf("the string has %d characters.",len);! K; ~' n O; K& [% L
}$ R( M x8 [7 @ c' l
length(p), l5 x1 X2 U; m- m+ O' W
char *p;9 j- P4 g2 g( ^! `) y2 G
{3 K, k: `9 |8 ]& G3 B$ q( Z
int n;
l2 w6 j5 F+ `+ X3 f. mn=0;
% O, t9 Q% \" @6 d* b7 ewhile(*p!='\0')3 ~5 k0 }/ w5 g6 M5 `. A- C
{/ f6 s7 l5 Z- t) j& `7 n
n++;% |$ f% u) i) x" l0 e
p++; ~* b' n0 @; A! c+ |' v
}2 R0 h5 B, E: t! S
return n;- F. P4 f# S- [4 v3 N7 l3 j' Z0 h& N
} |