diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/.gitignore b/1ano/2semestre/labi/tema02/Calculater/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/Calculater.iml b/1ano/2semestre/labi/tema02/Calculater/.idea/Calculater.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/Calculater.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/discord.xml b/1ano/2semestre/labi/tema02/Calculater/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/inspectionProfiles/Project_Default.xml b/1ano/2semestre/labi/tema02/Calculater/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..49fdfc7 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/inspectionProfiles/profiles_settings.xml b/1ano/2semestre/labi/tema02/Calculater/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/misc.xml b/1ano/2semestre/labi/tema02/Calculater/.idea/misc.xml new file mode 100644 index 0000000..a971a2c --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/.idea/modules.xml b/1ano/2semestre/labi/tema02/Calculater/.idea/modules.xml new file mode 100644 index 0000000..95994c2 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/aritmetica.py b/1ano/2semestre/labi/tema02/Calculater/aritmetica.py new file mode 100644 index 0000000..fe78c46 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/aritmetica.py @@ -0,0 +1,25 @@ +import math + +def soma(a, b): + return round(a + b, 2) + +def subtracao(a, b): + return round(a - b, 2) + +def multiplicacao(a, b): + return round(a * b, 2) + +def divisao(a, b): + if b == 0: + raise ZeroDivisionError("Não é possível dividir por zero.") + return round(a / b, 2) + +def resto_divisao_inteira(a, b): + if b == 0: + raise ZeroDivisionError("Não é possível dividir por zero.") + return round(a % b, 2) + +def raiz_quadrada(a): + if a < 0: + raise ValueError("Não é possível calcular a raiz quadrada de um número negativo.") + return round(math.sqrt(a), 2) diff --git a/1ano/2semestre/labi/tema02/Calculater/calculadora.py b/1ano/2semestre/labi/tema02/Calculater/calculadora.py new file mode 100644 index 0000000..0f6f95d --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/calculadora.py @@ -0,0 +1,138 @@ +import pytest +import aritmetica + + + +def main(): + print("Welcome to the Calculator!") + + while True: + print("Please choose an operation:") + print("1. Addition (+)") + print("2. Subtraction (-)") + print("3. Multiplication (*)") + print("4. Division (/)") + print("5. Resto (%)") + print("6. Square Root (sqrt)") + print("7. Exit") + + choice = input("Enter your choice (1-6): ") + + if choice == '7': + print("Goodbye!") + break + elif choice == '1': + while True: + try: + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + except ValueError as e: + print("Invalid input. Please enter a number.") + print("Error: ", e.__doc__) + continue + + result = aritmetica.soma(num1, num2) + + print(f"{num1} + {num2} = {result}") + + exit = input('To continue operation press ( y ) ?') + + if exit.upper() != 'Y': + break + elif choice == '2': + while True: + try: + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + except ValueError as e: + print("Invalid input. Please enter a number.") + print("Error: ", e.__doc__) + continue + + result = aritmetica.subtracao(num1, num2) + + print(f"{num1} - {num2} = {result}") + + exit = input('To continue operation press ( y ) ?') + + if exit.upper() != 'Y': + break + elif choice == '3': + while True: + try: + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + except ValueError as e: + print("Invalid input. Please enter a number.") + print("Error: ", e.__doc__) + continue + + result = aritmetica.multiplicacao(num1, num2) + + print(f"{num1} * {num2} = {result}") + + exit = input('To continue operation press ( y ) ?') + + if exit.upper() != 'Y': + break + elif choice == '4': + while True: + try: + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + except ValueError as e: + print("Invalid input. Please enter a number.") + print("Error: ", e.__doc__) + continue + + result = aritmetica.divisao(num1, num2) + + print(f"{num1} / {num2} = {result}") + + exit = input('To continue operation press ( y ) ?') + + if exit.upper() != 'Y': + break + elif choice == '5' : + while True: + try: + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + except ValueError as e: + print("Invalid input. Please enter a number.") + print("Error: ", e.__doc__) + continue + + result = aritmetica.resto_divisao_inteira(num1, num2) + + print(f"{num1} % {num2} = {result}") + + exit = input('To continue operation press ( y ) ?') + + if exit.upper() != 'Y': + break + elif choice == '6': + while True: + try: + num1 = float(input("Enter the first number: ")) + except ValueError as e: + print("Invalid input. Please enter a number.") + print("Error: ", e.__doc__) + continue + + result = aritmetica.raiz_quadrada(num1) + + print(f"sqrt({num1}) = {result}") + + exit = input('To continue operation press ( y ) ?') + + if exit.upper() != 'Y': + break + else: + print("Invalid input. Please enter a number between 1 and 6.") + + +if __name__ == "__main__": + pytest.main(['-x', 'test_unitarios.py']) + pytest.main(['-x', 'test_funcionais.py']) + main() \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/test_funcionais.py b/1ano/2semestre/labi/tema02/Calculater/test_funcionais.py new file mode 100644 index 0000000..62afb32 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/test_funcionais.py @@ -0,0 +1,114 @@ +import unittest +from aritmetica import soma, subtracao, multiplicacao, divisao, raiz_quadrada, resto_divisao_inteira + +class TestCalculadora(unittest.TestCase): + + def test_soma_numeros_validos(self): + self.assertEqual(soma(2, 3), 5) + self.assertEqual(soma(-2.5, 3.5), 1) + self.assertEqual(soma(0, 0), 0) + self.assertEqual(soma(0.1, 0.2), 0.3) + + def test_soma_argumentos_invalidos(self): + with self.assertRaises(TypeError): + soma("2", 3) + with self.assertRaises(TypeError): + soma(2, "3") + with self.assertRaises(TypeError): + soma("2", "3") + with self.assertRaises(TypeError): + soma(2) + with self.assertRaises(TypeError): + soma() + + def test_subtracao_numeros_validos(self): + self.assertEqual(subtracao(5, 3), 2) + self.assertEqual(subtracao(3, 5), -2) + self.assertEqual(subtracao(-2.5, 3.5), -6) + self.assertEqual(subtracao(0, 0), 0) + self.assertEqual(subtracao(0.1, 0.2), -0.1) + + def test_subtracao_argumentos_invalidos(self): + with self.assertRaises(TypeError): + subtracao("2", 3) + with self.assertRaises(TypeError): + subtracao(2, "3") + with self.assertRaises(TypeError): + subtracao("2", "3") + with self.assertRaises(TypeError): + subtracao(2) + with self.assertRaises(TypeError): + subtracao() + + def test_multiplicacao_numeros_validos(self): + self.assertEqual(multiplicacao(2, 3), 6) + self.assertEqual(multiplicacao(-2.5, 3.5), -8.75) + self.assertEqual(multiplicacao(0, 0), 0) + self.assertEqual(multiplicacao(0.1, 0.2), 0.02) + + def test_multiplicacao_argumentos_invalidos(self): + with self.assertRaises(TypeError): + multiplicacao("2", 3) + with self.assertRaises(TypeError): + multiplicacao(2, "3") + with self.assertRaises(TypeError): + multiplicacao("2", "3") + with self.assertRaises(TypeError): + multiplicacao(2) + with self.assertRaises(TypeError): + multiplicacao() + + def test_divisao_numeros_validos(self): + self.assertEqual(divisao(4, 2), 2) + self.assertEqual(divisao(3, 5), 0.6) + self.assertEqual(divisao(-10, 2), -5) + self.assertEqual(divisao(0, 5), 0) + + def test_divisao_argumentos_invalidos(self): + with self.assertRaises(TypeError): + divisao("2", 3) + with self.assertRaises(TypeError): + divisao(2, "3") + with self.assertRaises(TypeError): + divisao("2", "3") + with self.assertRaises(TypeError): + divisao(2) + with self.assertRaises(TypeError): + divisao() + with self.assertRaises(ZeroDivisionError): + divisao(5, 0) + + def test_resto_divisao_inteira_numeros_validos(self): + self.assertEqual(resto_divisao_inteira(7, 3), 1) + self.assertEqual(resto_divisao_inteira(10, 2), 0) + self.assertEqual(resto_divisao_inteira(-7, 3), 2) + self.assertEqual(resto_divisao_inteira(0, 5), 0) + + def test_resto_divisao_inteira_argumentos_invalidos(self): + with self.assertRaises(TypeError): + resto_divisao_inteira("2", 3) + with self.assertRaises(TypeError): + resto_divisao_inteira(2, "3") + with self.assertRaises(TypeError): + resto_divisao_inteira("2", "3") + with self.assertRaises(TypeError): + resto_divisao_inteira(2) + with self.assertRaises(TypeError): + resto_divisao_inteira() + + def test_raiz_quadrada_numeros_validos(self): + self.assertAlmostEqual(raiz_quadrada(4), 2) + self.assertAlmostEqual(raiz_quadrada(9), 3) + self.assertAlmostEqual(raiz_quadrada(2), 1.41) + self.assertAlmostEqual(raiz_quadrada(0), 0) + + def test_raiz_quadrada_argumentos_invalidos(self): + with self.assertRaises(ValueError): + raiz_quadrada(-4) + with self.assertRaises(TypeError): + raiz_quadrada("2") + with self.assertRaises(TypeError): + raiz_quadrada() + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/1ano/2semestre/labi/tema02/Calculater/test_unitarios.py b/1ano/2semestre/labi/tema02/Calculater/test_unitarios.py new file mode 100644 index 0000000..0368e99 --- /dev/null +++ b/1ano/2semestre/labi/tema02/Calculater/test_unitarios.py @@ -0,0 +1,37 @@ +import unittest +import aritmetica + +class TestAritmetica(unittest.TestCase): + + def test_soma(self): + self.assertEqual(aritmetica.soma(2, 3), 5) + self.assertEqual(aritmetica.soma(-2, 3), 1) + self.assertEqual(aritmetica.soma(2.5, 3.7), 6.2) + + def test_subtracao(self): + self.assertEqual(aritmetica.subtracao(2, 3), -1) + self.assertEqual(aritmetica.subtracao(-2, 3), -5) + self.assertEqual(aritmetica.subtracao(2.5, 3.7), -1.2) + + def test_multiplicacao(self): + self.assertEqual(aritmetica.multiplicacao(2, 3), 6) + self.assertEqual(aritmetica.multiplicacao(-2, 3), -6) + self.assertEqual(aritmetica.multiplicacao(2.5, 3.7), 9.25) + + def test_divisao(self): + self.assertEqual(aritmetica.divisao(6, 3), 2) + self.assertEqual(aritmetica.divisao(7, 3), 2.33) + self.assertRaises(ZeroDivisionError, aritmetica.divisao, 6, 0) + + def test_resto_divisao_inteira(self): + self.assertEqual(aritmetica.resto_divisao_inteira(7, 3), 1) + self.assertEqual(aritmetica.resto_divisao_inteira(10, 4), 2) + self.assertEqual(aritmetica.resto_divisao_inteira(15, 5), 0) + + def test_raiz_quadrada(self): + self.assertEqual(aritmetica.raiz_quadrada(9), 3) + self.assertEqual(aritmetica.raiz_quadrada(16), 4) + self.assertEqual(aritmetica.raiz_quadrada(25), 5) + +if __name__ == '__main__': + unittest.main()