c primer plus 第六版 第九章编程练习答案
2019-07-31 19:36
351 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43531895/article/details/97950672
(编译环境 Microsoft Visual Studio 2019)
1.
[code]/*1.*/ #include<stdio.h> double min(double x, double y); int main(void) { double value_1, value_2; printf("Please enter two decimal numbers.\n"); scanf_s("%lf %lf", &value_1, &value_2); printf("The smallest is %f\n", min(value_1, value_2)); return 0; } double min(double x, double y) { return x < y ? x : y; }
2.
[code]/*2.*/ #include<stdio.h> void chline(char ch, unsigned int i, unsigned int j); int main(void) { char ch; unsigned int row, column; printf("Please enter a character: "); ch = getchar(); printf("Please enter the number of rows: "); scanf_s("%u", &row); printf("Please enter the number of columns: "); scanf_s("%u", &column); chline(ch, column, row); return 0; } void chline(char ch, unsigned int i, unsigned int j) { unsigned int row, column; for (row = 0; row <j; row++) { for (column = 0; column < i; column++) putchar(ch); putchar('\n'); } }
3.
[code]/*3.*/ #include<stdio.h> void chline(char ch, unsigned int i, unsigned int j); int main(void) { char ch; unsigned int row, column; printf("Please enter a character: "); ch = getchar(); printf("Please enter the number of rows: "); scanf_s("%u", &row); printf("Please enter the number of columns: "); scanf_s("%u", &column); chline(ch, column, row); return 0; } void chline(char ch, unsigned int i, unsigned int j) { unsigned int row, column; for (row = 0; row <j; row++) { for (column = 0; column < i; column++) putchar(ch); putchar('\n'); } }
4.
[code] /*4.*/ #include<stdio.h> double h_average(double a, double b); int main(void) { double value_1, value_2; printf("Please enter two decimal numbers.\n"); scanf_s("%lf %lf", &value_1, &value_2); printf("Harmonic average: %f\n",h_average(value_1,value_2)); return 0; } double h_average(double a, double b) { double average; average = ((1 / a) + (1 / b)) / 2; return 1 / average; }
5.
[code]/*5.*/ #include<stdio.h> void larger_of(double* a, double* b); int main(void) { double value_1, value_2; printf("Please enter two decimal numbers.\n"); scanf_s("%lf %lf", &value_1, &value_2); printf("Originally x= %.3f y= %.3f\n", value_1, value_2); larger_of(&value_1, &value_2); printf("Now x= %.3f y=%.3f\n", value_1, value_2); return 0; } void larger_of(double* a, double* b) { if (*a > * b) * b = *a; else *a = *b; }
6.
[code]/*6.*/ #include<stdio.h> void sort(double* a, double* b, double* c); void swap(double* x, double* y); int main(void) { double a, b, c; printf("Please enter three decimal numbers.\n"); scanf_s("%lf %lf %lf", &a, &b, &c); printf("Originally a= %.3f b= %.3f c=%.3f\n", a, b, c); sort(&a, &b, &c); printf("Now a= %.3f b=%.3f c=%.3f\n", a, b, c); return 0; } void sort(double* a, double* b, double* c) { if (*a > * b) swap(a, b); if (*b > * c) swap(b, c); if (*a > * b) swap(a, b); } void swap(double* x, double* y) { double temp; temp = *x; *x = *y; *y = temp; }
7.
[code]/*7.*/ #include<stdio.h> #include<ctype.h> int position(char ch); int main(void) { char ch; int pos; printf("Please enter some text.(end with EOF)\n"); while ((ch = getchar()) != EOF) { pos = position(ch); if (pos != -1) printf("The alphabet position of %c is %d\n", ch, pos); else printf("%c is not a letter\n", ch); } return 0; } int position(char ch) { if (isupper(ch)) return ch - 'A' + 1; else if (islower(ch)) return ch - 'a' + 1; else return -1; }
8.
[code]/*8.*/ #include<stdio.h> double power(double n, int p); int main(void) { double x, xpow; int exp; printf("Please enter a number and integer power(q to quit)\n"); while (scanf_s("%lf %d", &x, &exp) == 2) { if (x == 0 && exp == 0) printf("0 to the power of 0 is undefined,processed as 1\n"); else { xpow = power(x, exp); printf("%.3g to the power %d is %.5g\n", x, exp, xpow); } printf("Enter next pair of numbers or q to quit.\n"); } printf("bye\n"); return 0; } double power(double n, int p) { int per; double pow = 1; if (n == 0) return 0; if (p == 0) return 1; else if (p < 0) { for (per = 0; per < -p; per++) pow *= n; return 1 / pow; } else { for (per = 0; per < p; per++) pow *= n; return pow; } }
9.
[code]/*9.*/ #include<stdio.h> double power(double n, int p); int main(void) { double x, xpow; int exp; printf("Please enter a number and integer power(q to quit)\n"); while (scanf_s("%lf %d", &x, &exp) == 2) { if (x == 0 && exp == 0) printf("0 to the power of 0 is undefined,processed as 1\n"); else { xpow = power(x, exp); printf("%.3g to the power %d is %.5g\n", x, exp, xpow); } printf("Enter next pair of numbers or q to quit.\n"); } printf("bye\n"); return 0; } double power(double n, int p) { double pow = 1; if (n == 0) return 0; if (p == 0) return 1; else if (p > 0) { pow = n * power(n, p - 1);//递归要注意每级函数都有自己的变量,每级函数的变量是不变的 return pow; } else { p = -p; pow = n * power(n, p - 1); return 1 / pow; } }
10.
[code]/*10.*/ #include<stdio.h> void to_base_n(unsigned long n, int p); int main(void) { unsigned long number; int radix; printf("Please enter an integer and a radix(q to quit).\n"); while (scanf_s("%lu %d", &number, &radix)==2) { if (radix < 2 || radix>10) printf("The radix must be between 2 and 10"); else { printf("Conversion result: "); to_base_n(number, radix); putchar('\n'); } printf("Enter an integer and a radix.(q to quit)\n"); } printf("bye\n"); return 0; } void to_base_n(unsigned long n, int p) { int r; r = n % p; if (n >= p) to_base_n(n / p, p); printf("%d", r); }
11.
[code]/*11.*/ #include<stdio.h> unsigned int fibonacci(int n); int main(void) { int n; printf("Please enter the number of terms in the Fibonacci sequence(q to quit): "); while (scanf_s("%d", &n)) { if (n <= 0) printf("Please enter a positive integer.\n"); else printf("Value: %lu\n", fibonacci(n)); printf("Enter the number of terms: "); } printf("bye\n"); return 0; } unsigned int fibonacci(int n) { unsigned int last_1 = 1; unsigned int last_2 = 1; unsigned int value; int per; if (n == 1 || n == 2) return 1; else { for (per = 3; per <= n; per++) { value = last_1 + last_2; last_1 = last_2; last_2 = value; } } return value; }
相关文章推荐
- C Primer Plus (第六版) 第九章_编程练习答案
- C ++ Primer Plus 第六版 第九章编程练习答案
- C++ Primer Plus第六版 第九章 编程练习答案
- C++ primer Plus(第六版)中文版 第九章 内存模型和名称空间 编程练习答案
- C++ Primer Plus 第六版 学习笔记 第七章 编程练习答案
- 《C++ Primer Plus(第六版)》(36)(第十六章 string类和标准模板库 编程练习和答案)
- 《C++ Primer Plus(第六版)》(41)(第十七章 输入、输出和文件 编程练习和答案1)
- C++ primer Plus(第六版)_课后编程练习_第九章
- C++ primer Plus(第六版)中文版 第五章 循环和关系表达式 编程练习答案
- C++ primer Plus(第六版)中文版 第六章 分支语句和逻辑运算符 编程练习答案
- C++primer plus第六版课后编程练习答案2.2
- C++primer plus第六版课后编程练习答案4.2
- C++primer plus第六版课后编程练习答案5.9
- C++primer plus第六版课后编程练习答案7.7
- C++primer plus第六版课后编程练习答案12.5与12.6
- c++ primer plus(第6版)中文版 第九章编程练习答案
- C Primer Plus (第六版)中文版 第七章 编程练习答案
- C-primer-plus 第六版 课后编程练习答案 中文版
- C++ Primer Plus(第六版)第四章编程练习参考答案
- C++ primer Plus(第六版)中文版 第十章 对象和类 编程练习答案