赞
踩
9.51
- #include
-
-
-
-
- #include
-
-
-
-
- using namespace std;
- string oops("0123456789");
- class time{
- public:
- time(const string & ss);
- friend void display(time & t);
-
- private:
- unsigned year;
- unsigned month;
- unsigned day;
- };
- time::time(const string & ss)
- {
- int biaozhi = 0;
- if (ss.find(",") < string::npos)//若有,号
- {
- biaozhi = 0;
- }
- if (ss.find("/") != string::npos)//若有/号
- {
- biaozhi = 1;
- }
-
- if (ss.find(",") == string::npos && ss.find("/") == string::npos)
- {
- biaozhi = 2;
- }
-
- switch (biaozhi){
- case 0:
- if (ss.find("Jan") != string::npos) month = 1;
- if (ss.find("Feb") != string::npos) month = 2;
- if (ss.find("Mar") != string::npos) month = 3;
- if (ss.find("Apr") != string::npos) month = 4;
- if (ss.find("May") != string::npos) month = 5;
- if (ss.find("Jun") != string::npos) month = 6;
- if (ss.find("Jul") != string::npos) month = 7;
- if (ss.find("Aug") != string::npos) month = 8;
- if (ss.find("Sep") != string::npos) month = 9;
- if (ss.find("Oct") != string::npos) month = 10;
- if (ss.find("Nov") != string::npos) month = 11;
- if (ss.find("Dec") != string::npos) month = 12;
- day = stoi(ss.substr(ss.find_first_of(oops), ss.find_first_of(",") - ss.find_first_of(oops)));
- year = stoi(ss.substr(ss.find_first_of(",")+1));
- break;
-
- case 1:
- month = stoi(ss.substr(0, ss.find_first_of("/")));
- day = stoi(ss.substr(ss.find_first_of("/") + 1, ss.find_last_of("/") - ss.find_first_of("/")));
- year = stoi(ss.substr(ss.find_last_of("/") + 1));
- break;
-
- case 2:
- if (ss.find("Jan") != string::npos) month = 1;
- if (ss.find("Feb") != string::npos) month = 2;
- if (ss.find("Mar") != string::npos) month = 3;
- if (ss.find("Apr") != string::npos) month = 4;
- if (ss.find("May") != string::npos) month = 5;
- if (ss.find("Jun") != string::npos) month = 6;
- if (ss.find("Jul") != string::npos) month = 7;
- if (ss.find("Aug") != string::npos) month = 8;
- if (ss.find("Sep") != string::npos) month = 9;
- if (ss.find("Oct") != string::npos) month = 10;
- if (ss.find("Nov") != string::npos) month = 11;
- if (ss.find("Dec") != string::npos) month = 12;
-
- day = stoi(ss.substr(ss.find_first_of(oops), ss.find_last_of(" ") - ss.find_first_of(oops)));
- year = stoi(ss.substr(ss.find_last_of(" ") + 1));
- break;
-
- }
- }
- void display(time & t)
- {
- cout << t.year<< "年" << t.month << "月" << t.day <<"日"<< endl;
- }
- int main(void)
- {
- time time1("January 4,1205");
- display(time1);
- time time2("1/2/1900");
- display(time2);
- time time3("Feb 3 1900");
- display(time3);
- }
-
-
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。