|
【程序61】
) t3 H7 ^6 r1 P4 w题目:打印出杨辉三角形(要求打印出10行如下图)
& C& Q' s) R1 p! j& D1.程序分析:
7 _% G1 h- F ?0 w5 J+ o% T. D9 y 1+ Y' K1 K! E& t' I1 C7 l
1 1
9 u; @! |, O: H' l 1 2 1) U) i. \- [* l/ u
1 3 3 1
9 j j' j W7 C: Q- M 1 4 6 4 1
0 q$ s1 [4 R9 n1 ~ 1 5 10 10 5 1 ' R! e; G, k4 x
2.程序源代码:
& h* {1 \4 a' cmain()) t2 N, L$ P$ b3 Z1 C+ n; M
{int i,j;
1 [: \! c6 p Nint a[10][10];
9 q9 |0 Q0 h/ {3 @* O9 [- kprintf("\n");
0 U/ f7 v* M1 u4 k, H5 Wfor(i=0;i<10;i++)' Q/ q0 ~; a1 F$ Z* O7 B! b6 g$ y
{a[0]=1;3 h. h* T1 H% q: G
a=1;}. N' Y Q( m) h1 M9 `
for(i=2;i<10;i++)
1 ?6 r' o. r3 \5 L, G for(j=1;j<i;j++)) h$ i: y' @% J2 q$ G+ ]
a[j]=a[i-1][j-1]+a[i-1][j];
+ S9 o* K; U- C; _1 @& `for(i=0;i<10;i++)
) Z+ i. e' E: ]' Z% V1 e {for(j=0;j<=i;j++)# i2 x+ V, c% p5 c. Q# l
printf("%5d",a[j]);6 ~5 d% j1 ? Q, u; @& V
printf("\n");. h. X+ [* p) M3 D" g, [: |; I
}
$ t: C5 B9 W" W/ o}
/ }# ^8 F; h3 |6 z' R* k==============================================================0 N! D: f' J. h% |0 k5 g$ H% H5 b
【程序62】7 f) g' n! R! J; N# P# N
题目:学习putpixel画点。* K3 U9 u/ c+ o4 R0 n! M( Y) y+ F, ]& o' u
1.程序分析:
3 G* R/ q" L& {3 P- o0 N2.程序源代码:) g0 K) s0 }! M% g _4 s7 P1 d
#include "stdio.h"
/ _1 H1 x) D9 G#include "graphics.h"4 U- E$ b& k. b
main()4 {$ {8 h1 d" l( ^- G9 K/ v
{
6 Q6 x2 k5 m% _+ @2 o6 Jint i,j,driver=VGA,mode=VGAHI;
" n; J0 R$ x# _: Ginitgraph(&driver,&mode,"");
8 a) j/ w$ H: k8 @setbkcolor(YELLOW);. N4 z' y, L# x0 A3 _
for(i=50;i<=230;i+=20)0 C7 H e+ M3 v
for(j=50;j<=230;j++)
' v; Y9 b7 D ^2 {" f0 a putpixel(i,j,1);
( N, ~4 s0 |$ H, Bfor(j=50;j<=230;j+=20)* I0 s ?& \0 d
for(i=50;i<=230;i++). G# C# M" B# s# i% D) c# _
putpixel(i,j,1);
% K8 T2 D& V+ x- d+ I& O}$ x U8 m' @$ ~+ P, _+ _. g" L! Z" W
==============================================================
1 f. c+ ?' n* p& Y【程序63】
; l) y2 F9 T- Y. V$ w& C6 R题目:画椭圆ellipse : \; `* F8 S" o
1.程序分析:
' o' X" v g C9 F* n! Z2 f" M2.程序源代码:
( i) h/ z$ L0 f+ d% F7 x#include "stdio.h"$ l+ P1 y; h: z2 a f5 U
#include "graphics.h"* t" M: @# b2 I4 U0 u
#include "conio.h"
" I d3 ]+ E$ p3 x& Jmain()9 _' H+ M5 p: P
{8 a2 }( C; D/ k
int x=360,y=160,driver=VGA,mode=VGAHI;
$ u$ U6 l" [( t* aint num=20,i;
9 ]# |, H/ y w& `% f Nint top,bottom;
3 N+ v5 I( R! Linitgraph(&driver,&mode,"");
/ L" O% F# Z% Stop=y-30;
+ K1 |9 s! D& O8 N$ Pbottom=y-30;, R7 ^: F; Y- A4 k% n" L
for(i=0;i<num;i++)0 Z5 n5 Z: w S( ]1 E
{
8 ~7 O5 m0 U& t. i) Zellipse(250,250,0,360,top,bottom);* |# O, Y- X# Q$ l. U# B' o" w
top-=5;2 \; j& v3 i- ^2 }. B3 u
bottom+=5;
4 ]9 ^6 I% H" \- N* H5 y}8 E' a1 b" K9 T b' @
getch();
! |1 _! Q9 j1 j5 ^& w}& \7 @3 A# `" \; W% ?5 j
==============================================================
6 U' C6 t; h/ |2 X【程序64】3 |% _8 m" s" ^* b, E7 |8 D
题目:利用ellipse and rectangle 画图。8 M0 [9 g* Y* K# a3 @1 v( v
1.程序分析:" i2 S2 G$ I7 b
2.程序源代码:
' U. p1 I+ c/ j#include "stdio.h"5 c# g& [0 }2 S8 ]
#include "graphics.h"
/ o0 X4 D' \2 w#include "conio.h"1 l1 w+ e6 k. i" K2 A6 d
main()
6 L) L e+ N, R `, |& t0 l{
- `1 s" ~* x4 zint driver=VGA,mode=VGAHI;
6 V7 @" s0 W( U r( J3 M; f1 ]int i,num=15,top=50;. B3 P8 D6 A3 F3 z1 @
int left=20,right=50;
; ?9 _0 ~) L; a, Rinitgraph(&driver,&mode,"");1 N8 T/ O! V% e: o5 H
for(i=0;i<num;i++)3 [1 ^( H {8 `& C2 F) Y B
{
. L* l$ t3 }! Oellipse(250,250,0,360,right,left);
& ~9 n3 d- d2 Vellipse(250,250,0,360,20,top);' S+ G8 B# `9 ?
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
8 L! q6 s, v; P" G* \right+=5;
1 i; W4 G! W' _. n- e& ?. m. d+ Wleft+=5;' T; t" W$ N r3 b* d) q7 g
top+=10;
- [5 e$ X* L# V: z}
/ i' Y; w) e1 {getch();
3 V, a& A* s, J7 W1 a" }}
. ~& }1 |% ?, {+ h$ t==============================================================
m8 r- m4 g3 T, g9 n【程序65】; R- a( p' `7 o: t7 ^
题目:一个最优美的图案。 $ f% d# b. d% V# \, v0 }+ W2 q
1.程序分析:. h9 E5 J7 B. v9 ~! K/ L2 v" ]
2.程序源代码:
[6 d5 i: ^" ]#include "graphics.h"
! v; F' B$ h3 C; L0 ^7 M/ c) t#include "math.h"+ G/ f, D. i3 H- p3 D
#include "dos.h"+ N( t' q* q9 p, d9 F
#include "conio.h") j# ]3 e5 E. I: {0 R
#include "stdlib.h"
7 t; y5 w% V6 M( X#include "stdio.h"
]3 i2 K7 e7 R#include "stdarg.h"
3 e; m5 W! D! W, g) U- y! v5 o#define MAXPTS 15* _' G/ U% S, h, ~/ w [
#define PI 3.1415926
: j+ T3 m4 F; l" s" ostruct PTS {1 ?9 d2 e# h( _
int x,y;
: o5 |3 Y* B( S# @( b+ L! C};- x% ~! J. A9 W: F" x
double AspectRatio=0.85;5 v8 o9 y: h ~5 X- @% s7 g, w4 m' Y
void LineToDemo(void)9 N: V. R2 ]8 F9 ^! C( ^
{" p* z- P6 B, r
struct viewporttype vp;2 ]1 R1 {) {8 W" X! ]
struct PTS points[MAXPTS];* F- x/ I, W! `9 W1 c1 B# }! b
int i, j, h, w, xcenter, ycenter;9 N. J6 J, F. X7 C( f: D
int radius, angle, step;
7 N, _5 {% J+ h! t+ h2 D' b( xdouble rads;
$ M- U- O A7 E! o8 ]3 @printf(" MoveTo / LineTo Demonstration" );
$ L, t+ _% k* Z4 ?7 _getviewsettings( &vp );4 s- V1 E" v; ], B/ S
h = vp.bottom - vp.top;
' ]8 a2 H( a# K7 O$ ~$ `3 e% [- q3 ]w = vp.right - vp.left;
$ T7 Z3 z$ E: |9 I3 e( @% |xcenter = w / 2; /* Determine the center of circle */, [1 B8 K7 t% z, h
ycenter = h / 2;
7 P0 _5 T% {7 Z& |7 p6 \, Xradius = (h - 30) / (AspectRatio * 2);
. L6 m. e; _ L% G1 H* w) }( @# Bstep = 360 / MAXPTS; /* Determine # of increments */% Q: E3 U; @% d3 R0 q/ x2 U
angle = 0; /* Begin at zero degrees */+ P3 I4 S' x+ @5 ?
for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */- p: W" a% u+ z) P' ]
rads = (double)angle * PI / 180.0; /* Convert angle to radians */
$ X8 @$ r L8 \points.x = xcenter + (int)( cos(rads) * radius );
0 \" Z% C; |, `% apoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );& f( z T+ I& o& f I# w
angle += step; /* Move to next increment */! j/ F6 I5 W5 Z5 S
}
$ I* f6 l. f8 H% x7 b$ B0 r- O4 icircle( xcenter, ycenter, radius ); /* Draw bounding circle */
5 B5 ]- I- X$ g Y) O) l( \for( i=0 ; i<MAXPTS ; ++i ){ /* Draw the cords to the circle */
! W( m9 M+ h5 U1 W0 Ufor( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */. u$ @: d5 W) \6 S
moveto(points.x, points.y); /* Move to beginning of cord */7 o' y; S! g4 v7 `
lineto(points[j].x, points[j].y); /* Draw the cord */
% J* R1 y! c+ ]1 _} } }! h: p6 {3 i4 R0 Q3 _2 a( T' m
main()- v' h- g, \1 Z/ [" H
{int driver,mode;- S' y5 L; M0 z _
driver=CGA;mode=CGAC0;( C" h2 j w6 y
initgraph(&driver,&mode,"");
* \6 S! {6 ]$ t/ r' Y* [setcolor(3);( X1 d! t, R9 k! A8 e' ~0 g i/ D
setbkcolor(GREEN);/ M) I4 z! v9 x7 {3 q# p
LineToDemo();}: x q( Z; K L9 n, A$ I0 R
==============================================================$ ~, L! z% _2 V# W
【程序66】/ b, ?6 o# u% O' V
题目:输入3个数a,b,c,按大小顺序输出。
% I& |" E T! T& y9 p1 ~6 `1.程序分析:利用指针方法。
0 v4 b0 U, y+ Y+ Y2 p2.程序源代码:
% C- d* \# d4 E9 Q/*pointer*/& k2 { i6 x- x H8 `
main()( T, q! `* ]& G$ F) F
{8 \4 l8 G( C- A: u ?* I% m
int n1,n2,n3;$ |4 i& z+ P* ^0 i: X
int *pointer1,*pointer2,*pointer3;" o& }( d. O8 P& n
printf("please input 3 number:n1,n2,n3:");
5 \: @: I, ^: g5 R2 Lscanf("%d,%d,%d",&n1,&n2,&n3);3 d3 k5 @( E2 Y. c' M
pointer1=&n1;! u, N) g9 R- l- N( i
pointer2=&n2;
Q& ~5 a3 \; G6 @pointer3=&n3;% K! J5 [2 }, Y3 Z$ T
if(n1>n2) swap(pointer1,pointer2);
5 i+ f- t1 X9 J' i; |3 Q7 F0 \* [if(n1>n3) swap(pointer1,pointer3);
8 K* f3 W8 j6 i) Pif(n2>n3) swap(pointer2,pointer3);
( e9 ^7 X" _2 ` vprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);- Y& v- X6 j0 R% j' t" b4 g2 S
}5 ]4 b0 G2 v z% `1 }1 w
swap(p1,p2)
6 b. a% O; G6 n5 H! iint *p1,*p2;$ b& u1 [: Z4 ^9 }% h
{int p;4 }) M; I! I2 a$ H' [$ V- Y0 s- f h
p=*p1;*p1=*p2;*p2=p;. m7 ~% Y& m5 R
}
/ r% j( O7 F# c3 ^* r$ W$ O& L1 X==============================================================
( [; |. F5 N7 e; Y$ Y- X' q【程序67】
( a7 B% Y y! l3 U1 I& n题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
9 o7 \8 d8 {! t5 v$ K1.程序分析:谭浩强的书中答案有问题。
9 _6 M7 b$ ^) K, L2.程序源代码:& i2 E5 x1 P4 }+ @. D6 m
main()) W) [8 [& \3 ]# W& [
{
$ U6 k* V/ }1 f0 Y4 J; L/ ~# L8 ~int number[10];
/ B0 o+ L( t: L6 \( Vinput(number);3 V! q' N; u9 D2 d, }
max_min(number);( F4 m; z) x3 L: J4 B" y
output(number);
+ x! n$ ^$ d2 \3 K! {& J% Q( p}- t+ ^( f, H9 ?0 y
input(number)- p1 |; x# N: `) C3 r" @( B9 T- ~
int number[10];, o) _' J, N2 o
{int i;2 R7 r2 V* A4 v: P, ~6 c2 R/ V1 v
for(i=0;i<9;i++)
. @1 G; |& h& L6 ]( v4 R, T: I scanf("%d,",&number);
' E5 z. q4 M9 P9 m( E. M7 u. A- { scanf("%d",&number[9]);
; I6 n1 y5 d! X' u; \. @+ y. h4 {}
! c, D" h9 A' Z9 n- A! Jmax_min(array)2 ]$ K4 g* P, j* K( z2 P8 Y
int array[10];1 `& Z4 I8 t: @& W: J
{int *max,*min,k,l;. p# N5 I8 j* F$ e V/ ]
int *p,*arr_end;1 X7 V' X$ h9 D+ W, U' H9 I( i
arr_end=array+10;
% l0 Q, c, }2 D% O5 _max=min=array;, h" B5 `2 B6 O4 O. k$ f* l4 Z8 l
for(p=array+1;p<arr_end;p++)
: u1 v# h) x! j) D4 r9 \ if(*p>*max) max=p;8 W. @2 m5 f9 s1 H6 ^+ \
else if(*p<*min) min=p;
' v( n- _) W+ H7 X k=*max;
" {5 F; R) I4 \( S# B0 q l=*min;
4 l ^5 R6 B% Q. g1 |6 o$ | *p=array[0];array[0]=l;l=*p;
% Z0 Q& Y4 l2 l *p=array[9];array[9]=k;k=*p;
9 {) k4 V, R$ I3 J3 I6 e/ m return;0 K" W8 c. T, R6 Y1 E
}( o. V2 M0 x2 r4 U L y
output(array)
1 F% S( W7 Z- L8 m- Y; u# w; n6 sint array[10];- E Y; N0 r8 [: ]
{ int *p;5 h+ d7 g5 ~6 V* _" B1 \
for(p=array;p<array+9;p++)- Y, p5 w& U3 {; V! u \) h5 U# S9 A
printf("%d,",*p);) W% I1 ~* r- w2 W, K7 }
printf("%d\n",array[9]);
0 p) w4 G0 ^( C* L! F: H5 W4 Z}! @8 H, T2 g" _- s9 {! t
==============================================================
3 f9 F+ O# K' P" _) n2 N【程序68】0 u+ V; M6 D ]8 O1 b
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数$ I, c) C+ p: }, b4 ?$ A8 q) V
1.程序分析:
/ A2 _6 z, q+ n2.程序源代码:
. H( M/ |$ D0 omain()
$ u/ Q5 U R" j{7 g8 F4 Y- k+ o @& }
int number[20],n,m,i;3 P; s7 w( _& v% o4 e8 v* u
printf("the total numbers is:");1 W7 U2 X. c( f% \8 P
scanf("%d",&n);. i9 h# `3 X) C0 w
printf("back m:");- F! W" m$ \. R; R/ B; _
scanf("%d",&m);3 U) A$ K% @) J
for(i=0;i<n-1;i++)
0 |1 i6 R) g+ L6 P' j scanf("%d,",&number);- V5 {4 B% L( \1 _9 e
scanf("%d",&number[n-1]);
' ^' v" x* h. J/ Umove(number,n,m);1 m8 G; Z1 n3 ~' a9 D7 @( R* G
for(i=0;i<n-1;i++)+ }7 P5 g3 b$ k# C& m
printf("%d,",number);6 r0 E3 n v# I( f
printf("%d",number[n-1]);: M2 [6 i6 m1 x- [; U. g
}
% ]" w1 y G& w: _5 }6 ?9 [( Fmove(array,n,m)
3 \0 p3 N3 E3 L" mint n,m,array[20];
6 I: v; W4 S, ?9 c4 R{& v) J9 W' S0 i" y
int *p,array_end;) c' ?* O! u0 Z+ V! |
array_end=*(array+n-1);
+ q: l4 k3 G, u% f3 E! Mfor(p=array+n-1;p>array;p--)' Y( \7 L m* M+ u8 V' K$ y
*p=*(p-1);
5 e# X0 Z0 H- J) C' d, \8 p$ { *array=array_end;/ R4 I# y2 s7 d# w3 s( J
m--;7 A% p, A6 G# [0 q' o$ e
if(m>0) move(array,n,m);1 ?4 M6 [% `5 B. B! @3 \$ T/ ?
}
- X- x8 }6 d% s2 I==============================================================
; E! d) q4 ?( D【程序69】7 a% V/ R( S. ?
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出
8 z" H" _1 a1 M5 n9 H 圈子,问最后留下的是原来第几号的那位。
& s0 P2 X2 E# P" O! D3 d1. 程序分析:0 F. q. _$ ]* [& h+ H0 {' O; M
2.程序源代码:6 G$ o% Z# h( Z
#define nmax 50
4 |) D3 J7 b: t3 J! j/ ]+ Fmain()
2 D! h1 m9 F$ v% b{
) b8 J- s5 B% T# xint i,k,m,n,num[nmax],*p; t1 J0 f1 Z9 g2 w3 @# c. I" u
printf("please input the total of numbers:");' f8 W- y$ W- T6 f
scanf("%d",&n);
# K6 q+ U1 }# ?& B6 J' i. d1 Op=num;
6 j) C2 x: l3 B4 p: b, cfor(i=0;i<n;i++) R! y) P E! \! t+ R8 Y
*(p+i)=i+1;2 J2 K' h: L& t" D% e5 K
i=0;% I- |# T* X6 P; G8 G
k=0;0 E0 `/ K' u! B |8 c8 p; q
m=0;
. a4 v5 ~2 t4 r! s0 f while(m<n-1)
5 b& r: M& S4 z* y- P. [7 D8 F+ c {
$ s; S. g. L3 F& a2 ^ if(*(p+i)!=0) k++;% y5 Z o" _4 j- `
if(k==3)
( Y* S, h, h7 a7 J+ G" `9 o { *(p+i)=0;
8 g! J; _% t$ x+ J* b% G k=0;
* H% [! S$ U" d' {2 I8 k$ p7 m2 p m++;, }# n A* a8 z& _/ h* X
}
# I" v$ G+ \% E2 P6 Ti++;
* A9 T0 P& w b8 @9 U) uif(i==n) i=0;3 {" f! |$ I M) m" ]
}8 j9 b" d" w1 T9 _9 V4 B
while(*p==0) p++;7 l! e; R( b+ k: A1 v) S: W
printf("%d is left\n",*p);- J& ?3 J% o- k& _, C
}( D9 V( h \( H4 U2 Q4 Y+ p f
==============================================================
\# a. c6 X, r+ h& G3 Y- C【程序70】0 d1 A' T: `* c/ D/ L3 C _$ C
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
" N$ s5 S% F) N1.程序分析:
; G& j2 \0 d: r5 H& W1 x z2.程序源代码:
0 J2 u5 m( W) Y! R" ^/ Y+ d' Mmain()( ^( @$ i1 E H% i" l! S
{
/ x7 n* B. w8 I5 B$ \8 o: p6 z. V: G R! ~int len;$ X" d. J6 c# m' j- w
char *str[20];
( K$ { [7 w3 q0 [printf("please input a string:\n");9 o( Z! O* ^9 y3 e! A2 |
scanf("%s",str);3 U8 O( |) ~& ?) U; F6 R3 l* y* p( s
len=length(str);
; {; j& F' Q& x$ R7 {9 `printf("the string has %d characters.",len);
; m! F+ W- }) H w7 K' ]}& G; W- j, Z/ w+ J: Y5 ?' C
length(p); M F- n: Q. O p; b
char *p;
- P0 ]. t2 }" g{
4 p2 V) \2 i* a: n" A. zint n;) _( s" p- p n; H8 ^0 d
n=0;
. ^8 v8 o+ H5 I7 @! [) C0 t7 Jwhile(*p!='\0')
5 u& o0 b, h |# ~# y/ M{
' M7 ]8 T% X1 ?, T _( N) }! q, d& ?7 ]: m n++;( l% l7 O& D8 Q* r: h3 B
p++;( G' S) Q J# V) x$ Q) g
}: z$ M; ]# e7 L7 K+ S
return n;
& _! N1 E) X i" ]* f) Y} |