后缀算式9+2+3+++-10+2+÷+-
时间: 2023-11-28 21:45:08 浏览: 37
根据提供的后缀算式,可以按照以下步骤进行计算:
1. 将9、2、3依次入栈;
2. 遇到+号,将栈顶两个元素弹出并相加,结果为5,将5入栈;
3. 遇到2,将2入栈;
4. 遇到+号,将栈顶两个元素弹出并相加,结果为7,将7入栈;
5. 遇到3,将3入栈;
6. 遇到+号,将栈顶两个元素弹出并相加,结果为10,将10入栈;
7. 遇到-号,将栈顶两个元素弹出并相减,结果为-8,将-8入栈;
8. 遇到10,将10入栈;
9. 遇到2,将2入栈;
10. 遇到/号,将栈顶两个元素弹出并相除,结果为-5,将-5入栈;
11. 遇到-号,将栈顶两个元素弹出并相减,结果为3,即为最终结果。
因此,后缀算式9+2+3+++-10+2+÷+-的值为3。
相关问题
中缀算式转化为后缀算式并求值c语言
中缀算式转化为后缀算式:
1. 首先初始化一个栈和一个结果数组
2. 遍历中缀算式,如果是数字则将其直接加入结果数组(可以通过判断字符是否为数字实现),如果是运算符则判断其与栈顶运算符的优先级,如果栈顶运算符优先级大于当前运算符则将栈顶运算符弹出并加入结果数组,然后继续比较新的栈顶运算符和当前运算符,直到遇到优先级小于等于当前运算符的栈顶运算符或者栈为空时,将当前运算符加入栈中
3. 遍历完中缀算式后,如果栈中仍有运算符,则依次弹出并加入结果数组
4. 最终结果数组中存储着后缀算式
后缀算式求值:
1. 初始化一个栈
2. 遍历后缀算式,如果是数字则将其入栈,如果是运算符则弹出栈顶的两个数字进行运算,然后将结果压入栈中
3. 遍历完后缀算式之后,栈中仅剩一个数字,该数字即为所求结果
C语言实现:
中缀算式转后缀:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int cmp(char a, char b) {
if ((a == '+' || a == '-') && (b == '*' || b == '/')) return -1;
else return 1;
}
void infix2postfix(char *infix, char *postfix) {
char stack[1000];
int top = -1, i = 0, j = 0;
while (infix[i] != '\0') {
if (isdigit(infix[i])) {
postfix[j++] = infix[i++];
} else {
while (top >= 0 && cmp(stack[top], infix[i]) == -1) {
postfix[j++] = stack[top--];
}
stack[++top] = infix[i++];
}
}
while (top >= 0) {
postfix[j++] = stack[top--];
}
postfix[j] = '\0';
}
int eval(char op, int a, int b) {
if (op == '+') return a + b;
else if (op == '-') return a - b;
else if (op == '*') return a * b;
else return a / b;
}
int postfix_eval(char *postfix) {
int stack[1000], top = -1, i = 0;
while (postfix[i] != '\0') {
if (isdigit(postfix[i])) {
stack[++top] = postfix[i++] - '0';
} else {
int b = stack[top--];
int a = stack[top--];
stack[++top] = eval(postfix[i++], a, b);
}
}
return stack[top];
}
int main() {
char infix[1000], postfix[1000];
scanf("%s", infix);
infix2postfix(infix, postfix);
printf("%d\n", postfix_eval(postfix));
return 0;
}
```
注意:实现时可以将操作符的优先级需要值设为更高的整数,以免在处理除法时因为优先级问题导致结果错误。
文件in.txt(测试数据) 共1行,是一个算式算式包含字符: “(”、“)”、“0-9”、“+”、“-”、“*”、“/”、“^”。(“/”用整数除法,算式长度<=50,其中所有数据在2^31-1的范围内)。写一段程序读取这个文件中的算式并算出结果
以下是使用Python语言实现的计算代码:
```python
# 读取文件中的算式
with open('in.txt', 'r') as f:
expr = f.readline().strip()
# 定义计算函数
def calculate(expr):
# 将算式中的字符分离出来
tokens = []
num = ''
for ch in expr:
if ch.isdigit():
num += ch
else:
if num:
tokens.append(int(num))
num = ''
if ch in '+-*/^()':
tokens.append(ch)
if num:
tokens.append(int(num))
# 定义优先级
priority = {
'^': 3,
'*': 2,
'/': 2,
'+': 1,
'-': 1,
'(': 0
}
# 定义栈和输出队列
stack = []
output = []
# 中缀表达式转后缀表达式
for token in tokens:
if isinstance(token, int):
output.append(token)
elif token in '^*/+-':
while stack and stack[-1] != '(' and priority[token] <= priority[stack[-1]]:
output.append(stack.pop())
stack.append(token)
elif token == '(':
stack.append(token)
elif token == ')':
while stack and stack[-1] != '(':
output.append(stack.pop())
stack.pop()
while stack:
output.append(stack.pop())
# 计算后缀表达式
stack = []
for token in output:
if isinstance(token, int):
stack.append(token)
elif token in '+-*/^':
op2 = stack.pop()
op1 = stack.pop()
if token == '+':
stack.append(op1 + op2)
elif token == '-':
stack.append(op1 - op2)
elif token == '*':
stack.append(op1 * op2)
elif token == '/':
stack.append(op1 // op2)
elif token == '^':
stack.append(op1 ** op2)
return stack[0]
# 输出结果
result = calculate(expr)
print(result)
```
假设测试数据文件in.txt中的算式为:"2+3*4-(5-6)",运行程序后输出的结果为:13