#include < iostream >
#include < string >
using namespace std;
class Sales_data{
// 友元函数
friend istream& operator >> (istream&, Sales_data&);
// 友元函数
friend ostream& operator << (ostream&, Sales_data&);
// 友元函数
friend bool operator < (const Sales_data&, const Sales_data&);
// 友元函数
friend bool operator == (const Sales_data&, const Sales_data&);
public: // 构造函数的3种形式
Sales_data() = default;
// Sales_data(const string &book): bookNo(book){ }
// Sales_data(istream &is){ is >> *this; }
public:
Sales_data& operator += (const Sales_data&);
string isbn() const { return bookNo; }
private:
string bookNo; // 书籍编号,隐式初始化为空串
static int units_sold; // 销售量,显式初始化为0
double sellingprice; // 原始价格,显式初始化为0.0
double sellprice; // 实售价格,显式初始化为0.0
double discount; // 折扣,显式初始化为0.0
};
inline bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs){
return lhs.isbn () == rhs.isbn ();
}
Sales_data operator + (const Sales_data&, const Sales_data&);
inline bool operator == (const Sales_data &lhs, const Sales_data &rhs){
return lhs.units_sold == rhs.units_sold &&
lhs.sellingprice == rhs.sellingprice &&
lhs.sellprice == rhs.sellprice &&
lhs.isbn () == rhs.isbn ();
}
inline bool operator != (const Sales_data &lhs, const Sales_data &rhs){
return !(lhs == rhs);
}
Sales_data& Sales_data::operator += (const Sales_data& rhs){
units_sold += rhs.units_sold ;
sellprice = (rhs.sellprice * rhs.units_sold + sellprice *units_sold)
/(rhs.units_sold + units_sold);
if (sellingprice != 0)
discount = sellprice / sellingprice;
return *this;
}
Sales_data operator += (const Sales_data &lhs, const Sales_data &rhs){
Sales_data ret(lhs);
ret += rhs;
return ret;
}
istream& operator>>(istream& in, Sales_data& s){
in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.sellprice;
if (in && s.sellingprice != 0)
s.discount = s.sellprice / s.sellingprice;
else
s = Sales_data();
return in;
}
ostream& operator<<(ostream& out, Sales_data& s){
out << s.isbn() << ” ” << s.units_sold << ” ”
<< s.sellingprice << ” ” << s.sellprice << ” ” << s.discount;
return out;
}
int main()
{
Sales_data book;
cout << “请输入销量纪录: ” << endl;
while (cin >> book) {
cout << ” ISBN、售出本数、原始价格、实售价格、折扣为” << book <<
endl;
}
Sales_data trans1, trans2;
cout << “请输入两条ISBN相同的销售记录: ” << endl;
cin >> trans1 >> trans2;
if (compareIsbn(trans1, trans2))
cout << “汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为 ”
<< trans1 + trans2 << endl;
else
cout << “两条销售记录的ISBN不同” << endl;
Sales_data total, trans;
cout << “请输入几条ISBN相同的销售记录: ” << endl;
if (cin >> total){
while (cin >> trans)
if (compareIsbn(total, trans))
total = total + trans;
else {
cout << “当前书籍ISNB不同” << endl;
return -1;
}
cout << “有效汇总信息:ISBN、售出本数、原始价格、实售价格、折扣为”
<< total << endl;
}
else {
cout << “没有数据” << endl;
return -1;
}
int num = 1; // 记录当前书籍的销售记录总数
cout << “请输入若干销售记录: ” << endl;
if (cin >> trans1) {
while (cin >> trans2)
if (compareIsbn(trans1, trans2))
num ++;
else {
cout << trans1.isbn () << “共有”
<< num << “条销售记录” << endl;
trans1 = trans2;
num = 1;
}
cout << trans.isbn () << “共有”
<< num << “条销售记录” << endl;
}
else {
cout << “没有数据” << endl;
return -1;
}
return 0;
}
VC 2008