#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("full stack\n"); exit(0); // 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; char extra; int scan = sscanf(temp, "%f %c", &value, &extra); // If it's a properly formatted number if (scan == 1) { push_stack(s, value); print_stack(s); } else if (scan > 1) { // If extra characters were found, it's an invalid input like .23.4 printf("bad input\n"); exit(0); } else { switch (temp[0]) { case '+': case '-': case '*': case '/': { if (s->size < 2) { printf("not enough operands\n"); exit(0); // 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"); exit(0); } result = a / b; 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 the loop } int main() { struct stack my_stack; init(&my_stack); while (1) { if (read(&my_stack) == 0) { break; } } return 0; }