C Primer Plus 第四章编程练习笔记
序
今天完成了第四章的习题,把自己的解题代码分享在这里。就不像上次那么啰嗦了,这回只展示源代码,需要注意的地方,在注释里已经注明。
C4Q1
题目
Write a program that asks for your first name, your last name, and then prints the names
in the format last, first .
代码
1 | // C4Q1.c 姓名的输入输出 |
C4Q2
题目
Write a program that requests your first name and does the following with it:
a. Prints it enclosed in double quotation marks
b. Prints it in a field 20 characters wide, with the whole field in quotes and the name at the right end of the field
c. Prints it at the left end of a field 20 characters wide, with the whole field enclosed in quotes
d. Prints it in a field three characters wider than the name
代码
1 | // C4Q2.c 名字的多种输出格式 |
C4Q3
题目
Write a program that reads in a floating-point number and prints it first in decimal-point notation and then in exponential notation. Have the output use the following formats (the number of digits shown in the exponent may be different for your system):
a. The input is21.3
or2.1e+001
.
b. The input is+21.290
or2.129E+001
.
代码
1 | // C4Q3.c 浮点数的输出 |
C4Q4
题目
Write a program that requests your height in inches and your name, and then displays the information in the following form:
Dabney, you are 6.208 feet tall
Use typefloat
, and use / for division. If you prefer, request the height in centimeters and display it in meters.
代码
1 | // C4Q4.c 输出名字和身高 |
C4Q5
题目
Write a program that requests the download speed in megabits per second (Mbs) and the size of a file in megabytes (MB). The program should calculate the download time for the file. Note that in this context one byte is eight bits. Use type
float
, and use / for division. The program should report all three values (download speed, file size, and download time) showing two digits to the right of the decimal point, as in the following:At 18.12 megabits per second, a file of 2.20 megabytes
downloads in 0.97 seconds.
代码
1 | // C4Q5.c 下载 |
C4Q6
题目
Write a program that requests the user’s first name and then the user’s last name. Have it print the entered names on one line and the number of letters in each name on the following line. Align each letter count with the end of the corresponding name, as in the following:
1 | Melissa Honeybee |
Next, have it print the same information, but with the counts aligned with the beginning of each name.
1 | Melissa Honeybee |
代码
1 | // C4Q6.c 姓名与字符数的输出 |
C4Q7
题目
Write a program that sets a type
double
variable to 1.0/3.0 and a typefloat
variable to 1.0/3.0. Display each result three times—once showing four digits to the right of the decimal, once showing 12 digits to the right of the decimal, and once showing 16 digits to the right of the decimal. Also have the program includefloat.h
and display the values ofFLT_DIG
andDBL_DIG
. Are the displayed values of 1.0/3.0 consistent with these values?
代码
1 | // C4Q7.c float 和 double 类型对数值的存储精度 |
C4Q8
题目
Write a program that asks the user to enter the number of miles traveled and the number of gallons of gasoline consumed. It should then calculate and display the miles-per-gallon value, showing one place to the right of the decimal. Next, using the fact that one gallon is about 3.785 liters and one mile is about 1.609 kilometers, it should convert the miles-per-gallon value to a liters-per-100-km value, the usual European way of expressing fuel consumption, and display the result, showing one place to the right of the decimal. Note that the U. S. scheme measures the distance traveled per amount of fuel (higher is better), whereas the European scheme measures the amount of fuel per distance (lower is better).
Use symbolic constants (usingconst
or#define
) for the two conversion factors.
代码
1 | // C4Q8.c 燃油效率 |
跋
本节题目不难,关键是掌握 printf()
和 scanf()
的修饰符,以合乎期望地显示输出内容。
其中,第六题用到了 %*d
修饰符,可以动态地调整 field 的宽度,很有应用价值;
第七题对 float
和 double
类型的变量进行了不同小数位数的输出,结合 floats.h
头文件,可以对浮点型数据的存储有更深的理解;
第八题使用了 #define
定义 symbolic constant ,同时要求对数据进行有效处理,具有应用价值。
对于这些题目,有共同点需要注意:
- 对于以数组类型存储的字符串,在
scanf()
中不需要在变量名前加 ‘&’; - 对于其他类型的变量,则需要加 ‘&’。