ZBLOG

#include #include #include #include class Express { private: std::string infix; public: Express(const std::string& exp) : infix(exp) {} bool isMatch() { std::stack s; for ...

在你的代码中,当输入的中缀表达式没有括号(如 1*1+1)时,转换为后缀表达式后生成了一个不正确的格式。出现这个问题的原因是在处理操作符和数字时,输出的格式没有正确处理空格,使得计算值时可能无法识别出操作数。

下面是对 Trans() 方法的一些修改,以确保后缀表达式的正确性,并且在运算时可以正确解析操作数。

  1. #include <iostream>
  2. #include <stack>
  3. #include <sstream>
  4. #include <string>
  5. class Express {
  6. private:
  7. std::string infix;
  8. public:
  9. Express(const std::string& exp) : infix(exp) {}
  10. bool isMatch() {
  11. std::stack<char> s;
  12. for (char ch : infix) {
  13. if (ch == '(') {
  14. s.push(ch);
  15. }
  16. else if (ch == ')') {
  17. if (s.empty() || s.top() != '(') return false;
  18. s.pop();
  19. }
  20. }
  21. return s.empty();
  22. }
  23. std::string Trans() {
  24. std::stack<char> opStack;
  25. std::ostringstream output;
  26. for (char ch : infix) {
  27. if (isdigit(ch)) {
  28. output << ch; // 添加数字
  29. }
  30. else if (ch == ' ') {
  31. continue; // 跳过空格
  32. }
  33. else {
  34. output << ' '; // 在每个数字和运算符之间加空格
  35. if (ch == '(') {
  36. opStack.push(ch);
  37. }
  38. else if (ch == ')') {
  39. while (!opStack.empty() && opStack.top() != '(') {
  40. output << opStack.top(); // 输出栈顶元素
  41. output << ' '; // 添加空格分隔
  42. opStack.pop();
  43. }
  44. opStack.pop(); // 弹出左括号
  45. }
  46. else {
  47. while (!opStack.empty() && precedence(opStack.top()) >= precedence(ch)) {
  48. output << opStack.top(); // 输出栈顶元素
  49. output << ' '; // 添加空格分隔
  50. opStack.pop();
  51. }
  52. opStack.push(ch); // 当前运算符入栈
  53. }
  54. }
  55. }
  56. while (!opStack.empty()) {
  57. output << opStack.top(); // 输出栈顶元素
  58. output << ' '; // 添加空格分隔
  59. opStack.pop();
  60. }
  61. return output.str();
  62. }
  63. int GetValue(const std::string& postfixExp) {
  64. std::stack<int> valueStack;
  65. std::istringstream iss(postfixExp);
  66. std::string token;
  67. while (iss >> token) {
  68. if (isdigit(token[0])) {
  69. valueStack.push(std::stoi(token));
  70. }
  71. else {
  72. int b = valueStack.top(); valueStack.pop();
  73. int a = valueStack.top(); valueStack.pop();
  74. switch (token[0]) {
  75. case '+': valueStack.push(a + b); break;
  76. case '-': valueStack.push(a - b); break;
  77. case '*': valueStack.push(a * b); break;
  78. case '/':
  79. if (b != 0)
  80. valueStack.push(a / b);
  81. else
  82. throw std::runtime_error("Division by zero");
  83. break;
  84. }
  85. }
  86. }
  87. return valueStack.top();
  88. }
  89. private:
  90. int precedence(char op) const {
  91. switch (op) {
  92. case '+':
  93. case '-':
  94. return 1;
  95. case '*':
  96. case '/':
  97. return 2;
  98. default:
  99. return 0;
  100. }
  101. }
  102. };
  103. int main() {
  104. std::string input;
  105. Express expr("");
  106. std::cout << "请输入中缀表达式: ";
  107. while (true) {
  108. getline(std::cin, input);
  109. expr = Express(input);
  110. if (expr.isMatch()) {
  111. std::cout << "中缀表达式中括号正确: " << input << '\n';
  112. break;
  113. }
  114. else {
  115. std::cout << "中缀表达式中输入的括号不对,请重新输入:";
  116. }
  117. }
  118. auto postfixExp = expr.Trans();
  119. std::cout << "后缀表达式:" << postfixExp.substr(1) << '\n';
  120. try {
  121. int result = expr.GetValue(postfixExp);
  122. std::cout << "求值结果:" << result << '\n';
  123. }
  124. catch (const std::exception& e) {
  125. std::cerr << e.what() << '\n';
  126. }
  127. return 0;
  128. }

主要改动如下:

  • Trans() 方法中,每次添加数字或操作符到输出字符串之前添加了一个空格。这确保了在获取后缀表达式时,数字和操作符之间都有明确的分隔。
  • 调整了相应地方确保无论是计算还是返回结果都能按照预期工作。

现在你可以尝试用不同形式的有效输入来测试该程序,包括带有或不带有括号的情况。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=20564

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?