From deea72d872032d10dacc4939f6023ca828ef930a Mon Sep 17 00:00:00 2001 From: vj586da Date: Thu, 21 Oct 2021 22:42:33 +0200 Subject: [PATCH] division by zero --- cv3/program.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cv3/program.c b/cv3/program.c index de4c827..8ec3532 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -29,6 +30,12 @@ int main(){ while(1){ ptr = fgets(char_value, STACK_SIZE-1, stdin); if(ptr != NULL ){ + if(char_value[0] == '0'){ + push_stack(&myStack,0); + print_stack(&myStack); + printf("\n"); + continue; + } float_value = strtof(char_value, NULL); if(float_value == 0){ if(char_value[0] == '+'){ @@ -57,6 +64,10 @@ int main(){ } if(char_value[0] == '/'){ float a = pop_stack(&myStack); + if(a == 0){ + printf("division by zero\n"); + break; + } float b = pop_stack(&myStack); float c = b/a; push_stack(&myStack, c); @@ -65,6 +76,7 @@ int main(){ } }else { + push_stack(&myStack, float_value); print_stack(&myStack); printf("\n");