From ec7edc0f3c1045e24058ffd4d47f678c66c4ce38 Mon Sep 17 00:00:00 2001 From: vj586da Date: Fri, 22 Oct 2021 00:30:38 +0200 Subject: [PATCH] not enough operands --- cv3/program.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cv3/program.c b/cv3/program.c index e677b5a..22dc251 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -17,7 +17,6 @@ void push_stack(struct stack* stack, float value); float pop_stack(struct stack* stack); - int main(){ @@ -43,27 +42,34 @@ int main(){ }*/ if(char_value[0] == '+'){ + if(myStack.size > 1){ float a = pop_stack(&myStack); float b = pop_stack(&myStack); float c = a+b; push_stack(&myStack, c); print_stack(&myStack); printf("\n"); + }else{puts("not enough operands"); break;} }else if(char_value[0] == '-'){ + if(myStack.size > 1){ float a = pop_stack(&myStack); float b = pop_stack(&myStack); float c = b-a; push_stack(&myStack, c); print_stack(&myStack); printf("\n"); + }else{puts("not enough operands"); break;} }else if(char_value[0] == '*'){ + if(myStack.size > 1){ float a = pop_stack(&myStack); float b = pop_stack(&myStack); float c = a*b; push_stack(&myStack, c); print_stack(&myStack); printf("\n"); + }else{puts("not enough operands"); break;} } else if(char_value[0] == '/'){ + if(myStack.size > 1){ float a = pop_stack(&myStack); if(a == 0){ printf("division by zero\n"); @@ -75,6 +81,7 @@ int main(){ print_stack(&myStack); printf("\n"); } + }else{puts("not enough operands"); break;} } else if(isdigit(char_value[0]) != 0 ){ float_value = strtof(char_value, NULL); //printf("float value: %f\n",float_value); @@ -122,3 +129,4 @@ float pop_stack(struct stack* stack){ stack->size -= 1; return value; } +