Search code examples
c++postfix-notationinfix-notationprefix-operator

c++ infix to prefix conversion?


I'm trying to write a simple program to convert infix notation to prefix and postfix. So far the postfix one works perfectly. However, I can't seem to get the prefix conversion right. I used the shunting yard algorithm for both. Apologies beforehand if my code is a bit unusual (i.e. writing my own stack class instead of using #include, unnecessarily using other things), I have to meet assignment requirements (this is a college assignment). Here's what I've tried so far:

#include<iostream>
#include<string.h>
using namespace std;

const int Max=255;
class Stack
{
private:
    char element[Max];
    int top;
public:
    Stack()
    {
        top=-1;
    }
    bool isFull()
    {
        if(top>=(Max-1)) return true;
        else return false;
    }
    bool isEmpty()
    {
        if(top==-1) return true;
        else return false;
    }
    bool push(char x)
    {
        if(!isFull())
        {
            top++;
            element[top]=x;
            return true;
        }
        else
        {
            cout<<"Stack is full"<<endl;
            return false;
        }
    }
    bool pop(char &x)
    {
        if(!isEmpty())
        {
            x=element[top--];
            return true;
        }
        else
        {
            //cout<<"Stack is empty"<<endl;
            return false;
        }
    }
    char retrieve()
    {
        if(!isEmpty())
        {
            return element[top];
        }
        else
        {
            //cout<<"Stack is empty"<<endl;
            return ' ';
        }
    }
};


class Converter
{
private:
public:
    Converter(){}
    bool isOperator(char x)
    {
        if(x=='+'||x=='-'||x=='*'||x=='/'||x=='^'||x=='('||x==')') return true;
        else return false;
    }
    bool isOperand(char x)
    {
        if(x>='0'&&x<='9') return true;
        else return false;
    }
    int Hierarchy(char x)
    {
        if(x=='+'||x=='-') return 1;
        else if(x=='*'||x=='/') return 2;
        else if(x=='^') return 3;
        else return 0;
    }
    char*ToPostfix(char infix[])
    {
        Stack stack1;
        char res[20];
        int resindex=0;
        for(int i=0;i<strlen(infix);i++)
        {
            if(isOperator(infix[i]))
            {
                if(infix[i]=='(')
                {
                    stack1.push(infix[i]);
                }
                else if(infix[i]==')')
                {
                    while(stack1.retrieve()!='(')
                    {
                        stack1.pop(res[resindex++]);
                    }
                    stack1.pop(res[resindex]);
                }
                else 
                {
                    while(Hierarchy(infix[i])<=Hierarchy(stack1.retrieve()))
                    {
                        stack1.pop(res[resindex++]);
                    }
                    stack1.push(infix[i]);
                }
            }
            else if(isOperand(infix[i]))
            {
                res[resindex++]=infix[i];
            }
        }
        while(!stack1.isEmpty())
        {
            stack1.pop(res[resindex++]);
        }
        res[resindex]='\0';
        return res;
    }
    char*ToPrefix(char infix[])
    {
        char res[20];
        strcpy(res,strrev(infix));
        for(int i=0;i<strlen(res);i++)
        {
            if(res[i]=='(') 
            {
                res[i]=')';
            }
            else if(res[i]==')')
            {
                res[i]='(';
            }
        }
        strcpy(res,ToPostfix(res));
        strcpy(res,strrev(res));
        return res;
    }
};
int main()
{
    Converter convert;
    char infix[20];
    cout<<"Enter infix expression: ";
    cin>>infix;
    cout<<endl<<"Prefix: "<<convert.ToPrefix(infix)<<endl;
    cout<<"Postfix: "<<convert.ToPostfix(infix)<<endl;
    return 0;
}

when I try to convert a simple infix notation, i.e. 1*(2+3)/4^5-6, the postfix conversion is right (123+*45^/6-) but the prefix conversion returns the wrong answer (-*1/+23^456) instead of -/*1+23^456. can anyone help?


Solution

  • Actually, both answers are correct because you can switch the order of the division and the multiplication if multiplication comes first in infix. So your wrong answer is correct in this case. However, there is a left to right precedence, so your hierarchy handling is not correct: change else if(x=='*'||x=='/') return 2;.