This commit is contained in:
Deinerovych 2024-10-16 10:07:34 +02:00
parent 23fd7ac3c3
commit e99adaa6a4

View File

@ -11,7 +11,7 @@ int stack_top = -1;
bool is_number(char *string) {
char *end;
strtof(string, &end);
return end != string && *end == '\0';
return end != string && *end == '\n';
}
bool is_operation(char *string) {
@ -33,7 +33,7 @@ float calculator(float n1, float n2, char operation) {
return n1 / n2;
} else {
printf("Error: division by zero\n");
exit(1);
return 0;
}
}
return 0;
@ -44,7 +44,6 @@ void push(float number) {
stack[++stack_top] = number;
} else {
printf("Error: stack overflow\n");
exit(1);
}
}
@ -53,7 +52,7 @@ float pop() {
return stack[stack_top--];
} else {
printf("Error: stack is empty\n");
exit(1);
return 0;
}
}
@ -70,12 +69,12 @@ int main() {
if (is_number(arr)) {
float number = strtof(arr, &pend);
push(number);
printf("Number added: %.2f\n", number);
printf("%.2f ", number);
}
else if (is_operation(arr)) {
if (stack_top < 1) {
printf("Error: not enough numbers in the stack for operation\n");
return 1;
printf("no input\n");
continue;
}
float n2 = pop();
@ -83,18 +82,18 @@ int main() {
char operation = arr[0];
float result = calculator(n1, n2, operation);
push(result);
printf("Operation result: %.2f\n", result);
printf("%.2f ", result);
}
else {
printf("Error: invalid input\n");
return 1;
printf("no input\n");
continue;
}
}
if (stack_top == 0) {
printf("Final result: %.2f\n", pop());
printf("\n%.2f\n", pop());
} else {
printf("Error: stack contains more than one number after operations\n");
printf("\nno input\n");
}
return 0;