【程序87】 ) H8 z7 g8 P; Q) u E3 r q. l" ~7 L9 t
题目:回答结果(结构体变量传递)
0 g; F2 P6 k3 J& d- a9 z% W1.程序分析:
1 x9 C0 u9 {0 _' O1 ] m k; S/ @& `2.程序源代码: ( G1 z! {: x& b' o" i- H
#include "stdio.h" . d* }0 q5 Z1 x8 Q" k
struct student ' ?" a6 i* z" }/ u5 o Q6 l6 C/ t
{ int x; / O( B# R; d1 o1 k
char c;
2 P$ T2 R( V2 v7 o} a; 4 S a0 x3 ^: F! x. Q
main() * q7 s; M1 x& s
{a.x=3;
7 S& t \# Y! w# @a.c='a';
/ a) n9 s: J1 Y+ ?& r E9 Jf(a);
( Z3 Y8 r& q% t" ?6 ?+ m1 Hprintf("%d,%c",a.x,a.c);
+ b. y; U% H7 K' i$ l4 f0 f& V}
; R' X5 b) g( @f(struct student b)
. I) w: t7 e4 T4 n{ 9 |$ M; r, g r
b.x=20;
& |" N3 o1 s) r( _b.c='y'; ! y1 U$ i- M2 A( c, t9 O# f" u
}
1 k2 ?0 Q& C. [- [==============================================================
% N( G. O) S7 d9 h- \【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】 4 K% {0 @! f0 v
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
- k* U& m0 Q; N7 Y( e& h' \" I4 ]" p1.程序分析: % |. M2 E& X4 K% p) ^5 m$ C
2.程序源代码:
0 Y, d( q( l" P/ w" `( T5 emain()
# a& [ C) n# P0 [. a4 G{int i,a,n=1; 9 Q7 r( k P% I
while(n<=7)
2 X& z3 t1 l7 Y5 ^3 o/ F{ do { & a1 g/ w4 u" X) m) [) y$ E: m
scanf("%d",&a); 7 Q. V3 |) C3 U' X
}while(a<1||a>50);
' A3 d$ u; o8 M1 n: k/ afor(i=1;i<=a;i++)
& U% ?3 h1 t7 n- C/ jprintf("*");
" v7 \6 u5 g1 x# o, R3 p" e6 |printf("\n");
- M2 C/ B- ]# j; X& z4 Y% |; ln++;} ( W# K g" K1 P1 s8 @9 g+ r+ O8 ?
getch(); - K' P3 i p n
}
# j8 A$ n& `3 _% _============================================================== ; _6 _5 s8 h5 ~+ v
【程序89】
7 A! B0 f) }& {- C! F题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 5 _+ {( {- R; x( ~9 H
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
5 t# q; r9 L3 U1.程序分析:
5 n% c4 |. H3 z2 p: L z9 V9 G2.程序源代码:
. A3 N$ J; j3 }) U" B) ]main()
. F* B4 N" A; }1 ^0 C$ M{int a,i,aa[4],t;
% U* F. l( x# v3 O7 U( g+ b9 escanf("%d",&a); 8 n$ l/ B4 W# \0 J
aa[0]=a ; / i# z4 ]8 T2 M& ]# J2 h
aa[1]=a 0/10;
: _8 O0 G' V3 o$ u7 [ Qaa[2]=a 00/100; 7 c, d) _0 C. a6 j5 n
aa[3]=a/1000; 0 {2 a& m/ m { s' m
for(i=0;i<=3;i++) ; |# q( t' S% ~4 `# p
{aa+=5; 3 _5 N/ D% X, v( @$ I! X
aa%=10;
# f) I) T- N/ k; Q8 W+ _3 p+ Y6 z}
4 Q/ @) V1 r! |& \( Lfor(i=0;i<=3/2;i++) 2 I2 z4 H1 n2 g" K$ w
{t=aa;
5 m6 S7 L% T5 C8 m- Q3 D0 Saa=aa[3-i]; 7 ]; `) [: B# @# X4 I v
aa[3-i]=t; 8 |0 {, Q& a1 m5 v. r8 F
}
M$ ]6 P* _ r( g6 Y* nfor(i=3;i>=0;i--)
, H6 n }5 B9 X7 C3 nprintf("%d",aa); $ M; T( P- }/ ]( @: K
} ) I W8 x. K1 U& T$ U; S
============================================================== 7 K7 e0 W$ v6 U3 v: ` `+ _( i
【程序90】
8 ?% \+ t5 n( z4 G' A- u0 L* z题目:专升本一题,读结果。 5 P p: }! p2 ?( a$ ~8 K2 \
1.程序分析: ' t4 d4 _0 x8 ?2 y/ m
2.程序源代码:
: j X. |! f- b W#include "stdio.h"
4 z' a' M. |- @( b: N6 L0 T* [#define M 5 ( g& o$ O8 o4 t4 S" T6 U, [ v
main()
4 l& L ~' [7 t: W{int a[M]={1,2,3,4,5}; $ z; U- ]% b5 c
int i,j,t;
! d, S. m0 E$ ri=0;j=M-1;
& p/ @. l# [7 u r/ _0 H& R: x% k! Twhile(i {t=*(a+i);
O7 Z4 k0 F; d% L) l7 S*(a+i)=*(a+j);
1 {3 C. G1 G( ? ~7 C6 o7 H/ |3 E*(a+j)=t; . j7 q6 M. g7 A
i++;j--; ' h. @& r0 j5 V% L$ a. x9 O* W
}
4 r, W3 _4 p6 sfor(i=0;i printf("%d",*(a+i));
' i; v8 i( C( n. k} - Z. P! I" m: a5 g4 l
【程序91】 / R g% g3 A' [" q
题目:时间函数举例1
7 q# T, p$ m" v& b# n% B5 d7 v1.程序分析:
+ {5 H% l8 |) W( B3 R; _2.程序源代码: 3 P# h% N! w$ u& s1 A
#include "stdio.h" . |* P7 R& J3 k: c% B% U
#include "time.h" ( w8 m9 L: h3 ]
void main() ' o! T6 ?: P; y6 p3 a5 J
{ time_t lt; /*define a longint time varible*/ 3 r8 ~ c! U6 N3 J. \, O! N/ s# ]5 t
lt=time(NULL);/*system time and date*/
/ U% U1 _' x, y' B; U% M" [printf(ctime(<)); /*english format output*/
, \" H8 q4 Z4 {printf(asctime(localtime(<)));/*tranfer to tm*/
6 L, ^4 I ?4 y9 w7 V- G( C" oprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ $ r) y/ N" ^0 [& m9 C. `4 J' V% U
}
$ ^0 b( o H8 `5 M3 S" _# ?==============================================================
/ P) n# }6 ^ S【程序92】
' T4 T: i4 [% p( Z' A题目:时间函数举例2
& m/ X s& X5 y1 P6 M: c1.程序分析: 4 z. d' {+ a0 J( D4 P* w5 A6 w
2.程序源代码:
* s( q! _8 \) p" m% I/*calculate time*/ 2 I( Z4 t! F8 R/ g N# [1 I. R
#include "time.h"
, n" Q1 k2 N3 C" E* C#include "stdio.h"
2 ^* v& R+ _5 c& t5 s# K: amain()
& }# a' F( G( `{ time_t start,end;
. ?6 @6 B% y6 E3 k0 m, yint i; 3 |& f0 `+ @( r; b; i
start=time(NULL); ) j5 k; E/ w/ {3 w5 I/ R7 g8 H
for(i=0;i<3000;i++) + p$ R* z V3 D- n' l4 b v3 E0 R' [
{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
& H$ _& `7 w4 W8 H$ Y- jend=time(NULL);
) S+ J8 m% T, Sprintf("\1: The different is %6.3f\n",difftime(end,start)); 0 r, T- L& s2 C
}
: n# a" V7 S: f3 F3 q, C1 e3 M1 ~============================================================== 4 o4 Z% R( e( s, I+ u1 Z! L
【程序93】
! P9 F* G* u) k! M题目:时间函数举例3
! ~ G6 j- N/ B K) f( [) l9 F7 g y1.程序分析:
. O( D+ _. ~. _* T' e( o2.程序源代码: 0 L& o) h3 B6 m- o* {. L! D. B
/*calculate time*/
! T ^- Y. k. G) g+ _#include "time.h"
7 X6 Z5 W% r# p& U#include "stdio.h"
8 {( p7 Y( m+ u& qmain()
3 [3 T+ M3 s, _0 A3 @' K3 y{ clock_t start,end; 2 w! E, g @# d1 _( i
int i; - s/ _: s* W& g, [, c9 W- v/ I9 m
double var; ( e; h' Q, e6 e2 [
start=clock(); ) G& b+ I. _3 }, f0 T7 f) b" @6 O
for(i=0;i<10000;i++)
6 p( e- w+ @, z0 x. s{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
- g7 q6 o5 B5 Q+ b3 }end=clock();
7 l7 x; Q% e5 S2 a: L% @printf("\1: The different is %6.3f\n",(double)(end-start)); ' ?" ?$ }" `0 m7 u+ ]9 C5 }& g5 p
} # w% \& Z5 [9 W- k: h! U6 ^3 ^8 F
============================================================== 9 E. u/ u3 ~1 I: x, y
【程序94】 6 u. v7 i2 Y! R1 F- S- R, m
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) 1 X1 \2 ]& N* a0 s' }
1.程序分析:
|( K- T/ b, I4 T. t G- i2.程序源代码:
`: a9 ], t, y7 e4 Y7 n#include "time.h" " _& }" ~+ X# x: r0 G
#include "stdlib.h" $ h8 Y2 G+ c$ H9 T6 B6 v( h
#include "stdio.h"
- u( W& U- I& a' \! ~main()
' [% ^# r: e8 r9 N$ B; U{char c;
4 O' Y) J1 \; i0 f% Uclock_t start,end;
: B' k/ y- k+ K5 W+ O: Btime_t a,b; : l7 f6 h) Y3 B% o* a* b
double var; 4 s: i3 t( ~/ \" t6 s
int i,guess;
/ o' z: e& z, `% lsrand(time(NULL)); 9 [& y; C. J$ l! ?1 {
printf("do you want to play it.('y' or 'n') \n");
7 n {4 X) n$ r) R" ~( _loop: , z9 H. |. M5 D6 c
while((c=getchar())=='y')
O0 X: h1 a, p Z$ z9 m2 A{
' i3 U- v6 y9 v# W( q" N) ti=rand() 0;
# g; N9 F- {3 Gprintf("\nplease input number you guess:\n");
) c/ [2 H8 @0 Y8 R( k6 Lstart=clock();
1 N# G( ?( ?0 B: [* A6 w! A1 da=time(NULL); ' R5 | t. ^7 B0 P* f3 [* h: s- D
scanf("%d",&guess);
" n& H% X3 d9 }5 m3 k: Swhile(guess!=i)
, a* B9 Z4 ?. x% _1 ~* k1 E{if(guess>i) " X( T- ~- q& W/ \) H
{printf("please input a little smaller.\n"); 7 A/ m" n$ d2 a F A6 q. g8 Q
scanf("%d",&guess);} 1 ?8 e! G; T+ C4 V' x
else
6 T2 U* M c: n$ y{printf("please input a little bigger.\n"); . ?$ Y* O+ w& O% q* `
scanf("%d",&guess);} $ }& L5 z# y0 o J
}
. |, ?7 |8 k: N/ s/ n. `$ ~2 Rend=clock(); 7 ? z- q+ R, i
b=time(NULL);
f& N' g3 i8 k' _7 I, tprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
1 R1 X/ [% L, P7 C5 ~ F9 ~printf("\1: it took you %6.3f seconds\n\n",difftime(b,a)); ; v/ z$ h- Y. S
if(var<15)
' h' u7 r) j xprintf("\1\1 You are very clever! \1\1\n\n"); . m' M2 t. d/ V) _
else if(var<25)
& a2 {0 \. ~0 e9 W4 Xprintf("\1\1 you are normal! \1\1\n\n"); : q& b8 B. i( J6 m& P
else
) m0 ]$ E3 [' g0 Sprintf("\1\1 you are stupid! \1\1\n\n");
$ v# l9 M7 U. h3 p- U; q5 Xprintf("\1\1 Congradulations \1\1\n\n");
. Q4 p9 Z( K G" Q$ ?1 Xprintf("The number you guess is %d",i);
$ @% d$ V5 y- R$ E} . T$ i0 O+ j1 g5 k9 H; q
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); : I. s8 H% r2 {) c$ K0 h! @
if((c=getch())=='y')
; f9 p' h/ @; i7 O$ sgoto loop;
; F/ i3 A L! _$ w: z1 J& |3 c/ g}
. u% w% Y2 G/ S1 s1 m |