-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
72 lines (48 loc) · 1.23 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
from abc import *
class Operator(ABC):
@abstractclassmethod
def calculate(self, a, b):
pass
@abstractclassmethod
def get_symbol(self):
pass
# o = Operator()
class Calculator:
def __init__(self):
self.operators = []
def register(self, op):
self.operators.append(op)
def calculate(self):
a = float(input("Value 1: "))
op = input("Operator: ")
b = float(input("Value 2: "))
for x in self.operators:
if x.get_symbol() == op:
print(a, x.get_symbol(), b, "=", x.calculate(a, b))
class Addition(Operator):
def calculate(self, a, b):
return a + b
def get_symbol(self):
return "+"
class Subtraction(Operator):
def calculate(self, a, b):
return a - b
def get_symbol(self):
return "-"
class Multiplication(Operator):
def calculate(self, a, b):
return a * b
def get_symbol(self):
return "*"
class Division(Operator):
def calculate(self, a, b):
return a / b
def get_symbol(self):
return "/"
c = Calculator()
add = Addition()
c.register(add)
c.register(Subtraction())
c.register(Multiplication())
c.register(Division())
c.calculate()