Simplify code

3

I did a program, based on a study I read. The study says it takes 10,000 hours to get genius / master in any area.

Can you simplify what I did? In the menu I only managed to do so, if you give me suggestions I thank you. I really want to learn and ideas to complex the program are also welcome.

Here is the code:

#include <stdio.h>
float converterDiasEmAnos(float dias) {
    return dias/365;
}
float converterHorasEmDias(int horas) {
    return 10000/horas;
}
int obterMeses(float anos) {
    return (anos -(int)anos)*(12);
}

int main()
{

    //prototipos
    float converterDiasEmAnos(float dias);
    float converterHorasEmDias(int horas);
    int obterMeses(float anos);
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    //variaveis locais
    int num_horas,meses,escolha;
    float anos,dias;
    //MENU
    printf("\nDigite a opcao correta:\n1-Continuar com o programa\n0-Para sair!\n\n");
    scanf("%d",&escolha);
    switch(escolha) {
        case 1:
            //pergunta
            printf("Quantas horas vai dedicar por dia para ser génio?");
            scanf("%d",&num_horas);
            //chamada
            dias=converterHorasEmDias(num_horas);
            anos=converterDiasEmAnos(dias);
            meses=obterMeses(anos);
            //afirmacao
            printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar génio\n",dias,anos,meses);
            break;
        case 0:
            return 0;
            break;
        default:
            printf("Digite corretamente!");
            break;

    }
return 0;
}
    
asked by anonymous 01.01.2016 / 18:36

1 answer

8

In your previous question I had already given a good organized code, now it's worse again. There is not much to simplify, you can only organize better.

One of the mistakes you make is not maintaining consistency. Time uses one way and time uses another. Time organizes, time heaps everything. Time uses unnecessary things. Pay attention character by character of what I wrote to see how it looks better.

What I "simplified":

I took the prototypes, they are only needed in three situations: a) will use in another file, b) there are circular references between the functions or c) the code is too disorganized. Since there are none of these cases in this code, I removed them (I had done this before in the previous question).

I took separate variable declarations, this is old style and unnecessary. Statements should be as close as possible to their use. You have to narrow the scope as much as possible.

I took the comments because they are not being useful at all. Comments should explain why you did something that looks strange and does not say what the code does. There are myths about comments . If you think a code snippet deserves a comment, either it is confusing or it deserves to be in a separate function. I preferred not to separate the menu, but it is a possibility.

One of the reasons I have not messed with the menu is that it seems to me that the code is in half. This menu does not seem to make any sense. If it's to simplify it anyway, the way it is, I'd just take it off. I did not take it because I think I'll do something useful with it later. If you do not, just take it off. Do not put anything in the code that does not add something really useful to the user experience. At the moment it only causes discomfort.

I took a variable declaration. On the other question I had said that it was unnecessary, but had left because it could serve anything if I tampered with the code. I have now made it simpler.

I took a% of% after% of%, it would never run. In fact it was to take other redundant things, but then% w / o% all would have to be taken because it has no function in the code. And finally the menu message would also have to be taken.

Obviously it's hard to do a code review in code that is in half. The next time you ask for this, present a complete code, even if you have to submit only one passage.

#include <stdio.h>
float converterDiasEmAnos(float dias) {
    return dias / 365;
}
float converterHorasEmDias(int horas) {
    return 10000 / horas;
}
int obterMeses(float anos) {
    return (anos - (int)anos) * 12;
}
int main() {
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    int escolha;
    printf("\nDigite a opcao correta:\n1-Continuar com o programa\n0-Para sair!\n\n");
    scanf("%d", &escolha);
    switch(escolha) {
        case 1:
            int num_horas;
            printf("Quantas horas vai dedicar por dia para ser genio?");
            scanf("%d", &num_horas);
            float dias = converterHorasEmDias(num_horas);
            float anos = converterDiasEmAnos(dias);
            printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar genio\n", dias, anos, obterMeses(anos));
            break;
        case 0:
            return 0;
        default:
            printf("Digite corretamente!");
            break;
    }
    return 0;
}

I'll put here the simplified code by removing the useless parts. You may not like it, but I'm doing what the question asks, taking everything that's useless simplifies the code:

#include <stdio.h>
float converterDiasEmAnos(float dias) {
    return dias / 365;
}
float converterHorasEmDias(int horas) {
    return 10000 / horas;
}
int obterMeses(float anos) {
    return (anos - (int)anos) * 12;
}
int main() {
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    int num_horas;
    printf("Quantas horas vai dedicar por dia para ser genio?");
    scanf("%d", &num_horas);
    float dias = converterHorasEmDias(num_horas);
    float anos = converterDiasEmAnos(dias);
    printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar genio\n", dias, anos, obterMeses(anos));
    return 0;
}

One last simplification since functions are being used only once. I know you may be wanting to use it for other things, but in that case you do not need to have functions and simplify it is usually to eliminate everything that does not need to be used:

#include <stdio.h>
int main() {
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    int num_horas;
    printf("Quantas horas vai dedicar por dia para ser genio?");
    scanf("%d", &num_horas);
    float dias = num_horas / 365;
    float anos = 10000 / dias;
    printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar genio\n", dias, anos, (anos - (int)anos) * 12);
    return 0;
}

Simplification can make it readable, but it can make it worse. I could do other things that would certainly get worse. Some that I did may have gotten worse depending on the context. Looking at this code alone has not gotten worse. It's pretty understandable, but it might not be in a larger context.

    
01.01.2016 / 19:23