【程序87】 # h/ T. j$ z& _. O! I5 d3 ^1 g7 ]
题目:回答结果(结构体变量传递) ; q; [6 C( D/ W% ~6 Y9 w" K
1.程序分析: , G5 N5 \ p* M4 z4 w7 y. }1 U
2.程序源代码:
5 b$ G( B( @5 ^9 R |) U" h#include "stdio.h"
; v' k/ i6 L. x; D" d9 Z' Vstruct student 3 X% b" ?# @; L, n
{ int x;
0 O9 y4 ^/ F- \' t: Tchar c;
9 y' K, I3 p+ m$ z' ?$ S0 B} a; ( B8 ]' S1 ]2 X8 c' e
main()
( m. j3 V) k0 b# o3 t{a.x=3;
9 Y1 U" O* g0 I& Ja.c='a';
% c) m2 t; \, D0 P6 Qf(a); 9 Z! q1 I7 E* `3 i
printf("%d,%c",a.x,a.c); 7 `, n# R5 u/ y7 j k
}
3 u. h% \0 o0 ~f(struct student b)
3 R; @' Y" w' ~, f3 B{ ! N% `' B0 [1 Z. l( K0 P1 X
b.x=20; ; l* b6 Q" B- D" ]
b.c='y';
; }- ]! I" ]; {2 E& {}
1 t5 v. a9 F) b, P==============================================================
8 ^! V) n& U3 A1 V, ?. e( ~& F【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】
5 w% |! l4 E( a& e1 J0 y% c题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
1 v- E9 f5 z1 I- i1.程序分析: 2 `& c% [2 u" g, U3 Q
2.程序源代码:
* p! c R3 T6 B1 B# u) C* E1 Zmain()
& I5 N5 ], H6 B. s8 {{int i,a,n=1;
* N: b: i F9 W& j# ]; r Ewhile(n<=7) % I4 B9 r; I) W" J
{ do {
' t* C! k, l6 Z% R% ^scanf("%d",&a); . e5 V! g6 b9 [
}while(a<1||a>50); * D) y' E" F5 c" @! \2 f
for(i=1;i<=a;i++) % H: D0 S# ^2 E; | `4 q9 m: A
printf("*");
: k2 Y7 \) @9 T3 u9 q% x+ h# k! zprintf("\n"); # M8 {7 F) D, T$ ?
n++;}
4 z8 _! Y. e9 F# j, n2 @getch(); 9 E @5 k2 f' t8 ?
} + `% I& h" {+ R3 w _
==============================================================
4 k: b9 y; d! D! Y【程序89】 # c) X2 B9 l$ ^! r8 Y/ x
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
$ B! F+ {1 U" t; A' J& P每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
1 h% C* W! `% W' h" W8 O i4 B7 z8 k1.程序分析:
1 n( @/ I6 [9 o- q9 q2.程序源代码:
! P& c8 T& q% Z5 Hmain() + ^! l. }# J4 W+ K
{int a,i,aa[4],t; / f% Q3 B6 p' @8 M. N
scanf("%d",&a); 1 O* z, j4 \7 Y0 _9 m0 Q
aa[0]=a ;
5 E% m7 M: Y5 _aa[1]=a 0/10; & N/ o# T7 W5 ?9 x* i5 J
aa[2]=a 00/100;
# l1 y; @# D% t, z9 K! Eaa[3]=a/1000;
- {" E ?$ Q7 Q) Q) Tfor(i=0;i<=3;i++)
% N5 Q8 M$ R9 }7 H{aa+=5; % f1 |, j' F/ R5 L* h. B
aa%=10;
7 G+ l/ @. U2 m( r3 P} $ W" @/ F8 R. q9 A- X7 q
for(i=0;i<=3/2;i++) - j/ ]3 K( ]4 E5 i2 U
{t=aa; 8 x/ Z0 t6 m5 C' @1 c$ U
aa=aa[3-i];
# |' `( T9 F; D) F' Z7 Maa[3-i]=t; B" K6 r3 k I, ^+ n0 M
} ; e( T5 Z* l9 k, H! e# X
for(i=3;i>=0;i--) 5 s3 R" }& l7 t+ V8 ?
printf("%d",aa);
o+ [! T. [* S' c2 J} * V" N" A4 ]4 f; I
============================================================== + I( W" O3 l) g% a; J- Z' g9 W
【程序90】 6 H/ `8 r+ C2 B/ \% j" `+ O/ S
题目:专升本一题,读结果。
/ L+ V' P+ F/ |1 {8 n& r1 n: s1.程序分析: & z- T: f" k+ f: ?0 C5 K G
2.程序源代码: % i% |$ I7 v; @: q
#include "stdio.h" . [0 u5 c. u( M) L
#define M 5
% Q/ |7 ~9 O4 S* Tmain() + T# E' [ H R% m u
{int a[M]={1,2,3,4,5}; # V0 Y+ W8 a P& c2 S/ y' ~3 N' s
int i,j,t; 7 a4 v! G; U, B9 N% v- u
i=0;j=M-1;
& N) G [* h+ q$ Hwhile(i {t=*(a+i); : r/ p D" y5 L @$ {8 I7 R
*(a+i)=*(a+j);
) R) y) O ~' @3 m0 n/ D s$ K*(a+j)=t;
4 v" F2 F W8 K! d; D4 Z8 F, ]i++;j--; 0 @1 Z% ^( l! x. r8 e' i
}
5 S8 _6 `# g0 |0 ?, nfor(i=0;i printf("%d",*(a+i)); , d+ l9 g/ Y: V' f( p$ z3 G {: j
} 2 v" g" Q8 e% B- @7 \
【程序91】
# L: e h( @1 |$ N9 T题目:时间函数举例1 0 D0 q6 B5 f N v& \# v
1.程序分析:
6 G4 v: R. R, \2.程序源代码:
. S. @: t; N' [5 I#include "stdio.h" ; U/ e/ s3 x' m( X c
#include "time.h"
7 Y7 z. w4 K& L% |2 k- Nvoid main()
6 g& x$ @5 U8 H+ D7 A{ time_t lt; /*define a longint time varible*/
% S6 Z! T2 m3 O) G2 u5 Tlt=time(NULL);/*system time and date*/
# m2 @; N7 b& d8 j8 |9 nprintf(ctime(<)); /*english format output*/ % {5 \( Y6 g$ v) t5 u" m! y
printf(asctime(localtime(<)));/*tranfer to tm*/
9 P9 O! c) _# | \printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/ 9 F: K* O9 [; t6 o) B
} ( Y% C h3 ?" n8 H' Q6 X& O
==============================================================
. t- ] Y8 S0 |% Y【程序92】 9 L* v+ N6 i6 L% a5 K
题目:时间函数举例2 8 n% E5 w) A% S E8 t# Q
1.程序分析:
& y# | n2 o6 @ Z4 A6 L2.程序源代码:
% R: d# O! R" @/*calculate time*/ ; R [& H/ |' [ ]4 ^, s6 N/ |1 I$ J: Z
#include "time.h" 0 y2 W& }6 i5 J" P
#include "stdio.h"
8 y- M8 d5 z# Mmain() ) D- i, T8 C( Z+ _* }
{ time_t start,end; 4 D1 F# U/ \# F1 t& M
int i; ) L' h! M1 \) ~" `
start=time(NULL); ?0 q; V; }, y/ F0 }
for(i=0;i<3000;i++)
4 I* Y3 X+ e& x6 Y{ printf("\1\1\1\1\1\1\1\1\1\1\n");} t# g i7 Q8 j0 A
end=time(NULL); / [0 W8 K+ P# X" O4 U. `& V0 ~ @
printf("\1: The different is %6.3f\n",difftime(end,start));
! c9 K% F( M4 d4 n}
$ e; d- v/ @. X4 d==============================================================
( \7 q* X3 A& T【程序93】
$ e1 \3 H# X+ b/ z# |题目:时间函数举例3
4 W# o$ T% h1 I% g: ]) N$ a1.程序分析: : g2 U) Q/ D/ u
2.程序源代码:
2 h+ ?+ n. Z- Q3 k' I: `$ O/*calculate time*/
4 |" _$ F& R" P: b4 L#include "time.h" , Q1 @: [1 `5 ~4 Q
#include "stdio.h"
7 B4 T+ ?) w6 h6 Dmain()
) \4 \. ^/ ~3 w, a# r6 i2 T4 ^{ clock_t start,end;
, N$ Q, A( q' h* |" T- {int i;
5 W$ Y( C2 r, K. w: `* n" hdouble var; & [8 }) m6 l/ j0 w$ D, M8 O0 m
start=clock(); , }' Q5 y7 `1 c; |& h
for(i=0;i<10000;i++) ( H$ ~/ E4 c- W8 e' Z5 C$ G
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} 5 p4 w- V4 K0 c5 d
end=clock();
2 A4 B7 B) y9 K' ~ ?" wprintf("\1: The different is %6.3f\n",(double)(end-start)); 0 F7 V! N. M0 ?
}
x/ W2 q1 \" A) {/ t============================================================== & i# L! `, D/ y. U1 s
【程序94】
, v/ F. p5 e5 m5 s- i题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
& A) b0 P3 I! o. m# D3 m1.程序分析: ' J$ O7 }7 `) Q/ l5 c2 @
2.程序源代码:
5 T$ n1 ?( \, z" X! t& j#include "time.h"
% q* o% W2 ?/ S4 `#include "stdlib.h"
$ R T# [; `' H W" j; E#include "stdio.h"
- V/ E! N; @* D3 a9 Qmain()
- |7 x- q( n' V$ k: q9 s$ v{char c;
1 Z. \# ^! K4 w7 v$ f' Jclock_t start,end; 8 V" f% x* Y' H8 L. A r
time_t a,b; " h. k9 `9 ^1 f4 v6 i
double var;
1 O. E4 ^5 n, K# k* Pint i,guess;
9 {4 k6 \& b4 \6 S& x" asrand(time(NULL)); 4 D% |6 a! f5 I; s V7 i
printf("do you want to play it.('y' or 'n') \n");
) i. G8 ]6 G# r0 ?- Jloop: & a& u8 {; h, W( {1 P# {. j
while((c=getchar())=='y') ! R2 B4 d% i# B% S1 M2 {" U
{
# F# R& W% N: J ?5 Y, si=rand() 0;
" O" @; u/ |) n3 M& V/ t J2 pprintf("\nplease input number you guess:\n"); 8 V9 j3 q c+ G5 u9 X# v
start=clock(); 1 d) t( ~; @! G
a=time(NULL); G+ b% U1 d2 C* U
scanf("%d",&guess); ) U4 C/ V! S1 o: N% d% k
while(guess!=i) ! R7 @$ \1 I* y* |6 s) F4 s. v( S
{if(guess>i) 3 r0 {5 w5 X" y' \) t a* k% |
{printf("please input a little smaller.\n"); * { k4 P6 q1 E, Z
scanf("%d",&guess);} 5 P9 }: j9 L2 S& @2 |4 ^* e
else ! R* d8 x* P" b* ^# }# B) o# p# J
{printf("please input a little bigger.\n"); % V% L3 |. ~4 w* o
scanf("%d",&guess);}
4 a1 F8 `6 d% I2 P} 2 w$ I! Z) W% l" [; u# x ]; H
end=clock();
: v4 Q. s. v& P3 Y* \/ Lb=time(NULL);
8 O f# |1 w5 D" \printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); " D) ~9 y1 N( ~
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
- T0 D$ u! u* S+ o1 u$ J# oif(var<15) 0 ^, X+ Y% G4 y/ ]% u$ r" v
printf("\1\1 You are very clever! \1\1\n\n"); ) P0 I# b( {+ H; v
else if(var<25)
- \; D$ a# P; p( L8 Uprintf("\1\1 you are normal! \1\1\n\n");
% `( R7 L7 F; x+ velse
# }( h, X! x9 d) yprintf("\1\1 you are stupid! \1\1\n\n");
9 M+ Y" E: r9 o5 J/ tprintf("\1\1 Congradulations \1\1\n\n");
+ o$ J. | C# {printf("The number you guess is %d",i);
# `, P( n: O) [0 K3 c5 d} * V; Q4 X. ^# v/ R4 V# I
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
0 \5 Q" V( w2 d' iif((c=getch())=='y')
" p+ `5 h; r- c( x% s2 L$ d9 t% q4 rgoto loop; 3 C e# L2 M0 l* N6 N$ }+ ?( e! D$ \
}
) ?- B6 g4 {/ q6 a. u |