From 1a8ad13b4516714c4ceaac077ccc0781344698c8 Mon Sep 17 00:00:00 2001 From: Anton Dolozin Date: Mon, 6 Oct 2025 14:36:18 +0200 Subject: [PATCH] Something strange with server --- du3/program.c | 68 ++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/du3/program.c b/du3/program.c index 279151b..7f441ec 100644 --- a/du3/program.c +++ b/du3/program.c @@ -26,49 +26,51 @@ void print_stack(struct stack* stack){ printf("%.2f ", stack->values[i]); printf("\n"); } - -void process_operations(struct stack* stack){ + + +void process_operations(struct stack* stack) { char line[STACK_SIZE]; int could_read = 0; - - while(fgets(line, sizeof(line), stdin)){ - line[strcspn(line, "\n")] = '\0'; - if(line[0] == '\0'){ + + while (fgets(line, sizeof(line), stdin)) { + line[strcspn(line, "\n")] = '\0'; + if (line[0] == '\0') { printf("no input\n"); return; } - 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[0] == '+') ? a + b : - (line[0] == '-') ? a - b : - (line[0] == '*') ? a * b : - (line[0] == '/') ? a / b : 0; - push_stack(stack, res); - 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{ + could_read= 1; + + if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || + strcmp(line, "*") == 0 || strcmp(line, "/") == 0)) { + if (stack->size >= 2) { + float b = pop_stack(stack); + float a = pop_stack(stack); + 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); + } else { + printf("bad input\n"); + return; + } + } else { char *endptr; float val = strtof(line, &endptr); + if (endptr == line) { + printf("bad input\n"); + return; + } push_stack(stack, val); print_stack(stack); + } + } - - - -} } -if (!could_read){ - printf("no input\n"); -} } + if (!could_read) { + printf("no input\n"); + } +} int main(void){ memset(&mystack,0,sizeof(struct stack)); process_operations(&mystack);