【程序27】 9 D. o* d+ G2 f# L' }3 r' A
題目:利用遞歸函數調用方式,將所輸入的5個字符,以相反順序打印出來。2 x4 _1 A& |1 X
1.程序分析:
$ a2 k6 v8 A4 A4 O: s2.程序源代碼:2 h, q8 H/ [' e* ?, A: P
#include "stdio.h"
8 ^3 w9 Q0 Q: l8 q: f" F8 ymain()" |5 y( b2 i- B4 w1 `
{
- E, ~6 G3 \1 iint i=5;6 ]5 Y0 A" S7 C5 \" j, i
void palin(int n);
. n/ M( h7 T! ?1 ?+ H* Dprintf("\40:");
4 |* x* t+ l# D4 xpalin(i);0 T; O4 ~* u, _0 m7 p+ J4 g5 f
printf("\n");/ S0 @2 \3 W% P& Y- O
}
: P. i& \+ S7 @. Fvoid palin(n). _6 k5 P6 {# O3 A9 s
int n;8 P; p' K* K- p! P n5 ?; h6 `
{8 a+ o. B$ Y, }; |1 c- Z6 G
char next;0 z- Q3 j' e# {2 l) k) I
if(n<=1)1 ^; N8 r1 D, e6 D7 ^( q
{- I# X0 e# A% e1 [/ A
next=getchar();
9 s- x! s% j' t, F( V( _ printf("\n\0:");
( \" ^* ]5 O* X putchar(next);. j$ r* P! d" U: |6 d
}8 z+ E2 I6 @4 k1 i
else& G/ |% d8 P+ e# c k' e/ R
{
3 G5 M4 D; F5 c) T% r7 M/ N9 Q5 C next=getchar();6 c% F" o$ x1 T" O
palin(n-1);
. M7 ] H, O- C7 V/ D putchar(next);2 ^9 I0 t9 r9 e0 B
}
; u3 L4 {( t3 W! p* V}
! ^, P, Z/ n! Z==============================================================
! g' M* M W; D) c1 u; }【程序28】信盈達嵌入式企鵝號要妖氣嗚嗚吧久零就要
! a' r6 E3 U4 D9 ~" v0 U2 i題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第
# a$ |. x) e# o( E+ @, P 3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后
) K8 b3 `$ _# N3 K4 _ x 問第一個人,他說是10歲。請問第五個人多大?
, s8 Q h1 z/ h8 ?6 ^1.程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲數,需知道7 ?1 W4 P/ f! a( G- [6 [
第四人的歲數,依次類推,推到第一人(10歲),再往回推。, V& V" W/ P8 O/ O; m) E
2.程序源代碼:
* D' S3 F2 R" ~' V+ d4 a; bage(n)- e. d$ P) [% b; ?( s+ v8 i: K
int n;
7 K$ C" u. I& F; Y$ D8 T{8 ~+ ]/ u! n e5 p! [; Y" T) @
int c;9 A; N# l1 q, B# A0 g! n+ P
if(n==1) c=10;7 d, q- H( ]' b5 b# a' H
else c=age(n-1)+2;
7 I n! V4 o7 W# b: `8 Oreturn(c);/ P" Z: G/ p/ {. i( P
}
) b# C0 Q6 V+ K) Vmain()
1 ^! s& \5 q i0 Z+ D* C{ printf("%d",age(5)); a1 y3 A* }2 J2 ]* i, w
}
, H2 U1 l6 ?. N% Y6 e==============================================================
8 ?+ N+ U5 Y/ c5 M% |' E4 S【程序29】 * X2 g! @# t+ Y) U4 z% a
題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
: y1 N& b4 D3 l+ |8 Q1. 程序分析:學會分解出每一位數,如下解釋:(這里是一種簡單的算法,師專數002班趙鑫提供) 1 T' _/ _0 j/ Y- F. O. ^' f9 z$ F
2.程序源代碼:
. B* k$ a2 V- {! zmain( )9 M: H5 p3 @2 X& U" N1 _" N
{4 A" D8 {1 k$ W) t# z- i
long a,b,c,d,e,x;& T4 r/ N2 J( y R+ @
scanf("%ld",&x);
( v3 x1 Q/ ?' Z/ ^0 I: |5 `a=x/10000;/*分解出萬位*/
4 N' B! K7 K) nb=x%10000/1000;/*分解出千位*/
! k: X! R J' Z. @. \c=x%1000/100;/*分解出百位*/. o4 v$ @7 N; f. e8 Z$ ~- c) c
d=x%100/10;/*分解出十位*/ ^) h4 F3 Q! {- ?
e=x%10;/*分解出個位*/
& [* v3 G5 B# j2 R% zif (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);2 d7 Z, b, `- T) g& `
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);1 E0 A& Q' e$ u6 S0 m
else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
' m* B+ U' W5 y2 [! S+ i8 S' R/ k else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
+ g1 `) W/ u) H( H else if (e!=0) printf(" there are 1,%ld\n",e);4 A/ B* y; ?: s3 O" v
}
5 D- H+ l/ Q7 z7 H8 }5 f==============================================================
' S- o: |5 q/ a【程序30】 ) k9 w0 H7 L+ L* M; w& H
題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。 . ~: j9 J4 @/ u2 J
1.程序分析:同29例
5 ?2 |6 j) i4 u9 T' N% [2.程序源代碼:* C4 ^" o/ T- U& g
main( )) f* d& F/ Y1 _3 n2 ?
{# o1 s: t! E! ]9 H
long ge,shi,qian,wan,x;4 \# X- I- A+ R
scanf("%ld",&x);
- C; p0 |( K3 p8 O% U2 Vwan=x/10000;
~2 j: y+ O7 o; R o/ c# Lqian=x%10000/1000;- x9 k2 r* ^- [6 F
shi=x%100/10;- ^. [; x3 E; y: F7 u* a$ ]" u5 V
ge=x%10;% Q- e: r3 h5 ]4 i% ^# B% w" n# X
if (ge==wan&&shi==qian)/*個位等于萬位并且十位等于千位*/
; A, Z# s$ e1 c* R `) [ printf("this number is a huiwen\n");
& D x# P/ ]( s! Nelse
% F9 R$ d( s( t6 P% s0 b printf("this number is not a huiwen\n");) {# l9 b$ v& ^ ? S
} 【程序31】
4 \- A7 f/ u' e題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續
[0 t9 u. {5 d 判斷第二個字母。
( |$ [5 X2 m$ h2 w1.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。
3 C# ^ c& D; O. o2.程序源代碼:
6 Q, j+ Z& Q# D( _& O#include 8 G! ?4 n. h+ |- o, s( a4 J' k# c4 X$ k
void main()* x# ?/ M3 T3 V
{
$ _* K& I' X: }0 A/ cchar letter;6 {6 Z5 g' I: P. @ a6 ]0 i
printf("please input the first letter of someday\n");3 T2 ~" |2 g; B* b
while ((letter=getch())!='Y')/*當所按字母為Y時才結束*/
. a5 @7 w* i( E. N2 n0 G" b{ switch (letter)
( K n+ _7 f( {4 [8 s {{case 'S':printf("please input second letter\n");
, ?; H1 K+ C' f if((letter=getch())=='a')
) q9 I$ Y# j0 x) d printf("saturday\n");, d( ~& U: t# }+ [+ A5 z! M
else if ((letter=getch())=='u')8 i# S- H% R4 Z; u7 V
printf("sunday\n");' r2 W. ~ s9 W. S4 Q
else printf("data error\n");4 K+ \. z1 V: z, M
break;) t8 S+ b4 w* L/ f
case 'F':printf("friday\n");break;8 z! r1 e" e d1 C5 }& _- |
case 'M':printf("monday\n");break;6 s, m! O# h1 _/ E9 A7 | Z/ U
case 'T':printf("please input second letter\n");
3 Q( T1 w) f/ E# m$ [ if((letter=getch())=='u')
& B- Q7 a1 n6 v/ v6 z printf("tuesday\n");' l2 m) M- ?% J- T1 X
else if ((letter=getch())=='h')$ l- c8 a2 H2 O" n7 a4 t$ I# M4 b
printf("thursday\n");/ `+ U" ]$ l0 C9 L
else printf("data error\n");1 i$ v$ U# M0 M! x' n% m
break;& h$ I8 X( S9 P; i- t! ?, n
case 'W':printf("wednesday\n");break;
2 y, `# g1 ` }4 tdefault: printf("data error\n");6 _& C! v/ A5 }4 s
}
2 ^1 v* j: Z3 Q1 ] }0 s8 o( w) G7 k6 Z' @
}! L+ e# G8 g" [9 u
==============================================================
( x3 s& L2 x) H【程序32】
; ?8 Q6 P8 p$ F. y" r5 c# b- [5 ? c題目:Press any key to change color, do you want to try it. Please hurry up!
, ^' q9 I0 u3 M1.程序分析: / z/ }; t3 Z5 X) J
2.程序源代碼:# M0 }# E O" e3 V
#include
% |2 b" U1 l2 o& j/ _void main(void)1 R; \" v K W0 ~
{
7 ~; Q- F1 Y; }9 `4 h, Tint color;
8 _2 [% J5 a; `1 D6 X! Kfor (color = 0; color < 8; color++)0 k4 [4 ]- i" I# r0 `
{
) J% D5 [( x6 ~6 V: f( s9 |* T textbackground(color);/*設置文本的背景顏色*/
) `# G5 U( c% j6 R+ A cprintf("This is color %d\r\n", color);
- M4 ?6 G5 Y9 ^/ q- x2 n' ^ cprintf("Press any key to continue\r\n");% G. I& @! H5 _. V' @" ]
getch();/*輸入字符看不見*/
3 g, I- B3 Q* k1 K- E$ o: a6 J }
+ l1 d) `0 W, a}+ G" \! o& S' w# I
==============================================================
' I/ @* j# d4 R8 |【程序33】4 w c2 `" F1 k! s) Z% t2 @" `$ t" e
題目:學習gotoxy()與clrscr()函數
" i: @$ y+ `0 m1 O4 n$ I e1.程序分析:- A" V1 T4 I3 Y' h6 ]6 h m
2.程序源代碼:# ~1 D$ o* P4 h- J8 J: R/ {+ P
#include
$ d7 G+ L4 H- [+ C( H8 Xvoid main(void)
8 A+ b. o3 Y2 q6 x4 N) j{/ e/ O' ? F5 ^! f
clrscr();/*清屏函數*/
: V+ N' O3 R, B7 |8 ^ Ftextbackground(2);* |2 t4 }- R1 k u, D
gotoxy(1, 5);/*定位函數*/ g1 ?' o2 A, _' |4 ?5 t
cprintf("Output at row 5 column 1\n");" u$ r2 d2 u- k* b2 I2 c! G) V: w
textbackground(3);3 R2 D2 Z/ o( Q: N6 f
gotoxy(20, 10);( B! h5 \4 e: I$ M
cprintf("Output at row 10 column 20\n");
, C- A, \5 F6 g}
% t5 ~9 O0 b! ?% R8 ~& Q==============================================================7 c$ n! i6 N" _4 h) c1 Y
【程序34】
) _) ?, n4 ^9 W題目:練習函數調用
8 |' ^- H' _6 y/ }5 G- ]1. 程序分析:
# J" C1 o1 n' B+ ]! o* W2 E& t2.程序源代碼:
: @: f4 D; b+ J9 z* ]8 y#include 1 V( ~2 J3 O! k( u
void hello_world(void), F& l, C1 O7 ?
{' I2 N& l" d1 p) r0 {& e& B
printf("Hello, world!\n");
% A# n0 {2 G$ R2 P}8 D( o# ^5 H! w6 Q
void three_hellos(void)9 b( {3 Y2 ], V3 u5 s
{ C# R' A5 ?6 [! i
int counter;
! d+ w3 g3 Y5 m+ \for (counter = 1; counter <= 3; counter++)
) ~ f, v# U% G+ {4 Rhello_world();/*調用此函數*/9 W$ {# l1 S, i* x0 Y
}$ ~3 s. U1 z, W+ s& h
void main(void)
3 T/ s" d5 k$ S$ |. p' k{, g7 F9 K2 _; l4 P
three_hellos();/*調用此函數*/3 N, J; T$ G$ |9 N9 b( L& j
}
) y2 z# r; ]4 l7 g3 M==============================================================+ m( m' b* P9 e. V# X# M* A1 @
【程序35】9 Y' |# R4 M( z
題目:文本顏色設置
! m y* D' e$ x1 L/ M, L% {1.程序分析:# f# O/ e o: B7 |# o' r" d5 A/ l
2.程序源代碼:
! p+ j z+ b1 ]! c6 F( Y#include
7 h# f( _- y( _$ g# {void main(void)
/ M- d5 U8 ~+ w: ]+ Z4 e3 |{6 z; d5 ?6 U# j* _- C- _/ _
int color;) v# A- ]* s% b/ u( i
for (color = 1; color < 16; color++)* p, R, ?0 t! J, L+ i! Y |
{
0 g2 ?+ {5 p' A; V0 C textcolor(color);/*設置文本顏色*/( {, ]4 V( }3 i% }
cprintf("This is color %d\r\n", color);9 s) h0 p: E7 H5 F
}: H9 ]4 K) |$ Z& G( g1 M
textcolor(128 + 15);
) J z( {3 a6 G+ d/ L7 z( v% |cprintf("This is blinking\r\n");) Z! V; v- |6 c6 h$ l4 `
}
( W$ t& e) _+ N/ x==============================================================: Q. A/ X6 M/ h; M
【程序36】
9 [0 O p: f/ e/ ~+ e8 b0 j* i: I4 Y題目:求100之內的素數 5 v: d9 e/ x; m5 M+ e0 W( ?
1.程序分析:
9 l' u! V; F7 U6 E2.程序源代碼:
+ ]6 I$ a7 ^" I! a+ E#include
. g. i9 k2 B+ [$ E$ Z4 r% X#include "math.h"' R7 H. A' w+ e" t- C
#define N 1016 |' z3 Z8 U9 I1 s) B
main()
/ l# z7 A7 _2 k; k ]# ]! `- f9 ~* q{3 J; R; m& _" q& X
int i,j,line,a[N];; `, J: t. O i: ~1 ?
for(i=2;ifor(i=2;i for(j=i+1;j {: e$ o$ ] s9 \4 ~1 f( ]
if(a!=0&&a[j]!=0)' [' ^2 f( X. i3 t, {. C0 ]
if(a[j]%a==0)6 m. b, n" L+ }
a[j]=0;}1 C! p1 j/ P) |9 J0 l4 c5 p
printf("\n");
0 c5 C, ]5 V" w. u4 O0 ]' \( kfor(i=2,line=0;i{
: C: U; ^' w: F7 {2 r2 g: X/ } if(a!=0)* S% ]" S. J' b$ u
{printf("%5d",a);, z t& ^) X( N* r5 h i0 R
line++;}
" G/ Z* O1 l/ A. {1 A if(line==10)3 ^- Y# R) j4 A4 H, }7 i( C& H
{printf("\n");: @) f; w! L3 T7 r3 w
line=0;}
2 y; Z" \% k" O3 r}$ f4 C1 Q# x4 Q$ B0 n+ Q5 F4 p' O; }
}. r8 b/ D" O- V$ o: j
==============================================================) e' ]5 c& l- }
【程序37】
" M9 E+ W5 n% t題目:對10個數進行排序# V6 @9 q- {7 A# C
1.程序分析:可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換,' k/ L: }! s8 Q, [; r6 G5 C6 f
下次類推,即用第二個元素與后8個進行比較,并進行交換。
! H( }5 f' u2 C6 g. w' K- [% T2.程序源代碼:
* C {, P* ? z- k9 r" x#define N 10
. u* j# O) U3 z. ^main()2 B9 Z8 A; o2 h! b* y% c; S
{int i,j,min,tem,a[N];3 d: }6 s. p3 Z& b% P" K4 Y# J
/*input data*/" X, F* a& k. p C7 {+ H+ N f& p
printf("please input ten num:\n");
* E9 l4 A- z0 n: M# |for(i=0;i{
/ s" p$ o( W( o4 l% zprintf("a[%d]=",i);
2 _% Z, K0 k6 Lscanf("%d",&a);}0 a$ K) V& z- Y# J" G- j; r- ^$ w" P
printf("\n");. z# u$ F$ ]7 c" ?: Q" g! i( k
for(i=0;iprintf("%5d",a);1 E# c+ i3 r) N4 z6 w
printf("\n");
{( L) h0 p& U+ @, g0 f6 s/*sort ten num*/1 M. q `/ e2 i8 X3 Y
for(i=0;i{min=i;5 }. I% p( v, g9 @+ n( ]( {% O
for(j=i+1;jif(a[min]>a[j]) min=j;
* A+ G/ s" A3 e7 G: V( v9 z- K+ N, i+ Btem=a; q$ x! {, N& {; H
a=a[min];. r# u3 }5 f l, O0 s. J' e
a[min]=tem;$ ]0 Z! l& [* D* C
}
7 K4 P" {8 P+ p3 h" E/*output data*/
( q5 w1 E0 k% K9 J: _9 aprintf("After sorted \n");
) w4 Q5 p+ Z' Sfor(i=0;iprintf("%5d",a);$ u, B- t0 k- q2 l4 d4 M/ ~
}" i: K! V% d- d: Y$ j) D, |
==============================================================
6 s: X* a, f9 V* y. h% b
. c! a) P: O! b. H. B c |