【C++ Primer(5th Edition) Exercise】练习程序 - Chapter7(第七章)(1-32)

以下程序由 Teddy van Jerry (我自己)编写并运行,基本保证正确性。(有时可能会为优化程序超前使用某些内容)

Before we comb through the codes

(I’m working on this chapter)
Review:
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter1(第一章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter2(第二章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter3(第三章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter4(第四章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter5(第五章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter6(第六章)


TIP:标有(C++/11)者为C++/11标准下可以使用的题,若为老版本应加以修改。


Exercise 7.1

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

int main()
{
    
    
	Sales_data total;
	double price;
	if (cin >> total.bookNo >> total.units_sold >> price)
	{
    
    
		total.revenue = price * total.units_sold;
		Sales_data trans;
		while (cin >> trans.bookNo >> trans.units_sold >> price)
		{
    
    
			if (total.bookNo == trans.bookNo)
			{
    
    
				total.revenue += price * trans.units_sold;
				total.units_sold += total.units_sold;
			}
			else
			{
    
    
				cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
				total = trans;
				total.revenue = price * total.units_sold;
			}
		}
		cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

Exercise 7.2

见 Exercise 7.3

Exercise 7.3

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
	string isbn() const
	{
    
    
		return bookNo;
	}
	Sales_data& combine(const Sales_data& rhs)
	{
    
    
		units_sold += rhs.units_sold;
		revenue += rhs.revenue;
		return *this;
	}
};

int main()
{
    
    
	Sales_data total;
	double price;
	if (cin >> total.bookNo >> total.units_sold >> price)
	{
    
    
		total.revenue = price * total.units_sold;
		Sales_data trans;
		while (cin >> trans.bookNo >> trans.units_sold >> price)
		{
    
    
			if (total.isbn() == trans.isbn())
			{
    
    
				trans.revenue = price * trans.units_sold;
				total.combine(trans);
			}
			else
			{
    
    
				cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
				total = trans;
				total.revenue = price * total.units_sold;
			}
		}
		cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

Exercise 7.4

见 Exercise 7.5

Exercise 7.5

#include <iostream>
#include <string>
using namespace std;

struct Person {
    
    
	string name;
	string add;
	string rn() const // return name
	{
    
    
		return name;
	}
	string ra() const // return address
	{
    
    
		return add;
	}
};

int main()
{
    
    
	Person p1, p2;
	p1.name = "Ludwig van Beethoven";
	p1.add = "Bonn, Germany";
	p2.name = "Teddy van Jerry";
	p2.add = "Nanjing, China";
	cout << p1.rn() << " - " << p1.ra() << endl;
	cout << p2.rn() << " - " << p2.ra() << endl;
	return 0;
}

const 比较普适,可适用于 const 和 plain 两种。

Exercise 7.6

见 Exercise 7.7

Exercise 7.7

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
	string isbn() const; // declare
	double avg_price() const; // declare
	Sales_data& combine(const Sales_data&); // declare
};

string Sales_data::isbn() const // define
	{
    
    
		return bookNo;
	}

double Sales_data::avg_price() const // define
{
    
    
	if (units_sold)
		return revenue / units_sold;
	else return 0;
}

Sales_data& Sales_data::combine(const Sales_data& rhs) // define
	{
    
    
		units_sold += rhs.units_sold;
		revenue += rhs.revenue;
		return *this;
	}

Sales_data add(const Sales_data& lhs, const Sales_data& rhs) // define
{
    
    
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

istream& read(istream& is, Sales_data& item) // define
{
    
    
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

ostream& print(ostream& os, const Sales_data& item) // define
{
    
    
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

int main()
{
    
    
	Sales_data total;
	if (read(cin, total))
	{
    
    
		Sales_data trans;
		while (read(cin,trans))
		{
    
    
			if (total.isbn() == trans.isbn())
				total = add(total, trans);
			else
			{
    
    
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

Exercise 7.8

Because read() makes a difference to them while print() makes no change on them.

Exercise 7.9

#include <iostream>
#include <string>
using namespace std;

struct Person {
    
    
	string name;
	string add;
	string rn() const // return name
	{
    
    
		return name;
	}
	string ra() const // return address
	{
    
    
		return add;
	}
};

istream& read(istream& is, Person& p) // define
{
    
    
	is >> p.name >> p.add;
	return is;
}
ostream& print(ostream& os, const Person& p) // define
{
    
    
	os << p.rn() << " " << p.ra();
	return os;
}

int main()
{
    
    
	Person p1, p2;
	read(cin, p1);
	read(cin, p2);
	print(cout, p1) << endl;
	print(cout, p2) << endl;
	return 0;
}

Exercise 7.11

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
	// constructors added
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {
    
    }
	Sales_data(const string& s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(p * n) {
    
    }
	Sales_data(istream&);
	// other members as before 
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
	string isbn() const; // declare
	double avg_price() const; // declare
	Sales_data& combine(const Sales_data&); // declare
};

string Sales_data::isbn() const // define
{
    
    
	return bookNo;
}

double Sales_data::avg_price() const // define
{
    
    
	if (units_sold)
		return revenue / units_sold;
	else return 0;
}

Sales_data& Sales_data::combine(const Sales_data& rhs) // define
{
    
    
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

Sales_data add(const Sales_data& lhs, const Sales_data& rhs) // define
{
    
    
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

istream& read(istream& is, Sales_data& item) // define
{
    
    
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

ostream& print(ostream& os, const Sales_data& item) // define
{
    
    
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

Sales_data::Sales_data(istream& is)
{
    
    
	read(is, *this);
}

int main()
{
    
    
	Sales_data total;
	if (read(cin, total))
	{
    
    
		Sales_data trans;
		while (read(cin, trans))
		{
    
    
			if (total.isbn() == trans.isbn())
				total = add(total, trans);
			else
			{
    
    
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

Exercise 7.12

直接拉进来会有错:
Error
Output 中第3行有:

1>D:\C++ Primer\Exercise 7.12\7.12.cpp(10,28): error C3861: ‘read’: identifier not found

应该这样:

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
	// constructors added
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {
    
    }
	Sales_data(const string& s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(p* n) {
    
    }
	Sales_data(istream& is)
	{
    
    
		double price;
		is >> bookNo >> units_sold >> price;
		revenue = price * units_sold;
	}
	// other members as before 
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
	string isbn() const; // declare
	double avg_price() const; // declare
	Sales_data& combine(const Sales_data&); // declare
};

string Sales_data::isbn() const // define
{
    
    
	return bookNo;
}

double Sales_data::avg_price() const // define
{
    
    
	if (units_sold)
		return revenue / units_sold;
	else return 0;
}

Sales_data& Sales_data::combine(const Sales_data& rhs) // define
{
    
    
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

Sales_data add(const Sales_data& lhs, const Sales_data& rhs) // define
{
    
    
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

istream& read(istream& is, Sales_data& item) // define
{
    
    
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

ostream& print(ostream& os, const Sales_data& item) // define
{
    
    
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

int main()
{
    
    
	Sales_data total;
	if (read(cin, total))
	{
    
    
		Sales_data trans;
		while (read(cin, trans))
		{
    
    
			if (total.isbn() == trans.isbn())
				total = add(total, trans);
			else
			{
    
    
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

第13-14行也可以写成

		is >> this -> bookNo >> this -> units_sold >> price;
		this -> revenue = this -> price * this -> units_sold;

Exercise 7.13

同 Exercise 7.12

Exercise 7.14

Exercise 7.12 第8行改成

	Sales_data(const string& s) :bookNo(s), units_sold(0), revenue(0) {
    
    }

一直在想第8行到底有什么用,毕竟有第9行在了。于是把第8行注释掉,结果去掉之后一样好用:
Output

Exercise 7.15

#include <iostream>
#include <string>
using namespace std;

struct Person {
    
    
	Person() :name(""), add("") {
    
    }
	string name;
	string add;
	string rn() const // return name
	{
    
    
		return name;
	}
	string ra() const // return address
	{
    
    
		return add;
	}
};

istream& read(istream& is, Person& p) // define
{
    
    
	is >> p.name >> p.add;
	return is;
}
ostream& print(ostream& os, const Person& p) // define
{
    
    
	os << p.rn() << " " << p.ra();
	return os;
}

int main()
{
    
    
	Person p1, p2;
	read(cin, p1);
	read(cin, p2);
	print(cout, p1) << endl;
	print(cout, p2) << endl;
	return 0;
}

Exercise 7.19

见 Exercise 7.22

Exercise 7.21

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
public:
	friend istream& read(istream&, Sales_data&);
	friend ostream& print(ostream&, const Sales_data&);
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {
    
    }
	Sales_data(const string& s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(p* n) {
    
    }
	Sales_data(istream& is)
	{
    
    
		double price;
		is >> bookNo >> units_sold >> price;
		revenue = price * units_sold;
	}
	Sales_data& combine(const Sales_data&); // declare
	string isbn() const; // declare
	double avg_price() const; // declare
private:
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

string Sales_data::isbn() const // define
{
    
    
	return bookNo;
}

double Sales_data::avg_price() const // define
{
    
    
	if (units_sold)
		return revenue / units_sold;
	else return 0;
}

Sales_data& Sales_data::combine(const Sales_data& rhs) // define
{
    
    
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

Sales_data add(const Sales_data& lhs, const Sales_data& rhs) // define
{
    
    
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

istream& read(istream& is, Sales_data& item) // define
{
    
    
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

ostream& print(ostream& os, const Sales_data& item) // define
{
    
    
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

int main()
{
    
    
	Sales_data total;
	if (read(cin, total))
	{
    
    
		Sales_data trans;
		while (read(cin, trans))
		{
    
    
			if (total.isbn() == trans.isbn())
				total = add(total, trans);
			else
			{
    
    
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

Exercise 7.22

#include <iostream>
#include <string>
using namespace std;

struct Person {
    
    
	friend istream& read(istream& is, Person& p);
public:
	Person() :name(""), add("") {
    
    }
	string rn() const // return name
	{
    
    
		return name;
	}
	string ra() const // return address
	{
    
    
		return add;
	}
private:
	string name;
	string add;
};

istream& read(istream& is, Person& p) // define
{
    
    
	is >> p.name >> p.add;
	return is;
}
ostream& print(ostream& os, const Person& p) // define
{
    
    
	os << p.rn() << " " << p.ra();
	return os;
}

int main()
{
    
    
	Person p1, p2;
	read(cin, p1);
	read(cin, p2);
	print(cout, p1) << endl;
	print(cout, p2) << endl;
	return 0;
}

Exercise 7.24

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Screen {
    
    
public:
	using sz = string::size_type;
	Screen() = default;
	Screen(sz ht, sz wd, char c) :height(ht), width(wd), contents(ht * wd,' ') {
    
    }
	char get() const
	{
    
    
		return contents[cursor];
	}
	char get(sz r, sz c)
	{
    
    
		cursor = r * width + c;
		return contents[cursor];
	}
private:
	sz cursor = 0;
	sz height = 0, width = 0;
	string contents;
};

int main()
{
    
    

}

Exercise 7.26

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    
    
public:
	friend istream& read(istream&, Sales_data&);
	friend ostream& print(ostream&, const Sales_data&);
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {
    
    }
	Sales_data(const string& s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(p* n) {
    
    }
	Sales_data(istream& is)
	{
    
    
		double price;
		is >> bookNo >> units_sold >> price;
		revenue = price * units_sold;
	}
	Sales_data& combine(const Sales_data&); // declare
	string isbn() const; // declare
	inline double avg_price() const; // declare
private:
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

string Sales_data::isbn() const // define
{
    
    
	return bookNo;
}

inline double Sales_data::avg_price() const // define
{
    
    
	if (units_sold)
		return revenue / units_sold;
	else return 0;
}

Sales_data& Sales_data::combine(const Sales_data& rhs) // define
{
    
    
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

Sales_data add(const Sales_data& lhs, const Sales_data& rhs) // define
{
    
    
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

istream& read(istream& is, Sales_data& item) // define
{
    
    
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

ostream& print(ostream& os, const Sales_data& item) // define
{
    
    
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
	return os;
}

int main()
{
    
    
	Sales_data total;
	if (read(cin, total))
	{
    
    
		Sales_data trans;
		while (read(cin, trans))
		{
    
    
			if (total.isbn() == trans.isbn())
				total = add(total, trans);
			else
			{
    
    
				print(cout, total) << endl;
				total = trans;
			}
		}
		print(cout, total) << endl; // Don't forget the last book number!
		return 0;
	}
	else
	{
    
    
		cerr << "No data?!" << endl;
		return -1;
	}
}

Exercise 7.27

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Screen {
    
    
public:
	using sz = string::size_type;
	Screen() = default;
	Screen(sz ht, sz wd, char c) :height(ht), width(wd), contents(ht * wd, ' ') {
    
    }
	char get() const
	{
    
    
		return contents[cursor];
	}
	char get(sz r, sz c)
	{
    
    
		cursor = r * width + c;
		return contents[cursor];
	}
	Screen& move(sz a, sz b)
	{
    
    
		height = a;
		width = b;
		return *this;
	}
	Screen& set(char c)
	{
    
    
		contents[cursor] = c;
		return *this;
	}
	Screen& display(ostream& os)
	{
    
    
		do_display(os);
		return *this;
	}
	const Screen& display(ostream& os) const
	{
    
    
		do_display(os);
		return *this;
	}
private:
	void do_display(ostream& os) const
	{
    
    
		os << contents;
	}
	sz cursor = 0;
	sz height = 0, width = 0;
	string contents;
};

int main()
{
    
    
	Screen myScreen(5, 5, 'X');
	myScreen.move(4, 0).set('#').display(cout);
	cout << "\n";
	myScreen.display(cout);
	cout << "\n";
	return 0;
}

Output:
Output

Exercise 7.29

Output

Exercise 7.30

class X; // a declaration
class Y
{
    
    
    X y;
};
class X
{
    
    
    Y* x;
};

Exercise 7.32

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Window_mgr
{
    
    
public:
	using ScreenIndex = vector<Screen>::size_type;
	void clear(ScreenIndex); // a declaration
private:
	vector<Screen> screens{
    
     Screen(24,28,' ') };
};

class Screen {
    
    
	friend void Window_mgr::clear(ScreenIndex);
public:
	using sz = string::size_type;
	Screen() = default;
	Screen(sz ht, sz wd, char c) :height(ht), width(wd), contents(ht* wd, ' ') {
    
    }
	char get() const
	{
    
    
		return contents[cursor];
	}
	char get(sz r, sz c)
	{
    
    
		cursor = r * width + c;
		return contents[cursor];
	}
	Screen& move(sz a, sz b)
	{
    
    
		height = a;
		width = b;
		return *this;
	}
	Screen& set(char c)
	{
    
    
		contents[cursor] = c;
		return *this;
	}
	Screen& display(ostream& os)
	{
    
    
		do_display(os);
		return *this;
	}
	const Screen& display(ostream& os) const
	{
    
    
		do_display(os);
		return *this;
	}
private:
	void do_display(ostream& os) const
	{
    
    
		os << contents;
	}
	sz cursor = 0;
	sz height = 0, width = 0;
	string contents;
};

void Window_mgr::clear(ScreenIndex i)
{
    
    
	Screen& s = screens[i];
	s.contents = string(s.height * s.width, ' ');
//	screens[i].contents = string(screens[i].height * screens[i].width, ' ');
}

int main()
{
    
    
	Screen myScreen(5, 5, 'X');
	myScreen.move(4, 0).set('#').display(cout);
	cout << "\n";
	myScreen.display(cout);
	cout << "\n";
	return 0;
}

怎么不对?
Error
(正在思考)

Next Chapter

猜你喜欢

转载自blog.csdn.net/weixin_50012998/article/details/108461034