"C + + Primer Fifth Edition" Exercise 9.51 Reference answer

Source: Internet
Author: User

Date.h#include <map> #include <string> #include <vector>using namespace Std;struct date{         explicit Date (const string & info) {//Detect input format, attempt to initialize, errorinit  if failed            if (Mymap.empty ()) {               Initmap ();            }            if (!checkinfoformat (info) ) {               ErrorInit ();                return;            }            String Monthstr=info.substr (0,info.find_first_of (",/"));           & nbsp; 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 () {//check date is legal              if (year>10000| | month>12| | DAY&GT;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)//detect leap years            {return ((year% 4 = = 0) && ( Year% 100 = 0)) | | (Year% 400 = = 0)) ? true:false;          }&NBsp;          Static map<string,unsigned> mymap;            void Errorinit ()             {                year=1970;                month=1;                 day=1;           }                      bool Trytoinit (const vector<string> & Data)//attempt to initialize            {                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)//detection format is correct            {               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,u                  Nsigned> ("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> ("June", 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> ("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)); Application of}//Table driving method

Unify the time to 1970 1 1 when any special case is encountered

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.