《C++ Primer 第五版》練習9.51參考答案

來源:互聯網
上載者:User

標籤:c++   c++ primer   習題答案   

//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.cppmap<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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.