我上周的作业是
编写一个程序,该程序将读取两辆汽车消耗的汽油升数和行驶的英里数,然后输出每辆汽车每加仑燃料消耗的英里数,并比较两者的燃油效率。
我完成了哪一个。这个星期的专案是要修改先前的专案,并且
添加两个void函数:
一个名为userInstruction的指令,告诉用户该做什么
另一个displayResult,它显示原始信息(公升和行驶的英里数)和结果(每加仑的英里数)。
看来我已经设法使其大部分工作了。它至少可以编译和运行。但是,第1汽车似乎显示的是从第2汽车获得的燃油升数和行驶的英里数,尽管它获得了每加仑正确的英里数。我不确定自己在做什么错。也许我对她想要的完全不满意,这就是为什么我不知道吗?
还有一个附带的问题,是否有任何方法可以将“对汽车2进行同样的处理”放入userInstruction
无效函数中?
const double LPG = 0.264179;
double mpg(double miles, double liters);
void userInstruction();
void displayResult(double mpg1, double mpg2, double liters, double miles);
int main()
{
char keepGoing;
double liters, miles, mpg1, mpg2;
do
{
userInstruction();
cin >> liters;
cin >> miles;
//calling function
mpg1 = mpg(miles, liters);
cout << endl << endl << "Do the same for Car 2. \n" << endl;
cin >> liters;
cin >> miles;
mpg2 = mpg(miles, liters);
displayResult(mpg1, mpg2, liters, miles);
if (mpg1 == mpg2)
{
cout << "Both cars have the same fuel efficiency. \n" << endl;
}
else if (mpg1 > mpg2)
{
cout << "Car 1 has better fuel efficiency. \n" << endl;
}
else
{
cout << "Car 2 has better fuel efficency. \n" << endl;
}
//Allows for repeating
cout << "Do you wish to keep going? (y/n) \n";
cin >> keepGoing;
}
while (keepGoing == 'y' || keepGoing == 'Y');
system("pause");
return 0;
}
double mpg(double miles, double liters)
{
double milesPerGallon;
milesPerGallon = miles / (liters * LPG);
return milesPerGallon;
}
void userInstruction()
{
cout << "Please enter the liters of gasoline consumed as well as the miles travelled. \n" << endl;
}
void displayResult(double mpg1, double mpg2, double liters, double miles)
{
cout << "Car 1 used: " << liters << " liters of fuel \n";
cout << "Car 1 drove: " << miles << " miles \n";
cout << "Car 1 gets: " << mpg1 << " miles per gallon \n" << endl;
cout << "Car 2 used: " << liters << " liters of fuel \n";
cout << "Car 2 drove: " << miles << " miles \n";
cout << "Car 2 gets: " << mpg2 << " miles per gallon \n" << endl;
}
在这里,我希望输入(5 30)和(55 123)的结果能在各自正确的位置打印出来。
Car 1 used: 55 liters of fuel Car 1 drove: 123 miles Car 1 gets: 22.7119 miles per gallon Car 2 used: 55 liters of fuel Car 2 drove: 123 miles Car 2 gets: 8.46533 miles per gallon Car 1 has better fuel efficiency.
答案 0 :(得分:0)
汽车1和汽车2分别为march=native
和liters
设置了一组单独的值,但是您只保存了用户输入的最后一组,然后将该最后一组传递给{{ 1}},因此最终您会为两辆汽车显示相同的值。您需要分别传递两个集合,就像两个计算的MPG一样,例如:
miles
话虽如此,请考虑使用displayResult()
或double mpg(double miles, double liters);
void userInstruction();
void displayResult(double miles1, double liters1, double mpg1,
double miles2, double liters2, double mpg2);
int main()
{
double miles1, liters1, mpg1;
double miles2, liters2, mpg2;
char keepGoing;
do
{
userInstruction();
cin >> liters1;
cin >> miles1;
//calling function
mpg1 = mpg(miles1, liters1);
cout << endl << endl << "Do the same for Car 2. \n" << endl;
cin >> liters2;
cin >> miles2;
mpg2 = mpg(miles2, liters2);
displayResult(miles1, liters1, mpg1, miles2, liters2, mpg2);
//Allows for repeating
cout << "Do you wish to keep going? (y/n) \n";
cin >> keepGoing;
}
while (keepGoing == 'y' || keepGoing == 'Y');
system("pause");
return 0;
}
static const double LPG = 0.264179;
double mpg(double miles, double liters)
{
return miles / (liters * LPG);
}
void userInstruction()
{
cout << "Please enter the liters of gasoline consumed as well as the miles travelled for Car 1. \n" << endl;
}
void displayResult(double miles1, double liters1, double mpg1,
double miles2, double liters2, double mpg2)
{
cout << "Car 1 used: " << liters1 << " liters of fuel \n";
cout << "Car 1 drove: " << miles1 << " miles \n";
cout << "Car 1 gets: " << mpg1 << " miles per gallon \n" << endl;
cout << "Car 2 used: " << liters2 << " liters of fuel \n";
cout << "Car 2 drove: " << miles2 << " miles \n";
cout << "Car 2 gets: " << mpg2 << " miles per gallon \n" << endl;
if (mpg1 == mpg2)
{
cout << "Both cars have the same fuel efficiency. \n" << endl;
}
else if (mpg1 > mpg2)
{
cout << "Car 1 has better fuel efficiency. \n" << endl;
}
else
{
cout << "Car 2 has better fuel efficency. \n" << endl;
}
}
来帮助您更好地组织每辆汽车的价值和功能,例如:
struct