From a16b56dd33978b20570c4b2c0069a3ac5c6c77bb Mon Sep 17 00:00:00 2001 From: Yurii Chechur Date: Mon, 14 Oct 2024 10:58:47 +0000 Subject: [PATCH] Update cv3/program.c --- cv3/program.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 2e7d96e..fc3b8f1 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -47,17 +47,18 @@ int read(struct stack *s) { // Read a line of input if (fgets(temp, sizeof(temp), stdin) == NULL) { - return 0; // Error reading input + printf("no input\n"); + return 0; // Error reading input or end of input } // Check for empty input if (strcmp(temp, "\n") == 0) { - printf("no input\n"); - exit(1); // Exit on empty input + return 1; // Continue to next iteration } // Check for termination input if (strcmp(temp, "end\n") == 0) { + printf("no input\n"); // Print "no input" upon end return 0; } @@ -67,24 +68,22 @@ int read(struct stack *s) { // If it's a number if (scan == 1) { push_stack(s, value); - print_stack(s); // Print the stack after adding + print_stack(s); } else { - // If it's an operator switch (temp[0]) { case '+': case '-': case '*': case '/': { - // Check for sufficient values in the stack if (s->size < 2) { printf("no input\n"); - return 1; // Don't exit; continue to next iteration + return 1; // Continue to next iteration } - float b = pop_stack(s); // Pop the last two values + float b = pop_stack(s); float a = pop_stack(s); - float result; + switch (temp[0]) { case '+': result = a + b; break; case '-': result = a - b; break; @@ -92,20 +91,18 @@ int read(struct stack *s) { case '/': if (b == 0) { printf("division by zero\n"); - exit(0); // Exit on division by zero + return 1; } result = a / b; break; } - push_stack(s, result); // Push the result back onto the stack - print_stack(s); // Print the stack after operation - //printf("no input\n"); // Print the "no input" message - // exit(0); // Exit the program after the operation - break; + push_stack(s, result); + print_stack(s); + return 1; // Continue without printing "no input" } default: printf("bad input\n"); - exit(0); // Exit on invalid input + return 1; // Continue on invalid input } }