-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path8.06.cpp
61 lines (57 loc) · 1.44 KB
/
8.06.cpp
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
/*
* Exercise 8.6: Rewrite the bookstore program from § 7.1.1 (p. 256) to read
* its transactions from a file. Pass the name of the file as an argument to
* main (§ 6.2.5, p. 218).
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <fstream>
#include <string>
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
int main(int argc, char **argv)
{
Sales_data total;
double averagePrice = 0.0;
double price = 0.0;
if (--argc != 1) {
std::cerr << "Usage: " + std::string(*argv) + " <filename>\n";
return -1;
}
auto p = argv + 1;
std::ifstream input(*p);
if (!input) {
std::cerr << "Couldn't open " << *p << '\n';
return -1;
}
if (input >> total.bookNo >> total.units_sold >> price) {
total.revenue = total.units_sold * price;
Sales_data trans;
while (input >> trans.bookNo >> trans.units_sold >> price) {
trans.revenue = trans.units_sold * price;
if (total.bookNo == trans.bookNo) {
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
averagePrice = total.revenue / total.units_sold;
} else {
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << " "
<< averagePrice << std::endl;
total = trans;
}
}
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << " "
<< averagePrice << std::endl;
} else {
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}