121 lines
2.9 KiB
C
121 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
#define MAX_RAZMER 10
|
|
|
|
typedef struct {
|
|
double chisla[MAX_RAZMER];
|
|
int vershina;
|
|
} StEk;
|
|
|
|
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("no input\n");
|
|
exit(1);
|
|
}
|
|
stek->chisla[stek->vershina] = chislo;
|
|
stek->vershina++;
|
|
}
|
|
|
|
double pop(StEk* stek) {
|
|
if (isEmpty(stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
return stek->chisla[--stek->vershina];
|
|
}
|
|
|
|
int main() {
|
|
StEk stek;
|
|
inicStEk(&stek);
|
|
char bufer[256];
|
|
while (fgets(bufer, sizeof(bufer), stdin) != NULL) {
|
|
char* konec;
|
|
double chislo = strtod(bufer, &konec);
|
|
if (*konec == '\n' && *bufer != '\n' && *bufer != ' ') {
|
|
if (konec - bufer == 1) {
|
|
printf("bad input\n");
|
|
exit(1);
|
|
}
|
|
push(&stek, chislo);
|
|
} else if (strcmp(konec, "+\n") == 0) {
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double b = pop(&stek);
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double a = pop(&stek);
|
|
push(&stek, a + b);
|
|
} else if (strcmp(konec, "-\n") == 0) {
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double b = pop(&stek);
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double a = pop(&stek);
|
|
push(&stek, a - b);
|
|
} else if (strcmp(konec, "*\n") == 0) {
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double b = pop(&stek);
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double a = pop(&stek);
|
|
push(&stek, a * b);
|
|
} else if (strcmp(konec, "/\n") == 0) {
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
double b = pop(&stek);
|
|
if (isEmpty(&stek)) {
|
|
printf("no input\n");
|
|
exit(1);
|
|
}
|
|
|
|
double a = pop(&stek);
|
|
if (b == 0) {
|
|
printf("division by zero\n");
|
|
return 0;
|
|
exit(1);
|
|
}
|
|
push(&stek, a / b);
|
|
} else {
|
|
printf("bad input\n");
|
|
exit(1);
|
|
}
|
|
for (int i = 0; i < stek.vershina; i++) {
|
|
printf("%.2lf ", stek.chisla[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("no input\n");
|
|
return 0;
|
|
}
|