From db97ea1240e1583089eb790b9606b6a73b885f47 Mon Sep 17 00:00:00 2001 From: Yevhen Kozirovskyi Date: Thu, 17 Oct 2024 18:03:51 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20cv3/program.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv3/program.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 592eaa8..864555c 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -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++) {