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