首页 > 代码库 > 《C++ Primer 第五版》练习9.51参考答案
《C++ Primer 第五版》练习9.51参考答案
//Date.h #include <map> #include <string> #include <vector> using namespace std; struct Date { explicit Date(const string & info){//检测输入格式,尝试初始化,若失败则进行errorInit if(mymap.empty()){ initMap(); } if(!checkInfoFormat(info)){ errorInit(); return; } string monthStr=info.substr(0,info.find_first_of(", /")); string dayStr=info.substr(info.find_first_of(", /")+1,info.find_last_of(", /")-info.find_first_of(", /")-1); string yearStr=info.substr(info.find_last_of(", /")+1,info.size()); if(!tryToInit({yearStr,monthStr,dayStr})){ errorInit(); } cout<<"year: "<<year<<endl; cout<<"month: "<<month<<endl; cout<<"day: "<<day<<endl; } unsigned year; unsigned month; unsigned day; static void initMap(); private: bool checkDate(){//检查日期是否合法 if(year>10000||month>12||day>31) return false; if(month==4||month==6||month==9||month==11){ if(day>30) return false; } if(month==2){ if(checkYear(year)&&day>29){ return false; } if(!checkYear(year)&&day>28) return false; } return true; } bool checkYear(unsigned year)//检测闰年 { return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? true : false; } static map<string,unsigned> mymap; void errorInit() { year=1970; month=1; day=1; } bool tryToInit(const vector<string> & data)//尝试初始化 { try{ year=stoul(data[0]); day=stoul(data[2]); }catch(invalid_argument & error){ return false; } try{ month=stoul(data[1]); }catch(invalid_argument & error){ if(mymap.find(data[1])==mymap.end()) return false; month=mymap[data[1]]; } return checkDate(); } bool checkInfoFormat(const string & info)//检测格式是否正确 { if(info.empty()) return false; if(info.find_first_of(", /")==info.size()) return false; if(info.find_first_of(", /")==info.find_last_of(", /")) return false; return true; } };
//Date.cpp map<string,unsigned> Date::mymap; void Date::initMap(){ mymap.insert(make_pair<string,unsigned>("January",1)); mymap.insert(make_pair<string,unsigned>("Jan",1)); mymap.insert(make_pair<string,unsigned>("February",2)); mymap.insert(make_pair<string,unsigned>("Feb",2)); mymap.insert(make_pair<string,unsigned>("March",3)); mymap.insert(make_pair<string,unsigned>("Mar",3)); mymap.insert(make_pair<string,unsigned>("April",4)); mymap.insert(make_pair<string,unsigned>("Apr",4)); mymap.insert(make_pair<string,unsigned>("May",5)); mymap.insert(make_pair<string,unsigned>("June",6)); mymap.insert(make_pair<string,unsigned>("Jun",6)); mymap.insert(make_pair<string,unsigned>("July",7)); mymap.insert(make_pair<string,unsigned>("Jul",7)); mymap.insert(make_pair<string,unsigned>("August",8)); mymap.insert(make_pair<string,unsigned>("Aug",8)); mymap.insert(make_pair<string,unsigned>("September",9)); mymap.insert(make_pair<string,unsigned>("Sep",9)); mymap.insert(make_pair<string,unsigned>("October",10)); mymap.insert(make_pair<string,unsigned>("Oct",10)); mymap.insert(make_pair<string,unsigned>("November",11)); mymap.insert(make_pair<string,unsigned>("Nov",11)); mymap.insert(make_pair<string,unsigned>("December",12)); mymap.insert(make_pair<string,unsigned>("Dec",12)); } //表驱动法的应用
当遇到任何特殊情况时,统一将时间设为1970 1 1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。