Here is my infix to postfix in c++... its a function i made which reads in 1 line at a time from a file.. you can mod it to just read whatever you have and it should work fine

this IS somewhat of a "brute force" attempt on the process because you can assign a # value to each operand and then check for < or > which would reduce code but this WORKS at least heh
CODE
string Utility::InfixToPostfixOperation(string& strInput)
{
vector<string> strvInput; //vector to hold the referenced Split(strInput)
stack<char> cStack; //stack of characters (Operators)
char cStackToPostfix = ' '; //character to add(pop) to postfix result
string strPostfixResult = " "; //string to hold postfix notation result
int nSize = strvInput.size(); //length of the infix notation expression
vector<string>::iterator Itv; //vector Iterator
strvInput = Split(strInput);
for(Itv = strvInput.begin(); Itv != strvInput.end(); Itv++)
{
string strTemp =* Itv;
if(strTemp == "*" || strTemp == "/" || strTemp == "+" || strTemp == "-" || strTemp == "=" || strTemp == "(" || strTemp == ")")
{
if(strTemp == "=")
{
cStack.push('=');
}//end if
else if(strTemp == "-" && !cStack.empty())
{
if(cStack.top() == '=' || cStack.top() == '*' || cStack.top() == '/' || cStack.top() == '+' || cStack.top() == ')' || cStack.top() == '(')
{
if(cStack.top() == '(')
{
cStack.push('-');
}
else if(cStack.top() == '*' || cStack.top() == '/' || cStack.top() == '+')
{
strPostfixResult += " ";
strPostfixResult += cStack.top();
cStack.pop();
cStack.push('-');
}
else
{
cStack.push('-');
}
}//end while
}//end else if
else if(strTemp == "+" && !cStack.empty())
{
if(cStack.top() == '=' || cStack.top() == '*' || cStack.top() == '/' || cStack.top() == '(' || cStack.top() == '(')
{
if(cStack.top() == '(')
{
cStack.push('+');
}
else if(cStack.top() == '*' || cStack.top() == '/')
{
strPostfixResult += " ";
strPostfixResult += cStack.top();
cStack.pop();
cStack.push('+');
}
else
{
cStack.push('+');
}
}//end while
}//end else if
else if(strTemp == "/" && !cStack.empty())
{
if(cStack.top() == '+' || cStack.top() == '-' || cStack.top() == '=' || cStack.top() == '*' || cStack.top() == '(' || cStack.top() == ')')
{
if(cStack.top() == '(')
{
cStack.push('/');
}
else if(cStack.top() == '*')
{
strPostfixResult += " ";
strPostfixResult += cStack.top();
cStack.pop();
cStack.push('/');
}
else
{
cStack.push('/');
}
}
}//end else if
else if(strTemp == "*" && !cStack.empty())
{
if(cStack.top() == '/' || cStack.top() == '+' || cStack.top() == '-' || cStack.top()== '=' || cStack.top() == '(' || cStack.top() == ')')
{
cStack.push('*');
}
}//end else if
else if(strTemp == "(")
{
cStack.push('(');
}
else if(strTemp == ")")
{
while(!cStack.empty() && cStack.top() != '(')
{
strPostfixResult += " ";
strPostfixResult += cStack.top();
strPostfixResult += " ";
cStack.pop();
}
if(!cStack.empty())
{
cStack.pop();
}
}
}//end if
if(strTemp != "*" && strTemp != "/" && strTemp != "+" && strTemp != "-" && strTemp != "=" && strTemp != "(" && strTemp != ")")
{
strPostfixResult += " ";
strPostfixResult += strTemp;
}//end if
}//end for
while(!cStack.empty())
{
if(cStack.top() == '(' || cStack.top() == ')')
{
cout << "Invalid Infix Notation Expression... Parantheses Mismatch";
if(cStack.top() == '(')
{
cout << "\nMissing Right Parantheses" << endl << endl;
cout << "\nAttempted ";
}//end if
else if(cStack.top() == ')')
{
cout << " \nMissing Left Parantheses" << endl << endl;
cout << "\nAttempted ";
}//end else if
}//end if
if(cStack.top() == '(' || cStack.top() == ')')
{
cStack.pop();
}//end if
strPostfixResult += " ";
strPostfixResult += cStack.top();
strPostfixResult += " ";
cStack.pop();
}
return strPostfixResult;
}//end InfixToPostfix
maybe this will help some
note: here is my split function too:
CODE
vector<string> Utility::Split(const string &strInput, const string &strDelims)
{
string strTokens;
vector<string> parsedString;
string strTemp = strInput;
int nCol = 0;
while(!strTemp.empty())
{
StripBoth(strTemp);
StripBoth(strTemp, '\t');
nCol = (int)strTemp.find_first_of(strDelims); //find the position of the first delimiter
if(nCol == string::npos) //if a delimiter is NOT found
{
strTokens = strTemp; //set string to Token (can't parse it so send it back)
strTemp = ""; //set the remaining string to nothing
}//END if
else if(nCol != string::npos) //if a delimiter IS found
{
if(nCol == 0)
{
nCol = 1;
}//end if
strTokens = strTemp.substr(0,nCol); //set strTemp to the string from 0 to the position of the 1st delimiter
//strTemp = strTemp.substr(nCol+1); //set strTemp to the remaining string
strTemp = strTemp.substr(nCol);
}//END else
parsedString.push_back(strTokens); //put token in vector
}//END while
return parsedString;
}//END split
This post has been edited by Topher84: 17 Jul, 2007 - 05:21 AM