#include #include #include #define STACK_SIZE 10 struct stack { float values[STACK_SIZE]; int size; }; void init(struct stack *s) { s->size = 0; } void push_stack(struct stack *s, float value) { if (s->size < STACK_SIZE) { s->values[s->size] = value; s->size++; } else { printf("no input.\n"); exit(1); // Exit the program on overflow } } float pop_stack(struct stack *s) { if (s->size > 0) { s->size--; return s->values[s->size]; } else { printf("no input\n"); exit(1); // Exit the program on underflow } } void print_stack(struct stack *s) { if (s->size > 0) { for (int i = 0; i < s->size; i++) { printf("%.2f ", s->values[i]); } printf("\n"); } } int read(struct stack *s) { char temp[100]; // Read a line of input if (fgets(temp, sizeof(temp), stdin) == NULL) { printf("no input\n"); return 0; // Error reading input or end of input } // Check for empty input if (strcmp(temp, "\n") == 0) { 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; } float value; int scan = sscanf(temp, "%f", &value); // If it's a number if (scan == 1) { push_stack(s, value); print_stack(s); } else { switch (temp[0]) { case '+': case '-': case '*': case '/': { if (s->size < 2) { printf("no input\n"); return 1; // Continue to next iteration } 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; case '*': result = a * b; break; case '/': if (b == 0) { printf("division by zero\n"); return 1; } result = a / b; break; } push_stack(s, result); print_stack(s); return 1; // Continue without printing "no input" } default: printf("bad input\n"); return 1; // Continue on invalid input } } return 1; // Continue the loop } int main() { struct stack my_stack; init(&my_stack); while (1) { if (read(&my_stack) == 0) { break; } } return 0; }