This commit is contained in:
Ivan Leichenko 2024-10-14 14:48:27 +02:00
parent 83f11ad4df
commit ea98c9f32b

View File

@ -27,7 +27,7 @@ float getnpop(struct stack* stack)
} }
float calculate(struct stack* stack, char operator) int calculate(struct stack* stack, char operator)
{ {
float num1 = getnpop(stack); float num1 = getnpop(stack);
float num2 = getnpop(stack); float num2 = getnpop(stack);
@ -45,11 +45,25 @@ float calculate(struct stack* stack, char operator)
res = num1*num2; res = num1*num2;
break; break;
case '/': case '/':
if(num1 == 0)
{
//res = 1;
return 1;
}
res = num2/num1; res = num2/num1;
break; break;
} }
add(stack, res); add(stack, res);
return res; return 0;
}
int check_op(char symb)
{
if(symb == '+' || symb == '-' || symb == '*' || symb == '/')
{
return 1;
}
return 0;
} }
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
@ -69,14 +83,18 @@ int main(int argc, char const *argv[])
} }
buf[strcspn(buf, "\n")] = '\0'; buf[strcspn(buf, "\n")] = '\0';
num = atof(buf); if(!check_op(buf[0]))
if(num != 0)
{ {
num = atof(buf);
add(&calc_stack, num); add(&calc_stack, num);
} }
else else
{ {
calculate(&calc_stack, buf[0]); if(calculate(&calc_stack, buf[0]) == 1)
{
printf("division by zero\n");
return 0;
}
} }
for (int i = 0; i < calc_stack.size; i++) for (int i = 0; i < calc_stack.size; i++)
{ {