From 1fbb3ac28656fd9bdbd481d15c815a86b5dfb2a7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:29:51 +0000 Subject: [PATCH 01/11] 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); } From 0c73d2017a4ffa2ea34811891d6b02cb353bd905 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:31:18 +0000 Subject: [PATCH 02/11] Initializacia --- cv3/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv3/program.c b/cv3/program.c index abb8659..b8093a9 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -61,7 +61,7 @@ void perform_operation(struct stack* s, char* operator) { result = a * b; } else if (strcmp(operator, "/") == 0) { if (b == 0) { - printf("Chyba: delenie nulou\n"); + printf("division by zero\n"); exit(1); } result = a / b; From 50e07c78dfb9eb65c2a50b2617fd67b90f86b0b5 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:32:39 +0000 Subject: [PATCH 03/11] Initializacia --- cv3/program.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index b8093a9..1c63355 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -25,7 +25,7 @@ void print_stack(struct stack* s) { void push_stack(struct stack* s, float value) { if (s->size >= STACK_SIZE) { printf("Chyba: zasobnik je plny\n"); - exit(1); + exit(0); } s->values[s->size++] = value; } @@ -33,7 +33,7 @@ void push_stack(struct stack* s, float value) { float pop_stack(struct stack* s) { if (s->size == 0) { printf("Chyba: zasobnik je prazdny\n"); - exit(1); + exit(0); } return s->values[--s->size]; } @@ -46,7 +46,7 @@ int is_operator(char* input) { void perform_operation(struct stack* s, char* operator) { if (s->size < 2) { printf("Chyba: nedostatok operandov\n"); - exit(1); + exit(0); } float b = pop_stack(s); @@ -62,7 +62,7 @@ void perform_operation(struct stack* s, char* operator) { } else if (strcmp(operator, "/") == 0) { if (b == 0) { printf("division by zero\n"); - exit(1); + exit(0); } result = a / b; } else { From fba426a0ebeece6ab6e80b4f66a038385b265f28 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:33:36 +0000 Subject: [PATCH 04/11] Initializacia --- cv3/{program.c => wprogram.c} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename cv3/{program.c => wprogram.c} (97%) diff --git a/cv3/program.c b/cv3/wprogram.c similarity index 97% rename from cv3/program.c rename to cv3/wprogram.c index 1c63355..82de3a0 100644 --- a/cv3/program.c +++ b/cv3/wprogram.c @@ -91,7 +91,7 @@ int main() { } else { - printf("Chyba: neplatny vstup\n"); + printf("bad input\n"); return 1; } From 824447cc6e76b60f05030b66129eb2acd353a8d3 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:35:46 +0000 Subject: [PATCH 05/11] Update cv3/program.c --- cv3/{wprogram.c => program.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cv3/{wprogram.c => program.c} (100%) diff --git a/cv3/wprogram.c b/cv3/program.c similarity index 100% rename from cv3/wprogram.c rename to cv3/program.c From d832de78627e09605cdd54ba1007405946f47e6a Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:36:47 +0000 Subject: [PATCH 06/11] Initializacia --- cv3/program.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 82de3a0..00adfe3 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -67,7 +67,7 @@ void perform_operation(struct stack* s, char* operator) { result = a / b; } else { printf("Chyba: neplatna operacia\n"); - exit(1); + exit(0); } push_stack(s, result); @@ -92,7 +92,7 @@ int main() { else { printf("bad input\n"); - return 1; + return 0; } print_stack(&mystack); From 952c832f80241a2eb0258b01fb0d6e0427f88178 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:39:28 +0000 Subject: [PATCH 07/11] Initializacia --- cv3/program.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 00adfe3..008266a 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -14,9 +14,9 @@ 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(" "); + printf(" "); } else { - printf(" "); + printf(" "); } } printf("\n"); @@ -25,7 +25,7 @@ void print_stack(struct stack* s) { void push_stack(struct stack* s, float value) { if (s->size >= STACK_SIZE) { printf("Chyba: zasobnik je plny\n"); - exit(0); + exit(1); } s->values[s->size++] = value; } @@ -33,7 +33,7 @@ void push_stack(struct stack* s, float value) { float pop_stack(struct stack* s) { if (s->size == 0) { printf("Chyba: zasobnik je prazdny\n"); - exit(0); + exit(1); } return s->values[--s->size]; } @@ -46,7 +46,7 @@ int is_operator(char* input) { void perform_operation(struct stack* s, char* operator) { if (s->size < 2) { printf("Chyba: nedostatok operandov\n"); - exit(0); + exit(1); } float b = pop_stack(s); @@ -62,12 +62,12 @@ void perform_operation(struct stack* s, char* operator) { } else if (strcmp(operator, "/") == 0) { if (b == 0) { printf("division by zero\n"); - exit(0); + exit(1); } result = a / b; } else { printf("Chyba: neplatna operacia\n"); - exit(0); + exit(1); } push_stack(s, result); @@ -84,15 +84,11 @@ int main() { float value; if (sscanf(input, "%f", &value) == 1) { push_stack(&mystack, value); - } - - else if (is_operator(input)) { + } else if (is_operator(input)) { perform_operation(&mystack, input); - } - - else { + } else { printf("bad input\n"); - return 0; + return 1; } print_stack(&mystack); From 94189ce4e451dd3023137d7660be0217919a04b0 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:42:19 +0000 Subject: [PATCH 08/11] Initializacia --- cv3/program.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 008266a..44580b1 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -25,7 +25,7 @@ void print_stack(struct stack* s) { void push_stack(struct stack* s, float value) { if (s->size >= STACK_SIZE) { printf("Chyba: zasobnik je plny\n"); - exit(1); + exit(0); } s->values[s->size++] = value; } @@ -33,7 +33,7 @@ void push_stack(struct stack* s, float value) { float pop_stack(struct stack* s) { if (s->size == 0) { printf("Chyba: zasobnik je prazdny\n"); - exit(1); + exit(0); } return s->values[--s->size]; } @@ -46,7 +46,7 @@ int is_operator(char* input) { void perform_operation(struct stack* s, char* operator) { if (s->size < 2) { printf("Chyba: nedostatok operandov\n"); - exit(1); + exit(0); } float b = pop_stack(s); @@ -62,12 +62,12 @@ void perform_operation(struct stack* s, char* operator) { } else if (strcmp(operator, "/") == 0) { if (b == 0) { printf("division by zero\n"); - exit(1); + exit(0); } result = a / b; } else { printf("Chyba: neplatna operacia\n"); - exit(1); + exit(0); } push_stack(s, result); @@ -88,7 +88,7 @@ int main() { perform_operation(&mystack, input); } else { printf("bad input\n"); - return 1; + return 0; } print_stack(&mystack); From 69fdc54776a4bf087999eb6aea1e0e9e97585e01 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:44:19 +0000 Subject: [PATCH 09/11] Initializacia --- cv3/program.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 44580b1..1bce719 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #define STACK_SIZE 10 @@ -43,6 +43,12 @@ int is_operator(char* input) { strcmp(input, "*") == 0 || strcmp(input, "/") == 0); } +int is_valid_float(char* input) { + char* endptr; + strtod(input, &endptr); + return *endptr == '\0'; +} + void perform_operation(struct stack* s, char* operator) { if (s->size < 2) { printf("Chyba: nedostatok operandov\n"); @@ -81,8 +87,8 @@ int main() { while (fgets(input, sizeof(input), stdin) != NULL) { input[strlen(input) - 1] = '\0'; - float value; - if (sscanf(input, "%f", &value) == 1) { + if (is_valid_float(input)) { + float value = atof(input); push_stack(&mystack, value); } else if (is_operator(input)) { perform_operation(&mystack, input); From c8d22b9589f99f92073c85837ff9bc36b0b61ae6 Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:45:37 +0000 Subject: [PATCH 10/11] Initializacia --- cv3/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv3/program.c b/cv3/program.c index 1bce719..f536259 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -51,7 +51,7 @@ int is_valid_float(char* input) { void perform_operation(struct stack* s, char* operator) { if (s->size < 2) { - printf("Chyba: nedostatok operandov\n"); + printf("not enough operands\n"); exit(0); } From b35831b69129c85770d107d17adfb5fc9e95f24e Mon Sep 17 00:00:00 2001 From: Kozar Date: Sun, 13 Oct 2024 09:46:30 +0000 Subject: [PATCH 11/11] Initializacia --- cv3/program.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index f536259..88a101b 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -24,7 +24,7 @@ void print_stack(struct stack* s) { void push_stack(struct stack* s, float value) { if (s->size >= STACK_SIZE) { - printf("Chyba: zasobnik je plny\n"); + printf("full stack\n"); exit(0); } s->values[s->size++] = value; @@ -32,7 +32,7 @@ void push_stack(struct stack* s, float value) { float pop_stack(struct stack* s) { if (s->size == 0) { - printf("Chyba: zasobnik je prazdny\n"); + printf("empty stack\n"); exit(0); } return s->values[--s->size];