From 5a0b033754d9164c12501d164eb3b34ab5b859c1 Mon Sep 17 00:00:00 2001 From: Manuel <31109774+Megalight09@users.noreply.github.com> Date: Sun, 24 Sep 2017 11:15:39 -0800 Subject: [PATCH] feat: Cambia de funcion plantilla a funcion con argumento auto. Es mas sencillo usar un parametro de tipo auto que un generico en una funcion cuando no hay restricciones con el argumento a trabajar. --- Punto.cpp | 9 +- Punto.h | 1 - main.cpp | 341 +++++++++++++++++++++++++++--------------------------- 3 files changed, 175 insertions(+), 176 deletions(-) diff --git a/Punto.cpp b/Punto.cpp index dcd0423..3e7c7d0 100644 --- a/Punto.cpp +++ b/Punto.cpp @@ -6,16 +6,17 @@ */ #include "Punto.h" #include -#include +#include #include using std::fstream; using std::string; + Punto::Punto() { abscisa = 0; ordenada = 0; persist = std::vector(); try { - leer(); + leer(); } catch (string) { } @@ -39,14 +40,14 @@ Punto Punto::buscar(const unsigned x) { bool Punto::alta(const int x, const int y) { persist.push_back(Punto(x, y)); - grabar(); + grabar(); return true; } bool Punto::baja(const unsigned pos) { persist.erase(persist.begin() + pos); - grabar(); + grabar(); return true; } diff --git a/Punto.h b/Punto.h index c01d970..4feea3e 100644 --- a/Punto.h +++ b/Punto.h @@ -9,7 +9,6 @@ #define PUNTO_H #include -#include class Punto { private: diff --git a/main.cpp b/main.cpp index 89aa3fc..5d0c514 100644 --- a/main.cpp +++ b/main.cpp @@ -1,171 +1,170 @@ -/* - * File: main.cpp - * Author: aybar - * - * Created on September 14, 2017, 4:59 PM - */ -#include -#include // Para coleccionar menu. -#include // Para archivo punto.txt. -#include -#include // toupper(); -#include // Valida entrada por teclado. -#include "Punto.h" -//------------------------- -using std::cout; -using std::cin; -using std::endl; -using std::toupper; -using std::string; -// Valida entrada por teclado. -using std::streamsize; -//------------------------- - -enum class Menu { - AGREGAR = 1, ELIMINAR, BUSCAR, SALIR = 0 -}; -// ------------------------ -template void validarNumero(T& arg, string mensaje); -unsigned short mostrarMenu(); -bool haSalido(); -void agregarPunto(Punto&); -void eliminarPunto(Punto&); -void buscarPunto(Punto&); -//------------------------ - -int main(int argc, char** argv) { - - // Opciones a comparar con switch case; - std::map opciones; - opciones[1] = Menu::AGREGAR; - opciones[2] = Menu::ELIMINAR; - opciones[3] = Menu::BUSCAR; - opciones[0] = Menu::SALIR; - unsigned short opcion; - // --------------------------------------- - - Punto punto; - - bool haTerminado = false; - while (!haTerminado) { - try { - opcion = mostrarMenu(); - - switch (opciones[opcion]) { - case Menu::AGREGAR: - agregarPunto(punto); - break; - case Menu::ELIMINAR: - eliminarPunto(punto); - break; - case Menu::BUSCAR: - buscarPunto(punto); - break; - default: - haTerminado = haSalido(); - } - } catch (string e) { - cout << e; - } - - cout << "\n\n"; - cout << "Presione ENTER para continuar..."; - cin.ignore(); - cin.get(); - cout << "\n\n"; - } - - return 0; -} - -template void validarNumero(T& arg, string mensaje) { - bool esValida = false; - while (!esValida) { - cout << mensaje; - if (cin >> arg) { - esValida = true; - } else { - cin.clear(); - cin.ignore(std::numeric_limits::max(), '\n'); - } - cout << "\n\n"; - } - -} - -unsigned short mostrarMenu() { - unsigned short opcion; - const unsigned short SALIR = 0; - const unsigned short ULTIMA = 4; - bool esValida = false; - string menu = "\t\tElija la opcion que desea dentro del plano: \n"; - menu.append("1. Agregar un punto.\n"); - menu.append("2. Eliminar un punto.\n"); - menu.append("3. Buscar un punto.\n"); - menu.append("0. Salir.\n\n"); - - while (!esValida) { - validarNumero(opcion, menu); - esValida = opcion >=SALIR && opcion <= ULTIMA; - } - - return opcion; -} - -bool haSalido() { - char salir; - bool esValida = false; // Valida entrada por teclado del siguiente mensaje. - while (!esValida) { - cout << "Desea salir del programa?[S/N]: "; - cin >> salir; - - salir = toupper(salir); // Convierte a mayuscula la opcion. - esValida = salir == 'S' || salir == 'N'; // Condicion de entrada por teclado. - } - - return (salir == 'S'); - -} - -void agregarPunto(Punto& punto) { - int x, y; - string mensajeX = "Ingrese valor de eje x: "; - validarNumero(x, mensajeX); - - string mensajeY = "Ingrese valor de eje y: "; - validarNumero(y, mensajeY); - - if (punto.alta(x, y)) { - cout << "El punto P(" << x << ", " << y << ") ha sido agregado correctamente." << endl; - } else { - throw "El punto P (" + std::to_string(x) + ", " + std::to_string(y) + ")" - "no fue agregado correctamente. Intente de nuevo. "; - } - -} - -void eliminarPunto(Punto& punto) { - int pos; - string mensaje = "Ingrese la posicion del elemento que desea eliminar: "; - validarNumero(pos, mensaje); - - int x = punto.buscar(pos).getAbscisa(); - int y = punto.buscar(pos).getOrdenada(); - - punto.baja(pos); - cout << "El punto P(" << x << ", " << y << ") del vector ha sido eliminado"; - -} - -void buscarPunto(Punto& punto) { - unsigned i; - string mensaje = "Ingrese indice de elemento a buscar [0-n]: "; - validarNumero(i, mensaje); - - const Punto p = punto.buscar(i); - - string x = std::to_string(p.getAbscisa()); - string y = std::to_string(p.getOrdenada()); - cout << "El punto #" << i << " del vector corresponde al punto P(" + x + ", " + y + ")\n"; - -} +/* + * File: main.cpp + * Author: aybar + * + * Created on September 14, 2017, 4:59 PM + */ +#include +#include // Para coleccionar menu. +#include +#include // toupper(); +#include // Valida entrada por teclado. +#include "Punto.h" +//------------------------- +using std::cout; +using std::cin; +using std::endl; +using std::toupper; +using std::string; +// Valida entrada por teclado. +using std::streamsize; +//------------------------- + +enum class Menu { + AGREGAR = 1, ELIMINAR, BUSCAR, SALIR = 0 +}; +// ------------------------ +void validarNumero(auto& numero, string mensaje); +unsigned short mostrarMenu(); +bool haSalido(); +void agregarPunto(Punto&); +void eliminarPunto(Punto&); +void buscarPunto(Punto&); +//------------------------ + +int main(int argc, char** argv) { + + // Opciones a comparar con switch case; + std::map opciones; + opciones[1] = Menu::AGREGAR; + opciones[2] = Menu::ELIMINAR; + opciones[3] = Menu::BUSCAR; + opciones[0] = Menu::SALIR; + unsigned short opcion; + // --------------------------------------- + + Punto punto; + + bool haTerminado = false; + while (!haTerminado) { + try { + opcion = mostrarMenu(); + + switch (opciones[opcion]) { + case Menu::AGREGAR: + agregarPunto(punto); + break; + case Menu::ELIMINAR: + eliminarPunto(punto); + break; + case Menu::BUSCAR: + buscarPunto(punto); + break; + default: + haTerminado = haSalido(); + } + } catch (string e) { + cout << e; + } + + cout << "\n\n"; + cout << "Presione ENTER para continuar..."; + cin.ignore(); + cin.get(); + cout << "\n\n"; + } + + return 0; +} + +void validarNumero(auto& numero, string mensaje) { + bool esValido = false; + while (!esValido) { + cout << mensaje; + if (cin >> numero) { + esValido = true; + } else { + cin.clear(); + cin.ignore(std::numeric_limits::max(), '\n'); + } + cout << "\n\n"; + } + +} + +unsigned short mostrarMenu() { + unsigned short opcion; + const unsigned short SALIR = 0; + const unsigned short ULTIMA = 4; + bool esValida = false; + string menu = "\t\tElija la opcion que desea dentro del plano: \n"; + menu.append("1. Agregar un punto.\n"); + menu.append("2. Eliminar un punto.\n"); + menu.append("3. Buscar un punto.\n"); + menu.append("0. Salir.\n\n"); + + while (!esValida) { + validarNumero(opcion, menu); + esValida = opcion >=SALIR && opcion <= ULTIMA; + } + + return opcion; +} + +bool haSalido() { + char salir; + bool esValida = false; // Valida entrada por teclado del siguiente mensaje. + while (!esValida) { + cout << "Desea salir del programa?[S/N]: "; + cin >> salir; + + salir = toupper(salir); // Convierte a mayuscula la opcion. + esValida = salir == 'S' || salir == 'N'; // Condicion de entrada por teclado. + } + + return (salir == 'S'); + +} + +void agregarPunto(Punto& punto) { + int x, y; + string mensajeX = "Ingrese valor de eje x: "; + validarNumero(x, mensajeX); + + string mensajeY = "Ingrese valor de eje y: "; + validarNumero(y, mensajeY); + + if (punto.alta(x, y)) { + cout << "El punto P(" << x << ", " << y << ") ha sido agregado correctamente." << endl; + } else { + throw "El punto P (" + std::to_string(x) + ", " + std::to_string(y) + ")" + "no fue agregado correctamente. Intente de nuevo. "; + } + +} + +void eliminarPunto(Punto& punto) { + int pos; + string mensaje = "Ingrese la posicion del elemento que desea eliminar: "; + validarNumero(pos, mensaje); + + int x = punto.buscar(pos).getAbscisa(); + int y = punto.buscar(pos).getOrdenada(); + + punto.baja(pos); + cout << "El punto P(" << x << ", " << y << ") del vector ha sido eliminado"; + +} + +void buscarPunto(Punto& punto) { + unsigned i; + string mensaje = "Ingrese indice de elemento a buscar [0-n]: "; + validarNumero(i, mensaje); + + const Punto p = punto.buscar(i); + + string x = std::to_string(p.getAbscisa()); + string y = std::to_string(p.getOrdenada()); + cout << "El punto #" << i << " del vector corresponde al punto P(" + x + ", " + y + ")\n"; + +}