Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Math
- Eat your heart out wolfram alpha
- '''
- ans = None
- def error(type):
- print("Error", type + ":")
- if type == "UNMATCHED_BRACKET":
- print("\tFound an unclosed \"(\", cannot solve expression")
- return None
- elif type == "INVALID_TERM":
- print("\tExpresson contains invalid term that is neither an operator nor a number")
- return None
- else:
- print("\tMissing definition for error")
- def solve(expr, explain = False):
- #doesn't ccount for negative terms
- global ans
- terms = expr.split(" ")
- for t in range(0, len(terms)):
- if terms[t] != "*" and terms[t] != "/" and terms[t] != "+" and terms[t] != "-" and terms[t] != "^":
- try:
- float(terms[t])
- except ValueError:
- return error("INVALID_TERM")
- for t in range(0, len(terms)):
- if terms[t] == "ans":
- terms[t] = ans
- for t in range(0, len(terms)):
- try:
- if terms[t] == "^":
- if explain:
- print("\t" + str(float(terms[t-1]) ** float(terms[t+1])))
- terms.insert(t + 2,str(float(terms[t-1]) ** float(terms[t+1])))
- terms.pop(t+1)
- terms.pop(t)
- terms.pop(t-1)
- except IndexError:
- #due to popping, the for loop will go past the end of the list, so catch this and break the loop
- break
- for t in range(0, len(terms)):
- try:
- if terms[t] == "/":
- terms.pop(t-1)
- print("\t" + str(float(terms[t-1]) / float(terms[t+1])))
- terms.insert(t + 2,str(float(terms[t-1]) / float(terms[t+1])))
- terms.pop(t+1)
- terms.pop(t)
- elif terms[t] == "*":
- if explain:
- print("\t" + str(float(terms[t-1]) * float(terms[t+1])))
- terms.insert(t + 2,str(float(terms[t-1]) * float(terms[t+1])))
- terms.pop(t+1)
- terms.pop(t)
- terms.pop(t-1)
- except IndexError:
- #due to popping, the for loop will go past the end of the list, so catch this and break the loop
- break
- for t in range(0, len(terms)):
- try:
- if terms[t] == "+":
- if explain:
- print("\t" + str(float(terms[t-1]) + float(terms[t+1])))
- terms.insert(t + 2,str(float(terms[t-1]) + float(terms[t+1])))
- terms.pop(t+1)
- terms.pop(t)
- terms.pop(t-1)
- elif terms[t] == "-":
- if explain:
- print("\t" + str(float(terms[t-1]) - float(terms[t+1])))
- terms.insert(t + 2,str(float(terms[t-1]) - float(terms[t+1])))
- terms.pop(t+1)
- terms.pop(t)
- terms.pop(t-1)
- except IndexError:
- #due to popping, the for loop will go past the end of the list, so catch this and break the loop
- break
- ans = "".join(terms)
- return "".join(terms)
- def parse(expr, explain = False):
- if "(" in expr:
- open_bracket = expr.find("(")
- close_bracket = None
- ob = 0
- if ")" in expr:
- for c in range(open_bracket, len(expr)):
- if expr[c] == ")":
- close_bracket = c
- if explain:
- print(expr, "\n" + " " * open_bracket + " ^")
- expr = expr.replace(expr[open_bracket:close_bracket+1], parse(expr[open_bracket + 1:close_bracket], explain), 1)
- else:
- return error("UNMATCHED_BRACKET")
- return solve(expr, explain)
- def interperit():
- while True:
- command = input(">")
- if command == "":
- return None
- elif command[0] == "?":
- parse(command[1:], True)
- else:
- answer = parse(command)
- print(answer if answer != None else "")
- interperit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement