( CIP) /. :, 2005. 2 ( ) ISBN 7-5624-3339-9.......... TP311. 1 CIP ( 2005) 011794 : : : : * : : 174 ( A ) :400030 : ( 023) 65102378 65105781 : ( 023) 65103686 65105565 : http: / /www. cqup. com. cn : fxk@cqup. com. cn ( ) * :787 1092 1/16 :24 :599 2005 2 1 2005 2 2 :5 001 7 000 ISBN 7-5624-3339-9 :30. 00,,,
( )
,,,,,,,, 21,, ;, ;,, 1
2, : 1.,,, 2., ; ;, 3.,,,,, 4.,,,,,, :, ;, ;, ;,,,,,, 2002 1
,, C, C,,,, :,,, C 1
2 ; C,, C ; C,, C ; C C + + Java C# C, C, C 3 : 1 2 C, C C, C 3 C, C C, C ASCII,, :, 1 9 ;, 4 ;, 6 7 ;, 2 3 ;, 5 8 ;,,,, :, E-Mail: xiongz@ cqu. edu. cn, xiongz@ cqcnc. com 2005 1
1 1. 1 ( 1) 1. 1. 1 ( 1) 1. 1. 2 ( 2) 1. 2 ( 3) 1. 2. 1 ( 4) 1. 2. 2 ( 4) 1. 2. 3 ( 7) 1. 2. 4 ( 8) 1 ( 12) 2. 1 C ( 13) 2. 2 C ( 15) 2. 2. 1 C ( 15) 2. 2. 2 C ( 17) 2. 3 C ( 17) 2. 3. 1 C ( 17) 2. 3. 2 C ( 20) 2. 4 ( 25)
2 2. 4. 1 ( 25) 2. 4. 2 ( 26) 2. 4. 3 ( 27) 2. 4. 4 ( 28) 2. 4. 5 ( 30) 2. 4. 6 sizeof ( 31) 2. 5 ( 32) 2. 5. 1 ( 32) 2. 5. 2 ( 33) 2. 5. 3 ( 33) 2. 6 C ( 34) 2. 6. 1 C ( 34) 2. 6. 2 C 7 ( 36) 2. 6. 3 C ( 38) 2. 6. 4 ( 38) 2. 7 ( 45) 2 ( 48) 3. 1 C ( 52) 3. 2 C ( 53) 3. 2. 1 ( 53) 3. 2. 2 ( 54) 3. 3 ( 57) 3. 3. 1 ( 57) 3. 3. 2 C ( 58) 3. 4 ( 73) 3. 4. 1 ( 73) 3. 4. 2 while ( 74) 3. 4. 3 do while ( 75) 3. 4. 4 for ( 77) 3. 4. 5 3 ( 80) 3. 4. 6 ( 81) 3. 4. 7 ( 84) 3. 5 ( 85) 3. 5. 1 goto ( 85) 3. 5. 2 break ( 86) 3. 5. 3 continue ( 88)
3. 6 ( 90) 3. 6. 1 ( 90) 3. 6. 2 ( 97) 3. 7 ( 105) 3. 7. 1 ( 105) 3. 7. 2 ( 106) 3. 7. 3 ( 107) 3. 7. 4 ( 108) 3. 7. 5 ( 109) 3. 7. 6 ( 109) 3. 7. 7 ( 110) 3 ( 111) 4. 1 ( 116) 4. 1. 1 ( 116) 4. 1. 2 C ( 117) 4. 2 ( 118) 4. 2. 1 ( 118) 4. 2. 2 ( 121) 4. 2. 3 ( 123) 4. 2. 4 ( 126) 4. 3 ( 128) 4. 4 ( 133) 4. 5 ( 137) 4. 5. 1 ( 138) 4. 5. 2 ( 140) 4. 5. 3 ( 144) 4. 6 C ( 147) 4. 6. 1 ( 147) 4. 6. 2 ( 148) 4. 6. 3 ( 149) 4. 6. 4 ( 150) 4. 6. 5 ( 151) 4. 6. 6 ( 156) 4 ( 157) 3
5. 1 ( 165) 5. 1. 1 ( 165) 5. 1. 2 ( 167) 5. 2 ( 174) 5. 2. 1 ( 174) 5. 2. 2 ( 176) 5. 3 ( 177) 5. 3. 1 ( 178) 5. 3. 2 ( 180) 5. 4 ( 181) 5. 5 ( 182) 5 ( 183) 4 6. 1 ( 188) 6. 1. 1 ( 188) 6. 1. 2 ( 190) 6. 1. 3 ( 196) 6. 1. 4 ( 197) 6. 1. 5 ( 200) 6. 1. 6 ( 207) 6. 1. 7 ( 211) 6. 1. 8 ( 219) 6. 2 ( 224) 6. 2. 1 ( 224) 6. 2. 2 ( 225) 6. 2. 3 ( 227) 6. 2. 4 ( 234) 6. 2. 5 ( 237) 6. 3 ( 241) 6. 3. 1 ( 241) 6. 3. 2 ( 242) 6. 3. 3 typedef ( 246) 6. 3. 4 ( 247) 6. 3. 5 ( 249)
6. 3. 6 ( 251) 6. 4 ( 254) 6. 4. 1 ( 254) 6. 4. 2 ( 255) 6. 5 ( 259) 6. 5. 1 ( 259) 6. 5. 2 ( 260) 6 ( 262) 7. 1 ( 265) 7. 1. 1 ( 265) 7. 1. 2 ( 266) 7. 2 ( 268) 7. 2. 1 ( 268) 7. 2. 2 ( 269) 7. 2. 3 ( 270) 7. 2. 4 ( 271) 7. 2. 5 ( 271) 7. 2. 6 ( 272) 7. 2. 7 ( 273) 7. 3 ( 273) 7. 4 ( 277) 7. 4. 1 ( 277) 7. 4. 2 ( 279) 7 ( 280) 5 8. 1 ( 282) 8. 1. 1 ( 282) 8. 1. 2 ( 282) 8. 1. 3 ( 284) 8. 2 ( 285) 8. 2. 1 ( 285) 8. 2. 2 ( 287) 8. 3 ( 287) 8. 3. 1 ( 287)
8. 3. 2 ( 294) 8. 3. 3 ( 296) 8. 3. 4 ( 297) 8. 4 ( 301) 8. 4. 1 ( 301) 8. 4. 2 ( 303) 8. 5 ( 304) 8. 5. 1 ferror ( 304) 8. 5. 2 clearerr ( 305) 8 ( 305) 6 9. 1 ( 310) 9. 1. 1 C ( 310) 9. 1. 2 ( 314) 9. 1. 3 ( 327) 9. 1. 4 ( 332) 9. 2 ( 339) 9. 2. 1 ( 339) 9. 2. 2 ( 342) 9. 2. 3 ( 344) 9. 2. 4 ( 346) 9. 2. 5 ( 348) 9. 2. 6 ( 350) 9. 3 ( 352) 9. 3. 1 ( 352) 9. 3. 2 ( 354) 9. 4 ( 356) 9. 4. 1 C ( 356) 9. 4. 2 C ( 357) 9. 4. 3 C ( 368) 9 ( 372)
1. 1 1 1. 1. 1,,,,,,, 1 ),,,,,,
,, :, ;, 2 ) 2,,,,,,,,,,,,,, 3 ), Fortran C,, ; C + + Smalltalk,,, :,,,, 1. 1. 2,,,
2 :,,,,, 1. 1 3 1. 1 1. 2,,,, :, ( Nikiklaus Wirth) : = +
1. 2. 1 4, 3,, ;, ;,, ( ),,,,, 2 :,, ( ),, 3, ( ),, ; ;,, C,, C ;,, 1. 2. 2 1 ),,,,
,, : 1 ; 2 : 2 a b; 3 a > b, a b ;, b a ; 4 2 ) 5,, : sinx = x - x 3 3! + x5 5! - x 7 7! + x9 9! -,, (, 10-5 ),,,,, 5,, : 1 ; 2 0 n( n 0 ) ; 3 n + 1 n( n 1) ; 4 3 ; 5,,,, :,,, 0,, ; 5
,,, 3 ) 6,,, 3, 3, 3 : ( ), :,,, 1. 2 ( ),, 1. 2, : ( ) 2, 1. 3 1. 3, :,, 1. 4 1. 4
1. 2. 3,,, ( ), A3 0 B A3 0 B,,,, 3 : N S ( ) 1 ), ( 1. 5 ), ( ),, 7 1. 5 3 1. 2 1. 3 1. 4 2 ) N S N S Nassi Shneiderman2,,, N S 1. 6 1. 6 N S
3 ) 8 N S,,,,,,,, 3 : ( 1) S1 S2 ( 2) If( ) then 1 Else 2 ( 3) Switch( ) 1: S1 2: S2 n: Sn ( 4) While( ) ( ) 5 ) Do ( ) While( ) 3,, 1. 2. 4,,
1. 1 r h ( 1) :,, ( 2) : SF - 01 r h 3. 14* r* r* h /3 1. 2 2 a b,, 2 ( 1) :, a b a* b < 0 ( 2) : SF - 02 2 a b If( a* b < 0 ) then Else S S ( a* b) 1 / 2 S ( a + b) /2 1. 3 ( 1) : : 4 100 ; 400 : 2, ( 2) : SF - 03 year If( ( year MOD 400 = 0) ( year MOD 4 = 0) ( year MOD 100 0) ) then Else year year : MOD ( ) 1. 4 3 x y z, ( 1) : x y x; y z, y > z y ; y < z z x ( 2) : SF - 04 9
10 x y z 3 If( x < y) then x y If( y > z) then Else y If( x > z) then Else 1. 5 z x ( 1) : ( 1 7 ), ( 2) : SF - 05 w Switch ( w) w = 1: " Monday" w = 2: " Tuesday" w = 3: " Wednesday" w = 4: " Thursday" w = 5: : " Friday" w = 6: " Saturday" w = 7: " Sunday" 1. 6 n, n! ( 1) :, 1 : 1 n ; ( 2) : SF - 06 n k < - 0 p < - 1 While( k < n) k < - k + 1 p = p* k
n! p 1. 7,, 1234 4321 ( 1) :, 2 : ; :, ; ; ; : 10 ( 10 ), ; ;, ( 2) : SF - 07 n s < - 0( 0) Do m < - n MOD 10 s < - s* 10 + m n < - n /10( n 10 n) While( n > 0 ) s 1. 8 : 5, 4, 3 1 100 100,? ( 1) : x, y, z : x + y + z = 100 11 5x + 3y + 1 3 z = 100 x : 1 19 y : 1 33 z : z = 100 - x - y ( 2) : SF - 08
x < - 1 y < - 1 While( x < 19) While( y < 33) z = 100 - x - y If( 5* x + 3* y + z /3 = 100) then x y z y < - y + 1 x < - x + 1 1 12 1. 1, 8 800 ; 8 1 80 1. 2 a 1 x + b 1 y = c 1 a 2 x + b 2 y = c 2 1. 3, 2 1. 4 A( x 1, x 2,, x n ) n R n, A O( 0, 0,, 0), n 1. 5 A m n, 1. 6 36, 36, 4, 3, 2 1,? 1. 7, : 0 999 1. 8, : 1 ) 1000, ; 2 ) 2000, ; 3 ) 3000, ; 4 ) 5000, ; 1. 9 :, 1. 10 ( - 1 ),, 9
C 2. 1 C 13 C,,, ( ), C C, Martin Richards 20 60 BCPL( Basic Combined Programming Language) 20 70, Ken Thompsom BCPL, B ( BCPL ) PDP-7 UNIX, PDP-11 UNIX, Dennis. M. Ritchie Vrian. W. Kernighan B, 1972 C ( BCPL ), 20 80, ( ANSI) C C, ANSI C ( 1989 ) C, C,,, C : C, C 32, 9
14,, C C, ( bit),, C, C, C,,, C C C : ( ),, C C, 34, + - * / ( &&) ( ) (! ), ( &) ( ) ( ) ( ^) ( < <, > > ), + + - - + = - = * = / = C, C C, C, ( UNIX MS-DOS Windows ) C,,, C,, C Microsoft C Turbo C Borland C,, C ( ), C 2. 2 C 2. 2. 1 C,
C C 2 C 2. 1 3, / * * / / * main * / { float ave, x, y, z; /* * / scanf( % f, % f, % f, &x, &y, &z) ; /* scanf * / ave = ( x + y + z) /3; / * * / printf( % f, ave) ; / * printf * /, main, C, main; { 4 C ; scanf C, ; printf C, / * * / 2. 2, : main, myputc, myputc,,,, / * ex2-2. cpp * / #define SIZE 80 / * ( SIZE) ( SIZE) * / { void myputc( char ch) ; / * myputc * / char str[ SIZE] ; / * ( str) * / int j; / * ( j) * / gets( str) ; / * gets * / for( j = 0 ; str[ j]! = \0 ; j + + ) myputc( str[ j] ) ; / * myputc* / void myputc( char ch) / * myputc* / { char cc; cc = ( ch > = a && ch < = z )? ch + A - a : ch; putchar( cc) ; 15
16 main myputc, myputc C,,, C : : C ; C : ( ) C main( ) : C main( ),, ( include ), ;,, ;, : C,, \ C, C /, / ( : scanf( ) printf( ) getchar( ) putchar( ) ) / * * /,, C : C, C C,, ( ; ) C,, C ( ), C, 2. 2. 2 C C,,, C : : (, ) : void myputc( char ch) : { ;
C ; C 3 : ( main( ) ) C,,, 2. 3 C 2. 3. 1 C,,,,,,,, 17 1 ) C, C 91 : a, b, c,, z A, B, C,, Z 0, 1, 2, 3,, 9 _ ( ) * & %! < >., ; : /? { [ ] ^ : C 32 ( ),, C, C ( ) :
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while 18, C,, : define undef include ifdef ifndef endif line error elif pragma C,, ;, :, ( Turbo C 1 32 ) C,,, C, total TO- TAL ToTaL total C, ( ),, name / xm( ) sex/ xb( ) age /nl( ) salary /gz( ) : a b1 file_name _buf : 5 c int up. to file name. 2 ) C, C,,, C C
C C, 2. 1,,,, 3 ), C,, 2. 1 C, C 3 :,,,,, 2. 2 SIZE,,, C,,,, :, : [ ] [, 2 ] ; ;, float. radius, length, area ; : : ( int float char ) ; : 19
2. 3. 2 C 1 ) 20 ( 1) C, 3 : : 0 888, - 123, 0, 45900 : 0, 0777, - 011 : 0x, 0x123, 0xff ( 2) C : 4, int short long unsigned int C,,,, 2. 1 IBM-PC 2. 1 IBM-PC ( 16 ) int 2-32768 32767 123 short int 2-32768 32767 123 long int 4-217483648 217483647 123l unsigned int 2 0 65535 123u char 1-128 + 127 A : l L ; u : ; : int a, b; / * a, b * / long x, y; / * x, y * / unsigned c, d; / * c, d * / 2. 3 { int x;
C long y; unsigend z; x = 888; y = 0x7ffa; z = 65535; printf( % d, % ld, % u \ n, x, y, z) ; : 888, 32762, 65535 2 ) ( 1), C,, : : 3 : 888. 88 0. 88 12. 345-54. 321 :, e E 3 : < > E( e) < > : : : : 1. 23e5 1. 23 10 5 ; 12. 345e - 3-0. 12345 E + 3 123E - 4 ; e3 2. 1e3. 5. e3 e 4, - 10 38 10 38, 7 ( 2) C, float double 6 7, 4, 8, 24 15 16, 8 : float double ; ; : float x, y; / * x, y * / 2. 4 double z; / * z * / { 21
float x1, x2; double y1, y2; x1 = 3141. 59; x2 = 0. 000001; y1 = 3141. 59; y2 = 0. 000001; printf( % f, % lf\ n, x1 + x2, y1 + y2) ; : 3141. 590089, 3141. 590001,,, 3 ) 22 ( 1) C 2 :, a? A \,, ( \ ) ASCII ( ) ( ), : \ r \ n \ 123 \ x12 2. 2 C 2. 2 C \0 NULL \ b \ n \r \ f \ t \ \ \ \ \0oo o \xhh h 1, ASCII,
C, ASCII ( 2) 1, 1 : char ; : char ch; / * ch * / : C, 2. 5 { int a; char b; a = 1; b = A ; printf( % c \n, b + a) ; : B ( 3) C,,, : Hello, World! a 1,, : abcd \ t123 \ n \ \ \ 111 11, : abcdefg abcdefg : + 1, ASCII, NULL, NULL ASCII 0, \ 0 CHINA : 23 C H I N A \0 01000011 01001000 01001001 01001110 01000001 00000000 : a,, ; 1
, ( a \0 ) 2 4 ) 24 C,, ( ), : #define : #define PI 3. 1415926 #define EOF - 1 C,,, : ;,,, ; 5 ) C,, : [ ] [ = ] [, 2 [ = 2 ] ] ; : float radius = 2. 5, length = 6. 5, area ; int a = 3, b = 5; char c = a ;, c a ASCII 2. 5 : { int a = 1; char b = A ; printf( % c \n, b + a) ;
C 2. 4 2. 4. 1, ( ),,,,,,,, C, C : ( + - * / % + + - - ) ; ( > > = < < = = =! = ) ; ( &&! ) ; ( = ) ; ( & ^ < < > > ) ; (? : ) ; (, ) ; ( * &) ; ( sizeof) ; ; 1 (. - > ) ; 12 ( [ ] ) ; 13 ( ) C C,,,,,, 25
C 15, 2. 3 2. 3 1 ( ) [ ] - >. 2! ++ - - - * & sizeof 3 * / % 4 + - 5 < < > > 6 < < = > > = 7 = =! = 8 & 9 ^ 10 26 11 && 12 13?: 14 = + = - = * = / = % = & = ^= = > > = < < = 15,,,, C,, 5 + x - 1, x + -,, x +, 5 + x, -, x = y = 5, y =,, y =, = x 2. 4. 2 C :, + -
C, 5 : + - * / %, :, : 7 /5 = 1, - 7 /5 = - 1,, : 7% 5 = 2, - 7 % 5 = - 2, 7% - 5 = 2 C C, : a* b / c - 1. 5 + a, a + b - 2. 3 % 6 :, ( ) + + - - * / % + - 2. 4. 3 27 =, : x = 10 : 10 x ; y = x + 1 : x + 1, y ( ) : = : : a = 3 b = a c = b = a = 3 :,, ( ) C, : a = ( b = 5 ), b = 5, 5 a = ( b = 5) a = 5, a 5, 5 2. 6 { int m, n, k; m = ( n = 4) + ( k = 10-7) ;
printf( m = % d \n, m) ; : m = 7,,, : j, j = 5. 5 j 5; x, x = 5 x 5. 00000;, 8 : num float, 2. 5, num = ( int) num, num? 2. 4. 4 28 1 ) C, =,, : = : a X = b ( X + - * / % ) : = ( ) a = a X b : x + = 3 /* x = x + 3* / y* = x + 6 /* y = y* ( x + 6), y = y* x + 6 * / x % = y - 5 /* x = y% ( y - 5) * /,, C,, : + = - = * = / = % = < < = > > = & = ^= = 10, 5 : 2. 7 { + = - = * = / = % =
C int a = 3, b = 9, c = - 7; a + = b; c + = b; b + = ( a + c) ; printf( a = % d, b = % d, c = % d \ n, a, b, c) ; a + = b = c; printf( a = % d, b = % d, c = % d \ n, a, b, c) ; a = b = c; printf( a = % d, b = % d, c = % d \ n, a, b, c) ; : a = 12, b = 23, c = 2 2 ) a = 14, b = 2, c = 2 a = 2, b = 2, c = 2 + + - - 2,, 2 : ( + + - -, + + i - - i), ( + + - -, i + + i - 2. 8 { int j = 10, k; printf( j = % d \ t, j) ; printf( j + + = % d \ n, j + + ) ; printf( j = % d \ t, j) ; printf( + + j = % d \ n, + + j) ; : j = 10 j + + = 10 j = 11 + + j = 12 C, : - ), : i k, i = 3, 29
i = 3 k = ( i + + ) + ( i ++ ) + ( i + + ) k 9 i ++ : i, i 1 k = 3 + 3 + 3 = 9 i = 3 k = ( + + i) + ( ++ i) + ( + + i) k 18 + + i: 1, i 3 i 1, i 6 k = 6 + 6 + 6 = 18 30, ( ), i + + + j, ( i + + ) + j : C,,, ( i + + ) + ( i + + ) + ( i + + ), : { int m, n, i = 3, j = 3; m = i + + + i + + + i + + ; /* m < - 3 + 3 + 3 * / n = + + j + ( + + j) + ( + + j) ; /* n < - 6 + 6 + 6 * / printf( % d \n, m) ; /* : 9 * / printf( % d \n, n) ; /* : 18 * / { int i = 3, j = 3; printf( % d \n, i + + + i + + + i + + ) ; / * : 3 + 4 + 5 5 + 4 + 3 * / printf( % d \n, + + j + ( + + j) + ( + + j) ) ; / * : 4 + 5 + 6 6 + 5 + 4 * / 2. 4. 5 C :, :
C 1, 2,, n, C, : int i, j; i = 3; j = ( + + i, i + +, i + +, + + i) ; i 7, j 7 { x = 1; y = ( 1, 2, 3, 4) ; z = ( z = 2, z* 5, z + 3 ) ; : x 1, y 4, z 5 a * a = 3 * 5, a * 4 = 60: a = 3 * 5, a = 15; 4 = 60, = 60 ( a = 3 * 5, a * 4), a + 5 = 20 : a = 3 * 5, a = 15; a * 4 = 60; a + 5 = 20, = 20 C,,,,, for, 2. 9 { int x, y, z; x = 1, 2, 3, 4; y = ( 1, 2, 3, 4) ; z = ( z = 2, z* 5, z + 3) ; printf( x = % d, y = % d, z = % d \ n, x, y, z) ; : x = 1, y = 4, z = 5 31 2. 4. 6 sizeof sizeof C,, x, sizeof( x) sizeof( int) 2 sizeof,,, sizeof( double), sizeof( ) C, sizeof 2. 10
{ printf( int: % d \ n, sizeof( int) ) ; printf( short: % d \ n, sizeof( short) ) ; printf( long: % d \ n, sizeof( long) ) ; printf( unsigned: % d \n, sizeof( unsigned) ) ; printf( unsigned short: % d \ n, sizeof( unsigned short) ) ; printf( unsigned long: % d \n, sizeof( unsigned long) ) ; printf( char: % d \ n, sizeof( char) ) ; printf( unsigned char: % d \ n, sizeof( unsigned char) ) ; printf( float: % d \n, sizeof( float) ) ; printf( double: % d \n, sizeof( double) ) ; 32 2. 5 2. 5. 1 C,,, C, : Char < int < unsigned < long < float < double 2. 2 2. 2 char short, int ; float, double ;, 2. 11 { int a, b, c, d, e; float f;
C a = 3; b = 5; c = 2; d = a + + ; printf( a = % d, d = % d \n, a, d) ; a = 3; e = + + a; printf( a = % d, e = % d \ n, a, e) ; f = b / c; e = b% c; printf( f = % f, e = % d \ n, f, e) ; : a = 4, d = 3 a = 4, e = 4 f = 1. 000000, e = 1, C : 33 2. 5. 2, ( ), :,, 2. 12 { char c = a ; int j = 3, k; float x = 5. 0; k = ( x* j) + ( c + j) ; printf( % d, % f \n, k, ( x* j) + ( c + j) ) ; : 115, 115. 000000
2. 5. 3 34 C, ( ) : ( ) < > :, :, 2. 13 { int k = 5, n, m; float x = 1. 2; m = ( int) x* k; n = ( int) ( x* k) ; printf( x = % f, m = % d, n = % d \n, x, m, n) ; : x = 1. 2, m = 5, n = 6 2. 6 C 2. 6. 1 C, 2 : C 5 1 ) C, C,, ;, : ; :
C x = y + z; / * ( ) * / y + z; / *,, * / i + + ; / * 1, i 1* / 3 : : : y = x + 1; ; = ; ; : ( ) ;,, : printf( Hello, World! \ n ) ;, ; ( ), : while( getchar( )! ; = \r ) : 35 2 ), C 9, 3 : : if switch : do while while for : break goto continue return 3 ) C {,, : { x = y + z; a = b + c; printf( % d% d, x, a) ; ;,
,, : { char ch; ch = getchar( ) ; putchar( ch) ; 2. 6. 2 C 7 36,,, C,,,, ( Nikiklaus Wirth) : + =,,,, 2. 3 2. 3,,,,, ( ), 3 :, ;, ;,
C,,,, : ( 1),,,,,, ( ) ( 2),,, ;,,,,, ( 3),,, ( 4),,,,, ( 5),,,, : ;, ;,, ;, ; ( 6),, ;, 37
,,,, ( 7),,, : ; ;, ; ; ; 2. 6. 3 C 38, C, C,, Turbo C Turbo C ( 2. 4 ) : : DOS : C: \ TC > TC < CR > Windows : : < F3 > < F10 > File Load : < F10 > File New : C : < F2 > < F10 > File Save < CR > < CR > : < F9 > Compile /MakeEXEfile < CR > < F6 >, : < Ctrl - F9 > Run Run : < Alt - F5 > Run User Screen 2. 4 Turbo C : < Alt - X > < F10 > File Quit C S : Turbo C + + IDE
C 2. 6. 4,, C, I / O I / O stdio. h, I / O, : #include stdio. h 4 : printf scanf getchar poutchar 1 ) printf printf, : printf(, ) ; : ( ) : 3,, %, : % [ ] [ ] [ ] [ * ] : :, 2. 4 2. 4 printf 39 d o x u f e g c s ( ) ( O) ( Ox) % f% e
40 :, % 5d 5 m. n, m, 1, n, % 5. 2f 5, 2,, 0,, 0, printf( % 05d \ n, 88 ) ; : 00088 :, ;, : l( long), ; h ( short), * : 2. 14 * { int i = 5, j = 6; printf( % d, % 0 * d \ n, i, i, j) ; : 5, 00006 2. 15 { char c; int a = 88 ; float b = 12. 34; c = b ; printf( % d, % 04d, % - 04d, % x, % c \n, a, a, a, a, a) ; printf( % c, % d \ n, c, c) ; printf( % f, % 6. 3f \n, b, b) ; printf( % s \ n, abcdefghijk ) ; printf( % 11. 4 s\ n, abcdefghijk ) ; : 88, 0088, 88, 58, X
C b, 98 12. 340000, 12. 340 abcdefghijk abcd 7 a, % 04 d 4, a 88 0 % - 04d 4,, a 88 % x a, 88 58 % c a, a 88, ASCII 88 X 8 c, % d c, b ASCII 9 b, % 6. 3d b, 3, b 12. 34, 2, 0 11 % 11. 4 s\ n abcdefghijk, 4, 7 2. 16 { printf( 10 : 60 = > 8 : % o \ n, 60) ; / * 10 60 8 74 * / printf( 8 : 074 = > 10 : % d \ n, 074 ) ; / * 8 074 10 60 * / printf( : % d \ n, 622) ; /* 10 * / printf( : % d \ n, - 622) ; /* 10 * / printf( 10 : 116 = > 16 : % x \n, 116) ; / * 10 116 16 74 * / printf( 16 : 0X74 = > 10 : % d \ n, 0X74) ; / * 16 0x74 10 116 * / : 10 : 60 = > 8 : 74 8 : 074 = > 10 : 60 : 622 : - 622 10 : 116 = > 16 : 74 16 : 0X74 = > 10 : 116 41 2 ) scanf scnaf : scanf(, ) ;
42 : ( ) : 2,, &, x, &x x : %, % [ * ] [ ] [ ] : :, : l( long), ; h( short), * :, : scanf( % 4d% * 3d% f, &j, &p ) ; 12345678. 9, j = 1234, p = 8. 9 :, 2. 5 2. 5 scanf d o x u f e c s ( ), 2 : 1, ; 2, scanf : scanf, ( ) :
C scanf( % 5d, &a) ; 12345678 12345 a, scanf( % 4d% 4d, &a, &b) ; : 12345678 1234 a, 5678 b scanf,,, TAB C, TAB, : scanf( % d, &a) ; : 12A345 12 a scanf( % d% d% d, &a, &b, &c) ;, Tab a = 5, b = 6, c = 7, : 5 6 7 5 TAB 6 TAB 7,, : c scanf( % c% c% c, &a, &b, &c) ; : pqr p a, b, q : scanf( % d, % d, % d, &a, &b, &c) ;,, : 5, 6, 7 = 7 scanf( a = % d, b = % d, c = % d, &a, &b, &c ) ; a = 5, b = 6, c,, % *, : scanf( % d, % * d, % d, &a, &b) ; : 1, 2, 3, 1 a, 2, 3 b, : ( Tab) ; ; scanf,, 2. 17 { int a, b, c; printf( % d \n, scanf( % 3d - % 2d - % 4 d, &a, &b, &c) ) ; printf( a = % d, b = % d, c = % d \ n, a, b, c) ; 43
, 123-45 - 6789,, : 3 a = 123, b = 45, c = 6789 12-345 - 6789, 2, : 2 a = 12, b = 34, c = xx( ) 3 ) getchar putchar 44 / getchar ; putchar putchar : putchar( ) : ( ) : : putchar( A ) ; A putchar( x) ; x putchar( \ n ) ;,, : getchar : getchar( ) :,, : char ch; ch = getchar( ) ; getchar( ), : getchar,, / stdio. h I / O : stdin stdout stderr, I / O 2. 18 { putchar( ) char ch1 = N, ch2 = E, ch3 = W ; putchar( ch1) ; / * ch1 * / putchar( ch2) ; putchar( ch3) ; putchar( \ n ) ; / * * /
C putchar( ch1) ; putchar( \n ) ; / * ch1, * / putchar( E ) ; putchar( \ n ) ; / * E, * / putchar( ch3) ; putchar( \n ) ; : NEW 2. 19 { char c1, c2; int b; c1 = A ; N E W printf( Please input two character: ) ; c2 = getchar( ) ; / * 1 c2* / b = 65 ; putchar( A ) ; putchar( \101 ) ; putchar( c1) ; putchar( b) ; putchar( c2) ; : Please input two character: : b : AAAAb 45 2. 7, :,,,,, 3 C ( ) / ;
46, : ( ),, :,, ; ; ; 2. 20 ( ) H, F,? ( 1) :, ( 2) :, X, Y, :, : X + Y = H 2X + 4Y = F X = 4H - F, Y = F - 2H 2 2 ( 3) :,,,, : ( ) : ( ) : ; ; ( ) : : ; ( 4) ( 2. 5 ),,,, : : : h, f input: h, f output: x, y 2. 5
C x < - ( 4* h - f) /2 y < - ( f - 2* h) /2 : x, y :, x = ( 4* h - f) /2 x = ( 4 * f - h) /2,,,,,,, ( 5) : { int h, f, x, y; printf( Enter Number of Heads and Feet: h, f\ n ) ; scanf( % d, % d, &h, &f) ; x = ( 4* h - f) /2; y = ( f - 2* h) /2; printf( Head = % d, Feet = % d \ n, h, f) ; printf( Cock = % d, Rabbit = % d \ n, x, y) ; ( 6) ( ) ( 7) ( ),,,, h f,, 2. 21, ( 1) :, : ( 2) ( ) : : input: ( length), ( width) output: ( girth), ( area) : length, width girth < - ( length + width) * 2 area < - length * width = 2 ( + ) = 47
48 : girth, area ( 3) : { float length, width, girth, area; / * * / printf( Enter the length and width: ) ; scanf( % f, % f, &length, &width) ; / * * / girth = ( length + width) * 2; / * * / area = length* width; / * * / printf( Girth = % f\n, girth) ; / * * / printf( Area = % f\ n, area) ; / * * / : Enter the length and width: 1. 2, 5. 0 2. 22 Girth = 12. 400000 Area = 6. 000000, ASCII, ASCII ( 1) : a ASCII = 97, A ASCII = 65, a - A 32;, ASCII = 32 ( : a - A ) ( 2) ( ) ( ) ( 3) : { char c1, c2; printf( Input a lowercase letter: ) ; c1 = getchar( ) ; putchar( c1) ; printf(, % d \ n, c1) ; c2 = c1-32; /* : c2 = c1 - ( a - A ) * / printf( % c, % d \ n, c2, c2) ; : Input a lowercase letter: a a, 97 A, 65
C 2 2. 1 ( 1) ( A) abc ( B) abc ( C) abc ( 2) C, ( D) a ( A) 0 L ( B) 4962711 ( C) 0. 123456 ( D) 2. 178e12 ( 3) int a = 3 ;, a + = a - = a * a;, a ( A) 3 ( B) 0 ( C) 9 ( D) - 12 ( 4), ( a = 2, b = 5, b + +, a + b ) ( A) 7 ( B) 8 ( C) 6 ( D) 2 ( 5) : int k, a, b; unsigned long w = 5; double x = 1. 42; C ( A) x% ( - 3) ( B) w + = - 2 ( C) k = ( a = 2, b = 3, a + b) ( D) a + = a - = ( b = 4) * ( a = 3) ( 6) 18 /4* sqrt( 4. 0) /8 : ( A) int ( B) float ( C) double ( D) ( 7) int x, y; double z;, scanf ( A) scanf( % d% lx, % le, &x, &y, &z) ;( B) scanf( % 2d * % d% lf &x, &y, &z ) ; ( C) scanf( % x % * d% o, &x, &y) ; ( D) scanf( % x% o% 6. 2f, &x, &y, &z) ; ( 8), a1, a2, c1, c2 10, 20, A, B,, int a1, a2; char c1, c2; scanf( % d% d, &a1, &a2) ; scanf( % c% c, &c1, &c2) ; ( < > ) ( A) 1020AB ( B) 10 20 AB ( C) 10 20 AB ( D) 10 20 AB ( 9) C, sizeof( float) ( A) sizeof ( B) ( C) ( D) 49
50 2. 2 ( 1)?? ( 2) C,?? ( 3) C,? 2. 3, ( 1) # include < stdio. h > { / * * / int a = 5 ; char c = a ; float f = 5. 3; double m = 12. 65; double result; / * * / printf( a + c = % d \ n, a + c) ; printf( a + c = % c \ n, a + c) ; printf( f + m = % f\ n, f + m) ; / * * / printf( a + m = % f \n, a + m) ; printf( c + f = % f\ n, c + f) ; / *, * / result = a + c * ( f + m) ; printf( double = % f\ n, result) ; ( 2) { int a = 5, b = 7; float x = 67. 8564, y = - 789. 124; char c = A ; long n = 1234567 ; unsigned u = 65535 ; printf( % d% d \ n, a, b) ; printf( % 3d% 3d \n, a, b) ; printf( % f, % f \n, x, y) ;
C printf( % - 10f, % - 10f\ n, x, y) ; printf( % 8. 2 f, % 8. 2f, % 4f, % 4f, % 3f, % 3f\ n, x, y, x, y, x, y) ; printf( % e, % 10. 2e \ n, x, y) ; printf( % c, % d, % o, % x\ n, c, c, c, c) ; printf( % ld, % lo, % x \ n, n, n, n) ; printf( % u, % o, % x, % d \ n, u, u, u, u) ; printf( % s, % 5. 3s \ n, COMPUTER, COMPUTER ) ; ( 3) # include < stdio. h > { int i, j, k; int m, n, p; i = 8; j = 10 ; k = 12; / * * / m = + + i; printf( i = % d \ n, i) ; printf( m = % d \ n, m) ; / * * / n = j - - ; printf( j = % d \ n, j) ; printf( n = % d \ n, n) ; / * * / p = ( + + m) * ( n + + ) + ( - - k) ; printf( k = % d \ n, k) ; printf( p = % d \ n, p) ; ( 4) { 2. 4 int x = 0, y = - 1, z = - 1; x + = - z - - - y; printf( % d, % d, % d \n, x, y, z) ; 51
( 1) r = 1. 5, h = 3, scanf,,, ( 2) ASCII ( 3) 30, 90, 52
C 3. 1 C 53, C C,,, 3 3 :,, 3. 1,,,,,,
3. 1 3 3. 2 C 54 3. 2. 1 2, 2 C 6, 3. 1 : 3. 1 < < = > > = ==! = ( ) ( ) 6, 4 ( < < = > > = ), 2 ( ==! = ), 2 2 ( ) : 1 2 : a + b > c - d, x > 3 /2, a + 1 < c, - i - 5 * j == k + 1, ( a > b) > ( b < c )
C C,, 1, 0, ; : a = 3, b = 2, c = 1, : a > b, 1 ( a > b) == c ( a > b 1, c ), 1 b + c < a, 0 : : d = a > b, f = a > b > c, : d 1 f 0 ( a > b 1, c, f = 0) : num1 = 3, num2 = 4, num3 = 5, : num1 > num2 = 0 ( num1 > num2)! = num3 = 1 num1 < num2 < num3 = 1 ( num1 < num2) + num3 = 6, num1 < num2 = 1, 1 + 5 = 6 : C 1, 0,,, 3. 1 / * ex3-1. cpp * / { int x = 1, y = 2, z; z = 5-1 > = x + 2 < = y + 3; /* * / printf( z = % d \n, z) ; : z = 1 55 3. 2. 2, x > = 0 x > = 0 x < 10, C 3, 3. 2 3. 2! ( ) && ( ) ( )
56 : &&,,,!,,,!, &&, :! ( ) &&( ) ( ) : ( a > b) && ( b > c ) b > d : ( ( a > b) && ( b > c) ) b > d, : b a c, b d :! && 2 ( ) : 1 2, : ( x > = 0) && ( x < 10) ( x < 1) ( x > 5 )! ( x == 0) ( year% 4 == 0) &&( year% 100! : x 0 x < 10 : x < 1 x > 5 : x 0 100, year 400! ( b == c) d < a a + b > c && x + y < b = 0) ( year% 400 == 0) : year 4 : b c a > d : a + b > c x + y < b 2, 1 0 3. 3 3. 3 &&( ) ( ) 5 > 0 && 4 > 2 2, 5 > 0, 4 > 2,, 2, 5 > 0 5 > 8, 2 5 > 0,,! ( ), ;,! ( 5 > 0) C, 1, 0,, 0, 0 : 5 3 0, 5&&3, 1
C : 5 0, 1 : num = 12,! num 0; num > = 1 && num < = 31 1; num num > 31 1 C, 2 :,, :,, :,, ;,, : a && b && c, a, b ; a b, c a, b c,, 3. 2 57 3. 2 3. 3 a b c, a, b c, a, b ; a b, c, 3. 3 3. 2 / * ex3-2. cpp * / { int a, b, c, d, j, k; a = 1, b = 2, c = 3, d = 4, j = k = 1 ; ( j = a > b) &&( k = c > d) ; / * * / printf( j = % d, k = % d \ n, j, k) ;
58 : j = 0, k = 1 : ( j = a > b), j 0, ( j = a > b) &&( k = c > d) 0 ;, ( k = c > d), k 1 3. 3 { int a, b, c, d; / * * / a = 1; b = 2; c = 0; / * * / d = a + b > c; / * * / printf( " % d \n", d) ; d = a + b < c; / * * / printf( " % d \n", d) ; d = a + c == b; / * * / printf( " % d \n", d) ; d = a ++ &&b ++++ &&c ++ ; / * * / printf( " a = % d, b = % d, c = % d, d = % d \ n", a, b, c, d) ; d = a ++ b ++ &&c ++ ; / * * / printf( " a = % d, b = % d, c = % d, d = % d \ n", a, b, c, d) ; : 1 0 0 a = 2, b = 3, c = 1, d = 0 a = 3, b = 3, c = 1, d = 1 3. 3 3. 3. 1,,
C ( ), :, 2 2 3. 3. 2 C C, 2 : if switch, if if 3 : if else if if else if else ( 1) if ( if-else ) : if( ) 1; else 2; :, ( 0 ), 1, if ;, 2, if 3. 4 : if ( ) 3. 4,,, 1 2, else ( ) if, if, 3. 4 a, a ( a 0) ( 1) : a - a ( a < 0) ( 2) ( ) : 59
60 a if( a > 0) then b = a else b b = - a ( 3) C : { float a, b; scanf( " % f", &a) ; if( a > = 0) else b = a; b = - a; printf( " The absolute value of % f is % f \n", a, b) ;,, 1 2, 3. 5,,, ( 1) :, a b c, : ( : a + b > c a + c > b b + c > a ) : ( ) : a, b, c if ( ) then else s = ( a + b + c) /2; s = ( a + b + c) /2 area = s( s - a ) ( s - b) ( s - c) area = sqrt( s* ( s - a) * ( s - b) * ( s - c) ) ; : area : " error" ( 3) C :
C #include < math. h > { float a, b, c, s, area; scanf( " % f, % f, % f", &a, &b, &c) ; if( a + b > c && a + c > b && b + c > a) { s = ( a + b + c) /2; area = sqrt( s* ( s - a) * ( s - b) * ( s - c) ) ; printf( " % f\ n", area) ; else printf( " data error \n" ) ;, ; 1 ; ( sqrt),, math. h, #include < math. h >, main if, C, if 3. 6 3 : 3 x, y, z, 2 x, y, max ; z max, max ;, max 3 ( 2) ( ) : : x, y, z if( x > y) then max = x else max = y if( z > max) then max = z : max ( 3) C : / * ex3-6. c* / { int num1, num2, num3, max; 61
62 printf( " Please input three numbers: " ) ; scanf( " % d, % d, % d", &num1, &num2, &num3) ; if ( num1 > num2 ) max = num1 ; else max = num2 ; if ( num3 > max) max = num3 ; printf( " The three numbers are: % d, % d, % d \ n", num1, num2, num3) ; printf( " max = % d \n", max) ; 1 if, else : max = num1; if( num2 > max) max = num2; : max( ), max, max, max,, max 3 3 ( 2) if ( if ) : if( ) ; :, ( 0),, if ; ( 0), if 3. 5 3. 5 : if ( ),,,,
C if else else 3. 7, ( if ) { float a; 3. 8 scanf( " % f", &a) ; if( a < 0) a = - a; printf( " % f\ n", a) ;,, ( 1) ( ) : : x if( x 2 ) then x, : x ( 2) C : / * ex3-8. cpp * / { int x; printf( " input: x \n" ) ; / *, * / scanf( " % d", &x) ; if( x% 2) / * : x 2, 0* / printf( % d is odd number. \ n, x) ; ( 3) if ( if else ) if if, if : 63, if
: 1,, 2,, 1; 2 2 ; 1, 3,, 3; 4, 3. 6 3. 6 64 : C,, : if else if ; if if ( else ) if, else if : else if : if ( 1) if ( 2 ) 1 else if ( 3 ) 2 else 3 else if,, else if,,?,,,, ( if else, ), if ( 1) { if ( 2 ) 1
C else if ( 3 ) 2 else 3,, : if, { 3. 9 : { int x, y; 3. 10 y = scanf( " % d", &x) ; if ( x < 1) y = x; if ( x > = 1 && x < 10) y = 2* x - 1; if ( x > = 10) y = 3* x - 11; printf ( " % d, % d", x, y) ; x ( x < 1) 2x - 1 ( 1 < x < 10) 3x - 11 ( x 0), ( 1) : 4 100 ; 400 ( 2) : : year if( ( year 4 ) ( year 100 ) ( year 400 ) ) else leap = 1 leap = 0 if ( leap = 1) else ( year, ) ( year, ) 65
66 ( 3) C : { int year, leap; scanf( " % d", &year) ; if( ( year% 4 == 0) &&( year% 100! = 0) ( year% 400 == 0 ) ) leap = 1; else leap = 0; if ( leap) printf( " % d is a leap year \ n", year) ; else printf( " % d is not a leap year \n", year) ; :, ( 4) elseif ( if elseif else ) if else, else if, if else ( : if elseif else ) : if( 1) 1 else if ( 2) 2 else if ( 3) 3 else if ( m) m else m + 1 : 1,, 1; 2 :,, 2; 3,, 3; m,, m, m + 1 3. 7 1, 2 ; 2
C 3. 7, 3, else, 1, 0( ),, ; 0 ( ), m + 1, else m + 1 3. 11 50, 5% ; 100, 7. 5% ; 300, 10% ; 500, 15% ; 50, 0? { float total, con; scanf( " % f", &total) ; if ( number > 500) con = 0. 15 ; else if ( number > 300) con = 0. 10 ; else if ( number > 100) con = 0. 075; else if ( number > 50) con = 0. 05 ; else con = 0; printf( " % f", total - total* con) ; 3. 12 5, 67
68 { long num, place; 3. 13 printf( " ( 0 99999) : " ) ; scanf( " % ld", &num) ; if( num > 9999 ) place = 5; else if( num > 999) place = 4; else if( num > 99) place = 3; else if( num > 9 ) else place = 2; place = 1; printf( % ld % d! : \ n, num, place) ; { int x, sign; sign( x) = printf( Please input a number \ n ) ; scanf( % d, &x) ; if( x > 0) sign = 1; else if( x = = 0) else sign = 0; sign = - 1; printf( The sign is % d \ n, sign) ; 2 ) - 1 ( x < 0) 0 ( x = 0) 1 ( x > 0) if, 2 :
C ( ), ; C if?:, C, 3 : 1? 2: 3 : 1, 1 0 ( ),, if : 2 ; 1 0 ( ), 3 if( x < y) else max = y; max = x; if :, : max = ( x < y)? y: x,, max = ( a > b)? a: b max = a > b? a: b? :, : a > b? a: c > d? c: d a > b? a: ( c > d? c: d),, 3 2 3 3. 14 { float a, b, sum; 3. 15 a + b printf( " Input 2 reals: " ) ; scanf( " % f, % f", &a, &b) ; sum = b > 0. 0? a + b: a - b; printf( a + b = % f\ n, sum) ;,, ;, { char ch; printf( " Input a character: " ) ; 69
scanf( " % c", &ch) ; ch = ( ch > = A && ch < = Z )? ( ch + 32) : ch; printf( " ch = % c \ n", ch) ;, if 3 ) switch case ( ) 70 if, if,,,, C switch, switch : switch( ) { case 1 : 1; [ break; ] case 2 : 2 ; [ break; ] case n: n; [ break; ] default: n + 1 :,, case, ; break, :, number, number 2, statement2,, break, switch, statement5, :, number, number 2, statement2,, break, statement3,, break, statement4,, switch, statement5
C :,, ( case ) case, ; case2: case 2: break default case break, case,, { case : case A : case B : case C : printf( " > 60 \ n" ) ; break; A B C3, default, ; default, default, default break switch : case switch { int x = 1, y = 0, a = 0, b = 0 ; switch( x) { case 1: switch( y) { case 0: a ++ ; break; case 1: b ++ ; break; 71
72 case 2: a ++ ; b ++ ; break; case 3: a ++ ; b ++ ; printf( " a = % d, b = % d \n", a, b) ;, 3. 16 { char grade; printf( " input the grade: " ) ; scanf( " % c", &grade) ; switch( grade) { case A : printf( " 85 100 \ n" ) ; 3. 17 case case break; B : printf( " 70 84 \ n" ) ; break; C : printf( " 60 69 \ n" ) ; break; case D : printf( " < 60 \ n" ) ; break; default: printf( " error \ n" ) ; { char grade; 60 printf( " Input the grade: " ) ; scanf( " % c", &grade) ; switch( grade) { case A : case B : case case C : printf( " > = 60 \ n" ) ; break; D : printf( " < 60 \ n" ) ; break;
C 3. 18 { char c; default: printf( " error \n" ) ;, ;, int i = 0; c = getchar( ) ; switch( c) { case 9 : i ++ ; 3. 19 case 8 : i ++ ; case 7 : i ++ ; case 6 : i ++ ; case 5 : i ++ ; case 4 : i ++ ; case 3 : i ++ ; case 2 : i ++ ; case 1 : i ++ ; case 0 : printf( " It is a digiter % d. \ n", i) ; break; case : printf( " It is a space. \n" ) ; break; default: printf( " It is other character. \n" ) ;, { int year, month, days; printf( " input year, month =? \ n" ) ; scanf( " % d, % d", &year, &month) ; switch( month) { case 1: case 3: case 5: case 7: case 8: 73
74 case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if( ( year% 4 == 0 ) &&( year% 100! = 0) ( year% 400 == 0) ) days = 29; else days = 28; break; default: printf( " month is error \ n" ) ; printf( " year = % d, month = % d, days = % d \n", year, month, days) ; 3. 4 3. 4. 1,,, ;, 3, C, 3, : while do while for 3. 4. 2 while while while, :
C, while : while( ) :, 0 ( ), ;, 0 ( ),, 0 ( ),, ; 3. 8 while : 3. 8 while while, 0,, C 75 3. 20 100 while n n = 1 ( 1) : sum, 0, sum + = n ( n 1, 2, 100), 3 : n 1 ; 1 sum + = n, n 1 ; n 101,, sum 1 100 ( 2) : sum n, 0; n < = 100,, ; sum + n sum, n + 1 n; ; sum. ( 3) : sum 0, n 1 While( i < n) Sum sum + i n n + 1 : sum
76 ( 4) C : { int n = 1, sum = 0; 3. 21 while( n < = 100) { sum + = n; / * sum = sum + n; * / n ++ ; printf( sum = % d \ n, sum) ; { char ch; 26 ch = Z ; while( ch > = A ) { putchar( ch) ; ch - - ; / *, * / printf( " \n" ) ; 3. 4. 3 do while do while while,, : do while( ) ; :, 0( ), ;, ; 0( ),, 0( ),, ; 3. 9 do while : do while,,
C, { C, while( ) 3. 9 donwhile { int n, sum; n = 1, sum = 0; do 3. 23 { sum + = n; n ++ ; while ( n < = 100) ; 3. 22 100 do while n n = 1 ( 1) : ( ) 3. 20 ( 2) C : printf( sum = % d \ n, sum) ; ( 1) :, ( 10 ), ;, ; ( 2) : : n While ( n 0) r < - ( n /10 ) ( : n 10, ) : r( ) n < - ( n /10) ( : n 10, ) ( 3) C : { int i, r; printf( " Input an integer \n" ) ; sanf( " % d", &i) ; do { r = i % 10; 77
printf( " % d", r) ; while ( ( i / = 10)! = 0 ) ; printf( " \n" ) ; 3. 4. 4 for 78 for C 3. 10 for,, for 3. 10 for : for( 1; 2; 3 ) for 3 ; ; : ; ; 1 ( ) 2 ( ) 3 ( ) : 3. 9, 0,, ;,,, ; for for : :,, 0 1, 3 ; 2,,, 2, 3, : for ( sum = 0; i < = 100; i ++ ) sum + = i; for ( sum = 0, i = 1 ; i < = 100 ; i ++ ) sum + = i; for ( i = 0, j = 100; i < = j; i ++, j - - ) k = i + j;, C for
C, ; for, for C, 3 1 2 3, : a. 1 3,, while : for( ; i < = 100; ) { sum = sum + i; i ++ ; : while( i < = 100) { sum = sum + i; i ++ ; b. 3, 2 ;, for ( i = 1; i < = 100; ) { sum = sum + i; i ++ ; c. 2,,, break, goto, exit( ), return ;, : for ( i = 1; ; i ++ ) sum = sum + i; : while ( 1 ) { sum = sum + i; i ++ ; for while do while, for while : 1; while ( 2 ) { 3; for : for : x 0 = < x < N, N, N ; for ( int x = 0; x < N; x ++ ) { x 0 = < x < = N - 1, N - 1, N ; for ( int x = 0; x < = N - 1; x ++ ) {,, 3. 24 100 for n n = 1 ( 1) : ( ) 3. 20 ( 2) C : { int n, sum = 0; 79
80 for( n = 1; n < = 100; n ++ ) sum + = n; printf( sum = % d \ n, sum) ; 3. 25 n n! ( n! = 1 2 3 4 5 n) ( 1) : fact, 1, fact = fact* fact 1; 1 fact = fact* i ( i 1, 2,, n), 3 : i, i 1; i n,, fact n! ( 2) 5 : fact n i; i = 1, fact = 1; i < = n,, ; fact* ; fact i fact, i + 1 i; ( 3) : : n fact 1, i 1 While( i < = n) fact fact* i : fact ( 4) C : { int i, n; 3. 26 long fact = 1; / * fact 1* / printf( Input n: ) ; scanf( % d, &n) ; for( i = 1 ; i < = n; i ++ ) fact * = i; / * * / printf( " % d! = % ld \ n", n, fact) ; 2, ( 1) : 1, m, 2 m - 1 m, m, m, m, m
C ( 2) : : num( : num > 2) flag 0 ( num ), i 2 while( i < = num - 1) if( num i ) flag = 1( num ) ; if( flag = 0) : num, " " else : num, " " ( 3) C : #include < math. h > { int m, i, flag; flag = 0; scanf( " % d", &m) ; for( i = 2 ; i < m - 1; i ++ ) if( m % i == 0) flag = 1; if( flag == 0) printf( " % d is a prime. ", m) ; else printf( " % d is not a prime. ", m) ; 81 3. 4. 5 3 while do while for 3,, 3 : 3 ( while do while for), while do while, while ;, ( ) for 3, 3 for, while
, for while do while, while do while for 1 while for,, ; do while,, 3. 4. 6 82,,,, ;, ( ) C 3 do while while for : while while for for do while do while while for :, : ;, 3. 27
C A BB CCC DDDD EEEEE ( 1) : 5, ; A, B C D E ( ASCII : A + i 64 + i) ;, ( 2) : i 1 j 1 : ch A While( i < = 5) While( j < = i) i i + 1 : ch j j + 1 ch < - ch + 1 ( 3) C : { int i, j; char ch; ch = A ; for( i = 0 ; i < 5; i ++ ) { for( j = 0 ; j < = i; j ++ ) 3. 28 printf( " % c", ch) ; + 1; * / printf( " \n" ) ; ch = ch + 1; { int i, j; printf( " * " ) ; /* printf( " % c", ch + i) ; ch = ch 83
84 for( i = 1 ; i < = 9; i ++ ) / * * / printf( " % 4d", i) ; printf( " \n" ) ; for( i = 1 ; i < = 9; i ++ ) / * * / { printf( " % 4d", i) ; / * * / : for( j = 1 ; j < = i; j ++ ) / * * / printf( " % 4d", i* j) ; / * * / printf( " \n" ) ; / * * / * 1 2 3 4 5 6 7 8 9 1 1 2 2 4 3 3 6 9 4 4 8 12 14 5 5 10 15 20 25 6 6 12 18 24 30 36 7 7 14 21 28 35 42 49 8 8 16 24 32 40 48 56 64 9 9 18 27 36 45 54 63 72 81 3. 29 2 a b, a b :, 1, 2 : n, 2 n /2 n,, n n, 2 n n,, n ( 2) : n, for n :, 2 ( n - 1 ) n,,, 0 ( 3) ( ) : : : a, b num a
C while( num < = b) k num i 2 while( i < = k) if( num i ) num, num; else : num ; num; i i + 1 num num + 2 ( 4) C : #include < math. h > { int a, b, num, i, k; scanf( " % d, % d", &a, &b) ; for( num = a; num < = b; num ++ ) { k = sqrt( num) ; for( i = 2 ; i < = k; i ++ ) if( num% i == 0) break; if( i > k) / * break : i < k * / printf( " % d is a primenumber. \ n", num) ; 85 3. 4. 7 C, for, while, do while,,,, CPU, ( b) ( a)
for ( row = 0; row < 100; row ++ ) { for ( col = 0; col < 5; col ++ ) { sum = sum + row* col; ( a) : for ( col = 0; col < 5; col ++ ) { for ( row = 0; row < 100; row ++ ) { sum = sum + row* col; ( b) :,, ( c) ( d) N - 1,,, N, ( d), N,, ( c), 86 for ( i = 0; i < N; i ++ ) { if ( condition) DoSomething( ) ; else DoOtherthing( ) ; ( c) if ( condition) { for ( i = 0; i < N; i ++ ) DoSomething( ) ; else { for ( i = 0; i < N; i ++ ) DoOtherthing( ) ; ( d) 3. 5 3. 5. 1 goto goto, : goto ; goto : :
C,, goto, goto if,, : for( ) { if c then goto L; L:, goto, goto,, goto,, goto, goto,,, goto, : goto state; float s1, s2; / / goto int sum = 0; / / goto state:, goto goto,,, goto goto,,, break ; : { for { for { for if( disaster) goto error; error:,, goto,, goto if, goto 87
1 100 : { int k = 1, sum = 0; loop: if( k < = 100) sum + = k; k ++ ; goto loop; printf( % d \n, sum) ; C, goto 3. 5. 2 break 88 break break break : break; : switch : switch ; " break " switch while for do while break 3. 30 break C : { int i; for( i = 1 ; i < = 100 ; i ++ )
C { printf( " % d ", i) ; 3. 31 if( i > 9) printf( " \n" ) ; break; 100 13 ( 1) : : 1 100, 13, j 100, ;, 13 ; 13, 13 ;, ( 2) ( ) ( 3) C : { int j; for( j = 100; j > = 0 ; j - - ) / * j 100, * / if( j% 13 == 0 ) / * 13 * / break; / * 13, * / printf( % d \n, j) ; 89 3. 5. 3 continue continue, : continue; : :, : continue, while for do while continue 3. 32 10 n : ( 1) : sum = 1 1 + 1 3 + 1 5 + 1 7 + 1 9 + 1 11 + + 1 n, ( 10 ), ;, ;
90 ( 2) : LIMIT 10 n 1, sum 0 while( sum < LIMIT) an 1 /n sum sum + an n < n + 2 : n ( 3) C : #define LIMIT 10 { int n = 1; float sum = 0. 0; for( ; ; ) { sum = sum + 1. 0 / n; if( sum > LIMIT) break; n ++ ; printf( " n = % d \ n", n) ; 3. 33 1 100 7 ( 1) : : 1 100, 7 : j 1, ;, 7 7,, ;,, ; j > 100, ( 2) : num 1 while( num < = 100)
C if( num 7 ) else, num;, num; num num + 1 ( 3) C : { int num; for( num = 1; num < = 100; num ++ ) { if( num% 7 == 0) continue; / * 7,, * / printf( % d \ n, num) ; 91 3. 6 3. 6. 1 3. 34 3, ( 1) : 3 x, y, z, x, y, x, y, x z, x, z ; y z, y ; z ;, 3 : x < y < z ( 2) ( ) : : input: x, y, z output: x, y, z( x < y < z) : x, y, z : x, y, z if( x > y) then x y
92 if( x > z) then x z if( y > z) then y z : x, y, z ( 3) C : { int x, y, z, temp; printf( Input x, y, z \ n" ; scanf( % d, % d, % d, &x, &y, &z) ; printf( % d, % d, % d \ n, x, y, z) ; if( x > y) if( x > z) emp = x, x = y, y = temp; temp = x, x = z, z = temp; if( y > z) temp = y, y = z, z = temp; printf( x < y < z: % d, % d, % d \ n, x, y, z) ; 3. 35 a x 2 + bx + c = 0 ( 1) :, : a b 2 b 2 b 2 = 0, - 4a c = 0, 2-4a c > 0, 2-4a c < 0, 2 ( 2) ( ) : : input: a, b, c temp: d( d = b 2 output: x1, x2 : a, b, c if( a = 0) then d < b 2 : " " - 4ac if( d = 0) then - 4ac), p, q x1, x2 < - b / ( 2* a)
C : " x1 = x2 = : ", x1 if( d > 0) then x1 < ( - b + sqrt( d) ) /( 2 * a) ; x2 < ( - b - sqrt( d) ) /( 2 * a) ; : " x1", x1, " x2 = ", x2 if( d < 0) then p < - b / ( 2* a) q < qrt( - d) /( 2* a) : " : ", x1, x2 ( 3) C : #include < math. h > { float a, b, c, d, x1, x2, p, q; printf( " a, b, c =?" ) ; scanf( " % f, % f, % f", &a, &b, &c) ; / * * / printf( " The equation" ) ; if ( fabs( a) < = 1e - 6) / * a = 0 * / printf( " is not quadratic" ) ; else { d = b* b - 4* a* c; /* * / if( fabs( d) < 1e - 6 ) /* = 0 * / printf( " has two equal roots: % 8. 4f\ n", - b / ( 2* a) ) ; else if( d > 1 e - 6) /* > 0 * / { x1 = ( - b + sqrt( d) ) / ( 2* a) ; x2 = ( - b - sqrt( d) ) / ( 2* a) ; printf( " has distinct real roots: % 8. 4 f and 8. 4f\ n", x1, x2) ; else /* < 0 * / { p = - b / ( 2* a) ; q = sqrt( - d) / ( 2* a) ; printf( " has complex roots: \n" ) ; printf( " % 8. 4f + % 8. 4fi \ n", p, q) ; printf( " % 8. 4f - % 8. 4fi \ n", p, q) ; 93
94 3. 36 500, profit( ) ( : ) : profit 1 000 ; 1000 < profit 2 000 10% ; 2000 < profit 5 000 15% ; 5000 < profit 1 0000 20% ; 10000 < profit 25% ( 1) : switch, profit, 1 000 ( 1 000, 2 000, 5 000 ), profit 1 000, : profit 1 000 0, 1 1 000 < profit 2 000 1, 2 2 000 < profit 5 000 2, 3, 4, 5 5 000 < profit 10 000 5, 6, 7, 8, 9, 10 10 000 < profit 10, 11, 12 ( 2), : profit 1 ( ), 1000 : profit 1 000 0 1 000 < profit 2 000 1 2 000 < profit 5 000 2, 3, 4 5 000 < profit 10 000 5, 6, 7, 8, 9 10 000 < profit 10, 11, 12 ( 2) ( ) : ( ) ( 3) C : { long profit; int grade; float salary = 500; /* * / printf( " Input profit: " ) ; scanf( " % ld", &profit) ; /* profit* / grade = ( profit - 1 ) / 1 000 ; / * - 1 1 000, switch case * / switch ( grade) { case 0: break; /* profit 1000 * / case 1: salary + = profit* 0. 1; break; /* 1000 < profit 2000 * / case 2:
C 3. 37 case 3: case 4: salary + = profit* 0. 15; case 5: case 6: case 7: case 8: break; /* 2000 < profit 5 000 * / case 9: salary + = profit* 0. 2; break; /* 5000 < profit 10000 * / default: salary + = profit* 0. 25; /* 10000 < profit * / printf( " salary = %. 2f \n", salary) ; 2, ( + - * / ), ;, ( 1) :,, if - else if, switch switch, switch, ( 2) ( ) : ( ) ( 3) C : { int a, b, d; char c; printf( " Enter Two Numbers: " ) ; scanf( " % d, % d", &a, &b) ; printf( " Enter Operator( +, -, *, / ) : " ) ; scanf( " % c", &c) ; switch ( c) { case + : d = a + b; printf( " % d + % d = % d \ n", a, b, d) ; break; case - : d = a - b; printf( " % d - % d = % d \ n", a, b, d) ; break; case * : d = a* b; printf( " % d* % d = % d \ n", a, b, d) ; break; 95
96 3. 38 200 case / : if( b == 0) printf( " Divisor is zero\ n" ) ; else { d = a / b; break; printf( " % d /% d = % d \n", a, b, d) ; default: printf( " Input Operator error! \n" ) ;, 1 ( 1) : n: 1 200, n, m* m == n ( : 1 < m < sqr( n) ),,, n ;,, n ; n > 100, ( 2) : n 1 while( n < = 200) m 1 while( m < sqr( n) ) if( m* m = n) else m < m + 1 n < - n + 1 ( 3) C : #include < math. h > void main ( ) { int m, n;, num;, num; for ( n = 1; n < = 200; n ++ ) for ( m = 1; m < = sqrt( n) ; m ++ ) if ( m * m == n) printf( " % d ", n) ; printf( " \n" ) ;
C 3. 39 sin( x 3 ) = x - x 3 /3! + x 5 /5! - x 7 /7! +, 10-7 ( 1) : : F n n, F n + 1 n + 1, F n + 1 x 2 = - F n 2n ( 2 n + 1 ), x ( 2) : : x, EPS k 1, an x, sum 0, n 1 while( an < EPS) sum sum + an an an* ( x* x) / ( ( 2* n* ( 2* n + 1) ) * k k - k n n + 1 : x, sum ( 3) C : #include < math. h > #define EPS 1e - 7 #define PI 3. 14159 { int n; double x, an, sum; printf( " input x: " ) ; scanf( " % lf", &x) ; x = x* PI /180; / * * / sum = 0; an = x; n = 1; / * an * / do { sum + = an; an* = ( - x* x) / ( ( 2 * n* ( 2* n + 1 ) ) ; /* * / ++ n; while( fabs( an) > = EPS) ; / * fabs( ), * / printf( " sin( %. 4f) = %. 4f \n", x, sum) ; 97
98 3. 40,, A Z, a z, : A E, a e, 4 ; W A, X B, Y C, Z D, China! Glmre!, ( 1) : ;, : 4 ( ASCII + 4 ), ( A E, a e) ( 2) ( ) : ch while( ch ) if( ch ) ch ch + 4 if( ch ) ch ch - 26 : ch ch ( 3) C : { char c; while ( ( c = getchar( ) )! = \ n ) { if( ( c > = a &&c < z ) ( c > = A &&c < = Z ) ) { c = c + 4; if( c > Z &&c < Z + 4 c > z ) c = c - 26; printf( " % c", c) ;
C 3. 6. 2 1 ) 3. 41 2 ( GCD) ( LCM) ( 1) : 2 m n( m > n) ( ) : 2 m n( m > n) ; m n, r( 0 < = r < n) r 0, r 0, ; r = 0, n,, n > m, r > n r m n ; n, n 2 m n ( GCD), ( LCM) : 2 ( 2) : : num1, num2 if( num1 < num2) num1 num2, ( m > n) m num1, n num2 while( n 0 ) GCD m r m n ) m n( m = n) n r ( n = r) LCM num1* num2 / m : :, GCD : :, LCM ( 3) C : { int m, n, num1, num2, temp; printf( " Input two positive integer: \ n" ) ; scanf( " % d, % d", &num1, &num2) ; 99
if( num1 < num2) /*, num1 num2, ( m > n) * / temp = num1, num1 = num2, num2 = temp; m = num1; n = num2; while( n! = 0) { temp = m% n; m = n; n = temp; printf( " The greatest common divisor is: % d \ n", m) ; printf( " The lowest common multiple is: % d \ n", num1* num2 / m) ; 2 ) 100 :,, ( ) 2 : (,,, ) (, ) 3. 42 36, 36 ; 4, 3, 2 1,? ( 1) :, : ( men) : 1 8 ( women) : 1 12 ( childs) : 36 - - : 4* + 3* + /2 = 36 ( 2) : men 1 while( men < 9) women 1 while( women < 12 ) else childs 36 - men - women if( 36 ) ( men, women, childs), ;, ;
C ( 3) C : { int men, women, childs; for( men = 1; men < 9; men ++ ) for( women = 1 ; women < 12; women ++ ) { childs = 36 - men - women; if( 4* men + 3* women + childs/2. 0 = = 36) printf( " men = % d, women = % d, childs = % d \ n ", men, women, childs) ; : childs/2. 0 childs/2, C 2,, 2 3. 43 6 2 ( 1) ( 6 20 000 ) : m = 6 m < = 20 000,, n1 = 2 n1 < = m /2,,, n1,,, n2 = m - n1, m = n1 + n2,, n1 = n1 + 1, m = m + 2, 3. 29 ( 2) : ( ) ( 3) C : #include < math. h > { int flag, flag1, k, m, n; for( m = 6; m < = 2000; m = m + 2) for( n1 = 2; n1 < = m /2; n1 ++ ) { flag = 0; for( k = 2; k < = ( int) sqrt( n1) + 1; k ++ ) if( n% k = = 0) { flag = 1; 101
102 break; if( flag = = 0) { flag1 = 0; n2 = m - n1; for( k = 2 ; k < = ( int) sqrt( n2) + 1; k ++ ) if( ( m - n) % k == 0) { flag1 = 1; break; if( flag1 == 0) { printf( " % d = % d + % d \n", m, n1, n2) ; break; 3. 44, 2, 1 ; 3, 2 ; 5, 4 ; 6, 5 ; 7,? ( 1) :, 7 ; 7, 7,, ; 14 ( 2) : : input: ladders( : 7, 7 ) temp: k( : k = 1, ; k = 0, ) output: ladders( ) ladders < -7( 7 ) while( k) if( ) then ladders < - ladders + 14 ladders ( 3) C :
C { int ladders = 7; while( ladders% 3! = 2 ladders% 5! = 4 ladders% 6! = 5) ladders + = 14; printf( flight of stairs = % d \ n, ladders) ; 3 ) 3 : ; ; ( ) 3. 45 ( Fibonacci ) 1, 3 1,, 1?, ( Fibonacci) : 1, 1, 2, 3, 5, 8, 13, 21, 34 ( 1) : : 3,, fn_1 = fn_2 = 1 fn = fn_1 + fn_2, C : fn_2 = fn_1 ; fn_1 = fn ( 2) : FN1 1, FN2 1 : FN1, FN2 i 3 while( i N ) fn = fn_1 + fn_2; / * 1 2 * / FN FN1 + FN2 / * i * / : FN / * i * / FN2 FN1 FN1 FN ( 3) C ( 20 ) : 103
{ int i, fn, fn_1 = 1, fn_2 = 1; printf( " % d, % d", fn_1, fn_2) ; for( i = 3 ; i < = 20; i ++ ) { fn = fn_1 + fn_2; printf( ", % d", fn) ; fn_2 = fn_1; fn_1 = fn; 4 ) 104 f( x) = 0, f( x) = 0 : x = ( x) ; x0, : x0, x1, x2, x3,, xn, ;,,, f( x) = 0,, ( ),, 3. 46 2 x 3-4 x 2 + 3 x - 6 = 0 1. 5 3. 11 ( 1) :, 3. 11 x k f( x) = 0 x*, P k ( x k, f( x k ) ) f ( x) : y = f ( x k ) + f ( x k ) * ( x - x k ) X : f( x k ) + f ( x k ) * ( x - x k ) = 0, : x k+ 1 = x k - f ( x k ) / f ( x k ), x k x*,,
C ( 2) : x ( 1. 5) while( ) X0 X F ( ( 2* X0-4) * x0 + 3) * x0-6 / * x0 * / F1 ( 6* X0-8) * x0 + 3 / * x0 : * / x x0 - F /F1; : ( x) ( 3) C : #include < math. h > { float x, x0, f, f1 ; x = 1. 5; do { x0 = x; f = ( ( 2 * x0-4) * x0 + 3) * x0-6; f1 = ( 6* x0-8) * x0 + 3; x = x0 - f /f1; while( fabs( x - x0) > = 1 e - 5) ; printf( " root = % f \n", x) ; 105 < 0 5 ) ( ) 3. 47 2 x 4-4 x 3 + 6 x 2-8 x - 8 = 0 ( 1) : : : f( x) = 0 x1 x2, x1 x2, f( x1 ) * f( x2) x1 x2 : x = ( x1 + x2) /2 x1 x x2 : y1 = f( x1) y = f( x) y2 = f( x2 ) y = 0,, x ;, y y1, x2 = x;, x1 = x, 3. 12 :, y = f( x) 0, x, f( x), 0 f( x) == 0,, f( x) 10 - k f( x) 0, f( x)
106 3. 12, x ( ) 10 - k, 10-5 ( 2) : ( ) ( 3) C : ( 10-5 ) #include < math. h > { float x1, x2, x, y1, y2, y; do / * x1 x2, x1 x2 * / { scanf( " % f, % f", &x1, &x2) ; y1 = 2 * x1* x1* x1 * x1-4* x1 * x1* x1 + 6 * x1* x1-8 * x1-8; y2 = 2 * x2* x2* x2 * x2-4* x2 * x2* x2 + 6 * x2* x2-8 * x2-8; while( y1 * y2 > 0) ; while( 1) { x = ( x1 + x2) /2; y = 2* x* x* x* x - 4* x* x* x + 6 * x* x - 8* x - 8; y1 = 2* x1* x1* x1* x1-4* x1* x1* x1 +6* x1* x1-8* x1-8; y2 = 2* x2* x2* x2* x2-4* x2* x2* x2 +6* x2* x2-8* x2-8; if( fabs( y) < 1e - 5 ) break; else if( y* y1 > 0) x1 = x; else x2 = x; printf( " x = % f\ n", x) ;
C 3. 7,,,,,,, 3. 7. 1,,, ( a),,, ( b) 107 / * * / void Function1( ) { / * * / void Function2( ) { / / * / void Function3( ) { ( a) : / * * / while ( condition) { statement1; / * * / if ( condition) { statement2; else { statement3; / * * / statement4; ( b) :
3. 7. 2,,, if for while do,, {, ( a), ( b) int width; / * * / int height; / * * / int depth; /* * / x = a + b; y = c + d; z = e + f; int width, height, depth; / * * / x = a + b; y = c + d; z = e + f; 108 if ( width < height) { dosomething( ) ; for ( initialization; condition; update) { dosomething( ) ; / * * / other( ) ; ( a) : if ( width < height) dosomething( ) ; for ( initialization; condition; update) dosomething( ) ; other( ) ; ( b) : : ( ),, : int width = 10; / * width* / int height = 10; / * height* / int depth = 10; / * depth* / 3. 7. 3 const virtual inline case, if for while
C (,, (, (, ), ;,,, Function ( x, y, z) ;,, for ( initialization; condition; update), = + = > = < = + * % && < <, ^,! ++ - - & ( ) [ ]. : for if, for ( i = 0 ; i < 10; i ++ ) if ( ( a < = b) && ( c < = d) ) void Func1( int x, int y, int z) ; void Func1 ( int x, int y, int z) ; if ( year > = 2000) if( year > = 2000) if ( ( a > = b) && ( c < = d) ) if( a > = b&&c <= d) for ( i = 0; i < 10; i ++ ) for( i = 0; i < 10; i ++ ) for ( i = 0; i < 10; i ++ ) x = a < b? a : b; x = a < b? a: b; int * x = &y; int * x = & y; array[ 5] = 0; array [ 5] = 0; / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / / * * / 109 3. 7. 4 {, {, { ( a), ( b)
110 void Function( int x) { /* program code* / if ( condition) { /* program code* / else { /* program code* / for ( initialization; condition; update) { /* program code* / While ( condition) { /* program code* / {,, : { { void Function( int x) { / * program code* / if ( condition) { program code* / else { / * program code* / for ( initialization; condition; update) { / * program code* / while ( condition) { / * program code* / {,, : { { ( a) : ( b) : 3. 7. 2, 70 80,,, ; ( ),,,
C if ( ( very_longer_variable1 > = very_longer_variable12) && ( very_longer_variable3 < = very_longer_variable14) && ( very_longer_variable5 < = very_longer_variable16) ) { dosomething( ) ; for ( very_longer_initialization; very_longer_condition; very_longer_update) { dosomething( ) ; 3. 7. 6 * &,, *, : int* x;, x int, : int* x, y; y x y, * & : char * name; int * x, y; / * y * / 111 3. 7. 7 C / * * / : ; ;,,,,,,,
,,,,,,, 112 /* * : * : * : * : * / void Function( float x, float y, float z) { if ( ) { while ( ) { / * end of while* / / * end of if* / 3 3. 1 ( 1) x [ 1, 10 ] [ 200, 210 ], ( A) ( x > = 1) &&( x < = 10) &&( x > = 200 ) &&( x < = 210) ( B) ( x > = 1 ) ( x < = 10) ( x > = 200) ( x < = 210) ( C) ( x > = 1 ) &&( x < = 10 ) ( x > = 200) &&( x < = 210) ( D) ( x > = 1) ( x < = 10) && ( x > = 200) ( x < = 210) ( 2) x y z, C : ( A) ( x > = y) AND ( y > = z) ( B) ( x > = y) && ( y > = z) ( C) ( x > = y > = z) ( D) ( x > = y) &( y > = z) ( 3 ) c 0, c a b ( A) ( a = c) &&( b = c) ( B) ( a = c) ( b = c) ; ( C) c = b = a; ( D) a = c = b; ( 4) a = 5, b = 4, ( A) ( a > b) &&( b - a) ( B) ( b > = 0) && ( a < = b? a + b: a - b) ( C) ( a < = 0) ( a% b) ( D) a &&! b
C ( 5) a int a, b, c; a = b = c = 1; ++ a ++ b&& ++ c ( A) ( B) 0 ( C) 2 ( D) 1 ( 6) for( i = 1; i + + < 4; ) ;, i ( A) 3 ( B) 4 ( C) 5 ( D) 1 ( 7) for( x = 0, y = 0; ( y! = 100 ) &&( x < 4 ) ; x + + ) ; ( A) ( B) ( C) 4 ( D) 3 ( 8) C while(! e) ;! e ( A) e == 0 ( B) e! = 1 ( C) e! = 0 ( D) e == 1 ( 9) i j int, for( i = 5; i; i - - ) for( j = 0; j < 4; j + + ) { ( A) 20 ( B) 24 ( C) 25 ( D) 30 113 3. 2 ( 1) C?? ( 2) C?? ( 3) switch - case? 3. 3, ( 2) # include < stdio. h > { / *, * / int logic; int a = 1; int b = 2; int c = 3; logic = a + b > c&&b < = c; printf( " logic = % d \n", logic) ; logic = a > = b + c b == c; printf( " logic = % d \n", logic) ;
logic =! ( a < c) + b! = 1 &&( a + c) /2 ; printf( " logic = % d \n", logic) ; 114 ( 2) { int a = 100, x = 10, y = 20, okl = 5, ok2 = 0; if ( x < y) if( y! = 10) if(! okl) a = 1; else if( ok2) a = 10; a = - 1 ; printf( " % d \n", a ) ; ( 3) void main3( ) { int num = 0; while( num < = 2) { num + + ; printf ( " % d \ n", num) ; ( 4), ADes iptor ( ), { char c; int v0 = 0, v1 = 0, v2 = 0; do{ switch( c = getchar( ) ) { case case a : A :
C case case case case case case case case e : E : i : I : o : O : u : U : v1 + = 1; default: v0 + = 1; v2 + = 1; while( c! = \ n ) ; printf( " v0 = % d, v1 = % d, v2 = % d \ n", v0, v1, v2) ; ( 5) { int x, y; for( x = 1, y = 1; y < = 50; y ++ ) { if( x > = 10) break; if( x% 2 == 1) { x + = 5 ; continue; x - = 3 ; printf( " x = % d, y = % d \ n", x, y) ; 115 3. 4 ( 1), ( :, ) ( 2),, : 123, 321 ( 3) : 3 5 7, YES, NO ( 4) : 3, ( 5) 0 m, 4 15 m,
( 2, 2) ( - 2, 2) ( 2, - 2) ( - 2, - 2), 1m,, ( 0 m) ( 6 ), x, y,, y = x( x + 2. 0) x1 2 x - 1 < x < 1 x - 1 x - 1 ( 7) 3, : 153 = 1 3 + 5 3 + 3 3, 153,, ( 8) e ( 10-6 ) : 116 ( 9) : ( 10) : 1 e = n = 0 n! * k * * * * * * * * * * * * * * * * * * * * * * * * AAAAAAAAAAAA BBBBBBBBBB CCCCCCCC DDDDDD EEEE FF ( 11) 3 5, 3 6, ( 12) 1 99 :, 5 25, 25 625, 5 25 ( 13) ( : n = 10 000) ( 14) 1 100 10 5 2 1 1, 40, 1 ( 15) 100 m, 10? 10?
C ( 16) 1, 1 ; 2 1 ; 10, 1 1 117
118 4. 1 4. 1. 1,,,,,,,,, :,,,,,,,,,,,,
,,, 4. 1. 2 C C, : C,, C,, main( ),,, C, IN- CLUDE H,,,,, C 4. 1 119 4. 1 C
4. 2 4. 2. 1 120 C : ( ) { ( ) : ( ) { ( ), : / * * / long fac( x) int x; { long y = 1; int k; for( k = 1; k < = x ; k + + ) y* = k; return( y) ; / * * / long fac( int x) { long y = 1; int k; for( k = 1; k < = x; k + + ) y* = k; return( y) ;
: C,, C : ( ) ( 1) ( ), return, ( int), ( long), ( char), ( float), ( double ),,, void ( ), : int putlll( int x, int y, int z) / * * / char * name( ) ; / * * / void student( int n, char * str) ; / * * / float calculate( ) ; / * * / : ( int) ( char ), : sum( int a, int b) ; / * * / ( 2), C, main( ),, ( 3),,, :, : int f( int a, float b) int float,, void : intf( ) intf( void),,,,,,, 121
122, ( 4) C, : void nothing( ) {,, C, main,,, ( max, power, ),, 2, : ( void, return) 2 : 1, : return < > ; return; 1 :,,,,, 2 : return;, return,,, (, ),,,,, return,, s1 s2 1, - 1 int find_char( char s1, char s2 ) { if( s1 = = s2 ) else return 1; return - 1; :, return
;,, C, C, ( ) (, goto ),, C,, ( ) C,, C C 3 : 1,,, sqr( ) sin( ) 2,, write( ),, write( ), - 1, 3 ( ),, printf( ) scanf( ) 123 4. 2. 2, :,,,,,,,, : #include < > #include : #inlcude stdio. h #include,, C ( include ) ;, : ( [ ], [ ], ) ; 1 ( ), ( ) : double funa( double a, int b, float c) {
124, : double funa( double a, int b, float c) ; / * * /,, double funa( double, int, float) ;, : double funa( a, b, c) ; / * * / : funa( double a, int b, float c) ; / * * / int char, : 4. 1 double funa( int b, float c, double a) ; / * * / { float add( float x, float y) ; / * add * / 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 z; ; z = x + y; return( z) ; : 2. 0, 3. 0 : sum is: 5. 000000 :,,,, ; 4. 2 float add( float x, float y) /* add * / { float z;
z = x + y; return( z) ; void / main( * add ) * / { float a, b, c; scanf( % f, % f, &a, &b) ; c = add( a, b) ; printf( sum is: % f \n, c) ; 4. 2. 3,, C,, C : ( ) ;,, :,, C, :,, C,, :,,, C,,,,, Turbo C MS C :, 125
126,,,,, return,,,, 4. 3 3 { float first, second, middle, third, result; float maxf( float num1, float num2) ; /* * / scanf( " % f, % f, % f", &first, &second, &third) ; middle = maxf( first, second) ; / * * / result = maxf( middle, third) ; / * * / printf( " The max is: % f", result) ; float maxf( float x, float y) / * * / { float z; z = x > y? x: y; return z; : 3. 0, 2. 0, 1. 0 : The max is: 3. 000000 main( ) 3, maxf( ) : 1, 2, 2 result 4. 4 2, ( 1) : n, prime n, n, reverse, prime, reverse( n), n, n ( 2) prime( ), 0, 1 : : prime( a) / * prime a * / b, k; if( a = 2) then 1 ; else if( a 2 ) then
else 0 ; { b 1, k 3 while( k < = a b = 1) if( a k ) then b 0 b; k k + 2 ( 3) reverse( int a) a, 12345 54321 : a% 10 a, a = a /10,, a = 0 : 12345 54321, 54321 = ( ( ( 5* 10 + 4) * 10 + 3) * 10 + 2) * 10 + 1 : : reverse( a) / * * / s 0 ; while( a 0) s; s s + a% 10 a a /10 if( a 0) then s s* 10 ( 4) : #include < math. h > { int reverse( int a) ; /* * / int prime( int a) ; /* * / int n; for( n = 11; n < = 99; n + + ) if( prime( n) &&prime( reverse( n) ) ) / * * / printf( 4% d, n) ; int prime( int a) / * * / { int b, k; if( a = = 2) 127
128 return 1; else if( a% 2 = = 0) return 0; else { b = 1; k = 3; while( k < = sqrt( 1. 0* a) &&b) { if( a% k = = 0) b = 0; k + = 2; return( b) ; int reverse( int a) / * * / { int s = 0; while( a! = 0) { s = s + a% 10 ; a = a /10; if( a) s = s* 10 ; return s; : 11 13 17 37 71 73 79 97, 4. 2. 4 C,, 2 :,,, ( ) ( )
,,,, 4. 5 { int a = 3, b = 5; void swap( int x, int y) ; swap( a, b) ; printf( a = % d, b = % d \n, a, b) ; void swap( int x, int y) { int temp; temp = x, x = y, y = temp; printf( " x = % d, y = % d", x, y) ; : x = 5, y = 3 a = 3, b = 5; swap 2, 2 x y, main ( ) 2 a b, 4. 2 129 4. 2 4. 6 float add( float x, float y) /* add * / { float z; z = x + y; x = x + y; /* x * / printf( " x = % f\ n", x) ; return z; /* add * / { float a, b, c;
scanf( " % f, % f", &a, &b) ; c = add( a, b) ; printf( " a = % f\ n", a) ; printf( " sum is: % f \n", c) ;, : 5. 0, 5. 0 : x = 10. 000000 add x ; a = 5. 000000 a x ; sum is: 10. 000000 4. 3 130 4. 3,,,,,,, 4. 3 C, C, C C, 4. 4 f1, f1 f2 ; f2, f1 f2 C ; f1, 4. 7 s = 1k + 2k + 3 k + + Nk ( 1) : 2 :, f1( ) n k, n k f2( ) n k,
4. 4 ( 2) C : long f1( int n, int k) / * n k * / { long power = n; int i; for( i = 1 ; i < k; i + + ) power * = n; return power; 131 long f2( int n, int k) / * 1 n k * / { long sum = 0; int i; for( i = 1 ; i < = n; i + + ) sum + = f1( i, k) ; return sum; { int n, k; scanf( % d, % d, &n, &k) ; printf( " Sum of % d powers of integers from 1 to % d = ", k, n) ; printf( " % d \n", f2( n, k) ) ; : 4, 5 : Sum of 5 powers of integers from 1 to 4 = 1300, main( ) f2( ), f2 ( ) f1 ( ), 4. 8 e( x), e x (,
10-6 ) 132 e x 1 + x + x2 2! + x3 3! + + xn n! ( 1 ) : ; 2 : powers( ) x, n, x n ; fac( ) n, n! ; sum( ) x, e x ( 2) C : #include < math. h > { double sum( float x) ; float x; scanf( " % f", &x) ; printf( " % f powers of e = % lf\ n", x, sum( x) ) ; double sum( float x) { double powers( float x, int n) ; double fac( int n) ; int i = 0; double s = 0; while( fabs( powers( x, i) / fac( i) ) > 0. 0000001 ) { s = s + powers( x, i) / fac( i) ; i + + ; return s; double powers( float x, int n) { double p = 1. 0; int i; for( i = 1 ; i < = n; i + + ) p* = x; return p; double fac( int n) { int i; double f = 1. 0; for( i = 1 ; i < = n; i + + )
f* = i; return f; : 0. 5 : 0. 500000 powers of e = 1. 648721 : - 1. 0 : - 1. 000000 powers of e = 0. 367879 main( ) sum( ), sum( ) fac( ) powers( ), fac( ) powers( ) double, n! x n 4. 9 x 5 - x 4 + 4x 2-1 = 0 0 133 ( 1) : 4. 5 x 1 x 2, f( x 1 ) f( x 2 ), ( x 1, x 2 ), f( x 1 ) f( x 2 ), x 1, x 2, f( x 1 ), f( x 2 ) x 1, x 2, ( x 1, x 2 ) f( x 1 ) f( x 2 ), X x, 4. 5 x, x f( x) x = x 1 f( x 2 ) - x 2 f( x 1 ) f( x 2 ) - f( x 1 ) f( x) f( x 1 ), ( x, x 2 ), x x 1 f( x) f( x 2 ), ( x 1, x), x x 2, f( x) <,, 10-6, f( x), : f( x), x 3 - x 4 + 4x 2-1 xpoint( x1, x2), f( x1) f( x2 ) x x
134 root( x1, x2 ), ( x1, x2) root xpoint, xpoint f ( 2) : : root( x1, x2) x, y, y1; y1 f( x1) while( ) x xpoint( x1, x2) / * x; * / y f( x) if( y* y1 > 0) then x1 x, y1 y else x2 x x; ( 3) : #include < math. h > float f( float x) { float y; y = x* x* x* x* x - x* x* x* x + 4* x* x - 1; return y; float xpoint( float x1, float x2) { float x; x = ( x1* f( x2) - x2* f( x1) ) / ( f( x2 ) - f( x1) ) ; return x; float root( float x1, float x2) { float x, y, y1; y1 = f( x1) ; do { x = xpoint( x1, x2) ; y = f( x) ; if( y* y1 > 0) { y1 = y; x1 = x; else x2 = x;
while( fabs( y) > = 0. 000001) ; return x; { float x1, x2, f1, f2, x; do { printf( input x1, x2: \n ) ; scanf( % f, % f, &x1, &x2) ; f1 = f( x1) ; f2 = f( x2) ; while( f1* f2 > = 0) ; x = root( x1, x2) ; printf( " The root of equation is % 8. 4f", x) ; : Input x1, x2: 0, 1. 0 : The root of equation is 0. 5081 : Input x1, x2: - 1. 0, 0 : The root of equation is - 0. 5291 135 4. 4,,,,,,, : ;, ;, ( ), ;,,,,,,,,
136 4. 10 { void reverse( ) ; reverse( ) ; void reverse( ) { char ch;, ch = getchar( ) ; if( ch = = # ) else printf( " % c", ch) ; { reverse( ) ; : abcdef# printf( " % c", ch) ; : #fedcba ( FILO),, 4. 10, abcdef,, printf( " : % c", ch), a f #,, #,, f a 4. 11 ( 1) : n! : n! = n* ( n - 1)! ( n > 1) : n! = 1 ( n = 0, 1) ( 2) : : fac( n) if( n = 0 n = 1) else 1 ; then fac( n - 1) * n ( 3) C : { long fac( long n) ;
long i, result; scanf( " % ld", &i) ; result = fac( i) ; printf( " % ld! = % ld \ n", i, result) ; long fac( long n) { if( n < = 1 ) return 1; else return fac( n - 1) * n; : 4 : 4! = 24 i 4, 4! 4 * 3!, 3!, 3! 3* 2!, 2!,,, 1! = 1,, 1!, 2!, 3!, 4!, 4. 6 137 4. 12 : ( 1) : f( n) k if( n = 0 n = 1) then else k 1 k f( n - 1) + f( n - 2) k ( 2) C : 4. 6 f( n) = 1 n = 0 n = 1 f( n - 1) + f( n - 2) n > 1
138 { int n; float f( int i) ; / * * / printf( " Input n: " ) ; scanf( " % d", &n) ; / * n* / printf( " \nf( % d) = % f", n, f( n) ) ; float f( int i) { float k; if( i = = 0 i = = 1) / * * / else k = 1; k = f( i - 1) + f( i - 2) ; / * * / return k; : Input in : 12 : f( 12) = 233. 000000, 2, 2,,,, 4. 13 ( Tower of Hanoi) A B C3, A 64 A B,,, 64 : 2 64 A) ; B* / ( 1) : - 1 = 18, 466, 744, 073, 709, 511, 615 1 : n - 1 B C, hanoi( n - 1, A, C, B) ; 2 : n A B ; 3 : C n - 1 A B, hanoi( n - 1, C, B, ( 2) : : Hanoi( n, A, B, C) / * Hanoi A n C if( n = 1) then else A-> B ; / * n = 1 * / Hanoi( n - 1, A, C, B) / * A n - 1 B C* / A-> B ; / * A B; * /
Hanoi( n - 1, C, B, A) / * C n - 1 A B* / ( 3) C : { int n; void hanoi( int n, char a, char b, char c) ; printf( " \nplease input the number of disks to be moved: " ) ; scanf( " % d", &n) ; hanoi( n, a, b, c ) ; void hanoi( int n, char a, char b, char c) { if( n > 0 ) { hanoi( n - 1, a, c, b) ; printf( " \nmove disc % d from pile % c to % c", n, a, b) ; hanoi( n - 1, c, b, a) ; : Please input the number of disks to be moved: 3 : Move disc 1 rom pile a to b Move disc 2 from pile a to c Move disc 1 from pile b to c Move disc 3 from pile a to b Move disc 1 from pile c to a Move disc 2 from pile c to b Move disc 1 from pile a to b,,,,,,,,, 139 4. 5 C C C,, C,, #,,
; C, 3 : C, 4. 5. 1 #define, 1 ) 140 : #define,,, ( ),, 4. 14 #define PI 3. 1415926 #define R 2. 0 { double circum( ) ; double area( ) ; circum( ) ; area( ) ; printf( " Circum = % f, Area = % f\ n", circum( ), area( ) ) ; double circum( ) { return( 2. 0 * PI* R) ; double area( ) { return( PI* R* R) ; circum( ) area ( ), : double circum( )
{ return( 2. 0 * 3. 1415926* 2. 0) ; double area( ) { return( 3. 1415926* 2. 0* 2. 0) ; :? Circum = 12. 566370, Area = 12. 566370,, 4, : # define R 4. 0,,,,,, #define EOF - 1 / * * / #define NULL 0 / * * / #define MIN 1 / * * / #define MAX 31 / * * / #define STEP 2 / * * / #define PI 3. 1415926 / * * / #undef, : #undef R R 141 2 ) C, : #define ( ) : ( ) :,, 4. 15 #define PI 3. 145926 #define S( r) ( PI* ( r) * ( r) ) { float a, b, area1, area2; a = 3. 3; b = 3. 2;
area1 = S( a) ; area2 = S( a + b) ; printf( " area1 = % f, area2 = % f \n", area1, area2) ; : area1 = 34. 259132, area2 = 132. 915375, area1 = S( a), area2 = S( a + b ) : area1 = 3. 1415926 * 3. 3* 3. 3, area2 = 3. 1415926* ( 3. 3 + 3. 2) * ( 3. 3 + 3. 2) :, 142 4. 16 #define SQ1( x) x* x #define SQ2( x) ( x) * ( x) #define SQ3( x) ( ( x) * ( x) ) { printf( % f, % f, % f, 1. 0 / SQ1 ( 1. 0 + 1. 0 ), 1. 0 / SQ2 ( 1. 0 + 1. 0 ), 1. 0 / SQ3( 1. 0 + 1. 0) ) ; : 3. 000000, 1. 000000, 0. 250000 3 x,, 3 : 1. 0 / SQ1( 1. 0 + 1. 0) 1. 0 /1. 0 + 1. 0* 1. 0 + 1. 0 ( ) 1. 0 / SQ2( 1. 0 + 1. 0) 1. 0 / ( 1. 0 + 1. 0) * ( 1. 0 + 1. 0) ( ) 1. 0 / SQ2( 1. 0 + 1. 0) 1. 0 / ( ( 1. 0 + 1. 0) * ( 1. 0 + 1. 0) )( ) 3 ), :,, :,,,,, ;,,,
4. 5. 2 : #include < > #include,, ;,, C : 1 ) math. h,,, ( 1) int abs( int x) ; double fabs( double x) ; / * x * / double floor( double x) ; / * x * / double fmod( double x, double y) ; / * x/ y * / double pow( double x, double y) ; / * x y * / double sqrt( double x) ; / * x * / ( 2) double sin( double x) ; double cos( double x) ; double tan( double x) ; double exp( double x) ; double log( double x) ; ( 3) ( stdlib. h ) int rand( void) ; int rand( int seed) ; /* - 90 32767 * / /* seek ( - 90 32767 ) * /,,,, 143
2 ) ctype. h,,,, int int isalpha( int ch) ; / * ch, 1, 0 * / int isdigit( int ch) ; / * ch ( 0 9), 1, 0 * / int iscntrl( int ch) ; / * ch, 1, 0 * / int isprint( int ch) ; / * ch, 1, 0 * / int tolower( int ch) ; / * ch * / int toupper( int ch) ; ch * / 3 ) 144 string. h,,, strlen( ) : int strlen( const char * s) ; s,, strcpy( ), : char * strcpy( char * s1, const char * s2) ;, s1 s2, s2 s1 s1, : s1 s2, strcat( ), : char * strcat( char * s1, char * s2) ;, s1 s2, s1,, s1 s2, s1 strlen( s1 ) + strlen( s2) + 1 strcmp( ), 0, ; 0, ; 0, : int strcmp( const char * s1, const char * s2 ) ; int strncmp( const char * s1, const char * s2, int n) ; strcmp( ), strncmp( ) s1 s2 n
,, 6 C,,,,, C,, C,, C, ( 1) #include C, C 4. 17 C #include " ex4_17b. cpp" { long fac( long n) ; long i, result; scanf( " % ld", &i) ; result = fac( i) ; printf( " % ld! = % ld \ n", i, result) ; / * ex4 _17b. c* / long fac( long n) { if( n < = 1 ) return( 1) ; else return( fac( n - 1) * n) ;, C ex4-17a. cpp ex4-17b. cpp, ex4-17a. cpp #include ex4-17 b. cpp ex4-17b. cpp ex4-17 a. cpp : ex4-17a. cpp ex4-17b. cpp,, : myfile. h, ( ),, : 145
146 #include < myfile. h > ( 2) C C Turbo C + + 3. 0IDE, Turbo C + + 3. 0IDE : Project( ) ; Project Open Project( ) ; ( ) ; ( ), OK Project: < > ; Project, Add Item Add to Project List ; Add to Project List C, Add ; 5,,, 4. 18 C C ex4-17a. cpp ex4-17b. cpp, ex4-17. prj Project( ) ; Project Open Project( ) ; ex4-16;, OK Project: ex4-17. prj ; Project, Add Item Add to Project List ; Add to Project List ex4-17a. cpp, Add ; ex4-17b. cpp, Add 4. 5. 3 C,, : ( 1) #if < 1 > < 1 > #elif< 2 > < 2 >
#elif< n > #else #endif < n > < > : 0 ( ),, 4. 19,,,, #, LETTER #include stdio. h #define LETTER 1 { unsigned char ch; 2 while( 1) { ch = getchar( ) ; if( ch = = # ) break; #if LETTER if( ch > = a &&ch < = z ) ch - = 32; #else if( ch > = A &&ch < = Z ) ch + = 32; #endif putchar( ch) ; : afaffas# : AFAFFAS ( 2) #ifdef #else #endif 1 2 0 : #define, 1, 4. 20 #ifdef 147
148, TRANS #define, #define TRANS { char ch; while( 1) { ch = getchar( ) ; if( ch = = # ) break; #ifdef TRANS if( ch > = a &&ch < = z ) ch - = 32; #else if( ch > = A &&ch < = Z ) ch + = 32; #endif putchar( ch) ; ( 3) #ifndef #else #endif 1 2 #ifdef #endif, 4. 21 #ifndef, TRANS #define, #define TRANS { char ch; while( 1) { ch = getchar( ) ; if( ch = = # ) break; #ifndef TRANS
if( ch > = a &&ch < = z ) ch - = 32; #else if( ch > = A &&ch < = Z ) ch + = 32; #endif putchar( ch) ; : ASDAF# : asdaf 4. 6 C C, C, C, C : C main ( ) C, ( ) (, goto ), : ( ) ; ( ) ; 149 4. 6. 1, C,,, ;, 3 :,,, 2,,,,,
150, 2,,,,,,,,,,,,, ;,, C, : ; ; : ( auto) ( register) ( static) ( extern) 4. 6. 2 C,, : auto auto, auto, auto,,, ;,, 3 : ; ; :, ;, 4. 22
{ int x = 1; { void print( ) ; int x = 3; print( ) ; printf( " 2ND x = % d \n", x) ; printf( " 1ST x = % d \n", x) ; void print( ) { int x = 5; printf( " 3TH x = % d \ n", x) ; : 3 TH x = 5 2 ND x = 3 1 ST x = 1, 3 x, main( ) print( ) x, ;, main( ) x, x x 4. 23 1 + 2 + 3 + + 10 { int sum( int j) ; int j, s; for( j = 1 ; j < = 10; j + + ) s = sum( j) ; printf( " s = % d \n", s) ; int sum( int j) { int x = 0; x + = j; return( x) ; : s = 10( s = 55),,, 151
,, 4. 6. 3 152 CPU : register : register ;, register, register :,,,, C,,, 4. 6. 4,,,, C, static,, : satic ; :,,,, 0 ( ) 0. 0 ( ) ( ),
, 4. 24 int fauto( ) { int j = 0; / * fstatic j * / j + + ; return j; int fstatic( ) { static int j = 0; / * fauto j * / * / j + + ; return j; { printf( " % d, % d \ n", fauto( ), fauto( ) ) ; printf( " % d, % d \ n", fstatic( ), fstatic ( ) ) ; : 1, 1 2, 1 4. 25 long fac( int n) { static long f = 1; f = f* n; return f; { int k; for( k = 1; k < = 5 ; k + + ) printf( % d! : 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 = % ld \ n, k, fac( k) ) ; / * printf 153
4. 6. 5 154 C,,,, ;,, : extern, 4. 26 extern int j = 1; { void add( ) ; add( ) ; j + + ; printf( " j = % d \ n", j) ; void add( ) { j + + ; : j = 3, 2, return, 4. 27 x, y float sum, ave; / * * / { int x, y; void f( int x, int y) ; / *, * / scanf( " % d, % d", &x, &y) ; / * * / f( x, y) ; / * * / printf( " sum = %. 2f\ n ave = %. 2f \n", sum, ave) ; void f( int x, int y) { sum = x + y; / * x, y, sum * / ave = ( x + y) /2. 0 ; / * x, y, ave * /
: 2, 3 : sum = 5. 00 ave = 2. 50,, :,,,,,,,,,,,, :, ( ), 4. 28 extern int j = 1; { void add( ) ; add( ) ; j + + ; printf( " j = % d \ n", j) ; void add( ) { int j = 0; j + + ; 4. 26 add ( ) j, add( ) j, : j = 2 extern,, extern 4. 29 extern, 155
156 { extern int a, b; / * a, b, int max( int x, int y) ; * / printf( " max = % d \n", max( a, b) ) ; int a = 5, b = 8; int max( int x, int y) { int z; z = x > y? x: y; return( z) ; : max = 8 / * a, b, * / extern C,, extern, extern 4. 30 / * ex4 _30A. cpp* / int x, y; / * x, y, ch* / char ch; { extern void fun1( ) ; / * fun1, fun2 * / extern void fun2( ) ; ch = a ; x = 10; y = 4; fun1 ( ) ; fun2 ( ) ; printf( " x = % d, y = % d \n", x, y) ; printf( " ch = % c \ n", ch) ; / * ex4-30b. cpp * / extern int x, y; / * x, y * / void fun1 ( ) { y + = 1; x = ( + + x + 4) % y;
/ * ex4-30c. cpp * / extern char ch; / * ch * / void fun2 ( ) { printf( " ch = % c in file ex4-30 c. cpp \n", ch + + ) ; ch + + ; ex4-30 a. exe : ch = a in file ex4-30c x = 0, y = 5 ch = c,, extern, static 4. 31 / * ex4 _31A. cpp* / int x, y; /* x, y* / static char ch; / * ch, * / { extern void fun1( ) ; /* fun1, fun2 * / extern void fun2( ) ; ch = a ; x = 10; y = 4; fun1 ( ) ; fun2 ( ) ; printf( " x = % d, y = % d \n", x, y) ; printf( " ch = % c \ n", ch) ; / * ex4-31b. cpp * / extern int x, y; /* x, y * / void fun1 ( ) { y + = 1; x = ( + + x + 4) % y; 157
/ * ex4-31c. cpp * / extern char ch; /* ch, * / void fun2 ( ) /* ch * / { printf( " ch = % c in file ex4-31 c. cpp \n", ch + + ) ; ch + + ; 3 C, ex4-31. prj, ex4-31prj : undefined symbol _ch in module ex4-31 c. cpp( ex4-31. cpp ch) 4. 1 4. 1 ( ) ( ) 158 CPU ( ) 4. 6. 6, C,,,,,,, static, : static ( ) { static,, static, :,,
:, static, extern, : [ extern] ( ) {, : [ extern] ( ) ; 4. 32 ( 1) mainf. c main( ) { extern void input( ), process( ), output( ) ; input( ) ; process( ) ; output( ) ; ( 2) subf1. c extern void input( ) / * * / { ( 3) subf2. c extern void process( ) / * * / { ( 4) subf3. c extern void output( ) / * * / { 159 4 4. 1, void main ( ) { int x, n, s; power( y) { int i, p = 1; for( i = 0; i < = n; + + i) p = p* y; ; 4. 2 ( 1) C, ( ) A. C,,,
160 B. C, C., D. C ( 2) C, ( ) A. auto B. static C. extern D. ( 3), ( ) A., B., C., D., ( 4) ( ) A. x = 1 y = 1 x = 1 y = 1 B. x = 1 y = 1 x = 2 y = 2 C. x = 1 y = 1 x = 2 y = 1 D. x = 1 y = 1 x = 1 y = 2 : { void f1( ) ; void f2( ) ; f1( ) ; f2( ) ; f1( ) ; f2( ) ; void f1( ) { int x = 0; printf( " x = % d \t", + + x) ; void f2( ) { static int y = 0; printf( " y = % d \t", + + y) ; ( 5) ( ) A., B., C. D. 4. 3
( 1) ( 2) : return < > 4. 4 ( 1) int a, b, c, d; { void p( int a, int b) ; a = b = c = d; printf( " % d, % d, % d, % d \n", a, b, c, d) ; p( a, b) ; printf( " % d, % d, % d, % d", a, b, c, d) ; void p( int a, int b) { static int c; a + +, b + + ; c - = 10, d + = 5 ; if( a < 3) p( a, b) ; printf( " % d, % d, % d, % d", a, b, c, d) ; ( 2) float f( int n) { float x; if( n < 0) printf( " n < 0, data error! \ n" ) ; else if( n = = 0 n = = 1) x = 1 else x = f( n - 1 ) * n; return x; { int i, n; float y = 0; printf( " input a integer number: " ) ; scanf( " % d", &n) ; for( i = n; i > = 1; i - - ) y = y + f( i) ; printf( " y = % 5. 1f \n", y) ; 161
162 ( 3) / * 1, 1* / #define PI 3. 14 #define S( r) PI* r* r { float a, b, s1, s2 ; printf( " Input the a, b: " ) ; scanf( " % f, % f", &a, &b) ; s1 = S( a) ; s2 = S( a + b) ; printf( " s1 = % f, % s2 = % f\ n", s1, s2) ; ( 4) / * 2005* / { void printd( int n) ; int num; printf( " Input the number" ) ; scanf( " % d", &num) ; printd( num) ; void printd( int n) { if( n < 0) { putchar( - ) ; n = - n; if( n /10) printd( n /10 ) ; putchar( n% 10 + 0 ) ; 4. 4 n n : 4, 13, 15, 17, 19 4 3 = 13 + 15 + 17 + 19 4. 5 Hermite, Hn( x) : H 0 ( x) = 1 n = 0 H 1 ( x) = 2x n = 1 H n ( x) = 2xH n - 1 ( x) - 2 ( n - 1 ) H n - 2 ( x) n > 1 4. 6 2 1000 (,,
, 25, 25 2 = 625, 625 25, 25 ) 4. 7 kkkkk kk( n k, n k ), : Sn = a + aa + aaa + + aaaa aa( n a) (, n = 4, k = 3, : s = 3 + 33 + 333 + 3333) 163
164 C ; ;, C, C, C 5. 1 5. 1. 1,? 1 1,, 16 2, 1,,,,,,
,, C, : scanf,,,,,, 5. 1 : 5. 1 *, ; :, auto register static extern ;,, ;, : int * ptr; / * ptr* / char * ch; /* ch* / :, ( * ),, ptr * ptr, : : * = ; int count = 18, * ptr = &count;, *, : int * ptr1, ptr2; ptr1 ptr2 2, : int * ptr1, * ptr2; 165
5. 1. 2 1 ) 166,,,,, C,,,, C &, &,, &x x ptr, a ptr 2 : ( 1) int a; int * ptr = &a; /* a ptr* / ( 2) int a; int * ptr; ptr = &a;, : int * p; p = 1000; /* * / *, * p = &a : int a; char * ptr; ptr = &a; /*, * / : int i, * ptr; i = 20; ptr = &i;
5. 2 & ptr i, ptr i 5. 2 2 ) * * ( ),, : int x = 20, y, * ptr; ptr = &x; y = * ptr; : x( 20 ) y ptr, x ptr, ptr, y, y = x;, 5. 3 167 5. 3, * : printf( % d, * i) ; / * error, * / : *, ( &) &,,,
3 ) p, n, p + n p - n n, n,, 16 char 1 int 2 long float 4 double 8, : 1 2 4 8,,,, n, ptr1, 3000, 2, ptr ++ ; ptr += 1; ptr 3002 3001 ptr, ptr - - ptr 2998, 5. 4 168 5. 4 1 1,, ptr = ptr + n; ptr n 2 2, 5. 1 { int a = 1, b = 2, c = 3, * ptr; ptr = &b; printf( " b: Address % x, Value % d \ n", ptr, b) ; ptr ++ ; printf( " c: Address % x, Value % d \ n", ptr, c) ; ptr - = 2;
) : printf( " a: Address % x, Value % d \ n", ptr, a) ;, ( b: Address fff2, Value 2 c: Address fff4, Value 3 a: Address fff0, Value 1 : 3 a b c, ptr b, b fff2; 1, 1 * 2 = 2, ptr c, fff4; ptr - 2, ptr a, fff0 :,, 5. 2 { int i = 20; int * iptr = &i; printf( " i: value % d \ n", * iptr) ; printf( " i address: % d \n", iptr) ; printf( " iptr address: % d \ n", &iptr) ;,, * iptr iptr &iptr, : i value: 20 i address: 6630 iptr address: 6628 2 5. 3 { int a = 1, b = 2, * aptr, * bptr; aptr = &a; bptr = &b; printf( " a: Address % x; b: Address % x \n", aptr, bptr) ; printf( " aptr > bptr = % d \n", aptr > bptr) ; printf( " aptr < = bptr = % d \ n", aptr < = bptr) ; 169
170 printf( " aptr&&bptr = % d \ n", aptr&&bptr) ;, : a: Address fff4, b: Address fff2 aptr > bptr = 1 aptr < = bptr = 0 aptr&&bptr = 1,, :, 2, : 2 ptra ptrb, ptra + ptrb,, : Null point assigment,, 5. 4 2 { int a = 1, b = 2, * aptr = &a, * bptr = &b; printf( " * ptra: Value % d \ t* ptrb: Value % d \n", * aptr, * bptr) ; bptr = aptr; printf( " * ptra: Value % d \ t* ptrb: Value % d \n", * aptr, * bptr) ; bptr = aptr bptr aptr ( a) : 5. 5 * ptra: Value 1 * ptrb: Value 2 * ptra: Value 1 * ptrb: Value 1 2 { int a = 1, b = 2, * aptr = &a, * bptr = &b, * tptr; printf( " * ptra: Value % d \ t* ptrb: Value % d \n", * aptr, * bptr) ; tptr = aptr, aptr = bptr, bptr = tptr; printf( " * ptra: Value % d \ t* ptrb: Value % d \n", * aptr, * bptr) ; 3 tptr = aptr = bptr = tptr aptr bptr, 2 :
5. 6 * ptra: Value 1 * ptrb: Value 2 * ptra: Value 2 * ptrb: Value 1 2 { int a = 1, b = 2, temp, * aptr = &a, * bptr = &b; printf( " a: Value % d \tb: Value % d \n", a, b) ; temp =* aptr, * aptr =* bptr, * bptr = temp; printf( " a: Value % d \tb: Value % d \n", a, b) ; 3 temp =* aptr * aptr =* bptr * bptr = temp a( * ptra) b( * ptrb), 2 : 5. 7 a: Value 1 b: Value 2 a: Value 2 b: Value 1 { int a = 20 ; int * aptr; aptr = &a; printf( " a: Value % d \n ", a) ; * aptr = 30; printf( " after changed a: Value % d ", a) ; * aptr = 30 a, a = 30, : 5. 8 a: Value 20 after changed a: Value 30 x y, z 3, ( ) ( 1) : 3 2, 3 3 ( 2) : : x, y, z pmax, pmin x, y, z if( x > y) then 171
172 : pmax = &x; : pmin = &y; else : pmax = &y; : pmin = &x; if( z >* pmax) then pmax = &z; if( z <* pmin) then pmin = &z; : * pmax, * pmin ( 3) : { int x, y, z, * pmax, * pmin; printf( " Input three numbers: \ n" ) ; scanf( " % d% d% d", &x, &y, &z) ; if( x > y) { pmax = &x; pmin = &y; else { pmax = &y; pmin = &x; if( z >* pmax) pmax = &z; if( z <* pmin) pmin = &z; printf( " max = % d \nmin = % d \n", * pmax, * pmin) ;
5. 2 5. 2. 1,,,, 5. 9 2, ( 1) : 2 swap 2 2,, 2 ( 2) : void wap( int x, int y) ; { int a, b; printf( " Input a, b: \ n" ) ; scanf( " % d% d", &a, &b) ; printf( " a = % d, \ tb = % d \ n", a, b) ; if( a < b) swap( a, b) ; printf( " after swap: \n" ) ; printf( " a = % d, \ tb = % d, \n", a, b) ; void wap( int x, int y) { int temp; temp = x; / * 2 * / x = y; y = temp; printf( " In function swap: x = % d, \ty = % d, \ ttemp = % d \n", x, y, temp) ; 173
174, : Input a, b: 7 10( ) a = 7, b = 10 In function swap: x = 10, y = 7, temp = 7 after swap: a = 7, b = 10,, swap x y main a b 5. 5 ( 3) : 5. 5 { void swap( int * ptr1, int * ptr2) ; int a, b, * p1, * p2; printf( " Input a, b: " ) ; scanf( " % d% d", &a, &b) ; printf( " a = % d, \ tb = % d \ n", a, b) ; p1 = &a; p2 = &b; if( a < b) swap( p1, p2 ) ; printf( " after swap: \n" ) ; printf( " a = % d, \ tb = % d \ n", a, b) ; void swap( int * ptr1, int * ptr2) { int temp; temp =* ptr1; * ptr1 =* ptr2; * ptr2 = temp; input a, b: 7 10( ) a = 7, b = 10 after swap: a = 10, b = 7 :, void swap( int * ptr1, int * ptr2) ;
, swap( p1, p2) ;, 2 5 swap( p1, p2 ), swap * ptr1 * ptr2 main a b temp, a b, a b, swap a b 5. 6 5. 10 2 { int add( int * ptr1, int * ptr2) ; int x1, x2, y; printf( " Input x1, x2: " ) ; scanf( " % d, % d", &x1, &x2) ; y = add( &x1, &x2) ; 5. 6 printf( " y = x1 + y2 = % d \ n", y) ; int add( int * ptr1, int * ptr2) { int z; z =* ptr1 +* ptr2; return z; 5. 10, add 2, 2 int add, x1 x2 &x1 &x2 ptr1 ptr2, ptr1 ptr2 x1 x2, * ptr1 * ptr2 x1 x2, * ptr1 +* ptr2 x1 + x2 :, 175 5. 2. 2 : int x, * ptr; ptr = &x;, ptr, x : 5. 11
176 { void swap( int * ptr1, int * ptr2) ; int x, y, * ptr1, * ptr2; printf( " Input x, y: " ) ; scanf( " % d% d", &x, &y) ; printf( " before swap: % d \ t% d \ n", x, y) ; ptr1 = &x; ptr2 = &y; if( x < y) swap( ptr1, ptr2) ; printf( " after swap: % d \ t% d \ n", x, y) ; void swap( int * ptr1, int * ptr2) { int * temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; printf( " in swap: % d \t% d \ n", * ptr1, * ptr2) ;, x y &x &y, ptr1 ptr2 x y x y, ptr1 ptr2,,, 2, : Input x, y: 30, 100( ) before swap: 30 100 in swap: 100 30 after swap: 30 1 000, 2 5. 3 C,,,,,, float fun( ), fun,,
,, ( ) : ( * ) ( ) ;, ; : int ( * function) ( ) ; ( ) function,, 5. 3. 1, : : void swap( int * ptr1, int * ptr2 ) ; : void ( * fptr) ( int * ptr1, int * ptr2) ; swap fptr : fptr = swap; ( ), 5. 12 1 n, n, : 1 + 1 /2 2 + 1 /4 2 + + 1 /n 2 n, : 1 + 1 /3 2 + 1 /5 2 + + 1 /n 2 ( 1) : ptr / * ptr * / n if( n > 1) then if( n ) then ptr fun1 else ptr fun2 else 177
178 ( 2) : double func1( int x), func2( int y) ; { double ( * fptr) ( int) ; / * fptr* / int n; printf( " Input a number: " ) ; scanf( " % d", &n) ; if( n > 1 ) { if( n% 2 == 0) fptr = func1; / * n, ptr func1( ) * / else fptr = func2; / * n, ptr func2( ) * / printf( " value = % 9. 4f\ n", ( * fptr) ( n) ) ; else printf( " error! \ n" ) ; double func1( int x) { int k; double value; value = 1. 0; for( k = 2; k < = x; k = k + 2) value = value + ( 1 / ( double) k) * ( 1 /( double) k) ; return value; double func2( int y) { int k; double value; value = 1. 0; for( k = 3; k < = y; k = k + 2) value = value + ( 1 / ( double) k) * ( 1 /( double) k) ; return value; main, fptr, :
,, fptr = func1; ( * fptr) ( n), : fptr = func1; fptr = func2; fptr : fptr = func1 ; fptr = func2;, : fptr, fptr = fptr + 1 fptr = fptr - 1 5. 3. 2 C,,,, ( ) 5. 13 f 1 ( x) = 1 + x 2 [ 0, 1] f 2 ( x) = 1 / ( 1 + 16x 2 ) [ - 1, 1] ( 1) : 2,, double, x, float : double collect( double ( * p) ( float x), float a, float b, int n) 4, p, double a, b, n, 2 collect n ( 2) : { int n; double y1, y2; double fun1( float x) ; double fun2( float x) ; double collect( double ( * p) ( float x), float a, float b, int n) ; y1 = collect( fun1, 0. 0, 1. 0, 200) ; y2 = collect( fun2, 0. 0, 2. 0, 200) ; printf( " y1 = % f \ny2 = % f\ n", y1, y2) ; double collect( double ( * p) ( float x), float a, float b, int n) 179
180 { int i; float f, h, area; h = ( b - a) / n; area = ( ( * p) ( a) + ( * p) ( b) ) /2. 0; for( i = 1 ; i < n; i ++ ) area += ( * p) ( a + i* h) ; return area* h; double fun1( float x) / * 1 + x 2 C * / { float f; f = 1 + x* x; return f; double fun2( float x) / * 1 / ( 1 + 16x 2 ) C * / { float f; f = 1 /( 1 + 16* x* x) ; return f; 5. 4,, ( void), C ( ), : * ( ) ;, : float * funcname( float x, float y) : float funcname, funcname, float * funcname( float x, float y) float * ptr ptr *, * ptr,, funcname *,, * x y
C, ( ), malloc malloc stdlib. h alloc. h, #include : void * malloc( unsigned size) ; : C ( ) size, ( ),, NULL, free free stdlib. h alloc. h, : void free( void * ptr) ; ptr 5. 14 malloc free #include < stdlib. h > { int * ptr; ptr = ( int * ) malloc( sizeof( int) ) ; if( ptr == NULL) { printf( " Failed to create a new object. " ) ; exit( 0) ; * ptr = 100; printf( " value of * ptr is: % d \ n", * ptr) ; free( ptr) ; malloc, ptr, * ptr, 100 * ptr,, free 181 5. 5 : C,, :,,
182,,, : { int i; char * ptr; i = 200 ; * ptr = i; ptr, ptr, * ptr = i i 200,, : char ch, * ptr1; / * ch ptr1* / double db, * ptr2; / * db ptr2* / ptr1 = &db; ptr2 = &ch; ptr1, db, ptr2, ch, ptr * ptr, * ptr ptr 5 5. 1 ( 1) char * ptr, a; ( ) A. a = ptr B. ptr = ptr /2 C. ptr = &a D. * p = 20 ( 2), ( ) A. B. C. D. ( 3) char ** pptr; ( ) A. pptr = 100 B. * pptr = " study" C. ** pptr = " study" D. * pptr = study ( 4) int a = 20; int * ptrl, * ptr2; ptrl ptr2 a, ( )
A. a = * ptr1 + * ptr2; B. ptr2 = a; C. ptrl = ptr2; D. a = * ptrl* ( * ptr2) ( 5) int ( * ptr) ( ) ; ( ) A. ptr, int B. ptr C. ptr, D. ptr ( 6 ) add( x, y), ptr add, ( ) A. ptr = max B. * ptr = max C. ptr = max( a, b) D. * ptr = max( a, b) ( 7) min( a, b), ptr, ( ) A. ( * ptr) min( a, b) B. * ptr min( a, b) C. ( * ptr) ( a, b) D. * ptr( a, b) ( 8), a b ( ) main( ) { int a, b, k = 4, m = 6, * ptr1 = &k, * ptr2 = &m; a = ptr1 == &m; b = ( * ptr1) / ( * ptr2 ) + * ptr1 ; printf( " a = % d \ n", a) ; printf( " b = % d \ n", b) ; A. 0, 6 B. 1, 6 C. 0, 4 D. 1, 4 ( 9) 2, ( ) main ( ) { void swap( int * p, int * q) ; int a = 10, b = 20; printf( " a = % d, b = % d \ n", a, b) ; swap( &a, &b) ; printf( " a = % d, b = % d \ n", a, b) ; void swap( int * p, int * q) { int * t; 183
184 t = p; p = q; q = t; A. B., swap( &a, &b) ; a, b C., swap ( ) p q t ( ) D. ( 10), ( ) main( ) { int * iptr, i; char * qptr, ch; iptr = &i; qptr = &ch; * iptr = 40; * iptr =* qptr; A. iptr qptr, * p =* q B. * iptr, * iptr = 40; C. qptr, * qptr D. qptr,, * iptr =* qptr; ( 11) char * ptr; char ch; ptr = &ch; ( ) A. ch, ptr, * &a B. ch, &ch, * ptr C. * &ptr, * ptr, &ch D. &ch, &* ptr, ptr 5. 2 ( 1 ) : char * func1 ( ) ; char ( * func2 ) ( ) ; ( 2) C, ( 3) main( ) { int * ptr; ptr = 200; ( 4)
main( ) { int ** ptr1, * ptr2, a = 20 ; ptr2 = &a; ptr1 = &ptr2; printf( " % d \ n", ** ptr1) ; ( 5) main( ) { int * ptr, a; a = 20; ptr = &a; a = * ptr + 10; printf( " % d \ n", a) ; ( 6) sub( int * s) ; main( ) { int i, k ; for( i = 0; i < 4; i ++ ) { k = sub( &i) ; printf( " % 3d", k) ; printf( " \ n" ) ; sub( int * s) { static int t = 0; t = * s + t ; return t; ( 7) #include " stdio. h" int func( int x, int y, int * cp, int * dp) ; main( ) { int a, b, c, d; 185
186 a = 4; b = 3 ; func( a, b, &c, &d) ; printf( " % d \ n", c + d) ; func( int x, int y, int * cp, int * dp) { * cp = x + y; * dp = x - y; return 1; 5. 3 ( 1)? ( 2)? ( 3)? 5. 4 ( 1) pa pb, int a int b pa pb : a b ; (, 0 ) ;, pa, pb ( 2) 3 a, b, c, ( ) ( 3) 3, ( 4) f 1 ( x) = 1 + x + 1 / x 2 ( 1, 2] ( 5), 2 ( 6) 1 n, n, : 1 + 1 /2 + 1 /4 + + 1 / n n, : 1 + 1 /3 + 1 /5 + + 1 / n
6. 1 187 6. 1. 1,,, C,,,, ( ), : array_int 65 73 45 23 68 91 35 18 37 15 0 1 2 3 4 5 6 7 8 9 word_arr T h e w o r d - f o r m a t i o n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17,
188, Turbo C + + 3. 0, 2, 1, 0,, : [ ] ;,,, C,,,,,, word _arr 100, int array_int[ 10] ; / * array_int, 10 * / float b[ 10], c[ 20] ; / * b, c; 10 20 * / char word_arr[ 100] ; / * word_arr, 100 * / :,,,, 2 :, ;,,,, C 0, : #define FD 50 { int a[ FD + 5], b[ FD] ; { : int n = 5; int a[ n] ; ( ), int a, b, c, d, k1 [ 10], k2[ 20] ;
( auto), ( static) ( extern) : static float score[ 50] ; 50 6. 1 : 10 #define arrlen 10 { int i, a[ arrlen] ; for( i = 0 ; i < arrlen; ) a[ i + + ] = 2* i + 1; i = 5; printf( " \ninput a[ % d] ", i + 2) ; scanf( " % d", &a[ i + 2 ] ) ; printf( " \noutput the list: \n" ) ; for( i = 0 ; i < = arrlen - 1; i + + ) printf( " a[ % d] \ t", i) ; for( i = 0 ; i < = arrlen - 1; i + + ) printf( " % d \ t", a[ i] ) ; printf( " \noutput the reverse list: \ n" ) ; for( i = arrlen - 1; i > = 0; i - - ) printf( " % d \ t", a[ i] ) ; printf( " \na[ 5. 2] = % d a[ 5. 8 ] = % d \ n", a[ 5. 2], a[ 5. 8] ) ; arrlen, 10,, a for, 3 i + +,, i, i = 5; scanf( ), a[ i + 2], 2 for, 3 for 4 for printf a[ 5 ], 189 6. 1. 2 : [ ]
,, i = 4, a [ 5 ], a [ i], a [ i + 2] 3 a[ i], a [ 5], a[ i + 2 ] C,, : int a[ 10 ] ; a[ 10] = 100; a = 100;,,, : ( 1) 190 int arr[ 20] ; for( i = 0; i < 20; i + + ) / * * / scanf( % d, &arr[ i] ) ; for( i = 0; i < 20; i + + ) / * 2340* / arr[ i] = 2340; arr[ 2] = ( 12 + 56) / ( 37* 23) ; / * * / ( 2) for( i = 0; i < 20; i + + ) printf( " % d \n", a[ i] ) ;,, C,, : 6. 2 int a[ 10 ] ; for( i = 0; i < = 20; i + + ) scanf( % d, &a[ i] ) ; ( 1) : i, j; 10 ; i < - 0, j < - 9; while( i < j) i j i + +, j - - 10 ; ( 2) :
{ int arr[ 10], i, j, temp; 6. 3 printf( " Input ten value of Array: \ n" ) ; for( i = 0; i < 10; i + + ) scanf( " % d", &arr[ i] ) ; for( i = 0, j = 9; i < = j; i + +, j - - ) temp = arr[ i], arr[ i] = arr[ j], arr[ j] = temp; for( i = 0 ; i < 10; i + + ) printf( " % 4d", arr[ i] ) ; 10 3 2 3 2 1 2, : ( 1) : 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1, 1 1, n n,,, : yh[ 1] = 1; / * 1 * / row = 2; yh[ row] = 1; / * 2 2, 1* / col = row - 1; / * 2-1 * / row[ col] = yh[ col] + yh[ col - 1] ; / * 2 1 = yh[ 1] + yh[ 0 ] ( yh [ 0] ( 2) : ; row, col; yh[ 1] < - 1 yh[ 1 ] < - 1 row < - 2 while( row < = 10) yh[ row] < - 1 col < - row - 1 while( col > = 2) = 0) * / 191
192 yh[ col] < - n yh[ col] + yh[ col - 1] col < - col - 1 col < - 1 while( col < = row) yh[ col] col < - col + 1 row < - row + 1 ( 3) : { int yh[ 11], row, col; yh[ 1 ] = 1; printf( " \n% 4 d \ n", yh[ 1 ] ) ; for( row = 2; row < = 10; row + + ) { yh[ row] = 1; for( col = row - 1; col > = 2; col - - ) yh[ col] = yh[ col] + yh[ col - 1] ; for( col = 1 ; col < = row; col + + ) printf( " % 4d", yh[ col] ) ; printf( " \n" ) ; : [ ] = { ;,, : int a[ 10 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ; a[ 0] = 1; a[ 1] = 2, ; a[ 9 ] = 10 : char word_arr[ ] = { T, h, e,, w, o, r, d, -, f, o, r, m, a, t, i, o, n ;,, word_arr 18 : {, : int a[ 10] = {0, 1, 2, 3, 4 ; a[ 0] a[ 4] 5, 5 0
, 10 1, : int a[ 10 ] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ; : int a[ 10] = 1;,, 0,,, : int a[ 5] = {1, 2, 3, 4, 5 ; : int a[ ] = {1, 2, 3, 4, 5 ; : int a[ 5] = {1, 2, 3, 4, 5, 6 ; 5, 6 6. 4, 5, 1 5,, 1 ( 1) 1 5 1 5 : int vote[ 6] ; ; ( ) ( 2) : { int vote[ 6] = {0, 0, 0, 0, 0, 0, x, max, k, k1; printf( " \nplease voting paper No. ( - 1 quit) " ) ; scanf( " % d", &x) ; while( x > 0&&x < 6) { vote[ x] = vote[ x] + 1; scanf( " % d", &x) ; max = vote[ 1] ; k1 = 1 ; for( k = 2; k < 6 ; k + + ) if( voet[ k] > max) max = vote[ k], k1 = k; for( k = 1; k < 6 ; k + + ) printf( " % 5d", vote[ k] ) ; printf( " \n The first voter is % d", k1) ;,, 6 a [ 6],, 193
194, x = 33, x[ 33 ] = x[ 33 ] + 1;, x x ( 0 < x < 6 ),, 6. 5, * : Element Value Striation 1 11 * * * * * * * * * * * 2 3 * * * 3 7 * * * * * * * 4 10 * * * * * * * * * * 5 20 * * * * * * * * * * * * * * * * * * * * ( 1), n *, n,,, ( 2) :, ; k < - 0; while( k < ArrSize) k + 1, arr[ k] m < - 0 while( m < arr[ k] ) * m < - m + 1 k < - k + 1 ( 3) : #define ArrSize 5 { int k, m, arr[ ArrSize] = { 11, 3, 7, 10, 20 ; printf( " \nelement \ tvalue \ t Striation \ n" ) ; for ( k = 0; k < ArrSize; k + + ) { printf( " % d \ t% d \ t ", k + 1, arr[ k] ) ;
for( m = 0; m < arr[ k] ; m + + ) printf( " * " ) ; printf( " \n" ) ; 6. 1. 3 C,,,,,, 6. 6 ( 1), 5 ( ), 5,,, i, i, i ( 2) : : arr_insert( int a[ ], int m, int len) n < - 0 while( n < len) if( m < a[ n] ) s < - len - 1 while( s > = n) a[ s + 1] < - a[ s] a[ n] < - m ; s < - s - 1 n < - n + 1 if ( m > a[ len - 1] ) a[ len] < - m len < - len + 1 195
196 len ( 3) : #define ARRLEN 20 { int arr_insert( int a[ ], int m, int len) ; int n, m, len, arr[ ARRLEN] ; len = 8; for( n = 0; n < len; n + + ) scanf( " % d", &arr[ n] ) ; printf( " input inserted a number: " ) ; scanf( " % d", &m) ; len = arr_insert( arr, m, len) ; / * len * / for( n = 0; n < len; n + + ) printf( " % d \ t", arr[ n] ) ; int arr_insert( int a[ ], int m, int len) { int n, s; for( n = 0; n < len; n + + ) if( m < a[ n] ) { for( s = len - 1; s > = n; s - - ) a[ s + 1] = a[ s] ; a[ n] = m; break; if ( m > a[ len - 1] ) a[ len] = m; len + + ; return len;, 8, 8, m : len = arr_insert( arr, m, len) ; arr, a[ ] main( ) arr[ ] a[ ] arr[ ], m, len + +,
6. 1. 4 C,,, : int arr[ 10], * ptr;, ptr arr ptr = &arr[ i] ptr i ( i), ptr = &arr[ 0] ptr = arr, ptr, ( * ) ptr = &arr [ i], * ptr arr[ i], : * ptr = < > scanf( % d, ptr) ; :,, arr = ptr arr + + ;, : ptr = arr ptr + +,, 3 :, : arr[ i], : * ( arr + i), : * ( ptr + i) 6. 7 { int arr[ 5], i, * ptr; 6. 8 ptr = arr; printf( " \ninput value of array: " ) ; for( i = 0 ; i < 5; i + + ) scanf( " % d", arr + i) ; printf( " \n" ) ; for( i = 0 ; i < 5; i + + ) printf( " \n" ) ; printf( " * ( arr + % d) = % d ", i, * ( arr + i) ) ; for( i = 0 ; i < 5; i + + ) printf( " * ( ptr + % d) = % d ", i, * ( ptr + + ) ) ; 197
198 ( 1) :,, 5 i, averandmax, ( 2) : #define ARRLEN 30 float averandmax( float * pa, float * pmax) ; { float sco[ ARRLEN], av, max = - 10000; int i; printf( " \ninput 5 scores: \ n" ) ; for( i = 0 ; i < 5; i + + ) scanf( " % f", &sco[ i] ) ; av = averandmax( sco, &max) ; printf( " \naverage score and max is % 5. 2 f, % 5. 2f", av, max) ; float averandmax( float * pa, float * pmax) { int i; float s = 0; for( i = 0 ; i < 5; i + + ) { s = s + * pa; if ( * pa > * pmax) * pmax = * pa; pa + + ; return s/5; 6. 9 #define ARRLEN 20 { int arr_insert( int * pa, int m, int len) ; int * pn, m, len, arr[ ARRLEN] ; len = 5; pn = arr; for( ; pn < arr + len; ) scanf( " % d", pn + + ) ; printf( " input inserted a number: " ) ;
scanf( " % d", &m) ; len = arr_insert( arr, m, len) ; pn = arr; for( ; pn < arr + len; pn + + ) printf( " % d \ t", * pn) ; int arr_insert( int * pa, int m, int len) { int * pn, * ps; pn = pa; for( ; pn < pa + len; pn + + ) if( m < * pn) { for( ps = pa + len - 1; ps > = pn; ps - - ) * ( ps + 1) = * ps; * pn = m; break; if ( m > * ( pa + len - 1) ) * ( pa + len) = m; len + + ; return len;,, m arr_insert( ), 3 : ( 1) 199 6. 1. 5,,,, 1 ) ( Bubble sorting),,,, ( ), n, n - 1, ( ) : : bubble_sort( int arr[ ], int size) i, j temp, flag;
200 i < - 0 while( i < size) flag < - 0 j < - size - 1 while( j > i) if( arr[ j] < arr[ j - 1] ) arr[ j] arr[ j - 1 ] flag < - 1 j < - j - 1 if( flag = 0) 6. 10 void outarray( int arr[ ], int size) ; void bubble_sort( int arr[ ], int size) ; { int a[ ] = {5, 6, 2, 9, 10, 3, 4, 7, 8, 1 ; int n; n = sizeof( a) / sizeof( int) ; printf( " Before sorting... \n" ) ; outarray( a, n) ; bubble_sort( a, n) ; printf( " After sorting... \ n" ) ; outarray( a, n) ; void bubble_sort( int arr[ ], int size) { int temp, flag; int i, j; for ( i = 0 ; i < size; i + + ) { flag = 0; for ( j = size - 1 ; j > i; j - - ) { if ( arr[ j] < arr[ j - 1 ] ) { temp = arr[ j] ; arr[ j] = arr[ j - 1] ; arr[ j - 1] = temp; flag = 1 ;
if ( flag = = 0) break; void outarray( int arr[ ], int size) { int i; for ( i = 0 ; i < size; i + + ) printf( " % d \ t", arr[ i] ) ; printf( " \n" ) ;, outarray( ) bubble_ sort( ),,, n,,, 2 ) ( Swap sorting),,,, n - 1 : swap_sort( int arr[ ], int size) i, j temp; i < - 0 while( i < size) j < - i + 1 while( j < size) if( arr[ j] < arr[ i] ) arr[ i] arr[ j] j < - j + 1 i < - i + 1 6. 11 void outarray( int arr[ ], int size) ; void swap_sort( int arr[ ], int size) ; { int a[ ] = {5, 6, 2, 9, 10, 3, 4, 7, 8, 1 ; int n; n = sizeof( a) / sizeof( int) ; printf( " Before sorting... \n" ) ; 201
202 outarray( a, n) ; swap_sort( a, n) ; printf( " After sorting... \ n" ) ; outarray( a, n) ; void swap_sort( int arr[ ], int size) { int temp; int i, j; for ( i = 0 ; i < size; i + + ) for ( j = i + 1; j < size; j + + ) if ( arr[ j] < arr[ i] ) { temp = arr[ i] ; arr[ i] = arr[ j] ; arr[ j] = temp; void outarray( int arr[ ], int size) { int i; for ( i = 0 ; i < size; i + + ) printf( " % d \ t", arr[ i] ) ; printf( " \n" ) ; 3 ) ( Select sorting),,, : 8 7 3 9 5 2 7 8 3 9 5 2 3 8 7 9 5 2 2 8 7 9 5 3 1 3 3, 9, 1, n - 1 n - 1 : 8 7 3 9 5 2 ( k = 5) ; 2 7 3 9 5 8 : n,, k, arr [ 0] arr[ k], :
: select_sort( int arr[ ], int size) i, j temp k i < - 0 while( i < size) k < - i; j < - i + 1 while( j < size) if( arr[ j] < arr[ k] ) k < - j; j < - j + 1 if( k i) arr[ i] arr[ k] i < - i + 1 6. 12 void outarray( int arr[ ], int size) ; void select_sort( int arr[ ], int size) ; { int a[ ] = {5, 6, 2, 9, 10, 3, 4, 7, 8, 1 ; int n; n = sizeof( a) / sizeof( int) ; printf( " Before sorting... \n" ) ; outarray( a, n) ; select_sort( a, n) ; printf( " After sorting... \ n" ) ; outarray( a, n) ; void select_sort( int arr[ ], int size) { int i, j, k; int temp; for ( i = 0 ; i < size; i + + ) { k = i; for ( j = i + 1; j < size; j + + ) if ( arr[ j] < arr[ k] ) k = j; if ( k! = i) temp = arr[ i], arr[ i] = arr[ k], arr[ k] = temp; 203
204 void outarray( int arr[ ], int size) { int i; for ( i = 0 ; i < size; i + + ) printf( " % d \ t", arr[ i] ) ; printf( " \n" ) ; 4 ) ( Insert sorting),,, : 49 38 65 97 13 27 48 38 49 65 97 13 27 48 38 49 65 97 13 27 48 38 49 65 97 13 27 48 13 38 49 65 97 27 48 : 1, : : insert_sort( int arr[ ], int size) i, j temp i < - 1 While( i < size) temp < - arr[ i] j < - i - 1 while( j > = 0) if( temp < arr[ j] ) arr[ j + 1 ] < - arr[ j] else j < - j - 1 arr[ j + 1 ] < - temp i < - i + 1 6. 13 void outarray( int arr[ ], int size) ;
void insert_sort( int arr[ ], int size) ; { int a[ ] = {5, 6, 2, 9, 10, 3, 4, 7, 8, 1 ; int n; n = sizeof( a) / sizeof( int) ; printf( " Before sorting... \n" ) ; outarray( a, n) ; insert_sort( a, n) ; printf( " After sorting... \ n" ) ; outarray( a, n) ; void insert_sort( int arr[ ], int size) { int i, j; int temp; for ( i = 1 ; i < size; i + + ) { temp = arr[ i] ; for ( j = i - 1; j > = 0; j - - ) if ( temp < arr[ j] ) arr[ j + 1 ] = arr[ j] ; else break; arr[ j + 1] = temp; void outarray( int arr[ ], int size) { int i; for ( i = 0 ; i < size; i + + ) printf( " % d \ t", arr[ i] ) ; printf( " \n" ) ; 205 6. 1. 6,,,,, : 1 ),
206,,,,, 50%,, 50%, - 1 : SequenceSearch( int arr[ ], int key, int size) n n < - 0 While( n < size) if ( arr[ n] = key) n - 1 6. 14 #include < stdlib. h > #include < time. h > #define ARRSIZE 300 int SequenceSearch( int arr[ ], int key, int size) ; { int x, a[ ARRSIZE], key, size; randomize( ) ; printf( " \ninput list size( < 300 ) : " ) ; scanf( " % d", &size) ; printf( " Auto make a list: \n" ) ; for( x = 0; x < size; x + + ) { a[ x] = rand( ) % 1000; printf( " % d \ t", a[ x] ) ; printf( " \ninput Search key: " ) ; scanf( " % d", &key) ; x = SequenceSearch( a, key, size) ; if ( x! = - 1) printf( " found value in element % d \n", x) ; else printf( " Value not found \n" ) ;
int SequenceSearch( int arr[ ], int key, int size) { int n; for ( n = 0; n < size; n + + ) if ( arr[ n] = = key) return n; return - 1;, 300 a[ ], x, key, size, 100, 100 3 a[ ] rand( ), randomize( ) ; 100 3, key,,, 100 000,, 2 ), ( ),,,,,,,, 1 024,, 10 1 024 2 10, 2, 1 024 2, 10 1, ( ) ;,,,, key = 21 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 < key < key = key,,,,, 100, 50, 20 ( 2 20 = 1 048 576) : : BinarySearch( int arr[ ], int key, int size) low, high, middle; ; low < - 0, high < - size - 1 207
208 while( low < = high) middle < - ( low + high) /2 if( key = arr[ middle] ) middle else if( key > arr[ middle] ) low < - middle + 1 else high < - middle - 1-1 6. 15 #include < stdlib. h > #include < time. h > #define ARRSIZE 300 int BinarySearch( int arr[ ], int key, int size) ; void insert_sort( int arr[ ], int size) ; { int x, a[ ARRSIZE], key, size; randomize( ) ; printf( " \ninput list size( < 300 ) : " ) ; scanf( " % d", &size) ; printf( " Auto make a list: \n" ) ; for( x = 0; x < size; x + + ) { a[ x] = rand( ) % 1000; printf( " % d \ t", a[ x] ) ; printf( " \n sorting a list: \ n" ) ; insert_sort( a, size) ; for( x = 0; x < size; x + + ) printf( " % d \ t", a[ x] ) ; printf( " \n input Search key: " ) ; scanf( " % d", &key) ; x = BinarySearch( a, key, size) ; if ( x! = - 1) printf( " found value in element % d \n", x) ; else printf( " Value not found \n" ) ; int BinarySearch( int arr[ ], int key, int size)
{ int low = 0, high = size - 1, middle; int count = 0 ; while( low < = high) { middle = ( low + high) /2 ; count + + ; if ( key = = arr[ middle] ) { printf( " compare count = % d \ n", count) ; return middle; else if( key > arr[ middle] ) low = middle + 1 ; else high = middle - 1; printf( " compare count = % d \ n", count) ; return - 1; void insert_sort( int arr[ ], int size) { int i, j; int temp; for ( i = 1 ; i < size; i + + ) { temp = arr[ i] ; for ( j = i - 1; j > = 0; j - - ) if ( temp < arr[ j] ) arr[ j + 1 ] = arr[ j] ; else break; arr[ j + 1] = temp; BinarySearch( ) insert_sort ( ) 3 6. 14, ( size = 100 ), 100, key,,, count, 209
6. 1. 7 210,, C 2, N N,,,,,,, N ( N - 1 ) : [ ] [ ] ; : int a2[ 3 ] [ 4], matrix[ 10] [ 10] ; float class_grade[ 35] [ 19] ; 2 1, a2, matrix 10* 10, class_grade 19, n m,,,, N, :,,, C, a2 [ 3 ] [ 4] : a2 [ 0] [ 0] a2[ 0] [ 1 ] a2[ 0] [ 2] a2[ 0 ] [ 3 ] a2[ 1] [ 0] a2[ 1] [ 1] a2 [ 1] [ 2] a2[ 1] [ 3 ] a2[ 2] [ 0] a2[ 2 ] [ 1 ] a2[ 2] [ 2] a2[ 2] [ 3] 3 * 4 = 12 0,, 0 4, 1 2 4,, C a2[ 3] [ 4], 3, a[ 0], a[ 1], a[ 2] 3 3 4,, a [ 0 ] a [ 0] [ 0], a [ 0] [ 1 ], a [ 0] [ 2], a[ 0] [ 3] a [ 0 ], a[ 1 ], a[ 2], 3, : int a[ 5] [ 3] = { {80, 75, 92, {61, 65, 71, {59, 63, 70,
{85, 87, 90, {76, 77, 85 ; : int a[ 5] [ 3] = { 80, 75, 92, 61, 65, 71, 59, 63, 70, 85, 87, 90, 76, 77, 85 ;, 0 : int a[ 3] [ 3] = { {1, {0, 2, {1, 0, 3 ; : 1 0 0 0 2 0 1 0 3, : int a[ 3] [ 3] = {1, 2, 3, 4, 5, 6, 7, 8, 9 ; : int a[ ] [ 3 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9 ; 6. 16 a[ 3] [ 4] b[ 3] : a, b, a, b C : { int a[ ] [ 4] = {3, 16, 87, 65, 4, 32, 11, 108, 10, 25, 12, 27 ; int b[ 3], i, j, l; for( i = 0 ; i < = 2; i + + ) { l = a[ i] [ 0] ; for ( j = 1 ; j < = 3; j + + ) if ( a[ i] [ j] > l) l = a[ i] [ j] ; b[ i] = l; printf( " \narray a: \ n" ) ; for( i = 0 ; i < = 2; i + + ) { for ( j = 0 ; j < = 3; j + + ) printf( " % 5d", a[ i] [ j] ) ; printf( " \n" ) ; printf( " \narray b: \n" ) ; for( i = 0 ; i < = 2; i + + ) printf( " % 5d", b[ i] ) ; 211
for for, 0 l, l, l l l, l b [ i], b a for a b 6. 17 1 2 3 4 5 6 7 8 20042356 89 79 89 77 67 45 67 89 20044569 67 87 98 78 67 89 79 67 20040098 65 64 78 89 87 85 98 99 20040056 45 38 78 67 56 78 90 89 212 :, ; ( 1) : score [ 300] [ 9 ] / * 0, 1 8 * / n score m /* m * / row < - 0 while ( row < n) if ( score[ row] [ 0] = = m) break; if ( row > = n) return; s < - 0 col < - 1 while ( col < ) s < - s + score[ row] [ col] col < - col + 1 ( m) col < - 1 while ( col < ) s < - 0 row < - 0
while( row < ) s < - s + score[ row] [ col] row < - row + 1 s / n col < - col + 1 ( 2) C : #include < stdlib. h > #include < time. h > #define ARRrow 300 #define ARRcol 9 { int row, col, n, s ; long score[ ARRrow] [ ARRcol], m; randomize( ) ; printf( " \ninput score list size( < 300) : " ) ; scanf( " % d", &n) ; printf( " Auto make a score list: \ n" ) ; for ( row = 0; row < n; row + + ) { score[ row] [ 0] = 20040000L + rand( ) % 10000 ; printf( " % ld \ t", score[ row] [ 0] ) ; for ( col = 1 ; col < ARRcol; col + + ) { score[ row] [ col] = rand( ) % 101 ; printf( " % d \ t", score[ row] [ col] ) ; printf( " \n" ) ; printf( " \n input student no: " ) ; scanf ( " % ld", &m ) ; for ( row = 0; row < n; row + + ) if ( score[ row] [ 0] = = m) break; if ( row > = n) return; s = 0; for ( col = 1; col < ARRcol; col + + ) s + = score[ row] [ col] ; printf( " \n student no: % ld score list: \ n", m) ; 213
214 printf( " courses no: \ t" ) ; for ( col = 1; col < ARRcol; col + + ) printf( " % d \ t", col) ; printf( " * AVG* \ n \t \t" ) ; for ( col = 1; col < ARRcol; col + + ) printf( " % d \ t", score[ row] [ col] ) ; printf ( " % d", s / ( ARRcol - 1 ) ) ; printf( " \n all student average score list: \ n \ t \ t" ) ; for( col = 1; col < ARRcol; col + + ) { s = 0; for ( row = 0; row < n; row + + ) s + = score[ row] [ col] ; printf( " % d \ t", s /n) ;,, for, 1,, 8,,, 8, s s 0 s ( ARRcol - 1), 2, ( ),,,,, 6. 18 ( 3 4 ) { int max_value( int arr1 [ ] [ 4 ] ) ; int arr[ 3] [ 4] = {1, 3, 5, 7, 2, 4, 6, 8, 15, 17, 34, 12 ; printf( " Max value is: % d \ n", max_value( arr) ) ; int max_value( int arr1[ ] [ 4] ) { int i, j, max; max = arr1[ 0] [ 0 ] ; for ( i = 0 ; i < 3; i + + ) for ( j = 0; j < 4 ; j + + ) if ( arr1[ i] [ j] > max)
max = arr1[ i] [ j] ; return max;,, :, max,, max,, max, : 6. 19 #include < stdlib. h > #include < time. h > #define ROW 8 #define COL 9 { int max_value( int arr1 [ ] [ COL] ) ; int arr[ ROW] [ COL], row, col; randomize( ) ; for ( row = 0; row < ROW; row + + ) { for( col = 0 ; col < COL; col + + ) { arr[ row] [ col] = rand( ) % 10000 ; printf( " % d \ t", arr[ row] [ col] ) ; printf( " \n" ) ; printf( " \nmax value is: % d \ n", max_value( arr) ) ; int max_value( int arr1[ ] [ COL] ) { int max, row, col; max = arr1[ 0] [ 0 ] ; for ( row = 0; row < ROW; row + + ) for ( col = 0; col < COL; col + + ) if ( arr1[ row] [ col] > max) max = arr1[ row] [ col] ; return max;,,,, ( 215
216 ), C 6. 20 N* M M* N #define M 2 #define N 3 void transpose( int a1[ ] [ N], int a2[ ] [ M] ) ; { int arr1[ M] [ N] = { {2, 4, 6, {1, 3, 5, arr2[ N] [ M], i, j; printf( " Array arr1: \ n" ) ; for ( i = 0 ; i < M; i + + ) { for ( j = 0 ; j < N; j + + ) printf( " % 5d", arr1[ i] [ j] ) ; printf( " \n" ) ; transpose( arr1, arr2) ; printf( " Array arr2: \ n" ) ; for ( i = 0 ; i < N; i + + ) { for ( j = 0 ; j < M; j + + ) printf( " % 5d", arr2[ i] [ j] ) ; printf( " \n" ) ; void transpose( int a1[ ] [ N], int a2[ ] [ M] ) { int i, j; for ( i = 0 ; i < M; i + + ) for ( j = 0; j < N; j + + ) a2 [ j] [ i] = a1 [ i] [ j] ; 6. 21 3 : 8 1 6 3 5 7 4 9 2 n ( 1) : : 1 1 ; 2 n* n : 1 1;
1, n; n, 1; ( ), 1 n, #define N 16 { int plot[ N] [ N] ; int i, j, k, p, n; p = 1; while( p = = 1 ) { printf( " \ninput a odd rank of magic matrix < 16 : " ) ; scanf( " % d", &n) ; if ( ( n > 1&&n < = 15) &&( n% 2! = 0) ) { printf( " The rank of magic matrix is % d \n", n) ; p = 0; for ( i = 1 ; i < = n; i + + ) for ( j = 1; j < = n; j + + ) plot[ i] [ j] = 0; i = 1; j = n /2 + 1 ; plot[ i] [ j] = 1; for ( k = 2; k < = n* n; k + + ) { i = i - 1; j = j + 1; if ( i < 1&&j > n) { i + = 2; j = j - 1; plot[ i] [ j] = k; continue; if ( i < 1) i = n; if ( j > n) j = 1; while( plot[ i] [ j]! = 0) { i = i + 2; 217
218 j = j - 1; if ( j < 1) j = n; if ( i > n) i = 1; plot[ i] [ j] = k; for ( i = 1 ; i < = n; i + + ) { for ( j = 1 ; j < = n; j + + ) printf( " % d \ t", plot[ i] [ j] ) ; printf( " \n" ) ;,, 16, 1, plot ( 1), 1 1 k ( 2) ( 4) 2, 1 n,,, continue k 3 4, 0 6. 1. 8 1 ), (, ), : int arr[ 3] [ 4] ; : arr[ 0 ] [ 4] arr[ 1 ] [ 4] arr[ 2] [ 4 ] 3, 4 : arr[ 0], arr[ 1], arr[ 2] arr arr[ 0 ] arr[ 0] [ 0] arr[ 0] [ 1] arr[ 0] [ 2] arr[ 0] [ 3] arr[ 1 ] arr[ 1] [ 0] arr[ 1] [ 1] arr[ 1] [ 2] arr[ 1] [ 3] arr[ 2 ] arr[ 2] [ 0] arr[ 2] [ 1] arr[ 2] [ 3] arr[ 2] [ 3]
, arr, 0, arr[ 0] : arr = arr[ 0] = &arr[ 0] [ 0] + i i arr + i arr i, n, n, arr[ i] + j i j, &arr[ i] [ j] arr[ i] * ( arr + i),, * ( arr + i) + j &a[ i] [ j] : arr[ i] + j = * ( arr + i) + j = &arr[ i] [ j], * ( * ( arr + i) + j) * ( arr[ i] + j) arr [ i] [ j] : arr + i *,, * ( arr + i) + j arr + i j arr[ 0 ] &,, &arr[ 0 ], arr &arr[ i] arr[ i], &arr[ i] + 1 arr[ i] + 1,,,,, 2 ),,, 6. 22 ( ) { int add( int * arr, int n) ; int i, j, arr[ 4] [ 3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, * ptr; ptr = arr[ 0] ; for ( i = 0 ; i < 4 ; i + + ) { for ( j = 0 ; j < 3; j + + ) printf( " % 4d", * ptr + + ) ; printf( " \n " ) ; ptr = arr[ 0] ; printf( " The array all elements sum = % d \ n", add( ptr, 12) ) ; int add( int * arr, int n) { int i, sum = 0; for ( i = 0 ; i < n; i + +, arr + + ) sum + = * arr; return sum; 219
220 3 ) m, C, m : ( * ) [ ] ; : int ( * ptr) [ 10 ] ; 10 ptr n n ptr + 1 10, 11,, m : m,, : int ( * ptr) [ 10], * p; ptr, ptr = ptr + 2; ptr 21,, : printf( % d, * ptr) ; ptr p, printf ( % d, * p) ; 6. 23 m { int a[ 3] [ 4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ; int ( * p) [ 4] ; int i, j; p = a; for ( i = 0 ; i < 3; i + + ) { for ( j = 0 ; j < 4; j + + ) printf( " % 5d ", * ( * ( p + i) + j) ) ; printf( " \n" ) ;, p 4 3 4, p 0, j 4, * ( * ( p + i) + j) 0 i, * ( p + i) i 6. 24 6 6,, 2, : Auto make a 6* 6 matrix: 3 78 0 79 76 41 55 84 46 39 22 21
7 88 86 29 95 78 78 26 39 81 63 47 53 6 45 62 27 16 27 57 80 41 87 9 output 6 6 matrix in 3 col swap result: 79 78 0 55 76 41 39 84 46 7 22 21 29 88 86 78 95 78 81 26 39 53 63 47 62 6 45 27 27 16 41 57 80 3 87 9, 3 79, 3 55, 3 39 3 3, 3 ( 1) 6 6, x, y, 2 p0, p3, 1 3 p; 6 6 ; p 2 * ARRSIZE - 1; p = ( int ( * ) [ 3 ] a;, p0, p3 p3 p + + ; x p, y ;, x, y #include < stdlib. h > #include < time. h > #define ARRSIZE 6 { int x, y, a[ ARRSIZE] [ ARRSIZE], * p0, * p3, ( * p) [ 3] ; randomize( ) ; printf( " Auto make a 6* 6 matrix: \ n" ) ; for( x = 0; x < ARRSIZE; x + + ) { for ( y = 0; y < ARRSIZE; y + + ) { a[ x] [ y] = rand( ) % 100 ; printf( " % d \ t", a[ x] [ y] ) ; printf( " \n" ) ; printf( " \noutput 6* 6 matrix in 3 col swap result: \ n" ) ; 221
222 p = ( int ( * ) [ 3 ] a; for ( x = 1; x < ARRSIZE* 2; x + + ) { p0 = ( int * ) p; p + + ; p3 = ( int * ) p; y = * p0; * p0 = * p3; * p3 = y; p0 = p3; for ( x = 0; x < ARRSIZE; x + + ) { for ( y = 0; y < ARRSIZE; y + + ) printf ( " % d \ t", a[ x] [ y] ) ; printf( " \ n" ) ; 6. 2 6. 2. 1, C NULL( \ 0 ), ASCII C : The C program :, \ n, 123. 67 C, : T h e C p r o g r a m \0 1 2 3. 6 7 \0
\ n \0 \0 : \0, \0 C,, : char s1[ 100] ; s1,,, : char s1[ 100] = { The C Programm ; cahr s2[ 100] = The C Programm ; char s3[ ] = The C Programm ;,,, : char * ps = s1; s1 ps s1, * ps s1 223 T h e C p r o g r a m \0 ps,,, 6. 2. 2 C : ( ) ; scanf( % s, ) ; gets( ),, & scanf( ), gets( ), : char string[ 100 ] ;
224 scanf( % s, string) ; gets( string) ; : printf( % s, ) ; puts( ) ; : printf( % s, string) ; puts( string) ; string 99 ( \0 ) 2 : char * ps; for( ps = string; * ps ; ps + = 2) putchar( * ps) ;, printf( ) puts( ) : char * ps, n = 10; ps = " this is a book" ; printf( " % s \ n", ps + n) ; puts( ps + 8 ) ; : book a book ps, ps, ps = ps + 10, ps b, NULL : " book" 6. 25 ( ) ( 1) :, \0, 1 ( 2) : { char str[ 100 ], * p, sum = 0 ; p = str ; gets( p) ; while( * p! { sum + + ; 6. 26 p + + ; putchar( \ n ) ; = \0 ) printf( " \n The string length = % d", sum) ; : int locateofchar( char * ps, char ch) ( 1) : 1 count,,, 0 ps \0,
( 2) : int locateofchar( char * ps, char ch) ; { char str[ 100 ], ch; printf( " \ninput a string: " ) ; gets( str) ; printf( " \n input a character: " ) ; ch = getchar( ) ; printf( " \nthe locate of \ % c \ in \" % s\" is % d", ch, str, locateofchar( str, ch) ) ; int locateofchar( char * ps, char ch) { int count = 1 ; while( * ps) { if ( * ps = = ch) return count; ps + + ; count + + ; return 0; gets( ), getchar( ),, \ \ 6. 27 ( ) ( 1) : str1, str2 str1, str1, ( \ 0 ), str2 str1, str2 ( \0 ) ( 2) : { char str1[ 80 ], str2[ 40], * p1 = str1, * p2 = str2; printf( " \ninput first string: " ) ; gets( p1) ; printf( " \ninput second string: " ) ; gets( p2) ; while( * p1 + + ) ; 225
p1 - - ; while( ( * p1 + + = * p2 + + )! ; printf( " \nthe string connect: " ) ; puts( str1) ; = \0 ),, p1 str1, * p1 NULL,,, p1 1, NULL, 1, p1 str1 ( NULL), * p1 = * p2; str2 str1, p1 + +, p2 + + ; p2 str2, NULL( ) 2, puts( p1) ; str1? 6. 2. 3 226 C,, 9, string. h, #include < string. h > ( 1) (, NULL) unsigned strlen( char * s) ( 2) char * strcat( char * s1, char * s2) s2 s1, s1 ( 3) char * strcpy( char * s1, char * s2) s2 s1, s1, \0, \0 C, str1 = str2; ( 4) int strcmp( char * str1, char * str2 ), : R < 0 R = 0 R > 0 str1 str2 str1 str2 str1 str2 2, \0
\ 0, C 2 : str1 = = str2 ( 5) str1 > str2 char * strchr( char * s, int ch) ch ( ) ( 6) char * strncat( char * s1, char * s2, int n ) s2 n s1 ( 7) int strncmp( char * s1, char * s2, int n) s1 s2 n 0 ( 8) char * strncpy( char * s1, char * s2, int n) s2 n s1, s1 ( 9) char * strstr( char * s1, char * s2) s2 s1, NULL 6. 28 2, #include < string. h > { char str1[ 40], str2[ 40] ; int n; 6. 29 printf( " \ ninput str1 : " ) ; gets( str1) ; printf( " Input str2: " ) ; gets( str2) ; n = strcmp( str1, str2) ; if ( n > 0) printf( " str1 > str2" ) ; else if ( n < 0) else printf ( " str1 < str2" ) ; printf( " str1 = str2" ) ;, strcat 227
228 ( 1) : 1 \0, 2 1 ( 2) : #include < string. h > { void mystrcat( char str1[ ], char str2[ ] ) ; char s1[ 80], s2 [ 40] ; printf( " Input strint s1: " ) ; gets( s1 ) ; printf( " Input strint s2: " ) ; gets( s2 ) ; mystrcat( s1, s2) ; puts( s1) ; void mystrcat( char str1[ ], char str2[ ] ) { int i; 6. 30 i = strlen( str1) ; strcpy( str1 + i, str2 ) ; 1, 2 ( 2 1 ), 1 abcdef, 2 d12345, abcd12345def ( 1) : 1 2 ; 1 ; 2 1 1 ; \0 ( 2) : #include < string. h > { void insert_string( char s1[ ], char s2[ ] ) ; char str1[ 80 ], str2[ 40] ; printf( " Input strint str1: " ) ; gets( str1) ; printf( " Input strint str2: " ) ; gets( str2) ;
insert_string( str1, str2) ; puts( str1) ; void insert_string( char s1[ ], char s2[ ] ) { char temp[ 80] ; int i; for ( i = 0 ; s1[ i]! = s2[ 0] ; i + + ) ; strcpy( temp, &s1 [ i] ) ; strcpy( &s1[ i], s2) ; strcpy( &s1[ i] + strlen( s2), temp) ; 6. 31 s1 s2 ( 1) :,,, ; s2 ; s1 s2 ; s2 s1 s2 ( 2) : delete_string( char * s1, char * s2 ) len < - s2 k < - 0 while( s1 ) if ( s1 k s2 ) s1 k s2 k < - k - 1 k < - k + 1 s1 ( 3) : #include < string. h > char * delete_string( char * s1, char * s2 ) { int k, len = strlen( s2 ) ; for ( k = 0 ; * ( s1 + k) ; k + + ) if ( strncmp( s1 + k, s2, len) = = 0) { strcpy( s1 + k, s1 + k + len) ; k - - ; return s1 ; 229
230 { char s1[ 80], s2[ 10] ; : 6. 32 gets( s1) ; gets( s2) ; puts( delete_string( s1, s2) ) ; 11 the People s Republic of China Japan the Republic of India the Republic of Korea Malaysia the Islamic Republic of Pakistan the Kingdom of Thailand the Republic of Singapore the Republic of the Philippines the Socialist Republic of Vietnam the Union of Myanmar ( 1) :,,, ( 2) : #include < string. h > { char temp[ 80], cs[ 20] [ 80] ; int i, j, p; printf( " input country s name: \ n" ) ; for( i = 0 ; i < 8; i + + ) gets( cs[ i] ) ; printf( " \n" ) ; for( i = 0 ; i < 8; i + + ) { p = i; strcpy( temp, cs[ i] ) ; for ( j = i + 1; j < 8; j + + ) if ( strcmp( cs[ j], temp) < 0 ) { p = j; strcpy( temp, cs[ j] ) ;
if ( p! = i) { strcpy( temp, cs[ i] ) ; strcpy( cs[ i], cs[ p] ) ; strcpy( cs[ p], temp) ; puts( cs[ i] ) ; printf( " \n" ) ; 1 for, gets 8, ( 20 ) gets cs[ i] cs [ i] temp, i p, temp cs[ i], temp temp, p p i cs[ i], cs[ i] temp cs i, 6. 33 ASCII, : ABCDeFGH, : eabcdfgh ( 1) p; max, q, i; * ( p + i) NULL,, ; i + +, * ( p + i) > max, max, max, q; ; q > p,, ; * q = * ( q - 1) ; q q, q - - ; ;, 231 ( 2) : void fun ( char * p) { char max, * q; int i = 0; max = * p; while ( * ( p + i) )
232 { i + + ; if ( max < * ( p + i) ) { max = * ( p + i) ; q = p + i; while ( p < q) { * q = * ( q - 1) ; q - - ; * p = max; { char str[ 80] ; 6. 34 printf ( " Enter a string: " ) ; gets( str) ; fun( str) ; printf( " \n The last string of changed \ n" ) ; puts( str) ;, printf, printf PF, { int a[ 3] [ 4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, k; char * PF, * PF2; PF = " % d, % d, % d, % d \ n" ; PF2 = % d - % d - % d \ n ; for ( k = 0; k < 3 ; k + + ) printf( PF, a[ k] [ 0 ], a[ k] [ 1], a[ k] [ 2], a[ k] [ 3] ) ; for ( k = 0; k < 4 ; k + + ) printf( PF2, a[ 0] [ k], a[ 1 ] [ k], a[ 2 ] [ k] ) ; 6. 2. 4,
: * [ ] ; : int * pa[ 3 ] pa 3,, : int a[ 3] [ 3] = {1, 2, 3, 4, 5, 6, 7, 8, 9 ; int * pa[ 3 ] = {a[ 0 ], a[ 1], a[ 2] ; pa a a[ 0] a[ 0] [ 0] a[ 0] [ 1] a[ 0] [ 2] a[ 1] a[ 1] [ 0] a[ 1] [ 1] a[ 1] [ 2] a[ 2] a[ 2] [ 0] a[ 2] [ 1] a[ 2] [ 2] int * p[ 4] ; m int ( * p) [ 4 ],, ( ),, 3,, C month[ ], 12 : char * month[ ] = { January, February, March, 6. 35 April, May, June, July, August, September, October, November, December ;, 1 7, { int i; char * day_name( int n) ; printf( " input Day No: \n" ) ; scanf( " % d", &i) ; printf( " Day No: % 2d - - > % s \ n", i, day_name( i) ) ; char * day_name( int n) { char * name[ ] = { " Illegal day", " Monday", " Tuesday", " Wednesday", 233
234 " Thursday", " Friday", " Saturday", " Sunday" ; return ( ( n < 1 n > 7 ) name[ 0] : name[ n] ) ;, name [ ], 8, name[ 0] " Illegal day", Day_name, n 1 7 " Illegal day", % s 6. 36 #include < string. h > #define N 5 { void sort( char * arr1[ ], int n) ; char * string[ N] = {" korea", " japan", " malaysia", " india", " pakistan" ; int i; sort ( string, N) ; for ( i = 0 ; i < N; i + + ) puts( string[ i] ) ; void sort( char * arr1[ ], int n) { int i, j, k; char * temp; for ( i = 0 ; i < n - 1; i + + ) { k = i; for ( j = i + 1; j < n; j + + ) if ( strcmp( arr1[ j], arr1[ k] ) > 0) k = j; if ( k! = i) temp = arr1[ i], arr1[ i] = arr1[ k], arr1[ k] = temp;,,, : * * ;, : * * * ;,,
C,,,,, : int * * pp; pp,, 6. 37 { int x, * p, * * pp; x = 10; p = &x; pp = &p; printf( " \nx = % d * * pp = % d \ n", x, * * pp) ;, p, x; pp, p pp x * * pp x 10 [ 6. 38] { char * string[ ] = { " basic", " fortran", " pascal", " lisp", * * ptr; int i; ptr = string; for ( i = 0 ; i < 4; i + + ) puts( * ( ptr + i) ) ; 6. 39 5, (, - 1 ) { int stu1 [ ] = { 78, 98, 73, - 1, stu2[ ] = {100, 98, - 1, stu3[ ] = {88, - 1, stu4 [ ] = { 100, 78, 33, 65, - 1, stu5[ ] = {99, 88, - 1 ; int * grad[ 5], * * ptr, i; grad[ 0] = stu1 ; grad[ 1] = stu2 ; grad[ 2] = stu3 ; grad[ 3] = stu4 ; grad[ 4] = stu5 ; for( ptr = grad, i = 1 ; i < = 5; ptr + +, i + + ) { printf ( " scores of student % d: ", i) ; 235
while( * * ptr > = 0) { printf ( " % 5d", * * ptr) ; ( * ptr) + + ; printf( " \n" ) ; 6. 2. 5 236, C,, 2,, argc ; argv,, Windows, command, Regedit /1, " /1 " /1 C, TC + + 3. 0, Arguments,,,, C mycopy. exe, : void main( int argc, char * argv[ ] ), : mycopy file1. txt file2. txt : argc = 3, argv[ 0 ] mycopy, argv[ 1]
file1. txt, argv[ 2 ] file2. txt 6. 40 void main( int argc, char * argv[ ] ) { while( - - argc > 0 ) : printf( " % s% c", * + + argv, ( argc > 1 : \ n ) ) ;, TC + + 3. 0 Run Arguments Windows : 237, s6-40. exe,, argc, argc 1 argv,, + + argv argv[ 1], 2,, : mychar. cpp mychar. exe TC + + 3. 0, RUN Arguments, : Computer and C Language, : Computer and C Language Windows : mychar Computer and C Language 6. 41 n, n #include < stdlib. h > #include < math. h > void main( int argc, char * argv[ ] )
{ int k = 0, n; n = atoi( * + + argv) ; printf( " \nk \ tk* k \tsqrt( k) \ n" ) ; while( k + + < n) printf( " % d \t% d \ t% f \n", k, k* k, sqrt( k) ) ; Run Arguments : 238, 1 ( 0 ) atoi( ), sqrt( ), k sqrt( ), % f 6. 42 1, ( 1) : 1,, ;, NULL ( 2) : #include < string. h > void main( int argc, char * argv[ ] ) { char * search( char * str1, char * str2) ; char * ptr; if ( argc < 3) printf( " error!! Lose arguments \n" ) ; ptr = search( argv[ 1 ], argv[ 2] ) ; if ( ptr! = NULL) { strcpy( ptr, ptr + strlen( argv[ 2] ) ) ; else puts( argv[ 1] ) ; printf( " sub_string % s is not in main_string % s! \n", argv[ 2], argv[ 1] ) ;
char * search( char * str1, char * str2) { int len = strlen( str2) ; while ( ( str1 = strchr( str1, * str2) )! = NULL) if ( strncmp( str1, str2, len) = = 0) break; else str1 + + ; return str1; Run Arguments : 239, strncmp( str1, str2, n) 2 n, 0, 0 strchr( str, ch) str ch, ch, NULL 1, : 1,,,,, NULL search : char * search( char * str1, char * str2) { int len = strlen( str2) ; for ( ; strncmp( str1, str2, len)! = 0&&* ( str1 + len)! = \0 ; str1 + + ) ; return( * ( str1 + len)! = \0 str1: NULL) ;
6. 3 6. 3. 1 240,,, ; ; ;, C,,, :,,,, : struct { ; ;,,, : struct student { char num[ 8] ; ; char name[ 20] ; char sex; int age; float score;, student, 5, 1, num, ; 2, name, ; 3, sex,, M F ; 4, age, ; 5, score,,,,
: struct student stu1, stu2 ; 2 : stu1, stu2, 2 student,, stu1 stu2 struct student num Name sex age score 6. 3. 2, 4 : 1 ) 3 ( 1) ( ) struct student { char num[ 8] ; char name[ 20] ; char sex; int age; float score; ; struct student stu1, stu2 ; ( 2) struct student { char num[ 8] ; char name[ 20] ; char sex; int age; float score; stu1, stu2; ( 3), struct { char num[ 8] ; 241
242 char name[ 20] ; char sex; int age; float score; stu1, stu2; 2 ), struct date { int month; int day; int year; struct student1 { char num[ 8] ; char name[ 20] ; char sex; int age; struct date birthday; float score; boy1, boy2; date, month( ) day( ) year( ) 3 boy1 boy2, birthday date, 3 ), ANSI C,, :.,. : boy1. num, boy2. sex 2 : boy1. birthday. month C scanf,, :
gets, ;, atoi atol atof : atoi( char * nptr), ; atol( char * nptr), ; atof( char * nptr), ( double) 3 stdlib. h, 6. 43 2 #include < stdlib. h > struct student { char num[ 8] ; ; char name[ 20] ; char sex; int age; float score; struct student stu1, stu2 ; { char temp[ 20] ; printf( " first one input: num, name, sex, age, score \ n" ) ; gets( stu1. num) ; gets( stu1. name) ; stu1. sex = getchar( ) ; getchar( ) ; gets( temp) ; stu1. age = atoi( temp) ; gets( temp) ; stu1. score = atof( temp) ; stu2 = stu1 ; printf( " Second input the num and name: \ n" ) ; gets( stu2. num) ; gets( stu2. name) ; printf( " first student information: \ n" ) ; printf( " % s % s % c % d % f\ n", stu1. num, stu1. name, stu1. sex, stu1. age, stu1. score) ; printf( " second student information: \ n" ) ; printf( " % s % s % c % d % f\ n", stu2. num, stu2. name, stu2. sex, stu2. age, stu2. score) ; 243
student, 2 stu1 stu2, age, score, gets( ), getchar( ), getchar( ), 2,, stu1 stu2, stu2 num, name 4 ),, : struct student s1 = { 20049988, LiBin, M, 19, 588. 6 ; : num name sex age score 244 20049988 LiBin M 19 588. 6 : ; 6. 44 struct student { char num[ 8] ; char name[ 20] ; char sex; int age; float score; boy2, boy1 = { 051818, " ZhangPing", M, 19, 646. 5 ; { boy2 = boy1 ; boy2. score = 688. 6; printf( " Number = % s \nname = % s \ n", boy2. num, boy2. name) ; printf( " Sex = % c \nage = % d \nscore = % f\n", boy2. sex, boy2. age, boy2. score) ;
6. 3. 3 typedef C typedef ( ),, : typedef ; : typedef int INTEGER; ( int) INTE- GER, int j, k; INTEGER j, k;, : typedef struct student { char num[ 8] ; char name[ 20] ; char sex; int age; float score; STU; struct student { char num[ 8] ; char name[ 20] ; char sex; int age; float score; ; typedef struct student STU; struct student STU, STU stu2, stu2, stu3 ; struct student stu1, stu2, stu3; typedef : typedef char STRING [ 80] ; STRING 80, STRING str; char str [ 80] ; :, : ( 1) Typedef char STRING[ 100] ; /* STRING 100 * / STRING c1, c2; char c1[ 100 ], c2[ 100] ; ( 2) typedef int * PNUM; /* PNUM * / 245
PNUM pn, pm; int * pn, * pm ; ( 3) typedef struct strname{ ST; / * ST * / ST a[ 10 ] ; struct strname { a[ 10] ; 6. 3. 4 246,,,,, : typedef struct student { char num[ 8] ; STU; char name[ 20] ; char sex; int age; float score; STU mclass[ 35] ; mclass, 35, mclass[ 0] mclass[ 34 ] struct student mclass 35, : STU mclass[ 35 ] = { { 20030901, " Li ping", " M", 445, { 20030902, " Zhang ping", " M", 622. 5, { 20030903, " He fang", " F", 592. 5, { 20030919, " Cheng ling", " F", 587, { 20030923, " Wang ming", " M", 558 ;,,, : [ ]. : mclass[ 2]. name mclass[ 2]. count 6. 45 ( 1)
, ; count < - 0, s < - 0 i < - 0 while( i < 5) s < - s + mclass[ i]. score / * * / count < - count + 1 / * * / i < - i + 1 s /coung, count ( 2) : typedef struct student { char num[ 9] ; STU; char name[ 20] ; char sex; int age; float score; STU mclass[ 35 ] = { { " 20030901", " Li ping", M, 19, 545, { " 20030902", " Zhang ping", M, 16, 642. 5, { " 20030903", " He fang", F, 17, 592. 5, { " 20030919", " Cheng ling", F, 18, 587. 4, { " 20030923", " Wang ming", M, 17, 558. 6 ; { int i, count = 0; float s = 0; for( i = 0 ; i < 5; i + + ) { s + = mclass[ i]. score; count + + ; printf( " % 10 s% 20s \ t% c \ t% d \t% f\ n", mclass[ i]. num, mclass[ i]. name, mclass[ i]. sex, mclass[ i]. age, mclass[ i]. score) ; printf( " average = % f \ncount = % d \n", s / count, count) ; mclass, 35, 5 247
: 8, 9, NULL main for 5 score s, count 1,,, 6. 3. 5 248 4 : ( 1), : struct ( ) { return ; ( 2) ( ),, ( 3),, : ( struct ) { : ( ) ; ( 4) ( ), ( ) ( ) 6. 46, #include < stdlib. h > #define N 6 typedef struct stud_type { char name[ 20] ; int age; float score;
STU; { STU input( int n) ; void print( STU x, int n) ; STU stu[ N] ; int i; for ( i = 0 ; i < N; i + + ) stu[ i] = input( i) ; for ( i = 0 ; i < N; i + + ) print( stu[ i], i) ; STU input( int n) { STU x; char in_buf[ 20] ; printf( " \ninput all data of stu[ % d] : \ n", n) ; gets( x. name) ; gets( in_buf) ; x. age = atoi( in_buf) ; gets( in_buf) ; x. score = atof( in_buf) ; return( x) ; void print( STU x, int n) { printf( " Student% d: % s, % d, % 6. 2 f\ n", n, x. name, x. age, x. score) ; 249 6. 3. 6 1 ), : struct * ; : typedef struct stud_type { char name[ 20] ; int age; float score; STU; STU * pt, stu1;
pt, struct stud_type pt,, stu1 stu1 pt, : pt = &stu1; stu1. name stu1. age stu1. score 250 pt : ( * ). ; : ( * pt). name ( * pt). score : - > ; : pt - > name; pt - > score - > : pt 6. 47 #include < stdlib. h > struct date { int day; ; int month; int year; struct stutype { char name[ 20] ; ; struct date birthday; { struct stutype * ps, stu1; char temp[ 20] ; ps = &stu1; printf( " Input name, year, month, day of birthday: \ n" ) ; gets( ps - > name) ; gets( temp) ; ps - > birthday. year = atoi( temp) ; gets( temp) ; ps - > birthday. month = atoi( temp) ;
gets( temp) ; ps - > birthday. day = atoi( temp) ; printf ( " \noutput name,, year, month, day of birthday \n" ) ; printf ( " % s % d / % d / % d \ n ", ps - > name, ps - > birthday. year, ps - > birthday. month, ps - > birthday. day) ; stu1, ps stu1, temp[ 20 ], atoi( ),, : stu_1. name ( * ps). name ps - > name ( ) 2 ) ( ),,,,,,, 6. 48, 588 #include < stdlib. h > #define N 40 typedef struct stud_type { char name[ 20] ; STU; int age; float score; { STU input( int n) ; void listofstu( STU * ps, int count) ; STU stu[ N], * ptr; int k, x; printf( " input the person sum: " ) ; scanf( " % d", &x) ; for ( k = 0; k < x; k + + ) stu[ k] = input( k) ; listofstu( stu, x) ; / / ptr = &stu[ 0 ] ; listofstu( ptr, x) ; 251
252 STU input( int n) { STU sx; char in_buf[ 20] ; printf ( " \ninput name, age, score of stu[ % d] : \ n", n) ; gets( sx. name) ; gets( in_buf) ; sx. age = atoi( in_buf) ; gets( in_buf) ; sx. score = atof( in_buf) ; return sx; void listofstu( STU * ps, int count) { int k, sum = 0; for ( k = 0; k < count; k + + ) { if( ps - > score > 588) { sum + + ; printf( " Student: % s, % d, % f \ n ", ps - > name, ps - > age, ps - > score) ; ps + + ; printf( " the person sum = % d", sum) ; 6. 4 6. 4. 1, C, : union {
; : union perdata { int mclass; char dept[ 20] ; ; perdata, mclass dept, perdata, 22, 20 perdata mclass MC MC dept,,,,,,, perdata dept 20, mclass MC 2 6. 52 mm, nn union data { int a; float b; double c; char d; mm ; struct stud { int a; float b; double c; char d; nn ; void main ( ) { printf( " % d, % d", sizeof( struct stud), sizeof( union data) ) ; : 1 5, 8 253
6. 4. 2 254 2 ( 1), union perdata { int mclass; char dept[ 20 ] ; un_1, un_2 ; ( 2) typedef { int mclass; union perdata char dept[ 20] ; UNPERDATA; UNPERDATA un_1, un_2 ; :. : un_1. mclass un_1. dept ( ) : un_1 = un_2 gets( un_1),,, 6. 53 union perdata { int mclass; char dept[ 20] ; un_1, un_2 ; { un_1. mclass = 65 ; printf( mclass \ t dept ) ; printf( " % d \t", un_1. mclass) ; printf( " % s", un_1. dept) ; gets( un_1. dept) ; printf( " % d \t", un_1. mclass) ; printf( " % s \ n", un_1. dept) ;
soft, : mclass 65 A dept 28531 soft mclass 65, ASCII A, dept, dept mclass, A, dept soft, mclass ( 16 ) 28531 6. 54, 3 3 4, / / S 1405 S 1407 T T 255 struct { char name[ 20] ; char courses[ 20] ; char flag; union { int mclass; depa; body[ 4 ] ; { int i; char dept[ 20] ; char flag; for( i = 0 ; i < 4; i + + ) { printf( " input name, courses, flag, class or dept \ n" ) ; gets ( body[ i]. name) ; gets ( body[ i]. courses) ; body [ i]. flag = getchar( ) ; if ( body[ i]. flag = = s ) else scanf( " % d", &body[ i]. depa. mclass) ;
256 getchar( ) ; gets( body[ i]. depa. dept) ; printf( " output techeres and students report \n" ) ; for( i = 0 ; i < 4; i + + ) { if ( body[ i]. flag = = s ) else printf ( " % s \ t% s \ t% d \n", body[ i]. name, body[ i]. courses, body[ i]. depa. mclass) ; printf ( " % s \ t% s \ t% s \ n", body[ i]. name, body[ i]. courses, body[ i]. depa. dept) ; body, 4 depa, 2, mclass, ; dept,, flag,, s, 1 for,, 3 name, courses flag, flag, " s" depa. mclass ( ), depa. dept ( ) 2 for, flag, 2, :,,,,,,,, union un { float f; int b; char d; u = {1. 8, 45, G ; ( 4), 6. 55 union keybord { unsigned char ch[ 2] ;
key; unsigned int i; ch[ 1] ch[ 0] key, ch[ 0] 8, ch[ 1] 8, :, 16, 8 0, ASCII ; 8 0, 8, : 0x48 0x50 0x46 d( IBM PC ), bioskey, bios. h, bios. h, bioskey 2, : 0, ; 1,, 0 #include < bios. h > #include < stdlib. h > union keybord { unsigned char ch[ 2] ; unsigned int i; key; { for( ; ; ) { printf( " \npress ESC key for exit to OS! \ n" ) ; while(! bioskey( 1) ) ; key. i = bioskey( 0) ; if ( key. ch[ 0]! = 0) { if ( key. ch[ 0] = = 0x1 b) else else exit( 0 ) ; printf( " \ nyou pressed the % c key! \n", key. ch[ 0] ) ; 257
switch( key. ch[ 1 ] ) { case 0x48: printf( " \nyou pressed the % c key! \n", 24) ; case 0x50: case 0x46: case 0x4d: break; printf( " \nyou pressed the % c key! \n", 25) ; break; printf( " \nyou pressed the % c key! \n", 27) ; break; printf( " \ nyou pressed the % c key! \ n", 26) ; break; 6. 5 258 6. 5. 1,, male female2 ; 7 Mon, Tues, Wed, Thurs, Fri, Sat, Sun ; 1 12,,, C,,,, : enum { ;, : enum weekday{ sun, mon, tue, wed, thu, fri, sat ; weekday, 7, 1 7 weekday 7
6. 5. 2 a, b, c;, 3 2 ( 1), typedef enum weekday{ sun, mon, tue, wed, thu, fri, sat WEEKTYPE; WEEKTYPE ( 2), enum weekday{ sun, mon, tue, wed, thu, fri, sat a, b, c; a, b, c, : a = mon; c = sun;, : a = a + 1; c + + ; :,, enum weekday : sun = 5; mon = 2 ; weekday. sun = mon;, 0 0, 1, 2 enum weekday, sun 0, mon 1,, sat 6 : enum weekday{ sun = 7, mon = 1, tud, wed, thu, fri, sat workday; sun 7, mon 1, tud sat 2 6, : a = sum; b = mon; ; a = 0; b = 1;, : a = ( enum weekday) 2; 2 a, : a = tue;,, enum year{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ; enum year { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec ; 6. 56 { enum color{ RED, YELLOW, GREENmcolor; 259
260 printf( " ( % d) RED, ( % d) YELLOW, ( % d) GREEN \n", RED, YELLOW, GREEN) ; printf( " Input color: " ) ; scanf( " % d", &mcolor) ; switch( mcolor) { case RED: printf( " Red color \n" ) ; break; case YELLOW: printf( " Yellow color \n" ) ; break; case GREEN: printf( " Green color \ n" ) ; break; default: printf( " No color color \ n" ) ; break;,, switch - case, 6. 57 bush, jack, rose, kenya, 1 15 { enum body{ bush, jack, rose, kenya month[ 26], j; int i; j = bush; for ( i = 1 ; i < = 15; i + + ) { month[ i] = j; j + + ; if ( j > kenya) j = bush; for( i = 1 ; i < = 15; i + + ) switch( month[ i] ) { case bush: printf( " % 2d % s \ n", i, " bush" ) ; break; case jack: printf( " % 2d % s \ n", i, " jack" ) ; break; case rose: printf( " % 2d % s \ n", i, " rose" ) ; break; case kenya: printf( " % 2d % s \ n", i, " kenya" ) ; break;
6 6. 1 SIZE, n( 3 ), n, n 6. 2,, 6. 3 : 1 2 2 2 4 5 4 5 4 1 5 : 1, 3, 0, 3, 2 6. 4 100,, 1, 6. 5 n, :,, n - 2 6. 6 5 000, 1 6, 6, ( 60 ), 111111111111111111111111111111111111111111111111111111111111 858 17. 2% 222222222222222222222222222222222222222222222222222222222222 858 17. 2% 33333333333333333333333333333333333333333333333333333333 809 16. 2% 444444444444444444444444444444444444444444444444444444444 819 16. 4% 555555555555555555555555555555555555555555555555555555555 828 16. 6% 666666666666666666666666666666666666666666666666666666666 828 16. 6% 6. 7, : 123454321 6. 8 ( ) 6. 9, ( ) 6. 10,,,,,, - 1 6. 11 2, 6. 12 9 9, 10, 9 9 ( 9, 0 ) ( 8 ) 6. 13 1 1 1 1 1 1 1 1 1 1 1 2 4 7 11 1 2 3 4 5 1 0 0 0 0 1 2 2 2 2 1 2 2 2 1 3 5 8 12 16 16 17 18 19 6 1 1 0 0 0 1 2 3 3 3 1 2 3 2 1 6 9 13 17 20 15 24 25 20 7 1 2 1 0 0 1 2 3 4 4 1 2 2 2 1 10 14 18 21 23 14 23 22 21 8 1 3 3 1 0 1 2 3 4 5 1 1 1 1 1 15 19 22 24 25 13 12 11 10 9 1 4 6 4 1 261
262 5, 5, 6. 14 n, m,,,,, ( 4, : 1289) 6. 15 : int FindMostWord( char * s, int n) ; : s ( ) n : Find- MostWord( The function is many functional module., 3 ) ; 5 6. 16 2 1 ASCII : 2 abcd abcfe, - 2 2 6. 17,, 6. 18,, 6. 19 :, 0 6. 20 :, 0 6. 21 :,, : ; 6. 22 :,, : 0 a,, 9 j, 6. 23 2 n, 2 1 n n, 2 ( strcat) 6. 24 :, 2, 6. 25 N, number, name, score [ 5],, 6. 26 (,,,, ) 5, 12 : ( 1), ( 2),,, ( 3)
C, C ( int) ( char),, 263 7. 1 7. 1. 1 ( bit) : 2 0 1 2 4 : 00, 01, 10, 11 4 8 : 000, 001, 010, 011, 100, 101, 110, 111 8 256 : 0000 0000,, 1111 1111 n 2 n ( byte) :, 8, LSB, MSB 1 2 4 8, 1
, 2, 4, 7. 1. 2 1 2 4 8, 1 1 ),, 1 2 8, 0 255 2 ) 264 1, bit7, bit6 bit0 2, bit15, bit14 bit0 : 0, 1 :, ( 1) : + 7 : + 00000111-7 : - 00000111 ( 2) : + 7 : 00000111 / * 0 * / - 7 : 10000111 / * 1* / ( 3) : 1, : + 7 : 00000111-7 : 11111000 + 0 : 00000000-0 : 11111111 ( 4),,,,, : 0 11, = 12 n 0 2 n - 1, = 2 n
,,, 12, 8 4, 4, 8, 8 4, n, n = 8, 11111111, 1 100000000( 9 ), 8, 1, 00000000, 8 2 8,, n, x : : : 1,, 1 : + 7 : 00000111-7 : - 00000111 10000111 11111000 1 11111001-7 11111001 265 3 ) :, N = S 10 j 0 < s < 10, : N = S 2 j 0 < s < 2 J ( ), 2, 0 < s < 2 32 : N, 8 M, 23 R = 1. M 2 ( N - 127 ) 4 ) C, 3, 7 = ( 111 ) 2 ; 4, 8 = ( 1000 ) 2, 15 = ( 1111) 2 C :, 6, 06 10 11 12 13 14 15 A B C D E F ( a f
) ; 0x, 12, 12 0xC; 0xFF 16 1 C scanf( ) printf( ) o, x,, 0x5A, : 5 A 7. 2 266 C 6 : & < < > > :,, 7. 2. 1 :, 1, 1, 0 : x = 10001001 y = 11101110 10001001 & 11101110 10001000 x&y = 10001000 7. 1 { unsigned int x, y; printf( " Input x and y: " ) ; scanf( " % x, % x", &x, &y) ; printf( " x&y = % x \ n", x&y) ;, 89, ee, x&y = 88 :
( 1) :, : x&0 = 0 00101011 & 00000000 00000000 ( 2) x :, : x, 1, x x : unsigned x = 0xAE x 4 : x&0xf 10101110 & 00001111 00001110 x bit2 bit4 bit6 : x&0x54 10101110 & 01010100 00000100 267 7. 2. 2 : 2, 1, 1, 0 unsigned x = 0x89; Y = 0xEE 10001001 11101110 11101111 x y = 0xEF 7. 2 { unsigned int x, y; printf( " Input x and y: " ) ; scanf( " % x, % x", &x, &y) ; printf( " x y = % x \ n", x y) ;, 89, ee, x y = EF : 1 :, : x 1
, 1, x x 1, : unsigned x = 0 xa0, 4 1 x 0xF = 0xAF 10100000 00001111 10101111 7. 2. 3 268 : 2, ( ), 1, 0 : unsigned x = 0 x89; y = 0xEE 10001001 11101110 01100111 x^y = 0x67 7. 3 { unsigned int x, y; printf( " Input x and y: " ) ; scanf( " % x, % x", &x, &y) ; printf( " x^y = % x\ n", x^y) ;, 89, ee, x^y = 67 : :, : x, 1, x : unsigned x = 0 xae, x 4 : x^0x0f = 0xA1 10101110 00001111 10100001 0, : 00001010 00000000 00001010 2 0 2, : a = a^b; b = b^a; a = a^b; 2 a,
b 7. 2. 4 :, 0 1, 1 0 : 0100001110010111 1011110001101000 : 7. 4 { unsigned int x; printf( " Input x: " ) ; scanf( " % x", &x) ; printf( " x = % x \n", x) ;, F8, x = FF07 : int 16, 00 FF : = + 1 0, 1 a, : a & 1 269 7. 2. 5 : (, 0 ) 1,, 2, n 2 n : a = a < < 2 a 2, 0 7. 5 { unsigned int x, n; printf( " \ ninput x: " ) ; scanf( " % u", &x) ; printf( " Input number to move: " ) ; scanf( " % u", &n) ;
printf( " x < < % u = % u \ n", n, x < < n) ;, 128, 2, x < < 2 = 512 : 0000000010000000 ( : 128) x < < 2 0000001000000000 ( : 512) : 128, 9,, x < < 9 = 0 : 0000000010000000 ( : 128) x < < 9 10000000000000000 ( : 0, 1 ) 7. 2. 6 270 : ( 0 / 1, ), 2, 2, n 2 n,,, : a = a > > 2 a 2 7. 6 { int x, n; printf( " \ ninput x: " ) ; scanf( " % d", &x) ; printf( " Input number to move: " ) ; scanf( " % d", &n) ; printf( " x > > % d = % d \ n", n, x > > n) ;, 128, 2, x > > 2 = 32 : 0000000010000000 ( : 128) x > > 2 0000000000100000 ( : 32) - 128, 2, x > > 2 = - 32 : 1000000010000000 ( : - 128 ) 1111111101111111 ( : - 128 ) 1111111110000000 ( : - 128, )
x > > 2 1111111111100000 ( : - 32, ) 1000000000011111 ( : - 32 ) 1000000000100000 ( : - 32 ) - 000000000100000 ( : - 32) :,, 0, 1 7. 2. 7 C : & =, : a& = b a = a&b =, : a = b a = a b > > =, : a > > = b a = a > > b < < =, : a < < = b a = a < < b =, : a = b a = a b 271 7. 3 7. 7 1 int num, 8 11 ( 0 ) ( 1) : num 8, 8 11 4 1 4 1 0 1 4 num ( 2) : { int num, mask; printf( " Input a integer number: " ) ; scanf( " % d", &num) ; num > > = 8; mask = ( 0 < < 4) ; printf( " result = 0x% x \ n", num & mask) ;
272 : ( 0 < < 4) 0, 1; 4, 4 0, 1;, 4 1, 0 7. 8 ( 1) : 1 int num, 1 1 0 ( ) mask = 1 < < 15 i 1 num&mask ( ), 0, 1 num 1, i% 4 0, 4 i < = 16, ( 2) : #include " stdio. h" { int num, mask, i; printf( " Input a integer number: " ) ; scanf( " % d", &num) ; mask = 1 < < 15; printf( " % d = ", num) ; for( i = 1; i < = 16; i + + ) { putchar( num&mask? 1 : 0 ) ; putchar(, ) ; printf( " B\ n" ) ; 7. 9 num < < = 1; if( i% 4 = = 0) a, 3 3 1101111110101011 - > 0111101111110101, > >, > > 0 1 ( 1) : ( ), n a n b n ( 16 - n ) b = a < < ( 16 - n) ;
a n, n 0 c = a > > n; c b c = a b ( 2) : { unsigned a, b, c; int n; printf( " \ ninput a number and move: " ) ; scanf( " % x% d", &a, &n) ; b = a < < ( 16 - n) ; c = a > > n; c = c b; printf( " % x \ n% x", a, c) ; 7. 10 C, C ( 1) : a ( 16 - n), n b n, 0 : b = a < < ( 16 - n) ; a n : a > > n; a b a, : a = b; 273 ( 2) :
274 #include < ctype. h > { unsigned rrmove( unsigned int a, int n) ; unsigned lrmove( unsigned int a, int n) ; unsigned int a, b; int n; char dir; printf( " \ nnnumber to be moved: " ) ; scanf( " % x", &a) ; printf( " Count to move: " ) ; scanf( " % d", &n) ; printf( " Direction( R / L) : " ) ; getchar( ) ; dir = toupper( getchar( ) ) ; if( dir = = R ) b = rrmove( a, n) ; else b = lrmove( a, n) ; printf( " % c move result: % x \n", dir, b) ; unsigned rrmove( unsigned int a, int n) { unsigned int b; b = a < < ( 16 - n) ; a > > = n; a = b; return a; unsigned lrmove( unsigned a, int n) { unsigned b; b = a > > ( 16 - n) ; a < < = n; a = b; return a;
7. 4 7. 4. 1 C,,,, ( int) ( unsigned int) : struct { ;,, : 275 bit5 bit0 : 0 CPU, 1 PC: Precision Control,, ( ) : 00: 24 ( ) 01: 10: 53 ( ) 11: 64 ( ) RC: Rounding Control, : 00, ( ) 01: 10: 11: C,,, struct FPU { unsigned : 4; / *, 4 * / unsigned RC: 2 / * RC, 2 * / unsigned PC: 2; unsigned : 2; / *, 2 * / unsigned PM: 1; / * PM, 1 * / unsigned UM: 1;
unsigned OM: 1; unsigned ZM: 1; unsigned DM: 1; unsigned IM: 1; ;, struct packed_data { unsigned ord: 2 ; unsigned coll: 6; unsigned adjust: 4; unsigned check: 4; int dat; data; data ord coll adjust check dat 2 6 4 4 16 276 : PC,, 2, : struct bs { unsigned a: 4 unsigned : 0 / * * / unsigned b: 4 / * * / unsigned c: 4, a 1 4, 4 0 ; b 2, 4 ; c 4,, 8, : struct k { int a: 1 int : 2 /* 2 * / int b: 3 int c: 2 ;,,
7. 4. 2,, : unsigned b: 1; struct control { int x; ; : char name[ 8 ] ; unsigned a: 3 ; unsigned : 0 ; unsigned c: 3 ; struct control control_var;, a b c : control_ var. a control _var. b control_var. c, control_var. a 3, 7 :,, ( ),,, n, 0 ( 2 n - 1) % d % x % u % o,,, 7. 11 { struct bs { unsigned a: 1; unsigned b: 3; unsigned c: 4; bit, * pbit; bit. a = 1 ; bit. b = 7; bit. c = 15; 277
printf( " % d, % d, % d \ n", bit. a, bit. b, bit. c) ; pbit = &bit; pbit - > a = 0; pbit - > b& = 3; pbit - > c = 1; printf( " % d, % d, % d \ n", pbit - > a, pbit - > b, pbit - > c) ; : 1, 7, 15 0, 3, 15 278 bs, 3 a, b, c bs bit bs pbit 3 ( ) 3 : bit pbit; a 0; " & = ", pbit - > b = pbit - > b&3, b 7, 3 3( 111&011 = 011, 3) ;, " = ", c 15( pbit - > c = pbit - > c 1) 3,, : struct control * ptr, var_data; ptr = &var_data; printf( % d \n, ptr - > c) ; / * var_data. c * /,, : &var_data. a 7 7. 1, 0 1 2 : void displaybits( unsigned x) ; 7. 2, 4, ( displaybits( ) ), 0 0 1 7. 3 1 2 power2 ( int number, int pow), : number* 2 p ow,
7. 4 2 2, 2, packcharacters PackCharacters : 1, 8, 2 8 7. 5 unpackcharacters, 2, : 0xFF00( 65280),, 8, 1 ; 0x00FF( 255), 8, 2 7. 6,, 7. 7, ( : 0xF9 ),, ( : 0xF9 ) encrypt( char * word, char code) ; : * word, code 7. 8 : unsigned getbits( unsigned value, int start, int end) ; start, end : getbits( 0xFE56, 5, 8) ; 0xFE56 5 8 4 7. 9 2 : unsigned extracteoddbits( unsigned x) ; 7. 10 : unsigned unmutexcode( unsigned value) ; value 1, value, 1; 7. 11 : 16, 7. 12 : 16,, 279
280 8. 1 8. 1. 1 ;,,,, :, ;, 8. 1. 2, : ( 1)
, : 1024, 4, 0000001000000000,, ( ) ASCII, ASCII : 127, ( ASCII ) 3 : 49 50 55 ASCII,, ( ) ( 2) C, C,,,,,,,, C ( 3),,, C,, C : KYBD: SCRN: PRN LPT1 : 3,, 3 : stdin: stdout: stderr: ; ; ; 3,, C,, ( ) C 281
, C, ( 4),,,, UNIX, 1983 ANSI C, 282 8. 1. 3,,,, FILE Turbo C + + 3. 0 stdio. h FILE : / * Definition of the control structure for streams* / typedef struct { int level; / * fill / empty level of buffer * / unsigned flags; / * File status flags * / char fd; / * File descriptor * / unsigned char hold; / * Ungetc char if no buffer * / int bsize; / * Buffer size * / unsigned char * buffer; / * Data transfer buffer * / unsigned char * curp; / * Current active pointer * / unsigned istemp; / * Temporary file indicator * / short token; / * Used for validity checking * / FILE; / * This is the FILE object * /, / FILE, C,,
FILE (, ), /,,,,, : FILE * fp1, * fp2,, * fpn; / * * / FILE fp1, fp2,, fpn, FILE, : :, : : 8. 2 283 : ( 1) / ; ( 2) ; ( 3) 8. 2. 1 C ( ANSI C) fopen / fopen : FILE * fpt; /* fpt* / fpt = fopen( file_name, file_mode) ; : fpt, ; file_name, ; file_mode /, file _ mode : r ; w ; a,,
284 rb wb ab r + ;, ; ; ; / ; ; w + ; a +,, ;, ; rb + ; wb + ; ab + / fopen,, fopen, NULL, : if( ( fpt = fopen( file_name, file_mode) ) = = NULL) { printf( Can t open this file! exit( 0) ;, : #include < stdlib. h > { FILE * fpt; \ n ) ; if( ( fpt = fopen( file1. txt, r ) ) = = NULL) { printf( Can t open this file! exit( 0) \ n ) ; : file1. txt, fpt ;, fpt NULL, exit( 0 ), ;,
8. 2. 2, 2 :,,,,, ( ), C fclose : fclose( ) ; :,, fclose, 0, fclose fclose( fp),, 285 8. 3 8. 3. 1 1 ) fgetc : ch = fgetc( fpt) ; : fpt, ch fgetc, EOF( - 1 ) fpt stdin, fgetc ( stdin), getchar( ) fgetc : fgetc,
286, fgetc( fp) ;,,, fgetc, ( fgetc ) fgetc,,,,,,,, 8. 1 ( 1) : ex8-1. cpp fp ex8-1. cpp, fp, 1 While(! = ) ( 2) : / * ex8-1. cpp * / #include < stdlib. h > { FILE * fp; char ch; if( ( fp = fopen( " ex8-1. cpp", " r" ) ) = = NULL) { printf( " Can t open file ex8-1. cpp! \ n" ) ; exit( 0) ; ch = fgetc( fp) ; / * * / while( ch! = EOF) /* * / { putchar( ch) ; / * putchar( ) * / ch = fgetc( fp) ; / * * / fclose( fp) ; / * fclose( ) * /, EOF( - 1), ASCII - 1 ;
,, - 1, ANSI C feof( pf), feof, feof 8. 2 8. 2 feof / * ex8-2. cpp * / #include < stdlib. h > { FILE * fp; char ch; if( ( fp = fopen( " ex8-2. cpp", " r" ) ) = = NULL) { printf( " Can t open file ex8-2. cpp! \ n" ) ; exit( 0) ; ch = fgetc( fp) ; while(! feof( fp) ) / * feof( ) * / { putchar( ch) ; fclose( fp) ; ch = fgetc( fp) ; 287 2 ) fputc : fputc( ch, fpt) ; : ch fpt fputc, EOF( - 1 ) fpt stdout, fputc ( stdout), putchar( ) fputc :,,,,, fputc,, EOF 8. 3 mydata, #,
288 ( 1) : FILE fp w mydata, fp mydata,,, do while, mydata, # while ( EOF),, ( 2) : / * ex8-3. cpp* / #include < stdlib. h > { FILE * fp; char ch; if( ( fp = fopen( mydata, w ) ) = = NULL) { printf( Can t create file mydata! exit( 0 ) ; printf( " input a string: \n" ) ; do \ n ) ; { ch = getchar( ) ; / * * / fputc( ch, fp) ; / * test. txt* / while( ch! = EOF) ; / * * / fclose( fp) ; / * * / fp = fopen( mydata, r ) ; / * r mydata * / ch = fgetc( fp) ; while( ch! { putchar( ch) ; ch = fgetc( fp) ; printf( " \ n" ) ; fclose( fp) ; = EOF) 3 ) C ( Turbo C + + 3. 0)
( ) putw getw putw(, fp) fp getw( fp) fp, 8. 4 putw 2 / * ex8-4. cpp * / #include < stdlib. h > { union { char c[ 2] ; int x; ch; FILE * fp; if( ( fp = fopen( " test. dat", " w" ) ) = = NULL) { printf( " Can t create file! \ n" ) ; exit( 0) ; ch. c[ 0] = getchar( ) ; getchar( ) ; / * getchar * / ch. c[ 1] = getchar( ) ; putw( ch. x, fp) ; fclose( fp) ; 289 4 ) 8. 5 ( ) / * ex8-5. cpp * / #include < stdlib. h > void main( int argc, char * argv[ ] ) { FILE * fpt; int count = 0 ; if( argc < 2) { printf( " File name unknown! \n" ) ; exit( 0) ; if( ( fpt = fopen( argv[ 1 ], " r" ) ) = = NULL) { printf( " Can t open file data. txt! \ n" ) ;
290 exit( 0) ; while( fgetc( fpt)! count + + ; fclose( fpt) ; printf( " File = EOF), fgetc( fpt)! % s contains % d characters. \ n", argv[ 1], count) ; 8. 6 = EOF :, ( ) / * ex8-6. cpp * / #include < stdlib. h > void main( int argc, char * argv[ ] ) { FILE * fpt; int count = 0 ; / * * / int space = 1; /* * / char ch; if( argc < 2) { printf( " File name unknown! \n" ) ; exit( 0) ; if( ( fpt = fopen( argv[ 1 ], " r" ) ) = = NULL) { printf( " Can t open file data. txt! \ n" ) ; exit( 0) ; while( ( ch = fgetc( fpt) )! switch( ch) { case : fclose( fpt) ; printf( " File case case default: \ t : = EOF) \ n : space + + ; break; if( space) { space = 0; count + + ; % s contains % d words. \ n", argv[ 1], count) ;
8. 7 / * ex8-7. cpp * / #include < stdlib. h > void main( int argc, char * argv[ ] ) { FILE * in, * out; char ch; if( argc! = 3) { printf( " You forgot to enter a filename. \n" ) ; exit( 0) ; if( ( in = fopen( argv[ 1 ], " rb" ) ) = = NULL) { printf( " Cannot open in_file. \ n" ) ; exit( 0) ; if( ( out = fopen( argv[ 2], " wb" ) ) = = NULL) { printf( " Cannot open out_file. \ n" ) ; exit( 0) ; while( 1) { ch = fgetc( in) ; if(! feof( in) ) fputc( ch, out) ; else break; fclose( in) ; fclose( out) ; 291 8. 3. 2 1 ) fputs : fputs( string, fpt) : string ( ) fpt fputs
292 8. 9 ( 1) :, EOF( - 1) stdout, puts, \0 : fputs \ 0 ; puts myfile. txt 6 10 FILE fp w myfile. txt, fp myfile. txt,,, for 6 6,, fputc( \ n, fp) / * ex8-8. cpp * / #include < stdlib. h > { FILE * fp; int i; char s[ 10 ] ; if( ( fp = fopen( " myfile. txt", " w" ) ) = = NULL) { printf( " error" ) ; exit( 0) ; printf( " enter string: \ n" ) ; for( i = 1; i < = 6; i + + ) { scanf( " % s", s) ; fclose( fp) ; fputs( s, fp) ; fputc( \n, fp) ; 2 ) fgets : fgets( string, n, fpt) ; : fpt n - 1, string n - 1 \ n EOF,, \ 0 fgets string
8. 9, NULL stdin, gets, \ n : fgets, \0 ; gets \0,, abc. txt, # abc. txt / * ex8-9. cpp * / #include < stdlib. h > #include < string. h > { FILE * fp; char str[ 80] ; int i = 0; if( ( fp = fopen( " abc. txt", " w" ) ) = = NULL) { printf( " can t open file \n" ) ; exit( 0) ; printf( " Enter a string: \ n" ) ; gets( str) ; while( str[ i]! = # ) { if( str[ i] > = a &&str[ i] < = z ) str[ i] = str[ i] - 32; fputc( str[ i], fp) ; i + + ; fclose( fp) ; fp = fopen( " abc. txt", " r" ) ; fgets( str, strlen( str) + 1, fp) ; printf( " % s \ n", str) ; fclose( fp) ; 293
8. 3. 3 1 ) fscanf 294 : fscanf( fpt,, ) ; : ASCII fpt, 8. 10 scanf, ( ) ( ) stdin, scanf test. dat, / * ex8-10. cpp * / #include < stdlib. h > { char str[ 80] ; int x; FILE * fpt; if( ( fpt = fopen( test. dat, r ) ) = = NULL) { puts( Can t open file. \n ) ; exit( 0) ; while( fscanf( fpt, % s % d, str, &x)! = EOF) printf( % s % d \n, str, x) ; fclose( fpt) ; 2 ) fprintf : fprintf( fpt,, ) ; : ASCII fpt fpt stdout, printf 8. 11 / * ex8-11. cpp * /
#include < stdlib. h > #include < string. h > { FILE * fpt; int num; char name[ 10] ; float score; if( ( fpt = fopen( score. dat, w ) ) = = NULL) { puts( Can t create file. \n ) ; exit( 0) ; printf( " Input name, number, score: " ) ; scanf( " % s % d % f", name, &num, &score) ; while( strlen( name)! = 0) { fprintf( fpt, " % s % d % f", name, num, score) ; printf( " Input name, number, score: " ) ; scanf( " % s % d % f", name, &num, &score) ; fclose( fpt) ; 295 8. 3. 4 1 ) fwrite : fwrite( buffer, size, count, fpt) ; : buffer ; size ; count size ; fpt : buffer count size ( count* size ) fpt, - 1 8. 12 n( n < 100) emp, :,,,, ( 1) :,,,, ; fp;
296 if( emp ) { ; ; n; n n ; n fwrite fp emp ; fp ( 2) : / * ex8-12. cpp * / #include < stdlib. h > #define num 100 static struct employee { char no[ 5] ; char name[ 10] ; char sex[ 3] ; int age; int salar; emp[ num] ; { FILE * fp; int n, i; char inbuf[ 20] ; if( ( fp = fopen( " emp", " wb" ) ) = = NULL) { printf( " can t create file emp \ n" ) ; exit( 0) ; printf( " Input emp number: \ n" ) ; scanf( " % d", &n) ; printf( " : < enter > \ n" ) ; for( i = 0; i < n; i + + ) { printf( " % d : ", i + 1) ; gets( emp[ i]. no) ; gets( emp[ i]. name) ; gets( emp[ i]. sex) ; gets( inbuf) ; emp[ i]. age = atoi( inbuf) ; gets( inbuf) ;
emp[ i]. salar = atoi( inbuf) ; for( i = 0; i < n; i + + ) fwrite( &emp[ i], sizeof( struct employee), 1, fp) ; fclose( fp) ; : Input emp number: 3 : < enter > 1 : 1 zhangli m 22 1500 2 : 2 wangwu f 23 3000 3 : 3 lisi m 32 1000 2 ) fread : fread( buffer, size, count, fpt) ; : buffer ; size ; count size ; fpt : fpt count size ( count* size ) buffer, - 1 8. 13 8. 12 emp / * ex8-13. cpp * / #include < stdlib. h > #define num 100 static struct employee { char no[ 5] ; char name[ 10] ; char sex[ 3] ; int age; int salar; emp[ num] ; { FILE * fp; int i = 0; if( ( fp = fopen( " emp", " r" ) ) = = NULL) 297
298 { printf( " can t open file emp \n" ) ; exit( 0) ; printf( \ n ) ; while( 1) { fread( &emp[ i], sizeof( struct employee), 1, fp) ; printf( " % - 9s% - 9s% - 9s% - 8d% - 8d \n", emp[ i]. no, emp[ i]. name, emp[ i]. sex, emp[ i]. age, emp[ i]. salar) ; i = i + 1; fclose( fp) ; if( feof( fp) ) break; : 1 zhangli m 22 1500 2 wangwu f 23 3000 3 lisi m 32 1000 8. 4 8. 4. 1,, ( ; ),,, rewind ftell fseek 1 ) rewind : rewind( fpt) ; : 8. 14 8. 3
/ * ex8-14. cpp* / #include < stdlib. h > { FILE * fp; char ch; if( ( fp = fopen( mydata, w + ) ) = = NULL) { printf( Can t create file mydata! exit( 0) ; printf( " input a string: \n" ) ; do \n ) ; { ch = getchar( ) ; / * * / fputc( ch, fp) ; / * mydata * / while( ch! = EOF) ; / * * / rewind( fp) / * * / ch = fgetc( fp) ; while( ch! = EOF) { putchar( ch) ; ch = fgetc( fp) ; printf( " \ n" ) ; fclose( fp) ; 299 2 ) ftell : ftell( fpt) ; :,, - 1L 8. 15 emp / * ex8-15. cpp * / #include < stdlib. h > viod main( ) { FILE * fp; int i; if( ( fp = fopen( " emp", " r" ) ) = = NULL) { printf( " can t open file emp" ) ;
exit( 0) ; fseek( fp, 0, SEEK_END) ; / * * / printf( " emp length is: % ldbyte \ n", ftell( fp) ) ; fclose( fp) ; 3 ) fseek 300 : fseek( fpt, offset, from) ; : fpt ; offset,, ; from, : SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 : from offset ( offset, offset ) : fseek( fp, 80 L, 0) ;, 80 : fseek( fp, 40L, 1) ; 40 : fseek( fp, - 40L, 2) ;, 40 8. 4. 2 C, C, 3 : ; fseek ; fread 8. 16 emp 1500 / * ex8-16. cpp * /
#include < stdlib. h > { FILE * fp; static struct employee { char no[ 5] ; emp; int i = 0; char name[ 10] ; char sex[ 3] ; int age; int salar; if( ( fp = fopen( " emp", " rb" ) ) = = NULL) { printf( " can t open file emp \n" ) ; exit( 0) ; printf( \ n ) ; while( 1) { fseek( fp, i* sizeof( struct employee), 0) ; fread( &emp, sizeof( struct employee), 1, fp) ; if( feof( fp) ) break; if( emp. salar > 1500) printf( " % - 9s% - 9s% - 9s% - 8d% - 8 d \ n", emp. no, emp. name, i = i + 1; emp. sex, emp. age, emp. salar) ; fclose( fp) ; 301 8. 5 C, I / O, fgets NULL, 2,,, C
8. 5. 1 ferror : ferror( fpt) ; : I /O, ferror ferror ( 0), ; ferror ( 0), I / O ferror, I / O, fopen, ferror 0 8. 5. 2 clearerr 302 : clearerr( fpt) ; : 0 8 8. 1 ( 1) C,, ( 2) fopen( " test", " w + " ), test, ( 3) fputs( buff, stdout) ( 4) ab. txt, fopen( " ab. txt", " r + " ) ( 5) feof( fp) ( 6 ) : fread ( buffer, size, count, fpt ) ; buffer ( 7) : fgets( buffer, n, fp) ; fp buffer ( 8) : main( ) { FILE * fp; char str1[ ] = thank you ; fp = fopen( PRN, w ) ; fputs( str1, fp) ;
fclose( fp) ; ( 9) #include < stdlib. h > main( ) { FILE * fp; if( ( fp = fopen( " test", " w" ) ) = = NULL) { printf( " Can t create file \n" ) ; exit( 0) ; for( i = 1; i < = 10 ; i + + ) fprintf( fp, " % 3d", i) ; for( i = o; i < 10; i + + ) { fseek( fp, i* 3L, SEEK_SET) ; fscanf( fp, " % d", &n) ; printf( " % 3 d", n) ; fclose( fp) ; ( 10) : struct stu{ char name[ 10] ; char sex[ 2] ; int telephone; char addr[ 20 ] ; student[ 40] ; student, fp, fwrite( student,, 1, fp) ; 8. 2 ( 1) ( ) A. B. C. D. ( 2) ( ) A. B. C. D. 303
304 ( 3) fopen 1 ( ) A. d: myfile \ test. txt B. d: \ myfile \abc. txt C. " d: \ myfile \ a. txt" D. " d: \ \ myfile \ \ mydata. txt" ( 4) fopen, ( ) A. B. 0 C. 1 D. error ( 5) fopen,, ( ) A. ab + B. wb C. rb + D. ab ( 6), fclose ( ) A. - 1 B. TRUE C. 0 D. 1 ( 7) feof, ( ) A. EOF B. 1 C. 0 D. ( 8) fseek ( ) A. B. C. D. ( 9) fgetc, ( ) A. B. C. D. B C ( 10) : fwrite( buffer, size, count, fp) ; buffer ( ) A., B., C., D., ( 11), : test abc xyz ( b) /* filename test. cpp* / main( int argc, char * argv[ ] ) { FILE * fp1, fp2; if( argc < 3) { printf( " Usage: test filename1 filename2 \ n" ) ; exit( 0 ) ; fp1 = fopen( argv[ 1], " r" ) ; fp2 = fopen( arg[ 2 ], " w" ) ; while(! feof( fp1 ) ) fputc( fgetc( fp1), fp2) ;
fclose( fp1) ; fclose( fp2) ; A. xyz abc B. abc xyz C. abc xyz, abc D. xyz abc, xyz ( 12), test ( ) #include < stdlib. h > main( ) { FILE * fpt; char * s1 = thank ; char * s2 = you ; if( ( fpt = fopen( test. txt, w + ) ) = = NULL) { printf( Can t open file test \ n ) ; exit( 0 ) ; fwrite( s1, 5, 1, fpt) ; fseek( fpt, 0l, SEEK_SET) ; fwrite( s2, 3, 1, fpt) ; fclose( fpt) ; A. thank B. you C. D. younk ( 13) fopen file_mode r w, ( ) A. r, w B. r, w C. r, w D., r, w ( 14) fread( buff, 64, 2, fp1) ( ) A. fp 64 2, buf B. fp 2 64, buf C. fp 2 64, buf D. fp 64 2, buf 305
306 8. 3 ( 1)?? ( 2)?? ( 3)? 8. 4 ( 1),, ( 2) 3,, ( 3) Fprint fscanf ( 4) 3,, ( 0, 1, 2), ( 5) cos 2* x /32( x = 0, 1, 2,, 32 ), result. txt ( 6) ( ) ( 7) 2 file1 file2,, 2 ( ), file3 ( 8),, test,! ( 9) 5, ( ), stud 3 ( 10) stud stud_sort ( 11) file1. c file2. c ( 12) mydata ( 13), ( 14) test. dat,
C 9. 1 307 9. 1. 1 C, :,, 2, ;, C 1 ) C C,, ( ), : struct test { char ch; struct test * next; /* next struct test * /
308 ;,, : struct test { char ch; struct test next; /* next struct test * / ; 2,, 2 : struct A { int x; struct B * pb; /* pb struct B * / ; struct B { int y; struct A * pa; /* pa struct A * / ; 9. 1 { struct B; /* struct B, * / struct A { int x; struct B * pb; a = {100, NULL ; struct B { int y; struct A * pa; b = {500, NULL ; a. pb = &b; b. pa = &a; printf( " % d \ n", b. pa - > x) ; printf( " % d \ n", a. pb - > y) ; a pb b, a. pb - > y b y b pa a, b. pa - > x a
C x 2 ) C, C stdlib. h alloc. h, 2, malloc realloc free 3 ( 1) malloc : void * malloc( unsigned int size) ; : size, ( ) void( ),, NULL ( 2) free : void free( void * ptr) ; : ptr, 9. 2, #include < string. h > #include < stdlib. h > { int num, i; char inbuf[ 100], * * strs; printf( " Enter the number of string for input: " ) ; scanf( " % d", &num) ; getchar( ) ; strs = ( char * * ) malloc( num* sizeof( char * ) ) ; for( i = 0 ; i < num; i + + ) { gets( inbuf) ; strs[ i] = ( char * ) malloc( strlen( inbuf) + 1) ; strcpy( strs[ i], inbuf) ; for( i = 0 ; i < num; i + + ) puts( strs[ i] ) ; strs = ( char * * ) malloc( num* sizeof( char * ) ) ; num, strs, strs 309
310, strs[ i] = ( char * ) malloc( strlen( inbuf) + 1 ) ;, i strs[ i], 9. 1 ( 3) realloc : void * realloc( void * ptr, unsigned int newsize) ; : ptr newsize, ptr, ; 9. 1 ptr, 9. 3, #include < string. h > #include < stdlib. h > { char inbuf[ 100], * strp; printf( " Enter several strings, null line for end. \ n" ) ; gets( inbuf) ; strp = ( char * ) malloc( strlen( inbuf) + 1) ; strcpy( strp, inbuf) ; while( strlen( gets( inbuf) ) ) { strp = ( char * ) realloc( strp, strlen( strp) + strlen( inbuf) + 1) ; strcat( strp, inbuf) ; puts( strp) ;, strcpy strp, realloc strp strlen( strp) + strlen ( inbuf) + 1,, ( )
C 9. 1. 2,,, 2 :,, :, ; 1 ;,, : ; ; ; ; ; ; ; ; ;,,, ( ),, 2 311 1 ), ;, ( 1) C,,, 9. 2, 1 ( 0 ) ( M),
312 : LOC( i) = LOC( 0) + i * M ( 2) : ; 9. 2,, ; ; : #define N 100 typedef struct stu { char name[ 20] ; float score; STU; STU stu[ N] ; int n = 0; : int insertlist( int i, STU x) / * i x * / { int j; if( n > = N) { printf( " overflow! " ) ; getch( ) ; return - 1; if( i < 0 i > n) { printf( " out of range! " ) ; getch( ) ; return - 1; for( j = n; j > i; j - - ) stu[ j] = stu[ j - 1] ; stu[ j] = x;
C n + + ; return i;,, ;,,,,,,,, n n,, ( 3) : ;, ( ) ; : #define N 100 typedef struct stu { char name[ 20] ; float score; STU; STU stu[ N] ; int n = 0; : int deletelist( int i) { int j; if( i < 0 i > n - 1) { printf( " no this element! " ) ; getch( ) ; return - 1; for( j = i + 1; j < n; j + + ) stu[ j - 1] = stu[ j] ; 313
314 n - - ; return i;,,,,, n n - 1,, 9. 4, ( 1) : int insertlist ( int i, STU x) ;, : int deletelist( int i) ; : void printlist( int m) ; ( 2) : While( 1) switch( ) Case insertlist Case deletelist Case printlist Case Case Case ( 3) C : #include < stdlib. h > #include < string. h > #include < conio. h >
C #define N 100 typedef struct stu { char name[ 20] ; STU; float score; STU stu[ N] ; int n = 0; int insertlist( int i, STU x) ; int deletelist( int i) ; void printlist( int m) ; { char inbuf[ 10] ; int del; char chose; STU x; for( ; ; ) { clrscr( ) ; printf( " 1: Insert element to the list. \ n" ) ; printf( " 2: Delete element from the list. \n" ) ; printf( " 3: Print the list. \ n" ) ; printf( " 0: Exit. \ n" ) ; printf( " \nchose the number: " ) ; chose = getchar( ) ; getchar( ) ; switch( chose) { case 1 : gets( x. name) ; gets( inbuf) ; x. score = atof( inbuf) ; insertlist( n, x) ; break; case 2 : printf( " Input the number for delete: " ) ; scanf( " % d", &del) ; getchar( ) ; deletelist( del) ; break; case 3 : printlist( n) ; printf( " Enter any key for return menu. \ n" ) ; getch( ) ; break; 315
316 case 0 : exit( 1) ; default: int insertlist( int i, STU x) { int j; if( n > = N) break; { printf( " overflow! \ nprint any key for return menu. " ) ; getch( ) ; return - 1; if( i < 0 i > n) { printf( " out of range! \ nprint any key for return menu. " ) ; getch( ) ; return - 1; for( j = n; j > i; j - - ) stu[ j] = stu[ j - 1] ; stu[ j] = x; n + + ; return i; int deletelist( int i) { int j; if( i < 0 i > n - 1) { printf( " no this element! \ nprint any key for return menu. " ) ; getch( ) ; return - 1; for( j = i + 1; j < n; j + + ) n - - ; return i; stu[ j - 1] = stu[ j] ; void printlist( int m) { int i; for( i = 0 ; i < m; i + + ) printf( " % d: % s \t% f\ n", i, stu[ i]. name, stu[ i]. score) ;
C 2 ),,,,,,, ( NULL ) 9. 3 9. 3 : ; : ;,,,, 9. 4 9. 4 C : typedef struct node { elementtype data; struct node * next; NODE;, elementtype ; NODE struct node ( 1) 2 : 2 :,, NODE * create( int n) / * n * / { NODE * p, * h; int i; char inbuf[ 10] ; 317
318 h = ( NODE * ) malloc( sizeof( NODE) ) ; /* * / h - > next = NULL; for( i = n; i > 0; i - - ) { p = ( NODE * ) malloc( sizeof( NODE) ) ; /* * / gets( p - > name) ; gets( inbuf) ; p - > score = atof( inbuf) ; p - > next = h - > next; /* * / h - > next = p; return h;,, ( 2), void printlist( NODE * h) { NODE * current = h; while( current - > next! = NULL) { current = current - > next; printf( " % s \t% f\ n", current - > name, current - > score) ; ( 3), 9. 5, : ; ; ; void insertlist( NODE * h, char * s) { NODE * p, * old, * last;
C p - > next = q - > next q - > next = p 9. 5 char inbuf[ 20] ; p = ( NODE * ) malloc( sizeof( NODE) ) ; / * * / printf( " \tinput the data of the new node: \ n" ) ; gets( p - > name) ; gets( inbuf) ; p - > score = atof( inbuf) ; last = h - > next; /* * / while( strcmp( last - > name, s)! = 0&&last - > next! = NULL) { old = last; last = last - > next; if( last - > next! = NULL) /*, * / { old - > next = p; p - > next = last; Else / *, * / { last - > next = p; p - > next = NULL;,, ( name), ( 4),, 9. 6,, : ; ; 319
320 q - > next = p - > next free( p) ; 9. 6 void deletelist( NODE * h, char * s) { NODE * q = h, * p = h - > next; while( strcmp( p - > name, s)! = 0&&p - > next! = NULL) /* * / { q = p; p = p - > next; if( p - > next! = NULL) /* * / { q - > next = p - > next; free( p) ; else /* * / { printf( " no this element! \ n" ) ; getch( ) ;,, 9. 5, ( 1) : NODE * create( int n) ; / * n * / void insertlist( NODE * h, char * s) ; / * * / void deletelist( NODE * h, char * s) ; / * * / void printlist( NODE * h) ; / * * / ( 2) : While( 1)
C switch( ) Case create Case insertlist Case deletelist Case printlist Case Case Case ( 3) C : #include < stdlib. h > #include < string. h > #include < conio. h > typedef struct stu { char name[ 20] ; float score; struct stu * next; NODE; NODE * create( int n) ; void insertlist( NODE * h, char * s) ; void deletelist( NODE * h, char * s) ; void printlist( NODE * h) ; { char chose, sname[ 20] ; int n; NODE * head = NULL; for( ; ; ) { clrscr( ) ; printf( " 1: Create the list. \ n" ) ; printf( " 2: Insert element to the list. \ n" ) ; printf( " 3: Delete element from the list. \n" ) ; 321
322 printf( " 4: Print the list. \ n" ) ; printf( " 0: Exit. \ n" ) ; printf( " \nchose the number: " ) ; chose = getchar( ) ; getchar( ) ; switch( chose) { case 1 : Zprintf( " Input the number of List: " ) ; scanf( " % d", &n) ; getchar( ) ; head = create( n) ; break; case 2 : printf( " Input the name for INSERT: " ) ; gets( sname) ; insertlist( head, sname) ; break; case 3 : printf( " Input the data for delete: " ) ; gets( sname) ; deletelist( head, sname) ; break; case 4 : printlist( head) ; printf( " Enter any key for return menu. \ n" ) ; getch( ) ; break; case 0 : exit( 1) ; default: NODE * create( int n) { NODE * p, * h; int i; char inbuf[ 10] ; break; h = ( NODE * ) malloc( sizeof( NODE) ) ; h - > next = NULL; for( i = n; i > 0; i - - ) { p = ( NODE * ) malloc( sizeof( NODE) ) ; gets( p - > name) ; gets( inbuf) ; p - > score = atof( inbuf) ;
C p - > next = h - > next; h - > next = p; return h; void insertlist( NODE * h, char * s) { NODE * p, * old, * last; char inbuf[ 20] ; p = ( NODE * ) malloc( sizeof( NODE) ) ; printf( " \tinput the data of the new node: \ n" ) ; gets( p - > name) ; gets( inbuf) ; p - > score = atof( inbuf) ; last = h - > next; while( strcmp( last - > name, s)! = 0&&last - > next! = NULL) { old = last; last = last - > next; if( last - > next! = NULL) { old - > next = p; p - > next = last; else { last - > next = p; p - > next = NULL; void deletelist( NODE * h, char * s) { NODE * q = h, * p = h - > next; while( strcmp( p - > name, s)! = 0&&p - > next! = NULL) { q = p; p = p - > next; if( p - > next! = NULL) { q - > next = p - > next; free( p) ; else { printf( " no this element! \ nenter any key for return menu. \ n" ) ; 323
getch( ) ; void printlist( NODE * h) { NODE * current = h; while( current - > next! = NULL) { current = current - > next; printf( " % s \t% f\ n", current - > name, current - > score) ; 9. 1. 3 1 ) 324,, ( LIFO), p, p = 0,,, : ; ( ), ; ( ), ;, ;,, 2 ) C, top, top M, top = M, top = 0,,,,,, push pop
C #define MAX 100 char stack[ MAX] ; int top = 0; void push( char c) / * * / { if( top > = MAX - 1) { printf( " Stack is full. \ n" ) ; exit( 1) ; else stack[ top + + ] = c; char pop( ) / * * / { char ch; if( top = = 0) { printf( " Stack is empty. \n" ) ; exit( 1 ) ; else ch = stack[ - - top] ; return ch; 9. 6 ( 1) : ;, 0; ; ; ; ( 2) C : #include < string. h > #include < stdlib. h > #define MAX 100 char stack[ MAX] ; int top = 0; void push( char c) ; char pop( ) ; 325
326 { char s[ 100] ; int i; printf( " Input the string s. \n" ) ; gets( s) ; for( i = 0 ; i < strlen( s) ; i + + ) /* * / push( s[ i] ) ; for( i = 0 ; i < strlen( s) ; i + + ) /* * / s[ i] = pop( ) ; s[ i] = \0 ; puts( s) ; void push( char c) /* * / { if( top > = MAX - 1) else { printf( " Stack is full. \ n" ) ; exit( 1) ; stack[ top + + ] = c; char pop( ) /* * / { char ch; if( top = = 0) { printf( " Stack is empty. \ n" ) ; else exit( 1) ; ch = stack[ - - top] ; return ch; 3 ),, stack, 9. 7, push pop 9. 7
C typedef struct node { char data; struct node * next; NODE; NODE * stack; void push( char c) / * * / { NODE * p; p = ( NODE* ) malloc( sizeof( NODE) ) ; p - > data = c; p - > next = stack; stack = p; char pop( ) / * * / { char ch; NODE * p; if( stack = = NULL) { printf( " Stack is empty. \ n" ) ; exit( 1) ; else { p = stack; stack = stack - > next; ch = p - > data; free( p) ; return ch; 9. 7, #include < string. h > #include < stdlib. h > typedef struct node { char data; struct node * next; NODE; NODE * stack; void push( char c) ; 327
328 char pop( ) ; { char s[ 100] ; int i; printf( " Input the string s. \n" ) ; gets( s) ; for( i = 0 ; i < strlen( s) ; i + + ) push( s[ i] ) ; for( i = 0 ; i < strlen( s) ; i + + ) s[ i] = pop( ) ; s[ i] = \0 ; puts( s) ; void push( char c) { NODE * p; p = ( NODE* ) malloc( sizeof( NODE) ) ; p - > data = c; p - > next = stack; stack = p; char pop( ) { char ch; NODE * p; if( stack = = NULL) { printf( " Stack is empty. \ n" ) ; else exit( 1) ; { p = stack; stack = stack - > next; ch = p - > data; free( p) ; return ch;
C 9. 1. 4 1 ), ( rear), ( front) ( FIFO) : ;, ;, ; ; 2 ) ( 1), front rear, front ; rear, 9. 8 329 9. 8 ( 2), front rear, 9. 9, rear, ;, 9. 9, 0 ( 3) front rear,
330, M : front = ( fornt + 1) % M rear = ( rear + 1) % M,, rear = front;, rear = fornt; : front ; rear ; front, ; ; : rear = front ( rear + 1 ) % M = front ( 4), : int addqueue( STU x) / * * / { if( ( rear + 1) % N = = front) { printf( " Full Queuer. \ n" ) ; else return - 1; { rear = ( rear + 1 ) % N; Que[ rear] = x; return 0; STU deletequeue( ) / * * / { STU x; if( front = = rear) { printf( " Empty Queuer. \ n" ) ; else exit( 1) ; { front = ( front + 1) % N; x = Que[ front] ; return x;
C 9. 8, #include < stdlib. h > #include < string. h > #include < conio. h > #define N 20 typedef struct stu { char name[ 20] ; STU; float score; STU Que[ N] ; int front = 0, rear = 0; int addqueue( STU x) ; STU deletequeue( ) ; void printqueue( ) ; { char inbuf[ 10] ; int del; char chose; STU x; for( ; ; ) { clrscr( ) ; printf( " 1: Add in queuer. \ n" ) ; printf( " 2: Delete from queuer. \n" ) ; printf( " 3: Print the queuer. \n" ) ; printf( " 0: Exit. \ n" ) ; printf( " \nchose the number: " ) ; chose = getchar( ) ; getchar( ) ; switch( chose) { case 1 : printf( " Input the element: \ n" ) ; gets( x. name) ; gets( inbuf) ; x. score = atof( inbuf) ; addqueue( x) ; break; case 2 : deletequeue( ) ; 331
332 break; case 3 : printqueue( ) ; printf( " Enter any key for return menu. \ n" ) ; getch( ) ; break; case 0 : exit( 1) ; default: int addqueue( STU x) break; { if( ( rear + 1) % N = = front) { printf( " Full Queuer. \ n" ) ; else return - 1; { rear = ( rear + 1 ) % N; Que[ rear] = x; return 0; STU deletequeue( ) { STU x; if( front = = rear) { printf( " Empty Queuer. \n" ) ; else exit( 1 ) ; { front = ( front + 1 ) % N; x = Que[ front] ; return x; void printqueue( ) { int i = front + 1; if( rear > = front) else for( ; i < = rear; i + + ) printf( " % d: % s\ t% f \n", i, Que[ i]. name, Que[ i]. score) ;
C { for( ; i < N; i + + ) printf( " % d: % s \t% f\ n", i, Que[ i]. name, Que[ i]. score) ; for( i = 0; i < = rear; i + + ) printf( " % d: % s \t% f\ n", i, Que[ i]. name, Que[ i]. score) ; 3 ),,, 9. 10,, 9. 11 9. 10, : front = rear ( ) ( ), 9. 11, : front = rear = ( NODE * ) malloc( sizeof( NODE) ) ; / * NODE * / rear - > next = NULL; void addqueue( NODE x) / *, x * / { NODE * p; p = ( NODE * ) malloc( sizeof( NODE) ) ; strcpy( p - > name, x. name) ; p - > score = x. score; p - > next = NULL; rear - > next = p; rear = p; void deletequeue( ) / * * / { NODE * p; if( front = = rear) { printf( " Empty Queue! \n" ) ; exit( 1) ; 333
334 p = front - > next; front - > next = p - > next; if( p - > next = = NULL) /*, * / rear = front; free( p) ; 9. 9,, ( ) #include < stdlib. h > #include < string. h > #include < conio. h > typedef struct stu /* * / { char name[ 20] ; float score; struct stu * next; STU; STU * front, * rear; void addqueue( STU x) ; void deletequeue( ) ; void printqueue( ) ; { char inbuf[ 10] ; int del; char chose; STU x; front = rear = ( STU* ) malloc( sizeof( STU) ) ; rear - > next = NULL; for( ; ; ) { clrscr( ) ; printf( " 1: Add in queuer. \ n" ) ; printf( " 2: Delete from queuer. \n" ) ; printf( " 3: Print the queuer. \n" ) ; printf( " 0: Exit. \ n" ) ; printf( " \nchose the number: " ) ; chose = getchar( ) ; getchar( ) ; switch( chose)
C { case 1 : printf( " Input the element: \ n" ) ; gets( x. name) ; gets( inbuf) ; x. score = atof( inbuf) ; addqueue( x) ; break; case 2 : deletequeue( ) ; break; case 3 : printqueue( ) ; printf( " Enter any key for return menu. \ n" ) ; getch( ) ; break; case 0 : exit( 1) ; default: void addqueue( STU x) { STU * p; break; p = ( STU * ) malloc( sizeof( STU) ) ; strcpy( p - > name, x. name) ; p - > score = x. score; p - > next = NULL; rear - > next = p; rear = p; void deletequeue( ) { STU * p; if( front = = rear) { printf( " Empty Queue! \n" ) ; exit( 1) ; p = front - > next; front - > next = p - > next; if( p - > next = = NULL) free( p) ; rear = front; void printqueue( ) 335
{ STU * p = front - > next; while( p! = NULL) { printf( " % s \t% f\ n", p - > name, p - > score) ; p = p - > next; 9. 2 336 ( C ),, N N - 1, ( ),,,, ( ),, ( ), ( ) 9. 2. 1 : n, ( ), 1 ; N - 1 ( ), 2 ;, ( ), : ( ), ; ; ( ), ;, 9. 12,
C : [ 5 4 9 17 24 3 1 ] 1 : 1 [ 4 9 17 24 3 5 ] 2 : 1 3 [ 9 17 24 4 5 ] 3 : 1 3 4 [ 17 24 9 5 ] 4 : 1 3 4 5 [ 24 9 17 ] 5 : 1 3 4 5 9 [ 24 17 ] 6 : 1 3 4 5 9 17 [ 24 ] 9. 12 : typedef struct word { char w[ 20] ; / *, * / int count; WORD; void selectsort( WORD v[ ], int n) { int i, j, k; WORD t; for( i = 0 ; i < n - 1; i + + ) * / { k = i; for( j = i + 1; j < n; j + + ) if( v [ j]. count > v[ k]. count) if( k! = i) k = j; t = v[ i], v[ i] = v[ k], v[ k] = t; / * n, n - 1 n, n - 1, 1 n - 1, 2 n - 2, i n - i, n - 1 1, : n - 1 ( n - i) = 1 i = 1 2 ( n2 - n) 337 O( n 2 ) 9. 10, #include < stdlib. h > #define N 10 / * * / typedef struct word / * * / { char w[ 20] ; int count; WORD;
338 void selectsort( WORD v[ ], int n) ; { WORD a[ N] ; char inbuf[ 20] ; int i; for( i = 0 ; i < N; i + + ) / * * / { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; selectsort( a, N) ; for( i = 0 ; i < N; i + + ) printf( " % d \ t% s \ n", a[ i]. count, a[ i]. w) ; void selectsort( WORD v[ ], int n) { int i, j, k; WORD t; for( i = 0 ; i < n - 1 ; i + + ) { k = i; for( j = i + 1; j < n; j + + ) if( v [ j]. count > v[ k]. count) k = j; if( k! = i) t = v[ i], v[ i] = v[ k], v[ k] = t; 9. 2. 2,,,,, :, ;, ;,
C ; ;,, 9. 13, : [ 6 ] 5 10 18 25 4 1 1 : [ 5 6 ] 10 18 25 4 1 2 : [ 5 6 10 ] 18 25 4 1 3 : [ 5 6 10 18 ] 25 4 1 4 : [ 5 6 10 18 25 ] 4 1 5 : [ 4 5 6 10 18 25 ] 1 6 : [ 1 4 5 6 10 18 25 ] : #define N 10 typedef struct word { char w[ 20] ; int count; WORD; 9. 13 void insertsort( WORD v[ ], int n) { int i, j; for( i = 2 ; i < = n; i + + ) { v[ 0] = v[ i] ; /* 0 * / for( j = i - 1; v[ 0 ]. count > v[ j]. count; j - - ) / * v[ j + 1] = v[ j] ; * / v[ j + 1 ] = v[ 0] ; / * * /,, ( 0 ),,, n n - 1, 1 2, 2 3, i i + 1, n n + 1 : n i = 1 i = 2 2 ( n + 2 ) ( n - 1) O( n 2 ) 9. 11, 339
340 #include < stdlib. h > #define N 10 typedef struct word { char w[ 20] ; int count; WORD; void insertsort( WORD v[ ], int n) ; { WORD a[ N + 1] ; / * * / char inbuf[ 20] ; int i; for( i = 1 ; i < N + 1; i + + ) /* 0, * / { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; insertsort( a, N) ; for( i = 1 ; i < N + 1; i + + ) printf( " % d \ t% s \ n", a[ i]. count, a[ i]. w) ; void insertsort( WORD v[ ], int n) { int i, j; for( i = 2 ; i < = n; i + + ) { v[ 0] = v[ i] ; for( j = i - 1; v[ 0 ]. count > v[ j]. count; j - - ) v[ j + 1] = v[ j] ; v[ j + 1 ] = v[ 0] ; 9. 2. 3,,, : While( ) ;
C If( ), ; Else, ; : #define N 10 typedef struct word { char w[ 20] ; int count; WORD; void binsertsort( WORD v[ ], int n) { int i, j, low, high, mid; for( i = 2 ; i < = n; i + + ) { v[ 0] = v[ i] ; / * * / low = 1, high = i - 1; / * low, high * / while( low < = high) / * * / { mid = ( low + high) /2; if( v [ 0]. count > v[ mid]. count) high = mid - 1; else low = mid + 1 ; for( j = i - 1; j > = low; j - - ) / * * / v[ j + 1] = v[ j] ; v[ low] = v[ 0] ; / * * /, O( nlog 2 n),, O( n 2 ), O( n 2 ) 9. 12, #include < stdlib. h > #define N 10 typedef struct word { char w[ 20] ; int count; 341
342 WORD; void binsertsort( WORD v[ ], int n) ; { WORD a[ N + 1] ; char inbuf[ 20] ; int i; for( i = 1 ; i < N + 1; i + + ) { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; binsertsort( a, N) ; for( i = 1 ; i < N + 1; i + + ) printf( " % d \ t% s \ n", a[ i]. count, a[ i]. w) ; void binsertsort( WORD v[ ], int n) { int i, j, low, high, mid; for( i = 2 ; i < = n; i + + ) { v[ 0] = v[ i] ; low = 1, high = i - 1; while( low < = high) { mid = ( low + high) /2 ; if( v [ 0]. count > v[ mid]. count) high = mid - 1; else low = mid + 1 ; for( j = i - 1; j > = low; j - - ) v[ j + 1] = v[ j] ; v[ low] = v[ 0] ; 9. 2. 4,, :
C,,,, ( ) ;, ;, n n - 1 : #define N 10 typedef struct word { char w[ 20] ; int count; WORD; void bubblesort( WORD v[ ], int n) { int i, j, flag = 1; / * flag 1* / i = n - 1; / * n - 1 * / while( i > 0&&! flag) / * * / { flag = 0; / * flag = 0* / for( j = 1 ; j < = i; j + + ) if( v[ j + 1]. count > v[ j]. count) { flag = 1 ; / *, flag i - - ; = 1* / v[ 0] = v[ j], v[ j] = v[ j + 1], v[ j + 1] = v[ 0] ;,,, flag( 1 ), flag 0 ( ),, 0, ; 1, : :, n n - 1, 2 ( i - 1) i = n = 1 2 ( n 2 - n), 3, 2 3 ( i - 1) = 3 i = n 2 ( n 2 - n) O( n 2 ) 9. 13, 343
344 #include < stdlib. h > #define N 10 typedef struct word { char w[ 20] ; int count; WORD; void bubblesort( WORD v[ ], int n) ; { WORD a[ N + 1] ; char inbuf[ 20] ; int i; for( i = 1 ; i < N + 1; i + + ) { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; bubblesort( a, N) ; for( i = 1 ; i < N + 1; i + + ) printf( " % d \ t% s \ n", a[ i]. count, a[ i]. w) ; void bubblesort( WORD v[ ], int n) { int i, j, flag = 1; i = n - 1; while( i > 0&&! flag) { flag = 0; for( j = 1 ; j < = i; j + + ) if( v[ j + 1]. count > v[ j]. count) { flag = 1 ; v[ 0] = v[ j], v[ j] = v[ j + 1], v[ j + 1] = v[ 0] ; i - - ; 9. 2. 5 ( D. L. Shell) 1959,
C :,,,, 1,, : ( ) ;, ; ( ) ;, 1, ( 38, 26, 54, 87, 65, 16, 3, 26 ), 4 2 1 9. 14 : 38 26 54 87 65 16 3 26 4 1 : 38 16 3 26 65 26 54 87 2 2 : 3 16 38 26 54 26 65 87 1 3 : 3 16 26 26 38 54 65 87 9. 14 : #define N 10 typedef struct word { char w[ 20] ; int count; WORD; void shellsort( WORD v[ ], int n) { int i, j, gap; WORD temp; for( gap = n /2; gap > 0; gap / = 2) /*, * / for( i = gap; i < n; i + + ) /* * / for( j = i - gap; j > = 0&&v[ j]. count < v[ j + gap]. count; j - = gap) temp = v[ j], v[ j] = v[ j + gap], v[ j + gap] = temp; 9. 14, #include < stdlib. h > #define N 10 typedef struct word { char w[ 20] ; int count; 345
346 WORD; void shellsort( WORD v[ ], int n) ; { WORD a[ N] ; char inbuf[ 20] ; int i; for( i = 0 ; i < N; i + + ) { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; shellsort( a, N) ; for( i = 0 ; i < N; i + + ) printf( " % d \ t% s \ n", a[ i]. count, a[ i]. w) ; void shellsort( WORD v[ ], int n) { int i, j, gap; WORD temp; for( gap = n /2; gap > 0; gap / = 2) for( i = gap; i < n; i + + ) for( j = i - gap; j > = 0&&v[ j]. count < v[ j + gap]. count; j - = gap) temp = v[ j], v[ j] = v[ j + gap], v[ j + gap] = temp; 9. 2. 6, :,,,,, ;,, ( 45, 21, 53, 13, 86, 79, 67, 36 ) 9. 15,,
C : 45 21 53 13 86 79 67 36 1 : [ 36 21 53 45 86 79 67] 13 2 : [ 67 53 86 79] 45 [ 36 21] 13 3 : [ 79 67 86] 53 45 [ 36 21] 13 4 : [ 86 79] 67 53 45 [ 36 21] 13 5 : 86 79 67 53 45 [ 36 21] 13 6 : 86 79 67 53 45 36 21 13 9. 15 : typedef struct word { char w[ 20] ; int count; WORD; void quicksort( WORD v[ ], int left, int right) { void swap( WORD v[ ], int i, int j) ; int i, last; WORD t; if( left > = right) / * 1 * / return; swap( v, left, ( left + right) /2) ; / * * / last = left; for( i = left + 1; i < = right; i + + ) if( v [ i]. count > v[ left]. count) swap( v, + + last, i) ; swap( v, left, last) ; quicksort( v, left, last - 1) ; / * * / quicksort( v, last + 1, right) ; / * * / void swap( WORD v[ ], int i, int j) / * * / { WORD t; t = v[ i], v[ i] = v[ j], v[ j] = t; 9. 15, #include < stdlib. h > #define N 8 typedef struct word { char w[ 20] ; 347
348 int count; WORD; void quicksort( WORD v[ ], int left, int right) ; { WORD a[ N] ; char inbuf[ 20] ; int i; for( i = 0 ; i < N; i + + ) { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; quicksort( a, 0, N - 1) ; for( i = 0 ; i < N; i + + ) printf( " % d \ t% s \ n", a[ i]. count, a[ i]. w) ; void quicksort( WORD v[ ], int left, int right) { void swap( WORD v[ ], int i, int j) ; int i, last; WORD t; if( left > = right) return; swap( v, left, ( left + right) /2) ; last = left; for( i = left + 1 ; i < = right; i + + ) if( v[ i]. count > v[ left]. count) swap( v, + + last, i) ; swap( v, left, last) ; quicksort( v, left, last - 1 ) ; quicksort( v, last + 1, right) ; void swap( WORD v[ ], int i, int j) { WORD t; t = v[ i], v[ i] = v[ j], v[ j] = t; 9. 3,
C :,,,, 3 : ; ;, n, ( ASL: Average Search Length) : n ASL = P i C i i = 1 : P i i, C i i ASL, ASL,,, 349 9. 3. 1, :,,,, : #define N 10 typedef struct word { char w[ 20] ; int count; WORD; int listsearch( WORD v[ ], int key, int n) { int i; v[ 0]. count = key; / * * / for( i = n; v[ i]. count! = key; i - - ) ; return i; / * 0, 0* /, 0
350,, :, n P = 1 /n,, i C i, : 9. 16 n ASL = P i C i i = 1 = 1 n n i = 1 i = 1 2 ( n + 1), ( ) #include < stdlib. h > #define N 10 typedef struct word { char w[ 20] ; int count; WORD; int listsearch( WORD v[ ], int key, int n) ; { WORD a[ N + 1] ; char inbuf[ 20] ; int i, key, result; for( i = 1 ; i < N + 1; i + + ) { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; printf( " Input the key: " ) ; scanf( " % d", &key) ; if( ( result = listsearch( a, key, N) ) > 0 ) else printf( " a[ % d] : % d \ t% s \n", result, a[ result]. count, a[ result]. w) ; printf( " No this element. \n" ) ; int listsearch( WORD v[ ], int key, int n) { int i; v[ 0]. count = key; return i; for( i = n; v[ i]. count! = key; i - - ) ;
C 9. 3. 2,, : :, 2,,,,, : #define N 10 typedef struct word { char w[ 20] ; int count; WORD; int binarysearch( WORD v[ ], int key, int n) { int low = 0, high = n - 1, mid; while( low < = high) / * * / { mid = ( low + high) /2; / * * / if( key > v[ mid]. count) low = mid + 1; / *, * / else if( key < v[ mid]. count) high = mid - 1; / *, * / else return mid; / *, * / return - 1; / *, - 1* / :,, : 351
352 9. 17 ASL = n + 1 log 2 ( n + 1) - 1 log 2 n n, 1 ( ) #include < stdlib. h > #define N 10 typedef struct word { char w[ 20] ; int count; WORD; int binarysearch( WORD v[ ], int key, int n) ; { WORD a[ N] ; char inbuf[ 20] ; int i, key, result; for( i = 0 ; i < N; i + + ) { gets( a[ i]. w) ; gets( inbuf) ; a[ i]. count = atoi( inbuf) ; printf( " Input the key: " ) ; scanf( " % d", &key) ; if( ( result = binarysearch( a, key, N) ) > 0 ) else printf( " a[ % d] : % d \ t% s \n", result, a[ result]. count, a[ result]. w) ; printf( " No this element. \n" ) ; int binarysearch( WORD v[ ], int key, int n) { int low = 0, high = n - 1, mid; while( low < = high) { mid = ( low + high) /2; if( key > v[ mid]. count) low = mid + 1; else if( key < v[ mid]. count) else high = mid - 1; return mid;
C return - 1; 9. 4 9. 4. 1 C, 2,, ; ( Shanding),,, ( ),, ; +,,, CAD,,,,,, 25 80,,, 9. 16, = 1024 768,, 9. 16,, X, Y 0, 1 024 768 1 023 767, C ( Turbo C + + 3. 0 ), graphics. h, 2 : 353
Options /Linker Graphics lib on; * BGI; 9. 4. 2 C 1 ) 354,,,, ( 25 80 ),, : void far initgraph( int far * gdriver, int far * gmode, char * path) ; gdriver gmode, path,. BGI, EGA VGA EGAVGA. BGI 9. 1, path, ( path ),, 2 :, : void far detectgraph( int * gdriver, * gmode) ; initgraph detectgraph gdriver gmode initgraph initgraph gdriver 9. 1 DETECT 9. 18 #include < graphics. h > #include < conio. h > { int gdriver = DETECT, gmode, maxx, maxy; initgraph( &gdriver, &gmode, " d: \ \ thec \ \ tc \ \ bgi" ) ; / * maxx = getmaxx( ) ; * / / * getmaxx X * /
C maxy = getmaxy( ) ; / * getmaxy Y * / printf( " resolving power = % d % d \ n", maxx, maxy) ; getch( ) ; closegraph( ) ; 9. 1 ( gdriver) ( gmode) CGA 1 CGAC0 0 C0 320* 200 CGAC1 1 C1 320* 200 CGAC2 2 C2 320* 200 CGAC3 3 C3 320* 200 CGAHI 4 2 640* 200 MCGA 2 MCGAC0 0 C0 320* 200 MCGAC1 1 C1 320* 200 MCGAC2 2 C2 320* 200 MCGAC3 3 C3 320* 200 MCGAMED 4 2 640* 200 MCGAHI 5 2 640* 480 EGA 3 EGALO 0 16 640* 200 EGAHI 1 16 640* 350 EGA64 4 EGA64LO 0 16 640* 200 EGA64HI 1 4 640* 350 EGAMON 5 EGAMONHI 0 2 640* 350 IBM8514 6 IBM8514LO 0 256 640* 480 IBM8514HI 1 256 1024* 768 HERC 7 HERCMONOHI 0 2 720* 348 ATT400 8 ATT400C0 0 C0 320* 200 ATT400C1 1 C1 320* 200 ATT400C2 2 C2 320* 200 ATT400C3 3 C3 320* 200 ATT400MED 4 2 320* 200 ATT400HI 5 2 320* 200 VGA 9 VGALO 0 16 640* 200 VGAMED 1 16 640* 350 VGAHI 2 16 640* 480 PC3270 10 PC3270HI 0 2 720* 350 DETECT 0 355 : initgraph( &gdriver, &gmode, " d: \ \ thec \ \ tc \ \ bgi" ) ;
356 : initgraph( &gdriver, &gmode, " " ) ;,, C GRAPHICS. LIB, ( EGAVGA. BGI ) : BGIOBJ ( EGAVGA. OBJ), C BGIOBJ EGAVGA TLIB ( EGAVGA. OBJ) LIB GRAPHICS. LIB, TLIB LIB\ GRAPHICS. LIB + EGAVGA, registerbgidriver( EGAVGA_driver) ; 9. 19 / *, * / #include < graphics. h > #include < conio. h > { int gdriver = DETECT, gmode, maxx, maxy; registerbgidriver( EGAVGA_driver) ; /* * / initgraph( &gdriver, &gmode, " " ) ; maxx = getmaxx( ) ; maxy = getmaxy( ) ; printf( " resolving power = % dx% d \ n", maxx, maxy) ; getch( ) ; closegraph( ) ; 2 ), : void far setbkcolor( int color) ; void far setcolor( int color) ; ( ) int far getbkcolor( void) ; ( )
C int far getcolor( void) ; int far getmaxcolor( void) ; voide far cleardevice( void) ; ( ) ( ) color, EGA VGA, 9. 2 9. 2 EGA VAG BLACK 0 DARKGRAY 8 BLUE 1 LIGHTBLUE 9 GREEN 2 LIGHTGREEN 10 CYAN 3 LIGHTCYAN 11 RED 4 LIGHTRED 12 MAGENTA 5 LIGHTMAGENTA 13 BROWN 6 YELLOW 14 LIGHTGRAY 7 WHITE 15 357 3 ) : int far getmaxx( void) ; x int far getmaxy( void) ; y int far getx( void) ; x void far gety( void) ; y void far moveto( int x, int y) ; ( x, y) void far moverel( int dx, int dy) ; ( x, y) ( x + dx, y + dy) void far putpixel( int x, int y, int color) ; color color 9. 2, x, y int far getpixel( int x, int y) ; ( x, y) 4 ), : void far line( int x0, int y0, int x1, int y1) ; ( x0, y0 ) ( x1, y1) void far lineto( int x, int y) ; ( x, y) void far linerel( int dx, int dy) ; ( x, y)
358 dx dy ( x + dx, y + dy) void far circle( int x, int y, int radius) ; ( x, y), radius, void far arc( int x, int y, int stangle, int endangle, int radius) ; ( x, y), radius, stangle endangle ( ) TURBO x 0,, 90, 180, 270 360, void ellipse( int x, int y, int stangle, int endangle, int xradius, int yradius) ; ( x, y), xradius, yradius x y, stangle endangle, stangle = 0, endangle = 360, void far rectangle( int x1, int y1, int x2, inty2 ) ; ( x1, y1), ( x2, y2) void far drawpoly( int numpoints, int far * polypoints) ; numpoints, polypoints polypoints 2 x, y, x, numpoints, polypoints void far setlinestyle ( int? linestyle, unsigned? upattern, int thickness) ;, linestyle, 9. 3, thickness, 9. 4 upattern, linestyle USERBIT_LINE ( upattern 0 ), uppattern 16, 1, ( ), ( ) 9. 3 SOLID_LINE 0 DOTTED_LINE 1 CENTER_LINE 2 DASHED_LINE 3 USERBIT_LINE 4 9. 4 NORM_WIDTH 1 THIC_WIDTH 3 void far getlinesettings( struct linesettingstype far * lineinfo) ;
C lineinfo, linesettingstype : struct linesettingstype { int linestyle; unsigned upattern; int thickness; 1 void far setwritemode( int mode) ; mode = 0, ; mode = 1, ( XOR), 9. 20 #include < conio. h > #include < graphics. h > { int gdriver = DETECT, gmode, i; registerbgidriver( EGAVGA_driver) ; initgraph( &gdriver, &gmode, " d: \ \ thec \ \ tc" ) ; setbkcolor( BLUE) ; cleardevice( ) ; setcolor( GREE N) ; circle( 320, 240, 98) ; setlinestyle( 0, 0, 3) ; setcolor( 2) ; rectangle( 220, 140, 420, 340) ; setcolor( WHITE) ; setlinestyle( 4, 0xaaaa, 1) ; getch( ) ; closegraph( ) ; 359 5 ),,, : void far bar( int x1, int y1, int x2, int y2) ; ( x1, y1), ( x2, y2),, void far bar3d( int x1, int y1, int x2, int y2, int? depth, int topflag) ;
topflag 0, topflag 0, ( ) bar3d( ), 45, void far pieslice( int x, int y, int stangle, int? endangle, int radius) ; ( x, y), radius, stangle, endangle, stangle = 0, endangle = 360, X void far sector( int x, int y, int stanle, int endangle, intxradius, int yradius) ; ( x, y), xradius, yradius x y, stangle, endangle,, : void far setfillstyle( int pattern, int color) ;, color pattern 9. 5 9. 5 pattern 360 EMPTY_FILL 0 SOLID_FILL 1 LINE_FILL 2 LTSLASH_FILL 3 ( ) SLASH_FILL 4 ( ) BKSLASH_FILL 5 ( ) LTBKSLASH_FILL 6 ( ) HATCH_FILL 7 XHATCH_FILL 8 INTTERLEAVE_FILL 9 WIDE_DOT_FILL 10 CLOSE_DOS_FILL 11 USER_FILL 12 void far setfillpattern( char * upattern, int color) ; upattern 8 8 8 8 8 8, 8 8, 9. 21 #include < graphics. h >
C #include < dos. h > #include < conio. h > { int gdriver = DETECT, gmode, i; registerbgidriver( EGAVGA_driver) ; initgraph( &gdriver, &gmode, " d: \ \ thec \ \ tc" ) ; setbkcolor( BLUE) ; cleardevice( ) ; for( i = 0 ; i < 13; i + + ) { setcolor( i + 3) ; setfillstyle( i, 2 + i) ; / * * / bar( 100, 150, 200, 50 ) ; bar3 d( 300, 100, 500, 200, 70, 1) ; pieslice( 200, 300, 90, 180, 90 ) ; sector( 500, 300, 180, 270, 200, 100) ; delay( 2000) ; / * 2 * / getch( ) ; closegraph( ) ; 361 6 ), ( 0, 0 ),,,,,,,, setviewport( ) : void far setviewport( int xl, int yl, int x2, int y2, int clipflag) ; ( xl, yl), ( x2, y2), x1, y1, x2, y2 clipflag 0,, clipflag 0, void far clearviewport( void) ; void far getviewsettings( struct viewporttype far * viewport) ;, viewporttype viewport, viewporttype : struct viewporttype
{ int left; int top; int right; int bottom; int cliplag; ; 7 ) 362,, printf( ), puts( ), putchar ( ),, 80, 25,, : void far outtext( char far * textstring) ; textstring void far outtextxy( int x, int y, char far * textstring) ; textstring ( x, y) x y int sprintf( char * str, char * format, variable_list) ;, printf( ) str, void far settexjustify( int horiz, int vert) ; outtextxy( int x, int y, char far * str textstring), ( x, y) :,,, 3,,, 3, 9 settextjustify( ) horiz 3, 2 vert 3,, outtextxy( ), ( x, y) outtext( ), horiz vert 9. 6 9. 6 horiz vert LEFT_TEXT 0 RIGHT_TEXT 2 BOTTOM_TEXT 0 TOP_TEXT 2 CENTER_TEXT 1
C void far settextstyle( int font, int direction, int charsize) ; ( font ) ( direction ) ( charsize ) font 9. 7, direction 9. 8, charsize 9. 9 9. 7 font( ) DEFAULT_FONT 0 8 8 ( ) TRIPLEX_FONT 1 3 SMALL_FONT 2 SANSSERIF_FONT 3 GOTHIC_FONT 4 9. 8 direction( ) HORIZ_DIR 0 VERT_DIR 1 363 9. 9 charsize( ) / 1 8 8 2 16 16 3 24 24 4 32 32 5 40 40 6 48 48 7 56 56 8 64 64 9 72 72 10 80 80 USER_CHAR_SIZE = 0 void far setusercharsize( int mulx, int divx, int muly, int divy) ;, settextstyle ( ) charsize 0 ( USER _ CHAR_SIZE), settextstyle ( ) setusercharsize( ),? : x( mulx/ divx),
364 : y( muly / divy) 9. 22 #include < graphics. h > #include < conio. h > { int gdriver = DETECT, gmode; registerbgidriver( EGAVGA_driver) ; initgraph( &gdriver, &gmode, " d: = \ \ thec \ \ tc" ) ; setbkcolor( BLUE) ; cleardevice( ) ; setfillstyle( 1, 2 ) ; setcolor( WHITE) ; rectangle( 100, 100, 330, 380) ; floodfill( 50, 50, 14) ; setcolor( RED) ; settextstyle( 1, 1, 8) ; outtextxy( 210, 120, " Chongqing University" ) ; setusercharsize( 2, 1, 4, 1) ; setcolor( 15) ; settextstyle( 3, 0, 5) ; outtextxy( 220, 220, " Chongqing University" ) ; setusercharsize( 4, 1, 1, 1) ; settextstyle( 3, 0, 0) ; outtextxy( 220, 120, " GOOD" ) ; getch( ) ; closegraph( ) ; 9. 4. 3 C,,, 1 ),, :
C X = xcos - ysin Y = xsini + ycos, ( X, Y), ( x, y), 4, ( x, y), void rotate_point( double theta, double * x, double * y, int x_org, int y_org) { double tx, ty; tx = * x - x_org; ty = * y - y_org; * x = tx* cos( theta) - ty* sin( theta) ; * y = tx* sin( theta) + ty* cos( theta) ; * x + = x_org; * y + = y_org; ( x, y), : ) ;, ;, (, 3 for( j = 0; j < 4; j + + ) { c = getcolor( ) ; / * * / 9. 23 setcolor( getbkcolor( ) ) ; /* * / line( ( int) ob[ j] [ 0], ( int) ob[ j] [ 1 ], ( int) ob[ j] [ 2], ( int) ob[ j] [ 3] ) ; / *, * / rotate_point( theta, &ob[ j] [ 0], &ob[ j] [ 1 ], x, y) ; / * / rotate_point( theta, &ob[ j] [ 2], &ob[ j] [ 3 ], x, y) ; setcolor( c) ; / * * / line( ( int) ob[ j] [ 0], ( int) ob[ j] [ 1 ], ( int) ob[ j] [ 2], ( int) ob[ j] [ 3] ) ; / * * /,, R, L, #include < graphics. h > #include < conio. h > #include < ctype. h > 365
366 #include < math. h > { void rotate_obj( double ob[ ] [ 4 ], double theta, int x, int y, int sides) ; void display_obj( double ob[ ] [ 4], int sides) ; double obj[ 4] [ 4] = {100, 100, 100, 150, 100, 150, 120, 150, 120, int gdriver = DETECT, gmode, maxx, maxy; registerbgidriver( EGAVGA_driver) ; 150, 120, 100, 120, 100, 100, 100 ; / * * / initgraph( &gdriver, &gmode, " d: \ \ thec \ \ tc" ) ; display_obj( obj, 4) ; rotate_obj( obj, 1. 55, 125, 110, 4) ; void display_obj( double ob[ ] [ 4 ], int sides) / * * / { int i; for( i = 0 ; i < sides; i + + ) line( ( int) ob[ i] [ 0], ( int) ob[ i] [ 1], ( int) ob[ i] [ 2], ( int) ob[ i] [ 3] ) ; void rotate_obj( double ob[ ] [ 4 ], double theta, int x, int y, int sides) { void rotate_point( double theta, double * x, double * y, int x_org, int y_org) ; int i, j; char ch; for( ; ; ) { ch = getch( ) ; switch( tolower( ch) ) { case l : theta = theta < 0? - theta: theta; break; case r : theta = theta > 0? - theta: theta; default: break; return; for( j = 0; j < sides; j + + ) { int c = getcolor( ) ; setcolor( getbkcolor( ) ) ; line( ( int) ob[ j] [ 0], ( int) ob[ j] [ 1], ( int) ob[ j] [ 2], ( int) ob[ j] [ 3] ) ; rotate_point( theta, &ob[ j] [ 0 ], &ob[ j] [ 1], x, y) ; rotate_point( theta, &ob[ j] [ 2 ], &ob[ j] [ 3], x, y) ; setcolor( c) ; line( ( int) ob[ j] [ 0], ( int) ob[ j] [ 1], ( int) ob[ j] [ 2], ( int) ob[ j]
C [ 3] ) ; void rotate_point( double theta, double * x, double * y, int x_org, int y_org) { double tx, ty; tx = * x - x_org; ty = * y - y_org; * x = tx* cos( theta) - ty* sin( theta) ; * y = tx* sin( theta) + ty* cos( theta) ; * x + = x_org; * y + = y_org; 2 ),, ( startx, starty), ( endx, endy) ( x, y) : void copy( int startx, int starty, int endx, int endy, int x, int y) { int i, j, c; for( i = startx; i < = endx; i + + ) for( j = starty; j < = endy; j + + ) { c = getpixel( i, j) ; putpixel( x + i, y + j, c) ;, ( startx, starty), ( endx, endy) ( x, y) : void move( int startx, int starty, int endx, int endy, int x, int y) { int i, j, c; for( i = startx; i < = endx; i + + ) for( j = starty; j < = endy; j + + ) { c = getpixel( i, j) ; putpixel( x + i, y + j, c) ; putpixel( i, j, c^c) ; 367
368 9. 24 #include < graphics. h > #include < conio. h > #include < ctype. h > #include < math. h > { void display_obj( double ob[ ] [ 4], int sides) ; void copy( int startx, int starty, int endx, int endy, int x, int y) ; void move( int startx, int starty, int endx, int endy, int x, int y) ; double obj[ 4] [ 4] = {100, 100, 100, 150, 100, 150, 120, 150, 120, 150, 120, 100, 120, 100, 100, 100 ; int gdriver = DETECT, gmode, maxx, maxy; registerbgidriver( EGAVGA_driver) ; initgraph( &gdriver, &gmode, " d: \ \ thec \ \ tc" ) ; display_obj( obj, 4) ; copy( 95, 95, 125, 155, 300, 150 ) ; getch( ) ; move( 95, 95, 125, 155, 160, 300) ; getch( ) ; void display_obj( double ob[ ] [ 4 ], int sides) { int i; for( i = 0 ; i < sides; i + + ) line( ( int) ob[ i] [ 0], ( int) ob[ i] [ 1], ( int) ob[ i] [ 2], ( int) ob[ i] [ 3] ) ; void copy( int startx, int starty, int endx, int endy, int x, int y) { int i, j, c; for( i = startx; i < = endx; i + + ) for( j = starty; j < = endy; j + + ) { c = getpixel( i, j) ; putpixel( x + i, y + j, c) ; void move( int startx, int starty, int endx, int endy, int x, int y) { int i, j, c; for( i = startx; i < = endx; i + + ) for( j = starty; j < = endy; j + + )
C { c = getpixel( i, j) ; putpixel( x + i, y + j, c) ; putpixel( i, j, c^c) ; 9 9. 1 : :,,, 9. 2 ( 0 ), :,, 9. 3 : n( n > 0), 1, 2,, n, start ( start > 0), 1 end( end > 0), end ;, end ; : ( 1) n n ( 2) ( 3),, newh, newh ( 4) newh 9. 4 2 : ( 1), ; ( 2) : typedef struct node { float coef; / * * / int exp; / * * / struct node * next; / * * / NODE; 369
370 [ 1] Brian W. Kernighan, Dennis M. Rithie, The C Proramming Language, Second Edition. Pretice - Hall International, Inc. :, 1997 [ 2],,.. :, 2000 [ 3].. :, 1989 [ 4],,. C. :, 1999 [ 5] Al Kelley, Ira Pohl, A Book on c: Programming in C( C ). :, 2004 [ 6] H. M. Deitel, P. J. Deitel.. C. :, 2000 [ 7] Samuel P. Harbison III, Guy L. Steele Jr., A Reference Manual( C ) 5th. :, 2003 [ 8] Matthias Felleisen,,. How To Design Programs( ). :, 2003 [ 9], C ( 3 ),, 1995 [ 10] Robert Sedgewick.. C ( : ) 3th. :, 2004 [ 11],. C. :, 2003 [ 12],,,,,. :, 2004 [ 13],. ( C ). :, 1999