C++ Primer第五版_第九章习题答案(51~52)

练习9.51

设计一个类,它有三个unsigned成员,分别表示年、月和日。为其编写构造函数,接受一个表示日期的string参数。你的构造函数应该能处理不同的数据格式,如January 1,1900、1/1/1990、Jan 1 1900 等。
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class date
{
private:
	unsigned year, month, day;

public:
	date(const string& s)
	{
		if (s.find_first_of("/") != string::npos)
			convert1(s);
		else if (s.find_first_of(",") != string::npos)
			convert2(s);
		else if (s.find_first_of(" ") != string::npos)
			convert3(s);
		else
			year = 1900, month = 1, day = 1;
	}

	void print()
	{
		cout << "day:" << day << " " << "month: " << month << " " << "year: " << year << endl;
	}

private:
	void convert1(const string& s)
	{
		day = stoi(s.substr(0, s.find_first_of("/")));
		month = stoi(s.substr(s.find_first_of("/") + 1, s.find_last_of("/") - s.find_first_of("/")));
		year = stoi(s.substr(s.find_last_of("/") + 1, 4));
	}
	void convert2(const string& s)
	{
		convert_month(s);
		day = stoi(s.substr(s.find_first_of("123456789"), s.find_first_of(",") - s.find_first_of("123456789")));
		year = stoi(s.substr(s.find_last_of(,) + 1, 4));
	}
	void convert3(const string& s)
	{
		convert_month(s);
		day = stoi(s.substr(s.find_first_of("123456789"), s.find_first_of(" ") - s.find_first_of("123456789")));
		year = stoi(s.substr(s.find_last_of( ) + 1, 4));
	}

	void convert_month(const string& s)
	{
		if (s.find("Jan") < s.size())  month = 1;
		if (s.find("Feb") < s.size())  month = 2;
		if (s.find("Mar") < s.size())  month = 3;
		if (s.find("Apr") < s.size())  month = 4;
		if (s.find("May") < s.size())  month = 5;
		if (s.find("Jun") < s.size())  month = 6;
		if (s.find("Jul") < s.size())  month = 7;
		if (s.find("Aug") < s.size())  month = 8;
		if (s.find("Sep") < s.size())  month = 9;
		if (s.find("Oct") < s.size())  month = 10;
		if (s.find("Nov") < s.size())  month = 11;
		if (s.find("Dec") < s.size())  month = 12;
	}
};

int main()
{
	date d1("9/5/1990");
	date d2("January 7,1970");
	date d3("Jan 11 1942");

	d1.print();
	d2.print();
	d3.print();

	return 0;
}

练习9.52

使用stack处理括号化的表达式。当你看到一个左括号,将其记录下来。当你在一个左括号之后看到一个右括号,从stack中pop对象,直至遇到左括号,将左括号也一起弹出栈。然后将一个值(括号内的运算结果)push到栈中,表示一个括号化的(子)表达式已经处理完毕,被其运算结果所替代。
#include <iostream>
#include <string>
#include <stack>
#include <cctype>

using namespace std;

string calc(string l, string r, string op)
{
	string s;
	if (op == "-")
		s = to_string(stoi(l) - stoi(r));
	return s;
}

int main()
{
	string s("1+2*(7-4)");
	stack<string> stack;
	for (auto iter = s.begin(); iter != s.end();)
	{
		if (*iter == ()
		{
			stack.push(string(1, *iter));
			++iter;
			while (*iter != ))
			{
				stack.push(string(1, *iter));
				++iter;
			}
		}
		else if (*iter == ))
		{
			string r = stack.top(); stack.pop();
			string op = stack.top(); stack.pop();
			string l = stack.top(); stack.pop();
			stack.pop(); // ( 弹出
			stack.push(calc(l, r, op));
			++iter;
		}
		else
		{
			++iter;
		}

	}

	while (!stack.empty())
	{
		cout << stack.top() << endl;
		stack.pop();
	}
	return 0;
}

这道题可以延伸为逆波兰求值,以及中缀转后缀表达式。

经验分享 程序员 微信小程序 职场和发展