I have a function that evaluates a postfix expression. It first converts the numeric digits to float and then evaluates the expression.
Problem is when it multiplies 2 floating types (e.g 3.25 and 5.7) it only gives 18.524996 instead of 18.525. same for division process.
For addition and subtraction, it works fine. The conversion to float also works. I have debugged this one and I dont know what's the source of the problem. Hope someone could help.
Code:
void post(Item_c *str, int q, char t, int u)
{
int l2 = u;
int i;
int j = 0;
float k, z, p, y,x;
double f;
STACKinit_i(l2);
puts(str);
for (i = 0; i < l2; i++)
{
if(isalpha(str[i]))
{
printf("%f",alpha[tolower(str[i]) - 'a']);
system("pause");
STACKpush_i(alpha[tolower(str[i]) - 'a']);
}
if(((str[i] >= 48) && (str[i] <= 57)) || (str[i] == '.'))
{
k = str[i] - 48;
while ( str[++i] != ' ' && i < l2 && str[i] != '.' && isdigit(str[i]) && str[++i] != '\t' )
{
k = k*10 + (str[i] - 48);
printf("%f", k);
system("pause");
}
if (str[i] == '.')
{
p = str[++i] - 48;
j++;
while ( (str[++i] != ' ') && (isdigit(str[i])) )
{
p = p*10 + (str[i] - 48);
j++;
}
f = p / pow(10 , j);
j = 0;
}
STACKpush_i(k + f);
}
if(str[i] == '+')
{
y = STACKpop_i();
x = STACKpop_i();
STACKpush_i(x+y);
}
if(str[i] == '-')
{
y = STACKpop_i();
x = STACKpop_i();
STACKpush_i(x-y);
}
if(str[i] == '*')
{
y = STACKpop_i();
x = STACKpop_i();
STACKpush_i(x*y);
}
if(str[i] == '/')
{
y = STACKpop_i();
x = STACKpop_i();
STACKpush_i(x/y);
}
}
if(q == 1)
{
alpha[tolower(t) - 'a'] = STACKpop_i();
printf("%f\n", alpha[tolower(t) - 'a']);
system("pause");
}
else if (q == 5)
{
printf("\nResulting value = %f\n\n", STACKpop_i());
system("pause");
}
}