C++ Primer 练习9.51

练习 9.51:

#include<bits/stdc++.h>
using namespace std;

struct date {
	unsigned long y;
	unsigned long m;
	unsigned long d;
	date(string s) {
		string::size_type pos;
		string::size_type pos2;	
		pos = s.find_first_of(" /");
		string month = s.substr(0, pos);
		if (isdigit(month[0]))
			m = stoul(month);
		else
			m = 1;	//此处其实应该处理不同月份的字符串,偷懒没写
		pos2 = s.find_first_of(",/ ", ++pos);
		d = stoul(s.substr(pos, pos2 - pos));
		y = stoul(s.substr(++pos2));
	}
	void print(void)
	{
		cout << y << "年" << m << "月" << d << "日" << endl;
	}
};
int main(void)
{
	date a("January 1,1900");
	a.print();
	date b("1/1/1900");
	b.print();	
	date c("Jan 1 1900");
	c.print();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Dzx1025/article/details/106841288
今日推荐