Skip to content

Commit a36f3e3

Browse files
committed
pro2 solved
1 parent 5dc8aca commit a36f3e3

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

unit3/input2.txt

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

unit3/output.txt

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

unit3/pro2.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'''
2+
RPN Calculator
3+
Modify Program so that it accepts expressions written in prefix (Polish) notation
4+
'''
5+
6+
from opus7.stackAsLinkedList import StackAsLinkedList
7+
8+
class Algorithms(object):
9+
10+
def __init__(self):
11+
self.stack = StackAsLinkedList()
12+
13+
def calculator(self, input, output):
14+
for line in input.readlines():
15+
# prefix is acrtually the reversed version of postfix
16+
line = line.split()
17+
operation = line[:-1]
18+
operation.reverse()
19+
operation.append(line[-1])
20+
for word in operation:
21+
if word == "+":
22+
print "it is +"
23+
arg2 = self.stack.pop()
24+
arg1 = self.stack.pop()
25+
self.stack.push(arg1 + arg2)
26+
elif word == "*":
27+
print "it is *"
28+
arg2 = self.stack.pop()
29+
arg1 = self.stack.pop()
30+
self.stack.push(arg1 * arg2)
31+
elif word == "-":
32+
print "it is -"
33+
arg2 = self.stack.pop()
34+
arg1 = self.stack.pop()
35+
self.stack.push(arg1 - arg2)
36+
elif word == "/":
37+
print "it is /"
38+
arg2 = self.stack.pop()
39+
arg1 = self.stack.pop()
40+
self.stack.push(arg1 / arg2)
41+
# exponential expression
42+
elif word == "^":
43+
print "it is ^"
44+
arg2 = self.stack.pop()
45+
arg1 = self.stack.pop()
46+
self.stack.push(arg1 ** arg2)
47+
elif word == "=":
48+
print "it is ="
49+
arg = self.stack.pop()
50+
output.write(str(arg) + "\n")
51+
print "ans is ", arg
52+
else:
53+
print "it is number"
54+
# make it double
55+
self.stack.push(float(word))
56+
'''
57+
def operating(a2, a1, word):
58+
print "operating: ", a2, a1, word
59+
if word == "+":
60+
print "it is +"
61+
return a2 + a1
62+
elif word == "*":
63+
print "it is *"
64+
return a2 * a1
65+
elif word == "-":
66+
print "it is -"
67+
return a2 - a1
68+
elif word == "/":
69+
print "it is /"
70+
return a2 / a1
71+
# exponential expression
72+
elif word == "^":
73+
print "it is ^"
74+
return a2 ** a1
75+
else:
76+
print "unrecognizable"
77+
# make it double
78+
operators = ["+", "-", "*", "/", "^"]
79+
for line in input.readlines():
80+
for word in line.split():
81+
if word in operators:
82+
print "push operator " + word
83+
self.stack.push(word)
84+
elif word == "=":
85+
arg = self.stack.pop()
86+
output.write(str(arg) + "\n")
87+
print "ans = ", arg
88+
else:
89+
print "push number " + word
90+
flag = 0
91+
if self.stack.getTop() not in operators:
92+
flag = 1
93+
self.stack.push(float(word))
94+
if flag == 1:
95+
arg2 = self.stack.pop()
96+
arg1 = self.stack.pop()
97+
oper = self.stack.pop()
98+
self.stack.push(operating(arg2, arg1, oper))
99+
print "push answer"
100+
'''
101+
102+
a = Algorithms()
103+
a.calculator(open("input2.txt", 'r'), open("output.txt", 'w+'))
104+

0 commit comments

Comments
 (0)