|
【程序61】
1 H1 \# g3 y( K2 e题目:打印出杨辉三角形(要求打印出10行如下图)
" V' H2 E5 d/ s l2 J0 o/ _1.程序分析:3 w' W8 V, [0 f$ C0 h/ t6 v; f
1
. N6 ~" J& U# w 1 1+ f& J- H) a# w1 `
1 2 1: G( @, x4 m7 p, Q
1 3 3 1& Q+ ^8 u$ k4 C1 H2 F
1 4 6 4 1& j/ p7 h/ A3 B! r" H
1 5 10 10 5 1 6 F! P. o7 k0 Q5 R. p/ _2 E) v1 y
2.程序源代码:7 t9 U8 r3 }- A, m
main()) z$ G$ z, o7 y- G5 F/ I3 {2 }1 f
{int i,j;
1 h6 x$ s% g# s' Rint a[10][10];+ F7 E0 Z, O* N) ^* D
printf("\n");" l2 \7 p0 q1 l' E
for(i=0;i<10;i++)
. b6 \+ ^ ]# ?$ D; l8 ` {a[0]=1;
% b. H( u; I; {! k0 x; J2 P# X a=1;}) n1 b0 [ r+ R, g
for(i=2;i<10;i++)8 ]) y9 C4 b0 m+ j& a n* w
for(j=1;j<i;j++) a2 \5 W4 C6 j
a[j]=a[i-1][j-1]+a[i-1][j];1 b# R C, g8 r D c6 H, Y4 F
for(i=0;i<10;i++)
$ `" v3 W8 |8 Q {for(j=0;j<=i;j++)$ d) q, o5 U( ?/ m. w
printf("%5d",a[j]);
0 }9 i' @& F% u8 _2 o0 E8 P printf("\n"); _ L% y A. \0 c* i. m
}
5 z, _& x0 E% l4 Y N; B1 O0 X6 l}+ r q7 q6 M) t( w
==============================================================
9 S# `. x$ m/ N1 Y& ^【程序62】! k# k; ^3 C* K4 L
题目:学习putpixel画点。0 d1 g! n$ i( e
1.程序分析: % g3 d- O2 l+ c. `- c9 D
2.程序源代码:. t9 ^; h% A1 O' [
#include "stdio.h"
* e5 I1 i2 A! J#include "graphics.h"$ e& E9 g$ T" {( _+ P8 {# ~4 V
main()
( J4 N9 i7 g1 S: k/ F t+ o0 Y- }{, H( Z, w _/ L9 y; q) B
int i,j,driver=VGA,mode=VGAHI;3 R0 G/ z% K3 |: T- x- q) }/ K
initgraph(&driver,&mode,""); }4 }5 S' _8 A3 d
setbkcolor(YELLOW);
- W5 S( Y2 u* L! b5 wfor(i=50;i<=230;i+=20)9 Z# } [3 d$ }5 \/ ?3 L% L
for(j=50;j<=230;j++)
3 r* ` C6 ?% ~ putpixel(i,j,1);. p E6 {/ ]# L" x
for(j=50;j<=230;j+=20)' Q; n% z3 [+ e. v; A# R ?/ ]
for(i=50;i<=230;i++)
- z- a# z" Y5 D% @ putpixel(i,j,1);
8 T+ s. ~3 U! L$ N% w+ W}
& a: U H; g! V z. E==============================================================
B* F/ n+ _6 r. O【程序63】
3 u6 _' E: [, k2 H! z: Q题目:画椭圆ellipse - F- c) s) d9 w. \/ |
1.程序分析:
6 u' j7 h" n Z' W2.程序源代码:
" | S5 ?) P+ w- a/ M7 h& D#include "stdio.h"
" u1 l! x3 m8 O#include "graphics.h"
' M" i7 o9 I3 ^#include "conio.h"
2 Y9 v, ]4 K* D+ Wmain()! a9 b+ B1 @$ g3 t$ s- D" s3 T
{
) {# U; W; p' N! Z- l+ `* d; Q* aint x=360,y=160,driver=VGA,mode=VGAHI;3 u& e, l* F& O/ N) l
int num=20,i;( D, F8 o& j- \* i% _, y- X9 x
int top,bottom;) k$ W+ X9 Q+ T9 N7 s) `* i
initgraph(&driver,&mode,"");3 Q: }+ Y# |8 q) l% U
top=y-30;4 a' ^: a |5 |% U% G
bottom=y-30;
^9 X2 d8 x* e7 K' @for(i=0;i<num;i++)
3 f9 l0 I: X" o+ d" X" a" _{' x9 }- ?5 d3 \2 s
ellipse(250,250,0,360,top,bottom);
; b0 l6 Z* s0 w8 g0 v: @' U ntop-=5;* O% k7 B7 J9 K0 H( B/ V
bottom+=5;
5 T; j* b' m% n1 B}. ~2 t# U8 R- [$ F! U
getch();9 i- A7 U: ?8 D+ M* j
}
: {0 g4 v2 y2 m" V3 J==============================================================) T& O5 C/ U6 h- V4 X* D5 ?% |
【程序64】
2 Y; N! C* T* h% Y9 S/ b题目:利用ellipse and rectangle 画图。' h+ }4 e- f8 @, F
1.程序分析:
3 V1 s) o8 i, k2 n2.程序源代码:
5 _. X. \1 D8 W' d V#include "stdio.h"5 E% @# T; `. q' N
#include "graphics.h"7 y& f8 {& i) F* f; }
#include "conio.h", u' y. h; s6 ^+ J6 n% v# ]4 G/ V* [
main()
0 I. E& V/ q: |: N: j/ `* T% X{! m/ h Q# {/ x+ t
int driver=VGA,mode=VGAHI;
0 u4 ?8 ~( N, l3 u0 x2 c# Sint i,num=15,top=50; ^" \' o: Q' q! i! Z, S+ e4 j
int left=20,right=50;6 l+ i% \& e2 r; G+ h4 u3 ~
initgraph(&driver,&mode,"");% I/ s& N- P r; ^! i0 h
for(i=0;i<num;i++); j/ @, z* p3 k
{
, G7 d! z* ^3 b B' u5 d" jellipse(250,250,0,360,right,left);
* H {: |7 b2 b, }* f" I- R$ Rellipse(250,250,0,360,20,top);
* {; L4 N* l5 i( n0 A: g' M8 Wrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
% P5 w l j5 r2 J7 Lright+=5;
& Z C; J! Y8 V) s3 sleft+=5;
! ^, E+ B& ]2 f- j2 vtop+=10;& G U( f- s; x! o- F
}; R% k+ X8 H( u) `5 `: u
getch();+ r: E$ U& N3 P8 \
}
1 W7 K* t) G& Z# M. {==============================================================
* u2 V2 w0 P; B4 u3 N4 |7 }【程序65】
. Y- ^/ ? b0 c x. E5 V! M5 i题目:一个最优美的图案。 + n' c5 [ M9 R% W, _8 Y. X
1.程序分析:
: @( w2 _9 w: |7 f8 b2 ?2.程序源代码:
$ [( T( ?+ [( H; ?#include "graphics.h"
& e2 c; g, ^1 G' {- y# d' I# x6 ~: P#include "math.h"
, e2 |) X+ o# t; G1 N+ M! Q#include "dos.h"
0 D% E6 J% w( m; [, i" H' v, C6 |& o$ l#include "conio.h"% f. t8 v7 A) L2 o( l& S- L7 o) S
#include "stdlib.h"$ V7 G) d' @/ W. J. i5 G
#include "stdio.h"
5 V4 r* e$ ^+ p. y0 ^3 @2 M1 D3 P5 b, y#include "stdarg.h") S' r1 n! ]& v1 f) d' U/ [
#define MAXPTS 158 H( n1 T: Z" f4 P
#define PI 3.1415926
( _) y: Q: H$ V: b3 }: J: xstruct PTS {: [* Y3 K: x% Y# Z6 |; W1 G
int x,y;
9 C/ @! y+ T5 |1 \! E};+ {$ l) |7 t7 [: m ^% ]4 F
double AspectRatio=0.85;6 _! |2 E9 ~, }5 K$ h& T
void LineToDemo(void)
. a+ ]( S1 S2 Y) v+ s{
& A" v8 b8 a. c$ e8 p& u( tstruct viewporttype vp;
) L& S6 _" D* }: D9 Jstruct PTS points[MAXPTS];
! c( U* l! u0 Z }int i, j, h, w, xcenter, ycenter;% w; X* M" D& a5 }
int radius, angle, step;
* m! H, x' ~( idouble rads;
, j) ~7 A% A: }0 J6 Fprintf(" MoveTo / LineTo Demonstration" );% Z+ x+ e) y( F# h6 x
getviewsettings( &vp );
# B4 N% C2 o% Wh = vp.bottom - vp.top;
" L( y8 j( L8 y. nw = vp.right - vp.left;' Y6 Y) Y" L! R5 l7 }
xcenter = w / 2; /* Determine the center of circle */
( p4 a: z+ u% ~8 K6 Jycenter = h / 2;
6 x- r5 @; T3 z: T/ \radius = (h - 30) / (AspectRatio * 2);7 q% O7 C+ H) U$ c6 v/ f& M
step = 360 / MAXPTS; /* Determine # of increments */' h% g( \+ W6 V" k4 H
angle = 0; /* Begin at zero degrees */
) H* u3 u2 W( j0 Efor( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */5 n; I% i6 e; A8 i; Y! E, A/ l
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
( k; e y+ p% A$ l9 D# S1 I# Ppoints.x = xcenter + (int)( cos(rads) * radius );
( b$ U+ a0 g: q4 ?. ]" w5 Kpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );/ ]9 P& y0 w+ G2 ], O" o9 f
angle += step; /* Move to next increment */2 q9 w1 b* n; G. y1 `) U( Y
}. I' v# F, W0 o& z
circle( xcenter, ycenter, radius ); /* Draw bounding circle */; M+ [( v$ Y) T2 K
for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
' }* R' K% f% K; I/ Jfor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */1 G% I! |; t" i
moveto(points.x, points.y); /* Move to beginning of cord */
7 ]! [$ @, p& u/ F; elineto(points[j].x, points[j].y); /* Draw the cord */
. {, o/ s! V6 y3 J1 ~ c B. S} } }
8 E$ o& l! t: ~; M+ t& t3 Bmain(): @2 M5 @5 [ B2 n6 B7 W
{int driver,mode;
5 D5 R2 h+ x& `$ Ddriver=CGA;mode=CGAC0;, w0 s% `* G( ]5 |4 @ D% p' L! z
initgraph(&driver,&mode,"");
! |4 S4 R% O* Q: g. n- C9 Isetcolor(3);% U# t9 ^4 N) Y1 G& n" F
setbkcolor(GREEN);% M4 i% w3 K0 ^5 ~/ s5 H- n
LineToDemo();}) c. S* s# ?, F/ a6 q2 p
==============================================================
5 v! z1 w6 z) H Q: D【程序66】
) g% D0 d. q; x' k题目:输入3个数a,b,c,按大小顺序输出。
# Z. V" b3 g% y+ P1.程序分析:利用指针方法。1 j/ k$ Q4 w3 ?. I4 Y
2.程序源代码:: @7 T7 V+ J% A8 {4 g, ?
/*pointer*/8 g3 r7 d5 V, A9 D4 ~" v1 G' ~+ w
main()! H( |- Z+ y6 J6 S1 X: t: k2 Y6 U: e
{8 A* ` I" D( g, t# j( Z( T
int n1,n2,n3;" \; {: Z7 b8 ]
int *pointer1,*pointer2,*pointer3;' X6 {4 j% u0 B, L1 f
printf("please input 3 number:n1,n2,n3:");
2 _ p* l: j/ a' v) T/ Dscanf("%d,%d,%d",&n1,&n2,&n3);
7 c" u- y5 z4 g& Y. G+ ~; A7 J4 z4 Xpointer1=&n1;
8 l4 k4 w7 Q. V1 p2 T& p3 p8 zpointer2=&n2;
% m% M2 L1 v# Q" S5 x& l0 \pointer3=&n3;2 I9 j3 n; M' d6 o6 s
if(n1>n2) swap(pointer1,pointer2);
' y, X; n' b& q* lif(n1>n3) swap(pointer1,pointer3);$ K6 O: I' h& K. X$ U; w, E# L0 b
if(n2>n3) swap(pointer2,pointer3);
+ ?; q" ~, ?- |9 l' Xprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
w) K0 \9 C; y9 m7 x7 c7 N' k}
1 d( k7 i0 x [+ H" _9 aswap(p1,p2)
. g" R) d s' a) wint *p1,*p2;/ A( q* I5 Y6 d8 |
{int p;2 ~+ o' h5 E5 x0 _* c6 |: W
p=*p1;*p1=*p2;*p2=p;" m# q4 b) [/ j; S/ g
}
* M; q B# A9 {* x8 Q& g+ F==============================================================) L3 g3 ~5 w. V k" w% W- l2 u
【程序67】
& } |' c t6 Y, i. l# m' w题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。- [% g6 ~! B" N6 u
1.程序分析:谭浩强的书中答案有问题。 ) _4 K1 R" W! h: E0 K7 ^
2.程序源代码:$ A: Y. z* _ r# Q: E/ Y
main()& H+ e. |7 g7 z( L ^1 b) D) M
{
6 Q( @9 n1 w0 d7 p. f" W; H# _! |int number[10];- g- s1 h+ P1 Q! E7 G' p* ^+ D# Q
input(number);
) G) }8 B G8 y! kmax_min(number);" H; f# ~5 I8 U5 _
output(number);" ~& o8 v# J1 x( ]% C
}" B5 B3 v. p6 @7 F/ D s( ^7 O& j( g+ }
input(number)
& y% h2 D0 ^6 ?3 W( v2 U8 kint number[10];
7 t# r, k; I" w$ E0 J) P, J' a{int i;* P" j/ o' Y+ [3 M
for(i=0;i<9;i++): k( o+ K* W9 T
scanf("%d,",&number);
& A. K* k4 g; t) I. }) E6 f+ X scanf("%d",&number[9]);- B% G% x' @9 x* i, l
}1 A2 L3 v4 F1 x
max_min(array)
; Y/ F' v9 H2 v$ O& X0 Dint array[10];
+ d. R8 r+ S, V, K{int *max,*min,k,l;4 z ?2 U6 o; N& u: T t. W7 k) V; m4 u
int *p,*arr_end;% ?5 ^& ~: ^: V* ]
arr_end=array+10;
0 s* j( n7 w6 c! lmax=min=array;1 C, L5 K* E0 z7 t) [
for(p=array+1;p<arr_end;p++)" T% [7 V* c1 O% a e- x' A' ^
if(*p>*max) max=p;
- m4 b9 W) X1 l/ S" k# A$ w9 ~ else if(*p<*min) min=p;
7 {6 Y! t- G4 E1 H. i! r( G k=*max;
" u( V, o. I" n) A; F6 J0 s) B# L l=*min;* N0 V! J( g9 K$ H
*p=array[0];array[0]=l;l=*p;
$ b# Y( H6 a$ c3 ], H5 F3 J2 b( [! ?9 e *p=array[9];array[9]=k;k=*p;
) d3 M( o3 b$ w6 M2 v return;
/ X( [, [$ }6 R3 H3 H2 w: e}
) Z/ u4 G- y9 V1 Woutput(array): n1 C0 X/ f6 K$ c- u+ H( n
int array[10];6 T) T& @: h# c+ I2 q
{ int *p;
8 b6 M3 U. k+ B$ A8 `( v$ Cfor(p=array;p<array+9;p++)
; ]) r! }3 u) H; b& ]" m& P) t1 q printf("%d,",*p);
8 c8 a' ~; V. y% Eprintf("%d\n",array[9]);
3 J4 y$ l& m/ N}& E% p K. _3 Y. |' ?
==============================================================
4 ?6 f& j4 u4 B# N' p【程序68】% @* ^6 M. e" \
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
, ]- e4 R9 s7 o3 m. h" I) O1.程序分析:6 J) y/ {/ |# |! S6 T6 j2 z
2.程序源代码:4 J- K8 s: O) }9 H5 M5 h
main()# F1 h* V9 ^8 E0 A) Y/ F0 m0 l( T
{' h: n/ `, c& @
int number[20],n,m,i;* f, c# |1 I. |8 c; [( u" A* H" `
printf("the total numbers is:");
3 H% Y6 w7 n. x1 @& Kscanf("%d",&n);4 F$ z" M* v1 [" \
printf("back m:");; u& `9 n' K ~6 g2 A
scanf("%d",&m);4 u5 Q. q. R" D
for(i=0;i<n-1;i++)
+ c- \, u2 F7 a. r( E scanf("%d,",&number);
. E Z$ Q5 L$ Q2 S' @' V# wscanf("%d",&number[n-1]);+ n' w! j- E$ K' \
move(number,n,m);
0 C* D" B& b' z t5 X2 p% [% {0 f0 Rfor(i=0;i<n-1;i++)" w+ V) K; Z0 M
printf("%d,",number);
2 m; s) c) O7 J" m- Z1 Jprintf("%d",number[n-1]);' h t) x e2 d' H8 H# \' f! W, S3 m
}% j8 B0 ]9 Z( v
move(array,n,m)# a+ e5 a9 S7 _; M5 p
int n,m,array[20];
) `8 ]5 o J! f{
- A, ]9 v+ B a1 _0 O4 eint *p,array_end;) S5 H5 U' J. u v7 x
array_end=*(array+n-1);7 |( _8 R% ]: L+ T0 m' X6 H% N; E0 p
for(p=array+n-1;p>array;p--)9 _: h3 X9 j- A" S* |3 e
*p=*(p-1);5 i- E5 k* X; b ^- k
*array=array_end;
3 n5 u) X5 @0 q5 u. d# _ F! i m--;
/ Z& X) d. @1 L2 j+ s if(m>0) move(array,n,m);. F5 U, l. ^5 S* x4 m' e& o& v! {
}
9 d, z, h8 z* K1 g7 |) x$ {1 m( S: W==============================================================
) }: y- o% ~ M% k& F+ G4 u【程序69】
# Q1 k" A, [9 f$ ?: s9 E2 G题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出
7 v4 B# l4 r2 ^; N7 E 圈子,问最后留下的是原来第几号的那位。. h, ^9 Y- W. B$ ?- W6 H/ \
1. 程序分析:
. L: `9 N- \) L( d9 ^2.程序源代码:
3 q$ I1 R+ D1 ]9 x#define nmax 50! K1 n! j$ y$ c
main()
" u5 j, X, I& \6 [7 z9 I0 o{
3 L, [; c8 U1 w8 w0 A7 nint i,k,m,n,num[nmax],*p;
0 v& A( _* U, Z, n: h; u$ oprintf("please input the total of numbers:");2 e9 j- I2 n" _7 x3 D# C2 h
scanf("%d",&n);# ^8 {$ T4 _& k9 Y) B
p=num;' n# A" a: A. K, K
for(i=0;i<n;i++)0 T- f+ x1 v6 {$ r
*(p+i)=i+1;* K2 h6 q$ f; z0 w4 Q2 E9 v8 A
i=0;
7 F! `! m0 [3 Q6 U( x k=0;
% w# V% c5 t# H" B6 \: c m=0;! h w6 N3 |/ s0 l) ]( x) o
while(m<n-1)- ^& f" x6 w5 r. y
{& F$ l8 ~" D2 y6 S/ u& Y0 d
if(*(p+i)!=0) k++;* ~: {- t+ F" f6 o5 K
if(k==3)! D7 H4 n4 B7 G6 ]5 W, e# |
{ *(p+i)=0;& r4 _6 n0 @8 h/ Z% s" S; M W
k=0;
2 \8 e6 G& p7 w, n! V m++;
, a- n! O* V5 J" Z }
1 p$ f9 b9 ^1 W( A6 Q2 N8 _i++;
?6 X/ D( x6 T4 _if(i==n) i=0;
: I% W) \1 }# ~( E& Z+ b7 n2 P4 ]( S}, \2 C% ~8 _8 S
while(*p==0) p++;. @/ p) x7 P3 g. k/ P
printf("%d is left\n",*p);+ r8 i& l6 I/ \# p# I2 O, f
}5 d/ S0 k( E+ W; j0 {2 L
==============================================================3 ?) d' K3 S8 G/ M1 c, A
【程序70】
; O6 U4 E/ j- U2 l' B题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 H. ~+ A3 ?8 N$ x/ v# u$ x* s- P
1.程序分析:, g" `8 G* U3 w6 l( v
2.程序源代码:
* W0 k& f+ y6 G- O1 T% S- Smain()7 Y2 v' U1 d# B2 ], M7 D7 ~
{3 n* t4 y$ [: D4 w4 r: A
int len;
# `3 [- o) [- U% G' |char *str[20];3 E2 k# I/ z4 {1 x( l
printf("please input a string:\n");( e2 u) d& J1 @8 I- [2 y3 S6 C
scanf("%s",str);& g v6 r( x3 e6 |$ z
len=length(str);
% G; O0 j' Y2 [$ I" qprintf("the string has %d characters.",len);# B! k- q8 @* |4 V r8 h) I
}
% [( P) e+ A2 I; Jlength(p)
1 Y4 ^/ n8 U* H5 J6 N8 I0 Wchar *p;" S- K9 B$ {# y6 t
{
& b6 O& w" l+ c# W+ ^+ T. Fint n;, X8 Y; m$ O3 c& @$ k- z
n=0;
# T/ Z" h4 [( `0 _8 C( twhile(*p!='\0')
" f: f ?7 A+ ]0 P" \6 B2 L/ J{$ Z! R6 h; d( F% T( l
n++;! ~4 y' F9 H2 G8 G, Y5 t0 J0 n6 [
p++;% `, R# I4 ^( C% |* u
}8 K) z9 t7 K. m/ J9 Q* {& |6 Y
return n;$ M8 }+ | F) \
} |