首页 > 代码库 > C++primer 7.3.1节练习

C++primer 7.3.1节练习

练习7.23

头文件

 1 #pragma once
 2 class Screen {
 3     typedef std::string::size_type pos;
 4 public:
 5     Screen() = default;
 6     Screen(pos h, pos w) : height(h), width(w), contents(h * w,  ) {};
 7     Screen(pos h, pos w, char c) : height(h), width(w), contents(h * w, c) {};
 8     Screen(std::string &s);
 9     Screen &move(pos r, pos c);
10     char getChar(pos x, pos y) const;
11     char getChar()const {
12         return contents[(x_axis - 1)*y_axis + x_axis];
13     }
14 
15 private:
16     std::string contents;
17     pos height = 0;
18     pos width = 0;
19     pos x_axis = 0;
20     pos y_axis = 0;
21 };

源文件

 1 #include <iostream>
 2 #include <string>
 3 #include "screen.h"
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     Screen s1(3, 4, c);
10     system("pause");
11     return 0;
12 }
13 
14 Screen::Screen(string &s)
15 {
16     (*this).contents = s;
17 }
18 
19 inline Screen & Screen::move(pos r, pos c)
20 {
21     x_axis = r;
22     y_axis = c;
23     return *this;
24     // TODO: 在此处插入 return 语句
25 }
26 
27 inline  char Screen::getChar(pos x, pos y) const
28 {
29     return contents[(x_axis - 1) * y_axis + x_axis];
30     // TODO: 在此处插入 return 语句
31 }

练习7.24

见上面

练习7.25

不能,string类型的变量不能安全的依赖于拷贝和赋值操作的默认版本;

练习7.26

  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 = 0.0;
 13     double revenue = 0.0;
 14     double avg_price() const;
 15 public:
 16     Sales_data() = default;
 17     Sales_data(const string &s, const unsigned &u, const double &p) : bookNo(s), units_sold(u), price(p) {};
 18     Sales_data(const string &s, const unsigned &u) : bookNo(s), units_sold(u) {};
 19     Sales_data(istream &is);
 20     string  isbn() const { return bookNo; }
 21     unsigned &rUnit() { return units_sold; }
 22     double &rPrice() { return price; }
 23     double &rRevenue() { return revenue; }
 24     Sales_data &combine(const Sales_data&);
 25 
 26 };
 27 
 28 Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
 29 ostream &print(ostream &os, const Sales_data &item);
 30 istream &read(istream &is, Sales_data &item);
 31 
 32 
 33 int main()                                                                            //main函数的部分需要改变
 34 {
 35     Sales_data test1("hello",56);
 36     print(cout, test1);
 37     Sales_data item1(cin);
 38     Sales_data item2;
 39     double totalRevenue = 0;
 40     double totalSold = 0;
 41     int counter = 1;
 42     if ((item1.isbn()).empty())
 43     {
 44         item1.rRevenue() = item1.rPrice() * item1.rUnit();
 45         while (read(cin,item2)) {
 46             item2.rRevenue() = item2.rPrice() * item2.rUnit();
 47             if (item1.isbn() == item2.isbn()) {
 48                 item1.combine(item2);
 49                 ++counter;
 50             }
 51             else {
 52                 print(cout, item1) << endl;
 53                 item1.isbn() = item2.isbn();
 54                 item1.rUnit() = item2.rUnit();
 55                 item1.rRevenue() = item2.rRevenue();
 56                 counter = 1;
 57             }
 58         }
 59         print(cout, item2) << endl;
 60     }
 61     system("pause");
 62     return 0;
 63 }
 64 
 65 Sales_data add(const Sales_data & lhs, const Sales_data & rhs) //非成员函数add
 66 {
 67     Sales_data sum = lhs;
 68     sum.combine(rhs);
 69     return sum;
 70     // TODO: 在此处插入 return 语句
 71 }
 72 
 73 istream & read(istream & is, Sales_data & item)
 74 {
 75     is >> item.bookNo >> item.units_sold >> item.price;
 76     item.revenue = item.units_sold * item.price;
 77     return is;
 78     // TODO: 在此处插入 return 语句
 79 }
 80 
 81 ostream & print(ostream & os, const Sales_data & item)
 82 {
 83     os << item.bookNo << " " << item.units_sold << " " << item.price << " " << item.revenue;
 84     return os;
 85     // TODO: 在此处插入 return 语句
 86 }
 87 
 88 Sales_data & Sales_data::combine(const Sales_data &rhs)
 89 {
 90     units_sold += rhs.units_sold;
 91     revenue += rhs.revenue;
 92     return *this;
 93 }
 94 
 95 inline double Sales_data::avg_price() const
 96 {
 97     return units_sold ? revenue/units_sold : 0;
 98 }
 99 
100 Sales_data::Sales_data(istream & is)
101 {
102     read(is, *this);
103 }

 

C++primer 7.3.1节练习