Está en la página 1de 7

Universidad‌Continental

“Año‌‌de‌‌la‌‌Universalización‌‌de‌‌la‌S
‌ alud”‌‌

ESTRUCTURA DE DATOS

NRC: 14873

ALUMNO: RIVERA RIVERA MARCO ANTONIO

DNI: 44016920

2022

CODIGO FUENTE:
#include <iostream>
#include <clocale>
#include<string>
#include <cstdlib>
#include <vector>
using namespace std;
struct pokemon{
string nombre;
int poder;
string tipo;
pokemon *pNodo;
};

void apilar(pokemon *&nodo);


void registrar(pokemon *&nodo);
void desapilar(pokemon *&nodo);
void imprimir(pokemon *&nodo);
void mostrar(pokemon *&nodo);
void vaciar(pokemon *&nodo);
void menu();
int pedirop();

bool isNumber(const string& str);


void existe(string nombre);
void mostrarData();

int nroDigito=1;
bool lbExiste = false;
vector<string> vectorNombre;
vector<string>::iterator it;

string listaPokemones[10][4]={
{"0","Pikachu","140","Eléctrico"},{"1","Charizard","135","Fuego"},
{"2","Squirtle","110","Agua"},
{"3","Jigglypuff","60","Ada"},{"4","Eevee","90","Normal"},
{"5","Gengar","80","Fantasma"},
{"6","Mewtwo","140","Legendario"},{"7","Mew","100","Legendario"},
{"8","Lugia","70","Legendario"},
{"9","Gyrados","78","Agua"}
};

int main() {
setlocale(LC_CTYPE,"Spanish");
mostrarData();
pokemon *nodo = NULL;
int *opt = new int;
do{
menu();
*opt = pedirop();
switch(*opt){
case 1: apilar(nodo); break;
case 2: desapilar(nodo); break;
case 3: mostrar(nodo); break;
case 4: vaciar(nodo); break;
case 5: cout<<"\nGracias!\n"; break;
}
}while(*opt!=5);
vaciar(nodo);
delete opt;
return 0;
}

void apilar(pokemon *&nodo){


pokemon *cola = new pokemon;
registrar(cola);
if (!lbExiste){
cola->pNodo = nodo;
nodo = cola;
}
return;
}

void registrar(pokemon *&nodo){


string lsDigito;
int lnDigito;
bool lbInicia = true;

do{
lbExiste = false;
cout<<"Ingrese el digito 0"<<nroDigito<<" de su dni: ";
cin>>lsDigito;

if (isNumber(lsDigito)){
lnDigito = std::atoi(lsDigito.c_str());
if (lnDigito < 0 || lnDigito > 9){
lbInicia = false;
cout<<"El valor ingresado debe encontrarse entre 0 y
9"<<endl;
} else {
existe(lsDigito);
if (!lbExiste){
for (int i=0; i<10; i++)
{
if (listaPokemones[i][0] == lsDigito){
nodo->nombre=listaPokemones[i][1];
nodo->poder=std::atoi(listaPokemones[i]
[2].c_str());
nodo->tipo=listaPokemones[i][3];
vectorNombre.push_back(listaPokemones[i]
[1]);
cout<<"El pokemon "<<nodo->nombre<<" fue
registrado"<<endl;
break;
}
}
nroDigito = nroDigito + 1;
}
lbInicia = true;
}
} else {
lbInicia = false;
cout<<"El valor ingresado no es correcto"<<endl;
}
} while (!lbInicia);
return;
}

void desapilar(pokemon *&nodo){


pokemon *cola = nodo;
if(cola!=NULL){
nodo = cola->pNodo;
cout<<"\nSe está eliminando el nodo: ";
imprimir(cola);
delete cola;
}else{
cout<<"\nLa pila está vacia.\n";
}
return;
}

void imprimir(pokemon *&nodo){


cout<<"*****"<<endl;
cout<<"Nombre: "<<nodo->nombre<<endl;
cout<<"Poder: "<<nodo->poder<<endl;
cout<<"Tipo: "<<nodo->tipo<<endl;
cout<<"*****"<<endl;
return;
}

void mostrar(pokemon *&nodo){


pokemon *cola = nodo;
if(cola!=NULL){
while(cola != NULL){
imprimir(cola);
cola = cola->pNodo;
}
//cout<<"\nFIN\n";
}else{
cout<<"\nLa pila está vacia\n";
}
return;
}

void vaciar(pokemon *&nodo){


pokemon *cola = nodo;
if(cola!=NULL){
while(cola!=NULL){
cola = nodo;
nodo = cola->pNodo;
desapilar(cola);
}

nroDigito = 1;
it = vectorNombre.begin();
vectorNombre.erase(it);

cout<<"\nPila destruida\n";
}else{
cout<<"\nLa pila está vacia\n";
}
return;
}

void menu(){
cout<<"\n*** MENU DE POKEMON **\n";
cout<<"\n[1] Apilar";
cout<<"\n[2] Desapilar";
cout<<"\n[3] Mostrar Nodo";
cout<<"\n[4] Vaciar Nodo";
cout<<"\n[5] Salir";
return;
}

int pedirop(){
int *x = new int;
do{
cout<<endl;
cout<<"\nIngrese opción: [1-5]: ";
cin>>*x;
cout<<endl;
if(*x<1||*x>5)
cout<<"\nError...";
}while(*x<1||*x>5);
return *x;
}

void mostrarData(){
string caracterIni, space, caracterFin;

cout<< endl;
cout<<"--------- LISTA DE POKEMONES PERMITIDOS ---------"<< endl;
cout<< endl;

cout<<"------------------------------------------------"<< endl;
cout<<"* NRO || NOMBRE || PODER || TIPO *"<<endl;
cout<<"------------------------------------------------"<< endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 4; j++)
{
//cout<<listaPokemones[i][0]<<endl;
if (j==0){
caracterIni = "* ";
space =" || ";
caracterFin= "*";
} else if(j==1){
caracterIni = "";
space =" || ";
} else if(j==2){
caracterIni = "";
space =" ||";
} else if(j==3){
caracterIni = "";
space =" *";
}
cout<<caracterIni<<listaPokemones[i][j]<<space;
}
cout<<endl;
}
cout<<"--------------------------------------------------"<< endl;
}

bool isNumber(const std::string& str) {


std::string::const_iterator it = str.begin();
while (it != str.end() && std::isdigit(*it)) ++it;
return !str.empty() && it == str.end();
}

void existe(string buscar){


for (int i=0; i<10; i++)
{
if (listaPokemones[i][0] == buscar){
for (size_t n=0; n<vectorNombre.size(); n++){
if(vectorNombre[n]==listaPokemones[i][1]){
lbExiste= true;
cout<<"El pokemon "<<listaPokemones[i][1]<<" ya
se encuentra registrado"<<endl;
return;
};
}
}
}
lbExiste = false;
return;
}

También podría gustarte