Skip to content

Commit cfde143

Browse files
committed
pro1 clear/print/unary solved
1 parent d4531c8 commit cfde143

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

unit3/input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
5.5 5 + =
33
5.5 2 / 4 + 5 * =
44
5 2 ^ =
5+
-5 5 + =
6+
4 5 32 2 5

unit3/output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
10.5
33
33.75
44
25.0
5+
0.0

unit3/pro1.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,60 @@
66

77
class Algorithms(object):
88

9-
@staticmethod
10-
def calculator(input, output):
11-
stack = StackAsLinkedList()
9+
def __init__(self):
10+
self.stack = StackAsLinkedList()
11+
12+
def calculator(self, input, output):
1213
for line in input.readlines():
1314
for word in line.split():
1415
if word == "+":
1516
print "it is +"
16-
arg2 = stack.pop()
17-
arg1 = stack.pop()
18-
stack.push(arg1 + arg2)
17+
arg2 = self.stack.pop()
18+
arg1 = self.stack.pop()
19+
self.stack.push(arg1 + arg2)
1920
elif word == "*":
2021
print "it is *"
21-
arg2 = stack.pop()
22-
arg1 = stack.pop()
23-
stack.push(arg1 * arg2)
22+
arg2 = self.stack.pop()
23+
arg1 = self.stack.pop()
24+
self.stack.push(arg1 * arg2)
2425
elif word == "-":
2526
print "it is -"
26-
arg2 = stack.pop()
27-
arg1 = stack.pop()
28-
stack.push(arg1 - arg2)
27+
arg2 = self.stack.pop()
28+
arg1 = self.stack.pop()
29+
self.stack.push(arg1 - arg2)
2930
elif word == "/":
3031
print "it is /"
31-
arg2 = stack.pop()
32-
arg1 = stack.pop()
33-
stack.push(arg1 / arg2)
32+
arg2 = self.stack.pop()
33+
arg1 = self.stack.pop()
34+
self.stack.push(arg1 / arg2)
3435
# exponential expression
3536
elif word == "^":
3637
print "it is ^"
37-
arg2 = stack.pop()
38-
arg1 = stack.pop()
39-
stack.push(arg1 ** arg2)
38+
arg2 = self.stack.pop()
39+
arg1 = self.stack.pop()
40+
self.stack.push(arg1 ** arg2)
4041
elif word == "=":
4142
print "it is ="
42-
arg = stack.pop()
43+
arg = self.stack.pop()
4344
output.write(str(arg) + "\n")
4445
print "ans is ", arg
4546
else:
4647
print "it is number"
4748
# make it double
48-
stack.push(float(word))
49+
self.stack.push(float(word))
50+
51+
# remove all by calling purge method of linked list
52+
def clear(self):
53+
self.stack.purge()
4954

55+
# use the iterator of linked list to access every remaining element
56+
def printall(self):
57+
for ele in self.stack:
58+
print ele
5059

51-
Algorithms.calculator(open("input.txt", 'r'), open("output.txt", 'w+'))
60+
a = Algorithms()
61+
a.calculator(open("input.txt", 'r'), open("output.txt", 'w+'))
62+
a.printall()
63+
a.clear()
64+
print "print all again"
65+
a.printall()

0 commit comments

Comments
 (0)