bad input

This commit is contained in:
Valér Jakubčo 2021-10-21 23:20:42 +02:00
parent deea72d872
commit 1cb9718f53

View File

@ -30,14 +30,17 @@ 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] == '\n'){
puts("no input");
break;
}
if(char_value[0] == '0'){ if(char_value[0] == '0'){
push_stack(&myStack,0); push_stack(&myStack,0);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
continue; 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 a = pop_stack(&myStack);
float b = pop_stack(&myStack); float b = pop_stack(&myStack);
@ -45,43 +48,47 @@ int main(){
push_stack(&myStack, c); push_stack(&myStack, c);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
} }else if(char_value[0] == '-'){
if(char_value[0] == '-'){
float a = pop_stack(&myStack); float a = pop_stack(&myStack);
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);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
} }else if(char_value[0] == '*'){
if(char_value[0] == '*'){
float a = pop_stack(&myStack); float a = pop_stack(&myStack);
float b = pop_stack(&myStack); float b = pop_stack(&myStack);
float c = a*b; float c = a*b;
push_stack(&myStack, c); push_stack(&myStack, c);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
} } else if(char_value[0] == '/'){
if(char_value[0] == '/'){
float a = pop_stack(&myStack); float a = pop_stack(&myStack);
if(a == 0){ if(a == 0){
printf("division by zero\n"); printf("division by zero\n");
break; }else{
}
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);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
} }
} else { } else {
float_value = strtof(char_value, NULL);
if(float_value == 0){
puts("bad input");
break;
} else {
push_stack(&myStack,float_value); push_stack(&myStack,float_value);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
continue;
}
} }
}else { }else {
//push_stack(&myStack, float_value);
//print_stack(&myStack);
//printf("\n");
puts("no input"); puts("no input");
break; break;
} }