Merge branch 'main' of git.kemt.fei.tuke.sk:ak643du/usaa24

This commit is contained in:
Anton 2024-10-14 09:52:18 +02:00
commit 85762b8731

View File

@ -1,56 +1,58 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <ctype.h>
#define STACK_SIZE 10 #define STACK_SIZE 10
// Stack structure
struct stack { struct stack {
float values[STACK_SIZE]; float values[STACK_SIZE];
int size; int size;
}; };
// Function to print the stack
void print_stack(struct stack* s) { void print_stack(struct stack* s) {
for (int i = 0; i < s->size; i++) { for (int i = 0; i < s->size; i++) {
printf("%.2f", s->values[i]); printf("%.2f", s->values[i]);
if (i < s->size - 1) { if (i < s->size - 1) {
printf(" "); // Print space between values printf(" ");
} else {
printf(" ");
} }
} }
printf("\n"); printf("\n");
} }
// Function to push a value onto the stack
void push_stack(struct stack* s, float value) { void push_stack(struct stack* s, float value) {
if (s->size >= STACK_SIZE) { if (s->size >= STACK_SIZE) {
printf("Chyba: zasobnik je plny\n"); printf("full stack\n");
exit(1); exit(0);
} }
s->values[s->size++] = value; s->values[s->size++] = value;
} }
// Function to pop a value from the stack
float pop_stack(struct stack* s) { float pop_stack(struct stack* s) {
if (s->size == 0) { if (s->size == 0) {
printf("Chyba: zasobnik je prazdny\n"); printf("empty stack\n");
exit(1); exit(0);
} }
return s->values[--s->size]; return s->values[--s->size];
} }
// Check if input is an operator
int is_operator(char* input) { int is_operator(char* input) {
return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 || return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
strcmp(input, "*") == 0 || strcmp(input, "/") == 0); strcmp(input, "*") == 0 || strcmp(input, "/") == 0);
} }
// Perform the operation on the top of the stack int is_valid_float(char* input) {
char* endptr;
strtod(input, &endptr);
return *endptr == '\0';
}
void perform_operation(struct stack* s, char* operator) { void perform_operation(struct stack* s, char* operator) {
if (s->size < 2) { if (s->size < 2) {
printf("Chyba: nedostatok operandov\n"); printf("not enough operands\n");
exit(1); exit(0);
} }
float b = pop_stack(s); float b = pop_stack(s);
@ -65,13 +67,13 @@ void perform_operation(struct stack* s, char* operator) {
result = a * b; result = a * b;
} else if (strcmp(operator, "/") == 0) { } else if (strcmp(operator, "/") == 0) {
if (b == 0) { if (b == 0) {
printf("Chyba: delenie nulou\n"); printf("division by zero\n");
exit(1); exit(0);
} }
result = a / b; result = a / b;
} else { } else {
printf("Chyba: neplatna operacia\n"); printf("Chyba: neplatna operacia\n");
exit(1); exit(0);
} }
push_stack(s, result); push_stack(s, result);
@ -82,26 +84,19 @@ int main() {
struct stack mystack; struct stack mystack;
mystack.size = 0; mystack.size = 0;
// Reading input from stdin until EOF or an error occurs
while (fgets(input, sizeof(input), stdin) != NULL) { while (fgets(input, sizeof(input), stdin) != NULL) {
input[strlen(input) - 1] = '\0'; // Remove newline character input[strlen(input) - 1] = '\0';
// Try to parse input as a number if (is_valid_float(input)) {
float value; float value = atof(input);
if (sscanf(input, "%f", &value) == 1) {
push_stack(&mystack, value); push_stack(&mystack, value);
} } else if (is_operator(input)) {
// If it's not a number, check if it's a valid operator
else if (is_operator(input)) {
perform_operation(&mystack, input); perform_operation(&mystack, input);
} } else {
// Invalid input printf("bad input\n");
else { return 0;
printf("Chyba: neplatny vstup\n");
return 1;
} }
// Print stack after each valid input
print_stack(&mystack); print_stack(&mystack);
} }