Обновить 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; int vershina;
} StEk; } StEk;
int end_program = 0;
void inicStEk(StEk* stek) { void inicStEk(StEk* stek) {
stek->vershina = 0; stek->vershina = 0;
} }
@ -26,6 +28,7 @@ int isFull(StEk* stek) {
void push(StEk* stek, double chislo) { void push(StEk* stek, double chislo) {
if (isFull(stek)) { if (isFull(stek)) {
printf("full stack\n"); printf("full stack\n");
end_program = 1;
return; return;
} }
stek->chisla[stek->vershina] = chislo; stek->chisla[stek->vershina] = chislo;
@ -35,6 +38,7 @@ void push(StEk* stek, double chislo) {
double pop(StEk* stek) { double pop(StEk* stek) {
if (isEmpty(stek)) { if (isEmpty(stek)) {
printf("not enough operands\n"); printf("not enough operands\n");
end_program = 1;
return 0; return 0;
} }
return stek->chisla[--stek->vershina]; return stek->chisla[--stek->vershina];
@ -44,7 +48,8 @@ int main() {
StEk stek; StEk stek;
inicStEk(&stek); inicStEk(&stek);
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') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
@ -61,15 +66,19 @@ int main() {
if (c == '\n' && *bufer != '\n' && *bufer != ' ') { if (c == '\n' && *bufer != '\n' && *bufer != ' ') {
push(&stek, chislo); push(&stek, chislo);
if(end_program == 1) {
return 0;
}
} else if (c == '+' || c == '-' || c == '*' || c == '/') { } else if (c == '+' || c == '-' || c == '*' || c == '/') {
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("not enough operands\n"); printf("not enough operands\n");
end_program = 1;
return 0; return 0;
} }
double b = pop(&stek); double b = pop(&stek);
if (isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("not enough operands\n"); printf("not enough operands\n");
end_program = 1;
return 0; return 0;
} }
double a = pop(&stek); double a = pop(&stek);
@ -82,15 +91,18 @@ int main() {
} else if (c == '/') { } else if (c == '/') {
if (b == 0) { if (b == 0) {
printf("division by zero\n"); printf("division by zero\n");
end_program = 1;
return 0; return 0;
} }
push(&stek, a / b); push(&stek, a / b);
} }
} else if (!isdigit(c) || !isalpha(c)) { } else if (!isdigit(c) || !isalpha(c)) {
printf("bad input\n"); printf("bad input\n");
end_program = 1;
return 0; return 0;
} else { } else {
printf("no input\n"); printf("no input\n");
end_program = 1;
return 0; return 0;
} }
for (int i = 0; i < stek.vershina; i++) { for (int i = 0; i < stek.vershina; i++) {