0
点赞
收藏
分享

微信扫一扫

C Primer Plus(第六版)第四章4.8编程代码

写心之所想 2022-03-30 阅读 47

题目:编写一个程序, 提示用户输入旅行的里程和消耗的汽油量。 然后计算
并显示消耗每加仑汽油行驶的英里数, 显示小数点后面一位数字。 接下来,
使用1加仑大约3.785升, 1英里大约为1.609千米, 把单位是英里/加仑的值转
换为升/100公里(欧洲通用的燃料消耗表示法) , 并显示结果, 显示小数点
后面 1 位数字。 注意, 美国采用的方案测量消耗单位燃料的行程(值越大越
好) , 而欧洲则采用单位距离消耗的燃料测量方案(值越低越好) 。 使用
#define 创建符号常量或使用 const 限定符创建变量来表示两个转换系数。

#include<stdio.h>
//#define GALLON_COVT 3.785
//#define MILE 1.609
const float GALLON_COVT = 3.785;
const float MILE = 1.609;
int main(){
float mileage,fuel_consumption;
float guochen1;    //每一加仑的千米数
float guochen2;    //转换100千米的倍数
float x1;          //x1为每加仑的英里数
float x2;          //x2为每100千米的加仑数
float x3;          //x3为每一升的千米数
printf("Please enter the mileage of your trip(unit:mile):\n");
scanf("%f",&mileage);
printf("Please enter the fuel consumption(unit:gallon):\n");
scanf("%f",&fuel_consumption);
x1 = mileage / fuel_consumption;
//printf("%f\n",x1);
guochen1 = x1 * MILE;
guochen2 = 100 / guochen1;
//printf("%f\n",guochen1);
//printf("%f\n",guochen2);
x2 = guochen2 * GALLON_COVT;   //美国算法
//printf("%f\n",x2);
x3 = guochen1 / GALLON_COVT;   //欧洲算法
//printf("%f\n",x3);
printf("Worth it based on your input:\n%.1f miles per gallon.\n\
%.1f gallon per 100 kilometres.\n%.1f kilometres per litre.\n ",x1,x2,x3);
return 0;
}

运行结果:以10英里,2加仑为例
在这里插入图片描述
备注:自己写的,也可能有问题,大家一起交流呀

举报

相关推荐

0 条评论