【程序87】 / g( j+ X8 m3 M0 d3 Q
题目:回答结果(结构体变量传递) 8 T P1 o+ o& V% F. d8 p
1.程序分析:
`5 O, u, W4 A+ \5 \ c( z: J2.程序源代码: 1 w: F; m4 Y) Z; w
#include "stdio.h"
7 k% |1 r1 @+ Y, y1 E( Tstruct student ; p6 V! N8 @" z; z$ |
{ int x; : W3 g% ]3 _' a% G: {
char c; # F& G7 \" j& o
} a; * o) S9 \- e: n2 r' a2 l; U6 Y
main() q2 ~0 X6 t8 g+ `9 `' d: `" T) O
{a.x=3;
' i5 h$ g6 Z: x) L6 Q; E$ C& ]0 @+ na.c='a'; " N) D# s/ t6 W6 |
f(a); : r+ w# u; z3 y. S4 a
printf("%d,%c",a.x,a.c);
+ }& M& \1 d/ _} ! l3 l# H6 y# z: r
f(struct student b) ( j$ L4 ~" {+ u
{ ; r' f) Z) o' m' i# t. \6 Q1 [ h+ S
b.x=20;
3 j% C) W: y( D# G' a4 w6 ]b.c='y';
. r) f& E$ Q1 ?! O}
# y, E* z$ m- z3 C: Q==============================================================
. h/ p) b5 G8 `2 M7 F+ @【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】
# U% c; Z+ `1 }; Z6 ]7 S5 v题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
, T! ]6 Q$ H# {1 {* E0 r3 \ W1.程序分析:
1 o I: y/ E) d. p* i2.程序源代码:
- M# @, [9 R/ C8 O6 G* a0 Dmain() ( F4 R, {& u% L- X/ @
{int i,a,n=1;
* @* Y r. N, v* c7 K4 ?: Mwhile(n<=7) 4 [& z0 w7 G" U( b8 [- W
{ do {
+ p7 j/ ]+ {$ w2 fscanf("%d",&a);
/ K6 l: E' Z1 v0 s! G}while(a<1||a>50);
- A! \- }# i7 gfor(i=1;i<=a;i++)
% H- ?& g7 u( b& I. Rprintf("*"); # @$ \# A- Z- n+ p1 _7 F# ^% `2 B
printf("\n");
8 ?/ ]% ?& {2 Q$ F6 _ d2 z& m" {' wn++;} 7 q0 R( S- b; [+ f! C! J
getch();
; g0 C: W& }" ]/ y W% j2 T7 j}
3 ^4 p; q! A, }( {; z==============================================================
4 M" z G: Q3 [; E: x* d【程序89】
% j3 G# k, G5 L1 n! C题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
|$ z+ S% Y( p% m5 W1 p2 B8 P% Q每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
- @/ r+ e/ g f, L1.程序分析:
7 Y4 q: K1 b) @8 _! M: G2.程序源代码:
" c9 `+ @$ d; o+ @main() - j: z9 L5 k: r, H% J
{int a,i,aa[4],t;
3 d& H$ s& w$ S* z% S2 ~8 bscanf("%d",&a); 9 }; a; S! g9 C
aa[0]=a ;
. C: {: }, ?% `% K) daa[1]=a 0/10;
M1 |7 A& i l% n1 `aa[2]=a 00/100;
# \1 e t8 y* Naa[3]=a/1000; % h+ Z+ y9 l- x7 x
for(i=0;i<=3;i++)
" ], ?- ~' v7 w, I$ t$ }4 E* Z{aa+=5; - i) V3 i( H4 x2 ?7 j) G9 Q
aa%=10; 4 I) v9 v2 [! H! g$ j
} 7 P# J* k; c& e# ?) j' k; P0 u8 ?
for(i=0;i<=3/2;i++) * @7 W3 n0 e9 A! F$ n
{t=aa; 0 J5 c* v' E% `# ?( h- L$ T
aa=aa[3-i];
& D2 C4 T1 B% T$ w) H2 x. @aa[3-i]=t; & A( U% ]- v$ c g+ N& O
}
# C2 j8 D8 e2 mfor(i=3;i>=0;i--) 4 C; J- y. J' }) j
printf("%d",aa);
2 ]/ O3 s0 D, ^) O} 1 Z+ T& ~! V0 D; y
==============================================================
: P$ c2 j7 Y7 X- w0 \) C2 P# @1 B【程序90】 1 ^+ E# T* B& I" L! d. z4 o/ p3 n
题目:专升本一题,读结果。
2 r% m4 m. R* m1.程序分析: , C7 V1 \; k6 w3 d# L/ }8 p
2.程序源代码:
$ V% G0 [ ^, a" y9 l# w1 x' y" b#include "stdio.h" 5 w+ x( Y4 [ L; ]' L
#define M 5 ; D z2 E i6 \
main() $ i5 e. m+ z0 E& {
{int a[M]={1,2,3,4,5}; 5 T+ `/ [- F- x4 u# p+ N& F
int i,j,t; . x) h3 w8 b' Q% R) v
i=0;j=M-1;
) ]$ c. l4 \' O; s2 V- cwhile(i {t=*(a+i);
# W3 ?# @: x. P1 k2 p/ ~ r2 R*(a+i)=*(a+j);
, b, @: S5 }' L*(a+j)=t; * e+ `5 K8 f# p. R$ o2 ]
i++;j--; ( c* F0 D. h3 T. i6 [
} . y; k6 X/ p8 C Z3 [
for(i=0;i printf("%d",*(a+i));
+ q) A; ~' J* R1 _. x3 X" r5 H4 h}
7 z" l, i7 R7 I, x1 f# A9 ^1 F【程序91】
# b# q; u& P/ M) M0 `题目:时间函数举例1
2 Y! c; p" y: j1 H, I1.程序分析: # ` J) N8 I6 A# W, E2 R
2.程序源代码:
% [3 J7 X7 Q8 Y! O+ u#include "stdio.h"
/ @& H6 s; X1 S: z" g# ^# c#include "time.h"
1 ?* M; r* L9 v' {! vvoid main()
6 l; Q" I1 p9 e{ time_t lt; /*define a longint time varible*/
( _6 g }! f O& d6 K+ I; \( A) _lt=time(NULL);/*system time and date*/
+ |2 v' Q8 }7 p" H8 D0 cprintf(ctime(<)); /*english format output*/
; [5 z0 h# ?5 W `, t( q2 ?/ Qprintf(asctime(localtime(<)));/*tranfer to tm*/
7 r" ~* L1 M5 L" ~printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ 7 b' a% ~* ?. X! r5 ?+ A1 a' x* W
} $ c1 W# H: ~% y" P5 I3 u5 H+ l
============================================================== ( d% }% n! c9 }# u( l
【程序92】
; e: l# ]; ]0 e5 Z( k6 J: F题目:时间函数举例2 + B5 D$ u8 p0 c! S$ Z
1.程序分析:
$ U: F2 s8 Q- N6 L; f2.程序源代码: ' a! s5 F& |' c K
/*calculate time*/ " \) K) u- j) b* ^/ j
#include "time.h" 8 o5 }4 S: f% G/ d
#include "stdio.h" * m" _4 t) t$ h8 {" d+ S- i; i
main() ) e7 E& C9 a& O+ t! [: P
{ time_t start,end;
8 [& s0 ?# N" |# @' J3 Vint i;
7 \, \5 |- k0 Hstart=time(NULL); & e5 ^& |3 L. I9 |& h8 O
for(i=0;i<3000;i++)
$ Z) Y( P: n) P. L: d{ printf("\1\1\1\1\1\1\1\1\1\1\n");} 1 \6 R* \2 [# C: o
end=time(NULL);
6 X7 {% I' W D& {printf("\1: The different is %6.3f\n",difftime(end,start)); & L* T: T4 q7 Z5 `% g1 @
}
$ p% I b7 Q1 g3 |, b============================================================== / n2 m+ Q9 i% a$ p3 z! ~! b
【程序93】 1 ]1 h# k" k# ?5 e9 B
题目:时间函数举例3
$ V, g3 \, |6 p, b9 {1.程序分析: 3 z, G& Y: q7 `6 r" a! |
2.程序源代码:
: ~& r: Z0 j5 }/*calculate time*/ % y+ n e1 ]* |
#include "time.h"
/ g1 [) H/ M Z/ F* _7 Q#include "stdio.h" , E2 C$ [3 e: \! A9 b' ?. H% D
main()
4 b7 b: h! n* p, C+ K{ clock_t start,end;
" m' X. K6 k6 lint i;
$ m5 ~9 m8 b& m6 X! B' qdouble var; 2 t' `' Q) ^) Y( }" I+ H# M
start=clock();
% z0 J# X4 U6 [4 G" mfor(i=0;i<10000;i++)
5 {& O7 ~! T- P' n$ z5 W{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
* n, U* g8 q) o2 Eend=clock();
- H1 a% \; u9 V7 l# r$ D4 c: c7 r$ \printf("\1: The different is %6.3f\n",(double)(end-start));
6 X/ |4 n% T0 L& K7 A( @' B}
- r8 q, \! J) H/ `============================================================== $ @% ]' k" @. o- {8 o1 n" K7 a
【程序94】 ) H2 s7 n c, i/ q4 [
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
: N2 f, f) G9 e$ Z1.程序分析:
; _+ N1 F' j# O- v! e2.程序源代码:
& B! n1 t, P+ {: L% ?! g! E6 e7 l#include "time.h"
+ G# s# ^8 e, \( W7 W4 V+ m X( R#include "stdlib.h"
. @0 Q* S3 f4 ], G#include "stdio.h"
7 w5 @+ s3 O+ \) i$ s" [. emain() 0 s6 w9 J) F. }4 k/ `: F( [8 c
{char c; " E8 [! Z( x2 k7 T s% |
clock_t start,end; 9 G; F$ Z* n$ \1 M0 J7 I# L- j- J2 m
time_t a,b; " o; G1 ~" S8 |( c
double var; : C) U. K+ A9 P" A' ^
int i,guess; ' p6 F; E) i1 {% N1 O( l5 I
srand(time(NULL)); 2 M1 X/ Q. A7 v8 X
printf("do you want to play it.('y' or 'n') \n");
l" n/ h5 z& |1 u# |loop:
. N! M0 g9 N8 A" Owhile((c=getchar())=='y')
. @8 `1 X% G# z7 X{
8 x2 U/ G# l6 w+ @2 f$ Vi=rand() 0; f& W/ k: E% g9 j
printf("\nplease input number you guess:\n");
2 A( i. {5 s5 a7 V) T; {* [# Sstart=clock();
/ |, A$ k- M8 o* v2 ?a=time(NULL);
( G- w/ A; z& Y/ J+ Mscanf("%d",&guess);
# n: C( J* M4 T+ F. T: x7 mwhile(guess!=i) , S2 {6 N- Y' A; ?. L. ]$ ]
{if(guess>i) . N, S$ [. Y& y6 t) K9 Y. S, f
{printf("please input a little smaller.\n");
( l" m& {+ `' i$ O) v3 X+ V6 ?scanf("%d",&guess);} 1 @( Q7 t+ p6 i4 H
else 8 l" C/ ~/ _7 ]6 [& ?) U% h
{printf("please input a little bigger.\n");
8 z) h& L% G+ ^, Q1 X6 D4 U3 qscanf("%d",&guess);} - W, j, J% g$ d- Y
} 2 t5 T% j' u. A/ `. h
end=clock();
. U4 @8 P j) i/ t; ^* l9 Tb=time(NULL); 0 k& n: y3 W( s6 f# k& D% x
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); 1 ? N7 ~7 u; f! N6 j5 R8 s" O/ e
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a)); 2 O- w7 j) P* J- p& V! H8 v8 I& |" N
if(var<15) 9 e8 p6 C& R6 z8 r# O
printf("\1\1 You are very clever! \1\1\n\n"); 2 h/ g/ a% t# W7 j% X. M) z
else if(var<25) 1 Z7 ]; ?" H& O' k7 `' J) ~2 Q0 I
printf("\1\1 you are normal! \1\1\n\n"); 3 ^2 f* |! m# V2 e
else & f' a( m/ `( I6 Q2 f2 @$ O7 n
printf("\1\1 you are stupid! \1\1\n\n");
& k1 Q( l* Y* p( s4 d5 sprintf("\1\1 Congradulations \1\1\n\n");
) M; r3 k; \ e& ]6 A1 Yprintf("The number you guess is %d",i);
/ U8 W& b" k' J4 h}
" z( k8 Z4 Z7 G, cprintf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); & z0 |( t% \% a8 s2 z6 t/ r
if((c=getch())=='y') * T$ G' k2 D. L0 x, h
goto loop; , Y9 p/ ?" Q" j: l( n: b) l" {
} ( e+ @) R$ S- ~
|