-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_01.py
executable file
·99 lines (77 loc) · 2.06 KB
/
assignment_01.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Exercicio 1 - pilha
#
# ---------------------------------------------
# <nome>, <matricula>
# Mannuel Victor Di Pace Maroja Limeira, dipacema
# ---------------------------------------------
# Incluir stack.py
from stack import Stack
pilha_calculo = Stack()
pilha_op = Stack()
def add_op_n1(op):
if pilha_op.is_empty():
pilha_op.push(op)
return
last = pilha_op.pop()
pilha_op.push(last)
if last is ("+" or "-"):
pilha_op.push(op)
return
if last is ("*" or "/"):
clean_op()
pilha_op.push(op)
return
if last is "(":
pilha_op.push(op)
return
def clean_op():
while not pilha_op.is_empty():
letra = pilha_op.pop()
if letra is "(":
pilha_op.push(letra)
return
else:
pilha_calculo.push(letra)
return
def add_op_n2(op):
pilha_op.push(op)
return
def clean_op_p():
while True:
letra = pilha_op.pop()
if letra is "(":
return
else:
pilha_calculo.push(letra)
return
def print_calculo():
temp = Stack()
calculo = ""
while not pilha_calculo.is_empty():
temp.push(pilha_calculo.pop())
while not temp.is_empty():
calculo = calculo + temp.pop()
print(calculo)
#------------------------ main ---------------------#
while True:
print("")
entrada = input("Digite o calculo: ")
for letra in entrada:
if letra is "(":
pilha_op.push(letra);
elif letra is ")":
clean_op_p()
elif letra is "+":
add_op_n1(letra)
elif letra is "-":
add_op_n1(letra)
elif letra is "*":
add_op_n2(letra)
elif letra is "/":
add_op_n2
else:
pilha_calculo.push(letra)
clean_op()
print("Forma pos fixada: ", end = "")
print_calculo()
print("")