【程序87】 4 s2 {9 D- V' M
题目:回答结果(结构体变量传递)
& V* F5 R" V& |, L9 I1.程序分析:
1 ]- P. _. b! ?9 `; s2.程序源代码: * U; A6 k" u. v9 y9 f# e. Z7 T& n
#include "stdio.h" 0 _$ J1 h6 n h6 q* \, e% C4 N
struct student 2 f w! n6 M( U! k
{ int x;
- B6 p) B/ T T7 Q' t8 w$ Echar c;
3 E0 p# ~3 {. P' A! ~% q- O, w3 A} a;
* ]% Q, p$ I) }7 G" d% N& Nmain() . X. E3 W& u6 \ a! }! N, U, V( L
{a.x=3;
" d/ o; I( \( ka.c='a';
2 J2 G" D6 U4 F" o/ G& b" Yf(a); . N4 o9 P0 G: a; [- ~
printf("%d,%c",a.x,a.c); & U$ d$ d7 j! c: S$ R
}
' A( E2 X: v: T. l* k6 g& q% Mf(struct student b)
# y6 P. ^/ c) A2 Y J9 h4 x4 G) ^{
4 N4 }& K) S) h. w, ?* Tb.x=20; 9 h6 d, }9 V* s5 J1 ~& u% f! s
b.c='y';
/ v, Z) b4 ^- t! N: { Z. z6 C} # p- X/ I0 F' `7 H
==============================================================
3 }1 r/ V3 D! E0 @【程序88嵌入式信盈达企鹅要妖气呜呜吧久零纪要】
S0 x7 @6 b& {# @) D3 k题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
& @) t) K* S, L9 e" Z1.程序分析: # a9 T, R; N0 K3 L
2.程序源代码:
$ V" r( _3 P( R) B( Smain() 9 q" J' ?8 s) L$ x9 t
{int i,a,n=1; % [7 D' R3 Q2 C3 ?7 @ l! K
while(n<=7)
# d/ M9 X) \ U{ do { 2 P# ^3 R0 Z0 W f
scanf("%d",&a);
* H; M, L n/ M0 h; v! s8 N}while(a<1||a>50);
4 C E5 S _9 d6 U% d( W: ?( Cfor(i=1;i<=a;i++)
3 R& q& S. i# s* j1 V, U8 xprintf("*"); ( h+ Q- q4 @) Q6 t
printf("\n"); : w! J- {& @* e( i& _& V6 Q! w8 r
n++;} ( J) x( X( o) V7 }" t& r
getch(); / p3 ] f9 v: A3 {
} 6 c) x2 q% X. _7 d k
============================================================== ! r% Y7 I/ D, v( m- y0 |
【程序89】 ; M2 ~6 N8 C: Y& d3 X& B- k* {
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: % M5 X$ w- G% [6 _/ K
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
' p n% Y0 ~5 R* o1.程序分析:
A# ^' m8 d" \. O2.程序源代码:
: T2 ?3 Y& D- pmain() |4 Y% _1 \7 `1 C
{int a,i,aa[4],t; ~' B) ~' D$ `+ M7 M
scanf("%d",&a);
9 U. |% ~& R+ L/ {2 vaa[0]=a ; 1 C3 d" g9 s, M9 X* I ?
aa[1]=a 0/10;
# \6 W& c, }* N: H0 [' f& maa[2]=a 00/100;
) d m5 K7 Z+ j: faa[3]=a/1000;
3 Y7 D. s) v: R3 t) Sfor(i=0;i<=3;i++) ( l& o+ X, i7 { B; N
{aa+=5;
5 v* H, {& g$ r% J8 y! d( } gaa%=10;
+ Q+ p. B5 L! ~# q7 r}
, I6 u0 o/ {' B. P2 M$ t K Xfor(i=0;i<=3/2;i++) % S- c l* J* _4 B) x6 S& w
{t=aa;
/ X* H- n( U6 i; g2 B3 f# S/ z7 s7 zaa=aa[3-i];
( ] }8 S7 W. }- c$ Waa[3-i]=t;
. n' D8 u( p+ a7 b* t} $ N; e7 |7 x0 o L0 z+ G
for(i=3;i>=0;i--)
% ]4 P% d' L5 ], ^" ]" g, R mprintf("%d",aa);
5 {# L' d+ [3 {7 k5 h, b}
+ m3 s4 ^ y/ ]& v. t9 F============================================================== ( N* h6 }0 U9 l) D% W
【程序90】
t! b$ N) ]+ R0 f, P题目:专升本一题,读结果。
+ g Q/ @# P- t. n1.程序分析: ( d# Q- s3 b& R x% b& G; f: B
2.程序源代码: ; ~6 u6 E2 h4 z* D
#include "stdio.h"
Y T3 z) E& K8 u% G#define M 5
5 r0 [- q2 D; g, x, {/ Rmain()
; L6 d6 n% B& Y{int a[M]={1,2,3,4,5}; 9 J# D( e- j- u1 a! D1 q
int i,j,t;
! E! w0 ]( n1 p+ p9 Xi=0;j=M-1;
) X3 a0 E' y0 Mwhile(i {t=*(a+i);
2 S6 x0 ^3 \* j! U, \*(a+i)=*(a+j);
2 `* [# y, h J& q1 W/ n9 Q* {*(a+j)=t; 4 m2 O5 L: P, [6 r6 m8 v
i++;j--; - k: V ~$ f/ f
}
9 c4 x# T! a1 m3 a, `for(i=0;i printf("%d",*(a+i));
# V- l, O3 i/ O6 B% L+ n% ?}
4 u0 i( t2 b2 X6 S3 W4 b; @- F( t【程序91】 C4 B7 Q: |2 C8 j: j! u) l1 f
题目:时间函数举例1 * M7 x1 @) _6 y& a8 {. E
1.程序分析:
; a& z2 R( |! B) \0 j2.程序源代码: ; X3 B& _' k4 y& ]- N
#include "stdio.h" 0 Y x# Z& z4 n! Q2 @" ~6 X: N
#include "time.h"
0 J7 z. o& E; [. w3 b0 W Vvoid main() 8 s$ ^2 {; W7 ~4 A/ c
{ time_t lt; /*define a longint time varible*/ 0 u2 E7 X) @3 w4 o8 E7 C9 |4 M
lt=time(NULL);/*system time and date*/ # T& P9 e; C% _! D0 v- ?, z
printf(ctime(<)); /*english format output*/
4 U! m; c. g* Zprintf(asctime(localtime(<)));/*tranfer to tm*/
( L' [7 d# r5 u7 f) L( G5 O6 lprintf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
* y8 d& L: p- l+ s3 D) d1 O}
' ~4 q; L6 F" }5 C' }; X==============================================================
9 i6 p$ S5 R: f* K2 q【程序92】 - U7 M1 @' P, M% x- I& q% ^
题目:时间函数举例2
2 {% H. q' ^9 @) C1.程序分析:
- I3 o. d, P6 H2.程序源代码: ! q4 M( V4 b4 w
/*calculate time*/
1 M6 ~' F; O# |% @4 f9 H#include "time.h"
0 q* C* T0 F8 q0 q4 L* x0 b# C#include "stdio.h"
+ s- M& i+ D; x! h% I/ k7 mmain() ! H# j, s3 W7 W
{ time_t start,end; " N: p# n; q8 r$ ?
int i; ) I& I5 e/ k) ]- n5 O4 f
start=time(NULL); w2 W# c: ~, d# F/ @
for(i=0;i<3000;i++) 0 q( Z5 i; z' Q( J$ `" S
{ printf("\1\1\1\1\1\1\1\1\1\1\n");} ' I! v4 K! e, C3 a5 @+ E
end=time(NULL);
( E/ j1 _* z6 {printf("\1: The different is %6.3f\n",difftime(end,start)); - |) d& G, p( S: f9 n! X3 e1 {
} & K( T M2 _6 @. n
============================================================== - G4 h; \$ ?2 }' c- o
【程序93】 * t" f+ Q/ e1 s
题目:时间函数举例3
% s6 e$ P6 e( G+ ]7 S/ H# B1.程序分析: 0 G; a/ T. J/ ?9 z7 P9 m
2.程序源代码:
9 ]2 [7 H; |2 ^& Q. H/*calculate time*/
, e7 I' X6 @7 u# w0 |7 N% ^- ^#include "time.h"
5 {) F: A, A, Y7 q1 d5 P#include "stdio.h" 4 V& S! D) _& ^
main() 0 @# Y; O' S9 r; i- Z
{ clock_t start,end; ) S0 H# D4 L, R/ J7 d+ V+ m9 f7 P
int i; & Y. A) _' S r9 C) \- h* F
double var; . A1 i# p# g, m: L2 u( t
start=clock();
. B g: G, Y1 `" A# l" K& p: cfor(i=0;i<10000;i++)
3 ?- g: A- G; o2 x{ printf("\1\1\1\1\1\1\1\1\1\1\n");}
' W- }) b! R! O/ ^( Z" T- q9 B; rend=clock();
7 ]* ]5 f3 F0 s; n1 G1 {printf("\1: The different is %6.3f\n",(double)(end-start));
1 U: v/ u+ k4 N* Q7 x} 5 ]* A/ o1 _% R& L0 G
============================================================== & s1 _* l. I5 N3 Q5 q, O
【程序94】
& d* X/ D1 n$ f: B1 ^$ \题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的) , e1 N3 O+ O2 ~
1.程序分析: 4 K6 X: U7 l9 A6 U
2.程序源代码: 0 K: |5 C! O/ r, G6 n
#include "time.h"
3 X' C' t5 c* u- V) i#include "stdlib.h"
% [9 V$ k# s; s4 v1 u0 i: W. U#include "stdio.h" 5 j& F$ I& c$ A7 u! [& M- @4 j2 Z
main() + ~( x$ t# i3 A3 T
{char c;
6 ?, j; ]+ ~3 R* f& c mclock_t start,end; / ~- Y H# x( U5 F! g2 c0 ~
time_t a,b;
! _" t: q6 K* H4 {7 ldouble var;
- L2 v- t& b; R' G, ~6 _int i,guess; ) T0 q" T( i F
srand(time(NULL));
' R' o3 V6 |: Z: c rprintf("do you want to play it.('y' or 'n') \n"); - M e! v- O# M9 { k3 B; _" _
loop: 1 v6 I. f. p A* g
while((c=getchar())=='y')
, u1 ~) m5 k2 c5 F{
- \( g+ J- C: K5 G, Y7 E" _4 Vi=rand() 0; 3 A/ N. i, Z8 x, i5 G. C) O5 i$ U
printf("\nplease input number you guess:\n"); # ]& J& T/ R: z' f3 Z% d3 P$ X6 |
start=clock(); 1 J7 Y% ?* B% X2 h5 E6 | _
a=time(NULL);
' i* T; @- N+ U3 m! B. tscanf("%d",&guess); 7 X9 A& O2 {; X2 u
while(guess!=i)
" o$ S! L4 i' u* ^ s{if(guess>i)
& ?9 H Y& Y1 W0 }{printf("please input a little smaller.\n");
; F: P% i3 p( J5 Pscanf("%d",&guess);}
K( v! N" j9 p5 [6 uelse 8 s5 A. N$ o3 Y, C4 G( T
{printf("please input a little bigger.\n"); ) \: u" G8 l5 S: r
scanf("%d",&guess);} 9 N; e' e# \" {
}
. ]% m2 a5 I' ?( G* g. ^- n% Fend=clock();
" n' j( ~: ^4 Qb=time(NULL);
6 s' i$ I5 e) k9 V" O, oprintf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2); 4 f0 S+ t* I K2 X
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
3 i2 T7 r! n4 I. V/ O* _if(var<15)
+ ]$ i% s8 k3 D3 cprintf("\1\1 You are very clever! \1\1\n\n"); ! b2 b Z, V# U! p- j
else if(var<25)
. Z% n& s7 m' }4 P! I& nprintf("\1\1 you are normal! \1\1\n\n"); / @# M5 a& H7 o; y9 ? G; w
else 7 @. |7 @* i5 H( U" Z1 f
printf("\1\1 you are stupid! \1\1\n\n");
* Y) |5 y$ ]6 u: _printf("\1\1 Congradulations \1\1\n\n"); , @/ L1 v) \- g" {3 _- W$ T
printf("The number you guess is %d",i);
8 t' {. s: S+ A5 j+ T; P1 L: E+ z, |} 4 d& h$ g' o m) {/ K1 C/ `
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n"); 1 ]2 ~! h0 ?* G
if((c=getch())=='y')
3 f: R+ Y) \4 y9 z! F" zgoto loop;
0 d7 @( V- |* u) v$ L4 r} # t' a' f) R2 o: [7 m& ?
|