2024-10-13 09:13:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2024-10-08 13:28:33 +00:00
|
|
|
|
2024-10-13 09:13:29 +00:00
|
|
|
#define STACK_SIZE 10
|
2024-10-08 13:28:33 +00:00
|
|
|
|
2024-10-13 09:13:29 +00:00
|
|
|
struct stack {
|
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
2024-10-08 13:48:01 +00:00
|
|
|
};
|
2024-10-08 13:28:33 +00:00
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
void print_stack(struct stack* s) {
|
|
|
|
for (int i = 0; i < s->size; i++) {
|
|
|
|
printf("%.2f", s->values[i]);
|
|
|
|
if (i < s->size - 1) {
|
2024-10-13 09:29:51 +00:00
|
|
|
printf(" ");
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
2024-10-13 09:25:57 +00:00
|
|
|
}
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
2024-10-08 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
void push_stack(struct stack* s, float value) {
|
|
|
|
if (s->size >= STACK_SIZE) {
|
2024-10-13 09:13:29 +00:00
|
|
|
printf("Chyba: zasobnik je plny\n");
|
2024-10-13 09:32:39 +00:00
|
|
|
exit(0);
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
2024-10-13 09:25:57 +00:00
|
|
|
s->values[s->size++] = value;
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
float pop_stack(struct stack* s) {
|
|
|
|
if (s->size == 0) {
|
2024-10-13 09:13:29 +00:00
|
|
|
printf("Chyba: zasobnik je prazdny\n");
|
2024-10-13 09:32:39 +00:00
|
|
|
exit(0);
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
2024-10-13 09:25:57 +00:00
|
|
|
return s->values[--s->size];
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int is_operator(char* input) {
|
|
|
|
return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
|
|
|
|
strcmp(input, "*") == 0 || strcmp(input, "/") == 0);
|
|
|
|
}
|
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
void perform_operation(struct stack* s, char* operator) {
|
|
|
|
if (s->size < 2) {
|
2024-10-13 09:13:29 +00:00
|
|
|
printf("Chyba: nedostatok operandov\n");
|
2024-10-13 09:32:39 +00:00
|
|
|
exit(0);
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
float b = pop_stack(s);
|
|
|
|
float a = pop_stack(s);
|
2024-10-13 09:13:29 +00:00
|
|
|
float result;
|
|
|
|
|
|
|
|
if (strcmp(operator, "+") == 0) {
|
|
|
|
result = a + b;
|
|
|
|
} else if (strcmp(operator, "-") == 0) {
|
|
|
|
result = a - b;
|
|
|
|
} else if (strcmp(operator, "*") == 0) {
|
|
|
|
result = a * b;
|
|
|
|
} else if (strcmp(operator, "/") == 0) {
|
|
|
|
if (b == 0) {
|
2024-10-13 09:31:18 +00:00
|
|
|
printf("division by zero\n");
|
2024-10-13 09:32:39 +00:00
|
|
|
exit(0);
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
|
|
|
result = a / b;
|
|
|
|
} else {
|
|
|
|
printf("Chyba: neplatna operacia\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
push_stack(s, result);
|
2024-10-13 09:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-10-13 09:25:57 +00:00
|
|
|
char input[100];
|
2024-10-13 09:13:29 +00:00
|
|
|
struct stack mystack;
|
2024-10-13 09:25:57 +00:00
|
|
|
mystack.size = 0;
|
2024-10-13 09:13:29 +00:00
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
while (fgets(input, sizeof(input), stdin) != NULL) {
|
2024-10-13 09:29:51 +00:00
|
|
|
input[strlen(input) - 1] = '\0';
|
2024-10-13 09:13:29 +00:00
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
float value;
|
2024-10-13 09:13:29 +00:00
|
|
|
if (sscanf(input, "%f", &value) == 1) {
|
|
|
|
push_stack(&mystack, value);
|
|
|
|
}
|
2024-10-13 09:29:51 +00:00
|
|
|
|
2024-10-13 09:13:29 +00:00
|
|
|
else if (is_operator(input)) {
|
|
|
|
perform_operation(&mystack, input);
|
|
|
|
}
|
2024-10-13 09:29:51 +00:00
|
|
|
|
2024-10-13 09:13:29 +00:00
|
|
|
else {
|
2024-10-13 09:33:36 +00:00
|
|
|
printf("bad input\n");
|
2024-10-13 09:13:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_stack(&mystack);
|
|
|
|
}
|
|
|
|
|
2024-10-13 09:25:57 +00:00
|
|
|
printf("no input\n");
|
2024-10-13 09:13:29 +00:00
|
|
|
return 0;
|
2024-10-08 13:28:33 +00:00
|
|
|
}
|