K
K
Kaktys_DH2020-07-25 14:59:59
C++ / C#
Kaktys_DH, 2020-07-25 14:59:59

Please criticize the C code and give advice?

#include <stdio.h>
int main()
{

float num_1 = 0;
float num_2 = 0;
float result = 0;
char wait[2] = {0};
char vopros[2] = {0};
char vopros_2[2] = {0};

do {
printf("\n\t\t\t\t\tКАЛЬКУЛЯТОР 3.0");

printf("\nВведите первое число: ");
scanf("%f", &num_1);

printf("\nВведите второе число: ");
scanf("%f", &num_2);

printf("\nВыберите действие - +, -, *, /: ");
scanf("%s", wait);

if (wait[0] == '+') {
result = num_1 + num_2;
}

else if (wait[0] == '-') {
result = num_1 - num_2;
}

else if (wait[0] == '*') {
result = num_1 * num_2;
}

else if (wait[0] == '/') {
result = num_1 / num_2;
}

else {
  printf("Ошибка синтаксиса\n");
}

if (  ( wait[0] == '/' && (num_1 == 0 && num_2 == 0) )  ||  ( wait[0] == '/' && (num_1 == 0 || num_2 == 0) )  ) {

printf("\n\nТЫ ЧТО-ТО ДЕЛАЕШЬ НЕ ТАК!!!\n\n");
}
goto metka;

printf("%f %s %f = %f\n", num_1, wait, num_2, result);

do {
printf("Хотите воспроизвести действие еще с одним числом?(Y/n)");
scanf("%s", vopros_2);

if (vopros_2[0] == 'n') {
   vopros_2[0] = 'N';
}

else if (vopros_2[0] != 'n' || vopros_2[0] != 'N') {
num_1 = 0;
num_2 = 0;
char wait[2] = {0};

printf("Введите число\n");
scanf("%f", &num_1);

printf("\nВыберите действие - +, -, *, /: ");
scanf("%s", wait);

if (wait[0] == '+') {
result = result + num_1;
}
else if (wait[0] == '-') {
result = result - num_1;
}
else if (wait[0] == '*') {
result = result * num_1;
}
else if (wait[0] == '/') {
result = result / num_1;
}
else {
  printf("Ошибка синтаксиса\n");
}

printf("Result - %.6f\n", result);
}


}
while (vopros_2[0] != 'N');
metka:
printf("Хотите попробывать еше раз? (Y/n): ");
scanf("%s", vopros);

if (vopros[0] == 'n') {
vopros[0] = 'N';
}

}
while (vopros[0] != 'N');

return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnT, 2020-07-27
@TheCalligrapher

Problems, in order of occurrence
1. In C, prefer over 2. Unreasonable use of type int main(void)instead of . 3. Some strange mixture of non-localized and localized variable declarations, incl. variables with the same name in nested scopes. Eg. 4. not protected from overflow in any way. And this despite the fact that the buffer has a minimum size. Better . Of course, this will not solve all potential problems with incorrect input... 5.int main()

if (  ( wait[0] == '/' && (num_1 == 0 && num_2 == 0) )  ||  ( wait[0] == '/' && (num_1 == 0 || num_2 == 0) )  )
- what is this strange double check of the same thing?
6. gotoit is not at all appropriate here.
7. What is this strange duplication of the same code?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question