Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A simple and efficient Scientific/Software Engineering Calculator built to perform a variety of mathematical and logical operations. This project is designed to demonstrate core programming concepts, modular design, and user interaction.
Binary file added __pycache__/calculator.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/exceptions.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/test_calculator.cpython-313.pyc
Binary file not shown.
35 changes: 35 additions & 0 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
import re
from trigonometry_module.trigonometric import Trigonometric

class Calculator:

def __init__(self):
self.trig = Trigonometric()

def add(self, a, b):
return a + b

def subtract(self, a, b):
return a - b

def multiply(self, a, b):
return a * b

def divide(self, a, b):
if b == 0:
raise ValueError("Division by zero")
return a / b

def evaluate(self, expr):
expr = expr.replace(" ", "")

functions = [
("asin", self.trig.asin),
("acos", self.trig.acos),
("atan", self.trig.atan),
("sinh", self.trig.sinh),
("cosh", self.trig.cosh),
("tanh", self.trig.tanh),
("sin", self.trig.sin),
("cos", self.trig.cos),
("tan", self.trig.tan),
]

for name, func in functions:
expr = re.sub(rf'{name}\((.*?)\)', lambda m: str(func(m.group(1))) if m.group(1) != "" else (_ for _ in ()).throw(ValueError("Empty input")), expr)

try:
return eval(expr)
except ZeroDivisionError:
raise ValueError("Division by zero")
except Exception:
raise ValueError("Invalid expression")
18 changes: 18 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CalculatorError(Exception):
"""Base class for calculator-related errors."""
pass


class InvalidExpressionError(CalculatorError):
"""Raised when the input expression is invalid."""
pass


class DivisionByZeroError(CalculatorError):
"""Raised when division by zero is attempted."""
pass


class DomainError(CalculatorError):
"""Raised when a trig function gets an invalid domain value."""
pass
Empty file added trigonometry_module/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
76 changes: 76 additions & 0 deletions trigonometry_module/test_trigonometric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import unittest
from calculator import Calculator

class TestTrigonometric(unittest.TestCase):

def setUp(self):
self.calc = Calculator()

def test_sin(self):
self.assertAlmostEqual(self.calc.evaluate("sin(30)"), 0.5, places=2)

def test_cos(self):
self.assertAlmostEqual(self.calc.evaluate("cos(60)"), 0.5, places=2)

def test_tan(self):
self.assertAlmostEqual(self.calc.evaluate("tan(45)"), 1.0, places=2)

def test_expression(self):
self.assertAlmostEqual(self.calc.evaluate("2 + 3*sin(30)"), 3.5, places=2)

def test_inverse(self):
self.assertAlmostEqual(self.calc.evaluate("asin(0.5)"), 30, places=2)

def test_hyperbolic(self):
self.assertAlmostEqual(self.calc.evaluate("sinh(0)"), 0.0, places=2)

#more test cases:
def test_sin_zero(self):
self.assertAlmostEqual(self.calc.evaluate("sin(0)"), 0.0, places=2)

def test_cos_zero(self):
self.assertAlmostEqual(self.calc.evaluate("cos(0)"), 1.0, places=2)

def test_tan_zero(self):
self.assertAlmostEqual(self.calc.evaluate("tan(0)"), 0.0, places=2)

def test_asin_one(self):
self.assertAlmostEqual(self.calc.evaluate("asin(1)"), 90.0, places=2)

def test_acos_one(self):
self.assertAlmostEqual(self.calc.evaluate("acos(1)"), 0.0, places=2)

def test_expression_mix(self):
self.assertAlmostEqual(self.calc.evaluate("5 + 2*cos(60)"), 6.0, places=2)


def test_invalid_expression(self):
with self.assertRaises(ValueError):
self.calc.evaluate("sin()")

def test_invalid_function(self):
with self.assertRaises(ValueError):
self.calc.evaluate("sinn(30)")

def test_division_by_zero(self):
with self.assertRaises(DivisionByZeroError):
self.calc.evaluate("10/0")

def test_invalid_expression(self):
with self.assertRaises(InvalidExpressionError):
self.calc.evaluate("2 + * 3")

def test_asin_domain_error(self):
with self.assertRaises(DomainError):
self.calc.evaluate("asin(2)")

def test_empty_expression(self):
with self.assertRaises(InvalidExpressionError):
self.calc.evaluate("")

def test_non_string_expression(self):
with self.assertRaises(InvalidExpressionError):
self.calc.evaluate(123)

if __name__ == "__main__":
unittest.main()
54 changes: 54 additions & 0 deletions trigonometry_module/trigonometric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import math
from exceptions import DomainError

class Trigonometric:

def to_radians(self, x):
return math.radians(float(x))

def sin(self, x):
return math.sin(self.to_radians(x))

def cos(self, x):
return math.cos(self.to_radians(x))

def tan(self, x):
x = float(x)

# tan is undefined at 90 + k*180
if x % 180 == 90:
raise ValueError("tan undefined at 90 + k*180")

return math.tan(self.to_radians(x))

def asin_domain(self, x):
x = float(x)
if x < -1 or x > 1:
raise ValueError("asin domain is [-1,1]")
return math.degrees(math.asin(x))

def acos_domain(self, x):
x = float(x)
if x < -1 or x > 1:
raise ValueError("acos domain is [-1,1]")
return math.degrees(math.acos(x))


def asin_value(self, x):
return math.degrees(math.asin(float(x)))

def acos_value(self, x):
return math.degrees(math.acos(float(x)))

def atan(self, x):
return math.degrees(math.atan(float(x)))

def sinh(self, x):
return math.sinh(float(x))

def cosh(self, x):
return math.cosh(float(x))

def tanh(self, x):
return math.tanh(float(x))