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
|
#include<iostream>
#include<fstream>
#include<time.h>
using namespace std;
//________________________
void populateArray(int n, int aId[], int aSum[], int aCount[], string fileName);
void calculateProduct(int n, int aId[], int aSum[], int aCount[], double aAvg[]);
void showAllRatings(int n, int aId[], double aAvg[], int aCount[]);
void showBestWorstProducts(int n, int aId[], double aAvg[]);
void searchAndShowSelectedProduct(int n, int aId[], double aAvg[], int aCount[]);
//___main()____________________
int main() {
int aId[11], aSum[11] = { 0 }, aCount[11] = { 0 };
double aAvg[11];
int n = 10;
string fileName = "c:\temp\rateMyProduct.txt";
populateArray(100, aId, aSum, aCount, fileName);
calculateProduct(n, aId, aSum, aCount, aAvg);
showAllRatings(n, aId, aAvg, aCount);
showBestWorstProducts(n, aId, aAvg);
searchAndShowSelectedProduct(n, aId, aAvg, aCount);
return 0;
}
//________________________
void populateArray(int n, int aId[], int aSum[], int aCount[], string fileName) {
ifstream file;
file.open(fileName);
int pid{};
int rating{};
while (n && file >> pid >> rating)
{
--n;
aId[pid] = pid;
aSum[pid] += rating;
aCount[pid]++;
}
}
//________________________
void calculateProduct(int n, int aId[], int aSum[], int aCount[], double aAvg[]) {
for (int i = 1; i <= n; i++) {
int pid = aId[i];
aAvg[pid] = (float)aSum[pid] / aCount[pid];
}
}
//________________________
void showAllRatings(int n, int aId[], double aAvg[], int aCount[]) {
cout << "Dataset - Product Rating (Five Stars)" << endl;
for (int i = 1; i <= n; i++) {
int pid = aId[i];
cout << "Prod. " << pid << "\t\t" << "Stars: " << aAvg[pid] << "\t" << "Review: " << aCount[pid] << endl;
}
}
//________________________
void showBestWorstProducts(int n, int aId[], double aAvg[]) {
double bestAvg = aAvg[1], worstAvg = aAvg[1];
int bestPid = aId[1], worstPid = aId[1];
for (int i = 2; i <= n; i++) {
int pid = aId[i];
if (aAvg[pid] > bestAvg) {
bestAvg = aAvg[pid];
bestPid = pid;
}
if (aAvg[pid] < worstAvg) {
worstAvg = aAvg[pid];
worstPid = pid;
}
}
cout << "---------------------------------------------" << endl;
cout << "Best Product(s): Product: " << bestPid << "(" << bestAvg << " stars)," << endl;
cout << "Worst Product(s): Product: " << worstPid << "(" << worstAvg << " stars)," << endl;
cout << "---------------------------------------------" << endl;
}
//________________________
void searchAndShowSelectedProduct(int n, int aId[], double aAvg[], int aCount[]) {
cout << "Retriev product by ID" << endl;
int pid;
while (true) {
bool flag = true;
cout << "Enter product ID[0-" << n << "]: ";
cin >> pid;
if (pid == 0) {
cout << "All done!" << endl;
return;
}
for (int i = 1; i <= n; i++) {
if (pid == aId[i]) {
cout << "Product " << pid << " (" << aAvg[pid] << " Stars, " << aCount[pid] << " Reviews)" << endl;
flag = false;
}
}
if (flag)
cout << "Sorry - Product not found - Try again" << endl;
}
}
|