第九章 习题9-51-习题9-60(mark)

mark一下,书的两段看得有点迷迷糊糊的。

习题9-51

照抄吧,,,有点不是很明白想干什么

参考https://github.com/huangmingchuan/Cpp_Primer_Answers/blob/master/ch09/exercise9_51.cpp

#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

这一题,不知道要干嘛。。。,看了下答案大致明白了。

#include <stack>
using std::stack;

#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

int main()
{
	auto& expr = "This is (Mooophy(awesome)((((wooooooooo))))) and (ocxs) over";
	auto repl = '#';
	auto seen = 0;

	stack<char> stk;

	for (auto c : expr) {
		stk.push(c);
		if (c == '(') ++seen;   // open
		if (seen && c == ')') { // pop elements down to the stack
			while (stk.top() != '(') stk.pop();
			stk.pop();      // including the open parenthesis
			stk.push(repl); // push a value indicate it was replaced
			--seen;         // close
		}
	}

	// Test
	string output;
	for (; !stk.empty(); stk.pop()) output.insert(output.begin(), stk.top());
	cout << output << endl; // "This is # and # over"
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值