C++Primer第七章习题答案

//7.1
#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	cout << "请输入交易记录“isbn、售出册数、总销售额、平均售价”" << endl;
	Sales_data total;//保存下一条交易记录的变量
	//读入第一条交易记录的变量
	if (cin >> total)
	{
		Sales_data trans;
		while (cin >> trans)
		{
			if (total.isbn() == trans.isbn())
				total += trans;
			else
			{
				cout << total << endl;
				total = trans;
			}
		}
		cout << total << endl;
	}
	else
	{
		cerr << "没有输入数据" << endl;
		return -1;
	}
	return 0;
}
//7.2
#pragma once
#include<string>
using namespace std;

class Sales_data {
private:
	string bookNo;//书籍编号
	unsigned units_sold = 0;//销售量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
public:
	string isbn() const {
		return bookNo;
	}
	Sales_data& combine(const Sales_data&);
};
Sales_data& Sales_data::combine(const Sales_data&rhs)
{
	if (units_sold)
	{
		saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (rhs.units_sold + units_sold);
	}
	else
		saleprice = 0;
	if (sellprice != 0)
	{
		discount = saleprice / sellprice;
	}
	return *this;
}
//7.3
#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	cout << "请输入交易记录“isbn、售出册数、总销售额、平均售价”" << endl;
	Sales_data total;//保存下一条交易记录的变量
	//读入第一条交易记录的变量
	if (cin >> total)
	{
		Sales_data trans;
		while (cin >> trans)
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				cout << total << endl;
				total = trans;
			}
		}
		cout << total << endl;
	}
	else
	{
		cerr << "没有输入数据" << endl;
		return -1;
	}
	return 0;
}
//7.4
#pragma once
#include<string>
using namespace std;

class person {
private:
	string name = "";
	string address = "";
};
//7.5
class person {
private:
	string name = "";
	string address = "";
public:
	string getname() const { return name; }//返回姓名
	string getadress() const { return address; }//返回住址
};
//7.6
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)
	{
		is >> item.bookNo >> item.sellprice >> item.units_sold >> item.saleprice;
		return is;
	}
	ostream& print(ostream &os, Sales_data &item)
	{
		os << item.isbn() <<" "<< item.units_sold << " "<<item.sellprice << " "<<
			item.saleprice <<" "<< item.discount;
		return os;
	}
//7.7
#pragma once
#include<string>
using namespace std;

class Sales_data {
public:
	string bookNo;//书籍编号
	unsigned units_sold = 0;//销售量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣

	string isbn() const {
		return bookNo;
	}
	Sales_data& combine(const Sales_data&);
};
Sales_data& Sales_data::combine(const Sales_data&rhs)
{
	if (units_sold)
	{
		saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (rhs.units_sold + units_sold);
	}
	else
		saleprice = 0;
	if (sellprice != 0)
	{
		discount = saleprice / sellprice;
	}
	return *this;
}
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)
{
	is >> item.bookNo >> item.sellprice >> item.units_sold >> item.saleprice;
	return is;
}
ostream& print(ostream &os, const Sales_data &item)
{
	os << item.isbn() << " " << item.units_sold << " " << item.sellprice << " " <<
		item.saleprice << " " << item.discount;
	return os;
}
#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	cout << "请输入交易记录“isbn、售出册数、总销售额、平均售价”" << endl;
	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);
				total = trans;
			}
		}
		print(cout, total);
	}
	else
	{
		cerr << "没有输入数据" << endl;
		return -1;
	}
	return 0;
}
//7.9
istream& read(istream & is, person &per)
{
	is >> per.name >> per.address;
	return is;
}
ostream& read(ostream & os, const person &per)
{
	os<<per.name<<" "<<per.address;
	return os;
}
//7.11
/*此程序未重载输入流运算符>>,输入类*/
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Sales_data{
public:
	Sales_data() = default;//默认构造函数
	Sales_data(const string &book) :bookNo(book) {};
	Sales_data(const string &book, int num, double sell, double sale) :
		bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
		if (sellprice)
			discount = saleprice / sellprice;
	}
	Sales_data(istream &is)
	{
		is >> *this;
	}
public:
	string bookNo;//书型号
	int units_sold = 0;//销售数量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
};
#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	Sales_data data1;
	Sales_data data2("jhfud-fasdi");
	Sales_data data3("jfias",4,5,3);
	Sales_data data4(cin);
	cout << data1 << endl << data2 << endl << data3 << endl << data4 << endl;
}
//7.12
class Sales_data{
public:
	Sales_data() = default;//默认构造函数
	Sales_data(const string &book) :bookNo(book) {};
	Sales_data(const string &book, int num, double sell, double sale) :
		bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
		if (sellprice)
			discount = saleprice / sellprice;
	}
	Sales_data(istream &is)
	{
		is >> *this;
	}
public:
	string bookNo;//书型号
	int units_sold = 0;//销售数量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
};
//7.13
/*此程序未重载输入流运算符>>,输入类*/
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Sales_data{
public:
	Sales_data() = default;//默认构造函数
	Sales_data(const string &book) :bookNo(book) {};
	Sales_data(const string &book, int num, double sell, double sale) :
		bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
		if (sellprice)
			discount = saleprice / sellprice;
	}
	Sales_data(istream &is)
	{
		is >> *this;
	}
public:
	string bookNo;//书型号
	int units_sold = 0;//销售数量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
public:
	Sales_data &combine(Sales_data &rhs);
	string isbn() const { return bookNo; }
};
Sales_data& Sales_data::combine(Sales_data &rhs)
{
	if (units_sold)
	{
		saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (units_sold + rhs.units_sold);
	}
	else
		saleprice = 0;
	if (sellprice)
		discount = saleprice / sellprice;
	return *this;
}
istream &read(istream &is, Sales_data &item)
{
	is >> item.bookNo >> item.units_sold >> item.sellprice >> item.saleprice;
	return is;
}
ostream &print(ostream &os, Sales_data &item)
{
	os << item.bookNo << " " << item.units_sold << " " << item.sellprice << " " << item.saleprice << item.discount << endl;
	return os;
}
#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	Sales_data total(cin);
	if (cin)
	{
		while (read(cin, trans))
		{
			if (total.isbn() == trans.isbn())
			{
				total.combine(trans);
				trans = total;
			}
			else
				print(cout,total);
		}
		print(cout, total);
	}
	else
	{
		cerr << "No data" << endl;
	}
	return 0;
}
//7.14
Sales_data(const string &book) :
		bookNo(book), units_sold(0), sellprice(0), saleprice(0) {};
//7.15
class person {
public:
	person() = default;
	person(const string &na, const string add):
		name(na), address(add) {}
	person(istream &is)
	{
		is >> *this;
	}
private:
	string name = "";
	string address = "";
public:
	string getname() const { return name; }//返回姓名
	string getadress() const { return address; }//返回住址
};
//7.21
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Sales_data {
	friend istream &read(istream &is, Sales_data &item);
	friend ostream &print(ostream &os, Sales_data &item);
public:
	Sales_data() = default;//默认构造函数
	Sales_data(const string &book) :bookNo(book) {};
	Sales_data(const string &book, int num, double sell, double sale) :
		bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
		if (sellprice)
			discount = saleprice / sellprice;
	}
	/*Sales_data(istream &is)
	{
		is >> *this;
	}*/
private:
	string bookNo;//书型号
	int units_sold = 0;//销售数量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
public:
	Sales_data &combine(const Sales_data &rhs);
	string isbn() const { return bookNo; }
};
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
	if (units_sold)
	{
		saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (units_sold + rhs.units_sold);
	}
	else
		saleprice = 0;
	if (sellprice)
		discount = saleprice / sellprice;
	return *this;
}
istream &read(istream &is, Sales_data &item)
{
	is >> item.bookNo >> item.units_sold >> item.sellprice >> item.saleprice;
	return is;
}
ostream &print(ostream &os, Sales_data &item)
{
	os << item.bookNo << " " << item.units_sold << " " << item.sellprice << " " << item.saleprice << item.discount << endl;
	return os;
}
Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	cout << "请输入交易记录“isbn、售出册数、总销售额、平均售价”" << endl;
	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);
				total = trans;
			}
		}
		print(cout, total);
	}
	else
	{
		cerr << "没有输入数据" << endl;
		return -1;
	}
	return 0;
}
//7.22
class person {
public:
	person() = default;
	person(const string &na, const string add):
		name(na), address(add) {}
	person(istream &is)
	{
		is >> *this;
	}
private:
	string name = "";
	string address = "";
public:
	string getname() const { return name; }//返回姓名
	string getadress() const { return address; }//返回住址
};
//7.23
class Screen {
public:
private:
	unsigned height = 0, width = 0;//坐标
	unsigned sursor = 0;//光标
	string contents;//屏幕显示
};
//7.24

class Screen {
public:
	Screen() = default;
	Screen(unsigned ht,unsigned wd):
		height(ht), width(wd), contents(ht*wd, ' ') {}
	Screen(unsigned ht, unsigned wd,char c) :
		height(ht), width(wd), contents(ht*wd,c) {}
private:
	unsigned height = 0, width = 0;//坐标
	unsigned cursor = 0;//光标
	string contents;//屏幕显示
};
//7.26
class Sale_data {
public:
	double avg_price()
	{
		if (units_sold)
		{
			return saleprice / units_sold;
		}
		else
			return -1;
	}
private:
	double units_sold = 0.0;
	double saleprice = 0.0;
};
class Sale_data {
public:
	double avg_price();
private:
	double units_sold = 0.0;
	double saleprice = 0.0;
};
inline double Sale_data::avg_price()
{
	if (units_sold)
	{
		return saleprice / units_sold;
	}
	else
		return -1;
}
//7.27
#include<string>
using namespace std;

class Screen {
public:
	Screen() = default;
	Screen(unsigned ht,unsigned wd):
		height(ht), width(wd), contents(ht*wd, ' ') {}
	Screen(unsigned ht, unsigned wd,char c) :
		height(ht), width(wd), contents(ht*wd,c) {}
public:
	typedef string::size_type pos;
	Screen &move(pos r, pos c) 
	{
		pos row = r*width;
		cursor = row + c;
		return *this;
	}
	Screen &set(char c)
	{
		contents[cursor] = c;
		return *this;
	}
	Screen &display(std::ostream &os)
	{
		do_display(os);
		return *this;
	}
private:
	unsigned height = 0, width = 0;//坐标
	unsigned cursor = 0;//光标
	string contents;//屏幕显示
	void do_display(std::ostream &os) const { os << contents; }
};
#include<iostream>
#include"sales_data.h"
#include<string>

using namespace std;
int main()
{
	Screen myScreen(5,5,'X');
	myScreen.move(4, 0).set('#').display(cout);
	cout << endl;
	myScreen.display(cout);
	cout << endl;
	return 0;
}
//7.32
#pragma once
#include<string>
#include<vector>
using namespace std;

class Window_mgr {
public:
	typedef vector<Screen> ScreenIndex;
	void clear(ScreenIndex);
private:
	vector<Screen> screens{ Screen(24,80,' ') };
};
void Window_mgr::clear(ScreenIndex i)
{
	Screen &s = screens[i];
	s.contents = string(s.height*s.width, ' ');
}
class Screen {
	friend void Window_mgr::clear(ScreenIndex);
public:
	Screen() = default;
	Screen(unsigned ht,unsigned wd):
		height(ht), width(wd), contents(ht*wd, ' ') {}
	Screen(unsigned ht, unsigned wd,char c) :
		height(ht), width(wd), contents(ht*wd,c) {}
public:
	typedef string::size_type pos;
	Screen &move(pos r, pos c) 
	{
		pos row = r*width;
		cursor = row + c;
		return *this;
	}
	Screen &set(char c)
	{
		contents[cursor] = c;
		return *this;
	}
	Screen &display(std::ostream &os)
	{
		do_display(os);
		return *this;
	}
private:
	unsigned height = 0, width = 0;//坐标
	unsigned cursor = 0;//光标
	string contents;//屏幕显示
	void do_display(std::ostream &os) const { os << contents; }
};
//7.40
#include<string>

class Book {
public:
	Book(const string &n, const string &i, const string &a, const string &pu, double p) :
		name(n), isbn(i), author(a), price(p) {}
	Book(std::istream &is) { is >> *this; }
private:
	string name;
	string isbn,author,publisher;
	double price;
};
//7.41
#pragma once

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

class Sales_data {
	friend istream &read(istream &is, Sales_data &item);
	friend ostream &print(ostream &os, Sales_data &item);
public:
	//受委托构造函数,4个参数
	Sales_data(const string &book, int num, double sell, double sale) :
		bookNo(book), units_sold(num), sellprice(sell), saleprice(sale) {
		if (sellprice)
			discount = saleprice / sellprice;
		cout << "该函数输如四个参数" << endl;
	}
	Sales_data() :Sales_data("", 0, 0, 0) 
	{
		cout << " 该函数不输入任何参数,执行默认实参" << endl;
	}
	Sales_data(const string &book) :Sales_data(book,0,0,0) 
	{
		cout << "该函数输入一个参数" << endl;
	}
	Sales_data(istream &is):Sales_data()
	{
		read(is, *this);
		cout << "该函数输入book对象" << endl;
	}
private:
	string bookNo;//书型号
	int units_sold = 0;//销售数量
	double sellprice = 0.0;//进价
	double saleprice = 0.0;//售价
	double discount = 0.0;//折扣
public:
	Sales_data &combine(const Sales_data &rhs);
	string isbn() const { return bookNo; }
};
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
	if (units_sold)
	{
		saleprice = (rhs.sellprice*rhs.units_sold + units_sold*sellprice) / (units_sold + rhs.units_sold);
	}
	else
		saleprice = 0;
	if (sellprice)
		discount = saleprice / sellprice;
	return *this;
}
istream &read(istream &is, Sales_data &item)
{
	is >> item.bookNo >> item.units_sold >> item.sellprice >> item.saleprice;
	return is;
}
ostream &print(ostream &os, Sales_data &item)
{
	os << item.bookNo << " " << item.units_sold << " " << item.sellprice << " " << item.saleprice << item.discount << endl;
	return os;
}
Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
{
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

#include"sales_data.h"

int main()
{
	Sales_data first("hfusdafhu", 10, 3, 4);
	Sales_data second;
	Sales_data third("fahsduf");
	Sales_data fourth(cin);
	return 0;
}
//7.43
class NoDefault 
{
public:
	NoDefault(int i) :index(i) {}
	int index;
};
class C
{
public:
	NoDefault No;
	C(int n = 0) : No(n) {};
};
#include"sales_data.h"
#include<iostream>
using namespace std;

int main()
{
	C c;
	cout << c.No.index<< endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38619342/article/details/84879740