// Programación en C++ para Ingenieros, Ed. Thomson Paraninfo, 2006 // Capítulo 5: Subprogramas: Acciones y funciones #include <iostream> #include <assert.h> using namespace std; int leeOpcion(int valMin, int valMax) { assert(valMin < valMax); cout << "Elige una opcion (" << valMin << "-" << valMax << "): "; int opcion; cin >> opcion; while (opcion < valMin || opcion > valMax) { cout << "Opcion Incorrecta" << endl; cout << "Elige una opcion (" << valMin << "-" << valMax << "): "; cin >> opcion; } return opcion; } void muestraMenu(void) { cout << "1.- Sumar" << endl; cout << "2.- Restar" << endl; cout << "3.- Multiplicar" << endl; cout << "4.- Dividir" << endl; cout << "0.- Salir" << endl; } void sumar(); void restar(); void multiplicar(); void dividir(); void leerOperandos(int& op1, int& op2); int main(void) { int op; do { muestraMenu(); op = leeOpcion(0, 4); switch (op) { case 1: sumar(); break; case 2: restar(); break; case 3: multiplicar(); break; case 4: dividir(); break; default: cout << "Gracias por utilizarme." << endl; } } while (op != 0); return 0; } void sumar() { int op1, op2; leerOperandos(op1, op2); cout << "El resultado es: " << op1 + op2 << endl; } void restar() { int op1, op2; leerOperandos(op1, op2); cout << "El resultado es: " << op1 - op2 << endl; } void multiplicar() { int op1, op2; leerOperandos(op1, op2); cout << "El resultado es: " << op1 * op2 << endl; } void dividir() { int op1, op2; leerOperandos(op1, op2); cout << "El resultado es: " << float(op1) / op2 << endl; } void leerOperandos(int& op1, int& op2) { cout << "Entra el primer operando: "; cin >> op1; cout << "Entra el segundo operando: "; cin >> op2; }