Help in C algorithmic activity

Asked

Viewed 545 times

1

Can anyone tell what I’m doing wrong in my code?

1) Make a program that determines the chronologically larger date between two dates provided by the user. Each date shall be composed of three integer values, in which the first represents the day, the second, the month and the third, the year.

Here’s what I tried:

#include <stdio.h>
#include <locale.h> //Biblioteca locale é uitlizada para definar o idioma do programa.

int main()
{
    setlocale(LC_ALL,"portuguese"); //Comando utilizado para caracteres especiais aparecerem.
    int dia,mes,ano;
    int dia2,mes2,ano2;

    printf("Comparador de datas \n\n");

    printf("Primeira data \n");
    printf("    Digite o dia (1 a 31): ");
    scanf("%d",&dia);
    printf("    Digite o mês (1 a 12): ");
    scanf("%d",&mes);
    printf("    Digite o ano: ");
    scanf("%d",&ano);

    printf("\n");

    printf("Segunda data \n");
    printf("    Digite o dia (1 a 31): ");
    scanf("%d",&dia2);
    printf("    Digite o mês (1 a 12): ");
    scanf("%d",&mes2);
    printf("    Digite o ano: ");
    scanf("%d",&ano2);

//Inicio compartivo de anos
    if (ano > ano2){
        printf("\n\n");
        printf("A maior data é %d/%d/%d \n",dia,mes,ano);
    }
    else if (ano2 > ano) {
        printf("\n\n");
        printf("A maior data é %d/%d/%d \n",dia2,mes2,ano2);
        printf("\n\n");
    }
//

//Inicio compartivo dos meses
    if (mes > mes2) {
        if (ano > ano2) {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia,mes,ano);
            printf("\n\n");
        }
        else if( ano2 > ano); {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia2,mes,ano2);
            printf("\n\n");
        }
    }

    else if (mes2 > mes) {
        if (ano > ano2) {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia,mes,ano);
            printf("\n\n");

        }
        else if (ano2 > ano); {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia2,mes2,ano2);
            printf("\n\n");

        }
    }
 //
 //Inicio compartivo dos dias
    if (dia > dia2) {
        if (ano > ano2) {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia,mes,ano);
            printf("\n\n");
        }
        else if( ano2 > ano); {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia,mes,ano2);
            printf("\n\n");
        }
    }

    else if (dia2 > dia) {
        if (ano > ano2) {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia2,mes,ano);
            printf("\n\n");

        }
        else if (ano2 > ano); {
            printf("\n\n");
            printf("A maior data é %d/%d/%d",dia2,mes2,ano2);
            printf("\n\n");

        }
    }
}

O resultado é o seguinte.

  • 1

    it’s actually simple... you’re not finishing the program after you answer.. and all the blocks if are running, so for each of them a response is being shown on the screen. Besides you are mixing the variables in printf

  • 2

    another way that you could verify is simply by adding up the values (ano + mes + dia) and checking which one is the biggest.. this would simplify your code to just one block if

  • MDS! It is true, as I had not thought of it, to hours suffering with this code. Thank you Irsael

  • The idea of Israel is good, but if it is naively done give pa problem. The sum should be made considering the quantities of each part of the date, which is much more complicated than it seems because of the irregularity of dates. , reaching the point that it ends up being simpler to convert to string and concatenate everything, even if less elegant and performatic.

  • Of course the current code is naive and accepts days 29, 30 and 31 in months that do not have this amount of days (well, in practice, it accepts even numbers completely out as millions or negatives). If accepting to do wrong at least eliminates the irregularity, then you can consider the day as a unit, the month as ten (multiplies by 31 in the full months, makes -1) and the year would be like the hundred (multiply by 372 in full years, so you have to do minus 1, obviously it gets wrong, but the basis is already wrong). In the end it would be complex too, because the problem is complex.

1 answer

1


Let’s see, under what conditions the first date is higher?

If the year of the first date is greater than that of the second date. Or if the years are equal, if the month of the first date is greater than that of the second. Or, if the years and months of the two dates are equal, but the day of the first is longer.

that is to say:

ano1 > ano2 || (ano1 == ano2 && mes1 > mes2) || (ano1 == ano2 && mes1 == mes2 && dia1 > dia2).

This can still be simplified a little:

ano1 > ano2 || ano1 == ano2 && (mes1 > mes2 || (mes1 == mes2 && dia1 > dia2)).

And to check whether the second date is larger than the first rather than the other way around, just reverse the > for <.

Another thing is that you have several ifs like this:

if (condicao) {
    comando1();
    comando2a();
    comando3();
} else {
    comando1();
    comando2b();
    comando3();
}

The comando1() and the comando3() are executed whatever the path taken in the if. That means they can be moved out of it:

comando1();
if (condicao) {
    comando2a();
} else {
    comando2b();
}
comando3();

With this, your code can be greatly simplified:

#include <stdio.h>
#include <locale.h> //Biblioteca locale é uitlizada para definar o idioma do programa.

int main() {
    setlocale(LC_ALL, "portuguese"); //Comando utilizado para caracteres especiais aparecerem.
    int dia1, mes1, ano1;
    int dia2, mes2, ano2;

    printf("Comparador de datas \n\n");

    printf("Primeira data \n");
    printf("    Digite o dia (1 a 31): ");
    scanf("%d", &dia1);
    printf("    Digite o mês (1 a 12): ");
    scanf("%d", &mes1);
    printf("    Digite o ano: ");
    scanf("%d", &ano1);

    printf("\n");

    printf("Segunda data \n");
    printf("    Digite o dia (1 a 31): ");
    scanf("%d", &dia2);
    printf("    Digite o mês (1 a 12): ");
    scanf("%d", &mes2);
    printf("    Digite o ano: ");
    scanf("%d", &ano2);

    // Compartivo
    printf("\n\n");
    if (ano1 > ano2 || ano1 == ano2 && (mes1 > mes2 || (mes1 == mes2 && dia1 > dia2))) {
        printf("A maior data é %d/%d/%d \n", dia1, mes1, ano1);
    } else if (ano1 < ano2 || ano1 == ano2 && (mes1 < mes2 || (mes1 == mes2 && dia1 < dia2))) {
        printf("A maior data é %d/%d/%d \n", dia2, mes2, ano2);
    } else {
        printf("As datas são iguais: %d/%d/%d \n", dia1, mes1, ano1);
    }
    printf("\n\n");

    return 0;
}

See here working on ideone.

Browser other questions tagged

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