2024-10-16 07:53:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#define STACK_SIZE 10
|
|
|
|
|
|
|
|
float stack[STACK_SIZE];
|
|
|
|
int stack_top = -1;
|
|
|
|
|
|
|
|
bool is_number(char *string) {
|
|
|
|
char *end;
|
|
|
|
strtof(string, &end);
|
2024-10-16 08:28:05 +00:00
|
|
|
return end != string && (*end == '\n' || *end == '\0');
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
2024-10-16 08:01:39 +00:00
|
|
|
|
|
|
|
bool is_operation(char *string) {
|
|
|
|
return strlen(string) == 2 && (string[0] == '+' || string[0] == '-' || string[0] == '*' || string[0] == '/');
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float calculator(float n1, float n2, char operation) {
|
|
|
|
if (operation == '+') {
|
|
|
|
return n1 + n2;
|
|
|
|
}
|
|
|
|
else if (operation == '-') {
|
|
|
|
return n1 - n2;
|
|
|
|
}
|
|
|
|
else if (operation == '*') {
|
|
|
|
return n1 * n2;
|
|
|
|
}
|
|
|
|
else if (operation == '/') {
|
|
|
|
if (n2 != 0) {
|
|
|
|
return n1 / n2;
|
|
|
|
} else {
|
2024-10-16 09:59:18 +00:00
|
|
|
printf("division by zero\n");
|
2024-10-16 10:22:57 +00:00
|
|
|
return n1;
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-16 08:01:39 +00:00
|
|
|
|
2024-10-16 07:53:15 +00:00
|
|
|
void push(float number) {
|
|
|
|
if (stack_top < STACK_SIZE - 1) {
|
|
|
|
stack[++stack_top] = number;
|
|
|
|
} else {
|
2024-10-16 08:01:39 +00:00
|
|
|
printf("Error: stack overflow\n");
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-16 08:01:39 +00:00
|
|
|
|
2024-10-16 07:53:15 +00:00
|
|
|
float pop() {
|
|
|
|
if (stack_top >= 0) {
|
|
|
|
return stack[stack_top--];
|
|
|
|
} else {
|
2024-10-16 08:01:39 +00:00
|
|
|
printf("Error: stack is empty\n");
|
2024-10-16 08:07:34 +00:00
|
|
|
return 0;
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 08:30:32 +00:00
|
|
|
void print_stack() {
|
|
|
|
for (int i = 0; i <= stack_top; i++) {
|
2024-10-16 10:08:39 +00:00
|
|
|
printf("%.2f", stack[i]);
|
|
|
|
if (i < stack_top) {
|
2024-10-16 09:56:53 +00:00
|
|
|
printf(" ");
|
2024-10-16 08:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-16 10:22:57 +00:00
|
|
|
printf("\n");
|
2024-10-16 08:30:32 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 07:53:15 +00:00
|
|
|
int main() {
|
|
|
|
char arr[50];
|
|
|
|
char *pend;
|
|
|
|
stack_top = -1;
|
|
|
|
|
|
|
|
while (fgets(arr, 50, stdin)) {
|
|
|
|
if (arr[0] == '\n' || arr[0] == EOF) {
|
|
|
|
break;
|
|
|
|
}
|
2024-10-16 08:01:39 +00:00
|
|
|
|
2024-10-16 07:53:15 +00:00
|
|
|
if (is_number(arr)) {
|
|
|
|
float number = strtof(arr, &pend);
|
|
|
|
push(number);
|
2024-10-16 09:54:04 +00:00
|
|
|
print_stack();
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
2024-10-16 08:01:39 +00:00
|
|
|
else if (is_operation(arr)) {
|
2024-10-16 07:53:15 +00:00
|
|
|
if (stack_top < 1) {
|
2024-10-16 08:18:57 +00:00
|
|
|
printf("no input\n");
|
2024-10-16 08:22:46 +00:00
|
|
|
return 0;
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
float n2 = pop();
|
|
|
|
float n1 = pop();
|
|
|
|
char operation = arr[0];
|
|
|
|
float result = calculator(n1, n2, operation);
|
2024-10-16 10:03:17 +00:00
|
|
|
if (!(operation == '/' && n2 == 0)) {
|
|
|
|
push(result);
|
|
|
|
}
|
2024-10-16 10:22:57 +00:00
|
|
|
print_stack();
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 08:18:57 +00:00
|
|
|
if (stack_top == 0) {
|
2024-10-16 10:22:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (stack_top == -1) {
|
2024-10-16 08:30:32 +00:00
|
|
|
printf("no input\n");
|
2024-10-16 07:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|