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);