计算汽车省油,用英里每加仑比较,允许重复比较
//mile for gallon 汽车省油
#include<iostream>
const double GALLON = 0.264179;
double milepergallon(double kilo,double mile);
int main()
{
using namespace std;
double kilo[2],mile[2],milebygallon[2];
char ans;
do
{
cout<<"Enter the kilo and the mile of number first:\n";
cin>>kilo[0]>>mile[0];
cout<<"Enter the kilo and the mile of number second:\n";
cin>>kilo[1]>>mile[1];
milebygallon[0] = milepergallon(kilo[0],mile[0]);
milebygallon[1] = milepergallon(kilo[1],mile[1]);
cout<<"The first result is "<<milebygallon[0]<<" mile/gallon.\n";
cout<<"The second result is "<<milebygallon[1]<<" mile/gallon.\n";
if(milebygallon[0] > milebygallon[1])
cout<<"The first car fuel saving!"<<endl;
else if(milebygallon[0] < milebygallon[1])
cout<<"The second car fuel saving!"<<endl;
else
cout<<"The two cat are same saving oil!"<<endl;
cout<<"Do you want again?";
cin>>ans;
}while('y' == ans || 'Y' ==ans);
return 0;
}
double milepergallon(double kilo,double mile)
{
double gallon;
gallon = kilo * GALLON;
return (mile / gallon);
}
结果:
Enter the kilo and the mile of number first:
1 0.264179
Enter the kilo and the mile of number second:
1 0.264179
The first result is 1 mile/gallon.
The second result is 1 mile/gallon.
The two cat are same saving oil!
Do you want again?y
Enter the kilo and the mile of number first:
1 1
Enter the kilo and the mile of number second:
10 2
The first result is 3.78531 mile/gallon.
The second result is 0.757062 mile/gallon.
The first car fuel saving!
Do you want again?y
Enter the kilo and the mile of number first:
1 1
Enter the kilo and the mile of number second:
2 2
The first result is 3.78531 mile/gallon.
The second result is 3.78531 mile/gallon.
The two cat are same saving oil!
Do you want again?