【程序87】
& _: ~; z4 {% n5 r# z题目:回答结果(结构体变量传递)
# | O4 c9 e' w0 A* F6 j1.程序分析: " H+ }* ]" U/ ^6 O; `
2.程序源代码:
; I2 @6 S) n( f4 \#include "stdio.h"
1 t& q. K+ O( a9 t9 ?$ @struct student
# U, S- h; O4 G" C9 i- V) x{ int x;
5 j9 d+ s4 S( B* [5 K8 Cchar c; : y* F& X# M: A6 N
} a; 0 y4 \: D0 N1 v5 x- v; K
main()
7 _: s0 [ s5 ^7 y{a.x=3; 4 ]6 @* Z. v5 C+ G+ q
a.c='a'; 4 }6 |5 h; n7 \7 m# z
f(a);
% ], l: y9 J6 p) ~printf("%d,%c",a.x,a.c);
0 b' |& t, x/ P/ L4 }# ]}
& P v4 z3 q7 z; C! a" b% _8 _& L! ~f(struct student b)
% |, l! N1 t) S& X{
9 ~$ l+ G( X! Z7 Ab.x=20;
# l. S, a# ~# {/ Wb.c='y';
3 ^( x+ X+ |' I; J} y$ Q% i' x6 C5 i
==============================================================
) W7 `, L( c+ M: u: X1 R0 y【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】
% ]- {0 O0 ?+ H题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
- c0 m/ Q) f# q; b/ A1.程序分析:
- ]! l m o! v* J, d5 h5 a8 d, r2.程序源代码: ! V4 U. H2 f t; k
main()
% h) `/ H9 m5 Q/ N r' c$ V& v! V- P{int i,a,n=1; , [* n' }6 X; o2 p
while(n<=7) % k% m: |, V4 x; N& k% ?- D7 `
{ do {
: S' D5 T1 K3 Iscanf("%d",&a); - g0 Z& `: F" n
}while(a<1||a>50);
% f* F' t5 g7 j( f: s, e* P6 Ofor(i=1;i<=a;i++) $ g2 s$ g* v/ [+ y" _! Z M1 ^
printf("*");
4 c* `' M \7 a( z2 W1 Qprintf("\n");
6 S1 e) A* i) o( _/ Fn++;} ~: B2 r# o, ~
getch(); R, ]7 A5 a' k& ^
} # Q( u% s" r( S' n5 M" J
============================================================== g# V# H2 a5 r% u$ A" X- C% y
【程序89】 2 _; u$ r2 Q T/ o6 f5 P) _
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 4 f3 X% Z- N; `
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
; U- k0 D! P5 v/ x6 P3 v1.程序分析:
5 i: T6 m% H d+ c2.程序源代码: ! Y: k: H& N; {0 O& o
main()
7 K) V( o8 F1 C" k6 X0 i{int a,i,aa[4],t;
" a n" {8 s! C6 Iscanf("%d",&a); 7 ~) I3 k# @' r1 N! Z. s6 J
aa[0]=a ;
5 ~4 }3 j% a# g/ U3 ^5 Xaa[1]=a 0/10; 4 V: R) X( ~/ a$ i7 {' r
aa[2]=a 00/100; V$ F" }6 V; l$ ~3 {- t
aa[3]=a/1000;
: i- i1 o8 n1 s0 V5 U& e8 Q gfor(i=0;i<=3;i++)
2 x4 u; Z6 G6 w7 D* t/ I{aa+=5; , ]7 b8 D4 G9 ~. [+ j6 g& l7 Z
aa%=10;
7 z( r* e9 }9 b# f' m* l} # d$ ~: Z/ n- V4 M9 ~9 D
for(i=0;i<=3/2;i++)
# t+ ~0 `( L/ V4 e. r1 e* @' n{t=aa; 1 O: K# j/ p8 d* s$ u
aa=aa[3-i];
! M9 r4 K, ^8 d( Kaa[3-i]=t;
' r9 p0 L+ z1 E" O4 j- G& I} - z' B7 d/ [" y6 j4 E
for(i=3;i>=0;i--)
' n' z$ {0 i" B+ u$ A' B7 J) rprintf("%d",aa);
4 b2 _6 L) K I) l' a+ F}
/ t" M' X( k& Q1 G' X==============================================================
( R4 H: r) [7 ]【程序90】
L6 d% O, M, ]题目:专升本一题,读结果。
) F) q6 f0 ?8 T3 D# s1.程序分析:
* B) ~- y- i4 m5 w- Z& x/ B: x2.程序源代码: + C, T7 }8 H6 X9 `2 Z% I, c1 e
#include "stdio.h"
, w" b P" g7 | z#define M 5
( W: N+ x5 V0 ~8 K, t( x7 t# Emain() 4 J& x" ~* T2 K7 R7 p- T
{int a[M]={1,2,3,4,5}; 7 I3 H1 ~* C3 g* z& r" H& v3 S
int i,j,t; R. R* U) O: y: i! P9 n+ ]0 ?* y
i=0;j=M-1;
& p$ f" p9 _% t9 e; ywhile(i {t=*(a+i); . N0 B) c" B/ l' X+ P$ a0 R' j" K
*(a+i)=*(a+j); z# o; y0 `" }0 s3 e3 S* T
*(a+j)=t;
$ `# q' k4 v9 Z- p0 Di++;j--;
: C5 ?% `. Z6 X* i}
$ E+ |7 E B2 F8 M6 F* }6 n4 U4 ufor(i=0;i printf("%d",*(a+i));
5 Y, I' K/ O: D. |5 Z}
) l& X" r! \# K8 b/ X% l* N【程序91】
& m2 I. q, L3 E1 \6 Y, [/ ]& O题目:时间函数举例1
# O& k3 u2 Y7 M; F1.程序分析: $ _9 l5 P6 u- v, [
2.程序源代码: 2 C( D* \1 P5 W w
#include "stdio.h" 5 _5 v+ m, B# V% F
#include "time.h" 6 a- B& w5 a4 X" V0 T: W: a/ ]! _
void main()
~* O1 e! w, J( v4 p8 c$ P& e, Y{ time_t lt; /*define a longint time varible*/ ; ~$ N# _6 U' n* M" r$ V
lt=time(NULL);/*system time and date*/ : p& a# ?" l" Q# L1 p
printf(ctime(<)); /*english format output*/
* A+ A( @7 J3 l2 uprintf(asctime(localtime(<)));/*tranfer to tm*/
$ U2 n" O1 w: S) k+ Zprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
, |/ i9 c( S; a; w' ~}
' W5 n4 j1 @2 ^/ i u============================================================== 3 [5 l h) |; A: j- `+ Q
【程序92】
# F( j G. Z3 _9 H/ }% `2 O9 m5 x题目:时间函数举例2 # V& O" S! d: k5 } V
1.程序分析: ~$ x9 M3 b, P+ U; G
2.程序源代码: 1 g; \5 _) G2 \ m) Q4 `9 \
/*calculate time*/ ) {# {( t' O, B+ L5 Z' k
#include "time.h" / d1 @+ z V7 m
#include "stdio.h"
4 W/ b+ u% A: f; V$ o* }6 l( umain() # L& x5 ~" a7 G2 _ |
{ time_t start,end;
: s4 R! v! s8 Q3 x3 ^& Bint i; 8 L9 q( t4 b. t5 j0 g
start=time(NULL); $ f8 ~# }% E3 U2 r3 D, e
for(i=0;i<3000;i++) E) U) M6 p0 s6 L3 u$ Y
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
) w0 t* b' T1 Y+ W7 K5 ?* Oend=time(NULL);
+ S1 ` k# d$ Tprintf("\1: The different is %6.3f\n",difftime(end,start)); / k6 q% b% R1 E) E
}
# n3 ?3 p9 D, ~) y4 I4 ^8 n============================================================== 2 t/ U8 P' A7 q( ]
【程序93】 # D9 p5 n* A0 A; p6 O
题目:时间函数举例3 6 z9 G1 S2 v9 s( ]
1.程序分析: 7 w9 ] c# Y5 r! {5 D2 r
2.程序源代码: 3 [5 N h/ B3 {! z$ Y D
/*calculate time*/
5 A0 N) `, s7 }8 i* H2 F, g8 N#include "time.h" 6 e d8 _# @# [# e. g
#include "stdio.h" * F8 V3 N: n3 ~. {4 W# j
main()
# }- D |" {2 S w: @ i{ clock_t start,end;
# Y" d1 i+ H1 e; _$ a7 G. wint i; ! g( q* T3 r3 ^8 j2 v
double var; 5 T# ~: P3 P& X w2 s$ x9 t/ z- q
start=clock(); ' G/ k* U1 I% e, F/ w0 n0 ^
for(i=0;i<10000;i++)
+ [- }6 f& U: f/ d2 N7 C- F{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
; S; M h. V: y. j: g' L! ~end=clock(); $ j$ r0 c, C; e% B/ b+ G
printf("\1: The different is %6.3f\n",(double)(end-start));
6 ~1 m+ V- H7 ~" K- Q4 E}
5 w( l# n" ]* r% X==============================================================
% N, _" o0 Q3 w: C. H" |& X% u2 L【程序94】
0 l0 F8 y6 `- o6 E' d题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) + m6 V( @0 @) t# z. i% R
1.程序分析: # B8 m) V# t: m* I9 D# y
2.程序源代码: % W8 _" S7 Y4 X" w. w% n
#include "time.h" 7 z, H7 y0 {' X; f
#include "stdlib.h" ! s% U$ F' }" J/ Y! k+ J
#include "stdio.h" + r. I2 n; B' [ w* O- W+ w
main() : g; x P( K i9 Z7 P! A
{char c;
! B3 H. `* T& T- u$ d6 Z* Fclock_t start,end; ( f- v+ }" j# Z( T
time_t a,b;
9 {: ~$ G" D8 K+ \/ F( }double var;
" g. P. O/ k1 e5 i; c$ m6 Aint i,guess; + [' t. j: f" ]" t- R! y0 X
srand(time(NULL)); 2 C0 f h9 Q! v5 G' D
printf("do you want to play it.('y' or 'n') \n");
0 v4 U# v6 O& z3 |' c& K% tloop: 4 l1 Y! ^3 w4 l! r' C
while((c=getchar())=='y') : W0 { v8 Q3 Q
{
; w/ ^$ ?, L: G K" Ri=rand() 0; $ t) Q1 }; `+ T3 i1 `( M
printf("\nplease input number you guess:\n"); 7 {3 E) r; t, N, J$ Y$ w- z
start=clock(); 1 H+ k+ d+ l- }: B. b. k1 n+ ?
a=time(NULL);
" p' m# v+ b9 W2 m* N! G$ h; j0 \scanf("%d",&guess);
- S: C+ \' ?3 ?% Iwhile(guess!=i)
$ p) v7 L4 g1 S; c' }{if(guess>i)
, t. B$ {, H4 i* i* B! ]{printf("please input a little smaller.\n"); $ D8 O q$ q) _1 v. J
scanf("%d",&guess);} 2 i+ ~2 J3 v" J1 c+ P' b
else
0 z& v7 X/ m7 U* A; K{printf("please input a little bigger.\n");
0 V: q; K0 P* F/ p4 v+ Jscanf("%d",&guess);}
7 y: g& N8 L/ B- J$ o0 Y}
\* t2 R7 w3 c! }% j6 R, g& X5 z, K& |end=clock(); % @9 G5 V; @7 _* {9 g* h- Q1 x( M# \
b=time(NULL);
% z3 S9 _( T$ `. R( vprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); ) |/ z6 r" Z5 W K
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a)); 3 B* u1 m8 b @8 f+ n
if(var<15)
8 l& `& s9 n4 F' {( I# ^printf("\1\1 You are very clever! \1\1\n\n"); $ S! K) G* k, V" S0 p
else if(var<25)
, V( R6 X: M! w4 l8 G: Zprintf("\1\1 you are normal! \1\1\n\n");
; [ {' F. C Q: aelse 3 c' ^2 v0 R# ?( L1 x
printf("\1\1 you are stupid! \1\1\n\n");
! B# t8 U0 L2 a- F( xprintf("\1\1 Congradulations \1\1\n\n");
4 l# o' {- t4 Z* G- u4 g' M9 Aprintf("The number you guess is %d",i); : |) X* D% K% v3 c; Q+ J
}
/ c: U7 o2 o$ _" Z7 @% qprintf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); - ]( s* b: V- ^; x% u3 A
if((c=getch())=='y')
) x; \. J7 Q1 lgoto loop; ! J7 K# N% V4 [" s! d
}
4 K# r/ r% j F ] |