赞
踩
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- unsigned long find_year_of_type1(const string &);
- int which_type(const string &);
- class DATE{
- public:
- static vector<string> all_month;
- static vector<string> all_abbr_month;
- DATE() = default;
- DATE(const string & date_input)
- {
-
- //There is three types of date.
- //type1:January 1,1900
- //type2:1/1/1990
- //type3:Jan 1 1900
- int type_id;
- type_id = which_type(date_input);
-
- // find month
- string::size_type pos,day_year_pos;
- int label = 0;
- if(type_id == 1)
- { int month_order = 1;
- for(vector<string>::iterator i = DATE::all_month.begin() ; i != DATE::all_month.end(); ++ i)
- {
- pos = date_input.find(*i);
- if(pos != string::npos)
- {
- //month = *i;
- label = 1;
- month = month_order;
- }//if
- ++ month_order;
- }//for
- if(label == 0)
- cout << "error month!!!" << endl;
- // find the day and year
-
- day = stoi(date_input.substr(date_input.find_first_of("0123456789")));
- if(day >31 || day < 0 )
- cout << "error day!!!" << endl;
-
- year = find_year_of_type1(date_input);
- if(year < 0)
- cout << "wrong year!!!" << endl;
-
- }//if(type_id == 1)
- else if(type_id == 2)
- {
- month = stoul(date_input);
- size_t day_beginning_position = 0;
- size_t day_ending_position = 0;
- size_t first_bar_position = date_input.find_first_of('/');
- size_t second_bar_position = date_input.find_last_of('/');
- day = stoul(date_input.substr(first_bar_position + 1));
- year = stoul(date_input.substr(second_bar_position + 1));
-
-
- }
- //type3:
- else {
- size_t first_backspace_position = date_input.find_first_of(' ');
- size_t second_backspace_position = date_input.find_last_of(' ');
- for(size_t i = 0; i != DATE::all_abbr_month.size() ; ++i)
- {
- if((date_input.find(all_abbr_month[i])) != string::npos)
- {
- month = i + 1;
- break;
- }
- }
-
-
- day = stoul(date_input.substr(date_input.find_first_of("0123456789")));
- if(day > 31 || day < 0)
- {
- cout << "error day!!!" << endl;
- }
- year = stoul(date_input.substr(second_backspace_position + 1));
- if(year < 0)
- cout << "error year!!!" << endl;
- }//type3
- }//DATE
- ostream & display_date(ostream &os)
- {
- os << year << " " << month << " " << day;
- return os;
- }
- private:
- unsigned year = 0;
- unsigned month = 0;
- unsigned day = 0;
- };
-
- vector<string> DATE::all_month{"January","February","March","April","May","June","July","August","September","October","November","December"};
- vector<string> DATE::all_abbr_month{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
- int sum_int(vector<string> &);
- int main()
- {
- string s = "January 1,1900";
- string s1 = "1/1/1990";
- string s2 = "Jan 1 1900";
- cout << which_type(s) << endl;
- cout << which_type(s1) << endl;
- cout << which_type(s2) << endl;
-
- DATE d1("January 23,2005");
- d1.display_date(cout) << endl;
- DATE d2("2/27/2020");
- d2.display_date(cout) << endl;
- DATE d3("Feb 3 2020");
- d3.display_date(cout) << endl;
-
-
- return 0;
- }
-
- int which_type(const string &s)
- { //test if date of "s" type1:
- for(int i = 0 ;i != s.size(); ++ i)
- {
- if(s[i] == ',')
- return 1;
- if(s[i] == '/')
- return 2;
- }
- return 3;
- }
-
- unsigned long find_year_of_type1(const string& date_input)
- {
- unsigned number_beginning_position = 0;
- unsigned past_number_ending_position = 0;//after the last number position
- size_t i = 0;
- for(;i != date_input.size() ; ++ i)
- {
- if(date_input[i] == ',')
- {
- number_beginning_position = i+1;
- break;
- }
- }
- unsigned long calculated_year = stoul(date_input.substr(number_beginning_position));
- return calculated_year;
- }
-
- r@r:~/computer_language/c++/9/9.5.5/ex$ ./123
- 1
- 2
- 3
- 2005 1 23
- 2020 2 27
- 2020 2 3
-
-
- 说明:前三个运行结果 1 2 3 是测试一个函数而已。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。