usaa24/cv3/program.c

111 lines
2.6 KiB
C
Raw Normal View History

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:25:57 +00:00
// Stack structure
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
// Function to print the stack
void print_stack(struct stack* s) {
for (int i = 0; i < s->size; i++) {
printf("%.2f", s->values[i]);
if (i < s->size - 1) {
printf(" "); // Print space between values
}
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
// Function to push a value onto the stack
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");
exit(1);
}
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
// Function to pop a value from the stack
float pop_stack(struct stack* s) {
if (s->size == 0) {
2024-10-13 09:13:29 +00:00
printf("Chyba: zasobnik je prazdny\n");
exit(1);
}
2024-10-13 09:25:57 +00:00
return s->values[--s->size];
2024-10-13 09:13:29 +00:00
}
2024-10-13 09:25:57 +00:00
// Check if input is an operator
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
// Perform the operation on the top of the stack
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");
exit(1);
}
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) {
printf("Chyba: delenie nulou\n");
exit(1);
}
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
// Reading input from stdin until EOF or an error occurs
while (fgets(input, sizeof(input), stdin) != NULL) {
input[strlen(input) - 1] = '\0'; // Remove newline character
2024-10-13 09:13:29 +00:00
// Try to parse input as a number
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);
}
// If it's not a number, check if it's a valid operator
else if (is_operator(input)) {
perform_operation(&mystack, input);
}
// Invalid input
else {
printf("Chyba: neplatny vstup\n");
return 1;
}
2024-10-13 09:25:57 +00:00
// Print stack after each valid input
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
}