/*
Input file: numbers.txt
Output file: numbers.out
Generated output:
Start End Gallon Stop City combi hiway
20265 20970 12.20 46 22.50 18.75 15.00
51350 51780 13.10 14 45.00 37.50 30.00
*/
/*
1) Attributes: starting odometer reading (int), ending odometer reading (int), gallons (double), number of stops (int).
2) Default constructor: all attributes are set to zero.
3) Initial value constructor. You must make sure that the ending odometer reading is greater than the starting odometer reading. If this condition is not met, output an error message and exit the program.
4) Accessors and mutators for all attributes. You must make sure that the ending odometer reading is greater than the starting odometer reading. If this condition is not met, output an error message and exit the program.
5) A method that calculates the average miles-per-gallon for the last fill-up, determines whether it's a city reading (miles per stop < 0.5), highway reading (miles per stop > 1), or combined reading (miles per stop between 0.5 and 1, inclusive), and then estimates the other two readings. Use the estimate that (city MPG) = 0.75*(combined MPG) and (combined MPG) = 0.75*(highway MPG). The output for this method must be an array with three elements (city MPG, combined MPG, and highway MPG).
The attached file contains 8 values: starting odometer reading, ending odometer reading, gallons, and number of stops for car 1, then the same sequence of quantities for car 2. Read the data and create a formatted output file that contains the read values for each car as well as each car's three calculated MPG values.
20265 20970 12.2 46
51350 51780 13.1 14
*/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class MPG {
public:
enum READINGS{cityMPG, combinedMPG, hiwayMPG, last_readings};
private:
static const string msg[1];
int starting_odometer_reading;
int ending_odometer_reading;
double gallons;
int number_of_stops;
double reading[last_readings];
public:
MPG() : starting_odometer_reading(0), ending_odometer_reading(0), gallons(0.0), number_of_stops(0)
{
}
MPG(const int& starting, const int& ending, const double& volume, const int& stops) : starting_odometer_reading(starting), ending_odometer_reading(ending),gallons(volume), number_of_stops(stops)
{
if (ending_odometer_reading <= starting_odometer_reading) throw(msg[0]);
reading[cityMPG] = 0.0;
reading[combinedMPG] = 0.0;
reading[hiwayMPG] = 0.0;
miles_per_gallon();
}
const int& get_start_reading() const {return starting_odometer_reading;}
const int& get_end_reading() const {return ending_odometer_reading;}
const double& get_gallons() const {return gallons;}
const int& get_stops() const {return number_of_stops;}
const double* get_readings() const {return reading;}
void set(const int& start, const int& stop)
{
if (stop <= start) throw(msg[0]);
starting_odometer_reading = start;
ending_odometer_reading = stop;
miles_per_gallon();
}
void set_gallons(const double& cgallons) {gallons = cgallons;}
void set_stops(const int& iStops) {number_of_stops = iStops;}
friend istream& operator>>(istream& is, MPG& mpg)
{
is >> mpg.starting_odometer_reading >> mpg.ending_odometer_reading >> mpg.gallons >> mpg.number_of_stops;
mpg.miles_per_gallon();
return is;
}
friend ostream& operator<<(ostream& os, const MPG& mpg)
{
os << setw(6) << right << mpg.starting_odometer_reading << " " <<
setw(6) << right << mpg.ending_odometer_reading << " " <<
setw(6) << right << fixed << setprecision(2) << mpg.gallons << " " <<
setw(4) << right << mpg.number_of_stops << " " <<
setw(6) << right << fixed << setprecision(2) << mpg.get_readings()[0] << " " <<
setw(6) << right << fixed << setprecision(2) << mpg.get_readings()[1] << " " <<
setw(6) << right << fixed << setprecision(2) << mpg.get_readings()[2];
return os;
}
/*
A method that calculates the average miles-per-gallon for the last fill-up, determines whether it's a city reading (miles per stop < 0.5), highway reading (miles per stop > 1), or combined reading (miles per stop between 0.5 and 1, inclusive), and then estimates the other two readings. Use the estimate that (city MPG) = 0.75*(combined MPG) and (combined MPG) = 0.75*(highway MPG). The output for this method must be an array with three elements (city MPG, combined MPG, and highway MPG).
*/
double miles_per_gallon()
{
double dblStops = (ending_odometer_reading - starting_odometer_reading) / number_of_stops;
if (dblStops < 0.5) {
reading[cityMPG] = dblStops;
reading[combinedMPG] = dblStops * 0.75;
reading[hiwayMPG] = dblStops * 0.75 * 0.75;
}
else if (dblStops < 1.0) {
reading[cityMPG] = dblStops * 1.25;
reading[combinedMPG] = dblStops;
reading[hiwayMPG] = dblStops * 0.75;
}
else {
reading[cityMPG] = dblStops * 1.5;
reading[combinedMPG] = dblStops * 1.25;
reading[hiwayMPG] = dblStops;
}
return dblStops;
}
};
const string MPG::msg[1] = {"Ending Odometer Reading must be larger than Starting Odometer Reading!\nProgram terminates!"};
void default_title(ostream& os)
{
os << setw(6) << "Start" << " " <<
setw(6) << "End" << " " <<
setw(6) << "Gallon" << " " <<
setw(4) << "Stop" << " " <<
setw(6) << "City" << " " <<
setw(6) << "combi" << " " <<
setw(6) << "hiway" << " " << endl;
}
void read_file(const string& strFilename, MPG* mpg)
{
const string msg = "Input file could not be opened!";
ifstream is(strFilename.c_str());
if (!is.good()) throw msg;
else {
is >> mpg[0];
is >> mpg[1];
}
}
void write_file(const string& strFilename, MPG* mpg)
{
ofstream os(strFilename.c_str());
default_title(os);
os << mpg[0] << endl << mpg[1];
}
int main()
{
try {
MPG mpg[2];
read_file("numbers.txt", mpg);
write_file("numbers.out", mpg);
default_title(cout);
cout << mpg[0] << endl << mpg[1] << endl;
}
catch(const string& str) {
cout << str << endl;
}
return 0;
}