From ea98c9f32b989bc3ac987ed751468afed29b6ad2 Mon Sep 17 00:00:00 2001 From: Ivan Leichenko Date: Mon, 14 Oct 2024 14:48:27 +0200 Subject: [PATCH] updt cv3 --- cv3/program.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index f8060ef..3bad3d7 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -27,7 +27,7 @@ float getnpop(struct stack* stack) } -float calculate(struct stack* stack, char operator) +int calculate(struct stack* stack, char operator) { float num1 = getnpop(stack); float num2 = getnpop(stack); @@ -45,11 +45,25 @@ float calculate(struct stack* stack, char operator) res = num1*num2; break; case '/': + if(num1 == 0) + { + //res = 1; + return 1; + } res = num2/num1; break; } add(stack, res); - return res; + return 0; +} + +int check_op(char symb) +{ + if(symb == '+' || symb == '-' || symb == '*' || symb == '/') + { + return 1; + } + return 0; } int main(int argc, char const *argv[]) @@ -69,14 +83,18 @@ int main(int argc, char const *argv[]) } buf[strcspn(buf, "\n")] = '\0'; - num = atof(buf); - if(num != 0) + if(!check_op(buf[0])) { + num = atof(buf); add(&calc_stack, num); } else { - calculate(&calc_stack, buf[0]); + if(calculate(&calc_stack, buf[0]) == 1) + { + printf("division by zero\n"); + return 0; + } } for (int i = 0; i < calc_stack.size; i++) {