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 243 244 245 246 247 248 249 250 251 252
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int NO_OF_SALES_PERSON = 6;
struct salesPersonRec
{
string ID; //salesperson's ID
double saleByQuarter[4]; //array to store the total
//sales for each quarter
double totalSale; //salesperson's yearly sales amount
};
void initialize(ifstream& indata, salesPersonRec list[],
int listSize);
void getData(ifstream& infile, salesPersonRec list[],
int listSize);
void saleByQuarter(salesPersonRec list[], int listSize,
double totalByQuarter[]);
void totalSaleByPerson(salesPersonRec list[], int listSize);
void printReport(ofstream& outfile, salesPersonRec list[],
int listSize, double saleByQuarter[]);
void maxSaleByPerson(ofstream& outData, salesPersonRec list[],
int listSize);
void maxSaleByQuarter(ofstream& outData, double saleByQuarter[]);
int main()
{
//Step 1
ifstream infile; //input file stream variable
ofstream outfile; //output file stream variable
string inputFile; //variable to hold the input file name
string outputFile; //variable to hold the output file name
double totalSaleByQuarter[4]; //array to hold the
//sale by quarter
salesPersonRec salesPersonList[NO_OF_SALES_PERSON]; //array
//to hold the salesperson's data
cout << "Enter the salesPerson ID file name: "; //Step 2
cin >> inputFile; //Step 3
cout << endl;
infile.open(inputFile.c_str()); //Step 4
if (!infile) //Step 5
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
initialize(infile, salesPersonList,
NO_OF_SALES_PERSON); //Step 6
infile.close(); //Step 7
infile.clear(); //Step 7
cout << "Enter the sales data file name: "; //Step 8
cin >> inputFile; //Step 9
cout << endl;
infile.open(inputFile.c_str()); //Step 10
if (!infile) //Step 11
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
cout << "Enter the output file name: "; //Step 12
cin >> outputFile; //Step 13
cout << endl;
outfile.open(outputFile.c_str()); //Step 14
outfile << fixed << showpoint
<< setprecision(2); //Step 15
getData(infile, salesPersonList,
NO_OF_SALES_PERSON); //Step 16
saleByQuarter(salesPersonList,
NO_OF_SALES_PERSON,
totalSaleByQuarter); //Step 17
totalSaleByPerson(salesPersonList,
NO_OF_SALES_PERSON); //Step 18
printReport(outfile, salesPersonList,
NO_OF_SALES_PERSON,
totalSaleByQuarter); //Step 19
maxSaleByPerson(outfile, salesPersonList,
NO_OF_SALES_PERSON); //Step 20
maxSaleByQuarter(outfile, totalSaleByQuarter); //Step 21
infile.close(); //Step 22
outfile.close(); //Step 22
return 0;
}
void initialize(ifstream& indata, salesPersonRec list[],
int listSize)
{
int index;
int quarter;
for (index = 0; index < listSize; index++)
{
indata >> list[index].ID; //get salesperson's ID
for (quarter = 0; quarter < 4; quarter++)
list[index].saleByQuarter[quarter] = 0.0;
list[index].totalSale = 0.0;
}
} //end initialize
void getData(ifstream& infile, salesPersonRec list[],
int listSize)
{
int index;
int quarter;
string sID;
int month;
double amount;
infile >> sID; //get salesperson's ID
while (infile)
{
infile >> month >> amount; //get the sale month and
//the sale amount
for (index = 0; index < listSize; index++)
if (sID == list[index].ID)
break;
if (1 <= month && month <= 3)
quarter = 0;
else if (4 <= month && month <= 6)
quarter = 1;
else if (7 <= month && month <= 9)
quarter = 2;
else
quarter = 3;
if (index < listSize)
list[index].saleByQuarter[quarter] += amount;
else
cout << "Invalid salesperson's ID." << endl;
infile >> sID;
} //end while
} //end getData
void saleByQuarter(salesPersonRec list[], int listSize,
double totalByQuarter[])
{
int quarter;
int index;
for (quarter = 0; quarter < 4; quarter++)
totalByQuarter[quarter] = 0.0;
for (quarter = 0; quarter < 4; quarter++)
for (index = 0; index < listSize; index++)
totalByQuarter[quarter] +=
list[index].saleByQuarter[quarter];
} //end saleByQuarter
void totalSaleByPerson(salesPersonRec list[], int listSize)
{
int index;
int quarter;
for (index = 0; index < listSize; index++)
for (quarter = 0; quarter < 4; quarter++)
list[index].totalSale +=
list[index].saleByQuarter[quarter];
} //end totalSaleByPerson
void printReport(ofstream& outfile, salesPersonRec list[],
int listSize, double saleByQuarter[])
{
int index;
int quarter;
outfile << "----------- Annual Sales Report ---------"
<< "----" << endl;
outfile << endl;
outfile << " ID QT1 QT2 QT3 "
<< "QT4 Total" << endl;
outfile << "____________________________________________"
<< "_________________" << endl;
for (index = 0; index < listSize; index++)
{
outfile << list[index].ID << " ";
for (quarter = 0; quarter < 4; quarter++)
outfile << setw(10)
<< list[index].saleByQuarter[quarter];
outfile << setw(10) << list[index].totalSale << endl;
}
outfile << "Total ";
for (quarter = 0; quarter < 4; quarter++)
outfile << setw(10) << saleByQuarter[quarter];
outfile << endl << endl;
} //end printReport
void maxSaleByPerson(ofstream& outData, salesPersonRec list[],
int listSize)
{
int maxIndex = 0;
int index;
for (index = 1; index < listSize; index++)
if (list[maxIndex].totalSale < list[index].totalSale)
maxIndex = index;
outData << "Max Sale by SalesPerson: ID = "
<< list[maxIndex].ID
<< ", Amount = $" << list[maxIndex].totalSale
<< endl;
} //end maxSaleByPerson
void maxSaleByQuarter(ofstream& outData,
double saleByQuarter[])
{
int quarter;
int maxIndex = 0;
for (quarter = 0; quarter < 4; quarter++)
if (saleByQuarter[maxIndex] < saleByQuarter[quarter])
maxIndex = quarter;
outData << "Max Sale by Quarter: Quarter = "
<< maxIndex + 1
<< ", Amount = $" << saleByQuarter[maxIndex]
<< endl;
} //end maxSaleByQuarter
|