#include #include #include #include #include #define MAX_RAZMER 10 typedef struct { double chisla[MAX_RAZMER]; int vershina; } StEk; int end_program = 0; void inicStEk(StEk* stek) { stek->vershina = 0; } int isEmpty(StEk* stek) { return stek->vershina == 0; } int isFull(StEk* stek) { return stek->vershina == MAX_RAZMER; } void push(StEk* stek, double chislo) { if (isFull(stek)) { printf("full stack\n"); end_program = 1; return; } stek->chisla[stek->vershina] = chislo; stek->vershina++; } double pop(StEk* stek) { if (isEmpty(stek)) { printf("not enough operands\n"); end_program = 1; return 0; } return stek->chisla[--stek->vershina]; } int main() { StEk stek; inicStEk(&stek); char bufer[256]; while (fgets(bufer, sizeof(bufer), stdin) != NULL ) { if (bufer[0] == '\n') { if (isEmpty(&stek)) { printf("no input\n"); } else { double result = pop(&stek); printf("%.2lf\n", result); } return 0; } char* konec; double chislo = strtod(bufer, &konec); char c = konec[0]; if (c == '\n' && *bufer != '\n' && *bufer != ' ') { push(&stek, chislo); if(end_program == 1) { return 0; } } else if (c == '+' || c == '-' || c == '*' || c == '/') { if (isEmpty(&stek)) { printf("not enough operands\n"); end_program = 1; return 0; } double b = pop(&stek); if (isEmpty(&stek)) { printf("not enough operands\n"); end_program = 1; return 0; } double a = pop(&stek); 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"); end_program = 1; return 0; } push(&stek, a / b); } } else if (!isdigit(c) || !isalpha(c)) { printf("bad input\n"); end_program = 1; return 0; } else { printf("no input\n"); end_program = 1; return 0; } for (int i = 0; i < stek.vershina; i++) { printf("%.2lf ", stek.chisla[i]); } printf("\n"); } printf("no input\n"); return 0; }