Skip to content

Commit

Permalink
feat: Merge pull request #3 from unibo-dtm-se/feature/trigonometry
Browse files Browse the repository at this point in the history
Feature/trigonometry
  • Loading branch information
gciatto authored May 7, 2024
2 parents 3795f39 + d2ef2fb commit 28e9b87
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
8 changes: 7 additions & 1 deletion calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ def power(self):

def logarithm(self):
self._append("log")

def cos(self):
self._append("cos")

def sin(self):
self._append("sin")

def compute_result(self) -> Number:
try:
from math import sqrt
from math import sqrt, cos, sin
result = eval(self.expression)
if isinstance(result, Number):
self.expression = str(result)
Expand Down
8 changes: 7 additions & 1 deletion calculator/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
['1', '2', '3', '-'],
['.', '0', '=', '+'],
['(', '√', '^', ')'],
['log'],
['log', 'cos', 'sin'],
]


Expand Down Expand Up @@ -81,6 +81,12 @@ def on_button_press(self, button):
case "log":
self._calc.logarithm()
self._calc.open_parenthesis()
case "cos":
self._calc.cos()
self._calc.open_parenthesis()
case "sin":
self._calc.sin()
self._calc.open_parenthesis()
case _:
self._calc.digit(button.text)
self.display.text = self._calc.expression or "0"
Expand Down
19 changes: 18 additions & 1 deletion tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,21 @@ def test_expression_with_pow(self):
self.assert_display("2**3")
self.press_button("=")
self.assert_display("8")


def test_expression_with_cos(self):
# cos(0) = 1
self.press_button("cos")
self.press_button("0")
self.press_button(")")
self.assert_display("cos(0)")
self.press_button("=")
self.assert_display("1.0")

def test_expression_with_sin(self):
# sin(0) = 0
self.press_button("sin")
self.press_button("0")
self.press_button(")")
self.assert_display("sin(0)")
self.press_button("=")
self.assert_display("0.0")
23 changes: 23 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,26 @@ def test_expression_with_pow(self):
self.calculator.digit(3)
self.assertEqual("(1+1)**3", self.calculator.expression)
self.assertEqual(8, self.calculator.compute_result())

def test_cosine(self):
# cos(0) = 1
self.assertEqual("", self.calculator.expression)
self.calculator.cos()
self.calculator.open_parenthesis()
self.assertEqual("cos(", self.calculator.expression)
self.calculator.digit(0)
self.calculator.close_parenthesis()
self.assertEqual("cos(0)", self.calculator.expression)
self.assertEqual(1, self.calculator.compute_result())

def test_sine(self):
# sin(0) = 0
self.assertEqual("", self.calculator.expression)
self.calculator.sin()
self.calculator.open_parenthesis()
self.assertEqual("sin(", self.calculator.expression)
self.calculator.digit(0)
self.calculator.close_parenthesis()
self.assertEqual("sin(0)", self.calculator.expression)
self.assertEqual(0, self.calculator.compute_result())

0 comments on commit 28e9b87

Please sign in to comment.