C Primer Plus 第6版 编程练习答案(第九章)
— C Primer Plus, 编程 — 6 min read
9.1.c
#include <stdio.h>double min(double, double);int main(void) { double x, y;
printf("Enter two numbers (q to quit): "); while (scanf("%lf %lf", &x, &y) == 2) { printf("The smaller number is %f.\n", min(x,y)); printf("Next two values (q to quit): "); } printf("Bye!\n");
return 0;}double min(double a, double b) { return a < b ? a : b;}/*double min(double a, double b){ if (a < b) return a; else return b;}*/
9.2.c
#include <stdio.h>void chline(char ch, int i, int j);int main(void) { char ch; int col, row;
printf("Enter a character (# to quit): "); while ( (ch = getchar()) != '#') { if (ch == '\n') continue; printf("Enter number of columns and number of rows: "); if (scanf("%d %d", &col, &row) != 2) break; chline(ch, col, row); printf("\nEnter next character (# to quit): "); } printf("Bye!\n");
return 0;}// start rows and cols at 0void chline(char ch, int i, int j) { int col, row; for (row = 0; row < j ; row++) { for (col = 0; col < i; col++) putchar(ch); putchar('\n'); } return;}
9.3.c
#include <stdio.h>void chline(char ch, int i, int j);int main(void) { char ch; int col, row;
printf("Enter a character (# to quit): "); while ( (ch = getchar()) != '#') { if (ch == '\n') continue; printf("Enter number of columns and number of rows: "); if (scanf("%d %d", &col, &row) != 2) break; chline(ch, col, row); printf("\nEnter next character (# to quit): "); } printf("Bye!\n");
return 0;}// start rows and cols at 0void chline(char ch, int i, int j) { int col, row; for (row = 0; row < j ; row++) { for (col = 0; col < i; col++) putchar(ch); putchar('\n'); } return;}
9.4.c
#include <stdio.h>double average(double num1, double num2);
int main(void) { double num1, num2, result; double rnum1, rnum2, rresult; printf("Enter two numbers (q to quit):\n"); while(scanf("%lf %lf", &num1, &num2) == 2) { rnum1 = 1 / num1; rnum2 = 1 / num2; rresult = average(rnum1, rnum2); result = 1 / rresult; printf("result is %lf", result); } printf("Done.\n"); return 0;}
double average(double num1, double num2) { double _average = (num1 + num2) / 2; return _average;}
9.5.c
#include <stdio.h>void larger_of(double *p1, double *p2);int main(void) { double x, y;
printf("Enter two numbers (q to quit): "); while (scanf("%lf %lf", &x, &y) == 2) { larger_of(&x, &y); printf("The modified values are %f and %f.\n", x, y); printf("Next two values (q to quit): "); } printf("Bye!\n");
return 0;}void larger_of(double *p1, double *p2) { if (*p1 > *p2) *p2 = *p1; else *p1 = *p2;}// alternatively:/*void larger_of(double *p1, double *p2){ *p1= *p2 = *p1 > *p2 ? *p1 : *p2;}*/
9.6.c
#include <stdio.h>void compare(double * i, double * j, double * k);
int main(void) { double min, mid, max;
printf("Enter three numbers (q to quit):\n"); while(scanf("%lf %lf %lf", &max, &mid, &min) == 3) { compare(&min, &mid, &max); printf("%lf < %lf < %lf", min, mid, max); } printf("Done.\n"); return 0;}
void compare(double * i, double * j, double * k) { double n; if(*i > *k) { n = *k; *k = *i; *i = n; } if(*j > *k) { n = *k; *k = *j; *j = n; } if(*i > *j) { n = *j; *j = *i; *i = n; }
return;}
9.7.c
#include <stdio.h>#include <ctype.h>int position(int ch);
int main(void) { int ch, i; printf("Enter a charater (EOF to quit):\n"); while((ch = getchar()) != EOF) { if(ch == '\n') continue; printf("%c %s letter.\n", ch, isalpha(ch) ? "is" : "isn't"); if(isalpha(ch)) { i = position(ch); printf("%c is %d in alphabet.\n", ch, i); } } printf("Done.\n"); return 0;}
int position(int ch) { ch = tolower(ch); return (ch - 'a' + 1);}
9.8.c
#include <stdio.h>double power(double a, int b);
int main(void) { double x, xpow; int n; printf("Enter a number and the integer power"); printf(" to which\nthe number will be raised. Enter q"); printf(" to quit.\n"); while (scanf("%lf%d", &x, &n) == 2) { xpow = power(x,n); printf("%.3g to the power %d is %.5g\n", x, n, xpow); printf("Enter next pair of numbers or q to quit.\n"); } printf("Hope you enjoyed this power trip -- bye!\n"); return 0;}double power(double a, int b) { double pow = 1; int i;
if (b == 0) { if (a == 0) printf("0 to the 0 undefined; using 1 as the value\n"); pow = 1.0; } else if (a == 0) pow = 0.0; else if (b > 0) for(i = 1; i <= b; i++) pow *= a; else pow = 1.0 / power(a, - b); return pow;}
9.9.c
#include <stdio.h>double power(double a, int b);
int main(void) { double x, pow; int n; printf("Enter a number and the integer power"); printf(" to which\nthe number will be raised. Enter q"); printf(" to quit.\n"); while (scanf("%lf%d", &x, &n) == 2) { if (n == 0) { if (x == 0) printf("0 to the 0 undefined; using 1 as the value\n"); pow = 1.0; } else if (x == 0) pow = 0.0; else if (n > 0) pow = power(x, n); else pow = 1.0 / power(x, -n); printf("%.3g to the power %d is %.5g\n", x, n, pow); printf("Enter next pair of numbers or q to quit.\n"); } printf("Hope you enjoyed this power trip -- bye!\n"); return 0;}double power(double a, int b) { if(b > 0) return a * power(a,b-1); return 1;}
9.10.c
#include <stdio.h>void to_base_n(int x, int base);int main(void) { int number; int b; int count;
printf("Enter an integer (q to quit):\n"); while (scanf("%d", &number) == 1) { printf("Enter number base (2-10): "); while ((count = scanf("%d", &b))== 1 && (b < 2 || b > 10)) { printf("base should be in the range 2-10: "); } if (count != 1) break; printf("Base %d equivalent: ", b); to_base_n(number, b); putchar('\n'); printf("Enter an integer (q to quit):\n"); } printf("Done.\n"); return 0;}void to_base_n(int x, int base) { int r; r = x % base; if (x >= base) to_base_n(x / base, base); putchar('0' + r); return;}
9.11.c
#include <stdio.h>void Fibonacci(int n);
int main(void) { int n; printf("Enter an integer(>0) (q to quit):\n"); while(scanf("%d", &n) == 1 && n > 0) { Fibonacci(n); printf("Enter another one(>0) (q to quit):\n"); } printf("Done.\n"); return 0;}void Fibonacci(int n) { int x, y, z, i; x = y = 1; for(i = 1; i <= n; i++) { if(i <= 2) printf("1\n"); else { z = x + y; x = y; y = z; printf("%d\n", z); } } return;}