当前位置:   article > 正文

C++ Primer 课后练习9.51_c++primer9.51

c++primer9.51

9.51


  1. #include
  2. #include
  3. using namespace std;
  4. string oops("0123456789");
  5. class time{
  6. public:
  7. time(const string & ss);
  8. friend void display(time & t);
  9. private:
  10. unsigned year;
  11. unsigned month;
  12. unsigned day;
  13. };
  14. time::time(const string & ss)
  15. {
  16. int biaozhi = 0;
  17. if (ss.find(",") < string::npos)//若有,号
  18. {
  19. biaozhi = 0;
  20. }
  21. if (ss.find("/") != string::npos)//若有/号
  22. {
  23. biaozhi = 1;
  24. }
  25. if (ss.find(",") == string::npos && ss.find("/") == string::npos)
  26. {
  27. biaozhi = 2;
  28. }
  29. switch (biaozhi){
  30. case 0:
  31. if (ss.find("Jan") != string::npos) month = 1;
  32. if (ss.find("Feb") != string::npos) month = 2;
  33. if (ss.find("Mar") != string::npos) month = 3;
  34. if (ss.find("Apr") != string::npos) month = 4;
  35. if (ss.find("May") != string::npos) month = 5;
  36. if (ss.find("Jun") != string::npos) month = 6;
  37. if (ss.find("Jul") != string::npos) month = 7;
  38. if (ss.find("Aug") != string::npos) month = 8;
  39. if (ss.find("Sep") != string::npos) month = 9;
  40. if (ss.find("Oct") != string::npos) month = 10;
  41. if (ss.find("Nov") != string::npos) month = 11;
  42. if (ss.find("Dec") != string::npos) month = 12;
  43. day = stoi(ss.substr(ss.find_first_of(oops), ss.find_first_of(",") - ss.find_first_of(oops)));
  44. year = stoi(ss.substr(ss.find_first_of(",")+1));
  45. break;
  46. case 1:
  47. month = stoi(ss.substr(0, ss.find_first_of("/")));
  48. day = stoi(ss.substr(ss.find_first_of("/") + 1, ss.find_last_of("/") - ss.find_first_of("/")));
  49. year = stoi(ss.substr(ss.find_last_of("/") + 1));
  50. break;
  51. case 2:
  52. if (ss.find("Jan") != string::npos) month = 1;
  53. if (ss.find("Feb") != string::npos) month = 2;
  54. if (ss.find("Mar") != string::npos) month = 3;
  55. if (ss.find("Apr") != string::npos) month = 4;
  56. if (ss.find("May") != string::npos) month = 5;
  57. if (ss.find("Jun") != string::npos) month = 6;
  58. if (ss.find("Jul") != string::npos) month = 7;
  59. if (ss.find("Aug") != string::npos) month = 8;
  60. if (ss.find("Sep") != string::npos) month = 9;
  61. if (ss.find("Oct") != string::npos) month = 10;
  62. if (ss.find("Nov") != string::npos) month = 11;
  63. if (ss.find("Dec") != string::npos) month = 12;
  64. day = stoi(ss.substr(ss.find_first_of(oops), ss.find_last_of(" ") - ss.find_first_of(oops)));
  65. year = stoi(ss.substr(ss.find_last_of(" ") + 1));
  66. break;
  67. }
  68. }
  69. void display(time & t)
  70. {
  71. cout << t.year<< "年" << t.month << "月" << t.day <<"日"<< endl;
  72. }
  73. int main(void)
  74. {
  75. time time1("January 4,1205");
  76. display(time1);
  77. time time2("1/2/1900");
  78. display(time2);
  79. time time3("Feb 3 1900");
  80. display(time3);
  81. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/178328
推荐阅读
相关标签
  

闽ICP备14008679号