Guest User

shitty calculator program

a guest
Jan 1st, 2017
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.12 KB | None | 0 0
  1. import commands
  2. import math
  3.  
  4. print """
  5.    Welcome to the calculator! This is really poorly-made; please forgive that.
  6. If everything is up and running (I put in about an hour or two into testing to
  7. make sure), it can perform log(x) functions, elementary trig functions and their
  8. reciprocals (please put in degrees, as I put in radian conversion for the sake
  9. of dedicating the command to memory without bruteforcing it ;-;), powers,
  10. commonly known constants (pi -> p, e), and of course basic arithmetic.
  11.  
  12.    Seeing as this is written in python, it's not really that impressive. Sorry
  13. about that. Anywho, the calculator is your typical desktop calculator.
  14.  
  15. log_e(x) = ln(x)
  16.  
  17.    Do the following: For the product of 3 * 4...
  18. *
  19. 3
  20. 4
  21. [comp] --------
  22. [comp] 12
  23.  
  24. To include an answer, type 'ans' without the quotes.
  25.  
  26. add sub mul div log exp exit
  27. sin cos tan csc sec cot cls
  28.  
  29. formatted to six decimal places
  30.  
  31. um... If  you need to get ahold of me for criticism or whatnot,
  32. """
  33.  
  34. answer = 0.0
  35. command = ''
  36.  
  37. # basically a shitload of command checks
  38.  
  39. while command != 'x' and command != 'exit':
  40.     arg1 = 0.0
  41.     arg2 = 0.0
  42.     arg = 0.0
  43.     command = raw_input("Command: ")
  44.     if 'add' in command or '+' in command:
  45.         arg1, arg2 = '~', '~'
  46.         while arg1 == '~' or arg2 == '~':
  47.             arg1 = raw_input("Arg1: ")
  48.             if arg1 == 'ans':
  49.                 arg1 = ans
  50.             elif 'e' in arg1:
  51.                 arg1 = commands.euler()
  52.             elif 'p' in arg1:
  53.                 arg1 = commands.pi()
  54.             elif not float(arg1):
  55.                 print "bad arg"
  56.                 arg1 = '~'
  57.             if not arg1 == '~':
  58.                 arg2 = raw_input("Arg2: ")
  59.                 if arg2 == 'ans':
  60.                     arg2 = ans
  61.                 elif 'e' in arg2:
  62.                     arg2 = commands.euler()
  63.                 elif 'p' in arg2:
  64.                     arg2 = commands.pi()
  65.                 elif not float(arg2):
  66.                     print "bad arg"
  67.                     arg1 = '~'
  68.                 if not arg2 == '~':
  69.                     ans = commands.plus(float(arg1),float(arg2))
  70.     elif 'sub' in command or '-' in command:
  71.         arg1, arg2 = '~', '~'
  72.         while arg1 == '~' or arg2 == '~':
  73.             arg1 = raw_input("Arg1: ")
  74.             if arg1 == 'ans':
  75.                 arg1 = ans
  76.             elif 'e' in arg1:
  77.                 arg1 = commands.euler()
  78.             elif 'p' in arg1:
  79.                 arg1 = commands.pi()
  80.             elif not float(arg1):
  81.                 print "bad arg"
  82.                 arg1 = '~'
  83.             if not arg1 == '~':
  84.                 arg2 = raw_input("Arg2: ")
  85.                 if arg2 == 'ans':
  86.                     arg2 = ans
  87.                 elif 'e' in arg2:
  88.                     arg2 = commands.euler()
  89.                 elif 'p' in arg2:
  90.                     arg2 = commands.pi()
  91.                 elif not float(arg2):
  92.                     print "bad arg"
  93.                     arg1 = '~'
  94.                 if not arg2 == '~':
  95.                     ans = commands.minus(float(arg1),float(arg2))
  96.     elif 'mul' in command or '*' in command:
  97.         arg1, arg2 = '~', '~'
  98.         while arg1 == '~' or arg2 == '~':
  99.             arg1 = raw_input("Arg1: ")
  100.             if arg1 == 'ans':
  101.                 arg1 = ans
  102.             elif 'e' in arg1:
  103.                 arg1 = commands.euler()
  104.             elif 'p' in arg1:
  105.                 arg1 = commands.pi()
  106.             elif not float(arg1):
  107.                 print "bad arg"
  108.                 arg1 = '~'
  109.             if not arg1 == '~':
  110.                 arg2 = raw_input("Arg2: ")
  111.                 if arg2 == 'ans':
  112.                     arg2 = ans
  113.                 elif 'e' in arg2:
  114.                     arg2 = commands.euler()
  115.                 elif 'p' in arg2:
  116.                     arg2 = commands.pi()
  117.                 elif not float(arg2):
  118.                     print "bad arg"
  119.                     arg1 = '~'
  120.                 if not arg2 == '~':
  121.                     ans = commands.times(float(arg1),float(arg2))
  122.     elif 'div' in command or '/' in command:
  123.         arg1, arg2 = '~', '~'
  124.         while arg1 == '~' or arg2 == '~':
  125.             arg1 = raw_input("Numerator: ")
  126.             if arg1 == 'ans':
  127.                 arg1 = ans
  128.             elif 'e' in arg1:
  129.                 arg1 = commands.euler()
  130.             elif 'p' in arg1:
  131.                 arg1 = commands.pi()
  132.             elif not float(arg1):
  133.                 print "bad arg"
  134.                 arg1 = '~'
  135.             if not arg1 == '~':
  136.                 arg2 = raw_input("Denominator: ")
  137.                 if arg2 == 'ans':
  138.                     arg2 = ans
  139.                 elif 'e' in arg2:
  140.                     arg2 = commands.euler()
  141.                 elif 'p' in arg2:
  142.                     arg2 = commands.pi()
  143.                 elif not float(arg2):
  144.                     print "bad arg"
  145.                     arg1 = '~'
  146.                 if not arg2 == '~':
  147.                     ans = commands.div(float(arg1),float(arg2))
  148.     elif 'exp' in command or '^' in command:
  149.         arg1, arg2 = '~', '~'
  150.         while arg1 == '~' or arg2 == '~':
  151.             arg1 = raw_input("Base: ")
  152.             if arg1 == 'ans':
  153.                 arg1 = ans
  154.             elif 'e' in arg1:
  155.                 arg1 = commands.euler()
  156.             elif 'p' in arg1:
  157.                 arg1 = commands.pi()
  158.             elif not float(arg1):
  159.                 print "bad arg"
  160.                 arg1 = '~'
  161.             if not arg1 == '~':
  162.                 arg2 = raw_input("Exponent: ")
  163.                 if arg2 == 'ans':
  164.                     arg2 = ans
  165.                 elif 'e' in arg2:
  166.                     arg2 = commands.euler()
  167.                 elif 'p' in arg2:
  168.                     arg2 = commands.pi()
  169.                 elif not float(arg2):
  170.                     print "bad arg"
  171.                     arg1 = '~'
  172.                 if not arg2 == '~':
  173.                     ans = commands.exponential(float(arg1),float(arg2))
  174.     elif 'log' in command:
  175.         arg1, arg2 = '~', '~'
  176.         while arg1 == '~' or arg2 == '~':
  177.             arg1 = raw_input("Log base: ")
  178.             if arg1 == 'ans':
  179.                 arg1 = ans
  180.             elif 'e' in arg1:
  181.                 arg1 = commands.euler()
  182.             elif 'p' in arg1:
  183.                 arg1 = commands.pi()
  184.             elif not float(arg1):
  185.                 print "bad arg"
  186.                 arg1 = '~'
  187.             if not arg1 == '~':
  188.                 arg2 = raw_input("Of? ")
  189.                 if arg2 == 'ans':
  190.                     arg2 = ans
  191.                 elif 'e' in arg2:
  192.                     arg2 = commands.euler()
  193.                 elif 'p' in arg2:
  194.                     arg2 = commands.pi()
  195.                 elif not float(arg2):
  196.                     print "bad arg"
  197.                     arg1 = '~'
  198.                 if not arg2 == '~':
  199.                     ans = math.log(float(arg1),float(arg2))
  200.     elif 'sin' in command:
  201.         arg = '~'
  202.         while arg == '~':
  203.             arg = raw_input("Argument (Degrees): ")
  204.             if 'ans' in arg:
  205.                 arg = ans
  206.             elif 'e' in arg:
  207.                 arg = commands.euler()
  208.             elif 'p' in arg:
  209.                 arg = commands.pi()
  210.             elif not float(arg):
  211.                 print 'bad arg'
  212.                 arg = '~'
  213.             if not arg == '~':
  214.                 ans = math.sin(math.radians(float(arg)))
  215.     elif 'cos' in command:
  216.         arg = '~'
  217.         while arg == '~':
  218.             arg = raw_input("Argument (Degrees): ")
  219.             if 'ans' in arg:
  220.                 arg = ans
  221.             elif 'e' in arg:
  222.                 arg = commands.euler()
  223.             elif 'p' in arg:
  224.                 arg = commands.pi()
  225.             elif not float(arg):
  226.                 print 'bad arg'
  227.                 arg = '~'
  228.             if not arg == '~':
  229.                 ans = math.cos(math.radians(float(arg)))
  230.     elif 'tan' in command:
  231.         arg = '~'
  232.         while arg == '~':
  233.             arg = raw_input("Argument (Degrees): ")
  234.             if 'ans' in arg:
  235.                 arg = ans
  236.             elif 'e' in arg:
  237.                 arg = commands.euler()
  238.             elif 'p' in arg:
  239.                 arg = commands.pi()
  240.             elif not float(arg):
  241.                 print 'bad arg'
  242.                 arg = '~'
  243.             if not arg == '~':
  244.                 ans = math.tan(math.radians(float(arg)))
  245.     elif 'csc' in command:
  246.         arg = '~'
  247.         while arg == '~':
  248.             arg = raw_input("Argument (Degrees): ")
  249.             if 'ans' in arg:
  250.                 arg = ans
  251.             elif 'e' in arg:
  252.                 arg = commands.euler()
  253.             elif 'p' in arg:
  254.                 arg = commands.pi()
  255.             elif not float(arg):
  256.                 print 'bad arg'
  257.                 arg = '~'
  258.             if not arg == '~':
  259.                 ans = 1 / math.sin(math.radians(float(arg)))
  260.     elif 'sec' in command:
  261.         arg = '~'
  262.         while arg == '~':
  263.             arg = raw_input("Argument (Degrees): ")
  264.             if 'ans' in arg:
  265.                 arg = ans
  266.             elif 'e' in arg:
  267.                 arg = commands.euler()
  268.             elif 'p' in arg:
  269.                 arg = commands.pi()
  270.             elif not float(arg):
  271.                 print 'bad arg'
  272.                 arg = '~'
  273.             if not arg == '~':
  274.                 ans = 1 / math.cos(math.radians(float(arg)))
  275.     elif 'cot' in command:
  276.         arg = '~'
  277.         while arg == '~':
  278.             arg = raw_input("Argument (Degrees): ")
  279.             if 'ans' in arg:
  280.                 arg = ans
  281.             elif 'e' in arg:
  282.                 arg = commands.euler()
  283.             elif 'p' in arg:
  284.                 arg = commands.pi()
  285.             elif not float(arg):
  286.                 print 'bad arg'
  287.                 arg = '~'
  288.             if not arg == '~':
  289.                 ans = 1 / math.tan(math.radians(float(arg)))
  290.     elif 'x' in command or 'exit' in command:
  291.         print "Goodbye!"
  292.     elif 'cls' in command or 'clear' in command:
  293.         answer = 0.0
  294.     else:
  295.         print "I don't believe that I understood that."
  296.     print "------------"
  297.     commands.scientific_notation(ans)
Advertisement
Add Comment
Please, Sign In to add comment