1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#include <iostream>
#include <iomanip>
#include <cmath>
#define FleetAvg = 25.0
double tripMileage;
double CostPerMile;
double TripCost;
double overallMPG;
double totalMiles = 0;
double totalGallons = 0;
double totalCost = 0;
double gallons;
double miles;
double price;
double MileageComp;
char ANSWER;
double getGallons();
double getMiles();
double getPricePerGallon();
double calcTripMileage (double miles, double gallons);
double calcTripCost (double price, double gallons);
double calcTripCostPerMile (double TripCost, double miles);
double calcOverallMPG (double totalMiles, double totalGallons);
void showOneTrip (double TripMileage, double TripCost, double TripCostPerMile);
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG);
void showMileageComparison (double overallMPG);
using namespace std;
double getGallons()
{
double gallons;
cout << "Enter the number of gallons of fuel: ";
cin >> gallons;
while (gallons <= 0 || gallons > 40)
{
cout << "\n\n";
cout << "Out of range: must be between 0 and 40 gallons. Re-enter: ";
cin >> gallons;
cout << "\n";
}
}
double getMiles()
{
double miles;
cout << "Enter the number of miles: ";
cin >> miles;
while (miles <= 0 || miles > 800)
{
cout << "\n\n";
cout << "Out of range: must be between 0 and 800 miles. Re-enter: ";
cin >> miles;
cout << "\n";
}
}
double getPricePerGallon()
{
double price;
cout << "Enter the price per gallon: ";
cin >> price;
while (price <= 2 || price > 7)
{
cout << "\n\n";
cout << "Out of range: must be between $2.00 and $7.00 dollars. Re-enter: ";
cin >> price;
cout << "\n";
}
return price;
}
double calcTripMileage (double miles, double gallons)
{
double tripMileage;
if (gallons == 0)
{
tripMileage = 0;
}
else if (gallons > 0)
{
tripMileage = miles / gallons;
}
return tripMileage;
}
double calcTripCost (double price, double gallons)
{
double TripCost;
TripCost = price * gallons;
return TripCost;
}
double calcTripCostPerMile (double TripCost, double miles)
{
double CostPerMile;
if (miles == 0)
{
CostPerMile = 0;
}
else if (miles > 0)
{
CostPerMile = TripCost / miles;
}
return CostPerMile;
}
double calcOverallMPG (double totalMiles, double totalGallons)
{
double overallMPG;
if (totalGallons == 0)
{
overallMPG = 0;
}
else if (totalGallons > 0)
{
overallMPG = totalMiles/totalGallons;
}
return overallMPG;
}
void showOneTrip (double TripMileage, double TripCost, double TripCostPerMile)
{
cout << "Trip Mileage: " << fixed << showpoint << setprecision(2) << tripMileage << " MPG" << endl;
cout << "Trip Cost: $" << fixed << showpoint << setprecision(2) << TripCost << endl;
cout << "Trip cost per mile: $" << fixed << showpoint << setprecision(2) << CostPerMile << endl;
}
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG)
{
cout << "Total Miles: " << totalMiles << endl;
cout << "Total Gallons: " << totalGallons << endl;
cout << "Total Cost: " << totalCost << endl;
cout << "Overall MPG: " << overallMPG << endl;
}
void showMileageComparison (double overallMPG)
{
float MileageComp = overallMPG - FleetAvg;
if (MileageComp > 0)
{
cout << "Your vehicle's mileage is greater than the fleet average by " << MileageComp << "mpg.";
}
else if (MileageComp == 0)
{
cout << "Your vehicle's mileage is equal to the fleet average of " << FleetAvg << "mpg.";
}
else if (MileageComp < 0)
{
cout << "Your vehicle's mileage is is less than the fleet average by " << MileageComp << "mpg.";
}
}
int main()
{
cout << " Fuel Usage Analysis";
cout << "\n\n";
do
{
gallons = getGallons ();
miles = getMiles ();
price = getPricePerGallon ();
cout << "\n\n";
showOneTrip (tripMileage, TripCost, CostPerMile);
cout << "\n\n";
calcTripMileage (miles, gallons);
calcTripCost (price, gallons);
calcTripCostPerMile (TripCost, miles);
totalMiles += miles;
totalGallons += gallons;
totalCost += TripCost;
overallMPG = calcOverallMPG (totalMiles, totalGallons);
cout << "Another? (y or n) ";
cin >> ANSWER;
}
while((ANSWER == 'Y')||(ANSWER == 'y'));
cout << "\n\n";
showTotals (totalMiles, totalGallons, totalCost, overallMPG);
cout << "\n\n";
showMileageComparison (overallMPG);
return 0;
}
| |