练习1-3 修改温度转换程序,使之能在转换表的顶部打印一个标题。
ChapterOneExerciseThreeOne.cpp
cpp
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */
main()
{
float fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */
printf("Fahr Celsius\n");
while(fahr <= upper){
celsius = (5.0 / 9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}
在循环语句之前增加的printf("Fahr Celsius\n");语句将在温度转换表的顶部产生一个表头。为了让输出内容与这个表头对齐,我们还在%3.0f和%6.1r之间增加了两个空格。上面这个程序中的其余语句与K&R原著第12页中给出的代码完全一致。