( 98 ) C ( )
( )158 1998 C : C C C,,, C,, : C ( ) : : (, 100084) : : : 7871092 1/ 16 :18 25 :415 : 2000 3 1 2000 3 1 : ISBN 7 302 01166 4/ T P432 : 00016000 : 22 00
( 98 ) 20 90,,, ;,,, 1994, 1998, 160, 70,,,,,,,,, : (B) :,, :, QBASIC, FORTRAN, PASCAL, C, FoxBASE : (A) : ; ( B) : :, 1995, B ( ) 8, 120, 1998, : DOS, Windows ; BASIC QBASIC ;,, ( 98 ) : ( 1 ) :, ( 2) :,, (3 ):
, 500600, ( 98 ) 1999 3
, C C, C C,,, ;, C C,, C,,, 1998 ( C ),, C,, (), C,,,,,, ( ),,,,, C,, ( ) C (, ),, C ( ),,,,, 1999 11
1 C 1 1.1 C 1 1.2 2 1.3 2 3 2 5 2.1 C 5 2.2 5 2.2.1 5 2.2.2 6 2.2.3 6 2.2.4 8 2.2.5 9 2.3 10 2.4 C 11 2.4.1 C 11 2.4.2 C 12 2.4.3 12 2.4.4 12 2.4.5 13 2.4.6 16 2.4.7 17 2.4.8 18 2.4.9 19 2.5 20 21 3 24 3.1 C 24 3.2 25 3.3 25 3.3.1 26 3.3.2 27 3.3.3 30
3.4 33 34 4 39 4.1 if 39 4.1.1 if 39 4.1.2 if 41 4.1.3 if 42 4.2 switch 43 4.3 goto 46 4.4 while do while 46 4.4.1 while 46 4.4.2 do while 47 4.4.3 while do while 47 4.5 for 48 4.6 break continue 51 4.6.1 break 51 4.6.2 continue 51 4.7 52 4.8 53 61 5 68 5.1 68 5.1.1 68 5.1.2 69 5.1.3 71 5.2 71 5.3 79 5.3.1 79 5.3.2 79 5.3.3 80 5.3.4 81 5.3.5 82 5.3.6 85 89 6 95 6.1 95 6.2 96 6.3 97
6.3.1 97 6.3.2 98 6.4 99 6.4.1 99 6.4.2 99 6.4.3 100 6.4.4 102 6.5 104 6.6 107 6.7 109 6.7.1 109 6.7.2 110 6.8 111 6.8.1 111 6.8.2 112 6.8.3 115 6.8.4 115 6.9 116 6.9.1 117 6.9.2 117 117 7 122 7.1 122 7.1.1 122 7.1.2 123 7.2 126 127 8 132 8.1 132 8.2 132 8.2.1 133 8.2.2 133 8.2.3 135 8.3 136 8.3.1 137 8.3.2 137 8.3.3 140 8.3.4 144
8.4 145 8.4.1 145 8.4.2 147 8.4.3 149 8.5 152 8.5.1 152 8.5.2 154 8.6 154 8.7 155 8.7.1 155 8.7.2 156 8.7.3 main 157 158 9 163 9.1 163 9.2 163 9.3 165 9.4 167 9.5 169 9.5.1 169 9.5.2 170 9.5.3 171 9.6 172 9.6.1 172 9.6.2 typedef 173 9.6.3 174 9.7 181 9.7.1 181 9.7.2 182 9.7.3 182 183 10 189 10.1 189 10.2 189 10.3 190 10.4 190 10.5 190 10.6 191
10.7 191 10.8 192 192 11 194 11.1 C 194 11.2 194 11.3 195 11.3.1 ( fopen ) 195 11.3.2 ( fclose ) 196 11.4 196 11.4.1 fputc fgetc ( putc getc ) 196 11.4.2 fputs fgets 198 11.4.3 fread fwrite 199 11.4.4 fprintf fscanf 200 11.5 200 11.5.1 rewind 200 11.5.2 fseek 201 202 206 1 ASCII 206 2 C 207 3 207 4 C 208 5 C 212 6 (C ) 219 7 (C ) 222 8 235 280
1 C 1.1 C C, [1.1] C # include < stdio.h > /, / main ( ) / / { int a,b, c ; / / scanf (% d, % d, & a, &b) ; / a b / c = max ( a, b) ; / max, c / printf ( max = % d, c) ; / c / int max (int x, int y)/ max,,x y, / { int z; / max z, / : if (x > y) z = x; else z = y; return (z) ; / x y z / / z, max / (1 ) C main C C, C, C (2 ) C main, main ( main,, ) (3 ) ( printf scanf ), (1.1 max ) (4 ), include, ( 1.1 )7 (5 ) C, 1.1 1
, (),, ( Link),, 1.1 C 1 1 C 1.2 : (1 ) 1.1 max :,,, main ( ) (2 ) {, {, 1.1 main int a, b, c max int z, a b c z,, C : c = a + b;, 2
1.3 : (1 ), ( 1.1 main max ) (2 ) C,,, (3 ),, if (4 ) / / C, 1.1 A) C B) C main C) C, D) C 1.2 A) B) C) D), 1.3 C A) main B) main C) main D) main 1.4 C A), main B), main C), D) 3
1.5 C, 1 1.6 : 2 3 1.7 4, 5 1.8 C 6 1.9 7, 8 1.10 C 9 1.11 C 10 1.12 C 11 1.13, C, : Very Good 1.14 C, a b c, 4
2 2.1 C C : ( ) C, 2.2.1 2.2,, 12 0-3, 4.6-1.23, a d, : # define PRICE 30 # define PRICE 30, PRICE 30,,,,, ( ),,,, 5
2.2.2, (identifier), C,, : sum, average, class, day, month, student - name, - above, lotus - 1-2 - 3, basic : M.D.John, 123, # 33, 3D64, a > b,, sum SUM,, C ( ), ( IBM PC MS C)8, 8, 8,, : student - name student - number, 8,,,, 2.2.3 1. C : (1 ), 112 0-38 (2 ), 0, 0123 123, 83 (3 ), 0x 0x123 123, 291 2. : int i ; / i / long j, k; / j k / C,, ( word) int, long int, short int Turbo C 2.1 6
2.1 Turbo C int 2-3276832767 - 2 15 - (2 15-1) short[int] 2-3276832767 - 2 15 - (2 15-1) long[int] 4-21474836482147483647 - 2 31 - (2 31-1) unsigned[int] 2 065535 02 16-1 unsigned short 2 065535 02 16-1 unsigned long 4 04294967295 02 32-1 [2.1] { int a, b, c, d; a = 10 ; b = 5 ; c = a + b; d = 32767 + 1 printf( c = % d \ n, c) ; printf( d = % d\ n, d) ; : c = 15 d = - 32768 a b c d, a b 10 5, c a b 15d 32767 1, int, d - 32768, 32768 32767: - 32768: 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0000000 00000000 2.1 2.1 32767, (0, 1 ) 32767 1 15 0, 1, - 32768 ( ),, 7
2.2.4 1., : (1), ( )0.123.123 123.0 123. 0.0 ( 2), 123e3 123E3 12310 3 e(e), e, e3 2.1e3.5.e3 e 2., : float a,b; / a b / double c ; / c / Turbo C, 2.2 2.2 Turbo C float 4 7-10 38 10 38 double 8 1516-10 308 10 308 [2.2] { float x ; double y; x = 123456.7777 ; y = 123456.7777 ; printf( x = % f, y = % f \ n,x, y) ; : x = 123456.781250,y = 123456.777700, x y, 4 8,,,,,,,,, 8
2.2.5 1. : ( ), a? = ;, \ 2.3 2.3 \ n \ t () \ v \ b \ r \ f \ \ \ \ ( ) \ ( ) \ ddd 1 3 8,\ 123, 8 123 \ xhh 1 2 16,\ 21, 16 23,, : char c = \ n ; 2. (), abc \ n a, a,,,, \ 0 ( ASCII 0, ), a, : 3. a \ 0, char : char a,b ; a = O ; b = K ; printf(%c%c \ n, a, b) ; 9
OK, : a = O ; b = K ;, a b, O K2 : (1 ), : char c = A ; (2 ), : char c = \ \ ; printf ( % c, c) ; \ (3 ) ASCII, aascii 97( 1), : char c = 97; printf ( % c, c) ; a, ( ASCII ),, aascii 97, : 01100001, % c, a ( ASCII ) % d (printf(% d, c) ), 97,, [ 2.3] { int a = A, b; char c = 97; b = c - a ; printf ( % d \ n, b) ; 32, AASCII 65, c - a 32 2.3,,, ( ) : i, f float, d double, e long, : 10 + a + i f - d/ e 10
2.2,,,,, short int, float, ( float, double, ) int double, int double, ( double ), 2 2 double,,, int unsigned, long, double int dou ble, int double, int long, int long : 10 + a, a 97, 107; i f, i f double, double ; 107 i f, 107 ( 0, 107.00000 ), double ; e double, d/ e double ; 10 + a+ i f + d/ e, double 2.4.1 C 2.4 C C,, =, C : (1 ) ( + - / % ) (2 ) ( > < = = > = < =! = ) (3 ) (!& & ) (4 ) ( < < > > & ) (5 ) ( = ) (6 ) (? : ) (7 ) (, ) (8 ) ( & ) (9 ) ( sizeof) (10) ( ) (11) (. ) 11
(12) ( [ ] ) (13) ( + + - - ) (14) ( ( ) ) 2.4.2 C C : (1 ), : a = 3 (2 ), :3 + 4 2 (3 ), :5 > 6 (4 ), :3 > 5 & & a > = b (5 ), : a > b? a : b (6 ), : a = 3, b = 4, c = 5 2.4.3, : a + b > c & & c + d < e + (c = f) C C,,, a - b c, b,,,, a - ( b c), : a - b + c, C ( ),,, b, a - b, c,, (, ) : a = b = c = 4 c =, c = 4, c 4,,, 4 b a, C, 3 2.4.4, : + - / % ( ) + + ( )- - ( ) : (1 ), 5/ 2 2,, double 5.0/ 2 2.5 12
(2 ) % ( ),, Turbo C, 15 %4 15 % - 4 3, - 15 %4-15% - 4-3, 15.0 %4 2.4.5 (3 ) + + - -, C, (1 ) =, a = 3( ), 3 a (2 ), : a = 5 :,,a = 5 5( a 5 ), : a = (b = 5) b = 5, 5, a = ( b = 5 ) b = 5 a = b, a 5, 5,, (), C C :,,,,, 0 + 5,, : a = 3 + 5 a,,, 3 + 5, : 3 + 5 = a,, (3 ) =, = + + = : a + = 3 a = a + 3 13
x = y + 8 x = x ( y + 8 ) x% = 3 x = x% 3 a + = 3, a (3 ), a 3, a,x = y + 8x ( y + 8 ), x ( ), C 10, : + =, - =, =,/ =, % =, < < =, > > =, & =,=, = (4 ),, ( ),, i, i = 3.56i 3,,,,, ( signed), ( unsigned),,,, ( ), 51.213468912, 51.21347, (5 ),, a, b, c long, : a = b + (double)c c long double, b : (),, (6 ), + + - - 1 1, : + + i, - - i ( i, i ( )1) i + +, i - - ( i,i ( )1 ), + + i i + + i = i + 1 + + i i + + + + i i = i + 1, i ; i + + i, i = i + 1 : i = 3 ; printf ( % d, + + i) ; 14
4 : printf ( % d, i + + ) ; 3 : ( + + ) ( - - ),,, 5 + + (a + b) + + + + - - - i + +, i,, 3 + +, ( ), - ( i + + ), i 3, printf(%d, - i + + ), i, - i - 3, i 4 (i + + ) i, i 1, 1, - 4, + + - -, i 3, : (i + + ) + (i + + ) + (i + + ) 3 + 4 + 5, 12 Turbo C MS C 9 i 3, i ; i, 9, i 6 : k = ( + + i) + ( + + i) + ( + + i) i, k = 4 + 5 + 6, 15 k = 18, : + + i,, i, i 6, k = 6 + 6 + 6, 18 : i 3, : printf ( % d, % d, i, i + + ),, 3,3, printf (i i + + ), i + + i i + +, i 3 i 1 4, 4 printf i printf 4,3, + + - - : i + +, i, i 1, [ 2.4] (1 ) x + a% 3 (int) ( x + y) %2/ 4 x = 2.5, a = 7, y = 4.7 (2 ) ( float) (a + b)/ 2 + ( int) x% (int) y a = 2, b = 3, x = 3.5, y = 2.3 : ( 1) 2.500000 (2 )3.500000 15
[ 2.5], a = 12 (1 ) a + = a ( 2) a - = 2 (3 ) a = 2 + 3 (4 ) a/ = a + a (5 ) a % = ( n% = 2 ), n 5 ( 6) a + = a - = a = a : ( 1) 24 (2 )10 ( 3) 60 (4 )0 (5 )0 (6 )0 2.4.6 C, : 3 + 5, 6 + 8, : 1, 2 : 1, 2 2, 3 + 5,6 + 8 14, : (a = 3 5, a 4 ), a + 5 a 15, a 4 (a ), a + 5 20, 20 : 1,2,3,,n n 3,,,,, ( for ),, : printf(% d, % d, % d, a,b, c) ; a, b, c, printf, : printf(% d, % d, % d, (a, b, c),b, c) ; ( a, b, c), c,, 16
2.4.7 1. C 6 : < ( ) < = () > ( ) > = () = =! = : () ( ) () () (1 ) 4 ( <, < =, >, > = ),,> = =, > < (2 ) (3 ) 2.3 2 3 2. (C ),, : a > b, a + b > b + c, (a = 3) > ( b = 5), a < b, (a > b) > ( b > c) C,,, 0, 1, 0, 1,, 0, 5 = = 30,5 > = 01 a = 3, b = 2, c = 1, : a > b, 1 (a > b) = = c ( a > b 1, c ), 1 b + c < a, 0 : d = a > b d 1 f = a > b > c f 0( >, a > b 1, 17
:1 > c, 0, f 2.4.8 1. C : (1 ) & & (AND) (2 ) (OR) (3 )! (NOT) & & (), ( ), ( a > b ) & & ( x > y), ( a > b) ( x > y)! ( ),,! ( a > b) 2 4 : a & &b a b,a & &b a b! a a b,a b a,! a, :! a & &b x > y& & c : (1 )! ( )& & () (),! (2 ) & &,!, 2.4 2., C, 1, 0,, 0, 0,, : (1 ) a = 4,! a 0, a 0,,,, 0 (2) a = 4, b = 5, a& & b 1, a b 0,, a& & b, 1 18
,, :, 5 > 3& &2 8 < 4 -! 0 : 5 > 3 ( & & ), 5 3,5 > 31; 1 & & 2, 1 2,, 1; 1 8 < 4 -! 0,! 0 1,, :1 8 < 4-1, 1 8 < 3, < 8 3,8 < 3 0 ( ) 1 0 1, 0 1, 0 0,, 0 0 : c & & d 1( c dascii 0, ),,, : a& & b & &c a (0 ), b b, a b c c a, b c ( ) a, b, c, 2.5 a b c a (0 ), b c; a, b; a b c, 2.6 2 5 2 6 2.4.9, (), C : 1? 2: 3 2.7 19
2 7 : (1 ) : 1, 0 () 2, 2 1 0 (), 3, 3 : max = (a > b)? a :b max, a b max (2 ),, max 2.5 [2.6] x y, : { float x, y, ave ; scanf( % f% f, &x, &y) ; ave = ( x + y)/ 2; printf( ave = % f \ n, ave) ; : 2 5 < > ave = 3.500000,, 3 [2.7] 2 4 3 5, 20
: { int r,h ; float v; r = 2 ; h = 4; v = 3.14 r r h ; printf( r = % d, h = % d, v = % f, r, h,v) ; scanf( % d% d, & r, & h) ; v = 3.14 r r h ; printf( r = % d, h = % d, v = % f, r, h,v) ; : r = 2, h = 4,v = 50.240002 3 5 < > r = 3, h = 5,v = 141.3000 r h, 3.14,, v C, r 2 r r 2.1 A) ABC L10 a - b - a1 B)? 123 printp a + b C) - 12 Zhangp 11F D) Li - Li P for 101 2.2 C, A) B) C) D) 2.3 C A) B) C) 21
D) 2.4 C int short char A) 16 (2 ) B) C) D) 2.5 A) ( ) & & ()! () B)! () ( ) & & () C)! ( ) & & () () D) & & ()! ( ) () 2.6 A) 10 + + b) ( x + y) - - C) + + ( a1 - b2 ) d) ( d + + ) + ( d + + ) + ( d + + ) 2.7 C A) 099-026 0x123 e5 B) 0034 0x102 13e - 3-0.78 C) - 0x22d 06f 8e2.3 e D).e7 0xffff 12% 2.5e1.2 2.8 A) \ n \ \ \ x35 \ B) \ t \ 1010 \ v \ 123 C) \ x110 \ b \ g \ xxx D) \ rr \ r \ 55 \ xff 2.9 A) chr a B) 123 \ C) string S D) 678 \ 0 2.10 A) a = 10 + b - - = 9 + k B) (a = 16 9, b + 5), b - 3 C) a = b - - = c - - D) a = b + 7 = a - b 2.11 a 1, a 2 2.12 32 + A - 2/ 3 5 3, a = b = c = 6 + 2/ 5 4, 22
b = 6, 18 + ( b + = 4 ) 3 5 2.13 a = 3, b = - 4, c = 5,! ( b > c) + ( b! = a) ( a + b) & & ( b - c) 6 2.14 a = 3, b = - 4, c = 5, a + + - c + b + + 7, + + a - c + ( + + b) 8 2.15 a = 3, b = - 4, c = 5, a + b, b 5, a = b + 4 9, b% = c + a - c/ 7 10 2.16 11 { int i, j; float a, b; char c; long m,n ; i = 5 ; j = - 3; a = 25.5 ;b = 3.0 ; m = a/ b; n = m + i/ j ; printf(% d \ n,n) ; 2.17 12 { char ch; ch = B ; printf(%c, % d \ n, ch, ch) ; 2.18 China, :, AE, E A,China Glmre, c1 c2 c3 c4 c5 China,, c1 c2 c3 c4 c5 G lmr e, Glmr e, 23
3 3.1 C C ( ) ( )( ), C : (1 ), C, : if ( )else ( ) for ( ) while ( ) dowhile( ) continue break switch goto return ( ) ( ) ( ) ( ) ( switch ) () ( ) () ( ), :if ( ) else :if( x > y) z = x; else z = y; (2 ), : printf ( This is a C statement. ) ; (3 ) :, : i + + ;, i 1: x + y;, x + y,, x + y, C,,, C ( 24
), C ; (4 ) :,,, (, ) (5 ) {, : { z = x + y; ) t = z/ 100 ; printf(% f, t) ; :, (PASCAL 3.2, C, : (1 ) C =, (2 ) C, : if( (a = b) > 0) t = a ; if a = b, : if ( (a = b; ) > 0) t = a ; if 3.3, C, C, printf scanf, printf scanf,, C, C,, ( printf scanf ),, ( ),, printf scanf 25
3.3.1 1. getchar getchar, : getchar ( ) : (1 ) (2 ), (3 ) getchar,, 2. getche getchar 3. putchar, : putchar (c) ; c, c,, putchar( 97 )a, a ASCII 97putchar ( 10 ) (DOS,, (ASCII 10 ) ) :, ( ) # include < stdio.h > I/ O stdio.h [ 3.1] getchar a b, c1 c2 # include < stdio.h > main ( ) { char c1, c2 ; c1 = getchar( ) ; c2 = getchar( ) ; printf(% c% c \ n, c1, c2 ) ;, ab < >, ab, a < > b < >, c1 a, c2, a, 26
[ 3.2] putchar # include < stdio.h > { char c1, c2, c3,c4, c5, c6, c7; c1 = P ; c2 = u ; c3 = t ; c4 = C ; c5 = h ; c6 = a ; c7 = r ; putchar (c1) ; putchar(c2) ; putchar(c3 ) ; putchar(c4) ; putchar (c5) ; putchar(c6) ; putchar(c7 ) ; : PutChar 3.3.2 1. printf printf printf : printf(, ),, : (1 ), %, % d, % f,, % (2 ),, : printf(% d % d, a,b) ; printf( a = % d ] b = % d, a,b) ; % d,, a b 3 4, : a = 3 ] b = 4 ( ] ), printf 3 4 a b ( 3 4 ), a b a b 12 123, : a = 12 ] b = 123 27
: printf,,, printf( 1, 2, 3,, n) printf 2n 1 2., 3.1 3.1 printf d o x u c s f e g ( ) 8 ( 0) 16 ( 0x) 10,, 6,6 % f % e,0, % ( ), 3.2 3.2 l, d o x u f m( ).n(), n ;, - a, 12478; b, 1274.678, 3.3 3.3 a a b b % d 12478 %f 1274.677979 % 8d ] ] ] 12478 %e 1.274678e + 03 % o 30276 %8.2f ]1274.68 % x 30be % g 1274.68 % u 12478 %10.2e ] ] 1.27e + 03 % - 8d 12478 ] ] ] % - 10.2e 1.27e + 03 ] ] 28
: (1 ) C, % e, 3 ( e + 003), 2 ( e + 03 ) 6, 5 (2 ) long ( d o x u ) l, % ld, % lo, % lx, % lu, l, (3 ) %o, % x,,, a = - 2, 1111111111111110, % o 177776 ( - 2) (4 ) ( int ) %u ;, unsigned %d, unsigned % o % x % u,, a = - 2, %u 65534 (5 ) printf \ n \ t \ b \ r \ f \ 377 (6 ) d o x u c s f e g, % %, 9,, : printf( c = % cf = % 10.3fs = %s, c, f, s) ; % c f, %10.3f, s, %s (7 ) %, %, : printf ( % f% %, 1.0/ 3 ) ; 0.333333% [3.3] # include < stdio.h > { int x1; float x2 ; char c1, c2 ; x1 = 99 ; x2 = 100 ; c1 = a ; c2 = b ; printf( c1 = % c ] c2 = % d ] x1 = % d, x2 = % f, c1, c2, x1, x2 ) ; 29
: c1 = a ] c2 = 98 ] x1 = 99, x2 = 100.000000 c1 c2 ab, c2 d, c2 b, ASCII 3.3.3 scanf 1. scanf scanf(, ) printf,, [3.4] scanf { int a,b, c ; scanf( % d % d% d, &a, &b, & c) ; printf(% d, % d, % d \ n, a,b, c) ; &a& b& c &, & a a scanf : a b c, a b c % d % d% d% d,, tab : 3 ] ] 4 ] ] ] ] 5 < > 3 < > 4 ] 5 < > 3( Tab )4 < > 5 < > % d%d% d,, : 3,4,5 : 3.4 scanf : scanf( % d, % d, % d, & a, &b, &c)? 2. 30 printf, %,,
() 3.4 scanf 3.5 scanf 3.4 scanf d o x c s f e,,, \ 0, f, e f 3.5 scanf l ( %ld, %lo, %lx) double (% lf % le) h ( % hd, % ho, % hx) ( ) ( ) : (1 ) C scanf % u, unsigned, % d %o% x (2 ), : scanf(% 3d% 3d, &a, & b) ; 123456 < >, 123 a, 456 b : scanf(% 3c, & ch), ch, abc, ch a (3 ) %, : scanf(% 2d ] % 3d ] % 2d, &a, &b) ; : 12 ] 345 ] 67 < > 12 a, 67 b, 345,, 31
(4 ), : scanf(% 7.2f, &a) ;, a 12345.67 1234567 < > 3. scanf (1 ) scanf,, a b, : scanf(% d, % d, a,b) ;, a, b& a, & b, C (2 ),, : scanf(% d: % d : % d, &h, & m, & s) ; : 122336 < > : scanf( a = % d, b = % d, c = % d, & a, &b, & c) ; : a = 12,b = 24, c = 36 < >, (3 ) % c, : scanf(%c%c%c, &c1, &c2, &c3) ; : a ] b ] c < > a c1, ] c2, b c3, % c,, ] c2 (4 ), :, ( Tab), %3d, 3 : scanf(% d%c% f, &a, &b, & c) ; : 32
1234 a 123o.26 < > a b c % d 1234 a, 1234,, 1234 aa b, % c, a, c 1230.26123o.26, 123 o,, 123 c [ 3.5] scanf : a = 13,b = 198, c1 = G,c2 = k,x = 2.38, y = - 77.6, z = 132.9 # include < stdio.h > { int a,b; char c1, c2 ; flat x,y, z ; scanf(%3d% 6d%c%c, % f, % f% f% f, & a, % b, % c1, & c2, & x, &y, &z) ; printf(% d, % d, %c, % c, % f, % f, % f \ n, a,b, c1,c2, x, y, z) ; : ] 13 ] ] ] 198 Gk,9.9,2.38-77.6 132.9 < > %3d, 13%6d,, 198 Gk% f, (9.9 )x, y, z : 13,198, G, k,2.38, - 77.6,132.9 3.4 [3.6], : # include < stdio.h > { char c1,c2; cl = getchar( ) ; printf(%c, % d \ n, cl, cl) ; c2 = c1 + 32 ; printf(%c, % d \ n, c2, c2 ) ; : A < > A,65 a,97 33
[ 3.7] a y, : # include < stdio.h > { char ch; ch = getchar( ) + 1-32; putchar (ch) ; : c < > D [ 3.8], (, ) : s = s( s - a) ( s - b) ( s - c) a, b, c, s = a + b + c 2 : # include < math.h > { float a, b, c,s, area ; scanf( % f, % f, % f, & a, &b, & c) ; s = 1.0/ 2 ( a + b + c) ; area = sqrt( s ( s - a) ( s - b) ( s - c) ) ; printf( a = % 7.2f, b = %7.2f, c = % 7.2f, s = % 7.2f \ n, a, b, c, s) ; printf( area = % 7.2f \ n, area) ; : 3,4,6 < > a = 3.00,b = 4.00, c = 6.00, s = 6.50area = 5.33 3.1 : 34 { float y; y = 1678.2431 ;
printf( % - 8.4f \ n, m) ; printf(%10.4f \ n, m) ; A) 1678.3431 B) 1678.2431 C) - 1678.2431 D) 1678.2431 1678.2431 00 1678.2431 001678.2431 1678.2431 3.2 int a = - 2; : printf(%8lx, a) ;, A) % d B) % x, C) % x,, %8lx D) %8lx, 8 3.3 m n (int ), A) getchar( ) B) scanf(% d% 1d, & m, & n) C) putchar (\ ) D) putchar(\ ) 3.4 getchar getche A) getchar, getche B) getche, getchar C) getchar, getche D) getchar, getchar 3.5, 22 o 98 p, { int x1, x2 ; char c1, c2 ; scanf(% d%c% d%c, &x1, &x2, &c1, &c2) ; printf(% d % c % d % c \ n, x1, x2,c1, c2) ; A) 22 o 98 p < > B) 22o 98p < > C) 22o98 p < > D) 22 o98p < > 3.6 { unsigned int l ; l = 65535; printf(% d \ n, l) ; 35
A) - 1 B) 65536 C)1 D) 3.7, 25 ] 13 ] 10 < >, { int x,y, z ; scanf(% d% d% d, &x, &y, & z) ; printf( x + y + z = % d \ n, x + y + z) ; A) x + y + z = 48 B) x + y + z = 35 C) x + z = 35 D) 3.8 : scanf( a = %d, b = % d, c = % d, &a, & b, & c), a 3, b 7, c 5, A) 375 < > B) 3, 7, 5 < > C) a = 3, b = 7, c = 5 < > D) a = 3 b = 7 c = 5 < > 3.9 x y float, A) + + x; B) y = ( x%2 ) / 10; C) x = y + 8; D) x = y = 0; 3.10 { long y = - 12345; printf( y = % - 8ld \ n, y) ; printf( y = % - 08ld \ n,y) ; printf( y = % 08ld \ n,y) ; printf( y = % + 8ld \ n, y) ; A) y = - 12345 B) y = - 12345 C)y = - 12345 D) y = - 12345 y = - 12345 y = 12345 y = - 12345 y = - 0012345 y = - 0012345 y = - 0012345 y = - 0012345 y = 00012345 y = - 12345 y = + - 12345 y = - 12345 y = + 12345 3.11, a 4, c 54.36 36 1, 2 { int a,b;
float c, d, e; scanf( a = % d, c = %f, & a, & c) ; printf( a = % d, c = % f \ n, a, c) ; 3.12 3 { int a = 3, b = 5; float c = 6.5, d = 123.785, e = - 198.34; printf( a = % 5d,b = % - 10d, c = %6.2f,d = % 6.2f, e = % - 10.2f \ n, a,b,c, d, e) ; 3.13, A, 4 # include < stdio.h > { char c; putchar (getchar( ) + 32 ) ; 3.14 5 { int i = 100 ; printf(% d, % o, % x, % u \ n, i, i, i, i) ; 3.15 6 { int i, j; unsigned int u1, u2; u1 = 10000 ;u2 = 40000; i = u1 ; j = u2 ; printf( u1 = % u,u2 = % u \ ni = % d, j = % d \ n, u1, u2, i, j) ; 3.16 % 7, 8 { float f; f = 100.0/ 30; printf(% f% %, f) ; 3.17 9 37
{ short n ; n = - 31; printf(\ ndecimal = % d,hex = % x,octal = % o,unsigned = % u \ n,n, n, n, n) ; 3.18 int a; float b, x; char c1, c2; a = 3, b = 6.5, x = 12.6, c1 = a, c2 = A, scanf 10, 11 3.19 r = 1.5, h = 3, scanf,, 3.20, :, 2 c = 5 9 ( f - 32 ) 3.21, getchar c1 c2, putchar printf 38
4 ( ) if ; switch ; for while do while, continue break 4.1 if if, ( ) 4.1.1 if C if : : if( ) : if( x > y) printf ( % d, x) ; if 4.1( a) : if( ) 1 else 2 : if ( x > y) printf ( % d, x) ; else printf (% d, y) ; 4.1( b) : 4 1 39
if ( 1) 1 else if( 2) 2 else if( 3) 3 else if ( m) m else m + 1 4.2 : 4 2 (1 ) if if,, if(a = = b & & x = = y) printf ( a = b, x = y ) ;,, 0,, 0,, if : if (3 ) printf ( O.K. ) ;, O.K., 3,,, ( ), if : if ( a ) printf (% d, a ) ; : a ASCII 97 (2 ) if, else, : 40
C, if,,, (if else ), if else, if, if (3 ) if else,, { : if (a + b < c & & b + c > a & & c + a > b) { s = 0.5 (a + b + c) ; else area = sqrt( s (s - a) ( s - b) (s - c) ) ; printf( area = % 6.2f, area) ; printf( it is not a trilateral ) ;, {, {, 4.1.2 if if if if, : if ( ) else if ( ) 1 else 2 if if ( ) 3 else 4 if if else if else, if else if if if else : if ( 1 ) if ( 2 ) 1 else if ( 3 ) 2 else 3 4.3 if else,, : if ( ) { if ( ) 1 ( if) 41
4 3 else 2 { if, else if 4.1.3 if [4.1],,, ;, : main ( ) { char ch; scanf (%c, &ch) ; if ( ch > = A& & ch < = Z ) ch = ch + 32 ; printf (%c, ch) ; : A < > a AASCII 65,a ASCII 97 ASCII 32 if if : ch = (ch > = A& & ch < = Z )? (ch + 32) ch; 42 :, if?? [4.2] a b c,
# include < stdio.h > : # include < math.h > { int a,b, c, max; scanf (% d, % d, % d, & a, &b, & c) ; max = a ; if ( abs( max ) < abs( b) ) max = b; / max a,b / if ( abs( max ) < abs(c) ) max = c ; / max a, b, c / printf( max = % d \ n, max) ; : 12, - 34,9 < > max = - 34, : # include < stdio.h > # include < math.h > { int a,b, c, temp, max; scanf( % d, % d, % d, & a, & b, & c) ; temp = (abs(a) > abs (b) )? a : b; max = (abs( temp) > abs(c) )? temp: c; printf( max = % dmax) ; 4.2 switch switch, : switch ( ) { case 1 : 1 : case 2 : 2 case n : n default : n + 1 (1 ) case, case 43
, case, default (2 ) case, (3 ) case (4 ) case, case case, switch, switch,, case, switch, switch, break (5 ) case, : switch( grade) { case A : case B : case C : printf ( > 60 \ n ) ; break; grade A B C [ 4.3], : 3 5/ < >, { float x,y; char o; double r; scanf(%f %f% c, & x, &y, &o) ; switch(o) { case + : r = x + y; break; case - : r = x - y; break; case : r = x y; break; case / : r = x/ y; break; / / printf(% f, r) ; :0.600000 [4.4], A B C D E90 A, 80 89 B, 7079 C,6069 D, 60 E : 44 { int score, temp; char grade ;
scanf ( % d, &score) ; if ( score = = 100) temp = 9; else temp = score/ 10; switch( temp) { case 9 : grade = A ;break; case 8 : grade = B ; break; case 7 : grade = C ;break; case 6 : grade = D ; break; case 5 : case 4 : case 3 : case 2 : case 1 : case 0 : grade = E ; printf( score = % d, grade = %c \ n,score, grade) ; : 90 < > score = 90, grade = A [ 4.5], # include < stdio.h > main( ) { int x = 1, y = 0, a = 0, b = 0; switch( x) { case 1 : / / switch( y) { case 0 : a + + ; break; /, switch / case 1 : b + + ; break ; case 2 : a + + ; b + + ; break; / switch, / printf( a = % d, b = % d, a, b) ; : a = 2, b = 1 switch : break switch 45
4.3 goto goto, : goto ;,,, goto, goto, goto, : (1 ) if ( 2), C break continue goto, goto,, ( ) 10 0 [4.6] if goto, n n = 1 : { int i,sum = 0 ; i = 1; loop: if ( i < = 100) { sum = sum + i ; i + + ; goto loop; printf(% d, sum) ; : 5050 4.4.1 while 4.4 while do while while, : while( ) 46
0, 4.4 :, : (1 ),,, while while (2 ), 4.4.2 do while, : do while( ) ; 4 4 :, :,, ( ),,, 0,, 4.5 4.4.3 while do while while, do while do while while do while while 4.4, 4.4 4.6 ( ), while do while,, while while ( 0 ), 10 [4.7] while do while n n = i (1) ( 2) { int sum = 0, i ; { int sum = 0, i ; scanf (% d, & i) ; scanf(% d, & i) ; while(i < = 10) { sum = sum + i ; { sum = sum + i ; do i + + ; i + + ; 4 5 4 6 while(i < = 10 ) ; printf( % d, sum) ; printf( % d,sum) ; 47
i 10, i > 10,, while, ( i < = 10), do while : while,, ( ) : do while,,,, until ( FORTRAN DO U N TIL), do while,, until() 4.5 for C for,,, while for for( 1; 2;3) : (1 ) 1 (2 ) 2, (0 ),, (3 ) (0 ),, ( 5) (3 ),, 3 (4 ) ( 2) (5 ) for 4.7 for for : for(; ; ) : for (i = 1 ; i < = 100 ; i + + ) sum = sum + i : i = 1 ; while (i < = 100 ) { sum = sum + i ; i + + ; 4 7, for for : 1 ; 48
while( 2) { for 3; : ( 1) for 1, for 1, : for ( ; i < = 100; i + + ) sum = sum + i ;, 1, (2 ) 2,, 2 4.8: for (i = 1 ; ; i + + ) sum = sum + i ; : i = 1 ; while (1 ) { sum = sum + i ; i + + ; 4 8 (3 ) 3, : for ( sum = 0, i = 1; i < = 100 ; ) { sum = sum + i ; i + + ; i + + for 3,,, (4 ) 1 3, 2, : for ( ; i < = 100 ; ) while (i < = 100) { sum = sum + i ; : { sum = sum + i ; i + + ; i + + ;, while for while,,, (5 ), : for ( ; ; ) 49
while (1 ), ( 2 ), (6 ) 1, : for (sum = 0; i < = 100 ; i + + ) sum = sum + i ; 3 1 3,,, : for ( sum = 0, i = 1; i < = 100; i + + ) sum = sum + i; for (i = 0, j = 100 ; i < = j; i + +, j ) k = i + j ; 1 3,,,, 4.9 4 9 4 10,, for (i = 1 ; j < = 100; i + +, i + + ) sum = sum + i; for (i = 1 ; i < = 100; i = i + 2) sum = sum + i ; (7 ) 2 ( i < = 100 ) ( a < b & & x < y),,, : for (i = 0 ; (c = getchar ( ) )! = \ n ; i + = c) ; 50 2 c,
\ n (), \ n, for 4.10,, ASCII, : for, 3, for, 4.6.1 break 4.6 break continue break switch,,, : break; break switch [4.8] { int i,s = 0 ; for(i = 1; i < = 10 ; i + + ) / i > 10 / { s + = i; : s = 1 s = 3 if(s > 5) break ; / s > 5 / printf( s = % d \ n,s ) ; : switch break, 4.6.2 continue : continue;,, [4.9] { int i,n,s ; s = n = 0; 51
for(i = 1; i < = 10 ; i + + ) {if(i% 2 = = 0 ) { s + = i; continue ; / i, n + + ; / n + + ; printf( s = % d, n = % d \ n, s, n) ; : s = 30, n = 5 continue break : continue, break, : ( 1) while( 1) (2 ) while(1) {...... {...... if ( 2) break; if (2) continue ;............ (1 ) 4.11, ( 2) 4.12 4.11 4.12 2 4 11 4 12 4.7,, 52
: (while do while for ), ( 1) while ( ) ( 2) do (3 ) for ( ; ; ) {...... {...... {...... while ( ) do for ( ; ; ) {...... {...... {..... while ( ) ; while ( ) ; ( 4) while ( ) ( 5) for ( ; ; ) ( 6) do {...... {...... {...... do while ( ) for ( ; ; ) {...... {...... {...... while( ) ;............ while ( ) ; 4.8 [ 4.10], 3 4, 3 4, 3 4 : { int number; printf( : ) ; scanf(% d, & number) ; if ( number %3 = = 0 & & number %4 = = 0 ) else printf( 3 4! printf( 3 4! \ n ) ; \ n ) ; : :13 < > 3 4! : :24 < > 3 4! [4.11] : 53
y = - 1 0 1 ( x < 0) ( x = 0) ( x > 0), x, y : { int x, y; scanf(% d, &x) ; if ( x < 0 ) y = - 1 ; else if( x = = 0) y = 0; else y = 1 ; : 3 < > printf( x % d, y % d, x, y) ; x 3,y 1 [ 4.12] year, : year 4, 100, ; year 400, ;, : # include < stdio.h > { int year, leap; : printf( Enter a year: \ n ) ; scanf( % d, &year) ; if(year %4! = 0 ) leap = 0; else if( year% 100! = 0) leap = 1 ; else if( year% 400 = = 0) leap = 1; else leap = 0 ; Enter a year: 37 < > if( leap! = 0 ) printf( % d is a leap year. \ n, year) ; else printf(% d is not a leap year. \ n, year) ; 37 is not a leap year. 54
,, 37 1996 2000 1000 [ 4.13] ax 2 + bx + c = 0, : x1 = - b + b2-4 ac 2 a : b 2, x2 = - b - b2-4 ac, (, a 0) 2 a - 4 ac > 0, x1 x2 ; b 2 b 2-4 ac = 0, x1 x2 ; - 4 ac < 0,, : # include < stdio.h > # include < math.h > { float a, b, c,d, p, r,x1, x2; scanf(%f %f% f, & a, &b, &c) ; d = b b - 4 a c; p = - b/ (2 a) ; if( d > = 0) { r = sqrt( d)/ (2 a) ; x1 = p + r; x2 = p - r; printf( x1 = % f, x2 = % f \ n, x1, x2) ; else printf( \ n ) ; : : 2 5 1 < > x1 = - 0.219224, x2 = - 2.280777 : 1 2 1 < > x1 = - 1.000000, x2 = - 1.000000 : 1 2 3 < > [4.14] 4 1-1 3 + 1 5-1 7 +, 55
10-6 : # include < math.h > { int s; float n, t, pi ; t = 1 ; pi = 0; n = 1.0; s = 1 ; while( (fabs( t) ) > = 1e - 6 ) { pi = pi + t ; n = n + 2 ; s = - s ; / / t = s/ n ; / t / / pi / 4 / pi = pi 4; printf( pi = % 10.6f \ n,pi) ; : pi = 3.141594 [ 4.15] 1!+ 2!+ 3!+ 4!+ + 20! : { float n,s, t ; n = 1 ; s = 0; t = 1; do { t = t n; s = s + t ; n + + ; while( n < = 20) ; printf( 1! + 2! + + 20! = %e \ n,s) ; : 1!+ 2! + + 20!= 2.56133e + 18 [4.16], 1.20,,, 100, : # include < stdio.h > 56
{ int t = 0, m ; float z = 0.0,pj ; for (m = 2; m < = 100 ; ) { z + = 1.20 m ; t + + ; m = 2 pj = z/ t ; printf(% f, pj) ; : 25.199999 [ 4.17],, : # include < stdio.h > { int i ; char number; for ( i = 1; ; i + + ) { scanf(%c, &number) ; if (number = = ) else break; printf( % c,number) ; printf( ) ; : ABC < > ABC for for ( i = 1; ; i + + ), 2 ( ),, if,, [4.18] 100120 3 : { int n; 57
for ( n = 100 ;n < 120 ;n + + ) { if (n %3 = = 0 ) continue ; : printf(% d,n) ; 100 101 103 104 106 107 109 110 112 113 115 116 118 119 n 3, continue,, printf, n 3, printf, [ 4.19], : # include < stdio.h > { char c ; int letter = 0, space = 0,digit = 0, other = 0 ; while ( (c = getchar( ) )! = \ n ) { if(c > = a& & c < = zc > = A& & c < = Z ) let ter + + ; else if(c = = ) space + + ; else if (c > = 0& & c < = 9 ) digit + + ; else other + + ; printf( letter = % d,space = % d,digit = % d,other = % d \ n, letter, space, digit,other) ; : He is 46 years old.< > letter = 12, space = 4, digit = 2,other = 1 58 [4.20]
: { int i, j, k; for ( i = 0; i < = 3; i + + ) { for (j = 0 ; j < = 2 - i ; j + + ) printf( ) ; for ( k = 0; k < = 2 i ;k + + ) printf( ) ; printf(\ n ) ; for ( i = 0; i < = 2 ; i + + ) { for (j = 0 ; j < = i; j + + ) printf ( ) ; for ( k = 0; k < = 4-2 i ; k + + ) printf( ) ; printf ( \ n ) ; [ 4.21],,,153, 153 = 1 3 + 5 3 + 3 3 : { int i, j, k, n; for ( n = 100 ; n < 1000; n + + ) /, n / { i = n/ 100; / / j = n/ 10 - i 10 ; / / k = n% 10 ; / / if(i 100 + j 10 + k = = i i i + j j j + k k k) printf(%5d,n) ; printf(\ n ) ; : 153 370 371 407 [4.22] m n, : { int a,b, num1, num2, temp; scanf(% d% d, &num1, &num2) ; if( num1 < num2) { temp = num1; num1 = num2; num2 = temp; a = num1; b = num2 ; / if a b / 59
while( b! = 0 ) { temp = a% b; a = b; b = temp; / / printf(: % d \ n, a) ; printf(: % d \ n,num1 num2/ a) ; : 12 8 < > :4 :24 [ 4.23], m, m 1 1, m, 2 3 m - 1 m, m, m, m, m, m 2 3, [ m ][ m ] m i > k, 2[ m ],, : # include < stdio.h > # include < math.h > { int i, m, k; printf( Enter m : \ n ) ; scanf(% d, &m) ; k = ( int) sqrt( ( float) m) ; / k = sqrt(m) ; / i = 2 ; while( i < = k & & m% i! = 0) i + + ; / i < k, / if(i > k) printf( % d is a prime number. \ n, m) ; else printf(% d is not a prime number., m) ; : Enter m : 60 17 < > 17 is a prime number. Enter m : 18 < >
18 is not a prime number. [ 4.24] 100200 : # include < math.h > main ( ) { int m, k, i,n = 0 ; for (m = 101 ; m < = 200; m = m + 2 ) { if ( n% 10 = = 0) printf(\ n ) ; : k = sqrt(m) ; for (i = 2; i < = k; i + + ) if ( m%i = = 0) break; if (i > = k + 1 ) { printf(% d, m) ; n = n + 1; 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 100200, [4.23], for break, [4.23] 4.1 A) 0 1 B) 0 0 C) D) 4.2 char ch A) A < = ch < = ZB) (ch > = A ) & (ch < = Z ) C) ( ch > = A ) & & ( ch < = Z ) D) ( A< = ch) AND ( Z> = ch) 4.3 if A) if ( a1 = = b1 & & c1 = = d1 ) printf( ) ; B) if ( 97 ) printf( ) ; C) if (cx! = cy) {cx - - ; cy + + ; 61
D) if (a > b) printf( % d, &a) else printf(%d, & b) ; 4.4 if, else A) if B) if C) if D) if 4.5 if A) if( a > b ) ; printf(% d %d, a, b) ; else printf(%d % d, a, b) ; B) if(a > b) temp = a; a = b; b = temp; printf(%d % d, a, b) ; else printf(% d % d, a, b) ; C) if( a > b ) { temp = a ; a = b; b = temp; printf(% d %d, a, b) ; ; else printf(% d % d, a, b) ; D) if(a > b) { temp = a; a = b; b = temp; printf(% d % d, a, b) ; else printf(%d % d, a, b) ; 4.6 x1 > x2? x1 : ( x3 > x4? x3: x4 ) x1 = 1, x2 = 3, x3 = 5, x4 = 7 A) 1 B) 3 C) 5 D) 7 4.7 { int x = 2,y = - 1,z = 2; if ( x < y) if (y < 0) z = 0 ; else z + = 1; printf( % d \ n, z) ; A) 3 B) 2 C) 1 D) 0 4.8 62 # include stdio.h { int a,b, d = 241 ;
a = d/ 100 % 9; b = ( - 1) & & ( - 1 ) ; printf( % d, % d, a, b) ; A) 6,1 B) 2,1 C) 6, 0 D) 2, 0 4.9 switch A) case B) case C) default D) break 4.10 class 3, switch ( class) { case 1: printf( First! \ n ) case 2: printf( Second! \ n ) ; case 3: printf( Third! \ n ) ;break; case 4: printf( Fourth \ n ) ; default : printf( Error! \ n ) ; A) Third B) Third Error Four th Error C) Third D) Error 4.11 x = 3; do { y = x - - ; if (! y) { printf( ) ; continue ; printf( # ) ; while(1 < = x < = 2) ; A) # # B) # # C) D) 4.12 while(! a)! a { int a ; scanf( % d, & a) ; while(! a) { printf(o.k. \ n ) ; 63
a =! a ; A) a = = 0 B) a! = 1 C) a! = 0 D) a = = 1 4.13 for ( n = 100 ; n < = 200 ; n + + ) { if( n% 3 = = 0) continue ; printf (%4d,n) ; A) for( n = 100 ; ( n% 3) & & n < = 200; n + + ) printf(%4d, n) ; B) for( n = 100; ( n%3 ) n < = 200; n + + ) printf(%4d, n) ; C) for( n = 100 ; n < = 200; n + + ) if( n% 3! = 0 ) printf(% 4d, n ) ; D) for( n = 100; n < = 200; n + + ) { if( n%3 ) printf(% 4d, n ) ; else con tinue; break; 4.14 C, 1 4.15 year, leap year, : 4, 100 ; 4, 400 () { int year; scanf(% d, &year) ; if( (year % 4 = = printf( leap year ) ; 2 year % 100! = 0) 3 year % 400 = = 0) 4.16 Fibonacci :1,1,2,3,5,8, 40, : F1 = 1 ( n = 1 ) F2 = 1 ( n = 2 ) F = Fn - 1 + Fn - 2 ( n 3) 4 64 { long int f1 = 1, f2 = 1; int i ;
for (i = 1 ; 4 ; i + + ) { printf( % 12ld% 12ld, f1, f2) ; if( 5 ) printf( \ n ) ; f1 = f1 + f2 ; f2 = 6 ; 4.17 7 { int i; for (i = 1; i < 6; i + + ) { if (i % 2) printf( ) ; else continue ; printf(& ) ; 4.18 1234 < >, 8 # include < stdio.h > { int c ; while( (c = getchar( ) )! = \ n ) switch(c - 2 ) { case 0 : case 1 : putchar(c + 4 ) ; case 2 : putchar(c + 4 ) ; break ; case 3 : putchar(c + 3 ) ; case 4 : putchar(c + 2 ) ; break ; printf(\ n ) ; 4.19 9 { int k = 1 ; while( k < = 20) if( + + k% 4! = 2) continue; else printf( % d, k) ; printf(\ n ) ; 4.20 10 65
{ int x = 1, y = 3, z = 5 ; switch( x = = 1) { case 1 : switch (y < 0) { case 1 : printf( A ) ;break; case 2 : printf( B ) ;break; case 0 : switch (z = = 2 x + y) { case 0: printf( C ) ; break; default : printf( F ) ; printf(\ n ) ; case 1: printf( D ) ;break ; default :printf( E ) ;break; 4.21 11 { int a,b ; ; for (a = 0,b = 10; a < b; a + = 2) b = b - 1 ; printf( a = % d, b = % d \ n, a, b) ; 4.22 12 { int x1 = 10, x2 = 5,x3 = 5, x4 = 5 ; int l = 0, m = 0,n = 0 ; for ( ;x1 > x2 ; + + x2 ) l + + ; while(x1 > + + x3 ) m + + ; do n + + ; while( x1 > x4 + + ) ; printf( l = % d, m = % d,n = % d \ n, l, m, n) ; 4.23, 13 ( ), 14 y = - 1 0 1 ( x < 0 ) ( x = 0 ) ( x > 0 ) 66 { int x,y; ( 1) scanf(% d, & x) ;
( 2) y = 0; ( 3) if ( x > = 0 ) ( 4) if ( x > 0 ) y = 1 ; ( 5) else y = - 1 ; ( 6) printf( x = % d, y = % d \ n,x,y) ; 4.24 ( I) 10, 10% ; 10, 20 ( 100000 < I200000), 10 10%, 10, 7.5 % ; 200000 < I400000, 20 ( ), 20 5 % ; 400000 < I600000, 40 3 % ; 600000 < I1000000, 60 1.5% ; I > 1000000, 100 1% I, : if ; switch 4.25,, 4.26,,, ;,,,, 4.27,, 6 1 2 3, 6 = 1 + 2 + 3, 6 1000, 6 its factors are 1,2,3 4.28 : 10 0 k = 1 50 k + k 2 k = 1 1 0 + k = 1 1 k 67
5 5.1.1 : [] ; : int a[10] ; a, 10 : 5.1 [] [] ; : float a[3] [4 ], b[5] [10 ] ; a 34 (3 4 ), b 510 (5 10 ) : float a[3,4],b[ 5,10] ; : (1 ), (2 ),, : int a (10 ) ; (3 ), : int n = 5 ; # define M 5 int a[ n] ; () int a[ M ] ; ( ) (4 ),, int a[10] ; a 10, 0, 10 : a[0 ], a[ 1], a[2 ], a[ 3], a[ 4], a[5 ], a[ 6], a[7 ], a [8 ], a[ 9] int a[3 ] [ 4] ; a 3 4, 0 12 : a[ 0] [0 ], a[0 ] [ 1 ], a[ 0 ] [2 ], a[ 0 ] [ 3], a[ 1 ] [ 0], a [1 ] [ 1 ], a [ 1] [ 2 ], a [ 1] 68
[3 ], a[ 2] [0 ], a[ 2] [1 ], a[ 2] [2 ], a[ 2] [3 ] (5 ) :,, 5.1 a[3 ] [ 4] 5 1 (6 ) C,, : float a[2] [3 ] [4 ] ; :, 5.1.2 1. (1 ) : int a[10] = {0, 1,2,3,4,5,6,7,8,9 ;, a[ 0 ] = 0, a[ 1 ] = 1, a[ 2 ] = 2, a[ 3 ] = 3, a[ 4 ] = 4, a[ 5] = 5, a[ 6] = 6, a[ 7] = 7, a[ 8] = 8, a[ 9] = 9, : int a[ ] = {0,1,2, 3,4,5,6,7, 8,9;, 10, a 10 (2 ) : int a[10] = {0, 1,2,3,4 ; a 10, 5, 5 0, 1, 2, 3, 4, 5 0,, 10 : int a[ ] = {0,1,2,3,4 ; a 5, 10 (3 ) 0, : 69
int a[10] = {0, 0,0,0,0,0,0,0,0,0 ; int a[ 10 ] = {0;,, static(, ), 0: int a[ 10] ; static int b[ 10 ] ; a[ 0] a[ 9], b [0 ] b [9 ]0 2. (1 ) : int a[ 3] [ 4] = {{1,2,3, 4, {5,6,7, 8, {9,10,11,12 ;,, (2 ), : int a[ 3] [ 4] = {1, 2,3,4,5,6, 7,8,9,10,11,12 ;,,,,, (3 ), : int a[ 3] [ 4] = {{1, {5, {9 ; 1, 0 : 1 0 0 0 5 0 0 0 9 0 0 0 : int a[ 3] [ 4] = {{1, {0, 6, {0,0,11; : : 1 0 0 0 0 6 0 0 0 0 11 0 int a[ 3] [ 4] = {{1, {5, 6 ; : 70 1 0 0 0 5 6 0 0 0 0 0 0
(4 ) ( ),, : int a[ 3] [ 4] = {1, 2,3,4,5,6, 7,8,9,10,11,12 ; : int a[ ] [ 4] = {1, 2,3,4,5,6,7,8,9,10,11,12 ;, 12, 4, 3, : int a[ ] [ 4] = {{0,0,3, {, {0, 10;, : 3 : 5.1.3 0 0 3 0 0 0 0 0 0 10 0 0, C : [] : [] [] a[ 2] [3 ] a[ 2-1 ] [ 2 2-1] : a[2, 3], a[ 2-1, 2 2-1 ],, : b[ 1] [2] = a[ 2] [ 3]/ 2, : int a[3 ] [4 ] ; ( ) a[3] [4 ] = 3; ( ) a 34, 02, 03 a[ 3] [4 ] a [ 3 ] [ 4 ] a [ 3 ] [ 4 ] a[ 3] [4 ], a [ 3 ] [ 4 ] 3 4, a[ 3] [4 ] 71
5.2,, [ 5.1] 10, : (1 ) 10, 10 ; (2 ) ; (3 ) ; (4 ) (, ) : { int i, j, t, a[ 10] ; for( i = 0; i < 10 ; i + + ) scanf(% d, &a[i] ) ; for( i = 0; i < 10 ; i + + ) printf(% 4d, a[ i] ) ; printf( \ n ) ; for( i = 9; i > = 0; i - printf( \ n ) ; for(i = 0, j = 9 ; i < j; ) - ) printf( % 4d, a[i] ) ; { t = a[i] ; a[i] = a[j] ; a[j] = t ; i + + ; j - - ; for( i = 0; i < 10 ; i + + ) printf(% 4d, a[ i] ) ; printf( \ n ) ; : 1 2 3 4 5 6 7 8 9 0 < > 1 2 3 4 5 6 7 8 9 0 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1,, : a[0 ] a[ 9 ], a[1 ] a[ 8 ],, i < j [5.2] 10,, : 72 { int i, j, t, a[10 ] = {2,4,1,6,5,9,7,0, 8,3; for(i = 0; i < 10 ; i + + ) printf(% 4d, a[i] ) ; printf(\ ninput t : \ n ) ;
scanf( % d, & t) ; for(i = 0; i < 10 ; i + + ) if( t = = a[i] ) break; for(j = i ; j < 9; j + + ) a[j] = a[j + 1] ; for(i = 0; i < 9; i + + ) printf(%4d, a[i] ) ; printf(\ n ) ; : 2 4 1 6 5 9 7 0 8 3 Input t : 1 < > 2 4 6 5 9 7 0 8 3,,, break,, 0, 7,, a[ 8] a[ 7], a[ 9] a[8 ] 3,, 9 [ 5.3], k,, k = 0, a[ k ] a[ 1], a[1 ] a [ k ], k = 1, k,, k a[0 ] a[ 1], a[ k ] a[2 ], a[2 ] a[ k ], k = 2, k, k a[0 ]a[ 2],, k a[0 ]a[ 9 ] for, a[ 0 ] a[ k], : # define M 10 { int i, j, a[ M ], k, t ; for(i = 0; i < M; i + + ) scanf(% d, &a[i] ) ; for(i = 0; i < M; i + + ) printf(% 4d, a[i] ) ; printf(\ n ) ; k = 0 ; for(j = k + 1 ; j < M ; j + + ) if(a[j] < a[ k] ) k = j ; t = a[0] ; a[0 ] = a[ k] ; a[ k] = t ; for(i = 0; i < M; i + + ) printf(% 4d, a[i] ) ; printf(\ n ) ; : 73
5 3 7 1 0 9 2 8 4 6 < > 5 3 7 1 0 9 2 8 4 6 1 3 7 5 0 9 2 8 4 6 [ 5.4] 10 5.3, k 0, a[ 0]a[9 ] 0,, k i(0i8),, a[i] a[ 9 ] i, i 0 8, i for : main ( ) { int i, j, k, temp, a[10] ; for (i = 0 ; i < 10; i + + ) scanf(% d, &a[i] ) ; for ( i = 0; i < 9 ; i + + ) { k = i ; for (j = k + 1; j < 10 ; j + + ) if(a[j] < a[ k] ) k = j ; temp = a[i] ; a[i] = a[k ] ; a[ k] = temp; for (i = 0 ; i < 10; i + + ) printf(% d, a[i] ) ; printf(\ n ) ; : 5 3 7 1 0 9 2 8 4 6 < > 0 1 2 3 4 5 6 7 8 9 [5.5] 10 ( ) :,, 5.2 6 8 9, 2 3 ( 9 5) 5, 8-5 - 4-2 - 0-9, : 9,, 0 ( 5 ),, 5, 5. 34, 8, 6 5, 5, 4 5 1 n, n - 1 j n - j n = 10, 11, a[ 0], a[ 1] a[ 10 ], 74
5 2 5 3 { int a[11 ] ; int i, j, t ; printf( input 10 numbers: \ n ) ; for ( i = 1; i < 11 ; i + + ) scanf(% d, & a[ i] ) ; printf(\ n ) ; for ( j = 1; j < = 9 ; j + + ) for (i = 1 ; i < = 10 - j ; i + + ) if(a[i] > a[i + 1] ) {t = a[i] ; a[i] = a[i + 1 ] ; a[i + 1] = t ; printf( the sorted numbers : \ n ) ; for ( i = 1; i < 11 ; i + + ) : printf( % d, a[i] ) ; input 10 numbers : 75
1 0 4 8 12 65-76 100-45 123 < > the sor ted numbers: - 76-45 0 1 4 8 12 65 100 123 [ 5.6] a 10, a, : { int i, j, x, a[11] = {1,3,5,7,9,11, 13,15,17,19 ; / / printf( Input x : \ n ) ; scanf(% d, &x) ; for( i = 0 ; i < 10; i + + ) printf( % 4d, a[i] ) ; / / printf(\ n ) ; i = 0 ; / / while( i < 10 & & a[i] < x ) i + + ; / / for( j = 9 ; j > = i ; j - - ) a[j + 1] = a[j] ; / / a[i] = x; / x / for(i = 0; i < 11 ; i + + ) printf(% 4d, a[i] ) ; / / printf(\ n ) ; : Input x: 4 < > 1 3 5 7 9 11 13 15 17 19 1 3 4 5 7 9 11 13 15 17 19 [5.7] : : 76 { int a[2] [3 ] = {{1,2,3, {4, 5,6 ; int b[ 2] [3], i, j; printf( array a : \ n ) ; for (i = 0 ; i < 2 ; i + + ) { for (j = 0; j < 3 ;j + + ) { printf( % 4d, a[i] [j] ) ; b[i] [2 - j] = a[i] [j] ; printf(\ n ) ; a = 1 2 3 4 5 6 b = 3 2 1 6 5 4
printf( array b: \ n ) ; for(i = 0; i < 2; i + + ) { for (j = 0 ; j < 3 ; j + + ) printf(% 4d,b[ i] [ j] ) ; printf( \ n ) ; : array a : 1 1 2 3 4 5 6 array b: 3 2 1 6 5 4 [ 5.8] 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 : (1 ) 1; (2 ),, : { int i, j, yh[ 6] [ 6] ; for ( i = 0; i < 6 ; i + + ) { yh[i] [0 ] = 1 ; yh[i] [i] = 1; / 1 / for ( i = 2; i < 6 ; i + + ) for (j = 1 ; j < i ; j + + ) yh[i] [j] = yh[i - 1] [j - 1] + yh [i - 1] [j] ; / / for ( i = 0; i < 6 ; i + + ) { for( j = 0; j < = i; j + + ) printf(% 4d, yh[ i] [ j] ) ; printf(\ n ) ; 77
[ 5.9], 36 5,, : # include < stdio.h > { float a[ 3] [ 6],x,sum ; int i, j; for ( i = 0; i < 3 ; i + + ) for(j = 0 ; j < 5 ; j + + ) { scanf(% f, & x) ; a[i] [j] = x; for (i = 0 ; i < 3; i + + ) { sum = 0 ; for (j = 0; j < 5; j + + ) sum = sum + a[i] [j] ; a[i] [5 ] = sum/ 5 ; for(i = 0 ; i < 3 ; i + + ) { for (j = 0; j < 6 ; j + + ) : printf( % 5.1f, a[i] [j] ) ; printf(\ n ) ; 67 87 67 56 78 < > 98 87 89 67 89 < > 56 76 66 61 52 < > 67.0 87.0 67.0 56.0 78.0 71.0 98.0 87.0 89.0 67.0 89.0 86.0 56.0 76.0 66.0 61.0 52.0 62.2, ( ), ( )sum = 0,,, [5.10] 34,, : 78
{ int i, j, row = 0, colum = 0, max; int a[3] [4 ] = {{1,2,3,4, {9,8,7,6, { - 10, 10, - 5,2 ; max = a[0 ] [ 0] ; for ( i = 0; i < = 2; i + + ) for (j = 0 ; j < = 3 ; j + + ) if ( max < a[i] [j] ) { max = a[i] [j] ; row = i; colum = j ; printf( max = % d, row = % d, colum = % d \ n, max, row, colum) ; : max = 10, row = 2, colum = 1 row colum max, max a[ 0] [0 ], a[0 ] [0 ], row colum 0, max, max row colum, 5.3 5.3.1, : char c[ 10 ] ; c, 10 : c[ 0] = I ; c[ 1] = ; c[2 ] = a ; c[3] = m ; c[4] = ; c[ 5] = h ; c[6 ] = a ; c[ 7] = p ; c[8 ] = p ; c[ 9] = y ; 5.4 c[ 0] c[1 ] c[2] c[ 3] c[ 4] c[5 ] c[6] c[ 7] c[ 8] c[9 ] I a m h a p p y 5.4, : int c[10] ; 79
5.3.2, : (1 ), : char c[ 10 ] = { I, a, m, h, a, p, p, y ; (),,, ( ASCII 0),, : char c[ ] = { I, a, m, h, a, p, p, y ; c 10, : char diamond[5] [5] = {{,,, {,,,, {,,,,, {,,,, {,, ;, 5.5 (2 ) : char c[ ] = { I am happy ;, : char c[ ] = I am happy ;, \ 0, 5 5 char c[ ] = { I, a, m, h, a, p, p, y,\ 0 ; 5.3.3, \ 0\ 0 ASCII 0,, % c \ 0, Hello, 6, : ASCII : H e l l o \ 0 80
72 101 108 108 111 0 6, 5 5.3.4 : : (1 ) % c (2 ) % s, ( string ) char c[ ] = China ; printf(% s, c) ; c 5.6, \ 0 : China : \ 0 C h i n a \ 0 5.6 % s, printf, : printf ( % s, c[ 0] ) ;, \ 0: char c[ 10 ] = { China ; printf ( % s, c) ; China, 10 \ 0, \ 0 : (1 ) % c: scanf (% c% c% c, & c[1 ], &c[2 ], &c[3] ) ; 3 (2 ) %s : scanf (% s, c) ; scanf c,, : 81
char c[ 6] ; : China < > \ 0, 6 scanf, : char str1[ 5],str2 [5], str3[5 ] ; scanf (% s % s% s, str1, str2, str3) ; : How are you? str1 str2 str3 5.7 str1 : str2 : str3 : H o w \ 0 \ 0 a r e \ 0 \ 0 y o u? \ 0 5 7 \ 0 : char str[13] ; scanf (% s, str ) ; 12 : How are you? 12 \ 0 str, How str, How, \ 0, \ 0str 5.8 & H o w \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 5 8 : scanf, 5.3.5,, : # include < string.h > 1. puts( ) 82 (\ 0 ) puts
char str[ ] = China \ nbeijing ; puts ( str) ; : China Beijing, 2. gets( ) :,, : gets ( str) : puts gets, : puts ( str1, str2 ) gets( str1, str2 ) 3. strcat( 1, 2), 2 1, 1, 1 ( 8 ) char str1[ 30] = People s Republic of ; : char str2[ ] = China ; printf ( % s, strcat(str1, str2) ) ; People s Republic of China : (1 ) 1, (2 ) \ 0, 1 \ 0, \ 0 4. strcpy( 1, 2), 2 1 : (1 ) 1, 1 2 (2 ) 1(str1 ), 2 83
:,, char str1[ 20] ; char str2[ 20] = China ; strcpy( str1, str2) ; strcpy ( str1, China ) ; (3 ) \ 0 1 (4 ) : str1 = China ; str1 = str2 ; strcpy : char c ; c = a ; 5. strcmp( 1,2) 1 2: strcmp ( str1,str2 ) ; strcmp ( China, Korea ) ; strcmp ( str1, Beijing ) ;, ( ASCII ), \ 0, ;, 1 2, 0 1 2, 1 2, :, : if ( str1 = = str2 ) printf ( yes ) ; : if ( strcmp ( str1,str2 ) = = 0) printf ( yes ) ; 6. strlen( ), \ 0: char str[10] = China ; 84
printf ( % d,strlen( str) ) ; 10, 6, 5, : strlen ( China ) 7. strlwr( ) lwr lowercase ( ) strlwr( ABC )abc 8. strupr( ) upr uppercase ( ) strupr( abc ) ABC, : C, ( ),, 5.3.6 [5.11] : # include < stdio.h > # include < string.h > { int i, j ; char t, ch[ 80] ; gets(ch) ; puts(ch) ; for (i = 0, j = strlen(ch) - 1 ; i < j ; i + +, j - - ) / j / { t = ch[i] ; ch[i] = ch [j] ; ch[j] = t ; puts(ch) ; : asdfgh < > asdfgh hgfdsa [5.12], strlen : 85
# include < stdio.h > { int i = 0 ; char a[ 80 ] ; gets(a) ; puts( a) ; while( a[i]! = \ 0 ) i + + ; /, i 1 / printf(% d \ n, i) ; : asdfgh < > asdfgh 6 [ 5.13], strcat : # include < stdio.h > { char s1 [80 ], s2 [40 ] ; int i = 0,j = 0; printf( \ n Input string1: ) ; scanf(% s, s1) ; printf( \ n Input string2: ) ; scanf(% s, s2) ; while( s1[ i]! = \ 0 ) i + + ; / i 1, / while( s2[ j]! = \ 0 ) s1 [i + + ] = s2[ j + + ] ; / / s1[ i] = \ 0 ; / / printf( \ n New string: % s, s1) ; : Input string1 : country < > Input string2 : side < > New string: country side [5.14], from to strcpy,\ 0 \ 0 : # include < string.h > 86
# include < stdio.h > { char from[80], to[80] ; int i; : printf( Input string: ) ; scanf(% s, from) ; for (i = 0; i < = strlen( from) ; i + + ) / i / to[i] = from[i] ; /, \ 0/ printf( Copied string: % s \ n, to) ; Input string: student < > Copied string: student [ 5.15], s1 s2 strcmp : # include < stdio.h > { int i = 0, a; : char s1 [80 ], s2 [80 ] ; gets( s1) ; gets (s2 ) ; puts( s1 ) ; puts( s2 ) ; while ( s1 [i] = = s2[i] & & s1[i]! = \ 0 ) i + + ; / s1[i]! = \ 0s2 [i]! = \ 0/ a = s1 [i] - s2 [i] ; boy < > if(a > 0 ) printf( ( s1 : % s) > ( s2: % s) \ n, s1,s2) ; if(a = = 0 ) printf( ( s1 : % s) = ( s2: % s) \ n, s1,s2 ) ; if(a < 0 ) printf( ( s1 : % s) < ( s2: % s) \ n, s1,s2) ; girl < > boy girl ( s1 :boy) < ( s2 :girl) [5.16],, : # include < stdio.h > { int i,num, word; 87
char str[ 80 ] ; gets( str) ; num = 0 ; / num, 0 / word = 0; /, / for ( i = 0; str[ i]! = \ 0 ; i + + ) if(str[ i] = = ) word = 0; /,, / else if( word = = 0 ) /, / { num + + ; / word 0, 1 / word = 1 ; / word = 1 / printf( words: % d \ n, num) ; : I am a boy.< > words :4 [ 5.17], : # include < stdio.h > # include < string.h > { int i ; char max[20],str[ 5] [ 20 ] ; / / gets( str[0 ] ) ; /, str[i]i + 1 / strcpy(max, str[0 ] ) ; / max, max / for(i = 1; i < 5; i + + ) { gets (str[ i] ) ; if( strcmp(max, str[i] ) < 0 ) strcpy(max, str [i] ) ; printf( The largest string is \ n% s \ n, max) ; : from < > 88 goto < > get < > girl < > first < > The largest string is goto,
, strcmp strcpy, str[i], 5.1 A) B) C) D) C, 1 5.2 A) int a(5 ) = {1, 2, 3, 4, 5; B) int a[ 5] = {1, 2, 3, 4, 5; C) int a[5 ] = {1-5; D) int a[ 5] = {0,1,2,3,4,5; 5.3 : char x[ ] = 12345 ; char y[ ] = { 1, 2, 3, 4, 5 ; A) x y B) x y C) x y D) 5.4 A) int s[2 ] [ ] = {{2,1,2, {6,3,9 ; B) int s[ ] [ 3] = {9,8,7,6,5,4; C) int s[3 ] [ 4] = {{1, 1, 2, {3, 3, 3, {3, 3, 4, {4, 4, 5; D) int s[ 3, 3] = {{1, {4, {6; 5.5 : char s1[ 80 ], s2 [80] ;,, A) scanf(% s% s, & s1, & s2) ; B) gets( & s1, & s2) ; C) scanf(% s% s, s1, s2 ) ; D) gets(% s%s, s1, s2) ; 5.6 A) if ( strcmp( str1, str2 ) ) printf(% s, str1 ) ; B) if ( str1 > str2 ) printf(%s, str1) ; 89
C) if ( strcmp( str1, str2 ) > 0 ) printf(%s, str1) ; D) if ( strcmp( str1) > strcmp( str2) ) printf(% s, str1) ; 5.7 # include < stdio.h > # include < string.h > { char s1 [80 ] = AB, s2 [80 ] = CDEF ; int i = 0 ; strcat( s1, s2 ) ; while( s1[ i + + ]! = \ 0 ) s2 [i] = s1[ i] ; puts( s2) ; A) CB B) ABCDEF C) AB D) CBCDEF 5.8, : AhaMA Aha < >, # include < stdio.h > { char s[ 80], c = a ; int i = 0 ; scanf( % s, s) ; while( s[i]! = \ 0 ) { if( s[ i] = = c) s [i] = s[i] - 32 ; else if( s[i] = = c - 32 ) s[i] = s [i] + 32; i + + ; puts( s) ; A) ahama B) AhAMa C) AhAMa aha D) ahama aha 5.9 # include < stdio.h > { char a[ ] = morming, t ; int i, j = 0; for(i = 1 ; i < 7 ; i + + ) if(a[j] < a[i] ) j = i ; t = a[j] ; a[j] = a[7] ; a[7 ] = a[ j] ; puts( a) ; A) mogninr B) mo C) morning D) mornin 5.10 s c 1 90
# include < stdio.h > { char s[80 ] ; int i, j; gets( s) ; for ( i = j = 0 ; s[i]! = \ 0 ; i + + ) if( s[i]! = c ) 1 ; s [j] = \ 0 ; puts( s) ; A) s[ j + + ] = s[i] B) s[ + + j] = s[i] C) s[ j] = s[i] ; j + + D) s[ j] = s[i] 5.11 C, 1 5.12 2 # define M 10 { int i; ( 1) int a[ M] = {1,2,3,4,5, 6,7,8; ( 2) for(i = 1 ; i < = M ; i + + ) ( 3) printf(% d, a[i] ) ; 5.13 a a[ 2] [2 ] 3 a[ 4] [5 ] = {{1,2,3, {3,2,1,6, {1 ; 5.14 c 4 char c[ ] = { c language ; 5.15 5 # include < stdio.h > { char c[ 6] ; int i = 0; for ( ; i < 6; c[i] = getchar( ), i + + ) ; for (i = 0 ; i < 6 ;putchar( c[i] ),i + + ) ; : p < > q < > 91
stru < > 5.16 : 9 33,, { int a[3 ] [3 ], sum1 = 0, sum2 = 0 ; / sum1,sum2 / int i, j; for( i = 0; i < 3; i + + ) for( j = 0; j < 3; j + + ) scanf( % d, 6 ) ; for( i = 0; i < 3; i + + ) { sum1 = sum1 + 7 ; sum2 = sum2 + 8 ; printf( sum1 = % d, sum2 = % d \ n, sum1, sum2) ; 5.17 a b { int a[ 2] [3] = {{1,2, 3, {4,5,6; int b[ 3] [ 2], i, j ; printf( array a: \ n ) ; for (i = 0; i < = 1 ; i + + ) { for ( j = 0; 9 ; j + + ) { printf(% 5d, a[ i] [ j] ) ; 10 ; printf(\ n ) ; printf( array b: \ n ) ; for (i = 0 ; 11 ; i + + ) { for (j = 0; j < = 1; j + + ) printf(%5d, b[i] [j] ) ; printf(\ n ) ; 5.18 a, 4, 92 # define N 20 { int a[ N ],i ; for(i = 0 ; i < N ; i + + ) scanf( % d, 12 ) ; for(i = 0 ; i < N; i + + )
{ if( 13 ) 14 ; printf(% 3d, a[i] ) ; printf(\ n ) ; 5.19 15 { int i, f[10] ; f[0 ] = f[ 1] = 1 ; for( i = 2; i < 10 ; i + + ) f[i] = f[i - 2] + f[ i - 1] ; for (i = 0 ; i < 10 ; i + + ) { if(i%4 = = 0) printf(\ n ) ; printf(%3d, f[i] ) ; 5.20 16 { int x,y, i, a[8], j,u, v; scanf(% d, &x) ; / 15 / y = x ; i = 0 ; do { u = y/ 2 ; a[i] = y% 2 ; i + + ;y = u ; while(y > = 1 ) ; for(j = i - 1; j > = 0; j - 5.21 17 { int m = 1,n = 3, j,k = 4 ; int b[5] = {1,3, 5 ; while(m < = n & & k > b[m] ) m + + ; for( j = n - 1 ; j > = m ; j - - ) b[j + 1] = b[ j] ; b[ m] = k ; for (m = 0; m < = n; m + + ) printf( % 3d, b[m] ) ; - ) printf(% d, a[j] ) ; 93
5.22 a, ( ) 5.23 0 19,,, 5.24,, 5.25,,, 5.26,, 5.27, 3, 80 5.28 : 5.29, : AZ az BY by CX cx 26, i (26 - i + 1 ), 94
6 6.1, C, C, [6.1] { int a,b, c ; scanf( % d, % d, & a, & b) ; c = max(a, b) ; printf(max is % d, c) ; max (int x, int y) { int z; z = x > y? x :y; return (z) ; : 7,8 < > max is 8, main, max x y : (1 ),, (2 ) C,, ( ), C, C ( 3) C main, main, main main (4 ),,,, (: main ) 95
(5 ), :,,,,,, (6 ), :,,,,,,, 6.2 (1 ) : { ( ) (2 ) : ( 1, 2, ) { : int max (int x, int y) / / { int z ; / / z = x > y? xy; / / return (z) ; / / (3 ), : ( ) { : 96
void dummy( ) { 6.3 6.3.1,, ( ),, ( ) : (1 ),,, (2 ),, :max (3, a + b), (,, 8 ) (3 ), (4 ),,,, (5 ) C,,,,,,,,,,,, [6.2] a b,, # include < stdio.h > { int a = 1, b = 2, c = 0 ; / a, b, c,1,2,0 / sum(a,b, c) ; /, a,b, c,1,2, 0 / printf( c = % d \ n, c) ;/ c,, z c / sum(int x, int y, int z) / x,y, z, x, y,z 1,2,0 / { z = x + y; / / /,x,y, z / 97
: c = 0,, z 6.3.2,, : (1 ) return return : return ( ) ; return ; return; return return, return, re turn, 6.1 max : max (int x, int y) { return ( x > y? xy) ;, return (2 ), return,,, (3 ),, : int max (float x, int y) char letter (char c1, char c2) double min (int x, int y ) C,, (4 ), return,,, 6.1 max z, max, max float,,, z float, max float (5 ), void ( ): void printstar( ) {,, 98,, void [6.3] 6.2,
# include < stdio.h > { int a = 1, b = 2, c ; / a,b, c, a,b 1,2 / c = sum(a,b) ; /, a,b, c / printf( c = % d \ n, c) ;/ c, c, c / int sum(int x, int y) / x,y 1,2, / { int z; / / z = x + y; return z ; /, x, y, z,z / : c = 3 return,, 6.3 return 6.4 6.4.1 : ( ) ;,,,,, 6.4.2, : 1. : printstar ( ) ;, 2.,, : c = 2 max( a, b) 99
3., : m = max (a, max ( b, c) ) ; max( b, c), max 6.4.3 ( )? (1 ) ( ) (2 ), # include, : # include stdio.h # include < stdio.h >,, : # include math.h # include < math.h > (3 ), ( ),, : ( ) ;, 6.4 [6.4] a b : # include < stdio.h > # include < math.h > / sqrt, / main ( ) { float add( ) ; / / 100 float a, b, c; scanf ( % f% f, & a, &b) ; c = add (a,b) ; printf ( Sum is % f \ n, c) ; float add( float x,float y) / add, float / { double z; z = sqrt( x) + sqrt(y) ; / sqrt( ( double) x) sqrt( ( double)y), sqrt double /
return (z) ; / return ( float)z ;, float / : 3.6 5.5 < > Sum is 4.216037 :float add ( ) ;, add C, : (1 ) ( ),, (2 ),,, (3 ),,, : char letter ( ) ; / 3 / float f( ) ; int i( ) ; main ( ) { / / char letter (char c1, char c2 ) / letter / { float f( float x, float y) / f / { int i(float j, float k) / i / {,, (4 ) ANSI,,, (, ) : : ( 1, 2,, n) ; ( 1 1, 2 2,) ; ( ) 6.4, : float add( float, float) ; 101
float add( float x,float y) ; 6.4, 6.4 add,,,, 6.4.4 [6.5], a b : { int a = 1, b = 2; swap( a, b) ; / a, b / printf( : a = % d, b = % d \ n, a,b) ; / a,b / swap(int a, int b) / a, b,a,b / { int c ; c = a ; a = b; b = c ; / a,b / printf( : a = % d, b = % d \ n, a,b) ; / a,b / /, a,b, c / : : a = 2,b = 1 : a = 1,b = 2, a b :,,, 6.1,,,, a, b 6 1 [6.6] a b c, 4.2, : # include < stdio.h > 102 # include < math.h >
{ int a,b, c, max; scanf (% d, % d, % d, & a, &b, & c) ; max = find( a, b, c) ; /,, printf(max = % d \ n, max) ; int find(int x, int y, int z) { int max; max = x; if(abs(max) < abs (y) ) max = y; if(abs(max) < abs (z) ) max = z ; return max ; : 12, - 34,9 < > max = - 34 / 4.2,, return [ 6.7] 4 1-1 3 + 1 5-1 7 10-6 ( 4.14 ) +, : # include math.h { float cal(float) ; / / float pi; pi = cal(1e - 6) ; printf( pi = % 10.6f \ n, pi) ; float cal(float e) { int s; float n, t, pi ; t = 1 ; pi = 0; n = 1.0; s = 1 ; while( (fabs( t) ) > = e) { pi = pi + t ; n = n + 2; s = - s; t = s/ n ; 103
pi = pi 4; return pi; : pi = 3.141594 [ 6.8] 100200 (4.24 ) : # include math.h main ( ) { int m, s = 0 ; for (m = 101 ; m < = 200 ; m = m + 2) s + = sum(m) ; printf( s = % d \ n, s) ; int sum(int m) { int i,k ; k = sqrt(m) ; for ( i = 2; i < = k ; i + + ) if (m% i = = 0 ) return 0 ; return m ; : s = 3167,,,,,,, 8 6.5 C,, C,,,,, 6.2 6.2 (main 3 ), : main ; a, a ; a ; b, b; b,, 104
b ; b, a ; a, a ; main a ; main :, [6.9 ] 1! + 2! + 3! + 4! + + 20! (4.15) : 6 2 1 : { float sum(int) ; / / float add; add = sum( 20 ) ; printf( add = %e, add) ; float sum(int n) { float fac(int) ; int i ; float s = 0 ; for(i = 1; i < = n ; i + + ) s + = fac(i) ; return s ; float fac(int i) { float t = 1 ; int n = 1; do {t = t n ; n + + ; while( n < = i) ; return t ; 2 : { float sum(int) ; float add ; add = sum( 20 ) ; printf( add = %e, add) ; float sum( int n) { int i ; float t = 1, s = 0 ; for (i = 1 ; i < = n; i + + ) {t = t i ; s = s + t ; 105
return s; : s = 2.56133e + 18 [ 6.10] 20, 20 : # include < stdio.h > # include < math.h > int isprime( int) ; / isprime, / { int i, a, s; float b, c ; int add(int) ; / add mul main,main / float mul(int) ; / int, float / for ( i = 2; i < = 20 ; i + + ) { s = isprime(i) ; if( s) printf(% d, i) ; a = add( 20) ; b = mul( 20 ) ; c = b/ a ; printf(\ nc = % 7.0f \ n, c) ; int add( int n) { int i,s,sum = 0 ; for ( i = 2; i < = n ; i + + ) { s = isprime(i) ; if( s) sum + = i ; return sum ; float mul( int n) { int i,s ; float t = 1.0; for ( i = 2; i < = n ; i + + ) { s = isprime(i) ; if( s) t = i ; return t ; int isprime( int m) { int i ; for ( i = 2; i < = sqrt( m) ; i + + ) if(m% i = = 0) return 0; return 1; 106
: 2 3 5 7 11 13 17 19 ( ) c = 125970 ( ),, 6.6, C : int f(int x) { int y,z ; z = f( y) ; return ( 2 z) ; f, f,, 6.3 : f1 f2, f2 f1, 6.4,,,,, if,, 6 3 6 4 [ 6.11] n! n! = n ( n - 1 )!, n!, ( n - 1 )!, ( n - 1)!, 107
: ( n - 2 )!,, 2!, 1!, 1! 1 n! = 1 n( n - 1)! : int fac ( int n) { int c ; n = 0 1 n > 1 if ( n = = 0 n = = 1 ) c = 1 ; / n 0 1, n! 1 / else c = n fac(n - 1 ) ; / n > 1, n! / return c ; / c n!,n! / main ( ) { int i ; scanf(% d, &i) ; if(i < 0) printf( Data error! \ n ) ; else printf(% d! = % d \ n, i, fac(i) ) ; : 5 < > 5! = 120 main, fac 6.5 6 5 [ 6.12], 603, 6 0 3 : # include < stdio.h > 108 { void fun(int) ; int n; scanf(% d, &n) ; if( n < 0 ) { putchar(- ) ; n = - n; fun( n) ;
void fun( int k) { int n; n = k/ 10; if( n! = 0 ) fun (n) ; putchar (k %10 + 0 ) ; C,,,,, 6.7 6.7.1,,, : float f1 (int a) / f1 / { int b, c; ( a, b, c, a,b, c) { int a,b; { int c ; c = a + b; (c ) (a, b ) : (1 ) main ( a, b), (2 ),,, f1 a b c main a b c, (3 ), f1 a, f1, 109
(4 ),,, [ 6.13] { int i, a = 0 ; : i = 1, a = 2 for (i = 1; i < = 2 ; i + + ) { int a = 1 ; / a, a / a + + ; printf( i = % d, a = % d \ n, i, a) ; / a, / printf( i = % d, a = % d \ n, i, a) ; / a a / i = 2, a = 2 i = 3, a = 0 6.7.2,,,, : : int p = 1, q = 5 ; / p,q, / float f1 (int a) / f1 / { int b, c; / a,b, c / char c1, c2 ; / c1, c2, / char f2 (int x, int y) / f2 / { int i, j; / x,y, i, j / main ( ) { int m, n; : / m, n / (1 ) :,,, 110
,,, (2 ), :, ;,,,,, ;,,,, (3 ),,,, extern, extern, [ 6.14] int max (int x, int y) / max / { int z; z = x > y? x :y; return (z) ; main ( ) {extern int a, b; /, / printf (% d, max (a,b) ) ; int a = 13,b = - 8 ; /, / : 13 (4 ),,, 6.8.1 6.8 ( ), : ( ),, 111
6.6, : (1 ) (2 ) (3 ),, 6 6, :, ; ;,, C : (), :, : ( auto), ( static), ( register), (extern) 6.8.2 (1 ),,,,, auto, : int f(int a) / f, a / { auto int b, c = 3; / b, c / auto, auto, auto, (2 ),,,,, static [6.15] void f(int c) { int a = 0; / 0, / 112 static int b = 0; / 0, / a + + ; b + + ; printf(% d: a = % d, b = % d \ n, c, a,b) ; { int i ;
for(i = 1; i < = 2; i + + ) f(i) ; / / : 1 : a = 1, b = 1 2 : a = 1, b = 2 a b, b,,, a b :, ( ),,,,,,,,,,, 0 ( ) ( ),,,, [ 6.16] { int i,b[5 ] ; for ( i = 0; i < 5 ; i + + ) printf( % d, b[i] ) ; / / printf(\ n ) ; printf( First time : \ n ) ; fun( ) ; /,a 0 / printf( Second time: \ n ) ; fun( ) ; /, a / fun( ) { int i ; static int a[5 ] ; /, / for ( i = 0; i < 5; i + + ) printf( % d, a[i] ) ; printf(\ n ) ; a[ 0] + + ; / a[0] / 113
: First time : 0 0 0 0 0 Second time : 1 0 0 0 0 ( ),, : (,, ),,,, (3 ),, C,, register [ 6.17] 1 5 : int fac ( int n) { register int i, f = 1 ; / i f, / for (i = 1 ; i < = n; i + + ) f = f i; return (f) ; main ( ) { int i ; for (i = 1, i < = 5; i + + ) printf(% d! = % d \ n, i, fac( i) ) ; f i, n, :, ( ),,,,,, : register static int a,b, c ; 114
a, b, c,,, 6.8.3, C,,, : (1 ), extern, file1.c a: int a ; main ( ) { file2.c file1.c a: extern int a ; power( n) { file2.c a, extern a, file1.c a (2 ), static file1.c file2.c static int a; extern int a ; fun (int n) { { a = a n; file2.c extern int a;, file2.c file1.c static a static ( ), 6.8.4 (1 ) : 115
auto register extern static () ( ) () () (2 ), :, (, ) (, ) (, ) () () (, ) (3 ),, ( ) ( ) () ( ) () (4 ),,, 6.1 6.1, ( ) 6.9,,, 116
6.9.1,, static, : static ( ) : static int fun(a,b),,, 6.9.2, extern,, : extern int fun (a,b) fun, extern,, extern ( ) 6.1 A), B) main, C) main D) C 6.2 A), B) C) D) 6.3 A) C B), C) D) C, 6.4 117
A) float swap (int x, y) B) int max (int a, int b) C) char scmp(char c1, char c2) ; D) double sum( float x; float y) 6.5, A) B) C) D) 6.6 A) B) C) D) 6.7 fun (int x ) { printf(% d \ n, x) ; A) void B) int C) D) 6.8, A) B) C) D) 6.9 A) return B) C) D) 6.10, return, : A) B) C) D) 6.11 func( (exp1, exp2 ), (exp3, exp4, exp5) ) ; A) 1 B) 2 C) 4 D) 5 6.12, 118 A) B) C) D)
6.13, 1 6.14 2 6.15 add, 3, 4 (1 ) void add(float a, float b) (2 ) { float c ; (3 ) c = a + b (4 ) return c ; 6.16 5 f( ) { int a = 3; static b = 4 ; a = a + 1; b = b + 1 ; printf( a = % d, b = % d \ n, a,b) ; { f( ) ; f( ) ; 6.17 6 { int i = 2,x = 5, j = 7 ; fun(j,6 ) ; printf( i = % d; j = % d ; x = % d \ n, i, j, x) ; fun(int i, int j) { int x = 7 ; printf( i = % d; j = % d ; x = % d \ n, i, j, x) ; 6.18 7 { int x = 2, y = 3, z = 0 ; printf(# x = % d y = % d z = % d \ n, x, y, z) ; add( x, y,z) ; printf(@ x = % d y = % d z = % d \ n, x, y, z) ; 119
add(int x, int y, int z) { z = x + y; x = x x ; y = y y; printf( x = % d y = % d z = % d \ n, x, y, z) ; 6.19 double a = 5.0;, int n = 5; mypow( a, n) ;, a n double mypow( double x, int y) double mypow (double x, int y) { 6.20 e n f 1, f 2 f1 f 2 float f2 (int n) { float f1 (int x, int n) { { float exp = 1.0; int n,x ; : printf( Input a number : ) ; e x = 1 + x + x2 2! + x3 + (20 ) 3! scanf(% d, &x) ; printf(% d \ n, x) ; exp = exp + x ; Input a number :3 for( n = 2 ;n < = 19; n + + ) exp = exp + f1 ( x, n)/ f2 ( n) ; printf(\ nthe is exp( % d) = % 8.4f \ n, x, exp) ; The is exp( 3) = 20.0855 6.21 20 6.22 Sn 2 1, 3 2, 5 3, 8 5, 13 8, 21 13, n a = a + aa + aaa + + aaa, a : 2 + 22 + 222 + 2222 + 22222( n 5), n 6.23 : 120
: x = xn + 1 = 1 2 xn a + a xn x 10-5 6.24 10 20 6.25,, 6.26 n, 483, 483n, 6.27, 121
7 C, C C ( C ) C (,, ),,, C : (1 ) (2 ) (3 ) C, # 7.1.1 7.1 (), : # define, ( ), # define : (1 ),, (2 ),, # define,,, : # define array - size 1000 int array[array - size] ;, # define : # define array - size 500 122
:, (3 ),, # define PI 3.l4l59 1 l,,, (4) C,, (5 ) # define,, # define,,, (6 ) # undef, : # define G 9.8 main ( ) { # undef G f1 ( ) # undef, G # undef, f1 G 9.8, (7 ),, [ 7.1] # define R 3.0 # define PI 3.1415926 # define L 2 P I R # define S PI R R main ( ) { printf (L = % f \ ns = % f \ n, L, S) ; : L = 18.849556 S = 28.274333 7.1.2,, 123
: # define ( ), : # define S(a,b) a b area = S( 3, 2) ; S( 3, 2 ), 3 2 a b, 3 2 S( 3, 2) area = 3 2 : ( S ( 3, 2 ) ), # define (a b), ( ), (a b ),,, 7.1 : (1 ) # define, : # define PI 3.1415926 # define S(r ) PI r r : area = S (a + b) ; a + b P I r r r, : area = PI a + b a + b; : area = PI (a + b) ( a + b) ;, : # define S(r ) PI (r ) ( r) S (a + b), a + b r, : PI ( a + b) (a + b) 7 1 (2 ),,, : # define S ( r) PI r r 124
: : S ( ), ( r ) PI r r area = S (a) ; : area = ( r) P I r r (a),,,,, : (1 ),,, S (a + b ), a + b, a + b (2 ),,,, (3 ),,,,,,,, (4 ), (5 ),,, (6 ),, ( ) [ 7.2] # define PR printf # define NL \ n # define D % d # define D1 D NL # define D2 D D NL # define D3 D D D NL # define D4 D D D D NL # define S % s main ( ) { int a,b, c,d ; char string[ ] = CH INA ; a = 1 ;b = 2 ; c = 3 ;d = 4 ; PR( D1, a) ; PR( D2, a,b) ; PR( D3, a,b, c) ; 125
PR( D4, a,b, c,d) ; PR( S, string) ; : 1 1 2 1 2 3 1 2 3 4 CHINA 7.2, C # include, : # include < > # inciude 7.2 file1.c file2.c, 7.2( c), 7 2, ( link ),, (.OBJ),, h ( h head() ), print - format.h h, c, h : (1 ) include, n, n in clude (2 ),, 7.3 7.4 ( ) 126
file1.c file2.h file3.h # include file2.c # include file3.c # include 7 3 file1.c file2.h file3.h # include file3.c # include # include # include file2.c 7.4 (3 ) # include,, file1.c : # include file2.h # include < file2.h >, : ( file2.h), (file1.c),, (< file2.h > ), file1.c 7.1 C A) C B) C) D) 7.2 A) B) C), D), 7.3 # define G 9.8 G A) B) 127
C) D) 7.4 # define K 2 # define X( k) ( ( K + 1 ) k ) C y = 2 ( K + X(5 ) ) ;, A) y B) y 65 C) D) y 34 7.5 # define MIN( a, b) ( a) < (b)? (a) : ( b) { int m = 10,n = 15, k; k = 10 MIN (m, n) ; printf( % d \ n, k) ; A)30 B) 180 C) 15 D) 200 7.6 A) include B) C) include D)# include, 7.7 # define FMT % d, { int b[ ] [ 4] = {1,3,5, 7,9,11,13, 15,17,19,21,23 ; printf( FMT, ( (b + 1) + 1) ) ; printf( FMT, b[2] [2 ] ) ; A) 1,11, B) 1,11 C) 11, 21, D) 11, 21 7.8 : 128 # define X 5 # define Y X + 1 # define Z Y X/ 2 printf,
int a ; a = Y ; printf(% d \ n, Z) ; printf(% d \ n, - - a) ; A) 7 B) 12 C) 12 D) 7 6 6 5 5 7.9 : # define MOD (x,y) x% y int z, a = 15, b = 100; z = MOD (b, a) ; printf(% d \ n, z + + ) ; A) 11 B) 10 C) 6 D) 7.10, 1 # define A 3 + 5 p = A A; 7.11 2 # define PR printf # define NL \ n # define D % d # define D1 D NL # define D2 D D NL { int a = 1, b = 2; PR ( D1, a) ; PR ( D2, a,b) ; 7.12 file2.c, 3 (1 ) file1.c : # define PI 3.14 float circle(float r) { float area = PI r r ; return (area) ; 129
(2 ) file2.c : # include file1.c { float r = 1; printf( area = %f \ n, circle( r) ) ; 7.13 4 # define DOUBLE( r) r r { int y1 = 1, y2 = 2, t ; t = DOUBLE( y1 + y2 ) ; printf(% d \ n, t) ; 7.14 5 # define MAX(a, b) (a > b? ab) + 1 {int i = 7, j = 9 ; printf(% d \ n, MAX (i, j) ) ; 7.15 for 6 # include stdio.h # define N 2 # define M N + 1 # define NUM ( M + 1) M/ 2 {int i, n = 0 ; for ( i = 1; i < = NUM ; i + + ) {n + + ; printf(% d, n) ; printf(\ n ) ; 7.16 7 130 # define PRINT( V) printf(v = % d \ t, V ) {int a,b; a = 1 ;b = 2 ; PRINT(a) ; PRINT( b) ;
7.17 8 # define PR( a) printf(% d \ t, (int) (a) ) # define PRINT( a) PR(a) ;printf( ok! ) { int i, a = 1 ; for ( i = 0 ; i < 3 ; i + + ) P RI NT(a + i) ; printf(\ n ) ; 7.18, 7.19 swap( x, y),, a b 131
8 C, C,,,,,,,, 8.1, ( 8.1 ( a), i 2000, 3) 8 1, C,, i - pointer, i ( 2000 )i - pointer, : i - pointer = &i ; i -, i i - pointer i, i pointer i,, 8.1 ( b), i - pointer i, i - pointer i, i, i 2000, i 2000 8.2 132
,,, i - pointer, i - pointer i - pointer, 8.2, i - i : i = 3; i - pointer = 3 ; pointer, : 3 i - pointer 8 2 8.2.1 C,, : int i, j; int pointer - 1, pointer - 2 ; i j, : pointer - 1, pointer - 2, : pointer - 1 = &i ; pointer - 2 = &j ; 8.3 : : 8 3 (1 ),, pointer - 1 point er - 2, pointer - 1pointer - 2 (2 ), pointer - 1,,, pointer - 1 pointer - 2 8.2.2, ( ), (), : pointer - 1 = 100; ( pointer - 1, 100 ) 133
pointer - 2 = &f; ) : ( pointer - 2, f (1 ) & : : &a a (2 ) : ( ) : p p [ 8.1] main ( ) {int a,b; int p1, p2 ; / p1,p2, / a = 100; b = 10 ; p1 = & a ; / p1 a, p1 a / p2 = &b ; / p2 b, p2 b / p1 = p1 + 2 ; / a 2, a / ( p2) + + ; / b 1,, / printf (% d, % d \ n, p1, p2) ; / a,b / p1 = p2 ; / p1 p2 b / printf( % d, % d \ n, p1, p2 ) ; / b / : 102,11 11,11,, [8.2] a b, a b : main ( ) { int p1, p2, p, a, b ; scanf( % d, % d, & a, &b) ; p1 = &a ; p2 = &b; if ( a < b) {p = p1 ; p1 = p2 ; p2 = p; printf (\ na = % d,b = % d \ n \ n, a,b) ; printf (max = % d, min = % d \ n, p1, p2) ; : 5, 9 < > a = 5, b = 9 134
max = 9, min = 5 a = 5, b = 9, a < b, p1 p2 ( p ), 8.4 (a), 8.4( b), a b,, p1 p2 8 4, ( a b ) 8.2.3,, [8.3], a b 6.5, { int a = 1, b = 2; swap( & a, &b) ; / a, b / printf( 1: a = % d, b = % d \ n, a, b) ;/ p, q a,b p,q / swap(int p, int q) / p, q, a b. p, q a,b / { int c ; c = p; p = q; q = c;/ p, q a, b,a, b / printf( 2: % d, % d \ n, p, q) ; / a,b / : 2 : 2,1 1 : a = 2, b = 1 8.5( a), a b, 8.5( b), a b p q, p q a b (8.5 (c) ), ( 8.5 ( d) ) 135
8 5, a b, p q,, swap(int p, int q) { int c; c = p; p = q; q = c ; printf( 2: % d, % d \ n, p, q) ; c, c, c p c, c,,,, n, : n ; n ;, n ; 8.3, C,, (,, ) ( ) ( ),,, 8.3, 136
, ( a[ 3 ] ),, (, ) 8.3.1, : int a[10] ; / a 10 / int p; / p /, int, int : p = &a[0 ] ; a[ 0]p, p a 0, 8.6 C,, a, : p = &a[0 ] ; p = a ; a, p = a; a p, a p: int p = & a[ 0] ; 8 6 : int p; p = &a[0 ] ; /, p = &a[0 ] ; / : int p = a ; a (a[0 ]) p( p) 8.3.2 p,, : p = 1 ; p ( 1 ) C p + 1 ( p 1), 137
, 4, p + 1 p ( )4, p + 1 p + 1d, d (, d = 2;, d = 4;, d = 1) p & a[ 0], : (1 ) p + i a + i a[i],, a i, 8.7 : a, a + i, p + i, a + id, p + 9 a + 9 &a[9 ], a[9 ] (2 ) ( p + i) (a + i)p + i a + i, a[ i],, a [i] ( a + i),,, a 1000,, a[ 3] : 1000 + 32 = 1006, 1006, a[ 3], [ ], a[i]a + i, (3 ), p[i] ( p + i),, : a[i],p[i], (a + i) (p + i) a, p, p = a 8 7 (4 ),,, p + + ; p - - ;, (5 ),,, [ 8.4] 10,, : { int i, a[ 10], p ; 138 for ( i = 0; i < 10 ; i + + ) scanf ( % d, & a[ i] ) ; for ( p = a ; p < a + 10 ; p + + ) / p < a + 10 p - a < 10,p - a p / printf ( % 4d, p) ; / / printf(\ n ) ;
, : (1 )p + + p,, p a (, a + + ), : for (p = a ; a < p + 10 ; a + + ) printf (% d, a) ; a,,, (2 ), [ 8.5] a 10 : main ( ) { int p, i, a[10] ; p = a ; for ( i = 0; i < 10 ; i + + ) scanf ( % d,p + + ) ; for ( i = 0; i < 10 ; i + +,p + + ) printf ( % d, p) ; printf(\ n ) ; : 1 2 3 4 5 6 7 8 9 0 < > 22153 234 0 0 30036 25202 11631 8259 8237 28483 a, p a ( 8.8 ), for, p a ( 8.8 ), for, p &a[0 ], a + 10, p + +, p a 10 for : 8 8 for (i = 0,p = a ; i < 10; i + +, p + + ) printf (% d, p) ; p & a[ 0], (3 ) p a(p = a), : p + + ( p + = 1 ), p a[ 1], p, a[ 1] p + +, + +,, ( p + + ) p ( p), p + 1 p for : 139
: for (i = 0,p = a ; i < 10; i + + ; p + + ) printf (% d, p) ; for (i = 0,p = a ; i < 10; i + + ) printf (% d, p + + ) ; p, p, p ( p + + ) ( + + p) p, p, p, p p a( & a[ 0 ] ), ( p + + ), a[ 0 ], ( + + p), a[ 1] ( p) + + p 1, ( a [ 0 ] ) + +, a[ 0 ] = 3, (a [0 ] ) + +, a[0 ]4, 1, 1 p a i, : ( p - - )a[i - - ], p, p ( + + p)a[ + + i], p, ( - - p)a[ - - i], p, p + +,, p p 8.3.3,, : main ( ) f( int arr) {int array[10] ; { f(array) ; array,, arr,, : f(int arr) f(int arr[10] ) f(int arr[ ] ),, C ( 8.9 ), arr [8.6] score, 10, : float average( float array) /, array score / 140 { int i ; float aver, sum = 0 ;
8 9 for ( i = 0; i < 10 ; i + + ) sum = sum + array[i] ; / array[i] (array + i), score[i] / aver = sum/ 10; return (aver) ; main ( ) {float score[10], ave ; int i; printf ( Input 10 scores\ n ) ; for (i = 0; i < 10 ; i + + ) scanf(% f, & score[ i] ) ; / scanf( % f, score + i) ; / printf (\ n ) ; ave = average (score) ; printf ( Average score is % 5.2f, ave) ; : Input 10 scores: 100 56 78 98.5 76 87 99 67.5 75 97 < > Average score is 83.40 : (1 ) array,,,, (2 ), ( ) [8.7] : float average( float array, int n) { int i ; float aver, sum = 0 ; for ( i = 0; i < n; i + + ) sum = sum + array[i] ; aver = sum/ n; return (aver) ; 141
{ float score - 1 [5] = {98.5,97,91.5,60,55 ; float score - 2 [10 ] = {67.5,89.5,99, 69.5, 77,89.5, 76.5, 54,60,99.5 ; printf ( Aaverage : % 6.2f \ n, average(score - 1,5 ) ) ; printf ( B - : Aaverage: 80.40 Baverage : 78.20 - average : % 6.2f \ n, average( score - 2, 10) ) ;,,, ( n), average :,,,, [ 8.8] 10, (, ) ( 5.1( 4) ) : { int i, a[ 10 ] ; for( i = 0; i < 10 ; i + + ) scanf(% d, &a[i] ) ; for( i = 0; i < 10 ; i + + ) printf(% 4d, a[ i] ) ; printf( \ n ) ; fun( a, 10) ; for( i = 0; i < 10 ; i + + ) printf(% 4d, a[ i] ) ; printf( \ n ) ; fun(int a, int n) { int i, j, t ; for ( i = 0, j = n - 1 ; i < j ; i + +,j - - ) { t = a[i] ; a[i] = a[j] ; a[j] = t ; [8.9] 10,, (5.2 ) : 142 { int i, t, a[10] = {2, 4,1,6,5, 9,7,0,8,3 ;
for(i = 0; i < 10 ; i + + ) printf( printf(\ ninput t : \ n ) ; scanf(% d, & t) ; del(a, t) ; % 4d, a[i] ) ; for(i = 0; i < 9; i + + ) printf( % 4d, a[i] ) ; printf(\ n ) ; del(int a[10], int t) { int i, j; for(i = 0; i < 10 ; i + + ) if( t = = a[i] ) break; for(j = i ; j < 9; j + + ) a[j] = a[j + 1] ; [ 8.10] 10 (5.4 ) : { int i, a[10] ; for (i = 0; i < 10 ; i + + ) scanf( % d, & a[i] ) ; sort(a,10 ) ; for (i = 0; i < 10 ; i + + ) printf( % d, a[i] ) ; printf( \ n ) ; sort( int p[ ], int n) { int i, j, k, temp; for (i = 0; i < n - 1 ; i + + ) { k = i ; for (j = k + 1 ; j < n; j + + ) if( p[j] < p[ k] ) k = j; temp = p[i] ; p[ i] = p[ k] ; p[ k] = temp; [8.11] a 10, a, ( 5.6) : { int i, x, a[11] = {1,3,5,7,9, 11, 13,15,17,19 ; printf( Input x: \ n ) ; scanf( % d, &x ) ; for( i = 0; i < 10 ; i + + ) printf(% 4d, a[i] ) ; printf( \ n ) ; insert(a, x) ; 143
for(i = 0 ; i < 11; i + + ) printf( \ n ) ; insert(int a, int x) { int i, j ; i = 0; while( i < 10 & & a[i] < x) i + + ; printf( % 4d, a[i] ) ; for( j = 9; j > = i ; j - - ) a[j + 1 ] = a[ j] ; a[i] = x ; [ 8.12], : { int i,k, max; int a[10 ] = {3,4,9,1, 7,6, - 10,10, - 5,2; for ( i = 0; i < 10 ; i + + ) printf( % d, a[i] ) ; printf(\ n ) ; max = find(a, &k ) ; printf(max = % d,k = % d \ n, max, k) ; find(int a[ 10 ], int k) { int i, max; max = a[0 ] ; k = 0 ; for ( i = 0; i < 10 ; i + + ) if (max < a[ i] ) { max = a[i] ; k = i; return max ; 8.3.4,, a: int a[3 ] [ 4] = {{1,3, 5,7, {9, 11,13,15, {17,19,21,23; : (1 ) a, ( 2000 ), a a 0, a 0 (2 ) a + 1 a 1, a + 1 1 (2008) 144
(3 ) a[0 ], a[ 1], a[2 ] ( ), (0, 1, 2 ), a[0 ] 0 0, a[ 0] 2000 (4 ) a[i] + j i j, (a[ i] + j) i j a[0 ] + 2 ( a[ 0] + 2)0 2 (5 ) a[i] (a + i), 8 10 a[ 2] (a + 2) 2, 2 0, &a[2 ] [ 0] (6 ) a[i] [j], ( a[i] + j), ( ( a + i) + j) i j (7 ) a + 1 a[ 1]2008 a + 1, a[1 ] ( ( a + 1 ) ),, 1 0 (8 ), : int ( p) [ 4] ; / p / p 4, : main ( ) { int a[3] [4 ] ; int p1, ( p2) [4 ] ; p1 = &a[3 ] [ 4] ; / p1 / p2 = a + 1; / p2 / : p1 = a + 1 ; p2 = & a[3] [4] ; / / [1 ] 10 8.4 8.4.1 C, 1. [8.13] 145
{ char string[ ] = I love China! ; printf (% s \ n, string) ; string, ( 8.11 ) 2., [8.14] main ( ) { char string = I love China! ; printf (% s \ n, string) ; C,, string, ( )( 8.12 ) 8 11 8 12 :, \ 0 ( 8.12 ),,,, [8.15] a b( ) main ( ) 146 {char a[ ] = I am a boy.,b[ 20], p1, p2 ; int i; for (p1 = a, p2 = b; p1! = \ 0 ; p1 + +, p2 + + ) p2 = p1;/ / p2 = \ 0 ; / /
printf ( String a is : % s \ n, a) ; printf ( String b is : % s \ n,b) ; p1 p2 p1 p2 a b, p1 p2, p1 \ 0 p1 p2, 8.13 8 13 8.4.2,,, : (1 ),, (), (2 ), char str[14] ; str = I love China! ; / str,, /, : char a ; a = I love China! ; a, (3 ) : char a = I love China! ; : char a ; 147
a = I love China! ; : char str[14] = { I love China! ; : char str[14] ; str[ ] = I love China! ;, (4 ),,,,,,,,, : char str[10] ; scanf (% s, str ) ;, : char a ; scanf (% s, a) ; a, a (&a), a, a,, a, a (5 ) [ 8.16] main ( ) {char a = I love China! ; / a I / a = a + 7 ; / a C / printf (% s, a) ; / China! /, :, ( ) [8.17] main ( ) 148 {char a = I LOVE CHINA. ; int i; printf ( The sixth charcter is %c \ n, a[ 5] ) ; / E/
for (i = 0; a[i]! = \ 0 ; i + + ) printf ( % c, a[i] ) ; / I LOVE CHINA. / a[i] ( a + i), a i, : (6 ), printf, char format ; : format = a = % d,b = %f \ n ; printf ( format, a,b) ; printf ( a = % d,b = % f \ n, a,b) ; format, printf, : char format[ ] = a = % d, b = % f \ n ; printf ( format, a,b) ;, : char format[ ] ; format = a = % d,b = % d \ n ;,, N ULL, : int p; p = NULL ; NULL stdio.h, 0, p : p = NULL ; p = \ 0 ; p = 0 ; NULL, # include < stdio.h >, 8.4.3,,,,, 149
[ 8.18] (5.11 ) : # include < stdio.h > # include < string.h > { int i, j ; char ch[ 80 ] ; gets(ch) ; puts(ch) ; j = strlen(ch) - 1; fun(ch, j) ; puts(ch) ; fun(char p, int j) { int i; char t, q; q = p + j ; : asdfgh < > asdfgh hgfdsa for ( ; p < q; p + +,q - - ) { t = p; p = q; q = t ; [8.19], strlen ( 5.12 ) : # include < stdio.h > { int len; 150 char a[ 80 ] ; gets(a) ; puts( a) ; len = lenth(a) ; printf(% d \ n, len) ; lenth(char p) { char q = p; / p q a / while( q! = \ 0 ) q + + ; / q, p / return q - p; / q - p /
asdfgh < > asdfgh 6 [ 8.20], strcat ( 5.13 ) : # include < stdio.h > { char s1 [80 ], s2 [40 ] ; printf( \ n Input string1: ) ; gets( s1) ; printf( \ n Input string2: ) ; gets( s2) ; scat( s1,s2 ) ; printf( \ n New string: % s, s1) ; scat(char s1, char s2) { while( s1! = \ 0 ) s1 + + ; : while( s2! = \ 0 ) { s1 = s2; s1 + + ; s2 + + ; s1 = \ 0 ; Input string1 : country < > Input string2 :side < > New string: countryside [8.21], from to strcpy,\ 0 \ 0 ( 5. 14 ) : # include < stdio.h > # include < string.h > { char from[80], to[80] ; printf( Input string: ) ; scanf(% s, from) ; strcopy( from, to) ; 151
printf( Copied string: % s \ n, to) ; strcopy(char from, char to) { int i; : for (i = 0; i < = strlen(from) ; i + + ) to[i] = from[i] ; Input string: student < > 15 ) Copied string: student [ 8.22], s1 s2 strcmp ( 5. : # include < stdio.h > { int a; char s1 [80 ], s2 [80 ] ; gets( s1) ; gets( s2) ; puts (s1) ; puts( s2) ; a = strcomp( s1,s2) ; if(a > 0 ) printf( ( s1: % s) > ( s2 : % s ) \ n, s1, s2 ) ; if(a = = 0 ) printf( ( s1: % s) = ( s2 : % s ) \ n, s1, s2) ; if(a < 0 ) printf( ( s1: % s) < ( s2 : % s ) \ n, s1, s2 ) ; strcomp(char s1, char s2) { while( s1 = = s2 & & s1! = \ 0 ) {s1 + + ; s2 + + ; : boy < > return s1 - s2; girl < > boy girl ( s1 :boy) < ( s2 :girl) 8.5 8.5.1, 152
,, : main ( ) { int max (int, int ) ; / max / { int ( p) (int, int) ; / p / int a,b, c ; p = max; / max p / scanf( % d, % d, & a, &b) ; c = ( p) (a,b) ; / a, b,max(a,b) / printf( max = % d \ n,c) ; int max (int x, int y) / max / { int z; if ( x > y) z = x ; else z = y; return (z) ; 8.14 p max : (1 ) : 8 14 ( ) ( 1, 2,... ) ;, (2 ), ( ) (3 ) ( p) (int, int),,,, (4 ),, : p = max; p, p = max (a, b) ; (5 ), ( p ) ( p ), ( p), : p, a b, c c = ( p) (a,b) ; (6 ), p + np + + p - - 153
(7 ) 8.5.2,, : ( sub), ( x1 x2 ), x1 x2 sub, f1 f2, sub f1 f2, : f1 f 2 sub(int ( x1) (int), int ( x2 ) (int, int) ) / x1 x2 / { int a,b, i, j ; a = ( x1) (i) ; / f1 / b = ( x2) (i, j) ; / f2 / i j f1 f2 sub x1 x2 sub,, sub, f1 f2 x1 x2, x1 x2 f1 f2, 8.15, sub, x1 x2 8 15 f1 f2, ( x1) (i) f1 (i), ( x2 ) ( i, j) f2 (i, j), sub, sub 8.6 :,, # include < stdio.h > char fun(char ) ; / / { char ch = a, p, q; 154 p = & ch; q = fun (p) ; / q,q = bag ; / puts( q) ; char fun(char s)
{ char t = big ; ( t + 1) = s; / s p ch / return t ; / / : bag : 8.5 8.6,,, 8.7 8.7.1,,,,, : [ ] : int p[4] ; int ( p ) [ 4] ;,,, ( 8.16 ( a) ),,,,, ( ),, 8.16 ( b),, 8.16 (c),, (, ), 8.16 ( d ),, ( ) [8.23],, 0 : # include < stdio.h > # include < string.h > { int i,k ; char temp, str[ ] = { Follow, QBASIC, Great, FORTRAN, Computer ; k = 0 ; for ( i = 1; i < 5 ; i + + ) if( strcmp( str[ k],str[i] ) < 0 ) k = i ; 155
8 16 if( k! = 0) { temp = str[ 0] ; str [0] = str[ k] ; str[ k] = temp; printf( The largest string is \ n% s \ n,str[ 0] ) ; : QBASIC 8.7.2, p q, p q,, : char p ; char q = abc ; p = & q; p q, p q,, p a,, 8.16( c) name ( ) name p( ), name ( 8.17 )name, p [8.24] 156 { char str[ ] = { ENGLISH,MATH,MUSIC, PHYSICS, CHEMISTRY ; char q; int num ;
8 17 q = str; for ( num = 0 ;num < 5 ;num + + ) printf(% s \ n, ( q + + ) ) ; : ENGLISH MATH MUSIC PHYSICS CHEMIST RY str,, str [ 0 ] ENGLISH, str[ 1 ] MATH q str,, q q str, str[ 0 ],, ( q ) q 1, q str,, 8.18 8 18 157
8.7.3 main, main : main ( ), main, : main (int argc, char argv) argc argv main main,, main ( ), main : 1 2 n argc,, argc1 : file1 China Beijing argc 3argv ( ), 8.19, argv [ 0 ] file1, argv [ 1 ] China, argv[ 2] Beijing 8 19 [8.25] file.c, main (int argc, char argv[ ] ) { argc - - ; argv + + ; while( argc > 0) { printf( % s, argv) ; : argc - - ; argv + + ; c : > file Computer and C Language < > : Computer and C Language 158
8.1 a char a = \ nmy Name is Zhang Li. \ \ n ; A) 26 B) 27 C) 28 D) 23 8.2 C, A) B) C) D) 8.3 p1 p2,, a, A) a = p1 B) a = p1 + p2 C) a = p1 - p2 D) p1 = a - p2 8.4 A) cde char s = abcde ; s + = 2 ; printf( % d,s ) ; B) C) cascii D) c 8.5 : int p[ 4] ; p A) B) C), D) 8.6 a[3 ] [ 4], 2 3 A) & a[ 2] [3 ] B) a[ 2] + 3 C) ( a + 2 ) + 3 D) (a[ 2] + 3) 8.7 char language[ ] = { FORTRAN, BASIC, PASCAL, JAVA, C ; : language[1 ] > language[3 ] 159
A) B J B) BASIC JAVA C) FORTRAN PASCAL D) F P 8.8 4 A) int ( p) [4 ] B) int p[ 4] C) int ( p[ 4] ) D) int ( p) [ ] 8.9 A) C main, B) C main, argc argv C) main, D) main (int argc, char argv), argc 1 8.10, char a[4 ] [12] = { China, Japan, Franch, England, p[4] ; int i ; for (i = 0 ; i < 4 ; i + + ) p[i] = a[ i] ; A) a[i] B) p[i] C ) p D) p 0i < 4 8.11 1 fun ( int p1, int p2) { if( p1 > p2 ) printf( % d \ n, p1) ; else printf(% d \ n, p2) ; { int a = 3, b = 7; fun ( & a, &b) ; 8.12, int a[3 ] [ 5], i, j;, 0i < 3,0j < 5, a[i] [j] 160 : (1 ) & a[i] [j] (2 ) a[i] + j (3 ) ( a + i) + j (4 ) & a[ 0] [0 ] + 2
(5 ) a[0 ] + 3 8.13 4 {char s[ ] = { China, Japan, Franch, England ; char p; int i; p = s + 2 ; printf(% s \ n, p) ; 8.14 5 c8.c main( int argc,char argv[ ] ) { while (argc > 1 ) { + + argv; printf(% s \ n, argv) ; - - argc ;, DOS : c8 C Language 8.15 : int s[ 2] [ 3] = {0, ( p) [ 3] ; p = s; p + 1 6 8.16 7 char str[ ] = abc \ 0def \ 0ghi, p = str ; printf( % s, p + 5 ) ; 8.17, 8.18, 8.19, 1 ; 0, level 8.20, 33 8.21 10,, 10 161
8.22, 8.23,,, 8.24, 8.25,,, 3, March, 8.26, m m, 162
9 9.1, C, ( structure), : struct { ; : struct student { int num ; ; char name[20] ; char sex ; int age; float score; char addr [30 ] ; struct student ( struct, ), ( num, name, sex, age, score, addr),, 9.2, int char,, : 1. struct student,, : struct student student1,student2 ; struct student, student1 student2 struct stu 163
dent, struct student, 9.1 9 1 2. : struct { ; : struct student { int num ; char name[20] ; char sex ; int age; float score; char addr [30 ] ; st udent1,student2 ; 3. : struct { ;,, : (1 ) : struct date 164 { int month;
int day; int year ; ; st ruct student { int num ; char name[20] ; char sex ; int age; struct date birthday; / struct date / char addr [30 ] ; student1, student2; birthday struct date, 6 ( = 2 + 2 + 2), student1 stu dent2 struct student, 61 ( = 2 + 20 + 1 + 2 + 6 + 30 )struct student 9.2 9 2 (2 ) : struct student { int num ; float score ; struct student next ; / next struct student / ;, : struct date { int month; int day; int year ; birthday = {11,1,1960 ; 9.3,, :..( ),, : student1.num = 10010; 165
: 10010 student1 num (1 ),,, : student1.birthday.year = 1959 ; : student1.birthday student1 birthday, birthday, (2 ) ( ): sum = student1.score + student2.score ; student1.age + + ; (3 ) C, : struct date { int month; int day; int year ; n1, n2 = {5,20,1999 ; n1 = n2;, n1 n2 (4 ),, : scanf (% d, & student1.num) ; ( student1.num ) printf(% o, &student1) ; (student1 ), : scanf (% d, % s, %c, % d, %f, % s, & student1) ;, [9.1], struct aa { long num ; name[20 ] ; stu = {990001, zhang hua bin ; stu.name[0 ] = Z ; stu.name[ 6] = H ; stu.name[ 10 ] = B ; printf( : % ld, : % s \ n,stu.num, stu.name) ; : :990001, : Zhang Hua Bin 166
9.4 ( ),, 1.,, : struct student { int num ; char name[20] ; char sex ; int age; float score; char addr [30 ] ; ; struct student stu[3] ;, : struct student {int num ; stu[ 3] ; : struct { int num ; stu[3 ] ; 9.3(), 9.4 9 3 2., : 167
struct student : { int num ; char name[20] ; char sex ; int age; float score; char add[30] ; stu[3 ] = {{10101, Li Lin, M,18, 87.5, 103 Beijing Road, {10102, Zhang Fun, M, 19, 99, 130 Shanghai Road, {10104, Wang Min, F, 20,78.5, 1010 Zhongshan Road ; stu,, stu[ ] = {{, {, {;,, : 9 4 struct student {int num ; ; struct student stu[ ] = {{, {, {;,,, : = { ; [ 9.2] : strct aa { long num ; char name[20] ; struct bb { int age ; struct aa first ; stu[2 ] ; (1 ) stu[ 1] 18, 990001, zhang hua bin, ; (2 ) z h b,? (3 ) stu[ 1] stu[0 ] : 168
(1 ) scanf( % d% ld% s, & stu[ 1].age, & stu[ 1].first.num, stu[ 1].first.name) ; (2 ) stu[1 ].first.name[ 0] = Z ; stu[1 ].first.name[ 6] = H ; stu[1 ].first.name[ 10 ] = B ; (3 ) strcpy( stu[ 0].first.name, stu[1 ].first.name) ; 9.5,,, 9.5.1 [9.3] {struct student { long int num ; ; char name[ 20 ] ; char sex; float score ; struct student stu - 1, p; p = &stu - 1 ; stu - 1.num = 89101; strcpy( stu - 1.name,Li Lin ) ; stu - 1.sex = M ; stu - 1.score = 89.5; printf ( No.: % ld \ nname: % s \ nsex: % c \ n score : % f \ n, stu - 1.num, stu - 1.name,stu - 1.sex,stu - 1.score) ; printf (\ n No.: % ld \ n name: % s \ n sex: %c \ n score : % f \ n, ( p).num, ( p).name, ( p).sex, ( p).score) ; p, struct student printf ( p).num, ( p).num p - > num- > :, ; ( p).; p - > : stu - 1.num, ( p).num, p - > num 169
9.5.2,, [9.4] struct student { int num ; char name[20] ; char sex ; int age; ; struct student stu [ 3] = {{10101,Li Lin,M, 18, {10102, Zhang Fun,M, 19, {10104, Wang Min, F, 20; {struct student p; printf (No. Name sex age \ n ) ; for ( p = stu; p < stu + 3; p + + ) printf (% 5d % - 20s % 2c% 4d \ n,p - > num,p - > name,p - > sex, p - > age) ; : 10101 LiLin M 18 10102 Zhang Fun M 19 10104 Wang Min F 20, p struct student, (p stu ), stu : p = &stu[2].num [9.5] : struct aa { int i ; char c ;t ; struct cc { int m ; struct aa n; ; struct cc w, p = & w ;,? : p - > m = 0; / p w m 0 / p - > n = & t; / t p w n / 170
p - > n - > i = 1; / p - > n, t, p - > n - > i t.i / p - > n - > c = A ; / p - > n - > c p - > n t c / :, [ 9.6] : struct aa { int i; struct aa n; w, t, p = & w ;,? : p - > i = 0 ; p - > n = & t; p - > n - > i = 1; p - > n - > n = \ 0 ; n 9.5.3 :,,, ; (), ( ) ; AN SI C, ( ),,,,,,,,,, 9.4 main print, print p struct student, & stu [i] stu void print ( struct student ) ; main ( ) { int i ; for (i = 0 ; i < 3; i + + ) print( & stu[ i] ) ; void print ( struct student p) { printf (% 5d % - 20s % 2c % 4d \ n, p - > num,p - > name, p - > sex,p - > age) ;, 171
9.6 9.6.1, 9.5 ( ),, head,,, :,, head,,,, N ULL ( ), 9 5 :,, ( head ), :,, : struct student {int num ; float score ; struct student next ; ; next,, struct student ( next ),, 9.6 9 6 struct student, next,, next 9.7,, 172
9 7,, 9.8 : struct student,,,,, C 9 8 (1 ) malloc( size) size, ( ),, 0 (2 ) calloc( n, size) n size ;, 0 (3 ) free( ptr) ptr, ptr calloc malloc, n size, ptr C malloc calloc void (),, 9.6.2 typedef C (int, char, float, double, long ), typedef : typedef int INTEGER; : typedef float REAL ;, INTEGER int, REAL float, int i, j; float a, b; INTEGER i, j; REAL a, b;, : typedef struct { int month; 173
int day; int year ; DATE ; DATE, DATE : DAT E birthday; ( struct DAT E birthday; ) DAT E p; (p ) : (1 ) typedef int N UM[100 ] ; ( N UM ) NUM n; ( n ) (2 ) typedef char STR ING; ( STRING ) ST RING p, s[ 10 ] ; ( p, s ) (3 ) typedef int ( POINTER) ( ) ( POINTER, ) POINTER p1, p2; ( p1, p2 POINTER ), : (1 ) ( : int i; ) (2 ) ( : i COU NT ) (3 ) typedef(: typedef int COU NT) (4 ) : : typedef, 9.6.3 1.,, ( [1 ] ) [9.7] : head, p, q, malloc ( ), head, p, q ( 9.9), n s, p, 0,, 9 9 n 0, p (, 9.10 (a) ), 174
9 10 p q - > next, next ( 9.10 ( b) ), q = p, q ( 9.10(c) ) n s n 0, p (, 9.11 ( a) ), n s p q - > next, ( 9.11 ( b) ) q = p, q ( 9.11 (c) ), n 0,, p NULL q - > next( ( 9.12 ) ), 9 11 175
9 12, : (1 ) head, p q head ; (2 ) ; (3 ) 0, p, (7 ) ; (4 ) ; (5 ) q p, q p ; (6 ), (3 ) ; (7 ) q next N ULL, head, p, q p : # include < stdio.h > 176 typedef struct st udent { long num ; float score; struct student next ; LIST ; /, LIST / LIST creat( ) / / { long n; float s ; LIST head, p, q; head = ( LIST ) malloc( sizeof( LIST) ) ; / head / p = q = head; scanf(%ld, % f, &n, &s ) ; / & p - > num, &p - > score, 0 while ( n! = 0 ) / { p = (LIST ) malloc (sizeof (LIST ) ) ; / p / p - > num = n ; p - > score = s; / / q - > next = p; / / q = p; / (q - > next ; ) / / q / scanf( % ld, % f, &n, &s) ;
q - > next = NULL ; / / return ( head) ; / head / void print( LIST head) {, 9.8 / { LIST head; head = creat( ) ; / / print( head) ; / / : (1 ) sizeof sizeof( double) 8 (2 ) malloc,,, struct student, (3 ) creat 2. [9.8] : head, p, p, p,, : void print( LIST head) / head / { LIST p; p = head - > next ; / p / while( p! = NULL) { printf(%ld % 5.1f \ n, p - > num, p - > score) ; p = p - > next ; / p / 3. [9.9] num ( ), : (1 ) ; (2 ),, 9.13 : # include < stdio.h > 177
9 13 178 typedef struct st udent {... LIST ; LIST creat( ) {... void print( LIST head) {... insert(list head, LIST stud) { LIST p, q; q = head ; p = head - > next ; while( p! = NULL & & stud - > num > p - > num ) { q = p; p = p - > next ; / /
stud - > next = p; q - > next = stud; /, { LIST head, stu ; head = creat( ) ; print( head) ; scanf(%ld, % f, &stu.num, &stu.score) ; insert( head, &stu) ; print( head) ; / main,, head, stuinsert, stu studp q, q, p 1, 9.13 (a) while,, p ( p! = N ULL ) p (, stud - > num > p - > num), 9.13, 89107 p 89101q p, p ( p = p - > next ),, 9.13 ( b) stud - > num 89107, p - > num 89109, stud - > num > p - > num, ( p q )while, p stud - > next, p, 9.13( c), q - > next = stud, q next, q ( p ), 9.13 ( d) :,,,, (,,,, ),,, 4. [9.10], 179
,,, : (1 ) ; (2 ), # include < stdio.h > typedef struct st udent {... LIST ; LIST creat( ) {... void print( LIST head) {... int del (LIST head, long num) { LIST p, q; if( head - > next = = NULL) /, / { printf( List is null! \ n ) ; return 0; q = head; / q / p = head - > next ; / p / while( p! = NULL & & num! = p - > num ) { q = p; p = p - > next ; / / if( p = = NULL) /, / else { int a ; { printf( Not been found. \ n ) ; return 0 ; { q - > next = p - > next ; long num ; free( p) ; / p / return 1; LIST head; head = creat( ) ; print( head) ; scanf(%ld, &num) ; a = del( head, num) ; if(a! = 0 ) print( head) ; /, /,,,, 180
9.7 9.7.1,, ( 9.14 ), ( 1000),,, ( union), : union { ; 9 14 : union data { int i ; char ch; float f; a,b, c ; : union data { int i ; ; char ch; float f; union data a,b, c; union data, a, b, c union data, : union { int i ; char ch; float f; a,b, c ;,, a, b, c 4 (4 ), 2 + 1 + 4 = 7 181
9.7.2,,, a, b, c, : a.i (i) a.ch a.f (ch) (f), : printf ( % d, a) ;, printf( % d, a.i)printf(% c, a.c) 9.7.3 : (1 ),,,,, 2.,, : a.i = 1 ; a.c = a ; a.f = 1.5;, a.f, a.i a.c (3 ), : & a, & a.i, & a.c, &a.f, (4 ),,,, : union { int i ; char ch; float f; a = {1, a,1.5; ( ) a = 1; ( ) m = a ; ( ) (5 ),, ( ) (6 ),,, 182
, 9.1 : struct stu {int a ; float b; stu type; A) struct B) struct stu C) stutype D) stu 2 + 4 = 6 9.2 stud age, struct student { int num ; char name[20] ; int age ; stud = {101, Li fun, 18, p = & stud; A) ( p).age B) stud.age C) student.age D) p - > age 9.3, A) B) C) D) 9.4 : struct pupil { char name[ 20 ] ; int age ; int sex; pup[5 ], p = pup ; scanf, A) scanf(% d, pup[ 0].name) ; B) scanf(% d, & pup[ 0].age) ; C) scanf(% d, & ( p - > sex ) ) ; 183
D) scanf( %d, & p - > age) ; 9.5 { union data { int num ; a; float score; char name; a. score = 98.5; a.name = China ; a.num = 901 ; printf( % f \ n, a.score) ; A) 98.5 B) 901 C) China D) 9.6 : struct st { int n ; ; struct st next ; struct st a[ 3] = {5, &a[1 ],7, & a[2],9,\ 0, p; p = & a[ 0] ; 6 A) + + p - > n B) ( p).n + + C) p + + - > n D) p - > n + + 9.7 A) typedef, B) typedef C) typedef D) typedef 9.8 16 IBM PC C,, ss struct s { int m ; ss; char c; double d; A) 8 B) 1 C) 11 D) 2 9.9 184 # include < stdio.h >
{ struct date { int year, month, day; today; printf(% d \ n, sizeof( struct date) ) ; A) 6 B) 8 C) 10 D) 12 9.10 # include < stdio.h > union un { int k; char zf[2 ] ; ch ; { ch.k = 20 ; ch.zf[ 0] = 0 ; ch.zf[ 1] = 10; printf(% d \ n, ch.k) ; A) 10 B) 20 C) 2560 D) 9.11 { struct cmplx {int x; int y; cnum[ 2] = {1,3,2,7 ; printf( % d \ n, cnum[ 0].y/ cnum[0 ].x cnum[1 ].x) ; A) 0 B) 1 C) 3 D) 6 9.12 : struct num { int a ; int b; float f; n = {1, 3,5.0; struct num pn = &n; 185
2 pn - > b/ n.a + + pn - > b 1, ( pn).a + pn - > f 9.13 3 struct s { int num ; ; char name[ 20] ; int age; { struct s stud[3 ] = {{101, Li, 18, {102,Wang,19, {103, Zhang, 21; fun( stud + 2) ; fun( struct s p) { printf( % s \ n, ( p).name) ; 9.14, st ruct student { int num ; ; char name[ 20] ; int age; { struct student p, stud [ 3 ] = {{101, Li, 18, {102,Wang, 19, { 103, Zhang, for( 21; 4 ; p < stud + 3 ;p + + ) printf(% s \ n, 5 ) ; 9.15 6 186 { struct EXAMPLE { struct { int x; int y; in; int a; int b; e ; e.a = 1 ; e.b = 2; e.in.x = e.a e.b; e.in.y = e.a + e.b;
printf( % d, % d, e.in.x, e.in.y) ; 9.16 bt, struct ps { double i ; ; char arr[20] ; { struct ps bt ; printf(% d \ n, 7 ) ; 9.17 8 union ks { int a; int b; ; union ks s[4 ] ; union ks p; { int n = 1, i ; printf(\ n ) ; for(i = 0; i < 4; i + + ) { s[i].a = n ; s[i].b = s[i].a + 1 ; n = n + 2; p = &s[0 ] ; printf( % d,, p - > a) ; printf( % d, + + p - > a) ; 9.18, head data next, data int, next, 9.19, 9.20, ( data int ) ( next) 9.21,, ( 0000 ), 9.22,, ( 0000 ), 9.23,, 187
10 C 10.1 10.1 & < < > > : (1 ), (), (2 ), : 10.1, 1, 1, 0, & : 0 & 0 = 0 ; 0 & 1 = 0 ; 1 & 0 = 0 ; 1 & 1 = 1 : 3 & 5 8,, 3 5, 3 : 00000011 ( & ) 5 : 00000101 00000001 1,, 3 & 5 1 10.2 1, 1 : 0 0 = 0; 0 1 = 1; 1 0 = 1; 1 1 = 1 :060 017 60 17 189
00110000 ( ) 00001111 0011 1111 4 1 a 4 1, a 017 1 10.3, XOR :, 0 () ; 1( ) : 00 = 0; 01 = 1; 10 = 1; 11 = 0; : 00111001 ( 57, 071 ) () 00101010 ( 42, 052 ) 00010011 ( 19, 023 ) 071052, 023( ) :, () (1 ), (0 ) 10.4, ( ),, 0 1, 1 0, 025 25 (0000000000010101) 0000000000010101 1111111111101010 177752, 025 177752, 025-025 10.5 < <, : a = a < < 2 a 2, 0 a = 15, 00001111, 2 00111100, 60, 1 2, 2 2 2 = 415 < < 190
2 = 60, 4,, 1, ( 8 ), a, a = 64, 0, 2, 1 a a 2 < < 1 a < < 2 64 01000000 010000000 0100000000 127 01111111 011111110 0111111100, a = 64 1 2, 2, 0, C 2, 2 n n 10.6 > >, a > > 2 a 2,,, 0 a = 017 : a 00001111, a > > 2 000000111 1 2, n 2 n,, 0, 0 ( ), 0, 1(), 0 1, 0, 10, 1,, a 113755 a: 1001011111101101 a > > 1 : a > > 1 : 0100101111110110 () 1100101111110110 (), a > > 1 045766, 145766 Turbo C C,, 1 10.7 : & =, =, > > =, < < =, =, a & = b a = a & b, a < < = 2 a = a < < 2 191
10.8 ( long int ) ( a & b, a long, b int ), b, 16 0 ; b, 1; b, 0,, 10.1, A) 4 B)2 C) 4 D) 2 10.2, A) 4 B) 2 C) 4 D) 2 10.3 a b int a = 1,b = 2 ; a = ab; b = ba; a = ab; A) a = 1, b = 2 B) a = 2, b = 2 C) a = 2, b = 1 D) a = 1, b = 1 10.4 char a = 56 ; a = a&056 ; printf( % d, % o \ n, a, a) ; A) 40,50 B) 1, 1 C) 56, 60 D) 50, 56 10.5 3 & & 5 1, 3 & 5 2, 3 5 3, 3 5 4 10.6 2 & 3 < < 2 3 5 10.7 5 3 2 < < 1 6 10.8 025 & 012 7 192
10.9 a, ( 0), : a = a & 8 10.10 a 8 1, 8, : a = a 9 10.11 a 4 ( 0 1, 1 0), : a = a 10 10.12 a 47 { unsigned a, b, c, d; scanf ( % o, & a) ; b = 11 ; c = (0 < < 4) ; d = b& c; printf( % o \ n,d) ; 10.13 a 4, 4 4, : a = 12 a < < ( 16-4) 10.14 13 { char a = 0x95,b,c ; b = (a & 0xf) < < 4; c = (a & 0xf0) > > 4; a = b c; printf( % x \ n, a) ; 10.15 getbits, (, 0), : getbits ( value, n1, n2 )Value ( ), n1, n2 getbits ( 0101675, 5, 8), 101675, 5 8 10.16, ( 1, 3, 5,, 15 ) 10.17 2 10.18,, 193
11 11.1 C (file), : ( ),,,,, ( ), C ( ), ( ), ASCII ASCII ( text), ASCII, ASCII,,,,, ( ASCII ),,,,, C, ( ),, C ( record) ( PASCAL ), C ( ) ( ), C, C,, ANSI, 11.2 C,,, ( ),, F ILE FILE, F ILE,,, FILE F ILE efile [ - MAXF ILE] ; 194
efile[ ], MAXFILE, MAXFILE,, : F ILE fp; fp FILE, fp,, n, n ( F ILE ), n (, ), 11.3,, 11.3.1 ( fopen ) ANSI C, fopen( ), fopen : FILE fp; fp = fopen(, ) ; 11.1 11.1 r( ) r + ( ) / w( ) w + ( ) / a( ) a + ( ) / rb( ) rb + ( ) / wb( ) wb + ( ) / ab ( ) ab + ( ) / : if ( ( fp = fopen( file1, r ) ) = = NULL) { printf( cannot open this file \ n ) ; exit(0 ) ; 195
: (1 ), fopen N ULL ( N ULL stdio.h 0) (2 ),,,,, (3 ), :,, stdin, stdout stderr, ( ) stdin, 11.3.2 ( fclose ),,,,, fclose, fclose : fclose( ) ; : fclose( fp) ; fopen fp, fclose :, 0;, ferror 11.4,, 11.4.1 fputc fgetc (putc getc ) 1. fputc fputc, : fputc (ch, fp) ; ch,, fp, fopen fputc( ch, fp) (ch )fp fputc, 196
;, EOF, EOF stdio.h, - 1 2. fgetc fgetc,, fgetc : ch = fgetc ( fp) ; fp, ch fgetc, ch fgetc, EOF, EOF stdio.h - 1, : ch = fgetc ( fp) ; while (ch! = EOF) { putchar (ch) ; ch = fgetc ( fp) ;, - 1, EOF,, ANSI C feof, feof( fp ) fp, feof (fp) 1 (), 0(), : while (! feof (fp) ) { c = fgetc ( fp) ;, feof( fp)0,! feof( fp ) 1, c ( ),, feof(fp) 1,! feof( fp) 0, while [11.1], file.dat, # : # include < stdio.h > { F ILE fp; char ch ; if( ( fp = fopen( file.dat, w ) ) = = NULL) 197
{ printf( Cannot open file. \ n ) ; exit(0) ; ch = getchar( ) ; while(ch! = # ) { fputc(ch, fp) ; putchar( ch) ; ch = getchar( ) ; fclose( fp) ; [ 11.2] 11.1,, : # include < stdio.h > { FILE in, out ; char ch; if( (in = fopen( file.dat, r ) ) = = NULL) printf( Cannot open file.dat. \ n ) ; exit( 0) ; if( (out = fopen( file0.dat, w ) ) = = NULL) printf( Cannot open file0.dat. \ n ) ; exit(0 ) ; while(! feof(in) ) { ch = fgetc(in) ; if(ch > = a& & ch < = z ) ch = ch - 32 ; fclose(in) ; fputc(ch,out) ; fclose(out) ; 11.4.2 fputs fgets 1. fputs fputs, : fputs (str, fp) ; str, fp fputs, 0;, 0 2. fgets fgets, fgets 198
fgets(str,n, fp) ; fp n - 1, str n - 1 EOF, \ 0, fgets str 11.4.3 fread fwrite ANSI C ( fread fwrite), : fread ( buffer,size,count, fp) ; fwrite ( buffer,size,count, fp) ; : buffer:, fread,, fwrite, () size: count : size fp: fread fwrite, count,, fread fwrite, : fread( f,4, 2, fp) ; f, 4, fp 2 ( 4 ), f, fread 2 : struct student - type { char name[10] ; int num ; int age; char addr [30 ] ; stud[ 40] ; stud 40, ( ), for fread 40 : for (i = 0 ; i < 40 ; i + + ) fread ( & stud[i], sizeof( struct student - type),1, fp) ;, for fwrite 199
for (i = 0 ; i < 40; i + + ) fwrite ( &stud[ i],sizeof ( struct student - type),1, fp) ; 11.4.4 fprintf fscanf fprintf fscanf printf scanf, : fprintf fscanf : fprintf(,, ) ; fscanf(,, ) ; : fprintf( fp,% d, % 6.2f, i, t) ; i t %d %6.2f fp i = 3, t = 4.5, : 3, 4.50, fscanf ASCII : fscanf(fp, % d, % f, &i, & t) ; : 3, 4.5 3 i, 4.5 t fprintf fscanf,,, ASCII,,,, fprint fscanf, fread fwrite 11.5,,,,,, 11.5.1 rewind rewind, rewind : rewind( ) 200
[ 11.3] stu.dat, 0, : # include < stdio.h > { F ILE fp; int num ; float score ; if ( ( fp = fopen( stu.dat, w + ) ) = = NULL) { printf( Can t open stu.dat. \ n ) ; exit( 0) ; scanf( % d% f, &num, & score) ; while( num! = 0) { fprintf( fp,% d % f \ n, num, score) ; scanf(% d% f, &num, &score) ; rewind(fp) ; while(! feof( fp) ) { fscanf( fp,% d% f, & num, &score) ; fclose( fp) ; printf( num = % d,score = % f \ n, num, score) ; 11.5.2 fseek,,,,, ( ), ( ), ( ) fseek fseek : fseek(,,) 0, 1, 2, 0, 1, 2, L, 11.1 C, 201
A) B) C) D) 11.2 fp, fopen, A) fp = fopen( file1, ab + ) B) fp = fopen( file1, r + ) C) fp = fopen( file1, wb + ) D) fp = fopen( file1, ab ) 11.3 fgetc, A) B) C) D) B C 11.4 fputc, A) EOF B) 1 C ) 0 D) 11.5 : struct st { char name[ 8] ; int num ; float s [4] ; student[50 ] ; student, fp, A) fwrite( student, sizeof( struct st ), 50, fp ) ; B) fwrite( student, 50 sizeof( struct st ), 1, fp ) ; C) fwrite( student, 25 sizeof( struct st ), 25, fp ) ; D) for( i = 0; i < 50; i + + ) fwrite( student, sizeof( struct st ), 1, fp ) ; 11.6 fread fwrite / A) B) C) D) 11.7 : fseek( fp, - 20L, 2) ; A) 20 B) 20 C) 20 D) 20 11.8 rewind A) B) C) D) 11.9 fclose, 202 A) 0 B) - 1 C) 1 D)
11.10 fp 1 11.11 fopen, fp = fopen( file1.c, r ) ; 2, 3 11.12 fopen, 4 11.13, : 5 stdin 6 7, 11.14 count # include < stdio.h > { FILE fp; long count = 0; if ( ( fp = fopen( let ter.dat, 8 ) ) = = NULL) {printf( cannot open file \ n ) ; exit(0 ) ; while(! feof( fp) ) { 9 ; 10 ; printf( count = % ld \ n, count) ; fclose( fp) ; 11.15, # include < stdio.h > struct rec { int num ; float total; { FILE f ; reout( f = fopen( bin.dat, rb) ; reout( f ) ; fclose( f ) ; 11 ) { struct rec r ; while (! feof( f) ) { fread( &r, 12,1, f ) ; printf( % d, % f \ n, 13 ) ; 203
11.16 feof( fp) fp, feof(fp) 14, 15 11.17 fread( p,50, 2, fp) p, fread 16, 17 11.18 fseek ( fp, 100L, 1 ) fp 18 100 ( 100L L ) 11.19 file1.c file2.c # include < stdio.h > { FILE 19 ; fp1 = fopen( file1.c, r ) ; fp2 = fopen( file2.c, w ) ; while(! feof(fp1 ) ) putchar (getc(fp1 ) ) ; 20 ; while(! feof(fp1 ) ) putc( 21 ) ; fclose( fp1) ; fclose( fp2) ; 11.20 stud - dat i 204 # include < stdio.h > struct studen t - type { char name[ 10] ; int num ; int age ; char sex ; stud[ 10 ] ; { int i ; FILE 22 ; if( (fp = fopen( stud - dat, rb ) ) = = NULL) { printf( cannot open file \ n ) ; exit(0 ) ; scanf(% d, &i) ; fseek( 23 ) ;
fread( 24, sizeof( struct student - type),1, fp) ; printf(% s% d % d%c \ n,stud[i].name, fclose(fp) ; stud[i].num, stud[i].age, stud[i].sex) ; 11.21,, test,! 11.22 5, 3, (,, ),, stud 11.23 employee, : 11.24, 205
1 ASCII
2 C au to break case char const continue default do double else enum extern float for goto if int long register return shor t signed sizeof static struct switch t ypedef union unsigned void volatiel while 3 1 ( ) [ ] - >! + + 2 - - - ( ) & 1 () sizeof 3 / % 2 () 4 5 + - < < > > 6 < < = > > = 2 () 2 () 2 () 7 = =! = 2 () 207
8 & 9 10 11 & & 12 13?: 2 () 2 () 2 () 2 () 2 () 3 () 14 = + = - = = / = % = = = & = = = 2 15, ( ) 4 C, C,, 1, 7 2 : (1 ) (0 ) (0x ) (L 1 ) (2 ) ( ), 208
(3 ) ( ) (4 ) 3 (1 ),, double, double (2 ), ( 0 1) (3 ), (4 ) ( ), ( float)a (5 ) ( ) : 1, 2,, n 1, 2,, n n (6 ) = (7 ) :? 1: 2, 1 ;, 2 (8 ), p - 2 p1 - p2& a ( p p1 p2 ),, (, ) 209
4, (1 ) : int short long unsigned char float double struct union typedef (, int ) : struct { ; union { ; typedef : typedef ; : typedef int COUNT ; (2 ) : auto static register extern (, auto ) : : static float a,b, c; : extern static, auto register 210
5 : ( ) extern static, : static int max (int x, inty) { int z; z = x > y? x :y; return(z) ; 6, ( ) ( ),, 7 (1 ) ; (2 ) ; (3 ) ; (4 ) ; (5 ) : (1 ) if () if () 1 else 2 (2 ) while( ) (3 ) do while ( ) ; (4 ) for (1; 2; 3 ) (5 ) switch () { case 1: 1; case 2: 2; 211
case n: n; default; n + 1; case default,, case, case, break switch (6 ) break (7 ) continue (8 ) return (9 ) goto 8 # define # define ( 1, 2,, n ) # undef # include (< > ) # if # ifdef # ifndef # else # endif 5 C C C, ANSI C C, C, ANSI C C, C (,, ),,, C, 1, : # include < math.h > 212
abs int abscint x) ; x acos double x) ; acos ( double cos - 1 (x) x - 1 1 asin double x) ; asin ( double sin - 1 ( x) x - 1 1 atan double atan x) ; ( double tan - 1/ (x/ y) atan2 double atan2 ( double x, bouble y) ; tan - 1 ( x/ y) cos double cos ( double x ) ; cos( x) x cosh double cosh x) ; ( bouble x cosh( x) exp double exp ( double x) ; e x fabs double x) ; fabs ( double x floor double floor x) ; ( double x fmod double fmod ( double x, double y) ; x/ y frexp double frexp ( double avl, int eptr) ; val ( )x 2 n, val = x 2 n, n eptr x 0. 5 x < 1 log double log(double x) ; logex,ln x log10 double log10 ( double x) ; log10x modf double modf ( double val, double iptr) ; val val, iptr pow double pow (double x, double y) ; x y rand int rand(void) ; 0 32767 sin double sin( double x) ; sinx x sinh double x) ; sinh ( double x sinh (x ) 213
sqrt double sqr t ( double x) ; x x 0 tan double tan ( double x) ; tan( x) x tanh double x) ; tanh ( double x tanh (x) 2. ANSI C < string.h >, < ctype.h > C ANSI C, isalnum int isalnum (int ch) ; ch ( alpha) ( numeric) 1; 0 ctype.h isalpha int isalpha (int ch) ; ch, 1;, 0 ctype.h iscntrl int iscntrl (int ch) ; ch (, 1; ASCII 0 0x1F, 0 ) ctype.h isdigit int isdigit (int ch) ; ch ( 09), 1, 0 ctype.h isgraph int isgraph ( int ch) ; ch, 1; ( ASCII ox21, 0 ox7e ), ctype.h islower int islower ( int ch) ; ch ( a z), 1;, 0 ctype.h isprint int isprint ( int ch) ; ch, 1; ( ), ASCII, 0 ox20 ox7e ctype.h ispunct int ispunct (int ch) ; ch ( ),, 1;, 0 ctype.h isspace int isspace (int ch) ; ch (), 1;, 0 ctype.h 214
isupper int isupper ( int ch) ; ch ( A Z), 1;, 0 ctype.h isxdigit int isxdigit ( int ch) ; ch 16 ( 09, A F, af), 1;, 0 ctype.h strcat char strcat(char str1, char str2) ; str2 str1,str1 \ 0 st r1 string.h strchr char strchr ( char str, int ch) ; str ch,, string.h strcm p int strcmp ( char str1,char str2 ) ; str1 < str2, str1 str2 str1 = str2, 0 string.h str1 > str2, st rcpy char strcpy (char str1, char str2) ; str2 str1 str1 string.h strlen unsigned in t strlen ( char str) ; str ( \ 0 ) string.h strstr char strstr(char str1 char str2 ) ; str2 str1, string.h (str2 ) tolower int tolower (int ch ) ; ch ch ctype.h toupper int toupper ( int ch) ; ch ch 3, # include < stdio.h > stdio.h 215
clearerr void clearerr ( file fp) ; close int close (int fp) ; 0,, - 1 ANSI creat int creat ( char filename, int mode) ; mode, - 1 ANSI eof inteof( int fd) ;, 1 ; 0 ANSI fclose int fclose ( F ILE fp) ; fp, 0, 0 feof int feof ( FILE fp) ;, 0 fgetc int fgetc ( F ILE fp) ; fp, EOF fgets char fgets (char buf, int n, FILE fp) ; fp buf, ( n - 1 ),, buf NULL fopen F ILE fopen ( char filename, char mode) ; mode filename, ( ), 0 fprint f int fprintf ( FILE fp, char format, args, ) ; args format fp fputc int fputc ( char ch, F ILE fp) ; ch fp, ; EOF fputs int fputs ( char str, F ILE fp) ; str fp 0, 0 fread int fread (char pt, unsigned size, un signed n, FILE fp) fp size n, pt, 0 216
fscanf int fscanf ( FILE fp, char format, args, ) ; fp for mat args (args ) fseek int fseek ( F ILE fp, long offset, in t base) ; fp base, offset, - 1 ftell fwrite getc long ftell ( FILE fp) ; int fwrite ( char ptr,unsigned size, un signed n, FILE fp) ; int getc ( FILE fp) ; fp fp ptr n size fp fp fp,, EOF getchar int getchar( void) ;, - 1 getw int getw ( F ILE fp) ; fp ( ) ANSI, - 1 open int open (char file name, int mode) ; mode filename ( ), - 1 ANSI format prin tf int printf ( char format args,) ; args,, putc int putc( int ch, FILE fp) ; ch fp ch, EOF 217
pu tchar int putchar ( char ch) ; ch ch, EOF puts int puts(char str) ; str, \ 0, EOF putw int pu tw ( int w, F ILE fp) ; w ( ) fp ANSI, EOF read int read ( int fd, char buf, unsigned count) ; fd count buf 0, - 1 ANSI rename int rename ( char oldname, char newname) ; oldname 0,,newname - 1 rewind void rewind ( F ILE fp) ; fp, scanf int scanf ( char format args,) ; format args args, args EOF, 0 write int write (int fd, char buf, unsigned count) ; buf count fd ANSI - 1 218
4 ANSI, calloc( )malloc( ) free( ) realloc( ), C, ANSI stdlib.h, C malloc.h stdlib. h ANSI void void, C char, void char calloc void calloc ( unsigned n, unsign size) ; n,, 0 size free void free( void p) p malloc realloc void malloc ( unsigned size) ; void realloc(void p; un signed size) ; size f,, 0 sizesize 6 ( C ) 1 2, 3, 4 () 1 219
2 3 : ( ), ( RAM ROM), ( ), ( ) ( ) 4, ;, ; ( ) 5( ) ; ( ) 6; 7 8 () DOS 1 2 DOS 3 4DOS, : ; ( TYPE, COPY, DEL, REN, XCOPY, ATT RIB) ; (DIR, MD, CD, RD, TREE, TATH) ; ( FORMAT, DISKCOPY, CHKDSK) ; ( VER, DATE, TIME, CLS, PROMP T, HELP ) ; (, ) ; () VINDOW 1 Windows 2 Windows : 3 Windows :,,,, 1 2 3 ( QBASIC FOR TRAN PAS CAL C FOXBASE, ) : 1( ) 220
2 3,, C () C 1, main 2 3 4 C () 1 C () 2 C 3 4 C ( ) () 1,, 2, 3 4 goto () 1 if 2 switch 3 () 1 for 2 while do while 3 continue break 4 () 1 2 () 1 2 3 4, 5,, 221
6 7 (), 8 () 1 : ; 2 () 1, 2 3 4 5,, main () ( ) ( ) 1 2, ( ) 1 2 ( ) ( ( I/ O ), ( I/ O ) 1( FILE ) 2( fopen, fclose) 3 (fputc, fgetc, fputs, fgets, fread, fwrite, fprintf, fscanf ), ( rewind, fseek ) 7 (C ) ( :, 25, C 75 120, 100 ) ( ( 1) ( 40) 1, (41 )( 50) 2, 60 ) A) B ) C ) D),, 222
(1 ), A) CPU B) CPU C) CPU D) CPU (2 ), BIOS A) RAM B) ROM C) D) (3 ) 2424 A) 2 B) 24 C) 32 D) 72 (4 ) bit A) B) C) D) (5 ) A) B) C) D) (6 ) BB A) 187 B) 188 C) 185 D) 186 (7 ), A) B) C) D) (8 ) 3 5 A) 360KB B) 720KB C) 1 44MB D) 1 2MB (9 ) 0 1 A) 0 1 B) 0 2 C) 0 4 D) 0 8 (10) DOS A) B) C) COMMAND.COM D) (11) K1, K2, K3, K4, A) COPY K1 + K2 + K3 K4 B) COPY K1 + K2 + K3 + K4 C) COPY K1 + K2 + K3 + K4 K4 D) COPY K4 + K1 + K2 + K3 (12) A) AUTOEXEC.C B) AUTOEXEC C) ATX.BAT D) AUTO.TXT (13), A) DIR \ > PRN B) TYP E \ > PRN C) DIR > \ PRN D) TYPE > PRN (14) ABC.EXE XYZ, XYZ A) ABC < XYZ > XYZ B) ABC < XYZ > > XYZ 223
C) ABC < < XYZ > XYZ D) ABC > > XYZ (15) \ A \ B \ C, \ A A) MD \ A \ C B) CD..C) MD \ A \ B \ A D) MD \ A \ B (16) A : C, A \ D2, A QR.C, A) \ D2 \ D4 \ QR.C B) D4 \ QR.C C) A: D4 \ QR.C D) AD2 \ D4 \ QR.C (17) DOS, A) TYPE AAB.FOR > CON B) TYPE? AB.FOR C) COPY.C CON D) COPY CON PRN (18) A, AB1.TXT C XYZ.TXT, A) COPY C: \ XYZ.TXT + AB1.TXT B) COPY AB1.TXT > > C: \ XYZ.TXT C) TYPE AB1.TXT > C: \ XYZ.TXT D) AB1.TXT + C: \ XYZ.TXT > C: \ XYZ.TXT (19) A) DISKCOPY A: A: B) DISKCOPY A: C: C) DISKCOPY C: A: D) DISKCOPY C: D: (20) \ A \ B \ C \ D \ XU, \ A.C,.FOR, A) COPY \.C.FOR B) COPY.C \ A \.FOR C) COPY.FOR \.C D) COPY \ A \.C.FOR (21) A) \ \ B) \ C) 074D) \ 0 (22) : 224
char a ; int b; float c ; double d a b + d - c A) float B) int C) char D) double (23) xyz C A) ( x < = y ) & & (y < = z) B) ( x < = y)and( y < = z) C) ( x < = y < = z) D) ( x < = y) & ( y < = z) (24) a = 1, b = 2, c = 3, d = 4, : a < b? ac < d? ad A) 4 B) 3 C) 2 D) 1 (25) x int,, x x = 10 ; x + = x - = x - x ; A) 10 B) 20 C) 40 D) 30 (26) C A) x = 3, y = 5 B) a = b = 6 C) i - - ; D) y = int ( x ) ; (27) x y z t int,, t x = y = z = 1; t = + + x + + y& & + + z ; A) B) 2 C) 1 D) 0 (28) i int, f float, : scanf( i = % d, f = % f, & i, &f) ; 100 765 12 i f, A) 100 < > 765 12 < > B) i = 100, f = 765.12 < > C) 100 < > 765 12 < > D) x = 100 < > y = 765.12 < > (29) x y int,, y for ( y = 1, x = 1; y < = 50; y + + ) { if( x > = 10) break; if( x% 2 = = 1) { x + = 5 ; continue; x - = 3 ; A) 2 B) 4 C) 6 D) 8 (30) : char x[ ] = abcdefg ; char y[ ] = { a, b, c, d, e, f, g ; 225
A) x y B) x y C) x y D) x y (31) A) B) C) D) (32) a b int, b a = 1; b = 10 ; do { b - = a; a + + ; while( b - - < 0 ) ; A) 9 B) - 2 C) - 1 D) 8 (33) j int, for for (j = 10; j > 3 ; j - - ) { if(j% 3)j - - ; - - j; - - j; printf(% d, j) ; A) 6 3 B) 7 4 C) 6 2 D) 7 3 (34) : # define N 3 # define Y ( n) ( ( N + 1 ) n) : z = 2 ( N + Y(5 + 1) ) ;, z A) B) 42 C) 48 D) 54 (35) p1 p2 int, k int, A) k = p1 + p2; B) p2 = k; C) p1 = p2; D) k = p1 ( p2) ; (36) : int arr[ ] = {6,7,8,9,10; int ptr; 226 ptr = arr; ( ptr + 2) + = 2 ;
printf( % d, % d \ n, ptr, ( ptr + 2) ) ; A) 8, 10 B) 6,8 C) 7, 9 D) 6, 10 (37) { int i, k, a[10 ], p[3 ] ; k = 5 ; for(i = 0 ; i < 10; i + + ) a[i] = i ; for(i = 0 ; i < 3 ; i + + ) p[i] = a[ i (i + 1 ) ] ; for(i = 0 ; i < 3 ; i + + ) k + = p[i] 2 ; printf(% d \ n,k) ; A) 20 B) 21 C) 22 D) 23 (38), m int a[2] [3 ] = {{ 1,2, 3, {4,5, 6 ; int m, p; p = &a[0 ] [ 0] ; m = ( p) ( ( p + 2) ) ( ( p + 4) ) ; A) 15 B) 14 C) 13 D) 12 (39) char arr [ ] = ABCDE ; char ptr; for(ptr = arr;ptr < arr + 5; ptr + + ) printf(% s \ n,ptr) ; A) ABCDE B) A C) E D) ABCDE (40) { int x, i; for(i = 1 ; i < = 50; i + + ) { x = i ; if( + + x %2 = = 0) if(x %3 = = 0) if( x% 7 = = 0 ) B D BCDE C C CDE D B DE E A E printf(% d, i) ; 227
A) 28 B) 27 C) 42 D) 41 (41) # include < stdio.h > { union { int k; char i[ 2] ; s, a ; s = & a ; s - > i[ 0] = 0x39; s - > i[1 ] = 0x38 ; printf(% x \ n,s - > k) ; A) 3839 B) 3938 C) 380039 D) 390038 (42) # include < stdio.h > { int a, b, c = 246; a = c/ 100 % 9; b = ( - 1 ) & & ( - 1 ) ; printf(% d, % d \ n, a,b) ; A) 2, 1 B) 3, 2 C) 4,3 D) 2, - 1 (43) : int ( ptr) ( ) ; A) ptr B) ptr int C) ptr, int D) ptr, int (44) 228 # include < stdio.h > struct stu
{ int num ; ; char name[ 10 ] ; int age ; void fun (struct stu p) { printf(% s \ n, ( p).name) ; { struct stu students[3] = { {9801, Zhang,20, fun ( students + 2 ) ; {9802,Wang, 19, {9803, Zhao,18 ; A) Zhang B) Zhao C) Wang D) 18 (45) : typedef union struct date { long i; int k[5] ; char c ; DATE ; { int cat ; DAT E cow; double dog; too; DAT E max ; printf( % d,sizeof(struct date) + sizeof(max) ) ; A) 26 B) 30 C) 18 D) 8 (46) { char a[ ] = programming, b[ ] = language ; char p1, p2 ; int i; p1 = a ; p2 = b; for (i = 0; i < 7; i + + ) if( ( p1 + i) = = ( p2 + i) printf( % c, (p1 + i) ) ; A) gm B) rg C) or D) ga (47) void fun (int a, int b, int c) { a = 456; b = 567; c = 678; 229
{ int x = 10, y = 20,z = 30 ; fun( x,y, z) ; printf(% d, % d, % d \ n,z,y, x) ; A) 30,20, 10 B) 10, 20,30 C) 456, 567, 678 D) 678, 567, 456 (48) int fun (int x, int y, int cp, int dp) { cp = x + y; dp = x - y; { int a,b, c,d; a = 30 ; b = 50 ; fun( a, b, &c, &d) ; printf(% d, % d \ n, c,d) ; A) 50,30 B) 30,50 C) 80, - 20 D) 80, 20 (49) { int i, j ; for (j = 10 ; j < 11 ; j + + ) { for(i = 9 ; i < j; i + + ) if(! ( j% i) )break; if(i > = j - 1) printf( % d, j) ; A) 11 B) 10 C) 9 D) 10 11 (50) 230 # include stdio.h int abc( int u, int v) ; { int a = 24, b = 16, c; c = abc(a,b) ; printf(% d \ n,c) ;
int abc(int u, int v ) { int w ; while ( v) { w = u %v; u = v; v = w ; return u; A) 6 B) 7 C) 8 D) 9 ( 2, 40 ) (1 ) 1 1 20, (2 ) DOS, COMP 2 (3 ) C, DOS A.DAT DOS 3 ( 4) XYZ.BAK.FOR, DOS 4 (5 ) DOS PATH WST.TXT, DOS 5 (6 ) x y int, x = 1, y = 2, 6 1.0 + x/ y (7 ) i j k int, for, k 7 for(i = 0, j = 10 ; i < = j; i + +, j - - ) k = i + j; (8 ) : int a[3] [2 ] = {10,20, 30,40,50,60, ( p) [2 ] ; p = a ; ( ( p + 2) + 2) 8 (9 ) 9 # include < stdio.h > int fun(int x, int y) { static int m = 0, i = 2 ; i + = m + 1 ; m = i + x + y; return m ; { int j = 4, m = 1, k; 231
k = fun (j, m) ; printf( % d,, k) ; k = fun (j, m) ; printf( % d \ n, k) ; (10) fun :, void fun (char str[ ] ) { char m ; int i, j ; for(i = 0, j = strlen( str) ; i < 10 ; i + +, j - - ) { m = str [i] ; str[i] = 11 ; str[j - 1 ] = m; printf(%s \ n, str) ; (11) :,,, # include ctype.h # include stdio.h { char str[ 81], sptr; int i; for(i = 0 ; i < 80; i + + ) { str[ i] = getchar( ) ; if( str[i] = = \ n ) break ; str[i] = 12 ; sptr = str; while( sptr) putchar( sptr 13 ) ; (12), ASCII : abcdefg abceef, - 1 232 # include < stdio.h > { char str1 [100],str2 [100], c ; int i, s; printf(\ n Input string 1 : \ n ) ; gets( str1) ; printf(\ n Input string 2 : \ n ) ; gets( str2) ; i = 0;
while( (str1 [i] = = str2[ i] ) & & ( str1[i]! = 14 ) ) s = 15 ; pintf(% d \ n, s) ; i + + ; (13 ) :,, test.txt, # include < stdio.h > { FILE fp; char str[100] ; int i = 0; if ( ( fp = fopen ( test.txt, 16 ) ) = = NULL) { printf( Can t open this file. \ n ) ; exit(0 ) ; printf( Input a string: \ n ) ; gets( str) ; while( str[i] ) { if ( str[i] > = a& & str[i] < = z ) str[i] = 17 ; fputc( str[i], fp ) ; i + + ; fclose( fp) ; fp = fopen( test.txt, 18 ) ; fgets( str, 100, fp) ; printf(% s \ n, str) ; fclose( fp) ; (14) (long ), # include.< stdio.h > { char b[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F ; int c[64],d, i = 0, base; long n; printf( Enter a number: \ n ) ;scanf( % ld, &n) ; printf( Enter new base: \ n ) ; scanf(% d, &base) ; do { c[i] = 19 ; i + + ; n = n/ base; 233
while( n! = 0) ; printf( Transmite new base : \ n ) ; for( - - i; i > = 0; - - i) { d = c[i] ; printf(% c, b 20 ) ; ( C ) ( (1 )( 40 )1 ; (41) (50) 2 ) (1 ) B (2 ) B ( 3) D (4 ) D ( 5) B (6 ) A (7 ) B ( 8) C (9 ) D ( 10 ) A (11) D (12) C ( 13 ) A (14) B ( 15 ) D (16) C (17) B ( 18 ) A (19) A ( 20 ) A (21) C (22) D ( 23 ) A (24) D ( 25 ) B (26) C (27) C ( 28 ) B (29) C ( 30 ) C (31) D (32) D ( 33 ) B (34) C ( 35 ) B (36) D (37) B ( 38 ) A (39) D ( 40 ) D (41) A (42) A ( 43 ) C (44) B ( 45 ) B (46) D (47) A ( 48 ) C (49) B ( 50 ) C ( 2, 40 ) ( 1) (5 ) (1 ) 1 AUTOEXEC.BAT (2 ) 2 (3 ) 3 COPY A:.DAT PRN ( COPY A:.DAT ) (4 ) 4 REN XYZ \.BAK.FOR RENAME XYZ \.BAK.FOR ( REN RENAME XYZ/.BAK ) (5 ) 5 ATTRIB + R WS T.TXT (6 ) 6 1 0 1 (7 ) 7 10 (8 ) 8 60 234 ( ATTRIB + R ) AT TRIB WST.TXT + R ( ATTRIB WST.TXT )
(9 ) 9 8,17 (10) 10 strlen( str)/ 2 strlen( str)/ 2.0 0.5 strlen (str) j j - 1 11 str[j - 1] ( str + j - 1 ) (11) 12 \ 00 13 + + (12) 14 \ 00 15 str[i] - str2 [i] (13) 16 w w + wt w + t wt + 17 str[i] - 32 str[i] - ( a- A )str[i] - a+ A 18 r r + r + t rt + (14) 19 n%base 20 [ d] 8 1 C 1.1 C 1.2 D 1.3 B 1.4 B 1.5 1 1.6 2 3 1.7 4 { 5 1.8 6 ; 1.9 7 / 8 / 1.10 9 1.11 10, 1.12 11 235
1.13 : { printf( \ n ) ; 1.14 : printf( Very Good \ n ) ; printf( \ n ) ; { int a,b, c, max; scanf( % d % d% d, &a, &b, & c) ; if(a > b) max = a ; else max = b; if(max < c) max = c ; printf( max = % d \ n, max) ; : 5 8 3 < > max = 8 2.1 A 2.2 D 2.3 D 2.4 B 2.5 C 2.6 D 2.7 B 2.8 A 2.9 C 2.10 C 2.11 1 1 236 2 2 2
2.12 3 97 4 6 5 48 2.13 6 1 2.14 7-6 8-4 2.15 9 0 10-4 2.16 11 7 2.17 12 B, 66 2.18 : { char c1 = C, c2 = h, c3 = i, c4 = n, c5 = a ; c1 + = 4 ; c2 + = 4 ; c3 + = 4 ; c4 + = 4 ; c5 + = 4 ; : printf(: %c%c%c%c%c \ n, c1, c2, c3,c4, c5) ; : Glmre 3 3.1 D 3.2 D 3.3 C 3.4 B 3.5 B 3.6 A 3.7 A 3.8 C 3.9 B 3.10 C 237
3.11 1 a = 4, c = 54.36 < > 2 a = 4, c = 54.360001 3.12 3 a = 3, b = 5, c = 6.50, d = 123.79, e = - 198.34 3.13 4 a 3.14 5 100, 144, 64,100 3.15 6 u1 = 10000, u2 = 40000 3.16 7 % i = 10000, j = - 25536 8 3.333333 % 3.17 9 decimal = - 31, hex = ffe1, octal = 177741, unsigned = 65505 3.18 10 scanf(% d% f % f % c% c, &a, & b, & x, &c1, & c2 ) ; 3.19 : 11 3 6.5 12.6aA { float r, s, v; 3.20 : int h; scanf( % f% d, & r, & h) ; s = 2 3.14 r ; v = s h/ 3 ; : printf( : %4.2f, : % 4.2f \ n, s,v) ; :6.71, : 6.71 { float f, c; printf( Enter f: \ n ) ; scanf( % f, &f) ; c = (f - 32 ) 5/ 9 ; printf( c = %4.2f \ n,c) ; 238 :
3.21 : Enter f: 15.6 < > c = - 9.11 # include < stdio.h > { char c1,c2; c1 = getchar ( ) ; c2 = getchar ( ) ; putchar( c1) ; putchar( c2) ; printf(\ nc1 = % c, c2 = % c \ n, c1, c2 ) ; : ab < > ab c1 = a, c2 = b 4.1 D 4.2 C 4.3 D 4.4 B 4.5 D 4.6 D 4.7 B 4.8 B 4.9 C 4.10 D 4.11 C 4.12 A 4.13 C 4.14 1 1 4.15 2 & & 4 239
3 4.16 4 i < = 20 5 i% 2 = = 0 6 f2 + f1 4.17 7 & & & 4.18 8 66778 4.19 9 2 6 10 14 18 4.20 10 DF 4.21 11 a = 8, b = 6 4.22 12 l = 5, m = 4, n = 6 4.23 13 ( 3) 14 if( x! = 0 ) 4.24 : (1 ) if : 240 { long i ; float bonus,bon1,bon2, bon4, bon6,bon10; bon1 = 100000 0.1 ; bon2 = bon1 + 100000 0.075; bon4 = bon2 + 200000 0.05 ; bon6 = bon4 + 200000 0.03 ; bon10 = bon6 + 400000 0.015 ; printf(: ) ; scanf(%ld, & i) ; if( i < = 1e5 ) bonus = i 0.1 ; else if (i < = 2e5) bonus = bon1 + (i - 100000 ) 0.075 ; else if (i < = 4e5) bonus = bon2 + (i - 200000 ) 0.05 ; else if (i < = 6e5) bonus = bon4 + (i - 400000 ) 0.03 ; else if (i < = 1e6) else bonus = bon6 + (i - 600000 ) 0.015 ; bonus = bon10 + (i - 1000000 ) 0.01 ; printf(: %10.2f \ n, bonus ) ;
: :234000 < > : 19200.00 :456000 < > : 29180.00 (2 ) switch : { long i ; float temp,bonus,bon1,bon2, bon4, bon6,bon10; int branch; bon1 = 100000 0.1 ; bon2 = bon1 + 100000 0.075; bon4 = bon2 + 200000 0.05 ; bon6 = bon4 + 200000 0.03 ; bon10 = bon6 + 400000 0.015 ; printf(: ) ; scanf(%ld, & i) ; temp = i/ 100000 ; if( i < = 100000) branch = 0; else if( temp = = 0) else branch = i/ 100000-1 ; branch = temp; switch( branch) { case 0: bonus = i 0.1 ; break ; : case 1: bonus = bon1 + ( i - 100000) 0.075 ;break; case 2: case 3: bonus = bon2 + ( i - 200000) 0.05; break; case 4: case 5: bonus = bon4 + ( i - 400000) 0.03; break; case 6: case 7: case 8: case 9: bonus = bon6 + ( i - 600000) 0.015 ;break; case 10 :bonus = bon10 + (i - 1000000) 0.01; printf(: % 10.2f \ n,bonus) ; 241
4.25 : :100000 < > : 10000.00 :234000 < > : 19200.00 :456000 < > : 29180.00 { int t, a, b, c, d; 4.26 : printf( : ) ; scanf( % d, % d, % d, % d, & a, &b, &c, &d) ; printf( a = % d, b = % d, c = % d, d = % d \ n, a,b, c,d) ; if(a > b) { t = a ; a = b; b = t ; if(a > c) { t = a ; a = c ; c = t ; if(a > d) { t = a ; a = d; d = t ; if( b > c) { t = b ; b = c; c = t ; if( b > d) { t = b ; b = d; d = t ; if(c > d) : { t = c ; c = d; d = t ; printf(: \ n ) ; printf(% d % d % d % d, a, b, c, d) ; :6,8,1, 4 < > a = 6, b = 8, c = 1, d = 4 : 1 4 6 8 242 { int day, x1, x2 ; day = 10; x2 = 1; while( day > 0 ) { x1 = ( x2 + 1) 2;
x2 = x1 ; day - - ; printf( = % d \ n, x1) ; 4.27 : : = 3070 # define M 1000 { int k0, k1,k2, k3,k4, k5, k6, k7, k8, k9; int i, j, n, s; for (j = 2; j < = M; j + + ) { n = 0 ; s = j; if ( s = = 0) for (i = 1 ; i < j ; i + + ) { if(j% i = = 0) { n + + ; s - = i ; switch( n) { case 1 : k0 = i ; break ; case 2 : k1 = i ; break ; case 3 : k2 = i ; break ; case 4 : k3 = i ; break ; case 5 : k4 = i ; break ; case 6 : k5 = i ; break ; case 7 : k6 = i ; break ; case 8 : k7 = i ; break ; case 9 : k8 = i ; break ; case 10 : k9 = i; break; { printf( % d, :, j) ; if(n > 1 ) printf(% d, % d, k0,k1 ) ; if(n > 2 ) printf(, % d, k2) ; if(n > 3 ) printf(, % d, k3) ; if(n > 4 ) printf(, % d, k4) ; if(n > 5 ) printf(, % d, k5) ; 243
if(n > 6 ) printf(, % d, k6) ; if(n > 7 ) printf(, % d, k7) ; if(n > 8 ) printf(, % d, k8) ; if(n > 9 ) printf(, % d, k9) ; printf( \ n ) ; 4.28 : : 6,:1, 2,3 28, :1,2,4, 7,14 496,: 1,2,4,8,16,31,62,124,248 { float k, s1 = 0, s2 = 0,s3 = 0; : for( k = 1 ;k < = 100 ; k + + ) s1 = s1 + k ; for( k = 1 ; k < = 50 ; k + + ) s2 = s2 + k k; for( k = 1 ; k < = 10 ; k + + ) s3 = s3 + 1/ k; printf( = % 8.2f \ n, s1 + s2 + s3) ; = 47977.93 5.1 C 5.2 B 5.3 B 5.4 B 5.5 C 5.6 C 5.7 D 5.8 A 5.9 B 5.10 A 5.11 1 244 5
5.12 2 (2 ) 5.13 3 0 5.14 4 11 5.15 5 p q st 5.16 6 &a[i] [ j] 7 a[i] [i] 8 a[i] [ 3 - i - 1] 5.17 9 i < = 1 10 b [j] [ i] = a[i] [j] 11 i < = 2 5.18 12 &a[i] (a + i) 13 i%4 = = 0 14 printf(\ n ) 5.19 15 1 1 2 3 5 8 13 21 34 55 5.20 16 1111 5.21 17 1 3 4 5 5.22 : # include < stdio.h > # include < string.h > { char a[80 ], b, max; int i = 1, j ; printf( Input a string: \ n ) ; gets( a) ; puts( a) ; b = getchar( ) ; max = a[ 0] ; j = 0 ; while(a[ i]! = \ 0 ) { if(max < a[i] ) { max = a[i] ; j = i; i + + ; 245
5.23 : for( i = strlen( a) + 1 ; i > j ; i - - ) a[i] = a[i - 1 ] ; a[ i + 1] = b; puts( a) ; : wdhyuks < > a < > wdhyauks # include < stdio.h > # include < stdlib.h > { int i, a[11] ; 5.24 : : for(i = 0 ; i < 10 ; i + + ) a[i] = rand( ) % 20; for(i = 0 ; i < 10 ; i + + ) printf( % 3d, a[i] ) ; printf(\ n ) ; for(i = 10; i > = 4 ; i - - ) a[i] = a[i - 1] ; for(i = 0 ; i < 11 ; i + + ) printf(%3d, a[i] ) ; printf(\ n ) ; 6 10 2 10 16 17 15 15 8 6 6 10 2 10 10 16 17 15 15 8 6 246 { int i, k, min, a[10] ; for (i = 0; i < 10 ; i + + ) { scanf(% d, & a[i] ) ; printf(%4d, a[i] ) ; printf(\ n ) ; min = a[0] ; k = 0 ; for (i = 1; i < 10 ; i + + ) if( min > a[i] ) { min = a[i] ; k = i;
printf( : % d \ n, k + 1) ; 5.25 : : 4 6 3 7 9 1 10 22 88 43 < > :6 { int a[ 3] = {5, 19,28; 5.26 : int b[ 5] = {12,24,26,37,48 ; int c[ 10], i = 0, j = 0,k = 0 ; while(i < 3 & & j < 5) if ( a[i] > b[j] ) else : {c[ k ] = b[j] ; k + + ; j + + ; {c[k ] = a[i] ; k + + ; i + + ; while( i < 3 ) {c[ k] = a[i] ; i + + ; k + + ; while ( j < 5 ) {c[ k] = b[ j] ;k + + ;j + + ; for(i = 0 ; i < k; i + + ) printf( % 3d, c[ i] ) ; printf( \ n ) ; 5 12 19 24 26 28 37 48 # define N 10 # define M 10 { int i, j, k, m,n, flag1,flag2, a[ N] [ M], max, maxj; printf(: ) ; scanf(% d% d, &n, & m) ; printf( % d : \ n, n m) ; for( i = 0; i < n ; i + + ) { for( j = 0; j < m ; j + + ) { scanf(% d, & a[i] [j] ) ; printf(%5d, a[i] [j] ) ; 247
flag2 = 0 ; printf( \ n ) ; for (i = 0 ; i < n; i + + ) { max = a[i] [0 ] ; for(j = 0; j < m ; j + + ) if(a[i] [j] > max) { max = a[i] [j] ; maxj = j ; for ( k = 0, flag1 = 1 ; k < n & & flag1; k + + ) if(max > a[ k] [maxj] ) flag1 = 0 ; if ( flag1 ) { printf(% d,% d % d. \ n, i, maxj, max) ; if (! flag2 ) flag2 = 1 ; printf(! \ n ) ; : : :3 4 < > 12 : 1 2 3 4 4 5 3 6 3 5 6 7 < > 1 2 3 4 4 5 3 6 3 5 6 7 0, 3 4. : :3 4 < > 12 : 2 4 90 7 3 4 5 8 9 1 2 3 < > 2 4 90 7 3 4 5 8 9 1 2 3! 5.27 : 248 # include < stdio.h >
{ int i, j,uppn, lown,dign, span,othn; 5.28 : char text[3 ] [80] ; uppn = lown = dign = span = othn = 0 ; for (i = 0; i < 3; i + + ) { gets( text[i] ) ; for ( j = 0; j < 80 & & text[i] [j]! = \ 0 ; j + + ) : if(text[ i] [ j] > = A& & text[i] [j] < = Z ) uppn + = 1 ; else if( text[i] [j] > = a& & text[i] [j] < = z ) lown + = 1 ; else if( text[i] [j] > = 0& & text[i] [j] < = 9 ) dign + = 1; else if( text[i] [j] = = ) else span + = 1; othn + = 1 ; for (i = 0 ; i < 3 ; i + + ) printf(% s \ n, text[ i] ) ; printf(: % d \ n,uppn) ; printf(: % d \ n, lown) ; printf( : % d \ n, dign) ; printf( : % d \ n, span) ; printf( : % d \ n,othn) ; I am a student.< > 123456 < > ASDFG < > I am a student. 123456 ASDFG :6 :10 :6 :3 :1 { char a[5] = {,,,, ; 249
5.29 : int i, j, k; char space = ; for (i = 0 ; i < 5 ; i + + ) { printf( \ n ) ; for (j = 1 ; j < = 3 i; j + + ) printf(% 1c,space) ; for ( k = 0 ; k < 5; k + + ) printf(% 3c, a[ k] ) ; # include < stdio.h > { int i, n; char ch [80 ], tran[ 80 ] ; printf(: ) ; gets(ch) ; printf(\ n : % s, ch) ; i = 0; while ( ch[i]! = \ 0 ) { if( ( ch[i] > = A ) & & (ch[i] < = Z ) ) tran[i] = 26 + 64 - ch[i] + 1 + 64 ; else if( ( ch[i] > = a ) & & (ch[i] < = z ) ) n = i ; : else i + + ; tran[i] = 26 + 96 - ch[i] + 1 + 96 ; tran[i] = ch[ i] ; printf(\ n : ) ; for(i = 0 ; i < n; i + + ) putchar( tran[i] ) ; : asdfzxcv < > : asdfzxcv : zhwuacxe 250 6
6.1 B 6.2 B 6.3 C 6.4 B 6.5 B 6.6 D 6.7 B 6.8 A 6.9 B 6.10 D 6.11 B 6.12 A 6.13 1 6.14 2,, 6.15 3 (1 ) 4 float 6.16 5 a = 4, b = 5 a = 4, b = 6 6.17 6 i = 7; j = 6; x = 7 i = 2; j = 7; x = 5 6.18 7 # x = 2 y = 3 z = 0 6.19 : x = 4 y = 9 z = 5 @ x = 2 y = 3 z = 0 double mypow( double x, int y) { int i; double p ; 6.20 : p = 1.0; for(i = 1 ; i < = y; + + i) p = p x; return p; float f2(int n) { if( n = = 1) return 1 ; 251
else return(f2 (n - 2 ) n) ; float f1 (int x, int n) { int i; float j = 1 ; 6.21 for(i = 1 ; i < = n; i + + ) j = j x; return j ; # include < stdio.h > { float sum( float, float, int ) ; int n = 20; float n1,n2, t ; n1 = 1 ; n2 = 2 ; t = sum(n1, n2, n) ; printf( t = % f \ n, t) ; float sum( float n1, float n2, int n) { int i ; float n3, t = 0; n3 = n2; for ( i = 1 ; i < = n; i + + ) { t = t + n3/ n1; return t ; : 6.22 n3 = n1 + n2; n1 = n2; n2 = n3; t = 32.660259 252 # include < stdio.h > { double sum(int a, int n) ; int a, n; double s; printf( Input a & n: \ n ) ; scanf( % d% d, & a, &n) ;
printf( a = % d, n = % d \ n, a, n) ; s = sum(a, n) ; printf( s = %.1lf \ n, s) ; double sum(int a, int n) { int i ; double t,s ; s = t = a; for( i = 2 ; i < = n; i + + ) { t = t 10 + a ; s = s + t ; return s ; : 6.23 Input a & n : 3 6 < > a = 3, n = 6 s = 370868.0 # include < math.h > { float root(float) ; float a, x; printf(: \ n ) ; scanf( % f, & a) ; x = root( a) ; printf(%5.2f % 8.5f \ n, a, x) ; float root(float a) { float xn0, xn1; xn0 = a/ 2; xn1 = ( xn0 + a/ xn0)/ 2; do { xn0 = xn1; xn1 = ( xn0 + a/ xn1)/ 2 ; while(fabs( xn0 - xn1 ) > = 1e - 5 ) ; return xn1; : 253
6.24 : 2 < > 2.00 1.41421 # include < stdio.h > # include < math.h > { int i, a,b, n = 0; for( i = 10; i < = 20 ; i + = 2 ) for( a = 2 ; a < = i/ 2 ; a + + ) if(isprime(a) ) { b = i - a ; if(isprime( b) ) { printf(%5d = % 2d + %3d, i, a,b) ; n + + ; break; printf( \ n ) ; isprime(int c) { int i ; double k; k = (double) sqrt( ( double) c) ; for ( i = 2 ; i < = k; i + + ) if( c%i = = 0 ) return 0 ; return 1; : 6.25 10 = 3 + 7 12 = 5 + 7 14 = 3 + 11 16 = 3 + 13 18 = 5 + 13 20 = 3 + 17 254 int alphabetic(char c) { if ( (c > a & & c < = z ) (c > A& & c < = Z ) ) else return( 1) ; return(0 ) ; int longest (char string) { int len = 0,i, length = 0, flag = 1, place,point ;
for (i = 0; i < = strlen( string) ; i + + ) { int i ; if (alphabetic( string[i] ) ) else if ( flag) { point = i ; flag = 0; else len + + ; { flag = 1; if(len > length) char line[ 100] ; { length = len ; place = point ; len = 0 ; return( place) ; printf(. \ n ) ; gets( line) ; printf(\ n : ) ; for (i = longest(line) ; alphabetic(line[i] ) ; i + + ) printf(%c, line[ i] ) ; printf(\ n ) ; : 6.26. I am a student < > : student # include < stdio.h > void convert(int n) { int i ; if( (i = n/ 10 )! = 0) convert(i) ; putchar (n %10 + 0 ) ; { int number; printf(\ n : ) ; 255
scanf( % d, & number) ; printf(\ n : ) ; if ( number < 0 ) { putchar ( - ) ; number = - number ; convert( number) ; : 6.27 : - 2345 < > : - 2345 { int year, month, day; int days; printf( : \ n ) ; scanf(% d, % d, % d, &year, & month, &day) ; printf(\ n% d % d % d,year, month,day) ; days = sum - day(month,day) ; if(leap(year ) & & month > = 3) days = days + 1 ; printf( % d. \ n, days) ; int day - tab[13 ] = {0,31,28, 31,30,31,30,31,31, 30,31,30,31; int sum - { int i ; day(int month, int day) for (i = 1 ; i < month; i + + ) day + = day - tab[i] ; return (day) ; int leap(int year) { int leap ; : leap = year %4 = = 0 & & year% 100! = 0 year% 400 = = 0; return( leap) ; 256 : 1999,11,26 < > 1999 11 26 330.
7 7.1 B 7.2 D 7.3 C 7.4 D 7.5 C 7.6 C 7.7 C 7.8 D 7.9 B 7.10 1 3 + 5 3 + 5 7.11 2 1 12 7.12 3 3.140000 7.13 4 5 7.14 5 10 7.15 6 8 7.16 7 v = 1 v = 2 7.17 8 1 2 3 ok! 7.18 : # define MOD(a, b) (a% b) { int a,b; 7.16 : printf( Input a, b: ) ; scanf( % d, % d, & a, &b) ; printf( a mod bis: % d \ n, MOD(a,b) ) ; # define swap(x,y) { int t ; t = x ; x = y; y = t ; { int i, a[10],b[ 10] ; 257
for (i = 0; i < 10 ; i + + ) scanf(% d, & a[i] ) ; for (i = 0; i < 10 ; i + + ) scanf(% d, &b[i] ) ; for (i = 0; i < 10 ; i + + ) swap(a[ i],b[i] ) ; for (i = 0; i < 10 ; i + + ) printf(% 4d, a[i] ) ; printf(\ n ) ; for (i = 0; i < 10 ; i + + ) printf(% 4d, a[i] ) ; printf(\ n ) ; 8 8.1 D 8.2 C 8.3 D 8.4 D 8.5 B 8.6 D 8.7 A 8.8 A 8.9 C 8.10 D 8.11 1 7 8.12 2 5 i + j 3 5 i + j 8.13 4 Franch 8.14 5 C Language 8.15 6 8.16 7 ef 8.17 : 258
{ int n1,n2,n3; int pointer - 1, pointer - 2, pointer - 3; printf(n1, n2,n3 : ) ; scanf(% d, % d, % d, &n1, &n2, &n3) ; pointer - 1 = &n1 ; pointer - 2 = &n2 ; pointer - 3 = &n3 ; if (n1 > n2 ) swap( pointer - 1,pointer - 2) ; if (n1 > n3 ) swap( pointer - 1,pointer - 3) ; if (n2 > n3 ) swap( pointer - 2,pointer - 3) ; printf( : % d, % d, % d \ n,n1, n2, n3) ; swap(int p1, int p2 ) { int p; 8.18 : p = p1 ; p1 = p2, p2 = p ; : n1, n2, n3 :34,21,25 < > :21,25,34 { char str1[20], str2 [20 ], str3[ 20 ] ; char swap( ) ; printf( : \ n ) ; scanf(% s,str1 ) ; scanf(% s,str2 ) ; scanf(% s,str3 ) ; if (strcmp( str1, str2) > 0) swap( str1, str2 ) ; if (strcmp( str1, str3) > 0) swap( str1, str3 ) ; if (strcmp( str2, str3) > 0) swap( str2, str3 ) ; printf(: \ n ) ; printf(% s \ n% s \ n% s \ n, str1, str2,str3 ) ; char swap(char p1, char p2) { char p[ 20 ] ; strcpy( p,p1 ) ; strcpy( p1, p2) ; strcpy( p2, p) ; 259
8.19 : 8.20 : : : asdfg < > wsed < > abdfr < > : abdfr asdfg wsed # include < stdio.h > # include < string.h > { int a,len; char s[ 50] ; gets( s) ; len = strlen( s) ; a = fun( s, s + len - 1) ; if(a = = 1 ) printf( Yes \ n ) ; else printf( No \ n ) ; fun(char s, char t) { while( t > s & & t = = s ) { s + + ; t - - ; if( t > s) return 0; else return 1; 260 { int a[3 ] [3 ], p, i; printf(33 : \ n ) ; for (i = 0 ; i < 3 ;i + + ) scanf( % d% d % d, &a[i] [0 ], &a[i] [1 ], &a[i] [2 ] ) ; p = & a[0 ] [0 ] ; move( p) ; printf(: \ n ) ; for (i = 0 ; i < 3 ;i + + ) printf( % 3d% 3d %3d \ n, a[i] [0], a[ i] [ 1], a[ i] [ 2] ) ; move (int pointer) { int i, j, t ;
for ( i = 0; i < 3; i + + ) for (j = i ; j < 3 ; j + + ) { t = (pointer + 3 i + j) ; ( pointer + 3 i + j) = ( pointer + 3 j + i) ; ( pointer + 3 j + i) = t ; 8.21 : : 33 : 1 2 3 < > 4 5 6 < > 7 8 9 < > : 1 4 7 2 5 8 3 6 9 # include < stdio.h > # include < string.h > { int i; char p, str[10] [ 10 ] ; printf(10 : \ n ) ; for (i = 0; i < 10 ; i + + ) scanf(% s,str [i] ) ; p = &str[ 0] [0] ; sort( p) ; printf( : \ n ) ; for (i = 0; i < 10 ;i + + ) printf(% s \ n, str[i] ) ; sort(char p) { int i, j; char s[10], smax, smin ; for (i = 0 ; i < 10 ; i + + ) { smax = p + 10 i ; for (j = i + 1 ; j < 10; j + + ) { smin = p + 10 j; if (strcmp( smax,smin) > 0 ) { strcpy( s, smin) ; 261
8.22 : strcpy( smin, smax) ; strcpy( smax, s) ; 8.23 : # include < stdio.h > # include < string.h > { char ch,s[ 50 ] ; int d; gets( s) ; puts( s) ; scanf( % d, &d) ; ch = fun( s, d) ; if( ch ) printf(%c % s, ch, s) ; else printf( Can t find! fun(char s, int d) { char ch; int i, len; len = strlen( s) ; \ n ) ; if( d > = len ) return \ 0 ; else { ch = s[ d] ; for( i = d; i < len; i + + ) s[i] = s[i + 1 ] ; return ch; 262 { int a[ 3] = {5, 19,28; int b[ 5] = {12,24,26,37,48 ; int c[ 10], i, k ; k = add(a, b, c) ; for (i = 0; i < k ; i + + ) printf(% 3d, c[i] ) ; printf(\ n ) ; int add(int a, int b, int c)
{ int i = 0, j = 0,k = 0 ; while(i < 3 & & j < 5) if ( a[i] > b[j] ) {c[ k] = b[j] ; k + + ; j + + ; else {c[ k] = a[i] ; k + + ; i + + ; while( i < 3 ) {c[ k] = a[i] ; i + + ; k + + ; while ( j < 5 ) {c[ k] = b[j] ;k + + ; j + + ; return k; 8.24 : # include < stdio.h > { int cle = 0, sle = 0,di = 0, wsp = 0,ot = 0, i ; 8.25 : char p, s[20] ; printf(: ) ; for (i = 0; i < 20 ; i + + ) s[i] = 0; i = 0; while ( ( s[i] = getchar( ) )! = \ n ) i + + ; p = &s [0] ; while ( p! = \ n ) { if ( ( A < = p) & & ( p < = Z ) ) + + cle ; : else if ( ( a < = p) & & ( p < = z ) ) + + sle ; else if( p = = ) + + wsp; else if ( ( p < = 9 ) & & ( p > = 0 ) ) + + di; else + + ot ; p + + ; printf(= % d = % d, cle, sle) ; printf( = % d = % d = % d, wsp,di,ot) ; : LET a = 1990.12.12 < > = 3 = 1 = 1 = 8 = 5 263
8.26 : { char month - name[13] = { illgal month, January, February, int n ; March, April,May, June, July, August, September, October, November, December ; printf( : \ n ) ; scanf( % d, & n) ; if ( (n < = 12) & & (n > = 1 ) ) else printf( % d % s.,n, (month - name + n) ) ; printf( % d., n) ; # include < stdio.h > # include < string.h > { int m ; char str1[ 80], str2[80] ; printf( Input a string: \ n ) ; gets( str2) ; printf( Input m : \ n ) ; scanf( % d, & m) ; if( strlen( str2) < m) printf( Err input! else { copystr( str1,str2, m) ; printf( Result is : % s \ n,str1 ) ; copystr(char p1, char p2, int m) { int n = 0 ; while( n < m - 1) { p2 + + ; n + + ; while( p2! = \ 0 ) { p1 = p2 ; p1 + + ; p2 + + ; p1 = \ 0 ; \ n ) ; 9 9.1 D 264
9.2 C 9.3 B 9.4 A 9.5 D 9.6 A 9.7 B 9.8 C 9.9 A 9.10 C 9.11 D 9.12 1 12 2 6.000000 9.13 3 Zhang 9.14 4 p = stud 5 p - > name ( p).name 9.15 6 2, 3 9.16 7 sizeof( bt) 9.17 8 2, 3 9.18 : typedef st ruct slist { int data ; SLIST ; struct slist next ; SLIST creat - slist( ) { int a ; SLIST h, s, r ; h = ( SLIST ) malloc( sizeof( SLIST) ) ; r = h; scanf(% d, &a) ; while( a! = 0) { s = ( SLIST )malloc(sizeof( SLIST) ) ; s - > data = a ; r - > next = s; r = s; scanf( % d, & a) ; 265
print - r - > next = return h ; \ 0 ; slist( SLIST head) { SLIST p ; p = head - > next ; if( p = = \ 0 ) printf( Linklist is null! else { printf( head ) ; do { printf(- - > % d, p - > data) ; p = p - > next ; while( p! = \ 0 ) ; printf(- int sum( SLIST h ) { int s = 0; 9.19 : 9.20 : SLIST p = h - > next ; - > end \ n ) ; \ n ) ; while( p! = \ 0) { s + = p - > data ; p = p - > next ; return s ; { int s ; SLIST head; head = creat - slist( ) ; print - slist( head) ; s = sum( head) ; printf( s = % d \ n, s) ; 9.18 sum( SLIST h) s + = p - > data ; s + + ; 266 typedef st ruct slist { int data ; struct slist next ; SLIST ; SLIST creat - slist( ) { int a ;
SLIST h, s, r ; h = ( SLIST ) malloc( sizeof( SLIST) ) ; r = h; scanf(% d, &a) ; while(a! = 0) { s = ( SLIST )malloc(sizeof( SLIST) ) ; s - > data = a ; r - > next = s ; r = s; scanf( % d, & a) ; r - > next = \ 0 ; return h ; print - slist( SLIST head) { SLIST p; p = head - > next ; if( p = = \ 0 ) printf( Linklist is null! else { printf( head ) ; do { printf(- - > % d, p - > data) ; p = p - > next ; while( p! = \ 0 ) ; printf(- - > end \ n ) ; SLIST max( SLIST h) { int m = - 32728; SLIST q, p; p = q = h - > next ; while( p! = \ 0) \ n ) ; { if( m < p - > data ) { m = p - > data ; q = p ; return q; p = p - > next ; { SLIST head, p; head = creat - slist( ) ; print - slist( head) ; p = max ( head) ; 267
printf( max = % d \ n, p - > data) ; 9.21 : 268 typedef st ruct slist { long num ; float score[3 ] ; struct slist next ; SLIST ; SLIST creat - slist( ) { int i ; print - long a; float s [3] ; SLIST h, w, r; h = ( SLIST ) malloc( sizeof( SLIST) ) ; r = h; scanf(%ld, % f, % f, % f, & a, &s[ 0], &s[ 1], &s [2] ) ; while( a! = 0) { w = ( SLIST ) malloc( sizeof( SLIST) ) ; w - > num = a ; for(i = 0; i < 3; i + + ) w - > score[i] = s[i] ; r - > next = w ; r = w ; scanf( % ld, % f, % f, % f, &a, & s[0 ], &s[1 ], &s[ 2] ) ; r - > next = \ 0 ; return h ; slist( SLIST head) { SLIST p ; p = head - > next ; if( p = = \ 0 ) printf( Linklist is null! else { printf( head ) ; do { printf(- - > %f,p - > score[ 1] ) ; p = p - > next ; while( p! = \ 0 ) ; printf(- fun( SLIST h) - > end \ n ) ; \ n ) ;
{ int i = 1; SLIST q, p = h - > next ; q = h; 9.22 : while(i < 3 ) { p = p - > next ; i + + ; p - > score[1] = 90 ; { int s ; SLIST head; head = creat - slist( ) ; print - slist( head) ; fun( head) ; print - slist( head) ; # include < stdio.h > typedef st ruct slist { long num ; float score[3] ; struct slist next ; SLIST ; SLIST creat - slist( ) { int i ; long a; float s [3] ; SLIST h, w, r; h = ( SLIST ) malloc( sizeof( SLIST) ) ; r = h; scanf(%ld, % f, % f, % f, & a, &s[ 0], &s[ 1], &s [2] ) ; while( a! = 0) { w = ( SLIST ) malloc( sizeof( SLIST) ) ; w - > num = a ; for(i = 0; i < 3; i + + ) w - > score[i] = s[i] ; r - > next = w ; r = w ; scanf( % ld, % f, % f, % f, &a, & s[0 ], &s[1 ], &s[ 2] ) ; r - > next = \ 0 ; return h ; fun( SLIST h) 269
9.23 : { int i ; float sum, ave; SLIST q, p = h - > next ; q = h; while(p! = NULL) { sum = 0 ; { int s ; for (i = 0; i < 3 ; i + + ) sum = sum + p - > score[i] ; ave = sum/ 3 ; printf( ave = % f \ n, ave) ; p = p - > next ; SLIST head; head = creat - slist( ) ; fun( head) ; typedef st ruct node { int data ; struct next ; linklist ; invert(linklist head) { linklist p, q; p = head - > next ; if ( p! = NULL) { head - > next = NULL ; do { q = p - > next ; p - > next = head - > next ; head - > next = p; p = q; while(p! = NULL) ; 270 10
10.1 B 10.2 D 10.3 C 10.4 A 10.5 1 1 2 1 3 1 4 7 10.6 5 15 10.7 6 6 10.8 7 0 10.9 8 0377 10.10 9 0377 10.11 10 017 10.12 11 a4 10.13 12 a4 10.14 13 59 10.15 : { unsigned a ; int n1, n2; printf( a: ) ; scanf( % o, &a) ; printf( n1,n2: ) ; scanf( % d, % d, &n1, &n2) ; printf( % o \ n,getbits(a, n1-1,n2 ) ) ; getbits( unsigned value, int n1, int n2 ) { unsigned z; z = 0; z = (z > > n1) &(z < < (16 - n2 ) ) ; z = value& z; z = z > > (16 - n2 ) ; return z; 271
: a :173253 < > n1,n2 :5,8 < > 6 10.16 : { unsigned int a ; printf( : ) ; scanf( % o, &a) ; printf( : % o \ n,getbits(a) ) ; getbits( unsigned int value) { int i, j, m,n ; 10.17 : unsigned int z, a, q; z = 0 ; for( i = 1 ; i < = 15 ; i + = 2) { q = 1 ; return z; : for (j = 1 ; j < = ( 16 - i - 1)/ 2; j + + ) q = q 2 ; a = value > > ( 16 - i) ; a = a < < 15 ; a = a > > 15 ; z = z + a q; : 145432 < > : 263 : 043526 < > : 21 272 { unsigned a ; int n; printf(: ) ; scanf(% o, & a) ; printf(: ) ; scanf(% d, &n) ;
if( n > 0 ) { moveright(a, n) ; else printf( : % o \ n, moveright( a, n) ) ; { n = - n; moveleft(a,n) ; printf( : %o \ n, moveleft(a,n) ) ; moverigh t(unsigned value, int n) { unsigned z; z = (value > > n) (value < < (16 - n ) ) ; return(z) ; moveleft( unsigned value, int n) { unsigned z; 10.18 : z = (value > > ( 16 - n) ) ( value < < n) ; return z; : : 152525 < > : 4 < > : 56525 : 152525 < > : - 4 < > : 52535 { unsigned int a ; unsigned int getbits ( ) ; printf(: ) ; scanf(% o, & a) ; printf(: % o \ n, getbits(a) ) ; unsigned int getbits( unsigned in t value) { unsigned int z ; z = value&0100000 ; if(z = = 0100000) z = value + 1 ; else z = value ; 273
return z; : : 2345 < > :2345 : 152525 < > :25253 11 11.1 C 11.2 C 11.3 C 11.4 D 11.5 C 11.6 D 11.7 C 11.8 B 11.9 A 11.10 1 F ILE fp 11.11 2 ASCII 3 11.12 4 NULL 11.13 5 6 stdout 7 stderr 11.14 8 r 9 fgetc( fp) 10 count + + 11.15 11 FILE f 12 sizeof( struct rec) 13 r.num, r.total 11.16 14 15 0 11.17 16 2 274
17 50 11.18 18 11.19 19 fp1, fp2 20 rewind( fp1 ) 21 getc( fp1 ), fp2 11.20 22 fp 23 fp, i sizeof( struct student - type), 0 24 & stud [i] 11.21 : 11.22 : # include < stdio.h > { FILE fp; char str[100], filename[10] ; int i = 0; if ( ( fp = fopen( test,w ) ) = = NULL) { printf(? \ n ) ; exit(0 ) ; printf(?: \ n ) ; getchar ( ) ; while(str[ i]! =! ) { if( str[i] > = a & & str[i] < = z ) str[i] = str[i] - 32 ; fputc( str[i], fp) ; i + + ; fclose( fp) ; fp = fopen( test, r ) ; fgts( str, strlen( str) + 1, fp) ; printf(% s \ n, str) ;fclose(fp) ; # include < stdio.h > struct student { char num[6 ] ; char name[ 8] ; int score[3 ] ; 275
fioat avr; stu[ 5] ; { int i, j, sum ; 11.23 : FILE fp; for( i = 0 ; i < 5; i + + ) { printf( \ n?% d?: \ n, i + 1 ) ; printf(?: ) ; scanf( % s, stu[ i],num) ; printf(?: ) ; scanf% s,stu[i].name) ; sum = 0 ; for (j = 0; j < 3 ; j + + ) { printf?% d., j + 1) ; scanf(% d, & stu[ i].score[j] ) ; sum + = stu[ i].score[j] ; stu[i].avr = sum/ 3.0; fp = fopen( stud, w ) ; for (i = 0 ; i < 5 ; i + + ) if ( fwrite( &stu[i], sizeof( struct student),1, fp)! = 1) fclose( fp) ; printf( file write error \ n ) ; 276 # include < stdio.h > struct emplo { char num[ 6] ; char name[8 ] ; char sex[3 ] ; int age ; char add[20 ] ; int salar; char state[8] ; char class [4] ; em[ 10] ; struct emp { char na[ 8] ; int sal ;
em - ease[ 10] ; { FILE fp1, fp2 ; int i, j ; if ( ( fp1 = fopen( emploee, r ) ) = = NULL) { printf( ) ; exit(0) ; for (i = 0; fread( & em[i], sizeof( struct emplo),1, fp1)! = 0 ; i + + ) { printf (\ n% 8s% 8s% 6s% 6d %8s% 6d% 8s% 8s, em[i].num, em[i].name, em[i].sex, em[i].age, em[i].add, em[i].salar, em[i].state, em[i].class) ; strcpy(em - ease[i].na, em[i].name) ; em - ease[i].sal = em[i].salar; printf( \ n \ n ) ; fp2 = fopen( emp - for(j = 0 ; j < i ; j + + ) sa, w ) ; { fwrite( & em - ease[j], sizeof( struct emp),1, fp2) ; printf(\ n % 12s %6d, em - ease[j].na, em - ease[j].sal) ; printf( \ n ) ; fclose (fp1 ) ; fclose( fp2 ) ; : 010 a1 a2 25 a3 125 a4 a5 011 b1 b2 40 b3 89 b4 b5 012 c1 c2 35 c3 131 c4 c5 013 d1 d2 50 d3 155 d4 d5 a1 125 b1 89 c1 131 d1 155 : emploee,, emp - sa emploee: # include < stdio.h > 277
st ruct emplo { char num[6 ] ; char name[ 8] ; char sex [3] ; in t age; char add[20] ; int salar ; char state[8 ] ; char class[4 ] ; em[ 10] ; { FILE fp; int i, j ; printf(? \ n ) ; for(i = 0 ; i < 4; i + + ) {scanf(% s % s % s % d % s % d % s % s, em[i].num, em[i].name, em[i].sex, & em[i].age, em[i].add, & em[ i].salar, em[i].state, em[i].class) ; printf(% s % s % s % d % s % d % s % s \ n, em[i].num, em[i].name, em[i].sex, em[i].age, em[i].add, em[i].salar, em[i].state, em[i].class ) ; fp = fopen( emploee, w ) ; for (i = 0; i < 4; i + + ) fwrite( & em[i],sizeof( struct emplo),1, fp) ; fclose(fp) ; : 010 a1 a2 25 a3 125 a4 a5 < > 011 b1 b2 40 b3 89 b4 b5 < > 012 c1 c2 35 c3 131 c4 c5 < > 013 d1 d2 50 d3 155 d4 d5 < > 11.24 : 278 # include < stdio.h > # include < string.h > struct emplo { char name[8 ] ; int salar; emp[20], tmp ; { FILE fp; int i, j, n, flag;
char na[ 8] ; int sa ; if ( ( fp = fopen( emp - sa, r + ) ) = = NULL) { printf( ) ; exit( 0) ; for( i = 0 ; fread( & emp[ i],sizeof( struct emplo),1, fp)! = 0; i + + ) printf(\ n% 8s % 6d, emp[i].name, emp[i].salar) ; fclose( fp) ; n = i ; printf(\ n : ) ; scanf(% s,na) ; for (flag = 1, i = 0; flag & & i < n; i + + ) { if( strcmp( na, emp[ i].name) = = 0) { for (j = i; j < n - 1; j + + ) { strcpy(emp[ j].name, emp[j + 1 ].name) ; flag = 0; if(! flag) n - = 1; emp[j].salar = emp[j + 1 ].salar; else printf( \ n! ) ; printf(\ n : \ n ) ; fp = fopen( emp - sa, w ) ; for (i = 0; i < n ; i + + ) fwrite( & emp[i],sizeof(struct emplo), 1,fp) ; fclose(fp) ; fp = fopen( emp - sa, r ) ; for(i = 0; fread( &emp[i], sizeof( struct emplo),1, fp)! = 0; i + + ) printf( \ n %8s %6d, emp[i].name, emp[i].salar) ; fclose(fp) ; 279
[1].C ( ). :,1999 [2].( C ). :,1995 [3].C, :,1992 [4].C, :,1998 [5] Waite M, Prata S.. C. :,1994 [6] Schildt H..C (), :, 1994 [7],(1998 ), :,1998 [8]. C. :,1999 280