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) {
|
2024-10-13 15:05:13 +00:00
|
|
|
if (s->size > 0) {
|
|
|
|
for (int i = 0; i < s->size; i++) {
|
|
|
|
printf("%.2f ", s->values[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-14 10:58:47 +00:00
|
|
|
printf("no input\n");
|
|
|
|
return 0; // Error reading input or end of 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) {
|
2024-10-14 10:58:47 +00:00
|
|
|
return 1; // Continue to next iteration
|
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-14 10:58:47 +00:00
|
|
|
printf("no input\n"); // Print "no input" upon end
|
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-14 10:58:47 +00:00
|
|
|
print_stack(s);
|
2024-10-13 14:13:46 +00:00
|
|
|
} else {
|
|
|
|
switch (temp[0]) {
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case '*':
|
|
|
|
case '/': {
|
|
|
|
if (s->size < 2) {
|
2024-10-13 14:30:56 +00:00
|
|
|
printf("no input\n");
|
2024-10-14 10:58:47 +00:00
|
|
|
return 1; // Continue to next iteration
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
2024-10-13 14:30:56 +00:00
|
|
|
|
2024-10-14 10:58:47 +00:00
|
|
|
float b = pop_stack(s);
|
2024-10-13 14:13:46 +00:00
|
|
|
float a = pop_stack(s);
|
|
|
|
float result;
|
2024-10-14 10:58:47 +00:00
|
|
|
|
2024-10-13 14:13:46 +00:00
|
|
|
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 15:09:40 +00:00
|
|
|
printf("division by zero\n");
|
2024-10-14 10:58:47 +00:00
|
|
|
return 1;
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
result = a / b;
|
|
|
|
break;
|
|
|
|
}
|
2024-10-14 10:58:47 +00:00
|
|
|
push_stack(s, result);
|
|
|
|
print_stack(s);
|
|
|
|
return 1; // Continue without printing "no input"
|
2024-10-13 14:13:46 +00:00
|
|
|
}
|
|
|
|
default:
|
2024-10-13 15:14:38 +00:00
|
|
|
printf("bad input\n");
|
2024-10-14 10:58:47 +00:00
|
|
|
return 1; // Continue 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);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (read(&my_stack) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|