《C Primer Plus 》第六版 习题 第四章

时间:2025-04-06 08:29:19
/** ***编写一个程序,提示用户输入旅行的里程和消耗的汽油量。 ***然后计算并显示消耗每加仑汽油行驶的英里数,显示小数点后面一位数字。 ***接下来,使用1加仑大约3.785升,1英里大约1.609千米,把单位是英里/加仑的值 ***转换为升/100公里(欧洲通用的燃料消耗表示法), ***并显示结果,显示小数点后面1位数字。 ***注意,美国采用的方案测量消耗单位燃料的行程(值越大越好) ***而欧洲采用单位距离消耗的燃料测量方案(值越低越好)。 ***使用#define 创建符号常量或使用const限定符创建变量来表示两个转换系数。 **/ #include <> #include <> #define GALLON 3.785 //1 gallon = 3.785 L #define MILE 1.609 //1 mile = 1.609 km int main() { system("color 0A"); float mile; //里程数 float gallon; //汽油量 printf("\n\tPlease enter the mileage of the trip:\n"); printf("\t请输入旅行的里程km:"); scanf("\t%f",&mile); printf("\n\tPlease enter the amount of petrol you need to consume:\n"); printf("\t请输入消耗的汽油量L:"); scanf("\t%f",&gallon); printf("\n\tThe oil consumption is %.1f mile /gallon\n",mile / gallon); printf("\t油耗为%.1f公里/升\n",mile / gallon); printf("\n\tLitre per 100km:%.1fL\n",gallon*GALLON/(mile*MILE)*100); printf("\t百公里消耗为:%.1f升\n",gallon*GALLON/(mile*MILE)*100); return 0; }