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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int noOfSalesPerson = 6;
enum quarterType{QT1,QT2,QT3,QT4};
struct salesPersonRec
{
string ID; //salesperson’s ID
double saleByQuarter[4];
double totalSale;
};
void initialize(ifstream& indata, salesPersonRec list[],int listSize)
{
int count;
quarterType quarter;
for(count = 0; count < listSize; count++)
{
indata>>list[count].ID; //get salesperson’s ID
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter + 1))
list[count].saleByQuarter[quarter] = 0.0;
list[count].totalSale = 0.0;
}
}
void getData(ifstream& infile, salesPersonRec list[],int listSize)
{
int count;
quarterType quarter;
string sID;
int month;
double amount;
infile>>sID;
while(infile)
{
infile>>month>>amount;
for(count = 0; count < listSize; count++)
{
if(sID == list[count].ID)
{
if(1 <= month && month <= 3)
quarter = QT1;
else if(4 <= month && month <= 6)
quarter = QT2;
else if(7 <= month && month <= 9)
quarter = QT3;
else
quarter = QT4;
list[count].saleByQuarter[quarter] += amount;
//Step 4
break;
}
}
infile>>sID;
void saleByQuarter(salesPersonRec list[], int listSize,double totalByQuarter[])
{
quarterType quarter;
int count;
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter+1))
totalByQuarter[quarter] = 0.0;
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter+1))
for(count = 0; count < listSize; count++)
totalByQuarter[quarter] += list[count].saleByQuarter[quarter];
}
void totalSaleByPerson(salesPersonRec list[],int listSize)
{
int count;
quarterType quarter;
for(count = 0; count < listSize; count++)
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter + 1))
list[count].totalSale +=
list[count].saleByQuarter[quarter];
}
void maxSaleByPerson(ofstream& outData,salesPersonRec list[], int listSize)
{
int maxIndex = 0;
int count;
for(count = 1; count < listSize; count++)
if(list[maxIndex].totalSale < list[count].totalSale)
maxIndex = count;
outData<<"Max Sale by SalesPerson: ID = "
<<list[maxIndex].ID
<<", Amount = $"
<<list[maxIndex].totalSale<<endl;
}
void maxSaleByQuarter(ofstream& outData,double saleByQuarter[])
{
quarterType quarter;
quarterType maxIndex = QT1;
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter+1))
if(saleByQuarter[maxIndex] < saleByQuarter[quarter])
maxIndex = quarter;
outData<<"Max Sale by Quarter: Quarter = "
<<static_cast<int>(maxIndex) + 1
<<", Amount = $"<<saleByQuarter[maxIndex]<<endl;
}
void printReport(ofstream& outfile,salesPersonRec list[], int listSize,double saleByQuarter[])
{
int count;
quarterType quarter;
outfile<<"----------- Annual Report -------------"
<<endl;
outfile<<endl;
outfile<<" ID QT1 QT2 QT3 "
<<"QT4 Total"<<endl;
outfile<<"________________________________________________"
<<"_______________"<<endl;
for(count = 0; count < listSize; count++)
{
outfile<<list[count].ID<<" ";
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter + 1))
outfile<<setw(10)<<list[count].saleByQuarter[quarter];
outfile<<setw(10)<<list[count].totalSale<<endl;
}
outfile<<"Total ";
for(quarter = QT1; quarter <= QT4;
quarter = static_cast<quarterType>(quarter + 1))
outfile<<setw(10)<<saleByQuarter[quarter];
outfile<<endl<<endl<<endl;
}
int main()
{
//Step 1
ifstream infile;
ofstream outfile;
char inputfile[25];
char outputfile[25];
double totalSaleByQuarter[4];
salesPersonRec salesPersonList[noOfSalesPerson];
cout<<"Enter SalesPerson ID file name : ";
cin>>inputfile;
cout<<endl;
infile.open(inputfile);
if(!infile)
{
cout<<"Cannot open input file."<<endl;
return 1;
}
initialize(infile, salesPersonList, noOfSalesPerson);
infile.close();
cout<<"Enter sales data file name: ";
cin>>inputfile;
cout<<endl;
infile.open(inputfile);
if(!infile)
{
cout<<"Cannot open input file."<<endl;
return 1;
}
cout<<"Enter output file name: ";
cin>>outputfile;
cout<<endl;
outfile.open(outputfile);
outfile<<fixed<<showpoint<<setprecision(2);
getData(infile, salesPersonList, noOfSalesPerson);
saleByQuarter(salesPersonList, noOfSalesPerson,totalSaleByQuarter);
totalSaleByPerson(salesPersonList, noOfSalesPerson);
printReport(outfile, salesPersonList, noOfSalesPerson,totalSaleByQuarter);
maxSaleByPerson(outfile, salesPersonList,noOfSalesPerson);
maxSaleByQuarter(outfile, totalSaleByQuarter);
infile.close();
outfile.close();
system("pause");
return 0;
|