forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
111 lines (86 loc) · 2.88 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Written by : Shreyas Daniel - github.com/shreydan
Description : Uses Pythons eval() function
as a way to implement calculator.
Functions available are:
--------------------------------------------
+ : addition
- : subtraction
* : multiplication
/ : division
% : percentage
e : 2.718281...
pi : 3.141592...
sine : sin(rad)
cosine : cos(rad)
tangent : tan(rad)
remainder : XmodY
square root : sqrt(n)
round to nearest integer : round(n)
convert degrees to radians : rad(deg)
"""
import math
import sys
def calc(term):
"""
input: term of type str
output: returns the result of the computed term.
purpose: This function is the actual calculator and the heart of the application
"""
# This part is for reading and converting arithmic terms.
term = term.replace(' ', '')
term = term.replace('^', '**')
term = term.replace('=', '')
term = term.replace('?', '')
term = term.replace('%', '/100')
term = term.replace('rad', 'radians')
term = term.replace('mod', '%')
functions = ['sin', 'cos', 'tan', 'cosh', 'sinh', 'tanh', 'sqrt', 'pi', 'radians', 'e']
# This part is for reading and converting function expressions.
for function in functions:
if function in term.lower():
withmath = 'math.' + function
term = term.replace(function, withmath)
try:
# here goes the actual evaluating.
term = eval(term)
# here goes to the error cases.
except ZeroDivisionError:
print("Can't divide by 0")
exit(1) # exit(1) for indicating an error.
except NameError:
print('Invalid input')
exit(1)
except AttributeError:
print('Check usage method')
exit(1)
return term
def result(term):
"""
input: term of type str
output: none
purpose: passes the argument to the function calc(...) and
prints the result onto console.
"""
print("\n" + str(calc(term)))
def main():
"""
main-program
purpose: handles the user inputs and prints
some informations onto console.
"""
print("\nScientific Calculator\nEg: sin(rad(90)) + 50% * (sqrt(16)) + round(1.42^2) - 12mod3\nEnter quit to exit")
if sys.version_info.major >= 3:
while True:
k = input("\nWhat is ")
if k == 'quit':
break
result(k)
else:
while True:
k = raw_input("\nWhat is ")
if k == 'quit':
break
result(k)
if __name__ == '__main__':
main()