[C++primer]第四章,第五章,第六章,第七章课后习题

第四章

4.21

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

int main()
{
    
    
	vector<int>v;
	for (int i = 0; i < 10; i++) {
    
    
		v.push_back(i);
	}

	for (auto &val : v) {
    
    
		val = ((val % 2 != 0) ? val * 2 : val);
	}
	for (auto val2 : v) {
    
    
		cout << val2 << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

4.22

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

int main()
{
    
    
	int grade = 0;
	while (cin >> grade && grade<101 && grade>=0) {
    
    
		string finalgrade = (grade > 90) ? "high pass"
			: (grade < 75 && grade>60) ? "low pass"
			: (grade < 60) ? "fail" : "pass";
		cout << finalgrade << endl;
	}

	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<string>

int main()
{
    
    
	int grade = 0;
	while (cin >> grade && grade<101 && grade>=0) {
    
    
		if (grade > 90) {
    
    
			cout << "heigh pass" << endl;
		}
		else if(grade > 75) {
    
    
			cout << "pass" << endl;
		}
		else if (grade > 60) {
    
    
			cout << "low pass";
		}
		else {
    
    
			cout << "fail";
		}
	}

	system("pause");
	return 0;
}

第五章

5.12

#include<iostream>
using namespace std;

int main()
{
    
    
	int ffcnt = 0, ficnt = 0, flcnt = 0;
	char ch;

	while (cin >> ch)
	{
    
    
		if (ch == 'f')
		{
    
    
			while (cin >> ch)
			{
    
    
				switch (ch)
				{
    
    
				case 'f':
					++ffcnt;
					break;
				case 'l':
					++flcnt;
					break;
				case 'i':
					++ficnt;
					break;
				default:
					break;
				}
				break;
			}
		}
	}
	cout << "ff: " << ffcnt << endl;
	cout << "fl: " << flcnt << endl;
	cout << "fi: " << ficnt << endl;
}

5.14

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

int main()
{
    
    
	string currstring="",maxstring="",nowstring="";
	int currnum = 1, maxnum = 0;

	while (cin >> currstring)
	{
    
    
		if (currstring == nowstring)
		{
    
    
			++currnum;
			if (currnum > maxnum)
			{
    
    
				maxnum = currnum;
				maxstring = currstring;
			}
		}
		else
		{
    
    
			nowstring = currstring;
			currnum = 1;
		}
	}
	cout << maxstring << " " << "number is:" << maxnum << endl;
}

5.17

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

int main()
{
    
    
	vector<int>v1={
    
    0,1,1,2};
	vector<int>v2={
    
    0,1,1,2,3,5,8};

	vector<int>::iterator it1 = v1.begin(), it2 = v2.begin();
	for (; it1 != v1.end() && it2 != v2.end(); it1++, it2++)
	{
    
    
		if (*it1 != *it2)
		{
    
    
			cout << "无关" << endl;
		}
	}

	if (it1 == v1.end())
	{
    
    
		cout << "v1 是 v2的前缀" << endl;
	}
	if(it2==v2.end())
	{
    
    
		cout << "v2 是 v1的前缀" << endl;
	}


	system("pause");
	return 0;
}

5.20

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

int main()
{
    
    
	string currstring, nowstring;
	bool flag = true;

	while (cin >> currstring)
	{
    
    
		if (currstring == nowstring)
		{
    
    
			flag = false;
			cout << "重复单词:" << currstring << endl;
			break;
		}
		nowstring = currstring;
	}
	if (flag)
	{
    
    
		cout << "无重复" << endl;
	}


	system("pause");
	return 0;
}

5.21

#include<iostream>
using namespace std;
#include<cstring>
#include<cctype>

int main()
{
    
    
	string currstring, nowstring;
	bool flag = true;

	while (cin >> currstring)
	{
    
    
		if (!isupper(currstring[0]))
			continue;

		if (currstring == nowstring)
		{
    
    
			flag = false;
			cout << "重复单词:" << currstring << endl;
			break;
		}
		nowstring = currstring;
	}
	if (flag)
	{
    
    
		cout << "无重复" << endl;
	}


	system("pause");
	return 0;
}

5.25

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

int main()
{
    
    
	int a = 0, b = 0;
	while (cin >> a >> b)
	{
    
    
		try
		{
    
    
			if (b == 0)
			{
    
    
				throw runtime_error("除数不能为0");
			}
			cout << a / b << endl;
		}
		catch (runtime_error chushu_is_zero)
		{
    
    
			cout << chushu_is_zero.what() << endl;
			cout<< "是否重新输入y or n" << endl;
			char c;
			cin >> c;
			if (!cin || c == 'n')
			{
    
    
				break;
			}
		}
	}
	system("pause");
	return 0;
}

第六章

6.3

#include<iostream>
using namespace std;

int fact(int a)
{
    
    
	int sum = 1;
	while (a > 1)
	{
    
    
		sum *= a;
		--a;
	}
	return sum;
}

int main()
{
    
    
	int i = fact(5);
	cout << i << endl;

	system("pause");
	return 0;
}

6.4

同上

6.5

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

int fact(int a)
{
    
    
	int num = abs(a);
	return num;
}

int main()
{
    
    
	int i = fact(-5);
	cout << i << endl;

	system("pause");
	return 0;
}

6.9

Chapter6.h

#pragma once
int fact(int a);

fact.cpp

#include"Chapter6.h"
using namespace std;

int fact(int a)
{
    
    
	int sum = 1;
	while (a > 1)
	{
    
    
		sum *= a;
		--a;
	}
	return sum;
}

main.cpp

#include<iostream>
using namespace std;
#include"Chapter6.h"

int main()
{
    
    
	
	cout << fact(5) << endl;

	system("pause");
	return 0;
}

6.10

#include<iostream>
using namespace std;

void exchange(int* p, int* q)
{
    
    
	int a = 0;
	a = *p;
	*p = *q;
	*q = a;

}

int main()
{
    
    
	int a = 10, b = 8;
	exchange(&a, &b);
	cout << "a= " << a << " " << "b= " << b << endl;

	system("pause");
	return 0;
}

6.12

#include<iostream>
using namespace std;

void exchange(int &p, int &q)
{
    
    
	int temp = 0;
	temp = p;
	p = q;
	q = temp;
}

int main()
{
    
    
	int a = 10, b = 8;
	exchange(a, b);
	cout << "a= " << a << " " << "b= " << b << endl;

	system("pause");
	return 0;
}

6.17

#include<iostream>
using namespace std;
#include<cstring>
#include<cctype>

bool have_da(const string& s)
{
    
    
	for (char val : s)
	{
    
    
		if (isupper(val))
		{
    
    
			return true;
		}
	}
	return false;
}

void tolow(string& s)
{
    
    
	if (have_da(s))
	{
    
    
		for (char &val : s)
		{
    
    
			val = tolower(val);
		}
	}
}

int main()
{
    
    
	string s("Hello Wrold");
	bool flag;
	flag = have_da(s);
	if (flag)
	{
    
    
		cout << "有大写字母" << endl;
		tolow(s);
		cout << "转化为小写字母后:" << s << endl;
	}
	else
	{
    
    
		cout << "无大写字母" << endl;
	}

	system("pause");
	return 0;
}

6.21

#include<iostream>
using namespace std;
#include<ctime>
#include<cstdlib>

int compare(int a, const int* b)
{
    
    
	if (a > *b)
	{
    
    
		return a;
	}
	else
	{
    
    
		return *b;
	}
}

int main()
{
    
    
	int a=0, b=0;
	srand((unsigned int)time(NULL));
	cout << "输入第一个数" << endl;
	cin >> a;
	b = rand() % 100 + 1;
	int max = compare(a, &b);
	cout << "a= " << a << " " << "b= " << b << endl;
	cout << "较大的数:" << max << endl;

	system("pause");
	return 0;
}

6.22

#include<iostream>
using namespace std;

void swap1(int* q, int* p)
{
    
    
	int* temp;
	temp = q;
	q = p;
	p = temp;//交换指针地址,只是临时交换,实参没有改变,所以值和地址都没有变
}

void swap2(int* q, int* p)
{
    
    
	int temp;
	temp = *q;
	*q = *p;
	*p = temp;//指针值传递,内部解引用交换了值
}

void swap3(int*& q, int*& p)
{
    
    
	int* temp;
	temp = q;
	q = p;
	p = temp; //引用方式,也改变了实参的地址和值
}

int main()
{
    
    
	int a = 10, b = 100;
	int* q = &a, * p = &b;
	cout << "before: " << endl;
	cout << "q= " << q << " " << "p= " << p << endl;
	cout << "q的值: " << *q << " " << "p的值: " << *p << endl;
	cout << "after:" << endl;
	swap1(q,p);
	cout << "q= " << q << " " << "p= " << p << endl;
	cout << "q的值: " << *q << " " << "p的值: " << *p << endl;

	a = 10, b = 100;
	q = &a, p = &b;
	cout << "before: " << endl;
	cout << "q= " << q << " " << "p= " << p << endl;
	cout << "q的值: " << *q << " " << "p的值: " << *p << endl;
	cout << "after:" << endl;
	swap2(q,p);
	cout << "q= " << q << " " << "p= " << p << endl;
	cout << "q的值: " << *q << " " << "p的值: " << *p << endl;

	a = 10, b = 100;
	q = &a, p = &b;
	cout << "before: " << endl;
	cout << "q= " << q << " " << "p= " << p << endl;
	cout << "q的值: " << *q << " " << "p的值: " << *p << endl;
	cout << "after:" << endl;
	swap3(q,p);
	cout << "q= " << q << " " << "p= " << p << endl;
	cout << "q的值: " << *q << " " << "p的值: " << *p << endl;

	system("pause");
	return 0;
}

6.23

#include<iostream>
using namespace std;

void print1(const int arr[])
{
    
    
	cout << *arr << endl;
}

void print2(const int* begin, const int* end)
{
    
    
	while (begin != end)
	{
    
    
		cout << *begin << " ";
		++begin;
	}
	cout << endl;
}

void print3(const int* arr, size_t size)
{
    
    
	for (size_t i = 0; i < size; i++)
	{
    
    
		cout << arr[i] << " ";
	}
	cout << endl;
}

int main()
{
    
    
	int i = 0, j[2] = {
    
     0,1 };
	print1(&i);
	print1(j);

	int* beg = begin(j);
	int* en = end(j);
	print2(beg,en);

	size_t size = sizeof(j)/sizeof(*j);
	print3(j, size);
	print3(&i, 1);

	system("pause");
	return 0;
}

6.25

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

int main(int argc,char**argv)
{
    
    
	string str;
	for (int i = 0; i != argc; ++i)
	{
    
    
		str += argv[i];
	}
	cout << str << endl;

	system("pause");
	return 0;
}

调试->调试属性->调试->命令参数 添加自定义命令

argv[0]=你保存程序的名字

在这里插入图片描述

6.26

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

int main(int argc,char**argv)
{
	for (int i = 0; i != argc; ++i)
	{
		cout << "argv[" << i << "]=" << argv[i] << endl;
	}
	
	system("pause");
	return 0;
}

在这里插入图片描述

6.27

#include<iostream>
using namespace std;

int count(initializer_list<int> l1)
{
    
    
	int sum = 0;
	for (auto val : l1)
	{
    
    
		sum += val;
	}
	return sum;
}

int main()
{
    
    
	cout << count({
    
     1,2,3 }) << endl;
	cout << count({
    
     1,2,3,4,5,6 }) << endl;
	
	system("pause");
	return 0;
}

6.32

#include<iostream>
using namespace std;

int& get(int* arry, int index)
{
    
    
	return arry[index];
}

int main()
{
    
    
	int ia[10];
	for (int i = 0; i != 10; ++i)
	{
    
    
		get(ia, i) = i;
	}
	for (int val : ia)
	{
    
    
		cout << val << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

6.33

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

void print(vector<int> &v, vector<int>::iterator it)
{
    
    
	if (!v.empty())
	{
    
    
		if (it != v.end())
		{
    
    
			cout << *it << " ";
			print(v, it+1);
		}
		cout << endl;
	}
	else
	{
    
    
		return;
	}
}

int main()
{
    
    
	vector<int> v = {
    
     0,1,2,3,4,5,6,7,8,9 };
	auto it = v.begin();
	auto itend = v.end();
	print(v, it);

	system("pause");
	return 0;
}

6.56

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

int f1(int a, int b)
{
    
    
	return a + b;
}
int f2(int a, int b)
{
    
    
	return a - b;
}int f3(int a, int b)
{
    
    
	return a *b;
}int f4(int a, int b)
{
    
    
	return a / b;
}

int main()
{
    
    
	//decltype(f1)* p1 = f1, * p2 = f2, * p3 = f3, * p4 = f4;
	//vector<decltype(f1)*>v{ p1,p2,p3,p4 };
	vector<decltype(f1)*>v{
    
     f1,f2,f3,f4 };

	for (auto val : v)
	{
    
    
		cout << val(1,2) << endl;
	}
}

第七章

7.1

Salse_data.h

#pragma once

#include<iostream>
#include<string>

class Sales_data
{
    
    
    friend istream& operator>>(std::istream&, Sales_data&);
    friend ostream& operator<<(std::ostream&, const  Sales_data&);

public:
    Sales_data() = default;
    Sales_data(const std::string& book) : bookNo(book), units_sold(0), sellingprice(0.0), saleprice(0.0), discount(0.0) {
    
    }
    Sales_data(std::istream& is)
    {
    
    
        is >> *this;
    }

public:
    Sales_data& operator+=(const Sales_data& rhs)
    {
    
    
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
        sellingprice = (rhs.sellingprice * rhs.units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
        {
    
    
            discount = saleprice / sellingprice;
        }
        return *this;
    }

    string isbn() const
    {
    
    
        return bookNo;
    }

private:
    string bookNo;
    int units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;

};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
    
    
    return lhs.isbn() == rhs.isbn();
}





istream& operator>>(std::istream& in, Sales_data& s)
{
    
    
    double price;
    in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;

    if (in && s.sellingprice != 0)
        s.discount = s.saleprice / s.sellingprice;
    else
        s = Sales_data();  // input failed: reset object to default state
    return in;
}

ostream& operator<<(std::ostream& out, const Sales_data& s)
{
    
    
    out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " << s.discount;
    return out;
}


main.cpp

#include<iostream>
using namespace std;
#include"Sales_data.h"


int main()
{
    
    
	Sales_data total;
	Sales_data trans;

	cout << "请输入:(ISBN,销售量,原价,实际售价)" << endl;

	if (cin >> total)
	{
    
    
		while (cin >> trans)
		{
    
    
			if (compareIsbn(total, trans))
			{
    
    
				total += trans;
			}
			else
			{
    
    
				cout << "ISBN ,销售量, 原价,平均销售价格,折扣" << total << endl;
				total = trans;
			}
		}
		cout << "ISBN ,销售量, 原价,平均销售价格,折扣" << total << endl;
	}
	else
	{
    
    
		cout << "无数据" << endl;
	}


	system("pause");
	return 0;
}

7.2

Salse_data.h

#pragma once

#include<iostream>
#include<string>

class Sales_data
{
    
    
    friend istream& operator>>(std::istream&, Sales_data&);
    friend ostream& operator<<(std::ostream&, const  Sales_data&);

public:
    Sales_data() = default;
    Sales_data(const std::string& book) : bookNo(book), units_sold(0), sellingprice(0.0), saleprice(0.0), discount(0.0) {
    
    }
    Sales_data(std::istream& is)
    {
    
    
        is >> *this;
    }

public:
    Sales_data& combine(const Sales_data& rhs)
    {
    
    
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
        sellingprice = (rhs.sellingprice * rhs.units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
        {
    
    
            discount = saleprice / sellingprice;
        }
        return *this;
    }

    string isbn() const//只读不写
    {
    
    
        return bookNo;
    }

private:
    string bookNo;
    int units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;

};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
    
    
    return lhs.isbn() == rhs.isbn();
}



istream& operator>>(std::istream& in, Sales_data& s)
{
    
    
    double price;
    in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;

    if (in && s.sellingprice != 0)
        s.discount = s.saleprice / s.sellingprice;
    else
        s = Sales_data();  // input failed: reset object to default state
    return in;
}

ostream& operator<<(std::ostream& out, const Sales_data& s)
{
    
    
    out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " << s.discount;
    return out;
}


7.3

main.cpp

#include<iostream>
using namespace std;
#include"Sales_data.h"


int main()
{
    
    
	Sales_data total;
	Sales_data trans;

	cout << "请输入:(ISBN,销售量,原价,实际售价)" << endl;

	if (cin >> total)
	{
    
    
		while (cin >> trans)
		{
    
    
			if (compareIsbn(total, trans))
			{
    
    
				total.combine(trans);
			}
			else
			{
    
    
				cout << "ISBN ,销售量, 原价,平均销售价格,折扣" << total << endl;
				total = trans;
			}
		}
		cout << "ISBN ,销售量, 原价,平均销售价格,折扣" << total << endl;
	}
	else
	{
    
    
		cout << "无数据" << endl;
	}


	system("pause");
	return 0;
}

7.4 7.5

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

class Person
{
    
    
public:
	string getname ()const
	{
    
    
		return name;
	}
	string getaddress()const
	{
    
    
		return address;
	}

private:
	string name;
	string address;
};

int main()
{
    
    

	system("pause");
	return 0;
}

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.units_sold >> item.sellingprice >> item.saleprice;
	return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
    
    
	os << item.isbn() << " " << item.units_sold << " " << item.sellingprice << " " << item.saleprice << " " << item.discount;
}

7.7

Saales_data.h

#pragma once

#include<iostream>
#include<string>

class Sales_data
{
    
    
    friend istream& read(istream& is, Sales_data& item);
    friend ostream& print(ostream& os, const Sales_data& item);


//public:
//    Sales_data() = default;
//    Sales_data(const std::string& book) : bookNo(book), units_sold(0), sellingprice(0.0), saleprice(0.0), discount(0.0) {}
//    Sales_data(std::istream& is)
//    {
    
    
//        is >> *this;
//    }

public:
    Sales_data& combine(const Sales_data& rhs)
    {
    
    
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
        sellingprice = (rhs.sellingprice * rhs.units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
        {
    
    
            discount = saleprice / sellingprice;
        }
        return *this;
    }

    string isbn() const//只读不写
    {
    
    
        return bookNo;
    }

private:
    string bookNo;
    int units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;

};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
    
    
    return lhs.isbn() == rhs.isbn();
}

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.units_sold >> item.sellingprice >> item.saleprice;
    return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
    
    
    os << item.isbn() << " " << item.units_sold << " " << item.sellingprice << " " << item.saleprice << " " << item.discount;
    return os;
}


main.cpp

#include<iostream>
using namespace std;
#include"Sales_data.h"


int main()
{
    
    
	Sales_data total;
	Sales_data trans;

	cout << "请输入:(ISBN,销售量,原价,实际售价)" << endl;

	if (read(cin,total))
	{
    
    
		while (read(cin, trans))
		{
    
    
			if (compareIsbn(total, trans))
			{
    
    
				total = add(total, trans);
			}
			else
			{
    
    
				cout << "ISBN ,销售量, 原价,平均销售价格,折扣:";
				print(cout, total);
				cout << endl;
				total = trans;
			}
		}
		cout << "ISBN ,销售量, 原价,平均销售价格,折扣:";
		print(cout, total);
		cout << endl;
	}
	else
	{
    
    
		cout << "无数据" << endl;
	}


	system("pause");
	return 0;
}

7.8

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

class Person
{
    
    
	friend istream& read(istream& is, Person& per);
	friend 	ostream& print(ostream& os, const Person& per);

public:
	string getname()const
	{
    
    
		return name;
	}
	string getaddress()const
	{
    
    
		return address;
	}

private:
	string name;
	string address;
};

istream& read(istream& is, Person& per)
{
    
    
	is >> per.name >> per.address;
	return is;
}

ostream& print(ostream& os, const Person& per)
{
    
    
	os << per.getname() << per.getaddress() << endl;
	return os;
}

int main()
{
    
    

	system("pause");
	return 0;
}

7.11

第三个构造函数 为什么const std::string &s 要使用引用,其他不加?

对不容易确定大小但又不想传入之后被修改的对象,比如:vector、string等数据,一般加上const引用,用引用传参的方式避免不必要的拷贝构造导致的内存和cpu支出,cosnt的限定也保证了传入的参数不会被修改。 对于int、double等简单类型,则一般使用传形参的方式,因为其大小较小而且确定,所以为了方便和保护传入的参数不被修改,一般都直接采用传形参

http://blog.csdn.net/cqk0100/article/details/72897504

Sales_data.h

#pragma once

#include<iostream>
#include<string>

class Sales_data
{
    
    
    friend istream& read(istream& is, Sales_data& item);
    friend ostream& print(ostream& os, const Sales_data& item);


public:
    Sales_data() = default;
    Sales_data(const string& s) :bookNo(s) {
    
    };
    Sales_data(const string& s, const int num, const double sell, double sale) :bookNo(s), units_sold(num), sellingprice(sell), saleprice(sale), discount(sale/sell) {
    
    };
    Sales_data(istream& is)
    {
    
    
        read(is, *this);
    }
public:
    Sales_data& combine(const Sales_data& rhs)
    {
    
    
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
        sellingprice = (rhs.sellingprice * rhs.units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
        {
    
    
            discount = saleprice / sellingprice;
        }
        return *this;
    }

    string isbn() const//只读不写
    {
    
    
        return bookNo;
    }

private:
    string bookNo;
    int units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;

};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
    
    
    return lhs.isbn() == rhs.isbn();
}

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.units_sold >> item.sellingprice >> item.saleprice;
    return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
    
    
    os << item.isbn() << " " << item.units_sold << " " << item.sellingprice << " " << item.saleprice << " " << item.discount;
    return os;
}



main.cpp

#include<iostream>
using namespace std;
#include"Sales_data.h"


void test1()
{
    
    
	Sales_data item1;
	print(cout, item1);
	cout << endl;

	Sales_data item2("xxxxxxxx");
	print(cout, item2);
	cout << endl;

	Sales_data item3("xxxxxxxx", 2, 3, 4);
	print(cout, item3);
	cout << endl;

	Sales_data item4(cin);
	print(cout, item4);
	cout << endl;
}

int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

7.12

同上,上面没有定义在外部

7.13

Sales_data.h

同上

main.cpp

#include<iostream>
using namespace std;
#include"Sales_data.h"


int main()
{
    
    
	Sales_data total(cin);
	Sales_data trans;

	cout << "请输入:(ISBN,销售量,原价,实际售价)" << endl;

	if (cin)
	{
    
    
		while (read(cin,trans))
		{
    
    
			if (compareIsbn(total, trans))
			{
    
    
				total.combine(trans);
			}
			else
			{
    
    
				cout << "ISBN ,销售量, 原价,平均销售价格,折扣" << total << endl;
				total = trans;
			}
		}
		cout << "ISBN ,销售量, 原价,平均销售价格,折扣" << total << endl;
	}
	else
	{
    
    
		cout << "无数据" << endl;
	}


	system("pause");
	return 0;
}

7.14

就用普通的构造方式一样

7.15

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

class Person
{
    
    
public:
	Person(const string& n, const string& ad) :name(n), address(ad) {
    
    }
public:
	string getname()const
	{
    
    
		return name;
	}
	string getaddress()const
	{
    
    
		return address;
	}

private:
	string name;
	string address;
};

int main()
{
    
    

	system("pause");
	return 0;
}

7.26

1.内部声明+外部inline显式内联

2.直接内部定义,隐式内联

7.27

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

class Screen
{
    
    
public:
	typedef string::size_type pos;

	Screen() = default;

	Screen(pos wd, pos he)
	{
    
    
		width = wd;
		height = he;
	}

	Screen(pos wd, pos he, char c)
	{
    
    
		width = wd;
		height = he;
		contents.assign(wd * he, c);
	}

	Screen& set(char c)
	{
    
    
		contents[cursor] = c;
		return *this;
	}

	Screen& set(pos r, pos col, char c)
	{
    
    
		contents[r * width + col] = c;
		return *this;
	}

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

	Screen& display(ostream& os)
	{
    
    
		do_display(os);
		return *this;
	}

	const Screen& display(ostream& os)const
	{
    
    
		do_display(os);
		return *this;
	}

private:
	pos height = 0;
	pos width = 0;
	pos cursor = 0;
	string contents;//存放数据

	void do_display(ostream& os)const//p248页解释,是个建议,把他单独写出来
	{
    
    
		os << contents;
	}
};

int main()
{
    
    

	Screen myScreen(5, 5, 'x');
	myScreen.move(4, 0).set('#').display(cout);
	cout << endl;
	myScreen.display(cout);
	cout << endl;

	system("pause");
	return 0;
}

7.31

class Y;

class X
{
    
    
	Y* a;
};
class Y
{
    
    
	X* b;
};

7.32

class window_mgr
{
    
    
public:
	typedef vector<Screen>::size_type ScreenIndex;

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

private:
	vector<Screen>screens{
    
     Screen(24,80,' ') };

};

7.41

.h

#pragma once

#include<iostream>
#include<string>

class Sales_data
{
    
    
    friend istream& read(istream& is, Sales_data& item);
    friend ostream& print(ostream& os, const Sales_data& item);


public:
    Sales_data(const string& s, const int num, const double sell, double sale) :bookNo(s), units_sold(num), sellingprice(sell), saleprice(sale), discount(sale / sell) {
    
     cout << "1" << endl; }
    Sales_data() :Sales_data(" ", 0, 0, 0) {
    
     cout << "2" << endl; }
    Sales_data(const string& s) :Sales_data(s, 0, 0, 0) {
    
     cout << "3" << endl; }
    
    Sales_data(istream& is):Sales_data()
    {
    
    
        read(is, *this);
        cout << "4" << endl;
    }
public:
    Sales_data& combine(const Sales_data& rhs)
    {
    
    
        units_sold += rhs.units_sold;
        saleprice = (rhs.saleprice * rhs.units_sold + saleprice * units_sold) / (rhs.units_sold + units_sold);
        sellingprice = (rhs.sellingprice * rhs.units_sold + sellingprice * units_sold) / (rhs.units_sold + units_sold);
        if (sellingprice != 0)
        {
    
    
            discount = saleprice / sellingprice;
        }
        return *this;
    }

    string isbn() const//只读不写
    {
    
    
        return bookNo;
    }
    
    double avg_price()const
    {
    
    
        if (units_sold)
        {
    
    
            return sellingprice / units_sold;
        }
        else
        {
    
    
            return 0;
        }
    }
private:
    string bookNo;
    int units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;

};

inline bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
    
    
    return lhs.isbn() == rhs.isbn();
}

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.units_sold >> item.sellingprice >> item.saleprice;
    return is;
}
ostream& print(ostream& os, const Sales_data& item)
{
    
    
    os << item.isbn() << " " << item.units_sold << " " << item.sellingprice << " " << item.saleprice << " " << item.discount;
    return os;
}



.c

#include<iostream>
using namespace std;
#include"Sales_data.h"


int main()
{
    
    
	Sales_data sd1("aaaaa", 11, 11, 11);
	Sales_data sd2;
	Sales_data sd3("aaa");
	Sales_data sd4(cin);

	system("pause");
	return 0;
}

7.53

#include <iostream>
using namespace std;

class Debug
{
    
    
public:
	constexpr Debug(bool b = 1) :hd(b), io(b), other(b){
    
     }
	constexpr Debug(bool a, bool b, bool c) : hd(a),io(b),other(c){
    
     }
	
	constexpr bool any() {
    
     return hd || io || other; }

	void set_hd(bool b) {
    
     hd = b; }
	void set_io(bool b) {
    
     io = b; }
	void set_other(bool b) {
    
     other = b; }

private:
	bool hd;
	bool io;
	bool other;
};

int main()
{
    
    

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_53953432/article/details/127769638