之前做了個中序式轉成後序式的題目... 卻不曉得要怎麼改成中序式轉式前序式... 請大家幫我看看..哪裡能夠修改..
謝謝... **************************************************************
#include
void main(void)
{
char poly[30]={0}; //多項式
char temp[10]={0}; //運算元
char b_poly[30]={0};//後置式結果
int result[10]={0};
int oper=0;
int p=0,b=0,t=0,i;
cout << "Please input poly: ";
cin >> poly;
//將中置式轉換成後置式
do
{
if(poly[p]==' ' ||poly[p]=='-'||poly[p]=='*'
||poly[p]=='/' ||poly[p]=='(')
{
switch(poly[p])
{
case '*':
case '(':
temp[t]=poly[p];
t ;
p ;
break;
case '/':
if(temp[t-1]!='*')
{
temp[t]=poly[p];
t ;
p ;
break;
}
else
{
b_poly[b]=temp[t-1];
temp[t-1]=0;
b ;
t--;
continue;
}
case ' ':
if(temp[t-1]!='*'&&temp[t-1]!='/')
{
temp[t]=poly[p];
t ;
p ;
break;
}
else
{
b_poly[b]=temp[t-1];
temp[t-1]=0;
b ;
t--;
break;
}
case '-':
if(temp[t-1]!='*'&&temp[t-1]!='/'&&temp[t-1]!=' ')
{
temp[t]=poly[p];
t ;
p ;
break;
}
else
{
b_poly[b]=temp[t-1];
temp[t-1]=0;
b ;
t--;
break;
}
}
}
else
if(poly[p] == ')') //處理刮號的部分如下
{
while(temp[t-1]!='(')
{
b_poly[b]=temp[t-1];
temp[t-1]=0;
b ;
t--;
}
temp[t-1]=0;
t--;
p ;
}
else
{
b_poly[b]=poly[p];
b ;
p ;
}
}while(poly[p]!='\0'); for(i=t-1;i>=0;i--)
{
b_poly[b 1]=b_poly[b];
b_poly[b]=temp[i];
b ;
}
b_poly[b]='\0';
cout << b_poly <<"\n"; //印出後置式結果
b=0,t=0; //利用後置式運算出最後答案
while(b_poly[b]!='\0')
{
switch(b_poly[b])
{
case '*':
oper=result[t-2]*result[t-1];
result[t-2]=0;
result[t-1]=0;
result[t-2]=oper;
t--;
b ;
break;
case '/':
oper=result[t-2]/result[t-1];
result[t-2]=0;
result[t-1]=0;
result[t-2]=oper;
t--;
b ;
break;
case ' ':
oper=result[t-2] result[t-1];
result[t-2]=0;
result[t-1]=0;
result[t-2]=oper;
t--;
b ;
break;
case '-':
oper=result[t-2]-result[t-1];
result[t-2]=0;
result[t-1]=0;
result[t-2]=oper;
t--;
b ;
break;
default:
result[t]=int(b_poly[b])-48;
t ,b ;
break; }
}
cout << result[0] << endl;
cin.get();
} **************************************************************** From:小蘋果咬一口...
------
From:小蘋果咬一口...