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");
|
|
|
|
|
if(i == calc->top){
|
|
|
|
|
printf("no input\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; // Не вдалося вилучити два числа
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
|
case '+':
|
|
|
|
|
push(calc, a + b);
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
push(calc, a - b);
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
push(calc, a * b);
|
|
|
|
|
break;
|
|
|
|
|
case '/':
|
|
|
|
|
if (b == 0) {
|
|
|
|
|
printf("Chyba: deleni nulou\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
push(calc, a / b);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("Chyba: neplatna operacia\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_stack(calc);
|
|
|
|
|
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;
|
|
|
|
|
}
|