-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calc_Main.py
152 lines (115 loc) · 3.5 KB
/
Calc_Main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#Group:
#
#Last_Edit: Oct. 10 4:19
#
#Calc_Main.py
#Main Program
import os
from Mult import *
from InputParser import *
from Add import *
from Divide import *
from SquareRoot import *
from Subtract import *
from Nth_root import *
#the .pyc files are created because we are importing them into the main
#simply ignore them, dont bother adding them to github.
def _history():
if os.path.exists('.history'):
history=open('.history','r+b')
for line in history:
pass
else:
history=open('.history','wb')
history.close()
history=open('.history','r+b')
return history
history=_history()
print('Welocome to Calculator!')
while True:
while True:
#parse the users input
ans = -1
arr = inputParser()
if arr==0:
break
#Print ANS history
if arr=='history':
history.close()
hist=open('.history','rb')
lines = hist.readlines()
for i in range(0, len(lines)):
print lines[i]
hist.close()
history=_history()
if arr=='clear':
history.close()
os.remove('.history')
history=_history()
#print arr
args=len(arr)
if args==1:
print ('\n\t'+str(arr[0]))
break
if arr=='invalid input':
print("\n\t"+arr)
break
if args >= 2:
if arr[0] == 'sqrt(': #test for sqrt
op = 'sqrt'
num1=arr[1]
elif arr[0] == 'nroot(':
op = 'nroot'
num1=arr[2]
else:
num1=arr[0]
op=arr[1]
#Use Prev. Ans
if(arr[0] == '*' or arr[0] == '/' or arr[0] == '+' or arr[0] == '-' or arr[0] == '//' or arr[0] == '^' or arr[0] == '%' or arr[0]=='~'):
op=arr[0]
num2=arr[1]
history.close()
hist = open('.history','rb')
lines = hist.readlines()
last = len(lines)
if last==0:
print 'NO HISTORY'
hist.close()
history=_history()
break
num1 = float(lines[last-1].strip())
hist.close()
history=_history()
if(op != '*'and op != '/'and op != '+' and op != '-' and op != '!' and op != '//' and op !="^" and op !='%' and op !='sqrt' and op != 'nroot' and op != '~'):
break
if args >= 3:
num2=arr[2]
print(num1,op,num2)
print('\nCalculating...\n')
#decide op ( + - * / ^ !)
if op == '*':
ans=multiply(num1, num2)
elif op == '!':
ans=fact(int(num1))
elif op == '^':
ans=power(num1, num2)
elif op == '+':
ans=add(num1, num2)
elif op == '/':
ans=divide(num1, num2)
elif op == '//': #int Division
ans=whole(num1, num2)
elif op == '%':
ans=mod(num1, num2)
elif op == '-':
ans=subtract(num1, num2)
elif op == 'sqrt':
ans=squareroot(num1)
elif op == '~':
ans=nth_root(num1,num2)
print '\tANSWER: ', ans
#Write ans to the history file
history.write(str(ans)+'\n')
if arr==0:
break
history.close()