C++ Primer(第五版) 第七章练习答案
目录
- C++ Primer(第五版) 第七章练习答案
-
-
- 7.1
- 7.2
- 7.3
- 7.4
- 7.5
- 7.6
- 7.7
- 7.8
- 7.9
- 7.10
- 7.11
- 7.12
- 7.13
- 7.14
- 7.15
- 7.16
- 7.17
- 7.18
- 7.19
- 7.20
- 7.21
- 7.22
- 7.23
- 7.24
- 7.25
- 7.26
- 7.27
- 7.28
- 7.29
- 7.30
- 7.31
- 7.32
- 7.33
- 7.34
- 7.35
- 7.36
- 7.37
- 7.38
- 7.39
- 7.40.cc
- 7.40.h
- 7.41.cc
- 7.41.h
- 7.42
- 7.43
- 7.44
- 7.45
- 7.46
- 7.47
- 7.48
- 7.49
- 7.50
- 7.51
- 7.52
- 7.53
- 7.54
- 7.55
- 7.56
- 7.57.cc
- 7.57.h
- 7.58
7.1
#include
#include
#include "../2/2_40.h"
using namespace std;
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.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
#include
struct Sales_data {
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
7.3
#include
#include
#include "7_2.h"
using namespace std;
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.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.4
#include
struct Person {
std::string name;
std::string address;
};
7.5
#include
struct Person {
std::string getname() const {
return name; }
std::string getaddress() const {
return address; }
std::string name;
std::string address;
};
7.6
#include
#include
struct Sales_data {
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
std::istream &read(std::istream &is, Sales_data &item)
{
is >> item.bookNo
>> item.units_sold
>> item.revenue;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.bookNo << " "
<< item.units_sold << " "
<< item.revenue;
return os;
}
7.7
#include
#include
#include "7_6.h"
using namespace std;
int main()
{
Sales_data total;
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
7.9
#include
#include
struct Person {
std::string getname() const {
return name; }
std::string getaddress() const {
return address; }
std::string name;
std::string address;
};
inline
std::istream &read(std::istream &is, Person &item)
{
is >> item.name >> item.address;
return is;
}
inline
std::ostream &print(std::ostream &os, const Person &item)
{
os << item.name << " "
<< item.address;
return os;
}
7.10
7.11
#include
#include
struct Sales_data {
Sales_data() = default;
Sales_data(std::istream &is) {
read(is, *this); }
Sales_data(const std::string &s) : bookNo(s) {
}
Sales_data(const std::string &s, unsigned u, double d) : bookNo(s), units_sold(u), revenue(d) {
}
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
inline
std::istream &read(std::istream &is, Sales_data &item)
{
is >> item.bookNo
>> item.units_sold
>> item.revenue;
return is;
}
inline
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.bookNo << " "
<< item.units_sold << " "
<< item.revenue;
return os;
}
7.12
#include
#include
struct Sales_data;
Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
std::istream &read(std::istream &is, Sales_data &item);
std::ostream &print(std::ostream &os, const Sales_data &item);
struct Sales_data {
Sales_data() = default;
Sales_data(std::istream &is) {
read(is, *this); }
Sales_data(const std::string &s) : bookNo(s) {
}
Sales_data(const std::string &s, unsigned u, double d) : bookNo(s), units_sold(u), revenue(d) {
}
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
inline
std::istream &read(std::istream &is, Sales_data &item)
{
is >> item.bookNo
>> item.units_sold
>> item.revenue;
return is;
}
inline
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.bookNo << " "
<< item.units_sold << " "
<< item.revenue;
return os;
}
7.13
#include
#include
#include "7_12.h"
using namespace std;
int main()
{
Sales_data total(cin);
if (cin)
{
do
{
Sales_data trans(cin);
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(cout, total) << endl;
total = trans;
}
} while(cin);
}
else
{
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
7.14
Sales_data() : bookNo(std::string()), units_sold(0), revenue(0.0) {
}
7.15
#include
#include
struct Person;
std::istream &read(std::istream &is, Person &item);
std::ostream &print(std::ostream &os, const Person &item);
struct Person {
Person() = default;
Person(std::istream &is) {
read(is, *this); }
Person(const std::string &s) : name(s) {
}
Person(const std::string &n, const std::string &a) : name(n), address(a) {
}
std::string getname() const {
return name; }
std::string getaddress() const {
return address; }
std::string name;
std::string address;
};
inline
std::istream &read(std::istream &is, Person &item)
{
is >> item.name >> item.address;
return is;
}
inline
std::ostream &print(std::ostream &os, const Person &item)
{
os << item.name << " "
<< item.address;
return os;
}
7.16
7.17
7.18
7.19
#include
#include
class Person;
std::istream &read(std::istream &is, Person &item);
std::ostream &print(std::ostream &os, const Person &item);
class Person {
friend std::istream &read(std::istream &is, Person &item);
friend std::ostream &print(std::ostream &os, const Person &item);
public:
Person() = default;
Person(std::istream &is) {
read(is, *this); }
Person(const std::string &s) : name(s) {
}
Person(const std::string &n, const std::string &a) : name(n), address(a) {
}
std::string getname() const {
return name; }
std::string getaddress() const {
return address; }
private:
std::string name;
std::string address;
};
inline
std::istream &read(std::istream &is, Person &item)
{
is >> item.name >> item.address;
return is;
}
inline
std::ostream &print(std::ostream &os, const Person &item)
{
os << item.name << " "
<< item.address;
return os;
}
7.20
7.21
#include
#include
class Sales_data {
friend std::istream &read(std::istream &is, Sales_data &item);
friend std::ostream &print(std::ostream &os, const Sales_data &item);
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
public:
Sales_data() = default;
Sales_data(std::istream &is) {
read(is, *this); }
Sales_data(const std::string &s) : bookNo(s) {
}
Sales_data(const std::string &s, unsigned u, double d) : bookNo(s), units_sold(u), revenue(d) {
}
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
private:
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
std::istream &read(std::istream &is, Sales_data &item);
std::ostream &print(std::ostream &os, const Sales_data &item);
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
7.22
#include
#include
class Person {
friend std::istream &read(std::istream &is, Person &item);
friend std::ostream &print(std::ostream &os, const Person &item);
public:
Person() = default;
Person(std::istream &is) {
read(is, *this); }
Person(const std::string &s) : name(s) {
}
Person(const std::string &n, const std::string &a) : name(n), address(a) {
}
std::string getname() const {
return name; }
std::string getaddress() const {
return address; }
private:
std::string name;
std::string address;
};
std::istream &read(std::istream &is, Person &item);
std::ostream &print(std::ostream &os, const Person &item);
7.23
#include
class Screen {
public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht * wd, ' ') {
}
Screen(pos ht, pos wd, char ch) : height(ht), width(wd), contents(ht * wd, ch) {
}
char get() const {
return contents[cursor]; }
char get(pos ht, pos wd) const {
return contents[ht * width + wd]; }
Screen &move(pos ht, pos wd) {
cursor = ht * width + wd; return *this; }
private:
pos cursor = 0;
pos height = 0;
pos width = 0;
std::string contents;
};
7.24
#include
class Screen {
public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht * wd, ' ') {
}
Screen(pos ht, pos wd, char ch) : height(ht), width(wd), contents(ht * wd, ch) {
}
char get() const {
return contents[cursor]; }
char get(pos ht, pos wd) const {
return contents[ht * width + wd]; }
Screen &move(pos ht, pos wd) {
cursor = ht * width + wd; return *this; }
private:
pos cursor = 0;
pos height = 0;
pos width = 0;
std::string contents;
};
7.25
7.26
#include
#include
class Sales_data {
friend std::istream &read(std::istream &is, Sales_data &item);
friend std::ostream &print(std::ostream &os, const Sales_data &item);
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
public:
Sales_data() = default;
Sales_data(std::istream &is) {
read(is, *this); }
Sales_data(const std::string &s) : bookNo(s) {
}
Sales_data(const std::string &s, unsigned u, double d) : bookNo(s), units_sold(u), revenue(d) {
}
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
double avg_price() const {
return units_sold ? revenue / units_sold : 0; }
private:
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
std::istream &read(std::istream &is, Sales_data &item);
std::ostream &print(std::ostream &os, const Sales_data &item);
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
7.27
#include
#include
class Screen {
public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht * wd, ' ') {
}
Screen(pos ht, pos wd, char ch) : height(ht), width(wd), contents(ht * wd, ch) {
}
char get() const {
return contents[cursor]; }
char get(pos ht, pos wd) const {
return contents[ht * width + wd]; }
Screen &move(pos ht, pos wd) {
cursor = ht * width + wd; return *this; }
Screen &set(char);
Screen &set(pos, pos, char);
Screen &display(std::ostream &os) {
do_display(os); return *this; }
const Screen &display(std::ostream &os) const {
do_display(os); return *this; }
private:
void do_display(std::ostream &os) const;
pos cursor = 0;
pos height = 0;
pos width = 0;
std::string contents;
};
inline
Screen &Screen::set(char ch)
{
contents[cursor] = ch;
return *this;
}
inline
Screen &Screen::set(pos ht, pos wd, char ch)
{
contents[ht * width + wd] = ch;
return *this;
}
inline
void Screen::do_display(std::ostream &os) const
{
for (pos i = 0; i != contents.size(); ++i)
{
os << contents[i];
if (i % width == width - 1)
os << std::endl;
}
}
7.28
7.29
#include
#include
class Screen {
public:
typedef std::string::size_type pos;
Screen() = default;
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht * wd, ' ') {
}
Screen(pos ht, pos wd, char ch) : height(ht), width(wd), contents(ht * wd, ch) {
}
char get() const {
return contents[cursor]; }
char get(pos ht, pos wd) const {
return contents[ht * width + wd]; }
Screen move(pos ht, pos wd) {
cursor = ht * width + wd; return *this; }
Screen set(char);
Screen set(pos, pos, char);
Screen display(std::ostream &os) {
do_display(os); return *this; }
const Screen display(std::ostream &os) const {
do_display(os); return *this; }
private:
void do_display(std::ostream &os) const;
pos cursor = 0;
pos height = 0;
pos width = 0;
std::string contents;
};
inline
Screen Screen::set(char ch)
{
contents[cursor] = ch;
return *this;
}
inline
Screen Screen::set(pos ht, pos wd, char ch)
{
contents[ht * width + wd] = ch;
return *this;
}
inline
void Screen::do_display(std::ostream &os) const
{
for (pos i = 0; i != contents.size(); ++i)
{
os << contents[i];
if (i % width == width - 1)
os << std::endl;
}
}
using namespace std;
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4, 0).set('#').display(cout);
myScreen.display(cout);
return 0;
}
7.30
7.31
struct X;
struct Y;
struct X {
Y *p;
};
struct Y {
X p;
};
7.32
#include
#include
#include "Screen.h"
class Window_mgr {
public:
using ScreenIndex = std::vector<Screen>::size_type;
void clear(ScreenIndex);
private:
std::vector<Screen> screens{
Screen(24, 80, ' ')};
};
inline
void Window_mgr::clear(ScreenIndex i)
{
Screen &s = screens[i];
s.contents = std::string(s.height * s.width, ' ');
}
7.33
7.34
7.35
#include
using std::string;
typedef string Type;
Type initVal();
class Exercise {
public:
typedef double Type;
Type setVal(Type);
Type initVal();
private:
int val;
};
Type Exercise::setVal(Type parm)
{
val = parm + initVal();
return val;
}
7.36
struct X {
X(int i, int j) : base(i), rem(base % j);
int base, rem;
};
7.37
#include "../Sales_data/Sales_data.h"
#include
using namespace std;
Sales_data first_item(cin);
int main()
{
Sales_data next;
Sales_data last("11111");
return 0;
}
7.38
Sales_data(std::istream is& = std::cin);
7.39
7.40.cc
#include
#include
#include "Date.h"
#define STR(i) (std::to_string(i))
Date::Date() : tim(time(&tim)), t(localtime(&tim)), year(STR(t->tm_year + 1900)),
month(STR(t->tm_mon + 1)), day(STR(t->tm_mday)), hour(STR(t->tm_hour)),
minute(STR(t->tm_min)), second(STR(t->tm_sec)) {
}
#undef STR
7.40.h
#ifndef DATE_H
#define DATE_H
#include
#include
#include
#include
class Date{
friend std::ostream &operator<<(std::ostream&, const Date&);
public:
Date();
private:
time_t tim;
struct tm *t;
std::string year;
std::string month;
std::string day;
std::string hour;
std::string minute;
std::string second;
};
#define SET std::setw(2) << std::setfill('0')
inline
std::ostream &operator<<(std::ostream &os, const Date &item)
{
os << item.year << "/" << SET << item.month << "/"
<< SET << item.day << " -- " << SET << item.hour << ":"
<< SET << item.minute << ":" << SET << item.second;
return os;
}
#undef SET
#endif
7.41.cc
#include "7_41.h"
#include
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
std::istream &read(std::istream &is, Sales_data &item)
{
is >> item.bookNo
>> item.units_sold
>> item.revenue;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.bookNo << " "
<< item.units_sold << " "
<< item.revenue;
return os;
}
int main()
{
Sales_data data1;
Sales_data data2(std::cin);
Sales_data data3("xi");
Sales_data data4("xi", 0, 0);
}
7.41.h
#ifndef SALES_DATA
#define SALES_DATA
#include
#include
class Sales_data {
friend std::istream &read(std::istream &is, Sales_data &item);
friend std::ostream &print(std::ostream &os, const Sales_data &item);
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
public:
Sales_data() : Sales_data("", 0, 0) {
puts("Sales_data()"); }
Sales_data(std::istream &is) : Sales_data() {
puts("Sales_data(std::istream&"); read(is, *this); }
Sales_data(const std::string &s) : Sales_data(s, 0, 0) {
puts("Sales_data(const std::string&)"); }
Sales_data(const std::string &s, unsigned u, double d) : bookNo(s), units_sold(u), revenue(d)
{
puts("Sales_data(const std::string&, unsigned, double)"); }
std::string isbn() const {
return bookNo; }
Sales_data& combine(const Sales_data &rhs);
double avg_price() const {
return units_sold ? revenue / units_sold : 0; }
private:
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
std::istream &read(std::istream &is, Sales_data &item);
std::ostream &print(std::ostream &os, const Sales_data &item);
inline
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
#endif
7.42
Date::Date(std::string &ye) : Date() {
year = ye; }
7.43
#include
#include
struct NoDefuault {
NoDefuault(int i) : i(i) {
}
int i = 0;
};
struct C {
C() : nd(1) {
}
NoDefuault nd;
};
int main()
{
C c;
printf("%d\n", c.nd.i);
std::vector<NoDefuault> d(10, 0);
std::vector<C> e(10);
return 0;
}
7.44
7.45
7.46
7.47
7.48
7.49
7.50
#include
#include
class Person {
friend std::istream &read(std::istream &is, Person &item);
friend std::ostream &print(std::ostream &os, const Person &item);
public:
Person() = default;
explicit Person(std::istream &is) {
read(is, *this); }
explicit Person(const std::string &s) : name(s) {
}
Person(const std::string &n, const std::string &a) : name(n), address(a) {
}
std::string getname() const {
return name; }
std::string getaddress() const {
return address; }
private:
std::string name;
std::string address;
};
std::istream &read(std::istream &is, Person &item);
std::ostream &print(std::ostream &os, const Person &item);
int main()
{
Person p("xiao");
return 0;
}
7.51
7.52
#include
#include
using namespace std;
struct Sales_data {
std::string bookNo;
unsigned units_sold = 10;
double revenue = 20;
};
int main()
{
Sales_data item = {
"xi", 1, 2};
cout << item.bookNo << item.units_sold << item.revenue << endl;
return 0;
}
7.53
class Debug {
public:
constexpr Debug(bool b = true) : hw(b), io(b), other(b) {
}
constexpr Debug(bool h, bool i, bool o) : hw(h), io(i), other(o) {
}
constexpr bool any() {
return hw || io || other; }
void set_io(bool b) {
io = b; }
void set_hw(bool b) {
hw = b; }
void set_other(bool b) {
other = b; }
private:
bool hw;
bool io;
bool other;
};
int main()
{
Debug d;
d.set_io(false);
}
7.54
7.55
7.56
7.57.cc
#include "Account.h"
void Account::rate(double newRate)
{
interestRate = newRate;
}
double Account::initRate()
{
return 1.0;
}
double Account::interestRate = initRate();
7.57.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include
class Account {
public:
Account() = default;
Account(std::string s, double d) : owner(s), amount(d) {
}
double calculate() {
return amount += amount * interestRate;}
static double rate() {
return interestRate; }
static void rate(double);
private:
std::string owner;
double amount = 0;
static double interestRate;
static double initRate();
};
#endif
7.58