From 3788c9731fe972debd9da72fad7b5163a6dc0828 Mon Sep 17 00:00:00 2001 From: Bohdan Kapliuk Date: Sat, 12 Oct 2024 15:42:30 +0300 Subject: [PATCH] cv3 --- cv3/program.c | 157 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 100 insertions(+), 57 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 11bfaf8..268874d 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,71 +1,114 @@ #include -#include #include -#include +#include +#include -#define STACK_SIZE 100 +#define STACK_SIZE 10 +#define INPUT_SIZE 100 -struct stack{ +// Štruktúra zásobníka +struct stack { float values[STACK_SIZE]; int size; }; -int main(){ +// Funkcia na vloženie hodnoty do zásobníka (push) +void push_stack(struct stack* s, float value) { + if (s->size >= STACK_SIZE) { + printf("Stack overflow error\n"); + exit(1); + } + s->values[s->size] = value; + s->size += 1; +} + +// Funkcia na výber hodnoty zo zásobníka (pop) +float pop_stack(struct stack* s) { + if (s->size <= 0) { + printf("Stack underflow error\n"); + exit(1); + } + s->size -= 1; + return s->values[s->size]; +} + +// Funkcia na výpis zásobníka +void print_stack(struct stack* s) { + for (int i = 0; i < s->size; i++) { + printf("%.2f ", s->values[i]); + } + printf("\n"); +} + +// Funkcia na spracovanie operácií (+, -, *, /) +void perform_operation(struct stack* s, char op) { + if (s->size < 2) { + printf("Not enough values in the stack for operation\n"); + exit(1); + } + + float b = pop_stack(s); + float a = pop_stack(s); + float result; + + switch (op) { + case '+': + result = a + b; + break; + case '-': + result = a - b; + break; + case '*': + result = a * b; + break; + case '/': + if (b == 0) { + printf("Division by zero error\n"); + exit(1); + } + result = a / b; + break; + default: + printf("Unknown operator\n"); + exit(1); + } + + push_stack(s, result); +} + +int main() { struct stack mystack; - memset(&mystack,0,sizeof(struct stack)); - char vstup[STACK_SIZE]; - int counter = 0; - while(1){ - fgets(vstup, STACK_SIZE, stdin); - vstup[strcspn(vstup, "\n")] = 0; - if(strlen(vstup) == 0){ - printf("no input"); + mystack.size = 0; + char input[INPUT_SIZE]; + + while (1) { + // Načítanie vstupu pomocou fgets + if (fgets(input, sizeof(input), stdin) == NULL) { + printf("No input\n"); return 0; } - if(vstup[0] == '-' || vstup[0] == '+' || vstup[0] == '*' || vstup[0] == '/'){ - if(vstup[0] == '-'){ - mystack.values[0] = mystack.values[0] - mystack.values[1]; - for(int i = 1; mystack.values[i] != 0;i++){ - mystack.values[i] = mystack.values[i+1]; - mystack.values[i+1] = 0; - counter--; - } - } - if(vstup[0] == '/'){ - mystack.values[0] = mystack.values[0] / mystack.values[1]; - for(int i = 1; mystack.values[i] != 0;i++){ - mystack.values[i] = mystack.values[i+1]; - mystack.values[i+1] = 0; - counter--; - } - } - if(vstup[0] == '*'){ - mystack.values[0] = mystack.values[0] * mystack.values[1]; - for(int i = 1; mystack.values[i] != 0;i++){ - mystack.values[i] = mystack.values[i+1]; - mystack.values[i+1] = 0; - counter--; - } - } - if(vstup[0] == '+'){ - mystack.values[0] = mystack.values[0] + mystack.values[1]; - for(int i = 1; mystack.values[i] != 0;i++){ - mystack.values[i] = mystack.values[i+1]; - mystack.values[i+1] = 0; - counter--; - } - } - } - else{ - float cislo = strtof(vstup, NULL); - mystack.values[counter] = cislo; - counter++; - + + // Odstránenie nového riadku (ak existuje) + input[strcspn(input, "\n")] = 0; + + // Skúšame, či je vstup číslo + char *endptr; + float num = strtof(input, &endptr); + + if (endptr != input) { + // Je to číslo, pridáme ho do zásobníka + push_stack(&mystack, num); + } else if (strlen(input) == 1 && (input[0] == '+' || input[0] == '-' || input[0] == '*' || input[0] == '/')) { + // Je to operácia, vykonáme ju + perform_operation(&mystack, input[0]); + } else { + printf("no input\n"); + return 1; } - mystack.size = counter; - for(int i =0; mystack.values[i] != 0;i++){ - printf("%.2f ", mystack.values[i]); - } - printf("\n"); + + // Výpis zásobníka po každom úspešnom vstupe + print_stack(&mystack); } + + return 0; } \ No newline at end of file