C++设计一个类,有三个unsigned成员,表示年月日.构造函数,接受表示日期的string参数,能够处理不同的数据格式

本文参考C++ Primer 第五版
博主的答案部分有误,因此进行了修改和润色,并加入了自己的注释便于理解.

#include<iostream>
#include<string>
using namespace std;

class Date
{
public:
	unsigned _year;
	unsigned _month;
	unsigned _day;
	void _show()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
	//构造函数
	Date(string);
};

Date::Date(string s)
{
	int flag = 0;
	string number = "0123456789/";
	string coma = ",";//逗号
	string kong = " ";//空格
	string month;
	unsigned pos, pos1 ,pos2;

	if ((pos = s.find_first_not_of(number)) == string::npos)//全部为number中的元素,则为1/1/1990形式
	{
		flag = 1;
	}
	if ((pos = s.find_first_of(coma)) != string::npos)//有逗号的形式January 1 ,1990
	{
		flag = 2;
	}

	switch (flag)
	{
	case 1:/*处理1/1/1991的格式*/
		pos1 = 0;
		pos1 = s.find_first_of("/", pos1);//获得/的位置
		_day = stoul(s.substr(0, pos1));//从位置0开始,截取pos1个字符作为日期

		pos2 = ++pos1;//pos2为第一个/后的第一个数字的位置
		_month = stoul(s.substr(pos2));//只截取pos2字符串的数字的部分直到不是数值为止,作为月份.stod的用法
		
		pos1 = s.find_first_of("/", pos1);//pos1为第2个/的位置,
		_year = stoul(s.substr(pos1 + 1));//一直到字符串末尾
		break;

	case 2:/*处理January 1,1900的格式*/

		pos1 = s.find_first_of(number);//找到第一个数字的位置,这里代表的是日期的第一个数的位置
		month = s.substr(0, pos1);//获得月份,注意这里获取的字符串末尾还会有一个空格字符

		_day = stoul(s.substr(pos1)); //截取_pos之后直到遇到不是数字的字符串作为日期.stod的用法
		if (month == "January ") _month = 1;
		if (month == "February ") _month = 2;
		if (month == "March ") _month = 3;
		if (month == "April ") _month = 4;
		if (month == "May ") _month = 5;
		if (month == "June ") _month = 6;
		if (month == "July ") _month = 7;
		if (month == "August ") _month = 8;
		if (month == "September ") _month = 9;
		if (month == "October ") _month = 10;
		if (month == "November ") _month = 11;
		if (month == "December ") _month = 12;


		pos1 = s.find_first_of(coma, pos1);//找到逗号的位置
		_year = stoul(s.substr(pos1 + 1));//逗号后面一位到末尾为年份

		break;

	case 0:/*处理Jan 1 1995的格式*/
		pos1;
		pos1 = s.find_first_of(number);//找到日期的第一个数字的位置
		month = s.substr(0, pos1);//获取月份
		_day = stoul(s.substr(pos1));//获取日期,从pos开始直到遇到第一个不为数字为止
		if (month == "Jan ") _month = 1;
		if (month == "Feb ") _month = 2;
		if (month == "Mar ") _month = 3;
		if (month == "Apr ") _month = 4;
		if (month == "May ") _month = 5;
		if (month == "Jun ") _month = 6;
		if (month == "Jul ") _month = 7;
		if (month == "Aug ") _month = 8;
		if (month == "Sep ") _month = 9;
		if (month == "Oct ") _month = 10;
		if (month == "Nov ") _month = 11;
		if (month == "Dec ") _month = 12;

		
		pos1 = s.find_first_of(kong, pos1);//找到空字符的位置
		_year = stoul(s.substr(pos1 + 1));//空字符后面一位,到末尾
		break;
	}
}

int main()
{
	Date _today("28/2/2017");
	_today._show();

	Date _tomorrow("January 22,1995");
	_tomorrow._show();

	Date _2tomorrow("Jan 11 1995");
	_2tomorrow._show();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值