-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.25.cpp
More file actions
43 lines (41 loc) · 1.44 KB
/
1.25.cpp
File metadata and controls
43 lines (41 loc) · 1.44 KB
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
#include "Sales_data.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
int main() {
Sales_data total; // variable to hold data for the next transaction
// read the first transaction and ensure that there are data to process
double price, average_price;
if (cin >> total.bookNo >> total.units_sold >> price) {
total.revenue = total.units_sold * price;
Sales_data trans; // variable to hold the running sum
// read and process the remaining transactions
while (cin >> trans.bookNo >> trans.units_sold >> price) {
trans.revenue = trans.units_sold * price;
// if we're still processing the same book
if (total.bookNo == trans.bookNo) {
// update the running total
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
} else {
// print results for the previous book
average_price = total.revenue / total.units_sold;
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " " << average_price << endl;
// total now refers to the next book
total.bookNo = trans.bookNo;
total.units_sold = trans.units_sold;
total.revenue = trans.revenue;
}
}
// print the last transaction
average_price = total.revenue / total.units_sold;
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " " << average_price << endl;
} else {
// no input! warn the user
cerr << "No data?!" << endl;
return -1;
}
return 0;
}