## Evaluation of four Operational expressions

##### Train of thought

Using dual stack to achieve — the stack of values`nums`

Stack and operator`ops`

.

###### Algorithm steps

- For the original expression string
`exp`

Preprocess it to a list of elements corresponding to a number or operator`explist`

. - ergodic
`explist`

Each element is pushed into the corresponding stack in turn. After each push, it is judged whether the top two stacks can be multiplied and divided. The necessary and sufficient conditions for the top of stack to perform multiplication and division are as follows,`ops`

The top of the stack is one of < * >, < / > and`nums`

Element ratio in`ops`

If it is possible to operate, the operation is performed and the result is pushed back`nums`

Medium `explist`

After traversing, all multiplication and division operations have been performed`ops`

Only addition and subtraction are left in. Next, another cycle is started, and the operation can be continued`nums`

The only remaining element in is the value of the expression

##### code

```
def operation(a, b, op: str):
"""
Calculate the result of an operation
:param a: val
:param b: val
: param OP: operator
:return: val
"""
if op == '+':
return a+b
elif op == '-':
return a-b
elif op == '*':
return a*b
elif op == '/':
return a/b
else:
Raise exception ('incorrect operator ')
def exp_str2list(exp: str):
"""
Set the expression exp:str Each element corresponds to a number < num > or operator < OP >
Exp is known as a standard string of four arithmetic expressions
: param exp: STR, expression
:return: list
"""
Opset = {'+', '-', '*', '/'} operator set
flag = -1
explist = list()
For I, char in enumerate (EXP): for each character in the expression string
If char in opset: if char is an operator
explist.append (Exp [Flag + 1: I]) from the previous operator to the current operator is a number, which is saved to explist
Flag = I ා update flag to current OP position
explist.append (char) ා the current OP is also added to explist
explist.append(exp[flag+1:])
return explist
def calculate_expression(exp: str):
"""
Evaluates the expression represented by the string exp and returns a numeric value
Exp is known to be a standard string of four arithmetic expressions without parentheses
: param exp: STR, expression
:return: val
"""
opset = {'+', '-', '*', '/'}
Nums = list(), number stack
Ops = list() ා operator stack
explist = exp_str2list(exp)
print(explist)
for e in explist:
#E stack
If e in opset: ා if e is an operator
ops.append(e)
else:
nums.append(eval(e))
#If e is a multiplication and division method with high priority, and
#Nums and OPS match exactly, the top of the stack can be calculated once, and the result is pushed back to nums
if ops and ops[-1] in {'*', '/'} and len(nums) == len(ops) + 1:
op = ops.pop()
y = nums.pop()
x = nums.pop()
nums.append(operation(x, y, op))
#At this time, all the elements in explist have been traversed, and the multiplication and division methods have been operated, and only addition and subtraction are left to be operated on the double stack. As we all know, the addition and subtraction method obeys the combination law and can be calculated all the way
while ops:
op = ops.pop()
y = nums.pop()
x = nums.pop()
x_op_y = operation(x, y, op)
nums.append(x_op_y)
return nums[0]
if __name__ == '__main__':
exp = '1*43+542+532*432'
print(calculate_expression(exp))
print(eval(exp))
```

### reference material

- CSDN: expression evaluation — Python Dafa is good!https://blog.csdn.net/Hpuer_R…