【程序87】 9 e( x g7 U4 B" e
题目:回答结果(结构体变量传递)
, S% ~* ^- b3 a" F' c+ b1.程序分析:
, G0 ?; E3 p* w' x o9 x Y2.程序源代码: , M7 D9 t5 K. E3 N4 V) K% M+ a/ i7 Z
#include "stdio.h" 7 l: o: [4 r( }( ]- } K# y
struct student
5 r) ?: i# g. l/ T{ int x; / y" S/ {' u* k% X% G" s
char c;
8 Y& K8 J6 I% b$ i} a; " S |8 a1 Z5 V: E* ~) T: r
main() ! l; N/ j; t$ s2 h( O% B
{a.x=3; 1 ?# c: v9 M! T; L y5 I
a.c='a';
$ y, M4 m$ _, y8 _- B6 nf(a);
* u5 T/ g C" v2 B# ?+ e$ j, Wprintf("%d,%c",a.x,a.c);
/ N- T2 C' B& Z }. D8 a' E} 1 T& t& H& {5 O4 ?& P6 O. l6 ?! H/ W
f(struct student b)
# s) }8 g6 h. h; A1 [# W6 {{
( a1 ^3 w5 D) P+ j: q. D. F5 @b.x=20; ( U! q. u2 P8 u( U, @' j
b.c='y'; ; k0 D" n* z+ ? g9 \
}
. G) f& }5 Z. n- R============================================================== % r: W: P9 c; Y
【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】
v% P$ {2 A& O0 H9 ]2 ]题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 ; m# T5 b2 J' g3 A5 @5 o* M
1.程序分析:
# k- c; Y9 g3 e2.程序源代码:
( y' c. e/ J& i; x' f6 G# v: Lmain()
% f3 }5 f: u% W K/ }! ^- Y( r+ O{int i,a,n=1; ) h: s- z: q2 _ N
while(n<=7)
1 z5 g% D* y% Z: h( d{ do { 0 V2 T! k' @% z, t4 a( e J
scanf("%d",&a); 2 f a4 [/ f9 N
}while(a<1||a>50);
; J4 u: ~0 ~; g5 ?9 p$ q1 z1 Ufor(i=1;i<=a;i++) 5 ^+ \+ a" G9 S4 E3 C, S$ C
printf("*");
6 Y4 h2 _$ Q( G w* W# N w Z7 }printf("\n");
: C+ }1 S* v( g# m6 _' nn++;} 1 A6 o. f# H% i4 @& t. M7 C: P
getch(); , I: R. ^/ q1 }, z
} # L9 h: t. ~# A" Y
============================================================== 9 `7 K1 p. O( Q8 K$ F( K
【程序89】
1 z+ \" q! a- t- D; W3 Y! r. B题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: , y# A+ B; q. D% D: K4 W6 c) e) P
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。 " P$ c; R0 I1 I) B1 Z) Z, t9 h' |
1.程序分析: 7 c. |. P7 k7 z7 O
2.程序源代码: ; W9 U# N. b( b( J" a
main()
# R& e3 s5 \9 w8 N; T' j{int a,i,aa[4],t; 1 A% ]4 m% i2 c% |& m! v- v2 Q
scanf("%d",&a);
6 i3 M; O8 B7 ]/ c# b+ e- [aa[0]=a ;
1 m% i. q% C$ S- T' oaa[1]=a 0/10;
2 @* i1 s9 G9 X# Jaa[2]=a 00/100; 4 y$ d+ O- I* T* e4 j# U& M
aa[3]=a/1000;
( s/ ~6 S( T3 F. p" Y! V$ P" U6 [for(i=0;i<=3;i++)
5 ]# X; y: }, x{aa+=5;
4 o$ r: F* o3 W* Yaa%=10;
# ]' Q+ w( Y/ N" x$ M5 k}
( [1 \) x6 S5 g! `3 l: Vfor(i=0;i<=3/2;i++) & \9 D; I, X. f# ?, H& @5 T
{t=aa;
2 T7 S* G5 o! }6 n1 X* Iaa=aa[3-i];
9 p" }0 G3 t8 B0 @ e, e' q( jaa[3-i]=t; 3 v) h6 Q5 ^7 c0 g( X9 d
}
/ M: |; M/ ]; Y' Y: f3 ^2 Z: }2 Z+ S$ Nfor(i=3;i>=0;i--)
" u- r$ b6 ^" m, K. F0 jprintf("%d",aa);
7 t* _ R' w' M2 {& C9 z+ S6 e2 h}
* R- T8 |! M- L w" O============================================================== : P* p7 @, J. G
【程序90】
* e% ?- }0 e4 `% [3 X8 h题目:专升本一题,读结果。
) b. d2 m E/ o1 q- k1.程序分析: 1 _8 w) m$ {5 e) ?; X) A1 q
2.程序源代码:
9 |% p( q. {2 ]6 I9 n3 v#include "stdio.h"
, k) m9 P; }4 X4 j2 m#define M 5 + l9 w0 L/ b3 y7 B
main()
6 U- g7 i% u4 Y' s5 {' }& V& P$ V{int a[M]={1,2,3,4,5}; ( Q2 N) [5 o0 V, p
int i,j,t;
% p0 U/ z7 z4 ~ f7 C* Oi=0;j=M-1; 6 g& x* r/ z% a' E5 M+ c% s
while(i {t=*(a+i);
1 p2 a K5 O1 G" D: k3 q+ [/ t/ Y*(a+i)=*(a+j); 9 i+ d6 Y& \( T' F; Z
*(a+j)=t; 7 b* p6 ]1 q! d, k; L/ c" N4 `6 q
i++;j--; : Z! C& n, |" ^3 }* k% g
}
4 ], \1 U3 w& Z% Lfor(i=0;i printf("%d",*(a+i));
% f a. K3 w8 R: F4 q& X} 8 L- ^. s' m, O: G7 \
【程序91】 * ^) V! m; x6 w) N0 z1 q, C
题目:时间函数举例1
6 a. K; ^9 J0 D( I- h6 B1 a1.程序分析:
9 A) a* j8 z3 u& M i) r. a2.程序源代码:
& g" z% J! F! g5 n# Y# P% a+ Z9 i#include "stdio.h" / K G2 `- X6 P7 X
#include "time.h"
( S3 n7 ]/ u7 O3 ~0 fvoid main() . j. B1 m9 e r4 @ C4 W
{ time_t lt; /*define a longint time varible*/
& J; P+ O: Q) y0 P" Q6 x9 W- I2 Plt=time(NULL);/*system time and date*/
' Y0 M+ d8 g) f8 ~& |3 e7 Tprintf(ctime(<)); /*english format output*/ * i5 c1 k6 o3 G8 F; D* Q0 d5 ^
printf(asctime(localtime(<)));/*tranfer to tm*/
% J+ n4 B( e2 Hprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
0 ~3 X- z0 z9 }2 R- U8 u; P% t}
! h$ K8 i& h! W' }' m- `============================================================== Q. W0 Z2 ^# S+ J
【程序92】
, M W+ `8 {$ b题目:时间函数举例2
; @- m8 t% p1 X6 w5 _5 h9 e1.程序分析: # C4 ~: b8 ], X
2.程序源代码: ( p+ m. b" [% J3 X/ }: r) F J
/*calculate time*/ - [$ Z) }* K9 v& _8 |
#include "time.h" . c9 P$ ^9 N: M3 P
#include "stdio.h"
5 {9 z. U& [* vmain()
1 J7 r. w- r4 P% [7 `4 n{ time_t start,end; * f6 |4 R7 ?% t: V
int i; 3 x4 d* W5 e8 ?8 p7 e$ X, q$ @$ D5 U3 T% w
start=time(NULL); - |2 X3 j1 g' b, T, l% ]: r2 R" Y
for(i=0;i<3000;i++)
) B4 `8 _ N" B9 F- F{ printf("\1\1\1\1\1\1\1\1\1\1\n");} # e6 d3 Z7 l/ \/ g) P
end=time(NULL);
* T# r h$ S$ l2 h1 bprintf("\1: The different is %6.3f\n",difftime(end,start)); 3 v: `% T3 ^4 Y$ ~. Z; s
} ) J+ B# x3 X6 t- [1 c
============================================================== ; F5 K3 _: q/ l8 L) b1 z
【程序93】 : h& n/ Z( L* j( v- d5 y
题目:时间函数举例3
i y, A P- D/ B/ k0 `% r' N4 X1.程序分析:
! N' J5 @/ N' T7 M; G5 O2.程序源代码: 1 f- Q3 A0 n; }, a0 V2 Y& ?
/*calculate time*/ 7 e3 r# R3 e; z4 Z8 e" H
#include "time.h"
1 M& d: @0 l8 l' M D9 B#include "stdio.h" * b' z+ p; c a" s+ |
main()
1 ?+ H* V4 [+ f- e1 E# |+ K* x{ clock_t start,end;
5 ]8 z. I. z, S5 Q/ C" wint i; . k+ e' o( a3 `
double var; ! p" {2 g0 m- Q0 n9 r& Y, L1 ~- `
start=clock(); ; }' ]; B/ `. Z. _- n4 b
for(i=0;i<10000;i++)
, M) x+ A1 o" a. t$ o! l{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
: I- R( }2 U' Rend=clock(); " Q' h% g5 b5 f+ z6 U
printf("\1: The different is %6.3f\n",(double)(end-start)); $ z* \% L# e3 P% h
} 1 v! ^% H" u/ M H6 X: |# w
============================================================== # H; S% B( g5 v2 Z: S2 |* U
【程序94】
% J$ i' p) t% a) @题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) . c3 V' g# K( x+ N! [! \1 w
1.程序分析:
: d5 ], g: ?' m' [6 F$ y2.程序源代码:
, D* [, i/ d% W" b) d/ N& t' O9 K#include "time.h" ! Q" h9 |! e) h& ~
#include "stdlib.h" 2 X0 f+ h: L( p+ j* `
#include "stdio.h"
9 ]" D4 R& A) z9 m) mmain() + L- E) W( N8 h- J
{char c;
3 w! {! g2 r9 K1 R& Fclock_t start,end; ; k( M- Z8 ^! k* T5 Q. W- p
time_t a,b; $ s2 C* Z. c. e: h* ~1 S
double var;
. C; O9 d# }' @5 kint i,guess; * k* L' |2 |( d: y, u! P& j, n
srand(time(NULL));
9 M; H& M; k& Y4 dprintf("do you want to play it.('y' or 'n') \n");
& d F7 L8 v# V1 ], b( Yloop:
1 ^' w7 A7 L& X, O* J0 M# U3 bwhile((c=getchar())=='y')
2 P& m" w0 O1 k, n! l( g{
3 X: r& j& b, [9 A9 A* qi=rand() 0;
* J0 N$ ?( u( m7 F7 gprintf("\nplease input number you guess:\n"); " @4 i* P' H2 d8 f. D
start=clock();
8 a' ] Y% k6 o" e, V: i# z# Fa=time(NULL);
, U# R! y5 X+ v5 Dscanf("%d",&guess);
& A& o9 b) s+ K" _while(guess!=i) 6 f2 N- c# {% Y" A% v
{if(guess>i)
/ g# v1 z. Q! N+ l4 t{printf("please input a little smaller.\n");
! X% v# {0 _- g3 u' ?2 z5 b% _scanf("%d",&guess);} 4 E! G3 [: y7 o1 U. o7 s- \% N
else ) u0 X0 e7 f& x, N4 d
{printf("please input a little bigger.\n"); # O# o5 J: G3 g/ Q- o+ g
scanf("%d",&guess);} 9 \& y+ B0 I# N5 V7 d7 _7 S* i
}
1 r3 _' p6 Z" B8 M8 H' i" s: wend=clock(); 0 n/ r8 K( t% r* w* E* i5 b4 R# [
b=time(NULL);
: f7 `6 {6 t) Wprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
; V0 ?* D, m+ c8 g2 L [printf("\1: it took you %6.3f seconds\n\n",difftime(b,a)); 2 L' }( {& p" {
if(var<15)
a$ [4 S) p" m6 A8 R* U$ S) Jprintf("\1\1 You are very clever! \1\1\n\n"); 6 I! @- {0 x& L
else if(var<25)
1 U1 _" n/ R2 y a( _6 ~printf("\1\1 you are normal! \1\1\n\n"); 5 y- j$ N2 v Q' A& Z) E9 ~- Q
else 1 p) E- ]9 F+ N4 D/ J g1 v
printf("\1\1 you are stupid! \1\1\n\n");
2 f8 I% {0 s- F- ~& w9 }" s3 W2 b" qprintf("\1\1 Congradulations \1\1\n\n"); ' A/ U r M9 o: k* R
printf("The number you guess is %d",i);
7 ^+ F7 P5 |/ h0 }. `- a} , b# w& v" Y+ R% G. D. j
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
+ g0 @* ~' v2 {% b' w3 q' Cif((c=getch())=='y')
0 r+ v1 Y8 ^2 x+ ]goto loop;
* p& g# B( l7 V% q} 4 e. }. m! r3 J9 p$ |9 g
|