-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,594 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <iostream> | ||
#include <tuple> | ||
#include <string> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <utility> | ||
#include <numeric> | ||
|
||
#include "ex_17_4_SalesData.h" | ||
|
||
using namespace std; | ||
|
||
// matches有三个成员:1.一个书店的索引。2.指向书店中元素的迭代器。3.指向书店中元素的迭代器。 | ||
typedef tuple<vector<Sales_data>::size_type, | ||
vector<Sales_data>::const_iterator, | ||
vector<Sales_data>::const_iterator> | ||
matches; | ||
|
||
// files保存每家书店的销售记录 | ||
// findBook返回一个vector,每家销售了给定书籍的书店在其中都有一项 | ||
vector<matches> findBook(const vector<vector<Sales_data>> &files, | ||
const string &book) | ||
{ | ||
vector<matches> ret; //初始化为空vector | ||
// 对每家书店,查找给定书籍匹配的记录范围 | ||
for (auto it = files.cbegin; it != files.cend(); ++it) | ||
{ | ||
// 查找具有相同ISBN的Sales_data范围,found是一个迭代器pair | ||
auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn); | ||
if (found.first != found.second) // 此书店销售了给定书籍 | ||
// 记住此书店的索引及匹配的范围 | ||
ret.push_back(make_tuple(it - files.cbegin(), found.first, found.second)); | ||
} | ||
return ret; //如果未找到匹配记录,ret为空 | ||
} | ||
|
||
void reportResults(istream &in, ostream &os, | ||
const vector<vector<Sales_data> > &files){ | ||
string s; //要查找的书 | ||
while (in >> s){ | ||
auto trans = findBook(files, s); | ||
if (trans.empty()){ | ||
cout << s << " not found in any stores" << endl; | ||
continue; // 获得下一本要查找的书 | ||
} | ||
for (const auto &store : trans) // 对每家销售了给定书籍的书店 | ||
// get<n>返回store中tuple的指定的成员 | ||
os << "store " << get<0>(store) << " sales: " | ||
<< accumulate(get<1>(store), get<2>(store), Sales_data(s)) | ||
<< endl; | ||
} | ||
} | ||
|
||
int main(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <iostream> | ||
using std::istream; using std::ostream; | ||
|
||
#include "ex_17_4_SalesData.h" | ||
|
||
Sales_data::Sales_data(std::istream &is) | ||
{ | ||
// read will read a transaction from is into this object | ||
read(is, *this); | ||
} | ||
|
||
double | ||
Sales_data::avg_price() const { | ||
if (units_sold) | ||
return revenue/units_sold; | ||
else | ||
return 0; | ||
} | ||
|
||
// add the value of the given Sales_data into this object | ||
Sales_data& | ||
Sales_data::combine(const Sales_data &rhs) | ||
{ | ||
units_sold += rhs.units_sold; // add the members of rhs into | ||
revenue += rhs.revenue; // the members of ``this'' object | ||
return *this; // return the object on which the function was called | ||
} | ||
// = Sales_data | ||
Sales_data &Sales_data::operator =(const Sales_data &rhs) | ||
{ | ||
this->bookNo = rhs.bookNo; | ||
this->revenue = rhs.revenue; | ||
this->units_sold = rhs.units_sold; | ||
|
||
return *this; | ||
} | ||
|
||
// =string | ||
Sales_data &Sales_data::operator =(const std::string &rhs) | ||
{ | ||
*this= Sales_data(rhs); | ||
return *this; | ||
} | ||
|
||
// += | ||
Sales_data &Sales_data::operator +=(const Sales_data &rhs) | ||
{ | ||
this->revenue += rhs.revenue; | ||
this->units_sold += rhs.units_sold; | ||
|
||
return *this; | ||
} | ||
|
||
Sales_data | ||
add(const Sales_data &lhs, const Sales_data &rhs) | ||
{ | ||
Sales_data sum = lhs; // copy data members from lhs into sum | ||
sum.combine(rhs); // add data members from rhs into sum | ||
return sum; | ||
} | ||
|
||
// transactions contain ISBN, number of copies sold, and sales price | ||
istream& | ||
read(istream &is, Sales_data &item) | ||
{ | ||
double price = 0; | ||
is >> item.bookNo >> item.units_sold >> price; | ||
item.revenue = price * item.units_sold; | ||
return is; | ||
} | ||
|
||
ostream& | ||
print(ostream &os, const Sales_data &item) | ||
{ | ||
os << item.isbn() << " " << item.units_sold << " " | ||
<< item.revenue << " " << item.avg_price(); | ||
return os; | ||
} | ||
|
||
// added 10.Jan 2014 | ||
std::ostream & | ||
operator <<(std::ostream &os, const Sales_data &item) | ||
{ | ||
os << item.isbn() << " " << item.units_sold << " " | ||
<< item.revenue << " " << item.avg_price(); | ||
|
||
return os; | ||
} | ||
|
||
// added 12.Jan 2014 | ||
std::istream& | ||
operator >>(std::istream &is, Sales_data &s) | ||
{ | ||
double price; | ||
|
||
// read input | ||
is >> s.bookNo >> s.units_sold >> price; | ||
|
||
// if successful, write into the object, give the object default state otherwise. | ||
if(is) | ||
s.revenue = s.units_sold * price; | ||
else | ||
s = Sales_data(); | ||
|
||
return is; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#ifndef SALES_DATA_H | ||
#define SALES_DATA_H | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
|
||
class Sales_data | ||
{ | ||
// friends | ||
friend Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs); | ||
|
||
friend std::ostream& | ||
operator << (std::ostream& os, const Sales_data& s); | ||
|
||
friend std::istream& | ||
operator >> (std::istream& is, Sales_data& s); | ||
|
||
friend Sales_data add(const Sales_data&, const Sales_data&); | ||
friend std::ostream &print(std::ostream&, const Sales_data&); | ||
friend std::istream &read(std::istream&, Sales_data&); | ||
|
||
public: | ||
// constructors | ||
Sales_data() = default; | ||
Sales_data(const std::string &s): bookNo(s) { } | ||
Sales_data(const std::string &s, unsigned n, double p): | ||
bookNo(s), units_sold(n), revenue(p*n) { } | ||
Sales_data(const Sales_data &s ): | ||
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue) | ||
{ } | ||
|
||
Sales_data(Sales_data&& s): | ||
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue) | ||
{ } | ||
|
||
~Sales_data(){ } | ||
Sales_data(std::istream &); | ||
|
||
std::string isbn() const { return bookNo; } | ||
Sales_data& combine(const Sales_data&); | ||
|
||
// assignments | ||
Sales_data& operator =(const Sales_data& rhs); | ||
Sales_data& operator =(const std::string& rhs); | ||
Sales_data& operator +=(const Sales_data& rhs); | ||
|
||
// conversion | ||
explicit operator std::string () const { return bookNo; } | ||
explicit operator double () const { return revenue; } | ||
|
||
double avg_price() const; | ||
private: | ||
std::string bookNo; | ||
unsigned units_sold = 0; | ||
double revenue = 0.0; | ||
}; | ||
|
||
|
||
// overloaded operators added 10.Jan.2014 for ex14.2 | ||
inline Sales_data | ||
operator+(const Sales_data& lhs, const Sales_data& rhs) | ||
{ | ||
Sales_data sum = lhs; | ||
sum += rhs; | ||
|
||
return sum; | ||
} | ||
|
||
std::ostream& | ||
operator << (std::ostream& os, const Sales_data& item); | ||
|
||
std::istream& | ||
operator >> (std::istream& is, Sales_data& s); | ||
|
||
// nonmember Sales_data interface functions | ||
Sales_data add(const Sales_data&, const Sales_data&); | ||
std::ostream &print(std::ostream&, const Sales_data&); | ||
std::istream &read(std::istream&, Sales_data&); | ||
|
||
// used in future chapters | ||
inline | ||
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) | ||
{ | ||
return lhs.isbn() < rhs.isbn(); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,7 +179,7 @@ int main() | |
} | ||
``` | ||
|
||
## 练习10.10 | ||
## 练习10.10 | ||
|
||
> 你认为算法不改变容器大小的原因是什么? | ||
|
Oops, something went wrong.