infix operation to postfix using stacks

Posted by Chris De La O on Stack Overflow See other posts from Stack Overflow or by Chris De La O
Published on 2012-10-02T03:18:40Z Indexed on 2012/10/02 3:37 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

We are writing a program that needs to convert an infix operation (4 5/3) to postfix (4 5 3 / ) using stacks. however my convert to postfix does not work as it doesnt not output the postFix array that is supposed to store the conversion from infix notation to postfix notation. here is the code for the convertToPostix fuction.

//converts infix expression to postfix expression
void ArithmeticExpression::convertToPostfix(char *const inFix, char *const postFix)
{

 //create a stack2 object named cow
 Stack2<char> cow;
 cout<<postFix;

 char thing = '(';

 //push a left parenthesis onto the stack
 cow.push(thing);


//append a right parenthesis to the end of inFix array
strcat(inFix, ")");

int i = 0;//declare an int that will control posFix position


//if the stack is not empty
if (!cow.isEmpty())
{

    //loop to run until the last character in inFix array
    for (int x = 0; inFix[x]!= '\0'; x++ )
    {

        //if the inFix element is a digit
        if (isdigit(inFix[x]))
        {
            postFix[i]=inFix[x];//it is assigned to the next element in postFix array
            i++;//move on to next element in postFix

        }

        //if the inFix element is a left parenthesis
        else if (inFix[x]=='(')
        {
            cow.push(inFix[x]);//push it unto the stack
        }

        //if the inFix element is an operator
        else if (isOperator(inFix[x]))
        {

            char oper2 = inFix[x];//char variable holds inFix operator

            if (isOperator(cow.stackTop()))//if the top node in the stack is an     operator
            {
            while (isOperator(cow.stackTop()))//and while the top node in the stack is an operator
            {

                char oper1 = cow.stackTop();//char variable holds node operator
                if(precedence( oper1, oper2))//if the node operator has higher presedence than node operator
                {
                    postFix[i] = cow.pop();//we pop such operator and insert it in postFix array's next element
                    cow.push(inFix[x]);//and push inFix operator unto the stack
                    i++;//move to the next element in posFix
                }

            }
        }
            //if the top node is not an operator
            //we push the current inFix operator unto the top of the stack
            else
            cow.push(inFix[x]);

        }

        //if the inFix element is a right parenthesis
        else if (inFix[x]==')')
        {
            //we pop everything in the stack and insert it in postFix
            //until we arrive at a left paranthesis
            while (cow.stackTop()!='(')
            {
                postFix[i] = cow.pop();
                i++;
            }
            //we then pop and discard left parenthesis
            cow.pop();
        }


    }

     postFix[i]='\0';

    //print !!postFix array!! (not stack)
    print();//code for this is just cout<<postFix;

} 

© Stack Overflow or respective owner

Related posts about c++

Related posts about stack