2024-10-07 12:38:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#define STACK_SIZE 10
|
|
|
|
|
|
|
|
struct stack {
|
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
void push_stack(struct stack* stack, float value);
|
|
|
|
float pop_stack(struct stack* stack);
|
|
|
|
void print_stack(struct stack* stack);
|
|
|
|
int is_number(const char* str);
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct stack mystack;
|
2024-10-17 11:15:27 +00:00
|
|
|
memset(&mystack, 0, sizeof(struct stack));
|
2024-10-07 12:38:16 +00:00
|
|
|
|
|
|
|
char input[50];
|
|
|
|
while (1) {
|
|
|
|
printf("Enter a number or an operator (+, -, *, /): ");
|
|
|
|
if (!fgets(input, sizeof(input), stdin)) {
|
2024-10-17 11:15:27 +00:00
|
|
|
break;
|
2024-10-07 12:38:16 +00:00
|
|
|
}
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:43:15 +00:00
|
|
|
if (strlen(input) == 0) {
|
|
|
|
printf("no input\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
if (is_number(input)) {
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
float value = atof(input);
|
|
|
|
push_stack(&mystack, value);
|
|
|
|
} else if (strlen(input) == 1 && strchr("+-*/", input[0])) {
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:43:15 +00:00
|
|
|
if (mystack.size < 2) {
|
|
|
|
fprintf(stderr, "Error: Not enough values in stack.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-10-07 12:38:16 +00:00
|
|
|
float b = pop_stack(&mystack);
|
|
|
|
float a = pop_stack(&mystack);
|
|
|
|
float result;
|
|
|
|
|
|
|
|
switch (input[0]) {
|
|
|
|
case '+':
|
|
|
|
result = a + b;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
result = a - b;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
result = a * b;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (b == 0) {
|
|
|
|
fprintf(stderr, "Error: Division by zero.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
result = a / b;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Error: Invalid operator.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
push_stack(&mystack, result);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Error: Invalid input.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
print_stack(&mystack);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
void push_stack(struct stack* stack, float value) {
|
2024-10-17 11:15:27 +00:00
|
|
|
assert(stack->size < STACK_SIZE);
|
2024-10-07 12:38:16 +00:00
|
|
|
stack->values[stack->size] = value;
|
|
|
|
stack->size += 1;
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
float pop_stack(struct stack* stack) {
|
2024-10-17 11:15:27 +00:00
|
|
|
assert(stack->size > 0);
|
2024-10-07 12:38:16 +00:00
|
|
|
stack->size -= 1;
|
|
|
|
return stack->values[stack->size];
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
|
2024-10-07 12:38:16 +00:00
|
|
|
void print_stack(struct stack* stack) {
|
|
|
|
for (int i = 0; i < stack->size; i++) {
|
|
|
|
printf("%.2f ", stack->values[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:15:27 +00:00
|
|
|
r
|
2024-10-07 12:38:16 +00:00
|
|
|
int is_number(const char* str) {
|
|
|
|
char* endptr;
|
|
|
|
strtod(str, &endptr);
|
|
|
|
return *endptr == '\0' && endptr != str;
|
|
|
|
}
|