当前位置:   article > 正文

C Primer Plus第三章编程练习答案_cprimerplus第三章编程答案

cprimerplus第三章编程答案

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

1.通过试验(即编写带有此类问题的程序)观察系统如何处理整数上溢、浮点数上溢和浮点数下溢的情况。

  1. #include <stdio.h>
  2. #include <float.h>
  3. #include <limits.h>
  4. int main(void)
  5. {
  6. int big_int = 2147483647;
  7. float big_float = 3.4e38;
  8. float small_flaot = 10.0 / 3;
  9. printf("The big int data is %d\n", big_int + 1);
  10. printf("The big float data is %f\n", big_float + 1);
  11. printf("The big float data is %f\n", small_flaot);
  12. printf("The Max float data is %f\n", FLT_MAX);
  13. printf("The Max int data is %d\n", INT_MAX);
  14. return 0;
  15. }

2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印 输入的字符。

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int input = '0';
  5. printf("请输入一个ASILL码值:");
  6. scanf("%d", &input);
  7. printf("input = %c\n", input);
  8. return 0;
  9. }

3.编写一个程序,发出一声警报,然后打印下面的文本:

Startled by the sudden sound, Sally shouted,

"By the Great Pumpkin, what was that!"

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. printf("\a");
  5. printf("Startled by the sudden sound, Sally shouted,\n");
  6. printf("\"By the Great Pumpkin, what was that!\"\n");
  7. return 0;
  8. }

4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。 按以下格式输出(实际显示的指数位数因系统而异):

Enter a floating-point value: 64.25

fixed-point notation: 64.250000

exponential notation: 6.425000e+01

p notation: 0x1.01p+6

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. float output = 64.25;
  5. printf("Enter a floating-point value:%.2f\n", output);
  6. printf("fixed-point notation:%f\n", output);
  7. printf("exponential notation:%e\n", output);
  8. printf("p notation: %.2a\n", output);
  9. return 0;
  10. }

5.一年大约有3.156×10 7秒。编写一个程序,提示用户输入年龄,然后显 示该年龄对应的秒数。

  1. #include <stdio.h>
  2. #define YEAR_TO_SECOND 3.156e7
  3. int main(void)
  4. {
  5. int age = 0;
  6. printf("请输入你的年龄:");
  7. scanf("%d", &age);
  8. printf("该年龄对应的秒数位:%f\n", age * YEAR_TO_SECOND);
  9. return 0;
  10. }

6.1个水分子的质量约为3.0×10 −23克。1夸脱水大约是950克。编写一个 程序,提示用户输入水的夸脱数,并显示水分子的数量。

  1. #include <stdio.h>
  2. #define MASS_PER_MOLE 3.0e-23
  3. #define MASS_PER_QUART 950
  4. int main(void)
  5. {
  6. float quart = 0.0;
  7. printf("请输入水的夸脱数:");
  8. scanf("%f", &quart);
  9. printf("水分子的数量为:%f\n", quart * MASS_PER_QUART / MASS_PER_MOLE);
  10. return 0;
  11. }

7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英 寸),然后以厘米为单位显示身高。

 

  1. #include <stdio.h>
  2. #define INCH_TO_CM 2.54
  3. int main(void)
  4. {
  5. float inch = 0.0;
  6. printf("请输入身高:");
  7. scanf("%f", &inch);
  8. printf("身高为 %.3f cm\n", inch * INCH_TO_CM);
  9. return 0;
  10. }

8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等 于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以 品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用 浮点类型比整数类型更合适?

  1. #include <stdio.h>
  2. #define PINT_CUP 2
  3. #define CUP_OUNCE 8
  4. #define OUNCE_SPOON 2
  5. #define SPOON_TEA 3
  6. int main(void)
  7. {
  8. float pint, cup, ounce, spoon, tea;
  9. printf("请输入杯数:");
  10. scanf("%f", &cup);
  11. pint = cup / PINT_CUP;
  12. ounce = cup * CUP_OUNCE;
  13. spoon = ounce * OUNCE_SPOON;
  14. tea = spoon * SPOON_TEA;
  15. printf("%.1f cup equals %.1f pint,%.1f ounce,%.1f spoon,%.1f tea\n",
  16. cup, pint, ounce, spoon, tea);
  17. return 0;
  18. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/497701
推荐阅读
相关标签
  

闽ICP备14008679号