练习8.7
1 #include2 #include 3 #include "factmain.h" 4 #include 5 using namespace std; 6 7 struct Sales_data { 8 friend ostream &print(ostream &os, const Sales_data &item); 9 friend istream &read(istream &is, Sales_data &item);10 private:11 string bookNo;12 unsigned units_sold;13 double price;14 double revenue;15 double avg_price() const;16 public:17 Sales_data(const string &s, const unsigned &u, const double &p) : bookNo(s), units_sold(u), price(p) { cout << "one" << endl; }18 Sales_data() : Sales_data("12345", 3, 12) { cout << "使用的是委托构造函数构造默认构造函数" << endl; }19 Sales_data(string s) : Sales_data(s, 2, 4) { cout << "仅仅输入一个字符串" << endl; }20 //Sales_data(const string &s, const unsigned &u) : bookNo(s), units_sold(u) {}21 Sales_data(istream &is) : Sales_data() { read(is, *this); cout << "最后一种" << endl; }22 string isbn() const { return bookNo; }23 unsigned &rUnit() { return units_sold; }24 double &rPrice() { return price; }25 double &rRevenue() { return revenue; }26 Sales_data &combine(const Sales_data&);27 28 };29 30 Sales_data add(const Sales_data &lhs, const Sales_data &rhs);31 ostream &print(ostream &os, const Sales_data &item);32 istream &read(istream &is, Sales_data &item);33 34 35 int main() //main函数的部分需要改变36 {37 ifstream in("sales_data.txt");38 ofstream out("shuchu.txt");39 //改动地方,新增一个Sales_data.txt 40 Sales_data total;41 if (read(in, total)) {42 //read(cin,total); 变为read(in,total); 43 Sales_data trans;44 while (read(in, trans)) {45 //read(cin, total); 变为read(in,total); 46 if (total.isbn() == trans.isbn())47 total.combine(trans);48 else49 {50 print(out, total) << endl;51 total = trans;52 }53 }54 print(out, total) << endl;55 }56 else {57 cerr << "No data?!" << endl;58 }59 system("pause");60 return 0;61 }62 63 Sales_data add(const Sales_data & lhs, const Sales_data & rhs) //非成员函数add64 {65 Sales_data sum = lhs;66 sum.combine(rhs);67 return sum;68 // TODO: 在此处插入 return 语句69 }70 71 istream & read(istream & is, Sales_data & item)72 {73 is >> item.bookNo >> item.units_sold >> item.price;74 item.revenue = item.units_sold * item.price;75 return is;76 // TODO: 在此处插入 return 语句77 }78 79 ostream & print(ostream & os, const Sales_data & item)80 {81 os << item.bookNo << " " << item.units_sold << " " << item.price << " " << item.revenue;82 return os;83 // TODO: 在此处插入 return 语句84 }85 86 Sales_data & Sales_data::combine(const Sales_data &rhs)87 {88 units_sold += rhs.units_sold;89 revenue += rhs.revenue;90 return *this;91 }92 93 inline double Sales_data::avg_price() const94 {95 return units_sold ? revenue/units_sold : 0;96 }
练习8.8
1 #include2 #include 3 #include "factmain.h" 4 #include 5 using namespace std; 6 7 struct Sales_data { 8 friend ostream &print(ostream &os, const Sales_data &item); 9 friend istream &read(istream &is, Sales_data &item);10 private:11 string bookNo;12 unsigned units_sold;13 double price;14 double revenue;15 double avg_price() const;16 public:17 Sales_data(const string &s, const unsigned &u, const double &p) : bookNo(s), units_sold(u), price(p) { cout << "one" << endl; }18 Sales_data() : Sales_data("12345", 3, 12) { cout << "使用的是委托构造函数构造默认构造函数" << endl; }19 Sales_data(string s) : Sales_data(s, 2, 4) { cout << "仅仅输入一个字符串" << endl; }20 //Sales_data(const string &s, const unsigned &u) : bookNo(s), units_sold(u) {}21 Sales_data(istream &is) : Sales_data() { read(is, *this); cout << "最后一种" << endl; }22 string isbn() const { return bookNo; }23 unsigned &rUnit() { return units_sold; }24 double &rPrice() { return price; }25 double &rRevenue() { return revenue; }26 Sales_data &combine(const Sales_data&);27 28 };29 30 Sales_data add(const Sales_data &lhs, const Sales_data &rhs);31 ostream &print(ostream &os, const Sales_data &item);32 istream &read(istream &is, Sales_data &item);33 34 35 int main() //main函数的部分需要改变36 {37 ifstream in("sales_data.txt");38 ofstream out("shuchu.txt",ofstream::app);39 //改动地方,新增一个Sales_data.txt 40 Sales_data total;41 if (read(in, total)) {42 //read(cin,total); 变为read(in,total); 43 Sales_data trans;44 while (read(in, trans)) {45 //read(cin, total); 变为read(in,total); 46 if (total.isbn() == trans.isbn())47 total.combine(trans);48 else49 {50 print(out, total) << endl;51 total = trans;52 }53 }54 print(out, total) << endl;55 }56 else {57 cerr << "No data?!" << endl;58 }59 system("pause");60 return 0;61 }62 63 Sales_data add(const Sales_data & lhs, const Sales_data & rhs) //非成员函数add64 {65 Sales_data sum = lhs;66 sum.combine(rhs);67 return sum;68 // TODO: 在此处插入 return 语句69 }70 71 istream & read(istream & is, Sales_data & item)72 {73 is >> item.bookNo >> item.units_sold >> item.price;74 item.revenue = item.units_sold * item.price;75 return is;76 // TODO: 在此处插入 return 语句77 }78 79 ostream & print(ostream & os, const Sales_data & item)80 {81 os << item.bookNo << " " << item.units_sold << " " << item.price << " " << item.revenue;82 return os;83 // TODO: 在此处插入 return 语句84 }85 86 Sales_data & Sales_data::combine(const Sales_data &rhs)87 {88 units_sold += rhs.units_sold;89 revenue += rhs.revenue;90 return *this;91 }92 93 inline double Sales_data::avg_price() const94 {95 return units_sold ? revenue/units_sold : 0;96 }