#include <iostream> #include <string> #include <iterator> #include "ChainStack.cpp" using namespace std; string reverseConvert(const string& inputString);//将中序表达式转换成逆波兰表达式 int calculateString(const string& inputString);//将逆波兰表达式求解 bool isNumber(const char e);//判断字符是否为数字 bool isSymbol(const char e);//判断字符是否为符号 bool checkLevel(const char c1, const char c2);//c1的运算级别小于c2;返回true; int levelSymbol(const char c1);//返回符号的级别 int main() { string Input = "2*(1+2/2) "; string Output = reverseConvert(Input); cout << Input << endl; cout << Output << endl; cout << "2*(1+2/2 = " << calculateString(Output) << endl; return 0; } int calculateString(const string& inputString) { ChainStack<char> resultStack; string temp = inputString; string::iterator i = temp.begin(); for (; i !=temp.end(); ++i) { if (isNumber(*i)) { resultStack.Push(*i); } else { char t1, t2; int result; resultStack.Pop(t1); resultStack.Pop(t2); if (*i=='+') { result = (t2 - '0') + (t1 - '0'); resultStack.Push(result+'0'); } if (*i == '-') { result = (t2 - '0') - (t1 - '0'); resultStack.Push(result + '0'); } if (*i == '*') { result = (t2 - '0') * (t1 - '0'); resultStack.Push(result + '0'); } if (*i == '/') { result = (t2 - '0') / (t1 - '0'); resultStack.Push(result + '0'); } if (*i == '%') { result = (t2 - '0') % (t1 - '0'); resultStack.Push(result + '0'); } } } char topChar; resultStack.GetTop(topChar); return topChar-'0'; } bool isNumber(const char e) { bool temp = false; switch (e) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': temp = true; break; default: break; } return temp; } bool isSymbol(const char e) { bool temp = false; switch (e) { case '+': case '-': case '*': case '/': case '(': case ')': case '%': temp = true; break; default: break; } return temp; } int levelSymbol(const char c1) { int i = 0; if (c1 == '+' || c1 == '-') { i = 1; } if (c1 == '*' || c1 == '/' || c1 == '%') { i = 2; } return i; } bool checkLevel(const char c1, const char c2) { return levelSymbol(c1) <= levelSymbol(c2); } string reverseConvert(const string& inputString) { ChainStack<char> tempStack; string temp, returnString; char topChar; temp = inputString; string::iterator i = temp.begin(); for (; i != temp.end(); ++i)//从左至右扫描中缀表达式 { tempStack.GetTop(topChar); if (isNumber(*i)) { returnString += *i; } else { if (isSymbol(*i))//若读取的是运算符 { if (*i == '(')//该运算符为左括号"(",则直接存入运算符堆栈。 { tempStack.Push(*i); } else{ if (*i == ')')//该运算符为右括号")",则输出运算符堆栈中的运算符到操作数堆栈,直到遇到左括号为止。 { while (topChar != '(') { tempStack.Pop(topChar); returnString += topChar; tempStack.GetTop(topChar);//运算完后重新获取栈顶元素 } tempStack.Pop(topChar);//把topChar='('弹出栈 } else { tempStack.GetTop(topChar); if (topChar == '(' || tempStack.Empty())//若运算符堆栈栈顶的运算符为左括号或者为空栈,则直接存入运算符堆栈。 { tempStack.Push(*i); } //若比运算符堆栈栈顶的运算符优先级低,则输出栈顶运算符到操作数堆栈,并将当前运算符压入运算符堆栈 else{ while (checkLevel(*i, topChar))//如果当前符合*i比栈顶符号topChar的运算级低或者相等 { tempStack.Pop(topChar);//将栈顶元素弹出赋给输出串,直到栈顶符合的运算级小于等于当前符号 returnString += topChar; if(!tempStack.GetTop(topChar)) break;//如果栈为空跳出循环 //tempStack.Push(*i); } tempStack.Push(*i);//把当前符号压入栈中 } } } } } } while (!tempStack.Empty()) { tempStack.Pop(topChar); returnString += topChar; } return returnString; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/shiwazone/article/details/47067921