Advertisement
Guest User

shitty calculator program

a guest
Jan 1st, 2017
723
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. init.jon@yahoo.com .
  33. """
  34.  
  35. answer = 0.0
  36. command = ''
  37.  
  38. # basically a shitload of command checks
  39.  
  40. while command != 'x' and command != 'exit':
  41.     arg1 = 0.0
  42.     arg2 = 0.0
  43.     arg = 0.0
  44.     command = raw_input("Command: ")
  45.     if 'add' in command or '+' in command:
  46.         arg1, arg2 = '~', '~'
  47.         while arg1 == '~' or arg2 == '~':
  48.             arg1 = raw_input("Arg1: ")
  49.             if arg1 == 'ans':
  50.                 arg1 = ans
  51.             elif 'e' in arg1:
  52.                 arg1 = commands.euler()
  53.             elif 'p' in arg1:
  54.                 arg1 = commands.pi()
  55.             elif not float(arg1):
  56.                 print "bad arg"
  57.                 arg1 = '~'
  58.             if not arg1 == '~':
  59.                 arg2 = raw_input("Arg2: ")
  60.                 if arg2 == 'ans':
  61.                     arg2 = ans
  62.                 elif 'e' in arg2:
  63.                     arg2 = commands.euler()
  64.                 elif 'p' in arg2:
  65.                     arg2 = commands.pi()
  66.                 elif not float(arg2):
  67.                     print "bad arg"
  68.                     arg1 = '~'
  69.                 if not arg2 == '~':
  70.                     ans = commands.plus(float(arg1),float(arg2))
  71.     elif 'sub' in command or '-' in command:
  72.         arg1, arg2 = '~', '~'
  73.         while arg1 == '~' or arg2 == '~':
  74.             arg1 = raw_input("Arg1: ")
  75.             if arg1 == 'ans':
  76.                 arg1 = ans
  77.             elif 'e' in arg1:
  78.                 arg1 = commands.euler()
  79.             elif 'p' in arg1:
  80.                 arg1 = commands.pi()
  81.             elif not float(arg1):
  82.                 print "bad arg"
  83.                 arg1 = '~'
  84.             if not arg1 == '~':
  85.                 arg2 = raw_input("Arg2: ")
  86.                 if arg2 == 'ans':
  87.                     arg2 = ans
  88.                 elif 'e' in arg2:
  89.                     arg2 = commands.euler()
  90.                 elif 'p' in arg2:
  91.                     arg2 = commands.pi()
  92.                 elif not float(arg2):
  93.                     print "bad arg"
  94.                     arg1 = '~'
  95.                 if not arg2 == '~':
  96.                     ans = commands.minus(float(arg1),float(arg2))
  97.     elif 'mul' in command or '*' in command:
  98.         arg1, arg2 = '~', '~'
  99.         while arg1 == '~' or arg2 == '~':
  100.             arg1 = raw_input("Arg1: ")
  101.             if arg1 == 'ans':
  102.                 arg1 = ans
  103.             elif 'e' in arg1:
  104.                 arg1 = commands.euler()
  105.             elif 'p' in arg1:
  106.                 arg1 = commands.pi()
  107.             elif not float(arg1):
  108.                 print "bad arg"
  109.                 arg1 = '~'
  110.             if not arg1 == '~':
  111.                 arg2 = raw_input("Arg2: ")
  112.                 if arg2 == 'ans':
  113.                     arg2 = ans
  114.                 elif 'e' in arg2:
  115.                     arg2 = commands.euler()
  116.                 elif 'p' in arg2:
  117.                     arg2 = commands.pi()
  118.                 elif not float(arg2):
  119.                     print "bad arg"
  120.                     arg1 = '~'
  121.                 if not arg2 == '~':
  122.                     ans = commands.times(float(arg1),float(arg2))
  123.     elif 'div' in command or '/' in command:
  124.         arg1, arg2 = '~', '~'
  125.         while arg1 == '~' or arg2 == '~':
  126.             arg1 = raw_input("Numerator: ")
  127.             if arg1 == 'ans':
  128.                 arg1 = ans
  129.             elif 'e' in arg1:
  130.                 arg1 = commands.euler()
  131.             elif 'p' in arg1:
  132.                 arg1 = commands.pi()
  133.             elif not float(arg1):
  134.                 print "bad arg"
  135.                 arg1 = '~'
  136.             if not arg1 == '~':
  137.                 arg2 = raw_input("Denominator: ")
  138.                 if arg2 == 'ans':
  139.                     arg2 = ans
  140.                 elif 'e' in arg2:
  141.                     arg2 = commands.euler()
  142.                 elif 'p' in arg2:
  143.                     arg2 = commands.pi()
  144.                 elif not float(arg2):
  145.                     print "bad arg"
  146.                     arg1 = '~'
  147.                 if not arg2 == '~':
  148.                     ans = commands.div(float(arg1),float(arg2))
  149.     elif 'exp' in command or '^' in command:
  150.         arg1, arg2 = '~', '~'
  151.         while arg1 == '~' or arg2 == '~':
  152.             arg1 = raw_input("Base: ")
  153.             if arg1 == 'ans':
  154.                 arg1 = ans
  155.             elif 'e' in arg1:
  156.                 arg1 = commands.euler()
  157.             elif 'p' in arg1:
  158.                 arg1 = commands.pi()
  159.             elif not float(arg1):
  160.                 print "bad arg"
  161.                 arg1 = '~'
  162.             if not arg1 == '~':
  163.                 arg2 = raw_input("Exponent: ")
  164.                 if arg2 == 'ans':
  165.                     arg2 = ans
  166.                 elif 'e' in arg2:
  167.                     arg2 = commands.euler()
  168.                 elif 'p' in arg2:
  169.                     arg2 = commands.pi()
  170.                 elif not float(arg2):
  171.                     print "bad arg"
  172.                     arg1 = '~'
  173.                 if not arg2 == '~':
  174.                     ans = commands.exponential(float(arg1),float(arg2))
  175.     elif 'log' in command:
  176.         arg1, arg2 = '~', '~'
  177.         while arg1 == '~' or arg2 == '~':
  178.             arg1 = raw_input("Log base: ")
  179.             if arg1 == 'ans':
  180.                 arg1 = ans
  181.             elif 'e' in arg1:
  182.                 arg1 = commands.euler()
  183.             elif 'p' in arg1:
  184.                 arg1 = commands.pi()
  185.             elif not float(arg1):
  186.                 print "bad arg"
  187.                 arg1 = '~'
  188.             if not arg1 == '~':
  189.                 arg2 = raw_input("Of? ")
  190.                 if arg2 == 'ans':
  191.                     arg2 = ans
  192.                 elif 'e' in arg2:
  193.                     arg2 = commands.euler()
  194.                 elif 'p' in arg2:
  195.                     arg2 = commands.pi()
  196.                 elif not float(arg2):
  197.                     print "bad arg"
  198.                     arg1 = '~'
  199.                 if not arg2 == '~':
  200.                     ans = math.log(float(arg1),float(arg2))
  201.     elif 'sin' in command:
  202.         arg = '~'
  203.         while arg == '~':
  204.             arg = raw_input("Argument (Degrees): ")
  205.             if 'ans' in arg:
  206.                 arg = ans
  207.             elif 'e' in arg:
  208.                 arg = commands.euler()
  209.             elif 'p' in arg:
  210.                 arg = commands.pi()
  211.             elif not float(arg):
  212.                 print 'bad arg'
  213.                 arg = '~'
  214.             if not arg == '~':
  215.                 ans = math.sin(math.radians(float(arg)))
  216.     elif 'cos' in command:
  217.         arg = '~'
  218.         while arg == '~':
  219.             arg = raw_input("Argument (Degrees): ")
  220.             if 'ans' in arg:
  221.                 arg = ans
  222.             elif 'e' in arg:
  223.                 arg = commands.euler()
  224.             elif 'p' in arg:
  225.                 arg = commands.pi()
  226.             elif not float(arg):
  227.                 print 'bad arg'
  228.                 arg = '~'
  229.             if not arg == '~':
  230.                 ans = math.cos(math.radians(float(arg)))
  231.     elif 'tan' in command:
  232.         arg = '~'
  233.         while arg == '~':
  234.             arg = raw_input("Argument (Degrees): ")
  235.             if 'ans' in arg:
  236.                 arg = ans
  237.             elif 'e' in arg:
  238.                 arg = commands.euler()
  239.             elif 'p' in arg:
  240.                 arg = commands.pi()
  241.             elif not float(arg):
  242.                 print 'bad arg'
  243.                 arg = '~'
  244.             if not arg == '~':
  245.                 ans = math.tan(math.radians(float(arg)))
  246.     elif 'csc' in command:
  247.         arg = '~'
  248.         while arg == '~':
  249.             arg = raw_input("Argument (Degrees): ")
  250.             if 'ans' in arg:
  251.                 arg = ans
  252.             elif 'e' in arg:
  253.                 arg = commands.euler()
  254.             elif 'p' in arg:
  255.                 arg = commands.pi()
  256.             elif not float(arg):
  257.                 print 'bad arg'
  258.                 arg = '~'
  259.             if not arg == '~':
  260.                 ans = 1 / math.sin(math.radians(float(arg)))
  261.     elif 'sec' in command:
  262.         arg = '~'
  263.         while arg == '~':
  264.             arg = raw_input("Argument (Degrees): ")
  265.             if 'ans' in arg:
  266.                 arg = ans
  267.             elif 'e' in arg:
  268.                 arg = commands.euler()
  269.             elif 'p' in arg:
  270.                 arg = commands.pi()
  271.             elif not float(arg):
  272.                 print 'bad arg'
  273.                 arg = '~'
  274.             if not arg == '~':
  275.                 ans = 1 / math.cos(math.radians(float(arg)))
  276.     elif 'cot' in command:
  277.         arg = '~'
  278.         while arg == '~':
  279.             arg = raw_input("Argument (Degrees): ")
  280.             if 'ans' in arg:
  281.                 arg = ans
  282.             elif 'e' in arg:
  283.                 arg = commands.euler()
  284.             elif 'p' in arg:
  285.                 arg = commands.pi()
  286.             elif not float(arg):
  287.                 print 'bad arg'
  288.                 arg = '~'
  289.             if not arg == '~':
  290.                 ans = 1 / math.tan(math.radians(float(arg)))
  291.     elif 'x' in command or 'exit' in command:
  292.         print "Goodbye!"
  293.     elif 'cls' in command or 'clear' in command:
  294.         answer = 0.0
  295.     else:
  296.         print "I don't believe that I understood that."
  297.     print "------------"
  298.     commands.scientific_notation(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement