我应该制作一个简单的控制台程序,允许用户输入他们的行驶里程和多次行程中使用的加仑。它使用while循环结构并计算两种燃料经济性,一种用于行程,以及整体燃料经济性。我的问题是让我的输出与我正在学习的书中的内容相匹配。它很接近,但不是现场。
这是本书的输出:
Enter miles driven (-1 to quit): 287
Enter gallons used: 13
MPG this trip: 22.076923
Total MPG: 22.076923
Enter miles driven (-1 to quit): 200
Enter gallons used: 10
MPG this trip: 20
Total MPG: 21.173913
Enter miles driven (-1 to quit): 120
Enter gallons used: 5
MPG this trip: 24
Total MPG: 21.678571
这是我到目前为止所做的:
#include <iostream>
int main ( )
{
// Set our variables and initialize them
double miles = 0;
double gallons = 0;
double economyTrip = 0;
double economyTotal = 0;
while (miles >= 0)
{
std::cout << "Enter miles driven (-1 to quit): ";
std::cin >> miles ;
// Check input
if (miles == -1)
break;
else
std::cout << "Enter gallons used: ";
std::cin >> gallons;
// Calculate trip fuel economy
economyTrip = (miles/gallons) ;
// Calculate total fuel economy
if (economyTotal == 0)
economyTotal = economyTrip;
else
economyTotal += (economyTrip - economyTotal)/2;
// Display the results
std::cout << "MPG this trip: " << economyTrip << std::endl;
std::cout << "Total MPG:" << economyTotal << std::endl << "\n";
}
}
我的平均方式错了吗?我知道在计算器上,如果你取三个行程里程的平均值,你会得到22.02563左右。 。 。 所以,至少可以说这本书比我更接近。我想知道我怎么能以不同的方式做到这一点,所以我可以得到一个更接近的答案。或者这是我应该担心的事情?
答案 0 :(得分:2)
您计算总经济的公式是错误的。你应该把驱动里程的总和除以所用燃料的总和。
答案 1 :(得分:1)
您无法继续计算MPG并将其添加到运行总计中。您需要存储总里程和总加仑,然后在最后计算MPG。
答案 2 :(得分:1)
有两种方法可以获得平均值:
使用第二种更复杂的方法通常没什么好处,所以我建议你跟上第一种方法。
只是为了好玩,让我们以第二种方式做到......但让我们先了解我们在做什么。
avg1 = a
avg2 = (a + b) / 2
avg3 = (a + b + c) / 3 = (a + b) / 3 + c / 3
= (a + b) * 2 / (2 * 3) + c / 3
= (a + b) / 2 * (2 / 3) + c / 3
= avg2 * (2 / 3) + c / 3
avg4 = (a + b + c + d) / 4 = avg3 * (3 / 4) + d / 4
因此:avg[N] = avg[N-1] * (N - 1) / N + val[N] / N
开始感觉像是重心:) 注意:avg[N-1] * (N-1)
实际上是N-1
之前的总数!
因此,我们可以保持英里/加仑的平均值和加仑的总和(而不是保持英里和加仑的总和)(在Python中,只是为了显示原理):
avgMiles = 0.0
totGallons = 0.0
while True:
miles = float(raw_input("Enter miles driven: "))
gallons = float(raw_input("Enter gallons used: "))
print "MPG this trip:", miles / gallons
newTotGallons = totGallons + gallons
avgMiles = avgMiles * totGallons / newTotGallons + miles / newTotGallons
totGallons = newTotGallons
print "Total MPG:", avgMiles
output is(或者如果是互动的话):
Enter miles driven: 200
Enter gallons used: 10
MPG this trip: 20.0
Total MPG: 20.0
Enter miles driven: 100
Enter gallons used: 10
MPG this trip: 10.0
Total MPG: 15.0
Enter miles driven: 50
Enter gallons used: 5
MPG this trip: 10.0
Total MPG: 14.0
Enter miles driven:
顺便说一句,通常使用-1
来表示用户输入的结束(至少在Linux上),而不是CTRL+D
。
答案 3 :(得分:0)
比发布时间晚了一段时间。我有相同的家庭作业,除了C(不是C ++),所以有一些微小的差异。我已经解决了这个问题,我将把这个解决方案提供给将来可能会遇到这篇文章的人。
#include <stdio.h>
int main()
{
float gallons, miles, mpg, averagempg, gallonstotal, milestotal;
gallons = 0;
miles = 0;
mpg = 0;
averagempg = 0;
gallonstotal = 0;
milestotal = 0;
printf("\nEnter gallons used (-1 to end): ");
scanf("%f", &gallons);
while (gallons != -1) {
gallonstotal = gallonstotal + gallons;
printf("Enter the miles driven: ");
scanf("%f", &miles);
milestotal = milestotal + miles;
mpg = miles / gallons;
printf("The miles / gallon for this tank was %f \n", mpg);
printf("\nEnter gallons used (-1 to end): ");
scanf("%f", &gallons);
}
if (gallonstotal != 0){
averagempg = milestotal / gallonstotal;
printf("\nThe overall average miles/gallon was %f \n", averagempg);
}
else {
puts("Error: no gallons were entered \n");
}
}
答案 4 :(得分:0)
#include <iostream>
using namespace std;
int main()
{
unsigned int tripCounter = 0;
double tripMPG = 0;
double totalTripMPG = 0;
cout << "Enter miles driven (-1 to quit): ";
double miles = 0;
cin >> miles;
while ( miles != -1 )
{
cout << "Enter gallons used: ";
double gallons = 0;
cin >> gallons;
tripMPG = miles / gallons;
cout << "MPG this trip: " << tripMPG << endl;
tripCounter = tripCounter + 1;
totalTripMPG = totalTripMPG + tripMPG;
double avgMPG = static_cast< double >( totalTripMPG ) / tripCounter;
cout << "Total MPG: " << avgMPG << endl;
cout << endl;
cout << "Enter miles driven (-1 to quit): ";
cin >> miles;
}
system("pause");
}