C++ Primer 课后练习9.51

9.51
#include<iostream>
#include<string>
using namespace std;
string oops("0123456789");
class time{
public:
	time(const string &  ss);
	friend void display(time & t);

private:
	unsigned year;
	unsigned month;
	unsigned day;
};
time::time(const string &  ss)
{
	int biaozhi = 0;
	if (ss.find(",") < string::npos)//若有,号
	{
		biaozhi = 0;
	}
	if (ss.find("/") != string::npos)
	{
		biaozhi = 1;
	}
	
	if (ss.find(",") == string::npos && ss.find("/") == string::npos)
	{
		biaozhi = 2;
	}

	switch (biaozhi){
	case 0:
		if (ss.find("Jan") != string::npos) month = 1;
		if (ss.find("Feb") != string::npos) month = 2;
		if (ss.find("Mar") != string::npos) month = 3;
		if (ss.find("Apr") != string::npos) month = 4;
		if (ss.find("May") != string::npos) month = 5;
		if (ss.find("Jun") != string::npos) month = 6;
		if (ss.find("Jul") != string::npos) month = 7;
		if (ss.find("Aug") != string::npos) month = 8;
		if (ss.find("Sep") != string::npos) month = 9;
		if (ss.find("Oct") != string::npos) month = 10;
		if (ss.find("Nov") != string::npos) month = 11;
		if (ss.find("Dec") != string::npos) month = 12;
		day = stoi(ss.substr(ss.find_first_of(oops), ss.find_first_of(",") - ss.find_first_of(oops)));
		year = stoi(ss.substr(ss.find_first_of(",")+1));
		break;

	case 1:
		month = stoi(ss.substr(0, ss.find_first_of("/")));
		day = stoi(ss.substr(ss.find_first_of("/") + 1, ss.find_last_of("/") - ss.find_first_of("/")));
		year = stoi(ss.substr(ss.find_last_of("/") + 1));
		break;

	case 2:
		if (ss.find("Jan") != string::npos) month = 1;
		if (ss.find("Feb") != string::npos) month = 2;
		if (ss.find("Mar") != string::npos) month = 3;
		if (ss.find("Apr") != string::npos) month = 4;
		if (ss.find("May") != string::npos) month = 5;
		if (ss.find("Jun") != string::npos) month = 6;
		if (ss.find("Jul") != string::npos) month = 7;
		if (ss.find("Aug") != string::npos) month = 8;
		if (ss.find("Sep") != string::npos) month = 9;
		if (ss.find("Oct") != string::npos) month = 10;
		if (ss.find("Nov") != string::npos) month = 11;
		if (ss.find("Dec") != string::npos) month = 12;
	
		day = stoi(ss.substr(ss.find_first_of(oops), ss.find_last_of(" ") - ss.find_first_of(oops)));
		year = stoi(ss.substr(ss.find_last_of(" ") + 1));
		break;
	
	}
}
void display(time & t)
{
	cout << t.year<< "年" << t.month << "月" << t.day <<"日"<< endl;
}
int main(void)
{
	time time1("January 4,1205");
	display(time1);
	time time2("1/2/1900");
	display(time2);
	time time3("Feb 3 1900");
	display(time3);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值