-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb1d029
commit b5b462f
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import re | ||
class Calculator: | ||
def __init__(self): | ||
self.num1 = None | ||
self.num2 = None | ||
|
||
def add(self): | ||
return self.num1 + self.num2 | ||
|
||
def subtract(self): | ||
return self.num1 - self.num2 | ||
|
||
def multiply(self): | ||
return self.num1 * self.num2 | ||
|
||
def division(self): | ||
if self.num2 != 0: | ||
return self.num1 / self.num2 | ||
else: | ||
return "Error: Division by zero is not allowed" | ||
|
||
def modulus(self): | ||
if self.num2 != 0: | ||
return self.num1 % self.num2 | ||
else: | ||
return "Error: Division by zero is not allowed" | ||
|
||
def operations(self, operations): | ||
|
||
match operations: | ||
case "+": | ||
return self.add() | ||
case "-": | ||
return self.subtract() | ||
case "*": | ||
return self.multiply() | ||
case "/": | ||
return self.division() | ||
case "%": | ||
return self.modulus() | ||
|
||
print("Basic Calculator - Simple Class") | ||
print("Operations") | ||
print("+" , end="\n") | ||
print("-" , end="\n") | ||
print("*" , end="\n") | ||
print("/" , end="\n") | ||
print("%" , end="\n") | ||
calc = Calculator() | ||
cache = 0 | ||
while(input != 'exit'): | ||
value = input() | ||
if value == 'clear': | ||
cache = 0 | ||
else: | ||
splittedValue = re.split(r'([+\-*/%])', value) | ||
print(f"{int(splittedValue[0])} {splittedValue[1]} {int(splittedValue[2])} = ") | ||
calc.num1 = int(splittedValue[0]) | ||
calc.num2 = int(splittedValue[2]) | ||
output = calc.operations(splittedValue[1]) | ||
print(output) | ||
cache += output |