-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_calculator_04_hard.py
96 lines (82 loc) · 2.6 KB
/
simple_calculator_04_hard.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
'''
Shows one way to implement a solution to Challenge Exercise 4
(simple calculator)
@author: sql.sith
'''
def calcThis(float1=None,
op1=None,
float2=None,
op2=None,
float3=None):
# handle cases with two operators:
if op2 in ('*', '/'):
partialResult = calcThis(float2, op2, float3)
return(calcThis(float1, op1, partialResult))
elif op2 in ('+', '-'):
partialResult = calcThis(float1, op1, float2)
return(calcThis(partialResult, op2, float3))
# handle cases with one operator:
elif op2 is not None:
_INVALID_OPERATOR2 = "Invalid operator: " + op2
raise(Exception(_INVALID_OPERATOR2))
elif op1 == "+":
return(float1 + float2)
elif op1 == "-":
return(float1 - float2)
elif op1 == "*":
return(float1 * float2)
elif op1 == "/":
return(float1 / float2)
else:
_INVALID_OPERATOR = "Invalid operator: " + op1
raise(Exception(_INVALID_OPERATOR))
# main:
_debug = False
print("Goals 1 through 4\n")
typedInput = raw_input("Enter an arithmetic problem in the form " +
"INT1 OPERATOR INT2 \n" +
" or in the form " +
"INT1 OPERATOR1 INT2 OPERATOR2 INT3: ")
# initialize variables:
firstNumberString = ""
secondNumberString = ""
thirdNumberString = ""
firstOperator = None
secondOperator = None
firstNumber = None
secondNumber = None
thirdNumber = None
# parse everything, another hard way:
for ch in typedInput:
# skip whitespace:
if ch in (" ", "\t"):
# this will go back to the top of the loop:
continue
# detect operators:
elif ch in ("+", "-", "*", "/"):
if firstOperator is None:
firstOperator = ch
else:
secondOperator = ch
# if we get to this else, we should be reading a number:
else:
if firstOperator is None:
firstNumberString += ch
elif secondOperator is None:
secondNumberString += ch
else:
thirdNumberString += ch
firstNumber = float(firstNumberString)
secondNumber = float(secondNumberString)
if thirdNumberString == "":
thirdNumber = None
else:
thirdNumber = float(thirdNumberString)
if _debug:
print("firstNumber: " + str(firstNumber))
print("firstOperator: " + firstOperator)
print("secondNumber: " + str(secondNumber))
print("secondOperator: " + secondOperator)
print("thirdNumber: " + str(thirdNumber))
result = calcThis(firstNumber, firstOperator, secondNumber,
secondOperator, thirdNumber)