分享

C语言程序设计现代方法第二版,第八章课后编程习题全部答案

 寒潭孤雁159 2019-12-09
  • 2018-07-18 

自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出

但这些代码确实已通过编译,实现了书上的输出结果,还希望能给需要之人作为个小参考


8_1

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int main (void)
  4. {
  5. bool digit_seen[10] = {false}, digit_wr[10] = {false}, flag = false;
  6. int digit;
  7. long n;
  8. printf ('Enter a number: ');
  9. scanf ('%ld', &n);
  10. while (n > 0) {
  11. digit = n % 10;
  12. if (digit_seen[digit] == true) {
  13. flag = true;
  14. digit_wr[digit] = true;
  15. }
  16. digit_seen[digit] = true;
  17. n /= 10;
  18. }
  19. if (flag) {
  20. printf ('Repeated digits(s): ');
  21. for (int i = 0; i < 10; i ) {
  22. if (digit_wr[i]) printf ('%d ', i);
  23. }
  24. }
  25. else
  26. printf ('No repeated digit\n');
  27. return 0;
  28. }

8_2

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main (void)
  5. {
  6. int digit_wr[10] = {0};
  7. int digit;
  8. int n;
  9. printf ('Enter a number:');
  10. scanf ('%d', &n);
  11. while (n > 0) {
  12. digit = n % 10;
  13. digit_wr[digit] ;
  14. n /= 10;
  15. }
  16. printf ('Digit:\t\t');
  17. for (int i = 0; i < 10; i ) {
  18. printf ('%3d', i);
  19. }
  20. printf ('\nOccurrences:\t');
  21. for (int i = 0; i < 10; i ) {
  22. printf ('%3d', digit_wr[i]);
  23. }
  24. printf('\n');
  25. system('pause');
  26. return 0;
  27. }

8_3

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. /* * * * * * * * * * * * * * * * * * * * * * * *
  4. * 连续输入一百以内数,当输入的数小于或等于0 *
  5. * 时程序终止。 *
  6. * * * * * * * * * * * * * * * * * * * * * * * */
  7. int main(void)
  8. {
  9. bool flag;
  10. int digit;
  11. int n;
  12. while (1) {
  13. //每次执行前将标志数组和标志变量初始化
  14. bool digit_seen[10] = {false};
  15. flag = false;
  16. printf ('Enter a number: ');
  17. scanf ('%d', &n);
  18. //如果输入是0跳出循环,结束
  19. if (n <= 0) break;
  20. while (n > 0) {
  21. digit = n % 10;
  22. if (digit_seen[digit] == true) flag = true;
  23. digit_seen[digit] = true;
  24. n = n / 10;
  25. }
  26. if (flag) {
  27. printf('Repeated digit\n');
  28. }
  29. else {
  30. printf('No repeated digit\n');
  31. }
  32. }
  33. return 0;
  34. }

8_4

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define SIZE (int)(sizeof (a) / sizeof (a[0]))
  5. #define N 10
  6. int main (void)
  7. {
  8. int a[N], i;
  9. printf ('Enter %d numbers: ', N);
  10. for (i=0; i < SIZE; i )
  11. scanf ('%d', &a[i]);
  12. printf ('In reverse order: ');
  13. for (i = SIZE - 1; i >= 0; i--)
  14. printf (' %d', a[i]);
  15. printf ('\n');
  16. printf('\n');
  17. system('pause');
  18. return 0;
  19. }

8_5

  1. #include <stdio.h>
  2. #define NUM_RATES ((int)(sizeof (value) / sizeof (value[0])))
  3. #define INITIAL_BALANCE 100.00
  4. int main (void)
  5. {
  6. int i, low_rate, num_years, year, month;
  7. double value[5];
  8. printf ('Enter interst rate: ');
  9. scanf ('%d', &low_rate);
  10. printf ('Enter number of years: ');
  11. scanf ('%d', &num_years);
  12. printf ('\nYears');
  13. for (i = 0; i < NUM_RATES; i ) {
  14. printf ('%6d%%', low_rate i);
  15. value[i] = INITIAL_BALANCE;
  16. }
  17. printf ('\n');
  18. for (year = 1; year <= num_years; year ) {
  19. printf ('%3d ');
  20. for (i = 0; i < NUM_RATES; i ) {
  21. for (month = 1; month <= 12; month ) {
  22. value[i] = ((double)(low_rate i) / 12) / 100.0 * value[i];
  23. }
  24. printf ('%7.2f', value[i]);
  25. }
  26. printf ('\n');
  27. }
  28. return 0;
  29. }

8_6

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main (void)
  4. {
  5. char mess[100];
  6. int i = 0, j;
  7. printf ('Enter message: ');
  8. while (((mess[i]) = getchar () ) != '\n') {
  9. i ;
  10. }
  11. printf ('In B1FF-speak: ');
  12. for (j = 0; j < i; j ) {
  13. if ((mess[j] <= 122) && (mess[j] >= 97)){
  14. mess[j] = toupper (mess[j]);
  15. }
  16. switch (mess[j]) {
  17. case 'A': printf ('4'); break;
  18. case 'B': printf ('8'); break;
  19. case 'E': printf ('3'); break;
  20. case 'I': printf ('1'); break;
  21. case 'O': printf ('0'); break;
  22. case 'S': printf ('5'); break;
  23. default : printf ('%c', mess[j]); break;
  24. }
  25. }
  26. printf ('!!!!!!!!!!');
  27. return 0;
  28. }

8_7

  1. #include <stdio.h>
  2. #define N 5
  3. int main(void)
  4. {
  5. int a[N][N] = {0};
  6. int i, j;
  7. for (i = 0; i < N; i ) {
  8. printf('Enter row %d: ', i 1);
  9. for (j = 0; j < N; j ) {
  10. scanf('%d', &a[i][j]);
  11. }
  12. }
  13. int sum;
  14. printf('Row totals: ');
  15. for (i = 0; i < N; i ) {
  16. for (j = 0; j < N; j ) {
  17. sum = a[i][j];
  18. }
  19. printf('%d ', sum);
  20. sum = 0;
  21. }
  22. printf('\nColumn totals: ');
  23. for (j = 0; j < N; j ) {
  24. for (i = 0; i < N; i ) {
  25. sum = a[i][j];
  26. }
  27. printf('%d ', sum);
  28. sum = 0;
  29. }
  30. return 0;
  31. }

8_8

  1. #include <stdio.h>
  2. #define N 5
  3. int main(void)
  4. {
  5. int a[N][N];
  6. int i, j;
  7. for (i = 0; i < N; i ) {
  8. printf('输入学生%d的五门成绩:', i 1);
  9. for (j = 0; j < N; j ) {
  10. scanf('%d', &a[i][j]);
  11. }
  12. }
  13. printf('\n');
  14. int sum = 0;
  15. float average = 0.0f;
  16. for (i = 0; i < N; i ) {
  17. for (j = 0; j < N; j ) {
  18. sum = a[i][j];
  19. }
  20. average = (float) sum / N;
  21. printf('学生%d的总成绩和平均分分别为%d和%.2f\n', i, sum, average);
  22. sum = 0;
  23. }
  24. printf('\n');
  25. int max, mix;
  26. sum = 0;
  27. average = 0.0f;
  28. for (j = 0; j < N; j ) {
  29. max = a[0][j];
  30. mix = a[0][j];
  31. for (i = 0; i < N; i ) {
  32. if (max < a[i][j]) max = a[i][j];
  33. if (mix > a[i][j]) mix = a[i][j];
  34. sum = a[i][j];
  35. }
  36. average = (float) sum / N;
  37. printf('学科%d的平均分是%.2f,最高分是%d,最低分是%d\n', j, average, max, mix);
  38. sum = 0;
  39. }
  40. return 0;
  41. }

8_9

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #define N 10
  6. #define WAY 4
  7. int main(void)
  8. {
  9. bool a[N][N] = {false};
  10. char b[N][N];
  11. int i, j;
  12. //初始化表
  13. for (i = 0; i < N; i ) {
  14. for (j = 0; j < N; j ) {
  15. b[i][j] = '.';
  16. }
  17. }
  18. srand((unsigned) time(NULL));
  19. int ways;
  20. i = 0; j = 0;
  21. b[0][0] = 'A';
  22. a[0][0] = true;
  23. for (int m = 0; m < 25; ) {
  24. //余0表上, 1表下, 2表左, 3表右
  25. ways = rand() % WAY;
  26. if (ways == 0) {
  27. //如果往上走出表格了,continue重新循环试试别的方向
  28. if (i - 1 < 0) {
  29. continue;
  30. }
  31. //如果往上走有字母了, 进行判断
  32. else if (a[i - 1][j] == true){
  33. //如果四面都是字母,退出
  34. if ((a[i 1][j] == true && a[i][j - 1] == true && a[i][j 1] == true)
  35. // 考虑左边和左下角情况
  36. || j - 1 < 0 && a[i 1][j] == true && a[i][j 1] == true
  37. || j - 1 < 0 && i 1 > 9 && a[i][j 1] == true
  38. // 右边和右下角
  39. || j 1 > 9 && a[i 1][j] == true && a[i][j - 1] == true
  40. || j - 1 < 0 && i 1 > 9 && a[i][j 1] == true
  41. //下边
  42. || i 1 > 9 && a[i][j 1] == true && a[i][j - 1] == true) break;
  43. //如果都不是,重新选方向
  44. continue;
  45. } else {
  46. a[i - 1][j] = true;
  47. b[i - 1][j] = 'B' m;
  48. m ;
  49. i--;
  50. continue;
  51. }
  52. }
  53. if (ways == 1) {
  54. if (i 1 > 9) {
  55. continue;
  56. } else if (a[i 1][j] == true){
  57. if (a[i - 1][j] == true && a[i][j - 1] == true && a[i][j 1] == true
  58. //右边和右上角
  59. || j 1 > 9 && a[i][j - 1] == true && a[i - 1][j] == true
  60. || j 1 > 9 && i - 1 < 0 && a[i][j - 1] == true
  61. //左边
  62. || j - 1 < 0 && a[i - 1][j] == true && a[i][j 1] == true
  63. //上边
  64. || i - 1 < 0 && a[i][j 1] == true && a[i][j - 1] == true) break;
  65. continue;
  66. } else {
  67. a[i 1][j] = true;
  68. b[i 1][j] = 'B' m;
  69. m ;
  70. i ;
  71. continue;
  72. }
  73. }
  74. if (ways == 2) {
  75. if (j - 1 < 0) {
  76. continue;
  77. } else if (a[i][j - 1] == true){
  78. if (a[i 1][j] == true && a[i - 1][j] == true && a[i][j 1] == true
  79. // 考虑下边和右下角情况
  80. || i 1 > 9 && a[i - 1][j] == true && a[i][j 1] == true
  81. || j 1 > 9 && i 1 > 9 && a[i - 1][j] == true
  82. // 上边和右上角
  83. || i - 1 < 0 && a[i 1][j] == true && a[i][j 1] == true
  84. || j 1 > 9 && i - 1 < 0 && a[i][j 1] == true
  85. // 右边
  86. || j 1 > 9 && a[i - 1][j] == true && a[i 1][j] == true) break;
  87. continue;
  88. } else {
  89. a[i][j - 1] = true;
  90. b[i][j - 1] = 'B' m;
  91. m ;
  92. j--;
  93. continue;
  94. }
  95. }
  96. if (ways == 3) {
  97. if (j 1 > 9) {
  98. continue;
  99. } else if (a[i][j 1] == true){
  100. if (a[i 1][j] == true && a[i][j - 1] == true && a[i - 1][j] == true
  101. //左边和左下角
  102. || j - 1 < 0 && a[i - 1][j] == true && a[i 1][j] == true
  103. || j - 1 < 0 && i 1 > 9 && a[i - 1][j] == true
  104. //下边
  105. || i 1 > 9 && a[i - 1][j] == true && a[i][j - 1] == true
  106. //上边
  107. || i - 1 < 0 && a[i][j - 1] == true && a[i 1][j] == true) break;
  108. continue;
  109. } else {
  110. a[i][j 1] = true;
  111. b[i][j 1] = 'B' m;
  112. m ;
  113. j ;
  114. continue;
  115. }
  116. }
  117. }
  118. for (i = 0; i < N; i ) {
  119. for (j = 0; j < N; j ) {
  120. printf('%c ', b[i][j]);
  121. }
  122. printf('\n');
  123. }
  124. return 0;
  125. }

8_10

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main(void)
  4. {
  5. const int lea[8] = {480, 583, 679, 767, 840, 945, 1140, 1305};
  6. const int arr[8] = {616, 712, 811, 900, 968, 1075, 1280, 1438};
  7. int hours, minutes;
  8. int time;
  9. printf ('Enter a 24-hour time:');
  10. scanf ('%d:%d', &hours, &minutes);
  11. //将时间换算成分钟
  12. time = hours * 60 minutes;
  13. int mix = 1440, m;
  14. //找到距离最近的时间,并记录其下标
  15. for (int i = 0; i < 8; i ) {
  16. if (fabs(lea[i] - time) < mix) {
  17. mix = fabs(lea[i] - time);
  18. m = i;
  19. }
  20. }
  21. int hour, minute;
  22. //判断是上午还是下午并输出
  23. if (lea[m] / 60 > 12) {
  24. if (arr[m] / 60 > 12) {
  25. printf('CLosest departure time is %d:%.2d p.m.', lea[m] / 60 - 12, lea[m] % 60);
  26. printf(', arriving at %d:%.2d p.m.', arr[m] / 60 -12, arr[m] % 60);
  27. }
  28. } else {
  29. if (arr[m] / 60 > 12) {
  30. printf('CLosest departure time is %d:%.2d a.m.', lea[m] / 60, lea[m] % 60);
  31. printf(', arriving at %d:%.2d p.m.', arr[m] / 60 -12, arr[m] % 60);
  32. } else {
  33. printf('CLosest departure time is %d:%.2d a.m.', lea[m] / 60, lea[m] % 60);
  34. printf(', arriving at %d:%.2d p.m.', arr[m] / 60, arr[m] % 60);
  35. }
  36. }
  37. return 0;
  38. }

8_11

  1. #include <stdio.h>
  2. int main (void)
  3. {
  4. int ch, i = 0;
  5. char c[15];
  6. printf ('Enter phone number: ');
  7. //输入字符直到为回车为止
  8. while ((ch = getchar ()) != '\n') {
  9. //输入的如果是字母,换成数字
  10. if (ch <= 'Z' && ch >= 'A') {
  11. switch (ch) {
  12. case 65: case 66: case 67:
  13. c[i] = '2';
  14. break;
  15. case 68: case 69: case 70:
  16. c[i] = '3';
  17. break;
  18. case 71: case 72: case 73:
  19. c[i] = '4';
  20. break;
  21. case 74: case 75: case 76:
  22. c[i] = '5';
  23. break;
  24. case 77: case 78: case 79:
  25. c[i] = '6';
  26. break;
  27. case 81: case 82: case 83: case 80:
  28. c[i] = '7';
  29. break;
  30. case 84: case 85: case 86: case 87:
  31. c[i] = '8';
  32. break;
  33. case 88: case 89: case 90:
  34. c[i] = '9';
  35. break;
  36. }
  37. i ;
  38. continue;
  39. }
  40. c[i] = ch;
  41. i ;
  42. }
  43. printf('In numeric form : ');
  44. for (int j = 0; j < i; j ) {
  45. printf('%c', c[j]);
  46. }
  47. return 0;
  48. }

8_12

  1. #include <stdio.h>
  2. //头文件ctpye中包含toupper()函数
  3. #include <ctype.h>
  4. int main(void)
  5. {
  6. //建立整数型数组模拟每个字母所代表的面值
  7. const int a[26] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
  8. int sum = 0;
  9. char ch;
  10. printf('Enter a word: ');
  11. while ((ch = getchar()) != '\n') {
  12. if (toupper(ch) < 'A' || toupper(ch) > 'Z') {
  13. printf('Illegal input\n');
  14. break;
  15. } else {
  16. sum = a[toupper(ch) - 'A'];
  17. }
  18. }
  19. printf('Scrabble value: %d', sum);
  20. return 0;
  21. }

8_13

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char a[20];
  5. char ch1, ch2;
  6. printf ('Enter a first and last name: ');
  7. scanf ('%c', &ch1);
  8. //循环输入直到空格为止
  9. while ((getchar()) != ' ')
  10. ;
  11. int i = 0;
  12. while ((ch2 = getchar()) != '\n') {
  13. a[i] = ch2;
  14. i ;
  15. }
  16. printf('You enered the name: ');
  17. for (int j = 0; j < i; j ) {
  18. printf('%c', a[j]);
  19. }
  20. printf(', %c.', ch1);
  21. return 0;
  22. }

8_14

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char sen[100];
  5. int i;
  6. printf('Enter a sentence: ');
  7. for (i = 0; i < 100; i ) { //建立字符数字sen并输入句子
  8. sen[i] = getchar();
  9. if (sen[i] == '!' || sen[i] == '.' || sen[i] == '?') break; //遇到句末符号break跳出循环(i 并没有执行),此时i指向末尾的符号
  10. }
  11. int pri = i; //记录下句子的长度,以后用于输出
  12. char sen1[100]; //建立一个数组用于存改变顺序后的句子 (一个一个单词地复制)
  13. int j = 0;
  14. int over, start; //start为每次开始复制时的数组下标,over为复制结束时的
  15. for (i = i - 1; i >= 0; i--, j ) { //从i-1的位置往前找空格,i-1表示先不处理句末符号
  16. over = i; //下次一个单词的复制就从i(i此时已经i-1)处结束
  17. while (sen[i] != ' ' && i > 0) i--; //遇到空格或者i<=0时,停止寻找
  18. start = i 1; //开始一个单词的复制,下标应该在空格后一个
  19. if (i == 0) start--; //这个if只针对已经复制到原句的第一个单词的情况,start需要从零开始故减1
  20. for (; start <= over; start , j ) {
  21. sen1[j] = sen[start]; //复制单词
  22. }
  23. sen1[j] = ' '; //每个单词后加个空格
  24. }
  25. sen1[j - 1] = sen[pri]; //之后就是sen1数组的输出了!
  26. printf('Reversal of sentence: ');
  27. for (int m = 0; m < pri 1; m ) {
  28. printf('%c', sen1[m]);
  29. }
  30. return 0;
  31. }

8_15

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. //sen为原句子
  5. char sen[80], sen1[80];
  6. int n;
  7. //初始化数组
  8. int i;
  9. printf('Enter message to be encrypted: ');
  10. for (i = 0; ; i ) {
  11. sen[i] = getchar();
  12. if (sen[i] == '\n') break;
  13. }
  14. printf('Enter shift amout (1~25): ');
  15. scanf('%d', &n);
  16. printf('Encrypted message: ');
  17. for (int j = 0; j < i 1; j ) {
  18. if (sen[j] <= 'z' && sen[j] >= 'a') {
  19. printf('%c', ((sen[j] - 'a') n) % 26 'a');
  20. } else if (sen[j] <= 'Z' && sen[j] >= 'A') {
  21. printf('%c', ((sen[j] - 'A') n) % 26 'A');
  22. } else {
  23. printf('%c', sen[j]);
  24. }
  25. }
  26. return 0;
  27. }

8_16

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. int main(void)
  5. {
  6. //定义一个整数数组用来记录每个字母出现的次数。第一输入单词,
  7. //对应数组元素加一,第二次输入减一,若最后每个元素都为零则 为变为词
  8. int num[26] = {0};
  9. char word[20];
  10. int i;
  11. printf('Enter first word: ');
  12. for (i = 0; i < 20; i ) {
  13. word[i] = getchar();
  14. if (!isalpha(word[i]) && word[i] != '\n') {
  15. printf('Illegal input!');
  16. return 0;
  17. }
  18. if (word[i] == '\n') break;
  19. word[i] = tolower(word[i]);
  20. num[word[i] - 'a'] ;
  21. }
  22. printf('Enter second word: ');
  23. for (i = 0; i < 20; i ) {
  24. word[i] = getchar();
  25. if (!isalpha(word[i]) && word[i] != '\n') {
  26. printf('Illegal input!');
  27. return 0;
  28. }
  29. if (word[i] == '\n') break;
  30. word[i] = tolower(word[i]);
  31. num[word[i] - 'a']--;
  32. }
  33. bool flag = false;
  34. for (i = 0; i < 26; i ) {
  35. if (num[i]) flag = true;
  36. }
  37. if (flag) printf('The words are not anagrams.');
  38. else printf('The words are anagrams.');
  39. return 0;
  40. }

8_17

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. printf('This is program creates a magic square of a specified size.\n');
  5. printf('The size must be an odd number between 1 and 99.\n');
  6. printf('Enter size of magic square: ');
  7. int size;
  8. scanf('%d', &size);
  9. int square[size][size];
  10. for (int i = 0; i < size; i ) {
  11. for (int j = 0; j < size; j ) {
  12. square[i][j] = 0;
  13. }
  14. }
  15. int i = 0, j = size / 2, num = 1;
  16. for (; ; ) {
  17. if (square[i][j] == 0) {
  18. square[i][j] = num;
  19. i = i size - 1; j ;
  20. i = i % size;
  21. j = j % size;
  22. } else {
  23. i ; i ; j = j size - 1;
  24. i = i % size;
  25. j = j % size;
  26. square[i][j] = num;
  27. i = i size - 1; j ;
  28. i = i % size;
  29. j = j % size;
  30. }
  31. num ;
  32. if (num > size * size) break;
  33. }
  34. for (i = 0; i < size; i ) {
  35. for (j = 0; j < size; j ) {
  36. printf('%3d', square[i][j]);
  37. }
  38. printf('\n');
  39. }
  40. return 0;
  41. }
文章最后发布于: 2018-07-18 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多