Created
November 17, 2019 09:43
-
-
Save VentGrey/0d3ea52e2d181da113a447a3845a4e20 to your computer and use it in GitHub Desktop.
A small CRUD made in C using Hero / Mission perspective.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -- Inclusión de cabeceras | |
#include <stdio.h> | |
#include <stdlib.h> | |
// -- Estrucuras | |
typedef struct { | |
int id; | |
char nombre[20]; | |
char apodo[20]; | |
char clase[20]; | |
char habilidades[80]; | |
}Heroe; | |
typedef struct { | |
int id; | |
double recompensa; | |
int experiencia; | |
char habilidades[80]; | |
}Misiones; | |
// -- Protoclase de menú | |
void menu(); | |
// -- Protoclases de alojamiento | |
void AltaHeroes(); | |
void BajaHeroes(); | |
void ModHeroes(); | |
void ListHeroes(); | |
// -- Protoclases de histórico | |
void AltaMision(); | |
void BajaMision(); | |
void ModMision(); | |
void ListMision(); | |
// -- Función main | |
int main() | |
{ | |
menu(); | |
return 0; | |
} | |
void menu() | |
{ | |
int opcion,opcion2; | |
do { | |
printf("---------------HIRE-A-HERO--------------\n"); | |
printf("--------------------------------------\n"); | |
printf(" 1) Heroes \n"); | |
printf(" 2) Misiones \n"); | |
printf(" 3) Salir \n"); | |
printf("--------------------------------------\n"); | |
printf("--------------------------------------\n"); | |
scanf("%d",&opcion); | |
printf("\n\n\n\n\n\n\n\n\n\n\n\n"); | |
printf("---------------HIRE-A-HERO--------------\n"); | |
printf("--------------------------------------\n"); | |
printf(" 1) Alta \n"); | |
printf(" 2) Baja \n"); | |
printf(" 3) Modificación \n"); | |
printf(" 4) Listado \n"); | |
printf(" 5) Volver al Menú \n"); | |
printf("--------------------------------------\n"); | |
printf(" -------------------------------------\n"); | |
scanf("%d",&opcion2); | |
printf("\n\n\n\n\n\n\n\n\n\n\n\n"); | |
switch (opcion) { | |
case 1: | |
switch(opcion2) { | |
case 1: | |
AltaHeroes(); | |
break; | |
case 2: | |
BajaHeroes(); | |
break; | |
case 3: | |
ModHeroes(); | |
break; | |
case 4: | |
ListHeroes(); | |
break; | |
case 5: | |
menu(); | |
break; | |
} | |
break; | |
case 2: | |
switch(opcion2){ | |
case 1: | |
AltaMision(); | |
break; | |
case 2: | |
BajaMision(); | |
break; | |
case 3: | |
ModMision(); | |
break; | |
case 4: | |
ListMision(); | |
break; | |
case 5: | |
menu(); | |
break; | |
} | |
break; | |
case 3: | |
break; | |
} | |
}while (opcion!=5); | |
} | |
void AltaHeroes() | |
{ | |
FILE *pf; | |
Heroe heroes; | |
pf = fopen("Heroes.dat","ab"); | |
printf("Ingrese el id del heroe\n"); | |
scanf("%i" ,&heroes.id); | |
printf("Ingrese el nombre del heroe\n"); | |
scanf("%s", heroes.nombre); | |
printf("Ingrese el apodo del heroe\n"); | |
scanf("%s", heroes.apodo); | |
printf("Ingrese la clase del heroe\ | |
(Ladrón, Caballero, Hechicero, etc)\n"); | |
scanf("%s", heroes.clase); | |
printf("Ingrese la habilidades que el héroe posee\n"); | |
scanf("%s", heroes.habilidades); | |
fseek(pf, 0L, SEEK_END); | |
fwrite(&heroes, sizeof(Heroe), 1, pf); | |
fclose(pf); | |
// -- Imprimir vergos de newlines para "simular" un clearscreen; | |
printf("\n\n\n\n\n\n\n\n\n\n\n\n"); | |
menu(); | |
} | |
void ListHeroes() | |
{ | |
FILE *pf; | |
Heroe heroes; | |
pf = fopen("Heroes.dat","rb"); | |
fread(&heroes, sizeof(Heroe), 1, pf); | |
while (!feof(pf)) { | |
printf("%i ; %s ; %s ; %s ; %s\n",heroes.id,heroes.nombre, | |
heroes.apodo,heroes.clase,heroes.habilidades); | |
fread(&heroes, sizeof(Heroe), 1, pf); | |
} | |
fclose(pf); | |
} | |
void ModHeroes() | |
{ | |
FILE *pf,*pfaux; | |
Heroe heroes; | |
int codigoaux; | |
pf = fopen("Heroes.dat","rb"); | |
pfaux = fopen("Heroesaux.dat","ab"); | |
printf("Ingrese el ID a modificar\n"); | |
scanf("%i",&codigoaux); | |
fread(&heroes, sizeof(Heroe), 1, pf); | |
while (!feof(pf)) { | |
if (heroes.id != codigoaux) { | |
fseek(pfaux,0l,SEEK_END); | |
fwrite(&heroes,sizeof(Heroe),1,pfaux); | |
} else { | |
printf("Ingrese el nombre\n"); | |
scanf("%s",heroes.nombre); | |
printf("Ingrese un nuevo apodo\n"); | |
scanf("%s",heroes.apodo); | |
printf("Ingrese la clase\n"); | |
scanf("%s",heroes.clase); | |
printf("Ingrese las habilidades\n"); | |
scanf("%s",heroes.habilidades); | |
fseek(pfaux, 0l, SEEK_END); | |
fwrite(&heroes, sizeof(Heroe), 1, pfaux); | |
} | |
fread(&heroes,sizeof(Heroe),1,pf); | |
} | |
fclose(pf); | |
fclose(pfaux); | |
remove("Heroes.dat"); | |
rename("Heroesaux.dat","Heroes.dat"); | |
} | |
void BajaHeroes() | |
{ | |
FILE *pf,*pfaux; | |
Heroe heroes; | |
int codigoaux; | |
pf = fopen("Heroes.dat","rb"); | |
pfaux = fopen("Heroesaux.dat","ab"); | |
printf("Ingrese el ID a buscar\n"); | |
scanf("%i",&codigoaux); | |
fread(&heroes, sizeof(Heroe), 1, pf); | |
while (!feof(pf)) { | |
if (heroes.id != codigoaux) { | |
fseek(pfaux, 0l, SEEK_END); | |
fwrite(&heroes, sizeof(Heroe), 1, pfaux); | |
} | |
fread(&heroes,sizeof(Heroe),1,pf); | |
} | |
fclose(pf); | |
fclose(pfaux); | |
remove("Heroes.dat"); | |
rename("Heroesaux.dat","Heroes.dat"); | |
} | |
void AltaMision() | |
{ | |
FILE *pf; | |
Misiones hist; | |
pf = fopen("historico.dat","ab"); | |
printf("Ingrese el id de la misión\n"); | |
scanf("%i" ,&hist.id); | |
printf("Ingrese la recompensa de la misión\n"); | |
scanf("%lf", &hist.recompensa); | |
printf("Ingrese la experiencia que otorgará la misión \n"); | |
scanf("%i", &hist.experiencia); | |
printf("Ingrese las habilidades necesarias para la mision\n"); | |
scanf("%s", hist.habilidades); | |
fseek(pf, 0L, SEEK_END); | |
fwrite(&hist, sizeof(Misiones), 1, pf); | |
fclose(pf); | |
// -- Imprimir vergos de newlines para "simular" un clearscreen; | |
printf("\n\n\n\n\n\n\n\n\n\n\n\n"); | |
menu(); | |
} | |
void ListMision() | |
{ | |
FILE *pf; | |
Misiones hist; | |
pf = fopen("Heroes.dat","rb"); | |
fread(&hist, sizeof(Misiones), 1, pf); | |
while (!feof(pf)) { | |
printf("%i ; %lf ; %i ; %s ;\n",hist.id, hist.recompensa, | |
hist.experiencia, hist.habilidades); | |
fread(&hist,sizeof(Misiones), 1, pf); | |
} | |
fclose(pf); | |
} | |
void ModMision() | |
{ | |
FILE *pf,*pfaux; | |
Misiones hist; | |
int codigoaux; | |
pf = fopen("Heroes.dat","rb"); | |
pfaux = fopen("Heroesaux.dat","ab"); | |
printf("Ingrese Código\n"); | |
scanf("%i",&codigoaux); | |
fread(&hist, sizeof(Misiones), 1, pf); | |
while (!feof(pf)) { | |
if (hist.id != codigoaux) { | |
fseek(pfaux, 0l, SEEK_END); | |
fwrite(&hist, sizeof(Misiones), 1, pfaux); | |
} else { | |
printf("Ingrese la recompensa\n"); | |
scanf("%lf",&hist.recompensa); | |
printf("Ingrese la experiencia del la Misión\n"); | |
scanf("%i",&hist.experiencia); | |
printf("Ingrese las habilidades\n"); | |
scanf("%s",hist.habilidades); | |
fseek(pfaux,0l,SEEK_END); | |
fwrite(&hist,sizeof(Misiones),1,pfaux); | |
} | |
fread(&hist,sizeof(Misiones),1,pf); | |
} | |
fclose(pf); | |
fclose(pfaux); | |
remove("Misioness.dat"); | |
rename("Misionessaux.dat","Misioness.dat"); | |
} | |
void BajaMision() | |
{ | |
FILE *pf,*pfaux; | |
Misiones hist; | |
int codigoaux; | |
pf = fopen("Misioness.dat","rb"); | |
pfaux = fopen("Misionessaux.dat","ab"); | |
printf("Ingrese el ID a buscar\n"); | |
scanf("%i",&codigoaux); | |
fread(&hist, sizeof(Misiones), 1, pf); | |
while (!feof(pf)) { | |
if (hist.id != codigoaux) { | |
fseek(pfaux, 0l, SEEK_END); | |
fwrite(&hist, sizeof(Misiones), 1, pfaux); | |
} | |
fread(&hist,sizeof(Misiones),1,pf); | |
} | |
fclose(pf); | |
fclose(pfaux); | |
remove("Misioness.dat"); | |
rename("Misionessaux.dat","HIstoricos.dat"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment