C++primer 7.1.4節練習

來源:互聯網
上載者:User

標籤:bsp   友元函數   als   sig   成員函數   isbn   函數   div   color   

練習7.11、練習7.13、練習7.14

 1 #include <iostream> 2 #include <string> 3 #include "factmain.h" 4 using namespace std; 5  6 struct Sales_data { 7     string  isbn() const { return bookNo; } 8     Sales_data &combine(const Sales_data&); 9     string bookNo;10     unsigned units_sold;11     double price = 0.0;12     double revenue = 0.0;13     Sales_data() = default;14     Sales_data(const string &s, const unsigned &u, const double &p) : bookNo(s), units_sold(u), price(p) {};15     Sales_data(const string &s, const unsigned &u) : bookNo(s), units_sold(u) {};16     Sales_data(istream &is);17 };18 19 Sales_data add(const Sales_data &lhs, const Sales_data &rhs);20 ostream &print(ostream &os, const Sales_data &item);21 istream &read(istream &is, Sales_data &item);22 23 24 int main()25 {26     Sales_data test1("hello",56);27     print(cout, test1);28     Sales_data item1(cin);29     Sales_data item2;30     double totalRevenue = 0;31     double totalSold = 0;32     int counter = 1;33     if ((item1.bookNo).empty())34     {35         item1.revenue = item1.price * item1.units_sold;36         while (read(cin,item2)) {37             item2.revenue = item2.price * item2.units_sold;38             if (item1.bookNo == item2.bookNo) {39                 item1.combine(item2);40                 ++counter;41             }42             else {43                 print(cout, item1) << endl;44                 item1.bookNo = item2.bookNo;45                 item1.units_sold = item2.units_sold;46                 item1.revenue = item2.revenue;47                 counter = 1;48             }49         }50         print(cout, item2) << endl;51     }52     system("pause");53     return 0;54 }55 56 Sales_data add(const Sales_data & lhs, const Sales_data & rhs) //非成員函數add57 {58     Sales_data sum = lhs;59     sum.combine(rhs);60     return sum;61     // TODO: 在此處插入 return 語句62 }63 64 istream & read(istream & is, Sales_data & item)65 {66     is >> item.bookNo >> item.units_sold >> item.price;67     item.revenue = item.units_sold * item.price;68     return is;69     // TODO: 在此處插入 return 語句70 }71 72 ostream & print(ostream & os, const Sales_data & item)73 {74     os << item.bookNo << " " << item.units_sold << " " << item.price << " " << item.revenue;75     return os;76     // TODO: 在此處插入 return 語句77 }78 79 Sales_data & Sales_data::combine(const Sales_data &rhs)80 {81     units_sold += rhs.units_sold;82     revenue += rhs.revenue;83     return *this;84 }85 86 Sales_data::Sales_data(istream & is)87 {88     read(is, *this);89 }

 

練習7.12

需要用到友元函數,待解決

練習7.15

見最後程式

 

C++primer 7.1.4節練習

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.