【程序87】
" r' H) ^8 V6 ?* A題目:回答結果(結構體變量傳遞)
]' E w$ ~) {9 ^7 _ _3 h1.程序分析: : ?5 U7 m3 ^) o/ v
2.程序源代碼: 2 ^0 }$ j( T" ?- T
#include "stdio.h" + o/ J) u( W2 Z- B( I
struct student ! p) _4 K$ @ Z+ n3 M( j
{ int x;
* G5 t1 u3 X8 zchar c; - X8 G. `8 s9 y: I2 C
} a; & I0 c4 R: S4 t
main() 7 I9 O) @& F. L3 V( ]4 w! e: |
{a.x=3;
" ]0 X1 A* R6 g8 K% d A1 ~a.c='a';
! @9 V% X# A; ]9 ?6 [, B; bf(a);
5 T2 y: l9 n& j7 p4 H; |printf("%d,%c",a.x,a.c);
1 i5 O7 U. a- v9 O/ T}
3 Q0 q& }% q) l* ~4 J5 w# P+ `f(struct student b) 1 I: c4 N1 Z& y8 j2 B0 s# ]
{
4 }1 Q0 U# B+ w* P* T, Fb.x=20; 3 K9 j7 d3 d3 {
b.c='y';
9 d+ p1 f2 H* p. ` `, Z* ]} ( k z" G4 Z* t6 V6 W& Q
==============================================================
" h/ w* T- K1 A5 X【程序88嵌入式信盈達企鵝要妖氣嗚嗚吧久零紀要】
" w) f) f6 j& z9 p7 |6 d& @1 @題目:讀取7個數(1—50)的整數值,每讀取一個值,程序打印出該值個數的*。 \+ w, X+ p! U2 u, Q/ h" |
1.程序分析:
V5 F( z4 e( {2.程序源代碼:
4 i% I& P5 @+ a Kmain()
4 X* d2 p3 r" ?' X- ]{int i,a,n=1; % s5 R8 f7 N8 x- w
while(n<=7) 6 } W0 o n7 ]6 e" T
{ do { 6 Y' R" P$ z* e% r% V3 h& }) @
scanf("%d",&a);
7 x& o0 j( D$ T3 |}while(a<1||a>50);
' r! D2 s) f8 g/ N1 K3 c2 {& bfor(i=1;i<=a;i++) 9 w# u- m: |" D8 _
printf("*"); 2 ~7 j+ \) O4 v8 S8 E& H4 q/ i
printf("\n");
, p( g5 D. F& o" ~4 u# [! Hn++;}
' u+ q9 e, n# ~* y# v# r% Vgetch();
& Z) N$ C% N( a. h2 \} ! l1 d" e# E# @! n: x
==============================================================
/ ]# p- M% j& Q【程序89】
# `: Q' V! i3 q, @+ |題目:某個公司采用公用電話傳遞數據,數據是四位的整數,在傳遞過程中是加密的,加密規則如下:
9 Y v6 }% W2 _- Y0 W' a每位數字都加上5,然后用和除以10的余數代替該數字,再將第一位和第四位交換,第二位和第三位交換。
6 q' @2 i, P. g1.程序分析: ( m/ C4 H- F0 S( o
2.程序源代碼: 5 o9 d7 `2 h6 v( W
main() # A. v. ^/ I8 V3 v
{int a,i,aa[4],t; - r" ]- a/ }% R
scanf("%d",&a); 5 ^6 |5 x5 k2 V+ r# A6 E
aa[0]=a ; * Y! ?& v5 R. V/ t
aa[1]=a 0/10; " V5 w4 Q, \" }+ Q
aa[2]=a 00/100;
" o B E1 S6 E9 ~aa[3]=a/1000;
9 s3 R K$ V# b: e* R9 Hfor(i=0;i<=3;i++)
( ^' }7 l1 i" g; G* {, b/ c{aa+=5; 6 p ^$ Y" {1 \# f( V/ {
aa%=10; 5 ?% g& \. H* V7 C" V$ s' w
} / l$ b: C" Q; l
for(i=0;i<=3/2;i++) 2 t! `* q$ U6 N! M) T# \2 h
{t=aa; R$ D: l- J# v, m
aa=aa[3-i]; 6 _6 i7 J" j+ U7 O, O# B
aa[3-i]=t;
2 @+ L1 [ z* u} ) H ~3 G0 k# Q- _; Y8 E
for(i=3;i>=0;i--) . p0 m5 e$ {/ \# |6 c
printf("%d",aa);
/ p" M' z$ L8 P. i' Y2 l. `} ) W& j" Z7 U1 }/ ~* ?4 Q: V {, Y. B
==============================================================
3 ]! c# D j' ?2 v6 g; e+ E7 r5 l【程序90】
" S3 S. G3 M! g* ]3 w6 k0 l題目:專升本一題,讀結果。 $ b, r2 q- Q" M: b% R5 C
1.程序分析: " I% P m* _$ F' _0 i0 ~
2.程序源代碼:
7 W0 M: c9 j$ B* ?3 a- Y& R" P' L#include "stdio.h"
4 j0 S6 |0 S9 O#define M 5
" A) P+ @9 A7 I+ J" Wmain()
$ q2 W$ X- g8 o/ @' U O{int a[M]={1,2,3,4,5}; 3 Z: o+ P; R4 F4 T3 Y5 q' r
int i,j,t; # I4 d% N, D r8 r) ?! H* V
i=0;j=M-1; 7 n* {* M" J' q; u
while(i {t=*(a+i); / z& J1 m# e, f
*(a+i)=*(a+j);
7 s; m4 L3 S! }1 K! I" ]*(a+j)=t; 6 G8 J' t( {' t
i++;j--; $ q. A+ F5 p5 I8 Z* s/ y! i
} 9 f& b7 s: r3 R
for(i=0;i printf("%d",*(a+i)); W; `$ g, x& m' c5 Z8 M
}
7 E% P( u5 Q* Y- B3 T【程序91】 1 j5 O; Z3 f: b2 T
題目:時間函數舉例1 8 u4 G* |. F9 f8 `7 @' y
1.程序分析:
9 |- q9 s% ~* S: ^" w! j( i+ m% O& K2.程序源代碼:
% l: z5 @$ D# a; A- E! m3 S0 T$ J#include "stdio.h" % V5 s+ g# D0 m/ u( i- A4 D) a5 e
#include "time.h" ' R; Q: ?4 ?8 c% Q! f% d& N
void main() 8 x" K( Q; u- z4 o( _3 w
{ time_t lt; /*define a longint time varible*/ ( B7 _9 w) I! A2 P8 g
lt=time(NULL);/*system time and date*/ 7 P4 |& G( @5 P2 k- q9 e
printf(ctime(<)); /*english format output*/
, F- i% b/ d: x9 \, Aprintf(asctime(localtime(<)));/*tranfer to tm*/ / |4 A3 C" K2 e3 B, E8 ~9 r5 G
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ 8 Z& ^: e% T0 B4 p2 K
} ' O, @0 j6 |. X4 Z% B! G9 ?8 }
============================================================== ; G" ~) m4 J' @7 x+ }
【程序92】
+ L$ q$ {4 h4 V8 Q; P$ ] A題目:時間函數舉例2
8 R- {- a* M* r0 ]9 `$ a8 L: c6 e1.程序分析:
- }8 u, P' F2 j, Y; d; u2.程序源代碼: & X j. H! k; j% w
/*calculate time*/
4 s1 {, U8 \1 m" S# G7 Y; ]#include "time.h"
* u/ _% G% P/ x7 T5 }9 D) ~, w#include "stdio.h" # G, y8 M+ f1 @ M+ [; C
main() / M2 \- E; l: P* q3 S" o& U) D
{ time_t start,end; 7 G5 O+ V+ w, q$ H# j6 s- v- q
int i; + X3 ~7 V& {. ]" r6 x. S2 x
start=time(NULL); 5 M- o- B( z+ }2 B
for(i=0;i<3000;i++) % t' S5 ^) F1 \* ~/ _& F
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
4 r" g W4 m& X4 z" g2 P) dend=time(NULL);
1 X' y' _8 P/ B5 [3 J" oprintf("\1: The different is %6.3f\n",difftime(end,start)); j* N5 |. f% W T9 f3 Q
} " p7 O4 M) Z+ V; f4 @
==============================================================
6 Z3 |0 R+ A" _4 v, E* U4 b【程序93】 : y# I. V+ J- K% p' q
題目:時間函數舉例3 7 ?9 ^2 ^! J0 V1 |1 [
1.程序分析: \. I8 A8 M* g9 ^
2.程序源代碼:
' G6 n" V g0 p8 A, j `& p/*calculate time*/
% l; J1 A9 C2 Z: h! d#include "time.h"
% l! q( J# f7 `6 ^#include "stdio.h" . e+ d, M0 L% W9 o& | r
main() & x" ]& k: J( M
{ clock_t start,end;
4 ?; c8 G4 S3 }0 _/ W& Nint i;
: l5 n! K/ l- ]# S; d3 kdouble var;
4 p* K; j" H- o! |% b* ^0 _/ T+ {start=clock(); & I( B0 P( _2 t0 P8 d* V) d
for(i=0;i<10000;i++)
# \* u% i/ L, H# X( b{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
& K% p9 I, |0 @# _$ cend=clock();
( X2 p* ~' _7 |/ ]" Q2 fprintf("\1: The different is %6.3f\n",(double)(end-start));
5 ]/ \3 @1 z1 D1 F3 P} - R) f' a* i* [0 c9 [- Q8 W$ F5 R
============================================================== : Y" {: p! I: C4 B( D4 m
【程序94】 " |/ W; h* C( s6 [ q
題目:時間函數舉例4,一個猜數游戲,判斷一個人反應快慢。(版主初學時編的) ( w8 q" k; j; K5 _% S% Y/ M
1.程序分析: - n& O' _& [3 ^- Q8 {3 ^
2.程序源代碼:
; ~' ?7 j+ c2 q. _#include "time.h" - B1 Y* Q9 w8 A' h3 q1 F- x
#include "stdlib.h" 1 k/ c4 b; v- ^
#include "stdio.h"
D7 @5 P4 }; @/ |. Cmain()
) X$ E/ P# \& f' F# w( E{char c;
) C$ l7 Q$ f8 i1 C* ?clock_t start,end;
' R; q3 e! z" S- L' N: z( Htime_t a,b; 8 |. @3 Y5 n! H; n8 I" q
double var; 6 n; b7 x: e3 A7 P
int i,guess; . V; }- i& H& ?$ Y5 Y
srand(time(NULL)); 0 E, ~) ~" e6 o1 V3 ?; K) j
printf("do you want to play it.('y' or 'n') \n");
5 w8 S% f. Z( ?8 }. x* ploop:
: C+ m x& u! N* owhile((c=getchar())=='y') 7 q0 Y- m% e4 x$ Q5 }5 X7 ^0 b
{ + y* c- N) I l
i=rand() 0;
' K# _5 z, @8 o3 W' @printf("\nplease input number you guess:\n"); 1 o) _% j+ _& Z7 L8 g" s; Z) j7 ?
start=clock();
" D( g# d" x7 V( ^) Q* @2 e; Za=time(NULL);
6 G {# T; W7 Escanf("%d",&guess); 3 n' ]) A/ c1 O7 J3 ~
while(guess!=i)
7 [) Z6 p% q' `& D{if(guess>i)
2 i* Z: P$ A( `# h{printf("please input a little smaller.\n"); - L4 f( s; r d, U0 V
scanf("%d",&guess);}
$ c3 Y% _& h" d! K- _. Z eelse % L1 D+ l7 e( M/ i' o# q" z
{printf("please input a little bigger.\n"); O6 F% J& R) h7 e/ H+ {1 B
scanf("%d",&guess);}
$ R! m" W! c5 z3 h: @} 4 O4 B7 _! x# H' ]$ y0 f) P
end=clock(); ) b$ G; R8 S4 |/ t
b=time(NULL);
6 q7 B0 U. A7 a. t4 zprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
8 a$ A9 R9 n/ I) Qprintf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
, f3 k7 H- C1 u; fif(var<15)
8 X3 ~" j5 S1 s0 f& g. ~8 Wprintf("\1\1 You are very clever! \1\1\n\n"); * [ v8 h0 R/ {0 @. Z
else if(var<25)
4 L5 K' |6 P1 `0 G. X# o+ Fprintf("\1\1 you are normal! \1\1\n\n"); : r" P* X) a# A4 W! W! Z
else 1 k w) \; t0 l% P
printf("\1\1 you are stupid! \1\1\n\n");
4 h, {7 ^7 h& h9 ]% Jprintf("\1\1 Congradulations \1\1\n\n");
+ e% m& {8 L3 h7 ]$ ~2 g" l" Sprintf("The number you guess is %d",i); 5 }% L# n' V3 `, j! m. T
} ' C% r6 O( e) j' ^5 G+ i; t# L" M, C" x
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); 7 [7 g( D5 Z0 ?0 e" D' f
if((c=getch())=='y') # ^2 _7 ]2 i) d5 S
goto loop;
3 o( j' V3 L2 r/ F) D} ) e* ?: @1 Q5 [
|