logo资料库

C程序设计语言(第二版)课后答案.doc

第1页 / 共334页
第2页 / 共334页
第3页 / 共334页
第4页 / 共334页
第5页 / 共334页
第6页 / 共334页
第7页 / 共334页
第8页 / 共334页
资料共334页,剩余部分请下载后查看
"The C Programming Language", 2nd edition, Kernigh
《c程序设计语言》英文的配套答案,所列页码均为英文版的。
Answer to Exercise 1-1
Answer to Exercise 1-2
Answer to Exercise 1-3
Answer to Exercise 1-4
Answer to Exercise 1-5
Answer to Exercise 1-6
Answer to Exercise 1-7
Exercise 1-8
Exercise 1-9
Exercise 1-10
Category 0
Category 1
Exercise 1-11
Exercise 1-12
Exercise 1-13
Exercise 1-14
Answer to Exercise 1-15, page 27
Answer to Exercise 1-16, page 30
Answer to Exercise 1-17, page 31
Answer to Exercise 1-18, page 31
Answer to Exercise 1-19, page 31
Answer to Exercise 1-20, page 34
Answer to Exercise 1-21, page 34
Answer to Exercise 1-22, page 34
Category 1 Solution
Answer to Exercise 1-23, page 34
Category 0 Solutions
Category 1 Solutions
Answer to Exercise 1-24, page 34
Answer to Exercise 2-1, page 36
Answer to Exercise 2-2, page 42
Answer to Exercise 2-3, page 46
Answer to Exercise 2-4, page 48
Answer to Exercise 2-5, page 48
Answer to Exercise 2-6, page 49
Answer to Exercise 2-7, page 49
Answer to Exercise 2-8, page 49
Answer to Exercise 2-9, page 51
Answer to Exercise 2-10, page 52
Answer to Exercise 3-1, page 58
Answer to Exercise 3-2, page 60
Answer to Exercise 3-3, page 63
Answer to Exercise 3-4, page 64
Answer to Exercise 3-5, page 64
Answer to Exercise 3-6, page 64
Answer to Exercise 4-1, page 71
Answer to Exercise 4-2, page 73
Answer to Exercise 4-3, page 79
Answer to Exercise 4-4, page 79
Answer to Exercise 4-5, page 79
Answer to Exercise 4-6, page 79
Answer to Exercise 4-7, page 79
Answer to Exercise 4-8, page 79
Answer to Exercise 4-12, page 88
Answer to Exercise 4-13, page 88
Answer to Exercise 4-14, page 91
Answer to Exercise 5-1, page 97
Answer to Exercise 5-2, page 97
Answer to Exercise 5-3, page 107
Answer to Exercise 5-4, page 107
Answer to Exercise 5-5, page 107
Exercise 5-6, page 107
Answer to Exercise 5-7, page 110
Exercise 5-8
Exercise 5-9, page 114
Answer to Exercise 8-6, page 189
Answer to Exercise 5-10, page 118
Answer to Exercise 5-11, page 118
Answer to Exercise 5-13, page 118
Answer to Exercise 5-14, page 121
Answer to Exercise 6-1, page 136
Answer to Exercise 6-3, page 143
Answer to Exercise 6-4, page 143
Answer to Exercise 6-5, page 145
Answer to Exercise 7-1, page 153
Answer to Exercise 7-2, page 155
Answer to Exercise 7-3, page 156
Answer to Exercise 7-6, page 165
Answer to Exercise 7-8, page 165
Answer to Exercise 7-9, page 168
Answer to Exercise 8-1, page 174
Answer to Exercise 8-3, page 179
Answer to Exercise 8-4, page 179
Answer to Exercise 8-6, page 189
"The C Programming Language", 2nd edition, Kernighan and Ritchie 《c 程序设计语言》英文的配套答案,所列页码均为英文版的。 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 1.23 1.24 2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09 2.10 3.01 3.02 3.03 3.04 3.05 3.06 4.01 4.02 4.03 4.04 4.05 4.06 4.07 4.08 4.12 4.13 4.14 5.01 5.02 5.03 5.04 5.05 5.06 5.07 5.08 5.09 5.10 5.11 5.13 5.14 6.01 6.03 6.04 6.05 7.01 7.02 7.03 7.06 7.08 7.09 8.01 8.03 8.04 8.06 Answer to Exercise 1-1 Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get. Murphy's Law dictates that there is no single correct answer to the very first exercise in the book. Oh well. Here's a "hello world" program: #include int main(void) { printf("hello, world\n"); return 0; } As you can see, I've added a return statement, because main always returns int, and it's good style to show this explicitly.
Answer to Exercise 1-2 Experimenttofindoutwhathappenswhenprintf'sargumentstringcontains \c, where c is some character not listed above. By 'above', the question is referring to: \n (newline) \t (tab) \b (backspace) \" (double quote) \\ (backslash) We have to tread carefully here, because using a non-specified escape sequence invokes undefined behaviour. The following program attempts to demonstrate all the legal escape sequences, not including the ones already shown (except \n , which I actually need in the program), and not including hexadecimal and octal escape sequences. #include int main(void) { printf("Audible or visual alert. \a\n"); printf("Form feed. \f\n"); printf("This escape, \r, moves the active position to the initial position of the current line.\n"); printf("Vertical tab \v is tricky, as its behaviour is unspecified under certain conditions.\n"); return 0; } Answer to Exercise 1-3 Modify the temperature conversion program to print a heading above the table. #include int main(void) { float fahr, celsius;
int lower, upper, step; lower = 0; upper = 300; step = 20; C\n\n"); printf("F fahr = lower; while(fahr <= upper) { celsius = (5.0 / 9.0) * (fahr - 32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } return 0; } Answer to Exercise 1-4 WriteaprogramtoprintthecorrespondingCelsiusto Fahrenheittable. #include int main(void) { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; F\n\n"); printf("C celsius = lower; while(celsius <= upper) { fahr = (9.0/5.0) * celsius + 32.0; printf("%3.0f %6.1f\n", celsius, fahr); celsius = celsius + step; } return 0; }
Answer to Exercise 1-5 Modifythetemperatureconversionprogramtoprintthetable inreverse order, that is, from 300 degrees to 0. This version uses a while loop: #include int main(void) { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; F\n\n"); printf("C celsius = upper; while(celsius >= lower) { fahr = (9.0/5.0) * celsius + 32.0; printf("%3.0f %6.1f\n", celsius, fahr); celsius = celsius - step; } return 0; } This version uses a for loop: #include int main(void) { float fahr, celsius; int lower, upper, step; lower = 0; upper = 300;
step = 20; F\n\n"); printf("C for(celsius = upper; celsius >= lower; celsius = celsius - step) { fahr = (9.0/5.0) * celsius + 32.0; printf("%3.0f %6.1f\n", celsius, fahr); } return 0; } Chris Sidi notes that Section 1.3 Has a short For statement example, and "Based on that example, I think the solution to 1.5: a) should do fahr to celsius conversion (whereas the solutions on your page do celsius to fahr) b) should be similar to the example and as small." He offers this solution: #include /* print Fahrenheit-Celsius table */ int main() { int fahr; for (fahr = 300; fahr >= 0; fahr = fahr - 20) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); return 0; } Answer to Exercise 1-6 Verify that the expression getchar() != EOF is 0 or 1. /* This program prompts for input, and then captures a character * from the keyboard. If EOF is signalled (typically through a * control-D or control-Z character, though not necessarily),
* the program prints 0. Otherwise, it prints 1. * * If your input stream is buffered (and it probably is), then * you will need to press the ENTER key before the program will * respond. */ #include int main(void) { printf("Press a key. ENTER would be nice :-)\n\n"); printf("The expression getchar() != EOF evaluates to %d\n", getchar() != EOF); return 0; } Answer to Exercise 1-7 Write a program to print the value of EOF . #include int main(void) { printf("The value of EOF is %d\n\n", EOF); return 0; } Exercise 1-8 Write a program to count blanks, tabs, and newlines. #include
int main(void) { int blanks, tabs, newlines; int c; int done = 0; int lastchar = 0; blanks = 0; tabs = 0; newlines = 0; while(done == 0) { c = getchar(); if(c == ' ') ++blanks; if(c == '\t') ++tabs; if(c == '\n') ++newlines; if(c == EOF) { if(lastchar != '\n') { ++newlines; /* this is a bit of a semantic stretch, but it copes * with implementations where a text file might not * end with a newline. Thanks to Jim Stad for pointing * this out. */ } done = 1; } lastchar = c; } printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines); return 0; } Exercise 1-9
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. #include int main(void) { int c; int inspace; inspace = 0; while((c = getchar()) != EOF) { if(c == ' ') { if(inspace == 0) { inspace = 1; putchar(c); } } /* We haven't met 'else' yet, so we have to be a little clumsy */ if(c != ' ') { inspace = 0; putchar(c); } } return 0; } Chris Sidi writes: "instead of having an "inspace" boolean, you can keep track of the previous character and see if both the current character and previous character are spaces:" #include
分享到:
收藏