-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
80 lines (67 loc) · 2.38 KB
/
calculator.cpp
File metadata and controls
80 lines (67 loc) · 2.38 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cmath> // Para sqrt() y pow()
using namespace std;
void mostrarMenu() {
cout << "Calculadora basica en C++\n";
cout << "--------------------------\n";
cout << "1. Suma\n";
cout << "2. Resta\n";
cout << "3. Multiplicacion\n";
cout << "4. Division\n";
cout << "5. Raiz cuadrada\n";
cout << "6. Potencia\n";
cout << "0. Salir\n";
cout << "Selecciona una opcion: ";
}
int main() {
int opcion;
double num1, num2;
do {
mostrarMenu();
cin >> opcion;
switch (opcion) {
case 1: // Suma
cout << "Introduce dos numeros: ";
cin >> num1 >> num2;
cout << "Resultado: " << num1 + num2 << "\n\n";
break;
case 2: // Resta
cout << "Introduce dos numeros: ";
cin >> num1 >> num2;
cout << "Resultado: " << num1 - num2 << "\n\n";
break;
case 3: // Multiplicacion
cout << "Introduce dos numeros: ";
cin >> num1 >> num2;
cout << "Resultado: " << num1 * num2 << "\n\n";
break;
case 4: // Division
cout << "Introduce dos numeros: ";
cin >> num1 >> num2;
if (num2 != 0)
cout << "Resultado: " << num1 / num2 << "\n\n";
else
cout << "Error: No se puede dividir entre cero.\n\n";
break;
case 5: // Raiz cuadrada
cout << "Introduce un numero: ";
cin >> num1;
if (num1 >= 0)
cout << "Resultado: " << sqrt(num1) << "\n\n";
else
cout << "Error: No se puede calcular la raiz de un numero negativo.\n\n";
break;
case 6: // Potencia
cout << "Introduce la base y el exponente: ";
cin >> num1 >> num2;
cout << "Resultado: " << pow(num1, num2) << "\n\n";
break;
case 0:
cout << "Saliendo de la calculadora.\n";
break;
default:
cout << "Opcion no valida. Intenta de nuevo.\n\n";
}
} while (opcion != 0);
return 0;
}