Skip to content

Commit d4531c8

Browse files
committed
pro1 expo added
1 parent 9a79476 commit d4531c8

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

unit3/input.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
5.5 9 + 2 * 6 1.5 * + =
2+
5.5 5 + =
3+
5.5 2 / 4 + 5 * =
4+
5 2 ^ =

unit3/output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
38.0
2+
10.5
3+
33.75
4+
25.0

unit3/pro1.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
RPN Calculator
3+
'''
4+
5+
from opus7.stackAsLinkedList import StackAsLinkedList
6+
7+
class Algorithms(object):
8+
9+
@staticmethod
10+
def calculator(input, output):
11+
stack = StackAsLinkedList()
12+
for line in input.readlines():
13+
for word in line.split():
14+
if word == "+":
15+
print "it is +"
16+
arg2 = stack.pop()
17+
arg1 = stack.pop()
18+
stack.push(arg1 + arg2)
19+
elif word == "*":
20+
print "it is *"
21+
arg2 = stack.pop()
22+
arg1 = stack.pop()
23+
stack.push(arg1 * arg2)
24+
elif word == "-":
25+
print "it is -"
26+
arg2 = stack.pop()
27+
arg1 = stack.pop()
28+
stack.push(arg1 - arg2)
29+
elif word == "/":
30+
print "it is /"
31+
arg2 = stack.pop()
32+
arg1 = stack.pop()
33+
stack.push(arg1 / arg2)
34+
# exponential expression
35+
elif word == "^":
36+
print "it is ^"
37+
arg2 = stack.pop()
38+
arg1 = stack.pop()
39+
stack.push(arg1 ** arg2)
40+
elif word == "=":
41+
print "it is ="
42+
arg = stack.pop()
43+
output.write(str(arg) + "\n")
44+
print "ans is ", arg
45+
else:
46+
print "it is number"
47+
# make it double
48+
stack.push(float(word))
49+
50+
51+
Algorithms.calculator(open("input.txt", 'r'), open("output.txt", 'w+'))

0 commit comments

Comments
 (0)