usaa24/cv3/program.c

106 lines
2.2 KiB
C
Raw Normal View History

2024-10-13 09:13:29 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-10-13 09:44:19 +00:00
#include <ctype.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:39:28 +00:00
printf(" ");
2024-10-13 09:29:51 +00:00
} else {
2024-10-13 09:39:28 +00:00
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:46:30 +00:00
printf("full stack\n");
2024-10-13 09:42:19 +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:46:30 +00:00
printf("empty stack\n");
2024-10-13 09:42:19 +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:44:19 +00:00
int is_valid_float(char* input) {
char* endptr;
strtod(input, &endptr);
return *endptr == '\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:45:37 +00:00
printf("not enough operands\n");
2024-10-13 09:42:19 +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:42:19 +00:00
exit(0);
2024-10-13 09:13:29 +00:00
}
result = a / b;
} else {
printf("Chyba: neplatna operacia\n");
2024-10-13 09:42:19 +00:00
exit(0);
2024-10-13 09:13:29 +00:00
}
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:44:19 +00:00
if (is_valid_float(input)) {
float value = atof(input);
2024-10-13 09:13:29 +00:00
push_stack(&mystack, value);
2024-10-13 09:39:28 +00:00
} else if (is_operator(input)) {
2024-10-13 09:13:29 +00:00
perform_operation(&mystack, input);
2024-10-13 09:39:28 +00:00
} else {
2024-10-13 09:33:36 +00:00
printf("bad input\n");
2024-10-13 09:42:19 +00:00
return 0;
2024-10-13 09:13:29 +00:00
}
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
}