diff --git a/du3/program.c b/du3/program.c index 5b7315a..279151b 100644 --- a/du3/program.c +++ b/du3/program.c @@ -23,37 +23,52 @@ return value; } void print_stack(struct stack* stack){ for(int i = 0; i < stack->size; i++) - printf("%g ", stack->values[i]); + printf("%.2f ", stack->values[i]); printf("\n"); } void process_operations(struct stack* stack){ char line[STACK_SIZE]; - if(!fgets(line, STACK_SIZE, stdin)){ - printf("Chyba\n"); - return; - } - for(int i = 0; line[i] != '\0'; i++){ - if(line[i] >= '0' && line[i] <= '9'){ - char *endptr; - float val = strtof(&line[i], &endptr); - push_stack(stack, val); - print_stack(stack); - i = endptr - line - 1; + int could_read = 0; + + while(fgets(line, sizeof(line), stdin)){ + line[strcspn(line, "\n")] = '\0'; + if(line[0] == '\0'){ + printf("no input\n"); + return; } - else if ((line[i] == '*' || line[i] == '+' || line[i] == '-' || line[i] == '/') + could_read = 1; + + if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || strcmp(line, "*") == 0 || strcmp(line, "/") == 0) && stack->size >= 2){ float b = pop_stack(stack); float a = pop_stack(stack); - float res = (line[i] == '+') ? a + b : - (line[i] == '-') ? a - b : - (line[i] == '*') ? a * b : - (line[i] == '/') ? a / b : 0; + float res = (line[0] == '+') ? a + b : + (line[0] == '-') ? a - b : + (line[0] == '*') ? a * b : + (line[0] == '/') ? a / b : 0; push_stack(stack, res); - print_stack(stack); + print_stack(stack); } - } -} + else if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || strcmp(line, "*") == 0 || strcmp(line, "/") == 0) + && stack->size <2){ + printf("no input\n"); + return; + } + + else{ + char *endptr; + float val = strtof(line, &endptr); + push_stack(stack, val); + print_stack(stack); + + + + +} } +if (!could_read){ + printf("no input\n"); +} } int main(void){ memset(&mystack,0,sizeof(struct stack)); process_operations(&mystack);