Skip to content

Instantly share code, notes, and snippets.

@OverlordEx3
Created August 13, 2017 23:26
Show Gist options
  • Save OverlordEx3/f13ea13d74e542a03eadecbb26040e34 to your computer and use it in GitHub Desktop.
Save OverlordEx3/f13ea13d74e542a03eadecbb26040e34 to your computer and use it in GitHub Desktop.

Revisions

  1. Exequiel Beker created this gist Aug 13, 2017.
    245 changes: 245 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,245 @@
    /*
    * File: menu.c
    * Author: Hernandez Gabriel
    *
    * Created on 5 de julio de 2016, 00:59
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct{
    unsigned int cp;
    char nombre[30];
    int habitantes;/*--habitantes.*/
    float superficie;
    unsigned int estado;
    }FormularioDatos;

    void Alta(){ /*Comienzo del proc.*/
    FormularioDatos Localidad;
    int i;
    char opc1;
    FILE *fp;


    printf("Usted eligio alta, esta seguro (S/N): ");
    scanf("%c",&opc1);
    fflush(stdin);
    if((opc1 != 's') && (opc1 != 'S')) {
    printf("no estas seguro :( \n");
    return;
    }

    fp = fopen("Alta.txt","ab+"); //creo el archivo.txt
    if(!fp) {
    return;
    }

    for(i=0;i<=19;i++){ /*Verifica en que lugar se puede ingresar los datos.*/
    if(Localidad.estado == 0) {
    break; /*salto de linea*/
    }
    }

    system("cls");

    printf("Ingrese codigo postal: ");
    scanf("%u",&Localidad.cp);
    fflush(stdin);

    printf("Ingrese un Nombre: ");
    fgets(Localidad.nombre, 29, stdin);
    fflush(stdin);

    printf("Ingrese cantidad de habitantes: ");
    scanf("%d",&Localidad.habitantes);
    fflush(stdin);

    printf("Ingrese la superficie: ");
    scanf("%f",&Localidad.superficie);
    fflush(stdin);

    Localidad.estado=1;


    fwrite(&Localidad, sizeof(Localidad), 1, fp);

    fclose(fp);
    /*system("clr"); borrar patalla*/
    printf("Los datos se han guardado\n");
    getchar();
    fflush(stdin);
    }

    //No se hacen operaciones sobre el archivo?

    void Baja(){/*Comienzo del proc.*/
    FormularioDatos Localidad;
    char opc2 = 0;
    int pos = 0;
    // FILE *ArchivoOriginal;
    // FILE *ArchivoExterno;
    printf("\nUsted eligio baja, esta seguro (S/N): ");
    scanf("%c",&opc2);
    if(opc2 == 's' || opc2 == 'S'){

    printf("Eliga la pocicion que quiera borrar: ");
    scanf("%d",&pos);
    Localidad.cp = 0;
    strcpy(Localidad.nombre,""); /*borrar cadena*/
    Localidad.habitantes = 0;
    Localidad.superficie = 0;
    Localidad.estado = 0;
    /* system("clr"); /*borrar patalla*/
    printf("Se borro la informacion..\n");
    getchar();
    return;
    }
    else{
    /* system("clr");*/
    printf("No esta seguro :( \n");
    getchar();
    }
    }

    void Modificar(){
    FormularioDatos Localidad;
    int pocicion = 0;
    int modif = 0;
    int i = 0;
    char opc3 = 0;
    fflush(stdin);
    printf("\nUsted eligio modificar, esta seguro (S/N): ");
    scanf("%c",&opc3);
    if(opc3 == 's' || opc3 == 'S'){
    printf("Eliga la pocicion que quiera modificar (empieza desde 0): ");
    scanf("%d",&pocicion);
    for(i=0;i<=19;i++){
    if(pocicion == i){
    while(modif!=5){
    fflush(stdin);
    printf("* Modificar\n");
    printf("* 1.cp\n");
    printf("* 2.nombre\n");
    printf("* 3.cantidad\n");
    printf("* 4.superficie\n");
    printf("* 5.salir\n");
    printf("Eliga la opcion: ");
    modif = getchar();
    switch(modif){
    case '1':
    printf("\ningrese nuevo cp: ");
    scanf("%d",&Localidad.cp);
    printf("\nSe a ingresado nuevo cp.");
    break;
    case '2':
    printf("\ningrese nuevo nombre: ");
    scanf("%d",&Localidad.nombre);
    printf("\nSe a ingresado nuevo nombre.");
    break;
    case '3':
    printf("\ningrese nuevo habitantes: ");
    scanf("%d",&Localidad.habitantes);
    printf("\nSe a ingresado nuevo habitantes.");
    break;
    case '4':
    printf("\ningrese nuevo superficie: ");
    scanf("%d",&Localidad.superficie);
    printf("\nSe a ingresado nuevo superficie.");
    break;
    case '5':
    modif = 5;
    break;
    case '\n':
    printf("\nUsted no selecciono nada...");
    break;
    default:
    printf("\nUsted apreto una tecla incorrecta...\a");
    }
    }
    }
    }
    }
    else{
    printf("\nNo esta seguro...");
    return;
    }
    }

    void Listado(){ /*Comienzo del proc.*/
    FILE *ArchivoListado;
    FormularioDatos Localidad;
    ArchivoListado = fopen ( "Alta.txt", "rb+" );
    char Opc4;
    printf("\nUsted eligio listado, esta seguro (S/N): ");
    Opc4 = getchar();
    fflush(stdin);
    if((Opc4 != 's') && (Opc4 != 'S')){
    printf("no estas seguro :( \n");
    return;
    }
    if (!ArchivoListado) {
    exit(1);
    return;
    }

    printf("\n");
    while(fread(&Localidad, sizeof(Localidad), 1, ArchivoListado) > 0){
    printf("CP: %d\nNombre: %sCantidad de habitantes: %d\nSuperficie: %.1f\n\n\n", Localidad.cp, Localidad.nombre, Localidad.habitantes, Localidad.superficie);
    }

    fclose(ArchivoListado);
    /* system("clr"); /*borrar patalla*/
    printf("Apriete una tecla para continuar...");
    getchar();
    return;
    }

    int main(int argc, char** argv) {
    int opcion = 0;

    while(opcion != '5') {
    printf("****** MENU ******\n");
    printf("* 1.ALTA *\n");
    printf("* 2.BAJA *\n");
    printf("* 3.MODIFICACION *\n");
    printf("* 4.LISTADO *\n");
    printf("* 5.SALIR *\n");
    printf("Eliga un numero: ");
    opcion = getchar();

    fflush(stdin);
    system("cls");
    switch (opcion){
    case '1':
    Alta();
    break;
    case '2':
    Baja();
    break;
    case '3':
    Modificar();
    break;
    case '4':
    Listado();
    break;
    case '5':
    break; //Se debería evaluar en el while()
    case '\n':
    printf("\nUsted no selecciono nada...");
    break;
    default:
    printf("\nUsted apreto una tecla incorrecta...\a");
    break;
    }
    system("CLS");
    }

    printf("Usted salio del menu.... \n");

    printf("Press enter to continue ...");
    getchar();
    return 0;
    }