ZBLOG

c++代码编写栈--中缀转后缀表达式 Description 表达式有三种表示方法,分别为: 前缀表示式:把运算符写在操作数之前,即运算符+操作数1+操作数2 中缀表示式:把运算符写在操作数之间,即操作数1+运算符+操作数2 后缀表示式:把运算符写在操作数之后,即操作数1+操作数2+运算符 例如:a+b*(c-d)-e/f 前缀表示式:-+a*b-cd/ef 中缀表示式:a+b*(c-d)-e/f 后缀表示式:abcd-*+ef/- 前缀表达式和后缀表达式与相应的中缀表达式...

下面是一个使用栈实现的C++代码示例,将中缀表达式转换为后缀表达式并计算结果:

  1. #include <iostream>
  2. #include <stack>
  3. #include <sstream>
  4. using namespace std;
  5. // 判断字符是否为运算符
  6. bool isOperator(char c) {
  7. return (c == '+' || c == '-' || c == '*' || c == '/');
  8. }
  9. // 获取运算符的优先级
  10. int getPriority(char op) {
  11. if (op == '+' || op == '-')
  12. return 1;
  13. else if (op == '*' || op == '/')
  14. return 2;
  15. return 0; // 其他情况返回0
  16. }
  17. // 中缀表达式转后缀表达式
  18. string infixToPostfix(string infix) {
  19. string postfix = ""; // 存放后缀表达式
  20. stack<char> operatorStack; // 运算符栈
  21. for (char& ch : infix) {
  22. if (ch == ' ') {
  23. continue; // 忽略空格字符
  24. } else if (isdigit(ch)) {
  25. postfix += ch; // 操作数直接加入后缀表达式中
  26. } else if (isOperator(ch)) {
  27. while (!operatorStack.empty() && operatorStack.top() != '(' &&
  28. getPriority(operatorStack.top()) >= getPriority(ch)) {
  29. postfix += operatorStack.top(); // 将栈顶的高优先级运算符弹出并加入后缀表达式中
  30. operatorStack.pop();
  31. }
  32. operatorStack.push(ch); // 当前运算符入栈
  33. } else if (ch == '(') {
  34. operatorStack.push(ch); // 左括号入栈
  35. } else if (ch == ')') {
  36. while (!operatorStack.empty() && operatorStack.top() != '(') {
  37. postfix += operatorStack.top(); // 将栈顶运算符弹出并加入后缀表达式中
  38. operatorStack.pop();
  39. }
  40. if (!operatorStack.empty())
  41. operatorStack.pop(); // 弹出左括号
  42. }
  43. }
  44. while (!operatorStack.empty()) {
  45. postfix += operatorStack.top(); // 将栈中剩余的运算符加入后缀表达式中
  46. operatorStack.pop();
  47. }
  48. return postfix;
  49. }
  50. // 计算后缀表达式的结果
  51. double evaluatePostfix(string postfix) {
  52. stack<double> operandStack; // 操作数栈
  53. stringstream ss(postfix);
  54. string token;
  55. while (getline(ss, token, ' ')) {
  56. if (isdigit(token[0])) {
  57. double operand = stod(token); // 将操作数从字符串转换为浮点数
  58. operandStack.push(operand);
  59. } else if (isOperator(token[0])) {
  60. double operand2 = operandStack.top();
  61. operandStack.pop();
  62. double operand1 = operandStack.top();
  63. operandStack.pop();
  64. switch (token[0]) { // 根据不同运算符进行计算并将结果压入操作数栈
  65. case '+':
  66. operand1 += operand2;
  67. break;
  68. case '-':
  69. operand1 -= operand2;
  70. break;
  71. case '*':
  72. operand1 *= operand2;
  73. break;
  74. case '/':
  75. if (operand2 != 0)
  76. operand1 /= operand2;
  77. else {
  78. cerr << "Error: Division by zero." << endl;
  79. exit(1);
  80. }
  81. break;
  82. default:
  83. cerr << "Error: Invalid operator." << endl;
  84. exit(1);
  85. }
  86. operandStack.push(operand1);
  87. }
  88. }
  89. if (!operandStack.empty())
  90. return operandStack.top();
  91. cerr << "Error: Invalid expression." << endl;
  92. exit(1);
  93. }
  94. int main() {
  95. string infixExpression;
  96. getline(cin, infixExpression);
  97. // 去除首尾的'#'字符
  98. infixExpression = infixExpression.substr(1, infixExpression.length() - 2);
  99. string postfixExpression = infixToPostfix(infixExpression);
  100. cout << "转换的后缀表达式为:" << postfixExpression << endl;
  101. double result = evaluatePostfix(postfixExpression);
  102. cout << "计算结果为:" << result << endl;
  103. return 0;
  104. }

你可以在输入时通过标准输入读取中缀表达式,然后调用infixToPostfix函数将其转换为后缀表达式,并调用evaluatePostfix函数计算结果。最后输出后缀表达式和计算结果。

注意:该代码只适用于符合题目要求的简单中缀表达式,对于复杂的运算符优先级、括号等情况可能需要进行修改。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?