Aktualizovat du3/program.c

This commit is contained in:
Mykola Syniavskyi 2025-10-17 00:48:44 +00:00
parent 369d5b5f86
commit bc1eb2d886

View File

@ -45,20 +45,23 @@ int main() {
char operator;
float new, a, b, result;
init_stack(&mystack); //обнуляємо стек
float chyslo;
char symbol;
init_stack(&mystack); //обнуляємо стек
while (fgets(buffer, BUFFER_SIZE, stdin)) {
buffer[strcspn(buffer, "\n")] = '\0';
if (strlen(buffer) == 0) {
printf("no input\n");
return 0;
return 1;
}
if (sscanf(buffer, "%f", &new) == 1) { //спроба прочитати число
if (!push_stack(&mystack, new)) return 1;
print_stack(&mystack);
}else if (sscanf(buffer, " %c", &operator) == 1 && strchr("+-*/", operator)) {
if (!pop_stack(&mystack, &b) || !pop_stack(&mystack, &a)) return 1;
if (sscanf(buffer, " %f %c", &chyslo, &symbol) == 1) { //спроба прочитати число але без символів
new = chyslo;
if (!push_stack(&mystack, new)) return 0;
print_stack(&mystack);
} else if (sscanf(buffer, " %c %c", &operator, &symbol) == 1 && strchr("+-*/", operator)) {
if (!pop_stack(&mystack, &b) || !pop_stack(&mystack, &a)) return 0;
switch (operator) {
case '+': result=a+b; break;
case '-': result=a-b; break;
@ -77,10 +80,11 @@ int main() {
}
print_stack(&mystack);
} else {
printf("bad input\n");
return 0;
printf("invalid input\n");
return 1;
}
}
printf("no input\n");
return 0;
}