这段程序问题出在那里了呢?
中序表达式转换成后序,
非常老的题目,
不过这段为什么不行呢,
至少12-(-4)这样的简单表达式都无法转换。
希望大家能指点一下
void Transform(char str[],char exp[])
{
SqStack S;
InitStack(S);
char ch;
int i=0;
int t=0;
while((ch=str[i++])!='#')
{
if((ch>='0'&&ch<='9')/*||ch=='.'*/)
{
exp[t]=ch;t++;
while(((ch=str[i++])!='#'&&ch>='0'&&ch<='9')/*||((ch=str[i++])=='.')*/)
{
exp[t]=ch;t++;
}
i--;
exp[t]='@';t++;
}
else if(ch=='(')
Push(S,ch);
else if(ch==')')
{
while(S.elem[S.top]!='(')
{
Pop(S,exp[t]);t++;
}
S.top--;//略过'('
}
else if(ch=='+'||ch=='-')
{
while((/*S.elem[S.top]>=0*/S.top!=-1)&&(S.elem[S.top]!='('))
{
Pop(S,exp[t]);t++;
}
Push(S,ch);
}
else if(ch=='*'||ch=='/')
{
while((S.elem[S.top]=='*')||(S.elem[S.top]=='/'))
{
Pop(S,exp[t]);t++;
}
Push(S,ch);
}
}
while(/*S.elem[S.top]>=0*/S.top!=-1)
{
Pop(S,exp[t]);t++;
}
exp[t]='#';
DestroyStack(S);
}
问题点数:100、回复次数:2Top
1 楼newbiestar()回复于 2005-07-03 12:10:39 得分 100
这个可不是简单的中缀了……这个已经把-作为一个一元操作符来用了,这样的话复杂性就增加了……
说实话,如果使用sscanf的话,效果会好得多……Top
2 楼www20602(小渺)回复于 2005-07-03 12:43:18 得分 0
似乎是麻烦一些,不过为了与其他的程序连接只好如此了,
不过为什么有runtime error呢Top




