码迷,mamicode.com
首页 > 编程语言 > 详细

C++primer 7.5.2节练习

时间:2017-08-07 20:28:39      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:blog   one   函数   name   默认   end   一个   add   unit   

练习7.41

  1 #include <iostream>
  2 #include <string>
  3 #include "factmain.h"
  4 using namespace std;
  5 
  6 struct Sales_data {
  7     friend ostream &print(ostream &os, const Sales_data &item);
  8     friend istream &read(istream &is, Sales_data &item);
  9 private:
 10     string bookNo;
 11     unsigned units_sold;
 12     double price;
 13     double revenue;
 14     double avg_price() const;
 15 public:
 16     Sales_data(const string &s, const unsigned &u, const double &p) : bookNo(s), units_sold(u), price(p) { cout << "one" << endl; }
 17     Sales_data() : Sales_data("12345", 3, 12) { cout << "使用的是委托构造函数构造默认构造函数" << endl; }
 18     Sales_data(string s) : Sales_data(s, 2, 4) { cout << "仅仅输入一个字符串" << endl; }
 19     //Sales_data(const string &s, const unsigned &u) : bookNo(s), units_sold(u) {}
 20     Sales_data(istream &is) : Sales_data() { read(is, *this); cout << "最后一种" << endl; }
 21     string  isbn() const { return bookNo; }
 22     unsigned &rUnit() { return units_sold; }
 23     double &rPrice() { return price; }
 24     double &rRevenue() { return revenue; }
 25     Sales_data &combine(const Sales_data&);
 26 
 27 };
 28 
 29 Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
 30 ostream &print(ostream &os, const Sales_data &item);
 31 istream &read(istream &is, Sales_data &item);
 32 
 33 
 34 int main()                                                                            //main函数的部分需要改变
 35 {
 36     Sales_data s1;
 37     Sales_data s2("123", 2, 3);
 38     Sales_data s3("12");
 39     Sales_data s4(cin);
 40     /*Sales_data test1("hello",56);
 41     print(cout, test1);
 42     Sales_data item1(cin);
 43     Sales_data item2;
 44     double totalRevenue = 0;
 45     double totalSold = 0;
 46     int counter = 1;
 47     if ((item1.isbn()).empty())
 48     {
 49         item1.rRevenue() = item1.rPrice() * item1.rUnit();
 50         while (read(cin,item2)) {
 51             item2.rRevenue() = item2.rPrice() * item2.rUnit();
 52             if (item1.isbn() == item2.isbn()) {
 53                 item1.combine(item2);
 54                 ++counter;
 55             }
 56             else {
 57                 print(cout, item1) << endl;
 58                 item1.isbn() = item2.isbn();
 59                 item1.rUnit() = item2.rUnit();
 60                 item1.rRevenue() = item2.rRevenue();
 61                 counter = 1;
 62             }
 63         }
 64         print(cout, item2) << endl;
 65     }*/
 66     system("pause");
 67     return 0;
 68 }
 69 
 70 Sales_data add(const Sales_data & lhs, const Sales_data & rhs) //非成员函数add
 71 {
 72     Sales_data sum = lhs;
 73     sum.combine(rhs);
 74     return sum;
 75     // TODO: 在此处插入 return 语句
 76 }
 77 
 78 istream & read(istream & is, Sales_data & item)
 79 {
 80     is >> item.bookNo >> item.units_sold >> item.price;
 81     item.revenue = item.units_sold * item.price;
 82     return is;
 83     // TODO: 在此处插入 return 语句
 84 }
 85 
 86 ostream & print(ostream & os, const Sales_data & item)
 87 {
 88     os << item.bookNo << " " << item.units_sold << " " << item.price << " " << item.revenue;
 89     return os;
 90     // TODO: 在此处插入 return 语句
 91 }
 92 
 93 Sales_data & Sales_data::combine(const Sales_data &rhs)
 94 {
 95     units_sold += rhs.units_sold;
 96     revenue += rhs.revenue;
 97     return *this;
 98 }
 99 
100 inline double Sales_data::avg_price() const
101 {
102     return units_sold ? revenue/units_sold : 0;
103 }

练习7.42

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Date {
 6 public:
 7     Date(unsigned y, unsigned m, unsigned d, const string &s) : years(y), mon(m), days(d), weather(s) {};
 8     Date() : Date(2000, 1, 1, "good") {}
 9     Date(string &s) : Date(1999, 9, 9, s) {}
10 
11 private:
12     unsigned years;
13     unsigned mon;
14     unsigned days;
15     string weather;
16 };

 

C++primer 7.5.2节练习

标签:blog   one   函数   name   默认   end   一个   add   unit   

原文地址:http://www.cnblogs.com/wuyinfenghappy/p/7300956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!