How to modularize the following C code

2

Below is a code for user input and validation, but I would like to modularize it. I do not know if there is a possibility of using it out of the box, or in an external file (eg "validation.h"). Can I make him leaner? Thanks in advance.

Edit1: In this case, I will have to run this program in a "broker.c" that is function based, and there are more tasks to be implemented in the code, so I would like it to be as lean as possible, first thing that came into my mind was to do the possible calculations and validations in an external file "lálálá.h" and leave only the interaction in the main file "main.c".

Edit2: Exactly what the user Lacobus mentioned, was what I needed to understand in order to proceed with the development of the issues. Thank you !!

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


struct DMA{
    int dia;
    int mes;
    int ano;
};
typedef struct DMA dma;


int main(void)
{
    dma data;

    printf("Informe a data: ");
    scanf("%d/%d/%d", &data.dia, &data.mes, &data.ano);

    if(data.ano >= 10 && data.ano <= 9999){ //!checa o ano

        if (data.mes >= 1 && data.mes <= 12){ //! checa o mes

            if ((data.dia >= 1 && data.dia <= 31) && (data.mes == 1 || data.mes == 3 || data.mes == 5 || data.mes == 7 || data.mes == 8  || data.mes == 10 || data.mes == 12)){
                puts("Data valida!"); //!checa os meses que tem 31 dias
            }
            else 
            if ((data.dia >= 1 && data.dia <= 30) && (data.mes == 4 || data.mes == 6 || data.mes == 9 || data.mes == 11)){
                puts("Data valida!"); //!checa os meses que tem 30 dias
            }
            else if (data.dia >=1 && data.dia <= 28 && data.mes == 2){
                puts("Data valida!"); //!valida as datas do mes 2
            }

            else if (data.dia == 29 && data.mes == 2 && (data.ano % 400 == 0 || (data.ano % 4 ==0 && data.ano % 100 != 0))){
                puts("Data valida! (Ano bissexto)"); //!checa o ano bissexto e imprime informando
            }

            else{
                puts("## ATENCAO: Dia invalido! ##"); //!informa caso DIA inserido for invalido
            }
        }
        else{
            puts("## ATENCAO: Mes invalido! ##"); //!informa caso MES inserido for invalido
        }
    }

    else{
        puts("## ATENCAO: Ano invalido! ##"); //!informa caso ANO inserido for invalido
    }


    return 0;
}

PS: The "!" in the comments is that I use VSCode and an extension that leaves the comments colorful with special characters.

    
asked by anonymous 18.06.2018 / 09:40

1 answer

0

Follow a lean, modularized code that can solve your problem:

foobar.h :

#ifndef __FOOBAR_H__
#define __FOOBAR_H__

#define ERR_DIA_BISSEXTO_INVALIDO  (-4)
#define ERR_DIA_INVALIDO           (-3)
#define ERR_MES_INVALIDO           (-2)
#define ERR_ANO_INVALIDO           (-1)
#define DATA_OK                    (0)

#define BISSEXTO(ano) ((ano % 4) == 0 && ((ano % 100) != 0 || (ano % 400) == 0))

int validar_data( int dia, int mes, int ano );

#endif

foobar.c :

#include "foobar.h"

int validar_data( int dia, int mes, int ano )
{
    if( ano < 10 || ano > 9999 )
        return ERR_ANO_INVALIDO;
    else if( mes < 1 || mes > 12 )
        return ERR_MES_INVALIDO;
    else if( (dia > 31) && (mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 8  || mes == 10 || mes == 12) )
        return ERR_DIA_INVALIDO;
    else if( (dia > 30) && (mes == 4 || mes == 6 || mes == 9 || mes == 11) )
        return ERR_DIA_INVALIDO;
    else if( !BISSEXTO( ano ) && mes == 2 && dia > 28 )
        return ERR_DIA_BISSEXTO_INVALIDO;
    else if( BISSEXTO( ano ) && mes == 2 && dia > 29 )
        return ERR_DIA_BISSEXTO_INVALIDO;
    else
        return DATA_OK;
}

main.c

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

#include "foobar.h"

int main( void )
{
    int ret, dia, mes, ano;

    printf("Informe a data (DD/MM/AAAA): ");

    ret = scanf("%d/%d/%d", &dia, &mes, &ano );

    if( ret != 3 )
    {
        printf( "Data nao formatada corretamente!\n" );
        return 0;
    }

    ret = validar_data( dia, mes, ano );

    switch( ret )
    {
        case DATA_OK:
            printf( "Data valida!\n" );
            break;
        case ERR_ANO_INVALIDO:
            printf( "Ano invalido!\n" );
            break;
        case ERR_MES_INVALIDO:
            printf( "Mes invalido!\n" );
            break;
        case ERR_DIA_INVALIDO:
            printf( "Dia invalido!\n" );
            break;
        case ERR_DIA_BISSEXTO_INVALIDO:
            printf( "Dia invalido para um ano bissexto!\n" );
            break;
        default:
            printf( "Data invalida!\n" );
            break;
    }

    return 0;
}
    
18.06.2018 / 13:35