您的位置:首页 > 其它

中缀到后缀表达式的转换

2006-12-17 15:52 260 查看

#include <iostream>


#include <stack>


#include <string>




using namespace std;




//移除空格


string RemoveSpace(const string str);




//符号


//-1--Error,


//-2--Done,


//0--Pop to postfix,


//1--Push




int op[5][4] = ...{ /**//* ( +,- *,/ 空 */




/**//* ) */ ...{ 0, 0, 0, -1},




/**//* ( */ ...{ 1, 1, 1, 1},




/**//* +,- */ ...{ 1, 0, 0, 1},




/**//* *,/ */ ...{ 1, 1, 0, 1},




/**//* 空 */ ...{-1, 0, 0, -2}


};


int GetIndex(char topStack);


int Compare(char ch, char topStack);




//验证字符是否是合法字符


//false--字符非合法


bool Parse(const char & ch);


//从中缀到后缀表达式的转换


string Convert(const string & str);




//---------------


int main(int argv, char * args)




...{


string str = "x - (y*a/b - (z + d * e) + c ) / f";


cout << Convert(str) << endl;


cout << Convert("a + c - h / b * r") << endl;


return 0;


}


//----------------




//移除空格


string RemoveSpace(const string str)




...{


string strTemp;


int len = str.size();




for(int i = 0; i < len; i++)...{


if(str[i] != ' ')


strTemp += str[i];


}


return strTemp;


}




//符号表


//-1--Error,


//-2--Done,


//0--Pop to postfix,


//1--Push




/**//*-------------------------------------


( +,- *,/ 空


) 0, 0, 0, -1


( 1, 1, 1, 1


+,- 1, 0, 0, 1


*,/ 1, 1, 0, 1


空 -1, 0, 0, -2


--------------------------------------*/




//called by Compare


int GetIndex(char topStack)




...{




switch(topStack)...{


case '(':


return 0;


case '+':


case '-':


return 1;


case '*':


case '/':


return 2;


default:


return 3;


}


}


int Compare(char ch, char topStack)




...{


int index = GetIndex(topStack);






switch(ch)...{


case ')':


//cout << ")" << endl;


return op[0][index];


break;


case '(':


//cout << "(" << endl;


return op[1][index];


break;


case '+':


case '-':


//cout << "+,-" << endl;


return op[2][index];


break;


case '*':


case '/':


//cout << "*,/" << endl;


return op[3][index];


break;


default:


return op[4][index];


break;


};


}






//验证字符是否是合法字符


//false--字符非合法




bool Parse(const char & ch)...{


if(ch == '+' || ch == '-' || ch == '*' || ch == '/' ||


ch == '(' || ch == ')')




...{


return true;


}




else...{




if(ch >= 'a' && ch <= 'z')...{


return true;


}


return false;


}


}


//从中缀到后缀表达式的转换


string Convert(const string & str)




...{


string result = "";


string temp = RemoveSpace(str);


stack<char> operatorStack;






if(temp[0] < 'a' || temp[0] > 'z')...{


return "string must begin with a char.";


}






for(int i=0; i<temp.length(); i++)...{


char ch = temp[i];




if(Parse(ch))...{ //如果解析正确




if(ch >= 'a' && ch <= 'z')...{


result += temp[i];


continue;


}




else...{ //ch = +, -, *, /, (, )




if(operatorStack.empty())...{


operatorStack.push(ch);


continue;


}


char topStack = operatorStack.top();


int n = Compare(ch, topStack);






switch(n)...{


case 1://push


operatorStack.push(ch);


continue;


case 0://pop




if(topStack != '(')...{ //如果栈顶字符是'(', 则直接出栈


result += topStack;


i--;


}


operatorStack.pop();


continue;


default:


return "convert wrong!";


}//end switch


}//end else


}


}//end for




while(!operatorStack.empty())...{


char topStack = operatorStack.top();


result += topStack;


operatorStack.pop();


}


return result;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: