Answer the question
In order to leave comments, you need to log in
I want to know the opinion of good C (SI) programmers about my calculator, can I have some advice?
I want to know the opinion of good C (SI) programmers about my calculator, can I have some advice? This is my first C project.
#include <stdio.h>
int main()
{
int num_1;
int num_2;
int result;
char wait[2];
char vopros[2];
char vopros_2[2];
do {
printf("\n\t\t\t\t\tКАЛЬКУЛЯТОР 2.0");
printf("\nВведите первое число: ");
scanf("%d", &num_1);
printf("\nВведите второе число: ");
scanf("%d", &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;
}
printf("%d %s %d = %d\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("%d", &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;
}
printf("Result - %d\n", result);
}
}
while (vopros_2[0] != 'N');
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
Where is the divide-by-0 handler?
If the user enters non-integer numbers?
And if, when dividing, the user enters, for example, 5 and 3. And you have a variable of an integer type.
You violated the DRY principle.
The code is like this.
Well ... for the first project it will do - anyway, as a rule, the first project is a place for stuffing cones and bumping into common places ... The
user is a moron! Always proceed from this. He introduces nonsense. He doesn't do what is expected of him. It ignores warnings. ALL his actions must be checked. ALL results must be verified.
Variables that will control the program must be initialized! The choices that run the program - check! What happens if the user enters any other character than the operation sign? That's right, SIGSEGV :) Because result does not matter, and in the ridiculous cascade if, the situation "the user entered nonsense" is not provided for, and an uninitialized variable will go to printf() :)
Avoid cascading ifs when you can use case
scanf() is justified only when you need to convert a number, in your case you need to use getchar()
Instead of complicating the code with a double condition (n or N), you should use tolower() / toupper() there was one
Calculation is integer and will give unexpected result with non-integer operands. For example 4 / 2 will give 2 (as it should), but 5 / 2 will also give 2!
No handling of operations with zero - for example, when dividing by zero, the program will most likely receive SIGILL
In general, there is something to work on :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question