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; } +