C++Primer 第9章51题

出错好几次终于搞定了,发个文章鼓励下自己,加油 得意
#include<string>
#include<iostream>
using std::string; using std::stoul;
using std::cout; 

#include<string>
using std::string; using std::stoul;
class Riqi {
private:
	unsigned long year, month, date;
public:
	Riqi(): year(0), date(0) {}
	Riqi(string  s) {
		if (!s.empty())
		{
			auto pos = s.find_last_of("1234567890");
			year = stoul(s.substr(pos - 3));
			s.erase(pos - 3);
			if (s.find_first_of("QWERTYUIOPASDFGHJKLZXCVBNM") != string::npos) {
				date = stoul(s.substr(s.find_first_of("1234567890")));
				string str(s, 0, 3);
				switch (s[0]) {
				case 'J':
					if (str == "Jan")
						month = 1;
					else if (str == "Jun")
						month = 6;
					else
						month = 7;
					break;
				case'F':
					month = 2;
					break;
				case'M':
					if (str == "Mar")
						month = 3;
					else
						month = 5;
					break;
				case'A':
					if (str == "Apr")
						month = 4;
					else
						month = 8;
					break;
				case'S':
					month = 9;
					break;
				case'O':
					month = 10;
					break;
				case'N':
					month = 11;
					break;
				case'D':
					month = 12;
					break;
				}
			}
			else {
				month = stoul(s);
				if (month > 9)
					date = stoul(s.substr(3));
				else
					date = stoul(s.substr(2));
			}
		}
	}
	void Print() {
		cout << year << " " << month << " " << date << "\n";
	}
};

int main()
{
	//*
	Riqi v1("January 1,1900"), v2("Feb 20,1909"), v3("23/23/1999"), v4("1 2 1987");
	v1.Print();
	v2.Print();
	v3.Print();
	v4.Print();
	//*/
	

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值