forked from MUICT-SERU/python-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
38 lines (33 loc) · 1.07 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
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b # b - a
def multiply(self, a, b):
result = 0
for i in range(b): # (b + 1)
result = self.add(result, a)
return result
def divide(self, a, b):
result = 0
if b == 0:
raise ZeroDivisionError("Division by zero is undefined.")
while a >= b: # a > b
a = self.subtract(a, b)
result += 1
return result
def modulo(self, a, b):
if b == 0:
raise ZeroDivisionError("Modulo by zero is undefined.")
while a >= b: # a <= b
a = a-b
return 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))