blob: 95df66f4f4cd2b7f813c2e982b7a00fac6cbd1c7 (
plain)
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
|
#include <iostream>
#include "sales_data.hpp"
/*
*
* 2.41 - 1.25
*
*
*/
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
int main() {
SalesData total; // variable to hold data for the next transaction
double price = 0.0;
if (cin >> total.BookNo >> total.UnitsSold >> price) {
total.Revenue = price * total.UnitsSold;
SalesData trans; // variable to hold the running sum
while (cin >> trans.BookNo >> trans.UnitsSold >> price) {
trans.Revenue = price * trans.UnitsSold; // We can either keep this line, or comment this line out and uncomment line 34. Same behavior either way, just 2 different ways of doing the same thing. Though i do think that this is the more "correct" way of doing it
if (total.BookNo == trans.BookNo) {
total.UnitsSold += trans.UnitsSold;
total.Revenue += trans.Revenue;
} else {
double average = total.Revenue / total.UnitsSold;
cout << total.BookNo << " " << total.UnitsSold << " "
<< total.Revenue << " avg: " << average << endl;
total = trans;
//total.Revenue = price * total.UnitsSold;
}
}
double average = total.Revenue / total.UnitsSold;
cout << total.BookNo << " " << total.UnitsSold << " "
<< total.Revenue << " avg: " << average << endl;
} else {
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
|