C + + primer (Fifth Edition) 14.8.2 -- 14.9.1 exercises

14.42 

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
using namespace std::placeholders;

bool divByAll(const vector<int> &ivec, int dividend)
{
	return !count_if(ivec.begin(), ivec.end(), bind(modulus<int>(), _1, dividend));
	
}

int main()
{
	vector<int> ivec = {800, 1985, 2017, 6};
	vector<int> ivec2 = {2, 4, 6, 8};
	vector<string> svec = {"pooh", "pooh", "dog"};
	
	auto wc = count_if(ivec.begin(), ivec.end(), bind(greater<int>(), _1, 1024));
	cout << wc << endl;

	auto iter = find_if(svec.begin(), svec.end(), bind(not_equal_to<string>(), _1, "pooh"));
	cout << *iter << endl;

	transform(ivec.begin(), ivec.end(), ivec.begin(), bind(multiplies<int>(), _1, 2));
	for (auto &i : ivec)
		cout << i << " ";
	cout << endl;

	cout << divByAll(ivec, 3) << " " << divByAll(ivec2, 2) << endl;
	
	return 0;
}

14.43 see divByAll function in the previous question.

14.44

#include <iostream>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

map<string, function<int(int, int)>> binops = {
	{"+", plus<int>()},
	{"-", minus<int>()},
	{"*", multiplies<int>()},
	{"/", divides<int>()},
	{"%", modulus<int>()},
};

int main()
{
	int lhs, rhs;
	string op;
	cin >> lhs >> op >> rhs;
	cout << "result: " << endl;
	cout << binops[op](lhs, rhs) << endl;
	return 0;
}

14.45    

operator string() const { return bookNo; }
operator double() const { return revenue; }

14.46 the above two types of conversion should not be defined. Only the combination of bookno, units "sold and revenue can make sense; it should be defined as explicit to prevent default conversion under the condition of tacit writing.

14.47 the first converts the object to const int; the second converts the object to int without changing the contents of the object to be converted.

14.48 defines the Date class. It can define a bool type conversion operator to check whether the Date is valid. It should be declared explicit to prevent errors caused by automatic conversion and can be used in condition judgment.

14.49    

#include <iostream>
#include <vector>

using namespace std;

class Date {
friend ostream &operator<<(ostream&, const Date&);
friend istream &operator>>(istream&, Date&);
public:
	Date() = default;
	Date(int y, int m, int d): year(y), month(m), day(d) { }
	explicit operator bool();
	bool isLeapYear() { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }
private:
	int year, month, day;	
};

ostream &operator<<(ostream &os, const Date &dt)
{
	os << "year:" << dt.year << " month:" << dt.month << " day:" << dt.day;
	return os;
}

istream &operator>>(istream &is, Date &dt)
{
	is >> dt.year >> dt.month >> dt.day;
	if (!is)
 		dt = Date();
	return is;
}

Date::operator bool()  
{
	vector<vector<int>> days_per_month = {
	    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
		{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
										 
	return (1<=month && month <= 12) && 
            (1<=day && day<=days_per_month[isLeapYear() ? 1:0][month-1]);
}

int main()
{
	Date d;
	cin >> d;
	
	if (!d)
		cout << "illegal date" << endl;
	else
		cout << "legal date" << endl;
	
	return 0;
}

 

Posted by stylusrose on Fri, 01 Nov 2019 13:25:19 -0700