Обновить cv3/program.c

This commit is contained in:
Yevhen Kozirovskyi 2024-10-17 17:52:48 +00:00
parent cbaea22303
commit bb34fa76cd

View File

@ -25,7 +25,7 @@ int isFull(StEk* stek) {
void push(StEk* stek, double chislo) { void push(StEk* stek, double chislo) {
if (isFull(stek)) { if (isFull(stek)) {
printf("no input\n"); printf("not enough operands\n");
exit(1); exit(1);
} }
stek->chisla[stek->vershina] = chislo; stek->chisla[stek->vershina] = chislo;
@ -34,7 +34,7 @@ void push(StEk* stek, double chislo) {
double pop(StEk* stek) { double pop(StEk* stek) {
if (isEmpty(stek)) { if (isEmpty(stek)) {
printf("no input\n"); printf("not enough operands\n");
exit(1); exit(1);
} }
return stek->chisla[--stek->vershina]; return stek->chisla[--stek->vershina];
@ -46,12 +46,15 @@ int main() {
char bufer[256]; char bufer[256];
while (fgets(bufer, sizeof(bufer), stdin) != NULL) { while (fgets(bufer, sizeof(bufer), stdin) != NULL) {
if (bufer[0] == '\n') { if (bufer[0] == '\n') {
printf("no input\n"); if (isEmpty(&stek)) {
printf("no input\n");
} else {
double result = pop(&stek);
printf("%.2lf\n", result);
}
return 0; return 0;
} }
char* konec; char* konec;
double chislo = strtod(bufer, &konec); double chislo = strtod(bufer, &konec);
char c = konec[0]; char c = konec[0];
@ -60,13 +63,13 @@ int main() {
push(&stek, chislo); push(&stek, chislo);
} else if (c == '+' || c == '-' || c == '*' || c == '/') { } else if (c == '+' || c == '-' || c == '*' || c == '/') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("not enough operands\n");
exit(1); return 0;
} }
double b = pop(&stek); double b = pop(&stek);
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("not enough operands\n");
exit(1); return 0;
} }
double a = pop(&stek); double a = pop(&stek);
if (c == '+') { if (c == '+') {
@ -85,7 +88,7 @@ int main() {
} else if (!isdigit(c) || !isalpha(c)) { } else if (!isdigit(c) || !isalpha(c)) {
printf("bad input\n"); printf("bad input\n");
return 0; return 0;
} else{ } else {
printf("no input\n"); printf("no input\n");
return 0; return 0;
} }