坪山区住房和建设局网站seo建站还有市场吗
文章目录
- 练习7.1
- 练习7.2
- 练习7.3
- 练习7.4
- 练习7.5
- 练习7.6
- 练习7.7
- 练习7.8
- 练习7.9
- 练习7.10
练习7.1
使用2.6.1节定义的Sales_data类为1.6节的交易处理程序编写一个新版本。
#include <iostream>
#include <string>
using std::cin; using std::cout; using std::endl; using std::string;struct Sales_data
{string bookNo;unsigned units_sold = 0;double revenue = 0.0;
};int main()
{Sales_data total;if (cin >> total.bookNo >> total.units_sold >> total.revenue){Sales_data trans;while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue){if (total.bookNo == trans.bookNo){total.units_sold += trans.units_sold;total.revenue += trans.revenue;}else{cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;total = trans;}}cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;}else{std::cerr << "No data?!" << std::endl;return -1;}return 0;
}
练习7.2
曾在2.6.2节的练习中编写了一个Sales_data类,请向这个类添加combine函数和isbn成员。
#ifndef CP5_ex7_02_h
#define CP5_ex7_02_h#include <string>struct Sales_data
{std::string isbn() const { return bookNo; };Sales_data& combine(const Sales_data&);std::string bookNo;unsigned units_sold = 0;double revenue = 0.0;
};Sales_data& Sales_data::combine(const Sales_data& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}#endif
练习7.3
修改7.1.1节的交易处理程序,令其使用这些成员。
#include "exercise7_2.h"
#include <iostream>
using std::cin; using std::cout; using std::endl;int main()
{Sales_data total;if (cin >> total.bookNo >> total.units_sold >> total.revenue){Sales_data trans;while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue){if (total.isbn() == trans.isbn())total.combine(trans);else{cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;total = trans;}}cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;}else{std::cerr << "No data?!" << std::endl;return -1;}return 0;
}
练习7.4
编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
#ifndef CP5_ex7_04_h
#define CP5_ex7_04_h#include <string>class Person
{std::string name;std::string address;
};#endif
练习7.5
在你的Person类中提供一些操作使其能够返回姓名和地址。这些函数是否应该是const的呢?解释原因。
#ifndef CP5_ex7_05_h
#define CP5_ex7_05_h#include <string>class Person
{std::string name;std::string address;
public:auto get_name() const -> std::string const& { return name; }auto get_addr() const -> std::string const& { return address; }
};#endif
应该是const的。因为常量的Person对象也需要使用这些函数操作。
练习7.6
对于函数add、read和print,定义你自己的版本。
#ifndef CP5_ex7_06_h
#define CP5_ex7_06_h#include <string>
#include <iostream>struct Sales_data
{std::string const& isbn() const { return bookNo; };Sales_data& combine(const Sales_data&);std::string bookNo;unsigned units_sold = 0;double revenue = 0.0;
};Sales_data& Sales_data::combine(const Sales_data& rhs)
{units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;
}std::istream &read(std::istream &is, Sales_data &item)
{double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = price * item.units_sold;return is;
}std::ostream &print(std::ostream &os, const Sales_data &item)
{os << item.isbn() << " " << item.units_sold << " " << item.revenue;return os;
}Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{Sales_data sum = lhs;sum.combine(rhs);return sum;
}#endif
练习7.7
使用这些新函数重写7.1.2节练习中的程序。
#include "exercise7_6.h"int main()
{Sales_data total;if (read(std::cin, total)){Sales_data trans;while (read(std::cin, trans)){if (total.isbn() == trans.isbn())total.combine(trans);else{print(std::cout, total) << std::endl;total = trans;}}print(std::cout, total) << std::endl;}else{std::cerr << "No data?!" << std::endl;return -1;}return 0;
}
Footer
练习7.8
为什么read函数将其Sales_data参数定义成普通的引用,而print函数将其参数定义成常量引用?
因为read函数会改变对象的内容,而print函数不会。
练习7.9
对于7.1.2节练习中代码,添加读取和打印Person对象的操作。
#ifndef CP5_ex7_09_h
#define CP5_ex7_09_h#include <string>
#include <iostream>struct Person
{std::string const& getName() const { return name; }std::string const& getAddress() const { return address; }std::string name;std::string address;
};std::istream &read(std::istream &is, Person &person)
{return is >> person.name >> person.address;
}std::ostream &print(std::ostream &os, const Person &person)
{return os << person.name << " " << person.address;
}#endif
练习7.10
在下面这条if语句中,条件部分的作用是什么?
if (read(read(cin, data1), data2))
read 函数的返回值是 istream 对象,if语句中条件部分的作用是从输入流中读取数据给两个data对象。