【程序87】
4 g$ y3 E$ C/ k- Q题目:回答结果(结构体变量传递)
& P0 v* B: p3 b1.程序分析: ^( [2 L* u3 l4 c! \" m, T
2.程序源代码: # c \8 A' Y& x+ d& j; M
#include "stdio.h" S" i2 o# g- ]! Y
struct student
T$ z/ i; ~ h2 x( {{ int x;
' y' r5 j4 p1 c& z T: F2 @3 c' e5 xchar c;
# V3 l. _0 K0 V, L+ V! Y} a; - `! h" T( p5 R. j% J/ ^! a
main()
) B7 P: c# z! a* j$ f$ ]" _, h{a.x=3; % a& T' K* }& K4 H% U) [+ l
a.c='a'; ( |& z& J, U) |1 J7 V2 q# d
f(a);
. M) {7 O1 c2 Q7 t% q: W' ~8 jprintf("%d,%c",a.x,a.c);
3 [0 W, l8 T5 g% s5 o& j}
! [4 Z- E! Q: T2 Qf(struct student b) + F3 T9 `" p |9 o2 R9 b
{ # }8 g1 `7 l9 r- ^8 q1 _$ l( |
b.x=20;
+ L; B* F6 ^: Z0 _7 ^b.c='y';
# D% K& p# B/ L) b1 ^/ I} 8 U6 S6 {& s! h& j& C
==============================================================
) R0 b/ p, _5 g1 i0 m【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】 % C) k' x( j$ a$ |3 y1 W/ O4 O
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
" \" P1 [1 x- b) r8 j1.程序分析:
6 w1 A* _( W) b$ V9 C7 {2.程序源代码:
( P) v& j4 C/ j7 S5 mmain()
8 O: ~! ?( k4 d, T% ~. B# N{int i,a,n=1;
% b' C5 o b2 k. t4 w6 g* L: [while(n<=7)
) b8 _3 g2 n ]& j{ do { / c/ s$ g1 j S/ [
scanf("%d",&a); ' Y3 T& \# _2 W u8 y
}while(a<1||a>50);
+ a. p W: E8 Jfor(i=1;i<=a;i++) 5 v0 N( E( b+ b4 C5 |
printf("*");
" u7 K# U7 G+ U7 u( bprintf("\n"); ; M7 ?5 J# ?# A7 M# i7 n3 V5 n
n++;}
3 J. u3 N* G7 `$ B0 tgetch(); : Y, i) D$ A, O% L. t) x
} ! P& A; m/ |: m/ |2 Y9 @
==============================================================
+ y/ V- J2 x$ L【程序89】 8 c9 Y4 s' Y8 v' i% E E. x
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
2 M0 T4 A7 I/ y: a) j每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。 4 u9 S3 i1 M/ ]9 K( E* U8 a* x3 J
1.程序分析:
5 t( K+ ~0 i; J: w* Y- p2.程序源代码:
) c v8 D, {. B/ [! }; @main() & T; D, W9 L) K; ?( s4 v8 H* u
{int a,i,aa[4],t; : N; m% S9 V# p% D
scanf("%d",&a); - N: i/ I0 Q: q+ N
aa[0]=a ;
6 ~' x# c2 @ ~7 @7 c( |aa[1]=a 0/10; 3 W" l9 b, J/ H4 ]/ c6 \
aa[2]=a 00/100;
# I4 r2 N/ h L; Y8 b* vaa[3]=a/1000; ( t' O V V! O7 \! c3 C% g$ j
for(i=0;i<=3;i++)
1 n& A' \% j9 a& E# z{aa+=5; ' k$ Z( D" U, B) T$ X$ C
aa%=10; - @# k: @ D. y! J* S6 S! ~
}
& v7 ~5 ~0 Y$ c2 C0 Mfor(i=0;i<=3/2;i++)
& C$ L/ i" |# Z' ^1 R/ v @( E+ P) I{t=aa;
( N1 ~! T' ] g* c. f; E( D2 gaa=aa[3-i]; 4 x5 E P& F- e
aa[3-i]=t;
% p% G& W8 k: Y}
- Z I! R g ~) Gfor(i=3;i>=0;i--)
: M. b! c/ Q* T/ {/ w. {printf("%d",aa); ! w# \3 Z; [( V
}
J h* `" d4 q==============================================================
+ |" l# J5 Z: ]: ]: g+ D5 Z【程序90】 6 p; `; g/ D8 K
题目:专升本一题,读结果。 " E L+ f8 M- }4 H7 w. Q
1.程序分析: + G8 Z$ @3 Z6 J% r" l1 z
2.程序源代码:
' I6 K( C# y8 T0 g#include "stdio.h" 1 G% |) c7 @# M/ R
#define M 5
; o' `. p6 S' ]5 a4 D+ u% Smain() ( [: b, K9 y4 M
{int a[M]={1,2,3,4,5}; / f) _+ Q8 }/ E; l. a1 m6 i. P
int i,j,t; ( u- j" d: z; B8 c4 H
i=0;j=M-1;
5 Y0 }; ?* @& w6 O. [# G, Nwhile(i {t=*(a+i);
: f: {5 {: V, z$ u1 `*(a+i)=*(a+j);
! e: d2 S. ^$ P% t5 ?*(a+j)=t;
3 w6 a f3 B4 hi++;j--;
2 i7 {- |6 M8 J} 5 E4 B/ j k s# P' ^0 K0 s1 \
for(i=0;i printf("%d",*(a+i));
2 H+ |( p$ M' I- R+ v* U& w4 w" }1 u}
4 n0 O! S9 v7 b7 g) ]【程序91】 # w" ~+ @) ~3 X8 A' X/ W' z) K
题目:时间函数举例1
( i4 v! m/ d$ M7 @3 f( ?1 G1.程序分析: - H. m. A1 Q8 y
2.程序源代码:
: K% w4 A- l. v/ P- k5 p h# f#include "stdio.h"
' ~& v% r3 l' Z7 K9 z( v#include "time.h" 8 V; v8 Q1 h5 V- o. j g. ]4 a
void main() * l! v a" ~0 x- X! k
{ time_t lt; /*define a longint time varible*/ 8 ~1 G0 U8 l1 b4 E
lt=time(NULL);/*system time and date*/ / ~7 [1 s% k& }1 B v
printf(ctime(<)); /*english format output*/ / L$ R6 g% D, M# d- n2 ]
printf(asctime(localtime(<)));/*tranfer to tm*/
) P1 h# M- Y. H# S1 j5 E9 @printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
9 a: O& |; L: e: U O} + _9 P. a# z: }# d5 T, }
==============================================================
1 J2 o' G/ }- C7 a% W9 \% I: `【程序92】 : o, E, C6 l1 Z4 S: f
题目:时间函数举例2 ; B" x G! h; i# [
1.程序分析:
% k) }1 _( z+ ^9 [* k2.程序源代码: & s/ c1 L: |. [. S! q
/*calculate time*/
. ^* v8 ?8 t* Z: `3 Y, I#include "time.h"
! y0 m, J+ m' F4 |- r8 W1 d#include "stdio.h"
$ A# p# U P" r7 e- nmain()
2 U$ ]+ k0 j& @) s{ time_t start,end;
, [2 s7 }9 _' \0 O, c& {( Z ^* ~$ Aint i;
/ O5 e( L% Q7 b) H5 S& V2 Hstart=time(NULL);
' M6 _* V- k9 i9 j9 `- q4 Sfor(i=0;i<3000;i++)
5 Q' `9 z' P# }( ]; E9 ~{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
8 j6 Q5 p# w" C- v1 A/ jend=time(NULL);
5 m% K0 ?: |) l1 h$ Sprintf("\1: The different is %6.3f\n",difftime(end,start)); - R2 H. ?6 W6 `) @
} ) Q& n2 V- b. D4 g% m* N
============================================================== 2 g4 m8 b8 t+ J1 ]
【程序93】 : Z8 W6 X, [5 ]& s( l" ?: v" E. @
题目:时间函数举例3
8 T" a8 F- n% O9 {& A" |" u; c! O1.程序分析:
% t5 [5 V4 C; I9 ^0 k: A2.程序源代码:
, K7 K8 E* {$ N6 n" Q$ |# X/*calculate time*/ / D/ M" t& b. b
#include "time.h" & `' s. m; C5 ?1 @" x; d+ k+ l; B5 C$ J
#include "stdio.h"
% q! B9 L/ S; F: G9 `5 omain() 8 m2 F% ] g5 F M, r. a1 Z- Q
{ clock_t start,end;
5 k3 T4 _$ [! d- S" Tint i;
7 ^1 ~ ?& H: c$ J! v8 Mdouble var;
# r* A: ?) [% A9 _ fstart=clock();
q$ N$ Y% K, u2 B4 ofor(i=0;i<10000;i++) 8 h" H, ~ p3 G9 O
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} ' h2 S8 c# t8 ~, ^4 U' ?' \
end=clock(); ) ?& h8 j! I+ Q" m- X+ H! a; u
printf("\1: The different is %6.3f\n",(double)(end-start));
" T+ R2 P- `5 _( O( b8 y( Q}
- K$ }% p. @7 _ I3 o! {1 A==============================================================
/ ]' @/ e4 e' Y$ s: v7 T* H _5 I【程序94】
! G6 L- _3 Q% t# C题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
! C+ H8 x) H- d/ M1 h7 u/ A1 r1 H1.程序分析:
6 S& o( s; w! Q* E/ X2.程序源代码:
5 O! g' Y9 V) D; ?% ~3 f#include "time.h"
' H5 h0 [8 `' [#include "stdlib.h"
( N. ~# ]9 b6 e; L7 R L: G#include "stdio.h" ' G& O5 ~; D: k* ^9 B
main() # a: N# c) t2 W& u. P( H; J
{char c; ; X, Y: z7 Q% h$ W1 q4 J
clock_t start,end;
1 K- O) {9 i4 N- E. v" ]( ytime_t a,b; 8 p' ? ^' Q7 w+ p' W: m
double var;
) l$ C) {% S+ C/ R& [. O5 ^int i,guess; 3 ?4 n6 i) s% R1 i/ f
srand(time(NULL)); 5 U: x* j d$ R+ h
printf("do you want to play it.('y' or 'n') \n");
6 {) Z% ]; M% P3 j9 i, ~' u9 S! @' zloop: % O5 j9 m9 M9 ~0 ~# O$ g
while((c=getchar())=='y') 3 Q8 O' g o6 T. K% U
{
! `7 d T6 r- T+ _i=rand() 0; + Q" g1 t) B5 h( c% S3 T
printf("\nplease input number you guess:\n"); * c7 X$ U' d7 \' t: O
start=clock();
$ ^. s5 w; H' A, ca=time(NULL);
7 P! k+ R8 u3 \: e. Dscanf("%d",&guess); 0 @" N) @& V- s
while(guess!=i) . J9 C) }# e' ]4 B
{if(guess>i) 6 [, A$ {5 R' i
{printf("please input a little smaller.\n"); % o: h0 k, _3 l7 I
scanf("%d",&guess);}
8 j% G; b$ k! s- v: K* w `) ^else
8 g% S. b# f- d0 T{printf("please input a little bigger.\n");
; d3 F) E. \8 F3 Sscanf("%d",&guess);}
+ B* v8 n* H& U$ ^& B+ }, [}
4 Q' z# @% {, m. i3 w) \* uend=clock();
! U o& k$ U( ~% e& lb=time(NULL); - N5 H2 A- ?% U* z& W
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); % w) H; h) Z4 n( P! [& p7 d( }
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
/ b4 b+ Q4 C' q' B+ L5 t7 o) Pif(var<15) ) o) x e- B0 @0 `
printf("\1\1 You are very clever! \1\1\n\n"); # N& e$ V" C: b7 M4 T$ I
else if(var<25) ! }/ m& q) X8 y) |
printf("\1\1 you are normal! \1\1\n\n"); 4 [5 K! y6 \7 U/ Y G0 Q ?
else . Y! f: b! S, k# Y* Q7 A& S. A; e
printf("\1\1 you are stupid! \1\1\n\n");
: d) `, C( S1 g# e& V* zprintf("\1\1 Congradulations \1\1\n\n");
) j7 { j) ]! Y* Z' aprintf("The number you guess is %d",i); + Q# j+ [& w8 n
} ) d# G3 K3 d; S5 @6 w* Z6 }( @! x
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
& z+ C0 ~* j: G% }' N4 d4 q$ O! q, Eif((c=getch())=='y')
+ [5 @6 ?7 I1 z* Egoto loop;
8 I, Y+ L2 y& L) Q} 9 a- c; Z8 F5 E% O( k
|