该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
{
if(suffix[i]>='0' && suffix[i]<='9'||suffix[i]=='.')
{
if (state_int == TRUE)
num=chartonum( suffix[i],i);
else num = suffix[i] - '0';
state_int = TRUE;
}
if(suffix[i]== ' ')
{
if (state_int == TRUE)
{
push_seq(ps, num);
state_int = FALSE;
}
}
if(suffix[i]== '+'||suffix[i]== '-'||suffix[i]== '*'||suffix[i]== '/')
{
if (state_int == TRUE)
{
push_seq(ps, num);
state_int = FALSE;
}
num2 = top_seq(ps);
pop_seq(ps);
num1 = top_seq(ps);
pop_seq(ps);
if (suffix[i] == '+')
push_seq(ps, num1 + num2);
if (suffix[i]== '-')
push_seq(ps, num1 - num2);
if (suffix[i]== '*')
push_seq(ps, num1 * num2);
if (suffix[i]== '/')
push_seq(ps, num1 / num2);
}
}
*presult = top_seq(ps);
pop_seq(ps);
free(ps);
return TRUE;
}
chartonum(char a[],int i)
{
float x=0.0;
int k=0;
while(a[i]>='0'&&a[i]<='9')
{
x=x*10+a[i]-'0';
i++;
}
if(a[i]=='.')
{
i++;
while (a[i]>='0'&&a[i]<='9')
{
x=x*10 + a[i] -'0';
i++;
k++;
}
}
while(k!=0)
{
x=x/10;
k=k-1;
}
return x;
}
void main()
{
char c, infix[MAXNUM], suffix[MAXNUM];
float result;
int i;
int k=0;
printf("Plese input an infix expression!\nInput:");
scanf("%s",&infix);
for(i=0;infix[i]!='\n';i++)
{
k=i++;
}
infix[k]='\0';
infixtoSuffix(infix, suffix);
printf("suffix: %s\n", suffix);
calculateSuffix(suffix, &result) ;
printf("Result: %10.2f\n", result);
getch();
}