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
|
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<sstream>
using namespace std;
//CONSTANTS
const string FILENAME_IN = "REGISTER.txt",
FILENAME_H = "HOUSEHOLDS.txt",
FILENAME_V = "VEHICLES.txt";
const int CURRENT_YEAR = 2013,
FULL = 20;
const float AUTO_ROAD_FEE = 24.00,
CYCLE_ROAD_FEE = 12.00,
LICENSE_PERCENT= .0025,
AGE_RANGE1 = 5,
RANGE1_PERCENT_MSRP_AUTO = .03,
RANGE1_PERCENT_AGE_AUTO = .0015,
RANGE1_PERCENT_MSRP_CYCLE = .025,
RANGE1_PERCENT_AGE_CYCLE = .0009,
AGE_RANGE2_MIN = 6,
AGE_RANGE2_MAX = 10,
RANGE2_PERCENT_MSRP_AUTO = .0275,
RANGE2_PERCENT_AGE_AUTO = .0012,
RANGE2_PERCENT_MSRP_CYCLE = .0225,
RANGE2_PERCENT_AGE_CYCLE = .0009,
AGE_RANGE3_MIN = 11,
AGE_RANGE3_MAX = 15,
RANGE3_PERCENT_MSRP_AUTO = .025,
RANGE3_PERCENT_AGE_AUTO = .0009,
RANGE3_PERCENT_MSRP_CYCLE = .02,
RANGE3_PERCENT_AGE_CYCLE = .0009,
AGE_RANGE4 = 15,
AGE_RANGE4_TAX_AUTO = 20.00,
AGE_RANGE4_TAX_CYCLE = 10.00;
//FUNCTION PROTOTYPES
void getRoadFee(char& type, float& roadFee);
void getTax(char& type, float& msrp, int& age, float& tax);
void readRegister (ifstream& inRegister, int& houseid, int& year,
float& msrp, string& plate, char& type, int& age, float& tax,
float& roadFee);
int main()
{
int houseid = 0,
year = 0,
full = 0,
age = 0,
lineNum = 0,
noOfVehicles = 0;
float msrp = 0,
roadFee = 0,
licenseFee = 0,
tax = 0,
total = 0;
char type,
ch;
string plate;
cout << "This program will read data from REGISTER.txt"
"\nand output data into HOUSEHOLDS.txt and VEHICLES.txt"
<< endl << endl;
ifstream inRegister; //ifstream
inRegister.open (FILENAME_IN.c_str()); //open file
if(!inRegister) //if not open
cout << "Error opening input file " << FILENAME_IN << endl;
inRegister >> houseid; //prime read
while (inRegister)
{
readRegister (inRegister, houseid, year, msrp, plate, type, age, tax,
roadFee);
inRegister >> houseid;
}
cout << "\nHousehold ID Number \t" << "Vehicles Registered \t"
<< "Total Charges\n";
cout << fixed << showpoint << setprecision(2) << houseid << "\t"
<< setw(21) << noOfVehicles << setw(19) << "\t" << "$"
<< right << total;
ofstream outHouse,
outVehicles;
outHouse.open (FILENAME_H.c_str());
outVehicles.open (FILENAME_V.c_str());
system("pause");
return 0;
}
/******************************************************************
* readRegister: reads REGISTER.txt file
* input: ifstream inRegister, int houseid, int year, float msrp, string plate,
* char type, int age, float tax, float roadFee
* processes: gets file info, calculates tax, and calculates road fee
* output: tax and road fee
******************************************************************/
void readRegister (ifstream& inRegister, int& houseid, int& year,
float& msrp, string& plate, char& type, int& age, float& tax,
float& roadFee)
{
char ch;
float licenseFee = 0,
total = 0;
inRegister.get(ch); //read space
while (inRegister && ch != '\n')
{
inRegister >> type >> plate >> year >> msrp;// read rest of info
age = CURRENT_YEAR - year;
licenseFee = msrp * LICENSE_PERCENT;
getRoadFee(type, roadFee); //get road fee function
getTax(type, msrp, age, tax); //get tax function
total = roadFee + licenseFee + tax;
inRegister.get(ch); //read space
cout << houseid << ' ' << type << ' ' << plate << ' ' << year
<< ' ' << msrp << ' ' << ' ' << roadFee << ' '
<< licenseFee << ' ' << tax << ' ' << total << endl;
}
return;
}
/******************************************************************
* getRoadFee: calculates road fee for each vehicle
* input: char type, float roadFee
* output: road fee for specific type of vehicle
******************************************************************/
void getRoadFee(char& type, float& roadFee)
{
switch (type)
{
case 'A':
roadFee = AUTO_ROAD_FEE;
break;
case 'M':
roadFee = CYCLE_ROAD_FEE;
break;
}
}
/******************************************************************
* getTax: calculates tax for each vehicle
* input: char type, float msrp, int age, float tax
* output: tax for each vehicle
******************************************************************/
void getTax(char& type, float& msrp, int& age, float& tax)
{
if(type == 'A' && age <= AGE_RANGE1)
tax = (RANGE1_PERCENT_MSRP_AUTO * msrp) -
(RANGE1_PERCENT_AGE_AUTO * age * msrp);
if(type == 'A' && age >= AGE_RANGE2_MIN ||
age <= AGE_RANGE2_MAX)
tax = (RANGE2_PERCENT_MSRP_AUTO * msrp) -
(RANGE2_PERCENT_AGE_AUTO * age * msrp);
if(type == 'A' && age >= AGE_RANGE3_MIN ||
age <= AGE_RANGE3_MAX)
tax = (RANGE3_PERCENT_MSRP_AUTO * msrp) -
(RANGE3_PERCENT_AGE_AUTO * age * msrp);
if(type == 'A' && age > AGE_RANGE4)
tax = AGE_RANGE4_TAX_CYCLE;
if(type == 'M' && age <= AGE_RANGE1)
tax = (RANGE1_PERCENT_MSRP_CYCLE * msrp) -
(RANGE1_PERCENT_AGE_CYCLE * age * msrp);
if(type == 'M' && age >= AGE_RANGE2_MIN ||
age <= AGE_RANGE2_MAX)
tax = (RANGE2_PERCENT_MSRP_CYCLE * msrp) -
(RANGE2_PERCENT_AGE_CYCLE * age * msrp);
if(type == 'M' && age >= AGE_RANGE3_MIN ||
age <= AGE_RANGE3_MAX)
tax = (RANGE3_PERCENT_MSRP_CYCLE * msrp) -
(RANGE3_PERCENT_AGE_CYCLE * age * msrp);
if(type == 'M' && age > AGE_RANGE4)
tax = AGE_RANGE4_TAX_CYCLE;
}
|