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

Commit 5a0b033

Browse files
authored
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.
1 parent 940e50c commit 5a0b033

File tree

3 files changed

+175
-176
lines changed

3 files changed

+175
-176
lines changed

Punto.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
*/
77
#include "Punto.h"
88
#include <fstream>
9-
#include <stdexcept>
9+
#include <stdexcept>
1010
#include <string>
1111
using std::fstream;
1212
using std::string;
13+
1314
Punto::Punto() {
1415
abscisa = 0;
1516
ordenada = 0;
1617
persist = std::vector<Punto>();
1718
try {
18-
leer();
19+
leer();
1920
} catch (string) {
2021

2122
}
@@ -39,14 +40,14 @@ Punto Punto::buscar(const unsigned x) {
3940

4041
bool Punto::alta(const int x, const int y) {
4142
persist.push_back(Punto(x, y));
42-
grabar();
43+
grabar();
4344
return true;
4445

4546
}
4647

4748
bool Punto::baja(const unsigned pos) {
4849
persist.erase(persist.begin() + pos);
49-
grabar();
50+
grabar();
5051
return true;
5152

5253
}

Punto.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define PUNTO_H
1010

1111
#include <vector>
12-
#include <iostream>
1312

1413
class Punto {
1514
private:

main.cpp

Lines changed: 170 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,170 @@
1-
/*
2-
* File: main.cpp
3-
* Author: aybar
4-
*
5-
* Created on September 14, 2017, 4:59 PM
6-
*/
7-
#include <iostream>
8-
#include <map> // Para coleccionar menu.
9-
#include <fstream> // Para archivo punto.txt.
10-
#include <string>
11-
#include <cctype> // toupper();
12-
#include <limits> // Valida entrada por teclado.
13-
#include "Punto.h"
14-
//-------------------------
15-
using std::cout;
16-
using std::cin;
17-
using std::endl;
18-
using std::toupper;
19-
using std::string;
20-
// Valida entrada por teclado.
21-
using std::streamsize;
22-
//-------------------------
23-
24-
enum class Menu {
25-
AGREGAR = 1, ELIMINAR, BUSCAR, SALIR = 0
26-
};
27-
// ------------------------
28-
template <class T> void validarNumero(T& arg, string mensaje);
29-
unsigned short mostrarMenu();
30-
bool haSalido();
31-
void agregarPunto(Punto&);
32-
void eliminarPunto(Punto&);
33-
void buscarPunto(Punto&);
34-
//------------------------
35-
36-
int main(int argc, char** argv) {
37-
38-
// Opciones a comparar con switch case;
39-
std::map<unsigned short, Menu> opciones;
40-
opciones[1] = Menu::AGREGAR;
41-
opciones[2] = Menu::ELIMINAR;
42-
opciones[3] = Menu::BUSCAR;
43-
opciones[0] = Menu::SALIR;
44-
unsigned short opcion;
45-
// ---------------------------------------
46-
47-
Punto punto;
48-
49-
bool haTerminado = false;
50-
while (!haTerminado) {
51-
try {
52-
opcion = mostrarMenu();
53-
54-
switch (opciones[opcion]) {
55-
case Menu::AGREGAR:
56-
agregarPunto(punto);
57-
break;
58-
case Menu::ELIMINAR:
59-
eliminarPunto(punto);
60-
break;
61-
case Menu::BUSCAR:
62-
buscarPunto(punto);
63-
break;
64-
default:
65-
haTerminado = haSalido();
66-
}
67-
} catch (string e) {
68-
cout << e;
69-
}
70-
71-
cout << "\n\n";
72-
cout << "Presione ENTER para continuar...";
73-
cin.ignore();
74-
cin.get();
75-
cout << "\n\n";
76-
}
77-
78-
return 0;
79-
}
80-
81-
template <class T> void validarNumero(T& arg, string mensaje) {
82-
bool esValida = false;
83-
while (!esValida) {
84-
cout << mensaje;
85-
if (cin >> arg) {
86-
esValida = true;
87-
} else {
88-
cin.clear();
89-
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
90-
}
91-
cout << "\n\n";
92-
}
93-
94-
}
95-
96-
unsigned short mostrarMenu() {
97-
unsigned short opcion;
98-
const unsigned short SALIR = 0;
99-
const unsigned short ULTIMA = 4;
100-
bool esValida = false;
101-
string menu = "\t\tElija la opcion que desea dentro del plano: \n";
102-
menu.append("1. Agregar un punto.\n");
103-
menu.append("2. Eliminar un punto.\n");
104-
menu.append("3. Buscar un punto.\n");
105-
menu.append("0. Salir.\n\n");
106-
107-
while (!esValida) {
108-
validarNumero(opcion, menu);
109-
esValida = opcion >=SALIR && opcion <= ULTIMA;
110-
}
111-
112-
return opcion;
113-
}
114-
115-
bool haSalido() {
116-
char salir;
117-
bool esValida = false; // Valida entrada por teclado del siguiente mensaje.
118-
while (!esValida) {
119-
cout << "Desea salir del programa?[S/N]: ";
120-
cin >> salir;
121-
122-
salir = toupper(salir); // Convierte a mayuscula la opcion.
123-
esValida = salir == 'S' || salir == 'N'; // Condicion de entrada por teclado.
124-
}
125-
126-
return (salir == 'S');
127-
128-
}
129-
130-
void agregarPunto(Punto& punto) {
131-
int x, y;
132-
string mensajeX = "Ingrese valor de eje x: ";
133-
validarNumero(x, mensajeX);
134-
135-
string mensajeY = "Ingrese valor de eje y: ";
136-
validarNumero(y, mensajeY);
137-
138-
if (punto.alta(x, y)) {
139-
cout << "El punto P(" << x << ", " << y << ") ha sido agregado correctamente." << endl;
140-
} else {
141-
throw "El punto P (" + std::to_string(x) + ", " + std::to_string(y) + ")"
142-
"no fue agregado correctamente. Intente de nuevo. ";
143-
}
144-
145-
}
146-
147-
void eliminarPunto(Punto& punto) {
148-
int pos;
149-
string mensaje = "Ingrese la posicion del elemento que desea eliminar: ";
150-
validarNumero(pos, mensaje);
151-
152-
int x = punto.buscar(pos).getAbscisa();
153-
int y = punto.buscar(pos).getOrdenada();
154-
155-
punto.baja(pos);
156-
cout << "El punto P(" << x << ", " << y << ") del vector ha sido eliminado";
157-
158-
}
159-
160-
void buscarPunto(Punto& punto) {
161-
unsigned i;
162-
string mensaje = "Ingrese indice de elemento a buscar [0-n]: ";
163-
validarNumero(i, mensaje);
164-
165-
const Punto p = punto.buscar(i);
166-
167-
string x = std::to_string(p.getAbscisa());
168-
string y = std::to_string(p.getOrdenada());
169-
cout << "El punto #" << i << " del vector corresponde al punto P(" + x + ", " + y + ")\n";
170-
171-
}
1+
/*
2+
* File: main.cpp
3+
* Author: aybar
4+
*
5+
* Created on September 14, 2017, 4:59 PM
6+
*/
7+
#include <iostream>
8+
#include <map> // Para coleccionar menu.
9+
#include <string>
10+
#include <cctype> // toupper();
11+
#include <limits> // Valida entrada por teclado.
12+
#include "Punto.h"
13+
//-------------------------
14+
using std::cout;
15+
using std::cin;
16+
using std::endl;
17+
using std::toupper;
18+
using std::string;
19+
// Valida entrada por teclado.
20+
using std::streamsize;
21+
//-------------------------
22+
23+
enum class Menu {
24+
AGREGAR = 1, ELIMINAR, BUSCAR, SALIR = 0
25+
};
26+
// ------------------------
27+
void validarNumero(auto& numero, string mensaje);
28+
unsigned short mostrarMenu();
29+
bool haSalido();
30+
void agregarPunto(Punto&);
31+
void eliminarPunto(Punto&);
32+
void buscarPunto(Punto&);
33+
//------------------------
34+
35+
int main(int argc, char** argv) {
36+
37+
// Opciones a comparar con switch case;
38+
std::map<unsigned short, Menu> opciones;
39+
opciones[1] = Menu::AGREGAR;
40+
opciones[2] = Menu::ELIMINAR;
41+
opciones[3] = Menu::BUSCAR;
42+
opciones[0] = Menu::SALIR;
43+
unsigned short opcion;
44+
// ---------------------------------------
45+
46+
Punto punto;
47+
48+
bool haTerminado = false;
49+
while (!haTerminado) {
50+
try {
51+
opcion = mostrarMenu();
52+
53+
switch (opciones[opcion]) {
54+
case Menu::AGREGAR:
55+
agregarPunto(punto);
56+
break;
57+
case Menu::ELIMINAR:
58+
eliminarPunto(punto);
59+
break;
60+
case Menu::BUSCAR:
61+
buscarPunto(punto);
62+
break;
63+
default:
64+
haTerminado = haSalido();
65+
}
66+
} catch (string e) {
67+
cout << e;
68+
}
69+
70+
cout << "\n\n";
71+
cout << "Presione ENTER para continuar...";
72+
cin.ignore();
73+
cin.get();
74+
cout << "\n\n";
75+
}
76+
77+
return 0;
78+
}
79+
80+
void validarNumero(auto& numero, string mensaje) {
81+
bool esValido = false;
82+
while (!esValido) {
83+
cout << mensaje;
84+
if (cin >> numero) {
85+
esValido = true;
86+
} else {
87+
cin.clear();
88+
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
89+
}
90+
cout << "\n\n";
91+
}
92+
93+
}
94+
95+
unsigned short mostrarMenu() {
96+
unsigned short opcion;
97+
const unsigned short SALIR = 0;
98+
const unsigned short ULTIMA = 4;
99+
bool esValida = false;
100+
string menu = "\t\tElija la opcion que desea dentro del plano: \n";
101+
menu.append("1. Agregar un punto.\n");
102+
menu.append("2. Eliminar un punto.\n");
103+
menu.append("3. Buscar un punto.\n");
104+
menu.append("0. Salir.\n\n");
105+
106+
while (!esValida) {
107+
validarNumero(opcion, menu);
108+
esValida = opcion >=SALIR && opcion <= ULTIMA;
109+
}
110+
111+
return opcion;
112+
}
113+
114+
bool haSalido() {
115+
char salir;
116+
bool esValida = false; // Valida entrada por teclado del siguiente mensaje.
117+
while (!esValida) {
118+
cout << "Desea salir del programa?[S/N]: ";
119+
cin >> salir;
120+
121+
salir = toupper(salir); // Convierte a mayuscula la opcion.
122+
esValida = salir == 'S' || salir == 'N'; // Condicion de entrada por teclado.
123+
}
124+
125+
return (salir == 'S');
126+
127+
}
128+
129+
void agregarPunto(Punto& punto) {
130+
int x, y;
131+
string mensajeX = "Ingrese valor de eje x: ";
132+
validarNumero(x, mensajeX);
133+
134+
string mensajeY = "Ingrese valor de eje y: ";
135+
validarNumero(y, mensajeY);
136+
137+
if (punto.alta(x, y)) {
138+
cout << "El punto P(" << x << ", " << y << ") ha sido agregado correctamente." << endl;
139+
} else {
140+
throw "El punto P (" + std::to_string(x) + ", " + std::to_string(y) + ")"
141+
"no fue agregado correctamente. Intente de nuevo. ";
142+
}
143+
144+
}
145+
146+
void eliminarPunto(Punto& punto) {
147+
int pos;
148+
string mensaje = "Ingrese la posicion del elemento que desea eliminar: ";
149+
validarNumero(pos, mensaje);
150+
151+
int x = punto.buscar(pos).getAbscisa();
152+
int y = punto.buscar(pos).getOrdenada();
153+
154+
punto.baja(pos);
155+
cout << "El punto P(" << x << ", " << y << ") del vector ha sido eliminado";
156+
157+
}
158+
159+
void buscarPunto(Punto& punto) {
160+
unsigned i;
161+
string mensaje = "Ingrese indice de elemento a buscar [0-n]: ";
162+
validarNumero(i, mensaje);
163+
164+
const Punto p = punto.buscar(i);
165+
166+
string x = std::to_string(p.getAbscisa());
167+
string y = std::to_string(p.getOrdenada());
168+
cout << "El punto #" << i << " del vector corresponde al punto P(" + x + ", " + y + ")\n";
169+
170+
}

0 commit comments

Comments
 (0)