This commit is contained in:
Valér Jakubčo 2021-10-22 01:21:30 +02:00
parent 1aff928a0d
commit ca85e52bc2

View File

@ -29,7 +29,7 @@ void substract(struct stack* stack);
void multip(struct stack* stack); void multip(struct stack* stack);
//function to divide last 2 operands from stack //function to divide last 2 operands from stack
void divis(struct stack* stack); void divis(struct stack* stack, float a);
int main(){ int main(){
@ -80,7 +80,13 @@ int main(){
//division //division
else if(char_value[0] == '/'){ else if(char_value[0] == '/'){
if(myStack.size > 1){ if(myStack.size > 1){
divis(&myStack); float a = pop_stack(&myStack);
if(a == 0){
printf("division by zero\n");
break;
}else{
divis(&myStack, a);
}
}else{puts("not enough operands"); break;} }else{puts("not enough operands"); break;}
} }
@ -155,16 +161,10 @@ void multip(struct stack* stack){
void divis(struct stack* stack){ void divis(struct stack* stack, float a){
float a = pop_stack(&myStack); float b = pop_stack(stack);
if(a == 0){
printf("division by zero\n");
break;
}else{
float b = pop_stack(&myStack);
float c = b/a; float c = b/a;
push_stack(&myStack, c); push_stack(stack, c);
print_stack(&myStack); print_stack(stack);
printf("\n"); printf("\n");
} }
}