forked from demon90s/CppStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_7_07.cpp
87 lines (73 loc) · 1.76 KB
/
exercise_7_07.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
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
/*
* 练习7.7:使用这些新函数重写7.1.2节(第233页)练习中的交易处理程序。
*/
// ./exercise_7_07 < data/book_sales
// PS: 代码和exercise_7_06.cpp一模一样
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cerr;
using std::cin;
using std::string;
struct Sales_data
{
Sales_data &combine(const Sales_data &rhs);
std::string isbn() const { return bookNo; }
std::string bookNo;
unsigned int units_sold = {0};
double revenue = {0.0};
};
Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
std::istream &read(std::istream &is, Sales_data &sd)
{
double price = 0.0;
is >> sd.bookNo >> sd.units_sold >> price;
sd.revenue = price * sd.units_sold;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &sd)
{
double avg_price = 0.0;
if (sd.units_sold > 0)
avg_price = sd.revenue / sd.units_sold;
os << sd.bookNo << " "
<< sd.units_sold << " "
<< sd.revenue << " "
<< avg_price;
return os;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data tmp = lhs;
return tmp.combine(rhs);
}
int main()
{
Sales_data total; // 保存下一条交易记录的变量
// 读入第一条交易记录,并确保有数据可以处理
if (read(cin, total)) {
Sales_data trans; // 保存和的变量
// 读入并处理剩余交易记录
while (read(cin, trans)) {
// 如果我们仍在处理相同的书
if (total.isbn() == trans.isbn()) {
total.combine(trans);
}
else {
print(cout, total) << endl; // 输出结果
total = trans; // 处理下一本书
}
}
print(cout, total) << endl; // 输出最后一条交易
}
else {
cerr << "No data?!" << endl;
}
return 0;
}