|
| 1 | +#include <iostream> |
| 2 | +#include <iomanip> |
| 3 | +#include <fstream> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +using namespace std; |
| 7 | +struct T_hijos{ |
| 8 | + string nombre; |
| 9 | + int ced, ced_padre, edad; |
| 10 | +}; |
| 11 | +struct T_padres{ |
| 12 | + string nombre; |
| 13 | + int ced, telf; |
| 14 | + string direccion; |
| 15 | +}; |
| 16 | +typedef T_padres _padres; |
| 17 | +typedef T_hijos _hijos; |
| 18 | +struct nodo_h{ |
| 19 | + _hijos info; |
| 20 | + nodo_h* sig; |
| 21 | +}; |
| 22 | +struct nodo_p{ |
| 23 | + _padres info; |
| 24 | + nodo_p* sig; |
| 25 | +}; |
| 26 | +typedef nodo_h* ptnodo_h; |
| 27 | +typedef nodo_p* ptnodo_p; |
| 28 | +ptnodo_h lista_h = NULL, Aux = NULL; |
| 29 | +ptnodo_p lista_p = NULL; |
| 30 | +// funciones |
| 31 | +void INSERTAR_FIN(ptnodo_p &Lista, _padres Elem); |
| 32 | +void INSERTAR_FIN(ptnodo_h &Lista, _hijos Elem); |
| 33 | +void MOSTRAR_LISTA (ptnodo_p Lista); |
| 34 | +void MOSTRAR_LISTA (ptnodo_h Lista); |
| 35 | +void BORRAR(ptnodo_h &Lista); |
| 36 | +void BORRAR(ptnodo_p &Lista); |
| 37 | +int BUSCAR_ced(ptnodo_h Lista, int ced_h); |
| 38 | +int BUSCAR_telf(ptnodo_p Lista, int ced_p); |
| 39 | +ptnodo_h hijos_porced(ptnodo_h Lista, int ced_p); |
| 40 | +int main() { |
| 41 | + string elem, num; |
| 42 | + int opc = 0, opc2 = 0, ced_h, ced_p, telf, ban = 0; |
| 43 | + ifstream A1; |
| 44 | + _padres registro_padre; |
| 45 | + _hijos registro_hijo; |
| 46 | + while(opc != 6){ |
| 47 | + cout << "Menu de opciones\n"; |
| 48 | + cout << "1.- Cargar datos\n"; |
| 49 | + cout << "2.- Mostrar datos cargados\n"; |
| 50 | + cout << "3.- Buscar el telefono de un padre conociendo la cedula de su hijo\n"; |
| 51 | + cout << "4.- Buscar y mostrar los hijos de un padre usando su numero de cedula\n"; |
| 52 | + cout << "5.- Opcion extra\n"; |
| 53 | + cout << "6.- Salir\n"; |
| 54 | + cin >> opc; |
| 55 | + system("cls"); |
| 56 | + switch(opc){ |
| 57 | + case 1: |
| 58 | + // liberar memoria |
| 59 | + if(lista_p != NULL) |
| 60 | + BORRAR(lista_p); |
| 61 | + if(lista_h != NULL) |
| 62 | + BORRAR(lista_h); |
| 63 | + /*cargando los datos de los padres e hijos por separado,no combinar, |
| 64 | + ya que los registros son distintos, igual que los nodos*/ |
| 65 | + A1.open("padres.txt"); |
| 66 | + if(!A1){ |
| 67 | + cout << "Error al abrir archivo padres\n"; |
| 68 | + break; |
| 69 | + } |
| 70 | + getline(A1,registro_padre.nombre,'\t'); |
| 71 | + A1>> registro_padre.ced; |
| 72 | + A1.ignore(1); |
| 73 | + A1>> registro_padre.telf; |
| 74 | + A1.ignore(1); |
| 75 | + getline(A1,registro_padre.direccion); |
| 76 | + INSERTAR_FIN(lista_p, registro_padre); |
| 77 | + while(A1.eof() == 0){ |
| 78 | + getline(A1,registro_padre.nombre,'\t'); |
| 79 | + A1>> registro_padre.ced; |
| 80 | + A1.ignore(1); |
| 81 | + A1>> registro_padre.telf; |
| 82 | + A1.ignore(1); |
| 83 | + getline(A1,registro_padre.direccion); |
| 84 | + INSERTAR_FIN(lista_p, registro_padre); |
| 85 | + } |
| 86 | + A1.close(); |
| 87 | + // ahora se cargan los hijos |
| 88 | + A1.open("hijos.txt"); |
| 89 | + if(!A1){ |
| 90 | + cout << "Error al abrir archivo hijos\n"; |
| 91 | + break; |
| 92 | + } |
| 93 | + getline(A1,registro_hijo.nombre,'\t'); |
| 94 | + A1>> registro_hijo.ced; |
| 95 | + A1.ignore(1); |
| 96 | + A1>> registro_hijo.ced_padre; |
| 97 | + A1.ignore(1); |
| 98 | + A1>> registro_hijo.edad; |
| 99 | + A1.ignore(1); |
| 100 | + INSERTAR_FIN(lista_h, registro_hijo); |
| 101 | + while(A1.eof() == 0){ |
| 102 | + getline(A1,registro_hijo.nombre,'\t'); |
| 103 | + A1>> registro_hijo.ced; |
| 104 | + A1.ignore(1); |
| 105 | + A1>> registro_hijo.ced_padre; |
| 106 | + A1.ignore(1); |
| 107 | + A1>> registro_hijo.edad; |
| 108 | + A1.ignore(1); |
| 109 | + INSERTAR_FIN(lista_h, registro_hijo); |
| 110 | + } |
| 111 | + A1.close(); |
| 112 | + ban = 1; |
| 113 | + break; |
| 114 | + case 2: |
| 115 | + opc2 = 0; |
| 116 | + while (opc2 != 3){ |
| 117 | + system("color 70"); |
| 118 | + cout << "menu de opciones mostrar\n"; |
| 119 | + cout << "1.- Lista de hijos\n"; |
| 120 | + cout << "2.- Lisa de padres\n"; |
| 121 | + cout << "3.- Salir del modo mostrar\n"; |
| 122 | + cin >> opc2; |
| 123 | + system("cls"); |
| 124 | + switch(opc2){ |
| 125 | + case 1: |
| 126 | + MOSTRAR_LISTA (lista_h); |
| 127 | + break; |
| 128 | + case 2: |
| 129 | + MOSTRAR_LISTA (lista_p); |
| 130 | + break; |
| 131 | + case 3: |
| 132 | + cout << "Salio de la modalidad mostrar\n"; |
| 133 | + system("color 0F"); |
| 134 | + break; |
| 135 | + default: |
| 136 | + cout << "Ingrese una opcion valida\n"; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + break; |
| 141 | + case 3: |
| 142 | + if (ban){ |
| 143 | + cout << "Ingrese la cedula del ni" << (char) 164 <<"o\n"; |
| 144 | + cin >> ced_h; |
| 145 | + ced_p = BUSCAR_ced(lista_h, ced_h); |
| 146 | + if(ced_p){ |
| 147 | + telf = BUSCAR_telf(lista_p, ced_p); |
| 148 | + if(telf) |
| 149 | + cout << "el numero de telefono del padre es: " << telf << endl; |
| 150 | + }else |
| 151 | + cout << "no existe coincidencia\n"; |
| 152 | + }else |
| 153 | + cout << "Se deben cargar los datos primero" << endl; |
| 154 | + break; |
| 155 | + case 4: |
| 156 | + if (ban){ |
| 157 | + cout << "Ingrese la cedula del representante\n"; |
| 158 | + cin >> ced_p; |
| 159 | + Aux = hijos_porced(lista_h, ced_p); |
| 160 | + if(Aux != NULL){ |
| 161 | + MOSTRAR_LISTA(Aux); |
| 162 | + }else |
| 163 | + cout << "No hay coincidencia\n"; |
| 164 | + }else |
| 165 | + cout << "Se deben cargar los datos primero" << endl; |
| 166 | + break; |
| 167 | + case 5: |
| 168 | + break; |
| 169 | + case 6: |
| 170 | + // liberar memoria |
| 171 | + BORRAR(Aux); |
| 172 | + BORRAR(lista_p); |
| 173 | + BORRAR(lista_h); |
| 174 | + break; |
| 175 | + default: |
| 176 | + cout << "Ingrese una opcion valida\n"; |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + return 0; |
| 181 | + } |
| 182 | +void INSERTAR_FIN(ptnodo_p &Lista, _padres Elem){ |
| 183 | + ptnodo_p A = Lista, NUEVO; |
| 184 | + NUEVO = new nodo_p; |
| 185 | + NUEVO -> info = Elem; |
| 186 | + NUEVO -> sig = NULL; |
| 187 | + if(Lista == NULL) |
| 188 | + Lista = NUEVO; |
| 189 | + else |
| 190 | + { while (A -> sig != NULL) |
| 191 | + A = A -> sig; |
| 192 | + A -> sig = NUEVO; |
| 193 | + } |
| 194 | +} |
| 195 | +void INSERTAR_FIN(ptnodo_h &Lista, _hijos Elem){ |
| 196 | + ptnodo_h A = Lista, NUEVO; |
| 197 | + NUEVO = new nodo_h; |
| 198 | + NUEVO -> info = Elem; |
| 199 | + NUEVO -> sig = NULL; |
| 200 | + if(Lista == NULL) |
| 201 | + Lista = NUEVO; |
| 202 | + else |
| 203 | + { while (A -> sig != NULL) |
| 204 | + A = A->sig; |
| 205 | + A -> sig = NUEVO; |
| 206 | + } |
| 207 | +} |
| 208 | +void MOSTRAR_LISTA (ptnodo_p Lista){ |
| 209 | + ptnodo_p A = Lista; |
| 210 | + if (A != NULL){ |
| 211 | + while( A != NULL){ |
| 212 | + cout << setw(15) << left << A -> info.nombre; |
| 213 | + cout << setw(9) << A -> info.ced; |
| 214 | + cout << setw(12) << A -> info.telf; |
| 215 | + cout << setw(20) << A -> info.direccion <<endl; |
| 216 | + A = A -> sig; |
| 217 | + } |
| 218 | + } |
| 219 | + else cout << "Lista Vacia\n"; |
| 220 | +} |
| 221 | +void MOSTRAR_LISTA (ptnodo_h Lista){ |
| 222 | + ptnodo_h A = Lista; |
| 223 | + if (A != NULL){ |
| 224 | + while( A != NULL){ |
| 225 | + cout << setw(15) << left << A -> info.nombre; |
| 226 | + cout << setw(9) << A -> info.ced; |
| 227 | + cout << setw(9) << A -> info.ced_padre; |
| 228 | + cout << setw(4) << A -> info.edad <<endl; |
| 229 | + A = A -> sig; |
| 230 | + } |
| 231 | + } |
| 232 | + else cout << "Lista Vacia\n"; |
| 233 | +} |
| 234 | +void BORRAR(ptnodo_p &Lista){ |
| 235 | + ptnodo_p Aux = Lista; |
| 236 | + if(Aux != NULL){ |
| 237 | + Lista = Lista -> sig; |
| 238 | + delete Aux; |
| 239 | + BORRAR(Lista); |
| 240 | + } |
| 241 | +} |
| 242 | +void BORRAR(ptnodo_h &Lista){ |
| 243 | + ptnodo_h Aux = Lista; |
| 244 | + if(Aux != NULL){ |
| 245 | + Lista = Lista -> sig; |
| 246 | + delete Aux; |
| 247 | + BORRAR(Lista); |
| 248 | + } |
| 249 | +} |
| 250 | +int BUSCAR_ced(ptnodo_h Lista, int ced_h){ |
| 251 | + ptnodo_h A = Lista; |
| 252 | + if(A != NULL) |
| 253 | + { while(A -> info.ced != ced_h && A -> sig != NULL) |
| 254 | + A = A -> sig; |
| 255 | + if(A -> info.ced == ced_h) |
| 256 | + return A -> info.ced_padre; |
| 257 | + else |
| 258 | + return 0; |
| 259 | + } |
| 260 | + else |
| 261 | + { cout<<"Lista de hijos vacia\n" << endl; |
| 262 | + return 0; |
| 263 | + } |
| 264 | +} |
| 265 | +ptnodo_h hijos_porced(ptnodo_h Lista, int ced_p){ |
| 266 | + ptnodo_h A = Lista, B = NULL; |
| 267 | + _hijos registro; |
| 268 | + if(A != NULL){ |
| 269 | + while(A != NULL){ |
| 270 | + if(A -> info.ced_padre == ced_p){ |
| 271 | + registro = A -> info; |
| 272 | + INSERTAR_FIN(B, registro); |
| 273 | + } |
| 274 | + A = A -> sig; |
| 275 | + } |
| 276 | + return B; |
| 277 | + }else{ |
| 278 | + cout<<"Lista de hijos vacia\n" << endl; |
| 279 | + return NULL; |
| 280 | + } |
| 281 | +} |
| 282 | +int BUSCAR_telf(ptnodo_p Lista, int ced_p){ |
| 283 | + ptnodo_p A = Lista; |
| 284 | + if(A != NULL) |
| 285 | + { while(A -> info.ced != ced_p && A -> sig != NULL) |
| 286 | + A = A -> sig; |
| 287 | + if(A -> info.ced == ced_p) |
| 288 | + return A -> info.telf; |
| 289 | + else |
| 290 | + return 0; |
| 291 | + } |
| 292 | + else |
| 293 | + { cout<<"Lista de padres vacia\n"; |
| 294 | + return 0; |
| 295 | + } |
| 296 | +} |
0 commit comments