C
( CIP) C /. :, 2001. 7 21 ISBN 7-5624 -2355-5. C........ C. TP312 CIP ( 2001 ) 034496 C * * : 7871092 1 /16 : 14. 25 : 356 20017 1 20017 1 : 1 6 000 ISBN 7-5624-2355-5 / TP311 : 21. 00
C, C,,,, C,, ( ),, ( ), C C ++ JAVA, C,, C,,,,,,, 1, C, 2 C 3 5 C, C,,, 1
C 6 7, 8,, 9, 10, 11 C ASCII C, C,,,, C,,,,,, 1 11,,,,,, 87 ANSI C,,, MS-DOS Windows MS-DOS Turbo C, UNIX C,, : ( 1 5 7 ), ( 2 4 ), ( 8 10 ), ( 11 ). ( ),,,,,,,, 2 2001 5
1 C 1 1.1 C 1 1.2 C 2 1.3 5 1.4 C 10 10 2 12 2.1 C 13 2.2 14 2.3 17 2.4 19 2.5 21 2.6 23 2.7 24 2.8 24 2.9 27 2.10 27 2.11 29 2.12 30 2.13 31 2.14 C 31 32 3 35 3.1 C 35 3.2 37 3.3 46 3.4 goto 55 1
C 3.5 56 57 4 59 4.1 59 4.2 62 4.3 67 4.4 70 4.5 70 4.6 71 73 5 75 5.1 75 5.2 78 5.3 82 5.4 86 5.5 89 5.6 92 5.7 main 94 5.8 95 98 6 100 6.1 C 100 6.2 104 6.3 109 6.4 114 6.5 115 6.6 118 6.7 121 6.8 125 6.9 127 131 7 C 133 7.1 C 133 7.2 139 7.3 144 145 2
8 147 8.1 147 8.2 153 8.3 156 8.4 163 8.5 165 8.6 typedef 166 168 9 169 9.1 169 9.2 172 9.3 174 176 10 177 10.1 177 10.2 179 10.3 181 10.4 187 10.5 189 10.6 191 193 11 C 194 11.1 194 11.2 198 11.3 199 209 A ASCII 209 B C 210 C C 210 D C 214 3
1 C, C C,, C, C C, 1. 1 C 1. 1. 1 C 1969, ( Bell Laboratories) Ken Thompson D. M. Rithie UNIX 1970, Ken Thompson UNIX, BCPL ( ALGOL 60 CPL ) B 1973, D. M. RithieB C, K. Thompson D. M. Rithie C UNIX 1978, B. W. Kernighan D. M. Rithie The C Programming Language C, CC, C, ( ),, ( ), C 1987, ( ANSI) C 87 ANSI C, 1990, ISO 87 ANSI C ISO C C DOS MS C5. 0 Turbo C 2. 0, UNIX UNIX 4. 2BSD, C C ++ C, C ++ C, C ++ C, C C ++ Borland C ++ Visual C ++ JAVA C, C, 1
C 1. 1. 2 C C : C,,, ( 10 % 20% ) C,,, C, C, C C, 34, C, C #, C,,, ( ),,, 1. 2 C C, C 1. 2. 1 C 1. 1 #include stdio. h { printf( Good morning! \ n ) ; printf( * * * * * * * * * * * * * * * \ n ) ; : Good morning! * * * * * * * * * * * * * * *, #include stdio. h, stdio. h, C printf( ), C {, printf( ), 2
1 C \n, 1. 2 #include stdio. h / *, C * / { int r; / *, r ( int ) * / float area; / *, area ( float ) * / printf( Input r: ) ; / * r, * / scanf( % d, &r) ; / * scanf( ), r * / area = 3. 1415926* r* r; / *, area * / printf( area = % f\ n, area) ; / *, area =, \n * / : Input r: 10 area = 314. 159260 :,, / * * /,,, 1. 3 #include stdio. h / *, C * / { int a, b, c; / *, a, b, c int * / int max( int x, int y) ; / * int max, 2, int * / / * * / printf( Input a, b: ) ; / * a, b, * / scanf( % d, % d, &a, &b) ; / * 2 a b, * / c = max( a, b) ; / * max( ), c * / printf( max = % d \ n, c) ; / * c * / int max( int x, int y) / * max( ), int, x, y int * / { int z; / * int z * / if ( x > y) else z = y; z = x; return z; / * z * / : Input a, b: 16, 20 max = 20 2, main max, ( ) max, a, b 3
C max x, ymax z 1. 2. 2 C, C : C, 1. 3, C : { ; ;,, C,, ; C, sum, mean, #include, #, ;, / * * /,,, C ( C ) 3 scanf( ) printf( ), 1. 3 max( ),, : ( ) { ; ;, 1. 1 1. 3,,,,,, 4
1 C 1. 3,, 1. 3. 1 ( 1 )?,,,,,,,,, 1. 4 100, : 1, 100 ; 2,, : 1 ; 2 ; ; 3 ; ; 4 ; 100 ; 99 100 ; 100, ;, 1 000,,,,,, 100, : sum,,, sum, sum = 0 c, mean,,, n,,, n, n = 0 5
C : sum0 ; n0; c; c sum, c + sumsum( ) ; n 1, n + 1n( ) ; n < 100, ( n < 100 100,, n 100, 100, a, ) ; sum /n, mean, sum / nmean; mean,,, 100 1 000, n < 100 n < 1000 ( 2 ), 1. 1 1. 4, 1. 2 Y 1. 1 N sum= 0 n= 0 c sum= sum+ c n= n+ 1 n< 100 mean= sum n mean 1. 4 ( ),, ( ), ( ), 1. 2,,, ( ),, 1. 1 sum = 0 n = 0 0 sum sum0 = 0 0 sum sum 0 6 1. 5,, ch, :
1 C ch; ch ( \ n ), ; ch a ch z ch - ( a - A ) ch ch, ch ; ; ch ; ;,, ch 1. 3 : Y 1. 6 s = 3! + 5! + 7! + 9! s, n!, n = 3, 5, 7, 9 s = 0 ; n = 3; s = s + n! ; n = n + 2; n9,, ; s= 0 n= 3 fact( n) s= s+ n! n= n+ 2 n9 s N 1. 4 fact( x) i= 1 m= 1 ix Y m= m* i i= i+ 1 m 1. 6 N 1. 3 s; ch ch n Y Y ch ch ch N N 1. 5 n!? fact( x), x = n, fact ( n ) n! n!, fact( n) fact( x) ( m, i, i = 1, 2, 3,, x) : i = 1; m = 1 ; ix, ; m = m* i; i = i + 1 ; ; m 1. 4 ( 3 ) : 7
C,, ( ), 0 1 0, 1. 3. 2 C C, 8 C 3 ( 1 ) 1. 4 C #include stdio. h / *, C * / { int c, n; / *, c, n ( int ) * / float sum, mean; / *, sum, mean ( float ) * / sum = 0; n = 0; / *, * / / * sum n * / do{ / * do{ while * / scanf( % d, &c) ; / * c* / sum = sum + c; / * * / n = n + 1; / * * / while( n < 100) ; / * * / mean = sum / n; / * * / printf( average = % f\ n, mean) ; /*, average =, \ n * / ( 2 ) 1. 5 C #include stdio. h / *, stdio. h, getchar( ) putchar( ) * / { char ch; / * ch ( char ) * / while ( ( ch = getchar( ) )! / * * / = \ n ) / * while, : ch * / { if ( ch >= a&& ch <= z ) / * if * / ch = ch - ( a - A ) ; / * ch, * / putchar( ch) ; / * * / printf( \ n ) ; / * * /
1 C : Good morning! GOOD MORNING!, while, {, { ( ), for, {, ( 3 ) 1. 6 C #include stdio. h /*, C * / { int n; /*, n int * / long s, fact( int) ; / * s long, long fact,, int * / for( s = 0, n = 3; n <= 9; n = n + 2) /* for, n = 3, 5, 7, 9, 4 * / s = s + fact( n) ; /* fact( x) * / printf( 3! + 5! + 7! + 9! = % ld \ n, s) ; /* s* / long fact( int x) / * fact( x), long, x int * / { long m = 1; / *, m i long int, m * / int i; / * * / for( i = 1; i < = x; i + + ) / * for, i = 1, 2, 3,, x, x * / m = m* i; / * i * / return m; / * m * / : 3! + 5! + 7! + 9! = 368046 long, s m fact int 1. 3. 3 :,,, C, ; C, ; C if for while do-while,, C,,, 9
C 1. 4 C, C,,, C : A. ASCII C C,. c, test. c B.,, UNIX. o, MS-DOS. obj, CP / M. CRL,,,, C.,,,,, UNIX. out, MS-DOS. exe, CP/ M. com D.,,,,, 1. 1 1. 1 C : * * * * * * * * * * * * * * * * * Welcome! * * * * * * * * * * * * * * * * * 1. 2,, 1. 3 s = 1 + 3 + 5 + + 99,, C 1. 4, :,,,, 64, :! C,? ( 1m 3 1. 4210 9 ) 1. 5 e 1 + 1 1! + 1 2! + + 1 e ( n n! ),, C 10 1. 6 C
1 C 1. 7 C 1. 8 1. 1 1. 2 1. 3 1. 9 1. 1 1. 2 1. 3 1. 10 C C 11
2 ( Nikiklaus Wirth) : + =, : C, :, C : int char float double, void C, C, C 12
2 2. 1 C 2. 1. 1 C C 5 : char intfloat double void void 4 char: 1 1 ASCII int: 2 4 1, PC 2 float: 4 1 double: 8 1 void:,, 2. 1. 2 void, 4, C : signed( ) unsigned( ) long( ) short( ) 4 signed unsigned short long char int, long double 2. 1 2. 1 Turbo C ++ / bit char 8-128 127 unsigned char 8 0 255 signed char 8-128 127 char int 16-32768 32767 unsigned int 16 0 65535 unsigned signed int 16-32768 32767 int signed unsigned short int 16 0 65535 unsigned short signed short int 16-32768 32767 short long int 32-2147483648 2147483647 long signed long int 32-2147483648 2147483647 long unsigned long int 32 0 4294967295 unsigned long float 32 3. 4e - 38 3. 4e + 38 double 64 1. 7e - 308 1. 7e + 308 long double 80 3. 4e - 4932 1. 1e + 4932 13
C signed int,, int ( signed) ( signed) ( unsigned), : 0, ; 1,, short long int, 16 32, short int long int 16 32, long int - 2147483648 2147483647, int int, int PC, int short int, 2 long 4 2. 2 C,,, C 2. 2. 1 ( 1 ),, C 0 x, 0, : x = 0 xff; / * 255 * / y = 011; / * 9* /,, 2 : ( short int) ( int) ( long int) ( unsigned),, 2. 1, L U 123L, 4 123U int, 123LU long ( 2 ) C, : ( ), : 0. 32. 98 34. 34123. 0 ; e( E) 1 3, : 14 25. 1e3 25. 110 3 0. 2345 e - 6 0. 234510-6 1 e5 1. 010 5
2,, 4,,, ( bit), ( 32 64 ), 2. 2. 2 :,, z, 4, #, ( ) C ( ),, 2. 2, ASCII, 1 1, A ASCII 65, A \ 101 \ x41 A, ASCII, 65, 0x41, 0101 1, 0 255,, A 65, A + 5 70 2. 2 \0 NULL \ a BELL \ b BS \ f FF \ n NL( LF) \ r CR \ t HT ( TAB) \ v VT \ \ \ \ \ \ddd 1 3 \ xhh 1 2 2. 2. 3 C, 15
C, This is a test,,,, ASCII C NULL( \0 ), n, n + 1, HELLO 5, 6, : H E L L O \0, a, a a, 2 2. 2. 4 C,, ( ) C : const ( 1 ), : #define : #define PI 3. 1415926 PI 3. 1415926,,, #define C, ; PI ( ),, ; 3. 1415926,,,,, ( ), ( 2 ) const ( ) const, const,, : const = ; C, : const double pi = 3. 1415926; piconst ( ), const, 16
2 const :,, ;,, #define, const #define const ( ), #define, const,,, #define 2. 3 2. 3. 1 C ( 1 ) C : C,, C, count, Count, COUNT 3, ANSI ( Turbo C) 31,, 32, 33,, C 3 : 1 ) C : asm, auto, break, case, cdecl, char, const, continue, default, do, double, else, enum, extern, far, float, for, goto, huge, if, interrupt, int, long, near, pascal, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while int, int1 Int INT 2 ) C, ( printf) ( define) C,, 3 ) ( 2 ),,,, 17
C, : fmem: free memory available,,,,,,, ( ), 2. 3, selected char c cinchar disk drive; int i ireturnvalue, long l lnumrecs SelectedDiskDrive, string sz szinputstring int array ai aierrornumber,, char * psz pszinputstring, Microsoft Charles Simonyi 2. 3 Microsoft Windows,, Visual Basic Acess,,,,, Microsoft Windows,, 2. 3. 2 C,,, : ;, C, ( ) : int x, y; / * int x, y * / float average; / * float average * / unsigned int z; / * unsigned int z * / double pi; / * double pi * / long m, n; / * long m, n * / 18
2 2. 3. 3, : = ; : { int event = 5; char heat = C ; float time = 27. 5; : printf( The winning time in heat % c, heat) ; printf( of event % d was % f., event, time) ; The winning time in heat C of event 5 was 27. 5, 2. 4 2. 4. 1 C,, printf( ) : printf(, ) ;, : % d % o % x % u % c % s % f printf( ),,,, printf( ), 19
C, printf( ),,, : printf ( % s% d \ n, this is a string, 100) ; this is a string 100 printf ( this is a string % d \ n, 100) ; this is a string 100 printf ( % d is int, % f is float. \ n, 10, 110. 789) ; 10 is int, 110. 789000 is float printf ( % s \ n, HELLO ) ; HELLO 2. 4. 2 scanf( ) C, scanf( ) : scanf (, ) ;, : % d % o % x % c % s % f : & &a &x,, scanf( ), Tab 20 2. 1 scanf( ) { int a, b; printf( Enter integer a and b: ) ; scanf( % d% d, &a, &b) ; printf( a + b = % d \ n, a + b) ; printf( a - b = % d \ n, a - b) ; : Enter integer a and b: 6 3
2 a + b = 9 a - b = 3, scanf ( ) 2,, 2, a b, scanf ( ), : scanf ( % d, % d, &a, &b) ;,, : 6, 3,, & 2. 5 2. 5. 1 C + ( ) - ( ) * ( ) / ( ) % ( ), C, ++ ( ) - - ( ) ( ), C, /,, 10 /4 2 2. 5%,,% - 1,, 2. 2 #include < stdio. h > { int x, y; : printf( enter dividend and divisor: ) ; / * * / scanf( % d, % d, &x, &y) ; printf( quotient % d \ n, x / y) ; / * * / printf( remainder % d, x% y) ; / * * / enter dividend and divisor: 23, 5 quotient 4 remainder 3 2. 5. 2 C, ++ - -, 21
C, 1 1, ++ 1, - - 1 : x ++ ; ++ x; x = x + 1; x - - ; - - x; x = x - 1; ++ x x + 1, x ++ x + 1 x, - - x x - 1, x - - x - 1 x x = 100, : x ++ x x ++ x - - x x - - x 100 101 100 101 99 100 99, 2. 3 { int a = 25, b = 80, aa, bb; a ++ ; b--; : aa = ++ a; bb = --b; printf( a = % d, b = % d, aa = % d, bb = % d \n, a, b, aa, bb) ; aa = a ++ ; bb = b--; printf( a = % d, b = % d, aa = % d, bb = % d \n, a, b, aa, bb) ; a = 27, b = 78, aa = 27, bb = 78 a = 28, b = 77, aa = 27, bb = 78, C, C,,, 2. 5. 3,,,,,,,,,,, : c = ( ++ a% b-- + c) /d - 6* e : ++ -- % + / * - = 2. 6 22
2 2. 6. 1 C, C, : 0, 0,, 0, ; 0,,, 2. 6. 2 C 2. 4 C :,, 0 ( ) 1 ( ) 2. 4 C > b > c b c >= b >= c b c == b == c b c <= b <= c b c < b < c b c! = b! = c b c, greater ch1 = A ; ch2 = B ; if ( ch2 > ch1) printf( great than ) ; than, ASCII, B A,, > >= < <= ==! =, x + 8 > y* 10, x + 8 y* 10, 2. 6. 3, : const float first = 0. 0; const float last = 70. 0; const float small = 0. 007; if( ( first + small* 10000) == last) ( first + small* 10000) == last, 1( ),, small ( first + small* 10000 ) last, 0( ) : if( abs( first + small* 10000 - last) < 1e - 6) ( 10-6 ), 23
C 2. 7, 3 : ( &&) ( ) (! ),, 1,0 ( 2. 5 ) 2. 5 p q p && q pq! p 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0 : 10 > 5 &&! ( 10 < 9) 3 >= 4 C, :! ( ), &&( ), ( ) : a + b > cd &&! e :! + > &&! ( 10 < 9), 1, 10 > 5&&! ( 10 < 9), 1, 3 <= 4, 1( ), if, if 1 100 #include < stdio. h > { int i; for ( i = 1; i <= 100; i ++ ) if (! ( i% 2) ) printf( % d, i) ; /* i, i% 2 0,! ( i% 2) 1 * / 2. 8 2. 8. 1 C, C, C : = 24 =, ( ) : a = c ++ ; / * * / b = x* y + 5; / * * /
2 e = a > b; / * * /,,, ;,, C : x = y = z = 2; x = ( y = ( z = 2) ) ; z = 2, 2 x, z = 2 2, y, y = z = 2 2, x, 2, : f = a = b + 3; / * f = ( a = b + 3) ; * /, C : if ( ( product = x* y) < 0) else printf( one number is negative \ n ) ; printf( positive product is: % d, product ) ; if, x* y product, 0, 1( ) 0( ) 2. 6 + = 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 2. 8. 2 C, 2. 6,, 2. 8. 3, C 2. 7 25
C 2. 7 ( ) ( ) int char 8, 8 0 char int int 8 char, int long int int float, double float double double double float float double 2. 9 : C, 3 1? 2: 3 (? ) ( : ) : 1, 0( ), 2 ; 0 ( ), 3 : x = ( a > b)? a: b; / * a b x * / y = ( x >= 0)? x: - x; / * x * / c = ( c >= a&& c <= z )? c - 32: c; / * c * / t? printf( % d, t) : printf( zero entered ) ; / * 0, printf( ), zero entered ;, * /,, : sgn = ( x > 0)? 1: ( x < 0? - 1: 0) /* x, 1, - 1, 0 0 * / 2. 10 2. 10. 1, :,, int x = 3, y = 5; float z = 10. 0, a; a = x /y* z 26
2 a 6. 0 0. 0 x / y x y, x / y 0. 6 0. 6 0. 0, C : char short unsigned short long unsigned long float double long double,, 2. 7, 2. 10. 2,, : char ch; int i; long l; float f; double d; : result= ( ch i ) + ( f * d ) - ( f + i ) ; int double double int double double double double double result float, float resultresult int, int result 2. 10. 2 : ( ), C,, : ( int) x % n / * x int, * / ( int) sqrt( ( double) n) / * n double, sqrt int * /, ( int) x / y ( int) ( x /y), x, x / y,, : 27
C #include < stdio. h > { int i = 3; int j = 4; float f1 = i / j; float f2 = ( float) i / j; printf( 3 /4 = % f or % f depending on the type used. \ n, f1, f2) ; f1 i j,, 0 f2 i j, ( float) i float,, 0. 75,,, : #include < stdio. h > { int i; for( i = 1; i <= 100; ++ i) printf( % d /3 is : % f\ n, i, ( float) i /3) ; ( float),,,, void *, void, malloc ( ) student : struct student * p; p = ( struct student * ) malloc( sizeof( struct student) ) ; 2. 11, : 1, 2,, n, :, : x = ( y = 3, y + 1) ; 28
2, y 3, y + 1, 4, 4 ( y = 3, y + 1) x : printf( % d \n, ( 3 + 5, 15) ) ; / * 3 + 5, 15 15 * / printf( % d, % d \ n, 3 + 5, 15) ; / * 3 + 5 15 * /, 2. 12 2. 8 C 9,,, : ( ) ( * / % ) ( + - ) ( ) ( < <= > >= ) (! = == ) ( & ) ( && ) (? : ) ( ), C,, 2. 8 C 1 ( ) [ ] - >. 2! - ++ - - * & sizeof 3 * / % 4 + - 5 6 < <= > >= 7! = == 8 & 9 10 11 && 12 13?: 14 = + = - = * = / = % = & = =! = = = 15,, : a = b + c /d / fun( ) * 5,, : 29
C a = b + ( ( ( c / d) / fun( ) ) * 5),, 2. 13 C /*, * / ANSI C,, : / * this is / * an error * / * / Turbo C,, C,,,,,,, ;,, ( ),,,,,,,, 2. 14 C C C,, C, 2. 14. 1 : int a = 3, x; x = a +++ a ++ ; :, a ++ 3, a 4, a ++ 4, a 5, x 3 + 4 = 7, 6, a ++, a x = a - - + a ++ ; y = x +++ x; y = a +++++ a; z = ++ a +++ a +++ a; x = a +++ a ++ ; : x = a ++ ; x = x + a ++ ; 30 int i = 3; printf( i = % d, i ++ = % d, ++ i = % d \ n, i, i ++, ++ i) ; :,, : i = 3, i ++ = 3, ++ i = 5, :
2 i = 5, i ++ = 4, ++ i = 4? PC,,,! 2. 14. 2 C, : a&&b&&c a b c a 0, b c,, a b, a 0, b c,, a b,,,,, : ( a = 5 + 3) ++ b / * 1, a 8, b, 1 * / ( a = 5 + 3) && a > 0++ b / * 1, 1, b, 1 * / ( a = 5 + 3) && a < 0 && ++ b / * a < 0 0, 0, b, 1 * /, ( ),, ++ b, : ( a = 5 + 3) b, b 2. 1 : ( 1 ) x + y% 3* ( int) ( x + y) % 3 /7, x = 4. 2, y = 2. 5 ( 2 ) ( float) ( a + b) /5 + ( int) b% ( int) a, a = 3, b = 4 ( 3 ) a + = a, a - = 2, a* = 2 + 3, a = 12 2. 2 : ( 1 ) short a = 1 -. 1e - 1; ( 2 ) double b = 1 + 5 e2. 5 ; ( 3 ) long do = 0xfdaL; ( 4 ) float 2 and = 1 - e - 3 2. 3 : ( 1) #include < stdio. h > { double d; float f; long l; int i; i = f = l = d = 20 / 3; printf( % d % ld % f % f\ n, i, l, f, d) ; ( 2) #include < stdio. h > { int i, j, m, n, s; i = 5; j = 10; m = ++ i; 31
C 32 n = j ++ ; s = m ++ - n; printf( i = % d \ tj = % d \ tm = % d \ tn = % d \ ts = % d \n, i, j, m, n, s) ; ( 3) #include < stdio. h > { char c1, c2; int i, j; c1 = a ; c2 = b ; printf( c1 = % c \t% d \ t% o\ t% x\ n, c1, c1, c1, c1) ; printf( c2 = % c \t% d \ t% o\ t% x\ n, c2, c2, c2, c2) ; ( 4) #include < stdio. h > { int x, y, z; x = y = z; z = 3; y = x ++- 1; printf( % d \t% d \ t, x, y) ; y = ++ x - 1; printf( % d \t% d \ n, x, y) ; y = z - - + 1; printf( % d \t% d \ t, z, y) ; y = - - z + 1; printf( % d \t% d \ n, z, y) ; 2. 4 ( 1) #include stdio. h ; { float r, s; printf( input the radius: \ n) ; scanf( % f, r) ; ( 2) #include stdio. h s = 3. 14159* r* r; printf( % f\ n, s) ; { int a, b, c, v; / * / * a, b, c are sides* /, / * v is volume of cube* /* / a = 2. 0; b = 3. 0; c = 4. 0 v = a* b* c; printf( % d \ n, v) ; 2. 5 :
2 C = 5 9 ( f - 32 ), : F 30, 60, 90, 120 2. 6 2. 7, 2. 8, a b, 2. 9, a, b, c,, 2. 10,, 3
3,,, C,, C 3. 1 C 3. 1. 1 C C C 3 ( 1 ) C, : x = 0; /* * / i ++ ; /* * / printf( ) ; /* * /, ( 2 ), : 1 ), : : if else switch( ) : while while( ) 34
3 do do while( ) for for( ) 2 ), : continue switch break return goto ( 3 ),,,, ( 4 ) C, {,,, : if ( x < y) { int t; / * t * / t = x; / * t, x y * / x = y; y = t; x y,, :,,,, 2 3. 1. 2 3, 3, 1966, Bohm Jacopini 3, 3 3. 1 3,, ;,,,,, C, 3 C ; if switch; while, for do while 35
C 杂员 再 条件 晕 条件 晕 杂圆 杂员 杂圆 杂员 再 顺序结构选择结构循环结构 3. 1 3 3. 2 3. 2. 1 if ( 1 ) else if else if : if ( ) ;, else if 3. 2,,, if ;, if if,, : 0 3. 2 else if 3. 1,, #include < stdio. h > { int choice, value; printf (" Convert: \ n" ) ; / * * / printf (" 1: decimal to hexadecimal \ n " ) ; 36 0
3 printf( " 2: hexadecimal to decimal \ n " ) ; printf( " 3: decimal to octal \ n " ) ; printf( " 4: octal to decimal \ n" ) ; printf( " enter your choice: " ) ; scanf( " % d", &choice) ; / * * / if ( choice == 1) / * 1 * / { printf( " enter a decimal value: " ) ; scanf( " % d", &value) ; printf( " % d in hexadecimal is: % x\ n ", value, value) ; if ( choice == 2) / * 2 * / { printf( " enter a hexadecimal value: " ) ; scanf( " % x", &value) ; printf( " % x in decimal is: % d \ n ", value, value) ; if ( choice == 3) / * 3 * / { printf( " enter a decimal value: " ) ; scanf( " % d", &value) ; printf( " % d in octal is: % o\ n ", value, value) ; if ( choice == 4) / * 4 * / { printf( " enter a octal value: " ) ; scanf( " % o", &value) ; printf( " % o in decimal is: % d \ n ", value, value) ; : Convert: 1: decimal to hexadecimal 2: hexadecimal to decimal 3: decimal to octal 4: octal to decimal enter your choice: 3 enter a decimal value: 28 in octal is: 34 if,,, : if ( 2 ) else if else if, if : 37
C if ( ) 1; else 2; if 3. 3,,, if 1;, else, else 2 : if, else, if else, 3. 2, #include < stdio. h > { int x; printf( " enter a integer number: " ) ; / * * / scanf( " % d", &x) ; if ( x% 2 == 0) printf( " The number is even \ n " ) ; / * x 2, * / else printf( " The number is odd number \ n " ) ; / * x 2, * / 3. 3 0, ( ) if #include < stdio. h > { int a, b; printf (" enter two number: " ) ; scanf (" % d% d", &a, &b) ; if( b) printf (" % f \ n", ( float) a /b) ; / * b 0, float * / else printf (" cannot divide by zero\ n" ) ; / * b 0, * /, if( b) if ( b! = 0) ( 3 ) if if if if, if if else if : if ( x) 38
3 if ( y) printf( " 1" ) ; else printf( " 2" ) ; if else? C : else else if,, else if ( y) else if( x), if : if ( x) { if ( y) printf( " 1" ) ; else printf( " 2" ) ; else if( x), if( y) if if_else_if, if_else_if, if,, if, if_else_if, if, 3. 4 #include < stdio. h > { int choice; int value; printf( " Convert: \ n" ) ; printf( " 1: decimal to hexadecimal \ n " ) ; printf( " 2: hexadecimal to decimal \ n " ) ; printf( " 3: decimal to octal \ n " ) ; printf( " 4: octal to decimal \ n" ) ; printf( " enter your choice: " ) ; scanf( " % d", &choice) ; if ( choice == 1) { printf( " enter decimal value: " ) ; scanf( " % d", &value) ; printf( " % d in hexadecimal is: % x\ n ", value, value) ; else if ( choice == 2) { printf( " enter hexadecimal value: " ) ; scanf( " % x", &value) ; printf( " % x in decimal is: % d \ n ", value, value) ; else if ( choice == 3) { printf( " enter decimal value: " ) ; scanf( " % d", &value) ; printf( " % d in octal is: % o\ n ", value, value) ; 39
C else if ( choice == 4) { printf( " enter octal value: " ) ; scanf( " % o", &value) ; printf( " % o in decimal is: % d \ n ", value, value) ; 3. 2. 2 switch if_else_if,,, C, switch ( 1 ) switch switch : switch( ) { case 1: 1 case 2: 2 case n: default: n n + 1 switch, case,, case ; case,, switch, default n + 1 default, default,,,, 3 + 5, 4 * 8 switch 3. 4 1 2 n default 1 2 1 n+ 1 40 3. 4 switch
3 switch : if : switch, if switch, case, switch, switch switch switch, switch if_else_if 3. 5 switch #include < stdio. h > { int choice; int value; printf( " Convert: \ n" ) ; /* * / printf( " 1: decimal to hexadecimal \ n " ) ; printf( " 2: hexadecimal to decimal \ n " ) ; printf( " 3: decimal to octal \ n " ) ; printf( " 4: octal to decimal \ n" ) ; printf( " enter your choice: " ) ; scanf( " % d", &choice) ; switch ( choice) { case 1: / * 1 * / printf( " enter decimal value: " ) ; scanf( " % d", &value) ; printf( " % d in hexadecimal is: % x\ n", value, value) ; break; case 2: / * 2 * / printf( " enter hexadecimal value: " ) ; scanf( " % x", &value) ; printf( " % x in decimal is: % d \ n", value, value) ; break; case 3: / * 3 * / printf( " enter decimal value: " ) ; scanf( " % d", &value) ; printf( " % d in octal is: % o \ n", value, value) ; break; case 4: / * 4 * / printf( " enter octal value: " ) ; scanf( " % o", &value) ; printf( " % o in decimal is: % d \ n", value, value) ; break; 41
C switch break, switch, switch ( 2 ) default switch default,, switch default : switch( ch) { case Y : case y : printf( " You answered YES! \ n" ) ; break; case N : case n : printf( " You answered NO! \n" ) ; break; switch,?,, default, : default: printf( " Unknown reponse: % d \n", ch) ;,, default default switch,, default, : default: printf( " invalid selection, try again \ n" ) ; ( 3 ) break 3. 5, break switch switch case,, break, switch switch, 1,..., n, n + 1 break, switch 3. 5 1 2 n default 2 n n+ 1 1 3. 5 break switch switch break, switch break, :,, break, break, break,, case break 42
3, switch, case, break : switch( country) { case China : case Japan : case India : printf( " Asia" ) ; break; case Britan : case France : case German : printf( " Europe" ) ; break; ( 4 ) switch switch switch, switch switch, switch case, : switch( x) { case 1: switch( y) { case 0: printf( " divide by zero error" ) ; break; case 2: 3. 2. 3 break; case 1: process( x, y) ; break; 3. 6 ax 2 + bx + c = 0,, : b = 0 c = 0 a = 0 c0 b0 x = - c / b a0 d = b 2-4ac d0 d < 0 : #include < stdio. h > #include < math. h > 43
C { float a, b, c, d, x1, x2, real, imag; printf( " please input a, b, c = " ) ; / * 3 * / scanf( " % f, % f, % f", &a, &b, &c) ; if ( fabs( a) < = 1e - 6) / * a = 0 * / { if ( fabs( b) < = 1e - 6) printf( " error \ n" ) ; / * b = 0 * / else / * b0 * / printf( " The equation has a root: % 6. 2f \n", - c / b) ; else / * a0 * / { d = b* b - 4* a* c; if ( fabs( d) < = 1e - 6) / * d = 0 * / printf( " The equation has two equal roots: % 6. 2f \n", - b / ( 2* a) ) ; else if ( d > 1e - 6) / * d > 0 x1 x2 * / { x1 = ( - b + sqrt( d) ) / ( 2* a) ; x2 = ( - b - sqrt( d) ) / ( 2* a) ; printf( " distinct real roots: % 6. 2f and % 6. 2f \ n", x1, x2) ; else / * d < 0 * / { real = - b / ( 2* a) ; / * * / imag = sqrt( - d) / ( 2* a) ; / * * / printf (" The equation has complex roots: \n) ; / * a + bi * / printf (" % 6. 2f + % 6. 2fi \ n", real, imag) ; printf (" % 6. 2f - % 6. 2fi \ n", real, imag) ; if-else, if-else : ( ) 0, : if ( d == 0)... 0, 0 fabs ( d ) ( 1 e - 6),, d = 0 3. 7 y = : x ( 0x < 10) 5 x + 3 ( 10x < 20) 3 x - 11 ( 20x < 30) 2 x + 7 ( 30x < 40) 6 x ( x40 ),, 10 ( 0, 10, 20, 30), 10, : 44
3 x 0 10 20 30 40 x /10 0 1 2 3 4, switch #include < stdio. h > { int x, y, m; printf( " Please input x: " ) ; scanf( " % d", &x) ; if ( x > = 40) m = 4; else m = x /10; switch( m) { case 0: y = x; break; case 1: y = 5* x + 3; break; case 2: y = 3* x - 11; break; case 3: y = 2* x + 7; break; case 4: y = 6* x ; break; default: printf( " The input data is invalid. \ n" ) ; printf( " The result is % d \ n", y) ; 3. 3,,, C C for while do_while 3. 3. 1 for :,, 50, 100,, for ( 1 ) for for : for( 1; 2; 3 ) ; 3,, for 1,, ; 2,, 2 45
C ; 3,,, 3 for 3. 6 3. 8 #include < stdio. h > { int x; 1 100 for( x = 1; x < = 100; x ++ ) printf( " % d ", x) ;, x 1x 100, printf( ) printf( ), x 1, x 1 00 x 1 0 0, 1 2 0 3 for, x, x 1 3. 6 for x < = 100, x,, 100 1 : #include < stdio. h > { int x; for( x = 100; x > 0; x - - ) printf( " % d", x) ;, for 0 99 : #include < stdio. h > { int i; for( i = 0; i < 100; i ++ ) { printf( " this is i: % d", i) ; printf( " and i square: % d \ n", i* i) ; for,, : x = 10; for ( y = 10; y! = x; ++ y) printf( " % d", y) ; printf( " % d", y) ;, x y,,, y 10, 10 46 0
3 ( 2 ) for for, 1 ) for,, x y : #include < stdio. h > { int x, y; for ( x = 0, y = 0; x + y < 100; ++ x, y ++ ) printf( " % d", x + y) ; 0 98, x y 1 2 ) for for, 2,, 3,, for 3,, 10: for ( x = 0; x! = 10; ) scanf( " % d", &x) ; 3 ) for, 3, : for ( ; ; ) printf( " this loop will run forever \ n" ) ;,, : for ( ; ; ) if ( scanf( " % d", &x) ) / *, * / printf( " if enter number then this loop will run forever \ n" ) ; else /*, scanf( ), 0, * / break; 4 ) for for, for : for ( t = 0; t < time; t ++ ) ; / * * /, time 3. 3. 2 while ( 1 ) while while : while ( ) ; 47
C,,,, for, while,, while 3. 7,, A: wait( ) { char ch; ch = \0 ; / * ch * / while ( ch! = A ) ch = getchar( ) ; while, ch A, ch,,,, A, ch!, = A while, ch,, ch A while, while : while( ( ch = getchar( ) )! ( 2 ) while = A ) ; 3. 7 while, while, for, while?, while 3. 9 #include < stdio. h > { int count = 0; printf( " type in a phrase: \ n" ) ; while ( getchar( )! count ++ ; = \r ) / * * / printf( " \ ncharacter count is % d", count) ; : type in a phrase: cat character count is 3, while for?,,, 48 测试表达式 非园 循环体 憎澡蚤造藻语句的后继语句 0
3, while while : ( getchar( )! = \ r ) getchar( ),,,, \ r, \ r, while,, ( 3 ) while while, getchar( ), while ch getchar( ) : #include < stdio. h > { int count = - 1; char ch; printf( " type in a phrase: \ n" ) ; ch = getchar( ) ; while ( ch! = \ r ) { ch = getchar( ) ; count ++ ; printf( " \ ncharacter count is % d", count) ; while,?, ch ch, while, getchar( ) while, C while( 1 ) {,, : while( 1) { ch = getchar( ) ; printf( " \ nthe code for % c id % d. \ n", ch, ch) ;,, : if ( ) break; 非园 循环体 测试表达式 0 憎澡蚤造藻语句的后继语句 3. 8 do while 49
C 3. 3. 3 do while ( 1 ) do while do{ ; while ( ) ;,, while do while do while 3. 8 do, do while do, ; while,, do while for while,,, ;, do while do while, 100 do{ scanf( " % d", &num) ; while( num > 100) ; ( 2 ) do_while do?,, do while while, while,, do while,, do while, do while : do{ printf( " convert: \ n" ) ; printf( " 1: decimal to hexadecimal \ n " ) ; printf( " 2: hexadecimal to decimal \ n " ) ; printf( " 3: decimal to octal \ n " ) ; printf( " 4: octal to decimal \ n" ) ; printf( " enter your choice: " ) ; scanf( " % d", &choice) ; while( choice < 1 choice > 4) ;,, 100 : { int count = 1; 50
3 int total = 0; do{ total + = count; count + = 2; while( count < 100) ; printf( " total = % d \ n", total) ; 3. 3. 4 ( 1 ) while, a e : #include " stdio. h" #include " conio. h" / * clrscr( ) * / { char ch1, ch2; while ( ( ch1 = getchar( ) )! = EOF) / * ch1, ctrl + z * / { clrscr( ) ; / *, * / printf( " \ ntype in a letter from a to e : \ n" ) ; while ( ( ch2 = getchar( ) )! = ch1) / *, * / { printf( " \ nsorry, % c is incorrect. \ n", ch) ; / * * / printf( " \ ntry again. \ n" ) ; printf( " \ nthat s it! \ n" ) ; / *, * / while EOF( stdio. h ),, ;,,, ( 2 ) C,,, {,,, { Kernighan Ritchie, Allman, Whitesmiths 3 3 1) K&R C ( The C Programming Language), Brian Kernighan Dennis Ritchie, : 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; 51
C printf( " % c", c) ; K&R, {,, 2) Allman Allman : 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) ; Allman, {, { 3) Whitesmiths Whitesmiths : 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) ; Allman, Whitesmiths {,,, 3. 3. 5 break continue ( 1 ) break break : break; break switch switch break switch, break,, 52 3. 10 s = 1 2 + 2 2 + 3 3 + + n 2 1000 n
3 #include < stdio. h > { int i = 1, s = 0; while( 1) { s + = i* i; if ( s > 1000) / * s, s > 1000 * / break; printf( " n = % d \ n", i - 1) ;,, break ( 2 ) continue continue : continue; continue,, continue 3. 9 3. 11 100 3, 7 #include < stdio. h > { int s; for( s = 1; s < = 1000; s ++ ) if ( s% 3 == 0) / * 3 * / if( s% 7 == 0) / * 7 * / continue; / *, * / else /* 3, 7 * / printf( " % d ", s) ; continue break continue, break 3. 3. 6 3. 9 3. 12 12345, 54321 { int num, c; printf( " input an integer: " ) ; / * * / scanf( " % d", &num) ; 0 测试表达式非园 continue 憎澡蚤造藻语句的后继语句 continue do { c = num% 10; / * 10,, num * / printf( " % d", c) ; / * * / 53
C while( ( num / = 10) > 0) ; / * num, 10 * / printf( " \ n" ) ; 3. 13 1000 1 { int m, n, flag; /* flag, 1, 0 * / printf( " % d ", 2) ; /* 2 * / for( m = 3; m < = 1000; m ++ ) /* m 3 1000 * / { flag = 1; /* flag 1, m * / for( n = 2; n < m; n ++ ) /* n, 2 m - 1 * / if( m% n == 0) /* n m, * / { flag = 0; break; if( flag == 1) printf( " % d", m) ; / *, * / m, m : break n m, flag : 3 1000 ;, n, n sqrt( m) m - 1,, #include " math. h" / * sqrt( ) * / { int m, n, flag; /* flag, 1, 0 * / printf( " % d", 2) ; /* 2 * / for( m = 3; m < = 1000; m + = 2) /* m 3 1000 * / { flag = 1; /* flag 1, m * / for( n = 2; n < sqrt( m) ; n ++ ) / * n, 2 sqrt( m) * / if( m% n == 0) /* n m, * / { flag = 0; break; if( flag == 1) printf( " % d", m) ; / *, * / 3. 4 goto 3. 4. 1 goto goto C,, goto, 1 100, goto : 54
3 x = 1; loop1: x ++ ; if ( x < 100) goto loop1; goto : for(... ) { for(... ) { while(... ) { if(... ) goto stop; stop: printf( " error in program. \ n) ; break,,, goto, 3. 4. 2 goto goto,,, goto, goto,, goto 3. 5 3. 14, 81, 9 :, : 144( 12* 12) 676( 26* 26), #include < stdio. h > { int n, k, a, b, c; for ( k = 1; ; k ++ ) / * * / { n = k* k; if ( n < 100) continue; / * n, * / if ( n > 999) break; / * n, * / a = n / 100; / * a * / b = ( n - a* 100) /10; / * b * / c = n% 10; / * c * / if (! ( a - b) * ( a - c) * ( b - c) ) / * * / printf( " n = % d = % d* % d \ n", n, k, k) ; 55
C 3. 15 s = 1 - #include < stdio. h > { float s = 0. 0, w, f = - 1. 0; int i; printf( " please input n: " ) ; scanf( " % d", &n) ; for ( i = 0; i < = n; i ++ ) { 3. 16 1 3 + 1 5-1 7 + + 1 2n - 1 f = ( - 1. 0) * f; / * f * / w = f / ( 2* i + 1) ; / * w * / s + = w; printf( " the result is % d. ", s) ; : x sin( x) = 0 x0 = 1, 10-6 x n + 1 = x n - f( x n ) f ( x n ), x n + 1 f( x) = 0 f( x) = x sin( x), f ( x) = sin( x) + xcos( x) #include < stdio. h > #include < math. h > { float x1, x0 = 1, f, f1; do{ x1 = x0; /* * / f = x0* sin( x0) ; / * f( x) * / f1 = sin( x0) + x0* cos( x0) ; / * f ( x) * / x1 = x0 - f/ f1; / * * / while( fabs( x1 - x0) > 1e - 6) / * * / printf( " The result is % f6. 2. ", x1) ; 3. 1, 3. 2, 3. 3, 3. 4 : 56
3 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 3. 5, 3 2, 5 3, 7 2,? 3. 6 e = 1 1! + 1 2! + + 1 n!, 10-8 3. 7 1 10 3. 8 100 3 6 3. 9,,, 3. 10 n, 57
4 C, C,, 4. 1 4. 1. 1 ( 1 ), : [ ] ;,, ;,, s 10 50 name : int s[ 10] ; char name[ 50] ; ( 2 ),,?, : [ ], s[ 5] name[ 33] C, 0, s 9, 10, s[ 0 ] s [ 1 ] s [ 2 ]... s[ 9 ] name 49, 50, name[ 0 ] name[ 49] 58
4 :, ( 3 ), C,, s, s 4. 1 s[ 0] s[ 1] s[ 2] s[ 3] s[ 4] s[ 5] s[ 6] s[ 7] s[ 8] s[ 9] 4. 1 s 4. 1. 2 ( 1 ) C,, : [ ] = { ;,,,,, : int s[ 10] = {0, 2, 4, 6, 8, 9, 7, 5, 3, 1 ; 4. 1, #include < stdio. h > #define LIM 5 int table[ LIM] = {50, 20, 10, 5, 1 ; /* table * / { int dex, amount, quantity; printf( " : " ) ; scanf( " % d", &amount) ; for ( dex = 0; dex < LIM; dex ++ ) { quantity = amount /table[ dex] ; printf( " = % 2d, ", table[ dex] ) ; printf( " = % 2d \ n", quantity) ; amount = amount% table[ dex] ; : : 138 = 50, = 2 = 20, = 1 = 10, = 1 = 5, = 1 = 1, = 3 59
C 1. 38 2, 1, 1, 1 3 : 50 ( amount ), 50 ( ) ;, ( % ) amount, 20 5,, : int table[ LIM] = { 50, 20, 10, 5, 1 ; {,, table [ 0 ] 50, table[ 1 ] 20,, table[ 4] 1 ( 2 ) LIM ( #define 5) : int table[ LIM] = {50, 20, 10, 5, 1 ;,,?, 0 ; : int s[ 10] = {0, 2, 4, 6, 8; s[ 5] s[ 9] 0 : int table[ ] = {50, 20, 10, 5, 1; table? :,,,,, 4. 1. 3 60 4. 2 #include < stdio. h > #define N 20 / * 20 * / { int i, max, min, s[ N] ; for( i = 0; i < N; i ++ ) scanf( " % d", &s[ i] ) ; / * s * / max = min = s[ 0] ; / * 1 * / for( i = 1; i < N - 1; i ++ ) / *, * / if ( s[ i] > max) max = s[ i] ; else if ( s[ i] < min) min = s[ i] ; printf( " max = % d, min = % d \ n", max, min) ;
4 4. 3 fabonacci 20 1, 2 : 1, 1, fabonacci : 1, 1, 2, 3, 5, 8, 13, 21, { int i; int f[ 20] = {1, 1 ; /* f, 1 * / for ( i = 2; i < 20; i ++ ) / * 3, * / f[ i] = f[ i - 1] + f[ i - 2] ; for ( i = 0; i < 20; i ++ ) / * * / { if( i% 5) printf( " \ n" ) ; / * 5 * / printf( " % 12d", f[ i] ) ;,, for, 1 1, 4. 2 4. 2. 1 ( 1 ) C,, \0 ( 0), 1, 10, : char str[ 11] ;,, \0, ( 2 ) 1 ),, : char s1[ 10] = { H, e, l, l, o, \0 ;, s1 4. 2 61
C S1 0 1 2 3 4 5 6 7 8 9 H e 1 1 o \0 \ 0 \0 \0 \0 : 2 ) 4. 2 s1 C,, char s1[ 6] = " Hello" ;, \ 0,,, : char s2[ ] = " This is a string" ; s2 17 4. 2. 2,?, scanf( ), : { char name[ 15] ; printf( " Enter your name: " ) ; scanf( " % s", name) ; printf( " Greetings, % s. \ n", name) ; : Enter your name: Mary Greetings, Mary. scanf( ) name \0 : for ( i = 0; i < 5; i ++ ) scanf( " % c", &name[ i] ) ; : name[ 0] = M ; name[ 1] = a ; name[ 2] = r ; name[ 3] = y ; name[ 4] = \ 0 ;, : name = Mary ;, name,, : char name[ 15] = Mary, name1[ 15] ; name1 = name; 4. 2. 3 62 C,
4 ( 1 ) gets( ) gets( ) : gets( ) ;, gets( ),, gets( ) stdio. h, #include < stdio. h > { char str[ 80] ; printf( " enter a string: " ) ; gets( str) ; printf( " % s \ n", str) ; str printf( ), : gets( ),,, gets( ), ( 2 ) C : strcpy ( ) strcat( ) strlen( ) strcmp ( ), : string. h 1 ) strcpy( ) strcpy( ) : strcpy( to, from) strcpy( ) from to to from, hellostr : #include < stdio. h > #include < string. h > { char str[ 80] ; strcpy( str, " hello" ) ; printf( " % s", str) ; 2 ) strcat( ) strcat( ) : 63
C strcat( s1, s2) ; strcat( ) s2 s1, s2, hello there : #include < stdio. h > #include < string. h > { char s1[ 20], s2[ 10] ; strcpy( s1, " hello" ) ; strcpy( s2, " there" ) ; strcat( s1, s2) ; printf( " % s", s1) ;, s1, s1 s2, 3 ) strlen( ) strlen( ) : strlen( s) ; s strlen( s) s : #include < stdio. h > #include < string. h > { char str[ 80] ; printf( " enter a string: " ) ; gets( str) ; printf( " % d", strlen( str) ) ;, hi, there, 8 strlen( ) 4 ) strcmp( ) strcmp( ) : strcmp( s1, s2) ; strcmp( ), 0, s1 s2, ; s1 s2, flag 64 4. 4, { char s[ 80], password[ ] = A123 ; / * password * / int flag = 0, n = 3; / * flag, 0, n * / do{ printf( " enter password: " ) ;
4 gets( s) ; / * * / if (! ( strcmp( s, password) ) / * * / { printf( " OK! \ n" ) ; flag = 1; / * flag 1 * / break; / * * / else if( n > 0) / * * / { printf( " password error, input again! \ n" ) ; while( n > 0) ; if(! flag) n - - ; / * * / { printf( " sorry, you is nt valid user, goodbye! \ n" ) 4. 2. 4 4. 5 2, binary, #include < stdio. h > #include < math. h > #define M 16 { char ch, binary[ M] ; int i, j, dec, ndec, index = 0; / * index binary * / printf( " : " ) ; scanf( " % d", &dec) ; ndec = abs( dec) ; / * dec * / while( dec) { str[ index ++ ] = dec% 2 + 0 ; / * * / dec / = 2; binary[ index - - ] = \0 ; / *, index \0 * / for ( i = 0, j = index; i < j; i ++, j - - ) /* * / { ch = binary[ i] ; binary[ i] = binary[ j] ; binary[ j] = ch; printf( " % d : % s \ n", dec, str) ; : 57 57 : 111001 4. 6, ( ) 65
C #include < stdio. h > main ( ) { int c, i, nwhite, nother, ndigit[ 10] ; / * : nwhite, ndigit[ 10] ndigit[ 0] ndigit[ 9] 0 9, nother* / nwhite = nother = 0; / * 0 * / for ( i = 0; i < 10; ++ i) ndigit[ i] = 0; while( ( c = getchar( ) )! { switch( c) = EOF) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : case 8 : case 9 : ++ ndigit[ c - 0 ] ; / * * / break; case : case \ n : case \ t : ++ nwhite; / * * / break; default: printf( " digits = " ) ; ++ nother; / * * / break for ( i = 0; i < 10; ++ i) printf( " % d", ndigit[ i] ) ; printf( ", white space = % d, other = % d \n", nwhite, nother) ;, int ndigit[ 10] 10 ndigit : if ( c > = 0&& c < = 9 ) c, : c - ndigit 0, 4. 3 C,, 4. 3. 1 66 :
4 [ 1] [ 2] ; [ 1] [ 2] = { ;, 1, 2, a 34( 3 4 ) int : int a[ 3] [ 4] ; 0, a 2, 3,,, ( a ) : a[ 0 ] [ 0 ] a [ 0 ] [ 1 ] a [ 0 ] [ 2 ] a [ 0 ] [ 3 ] a [ 1 ] [ 0 ] a [ 1 ] [ 1 ] a [ 1 ] [ 2 ] a[ 1 ] [ 3 ] a[ 2] [ 0] a[ 2] [ 1] a[ 2] [ 2] a[ 2] [ 3 ], ( ) ( ) a [ 3 ] [ 4] 3 a[ 0] a [ 1 ] a [ 2 ], a [ 0] a[ 1] a[ 2 ] 3 a[ 0 ] 4, a[ 0] [ 0] a[ 0] [ 1 ] a[ 0] [ 2] a[ 0] [ 3] a[ 1] 4, a[ 1] [ 0] a[ 1] [ 1 ] a[ 1] [ 2] a[ 1] [ 3], a[ 2] 4, a[ 2 ] [ 0 ] a[ 2 ] [ 1 ] a[ 2] [ 2] a[ 2] [ 3], : int a[ 3] [ 4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ; : 1 2 3 4 5 6 7 8 9 10 11 12 : int a[ ] [ 4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ; 2 : int a[ 3] [ 4] = { {1, 2, 3, 4, {5, 6, 7, 8, {9, 10, 11, 12 ; int a[ ] [ 4] = { {1, 2, 3, 4, {5, 6, 7, 8, {9, 10, 11, 12 ;, : int a[ ] [ 4] = {{1, 2, {, {0, 10, 11, 12 ; : 1 2 0 0 0 0 0 0 0 10 11 12 4. 3. 2 : 67
C [ 1] [ 2] 1, 2 4. 7 #include < stdio. h > #define M 5 #define N 4 { int i, j, data[ M] [ N] ; for ( i = 0; i < M; i ++ ) / *, i, j * / for ( j = 0; j < N; j ++ ) scanf( " % d", &data[ i] [ j] ) ; for ( i = 0; i < M; i ++ ) / *, i, j * / { for ( j = 0; j < N; j ++ ) printf( " % 8d", &data[ i] [ j] ) ; printf( " \ n" ) ; / * * /,,,, : a[ 1] [ 2] = 3; a[ 2] [ 3] = a[ 1] [ 2] + 5; 4. 3. 3 68 4. 8, : 3 1 60, 61 { char daytab[ 2] [ 13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, / * * / { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ; / * * / int year, month, day; int i, leap, dayth; /* dayth * / printf( " - - : " ) ; scanf( " % d - % d - % d", &year, &month, &day) ; leap = year% 4 == 0 && year% 100! = 0 year% 400 == 0; / * leap * / dayth = day; for( i = 1; i < month; i ++ ) dayth + = daytab[ leap] [ i] ; printf( " % d % d % d % d ", year, month, day, dayth) ;? daytab
4 leap : leap = year% 4 == 0&&year% 100! = 0 year% 400 == 0; 4 100, 400, 1900, 2000 leap 0( ) 1( ), daytab :? 4. 4 C : [ 1] [ 2] [ N] ; [ 1] [ 2] [ N] = { ;, t 223 float : float t[ 2] [ 2] [ 3] ;, 3, 2, 1, : t[ 0] [ 0] [ 0] t[ 0] [ 0] [ 1] t[ 0] [ 0] [ 2] t[ 0] [ 1] [ 0] t[ 0] [ 1] [ 1] t[ 0] [ 1] [ 2] t[ 1] [ 0] [ 0] t[ 1] [ 0] [ 1] t[ 1] [ 0] [ 2] t[ 1] [ 1] [ 0] t[ 1] [ 1] [ 1] t[ 1] [ 1] [ 2], t[ 2] [ 2] [ 3] 22 t[ 0] [ 0] t[ 0] [ 1] t[ 1] [ 0] t[ 1 ] [ 1], 3 2 t[ 0] t[ 1], 2 3,,,, 10694 : 10694 = 2 160, 4 320, ( 8 ), 34 560, 4. 5 C,,,, N, N,, C,, (,! ) { int crash[ 10] ; for ( i = 0; i < 100; i ++ ) crash[ i] = i; 69
C, crash 10, 20, 100, 200,,, C?, C, 4. 6 4. 9,,, :, ( ),, ;,,, N N - 1, #include < stdio. h > #define N 20 / * 20 * / { int i, j; int s[ N], t; for( i = 0; i < N; i ++ ) scanf( " % d", &s[ i] ) ; / * s * / for( i = 0; i < N - 1; i ++ ) / *, N - 1 * / for( j = 0; j < N - i - 1; j ++ ) / * * / if ( s[ j] > s[ j + 1] ) / *, * / { t = s[ j] ; s[ j] = s[ j + 1] ; s[ j + 1] = t; for( i = 0; i < N; i ++ ) / * * / printf( " % 10d", s[ i] ) ; 4. 10 50,, 505 mark 50, 50 average 50 : #include < stdio. h > #define M 50 #define N 5 { int mark[ M, N], i, j; float average[ N], t; printf( " : \ n" ) ; printf( " \n" ) ; for( i = 0; i < M; i ++ ) 70
4 for( j = 0; j < N; j ++ ) scanf( " % d", &mark[ i] [ j] ) ; for( i = 0; i < M; i ++ ) /* * / { t = 0; /* t * / for( j = 0; j < N; j ++ ) t + = mark[ i] [ j] ; average[ i] = t / N; printf( " \ n" ) ; printf( " " ) ; printf( " " ) ; for( i = 0; i < M; i ++ ) /* * / { for( j = 0; j < N; j ++ ) printf( " % 6d", mark[ i] [ j] ) ; printf( " % 12. 1f\ n", average[ i] ) ; printf( " " ) ; : : 84 92 76 87 66 67 82 75 91 92 80 68 80 88 94 84 92 76 87 66 80. 6 67 82 75 91 92 81. 4 80 68 80 88 94 82. 0 4. 11 100 : 2, 2 2,, 3 3 3, 5,, 50 :, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,, 97, 98, 99, 100,, 5, 7, 9, 11,, 97, 99,,, 7, 11,, 97 #include < stdio. h > #define M 100 71
C { int i, p, flag, new, s[ M] ; for( i = 2; i < = M; i ++ ) s[ i] = i; / * 2 100 s * / p = 0; do{ flag = 0; / * flag, 1 * / while (! flag) / * * / { if ( s[ p]! = - 1) / *,, * / { new = s[ p] ; flag = 1; else p ++ ; / * s[ p] - 1,, * / for( i = p + 1; i < = M; i ++ ) / * new * / if ( ( s[ i]! = - 1) &&( s[ i] % new == 0) ) s[ i] = - 1; while( new < = M/2) ; for( i = 2; i < = M; i ++ ) /* 2 100 * / if ( s[ i]! = - 1) printf( " % 4d", s[ i] ) ; 72 4. 1 ( 1) #include < stdio. h > { int a[ 10], i, k = 0; for( i = 0; i < 10; i ++ ) a[ i] = i; for( i = 0; i < 10; i ++ ) k + = a[ i] + i; printf( " % d \ n", k) ; ( 2) #include < stdio. h > { int i, j, row, col, m; int a[ 3] [ 3] == {{1, - 2, 3, {27, 36, 45, { - 56, 47, 89 ; m = a[ 0] [ 0] ; for( i = 0; i < 3; i ++ ) for( j = 0; j < 3; j ++ ) if ( a[ i] [ j] < m) { m = a[ i] [ j] ; row = i; col = j;
4 printf( " % d, % d, % d \ n", m, row, col) ; 4. 2, : AZ BY CX az by cx 1 26, i ( 26 - i + 1), 4. 3, s2 s1 strcpy, \0, \0 : 4. 4,, space space paces acesp cespa espac 4. 5, string position, character abcdefgh 5 D, abcddefgh 4. 6 strcmp,,, ASCII : abcdefg abceef, - 1 4. 7, 4. 8, 4. 9 A + B AB 4. 10 a, 3
5 C C,,, C,, C 5. 1 5. 1. 1 ( 1 ),,,,,, short a 2010 2011 2, a 2010 a, a a, 5. 1 C,,, pa, a, pa = &a; &, &a a 2010 2011 5. 1 a &a: 2010 a, a, a ; pa, a, apa a a 74
5 pa pa = &a a, pa a, a pa pa * pa, *,, 5. 2,, a 2010, a 2010,, pa s a pa ( 2 ) & * 5. 2 & *, &, : &, &a &b[ 2] *, : * pa, &( * px) px, px * ( &x) x, x, & * &a a * pa pa a 5. 3 y = x px = &x; y = * px y = x; px = &x; y = * px; 5. 4 pa = &a * pa = a, x y,,, 5. 3 px = &z, y = * px, y z x pa = &a * pa = a, pa a, a pa, * pa a; pa a, a * pa, 5. 4 3 ( 5. 5) : px, * pxpx, &pxpx &px 5. 5 px * px px * px &px 75
C ( 3 ),,,, 5. 1. 2,,, ( 1 ) : * ; : int * pc; / * pc int * / float * pa; / * pa float * / char * name; / * name char * /, : int * pc, * pd, a1, a2; float * pa, d; char * name, * addr, str[ 80] ;, C *, * *,,,,,,,, C, int pc char name, int char ( 2 ), : : : * = ; int * pa = &a, * px = &x; / * pa px, int a x * / float * s = &ss; / * s, float ss * /,, : 76 int n; int * p = &n; :
5 int n, * p = &n;, : float x, * p = &x, * q = p;, q p, x, : int * p = 0; int * p = NULL; NULL 0 stdio. h NULL : #define NULL 0 5. 2,,, 5. 2. 1 : &, : int a, * pa; pa = &a; / * pa a * /, : char c, * s1 = &c, * s2; s2 = s1; / * s1 s2 c * /, : char * str, ch[ 80] ; str = ch; / * str ch, str ch * /,, int * p1, * p2, a[ 20] ; p1 = a; p2 = p1 + 5; p1 = p2-3; p1 + = 2; p2 - = 10; 5. 2. 2 int * c, a = 20, * b = &a; float * p; : b = 2000; / * * / p = b; / * * / c = * b; c = a; / * * / 5. 1 a b, #include " stdio. h" 77
C {int a, b, * t, * a1 = &a, * a2 = &b; / * a1 a2, a1 a, a2 b * / scanf( " % d, % d", &a, &b) ; if ( a > b) / * a > b a1, a2 * / { t = a1; a1 = a2; a2 = t; / * a1 a2, a1 b, a2 a * / printf( " a = % d, b = % d \ n", a, b) ; printf( " min = % d, max = % d \ n", * a1, * a2) ; : 6, 3 a = 6, b = 3 min = 3, max = 6 5. 6 5. 1, a b, a1 a2, a1, a2,, 5. 6,,,, p : int * p, a = 5; /* p, p, p * / * p = a; /* p, * / 5. 2. 2 pa pb ( ), n, : pa + n, pa - n, pa + +, + + pa, pa - -, - - pa, pa - pb ( 1 ) n, n,, pa short, px long, pa = pa + 1, pa 2; px = px + 1, px 4, p, p n : p ( n ) 78
5, p n, C,,, 5. 7, pa a pa a[ i], pa n a[ i] n, * ( pa n) ( 2 ) (! ) pa - 4 pa - 3 pa - 2 pa - 1 pa pa + 1 pa + 2 pa + 3 pa + 4 a[ i - 4] a[ i - 3] a[ i - 2] a[ i - 1] a[ i] a[ i + 1] a[ i + 2] a[ i + 3] a[ i + 4] * ( pa - 4) * ( pa - 3) * ( pa - 2) * ( pa - 1) * pa * ( pa + 1) * ( pa + 2) * ( pa + 3) * ( pa + 4) 5. 7 pa n a,,, int pa, pa n int a a : pa = a; / * pa a * / for( i = 0; i < n; i + + ) { printf( " a[ % d] = % d \ n", i, * pa) ; / * pa * / pa + + ; / * pa * /,, 4 ( y pa ) y = * pa + + ;,, : y = * ( pa + + ), pa, y, pa y = * + + pa; y = * ( + + pa),, pa, pa ( ), y y = ( * pa) + + ;, pa, y, pa y = + + * pa; y = + + ( * pa),, pa,,, y 79
C 5. 2 #include " stdio. h" s r {char s[ ] = " Good morning! " char r[ 20], * st, * rt; st = s; rt = r; / * st rt s r * / while ( ( * rt + + = * st + + )! printf( " s string is % s \ n", s) ; printf( " r string is % s\ n", r) ; : s string is Good morning! r string is Good morning! = \0 ) ;, ( * rt + + = * st + + )! = \0 : st rt, \0 st rt,,, st rt s r,, st rt,, \0, while ( ( * rt + + = * st + + )! = \0 ) ; while ( * rt + + = * st + + ) ;, : ( * rt = * st)!? ( 3 ) C,,,,, 5. 8, p q a p q a[ 2 ] a[ 9 ], q - p 7, p - q - 7 5. 3 80 #include " stdio. h" {char str[ 30], * s; s = str; / * s str * / printf( " Enter a string( less than 30 characters) : \ n" ) ; scanf( " % s", str) ; / * * / while ( * s! = \ 0 ) s + + ; / * s * / printf( " The string length is % d \n", s - str) ; : Enter a string( less than 30 characters) : p q 5. 8 = \0, a[ 2] a[ 3] a[ 4] a[ 5] a[ 6] a[ 7] a[ 8] a[ 9] a
5 WINDOWS2000 The string length is 11, s \0 : while ( * s + +! = \0 ) ; while ( * s + + ) ;,,, s?,? 5. 2. 3, p q, p > q, p q ( ), p = = q 1, p q, p < q, p q p, p = = 0 p! = 0, p = = NULL p! = NULL 5. 3,,, 5. 3. 1 ( 1 ), int a pa, : int a[ 20], * pa; : pa = a; pa = &a[ 0] ; a, a[ 0], pa * pa a[ 0] : int a[ 20], * pa = a;, a[ 5] : pa = &a[ 5] ; * pa a[ 5] ( 2 ),, 3 : 81
C, a[ i],, C, a + i a i a[ i], * ( a + i) a[ i], A. pa = a, pa + i pa i a[ i], * ( pa + i) a[ i] B. pa = a, * ( pa + i) a[ i], C pa[ i] pa i, pa a, pa = a ( pa a ), a[ i] : 5. 4 a[ i] * ( a + i) * ( pa + i) pa[ i] #include " stdio. h" {int * p, num[ 4], i; p = num; / * * / for ( i = 0; i < 4; i + + ) * ( p + i) = i + 1; / * * / for ( i = 0; i < 4; i + + ) printf( " num[ % d] = % d \ t", i, num[ i] ) ; / * * / printf( " \ n" ) ; : 82 num[ 0] = 1 num[ 1] = 2 num[ 2] = 3 num[ 3] = 4 5. 5 #include " stdio. h" 10, data, 10 {float * p, * pend, data[ 10], max, min; * p = * pend = data; / * * / printf( " Enter 10 number: \ n" ) ; for ( i = 0; i < 10; i + + ) scanf(% f, pend ++ ) ; / * pend + +, * / / *, pend data[ 9] * / max = min = * data; / * data[ 0] * / for ( p = data + 1; p < pend; p + + ) / *, p data[ 1] data[ 9] * / if ( * p > max) max = * p; else if ( * p < min) min = * p; printf( " max = % f\ tmin = % f\ n", max, min) ;
5 : Enter 10 number: 5. 2 7. 3-1. 5 6. 6 4 3. 5 2. 7-2. 8 8. 6 10. 9 max = 10. 900000 min = - 2. 800000,,?,, ;,,,, a p b, a = p a = &b a + + a - - + + a- - a a + = n a,, ( 3 ) C,, C, : #include " stdio. h" { char str[ ] = { A, B, C, D, * pc = str, int x = 3191; /* x = 0xc77 * / long y = 2364064; / * y = 0x2412a0 * / printf( " pc[ % d] = % c \ n", 3, * ( pc + 3) ) ; printf( " pc[ % d] = % c \ n", 4, * ( pc + 4) ) ; printf( " pc[ % d] = % c \ n", 8, * ( pc + 8) ) ; 5. 9, str, 4, x ( 2 short ) y ( 4 long ), : pc[ 3] = D pc[ 4] = w pc[ 8] =,,,,,,, 5. 3. 2 pc + 0 str pc + 1 pc + 2 pc + 3 pc + 4 x pc + 5 pc + 6 y pc + 7 pc + 8 pc + 9 5. 9 01000001 01000010 01000011 01000100 01110111 00001100 10100000 00010010 00100100 00000000 a,, 5. 10,, a a[ 3 ], a;, a[ 0 ] a[ 1] a[ 2 ],, a[ 0 ] a [ 1 ] a [ 2 ], 83
C 4, a a &a[ 0] a [ 0 ] &a [ 0] [ 0 ],, a, &a[ 0 ] a[ 0 ] a[ 0], &a[ 0] [ 0] a[ 0] a[ 1] a[ 2], a[ 0] &a[ 0] [ 0], a[ 1] &a[ 1] [ 0], a[ 2 ] &a[ 2] [ 0], +, : : a[ 0 ] * ( a + 0 ) * a ; a[ i] * ( a + i) ; : ; ;, ; &a[ 1] [ 3] a [ 1 ] + 3 * ( a + 1) + 3 &a[ 2] [ 1] a [ 2 ] + 1 * ( a + 2) + 1 &a[ i] [ j] a[ i] + j * ( a + i) + j, a[ i] [ j] * ( a[ i] + j), * ( * ( a + i) + j) ; i = 0, 1, 2; j = 0, 1, 2, 3, a [ i] * ( a + i), 5. 6 #include " stdio. h" { int a[ 3] [ 4] = {1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24 ; int i, j; for ( i = 0; i < 3; i + + ) { for( j = 0; j < 4; j + + ) : a[ 0] a[ 1] a[ 2] 5. 10 printf( " % 4d", * ( * ( a + i) + j) ) ; / * a[ i] [ j] * / printf( " \n" ) ; 1 2 3 4 11 12 13 14 21 22 23 24 * ( * ( a + i) + j) a[ i] [ j] a a[ 0] [ 0] a[ 0] [ 1] a[ 0] [ 2] a[ 0] [ 3] a[ 1] [ 0] a[ 1] [ 1] a[ 1] [ 2] a[ 1] [ 3] a[ 2] [ 0] a[ 2] [ 1] a[ 2] [ 2] a[ 2] [ 3] a[ 0 ] + 0 a[ 0 ] + 0 a[ 0 ] + 1 a[ 0 ] + 1 a[ 0 ] + 2 a[ 0 ] + 2 a[ 0 ] + 3 a[ 0 ] + 3 a[ 1 ] + 0 a[ 0 ] + 4 a[ 1 ] + 1 a[ 0 ] + 5 a[ 1 ] + 2 a[ 0 ] + 6 a[ 1 ] + 3 a[ 0 ] + 7 a[ 2 ] + 0 a[ 0 ] + 8 a[ 2 ] + 1 a[ 0 ] + 9 a[ 2 ] + 2 a[ 0] + 10 a[ 2 ] + 3 a[ 0] + 11 a, ( ), &a[ 1 ] [ 1] a[ 0 ] + 5 &a[ 0] [ 0] + 5, a + 5, 84
5, * ( a + 5) a[ 5], 5. 7 #include " stdio. h" { int a[ 3] [ 4] = {1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24 ; int * p; p = a[ 0] ; while( p < a[ 0] + 12) printf( ( i% 4)? " % 4d" : " \ n% 4d", * p ++ ) ; 5. 6,,, * p + +, p = a[ 0 ] ; p = a;??,, * ( a[ i] + j) * ( * ( a + i) + j) a[ i] [ j] p, ( * p) [ i] 5. 8 #include " stdio. h" { int a[ 3] [ 4] = {1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24 ; int ( * p) [ 4], i, j; / * p 4 * / p = a; / * p * / printf( " Enter i, j: " ) ; scanf( " % d, % d", &i, &j) ; printf( " a[ % d, % d] = % d \ n", i, j, ( * p + i) [ j] ) ; : Enter i, j: 2, 3 a[ 2, 3] = 24 t[ 3] [ 4] [ 5], t[ 0] t[ 1] t[ 2 ], 4, 5 ;, t [ 0] t[ 0] [ 0] t[ 0] [ 1] t[ 0] [ 2] t[ 0] [ 3], t[ i] [ j] * ( t[ i] + j), t[ i] [ j] [ k] * ( * ( t[ i] + j) + k) * ( * ( * ( t + i) + j) + k), i = 0, 1, 2; j = 0, 1, 2, 3; k = 0, 1, 2, 3, 4 5. 4 char, C,, 85
C 5. 4. 1 3 ( 1 ) \0, 5. 9 #include " stdio. h" { char s[ ] = " Good morning! " ; char * p; p = s; /* p s * / printf( " s string is % s\ n", s) ; printf( " ( 1) p string is % s \ n", p) ; p = s + 5; /* p s 5 m * / printf( " ( 2) p string is % s \ n", p) ; : s string is Good morning! ( 1) p string is Good morning! ( 2) p string is morning! % s p s, s ; p s, ( 2 ), : char * str = " I am a student. " ;,, ( 3 ) : char * str; str = " I am a student. " ;,, : char s[ 80] ; s = " I am a student. " ;, str = " I am a student. " ;, 86
5 5. 4. 2 5. 10 #include " stdio. h" { char ch, * pc = " C language program. ", * p; printf( " Enter a character: " ) ; scanf( " % c", &ch) ; /* * / p = pc; /* p * / while( * p! = \0&& * p! = ch) /* p * / p + + ; /* p * / if ( * p = = ch) /*,, p * / else printf( " The character % c is % d-th \ n", ch, p-pc + 1) ; printf( " The character not found \ n" ) ; : Enter a character: e The character e is 10-th : Enter a character: s The character not fount 5. 11 #include " stdio. h" #include " string. h" /* ( 1 ) * /, { char str[ 80], * p, * q, t; printf( " Enter a string: " ) ; scanf( " % s", str) ; / * * / for( p = str, q = p + strlen( str) -1; p < q; p + +, q - - ) { t = * p; /*,, p \0 * / / * p, q \ 0,, p q, p, q, p > = q 5. 11 * / * p = * q; * q = t; printf( " The reversed string is: % s \ n", str) ; : Enter a string: abcdefgh / * * / 5. 11 87
C The reversed string is: hgfedcba strlen( str), str string. h 5. 5 5. 5. 1,, : * [ ] ;,,, p, 3, int : int * p[ 3] ;, : float * px[ 5], * py[ 3], data[ 5] [ 10], a, b, * pa = &a; : int b[ 3] [ 4], * pb[ 3] ; b[ 3] [ 4] b[ 0 ] b[ 1 ] b [ 2] 3, 3 3 : 88 pb[ 0] = b[ 0] ; pb[ 1] = b[ 1] ; pb[ 2] = b[ 2] ; pb 5. 12 #include " stdio. h" { int b[ 3] [ 4], * pb[ 3], i, j; for( i = 0; i < 3; i + + ) / * * / for( j = 0; j < 4; j + + ) b[ i] [ j] = ( i + 1) * ( j + 1) ; pb[ 0] = b[ 0] ; / * 3 * / pb[ 1] = b[ 1] ; pb[ 2] = b[ 2] ; for( i = 0; i < 3; i + + ) / * * / { for( j = 0; j < 4; j + + ) printf( " b[ % d] [ % d] = % d ", i, j, * ( pb[ i] + j) ) ; printf( " \n" ) ;
5 : b[ 0] [ 0] = 1 b[ 0] [ 1] = 2 b[ 0] [ 2] = 3 b[ 0] [ 3] = 4 b[ 1] [ 0] = 2 b[ 1] [ 1] = 4 b[ 1] [ 2] = 6 b[ 1] [ 3] = 8 b[ 2] [ 0] = 3 b[ 2] [ 1] = 6 b[ 2] [ 2] = 9 b[ 2] [ 3] = 12 5. 3. 2,, pb = b b[ i] [ j] * ( b[ i] + j) * ( pb[ i] + j) pb[ i] [ j] 5. 5. 2,,, 5, : char name[ 5] [ 20] = { " Li Zhi", " Zhang Ji", " Jin Xi", " Wang Fei", " Liu Yi" ; : char * pname[ 5] = { " Li Zhi", 5. 12 " Zhang Ji", " Jin Xi", " Wang Xiaofang", " Liu Yi" ;,,,,,,,, 5. 13 5. 12,,, #include " stdio. h" #include " string. h" { int i; char * pname[ 5] = { " Li Zhi", " Zhang Ji", 89
C " Jin Xi", " Wang Xiaofang", " Liu Yi" ; / * * / char name[ 20] ; printf( " Enter a name: " ) ; / * * / gets( name) ; for( i = 0; i < 5; i + + ) / * * / if ( strcmp( pname[ i], name) = = 0) / * * / break; if ( i < 5) / * i < 5, * / printf( " % s is No. % d student. \ n", pname[ i], i + 1) ; else printf( " % s is not in this class. \ n", name) ; : Enter a name: Zhang Ji Zhang Ji is No. 2 student. : Enter a name: Zang Ji Zhang Ji is not in this class. scanf, gets 5. 14 3 #include " stdio. h" #include " string. h" { int i; char * pstr[ 3] = { " Visual C ++ ", " C Programming", " C Language" ; char * temp; if ( strcmp( pstr[ 0], pstr[ 1] ) > 0) / * pstr[ 0] pstr[ 1] * / { temp = pstr[ 0] ; pstr[ 0] = pstr[ 1] ; pstr[ 1] = temp; / * pstr[ 0] pstr[ 1], pstr[ 0] pstr[ 1], pstr[ 1] pstr[ 0],,, * / if ( strcmp( pstr[ 0], pstr[ 2] ) > 0) { temp = pstr[ 0] ; pstr[ 0] = pstr[ 2] ; pstr[ 2] = temp; if ( strcmp( pstr[ 1], pstr[ 2] ) > 0) { temp = pstr[ 1] ; 90
5 pstr[ 1] = pstr[ 2] ; pstr[ 2] = temp; for( i = 0; i < 3; i + + ) printf( " % s \n", pstr[ i] ) ; : C Language C Programming Visual C ++ 5. 6 5. 13, pa a, ppa pa, ppa,, * ppa pa, * * ppa = * ( * ppa) = * pa a, 5. 12, pname, * pname pname[ 0], * pname[ 0] * * pname pname[ 0 ] L ppa pa a &pa &a &pa * ppa * pa, * * ppa 5. 13 : ppa a * * ;,, : int a, * pa, * * ppa; char * pname[ 3], * * ppname = pname; 5. 15 #include " stdio. h" { int i; char * name[ ] = { " WINDOWS", " MS-DOS", " UNIX", " LINUX" ; char * * pp; for( i = 0; i < 4; i + + ) { pp = name + i; / * pp name[ i], pp = &name[ i] * / printf( " % s \ n", * pp) ; 91
C : WINDOWS MS-DOS UNIX LINUX : { char * name[ ] = {" WINDOWS", char while( * * pp! * * pp = name; = \0 ) " MS-DOS", " UNIX", " LINUX", " " ; printf( " % s \ n", * pp + + ) ;, pp name[ 0 ] name[ 4], * * pp, \ 0, ( ),,,,,, 5. 16 #include " stdio. h" char * a[ ] = { " gain", " much", " stronger", " point" ; char * * aa[ ] = { a + 2, a, a + 3, a + 1 ; char * * * aaa = aa; { printf( " % s", * aaa[ 2] ) ; / * * ( * ( aaa + 2) ) * aa[ 2] a + 3 " point" * / printf( " % s", * * aaa + 6) ; / * * aa[ 0] + 6a + 2 " stronger" 6 " er" * / printf( " % s", * + + * + + aaa + 2) ; / * * ( aa [ 1] + 1) + 2* ( a + 1) + 2a[ 1] " printf( " % s", aaa[ 0] [ - 1] + 1) ; much" 2 " ch", aaa aa [ 1], aa[ 1] a + 1 * / / * * ( aa[ 1] - 1) + 1* a + 1a[ 0] gain 92
5 1 " ain" * / : pointer chain 5. 14 5. 14 5. 7 main :,, copy file1. txt file2. txt, : 1 2 < >, copy,, file1 file2,, C,, C? C main,, C main main : main( int argc, char * argv[ ] ) { argc int, argv, C exefile 3, : exefile proc1. c proc2. c proc3. c, argc 4, argv 4, : argv[ 0] = " exefile" ; argv[ 1] = " proc1. c" ; 93
C argv[ 2] = " proc2. c" ; argv[ 3] = " proc3. c" ; main, argc argv,, int, char 5. 17 #include " stdio. h" main( int argc, char * argv[ ] ) { int i; printf( " argc = % d \ n", argc) ; printf( " Command name: % s \n", argv[ 0] ) ; for( i = 1; i < argc; i ++ ) printf( " Argument No. % d: % s \ n", i, argv[ i] ) ; cprog. exe, : cprog book pen paper / s : argc = 5 Command name : cprog Argument No. 1: book Argument No. 2: pen Argument No. 3: paper Argument No. 4: / s 5. 8 94 5. 18 #include " stdio. h" s1 s2, s2 s1, { char s1[ 80], s2[ 80], * p1, * p2; int i; printf( " ( 40) : " ) ; gets( s1) ; printf( " ( 40) : " ) ; gets( s2) ; p1 = s1; / * p1 p2 s1 s2 * / p2 = s2; while( * p1! p1 + + ; = \0 ) / * p1 s1 \0* / while( ( * p1 + + = * p2 + + )! = \0 ) ; / * p2 p1 * / * p1 = \0 ; / * * /
5 : printf( " : % s \ n", s1) ; : abcde : fghijklm : abcdefghijklm strcat( s1, s2) 5. 19 #include " stdio. h" { char ch1, ch2, * pc = " C language program. ", * p; int flag; printf( " : % s\ n", pc" ) ; printf( " : " ) ; scanf( " % c", &ch1) ; printf( " : " ) ; scanf( " % c", &ch2) ; flag = 0; / * * / p = pc; / * p * / while( * p! = \0 ) / * p * / { if( * p = = ch1) / * ch1 ch2, * / { * p = ch2; flag = 1; p ++ ; / * p * / if ( flag = = 0) / * * / else : printf( " \ n" ) ; printf( " : % s \ n", pc) ; : C language program. : a : o : C longuoge progrom. 5. 20 #include " stdio. h" #define N 5 ( ) { char * name[ ] = { " Li jun", " Wang xiaogang", " Che hong", 95
C " Shun dasheng", " Zhang zhigang" ; char * temp; int i, j, k; printf( " : \ n" ) ; for( i = 0; i < N; i + + ) / * * / puts( name[ i] ) ; for( i = 0; i < N - 1; i + + ) / * * / { k = i; for( j = i + 1; j < N; j + + ) if( strcmp( name[ j], name[ k] ) < 0) k = j; if( k < > i) { temp = name[ k] ; name[ k] = name[ i] ; name[ i] = temp; printf( " : \ n" ) ; for( i = 0; i < N; i + + ) / * * / puts( name[ i] ) ; : : Li jun Wang xiaogang Che hong Shun dasheng Zhang zhigang : Che hong Li jun Shun dasheng Wang xiaogang Zhang zhigang,, 5. 21 s1 s2,, 0;, ASCII : s1 > s2,, s1 < s2, #include " stdio. h" { char s1[ 80], s2[ 80], * p1, * p2; int n, i; printf( " : " ) ; 96
5 gets( s1) ; printf( " : " ) ; gets( s2) ; p1 = s1; p2 = s2; /* p1 p2 s1 s2 * / while( * p1 = = * p2) /*, * / { if( * p1 = = \0 ) /* * / break; p1 ++ ; /* * / p2 ++ ; n = * p1 - * p2; printf( " : % d \ n", n) ; : : abcde : abcde : 0 : : fghijklmn : fghijopq : - 5, n = strcmp( s1, s2) /* \0, * / 5. 1? 5. 2?? 5. 3 * ( 1 ) num[ 5] ( 2 ) data[ k + 1] ( 3) array[ 6] [ 4] 5. 4 10 5. 5 33 5. 6 33 5. 6 a, " Fortran program", " Fortr", 5. 7, 5. 8 5. 9 ( 1 ) " efg" " abcdefghijk" 5, 0 5. 10, 97
C " student" " ustdent" 5. 11 5 5. 12 n 5. 13, 98
6, C, C, C, C ( C ),, C C C 6. 1 C 6. 1. 1 C,,, 4 : ;, ; ; 3 x = sqrt( 12. 3) ;,, 12. 3,, x strcmp( str1, str2), char, str1 < str2, ; 0; str1 > str2,, int. h, #include sqrt math. h, strcmp string. h 99
C C ANSI C : int strcmp( char * str1, char * str2) strcmp int, char, C 6. 1. 2 scanf( ) printf( ), ( 1 ) printf( ) printf( ) : printf(, ) ; printf( ) :,, ( ) int n = 25; float x = 123. 45; printf( " x = % f, n = % d \ n", x, n) ;, " x = % f, n = % d \ n" x n, % f % d x n : : % % (, % ), printf ( ),x =, n = \ n,, : : : x = 123. 450000, n = 25 printf( " % f* % d = % f \ n", x, n, x* n) ; 123. 45* 25 = 3086. 25 %, : % 0 m. n l h %,, 0m. n l h,, 100 l h l h,, 6. 1 : long % ld % lx % lo % lu double % lf % le % lg m. n A., m, n
6 m. n,, n : float x = 123. 4567; printf( " % 12. 5f, % 5. 0f, % 0. 3f \n", x, - x, x) ; 6. 1 d x int char int x = 123; printf( " % d", x) ; 123 int char int x = 123; printf( " % x", x) ; 7b o u int char int x = 123; printf( " % o", x) ; 173 int char int x = - 123; printf( " % u", x) ; 131 ld lxlo long long x = 34567; printf( " % ld", x) ; 34567 lu hd hx ho short short x = 123; printf( " % hd", x) ; 123 hu c char char x = 65; printf( " % c", x) ; A s char * p = " ABC" ; printf( " % s", p) ; f float float x = 123. 45; printf( " % f", x) ; 123. 450000 e float float x = 123. 45; printf( " % e", x) ; 1. 234500e + 002 g float float x = 123. 45; printf( " % g", x) ; e f 123. 450000 ABC lf le lg double double x = 123. 45; printf( " % lf", x) ; 123. 4500000000000000 : 123. 45670, - 123, 123. 457 6, 3, 5,, 12 5, 0,, 1 m = 0,, 3,, % f 6, % lf 16 :,, 6 ( ), B., m, n n m n m, : char * s = " ABCDEFG" ; printf( " % 5. 3s \ n", s) ; 101
C : ABC printf( " % 5s\ n", s) ; ABCDEFG +,, -, : : ABC char * s = " ABCDEFG" ; printf( " % - 5. 3s \ n", s) ; : float x = 123. 4567; printf( " % - 12. 5f \ n", x) ; : 123. 45670 0 0, 0 : float x = 123. 4567; : 000123. 45670 printf( " % 012. 5f\ n", x) ; ( 2 ) scanf( ) scanf( ) : scanf(, ) ; scanf( ) :, ( ) : scanf( " % f% d", &x, &n) ; " % f% d" &x&n, % f % d &x &n, ( Tab ) : : % % (, % ), scanf ( ), : scanf( " x = % f, n = % d", &x, &n) ;, x = 1. 23, n = 10, : x = 1. 23, n = 10 x =, n =,,, scanf, scanf,, : : 102 printf( " x, n = " ) ; scanf( " % f, % d", &x, &n) ;
6 x, n = 1. 23, 10,, : 4 ) : scanf( " % d: % d: % d", &hour, &minute, &second) ;, : 12: 30: 15 %, : % * m l h l h, m,, ( 6 ) ( scanf( " % 6d% 4f", &xh, &cj) ; : 12345678. 5 xh 123456, cj 78. 5 % * : scanf( " % 3d% * 3d% 3c% 4d", &x, &ch, &y) ; : 1234567891011 x 123, ch 7, y 1011 456, 789 ch, ch, scanf( ) int,, EOF, 0,, : while( scanf( " % f", &x) ) s = s + x;, % f, scanf( ) 0 6. 2, C, 6. 2. 1, : 103
C ( ) { ; ;, static static, 7. 1 int, ;, void 6. 1 float float trapezoid( int a, int b, int h) / * trapezoid( ), int a, b, h, ; * / { return ( a + b) * h / 2. 0; / * * / 6. 2 max( int x, int y) { if ( x > y) 6. 3 int return x; return ( y) ; void / * max( ) x y * / void display( int n) /* display( ), int n* / { int i, j; for( i = 1; i < = n; i + + ) { for( j = 1; j < = i; j + + ) return; printf( " % 2c", * ) ; printf( " \ n" ) ; return n 3, 3 * : * * * * * * ( ), { float trapezoid( int a, int b, int h) : float trapezoid( a, b, h) int a, b, h;, :, C, : 104 float trapezoid( int a, b, h)
6, ( ) void,, : int func( void), : int func( ) 6. 4 void dispstar( void) / * dispstar( ) * / { int i; for( i = 1; i < = 10; i + + ) / * 10 * / printf( " " ) ; for( i = 0; i < 60; i + + ) / * 60 * * / printf( " * " ) ; printf( " \ n" ) ; auto ( ) register,, 6. 7, return return : return( ) ; return ; return; ;, return ;, 3 4 5, int int trapezoid( int a, int b, int h) { return ( a + b) * h /2. 0; 17 17. 5,,, trapezoid( ) float, : empty( ) {, empty,, 6. 2. 2, : ( ) ; : 105
C float trapezoid( int, int, int ) ; 3 int, : ( ) ; : float trapezoid( int x, int y, int z) ;, a, b, h,, : float trapezoid( ) ;,,,,, 6. 5 float float_func( long) ; / * float_func * / { long long_func( int), a, b; / * long_func * / int n, c, d; float x; x = float_func( a) ; n = int_func( c) ; / *, int * / b = long_func( d) ; float float_func( long y ) / * float_func float * / { long long_func( int p ) / * long_func long * / { int_func( int z ) / *, int_func int * / { :, float_func( ), int, int_func( ), 6. 5, float_func long_func, : float float_func( long y ) / * float_func * / 106
6 { long long_func( int p ) / * long_func * / { { long a, b; / * long_func float_func * / int n, c, d; float x; x = float_func( a) ; n = int_func( c) ; / *, int * / b = long_func( d) ; int_func( int z ) / *, int_func int * / { 6. 2. 3 ( 1 ), return : ( ),, 3 :, 6. 3 : display( 5) ;, ( ) 5 n 6. 1 : sum = 8* trapezoid( 4, 5, 6) ;, 4 5 6 a b h, a 4, b 5, h 6 27. 0, sum 216. 0 ( sum float ) 6. 2 3 a, b, c ( ) : m = max( a, max( b, c) ) ; a = 9 b = 15 c = 3,, b c x y, max( b, c) 15, max( a, 15) ; 15, m 15 : printf( " max = % d \ n", max( a, max( b, c) ) ) ;,, a b c,, max( ), : max = 15 ( 2 ), : 107
C,,,,,,,, 6. 6 C n m = #include " stdio. h" m! n! ( m - n)! long fact( int) ; / * fact( ) * / { int n, m; long cmn, fact( int) ; printf( " Enter m and n: " ) ; scanf( " % d, % d", &m, &n) ; cmn = fact( m) /( fact( n) * fact( m - n) ) ; / * fact( ) cmn * / printf( " cmn = % ld \ n", cmn) ; long fact( int x) { long m = 1; for( ; x > 1; x - - ) / * for, x 1 * / m = m* x; / * x * / return m; / * m * / : Enter m and n: 15, 10 cmn = 3003,, ( ) ( ) ( ),,, 6. 3,,,,, 108
6 6. 3. 1 6. 2. 3 6. 7 #include " stdio. h" ( ) int swap( int x, int y) ; /* swap( ) * / { int a = 88, b = 66; printf( " : a = % d, b = % d \ n", a, b) ; swap( a, b) ; /* swap( ) * / printf( " : a = % d, b = % d \ n", a, b) ; int swap( int x, int y) / * swap( ) * / { int temp; : printf( " : x = % d, y = % d \ n", x, y) ; temp = x; x = y; y = temp; printf( " : x = % d, y = % d \ n", x, y) ; : a = 88, b = 66 : x = 88, y = 66 : x = 66, y = 88 : a = 88, b = 66 6. 1 6. 7 x y, a b? a b x y swap( ),, a x, b yswap ( ) x y, a b ( 6. 1 ), 6. 3. 2,, 6. 2, 6. 6, 6. 8 #include " stdio. h" ( ) int swap( int * x, int * y) ; { int a = 88, b = 66; printf( " : a = % d, b = % d \ n", a, b) ; 109
C swap( &a, &b) ; / * * / printf( " : a = % d, b = % d \ n", a, b) ; int swap( int * x, int * y) / * * / { int temp; printf( " : * x = % d, * y = % d \ n", * x, * y) ; temp = * x; * x = * y; * y = temp; / * * x * y a b * / printf( " : * x = % d, * y = % d \ n", * x, * y) ; & [ ] 6. 2 6. 3 6. 8 : : a = 88, b = 66 : * x = 88, * y = 66 : * x = 66, * y = 88 : a = 66, b = 88 2, 6. 3,,,, 6. 9 ( ) #include " stdio. h" #define N 20 float mean( int * data, int n) / * data * / { int i; float aver = 0; for( i = 0; i < n; i ++, data ++ ) / * * / aver + = * data; aver / = n; / * * / return ( aver) ; / * * / { int i, a[ N] ; float av; / * mean( ), * / for( i = 0; i < N; i ++ ) / * * / 110
6 scanf( " % d", a[ i] ) ; av = mean( a, N) ; / * mean( ), a, data * / printf( " Mean: % f\ n", av) ; / * * / a data, data a ( 6. 4),, * data a,,,, &a [ 0 ] 6. 4 a,, p: int i, a[ N], * p = a; av = mean( p, N) ;, mean : float mean( int data[ ], int n) data, data,,!, 6. 10,, ( ) #include " stdio. h" #define N 20 int maxdata( int data[ ], int n) ; int movedata( int * data, int mi) ; { int i, a[ N], m; for( i = 0; i < N; i + + ) / * * / scanf( " % d", a[ i] ) ; m = maxdata( a, N) ; / * maxdata( ), * / movedata( a, m) ; / * movedata( ), * / for( i = 0; i < N; i + + ) / * * / printf( " % d \ t", a[ i] ) ; printf( " \ n" ) ; int maxdata( int data[ ], int n) / *, data[ ] * / { int i, max, mi; max = data[ 0] ; 111
C for( i = 1; i < n; i + + ) / * * / if ( max < data[ i] ) mi = i; / * max * / return ( mi) ; / * * / int movedata( int * data, int mi) { int i, mx; / *,, data, * / mx = data[ mi] ; / * mx * / for( i = mi; i > 0; i - - ) / * * / data[ i] = data[ i - 1] ; data[ 0] = mx; / * * / return; / * * /,,,, char char,, char, ; 6. 11 #include " stdio. h" int strlen( char s[ ] ) ; { char * str = " c program" ; int len; len = strlen( str) ; / * * / printf( " The first string \ s length: % d \ n", len) ; len = strlen( " fortran languige" ) ; / * * / printf( " The second string\ s length: % d \ n", len) ; int strlen( char s[ ] ) / * strlen( ) * / { int n; for( n = 0; * s! n ++ ; return ( n) ; : The first string s length: 9 = \0 ; s + + ) The second string s length: 16 strlen( ),, 112
6 6. 4,,, : * ( ), static : int * s( int a, char ch) ; int s,, int : * ( ) ; : int * s( ), i, n; / * int s i n * /, return 6. 12,, : #include " string. h" / * strcmp( ) * / char * maxstr( char * ps[ ], int n) { char * max; int i; max = ps[ 0] ; / * maxstr( ), ps * / for( i = 1; i < n; i ++ ) / * max * / if ( strcmp( max, ps[ i] ) < 0) max = ps[ i] ; return ( max) ; / * max * /, maxstr( ), : #include " stdio. h" /* gets( ) * / #define N 10 { char * s[ N], * p, * maxstr( char * ps[ ], int n) ; int i; /* maxstr( ) * / 113
C for( i = 0; i < N; i + + ) gets( s[ i] ) ; /* N * / p = maxstr( s, N) ; /* maxstr( ), * / printf( " The max string is % s \ n", p) ; /* * / 6. 5 6. 5. 1,,,, 6. 13, 3 : : s, htoi( s) s a, htoi( char * ps) :, ps,, ctod( ),, 114 ctod( char c) : c d : #include " stdio. h" long htoi( char * ps) ; / * long htoi( ) * / char ctod( char c) ; / * char ctod( ) * / { char * s[ 80] ; long a; printf( " Enter a hexadecimal number: " ) ; scanf( " % s", s) ; / * * / a = htoi( s) ; / * htoi( ), * / if ( a < 0) else printf( " Input error! \ n" ) ; / * a < 0, * / printf( " The decimal number is % ld \ n", a) ; /* * / long htoi( char * ps) / * long htoi( ), ps * / { char * p; int i, d; long n = 0; for( p = ps; * p! = \ 0 ; p + + ) / * ps * / { d = ctod( * p) ; / * ctod( ), d * /
6 if ( d < 0) else return - 1; / * * p,, - 1* / n = n* 16 + d; / * * / return ( n) ; / * n * / ctod( char c) / * int ctod( ), c char * / { int d; if ( c > = 0&& c < = 9 ) else d = c - 0 ; / * c * / if ( c > = a&& c < = f ) else d = c - a + 10; / * c a f * / if ( c > = A&& c < = F ) d = c - A + 10; / * c A F * / else d = - 1; / * c,, d - 1 * / return ( d) ; / * d * /, 6. 5 6. 5. 2,, htoi( ) 6. 5 htoi( ) c tod( ) c tod( ) 6. 12,, 4, 3, 3, 3, 20,?,,,, 20,,,,,,,, ;,,,, 1 1. 3,,, : 6. 14 long fact( int n) { if ( n = = 1) else return ( 1) ; n! = n( n - 1 )! ( n > 1 ) 1 ( n = 1) return ( n* fact( n - 1) ) ; / * * / 115
C fact( 5), 6. 6 6. 6 fact( 5) 6. 6, n,,, n, 5 4 3 2 1,, n, n,,,,, auto C,,,,, 6. 15 ( Tower of Hanoi) 3, 6. 7, 64,,,,,, 6. 7 A B C, A n, n A B C hanoi( n, a, b, c), : 116 C A n - 1 B, hanoi( n - 1, a, c, b) n A C A B n - 1 C, hanoi( n - 1, b, a, c) : #include " stdio. h"
6 void hanoi( int n, char a, char b, char c) ; { int n; printf( " Input number of diskes: " ) ; scanf( " % d", &n) ; printf( " The step to moving % d diskes: \n", n) ; hanoi( n, A, B, C ) ; void hanoi( int n, char a, char b, char c) { if ( n = = 1) printf( " % c - > % c \ n", a, c) ; / *, " A - > C" * / else { hanoi( n - 1, a, c, b) ; printf( " % c - > % c \n", a, c) ; hanoi( n - 1, b, a, c) ; : Input number of diskes: 3 The step to moving 3 diskes: A - > C A - > B C - > B A - > C B - > A B - > C A - > C 6. 6 6. 6. 1 C,,,,, C,,,,, : ( * ) ( ) ;,, : 117
C int ( * f1) ( ), ( * f2) ( ) ; / * int f1 f2 * / int * pfunc( ), ifunc( ) ; / * int pfunc( ) int ifunc( ) * / f1 = pfunc; / * f1, f1 pfunc( ) * / f2 = ifunc; / * f2, f2 ifunc( ) * / : : int ( * f1) ( ) ;, int * f1( ) ; f1 ( ),,, 6. 6. 2 : ( 1 ) *,, ( * ) ( ) ; *,,,, int f1( int x, int y) int f2( char ch), fs: int ( * fs) ( ) ;, : fs = f1; / * fs f1( ) * / x = ( * fs) ( a, b) ; / * x = f1( a, b) ; * / fs = f2; / * fs, fs f2( ) * / y = ( * fs) ( str) ; / * y = f2( str) ; * /,,, ( 2 ), : : : 6. 16 #include " stdio. h" #define N 5 { int maxnum( int x[ ], int n ), minnum( int x[ ], int n ) ; int sumnum( int x[ ], int n ) ; int func( int x[ ], int n, int ( * fun) ( ) ) ; / * * / int a[ N], i; printf( " Enter % d integer numbers: \ n", N) ; for( i = 0; i < N; i + + ) / * a * / scanf( " % d", &a[ i] ) ; 118
6 printf( " max = " ) ; func( a, N, maxnum) ; / * func( ) a, maxnum * / printf( " min = " ) ; func( a, N, minnum) ; / * func( ) a, minnum * / printf( " sum = " ) ; func( a, N, sumnum) ; / * func( ) a, sumnum * / func( int x[ ], int n, int ( * fun) ( ) ) / * fun * / { int result; result = ( * fun) ( x, n) ; / * fun * / printf( " % d \ n", result) ; maxnum( int x[ ], int n) { int max = x[ 0], k; for( k = 1; k < n; k + + ) if ( max < x[ k] ) max = x[ k] ; return ( max) ; minnum( int x[ ], int n) { int min = x[ 0], k; for( k = 1; k < n; k + + ) if ( min > x[ k] ) min = x[ k] ; return ( min) ; sumnum( int x[ ], int n) { int sum = 0, k; for( k = 0; k < n; k + + ) sum + = x[ k] ; return ( sum) ; : Enter 5 integer numbers: 10 25 33 15 27 max = 33 min = 10 sum = 110 ( 3 ),, 119
C 6. 17 #include " stdio. h" #define N 5 { int func1( int, int), func2( int, int), func3( int, int), func4( int, int) ; int execute( int, int, int) / * * / int ( * function[ 4] ) ( ) ; / * int * / int a, b, i; function[ 0] = func1; / * * / function[ 1] = func2; function[ 2] = func3; function[ 3] = func4; a = 20; b = 5; for( i = 0; i < 4; i ++ ) /*,, execute( ), * / printf( " func No. % d > % d \n", i + 1, execute( a, b, function[ i] ) ) ; execute( int x, int y, int ( * func) ( ) ) / * func * / { return( ( * func) ( x, y) ) ; / *, * / func1( int x, int y) { return ( x + y) ; func2( int x, int y) { return ( x - y) ; func3( int x, int y) { return ( x* y) ; func4( int x, int y) { return ( x /y) ; : func No. 1 > 25 func No. 2 > 15 func No. 3 > 100 func No. 4 > 4 6. 7,,, 120
6 6. 7. 1 C 4, auto( ) register( ) static ( ) extern( ), auto register,,, auto, C auto, C, auto, auto, auto register CPU,,, register, ( 2 ), short static,, static 0 extern, static, 6. 7. 2, ( ), auto register static,,,, ;, ( ),, ( static ) static,, 0 6. 18 #include " stdio. h" int other( int n) ; { int i, n; / * auto i n * / static int a; / * static a * / register int b; / * register b * / i = 1; a = 10; b = 100; n = 5; if( a < b) { int t; / * auto t, t * / 121
C t = a; a = b; b = t; if( printf( " t = % d \ n", t) < 0) printf( " MAIN: error \ n" ) ; printf( " MAIN i: % d a: % d b: % d \ n", i, a, b) ; other( n) ; / * other * / printf( " MAIN i: % d a: % d b: % d \ n", i, a, b) ; other( n) ; int other( int n) { int i = 0; : / * t, t, printf( ) < 0 * / static int a = 20; / * static a, 20 * / i = i + 10; a = a + 20; n = i + a + n; printf( " OTHER i: % d a: % d n: % d \ n", i, a, n) ; MAIN: error MAIN i: 1 a: 100 b: 10 OTHER i: 10 a: 40 n: 55 MAIN i: 1 a: 100 b: 10 OTHER i: 10 a: 60 n: 75 : 6. 18 other( ) b, t t 6. 18 other( ) i a,,,, i a ( ),, i a,, C static,, 6. 18 other( ) a,, a = 20, a = a + 20 40;, a, a 40, a = a + 20 60 other( ) i,, i = 0, i = i + 10 10,, i ;, i 0, i = i + 10 10 auto,, auto register, static 0 122, ( 6. 19) 6. 19 ( )
6 str m n, string,, : char * strcut( char * str, int m, int n) / * strcut( ) * / { char string[ 80] ; int i; / * str, * / for( i = 0; i < n; i ++ ) / * string, m m - 1 * / string[ i] = str[ m + i - 1] ; string[ i] = \0 ; / * string * / return ( string) ; / * string * /, : { static char s[ ] = " Computer science" ; char * strcut( char * str, int m, int n ), * p; / * strcut( ) * / p = strcut( s, 10, 7) ; / * strcut( ), * / printf( " % s\ n", p) ; / * * /, string, auto,,,, p string static,,, m + n s,? 6. 20 #include " stdio. h" : int maxmin( float x[ ], int n) ; float max; / * max n * / int n = 100; { extern float min; / * min * / int i; float a[ 10] ; for( i = 0; i < 10; i + + ) / * a * / scanf( " % f", &a[ i] ) ; maxmin( a, 10) ; / * * / printf( " max = % f, min = % f \n", max, min) ; / * max min * / float min; / * min * / int maxmin( float x[ ], int n) / * * / { int i; max = min = x[ 0] ; 123
C for( i = 1; i < n; i + + ) if ( max < x[ i] ) max = x[ i] ; else if ( min > x[ i] ) min = x[ i] ; :, 6. 20 max min maxmin( ), 6. 20 n maxmin( ) n, maxmin( ), n n, 6. 20 min, extern extern, 7. 1 6. 7. 3 C 4, ( auto register ) ( static ),,,,,,,,,,,,,,, 6. 2 6. 2 auto register CPU static extern,, : ( static ) ( static ) ( ) 6. 8, C ;,,,, 124
6 ;,,,,,, C 4 6. 8. 1 ( 1) malloc malloc void *, malloc( size ), size unsigned int malloc size,,, void,, char, ( NULL),, : int float * pi; * pf; pi = ( int* ) malloc( sizeof( int) ) ; / * pi int * / pf = ( float* ) malloc( sizeof( float) ) ; / * pf float * / if ( pi! = NULL) / * * / * pi = 123; if ( pf! = NULL) * pf = 123. 456; ( int* ) *, int int,, : pi = ( int* ) malloc( 2) ; pf = ( float* ) malloc( 4) ; malloc ( 2) calloc malloc, n, calloc calloc void *, calloc( n, size), n size unsigned int size,, calloc, 0 : char * pc; pc = ( char* ) calloc( 80, sizeof( char) ) ; / * pc 80 char * /, calloc 6. 8. 2 malloc calloc, free : free( p) ; p, p, p free 6. 8. 3, realloc realloc void *, 125
C realloc( p, size), p, size unsigned intrealloc p sizesize,, : int * pi; float * pf; pf = ( float* ) malloc( sizeof( float) ) ; / * pf float * / pi = ( int* ) realloc( pf, sizeof( int) ) ; / * pf int * / 6. 9 6. 21 ( C. Goldbach) : 6 #include " stdio. h" #include " math. h" / * sqrt( ) * / int prime( long) ; { long n, a, b, c; printf( " : " ) ; scanf( " % ld", n) ; for( a = 6; a < = n; a + = 2) / * 6 * / { for( b = 3; b < = a /2; b + = 2) / * b * / if( prime( b) ) / * b * / { c = a - b; / * c * / if( prime( c) ) / * c,, b * / break; printf( " % ld = % ld + % ld \ n", a, b, c) ; / * * / prime( long x) / * * / { int flag = 1; / * flag, 1, 0 * / long i; for( i = 2; i < sqrt( x) ; i ++ ) if( x% i = = 0) / * x i,, * / { flag = 0; break; return ( flag) ; : : 10 126
6 6 = 3 + 3 8 = 3 + 5 10 = 3 + 7 6. 22 3, ; del_str( ) ; del_char( ) #include " stdio. h" char * del_str( char str[ ], char ch) ; / * del_str( ) : * / { char s[ 80], dc; / * s[ 80] :, dc: * / printf( " : " ) ; gets( s) ; printf( " : " ) ; getchar( dc) ; del_str( s, dc) ; / * * / printf( " : % s \ n", s) ; char * del_str( char str[ ], char ch) { int i; char * del_char( char * st, int k ) ; / * : st k * / for( i = 0; i < strlen( str) ; i ++ ) if ( str[ i] = = ch) { del_str( str, i) ; / * * / i - - ; / * * / return ( str) ; char * del_char( char * st, int k) { int i; i = k; while( i < strlen( st) - 1) st[ i] = st[ i + 1] ; / *, 6. 8 * / return ( st) ; : : Information science : i : Informaton scence 6. 8, del_str del_char( str, i) ; strcpy( &str[ i], &str [ i + 1] ) ; del_char( ) 6. 23 x 3-2x 2 + 5x - 8 = 0, 10-5 f( x), ( 6. 9) : f( x) x x 0 x 1, x 2 127
C f( x 1 ) f( x 2 ), x x, 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 4 : f( x) x : x 3-2x 2 + 5x - 8; point( x1, x2) f( x1) f( x2 ) x x; root( x1, x2) ( x1, x2), #include " stdio. h" #include " math. h" float f( float x) ; float point( float x1, float x2) ; float root( float x1, float x2) ; { float x1, x2, x; do { printf( " Input x1, x2: " ) ; scanf( " % f, % f", &x1, &x2) ; 6. 9 / * x1 x2 * / while( f( x1) * f( x2) > = 0) ; / * f( x1) f( x2) * / x = root( x1, x2) ; / * root x * / printf( " A root of the equation is % 8. 5f", x) ; / * x * / float f( float x) / * f( ), f( x) * / { return ( ( ( x - 2. 0) * x + 5. 0) * x - 8. 0) float point( float x1, float x2) / * point, x x * / { return ( ( x1* f( x2) - x2* f( x1) ) / ( f( x1) - f( x2) ) ) float root( float x1, float x2) / * root, * / { float x, y, y1; : 128 y1 = f( x1) ; / * f( ), f( x1) * / do { x = point( x1, x2) ; / * point( ) x x * / y = f( x) ; if( y* y1 > 0) / * f( x) f( x1), x x1 * / { y1 = y; x1 = x; else x2 = x; / * x x2 * / while ( fabs( y) > = 1e - 6) ; / * f( x) 10-6 * / return ( x) ;
6 Input x1, x2: 1, 9 A root of the equation is 1. 75217 6. 24 : 1 y1 = ( 1 + x + 0 x2 + x 3 ) dx 2. 5 y2 = ( 1 + x 2 ) dx 0 3. 5 y3 = 0 x dx 1 + x 2, f( x), b a, n, h = ( b - a) /n, 6. 10, : s = f( a ) + f( a + h) 2 h 2 h h + f( a + h) + f( a + 2h) 2 f( a + ( n - 1) h) f( b) h + h = 2 [ f( a) + 2f( a + h) + 2f( a + 2h) + + 2f( a + ( n - 1) h) + f( b) ] = f( a) + f( b) 2 + f( a + h) + f( a + 2h) + + f( a + ( n - 1) h), n, f( x) : float integral( float ( * f) ( ), float a, float b, int n) { float s, h, y; int i; s = ( ( * f) ( a) + ( * f) ( b) ) /2; h = ( b - a) /n; for( i = 1; i < n; i + + ) s = s + ( * f) ( a + i* h) ; y = s* h; / * f, f( x) * / return ( y) ; / * * / : float f1( float x) { return ( 1 + x + x* x + x* x* x) ; float f2( float x) { return ( 1 + x* x) ; float f3( float x) { return ( x/ ( 1 + x* x) ) ; : 6. 10 { float f1( float x), f2( float x), f3( float x) ; / * * / float integral( float ( * f) ( ), float a, float b, int n) ; float y1, y2, y3; int n = 100; y1 = integral( f1, 0, 1, n) ; 129
C : y2 = integral( f2, 0, 2. 5, n) ; y3 = integral( f3, 0, 3. 5, n) ; printf( " y1 = % 6. 3f\ ny2 = % 6. 3f\ ny3 = % 6. 3f\ n", y1, y2, y3) ; y1 = 2. 083 y2 = 7. 709 y3 = 1. 292 6. 25 #include " stdio. h" int data[ ] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20; /* data * / int binary( int key, int low, int high) ; { int a, k; printf( " : " ) ; scanf( " % d", &k) ; a = binary( k, 0, 9) ; if( a = = - 1) printf( " % d \ n", k) ; else printf( " % d % d \ n", k, a) ; int binary( int key, int low, int high) { int mid; / * key:, high low: * / if( low > high) return ( - 1) ; /*, - 1 * / mid = ( low + high) /2; if( data[ mid] > x) /* * / return ( binary( x, low, mid - 1) ) ; else if( data[ mid] < x) /* * / else : return ( binary( x, mid + 1, high) ) ; return ( mid) ; /* * / : 15 15 5 6. 1 5. 18, s1 s2 strcat( s1, s2), 6. 2 5. 19, c1 s c2 strrpl( s, c1, c2 ), 130
6 6. 3 5. 11, reverse( ), 6. 4 10 6. 5, 6. 6, x 3-5x 2 + 16x - 80 = 0 [ 2, 8] 6. 7, 7x 4 + 6x 3-5x 2 + 4x + 3 = 0 6. 8, : y 1 = 7 ( 2 6x2 + 5x - 3. 8) dx y 2 = 2 0 1 + sinxdx 6. 9 6. 10,, 6. 11, str substr 6. 12 itob( n), n, 6. 13 6. 3 reverse 6. 14 20, 6. 15 n, 1, 1 5, 5,? 6. 16 20 a, part( a, m, n), m n, : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20; m = 5, n = 10, : 1, 2, 3, 4, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 15, 16, 17, 18, 19, 20 6. 17 substr str insstr( str, ch, substr), ch str ch, 6. 18 malloc, 10, 10 6. 19, n, : f( n) = 1 n = 0 1 n = 1 f( n - 1) + f( n - 2) n > 1 6. 20, a b, : gcd( a, b) = b a% b = = 0 gcd( b, a% b) a% b! = 0 1
7 C, C,,, C, :,,,, 7. 1 C 7. 1 C 7. 1. 1 ( 1 ) C C C ( 7. 1 ), : ;,,,,,,,, ( ),, 132
7 C,,, C C C,. CC, C C,, ( 7. 1. 3 ) ( 10 ) 7. 2 C 7. 3,,,,,,, ( 7. 1. 3 ) 7. 2 7. 3,, ( 2 ) C, C,,,, 7. 4 7. 4 A B 133
C A: B1 B2 B3 B4 B5 B6 B7 B8 B9 C31 C32 C33 C34 7. 4 ( ) ***** ***** 1. 3. 5. 7. 9. 2. 4. 6. 8. 0. 7. 1 7. 5 7. 1, 7. 5 void mainmenu( ) { char choice = 1 ; while( choice! = 0 ) { / * * / printf( " ***** ***** \ n" ) ; printf( " \ n" ) ; printf( " 1. 2. \ n" ) ; printf( " 3. 4. \ n" ) ; printf( " 5. 6. \ n" ) ; printf( " 7. 8. \ n" ) ; printf( " 9. 0. \ n" ) ; printf( " \ n" ) ; printf( " ( 09) : " ) ; choice = getchar( ) ; switch( choice) { case 1 : 134
7 C b1( ) ; break; / * * / case 2 : case 3 : b2( ) ; break; / * * / b3( ) ; break; / * * / case 4 : b4( ) ; break; / * * / case 5 : b5( ) ; break; / * * / case 6 : b6( ) ; break; / * * / case 7 : b7( ) ; break; / * * / case 8 : b8( ) ; break; / * * / case 9 : b9( ) ; break; / * * / case 0 : default: exit( 0) ; / *, * / printf( ", ( 09) : " ) ;, b3 ( ),, C31 C32 C33 C34,,, C 7. 1. 2 ( 10 ) ( 1), static,,, extern 7. 6, file1. c file2. c file3. c int aa; extern char cc func1( ) { satic int bb; extern int aa; func2 ( ) { func2_1( ) { char cc; static int bb; func3( ) {extern int aa; 7. 6 135
C file1 aa file2, file3 funo3 file3 cc file1, file2 bb file3 bb, 7. 2 int k; /* k * / { void pstar( ) ; for( k = 0; k < 5; k + + ) /* 5 * / pstar( ) ; /* 5 * * / void pstar( ) /* 5 * * / { for( k = 0; k < 5; k ++ ) printf( " % c", * ) ; printf( " \ n" ) ; : ***** 5, 5 *, pstar( ) k 5,,,,,, ( 2) file_1. c file_2. c extern int fun2( ) ; fun1( ) { extern int fun3( ) ; fun2( ) ; fun3( ) static int fun4( ) { extern int fun1( ) ; fun2 { fun3( ) { static int fun4( ) { fun1( ) ; 7. 7 static,,,, extern 7. 7, file_1 extern fun2 fun3, fun4, fun3 fun1 extern, fun3 ( ), file_2 fun1( ) fun4( ) file_1, 136 7. 1. 3, C,
7 C,,,,,,, 7. 3 / * exam1. c * / #include " stdio. h" extern void count( ) ; / * count( ) * / extern void find( ) ; / * find( ) * / char * name[ 50] ; / * * / int score[ 50] ; / * * / { int i; char select; for( i = 0; i < 50; i + + ) / * 50 * / { printf( " % d : ", i) ; gets( name[ i] ) ; printf( " % d : ", i) ; scanf( " % d", &score[ i] ) ; while( 1) / * * / { printf( " : A B C \ n" ) ; getchar( select) ; if ( select == A select == a ) count( ) ; /* count( ) * / else if( select == B select == b ) find( ) ; / * find( ) * / else exit( 0) ; / * * / / * exam2. c * / extern int score[ ] ; / *, * / void count( ) / * count( ) * / { int i; float aver = 0; for( i = 0; i < 50; i ++ ) / * 50 * / aver + = score[ i] ; aver / = 50; / * * / printf( " : % 6. 2f\ n", aver) ; return; 137
C / * exam3. c * / extern int score[ 50] ; / * * / extern char * name[ ] ; / * * / void find( ) / * find( ) * / { int i; char * sn, ch = y ; while( ch == y ch == Y ) { printf( " : " ) ; return; gets( sn) ; for( i = 0; i < 50; i + + ) / * * / if( strcmp( sn, name[ i] ) == 0) / * * / { printf( " % s : % d \ n", sn, score[ i] ) ; break; if ( i > = 5 0) printf( " \ n" ) ; printf( "? Y/ N " ) ; getchar( ch) ; 3, exam1. obj exam2. obj exam3. obj, C, 7. 2 C,, #,, ;, 3 : 7. 2. 1 ( 1 ) : #define, C, 138,
7 C,, : #define R 10. 5 #define PI 3. 1415926 : circle = 2* PI* R; : circle = 2* 3. 1415926* 10. 5;, 7. 4 #define WIDTH 60 #define LENGTH WIDTH + 40 #define PR printf { int area1, area2, area3; area1 = WIDTH* 5; / * area1 = 60* 5; * / area2 = LENGTH* 5; / * area2 = WIDTH + 40* 5; area2 = 60 + 40* 5; * / area3 = WIDTH* LENGTH; / * area2 = 60 * WIDTH + 40; area2 = 60 * 60 + 40; * / PR( " area1 = % d, area2 = % d, area3 = % d \ n", area1, area2, area3) ; / * printf( " area1 = % d, area2 = % d, area3 = % d \ n", area1, area2, area3) ; * / PR( " WIDTH = % d, LENGTH = % d \n", WIDTH, LENGTH) ; / * printf(width = % d, LENGTH = % d \ n", 60, 60 + 40) ; * / : area1 = 300, area2 = 260, area3 = 3640 WIDTH = 60, LENGTH = 100 :, printf PR, 60 WIDTH, LENGTH WIDTH, WIDTH,, LENGTH, : #define LENGTH ( WIDTH + 40) area2 = LENGTH* 5; area2 = ( 60 + 40) * 5 area3 = WIDTH* LENGTH; area3 = 60* ( 60 + 40 ),, printf( ) 139
C WIDTH LENGTH : ( 2 ) : : #define ( ) #define AREA( x) x* x + 5 z = AREA( 10) ;, 10 x,, : 7. 5 z = 10* 10 + 5; #define POWER( x) ( ( x) * ( x) ) / * x * / #define MAX( x, y) ( x) > ( y)? ( x) : ( y) / * x y * / #define PR printf { int a, b, c, d, x; a = 5; b = 10; x = 2250; c = POWER( a + b) ; / * c = ( ( a + b) * ( a + b) ) ; * / x = x / POWER( a + b) ; / * x = x/ ( ( a + b) * ( a + b) ) ; * / d = MAX( a + 6, b) ; / * d = ( a + 6) > ( b)? ( a + 6)( b) ; * / PR( " c = % d, d = % d, x = % d \ n", c, d, x) ; : c = 225, d = 11, x = 10 : #define POWER( x) ( ( x) * ( x) ), : : #define POWER( x) ( x) * ( x) / * printf( " c = % d, d = % d, x = % d \ n", c, d, x) ; * / c = POWER( a + b) ; c = ( a + b) * ( a + b) ; c 225 x = x / POWER( a + b) ; x = x/ ( a + b) * ( a + b) ; x 2250 #define POWER( x) x* x c = POWER( a + b) ; c = a + b* a + b; c 65 x = x / POWER( a + b) ; x = x/ a + b* a + b; x 510, 140,
7 C,,,,,,, #undef : 7. 2. 2, 1 : #include " ",,,, 2 : #include < >,,,,, 7. 8, : 7. 8 file2. c file1. c #include " stdio. h" stdio. h stdio. h, type include, include 1 2 include, 2 3 include, 1 3 2 141
C,, #include 7. 6 cpublic. c: #define PI 3. 1415926 #define YES 1 #define NO 0 #define PR printf file1. c: cpublic. c, #include " cpublic. c" { 7. 2. 3 : #if 1 #else 2 #endif,,, 1, 2 ; 2,, : #define DEBUG 1 #if DEBUG printf( " a = % d \ n", a) ; printf( " x = % f \n", x) ; #endif, DEBUG : #define DEBUG 0, : 1) 1 #ifdef 1 #else 2 #endif 142
7 C : ( #define ), 1, 2 : #ifdef IBM_PC #define INTEGER_SIZE 16 #else #define INTEGER_SIZE 32 #endif IBM_PC, INTEGER_SIZE 16 2 ) 2 #ifndef 1 #else 2 #endif :, 1, 2 1 ) 7. 3 7. 7, #include " stdio. h" #define SWAP( x, y) { int t; t = x; x = y; y = t; { int a, b, c; printf( " a, b, c: " ) ; scanf( " % d, % d, % d", &a, &b, &c) ; if ( a < b) SWAP( a, b) if ( a < c) SWAP( a, c) if ( b < c) SWAP( b, c) printf( " a, b, c : % d, % d, % d \ n", a, b, c) ; : a, b, c: 9, 3, 5 a, b, c : 9, 5, 3 7. 8, #define LEAP_YEAR( year) year%4 ==0 && year% 100! = 0 year%400 ==0 / * * / { int year, month, day, n; do{ printf( " - - : " ) ; 143
C scanf( " % d - % d - % d", &year, &month, &day) ; if ( month < 1 month > 12 day < 1 day > 31 year < 0) printf( ",! \ n" ) else break; while( 1) ; / * * / n = days( year, month, day) ; / * * / printf( % d \ n, n) ; / * * / int days( int y, int m, int d) { int n = 0, mt[ ] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ; / * mt * / : if ( LEAP_YEAR( y) ) mt[ 2] = 29; / * 2 * / for( i = 1; i < m; i + + ) n + = mt[ i] ; / * * / n + = d; / * * / return n; / * * /.. : 2000-3 - 5 65 7. 1, C 7. 2 swap( a, b),, 7. 3 #define max( a, b) ( ( a) > ( b)? ( a) : ( b) ) max( x, max( y, z) ) 7. 4 : #define MULT1( a, b) a* b #define MULT2( a, b) ( a) * ( b) y = MULT1( 3 + 5, 6 + 4 ) ; z = MULT2( 3 + 5, 6 + 4) ; y z? 7. 5 C 7. 6 7. 7,? 7. 8,, 7. 9,, 144
7 C 7. 9 145
8 8. 1,, :, ;,,,,? C, ( Struct) 8. 1. 1, : struct student { char name[ 10] ; ; char sex[ 2] ; int age; int score;, ( : ), 4 ( : ) : ( name) ( sex) ( age) ( score ), struct C, student, student, struct, int char, : 146 struct stedent stu1, stu2; /* stu1, stu2* /
8 struct student, : struct stedent stua[ 100], * pstu;, stua struct student, pstu struct student,, ( ) C, : struct { 1 ; ; 2;...... n; C, C, : struct date / * struct date * / { int year; ; int month; int day; struct student_1 { char name[ 10] ; ; char sex[ 2] ; struct date birthday; / * birthday * / int score; struct student_1 stu3, stu4;, struct student_1 birthday struct date,, struct date, stu3, stu4 C,, : struct student { char name[ 10] ; char sex[ 2] ; int age; int score; stu1, stu2 = {" Wang", " ", 25, 88 ; / * stu1 stu2, stu2 * /,, : 147
C struct / * * / { char name[ 10] ; char sex[ 2] ; int age; int score; stu1, stu2 = {" Wang", " ", 25, 88;, stu1 stu2, stu2, ;,,,,,,,, 8. 1. 2 ( 1 ),,,,, struct student stu1, : stu1. name[ 10] = " wang" ; stu1. sex[ 2] = " " ; stu1. age = 18; stu1. score = 95;.,,, :. : aver = ( stu1. score + stu2. score) /2; / * * / stu1, : scanf( " % s, % s, % d, % d", stu1. name, stu1. sex, &stu1. age, &stu1. score) ; printf( " % s, % s, % d, % d", stu1. name, stu1. sex, stu1. age, stu1. score) ;, struct student_1 stu3, : stu3. name[ 10] = " wang" ; stu3. sex[ 2] = " " ; stu3. birthday. year = 1980; stu3. birthday. month = 10; stu3. birthday. day = 18; stu3. score = 95;, stu3 birthday,, 148
8.,,,, C, ( 2 ),, : ; ; : ;, 8. 1, strut stu_score / * stu_score, * / { char name[ 8] ; / * 4 * / ; int english; int chinese; int math; int stu_aver_score( strut stu_score stuv) ; /* * / { strut stu_score stus, * stusp, stusa[ 100] ; int aver; scanf( " % s, % d, % d, % d", stus. name, &stus. english, &stus. chinese, &stus. math) ; aver = stu_aver_score( stus) ; / *, * / printf( " % s, % d, % d, % d, aver is: % d \ n", stus. name, stus. english, stus. chinese, stus. math, aver) ; int stu_aver_score( strut stu_score stuv) { int aver_score; aver_score = ( stuv. english + stuv. chinese + stuv. math) /3; / *, * / return( aver_score) ; / * * / : Enter a name and score for english, chinese, math: Lihua, 69, 75, 84 Lihua, 69, 75, 84, aver is: 76 8. 1. 3 C ( ), ( 1 ) stua: strut student 149
C { char name[ 8] ; int age; int score; ; struct student stua[ 100] ; stua 100 struct student,,, : for( i = 0; i < 100; i + + ) scanf( " % s, % d, % d", stua[ i]. name, &stua[ i]. age, &stua[ i]. score) ; 100, ( 2 ) stup stua stuv: strut student { char name[ 8] ; int age; int score; ; struct student stuv, * stup, stua[ 100] ;,, stup : stup = &stuv; stup stuv, stup stuv, stup stuv : ( * stup). ame = " Wang" ; ( * stup). age = 18; ( * stup). score = 97; :( * ).,,, ( * stup ). name * stup. name, * ( stup. name),,, C ( ), : stup - > name = " Wang" ; stup - > age = 18; stup - > score = 97 ; - >, - > stup - > score stup score, stua : for( i = 0; i < 100; i + + ) / * i int * / scanf( " % s, % d, % d", ( stua + i) - > name, &( stua + i) - > age, &( stua + i) - > score) ; 150
8 : for( stup = stua; stup < stua + 100; stup + + ) /* stup struct student * * / scanf( " % s, % d, % d", stup - > name, &stup - > age, &stup - > score) ; 8. 1. 4,, C,, struct mystruct, 8. 1 : 8. 1 Struct mystruct struct mystruct Struct mystruct struct mystruct struct mystruct & struct mystruct struct mystruct struct mystruct Struct mystruct struct mystruct 8. 2, 8. 1, strut stu_score / * stu_score, * / { char name[ 8] ; / * 4 * / int english; int chinese; int math; ; int stu_aver_score( strut stu_score * stuvp) ; / * * / { strut stu_score stus, * stusp; int aver; printf( " \ nenter a name and score for english, chinese, math: " ) ; scanf( " % s, % d, % d, % d", stus. name, &stus. english, &stus. chinese, &stus. math) ; stusp = &stus; aver = stu_aver_score( stusp) ; / * aver = stu_aver_score( &stus) ; * / printf( " % s, % d, % d, % d, aver is: % d \ n", stus. name, stus. english, stus. chinese, stus. math, aver) ; int stu_aver_score( strut stu_score * stuvp) / *, * / { int aver_score; 151
C aver_score = ( stuvp - > english + stuvp - > chinese + stuvp - > math) /3; / * * / return( aver_score) ; / * * / : Enter a name and score for english, chinese, math: Lihua, 69, 75, 84 Lihua, 69, 75, 84, aver is: 76, 8. 2 8. 1, 8. 2 8. 2 8. 3 ) :, ( struct box_type / * * / { int bl; / *, * / ; int bw; / * * / int bh; / * * / float price; / * * / struct box_type input( ) ; / *, * / float getval( struct box_type box2) ; / *, * / { struct box_type box; / * box * / float value; / * box * / box = input( ) ; / * box, * / value = getval( box) ; / * box, * / printf(box\ s long: % d, width: % d, high: % d, material price: % f, value is % f\ n, box. bl, box. bw, box. bh, box. price, value) struct box_type input( ) / *, * / { struct box_type box1; 152 printf( " \ nenter box\ s long, width, high and material price: " ) ; scanf( " % d, % d, % d, % f ", &box1. bl, &box1. bw, &box1. bh, &box1. price ) ; return ( box1) ; / * * / float getval( struct box_type box2) / *, * / { int bs; float tv; bs = box2. bl* box2. bh* 6 + box2. bw* box2. bh* 2; / * * / tv = bs* box2. price; / * * / return ( tv) ;
8 : Enter box s long, width, high and material price: 5, 4, 4, 2. 5 Box s long: 5, width: 4, high: 4, material price: 2. 500000, value is 380. 0000 8. 4 8. 3, struct box_type / * * / { int bl; / * * / ; int bw; / * * / int bh; / * * / float price; / * * / void input( struct box_type * p_box1) ; / *, * / float getval( struct box_type * p_box2) ; / *, * / { struct box_type box; / * box * / float value; / * box * / input( &box) ; / * box, * / value = getval( &box) ; / * box, * / printf( " \ nbox \ s long: % d, width: % d, high: % d, material price: % f, value is % f", box. bl, box. bw, box. bh, box. price, value ) void input( struct box_type * p_box1) / *, * / { printf( " \ nenter box\ s long, width, high and material price: " ) ; scanf( " % d, % d, % d, % f", &p_box1 - > bl, &p_box1 - > bw, &p_box1 - > bh, &p_box1 - > price ) ; return; / * * / float getval( struct box_type * p_box2) / *, * / { int bs; float tv; bs = p_box2 - > bl* p_box2 - > bh* 6 + p_box2 - > bw* p_box2 - > bh* 2; / * * / tv = bs* p_box2 - > price; / * * / return ( tv) ; : Enter box s long, width, high and material price: 5, 4, 4, 2. 5 Box s long: 5, width: 4, high: 4, material price: 2. 500000, value is 380. 0000, 8. 4 8. 3, 8. 4 8. 5 #define N, 5 8. 1, 5, 5 ( ) strut stu_score / * stu_score, * / { char name[ 8] ; / * 4 * / int english; 153
C int chinese; int math; ; strut stu_score aver_score( strut stu_score stusav[ ], int n) ; / * * / void stu_input( strut stu_score * stup, int n) ; / * * / { strut stu_score stusa[ N] ; strut stu_score aver ; /* aver * / stu_input( stusa, N) ; / *,, * / aver = aver_score( stusa, N) ; / *,, * / printf( " Average english is: % d, chinese is % d, math is % d \ n", aver. english, aver. chinese, aver. math) ; void stu_input( strut stu_score * stup, int n) / *, * / { int i; for( i = 0; i < N; i + +, stup + + ) { printf( " \ nenter a name and score for english, chinese, math: " ) ; scanf( " % s, % d, % d, % d", stup - > name, &stup - > english, &stup - > chinese, &stup - > math) ; strut stu_score aver_score( strut stu_score stusav[ ], int n) / *, * / { strut stu_score aver1 = { " ", 0, 0, 0 ; / * * / int i; for( i = 0; i < n; i + + ) ; /* * / { aver1. english = aver1. english + stusav[ i]. english; aver1. chinese = aver1. chinese + stusav[ i]. chinese; aver1. math = aver1. math + stusav[ i]. math; aver1. english = aver1. english / n; / * * / aver1. chinese = aver1. chinese / n; aver1. math = aver1. math / n; return ( aver1) ; / * * / : Enter a name and score for english, chinese, math: Lihua, 69, 75, 84 Enter a name and score for english, chinese, math: Wang, 70, 83, 64 Enter a name and score for english, chinese, math: Huwei, 79, 88, 77 Enter a name and score for english, chinese, math: Yanzi, 90, 70, 83 Enter a name and score for english, chinese, math: Mali, 92, 89, 87 Average english is: 80, chinese is 81, math is 79 8. 5, 154
8 8. 3 C : strut student { char name[ 8] ; ; int age; int score; strut student * next; / * * /, ( strut student ) next,, strut student next, 8. 1 head ne xt 8. 1, next,, ( nod), head struct student,, next, NULL,,,, 8. 3. 1 C C, ( 1), ( ), ( ) ( ) ( NULL ) ( ),,,,,,,,, ( ) ( ) ( 2),, ( table),,, 155
C :,, 3 : ; ;,, 13 2 A,,,,,,,,, 8. 3. 2,, ( LIFO) ( FIFO), ( 1),,,, NULL,,, 8. 2 : head : strut student { char name[ 8] ; ; int age; int score; strut student * next; ne xt 8. 2, : struct student struct student * ps ; / * ps * / for( ps = head; ps! = NULL; ps = ps - > next ) / * * / NULL printf( " % s, % d, % d \ n", ps - > name, ps - > age, ps - > score) ; / * * / : void search( struct student * ps) /* head * / { if( ps = = NULL ) / * * / printf( " The end! " ) ; /* * / else { printf(% s, % d, % d \ n, ps - > name, ps - > age, ps - > score) ; / * * / search( ps - > next) ; / * * /, ps, ps, ps, 156
8 ( ),,, ( ),, : struct student * ps; for( ps = head; ps - > next! = NULL; ps = ps - > next ) / *, * / / * * / printf( " % s, % d, % d \ n", ps - > next - > name, ps - > next - > age, ps - > next - > score) ; ( 2),,,,, struct stendent, : struct student * pt ; pt = ( struct student* ) malloc( sizeof( struct student) ) ; / *, * / scanf( " % s, % d, % d", pt - > name, &( pt - > age), &( pt - > score) ) ; / * * / pt - > next = head; / * * / head = pt; / * head * / : struct student * pt, * ps; pt = ( struct student* ) malloc( sizeof( struct student) ) ; / * * / scanf( " % s, % d, % d", pt - > name, &( pt - > age), &( pt - > score) ) ; / * * / for( ps = head; ps - > next! = NULL; ps = ps - > next) ; / *, ps * / ps - > next = pt; / * * / pt - > next = NULL; /* * / :,, : for( ps = head; ps! = NULL; ps = ps - > next) ; ps NULL,, : if( head == NULL) / * * / { head = pt; / *, head * / else pt - > next = NULL; { for( ps = head; ps - > next! = NULL; ps = ps - > next) ; / *, ps * / ps - > next = pt; / * * / pt - > next = NULL; /* * /, struct student, age,,,, :,, 8. 3 struct student, head struct student * pt, * ps ; pt = ( struct student* ) malloc( sizeof( struct student) ) ; /* * / 157
C 8. 3 pt - > name = " Wang" ; / * * / pt - > age = 18; pt - > score = 85; for( ps = head; ps - > next! = NULL && ps - > next - > age < = 18; ps = ps - > next) ; / *, age 18, ps * / / * age 18, ps * / if( ps - > next == NULL) / * * / { ps - > next = pt; / *, * / pt - > next = NULL; / * * / else / * * / { pt - > next = ps - > next; / * * / ps - > next = pt;, : age 18, ps,,,,,, ( ), ( 3) :, ; 8. 4 8. 4 : struct student, age 18 struct student * pt, * ps; for( ps = head; ps - > next! = NULL && ps - > next - > age! = 18; ps = ps - > next) ; / *, age 18, ps * / / * age 18, ps * / if( ps - > next = = NULL) /* * / printf( "! " ) ; else / * * / 158
8 { pt = ps - > next; / * pt, ps * / ps - > next = ps - > next - > next; / * * / free( pt) ; / * * /, ( ) 8. 3. 3,, 8. 6 100, 3,, 30 35 struct employee / * * / { char name; int age; int salary; struct employee * next; ; int creat( struct employee * phead) ; / * * / void display( struct employee * phead) ; / * * / int delete( struct employee * phead) ; / * * / struct employee * head = NULL ; / * * / { int i; for( i = 0; i < 100; i ++ ) / * * / if(! creat( head) ) / * * / break; / * * / display( head) ; / * * / for( ; delet( head) ; ) / * * / display( head) ; / * * / int creat( struct employee * phead) / * * / { struct employee * ps, * pt; if( phead == NULL) / * phead * / { phead = ( struct employee * ) malloc( sizeof( struct employee) ) ; phead - > next = NULL; / * phead * / if( ( pt = ( struct employee * ) malloc( sizeof( struct employee) ) ) = = NULL) ; / * * / { printf( " error! " ) ; / * * / return( 0) ; / * * / printf( " Enter a employee name, age, salary: " ) ; scanf( " % s, % d, % d", pt - > name, &( pt - > age), &( pt - > salary) ) ; / * * / for( ps = phead; ps - > next! = NULL && ps - > next - > age < = pt - > age; ps = ps - > next) ; / *, age, ps * / 159
C /* age, ps * / if( ps - > next = = NULL) / * * / { ps - > next = pt; / * * / pt - > next = NULL; / * * / else / * * / { pt - > next = ps - > next; / * * / ps - > next = pt; head = phead; / * * / return( 1) ; / * * / void display( struct employee * phead) / * * / { struct employee * ps; if( phead = = NULL) / * phead * / else printf( " No massage! " ) ; { for( ps = phead; ps - > next! = NULL && ps - > next - > age < = pt - > age; ps = ps - > next) /* * / printf( " % s, % d, % d \ n", ps - > next - > name, ps - > next - > age, ps - > next - > salary) ; int delete( struct employee * phead) / * * / { struct student * pt, * ps ; for( ps = phead; ps - > next! = NULL && ( ps - > next - > age > = 30 && ps - > next - > age < = 35) ; ps = ps - > next; / *, age 30-35, ps * / / * age 30-35, ps * / if( ps - > next = = NULL) / * * / { printf( "! " ) ; return( 0) ; else / * * / { pt = ps - > next; / * pt * / ps - > next = ps - > next - > next; / * * / free( pt) ; head = phead; / * * / return( 1) ; 8. 3. 4, : : 160
8 strut student { char name[ 8] ; int age ; int score ; strut student * next ; / * * / strut student * last ; / * * / ; 8. 5, ( strut student ) next last,, strut student next last, 8. 5, last NULL,, next NULL,, ( ),, ( last ) ( next ),, : strut student { char name[ 8] ; int age, score; strut student * left; / * * / strut student * right; / * * / ;, ( strut student ) left right, 8. 6,, ( ) ( root) : void search( struct student * ps) / * ps * / { if ( ps! = NULL) { search( ps - > left) ; printf( " % s, % d, % d \n", ps - > name, ps - > age, ps - > score) ; search( ps - > right) ;,,,,, C,, C C,,,, 161
C 8. 6 :, C ( Object Oriented Programming OOP), OOP ( Object), ( ), ( Class), ( Object) ( Instance), 8. 4 8. 4. 1, C, : : union data { int n; ; char c; float f; union { 1; ; 2 ;...... n ;, 3 : n c f union C, data data, union 162
8, int char, : union data d1, d2, d3[ 100], * pd;, d1 d2 union data, d3 union data, d3 union data, pd union data,,, n c f, ( ), ( ) ( ),,,, C, 8. 4. 2,,., union data d1, : d1. n = 25; : d1. c = A ; : d1. f = 25. 78;,, &d1 &d1. n &d1. c &d1. f,, : d1. n = 25; d1. c = A ; d1. f = 25. 78; 3, d1. f = 25. 78,,, :,, 8 double, : union temp { double d; ; char db[ 8] ; union temp dsen, drec; double m, : dsen. d = m; dsen. db[ 0 ] dsen. db[ 7 ], drec. db[ 0] drec. db[ 7], 8, drec. d double 163
C 8. 5,,,, : enum weekday { sun, mon, tue, wed, thu, fri, sat ; /* * / enum weekday workday, otherday; / * * / 1, 7, sun mon... satenum C, weekday weekday, enum 2 enum weekday workday otherday, sun mon tue wed thu fri sat : workday = mon; otherday = sun; sun mon... sat,, sun, sd, :, C : sun = 0; mon = 1;, C 0, 1, 2,, sun 0, mon 1,, sat 6: workday = mon; printf( " % d", workday) ; 1, : enum weekday { sun = 7, mon = 1, tue, wed, thu, fri, sat; sun 7, mon sat 1 6 : ( ) : if( workday > otherday) if( workday = = mon) : workday = ( enum weekday) 2; workday = tue;, 164
8 8. 7 A B C D E { enum sclass {a = 9, b = 8, c = 7, d = 6, e = 5 ; / * * / enum sclass sc; /* * / int score, n; printf( " \ nenter a score: " ) ; scanf( " % d", &score) ; / * score * / for( ; score > 100 score < 0; ) ; / * score * / { printf( " \ nscore must between 0 100, Please againe: " ) ; scanf( " % d", &score) ; / * * / n = ( score - score% 10) /10; / * int n * / n = n > 9? 9: n; / * n * / n = n < 5? 5: n; sc = ( enum sclass) n; / * n enum sclass * / switch( sc) { case a: peintf( " \ nthe class is A" ) ; break; case b: peintf( " \ nthe class is B" ) ; break; case c: peintf( " \ nthe class is C" ) ; break; case d: peintf( " \ nthe class is D" ) ; break; case e: peintf( " \ nthe class is E" ) ; break; default: break; : Enter a score: 89 The class is B : Enter a score: 599 Score must between 0 100, Please againe: 59 The class is D 8. 6 typedef C, typedef ( C ), : typedef int INTEGER; typedef float REAL; INTEGER intreal float, : INTEGER a, b, c; : int a, b, c; 165
C REAL x, y, z; : float x, y, z;, INTEGER REAL? : : struct date { int month; ; : int int typedef struct day; year; { int month; int int DATE; day; year;, DATE, DATE, : DATE birthday; : struct date birthday; DATE : struct date * * pdate; pdate;,, C typedef : typedef int INTARR[ 100] ; / * INTARR 100 * / INTARR n; : int n[ 100] ; typedef char * STRING; / * STRING char * * / STRING p, s[ 10] ; : char * p, * s[ 10] ; /* p, s * /, typedef,, C : typedef,,, typedef typedef #define, : typedef int INTEGER; #define INTEGER int INTEGER int, #define,, typedef, C, typedef,, 166
8 8. 1 C?? 8. 2,,? 8. 3 student, 8. 4 8. 3 student, sdate, student 8. 5,, 8. 6 8. 3 student, 8. 7,,, 8. 8 8. 7, ( ) 8. 9,,, ( ) ( ) ( FIFO), 167
9, C,, C ( bit) C, C ( bit),, ( Word) ( Byte),, bit ; 4, 4 bit, bit 1 0,,, C, ( ) C 9. 1 C 9. 1 9. 1 : 9. 1,, & ( int or Word) ( char or Byte) ^, ( float) ( 1 ) ( &) << &, 1, >> 1, 0, : 0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1; : 3&5 1, : 168
9 3 : 00000011 & 5 : 00000101 : 1 : 00000001 C : char a = 3, b = 5, c ; c = a & b;, c 1 ( bit) 0, ( bit), : a( 2 ) 0: a = a & oxff ; / * byte, byte * / a( 2 ) 3 0 1: c = a & ox04 ; / * c 1 1, 0 * / ( 2 ) ( ), 1, 1, 0, : 0 0 = 0; 0 1 = 1; 1 0 = 1; 1 1 = 1; : 3&5 7, : 3 : 00000011 5 : 00000101 : 7 : 00000111 C : char a = 3, b = 5, c ; c = a b;, c 7 ( bit) 1, : char a 4 ( bit) 1 : a = a 0xf0 ; /* 4 1, 4 * / char a 3 ( bit) 1 : a = a 0x04 ; / * 3 ( bit) 1, * / ( 3 ) ( ^) ^, ( ), 1, 0 : 0 ^ 0 = 0; 0^1 = 1 ; 1 ^ 0 = 1; 1 ^ 1 = 0; : 3&5 6, : 3 : 00000011 ^ 5 : 00000101 : 6 : 00000110 C : char a = 3, b = 5, c ; c = a ^ b; 169
C, c 6 : 0, 1 ( bit), 1 0, 0 1, : char a 4 ( bit) : a = a ^ 0xf0 ; / * 4, 4 * / char a 3 ( bit) : a = a ^ 0x04 ; /* 3 ( bit), * / ( 4 ) ( ),,, ( bit), 1 0, 0 1, : 0 = 1; 1 = 0; : 9 0xf6, : 9 : 00001001 : : 11110110 C : char a = 9, c ; c = a;, c 0 xf6, 0, : a = a & 1; / * * / ( 5 ) ( << ) <<, ( bit), 0 : 3 1 6, : << 3 : 00000011 : 6 : 00000110 C : char a = 3, c ; c = a << 1; a 2, : << 3 : 00000011 2 : 00 00001100 C : char a = 3, c; c = a << 2;, c 12, ( 1 ), 1, 2 ( 6 ) ( >> ) >>, ( bit) : 170
9 12 6, : >> 12 : 00001100 : 6 : 00000110 C : char a = 12, c; c = a >> 1;, c 6, a 2, 3, ( 0) ( 1 ), 1, 2 0,, 0, 1 ( ), 0 1?, 0 ; 1, 0x8c,, 0xc6,, 0x46 ( 7 ) 1 ) ( int char ), C :,, : int a = 17; char b = - 3; a = a ^ b; a b, ( - 3 11111101) : a : 0000000100000001 ^ b : 1111111111111101 a : 1111111011111100 2 ) C C : a = a & b; a & = b; c = c >> 1; c >> = 1;, = ^= << = C 9. 2,,, 1 bit, 1,, 9. 1, a b c d 2 6 5 3 bit,,, C?,, ( bit), unsigned : 171
C struct pack_data { unsigned a : 2; / * " : " 2 2 bit, * / ; unsigned b : 6; unsigned c : 5; unsigned d : 3; int n; struct pack_data data_1; data: a b c d 15 14 13 8 7 3 2 0 9. 1 data_1 struct pack_data, 5, 4, 4 a b c d 2 :,, data_1 data_1. a = 2; data_1. b = 7; data_1. c = 25; data_1. d = 6; data_1. n = 100; data_1: 10 000111 11001 110 0000000001100100 a b c d n 9. 2 data_1 data _ 1 4 : 10000111110000000000111001100100 ( 9. 2 ), : struct pack_data { unsigned a : 2; / * " : " 2 2 bit * / ; unsigned b : 3; unsigned c : 4; unsigned d : 3; int n; struct pack_data data_2; data_2: a b c d n 1 1 2 9. 3 data_2 data_2 a b c d 12 bitc,, a b 5, 3, 172
9 8 c d 7, 1 data_2 9. 3, : data_1. a = 8;, 3,,, data_1. a 0, 1, C, :,, : struct pack_data { unsigned a : 3; ; unsigned b : 6; /* 5 bit, b * / unsigned c : 2;, : struct pack_data { unsigned a : 3; ; unsigned : 0; / * * / unsigned b : 6; / * b * / 8,, : printf( " % d, % d", data_1. a, data_1. b) ;, % u % o % x, : data_1. a + 6 /data_1. b 9. 3 9. 1? 1 char a, 3 ( 1 0 ),, 1 ( &), 0, 0, int test( char a) { if( a & 8) / * 8 3 1, 0 * / printf( " switch is on! " ) ; 173
C 9. 2 else printf( " switch is off! " ) ; return a 4 4, 12 4 4, 12 4 4, { int a, b, c; printf( " Input a: " ) ; scanf( " % d", &a ) ; / * a @ @@ @ &&&&, @ & 4, 8 4 * / printf( " Before a = % xh \ n", a) ; / * a * / b = a; / * b * / c = a & 0x0ff0; / * c 00000000, a 8 * / a << = 12; / * a &&&&000000000000, 4 4 * / b >> = 12; / * b 000000000000@ @ @ @, 4 4 * / a = b; / * a &&&&00000000@ @@ @ * / a = c; / * a &&&& @ @ @ @ * / printf( " After a = % xh \ n", a) ; / *a * / : Input a: 115 Before a = 73H After a = 3070H,, : { struct data 174 { unsigned a : 4; ; unsigned b : 4; unsigned c : 4; unsigned d : 4; union u_data { struct data m; ; int n; union u_data v; scanf( " % d", &v. n ) ; /* v. n * / printf( " \ nbefore is: % d \ n", v. n ) ; v. m. a ^= v. m. d; v. m. d ^= v. m. a; v. m. a ^= v. m. d; / * v. m. a v. m. d * / printf( " \ nafter is: % d \ n", v. n ) ;
9 : C, 9. 1 16 ( D0 D2 D4 D14 ), 9. 2, 9. 3 : 4 4, 9. 4 getbit, int bit : unsigned getbit( int value, int n1, int n2) : int value n1 n2 9. 5 8. 9 1bit,, 1,,, 8. 9 5
1 0 10. 1 10. 1. 1 C ( file) ( ),, PC, ( byte),,, 3 : ( text),,, ASCII, GB,,,, ( CPU), CPU CPU,, CPU ( ),,,,,,. ini,,, C,,? 176
10 C ( ),,, ( ASCII ),,, 100, 2 ( 00000000 01100100), 3 ( 00110001 00110000 00110000), 1 0 0 ASCII,,,,,,,, CPU,,,, C, :, : 10. 1 C,,,,, 10. 1, C ( ANSI C), C 10. 1. 2 C,,,? FILE : FILE * fp ; fp FILE?, stdio. h : typedef struct { int _fd ; / * * / int _cleft ; / * * / int _mode ; / * * / char * _nextc ; / * * / char * _buff ; / * * / FILE ;, FILE,, 177
C FILE C, FILE FILE fp, FILE, C FILE 10. 2,,,, 10. 2. 1 ( fopen ) C, fopen( ), stdio. h, : FIILE* fp ; fp = fopen(, ) ; : fp = fopen( " mydat. dat", " r" ) ; " mydat. dat", " r" : fopen( ) mydat. dat ( ), FILE, fp, fp mydat. dat, fopen( ) NULL,, : if ( ( fp = fopen( " mydat. dat", " r" ) ) == NULL) { 10. 1, fopen ( ) 3 : " r" " w" ( ) " a" ( ) " rb" ( char* ) " wb" " ab" ( ),, " r + " /, : " w + " / fp = fopen( " a: \\ data \\ mydat. dat", " r" ) ; " a + " / ( ) a data mydat. dat " rb + " / ( char* ) " wb + " /, 10. 1 " ab + " / ( ) 178
10 : " r" " rb" " r + " " rb + ",, fopen ( ) NULL " r" " rb" ( ), " r + " " rb + " ( ) ( ) " w" " wb" " w + " " wb + ",, fopen ( ) ( ), fopen( ), " w" " wb" ( ), " w + " " wb + " ( ) ( ) " a" " ab" " a + " " ab + ", ( ), fopen ( ) NULL " a" " ab" ( ), " a + " " ab + " ( ) ( ),,,, C, 3 : 3 stdin stdout stderr C : stdout = fopen( " prn", " w" ) ;, printf( ), " prn" 10. 2. 2 ( fclose ),, C fclose( ), : : fclose( ) ; fclose( fp) ; / * fp * / fp?,, fp, fp,,,,,,, 3, 179
C 10. 3, C, stdio. h, ( # include) stdio. h 10. 3. 1 fputc fgetc ( 1 ) fputc ( ) : fputc( ch, fq) ; ch char, fp FILE, ch fp( fopen ), fputc ch, EOFEOF stdio. h, - 1 stdio. h : # define putchar( c) fputc( c, stdout), putchar( ),, fputc( ), : fputc( ), ( 2 ) fgetc ( ) : ch = fgetc( fp) ; ch char, fp FILE, fp( fopen ) ( ), ch fputc( ), fgetc( ), fgetc( ),,, fgetc( ) EOF,, : ch = fgetc( fp) ; / * fp * / while( ch! = EOF) / *, * / { putchar( ch) ; ch = fgetc( fp) ;, - 1, C ( ) 180
10, feof( ) feof( ) : while(! feof( fp) ) / * fp * / { c = fgetc( fp) ; feof( fp) 1( ), 0 ( ),,, c,, stdio. h : # define getchar( c) fgetc( c, stdin), getchar( ),, fgetc( ), ( 3 ) 10. 1,, # # include " stdio. h" { FILE * fp ; : char ch, fname[ 10] ; printf( " \ nenter a filename: " ) ; scanf( " % s", fname ) ; if ( ( fp = fopen( fname, " w" ) ) = = NULL) / * ( ) * / { printf ( " Cannot open file \ n" ) ; exit ( 0) ; / *, * / while ( ( ch = getchar( ) )! = " #" ) { fputc( ch, fp) ; / * * / putchar( ch ) ; / * * / printf( " \ no. K! \ n" ) ; / * * / fclose( fp) ; / * * / Enter a filename: mywords. c ( ) I love china # I love china O. K! ( ) ( ),, fname : C stdio. h : #define putc( ch, fp) #define getc( fp) fputc( ch, fp) fgetc( fp), putc( ) getc( ) fputc( ) fgetc( ) 181
C 10. 3. 2 fwrite fread fputc( ) fgetc( ) C, fputc ( ) fgetc( ), C, fwrite( ) fread( ) : ( 1 ) fwrite : fwrite( buffer, size, count, fp) ; buffer:, size: int, ( ) count: int, ( size ) fp: FILE fp( fopen) : struct student { char name[ 8] ; ; int age ; int score ; char addr[ 30] ; struct student stu ; fwrite( &stu, sizeof( struct student), 1, fp) ; / * stu fp * /, stu struct student, fwrite( ) : fwrite( ), count ( 2 ) fread : fread( buffer, size, count, fp) ; buffer:, buffer 182 size: int, ( ) count: int, ( size ) fp: FILE ( ) struct student, : struct student stu ; fread( &stu, sizeof( struct student), 1, fp) ; / * stu* /
10 fread( ), count, fread( ),,, fwrite( ), ( 3 ) 10. 2 5, student. dat #include " stdio. h" #define SIZE 5 struct student { char name[ 8] ; int age ; int score ; char addr[ 30] ; ; int savedata( struct student stua[ ], int m) ; volid read_print( ) ; { struct student stua[ SIZE] ; int n ; for ( n = 0; n < SIZE; n + + ) scanf( " % s, % d, % d, % s", stua[ n]. name, &stua[ n]. age, &stua[ n]. score, stua[ n]. addr) ; if( savedata( stua, SIZE) ) read_print( stua, SIZE) ; int savedata( struct student stua[ ], int m) /* savedata : student. dat, m stua * / { FILE* fp ; int i ; if( ( fp = fopen( " student. dat", " wb" ) ) == NULL ) /* * / { printf ( " cannot open out file! \ n" ) ; return ( 0) ; / *, 0* / for ( i = 0; i < m; i + + ) fwrite( &stua[ i], sizeof( struct student), 1, fp) ; / * stua[ i] * / fclose( fp) ; / * * / return ( 1) ; / *, 1* / volid read_print( ) /* read_print( ) : student. dat * / { struct student stu ; FILE * fp ; if( ( fp = fopen( " student. dat", " rb" ) ) = = NULL ) / * * / 183
C { printf ( " cannot open out file! \ n" ) ; exit ( 0) ; printf( " \ n" ) ; while(! feof( fp) ) { fread( &stu, sizeof( struct student), 1, fp) ; / * struct student * / printf( " % s, % d, % d, % s\ n", stu. name, stu. age, stu. score, stu. addr) ; fclose( fp) ; / * * / fread fwrite, C, ( ), fread fwrite, fread fwrite, fread,, ( ) ( buffer ), fread fwrite C,, 10. 3. 3 ( fprintf fscanf ) fprintf( ) fscanf( ) printf( ) scanf( ), : printf( ) scanf( ), fprintf( ) fscanf( ) : : fprintf(,, ) ; fscanf(,, ) ; fprintf( fp, " % s: % 6. 2f", name, score) ; / * fp * / char* name float score,, fp name " Wang" score 652. 34, : Wang: 652. 34 : fprintf( stdout, " % s: % 6. 2f", name, score) ; : printf( " % s: % 6. 2f", name, score) ;, fscanf( ) : fscanf ( fp, " % s: % f", name, &score) ; / * fp * / fp : 184 Wang: 652. 34
10 " Wang" name, 652. 34 score, fprintf( ), fscanf( ),,, fprintf( ),,, fprintf( ),,,, fprintf( ) fscanf( ) 10. 3 10. 2 student. dat student. rep # include " stdio. h" struct student { char name[ 8] ; ; int age ; int score ; char addr[ 30] ; { struct student stu ; FILE * dat, * rep ; / * * / if ( ( dat = fopen( " student. dat", " rb" ) ) = = NULL) /* * / { printf ( " cannot open out file! \ n" ) ; return ( 0) ; if ( ( rep = fopen( " student. rep", " w" ) ) = = NULL) / * * / { printf ( " cannot open out file! \ n" ) ; return ( 0) ; while(! feof( dat) ) { fread( &stu, sizeof( struct student), 1, dat) ; /* struct student * / fprintf( rep, " % s, % d, % d, % s \n", stu. name, stu. age, stu. score, stu. addr) ; /* student. rep* / printf( " % s, % d, % d, % s\ n", stu. name, stu. age, stu. score, stu. addr) ; fclose( dat) ; fclose( rep) ; 10. 3. 4 fgets fputs /* * / fgets( ) fputs( ) fgets( ) : fgets( str, n, fp) ; /* str char*, n int, fp * / : n - 1, str 185
C n - 1,, \0 str ( str ) fputs( ) : fputs( " china", fp ) ; / * fp * / : " china" fp 0, 0 fgets( ) fputs( ) gets( ) puts( ), : gets( ) puts( ), fprintf( ) fscanf( ) : fputs( " china", stdout ) ; : puts( " china" ) ; 10. 4 C,,,, : ( C ), " r" " rb" " r + " " rb + " " w" " wb" " w + " " wb + ",,,,,, C 10. 4. 1 rewind rewind( ) rewind( ) 10. 4 10. 2 student. dat, 186 # include " stdio. h" struct student { char name[ 8] ; ; int age ; int score ; char addr[ 30] ; { struct student stu ; FILE* fp ; / * * / scanf( " % s, % d, % d, % s\ n", stu. name, &stu. age, &stu. score, stu. addr) ;
10 / * * / if ( ( fp = fopen( " student. dat", " ab + " ) ) = = NULL) / * " ab + " * / { printf ( " cannot open out file! \ n " ) ; return ( 0) ; fwrite( &stu, sizeof( struct student), 1, fp) ; / * stu * / / * * / rewind( fp ) ; / * * / while(! feof( fp) ) { fread( &stu, sizeof( struct student), 1, fp) ; / * struct student * / printf( " % s, % d, % d, % s\ n", stu. name, stu. age, stu. score, stu. addr) ; / * * / fclose( fp ) ; 10. 4. 2 fseek,, rewind( ),, C, fseek( ) : fseek( ) : fseek( fp, 100L, 0) ; fseek(,, ) ; : fp 100, 101 : fp FILE*, 100L long ( C, L, long ),,, C long 3, 0 1 2, 0, 1, 2 stdio. h : #define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 fseek( ) 3 fseek( ),, fseek( ) : fseek( fp, 100L, 0) ; / * 100 * / 187
C fseek( fp, 1000L, 1) ; / * 1000 * / fseek( fp, 150L, 2) ; / * 150 * / 10. 5 10. 2 student. dat, student. dat struct student, struct student 42, name 8,, 34 # include " stdio. h" struct student { char name[ 8] ; int age ; int score ; char addr[ 30] ; ; { char stu_name[ 8] ; FILE* fp ; / * * / if ( ( fp = fopen( " student. dat", " ab + " ) ) == NULL) / * " ab + " * / { printf ( " cannot open out file! \ n " ) ; return ( 0) ; while(! feof( fp) ) { fread( stu_name, 8, 1, fp) ; / * name * / printf( " % s\ n", stu_name) ; / * * / rewind( fp, 34L, 1 ) ; / * 34 * / fclose( fp ) ; 10. 4. 3 ftell ftell( ), ftell( ) long, - 1 L : s = ftell( fp ) ; / * s long, fp FILE* * / if ( s == - 1L) printf( " error! \ n" ) ; s ( ), ( ), " error! " 10. 5 188,, Windows
10, DOS C 10. 5. 1 ferror C, ( ),,, ferror( ) : ferorr( fp) ;, ferror( ) 0 ( ),, 0 ( ),, ferorr( ), ferror( ), ferror( ) : 10. 6 10. 1, #include " stdio. h" { FILE * fp ; char ch, fname[ 10], err_flag = 0 ; / * err_flag * / printf( " \ nenter a filename: " ) ; scanf( " % s", fname ) ; if ( ( fp = fopen( fname, " w" ) ) == NULL ) / * ( ) * / { printf ( " cannot open file \ n " ) ; exit ( 0) ; while ( ( ch = getchar( ) )! = " #" ) { fputc( ch, fp) ; / * * / if ( ferorr( fp ) ) / * * / { err_flag = 1 ; / * * / break ; putchar( ch ) ; / * * / if ( err _flag) printf( " \ nwrite disk err! " ) ; / * * / else printf( " \ no. K! " ) ; / * * / : Enter a filename: mywords. c ( ) I love china # ( ) I love china ( ) O. K! 189
C 10. 5. 2 clearerr clearerr ferror( ) 0( ),, clearerr( ),, rewind( ), 10. 6 10. 7 10. 1 ( mywords. c) ( youwords. c) DOS COPY, # include " stdio. h" main( int argc, char* argv[ ] ) { FILE* f_in, * f_out ; char databyte ; if ( argc < 3 ) / * * / { printf( " Please enter command again! \ n" ) ; / *, * / exit( 0) ; if ( ( f_in = fopen( argv[ 1], " rb" ) ) = = NULL ) / * * / { printf ( " cannot open in file! \ n " ) ; exit ( 0) ; if ( ( f_out = fopen( argv[ 2], " wb" ) ) = = NULL ) / * * / { printf ( " cannot open out file! \ n " ) ; exit ( 0) ; for( ;! feof( f_in ) ; fputc( fgetc( f_in ), f_out) ) ; / *, * / fclose( f_in) ; fclose( f_out) ; printf( " \ no. K! \n " ) ; / * * / / * * / for( f_out = fopen( argv[ 2], " r" ) ;! feof( f_out ) ; putchar( fgetc( f_out ) ) ) ; printf( " Is all right? \ n" ) ; / * * / exam. c, exam. exe, : exam mywords. c youwords. c ( ) O. K! I love china ( ) Is all right? 10. 8 muti. rep, : 1* 1 = 1 190
10 1* 2 = 2 2* 2 = 4 1* 3 = 3 2* 3 = 6 3* 3 = 9 1* 9 = 9 9* 9 = 81,, # include " stdio. h" { int m, n ; FILE * rep ; /* * / if( ( rep = fopen( " muti. rep", " w + " ) ) = = NULL) /* * / { printf ( " cannot open out file! \ n" ) ; return ( 0) ; for ( m = 1 ; m < = 9; m + + ) { for ( n = 1 ; n < = m; n ++ ) fprintf( rep, " % d* % d = % d", n, m, n* m) ; / * muti. rep* / fprintf( " \ n" ) ; /* * / rewind( rep) ; / * * / for ( ;! feof( rep) ; putchar( fgetc( rep) ) ) ; / * * / fclose( rep) ; / * * / 10. 9 10. 2 student. dat, # include " stdio. h" struct student { char name[ 8] ; int age ; int score ; char addr[ 30] ; ; { struct student * stu, * top_stu ; / * top_stu * / int top_score = 0 ; / * * / FILE * fp ; / * * / if ( ( fp = fopen( " student. dat", " rb" ) ) = = NULL) / * " rb" * / { printf ( " cannot open out file! \ n " ) ; return ( 0) ; fread( top_stu, sizeof( struct student), 1, fp) ; / * top_stu 1 * / while(! feof( fp) ) { fread( stu, sizeof( struct student), 1, fp) ; / * stu * / 191
C if( stu - > score > top_stu - > score ) /* * / top_stu = stu ; /* top_stu * / printf( " % s, % d, % d, % s\ n", top_stu - > name, top_stu - > age, top_stu - > score, top_stu - > addr) ; /* * / rewind( fp) ; /* * / while(! feof( fp) ) /* * / { fread( stu, sizeof( struct student), 1, fp) ; / * struct student * / printf( " \ n% s, % d, % d, % s", stu - > name, stu - > age, stu - > score, stu - > addr) ; / * * / fclose( fp ) ; 10. 1, C 10. 2 10. 3, ( #) 10. 4 10. 3 10. 5, ( # ), ( ASCII 4), 10. 6, ( ), ( ) 10. 7 5, 3, 5 3,, stud 10. 8 10. 7 stud 5, stud_sort 10. 9 10. 8 stud _sort 5 stud_aver 192
1 1 C C,, C C, MS-DOS Windows MS-DOS 11. 1 PC ROM-BIOS, ( ) ROM-BIOS, DOS,,, ( ), ;,, C,,,, Windows DOS, API( ), Windows SDK Windows 11. 1. 1 ROM-BIOS C ROM-BIOS, int86( ), : int int86( int IntNo, union REGS* r_in, union REGS* r_out) : IntNo ; r_in REGS, ; r_out, : 193
C int86( ) AX int86( ) dos. h, REGS dos. h : struct WORDREGS{ unsigned int ax, bx, cx, dx, si, di, cflag, flags; ; struct BYTEREGS{ unsigned char al, ah, bl, bh, cl, ch, dl, dh; ; union REGS{ struct WORDREGS x; struct BYTEREGS h; ; REGS h x, ax al ah ROM-BIOS 11. 1 PC, 1AH, 02H 04H, AH ;, CH CL DH DL, ( BCD ), 4 : #include " stdio. h" #include " dos. h" { union REGS r_in, r_out; r_in. h. ah = 0x04; / * 04H, * / int86( 0x1A, &r_in, &r_out) ; / * 1AH, * / printf( " date: % 2x% 02x - % x - % x\ n", r_out. h. ch, r_out. h. cl, r_out. h. dh, r_out. h. dl) ; / * * / r_in. h. ah = 0x02; / * 02H, * / int86( 0x1A, &r_in, &r_out) ; / * 1AH, * / printf( " time: % 2x: % 02x: % 02x \ n", r_out. h. ch, r_out. h. cl, r_out. h. dh) ; 11. 2, / * : : * /, 25 80,, ROM-BIOS 10H,, 194 10H 02H,, : AH = 02H DH ( 0-24) DL ( 0-79) BH, 0 09H,, : AH = 09H BH, 0 AL BL, CX, 0, 8 : 7 : 1, 0 7 6 5 4 3 2 1 0
11 C 6-4 :, : 000 001 010 011 100 101 110 111 3 : 1, 0 2-0 :,, : #include " stdio. h" #include " dos. h" void SetCursorPosition( int row, int column) ; / * * / void PutCharProperty( char c, char property) ; /* * / { SetCursorPosition( 12, 20) ; / * 12 20 * / PutCharProperty(A, 0x41) ; / * 01000001,,, * / void SetCursorPosition( int row, int column) { union REGS r_in, r_out; / * * / r_in. h. dh = row% 25; r_in. h. dl = column% 80; r_in. h. bh = 0; / * 0* / r_in. h. ah = 0x02; / * 02H, * / int86( 0x10, &r_in, &r_out) ; / * 10H * / void PutCharProperty( char c, char property) { union REGS r_in, r_out; r_in. h. al = c; / * * / r_in. h. bl = property; / * * / r_in. h. bh = 0; / * 0* / r_in. h. cx = 1; / * 1* / r_in. h. ah = 0x09; / * 09H, * / int86( 0x10, &r_in, &r_out) ; / * 10H * /, 11. 1. 2 DOS DOS, 21H, DOS DOS, int86( ), 0x21, intdos( ), 21H, : int intdos( union REGS* r_in, union REGS* r_out) : r_in, ; r_out, : 195
C intdos( ) AX intdos( ) dos. h DOS 11. 3 36H, : AH = 36H DL, 0, 1 A, 2 B, 3 C,, : AX, FFFFH BX ( ) CX DX C : #include " dos. h" #include " stdio. h" { union REGS r_in, r_out; long free; r_in. h. dl = 0x03; / * C * / r_in. h. ah = 0x36; / * 36H* / intdos( &r_in, &r_out) ; /* DOS 36H* / printf( " Total number of clusters: % u \ n", r_out. x. dx) ; / * * / printf( " Number of available cluters: % d \n", r_out. x. bx) ; /* * / printf( " Sectors per cluster: % d \ n", r_out. x. ax) ; / * * / printf( " Bytes per sector: % d \ n", r_out. x. cx) ; / * * / free = 1L* r_out. x. bx* r_out. x. ax* r_out. x. cx/1024; printf( " Free total: % ldkb\ n", free) ; / * KB * / 11. 4 DOS 47H, : AH = 47H DL, 0, 1 A, 2 B, DS: SI, : DS: SI ASCII, PC C, DS, C DS, SI C : #include " stdio. h" #include " dos. h" { union REGS r_in, r_out; char buffer[ 65] ; r_in. h. ah = 0x47; /* 47H* / r_in. h. dl = 0x03; / * C * / r_in. x. si = ( int) buffer; / * * / 196
11 C intdos( &r_in, &r_out) ; / * DOS 47H* / if ( r_out. x. cflag) / * * / printf( " Drive number is invalid! \ n" ) ; else printf( " Current directory is: \ \ % s \ n", buffer) ; / * C * / 11. 2 C dos. h inportb( ) outportb( ), PC inportb( ),, outportb( ) 11. 1 PC 2, 2, 0x43 0x42 2 2 11. 1 PC 8255 B D1 2, D0 D1 D0 1,, ; D1 D0 0,, #include " dos. h" #include " stdio. h" void MySound( unsigned frequency, long delay) ; / * * / { MySound( 262, 19123456L) ; MySound( 294, 19123456L) ; MySound( 330, 19123456L) ; MySound( 349, 19123456L) ; MySound( 392, 19123456L) ; MySound( 440, 19123456L) ; MySound( 493, 19123456L) ; MySound( 523, 19123456L) ; /*, frequency ; delay,, 197
C * / void MySound( unsigned frequency, long delay) { union { unsigned value; struct { unsigned char low; unsigned char high; h; LSB; unsigned char temp; long i; LSB. value = 1192576ul / frequency; / * * / outportb( 0x43, 0xB6) ; / * 2 3* / outportb( 0x42, LSB. h. low) ; / * * / outportb( 0x42, LSB. h. high) ; / * * / temp = inportb( 0x61) ; / * 8255 B temp * / outportb( 0x61, temp 0x03) ; / * temp 1 B, * / for( i = 0; i < delay; i + + ) ; / * * / outportb( 0x61, temp&0xfc) ; / * temp 0 B, * / 11. 3, PC, Turbo C 2. 0DOS ( ), Windows API Direct Draw, Open GL Turbo C 2. 0, C, graphics. h 11. 3. 1 ( 1) void initgraph( int* graphdriver, int* graphmode, char* pathtodriver) ;,, initgraph( ) 3 graphdriver ( ), graphmode ( ), pathtodriver, BGI, EGAVGA. BGIEGA VGA, TC2. 0 198
11 C TC2. 0, EGAVGA. BGI graphics. h : enum graphics_drivers / * define graphics drivers* / { DETECT, /* requests autodetection* / ; CGA, MCGA, EGA, EGA64, EGAMONO, IBM8514, / * 1-6* / HERCMONO, ATT400, VGA, PC3270, / * 7-10* / CURRENT_DRIVER = - 1 enum graphics_modes / * graphics modes for each driver* / {...... / * CGA EGA * / ; VGALO = 0, / * 640200 16 color 4 pages* / VGAMED = 1, / * 640350 16 color 2 pages* / VGAHI = 2, / * 640480 16 color 1 page* / PC3270HI = 0, / * 720350 1 page* / IBM8514LO = 0, / * 640480 256 colors* / IBM8514HI = 1 / * 1024768 256 colors* /, VGA 3, VGALO( 640200, 16, 4 ) VGAMED( 640350, 16, 2 ) VGAHI( 640480, 16, 1 ) VGAMED, EGAVGA. BGI C TC, : int gd = VGA, gm = VGAMED; initgraph( &gd, &gm, " C: \ \ TC" ) ;,, : int gd = DETECT, gm; initgraph( &gd, &gm, " " ) ; ( 2) void closegraph( void) ;,, initgraph( ), ( ) : closegraph( ) ; ( 3) : void setactivepage( int page) : void setvisualpage( int page), VGALO, 4 ; VGAMED, 2 ; VGAHI, 1,,, ( 0 199
C ), VGALO, 2, : setactivepage( 1) ; setvisualpage( ), 1 : setvisualpage( 0) ; ( 4) void cleardevice( void) ;, ( 0, 0) 3, : setactivepage( 2) ; / * 3 * / cleardevice( ) ; / * ( 3 ) * / 11. 3. 2 ( 1) void setcolor( int color), void setbkcolor( int color) graphics. h, : enum COLORS { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE ;, : setcolor( GREEN) ; : setcolor( 2) ; 2 GREEN,, : setbkcolor( BLUE) ; setcolor( 1) ; ( 2) void putpixel( int x, int y, int color) ( x, y) ( 100, 80), : putpixel( 100, 80, RED) ; unsigned getpixel( int x, int y) ( 20, 30), : c = getpixel( 20, 30) ; 200
11 C, ( 20, 30) c ( 3) : void line( int x1, int y1, int x2, int y2) ; void lineto( int x, int y) ; void moveto( int x, int y) ; void setlinestyle( int linestyle, unsigned user_pattern, int thickness) ; line( ) ( 10, 20 ) ( 150, 50), : line( 10, 20, 150, 50) ; lineto( ), ( 200, 80), : lineto( 200, 80) ; moveto( ), lineto( ) moveto( ), ( 50, 0) ( 100, 50) ( 60, 80), : moveto( 50, 0) ; lineto( 100, 50) ; lineto( 60, 80) ; lineto( 50, 0) ; 11. 1 SOLID_LINE DOTTED_LINE CENTER_LINE DASHED_LINE USERBIT_LINE 0 1 2 3 4 setlinestyle( ), linestyle 11. 1 linestyle USERBIT _LINE, user_pattern ( 16 ) user_pattern, 1, 0 linestyle USERBIT_LINE, user_pattern 0 thickness : NORM_WIDTH( 1) THICK_ WIDTH( 3 ) NORM_ WIDTH, THICK_WIDTH, : setlinestyle( DOTTED_LINE, 0, NORM_WIDTH) ; ( 4) : void circle( int x, int y, int radius) ; void ellipse( int x, int y, int stangle, int endangle, int xradius, int yradius) ; circle( ), ( 100, 80), 30( ), : circle( 100, 80, 30) ; ellipse( ) ( x, y), stangle, endangle, xradius yradius x y : ellipse( 150, 100, 0, 180, 40, 30) ; 201
C ( 5) : void rectangle( int left, int top, int right, int bottom) ; void drawpoly( int numpoints, int* polypoints) ; rectangle( ), ( left, top), ( right, bottom) : rectangle( 10, 20, 80, 50) ; drawpoly( ),,, numpoints, polypoints, ( x, y), : int v[ ] = {50, 10, 100, 10, 120, 60, 30, 60, 50, 10 ; drawpoly( 5, v) ; 11. 3. 3 ( 1) : void setfillpattern( char* upattern, int color) ; void setfillstyle( int pattern, int color) ; setfillpattern ( ) 11. 2 upattern ( 88 ) 8, color : EMPTY_FILL SOLID_FILL LINE_FILL 0 1 2 char pattern [ ] = { 0xFF, 0x81, LTSLASH_FILL 3 / / / 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF ; setfillpattern( pattern, BLUE) ; setfillstyle ( ) pattern 11. 2,, setfillpattern( ), setfillstyle( ) SLASH_FILL BKSLASH_FILL LTBKSLASH_FIL HATCH_FILL XHATCH_FILL INTERLEAVE_FILL WIDE_DOT_FILL CLOSE_DOT_FILL USER_FILL 4 5 6 7 8 9 10 11 12 / / / \ \ \ \ \ \ USER_FILL, : setfillstyle( HATCH_FILL, YELLOW) ; ( 2) void floodfill( int x, int y, int border) ; border, ( x, y), border ( ), : setcolor( GREEN) ; circle( 80, 60, 30) ; 202
11 C floodfill( 80, 63, RED) ; ( 3) : void bar( int left, int top, int right, int bottom) ; void fillpoly( int numpoints, int* polypoints) ; bar( ) rectangle( ),, bar( ), fillpoly( ) drawpoly( ),, fillpoly( ), ( 4) : void fillellipse( int x, int y, int xradius, int yradius ) ; void pieslice( int x, int y, int stangle, int endangle, int radius) ; void sector( int x, int y, int stangle, int endangle, int xradius, int yradius ) ; fillellipse( ) ellipse( ), fillellipse( ), sector( ) fillellipse( ) : fillellipse( ), sector( ), stangle, endangle, sector( ), ellipse( ) pieslice( ) sector( ) : pieslice( ), sector( ) ( xradius yradius sector( ) ), : sector( 120, 120, 90, 180, 30, 40) ; 11. 3. 4 ( 1) viewport,,,, ( 0, 0) void setviewport( int left, int top, int right, int bottom, int clip) ; ( left, top) ( ), ( right, bottom) ( ) clip, clip 0,, ; clip 0,,, : setviewport( 50, 50, 200, 200, 1) ; ( 2) : void outtext( char* textstring) ; void outtextxy( int x, int y, char* textstring) ; void settextstyle( int font, int direction, int charsize) ; 203
C outtext( ) textstring, outtextxy( ) ( x, y) textstring settextstyle( ) 11. 3 font TC2. 0, 11. 3 88, DEFAULT_FONT 0 88 CHR TRIPLEX_FONT 1, TRIP. CHR SMALL_FONT 2, *. CHR SANS_SERIF_FONT 3 initgraph( ), GOTHIC_FONT 4 direction, : HORIZ_DIR( 0) VERT_DIR( 1) HORIZ_DIR, VERT_DIR charsize, 0 10, 3,, " China", : settextstyle( SMALL_FONT, HORIZ_DIR, 3) ; outtextxy( 20, 20, " China" ) ; TC2. 0,, putpixel( ), ( 3) : void getimage( int left, int top, int right, int bottom, void* bitmap) ; void putimage( int left, int top, void* bitmap, int op) ; unsigned imagesize( int left, int top, int right, int bottom) ; getimage( ) ( left, top), ( right, bottom), bitmap imagesize( ) 11. 4 putimage( ) getimage( ) ( left, top) COPY_PUT 0, bitmap getimage ( ) XOR_PUT 1, op, OR_PUT 2 11. 4 AND_PUT 3 NOT_PUT 4, : void* buffer; /* * / unsigned s; / * * / s = imagesize( 20, 30, 50, 65) ; / * * / buffer = malloc( s) ; / * * / getimage( 20, 30, 50, 65, buffer) ; / * buffer * / 204 putimage( 100, 100, buffer, COPY_PUT) ; / * * /
11 C free( buffer) ; / * * /, : void* buffer; / * * / unsigned s; /* * / s = imagesize( 20, 30, 50, 65) ; / * * / buffer = malloc( s) ; / * * / getimage( 20, 30, 50, 65, buffer) ; / * buffer * / putimage( 100, 100, buffer, COPY_PUT) ; / * * / free( buffer) ; / * * / 11. 3. 5 11. 5 #include < graphics. h > #include < math. h > #define PI 3. 14159 { int gd = VGA, gm = VGAHI; int d, k, x1, y1, x2, y2; int color = 1; double a, e; initgraph( &gd, &gm, " C: \ \ TC" ) ; /* * / d = 100; setcolor( color) ; for ( a = 0; a < = 2* PI; a + = PI /360) { e = d* ( 1 + 0. 25* sin( 4* a) ) ; e = e* ( 1 + sin( 8* a) ) ; x1 = 320 + e* cos( a) ; x2 = 320 + e* cos( a + PI /8) ; y1 = 200 + e* sin( a) /2; y2 = 200 + e* sin( a + PI /8) /2; line( x1, y1, x2, y2) ; / * * / color + + ; / * color * / if( color > 15) color = 1; setcolor( color) ; / * * / getch( ) ; / *, * / closegraph( ) ; / * * / 11. 6 #include " graphics. h" #include " dos. h" #include " conio. h" 205
C #define XR 30 /* XR * / #define YR 28 / * YR * / #define XMAX 640 / * XMAX * / #define YMAX 480 / * YMAX * / void drawball( int x, int y) ; { int gd = VGA, gm = VGAHI; 206 int x = XR, y = YR; /* x y * / int xincrease = 5, yincrease = 3; / * xincrease yincrease * / initgraph( &gd, &gm, " c: \ \ tc" ) ; / * * / x = 100, y = 100; / * ( x, y) * / while(! bioskey( 1) ) { long i; drawball( x, y) ; / * ( x, y) * / for( i = 0; i < 999999L; i + + ) ; / * * / x + = xincrease; / * x xincrease * / y + = yincrease; / * y yincrease * / if( x > = XMAX - XR x < XR) / * * / { xincrease = - xincrease; / * xincrease, * / x + = xincrease; if( y > = YMAX - YR y < YR) / * * / { yincrease = - yincrease; / * yincrease, * / y + = yincrease; cleardevice( ) ; / * * / closegraph( ) ; / * * / void drawball( int x, int y) / * ( x, y) * / { static int color = 1; int currentcolor; int i; currentcolor = color; color + + ; if( color > 15) color = 1; for( i = 0 ; XR - 3* i > 0 && YR - 3* i > 0 ; i + + ) { setcolor( currentcolor) ; setfillstyle( SOLID_FILL, currentcolor) ; fillellipse( x, y, XR - 3* i, YR - 3* i) ; currentcolor ++ ; if( currentcolor > 15) currentcolor = 1;
11 C, drawball( ), color, drawball( ), color,, bioskey( ),, 1, 0, ( cleardevice( ) ),,,, initgraph( ) *. BGI *. BGI C: TC 207
A ASCII ASCII ASCII ASCII ASCII 000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 NULL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 ( space)! # % & ( ) * +, _. / 0 1 2 3 4 5 6 7 8 9 : ; < = >? 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ - 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 a b c d e f g h i j k l m n o p q r s t u v w x y z { DEL BEL DLE EM BS DC1 1 SUB NULL HT DC2 2 ESC SOH LF DC3 3 FS STX VT DC4 4 GS ETX FF NAK RS EOT CR SYN US ENQ SO ETB DEL ACK SI CAN 208
B C auto break case char const continue default do double else enum estern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while C C C, ANSI C, ( 1 ) : #include " math. h" abs int abs( int x) x acos double acos( double x ) arccos( x) x - 1 1 asin double asin( double x ) arcsin( x) x - 1 1 atan double atan( double x ) arctan( x) atan2 double atan2 ( double x, double y) arctan( x/ y) cos double cos( double x) cos( x) x cosh double cosh( double x) x cosh( x) exp double exp( double x) e x fabs double fabs( double x) x floor double floor( double x) x fmod double fmod ( double x, x / y double y) val x frexp double frexp ( double val, 2 n, x int* eptr) val = x* 2 n, n eptr 0. 5x < 1 log double log( double x) ln x log10 double log10( double x) log 10 x 209
C modf val double modf ( double val, val, iptr int* iptr) pow double pow ( double x, double y) x y rand int rand( void) - 90 32767 sin double sin( double x) sin x x sinh double sinh( double x) x sinh( x) sqrt double sqrt( double x) x x 0 tan double tan( double x) tan( x) x tanh double tanh( double x) x tanh( x) ( 2 ) " ctype. h" isalnum int isalnum( int ch) ch 1, 0 isalpha int isalpha( int ch) ch 1, 0 iscntrl int iscntrl( int ch) ch ( ASI- CII 0 0x1F ) 1, 0 isdigit int isdigit( int ch) ch ( 0-9) 1, 0 ch ( isgraph int isgraph( int ch) ASICII 0x21 0x7F 1, 0 ), islower int islower( int ch) ch ( a - z) 1, 0 ch ( isprint int isprint( int ch) ASICII 0x20 0x7F 1, 0 ), ch ( ispunct int ispunct( int ch) ), 1, 0 isspace int isspace( int ch) ch ( 1, 0 ) isupper int isupper( int ch) ch ( A - Z) 1, 0 isxdigit int isxdigit( int ch) ch 1, 0 ( 0-9 a - f A - F) tolower int tolower( int ch) ch ch toupper int toupper( int ch) ch ch ( 3 ) " string. h" 210
strcat strchr strcmp strcpy strlen strstr char * strcat ( char * str1, char* str2) char* strchr( char* str, int ch) int strcmp ( char * str1, char* str2) char * strcpy ( char * str1, char* str2) unsigned int strlen ( char * str) char * strstr ( char * str1, char* str2) str2 str1, str2 \0 str1 str,, ch str1 str2 str2 str1 str1 < str2, str1 = str2 0; str1 > str2 str1 str ( \ 0 ) str2 str1,, ( str2 ) ( 4 ) : #include" stdio. h" clearerr void clearer( FILE* fp) close int close( int fp) 0, ANCI - 1 creat int creat ( char * filename,, ANCI mode int mode) - 1 eof int eof( int fd) 1, ANCI 0 fclose int fclose( FILE* fp) fp 0, 0 feof int feof( FILE* fp), 0 fgetc int fgetc( FILE* fp) fp, EOF fgets fp buf, int* fgets ( char * buf, int ( n - 1 ), n, FILE* fp) buf NULL fopen FILE* fopen( char mode ( * filename, char* mode) filename ), 0 args format fprintf int fprintf( FILE* fp, char * format, args,... ) fp 211
C fputc fputs fread fscanf fseek ftell fwrite getc getchar getw open printf putc putchar puts int fputc( char ch, FILE * fp) int fputs ( char * str, FILE * fp) int fread ( char * pt, unsigned size, unsigned n, FILE* fp) int fscanf( FILE * fp, char format, args,... ) int fseek ( FILE * fp, long offset, int base) long ftell( FILE* fp) int fwrite ( char * ptr, unsigned size, unsigned n, FILE* fp) int getc( FILE* fp) int getchar( void) int getw( FILE* fp) int open ( char * filename, int mode) int printf ( char * args,... ) int putc( int ch, FILE * fp) int putchar( char ch) int puts( char* str) format, ch fp ch, str fp, EOF 0,, 0 fp size n, pt fp format 0 args ( args ) fp, base, - 1 offset fp fp ptr n * size fp fp fp, EOF, - 1 ANCI fp, ( ) - 1 ( ) ANCI mode, filename - 1 format args,, ch fp ch,, EOF ch ch,, EOF str, \0, EOF 212
putw int putw( int w, FILE * fp) w( ) fp,, EOF ANCI read int read( int fd, char * buf, unsigned count) fd count buf 0-1 ANCI rename int rename ( char * oldname, char* newname) oldname, newname 0-1 fp rewind void rewind( FILE* fp), scanf int scanf ( char * args,... ) format, format, args args, EOF 0 args write int write( int fd, char * buf, unsigned count) buf count fd - 1 ANCI ( 5 ) ANCI C " stdlib. h", C " malloc. h" " stdlib. h" calloc void * calloc ( unsigned n, n,, unsigned size) size 0 free void free( void* p) p malloc void* malloc( unsigned size size), 0 realloc p void* realloc ( void * p, unsigned size) size, size D C, scanf ( ), scanf, & : scanf( " % d% d", x, b) ; 213
C,,, scanf C,, scanf : scanf( " % d% d", &x, &b) ; ( TAB ) printf scanf : int x = 1; float a = 2. 5; printf( " x = % f, a = % d \ n", x, a) ;, : X = 0. 000000, y = 16300 x, b, : scanf( " % f% d", &x, &b) ; : 10. 2 3 x b : int n; n = 37750; printf( " n = % d \ n", n) ;, n C, int - 32768 + 32767, 37750,,, long, : 4 long int n n = 37750; printf( " n = % ld \ n", n) ; scanf, : scanf( " % 5. 2f", &m) ; : 31. 14, n ( printf n ) C : 214 scanf( " % f", &n) ; : { m = 1; / * m n * / n = 2; : printf( " % d, % d \ n", m, n) ; { int b; B = 2; printf( " % d \ n", b) ;
C, C, b B, B b, B, : stu: struct stu { char n [ 10] ; float s; ; : stu. s = 67. 5; stu,, : { float x, y; scanf( " % f, % f", &x, &y) ; printf( " The maxmum number is % f", max( x, y) ) ;... float max( float a, float b) { if ( a < b) a = b; return( a) ;, : float max( float a, float b ) ;, C, / *... * / / * < =, > =, =! =, A. C,, : m = 1 n = 0;,, C B., : { if ( x > 0) x + + ; printf( " % d \ n", x) /* * / 215
C C., : struct st { char data; int next; / * * / : struct st { char data; ; int next; A., : int max( int x) ; / *, * / {..., B. : if( x > 0) ; x - - ; if( x > 0), ( x > 0),, x 0 x - - ; : for( i = 0; i < 10; i + + ) ; scanf( " % d \ n", &a[ i] ) ; a[ i] 10, for,, a[ i] ( i = 0),,,!, : main ( ) { int x, y, t; scanf( " % d, % d", &x, &y) ; if ( x < y) t = x; x = y; y = t; printf( " The largest number is: % d", x) ; if, ( x > y), x y,, t = x ( x > y), x = y; y = t;, : if ( x < y) {t = x; x = y; y = t;,, C 216
10 C,,, : char m; m = " a" ; 2 : m = a ; 11 " = " " == " C, " = ", " == " " = " : int a = 10; if( a = 1) printf( " a = % d", 1) ; else printf( " a! = % d", 1) ; : a = 1, C ( a = 1), 1 a a a 1,, a = 1 if if( a == 1) 12 C,,, : int a( 8) ; / *, C, int a[ 8] ; * / char b[ 5, 2] ; / *, char b[ 5] [ 2] ; * / a( 1) = 3; / *, a[ 1] = 3; * / 13 C, 0, 1 int a[ 5], i; for ( i = 1; i < 6; i + + ) scanf( " % d \ n", &a[ i] ) ; a[ 1] a[ 5 ] 5, a a[ 0] a[ 4], a[ 5], a[ 5] for : for( i = 0; i < 5; i + + ) 14 + + - - : int a = 10; printf( " % d \ n", a ++ ) ; printf, 11, 10 a + + a, a 1, + + a a 1, printf, 10 15, : int * p, i = 10; 217
C * p = i; / * p, p * / : int a, * p = &a, i = 10; / * p a* / * p = i;,, 16, : char* p, n; * p = &n; p * ; p *, * p p : * p = n; p = &n; 17 : int n = 0, * s = &n; float* q; s = q; s = q : s = ( int* ) q; 18,,, : int n, array[ 5] = {1, 2, 3, 4, 5; for ( n = 0; n < 5; n ++ ) printf( " % d \ n", * array + + ) ; : int n, array[ 5] = {1, 2, 3, 4, 5, * P; p = a; for ( n = 0; n < 5; n ++ ) printf( " % d \ n", * p + + ) ; 19, : f1 ( int a, int b) { c = a; if ( a < b) c = b; return( c) ; 218
, : f1( int a, int b) { int c;... 20 : { int a = 1, b = 3, f1( int a, int b) ; f1 ( int x, int y) f 1( a, b) ; { x + + ; y + + ; printf(% d, % d \ n, a, b) ; f1 a b 1,, a b 1 C,,, ( ), 21switch : scanf( " % d", &x) ; switch( x) { case 1: printf( " monday" ) ; case 2: printf( " tuseday" ) ; case 3: prlntf( " wednsday" ) ; case 4: printf( " thurthday" ) ; case 5: printf( " friday" ) ; case 6: prlntf( " saturday" ) ; case 7: prlntf( " sunday" ) ; default: prlntf( " error" ) ;, x 5, : friday saturday sunday error case break, case printf break, case, switch break continue continue break : { int n; for ( n = 100; n < 200; n + + ) 219
C { if ( n% 3 == 0) break; printf ( " % d \ n", n) ; 100 200 3, break, 3 n continue break 3 n, C,, C,,, 220