2024-10-07 12:08:33 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#define STACK_SIZE 10
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
double stack[STACK_SIZE];
|
|
|
|
|
int top;
|
|
|
|
|
} Calculator;
|
|
|
|
|
|
2024-10-07 12:16:34 +00:00
|
|
|
|
|
2024-10-07 12:08:33 +00:00
|
|
|
|
void init(Calculator *calc) {
|
|
|
|
|
calc->top = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-07 12:16:34 +00:00
|
|
|
|
|
2024-10-07 12:08:33 +00:00
|
|
|
|
int push(Calculator *calc, double value) {
|
|
|
|
|
if (calc->top >= STACK_SIZE - 1) {
|
|
|
|
|
printf("Chyba: zasobnik je plny\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
calc->stack[++(calc->top)] = value;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-07 12:16:34 +00:00
|
|
|
|
|
2024-10-07 12:08:33 +00:00
|
|
|
|
int pop(Calculator *calc, double *value) {
|
|
|
|
|
if (calc->top < 0) {
|
|
|
|
|
printf("Chyba: zasobnik je prazdny\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
*value = calc->stack[(calc->top)--];
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-07 12:16:34 +00:00
|
|
|
|
|
2024-10-07 12:08:33 +00:00
|
|
|
|
void print_stack(Calculator *calc) {
|
|
|
|
|
for (int i = 0; i <= calc->top; i++) {
|
2024-10-07 12:16:34 +00:00
|
|
|
|
printf("%.2f ", calc->stack[i]);
|
2024-10-07 12:08:33 +00:00
|
|
|
|
}
|
2024-10-07 12:22:27 +00:00
|
|
|
|
printf("\n");
|
2024-10-07 12:08:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Основна функція для роботи з операціями
|
|
|
|
|
int perform_operation(Calculator *calc, char op) {
|
|
|
|
|
double a, b;
|
|
|
|
|
if (!pop(calc, &b) || !pop(calc, &a)) {
|
|
|
|
|
return 0; // Не вдалося вилучити два числа
|
|
|
|
|
}
|
2024-10-07 12:37:02 +00:00
|
|
|
|
int bub = 0;
|
2024-10-07 12:08:33 +00:00
|
|
|
|
switch (op) {
|
|
|
|
|
case '+':
|
|
|
|
|
push(calc, a + b);
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
push(calc, a - b);
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
push(calc, a * b);
|
|
|
|
|
break;
|
|
|
|
|
case '/':
|
|
|
|
|
if (b == 0) {
|
2024-10-07 12:29:48 +00:00
|
|
|
|
printf("division by zero\n");
|
2024-10-07 12:37:02 +00:00
|
|
|
|
bub = 1;
|
2024-10-07 12:31:16 +00:00
|
|
|
|
break;
|
2024-10-07 12:08:33 +00:00
|
|
|
|
}
|
2024-10-07 12:33:45 +00:00
|
|
|
|
|
2024-10-07 12:08:33 +00:00
|
|
|
|
push(calc, a / b);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("Chyba: neplatna operacia\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_stack(calc);
|
2024-10-07 12:37:02 +00:00
|
|
|
|
if(bub == 0) {
|
|
|
|
|
printf("no input\n");
|
|
|
|
|
}
|
2024-10-07 12:08:33 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Головна функція програми
|
|
|
|
|
int main() {
|
|
|
|
|
Calculator calc;
|
|
|
|
|
init(&calc);
|
|
|
|
|
|
|
|
|
|
char input[50];
|
|
|
|
|
|
|
|
|
|
while (fgets(input, sizeof(input), stdin)) {
|
|
|
|
|
// Спроба обробити як число
|
|
|
|
|
char *end;
|
|
|
|
|
double value = strtod(input, &end);
|
|
|
|
|
|
|
|
|
|
if (end != input && *end == '\n') {
|
|
|
|
|
// Це дійсне число
|
|
|
|
|
if (!push(&calc, value)) {
|
|
|
|
|
return 1; // Стек переповнений
|
|
|
|
|
}
|
|
|
|
|
print_stack(&calc);
|
|
|
|
|
} else if (strlen(input) == 2 && strchr("+-*/", input[0])) {
|
|
|
|
|
// Це операція
|
|
|
|
|
if (!perform_operation(&calc, input[0])) {
|
|
|
|
|
return 1; // Помилка операції
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf("Chyba: neplatny vstup\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|