C Primer Plus 第三章编程练习笔记
前言
最近假期重拾了C Primer Plus,做了做第三章的编程练习题。因为才是第三章,所以难度不是很大,不过有一些小地方还是值得注意的。因为中文版翻译的水平实在难以恭维,而且英文版确实上手不难,所以就用英文版的题目了。
题目1
Find out what your system does with integer overflow, floating-point overflow, andfloating-point underflow by using the experimental approach; that is, write programs having these problems. (You can check the discussion in Chapter 4 of
limits.h
andfloat.h
to get guidance on the largest and smallest values.)
思路
这题比较简单,在于考察底层的知识。可以参考注释,找找自己电脑的 limits.h
头文件,找到相关的定义。像我这台64位的电脑,int
和 long
就都占了4个字节。还能获取到具体的数值。
源代码
1 | // <C3Q1> integer overflow and floating-point overflow / underflow |
题目2
Write a program that asks you to enter an ASCII code value, such as 66, and then prints the character having that ASCII code.
思路
本题涉及到之前讲的,和转义字符有关的知识。注意事项有输入输出的类型等等,注释里写的很详尽。为了让代码更有整体性,注意的地方和疑难在注释中更全面,这是本文的一个约定。
源代码
1 | // <C3Q2.c> print a character based on given ASCII code |
题目3
Write a program that sounds an alert and then prints the following text:
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"
思路
还是转义字符的知识,如何打印出\a,以及如何打印出有特殊含义的转义字符,如 \、”等。
源代码
1 | /* <C3Q3.c> using printf */ |
题目4
Write a program that reads in a floating-point number and prints it first in decimal-point notation, then in exponential notation, and then, if your system supports it, p notation.
Have the output use the following format (the actual number of digits displayed for the exponent depends on the system):Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.01p+6
思路
浮点数的格式化输出,还挺有用的。之前不太喜欢 %e
格式的输出,用习惯了还觉得挺顺眼的。
源代码
1 | /* <C3Q4.c> handle floats */ |
题目5
There are approximately 3.156 × 107 seconds in a year. Write a program that requests your age in years and then displays the equivalent number of seconds.
思路
下边几道题都是单位换算,比较简单。
源代码
1 | // <C3Q5.c> year to seconds |
题目6
The mass of a single molecule of water is about 3.0×10-23 grams. A quart of water is about 950 grams.Write a program that requests an amount of water, in quarts, and displays the number of water molecules in that amount.
源代码
1 | /* <C3Q6.c> quarts to molecules */ |
题目7
There are 2.54 centimeters to the inch. Write a program that asks you to enter your height in inches and then displays your height in centimeters. Or, if you prefer, ask for the height in centimeters and convert that to inches.
源代码
1 | /* <C3Q7.c> centimeter to inch */ |
题目8
In the U.S. system of volume measurements, a pint is 2 cups, a cup is 8 ounces, an ounce is 2 tablespoons, and a tablespoon is 3 teaspoons. Write a program that requests a volume in cups and that displays the equivalent volumes in pints, ounces, tablespoons, and teaspoons. Why does a floating-point type make more sense for this application than an integer type?
源代码
1 | /* <C3Q8.c> volume measurements */ |
小结
本章的题目大多和数据类型的转换有关。在把代码整合为文章的过程中,又习得了 MarkDown 的另一个语法,用左右各三个`符号可以框住代码块。当然还有上标 <sup></sup>
以及下标 <sub></sub>
。还能学到HTML,一举两得。