Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from enum import Enum
- from sys import argv
- OP = Enum('Op', 'PUSH SUM MULT SUB DIV PRINT')
- if len(argv) > 1:
- filename = argv[1]
- else:
- filename = "test"
- try:
- file = open(filename, 'r')
- except:
- print("Erro ao abrir arquivo.")
- quit()
- filelines = file.read().split('\n')
- file.seek(0)
- fileread = file.read().split()
- i = 0
- isPush = False
- isFirst = True
- acc = 0
- pilha = []
- try:
- for exp in fileread:
- #print("Exp: [{}]".format(exp))
- #print("Line: {}".format(filelines[i]))
- if isPush:
- if exp.isnumeric():
- pilha.append(int(exp))
- isPush = False
- i+=1
- continue
- else:
- raise Exception()
- op = OP[exp]
- if op == OP.PUSH:
- isPush = True
- continue
- elif op == OP.PRINT:
- print(acc)
- elif op == OP.SUM:
- if isFirst:
- acc = pilha.pop() + pilha.pop()
- isFirst = False
- else:
- acc += pilha.pop()
- elif op == OP.MULT:
- if isFirst:
- acc = pilha.pop() * pilha.pop()
- isFirst = False
- else:
- acc *= pilha.pop()
- elif op == OP.SUB:
- if isFirst:
- acc = pilha.pop()
- isFirst = False
- acc = pilha.pop() - acc
- elif op == OP.DIV:
- if isFirst:
- acc = pilha.pop()
- isFirst = False
- acc = pilha.pop() / acc
- """
- match op:
- case OP.PUSH:
- isPush = True
- continue
- case OP.PRINT:
- print(acc)
- case OP.SUM:
- if isFirst:
- acc = pilha.pop() + pilha.pop()
- isFirst = False
- else:
- acc += pilha.pop()
- case OP.MULT:
- if isFirst:
- acc = pilha.pop() * pilha.pop()
- isFirst = False
- else:
- acc *= pilha.pop()
- case OP.SUB:
- if isFirst:
- acc = pilha.pop()
- isFirst = False
- acc = pilha.pop() - acc
- case OP.DIV:
- if isFirst:
- acc = pilha.pop()
- isFirst = False
- acc = pilha.pop() / acc
- """
- i+=1
- except:
- print("Erro na linha: \"{}\"".format(filelines[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement