2024-10-07 12:38:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define STACK_SIZE 10
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
struct stack {
|
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
void init_stack(struct stack* s) {
|
|
|
|
memset(s, 0, sizeof(struct stack));
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
void push_stack(struct stack* s, float value) {
|
2024-10-17 12:02:05 +00:00
|
|
|
assert(s->size < STACK_SIZE);
|
2024-10-17 11:38:16 +00:00
|
|
|
s->values[s->size] = value;
|
|
|
|
s->size += 1;
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
float pop_stack(struct stack* s) {
|
2024-10-17 12:02:05 +00:00
|
|
|
assert(s->size > 0);
|
2024-10-17 11:38:16 +00:00
|
|
|
s->size -= 1;
|
|
|
|
return s->values[s->size];
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
void print_stack(struct stack* s) {
|
|
|
|
for (int i = 0; i < s->size; i++) {
|
2024-10-17 12:02:05 +00:00
|
|
|
printf("%.2f ", s->values[i]);
|
2024-10-17 11:38:16 +00:00
|
|
|
}
|
2024-10-17 12:02:05 +00:00
|
|
|
printf("\n");
|
2024-10-17 11:38:16 +00:00
|
|
|
}
|
2024-10-07 12:38:16 +00:00
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:58:16 +00:00
|
|
|
void process_operation(struct stack* s, const char* operation) {
|
|
|
|
float b = pop_stack(s);
|
|
|
|
float a = pop_stack(s);
|
|
|
|
if (strcmp(operation, "+") == 0) {
|
|
|
|
push_stack(s, a + b);
|
|
|
|
} else if (strcmp(operation, "-") == 0) {
|
|
|
|
push_stack(s, a - b);
|
|
|
|
} else if (strcmp(operation, "*") == 0) {
|
|
|
|
push_stack(s, a * b);
|
|
|
|
} else if (strcmp(operation, "/") == 0) {
|
|
|
|
if (b == 0) {
|
|
|
|
printf("Chyba: Delenie nulou.\n");
|
2024-10-17 12:02:05 +00:00
|
|
|
exit(1);
|
2024-10-17 11:58:16 +00:00
|
|
|
}
|
|
|
|
push_stack(s, a / b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
int main() {
|
|
|
|
struct stack mystack;
|
2024-10-17 12:02:05 +00:00
|
|
|
init_stack(&mystack);
|
2024-10-07 12:38:16 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
char input[256];
|
2024-10-17 12:02:05 +00:00
|
|
|
int has_input = 0;
|
2024-10-17 11:44:53 +00:00
|
|
|
|
2024-10-17 11:58:16 +00:00
|
|
|
printf("Poľská kalkulačka\n");
|
2024-10-07 12:38:16 +00:00
|
|
|
while (1) {
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:53:56 +00:00
|
|
|
printf("Zadajte číslo alebo operáciu (+, -, *, /): ");
|
2024-10-07 12:38:16 +00:00
|
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
2024-10-17 12:02:05 +00:00
|
|
|
break;
|
2024-10-07 12:38:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
input[strcspn(input, "\n")] = 0;
|
2024-10-17 11:20:43 +00:00
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:38:16 +00:00
|
|
|
char* endptr;
|
|
|
|
float value = strtof(input, &endptr);
|
2024-10-17 12:02:05 +00:00
|
|
|
if (endptr != input) {
|
2024-10-17 11:38:16 +00:00
|
|
|
push_stack(&mystack, value);
|
2024-10-17 12:02:05 +00:00
|
|
|
has_input = 1;
|
2024-10-17 11:38:16 +00:00
|
|
|
print_stack(&mystack);
|
2024-10-17 12:02:05 +00:00
|
|
|
continue;
|
2024-10-07 12:43:15 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:58:16 +00:00
|
|
|
if (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
|
|
|
|
strcmp(input, "*") == 0 || strcmp(input, "/") == 0) {
|
2024-10-17 11:38:16 +00:00
|
|
|
if (mystack.size < 2) {
|
|
|
|
printf("Chyba: Nedostatok hodnôt v zásobníku.\n");
|
2024-10-17 12:02:05 +00:00
|
|
|
break;
|
2024-10-07 12:38:16 +00:00
|
|
|
}
|
2024-10-17 11:58:16 +00:00
|
|
|
process_operation(&mystack, input);
|
2024-10-17 11:38:16 +00:00
|
|
|
print_stack(&mystack);
|
2024-10-07 12:38:16 +00:00
|
|
|
} else {
|
2024-10-17 11:38:16 +00:00
|
|
|
printf("Chyba: Neplatný vstup.\n");
|
2024-10-17 12:02:05 +00:00
|
|
|
break;
|
2024-10-07 12:38:16 +00:00
|
|
|
}
|
2024-10-17 11:32:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:02:05 +00:00
|
|
|
|
2024-10-17 11:47:16 +00:00
|
|
|
if (has_input) {
|
2024-10-17 11:58:16 +00:00
|
|
|
printf("no input\n");
|
2024-10-17 11:47:16 +00:00
|
|
|
} else {
|
2024-10-17 11:41:41 +00:00
|
|
|
printf("no input\n");
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|