Guest User

Untitled

a guest
Mar 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. from sys import exit
  2.  
  3. #Whenever is_number(x) exists, it is checking to see if x is a number, obviously.
  4. def is_number(item):
  5. try:
  6. float(item)
  7. return True
  8. except ValueError:
  9. return False
  10.  
  11.  
  12. def set_up_list():
  13. #First part deletes whitespace and makes list
  14. astring = raw_input("Calculation: ")
  15. #First it will check to see if any non_supported characters are in the user input
  16. for item in astring:
  17. if item not in ["0", "1", "2", "3" , "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "(", ")"]:
  18. print ("Unsupported Character: " + item)
  19. exit()
  20. astring = astring.replace(" ", "")
  21. a_list = []
  22. for item in astring:
  23. a_list.append(item)
  24. count = 0
  25. #Next part combines individual numbers into actual numbers based on user input
  26. while count < len(a_list) - 1:
  27. if is_number(a_list[count]) and a_list[count + 1] in ["(", ")"]:
  28. print ("Program does not accept parentheses directly after number, must have operator in between.")
  29. exit()
  30. if is_number(a_list[count]) and is_number(a_list[count + 1]):
  31. a_list[count] += a_list[count + 1]
  32. del a_list[count + 1]
  33. elif is_number(a_list[count]) and a_list[count + 1] == ".":
  34. try:
  35. x = a_list[count+2]
  36. except IndexError:
  37. print ("Your formatting is off somehow.")
  38. exit()
  39. if is_number(a_list[count + 2]):
  40. a_list[count] += a_list[count + 1] + a_list[count + 2]
  41. del a_list[count + 2]
  42. del a_list[count + 1]
  43. else:
  44. count += 1
  45. return a_list
  46.  
  47.  
  48. def perform_operation(n1, operand, n2):
  49. if operand == "+":
  50. return str(float(n1) + float(n2))
  51. elif operand == "-":
  52. return str(float(n1) - float(n2))
  53. elif operand == "*":
  54. return str(float(n1) * float(n2))
  55. elif operand == "/":
  56. try:
  57. n = str(float(n1) / float(n2))
  58. return n
  59. except ZeroDivisionError:
  60. print ("You tried to divide by 0.")
  61. print ("Just for that I am going to terminate myself")
  62. exit()
  63.  
  64.  
  65. expression = set_up_list()
  66. emergency_count = 0
  67. #Makes code shorter, short for parentheses
  68. P = ["(", ")"]
  69. #If the length of the list is 1, there is only 1 number, meaning an answer has been reached.
  70. while len(expression) != 1:
  71. #If there are parentheses around a single list item, the list item is obviously just a number, eliminate parentheses
  72. #Will check to see if the first parentheses exists first so that it does not throw an index error
  73. count = 0
  74. while count < len(expression) - 1:
  75. if expression[count] == "(":
  76. if expression[count + 2] == ")":
  77. del expression[count + 2]
  78. del expression[count]
  79. count += 1
  80. #After that is done, it will multiply and divide what it can
  81. count = 0
  82. while count < len(expression) - 1:
  83. if expression[count] in ["*", "/"] and not (expression[count+1] in P or expression[count-1] in P):
  84. expression[count - 1] = perform_operation(expression[count - 1], expression[count], expression[count + 1])
  85. del expression[count + 1]
  86. del expression[count]
  87. count += 1
  88. #Then it will add and subtact what it can
  89. count = 0
  90. while count < len(expression) - 1:
  91. if expression[count] in ["+", "-"] and not (expression[count+1] in P or expression[count-1] in P):
  92. expression[count - 1] = perform_operation(expression[count - 1], expression[count], expression[count + 1])
  93. del expression[count + 1]
  94. del expression[count]
  95. count += 1
  96. #The steps will repeat until only one character is left. Operations that fail will be stopped by emergency count.
  97. emergency_count += 1
  98. if emergency_count >= 1000:
  99. print ("Operation was too long or was bugged")
  100. exit()
  101.  
  102. print (float(expression[0]))
Add Comment
Please, Sign In to add comment