当前位置:   article > 正文

C++ Primer(第五版) 9.5.5--9.6节练习_#include #include using namespac

#include #include using namespace std; template

9.50

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<string> int_val = {"123", "456", "-789"};
  8. int sum1 = 0;
  9. for (auto v : int_val) {
  10. sum1 += stoi(v);
  11. }
  12. cout << "sum1 = " << sum1 << endl;
  13. vector<string> double_val = {"12.3", "-4.56", "+7.8e-2"};
  14. double sum2 = 0;
  15. for (auto v : double_val) {
  16. sum2 += stod(v);
  17. }
  18. cout << "sum2 = " << sum2 << endl;
  19. return 0;
  20. }

9.51

  1. #include <iostream>
  2. #include <string>
  3. #include <stdexcept>
  4. #include <vector>
  5. using namespace std;
  6. const vector<string> month_name1 = {"January", "February", "March",
  7. "April", "May", "June", "July", "August",
  8. "September", "October", "November", "December"};
  9. const vector<string> month_name2 = {"Jan", "Feb", "Mar",
  10. "Apr", "May", "Jun", "Jul", "Aug",
  11. "Sept", "Oct", "Nov", "Dec"};
  12. class date {
  13. friend ostream& operator<<(ostream&, const date&);
  14. public:
  15. typedef string::size_type size_type;
  16. date() = default;
  17. date(const string &s);
  18. private:
  19. unsigned year, month, day;
  20. };
  21. inline void format1(const string &ds, const string::size_type &index, unsigned &y, unsigned &m, unsigned &d)
  22. {
  23. string::size_type day_p = index; //day position
  24. string::size_type spa_p = ds.find_first_of(" "); //first space position
  25. string::size_type sep_p; //seperator(between day and year) position
  26. string::size_type year_p; //year position
  27. const vector<string> *p = &month_name1;
  28. if (index == 4) {
  29. sep_p = ds.find(" ", day_p);
  30. year_p = sep_p + 1;
  31. p = &month_name2;
  32. } else if (index > 4) {
  33. sep_p = ds.find(", ", day_p);
  34. year_p = sep_p + 2;
  35. } else {
  36. throw invalid_argument("month error");
  37. }
  38. //check the format
  39. if (spa_p == string::npos || sep_p == string::npos || day_p - spa_p != 1 || (sep_p - day_p != 1 && sep_p - index != 2))
  40. throw invalid_argument("fromat error");
  41. auto year_size = ds.size() - year_p;
  42. string::size_type pos;
  43. if (year_size <= 0 || (pos = ds.find_first_not_of("0123456789", year_p)) != string::npos)
  44. throw invalid_argument("year error");
  45. //get month value
  46. auto month = ds.substr(0, spa_p);
  47. unsigned i = 0, j = 0, month_len = 0;
  48. for (; i < 12; i++) {
  49. month_len = (*p)[i].size();
  50. for (j = 0; j < month_len; j++) {
  51. if (month[j] != (*p)[i][j])
  52. break;
  53. }
  54. if (j == month_len) {
  55. m = ++i;
  56. break;
  57. }
  58. }
  59. if (i == 12)
  60. throw invalid_argument("month error");
  61. d = stoi(ds.substr(day_p, sep_p - day_p));
  62. y = stoi(ds.substr(year_p, ds.size() - year_p));
  63. }
  64. inline void format2(const string &ds, unsigned &y, unsigned &m, unsigned &d)
  65. {
  66. string::size_type sep1 = ds.find_first_not_of("0123456789");
  67. string::size_type sep2 = ds.find_first_not_of("0123456789", sep1 + 1);
  68. // check the seperator
  69. if (sep1 == string::npos || sep2 == string::npos || ds[sep1] != '/' || ds[sep2] != '/')
  70. throw invalid_argument("format error");
  71. // check the year
  72. string::size_type pos = ds.find_first_not_of("0123456789", sep2 + 1);
  73. if (pos != string::npos)
  74. throw invalid_argument("year error");
  75. string day = ds.substr(0, sep1);
  76. string month = ds.substr(sep1 + 1, sep2 - sep1 - 1);
  77. string year = ds.substr(sep2 + 1, ds.size() - sep2 - 1);
  78. d = stoi(day);
  79. m = stoi(month);
  80. y = stoi(year);
  81. }
  82. date::date(const string &ds)
  83. {
  84. string::size_type index;
  85. if ((index = ds.find_first_of("0123456789")) == string::npos)
  86. throw invalid_argument("illegal date");
  87. if (index > 0) { // use month name
  88. format1(ds, index, year, month, day);
  89. } else { //use month number
  90. format2(ds, year, month, day);
  91. }
  92. }
  93. ostream& operator<<(ostream& out, const date &d)
  94. {
  95. out << d.year << " " << d.month << " " << d.day << endl;
  96. return out;
  97. }
  98. int main()
  99. {
  100. string ds1 = {"February 1, 2014"};
  101. string ds2 = {"Jan 15 1900"};
  102. string ds3 = {"28/8/1985"};
  103. try {
  104. date d1(ds1);
  105. date d2(ds2);
  106. date d3(ds3);
  107. cout << d1;
  108. cout << d2;
  109. cout << d3;
  110. } catch (invalid_argument e) {
  111. cout << e.what() << endl;
  112. }
  113. return 0;
  114. }

9.52

  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <stdexcept>
  5. using namespace std;
  6. void handle(stack<char> &s)
  7. {
  8. string exp;
  9. if (s.empty())
  10. throw invalid_argument("format error");
  11. while (!s.empty() && s.top() != '(') {
  12. auto c = s.top();
  13. exp.insert(exp.begin(), c);
  14. s.pop();
  15. }
  16. if (!s.empty()) {
  17. if (s.top() == '(')
  18. s.pop();
  19. else
  20. throw invalid_argument("format error");
  21. }
  22. if (exp[0] != '-')
  23. exp.insert(0, 1, '+');
  24. double val = 0, result = 0;
  25. string::size_type pos = 0, prev = 0;
  26. while ((pos = exp.find_first_of("+-", prev + 1)) != string::npos) {
  27. val = stod(exp.substr(prev, pos));
  28. result += val;
  29. prev = pos;
  30. }
  31. val = stod(exp.substr(prev, exp.size()-prev));
  32. result += val;
  33. if (!s.empty()) {
  34. if (s.top() == '+' && (result < 0))
  35. s.pop();
  36. else if (s.top() == '-' && (result < 0)) {
  37. s.pop();
  38. s.push('+');
  39. result = 0 - result;
  40. }
  41. }
  42. string ret = to_string(result);
  43. for (auto p = 0; p < ret.size(); p++)
  44. s.push(ret[p]);
  45. }
  46. double calculate(stack<char> &s)
  47. {
  48. string exp;
  49. if (s.empty())
  50. throw invalid_argument("format error");
  51. //生成表达式
  52. while (!s.empty()) {
  53. auto c = s.top();
  54. exp.insert(exp.begin(), c);
  55. s.pop();
  56. }
  57. //计算结果
  58. double val = 0, result = 0;
  59. string::size_type pos = 0, prev = 0;
  60. while ((pos = exp.find_first_of("+-", prev + 1)) != string::npos) {
  61. val = stod(exp.substr(prev, pos));
  62. result += val;
  63. prev = pos;
  64. }
  65. val = stod(exp.substr(prev, exp.size()-prev));
  66. result += val;
  67. return result;
  68. }
  69. int main()
  70. {
  71. string exp;
  72. stack<char> s;
  73. string::size_type i = 0;
  74. cout << "input expression: " << endl;
  75. getline(cin, exp);
  76. while (i < exp.size()) {
  77. if (exp[i] == ')') {
  78. handle(s);
  79. } else {
  80. s.push(exp[i]);
  81. }
  82. i++;
  83. }
  84. auto ret = calculate(s);
  85. cout << "result = " << ret << endl;
  86. return 0;
  87. }

 

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