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

This commit is contained in:
Yevhen Kozirovskyi 2024-10-17 18:03:51 +00:00
parent 3acee80c88
commit db97ea1240

View File

@ -11,6 +11,8 @@ typedef struct {
int vershina;
} StEk;
int end_program = 0;
void inicStEk(StEk* stek) {
stek->vershina = 0;
}
@ -26,6 +28,7 @@ int isFull(StEk* stek) {
void push(StEk* stek, double chislo) {
if (isFull(stek)) {
printf("full stack\n");
end_program = 1;
return;
}
stek->chisla[stek->vershina] = chislo;
@ -35,6 +38,7 @@ void push(StEk* stek, double chislo) {
double pop(StEk* stek) {
if (isEmpty(stek)) {
printf("not enough operands\n");
end_program = 1;
return 0;
}
return stek->chisla[--stek->vershina];
@ -44,7 +48,8 @@ int main() {
StEk stek;
inicStEk(&stek);
char bufer[256];
while (fgets(bufer, sizeof(bufer), stdin) != NULL) {
while (fgets(bufer, sizeof(bufer), stdin) != NULL ) {
if (bufer[0] == '\n') {
if (isEmpty(&stek)) {
printf("no input\n");
@ -61,15 +66,19 @@ int main() {
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);
@ -82,15 +91,18 @@ int main() {
} 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++) {