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

C++primer 7.1.4节练习

时间:2017-08-05 16:43:07      阅读:291      评论:0      收藏:0      [点我收藏+]

标签: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) //非成员函数add
57 {
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节练习

标签:bsp   友元函数   als   sig   成员函数   isbn   函数   div   color   

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

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