Найти - Пользователи
Полная версия: почему не компилируется как надо( алгоритм сортировочной станции)? высвечивает press any key to continue
Начало » Python для новичков » почему не компилируется как надо( алгоритм сортировочной станции)? высвечивает press any key to continue
1
Ksyu2018
OPERATORS = {'+': (1, lambda x, y: x + y), ‘-’: (1, lambda x, y: x - y),
‘*’: (2, lambda x, y: x * y), ‘/’: (2, lambda x, y: x / y)}


def eval_(formula):
def parse(formula_string):
number = ‘'
for s in formula_string:
if s in ’1234567890.':
number += s
elif number:
yield float(number)
number = ''
if s in OPERATORS or s in “()”:
yield s
if number:
yield float(number)

def shunting_yard(parsed_formula):
stack =
for token in parsed_formula:
if token in OPERATORS:
while stack and stack != “(” and OPERATORS <= OPERATORS[stack]:
yield stack.pop()
stack.append(token)
elif token == “)”:
while stack:
x = stack.pop()
if x == “(”:
break
yield x
elif token == “(”:
stack.append(token)
else:
yield token
while stack:
yield stack.pop()

def calc(polish):
stack =
for token in polish:
if token in OPERATORS:
y, x = stack.pop(), stack.pop()
stack.append(OPERATORS(x, y))
else:
stack.append(token)
return stack

return calc(shunting_yard(parse(formula)))
Shaman
гладиолус
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB