|
1 |
| -""" |
2 |
| -Written by: Shreyas Daniel - github.com/shreydan |
3 |
| -Description: Uses Pythons eval() function |
4 |
| - as a way to implement calculator |
5 |
| -
|
6 |
| -Functions available: |
7 |
| -
|
8 |
| -+ : addition |
9 |
| -- : subtraction |
10 |
| -* : multiplication |
11 |
| -/ : division |
12 |
| -% : percentage |
13 |
| -sine: sin(rad) |
14 |
| -cosine: cos(rad) |
15 |
| -tangent: tan(rad) |
16 |
| -square root: sqrt(n) |
17 |
| -pi: 3.141...... |
18 |
| -""" |
19 |
| - |
20 |
| -import math |
21 |
| -import sys |
22 |
| - |
23 |
| - |
24 |
| -def main(): |
25 |
| - |
26 |
| - def calc(k): |
27 |
| - |
28 |
| - functions = ['sin', 'cos', 'tan', 'sqrt', 'pi'] |
29 |
| - |
30 |
| - for i in functions: |
31 |
| - if i in k.lower(): |
32 |
| - withmath = 'math.' + i |
33 |
| - k = k.replace(i, withmath) |
34 |
| - |
35 |
| - try: |
36 |
| - k = eval(k) |
37 |
| - except ZeroDivisionError: |
38 |
| - |
39 |
| - print("Can't divide by 0") |
40 |
| - exit() |
41 |
| - except NameError: |
42 |
| - print('Invalid input') |
43 |
| - exit() |
44 |
| - |
45 |
| - return k |
46 |
| - |
47 |
| - def result(k): |
48 |
| - k = k.replace(' ', '') |
49 |
| - k = k.replace('^', '**') |
50 |
| - k = k.replace('=', '') |
51 |
| - k = k.replace('?', '') |
52 |
| - k = k.replace('%', '/100') |
53 |
| - |
54 |
| - print("\n" + str(calc(k))) |
55 |
| - |
56 |
| - print("\nScientific Calculator\nEg: pi * sin(90) - sqrt(81)\nEnter quit to exit") |
57 |
| - |
58 |
| - if sys.version_info.major >= 3: |
59 |
| - while True: |
60 |
| - k = input("\nWhat is ") |
61 |
| - if k == 'quit': |
62 |
| - break |
63 |
| - result(k) |
64 |
| - |
65 |
| - else: |
66 |
| - while True: |
67 |
| - k = raw_input("\nWhat is ") |
68 |
| - if k == 'quit': |
69 |
| - break |
70 |
| - result(k) |
71 |
| - |
72 |
| - |
73 |
| -if __name__ == '__main__': |
74 |
| - main() |
| 1 | +""" |
| 2 | +Written by : Shreyas Daniel - github.com/shreydan |
| 3 | +Description : Uses Pythons eval() function |
| 4 | + as a way to implement calculator. |
| 5 | + |
| 6 | +Functions available: |
| 7 | +-------------------------------------------- |
| 8 | + + : addition |
| 9 | + - : subtraction |
| 10 | + * : multiplication |
| 11 | + / : division |
| 12 | + % : percentage |
| 13 | + e : 2.718281... |
| 14 | + pi : 3.141592... |
| 15 | + sine : sin(rad) |
| 16 | + cosine : cos(rad) |
| 17 | + tangent : tan(rad) |
| 18 | + square root : sqrt(n) |
| 19 | + round to nearest integer : round(n) |
| 20 | +convert degrees to radians : rad(deg) |
| 21 | +""" |
| 22 | + |
| 23 | +import math |
| 24 | +import sys |
| 25 | + |
| 26 | + |
| 27 | +def calc(k): |
| 28 | + |
| 29 | + k = k.replace(' ', '') |
| 30 | + k = k.replace('^', '**') |
| 31 | + k = k.replace('=', '') |
| 32 | + k = k.replace('?', '') |
| 33 | + k = k.replace('%', '/100') |
| 34 | + k = k.replace('rad', 'radians') |
| 35 | + |
| 36 | + functions = ['sin', 'cos', 'tan', 'sqrt', 'pi', 'radians', 'e'] |
| 37 | + |
| 38 | + for i in functions: |
| 39 | + if i in k.lower(): |
| 40 | + withmath = 'math.' + i |
| 41 | + k = k.replace(i, withmath) |
| 42 | + |
| 43 | + try: |
| 44 | + k = eval(k) |
| 45 | + except ZeroDivisionError: |
| 46 | + print("Can't divide by 0") |
| 47 | + exit() |
| 48 | + except NameError: |
| 49 | + print('Invalid input') |
| 50 | + exit() |
| 51 | + except AttributeError: |
| 52 | + print('Check usage method') |
| 53 | + exit() |
| 54 | + |
| 55 | + return k |
| 56 | + |
| 57 | + |
| 58 | +def result(k): |
| 59 | + print("\n" + str(calc(k))) |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + |
| 64 | + print("\nScientific Calculator\nEg: sin(rad(90)) + 50% * (sqrt(16)) + round(1.42^2)\nEnter quit to exit") |
| 65 | + |
| 66 | + if sys.version_info.major >= 3: |
| 67 | + while True: |
| 68 | + k = input("\nWhat is ") |
| 69 | + if k == 'quit': |
| 70 | + break |
| 71 | + result(k) |
| 72 | + |
| 73 | + else: |
| 74 | + while True: |
| 75 | + k = raw_input("\nWhat is ") |
| 76 | + if k == 'quit': |
| 77 | + break |
| 78 | + result(k) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + main() |
0 commit comments