中缀到后缀表达式的转换
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;
}
相关文章推荐
- 中缀表达式到后缀表达式的转换
- 中缀表达式转换成前缀表达式和后缀表达式的极其简单方法【转】
- 前缀---中缀--后缀 表达式的相互转换
- 中缀表达式到后缀表达式的转换
- 二叉树中缀表达式到后缀表达式的转换
- 中缀 前缀 后缀表达式 相互转换
- 数据结构之栈----PTA题目7-20表达式转换(中缀转后缀)
- 中缀和后缀表达式之间的转换
- 数据结构——从中缀向后缀转换表达式
- 漫谈栈队列及后缀表达式,后缀中缀表达式间的转换
- 从中缀向后缀转换表达式
- 前缀、中缀、后缀表达式转换
- [栈和队列]从中缀向后缀转换表达式
- 中缀表达式 转换为 前缀 后缀表达式
- 中缀表达式转换成后缀表达式
- 中缀表达式转换成前缀表达式和后缀表达式的极其简单方法
- 中缀表达式转换为后缀表达式的算法
- 栈的应用之中缀表达式转换为后缀表达式
- 一天一个算法: 前缀,中缀,后缀表达式的转换
- [栈和队列]从中缀向后缀转换表达式