我需要一個帶中綴字符串(如「3 + 4 * 9」)並將其轉換爲後綴(如「4 9 * 3 +」)的函數。C++中的分流碼算法
我得到它的工作,直到你把括號內的括號。我一整天都在努力研究,無法弄清楚自己在做什麼錯了 - 有人可以用清新的頭腦看到它,也許?我覺得我真的很接近!
謝謝!下面的代碼:
string ExpressionManager::infixToPostfix(string infixExpression)
{
cout << "itop Testing : " << infixExpression << endl;
string posnums = "";
string posops = "+-*/%(){}[]";
string onlyops = "+-/%*";
string space = " ";
string openbra = "([{";
string closebra = ")]}";
stack <string> nums;
stack <string> ops;
string output = "";
//check to make sure expression is valid
if (!(isBalanced(infixExpression)))
{
cout << "Infix Expression isn't balanced!" << endl;
return "invalid";
}
for (int i=0; i<infixExpression.size(); i++)
{
if ((posnums.find(infixExpression[i])!=string::npos) || (posops.find(infixExpression[i])!=string::npos) || (space.find(infixExpression[i])!=string::npos))
{}
else
{
cout << "Invalid character " << infixExpression[i] << " found." << endl;
return "invalid";
}
}
int numcount = 0;
int opcount = 0;
//Numbers vs. Operators
for (int i = 0; i < infixExpression.size(); i++)
{
if (posnums.find(infixExpression[i]) != -1)
{
while(infixExpression[i] != ' ')
{
if (i == (infixExpression.size()-1))
break;
i++;
}
numcount++;
}
if (onlyops.find(infixExpression[i]) != -1)
{
opcount++;
}
}
if (opcount == (numcount - 1))
{
}
else
{
cout << "Bad operators to numbers ratio!" << endl;
return "invalid";
}
//Get rid of proceeding whitespaces.
int safety = 0;
int net = infixExpression.size();
while (infixExpression[0] == ' ')
{
infixExpression.erase(0,1);
safety++;
if (safety>=net)
break;
}
//cout << "At this point, it is " << postfixExpression << endl;
//the fun part! Set up stacks
for (int i =0; i< infixExpression.size(); i++)
{
cout << "It gets hung up on character " << infixExpression[i] << endl;
if(openbra.find(infixExpression[i]) != -1)
{
string word = "";
word += infixExpression[i];
ops.push(word);
cout << "Pushing onto stack: " << word << endl;
}
else if(closebra.find(infixExpression[i]) != -1)
{
cout << "contents of remaining ops stack: "<< endl;
stack <string> temp;
temp = ops;
for (int j = 0; j < temp.size(); j++)
{
cout << "\t" << temp.top() << endl;
temp.pop();
}
while (openbra.find(ops.top()) == -1)
{
output += " " + ops.top();
cout << "Pushing from stack: " << ops.top() << endl;
ops.pop();
}
cout << "Pushing from stack: " << ops.top() << endl;
ops.pop();
}
else if (posnums.find(infixExpression[i]) != -1)
{
string word = "";
while (infixExpression[i] != ' ')
{
word += infixExpression[i];
i++;
if (i== infixExpression.size())
break;
}
output += " " + word;
}
else if (onlyops.find(infixExpression[i]) != -1)
{
if (ops.size() == 0)
{
string word = "";
word += infixExpression[i];
ops.push(word);
}
else
{
int o1p = 0;
int o2p = 0;
if ((infixExpression[i] == '+') || (infixExpression[i] == '-'))
o1p = 0;
else
o1p = 1;
if ((ops.top() == "+") || (ops.top() == "-"))
o2p = 0;
else
o2p = 1;
while ((ops.size() > 0) && (o1p <= o2p))
{
output += " " + ops.top();
cout << "(odd) Pushing from stack: " << ops.top() << endl;
if ((ops.top() == "+") || (ops.top() == "-"))
o2p = 0;
else
o2p = 1;
if (ops.size() > 0)
{
ops.pop();
}
else
{
break;
}
}
string word = "";
word += infixExpression[i];
ops.push(word);
}
}
}
while (output[0] == ' ')
{
output.erase(0,1);
}
return output;
}
如果只是它開始遞歸下降,不分流.. :) – 2012-10-12 01:35:03
哇。 shift-reduce-parser flashback。我以爲我在很多個月前的大學裏留下這樣的問題。 – WhozCraig
正確的輸出應該是「3 4 9 * +」。當您稍後解析它時,您將遍歷所有元素,直到找到第一個運算符。然後用該操作的結果替換該運算符和前面的兩個操作數,然後查找下一個運算符。 – Atle