division by zero

This commit is contained in:
Valér Jakubčo 2021-10-21 22:42:33 +02:00
parent d3c17b1c94
commit deea72d872

View File

@ -1,3 +1,4 @@
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -29,6 +30,12 @@ int main(){
while(1){ while(1){
ptr = fgets(char_value, STACK_SIZE-1, stdin); ptr = fgets(char_value, STACK_SIZE-1, stdin);
if(ptr != NULL ){ if(ptr != NULL ){
if(char_value[0] == '0'){
push_stack(&myStack,0);
print_stack(&myStack);
printf("\n");
continue;
}
float_value = strtof(char_value, NULL); float_value = strtof(char_value, NULL);
if(float_value == 0){ if(float_value == 0){
if(char_value[0] == '+'){ if(char_value[0] == '+'){
@ -57,6 +64,10 @@ int main(){
} }
if(char_value[0] == '/'){ if(char_value[0] == '/'){
float a = pop_stack(&myStack); float a = pop_stack(&myStack);
if(a == 0){
printf("division by zero\n");
break;
}
float b = pop_stack(&myStack); float b = pop_stack(&myStack);
float c = b/a; float c = b/a;
push_stack(&myStack, c); push_stack(&myStack, c);
@ -65,6 +76,7 @@ int main(){
} }
}else { }else {
push_stack(&myStack, float_value); push_stack(&myStack, float_value);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");