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
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
const int MAX_ITEMS = 100;
struct prodType
{
int prodNum;
string prodName;
double prodPrice;
int numSold;
double prod;
};
int getLineNum()
{
int number_of_lines = 0;
string line;
ifstream myfile("sales.txt");
while (getline(myfile, line))
++number_of_lines;
cout << "Number of lines in text file: " << number_of_lines << endl;
return number_of_lines;
}
void getMenuFromFile(ifstream& infile, prodType sales[], int LineNum)
{
string strLine = "";
string strProdNum = "";
string strProdName = "";
string strProdPrice = "";
string strNumSold = "";
int LastEmptyPos;
int j = 0;
while (getline(infile, strLine) && (j < LineNum))
{
for (int i = 0; i < strLine.size(); i++)
if (strLine[i] == ' ') LastEmptyPos = i;
strProdNum = "";
strProdNum = strProdNum.append(strLine); //Store product number
stringstream(strProdNum) >> sales[j].prodNum;
strProdName = "";
strProdName = strProdName.append(strLine, LastEmptyPos - 20); //Store product name
stringstream(strProdName) >> sales[j].prodName;
strProdPrice = "";
strProdPrice = strProdPrice.append(strLine, LastEmptyPos - 8); //Store product price
stringstream(strProdPrice) >> sales[j].prodPrice;
strNumSold = "";
strNumSold = strNumSold.append(strLine, LastEmptyPos + 1, strLine.size() - LastEmptyPos); //Store number of units sold
stringstream(strNumSold) >> sales[j].numSold;
j++;
}
}
void makeProd(prodType sales[], double prod[], int LineNum)
{
for (int i = 0; i < LineNum; i++)
{
prod[i] = sales[i].prodPrice * sales[i].numSold;
};
}
void selectionSort(prodType sales[], int LineNum, double prod[])
{
int i, j, first;
for (i = LineNum - 1; i > 0; i--)
{
first = 0;
for (j = 1; j <= i; j++)
{
if (prod[j] > prod[i])
first = j;
}
prodType temp = sales[first];
sales[first] = sales[i];
sales[i] = temp;
}
}
void printStructMembers(prodType sales[])
{
cout << "The top selling product is " << sales[4].prodName << " with total sales of $" << sales[4].prodPrice * sales[4].numSold << endl;
cout << "The second top selling product is " << sales[3].prodName << " with total sales of $" << sales[3].prodPrice * sales[3].numSold << endl;
cout << "The third top selling product is " << sales[2].prodName << " with total sales of $" << sales[2].prodPrice * sales[2].numSold << endl;
cout << "The fourth top selling product is " << sales[1].prodName << " with total sales of $" << sales[1].prodPrice * sales[1].numSold << endl;
cout << "The lowest selling product is " << sales[0].prodName << " with total sales of $" << sales[0].prodPrice * sales[0].numSold << endl;
}
int main()
{
string strline;
prodType sales[MAX_ITEMS];
ifstream inFile("sales.txt");
double prod[MAX_ITEMS];
int LineNum = getLineNum();
if (inFile.is_open())
{
getMenuFromFile(inFile, sales, LineNum);
}
else
{
cout << "Cannot open the input file." << endl;
}
inFile.close();
makeProd(sales, prod, LineNum);
selectionSort(sales, MAX_ITEMS, prod);
printStructMembers (sales);
return 0;
}
|