c++ primer 学习笔记-第七章

习题7.1:

#include   
#include   

using std::cin; using std::cout;
using std::endl; using std::string;

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

int main(){
	cout << "this is a test:" << endl;
	Sales_data total;
	if (cin >> total.bookIsbn >> total.units_sold >> total.price)
	{
		Sales_data trans;
		while (cin >> trans.bookIsbn >> trans.units_sold >> trans.price)
		{
			if (total.bookIsbn == trans.bookIsbn)
			{
				total.revenue = total.price*total.units_sold + trans.price*trans.units_sold;
				cout << total.revenue << endl;
				total.units_sold += trans.units_sold;
				total.price = total.revenue / total.units_sold;
			}
			else
			{
				cout << "书号:" << total.bookIsbn << " 已售本数:"
					<< total.units_sold << " 平均价格:" << total.price << endl;
				total = trans;
			}
		}
		cout << "书号:" << total.bookIsbn << " 已售本数:"
			<< total.units_sold << " 平均价格:" << total.price << endl;
	}
	else
	{
		cout << "你输入了个啥?大萨比!" << endl;
	}
	getchar();
	getchar();
	return 0;
}

习题7.2-7.3:

头文件Sales_data.h:

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include 
using std::string;
//类
struct Sales_data
{
	//成员函数
	string isbn()const;
	Sales_data &combine(const Sales_data &);
	//成员变量
	string bookIsbn;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

#endif

Sales_data.cpp:

#include 
#include "Sales_data.h"

using std::string;

string Sales_data::isbn()const
{ 
	return bookIsbn; 
}
Sales_data & Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

源.cpp:

#include   
#include   
#include "Sales_data.h"
using std::cin; using std::cout;
using std::endl; using std::string;

int main(){
	cout << "this is a test:" << endl;
	Sales_data total;
	if (cin >> total.bookIsbn >> total.units_sold >> total.revenue)
	{
		Sales_data trans;
		while (cin >> trans.bookIsbn >> trans.units_sold >> trans.revenue)
		{
			if (total.isbn() == trans.isbn())
				total.combine(trans);
			else
			{
				cout << "ISBN编号:" << total.isbn() << " 已售出:"
					<< total.units_sold << " 平均价格:" << total.revenue / total.units_sold
					<< " 总收益:" << total.revenue << endl;
				total = trans;
			}
		}
		cout << "ISBN编号:" << total.isbn() << " 已售出:"
			<< total.units_sold << " 平均价格:" << total.revenue / total.units_sold
			<< " 总收益:" << total.revenue << endl;
	}
	else
	{
		cout << "特么的啥也没有?" << endl;
	}
	getchar();
	getchar();
	return 0;
}

习题7.6:

非成员函数定义所在cpp:

#include 
#include 
#include //使用runtime_error
#include "Sales_data.h"

using std::cin; using std::cout; using std::endl;
using std::string;
using std::runtime_error;
//成员函数
string Sales_data::isbn()const
{ 
	return bookIsbn; 
}
Sales_data & Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

//非成员函数
istream & read(istream &is, Sales_data &item)
{
	double price = 0;
	is >> item.bookIsbn >> item.units_sold >> price;
	item.revenue = item.units_sold*price;
	return is;
}
ostream & print(ostream &os, const Sales_data &item)
{
	os << "ISBN编号:" << item.isbn() << " 已售出:"
		<< item.units_sold << " 平均价格:" << item.revenue / item.units_sold
		<< " 总收益:" << item.revenue;
	return os;
}
Sales_data &add(const Sales_data &lhs, const Sales_data &rhs)
{
	Sales_data sum = lhs;
	while (true)
	{
		try{
			if (lhs.isbn() == rhs.isbn)
			{
				sum.combine(rhs);
				return sum;
			}
			else
				throw runtime_error("输入的ISBN不相同,不能相加!");
		}
		catch (runtime_error err){
			cout << err.what() << endl
				<< "Oops, try again? Enter Y/N." << endl;
			char c;
			cin >> c;
			if (!cin || tolower(c) == 'n')
				break;
		}
	}
	cout << "输入不正确,接下来的结果不予保证!" << endl;
	return sum;//到这里应该只是随意返回一个错误对象了
}

习题7.7:

#include   
#include   
#include "Sales_data.h"
using std::cin; using std::cout;
using std::endl; using std::string;

int main(){
	cout << "this is a test:" << endl;
	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
	{
		cout << "特么的啥也没有?" << endl;
	}
	getchar();
	getchar();
	return 0;
}

习题7.9:

Person.cpp

#include 
#include "Person.h"

using std::cin; using std::cout; using std::endl;
using std::istream; using std::ostream;


istream &read(istream &is, Person &person)
{
	is >> person.name >> person.address;
	return is;
}
ostream &print(ostream &os, const Person &person)
{
	os << "姓名:" << person.name << " 家庭住址:" << person.address;
	return os;
}

习题7.11:

头文件:

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include 
using std::string;
using std::istream;
using std::ostream;
//类
struct Sales_data
{
	//成员函数
	string isbn()const;
	Sales_data &combine(const Sales_data &);
	//构造函数
	Sales_data() = default;
	Sales_data(const string &s) :bookIsbn(s){};
	Sales_data(const string &s, unsigned int n, double p) :bookIsbn(s), units_sold(n), revenue(p*n){};
	Sales_data(istream &is);

	//成员变量
	string bookIsbn;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

istream &read(istream &is, Sales_data &item);
ostream &print(ostream &os, const Sales_data &item);
Sales_data &add(const Sales_data &lhs, const Sales_data &rhs);

#endif


cpp文件:

#include 
#include 
#include //使用runtime_error
#include "Sales_data.h"

using std::cin; using std::cout; using std::endl;
using std::string;
using std::runtime_error;

//类外定义的构造函数
Sales_data::Sales_data(istream &is)
{
	cout << "请输入ISBN、已售数目、书本单价:" << endl;
	read(is, *this);
}
//成员函数
string Sales_data::isbn()const
{ 
	return bookIsbn; 
}
Sales_data & Sales_data::combine(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

//非成员函数
istream & read(istream &is, Sales_data &item)
{
	double price = 0;
	is >> item.bookIsbn >> item.units_sold >> price;
	item.revenue = item.units_sold*price;
	return is;
}
ostream & print(ostream &os, const Sales_data &item)
{
	os << "ISBN编号:" << item.isbn() << " 已售出:"
		<< item.units_sold << " 平均价格:" << item.revenue / item.units_sold
		<< " 总收益:" << item.revenue;
	return os;
}
Sales_data &add(const Sales_data &lhs, const Sales_data &rhs)
{
	Sales_data sum = lhs;
	while (true)
	{
		try{
			if (lhs.isbn() == rhs.isbn())
			{
				sum.combine(rhs);
				return sum;
			}
			else
				throw runtime_error("输入的ISBN不相同,不能相加!");
		}
		catch (runtime_error err){
			cout << err.what() << endl
				<< "Oops, try again? Enter Y/N." << endl;
			char c;
			cin >> c;
			if (!cin || tolower(c) == 'n')
				break;
		}
	}
	cout << "输入不正确,接下来的结果不予保证!" << endl;
	return sum;//到这里应该只是随意返回一个错误对象了
}


源文件:

#include   
#include   
#include "Sales_data.h"
using std::cin; using std::cout;
using std::endl; using std::string;

int main(){
	cout << "this is a test:" << endl;
	/*
	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
	{
		cout << "特么的啥也没有?" << endl;
	}
	*/
	Sales_data book1, book2("Harry Potter"), book3("C++ primer 5th", 10, 110), book4(cin);
	print(cout, book1) << endl;
	print(cout, book2) << endl;
	print(cout, book3) << endl;
	print(cout, book4) << endl;
	system(0);
	getchar();
	getchar();
	return 0;
}

习题7.13:

#include   
#include   
#include "Sales_data.h"
using std::cin; using std::cout;
using std::endl; using std::string;

int main(){
	cout << "this is a test:" << endl;
	
	Sales_data total(cin);
	if (!total.isbn().empty())
	{
		while (cin)//到最后一条时 此处不是输入数据的地方,cin返回true
		{
			Sales_data trans(cin);//调用read(),但是ctrl^z,不输入,默认初始化,继续向下走
			if (total.isbn() == trans.isbn())//可在else下加入cout测得trans.bookIsbn为空,返回0,进入else
				total.combine(trans);
			else
			{
				print(cout, total) << endl;//这样写可以保证最后一条数据正常显示
				total = trans;
			}
		}
	}
	else
	{
		cout << "特么的啥也没有?" << endl;
	}
	
	getchar();
	getchar();
	return 0;
}

习题7.15:

Person.h:

#ifndef PERSON_H
#define PERSON_H

#include 
#include 

using std::string;
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ostream;

struct Person;
istream &read(istream &is, Person &person);//istream版构造函数需要使用read,此处为前置声明
struct Person{
	//构造函数
	Person() = default;
	Person(const string &n, const string &addr) :name(n), address(addr){
		//cout << "请输入您的姓名、家庭住址:" << endl;
	}
	Person::Person(istream &is){ read(cin, *this); }
	//成员变量
	string name = "毛蛋";
	string address = "合群新村";
	//成员函数
	auto getName()const->const string & { return name; }//返回对象姓名
	auto getAddress()const->const string & { return address; }//返回对象家庭住址
};
//非成员对象
istream &read(istream &is, Person &person);//读入Person对象信息
ostream &print(ostream &os, const Person &person);//打印Person对象信息
#endif

习题7.23:

Screen.h:

#ifndef SCREEN_H
#define SCREEN_H

#include 
#include 
#include 
#include 

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector;

class Screen{
public:
	using pos = string::size_type;//类型别名
	//构造函数
	Screen() = default;
	Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c){}
	//成员函数
	char get()const{ return contents[cursor]; }
	char get(pos r, pos c)const{ return contents[r*width + c]; }
	Screen &move(pos r, pos c);
private:
	//成员变量
	pos cursor = 0;
	pos height = 0, width = 0;
	string contents;
};
#endif

Screen.cpp:

#include 
#include 
#include 
#include 

#include "Screen.h"

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector;

inline Screen & Screen::move(pos r, pos c)
{
	cursor = r*width + c;
	return *this;
}

习题7.24:

#ifndef SCREEN_H
#define SCREEN_H

#include 
#include 
#include 
#include 

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector;

class Screen{
public:
	using pos = string::size_type;//类型别名
	//构造函数
	Screen() = default;//1
	Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' '){}//2
	Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c){}//3
	//成员函数
	char get()const{ return contents[cursor]; }
	char get(pos r, pos c)const{ return contents[r*width + c]; }
	Screen &move(pos r, pos c);
private:
	//成员变量
	pos cursor = 0;
	pos height = 0, width = 0;
	string contents;
};
#endif

习题7.42:

#ifndef BOOK_H
#define BOOK_H

#include 
#include 
using std::string;
using std::istream;
class Book
{
	friend void read(istream &is, Book book);
public:
	Book(const string &a, const string &n, const string &i, const string &d)
		:author(a), name(n), isbn(i), pubdate(d)
	{ }
	Book() :Book("", "", "", ""){}
	Book(const string &book_name) :Book(book_name, "", "", ""){}
	Book(istream &is) :Book(){ read(is, *this); }
private:
	string name;
	string isbn;
	string author;
	string pubdate;
};

void read(istream &is, Book book)
{
	std::cout << "请输入书名、ISBN、作者、出版日期" << std::endl;
    is >> book.name >> book.isbn >> book.author >> book.pubdate;
}


#endif

习题7.42:


你可能感兴趣的:(c++)