当前位置:   article > 正文

c++ primer 5 th p328 9.5.5节 练习9.51 自己编写的答案_c++primer9.51

c++primer9.51
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. unsigned long find_year_of_type1(const string &);
  6. int which_type(const string &);
  7. class DATE{
  8. public:
  9. static vector<string> all_month;
  10. static vector<string> all_abbr_month;
  11. DATE() = default;
  12. DATE(const string & date_input)
  13. {
  14. //There is three types of date.
  15. //type1:January 1,1900
  16. //type2:1/1/1990
  17. //type3:Jan 1 1900
  18. int type_id;
  19. type_id = which_type(date_input);
  20. // find month
  21. string::size_type pos,day_year_pos;
  22. int label = 0;
  23. if(type_id == 1)
  24. { int month_order = 1;
  25. for(vector<string>::iterator i = DATE::all_month.begin() ; i != DATE::all_month.end(); ++ i)
  26. {
  27. pos = date_input.find(*i);
  28. if(pos != string::npos)
  29. {
  30. //month = *i;
  31. label = 1;
  32. month = month_order;
  33. }//if
  34. ++ month_order;
  35. }//for
  36. if(label == 0)
  37. cout << "error month!!!" << endl;
  38. // find the day and year
  39. day = stoi(date_input.substr(date_input.find_first_of("0123456789")));
  40. if(day >31 || day < 0 )
  41. cout << "error day!!!" << endl;
  42. year = find_year_of_type1(date_input);
  43. if(year < 0)
  44. cout << "wrong year!!!" << endl;
  45. }//if(type_id == 1)
  46. else if(type_id == 2)
  47. {
  48. month = stoul(date_input);
  49. size_t day_beginning_position = 0;
  50. size_t day_ending_position = 0;
  51. size_t first_bar_position = date_input.find_first_of('/');
  52. size_t second_bar_position = date_input.find_last_of('/');
  53. day = stoul(date_input.substr(first_bar_position + 1));
  54. year = stoul(date_input.substr(second_bar_position + 1));
  55. }
  56. //type3:
  57. else {
  58. size_t first_backspace_position = date_input.find_first_of(' ');
  59. size_t second_backspace_position = date_input.find_last_of(' ');
  60. for(size_t i = 0; i != DATE::all_abbr_month.size() ; ++i)
  61. {
  62. if((date_input.find(all_abbr_month[i])) != string::npos)
  63. {
  64. month = i + 1;
  65. break;
  66. }
  67. }
  68. day = stoul(date_input.substr(date_input.find_first_of("0123456789")));
  69. if(day > 31 || day < 0)
  70. {
  71. cout << "error day!!!" << endl;
  72. }
  73. year = stoul(date_input.substr(second_backspace_position + 1));
  74. if(year < 0)
  75. cout << "error year!!!" << endl;
  76. }//type3
  77. }//DATE
  78. ostream & display_date(ostream &os)
  79. {
  80. os << year << " " << month << " " << day;
  81. return os;
  82. }
  83. private:
  84. unsigned year = 0;
  85. unsigned month = 0;
  86. unsigned day = 0;
  87. };
  88. vector<string> DATE::all_month{"January","February","March","April","May","June","July","August","September","October","November","December"};
  89. vector<string> DATE::all_abbr_month{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  90. int sum_int(vector<string> &);
  91. int main()
  92. {
  93. string s = "January 1,1900";
  94. string s1 = "1/1/1990";
  95. string s2 = "Jan 1 1900";
  96. cout << which_type(s) << endl;
  97. cout << which_type(s1) << endl;
  98. cout << which_type(s2) << endl;
  99. DATE d1("January 23,2005");
  100. d1.display_date(cout) << endl;
  101. DATE d2("2/27/2020");
  102. d2.display_date(cout) << endl;
  103. DATE d3("Feb 3 2020");
  104. d3.display_date(cout) << endl;
  105. return 0;
  106. }
  107. int which_type(const string &s)
  108. { //test if date of "s" type1:
  109. for(int i = 0 ;i != s.size(); ++ i)
  110. {
  111. if(s[i] == ',')
  112. return 1;
  113. if(s[i] == '/')
  114. return 2;
  115. }
  116. return 3;
  117. }
  118. unsigned long find_year_of_type1(const string& date_input)
  119. {
  120. unsigned number_beginning_position = 0;
  121. unsigned past_number_ending_position = 0;//after the last number position
  122. size_t i = 0;
  123. for(;i != date_input.size() ; ++ i)
  124. {
  125. if(date_input[i] == ',')
  126. {
  127. number_beginning_position = i+1;
  128. break;
  129. }
  130. }
  131. unsigned long calculated_year = stoul(date_input.substr(number_beginning_position));
  132. return calculated_year;
  133. }
  1. r@r:~/computer_language/c++/9/9.5.5/ex$ ./123
  2. 1
  3. 2
  4. 3
  5. 2005 1 23
  6. 2020 2 27
  7. 2020 2 3
  8. 说明:前三个运行结果 1 2 3 是测试一个函数而已。

 

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

闽ICP备14008679号