【程序87】 / L/ @. {- B% I$ F( Q, ^# v/ K' s
題目:回答結果(結構體變量傳遞) ; W5 U4 v q' S4 @' o0 S4 d& G
1.程序分析:
* G7 B6 C/ s9 h/ d/ l. c6 l2.程序源代碼:
7 I: u: V9 F; R$ H+ q- Y#include "stdio.h"
2 [9 F& b# v4 _; F0 k0 @, T/ Fstruct student - K1 L- c' p5 l3 D
{ int x; , ^( D: T/ z: x9 S
char c;
|4 n B# b/ G. ]} a; $ e( p) t `6 t* D7 _( k
main() ! v# m* }; u1 S" h3 |
{a.x=3; & C, \6 y" B& [
a.c='a';
. u' _) A: f. [# r6 qf(a); 0 n7 o3 g) ^" q) ^4 h$ p
printf("%d,%c",a.x,a.c);
% Y. @. O7 B9 c}
- J6 I4 f3 ]9 S0 j7 F) If(struct student b) ' I9 u) g5 E% r9 U" @
{ 1 [7 z: O8 a0 i7 S- L
b.x=20;
, `2 O& F7 Z/ B3 [b.c='y'; 6 E" a- V. T! @9 z( ]
}
# P) I6 l3 T( Z. ]7 c==============================================================
# C# ]" H; T# d% z0 x& h【程序88嵌入式信盈達企鵝要妖氣嗚嗚吧久零紀要】 , _ o) P3 y% y
題目:讀取7個數(1—50)的整數值,每讀取一個值,程序打印出該值個數的*。 1 U w$ {* G$ { B/ P. R) }+ O6 }
1.程序分析:
( R, g1 W& _* I3 Y2.程序源代碼:
2 r4 T/ `) a6 Q+ {0 o; w {main() / u i# w" b' {" Y! k
{int i,a,n=1; 1 p7 \3 ~4 F' R, u; z" ?9 Q
while(n<=7) : b& d% y( c" B8 ~) K# M
{ do {
: ~4 ]* {' E* e$ T& dscanf("%d",&a); : x p+ U. X2 W! p J
}while(a<1||a>50); p0 t/ b) ^ _9 O2 H$ P* q
for(i=1;i<=a;i++)
: Q4 n% R* k' S2 Eprintf("*"); 4 V$ G& w9 W. I$ C' o. t
printf("\n"); , |) ]8 W1 B9 H" u
n++;} , q, q- `5 i$ |, Z* Z# v3 e
getch(); , u# F- w# m ]7 ~& r2 E
}
1 t4 _3 }% P9 S; d* x8 K============================================================== 1 s/ S; ], n& m, f
【程序89】
! e! u. G& Z n( O3 U" f2 D; O題目:某個公司采用公用電話傳遞數據,數據是四位的整數,在傳遞過程中是加密的,加密規則如下:
, U3 O5 M) a h$ A' q每位數字都加上5,然后用和除以10的余數代替該數字,再將第一位和第四位交換,第二位和第三位交換。 9 Y0 ^$ V' ^0 {7 ] k
1.程序分析:
8 j; P$ V0 S" e# L! P2.程序源代碼: $ y. S h4 s1 |' \% Q' o8 C
main()
5 E9 n( S# a0 c# I5 V) o4 r9 Y{int a,i,aa[4],t; 7 B5 j; J- h8 V9 u1 N' Y% b
scanf("%d",&a);
' \2 K" J8 w# K8 ~aa[0]=a ; ^7 x/ ?/ V5 ?+ C% F
aa[1]=a 0/10; - u$ G# V! k4 Z& i0 G+ K
aa[2]=a 00/100;
+ T# N! G- h' f3 a3 y0 i5 P# Iaa[3]=a/1000; 7 F1 n _% S1 g/ M
for(i=0;i<=3;i++)
) ~2 i0 y3 b6 ^( O9 Q. n2 F5 u( R0 A" s( E{aa+=5;
: m4 R2 R7 A6 P% c6 c5 laa%=10; - Z5 k% V6 L( ?
}
6 D4 B1 s* B. e# tfor(i=0;i<=3/2;i++) 4 B8 D2 r. p9 Z+ k
{t=aa; : ~9 U2 M$ D6 B8 u. `: U
aa=aa[3-i]; ' s% n+ D, A% r) V t! b1 Q
aa[3-i]=t; ( Z; ?. \4 p. v0 f+ v
} - e. [' ~2 _/ p5 i/ b0 I
for(i=3;i>=0;i--)
0 u8 F5 M2 t* G; e& {( lprintf("%d",aa); * E7 |6 E" k" _* N$ E# I. \
} ) t7 h2 r6 D5 V( H3 C8 N
==============================================================
4 o. L# a) D9 `& l" ]【程序90】 , d% z& W" c7 Z
題目:專升本一題,讀結果。
/ ~) Q, s+ M; _3 _1.程序分析:
d8 ?) m: G5 Q2.程序源代碼:
- L8 |. u; ]9 O2 m; Q( P#include "stdio.h" ! p$ @) ?6 [" L
#define M 5 ! a9 o+ c! Q7 w: B5 w6 N8 S
main() 7 F0 g& [7 R! ^8 _" B9 d1 Q' L
{int a[M]={1,2,3,4,5};
& [) Y1 Z- ^5 @8 r/ s$ t( x7 [' K( cint i,j,t;
* |2 [- M, c4 M1 \* J/ Gi=0;j=M-1;
# v4 `6 v* U& R6 |2 u# mwhile(i {t=*(a+i); r' }# i/ J7 W; }
*(a+i)=*(a+j); : z3 H6 \2 j+ d7 g* \- g
*(a+j)=t; 9 N+ f6 b4 r$ ]; O
i++;j--;
4 S2 c( w8 u$ \% b}
% |0 I0 u; x5 `( dfor(i=0;i printf("%d",*(a+i)); & T+ O0 _( s, K8 Z# V3 t
}
9 I7 T% n5 @+ |( |3 S! @【程序91】 . `) I; }& e9 L5 t- O& F
題目:時間函數舉例1 ; R8 t) [% D2 F! t2 c$ m
1.程序分析: 9 H! @1 q* v! i8 s3 h0 n
2.程序源代碼: 6 g P T* @# [* p8 [) y1 z5 A
#include "stdio.h" 2 ^/ s H9 h% F P
#include "time.h"
( n( c7 \/ N( G( {4 [8 V, _void main() 2 Y0 X1 l6 [ P, \# @4 M+ s
{ time_t lt; /*define a longint time varible*/
7 y% A9 G. W& Q5 m9 Ylt=time(NULL);/*system time and date*/ ) \" d: `7 K6 w- \
printf(ctime(<)); /*english format output*/
, x5 W7 A" G2 H' @0 u7 `* Nprintf(asctime(localtime(<)));/*tranfer to tm*/ 9 r5 ^9 u4 ^! o, J5 i4 Q$ B3 W
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ 7 W& @4 S' G# y* T2 _# p. T' o& a" X
} : s5 M$ e, w7 P7 j' _
==============================================================
, n4 H8 H9 u1 ~【程序92】 ; l. f. K, f% n' O0 R
題目:時間函數舉例2 k( \. d1 u; G! v! S; b0 ?+ D8 m
1.程序分析:
3 p8 X0 M" g# Q' u. h0 T2.程序源代碼: + J2 I! n( V) |) o: _
/*calculate time*/ * _% l. \0 f. D
#include "time.h" # O. F% S/ e( _
#include "stdio.h" : `& K% k3 M& v' ?5 s k6 {$ u
main()
$ p+ Y: l, q3 H% B& {{ time_t start,end;
% A5 j8 k6 T) |7 M; d0 gint i; - C4 t# n8 Y. L" j9 k& D2 c: P
start=time(NULL);
4 _8 ~% T* R& Afor(i=0;i<3000;i++) 3 Q% o& f; H9 d) Y5 l* n$ u
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
2 A( ?- ^- F0 A: W+ eend=time(NULL); - t( i @, U# V: j* T8 H! f
printf("\1: The different is %6.3f\n",difftime(end,start)); _/ b6 L4 W6 e) T1 P
} & w5 ?' R' Y/ b( l0 i- ^8 H
==============================================================
3 Z* M* P/ x: l, Z3 [3 z }. R【程序93】
9 V* n* l2 Q) s* b2 e9 P& @8 ]: e) p題目:時間函數舉例3 / _) r/ ~/ c2 F( U- D8 S6 {
1.程序分析: - ` ]! O- b0 |
2.程序源代碼: + `$ |+ X9 k. G: `; J# k [8 |
/*calculate time*/ % `3 p4 l) G& b% S5 E2 G
#include "time.h" . ?/ Z' n+ K* ?
#include "stdio.h" 0 F" ^' _/ O6 n! ?3 S: B0 {
main()
7 t: K1 q, j9 o2 m; T- ?* v0 U{ clock_t start,end;
0 R# W U" c6 `* k' j& s5 bint i;
. _& e/ n, _- Y- _; b/ C# ~1 sdouble var; 9 ?( T4 T$ \* O# u; K
start=clock(); # F1 d8 O& Q& x
for(i=0;i<10000;i++) 9 p& F3 P0 m& j- H4 t
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} ( q' d( b z) H6 G: b! F7 _' }4 w _
end=clock(); 6 Z. F& k) F% h! O
printf("\1: The different is %6.3f\n",(double)(end-start)); " L% a1 y n$ B2 x1 R) ~
} $ a; j& `/ k+ k/ `. D1 a8 Y3 b2 L
============================================================== 3 X: A1 O1 Z6 [. ]8 z: `1 e
【程序94】 # R1 l* L" L2 \- |+ b' B) {9 P
題目:時間函數舉例4,一個猜數游戲,判斷一個人反應快慢。(版主初學時編的) 6 Z3 d2 [* T' W% w( g- _/ j
1.程序分析: ; V) w5 ]% g6 r0 f& Q }
2.程序源代碼: 6 @- ^8 O; H" `
#include "time.h" % }4 R4 l# B. G" L/ v9 ?
#include "stdlib.h"
3 I8 T2 x* r% u/ U2 I#include "stdio.h"
/ l: w9 w' b- P! C# kmain()
/ |) K7 G( u" O6 ]5 e Z{char c; - E, X% ]1 M& Y" l4 U
clock_t start,end;
4 J9 y' J' G5 ^4 V6 x' Q' {time_t a,b; ! K% @7 V" F! T4 g# ]7 ~1 q
double var; $ z$ r5 S" `7 _8 e2 V/ H
int i,guess;
- W2 ^( w$ `& l5 Ssrand(time(NULL));
& N3 |; K' D1 m' xprintf("do you want to play it.('y' or 'n') \n"); 5 m. k: {5 ^+ X E7 L( d
loop:
0 s5 G9 c' E) [+ Gwhile((c=getchar())=='y') ! _& E& L# \" _5 A% E
{
4 \) U% m0 l7 m2 u" x' R6 ei=rand() 0;
$ o7 F% \4 q8 mprintf("\nplease input number you guess:\n");
2 T" R0 ^) b* d- D! ustart=clock();
' F+ f6 b2 C; t Ma=time(NULL); ; d0 I. B) e( a. t _
scanf("%d",&guess);
: d6 R5 j8 a- R* }, Z/ b9 Uwhile(guess!=i)
/ W( }5 r$ a' t# p{if(guess>i) 4 Q" L) g" z4 x1 K/ m* ]$ M
{printf("please input a little smaller.\n");
% Y3 C: Z7 i: y/ }: U* R1 l# `" Fscanf("%d",&guess);} # w& T1 k9 U! ^, w: t6 n, P
else
- ?% M8 I Z( v. y. x) P: [{printf("please input a little bigger.\n"); 4 a E, z. {2 }- p s
scanf("%d",&guess);} ; w' N% {- A! y# D) g
}
3 ]& e m8 }4 L3 ~2 [end=clock(); 0 R0 f% g- \" x, H$ m. A2 P
b=time(NULL);
2 Z% ^# X1 r E) q1 r. |printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); ' P# e, e$ c; X
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a)); 5 p& C% u+ K# x0 o
if(var<15)
! T/ l# K, l7 ]! \9 L8 o- Dprintf("\1\1 You are very clever! \1\1\n\n");
# j* I5 L- r/ E! Ielse if(var<25) ( v' q/ t( \ [) k; j3 z3 R) @ K
printf("\1\1 you are normal! \1\1\n\n");
2 H8 W/ O! A8 T2 |8 K% Belse 3 c3 ^+ M) p" r. b3 }6 \ p: X
printf("\1\1 you are stupid! \1\1\n\n"); / @- s! }; s8 Y2 U& ]1 x
printf("\1\1 Congradulations \1\1\n\n"); + U+ N# h- ]7 b. t4 s- h$ R' R# x
printf("The number you guess is %d",i);
$ c S+ W D A' P}
1 q N- _5 V9 r" i% F; Eprintf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); : |2 ^6 D! v k: N1 b
if((c=getch())=='y') ; T) J" w/ M; T5 q* T1 i4 g
goto loop;
" V( k/ l& v( f} 8 B3 Z8 Z; b1 t1 {1 W9 a! ]
|