【程序87】
R. y% G: C8 {, p题目:回答结果(结构体变量传递)
2 e1 _. q* v2 e7 R; z1.程序分析: % J! U5 ~5 d( n( u/ \
2.程序源代码:
; {7 W6 ^- r8 \0 A; v; y#include "stdio.h" $ A/ R7 E5 l$ D0 n9 z6 I
struct student
/ n2 ^5 n# F" v3 U{ int x;
2 t9 e1 p: h7 Dchar c;
! ]; |4 D' b$ }} a;
) S- Z2 I0 z4 j) bmain() ( T& Q( j1 I; H/ h4 h0 S* m
{a.x=3;
9 `) {- {$ c" N! ia.c='a'; 7 J) `$ ~6 f! X6 D! y: L
f(a); 3 c, V) {. B0 L2 H* r% ?: C4 S ]
printf("%d,%c",a.x,a.c); 3 I2 t" c& \3 k$ {/ T9 ^
}
" c5 v$ {! k1 o/ o8 r; ~f(struct student b)
3 R+ z n* y* p( x7 _( I{
! j/ S: ]+ p3 j7 k& R3 eb.x=20;
) Q7 m' ~) F$ C! R6 b; Mb.c='y';
9 n+ k; h7 O7 f7 ]) ]7 B}
# ^/ V& B0 p5 d3 W1 }2 I==============================================================
) k3 M7 V+ p( L+ o. X1 `, k' s [【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】 + L; n- O, k7 h7 V- T, R
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 ; M, ^9 p* M+ N. R9 ~
1.程序分析:
! p9 z0 z& J# W. x4 y9 S& A6 a2.程序源代码: % e1 h. l! {2 ]
main()
9 A! u- }: _( _& ~5 B{int i,a,n=1; ) ^1 s8 f, D. E# u8 {; y S- q8 b5 M
while(n<=7) 4 k9 L) W& t: z0 b
{ do { t7 r7 {8 c% K: L
scanf("%d",&a); 7 w, C6 s' N: m3 q+ o
}while(a<1||a>50); $ O% l& m( }% u8 f F" b
for(i=1;i<=a;i++) : w7 F! ], K& w9 _! ]0 Z
printf("*"); $ X0 @0 e- f4 k. n: V9 s
printf("\n"); : U7 e4 r- u3 c4 r+ ?) B# T
n++;} ) c) h# q! _& j4 J/ ^' ?: i
getch();
7 C t7 p" X4 ]0 G; [} 7 O6 J' o& _" b# d
==============================================================
; d8 Y* a: M" l/ |+ F5 g9 T【程序89】 & ~. J! Z) X& W6 |* O/ s
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
' _! W8 i4 [- J/ x h9 D. z) K每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。 , ~( J& \- V$ v5 m' W% I
1.程序分析:
$ J# T( }: Y+ b3 e2.程序源代码: 9 l1 {8 y$ L% e) Q# ^6 x: _3 p; Q; X. p
main() I+ J4 j& k$ n2 ^6 ]. l4 u+ H. y
{int a,i,aa[4],t; h9 K) E N/ A: Q
scanf("%d",&a); 6 o$ O4 t! p! O. \* G4 R5 H# `
aa[0]=a ;
V4 L8 M* S2 v( ?# e, V- h" W! Vaa[1]=a 0/10;
! L( Q7 [* W; Q# _& P3 d3 v: n7 Taa[2]=a 00/100;
$ F( c$ `. E m. p# Z. o {* aaa[3]=a/1000;
% g U3 O* M/ J+ mfor(i=0;i<=3;i++) # O0 I+ J+ ]( x2 N! s8 H+ y
{aa+=5;
3 d+ W$ ~2 f5 R; Taa%=10; " B. \1 t. F( p2 {' u
} ' M6 T7 K1 { k: T2 \2 [
for(i=0;i<=3/2;i++) # c, F" W* W9 H8 W" t% _$ X; C3 S5 U
{t=aa;
4 H4 l1 E M8 X* D) p/ taa=aa[3-i]; ) i1 L P( O, `) A+ F5 `
aa[3-i]=t; ' b' U& c& {7 M3 A1 j, Z$ t
}
! t6 q% ?$ O# m* x0 |for(i=3;i>=0;i--)
- X& F% f* g5 Y+ m, Q W1 Qprintf("%d",aa);
/ U5 j7 L! x- S! J/ {6 A- S; d, s! P}
{, ^3 _" b7 X; r. {============================================================== . O9 d/ G! q+ @ |0 _
【程序90】 ! @. q: y: [0 [9 O$ y8 g7 S
题目:专升本一题,读结果。
" I" w; _7 _$ Y! V0 l1 d1.程序分析:
0 t4 t/ T- M" t/ [2.程序源代码:
* ^& J! Y- c2 s1 p* O4 G#include "stdio.h"
6 Y* @' ]4 Z& ]3 O0 M. j#define M 5
( T5 e# k q( ^6 nmain()
; B& P" S- R0 ~" O: t# B{int a[M]={1,2,3,4,5}; 5 B: v, ?) [( e1 ~) V! }. ]
int i,j,t; 9 q: O8 c" {% ]+ K! ?: b) a
i=0;j=M-1;
0 O4 ?1 X: D. M- F. Vwhile(i {t=*(a+i); 0 w6 |- t$ I, [0 f$ o* L4 ]# o
*(a+i)=*(a+j);
0 ^0 K$ j0 T, J( F* ?3 X& q*(a+j)=t; & b8 Z, ^5 d! }6 ~
i++;j--; ( y1 L! ^0 s/ L) @; w' p
} H& A6 _# q# w" q6 B, V3 G' T
for(i=0;i printf("%d",*(a+i));
1 Y0 s. d' a2 d}
3 I( ~% c7 c s【程序91】 ( q( l5 F- H. h: F4 I) y: r" h, [/ X' d
题目:时间函数举例1
& Z* m6 N2 T( K/ F6 B% a/ t1.程序分析: + F1 i+ F8 t+ \$ v& W
2.程序源代码: ?! T8 ~7 I" `# o5 L0 l9 y q
#include "stdio.h"
" D# v; c) r6 S$ I/ P: \8 E#include "time.h"
; m0 U/ d" O$ ^: vvoid main() ( S7 U, J# x5 ^' P; H
{ time_t lt; /*define a longint time varible*/ ! I+ `5 d9 A! G- v& ?$ B! I
lt=time(NULL);/*system time and date*/
! b0 f! t, Y0 V5 |printf(ctime(<)); /*english format output*/ 7 o8 s- ?1 F X0 e2 D
printf(asctime(localtime(<)));/*tranfer to tm*/
3 N2 g e" w& o* e) ^printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
9 w/ u4 e- Q8 F3 S} " y/ H2 d9 g5 I7 V4 o0 i- b
============================================================== + k5 Q& A% i: o6 q( {
【程序92】
" E! Y3 g6 @3 u3 }% r% c* l题目:时间函数举例2
/ b3 `& f. \ Q% v1.程序分析:
: v( _6 t7 S: ~1 s6 b" D. n2 C2.程序源代码:
" ]! S8 o6 {' s/*calculate time*/
- r& ]5 T+ E+ g- s2 r* w+ i#include "time.h" s1 W# H3 X" Y
#include "stdio.h"
, P* O3 l$ n6 _: _. _) G) jmain() 6 ~+ W2 Y! d* k5 j7 ?
{ time_t start,end; $ e- w/ u7 B% N* @4 P# f
int i;
% Z3 V ~' b0 \9 V0 ^3 v4 Bstart=time(NULL); , [0 `- c' K% D/ p0 U5 \
for(i=0;i<3000;i++)
/ {9 G$ k0 l% ]' U{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
m, t. e9 Z$ f# u- vend=time(NULL);
; b# o4 b' y8 z( K1 ^+ a$ c0 _printf("\1: The different is %6.3f\n",difftime(end,start)); 9 A9 w' R- {& G5 f& Y' b: n! [3 v# n: G
}
8 K: Y j3 o" o& W/ @==============================================================
6 n/ Q! R$ _$ V: h【程序93】
( ]" L# K) ]2 i. G2 D) ~, p% G题目:时间函数举例3 5 t- ^6 e. s% R4 V6 y, b3 b3 w
1.程序分析:
" M. }7 f+ ]' k6 J: ?+ [: ~7 |2.程序源代码:
, i* }* t1 ?2 g+ s6 N/*calculate time*/ u: w5 S5 R% M L5 w1 I* t
#include "time.h" 6 Y: z+ b+ d, ]! i9 p- ]3 C! o
#include "stdio.h" 6 u% V7 j- ?4 g1 f3 m
main() ) T' T' L( x. E2 F4 I0 b
{ clock_t start,end; c0 _0 g) i, @
int i; 8 Y% y/ J. X( b2 V
double var;
# }! P) _; c) e/ g' P% _/ ^start=clock();
) C0 U9 `" N- g6 Z/ Qfor(i=0;i<10000;i++) / G! U$ V0 S, E: c, K9 O1 Q
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} , o3 b1 R! j8 C7 P& H; k
end=clock(); 3 u3 \. [" I/ V' ~: b! S3 s+ P
printf("\1: The different is %6.3f\n",(double)(end-start));
) f% j" a$ J5 _: ]0 ?} 9 }! X2 P8 a- p/ B4 C
==============================================================
! P5 [0 @" _3 F2 c5 j& r5 S* h+ h【程序94】
6 d( M. j) F* E1 ]8 {+ i题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) . e) b- y! i L1 L7 Q
1.程序分析:
+ m5 M7 L1 y- Y$ v: a2.程序源代码: : i# l0 J- b9 _/ h" {
#include "time.h"
4 h: K' E; r, U#include "stdlib.h"
' ]( @$ D& J0 `( _, J& D$ P& v: }#include "stdio.h"
9 `" x+ b: r* ^0 [8 dmain() 4 g6 W/ M4 \( K0 `7 x& F% g$ Z
{char c;
9 c6 \% i' O E9 K" W6 dclock_t start,end;
' X1 F4 Z* w& M1 i8 ^time_t a,b;
$ T, k+ z) ?- R7 hdouble var; 0 q/ |8 s( j: ^1 _5 w& p
int i,guess;
6 w+ j( s3 ^3 c7 @' ]3 Qsrand(time(NULL));
( {/ s6 s5 n- \1 Q5 _$ `printf("do you want to play it.('y' or 'n') \n"); 0 C [ Y5 h/ M) h3 t( C8 x, C
loop:
4 k( I1 f8 a: V" h. k% Z( q, _& Q1 Gwhile((c=getchar())=='y')
$ Z1 @. u5 ^! ^3 y{ ; E! n, {( j# M+ G( C
i=rand() 0;
! x: Y: ~/ ^: k- Iprintf("\nplease input number you guess:\n"); : X4 E% g( A# a" Z9 W
start=clock(); . `$ ^+ |( S% {# f; |
a=time(NULL);
& W4 F! q" }8 v! S, mscanf("%d",&guess); $ v8 N: M6 b! t) W% m& L# G; g4 G$ ?
while(guess!=i)
' T: s) N' j' Z1 J6 s{if(guess>i) 0 W. X7 D2 R7 @1 I- J( C1 |, v ~
{printf("please input a little smaller.\n");
7 }. j' [ ]5 s+ `0 ^! Hscanf("%d",&guess);}
1 _& r) h4 O( celse
9 j. r% h- O/ p9 v* O{printf("please input a little bigger.\n"); # d: m/ S$ F7 k+ j% C# F
scanf("%d",&guess);}
8 x1 ]+ z; d2 g& H0 X9 G+ d8 G}
: ^9 f0 e. l1 n$ ]end=clock();
1 ^1 U) r6 T+ d; D* x# ab=time(NULL); & k- s. D. Z' g. X& l. z- D4 L% e
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); , @* S6 e+ M) l" E
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
* F6 K0 f2 e' i$ d5 Vif(var<15)
I4 ]% R9 U4 a1 I* Iprintf("\1\1 You are very clever! \1\1\n\n");
) L/ h! M L+ l* k/ K# xelse if(var<25) : o; k7 E, R8 t! s, p5 g8 R
printf("\1\1 you are normal! \1\1\n\n");
& x1 n& w- V# b: [- n$ x$ h$ w Q! \else 9 G: ~3 i7 [! q q, ]4 x3 ~( c3 j
printf("\1\1 you are stupid! \1\1\n\n"); ; o. I- s8 E3 x
printf("\1\1 Congradulations \1\1\n\n"); 9 @# \& I ?0 b, P) H* J4 o
printf("The number you guess is %d",i); 5 M' H( B: |$ O1 ~6 z& ^2 J
}
7 w% R* Z3 l v/ U; Y3 z8 C1 O5 ^printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); " C" Q0 x: e3 A9 \7 Q
if((c=getch())=='y')
& T$ Q: j& S, @* [ T: h( _: agoto loop; 9 W f/ m; B- Q, @
}
8 S4 O2 }# Y5 i6 M+ a% { |