C Primer Plus第九章编程练习答案

news/2024/10/21 3:14:59/

学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出!

1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单 的驱动程序中测试该函数。

#include <stdio.h>
double Min(double x, double y)
{return x > y ? y : x;
}
int main(void)
{double min_num = Min(20.0, 15.0);printf("min_num : %lf\n", min_num);min_num = Min(15.0, 20.0);printf("min_num : %lf\n", min_num);return 0;
}

2.设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱 动程序中测试该函数。

#include <stdio.h>
void chline(char ch, int i, int j)
{for (int m = 0; m < i; m++){for (int n = 0; n < j; n++){printf("%c ", ch);}printf("\n");}
}
int main(void)
{char ch = '0';int i = 0, j = 0;printf("请输入一个字符:");scanf("%c",&ch);printf("请输入行数:");scanf("%d", &i);printf("请输入列数:");scanf("%d", &j);chline(ch, i, j);return 0;
}

3.编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待 打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指 定字符的行数。编写一个调用该函数的程序。

#include <stdio.h>
void chline(char ch, int i, int j)
{for (int m = 0; m < j; m++){for (int n = 0; n < i; n++){printf("%c ", ch);}printf("\n");}
}
int main(void)
{char ch = '0';int i = 0, j = 0;printf("请输入一个字符:");scanf("%c",&ch);printf("请输入一行中打印字符的个数:");scanf("%d", &i);printf("请输入指定打印字符的个数:");scanf("%d", &j);chline(ch, i, j);return 0;
}

4.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数 的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的 参数,返回这两个参数的调和平均数。

#include <stdio.h>
double Reconcile_the_average(double d1, double d2)
{return 1.0 / ((1.0 / d1 + 1.0 / d2) / 2.0);
}
int main(void)
{double ret = Reconcile_the_average(3.0,4.0);printf("ret : %lf\n", ret);ret = Reconcile_the_average(4.0, 5.0);printf("ret : %lf\n", ret);return 0;
}

5.编写并测试一个函数larger_of(),该函数把两个double类型变量的值替 换为较大的值。例如, larger_of(x, y)会把x和y中较大的值重新赋给两个变 量。

#include <stdio.h>
void larger_of(double* d1,double* d2)
{if (*d1 > *d2)*d2 = *d1;else*d1 = *d2;
}
int main(void)
{double d1 = 1.0, d2 = 2.0, d3 = 3.0,d4 = 4.0;printf("d1 = %lf,d2 = %lf d3 = %lf,d4 = %lf\n", d1, d2, d3, d4);larger_of(&d1, &d2);larger_of(&d3, &d4);printf("d1 = %lf,d2 = %lf d3 = %lf,d4 = %lf\n", d1, d2, d3, d4);return 0;
}

6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最 小值放入第1个函数,中间值放入第2个变量,最大值放入第3个变量。

#include <stdio.h>
void sort(double* d1, double* d2, double* d3)
{double temp = 0.0;if (*d1 > *d2){temp = *d1;*d1 = *d2;*d2 = temp;}if (*d1 > *d3){temp = *d1;*d1 = *d3;*d3 = temp;}if (*d2 > *d3){temp = *d2;*d2 = *d3;*d3 = temp;}
}
int main(void)
{double d1 = 2.0, d2 = 3.0, d3 = 1.0;printf("排序前:d1 = %lf,d2 = %lf,d3 = %lf\n", d1, d2, d3);sort(&d1, &d2, &d3);printf("排序后:d1 = %lf,d2 = %lf,d3 = %lf\n", d1, d2, d3);return 0;
}

7.编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要 报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位 置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为 参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。

#include <stdio.h>
#include <ctype.h>
int position(char ch)
{if (islower(ch))return ch - 'a' + 1;else if (isupper(ch))return ch - 'A' + 1;return -1;
}
void get_char_pos(void)
{char ch = '0';printf("请输入一些字符:");while ((ch = getchar()) != EOF){if (ch == '\n')continue;if (position(ch) != -1)printf("该字符是一个字母,且位于字母表第%d位\n", position(ch));else{printf("该字符不是一个字母\n");}}
}
int main(void)
{get_char_pos();return 0;
}

8.第6章的程序清单6.20中,power()函数返回一个double类型数的正整数 次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂 都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处 理为1)。要使用一个循环,并在程序中测试该函数。

#include <stdio.h>
double power(double n, int p)
{int i;double pow = 1.0;if ((0 == p) && (0 == n)){printf("0 to the 0 undefined, using 1 as the value.\n");return pow;}if (0 == n){pow = 0.0;return pow;}if (0 == p){return pow;}if (p > 0){for (i = 1; i <= p; i++){pow *= n;}return pow;}else{for (i = 1; i <= -p; i++){pow *= 1 / n;}return pow;}
}
int main(void)
{double x, xpow;int exp;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, &exp) == 2){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("Hope you enjoyed this power trip -- bye!\n");return 0;
}

9.使用递归函数重写编程练习8。

#include <stdio.h>
double power(double n, int p)
{double pow = 1.0;if ((0 == p) && (0 == n)){printf("0 to the 0 undefined, using 1 as the value.\n");return pow;}if (0 == n){pow = 0.0;return pow;}if (0 == p){return pow;}if (p > 0){return n * power(n, p - 1);}else{return power(n, p + 1) / n;}
}
int main(void)
{double x, xpow;int exp;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, &exp) == 2){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("Hope you enjoyed this power trip -- bye!\n");return 0;
}

10.为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n() 函数接受两个在2~10范围内的参数,然后以第2个参数中指定的进制打印第 1个参数的数值。例如,to_base_n(129, 8)显示的结果为201,也就是129的 八进制数。在一个完整的程序中测试该函数。

#include <stdio.h>
void to_base_n(int x, int base)
{int r;r = x % base;if (x >= base){to_base_n(x / base, base);}printf("%d", r);return;
}
int main(void)
{int b;long int n;printf("Please enter a number (q to quit): ");while (scanf("%ld", &n) == 1){if (n <= 0){printf("Illegal data! Please enter again: ");continue;}printf("Please enter a base system number (2 - 10): ");while (scanf("%d", &b) != 1 || (b < 2 || b > 10)){while (getchar() != '\n')continue;printf("Please enteragain (2 - 10): ");}printf("%d in %d base system is: ", n, b);to_base_n(n, b);printf("\nYou can enter a number again (q to quit): ");}printf("Done.\n");return 0;
}

11.编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契 数。

#include <stdio.h>
void Fibonacci(int len)
{int i;unsigned long t, x, y;x = y = 1;for (i = 0; i < len; i++){printf("%lu\n", x);t = x + y;x = y;y = t;}return;
}
int main(void)
{int n;printf("Please enter a integer (<= 0 or q to quit): ");while (scanf("%d", &n) == 1){printf("Top %d items of Fibonacci sequence:\n", n);Fibonacci(n);printf("You can enter again (<= 0 or q to quit): ");}printf("Done.\n");return 0;
}


http://www.ppmy.cn/news/96556.html

相关文章

电子科技大学编译原理复习笔记(四):程序语言的设计

目录 前言 重点一览 语言的定义 比较&#xff1a;生成观点与识别观点 语义又该怎么描述&#xff1f; 符号串 符号串集合 ⭐文法&#xff08;超重点&#xff09; 定义 组成 表示 ⭐分类&#xff08;重点&#xff09; 文法产生的语言 ⭐短语、直接短语和句柄&…

Spring Boot问题汇总

1.IDEA里yaml文件编辑时没有提示 网上很多教程说在设置里的File Types里把yaml格式加入到关联中 但其实我打开IDEA默认就是这么设置的&#xff0c;所以并没有什么用处。 不过在翻看这篇教程&#xff08;IDEA创建yml文件不显示小树叶创建失败问题的解决方法-eolink官网&#x…

VUE与React区别

相似点&#xff1a; -组件化开发 -vue2 option api -vue3 components api -生命周期 -react16以后偏向hook -vdom树

大话Stable-Diffusion-Webui-客制化主题(一)

文章目录 Gradio客制化主题上传主题至 HuggingFace 仓库创建huggingface空间修改 README.md生成huggingface的token上传主题至huggingface在SD中使用DIY好的主题笔者DIY的一个简单的主题stable-diffusion-webui(以下简称sd)目前可谓是最火的开源的AI绘图项目,其原因首当其冲…

快速入门Matlab——深入学习字符串

相关matlab资源&#xff08;免费&#xff09;MatlabYALMIPCPLEX解决带储能的微电网优化调度问题 基于拉丁超立方法的风光场景生成与削减 考虑用户舒适度的冷热电多能互补综合能源系统优化调度 学习目标&#xff1a;1.深入学习字符串 2.字符串的查找 3.字符串的替换 4.进制转换…

Unity之TileMap

1、创建瓦片资源 教程中老师在Asset---Create---Tile创建&#xff0c;但是新版本Unity不能这样创建 新版本是在Asset---Create---2D--Tile里面选择&#xff0c;跟老师的不太一样&#xff0c;暂时也不懂怎么解决 所以我们可以用方法二创建&#xff1a; 在Window---2D---Tile…

ES6语法新特性(下)

7. set Set 是 ES6 提供的一种数据结构&#xff0c;它和数组很像&#xff0c;但是它里面的数据是不可重复的。 7.1.1 初始化 const set1 new Set([1, 2, 3, 4, 5, 5]); const set2 new Set([苹果,橘子,橘子]); console.log(set1); console.log(set2); /* 控制台输出&#…

【华为OD机试】投骰子【2023 B卷|200分】

【华为OD机试】-真题 !!点这里!! 【华为OD机试】真题考点分类 !!点这里 !! 题目描述 骰子是一个立方体,每个面一个数字,初始为左1,右2,前3(观察者方向),后4,上5,下6, 用123456表示这个状态,放置在平面上, 可以向左翻转(用L表示向左翻转1次), 可以向右翻转(用…