Skip to content

Commit 21ba988

Browse files
committed
finish ch17
1 parent c4372ab commit 21ba988

File tree

8 files changed

+1594
-42
lines changed

8 files changed

+1594
-42
lines changed

cpp_source/ch17/ex_17_4.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <tuple>
3+
#include <string>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <utility>
7+
#include <numeric>
8+
9+
#include "ex_17_4_SalesData.h"
10+
11+
using namespace std;
12+
13+
// matches有三个成员:1.一个书店的索引。2.指向书店中元素的迭代器。3.指向书店中元素的迭代器。
14+
typedef tuple<vector<Sales_data>::size_type,
15+
vector<Sales_data>::const_iterator,
16+
vector<Sales_data>::const_iterator>
17+
matches;
18+
19+
// files保存每家书店的销售记录
20+
// findBook返回一个vector,每家销售了给定书籍的书店在其中都有一项
21+
vector<matches> findBook(const vector<vector<Sales_data>> &files,
22+
const string &book)
23+
{
24+
vector<matches> ret; //初始化为空vector
25+
// 对每家书店,查找给定书籍匹配的记录范围
26+
for (auto it = files.cbegin; it != files.cend(); ++it)
27+
{
28+
// 查找具有相同ISBN的Sales_data范围,found是一个迭代器pair
29+
auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn);
30+
if (found.first != found.second) // 此书店销售了给定书籍
31+
// 记住此书店的索引及匹配的范围
32+
ret.push_back(make_tuple(it - files.cbegin(), found.first, found.second));
33+
}
34+
return ret; //如果未找到匹配记录,ret为空
35+
}
36+
37+
void reportResults(istream &in, ostream &os,
38+
const vector<vector<Sales_data> > &files){
39+
string s; //要查找的书
40+
while (in >> s){
41+
auto trans = findBook(files, s);
42+
if (trans.empty()){
43+
cout << s << " not found in any stores" << endl;
44+
continue; // 获得下一本要查找的书
45+
}
46+
for (const auto &store : trans) // 对每家销售了给定书籍的书店
47+
// get<n>返回store中tuple的指定的成员
48+
os << "store " << get<0>(store) << " sales: "
49+
<< accumulate(get<1>(store), get<2>(store), Sales_data(s))
50+
<< endl;
51+
}
52+
}
53+
54+
int main(){
55+
return 0;
56+
}

cpp_source/ch17/ex_17_4_SalesData.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <iostream>
2+
using std::istream; using std::ostream;
3+
4+
#include "ex_17_4_SalesData.h"
5+
6+
Sales_data::Sales_data(std::istream &is)
7+
{
8+
// read will read a transaction from is into this object
9+
read(is, *this);
10+
}
11+
12+
double
13+
Sales_data::avg_price() const {
14+
if (units_sold)
15+
return revenue/units_sold;
16+
else
17+
return 0;
18+
}
19+
20+
// add the value of the given Sales_data into this object
21+
Sales_data&
22+
Sales_data::combine(const Sales_data &rhs)
23+
{
24+
units_sold += rhs.units_sold; // add the members of rhs into
25+
revenue += rhs.revenue; // the members of ``this'' object
26+
return *this; // return the object on which the function was called
27+
}
28+
// = Sales_data
29+
Sales_data &Sales_data::operator =(const Sales_data &rhs)
30+
{
31+
this->bookNo = rhs.bookNo;
32+
this->revenue = rhs.revenue;
33+
this->units_sold = rhs.units_sold;
34+
35+
return *this;
36+
}
37+
38+
// =string
39+
Sales_data &Sales_data::operator =(const std::string &rhs)
40+
{
41+
*this= Sales_data(rhs);
42+
return *this;
43+
}
44+
45+
// +=
46+
Sales_data &Sales_data::operator +=(const Sales_data &rhs)
47+
{
48+
this->revenue += rhs.revenue;
49+
this->units_sold += rhs.units_sold;
50+
51+
return *this;
52+
}
53+
54+
Sales_data
55+
add(const Sales_data &lhs, const Sales_data &rhs)
56+
{
57+
Sales_data sum = lhs; // copy data members from lhs into sum
58+
sum.combine(rhs); // add data members from rhs into sum
59+
return sum;
60+
}
61+
62+
// transactions contain ISBN, number of copies sold, and sales price
63+
istream&
64+
read(istream &is, Sales_data &item)
65+
{
66+
double price = 0;
67+
is >> item.bookNo >> item.units_sold >> price;
68+
item.revenue = price * item.units_sold;
69+
return is;
70+
}
71+
72+
ostream&
73+
print(ostream &os, const Sales_data &item)
74+
{
75+
os << item.isbn() << " " << item.units_sold << " "
76+
<< item.revenue << " " << item.avg_price();
77+
return os;
78+
}
79+
80+
// added 10.Jan 2014
81+
std::ostream &
82+
operator <<(std::ostream &os, const Sales_data &item)
83+
{
84+
os << item.isbn() << " " << item.units_sold << " "
85+
<< item.revenue << " " << item.avg_price();
86+
87+
return os;
88+
}
89+
90+
// added 12.Jan 2014
91+
std::istream&
92+
operator >>(std::istream &is, Sales_data &s)
93+
{
94+
double price;
95+
96+
// read input
97+
is >> s.bookNo >> s.units_sold >> price;
98+
99+
// if successful, write into the object, give the object default state otherwise.
100+
if(is)
101+
s.revenue = s.units_sold * price;
102+
else
103+
s = Sales_data();
104+
105+
return is;
106+
}

cpp_source/ch17/ex_17_4_SalesData.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#ifndef SALES_DATA_H
2+
#define SALES_DATA_H
3+
4+
#include <string>
5+
#include <iostream>
6+
7+
8+
class Sales_data
9+
{
10+
// friends
11+
friend Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs);
12+
13+
friend std::ostream&
14+
operator << (std::ostream& os, const Sales_data& s);
15+
16+
friend std::istream&
17+
operator >> (std::istream& is, Sales_data& s);
18+
19+
friend Sales_data add(const Sales_data&, const Sales_data&);
20+
friend std::ostream &print(std::ostream&, const Sales_data&);
21+
friend std::istream &read(std::istream&, Sales_data&);
22+
23+
public:
24+
// constructors
25+
Sales_data() = default;
26+
Sales_data(const std::string &s): bookNo(s) { }
27+
Sales_data(const std::string &s, unsigned n, double p):
28+
bookNo(s), units_sold(n), revenue(p*n) { }
29+
Sales_data(const Sales_data &s ):
30+
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
31+
{ }
32+
33+
Sales_data(Sales_data&& s):
34+
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
35+
{ }
36+
37+
~Sales_data(){ }
38+
Sales_data(std::istream &);
39+
40+
std::string isbn() const { return bookNo; }
41+
Sales_data& combine(const Sales_data&);
42+
43+
// assignments
44+
Sales_data& operator =(const Sales_data& rhs);
45+
Sales_data& operator =(const std::string& rhs);
46+
Sales_data& operator +=(const Sales_data& rhs);
47+
48+
// conversion
49+
explicit operator std::string () const { return bookNo; }
50+
explicit operator double () const { return revenue; }
51+
52+
double avg_price() const;
53+
private:
54+
std::string bookNo;
55+
unsigned units_sold = 0;
56+
double revenue = 0.0;
57+
};
58+
59+
60+
// overloaded operators added 10.Jan.2014 for ex14.2
61+
inline Sales_data
62+
operator+(const Sales_data& lhs, const Sales_data& rhs)
63+
{
64+
Sales_data sum = lhs;
65+
sum += rhs;
66+
67+
return sum;
68+
}
69+
70+
std::ostream&
71+
operator << (std::ostream& os, const Sales_data& item);
72+
73+
std::istream&
74+
operator >> (std::istream& is, Sales_data& s);
75+
76+
// nonmember Sales_data interface functions
77+
Sales_data add(const Sales_data&, const Sales_data&);
78+
std::ostream &print(std::ostream&, const Sales_data&);
79+
std::istream &read(std::istream&, Sales_data&);
80+
81+
// used in future chapters
82+
inline
83+
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
84+
{
85+
return lhs.isbn() < rhs.isbn();
86+
}
87+
#endif

excersize/ch10.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ int main()
179179
}
180180
```
181181

182-
## 练习10.10
182+
## 练习10.10
183183

184184
> 你认为算法不改变容器大小的原因是什么?
185185

0 commit comments

Comments
 (0)