From 3be4df45de62c004eb892b42f7a7d6de36a63f85 Mon Sep 17 00:00:00 2001 From: Deinerovych Date: Wed, 16 Oct 2024 22:38:56 +0200 Subject: [PATCH] 34 --- cv3/program.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index e608204..ed5f2f2 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -33,7 +33,7 @@ float calculator(float n1, float n2, char operation) { return n1 / n2; } else { printf("division by zero\n"); - return n1; + exit(0); } } return 0; @@ -43,7 +43,8 @@ void push(float number) { if (stack_top < STACK_SIZE - 1) { stack[++stack_top] = number; } else { - printf("stack overflow\n"); + printf("Error: stack overflow\n"); + exit(0); } } @@ -51,21 +52,19 @@ float pop() { if (stack_top >= 0) { return stack[stack_top--]; } else { - printf("stack is empty\n"); - return 0; + printf("Error: stack is empty\n"); + exit(0); } } void print_stack() { - if (stack_top >= 0) { - for (int i = 0; i <= stack_top; i++) { - printf("%.2f", stack[i]); - if (i < stack_top) { - printf(" "); - } + for (int i = 0; i <= stack_top; i++) { + printf("%.2f", stack[i]); + if (i < stack_top) { + printf(" "); } - printf(" \n"); } + printf(" \n"); } int main() { @@ -93,13 +92,10 @@ int main() { float n1 = pop(); char operation = arr[0]; float result = calculator(n1, n2, operation); - if (!(operation == '/' && n2 == 0)) { - push(result); - print_stack(); - } + push(result); + print_stack(); } } - printf("no input\n"); return 0;