Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. '''
  2. Math
  3. Eat your heart out wolfram alpha
  4. '''
  5.  
  6. ans = None
  7.  
  8. def error(type):
  9.     print("Error", type + ":")
  10.     if type == "UNMATCHED_BRACKET":
  11.         print("\tFound an unclosed \"(\", cannot solve expression")
  12.         return None
  13.     elif type == "INVALID_TERM":
  14.         print("\tExpresson contains invalid term that is neither an operator nor a number")
  15.         return None
  16.     else:
  17.         print("\tMissing definition for error")
  18.  
  19. def solve(expr, explain = False):
  20.     #doesn't ccount for negative terms
  21.  
  22.     global ans
  23.     terms = expr.split(" ")
  24.     for t in range(0, len(terms)):
  25.         if terms[t] != "*" and terms[t] != "/" and terms[t] != "+" and terms[t] != "-" and terms[t] != "^":
  26.             try:
  27.                 float(terms[t])
  28.             except ValueError:
  29.                 return error("INVALID_TERM")
  30.     for t in range(0, len(terms)):
  31.         if terms[t] == "ans":
  32.             terms[t] = ans
  33.     for t in range(0, len(terms)):
  34.         try:
  35.             if terms[t] == "^":
  36.                 if explain:
  37.                     print("\t" + str(float(terms[t-1]) ** float(terms[t+1])))
  38.                 terms.insert(t + 2,str(float(terms[t-1]) ** float(terms[t+1])))
  39.                 terms.pop(t+1)
  40.                 terms.pop(t)
  41.                 terms.pop(t-1)
  42.         except IndexError:
  43.             #due to popping, the for loop will go past the end of the list, so catch this and break the loop
  44.             break
  45.     for t in range(0, len(terms)):
  46.         try:
  47.             if terms[t] == "/":
  48.                 terms.pop(t-1)
  49.                 print("\t" + str(float(terms[t-1]) / float(terms[t+1])))
  50.                 terms.insert(t + 2,str(float(terms[t-1]) / float(terms[t+1])))
  51.                 terms.pop(t+1)
  52.                 terms.pop(t)
  53.             elif terms[t] == "*":
  54.                 if explain:
  55.                     print("\t" + str(float(terms[t-1]) * float(terms[t+1])))
  56.                 terms.insert(t + 2,str(float(terms[t-1]) * float(terms[t+1])))
  57.                 terms.pop(t+1)
  58.                 terms.pop(t)
  59.                 terms.pop(t-1)
  60.         except IndexError:
  61.             #due to popping, the for loop will go past the end of the list, so catch this and break the loop
  62.             break
  63.     for t in range(0, len(terms)):
  64.         try:
  65.             if terms[t] == "+":
  66.                 if explain:
  67.                     print("\t" + str(float(terms[t-1]) + float(terms[t+1])))
  68.                 terms.insert(t + 2,str(float(terms[t-1]) + float(terms[t+1])))
  69.                 terms.pop(t+1)
  70.                 terms.pop(t)
  71.                 terms.pop(t-1)
  72.             elif terms[t] == "-":
  73.                 if explain:
  74.                     print("\t" + str(float(terms[t-1]) - float(terms[t+1])))
  75.                 terms.insert(t + 2,str(float(terms[t-1]) - float(terms[t+1])))
  76.                 terms.pop(t+1)
  77.                 terms.pop(t)
  78.                 terms.pop(t-1)
  79.         except IndexError:
  80.             #due to popping, the for loop will go past the end of the list, so catch this and break the loop
  81.             break
  82.     ans = "".join(terms)
  83.     return "".join(terms)
  84.  
  85. def parse(expr, explain = False):
  86.     if "(" in expr:
  87.         open_bracket = expr.find("(")
  88.         close_bracket = None
  89.         ob = 0
  90.         if ")" in expr:
  91.             for c in range(open_bracket, len(expr)):
  92.                 if expr[c] == ")":
  93.                     close_bracket = c
  94.             if explain:
  95.                 print(expr, "\n" + " " * open_bracket + " ^")
  96.             expr = expr.replace(expr[open_bracket:close_bracket+1], parse(expr[open_bracket + 1:close_bracket], explain), 1)
  97.         else:
  98.             return error("UNMATCHED_BRACKET")
  99.  
  100.     return solve(expr, explain)
  101.  
  102. def interperit():
  103.     while True:
  104.         command = input(">")
  105.         if command == "":
  106.             return None
  107.         elif command[0] == "?":
  108.             parse(command[1:], True)
  109.         else:
  110.             answer = parse(command)
  111.             print(answer if answer != None else "")
  112.  
  113. interperit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement