From 1cb9718f538baf0047553d9703bf4f343e61216a Mon Sep 17 00:00:00 2001 From: vj586da Date: Thu, 21 Oct 2021 23:20:42 +0200 Subject: [PATCH] bad input --- cv3/program.c | 59 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 8ec3532..33fb3ec 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -29,63 +29,70 @@ int main(){ memset(&myStack,0,sizeof(struct stack)); while(1){ ptr = fgets(char_value, STACK_SIZE-1, stdin); - if(ptr != NULL ){ + if(ptr != NULL){ + if(char_value[0] == '\n'){ + puts("no input"); + break; + } 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] == '+'){ + + if(char_value[0] == '+'){ float a = pop_stack(&myStack); float b = pop_stack(&myStack); float c = a+b; push_stack(&myStack, c); print_stack(&myStack); printf("\n"); - } - if(char_value[0] == '-'){ + }else if(char_value[0] == '-'){ float a = pop_stack(&myStack); float b = pop_stack(&myStack); float c = b-a; push_stack(&myStack, c); print_stack(&myStack); printf("\n"); - } - if(char_value[0] == '*'){ + }else if(char_value[0] == '*'){ float a = pop_stack(&myStack); float b = pop_stack(&myStack); float c = a*b; push_stack(&myStack, c); print_stack(&myStack); printf("\n"); - } - if(char_value[0] == '/'){ + } else if(char_value[0] == '/'){ float a = pop_stack(&myStack); if(a == 0){ printf("division by zero\n"); - break; + }else{ + float b = pop_stack(&myStack); + float c = b/a; + push_stack(&myStack, c); + print_stack(&myStack); + printf("\n"); } - float b = pop_stack(&myStack); - float c = b/a; - push_stack(&myStack, c); - print_stack(&myStack); - printf("\n"); - } - - }else { - - push_stack(&myStack, float_value); + } else { + float_value = strtof(char_value, NULL); + if(float_value == 0){ + puts("bad input"); + break; + } else { + push_stack(&myStack,float_value); print_stack(&myStack); printf("\n"); + continue; + } } - }else{ - puts("no input"); - break; - } - } + }else { + //push_stack(&myStack, float_value); + //print_stack(&myStack); + //printf("\n"); + puts("no input"); + break; + } + } return 0;