How can I improve?

Asked

Viewed 44 times

0

I started learning C a few days ago and created this program, which in a simple way calculates a person’s salary after receiving some data from it... However I don’t work in the IT area and I have no idea what the standards used in everyday life among programmers are like to ask you to show me what in my code is outside of those standards and how I could make the code simpler or more effective...

#include <stdio.h>

int main() {
        float h, din, valorh, horasnormais, normal,
        extras, horas, inicio, resultado, minimo, turno, nivel;

        float base(float x, float y);
        float horaextra1(float x, float y, float z);
        float horaextra2(float x, float y);
        float inss(float z);
        float irrf(float z);
        float calcula_salario(float b, float h1, float h2, float ir, float ins, float insa, float a);
        float insalubridade(float minimo, float nivel);
        float adicional(float t, float valorh);

        minimo = 1045;
        system("cls || clear");

        printf("SISTEMA PARA CALCULAR SALARIO\n");
        printf("Digite a quantidade de horas trabalhadas: ");
        scanf("%f", &h);
        printf("Digite o valor da hora: ");
        scanf("%f", &valorh);
        printf("Qual o nível da insalubridade? ");
        scanf("%f", &nivel);
        printf("Qual o turno de trabalho? ");
        scanf("%f", &turno);

        if (h > 220 && h <= 242) {
                resultado = calcula_salario(base(h, valorh), horaextra1(h, valorh, base(h, valorh)),
                                         0, inss(base(h, valorh)), irrf(base(h, valorh)), insalubridade(minimo, nivel), adicional(turno, valorh));
                printf("O valor do salario é de: %.2f\n", resultado);
        }else if (h > 242) {
                inicio;
                resultado = calcula_salario(base(h, valorh), horaextra1(h, valorh, base(h, valorh)),
                                         horaextra2(h, valorh), inss(base(h, valorh)), irrf(base(h, valorh)), insalubridade(minimo, nivel), adicional(turno, valorh));
                printf("O valor do salário é de: %.2f\n", resultado);
        }else {
                normal = calcula_salario(base(h, valorh), 0, 0, inss(base(h, valorh)),
                                        irrf(base(h, valorh)), insalubridade(minimo, nivel), adicional(turno, valorh));
                printf("O valor do salário é de: %.2f\n", normal);
        }

        return 0;
}

float calcula_salario(float base, float extra1, float extra2, float inss, float irrf, float insa, float add) {
        float salario;
        salario = base + extra1 + extra2 - inss - irrf + insa + add;
        return salario;
}

float base(float x, float y){
        return x * y;
}

float horaextra1 (float x, float y, float z){
        float horasamais, valorextra, extra, salarioextra;
        horasamais = x - 220;
        extra = horasamais * 0.5;
        salarioextra = horasamais + extra * y;
        return salarioextra;
}

float horaextra2 (float x, float y) {
        float base, horasemdobro, valorextra, salarioemdobro, z;
        z = 33 * y;
        horasemdobro = x - 242;
        base = x - 22 * y;
        valorextra = horasemdobro * y;
        return valorextra;
}

float inss(float z){
        float valordesconto, diferenca;
        if(z <= 1045) {
                valordesconto = z * 0.075;
                return valordesconto;
        }else if(z <= 2089.60) {
                diferenca = z - 1045;
                valordesconto = diferenca * 0.09 + 78.37;
                return valordesconto;
        }else if(z <= 3134.41) {
                diferenca = z - 2089.60;
                valordesconto = diferenca * 0.12 + 78.37 + 94.01;
                return valordesconto;
        }else if(z >= 6104.06) {
                valordesconto = 713.08;
                return valordesconto;
        }else {
                printf("Algo deu errado inss\n");
        }

}

float irrf(float z) {
        if (z > 1903.99 && z <= 2826.66) {
                return z * 0.075 - 142.8;
        }else if(z > 2826.66 && z <= 3751.05) {
                return z * 0.15 - 354.8;
        }else if(z > 3751.05 && z <= 4664.68) {
                return z * 0.225 - 636.13;
        }else if(z > 4664.68) {
                return z * 0.275 - 869.36;
        }
}
float irrf(float z) {
        if (z > 1903.99 && z <= 2826.66) {
                return z * 0.075 - 142.8;
        }else if(z > 2826.66 && z <= 3751.05) {
                return z * 0.15 - 354.8;
        }else if(z > 3751.05 && z <= 4664.68) {
                return z * 0.225 - 636.13;
        }else if(z > 4664.68) {
                return z * 0.275 - 869.36;
        }
}

float insalubridade(float minimo, float nivel) {
        if (nivel == 1) {
                return minimo * 0.1;
        }else if(nivel == 2) {
                return minimo * 0.2;
        }else if(nivel == 3) {
                return minimo * 0.4;
        }
        else {
                printf("Insalubridade desconhecida\n");
        }
}

float adicional(float t, float valorh) {
        float horasnoturnas, valornoturno, adicional;
        if(t == 2) {
                printf("Horas trabalhadas por dia após 21h: ");
                scanf("%f", &horasnoturnas);
                valornoturno = valorh * 0.2;
                adicional = horasnoturnas * valornoturno;
                return adicional;
                }else if(t == 1) {
                        return adicional = 0;
                }else{
                        printf("Turno não reconhecido\n");
                }
}
´´´

1 answer

0

Some tips for better your code:

1 - the function main, responsible for executing a code, returns an integer and receives no parameter. If the function does not receive any parameter, the most indicated is that you put the word void in parameter space. Although it seems a small difference, some compilers do not compile if you do not. It would look like this:

int main(void) {//resto do código}

2 - your code is quite modularized, this is a positive point of it, congratulations. You made the signatures of the methods within the function main. Usually, they are done before the main function. The purpose of modularization (the division of the code into several functions) is to make the functions as independent as possible from each other. In doing so, you leverage this concept within your code;

3 - Always think about using variable names that make your content as clear as possible. You did this for variables within the function main, but some function parameters did not follow the same pattern. In function irrf(), for example, it is difficult for a person who is having the first contact with their code to know the utility of the variable z within the function. Make it a habit to use meaningful variable names from the very beginning of programming. Most of the codes you write will be read by other people or by yourself in the future. Having this habit facilitates this future contact;

4 - in C, functions that return a certain type of data (i.e., all those that are not void) must necessarily return a value at the end of its execution. In some functions of your code, for example in the method unsanitary, if the value of the variable level not 1, 2 or 3, you print an error message, but you do not return any value. By itself this would generate a warning message in most compilers and, during execution, would generate an error. As for this, two points:

  • you should check if the variable passed to a function is correct before calling the function. You could do this through conditional structures within the function main(), before performing the functions;
  • If you do not want to do this, return in the function a value that indicates error, that is, an invalid value within the context of the function. In this case, you could return the number -1. If on receiving, in the function main(), this value of this function, you know an error occurred.

5 - the processing that you within some functions return specific values according to the received values, as in the function irrf(), where the return value depends on the variable value z, received by parameter. As to the numbers used within the ifs and elses, avoid placing numeric values directly inside the code. Instead, use the directive DEFINES, that allows defining constants identified by a certain name. Its application is small, and it wouldn’t make much difference. But imagine that you are developing a large program, and that these values are used in many different functions. If you need to change their content, you will need to change function by function, which will generate a very large job. Using the directive DEFINES, changing the value of the constant only once the entire code will be changed. In addition, it will be easier for other developers (or, as already stated, for yourself reading your code in the future) to understand or remember what the context of these values is within the code.

Moreover, for a beginner your code is very good. Congratulations!

Browser other questions tagged

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