This commit is contained in:
Oleksandr Vyshniakov 2025-10-12 21:57:19 +02:00
parent 780529c399
commit e7a9c71f3d
2 changed files with 42 additions and 7 deletions

Binary file not shown.

View File

@ -57,12 +57,47 @@ void destroy_stack (struct stack* s) {
}
int main() {
struct stack* novystack = create_stack(STACK_SIZE);
push_stack(novystack, 3.14);
push_stack(novystack, 2.71);
float x = pop_stack(novystack);
printf("Popped: %2.f\n", x);
print_stack(novystack);
destroy_stack(novystack);
struct stack* s = create_stack(STACK_SIZE);
char input[101];
while (scanf("%s", input)==1) {
char *end;
float value = strtof(input, &end);
if (*end == '\0') {
push_stack(s, value);
print_stack(s);
} else if (strcmp(input, "+")==0 || strcmp(input, "-")==0 || strcmp(input, "*")==0 || strcmp(input, "/")==0) {
if (s->size < 2) {
printf("no input\n");
destroy_stack(s);
return 0;
}
float b = pop_stack(s);
float a = pop_stack(s);
float result = 0;
if (strcmp(input, "+")==0) {
result = a+b;
}
if (strcmp(input, "-")==0) {
result = a - b;
}
if (strcmp(input, "*")==0) {
result = a * b;
}
if (strcmp(input, "/")==0) {
result = a/b;
}
push_stack(s);
print_stack(s);
}
else {
printf("no input");
destroy_stack(s);
return 0;
}
}
destroy_stack(s);
return 0;
}