1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include<iostream> #include<string>
using namespace std;
const string mm[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec" };
int findmonth(const string& mon) { int pos; for (int i = 0; i < 12; ++i) { if ((pos = mon.find(mm[i])) != string::npos) { return i + 1; } } } <!-- more -->
class Date { public: Date(const string& str) { string data_str = str; string::size_type index1 = 0; string::size_type index2 = 0; if (str.find(',') != string::npos) { index1 = str.find(' '); index2 = str.find(',', index1 + 1); string mon = str.substr(0, index1); month = findmonth(mon); day = stoi(str.substr(index1 + 1, index2-index1-1)); year = stoi(str.substr(index2 + 1)); } else if (str.find('/') != string::npos) { index1 = str.find_first_of('/'); index2 = str.find_first_of('/', index1 + 1); year = stoi(str.substr(index2 + 1)); month = stoi(str.substr(index1 + 1, index2 - 1 - index1)); day = stoi(str.substr(0, index1)); } else { index1 = str.find_first_of(' '); index2 = str.find_first_of(' ', index1 + 1); string mon = str.substr(0, index1); month = findmonth(mon); day = stoi(str.substr(index1 + 1, index2 - 1 - index1)); year = stoi(str.substr(index2 + 1)); } } void getdate() { cout << "Year:" << year << " " << "Month:" << month << " " << "Day:" << day << endl; } private: unsigned year, month, day; };
int main() { string d1 = "May 5,2016", d2 = "9/9/1999", d3 = "Mar 11 2022"; Date a(d1), b(d2), c(d3); a.getdate(); b.getdate(); c.getdate();
system("pause"); return 0; }
|