C++ Primer 0x07 练习题解
Posted 鱼竿钓鱼干
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ Primer 0x07 练习题解相关的知识,希望对你有一定的参考价值。
📔 C++ Primer 0x07 练习题解
7.1 定义抽象数据类型
7.1.1 定义抽象数据类型
7.1 使用2.6.1节定义的
Sales_data
类为 1.6节 的交易处理程序编写一个新版本。
#include <iostream>
#include <string>
struct Sales_data
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
;
int main()
Sales_data total,item;
double price = 0;
if (std::cin >> total.bookNo >> total.units_sold >> price)
total.revenue = total.units_sold * price;
while (std::cin >> item.bookNo >> item.units_sold >> price)
item.revenue = item.units_sold * price;
if (total.bookNo == item.bookNo)
total.units_sold += item.units_sold;
total.revenue += item.revenue;
else
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
total = item;
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
else
std::cerr << "No data?!" << std::endl;
return -1;
return 0;
7.1.2 定义改进的 Salses_data 类
7.2 曾在2.6.2节的练习中编写了一个
Sales_data
类,请向这个类添加combine
函数和isbn
成员。7.3 修改7.1.1节的交易处理程序,令其使用这些成员。
#include <iostream>
#include <string>
struct Sales_data
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data &rhs);
std::string isbn()return bookNo;
;
Sales_data& Sales_data::combine(const Sales_data &rhs)
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
int main()
Sales_data total,item;
double price = 0;
if (std::cin >> total.bookNo >> total.units_sold >> price)
total.revenue = total.units_sold * price;
while (std::cin >> item.bookNo >> item.units_sold >> price)
item.revenue = item.units_sold * price;
if (total.isbn() == item.isbn())
total.combine(item);
else
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
total = item;
std::cout << total.bookNo << " "
<< total.units_sold << " "
<< total.revenue << std::endl;
else
std::cerr << "No data?!" << std::endl;
return -1;
return 0;
7.4 编写一个名为
Person
的类,使其表示人员的姓名和地址。使用string
对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
struct Person
std::string name;
std::string address;
;
7.5 在你的
Person
类中提供一些操作使其能够返回姓名和地址。 这些函数是否应该是const
的呢?解释原因。
struct Person
std::string name;
std::string address;
std::string get_name()constreturn name;
std::string get_address()constreturn address;
;
应该加,考虑常量对象可能也要使用这些操作
7.1.3 定义类相关的非成员函数
7.6 对于函数
add
、read
和7.7 使用这些新函数重写7.1.2节练习中的程序。
#include <iostream>
#include <string>
struct Sales_data
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data &rhs);
std::string isbn()constreturn bookNo;
;
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 = item.units_sold * price;
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;
int main()
Sales_data total,item;
if (read(std::cin,total))
while (read(std::cin,item))
if (total.isbn() == item.isbn())
total.combine(item);
else
print(std::cout,total);
std::cout << std::endl;
total = item;
print(std::cout,total);
std::cout << std::endl;
else
std::cerr << "No data?!" << std::endl;
return -1;
return 0;
7.8 为什么
read
函数将其Sales_data
参数定义成普通的引用,而
read
会改变Sales_data item
的内容,print
不会
7.9 对于7.1.2节练习中代码,添加读取和打印
Person
对象的操作。
#include <iostream>
#include <string>
struct Person
std::string name;
std::string address;
std::string get_name()constreturn name;
std::string get_address()constreturn address;
;
std::istream& read(std::istream &is,Person &p)
is >> p.name >> p.address;
return is;
std::ostream& print(std::ostream &os,const Person &p)
os << p.get_name() << " " << p.get_address();
return os;
int main()
Person p;
read(std::cin,p);
print(std::cout,p);
return 0;
7.10 在下面这条
if
语句中,条件部分的作用是什么?
if (read(read(cin, data1), data2)) //连续读入 data1 data2 的数据
7.1.4 构造函数
7.11 在你的
Sales_data
类中添加构造函数, 然后编写一段程序令其用到每个构造函数。
#include <iostream>
#include <string>
struct Sales_data
Sales_data() = default;
Sales_data(const std::string &s):bookNo(s)
Sales_data(const std::string &s,unsigned u,double p)
:bookNo(s),units_sold(u),revenue(u*p)
Sales_data(std::istream &);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data &rhs);
std::string isbn()constreturn bookNo;
double avg_price()const;
;
double Sales_data::avg_price()const
return units_sold?(revenue/units_sold):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 = item.units_sold * price;
return is;
std::ostream & print(std::ostream &os,const Sales_data &item)
os << item.isbn() << " "<< item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
Sales_data::Sales_data(std::istream &is)
read(is,*this);
Sales_data add(const Sales_data &lhs,const Sales_data &rhs)
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
int main()
Sales_data A;
print(std::cout,A) << std::endl;
Sales_data B("bookB");
print(std::cout,B) << std::endl;
Sales_data C("bookC",12,3.0);
print(std::cout,C) << std::endl;
Sales_data D(std::cin);
print(std::cout,D) << std::endl;
return 0;
7.12 把只接受一个
istream
作为参数的构造函数移到类的内部。
#include <iostream>
#include <string>
struct Sales_data;
std::istream &read(std::istream&,Sales_data&);
struct Sales_data
Sales_data() = default;
Sales_data(const std::string &s):bookNo(s)
Sales_data(const std::string &s,unsigned u,double p)
:bookNo(s),units_sold(u),revenue(u*p)
Sales_data(std::istream &is)read(is,*this);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data &rhs);
std::string isbn()constreturn bookNo;
double avg_price()const;
;
double Sales_data::avg_price()const
return units_sold?(revenue/units_sold):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 = item.units_sold * price;
return is;
std::ostream & print(std::ostream &os,const Sales_data &item)
os << item.isbn() << " "<< item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
Sales_data add(const Sales_data &lhs,const Sales_data &rhs)
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
int main()
Sales_data A;
print(std::cout,A) << std::endl;
Sales_data B("bookB");
print(std::cout,B) << std::endl;
Sales_data C("bookC",12,3.0);
print(std::cout,C) << std::endl;
Sales_data D(std::cin);
print(std::cout,D) << std::endl;
return 0;
7.13 使用
istream
构造函数重写第229页的程序。
#include <iostream>
#include <string>
struct Sales_data;
std::istream &read(std::istream&,Sales_data&);
struct Sales_data
Sales_data() = default;
Sales_data(const std::string &s):bookNo(s)
Sales_data(const std::string &s,unsigned u,double p)
:bookNo(s),units_sold(u),revenue(u*p)
Sales_data(std::istream &is)read(is,*this);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
Sales_data& combine(const Sales_data &rhs);
std::string isbn()constreturn bookNo;
double avg_price()const;
;
double Sales_data::avg_price()const
return units_sold?(revenue/units_sold):0;
Sales_data& Sales_data::combine(const Sales_data &rhs)
units_sold += rhsC++ Primer 0x0B 练习题解