Initializacia

This commit is contained in:
Kozar 2024-10-13 09:44:19 +00:00
parent 94189ce4e4
commit 69fdc54776

View File

@ -1,7 +1,7 @@
#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
@ -43,6 +43,12 @@ int is_operator(char* input) {
strcmp(input, "*") == 0 || strcmp(input, "/") == 0); 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) { void perform_operation(struct stack* s, char* operator) {
if (s->size < 2) { if (s->size < 2) {
printf("Chyba: nedostatok operandov\n"); printf("Chyba: nedostatok operandov\n");
@ -81,8 +87,8 @@ int main() {
while (fgets(input, sizeof(input), stdin) != NULL) { while (fgets(input, sizeof(input), stdin) != NULL) {
input[strlen(input) - 1] = '\0'; input[strlen(input) - 1] = '\0';
float value; if (is_valid_float(input)) {
if (sscanf(input, "%f", &value) == 1) { float value = atof(input);
push_stack(&mystack, value); push_stack(&mystack, value);
} else if (is_operator(input)) { } else if (is_operator(input)) {
perform_operation(&mystack, input); perform_operation(&mystack, input);