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

This commit is contained in:
Yevhen Kozirovskyi 2024-10-17 17:02:11 +00:00
parent 7806888c40
commit 863f360310

View File

@ -47,11 +47,11 @@ int main() {
while (fgets(bufer, sizeof(bufer), stdin) != NULL) { while (fgets(bufer, sizeof(bufer), stdin) != NULL) {
char* konec; char* konec;
double chislo = strtod(bufer, &konec); double chislo = strtod(bufer, &konec);
char c = konec[0];
if (*konec == '\n' && *bufer != '\n' && *bufer != ' ') { if (c == '\n' && *bufer != '\n' && *bufer != ' ') {
push(&stek, chislo); push(&stek, chislo);
} else if (strcmp(konec, "+\n") == 0) { } else if (c == '+') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
@ -63,7 +63,7 @@ int main() {
} }
double a = pop(&stek); double a = pop(&stek);
push(&stek, a + b); push(&stek, a + b);
} else if (strcmp(konec, "-\n") == 0) { } else if (c == '-') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
@ -75,7 +75,7 @@ int main() {
} }
double a = pop(&stek); double a = pop(&stek);
push(&stek, a - b); push(&stek, a - b);
} else if (strcmp(konec, "*\n") == 0) { } else if (c == '*') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
@ -87,7 +87,7 @@ int main() {
} }
double a = pop(&stek); double a = pop(&stek);
push(&stek, a * b); push(&stek, a * b);
} else if (strcmp(konec, "/\n") == 0) { } else if (c == '/') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
@ -97,21 +97,18 @@ int main() {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double a = pop(&stek); double a = pop(&stek);
if (b == 0) { if (b == 0) {
printf("division by zero\n"); printf("division by zero\n");
return 0; return 0;
exit(1);
} }
push(&stek, a / b); push(&stek, a / b);
} else if (isalpha(konec[-1])) { } else if (isalpha(c)) {
printf("bad input\n"); printf("bad input\n");
exit(1);
return 0; return 0;
} else { } else {
printf("bad input\n"); printf("no input\n");
exit(1); return 0;
} }
for (int i = 0; i < stek.vershina; i++) { for (int i = 0; i < stek.vershina; i++) {
printf("%.2lf ", stek.chisla[i]); printf("%.2lf ", stek.chisla[i]);