1.20
头文件:
1 #ifndef SALESITEM_H 2 // we're here only if SALESITEM_H has not yet been defined 3 #define SALESITEM_H 4 5 // Definition of Sales_item class and related functions goes here 6 #include <iostream> 7 #include <string> 8 9 class Sales_item { 10 // these declarations are explained section 7.2.1, p. 270 11 // and in chapter 14, pages 557, 558, 561 12 friend std::istream& operator>>(std::istream&, Sales_item&); 13 friend std::ostream& operator<<(std::ostream&, const Sales_item&); 14 friend bool operator<(const Sales_item&, const Sales_item&); 15 friend bool 16 operator==(const Sales_item&, const Sales_item&); 17 public: 18 // constructors are explained in section 7.1.4, pages 262 - 265 19 // default constructor needed to initialize members of built-in type 20 Sales_item() = default; 21 Sales_item(const std::string &book): bookNo(book) { } 22 Sales_item(std::istream &is) { is >> *this; } 23 public: 24 // operations on Sales_item objects 25 // member binary operator: left-hand operand bound to implicit this pointer 26 Sales_item& operator+=(const Sales_item&); 27 28 // operations on Sales_item objects 29 std::string isbn() const { return bookNo; } 30 double avg_price() const; 31 // private members as before 32 private: 33 std::string bookNo; // implicitly initialized to the empty string 34 unsigned units_sold; // explicitly initialized 35 double revenue; 36 }; 37 38 // used in chapter 10 39 inline 40 bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 41 { return lhs.isbn() == rhs.isbn(); } 42 43 // nonmember binary operator: must declare a parameter for each operand 44 Sales_item operator+(const Sales_item&, const Sales_item&); 45 46 inline bool 47 operator==(const Sales_item &lhs, const Sales_item &rhs) 48 { 49 // must be made a friend of Sales_item 50 return lhs.units_sold == rhs.units_sold && 51 lhs.revenue == rhs.revenue && 52 lhs.isbn() == rhs.isbn(); 53 } 54 55 inline bool 56 operator!=(const Sales_item &lhs, const Sales_item &rhs) 57 { 58 return !(lhs == rhs); // != defined in terms of operator== 59 } 60 61 // assumes that both objects refer to the same ISBN 62 Sales_item& Sales_item::operator+=(const Sales_item& rhs) 63 { 64 units_sold += rhs.units_sold; 65 revenue += rhs.revenue; 66 return *this; 67 } 68 69 // assumes that both objects refer to the same ISBN 70 Sales_item 71 operator+(const Sales_item& lhs, const Sales_item& rhs) 72 { 73 Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return 74 ret += rhs; // add in the contents of (|rhs|) 75 return ret; // return (|ret|) by value 76 } 77 78 std::istream& 79 operator>>(std::istream& in, Sales_item& s) 80 { 81 double price; 82 in >> s.bookNo >> s.units_sold >> price; 83 // check that the inputs succeeded 84 if (in) 85 s.revenue = s.units_sold * price; 86 else 87 s = Sales_item(); // input failed: reset object to default state 88 return in; 89 } 90 91 std::ostream& 92 operator<<(std::ostream& out, const Sales_item& s) 93 { 94 out << s.isbn() << " " << s.units_sold << " " 95 << s.revenue << " " << s.avg_price(); 96 return out; 97 } 98 99 double Sales_item::avg_price() const 100 { 101 if (units_sold) 102 return revenue/units_sold; 103 else 104 return 0; 105 } 106 #endif
代码:
1 #include <iostream> 2 #include "Sales_item.h" 3 4 using namespace std; 5 6 int main() 7 { 8 Sales_item book; 9 while (cin >> book) { 10 cout << book << endl; 11 } 12 return 0; 13 }
1.21
1 #include <iostream> 2 #include "Sales_item.h" 3 4 using namespace std; 5 6 int main() 7 { 8 Sales_item book1, book2; 9 while (cin >> book1 >> book2) { 10 if (book1.isbn() == book2.isbn()) 11 cout << book1 + book2 << endl; 12 else cout << "两本书的ISBN必须相同! "; 13 } 14 return 0; 15 }
1.22
1 #include <iostream> 2 #include "Sales_item.h" 3 4 using namespace std; 5 6 int main() 7 { 8 Sales_item total, book; 9 cin >> total; 10 while (cin >> book) { 11 if (total.isbn() != book.isbn()) 12 break; 13 total += book; 14 } 15 cout << total << endl; 16 return 0; 17 }
1.23&1.24
1 #include <iostream> 2 #include "Sales_item.h" 3 4 using namespace std; 5 6 int main() 7 { 8 Sales_item cur, book; 9 int cnt = 1; 10 cin >> cur; 11 while (cin >> book) { 12 if (book.isbn() == cur.isbn()) { 13 cnt++; 14 } 15 else { 16 cout << cur.isbn() << "有" << cnt << "条销售记录。 "; 17 cnt = 1; 18 cur = book; 19 } 20 } 21 return 0; 22 }
注:要保证同一个ISBN号的输入是相连的。