7.1 Sales_data类:
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
#endif
使用Sales_data实现交易处理程序:
#include "Sales_data.h"
using namespace std;
int main()
{
Sales_data total;
double price = 0;
if (cin >> total.bookNo >> total.units_sold >> price) {
total.revenue = total.units_sold * price;
Sales_data trans;
while (cin >> trans.bookNo >> trans.units_sold >> price) {
trans.revenue = trans.units_sold * price;
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.bookNo = trans.bookNo;
total.units_sold = trans.units_sold;
total.revenue = trans.revenue;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
} else {
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
7.2 添加combine和isbn成员:
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string isbn const { return bookNo; };
Sales_data& combine(const Sales_data&);
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 使用combine和isbn():
#include "Sales_data.h"
using namespace std;
int main()
{
Sales_data total;
double price = 0;
if (cin >> total.bookNo >> total.units_sold >> price) {
total.revenue = total.units_sold * price;
Sales_data trans;
while (cin >> trans.bookNo >> trans.units_sold >> price) {
trans.revenue = trans.units_sold * price;
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 {
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
7.4 见练习7.5
7.5
#ifndef PERSON_H
#define PERSON_H
class Person {
public:
string getName() const { return name; }
string getAddress() const { return address; }
private:
string name;
string address;
};
#endif
成员函数不改变调用它的对象的内容,应该定义为const的。
7.6 增加avg_prce成员,增加add,read和print函数:
double
Sales_data::avg_price() const {
if (units_sold)
return revenue/units_sold;
else
return 0;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
istream &read(istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = item.units_sold * price;
return is;
}
ostream &print(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
7.7 使用新增加的函数:
#include "Sales_data.h"
using namespace std;
int main()
{
Sales_data total;
double price = 0;
if (read(cin, total)) {
Sales_data trans;
while (read(cin, trans)) {
if (total.isbn() == trans.isbn()) {
total.combine(trans);
} else {
print(cout, total) << endl;
total = trans;
}
}
print(cout, total) << endl;
} else {
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
7.8 read函数需要修改对象,所以是普通引用,print函数不需要,为常量引用。
7.9
istream &read(istream &is, Person &item)
{
is >> name >> address;
return is;
}
ostream &print(ostream &os, const Person &item)
{
os << item.getName() << " " << item.getAddress();
return os;
}
7.10 连续读入data1,和data2,输入正确则满足条件,否则不满足条件。内层read(cin, data1)返回cin,可以作为外层read的第一个参数使用。
这篇博客介绍了C++ Primer第五版中7.1.1到7.1.3章节关于Sales_data类的实践,包括使用Sales_data处理交易、添加combine和isbn成员函数、利用这些函数进行操作。内容还涉及了const成员函数的使用、avg_price成员、add、read和print函数的增删以及read和print函数引用类型的考虑。
614

被折叠的 条评论
为什么被折叠?



