我需要一个接受中缀字符串(如“3 + 4 * 9”)并将其转换为后缀(如“4 9 * 3 +”)的函数。
我让它工作,直到你在括号内加上括号。我整天都在努力,无法弄清楚我做错了什么——也许有头脑的人能看到它吗?我觉得我真的很亲近!
谢谢!这是代码:
string ExpressionManager::infixToPostfix(string infixExpression)
{
cout << "itop Testing : " << infixExpression << endl;
string posnums = "0123456789";
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;
}