1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /*
- * @Description: cpp format/regex test
- * @Author: yct
- * @Date: 2020-03-30 11:23:51
- * @LastEditTime: 2020-03-30 12:20:33
- * @LastEditors: yct
- */
- #define __STDC_WANT_LIB_EXT1__ 1
- #include <iostream>
- #include <ctime>
- #include <string>
- #include <regex>
- using namespace std;
- class Date
- {
- private:
- string time1;
- public:
- void show1() { cout << "一年中的第"; }
- Date(string a) { time1 = a; }
- Date() { time1 = ""; }
- void Show() { cout << time1 << endl; }
- void Shownow();
- };
- void Input();
- int main(void)
- {
- Input();
- }
- void Date::Shownow()
- {
- std::time_t now = std::time(nullptr);
- char dt[26] = {0};
- sprintf(dt, "%s", ctime(&now));
- Date a(dt);
- cout << "当前时间: ";
- a.Show();
- }
- void Input()
- {
- cout << "按如下格式输入日期: DDD/YYYY(一年中第几天), MM/DD/YY, June/DD/YYYY"<<endl;
- regex reg1("(\\d{3})(.)(\\d{4,5})");
- regex reg2("(\\d{2}(.)\\d{2}(.)(\\d{2}))");
- regex reg3("([A-Za-z]{3,9})(.)(\\d{2})(.)(\\d{4,5})");
- string k;
- int i = 0, k1 = 0, k2 = 0;
- int p1, p2 = 0, p3 = 0;
- while (i != 1)
- {
- cin >> k;
- if (regex_search(k, reg1))
- {
- string o1 = k.substr(0, 3);
- string o2 = k.substr(4, 4);
- p1 = stoi(o1);
- p2 = stoi(o2);
- cout << p1 << " " << p2;
- break;
- }
- if (regex_search(k, reg2))
- {
- string o1 = k.substr(0, 2);
- string o2 = k.substr(3, 2);
- string o3 = k.substr(6, 4);
- p1 = stoi(o1);
- p2 = stoi(o2);
- p3 = stoi(o3);
- cout << p1 << " " << p2 << " " << p3;
- break;
- }
- if (regex_search(k, reg3))
- {
- string o1 = k.substr(0, k.length()-8);
- string o2 = k.substr(k.length()-7, 2);
- string o3 = k.substr(k.length()-4, 4);
- p2 = stoi(o2);
- p3 = stoi(o3);
- cout << o1 << " " << p2 << " " << p3;
- break;
- }
- cout<<"请重新输入"<<endl;
- }
- }
|