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

@ -29,63 +29,70 @@ int main(){
memset(&myStack,0,sizeof(struct stack)); memset(&myStack,0,sizeof(struct stack));
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);
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);
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 c = b/a;
push_stack(&myStack, c);
print_stack(&myStack);
printf("\n");
} }
float b = pop_stack(&myStack); } else {
float c = b/a; float_value = strtof(char_value, NULL);
push_stack(&myStack, c); if(float_value == 0){
print_stack(&myStack); puts("bad input");
printf("\n"); break;
} } else {
push_stack(&myStack,float_value);
}else {
push_stack(&myStack, float_value);
print_stack(&myStack); print_stack(&myStack);
printf("\n"); printf("\n");
continue;
}
}
}else {
//push_stack(&myStack, float_value);
//print_stack(&myStack);
//printf("\n");
puts("no input");
break;
} }
}else{
puts("no input");
break;
} }
}
return 0; return 0;