How to modularize the following code C

Asked

Viewed 276 times

2

Below follows a code for date entry by the user and validation of it, but would like to modularize it. I don’t know if there is a possibility to use it outside of the box, or in an external file (e.g., "validation.h"). Can I get it leaner? Thanks in advance.

Edit1: In case, I will have to run this program in a "broker. c" which is based on function, and there are more tasks to be implemented in the code, so I would like it to stay as dry as possible, and the first thing that came into my mind was to exactly do the possible calculations and validations in an external file "lá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 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 comments colored with special characters.

1 answer

0


Follows a streamlined and modularized code capable of solving 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.