You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
781 B
Python

def parse(expr):
opers = ['+', '-', '*', '/']
num_par = 0
tokenized = []
temp_string = ""
index = 0
while index < len(expr):
if expr[index] in opers:
tokenized.append(expr[index])
elif expr[index].isdigit() or expr[index] == '.':
while (index < len(expr)) and (expr[index].isdigit() or expr[index] == '.'):
temp_string += expr[index]
index += 1
tokenized.append(temp_string)
temp_string = ""
elif expr[index] == '(':
num_par = 1
temp_string += expr[index]
index += 1
while num_par != 0 and index < len(expr):
temp_string += expr[index]
if expr[index] == '(':
num_par += 1
if expr[index] == ')':
num_par -= 1
index += 1
tokenized.append(temp_string)
temp_string = ""
index += 1
return tokenized