c++ - 需要在 C++ 程序中询问汽车制造商
问题描述
三辆汽车行驶 500 英里。编写一个程序,输入汽车品牌和每辆汽车使用的燃料加仑数。在为每辆车调用一次 calcMPG() 函数后,主要确定并显示哪辆车最省油以及每加仑行驶了多少英里。calcMPG() 函数应传递行驶距离和用作参数的汽油加仑数,并应返回获得的每加仑英里数。--原始问题,这是我已经完成的,我需要添加汽车的品牌问题,我将在哪里为三辆汽车中的每一辆添加这个问题?
#include <iostream>
using namespace std;
float calcMPG(float distInMiles, float gallon)
{
return distInMiles / gallon; //return miles per gallon
}
int main()
{
// Establish variables
float distInMiles = 500;
float effi_c1, effi_c2, effi_c3, gallons;
// Ask for car make and amount of gallons used
cout << " How many gallons of gas did this car use? ";
cin >> gallons;
effi_c1 = calcMPG(distInMiles, gallons);
cout << " How many gallons of gas did this car use? ";
cin >> gallons;
effi_c2 = calcMPG(distInMiles, gallons);
cout << " How many gallons of gas did this car use? ";
cin >> gallons;
effi_c3 = calcMPG(distInMiles, gallons);
// Determine which car is most efficent
if (effi_c1 > effi_c2)
{
if (effi_c1 > effi_c3)
{
cout << "car 1 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c1;
}
else
{
cout << "car 3 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c3;
}
}
else
{
if (effi_c2 > effi_c3)
{
cout << "car 2 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c2;
}
else
{
cout << "car 3 is most efficient.\n";
cout << "efficiency (in miles per gallons) : " << effi_c3;
}
}
return 0;
}
解决方案
推荐阅读
- javascript - React:componentWillRecieveProps 始终比父状态落后一步
- oracle - 如何从pl sql中的变量将元素添加到数组中
- sapui5 - sapui5 TreeTable 更新绑定
- kotlin - Kotlin - 使扩展功能在整个项目中都可以访问
- java - 在Java中将字符串拆分为字符串(包含一些特殊字符)和整数
- docker - Docker - 在构建期间可访问的配置文件
- java - 使用 Jackson 将不同数量的对象反序列化为 Java 中的列表
- alexa - Alexa Skill - 在多轮对话中检索用户的邮件
- php - Codeigniter 3 升级会话锁定导致问题
- elasticsearch - Kubernetes 集群上的 Elasticsearch 集群。进还是出?