-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate.py
More file actions
53 lines (34 loc) · 1.01 KB
/
calculate.py
File metadata and controls
53 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# Sean Reid
import array_stack #for pop, push etc
def solve(num, op): #evaluate the numbers
num2, num1 = number.pop(), number.pop()
if op == "+":
result = num1 + num2
elif op == "-":
result = num1 - num2
elif op == "*":
result = num1 * num2
elif op == "/":
result = num1 / num2
return result
def calculate(expression): #calculate the result of the postfix expression
nums = array_stack.ArrayStack()
for e in expression:
if type(e) is int: #check if element is a number or not
nums.push(float(e))
else:
nums.push(solve(nums, e))
return (nums.pop())
def read_file(): #read text file line by line
fp = open("input.txt", "r")
return fp.readlines()
def main(): #read file line by line and solve each expression
answers = open("answers.txt", "w")
lines = read_file()
for expression in file:
expression.strip().split() #split expression
answer = calculate(expression)
answers.write(str(answer) + "\n") #string to print onto file
if __name__ == '__main__':
main()