C++primer第5版课后练习习题答案9.51

#include <iostream>
#include <vector>
using namespace std;
unsigned getmonth(const string & smonth);
class cdate
{
public :
	friend void print_cdate(cdate & cd)
	{
		cout<<cd.year<<"年"<<cd.month<<"月"<<cd.day<<"日"<<endl;
	}

	cdate()=default;
	cdate (string & sday);
private:
	unsigned year;
	unsigned month;
	unsigned day;

};
cdate::cdate (string & sday)
{
	year=stoi(sday.substr(sday.size()-4,4));
	string::size_type pos=0;
	pos=sday.find_first_of(" /");
	month=getmonth(sday.substr(0,pos));
	pos++;
	day=stoi(sday.substr(pos,sday.find_first_not_of("0123456789",pos)-pos));

}
unsigned getmonth(const string & smonth)
{
	vector<string> vmonth = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	vector<string> vnmonth={"1","2","3","4","5","6","7","8","9","10","11","12"};
	//vector<string> vsmonth={"Jan","Fer","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
	for (decltype(vmonth.size()) i = 0; i != 12; ++i)
	{
		if (vmonth[i].compare(0, 3, smonth) == 0 || vmonth[i].compare(smonth) == 0||vnmonth[i].compare(smonth)==0)
		{
			return i + 1;
		}
	}
	return 0;
}
int main()
{
	string str1 = "1/2/1990", str2 = "Apr 3 1989", str3 = "October 4, 1980";
	cdate mydate(str1);
	print_cdate(mydate);
	cdate mydate2(str2);
	print_cdate(mydate2);
	cdate mydate3(str3);
	print_cdate(mydate3);



	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值