From 1fbb3ac28656fd9bdbd481d15c815a86b5dfb2a7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:29:51 +0000 Subject: [PATCH] Initializacia --- cv3/program.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 40e58c0..abb8659 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -5,24 +5,23 @@ #define STACK_SIZE 10 -// Stack structure struct stack { float values[STACK_SIZE]; int size; }; -// 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 + printf(" "); + } else { + printf(" "); } } printf("\n"); } -// Function to push a value onto the stack void push_stack(struct stack* s, float value) { if (s->size >= STACK_SIZE) { printf("Chyba: zasobnik je plny\n"); @@ -31,7 +30,6 @@ void push_stack(struct stack* s, float value) { s->values[s->size++] = value; } -// Function to pop a value from the stack float pop_stack(struct stack* s) { if (s->size == 0) { printf("Chyba: zasobnik je prazdny\n"); @@ -40,13 +38,11 @@ float pop_stack(struct stack* s) { return s->values[--s->size]; } -// Check if input is an operator int is_operator(char* input) { return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 || strcmp(input, "*") == 0 || strcmp(input, "/") == 0); } -// Perform the operation on the top of the stack void perform_operation(struct stack* s, char* operator) { if (s->size < 2) { printf("Chyba: nedostatok operandov\n"); @@ -82,26 +78,23 @@ int main() { struct stack mystack; mystack.size = 0; - // 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 + input[strlen(input) - 1] = '\0'; - // Try to parse input as a number float value; 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; } - // Print stack after each valid input print_stack(&mystack); }