Header Files:
Persona.h
Código C++:
Ver original
#ifndef PERSONA_H #define PERSONA_H using namespace std; struct persona{ int id; string nom; string ape; }; #endif /* PERSONA_H */
ManPersonas.h
Código C++:
Ver original
#include "Persona.h" #ifndef CONTRPERSONAS_H #define CONTRPERSONAS_H using namespace std; struct persona lista[20]; int maxIdP = 0; int lastP = 0; int ind = 0; int getMaxIdP(){ return maxIdP+1; } void upMaxIdP(){ maxIdP++; } bool check(persona p){ for (int i=0; i< lastP; i++){ if(lista[i].nom == p.nom){ ind = i; return true; } } return false; } persona getRow(string xnom){ persona p; for (int i=0; i< lastP; i++){ if(lista[i].nom == xnom){ p = lista[i]; break; } } return p; } persona getRow(int id){ persona p; for (int i=0; i< lastP; i++){ if(lista[i].id == id){ p = lista[i]; break; } } return p; } bool create(persona p){ if(check(p)){ return false; } else{ lista[lastP] = p; lastP++; upMaxIdP(); return true; } } bool update(persona p){ persona p1 = getRow(p.id); if(p1.nom != p.nom){ if(check(p)){ return false; } } lista[ind] = p; ind = 0; return true; } if(check(p)){ for(int i=ind; i<lastP-1; i++){ lista[i] = lista[i+1]; } lastP--; ind = 0; } } persona * read(){ return lista; } #endif /* CONTRPERSONAS_H */
crud_per.h
Código C++:
Ver original
#include <iostream> #include "Util.h" #include "ManPersonas.h" #ifndef CRUD_PER_H #define CRUD_PER_H using namespace std; void created(){ int op; cout << "\n ¿Desea crear una nueva persona? Aceptar=1/Cancelar=2 : "; cin >> op; if(op == 1) { persona p; p.id = getMaxIdP(); p.nom = addString("Nombre"); p.ape = addString("Apelido"); if(create(p)){ cout << "\n persona creada :)" << endl; } else { cout << "\n la persona ya existe :(" << endl; } } else if(op == 2) { pause(2); } else { cout << "\n error :(" << endl; } } void updated(){ int op; string xnom; persona p; cout << "\n Ingrese nombre: "; cin >> xnom; p = getRow(xnom); cout << "\n Persona: " << p.id << " Nombre: " << p.nom << " Apellido: " << p.ape; cout << "\n ¿Desea algún dato? Aceptar=1/Cancelar=2 :"; cin >> op; if(op == 1){ cout << "\n ¿Desea editar el nombre ("<< p.nom <<") actual? Aceptar=1/Cancelar=2 :"; cin >> op; if(op == 1){ p.nom = addString("Nombre"); } else { pause(2); } cout << "\n ¿Desea editar el apellido ("<< p.ape <<") actual? Aceptar=1/Cancelar=2 :"; cin >> op; if(op == 1){ p.ape = addString("Apellido"); } else { pause(2); } if(update(p)){ cout << "\n persona editada :)" << endl; } else { cout << "\n la persona ya existe :(" << endl; } } else if(op == 2) { pause(2); } else { cout << "\n error :(" << endl; } } void deleted(){ int op; string xnom; persona p; cout << "\n Ingrese nombre: "; cin >> xnom; p = getRow(xnom); cout << "\n Persona: " << p.id << " Nombre: " << p.nom << " Apellido: " << p.ape; cout << "\n ¿Desea eliminar la persona seleccionada? Aceptar=1/Cancelar=2 :"; cin >> op; if(op == 1){ cout << "\n persona eliminada :)" << endl; } else if(op == 2) { pause(2); } else { cout << "\n error :(" << endl; } } void list(){ cout << "\n ---Mostrar Personas--- \n"; cout << "Persona\t Nombre\t Apellido \n"; for (int i=0; i< lastP; i++){ cout << read()[i].id << "\t " << read()[i].nom << "\t " << read()[i].ape << "\n"; } } #endif /* CRUD_PER_H */
menu_per.h
Código C++:
Ver original
#include "crud_per.h" #ifndef MENU_PER_H #define MENU_PER_H void menu(){ int op = 0; cout << "\n ---CRUD Personas---\n"; cout << "\n 1. Crear Persona"; cout << "\n 2. Editar Persona"; cout << "\n 3. Borrar Persona"; cout << "\n 4. Mostrar Personas"; cout << "\n 5. Salir \n"; cout << "\n Ingrese Opción: "; cin >> op; switch(op){ case 1: created(); break; case 2: updated(); break; case 3: deleted(); break; case 4: list(); break; case 5: cout << "\n Gracias por usarme ;) \n"; break; default: cout << "\n Error :( \n"; break; } menu(); } #endif /* MENU_PER_H */
Util.h
Código C++:
Ver original
#ifndef UTIL_H #define UTIL_H using namespace std; bool checkInt(int s){ return s >= 48 and s <= 57; } bool checkStr(string s){ for(int i=0; i < s.size(); i++){ if((s[i] >= 65 and s[i]<= 90) or (s[i] >= 90 and s[i]<= 122)){ return true; } } return false; } void pause(int dur) { } string addString(string type){ int intento = 1; string nom = ""; while(!checkStr(nom)){ cout << "\n intento nro "<< intento << " Ingrese " << type <<": "; cin >> nom; intento++; } return nom; } #endif /* UTIL_H */
Sources Files:
main.cpp
Código C++:
Ver original
#include <iostream> #include <stdlib.h> #include "menu_per.h" using namespace std; int main(int argc, char** argv) { menu(); }
Espero sus respuestas y saludos

) y que termines tu aprendiendo. Que mal por tu primo, porque el mundo que viene exige soltura con la programación al igual que en el habla. En un futuro no muy lejano, no saber programar lo básica será lo mismo que ser analfabeta. 

