Aktualizovat du3/program.c
This commit is contained in:
parent
369d5b5f86
commit
bc1eb2d886
@ -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;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user