2024-10-17 16:54:08 +00:00
|
|
|
#include <ctype.h>
|
2024-10-12 20:09:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-10-17 16:43:40 +00:00
|
|
|
#include <math.h>
|
2024-10-12 20:09:28 +00:00
|
|
|
|
2024-10-17 16:36:28 +00:00
|
|
|
#define MAX_RAZMER 10
|
2024-10-16 22:44:59 +00:00
|
|
|
|
2024-10-12 20:09:28 +00:00
|
|
|
typedef struct {
|
|
|
|
double chisla[MAX_RAZMER];
|
|
|
|
int vershina;
|
|
|
|
} StEk;
|
|
|
|
|
|
|
|
void inicStEk(StEk* stek) {
|
|
|
|
stek->vershina = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int isEmpty(StEk* stek) {
|
|
|
|
return stek->vershina == 0;
|
|
|
|
}
|
2024-10-16 22:44:59 +00:00
|
|
|
|
2024-10-17 16:36:28 +00:00
|
|
|
int isFull(StEk* stek) {
|
2024-10-12 20:09:28 +00:00
|
|
|
return stek->vershina == MAX_RAZMER;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(StEk* stek, double chislo) {
|
2024-10-17 16:36:28 +00:00
|
|
|
if (isFull(stek)) {
|
2024-10-17 17:54:03 +00:00
|
|
|
printf("full stack\n");
|
2024-10-12 20:09:28 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
stek->chisla[stek->vershina] = chislo;
|
|
|
|
stek->vershina++;
|
|
|
|
}
|
|
|
|
|
|
|
|
double pop(StEk* stek) {
|
2024-10-17 16:36:28 +00:00
|
|
|
if (isEmpty(stek)) {
|
2024-10-17 17:52:48 +00:00
|
|
|
printf("not enough operands\n");
|
2024-10-12 20:09:28 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return stek->chisla[--stek->vershina];
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
StEk stek;
|
|
|
|
inicStEk(&stek);
|
|
|
|
char bufer[256];
|
2024-10-17 16:41:23 +00:00
|
|
|
while (fgets(bufer, sizeof(bufer), stdin) != NULL) {
|
2024-10-17 17:51:27 +00:00
|
|
|
if (bufer[0] == '\n') {
|
2024-10-17 17:52:48 +00:00
|
|
|
if (isEmpty(&stek)) {
|
|
|
|
printf("no input\n");
|
|
|
|
} else {
|
|
|
|
double result = pop(&stek);
|
|
|
|
printf("%.2lf\n", result);
|
|
|
|
}
|
2024-10-17 17:51:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-12 20:09:28 +00:00
|
|
|
char* konec;
|
|
|
|
double chislo = strtod(bufer, &konec);
|
2024-10-17 17:02:11 +00:00
|
|
|
char c = konec[0];
|
2024-10-17 16:57:43 +00:00
|
|
|
|
2024-10-17 17:02:11 +00:00
|
|
|
if (c == '\n' && *bufer != '\n' && *bufer != ' ') {
|
2024-10-12 20:09:28 +00:00
|
|
|
push(&stek, chislo);
|
2024-10-17 17:07:03 +00:00
|
|
|
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
|
2024-10-17 16:36:28 +00:00
|
|
|
if (isEmpty(&stek)) {
|
2024-10-17 17:52:48 +00:00
|
|
|
printf("not enough operands\n");
|
|
|
|
return 0;
|
2024-10-12 20:09:28 +00:00
|
|
|
}
|
|
|
|
double b = pop(&stek);
|
2024-10-17 16:36:28 +00:00
|
|
|
if (isEmpty(&stek)) {
|
2024-10-17 17:52:48 +00:00
|
|
|
printf("not enough operands\n");
|
|
|
|
return 0;
|
2024-10-12 20:09:28 +00:00
|
|
|
}
|
|
|
|
double a = pop(&stek);
|
2024-10-17 17:07:03 +00:00
|
|
|
if (c == '+') {
|
|
|
|
push(&stek, a + b);
|
|
|
|
} else if (c == '-') {
|
|
|
|
push(&stek, a - b);
|
|
|
|
} else if (c == '*') {
|
|
|
|
push(&stek, a * b);
|
|
|
|
} else if (c == '/') {
|
|
|
|
if (b == 0) {
|
|
|
|
printf("division by zero\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
push(&stek, a / b);
|
2024-10-17 16:43:40 +00:00
|
|
|
}
|
2024-10-17 17:51:27 +00:00
|
|
|
} else if (!isdigit(c) || !isalpha(c)) {
|
2024-10-17 16:54:08 +00:00
|
|
|
printf("bad input\n");
|
2024-10-17 16:57:43 +00:00
|
|
|
return 0;
|
2024-10-17 17:52:48 +00:00
|
|
|
} else {
|
2024-10-17 17:02:11 +00:00
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
2024-10-12 20:09:28 +00:00
|
|
|
}
|
2024-10-17 16:36:28 +00:00
|
|
|
for (int i = 0; i < stek.vershina; i++) {
|
2024-10-12 20:09:28 +00:00
|
|
|
printf("%.2lf ", stek.chisla[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2024-10-17 16:41:23 +00:00
|
|
|
printf("no input\n");
|
2024-10-12 20:09:28 +00:00
|
|
|
return 0;
|
2024-10-17 16:57:43 +00:00
|
|
|
}
|