Skip to content
This repository has been archived by the owner on May 2, 2021. It is now read-only.

Commit

Permalink
feat: Cambia de funcion plantilla a funcion con argumento auto.
Browse files Browse the repository at this point in the history
Es mas sencillo usar un parametro de tipo auto que un generico
en una funcion cuando no hay restricciones con el argumento
a trabajar.
  • Loading branch information
Mandroide committed Sep 24, 2017
1 parent 940e50c commit 5a0b033
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 176 deletions.
9 changes: 5 additions & 4 deletions Punto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
*/
#include "Punto.h"
#include <fstream>
#include <stdexcept>
#include <stdexcept>
#include <string>
using std::fstream;
using std::string;

Punto::Punto() {
abscisa = 0;
ordenada = 0;
persist = std::vector<Punto>();
try {
leer();
leer();
} catch (string) {

}
Expand All @@ -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;

}
Expand Down
1 change: 0 additions & 1 deletion Punto.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#define PUNTO_H

#include <vector>
#include <iostream>

class Punto {
private:
Expand Down
341 changes: 170 additions & 171 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,171 +1,170 @@
/*
* File: main.cpp
* Author: aybar
*
* Created on September 14, 2017, 4:59 PM
*/
#include <iostream>
#include <map> // Para coleccionar menu.
#include <fstream> // Para archivo punto.txt.
#include <string>
#include <cctype> // toupper();
#include <limits> // 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 <class T> 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<unsigned short, Menu> 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 <class T> 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<streamsize>::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 <iostream>
#include <map> // Para coleccionar menu.
#include <string>
#include <cctype> // toupper();
#include <limits> // 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<unsigned short, Menu> 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<streamsize>::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";

}

0 comments on commit 5a0b033

Please sign in to comment.