hw.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * @Description: cpp format/regex test
  3. * @Author: yct
  4. * @Date: 2020-03-30 11:23:51
  5. * @LastEditTime: 2020-03-30 12:20:33
  6. * @LastEditors: yct
  7. */
  8. #define __STDC_WANT_LIB_EXT1__ 1
  9. #include <iostream>
  10. #include <ctime>
  11. #include <string>
  12. #include <regex>
  13. using namespace std;
  14. class Date
  15. {
  16. private:
  17. string time1;
  18. public:
  19. void show1() { cout << "一年中的第"; }
  20. Date(string a) { time1 = a; }
  21. Date() { time1 = ""; }
  22. void Show() { cout << time1 << endl; }
  23. void Shownow();
  24. };
  25. void Input();
  26. int main(void)
  27. {
  28. Input();
  29. }
  30. void Date::Shownow()
  31. {
  32. std::time_t now = std::time(nullptr);
  33. char dt[26] = {0};
  34. sprintf(dt, "%s", ctime(&now));
  35. Date a(dt);
  36. cout << "当前时间: ";
  37. a.Show();
  38. }
  39. void Input()
  40. {
  41. cout << "按如下格式输入日期: DDD/YYYY(一年中第几天), MM/DD/YY, June/DD/YYYY"<<endl;
  42. regex reg1("(\\d{3})(.)(\\d{4,5})");
  43. regex reg2("(\\d{2}(.)\\d{2}(.)(\\d{2}))");
  44. regex reg3("([A-Za-z]{3,9})(.)(\\d{2})(.)(\\d{4,5})");
  45. string k;
  46. int i = 0, k1 = 0, k2 = 0;
  47. int p1, p2 = 0, p3 = 0;
  48. while (i != 1)
  49. {
  50. cin >> k;
  51. if (regex_search(k, reg1))
  52. {
  53. string o1 = k.substr(0, 3);
  54. string o2 = k.substr(4, 4);
  55. p1 = stoi(o1);
  56. p2 = stoi(o2);
  57. cout << p1 << " " << p2;
  58. break;
  59. }
  60. if (regex_search(k, reg2))
  61. {
  62. string o1 = k.substr(0, 2);
  63. string o2 = k.substr(3, 2);
  64. string o3 = k.substr(6, 4);
  65. p1 = stoi(o1);
  66. p2 = stoi(o2);
  67. p3 = stoi(o3);
  68. cout << p1 << " " << p2 << " " << p3;
  69. break;
  70. }
  71. if (regex_search(k, reg3))
  72. {
  73. string o1 = k.substr(0, k.length()-8);
  74. string o2 = k.substr(k.length()-7, 2);
  75. string o3 = k.substr(k.length()-4, 4);
  76. p2 = stoi(o2);
  77. p3 = stoi(o3);
  78. cout << o1 << " " << p2 << " " << p3;
  79. break;
  80. }
  81. cout<<"请重新输入"<<endl;
  82. }
  83. }