-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputParser.py
72 lines (62 loc) · 1.91 KB
/
InputParser.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
import string
def inputParser():
word=''
inArr=[]
inString=raw_input('\ninput: ').strip()
if len(inString)==0:
return 0
if inString=='history':
return inString
if inString=='clear':
return inString
if inString[:5]=='sqrt(':
inArr=['sqrt(']
inString=inString[5:]
if inString.find('rt(')!=-1:
if inString.find('~')!=-1:
return('invalid input')
inString=inString.replace(')','')
inString=inString.replace('rt(', '~')
inString=inString.replace('pi()',"3.141592653589793")
inString=inString.replace('e()',"2.718281828459045")
for char in inString:
if char.isdigit() or char=='.':
word+=char
elif char.isdigit()==False:
if char.isalpha():
return('invalid input')
try:
inArr+=[float(word)]
except:
pass
word=''
inArr+=[char]
try:
inArr+=[float(word)]
except:
None #None for fact to work
#test for //
for i in range(len(inArr)-1):
if inArr[i]=='/' and inArr[i+1]=='/':
inArr[i]='//'
inArr[i+1]='//'
inArr=inArr[:i]+inArr[i+1:]
if inArr[0]=='-':
inArr[1]*=-1
inArr=inArr[1:]
#test for negative numbers
for i in range(2,len(inArr)-1):
if inArr[i]=='-':
if type(inArr[i-1]) is str and type(inArr[i+1]) is float:
inArr[i+1]*=-1
inArr=inArr[:i]+inArr[i+1:]
#test for double characters
for i in range(len(inArr)-1):
if type(inArr[i])!=float and type(inArr[i+1])!=float:
return("invalid input")
#don't allow more than 2 numbers and an operation
if len(inArr)>3:
return("invalid input")
#return array of inputs
return(inArr)
#print(inputParser())