forked from MUICT-SERU/python-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
54 lines (43 loc) · 1.34 KB
/
calculator.py
File metadata and controls
54 lines (43 loc) · 1.34 KB
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
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
result = 0
for i in range(abs(b)):
result = self.add(result, a)
if b<0:
result = -result
return result
def divide(self, a, b):
if b==0:
raise ZeroDivisionError("Cannot divide by 0")
abs_a = abs(a)
abs_b = abs(b)
result = 0
while abs_a >= abs_b:
abs_a = self.subtract(abs_a, abs_b)
result += 1
if (a<0) ^ (b<0) :
result = -result
return result
def modulo(self, a, b):
if b==0:
raise ZeroDivisionError("Cannot divide by 0")
abs_a = abs(a)
abs_b = abs(b)
while abs_a >= abs_b:
abs_a = self.subtract(abs_a, abs_b)
if (a<0) ^ (b<0):
abs_a
return abs_a if a>=0 else -abs_a
# Example usage:
if __name__ == "__main__":
calc = Calculator()
print("This is a simple calculator class!")
print("Example: addition: ", calc.add(1, 2))
print("Example: subtraction: ", calc.subtract(4, 2))
print("Example: multiplication: ", calc.multiply(2, 3))
print("Example: division: ", calc.divide(10, 2))
print("Example: modulo: ", calc.modulo(10, 3))