2024-10-13 14:13:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#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 {
|
2024-10-13 14:40:54 +00:00
|
|
|
printf("no input.\n");
|
2024-10-13 14:46:53 +00:00
|
|
|
exit(1); // Exit the program on overflow
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float pop_stack(struct stack *s) {
|
|
|
|
if (s->size > 0) {
|
|
|
|
s->size--;
|
|
|
|
return s->values[s->size];
|
|
|
|
} else {
|
2024-10-13 14:40:54 +00:00
|
|
|
printf("no input\n");
|
2024-10-13 14:46:53 +00:00
|
|
|
exit(1); // Exit the program on underflow
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_stack(struct stack *s) {
|
|
|
|
for (int i = 0; i < s->size; i++) {
|
|
|
|
printf("%.2f ", s->values[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int read(struct stack *s) {
|
|
|
|
char temp[100];
|
|
|
|
|
2024-10-13 14:46:53 +00:00
|
|
|
// Read a line of input
|
2024-10-13 14:13:46 +00:00
|
|
|
if (fgets(temp, sizeof(temp), stdin) == NULL) {
|
2024-10-13 14:46:53 +00:00
|
|
|
return 0; // Error reading input
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 14:46:53 +00:00
|
|
|
// Check for empty input
|
2024-10-13 14:23:30 +00:00
|
|
|
if (strcmp(temp, "\n") == 0) {
|
|
|
|
printf("no input\n");
|
2024-10-13 14:46:53 +00:00
|
|
|
exit(1); // Exit on empty input
|
2024-10-13 14:23:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 14:46:53 +00:00
|
|
|
// Check for termination input
|
2024-10-13 14:13:46 +00:00
|
|
|
if (strcmp(temp, "end\n") == 0) {
|
2024-10-13 14:30:56 +00:00
|
|
|
return 0;
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float value;
|
|
|
|
int scan = sscanf(temp, "%f", &value);
|
|
|
|
|
2024-10-13 14:46:53 +00:00
|
|
|
// If it's a number
|
2024-10-13 14:13:46 +00:00
|
|
|
if (scan == 1) {
|
|
|
|
push_stack(s, value);
|
2024-10-13 14:46:53 +00:00
|
|
|
print_stack(s); // Print the stack after adding
|
2024-10-13 14:13:46 +00:00
|
|
|
} else {
|
2024-10-13 14:46:53 +00:00
|
|
|
// If it's an operator
|
2024-10-13 14:13:46 +00:00
|
|
|
switch (temp[0]) {
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '*':
|
|
|
|
case '/': {
|
2024-10-13 14:46:53 +00:00
|
|
|
// Check for sufficient values in the stack
|
2024-10-13 14:13:46 +00:00
|
|
|
if (s->size < 2) {
|
2024-10-13 14:30:56 +00:00
|
|
|
printf("no input\n");
|
2024-10-13 14:46:53 +00:00
|
|
|
exit(1); // Exit if not enough values
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
2024-10-13 14:30:56 +00:00
|
|
|
|
2024-10-13 14:46:53 +00:00
|
|
|
float b = pop_stack(s); // Pop the last two values
|
2024-10-13 14:13:46 +00:00
|
|
|
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) {
|
2024-10-13 14:40:54 +00:00
|
|
|
printf("no input\n");
|
2024-10-13 14:46:53 +00:00
|
|
|
exit(1); // Exit on division by zero
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
result = a / b;
|
|
|
|
break;
|
|
|
|
}
|
2024-10-13 14:46:53 +00:00
|
|
|
push_stack(s, result); // Push the result back onto the stack
|
|
|
|
// Print the result
|
|
|
|
print_stack(s); // Print the stack after operation
|
2024-10-13 14:13:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2024-10-13 14:40:54 +00:00
|
|
|
printf("no input\n");
|
2024-10-13 14:46:53 +00:00
|
|
|
exit(1); // Exit on invalid input
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-13 14:46:53 +00:00
|
|
|
return 1; // Continue the loop
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct stack my_stack;
|
|
|
|
init(&my_stack);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (read(&my_stack) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|