Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- x = Formula('2.34 + valueX * 8 - 49879 * (valueY + 178)')
- result = x.execute(valueX=2, valueY=3)
- ['2.34', '+', 'valueX', '*', '8', '-', '49879', '*',
- '(', 'valueY', '+', '178', ')']
- ['valueX', '8', '*', '2.34', '+', 'valueY', '178',
- '+', '49879', '*', '-']
- """
- class Formula:
- OPERATORS = ['+', '-', '*', '/', '**', '(', ')']
- def __init__(self, source):
- self.source = source.strip().replace(' ', '')
- self.tokens = []
- self.rpn = []
- self.analyze_syntax()
- self.compile()
- def analyze_syntax(self):
- tokens = []
- token = ''
- for i, ch in enumerate(self.source):
- if ch not in self.OPERATORS:
- token += ch
- else:
- if self.source[i:i+2] == '**':
- continue
- if token:
- tokens.append(token)
- token = ''
- if self.source[i-1:i+1] == '**':
- tokens.append('**')
- else:
- tokens.append(ch)
- print(ch, token, tokens)
- if token:
- tokens.append(token)
- self.tokens = tokens
- print(tokens)
- def op_prec(self, operator):
- if operator in '+-':
- return 1
- elif operator in '*/':
- return 2
- elif operator == '**':
- return 3
- elif operator == '(':
- return 100
- def compile(self):
- result_queue = []
- operator_stack = []
- for token in self.tokens:
- if token == ')':
- while operator_stack and operator_stack[-1] != '(':
- result_queue.append(operator_stack.pop())
- operator_stack.pop()
- elif token in self.OPERATORS:
- if operator_stack:
- if self.op_prec(token) < self.op_prec(operator_stack[-1]):
- while self.op_prec(token) < self.op_prec(operator_stack[-1]) and operator_stack[-1] != '(':
- result_queue.append(operator_stack.pop())
- operator_stack.append(token)
- else:
- operator_stack.append(token)
- else:
- operator_stack.append(token)
- else:
- result_queue.append(token)
- # print(token, operator_stack, result_queue)
- while operator_stack:
- result_queue.append(operator_stack.pop())
- self.rpn = result_queue
- def execute(self, **kwargs):
- def get_value(value):
- if value.isdigit():
- return int(value)
- else:
- try:
- return float(value)
- except ValueError:
- if value in self.OPERATORS:
- return value
- else:
- return kwargs[value]
- expr = [get_value(x) for x in self.rpn]
- i = 0
- while len(expr) > 1:
- if expr[i] in self.OPERATORS:
- result = None
- if expr[i] == '+':
- result = expr[i-2] + expr[i-1]
- elif expr[i] == '-':
- result = expr[i-2] - expr[i-1]
- elif expr[i] == '*':
- result = expr[i-2] * expr[i-1]
- elif expr[i] == '/':
- result = expr[i-2] / expr[i-1]
- elif expr[i] == '**':
- result = expr[i-2] ** expr[i-1]
- expr[i] = result
- del expr[i-2:i]
- i = 0
- else:
- i += 1
- # print(expr)
- return expr[0]
- # x = Formula('2.34 + valueX * 8 - 49879 * (valueY + 178)')
- # print(x.source)
- # print(x.tokens)
- # print(x.rpn)
- # print(x.execute(valueX=2, valueY=3))
- # print(2.34 + 2 * 8 - 49879 * (3 + 178))
- x = Formula(input('>> '))
- for i in range(1, 11):
- print(x.execute(X=i))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement