//I make program of infix to post fix xpression plz tell me waht wrong with my code 
//some it true result for arthematic opertaors b,w two oprend but with more then it does //not provide valid output plxxxxxxxxxxxx help me 
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int presidence( );
void infixToPostfix(char *);
void postfixEvaluation();
template<class T>
class Stack
{
private:
char read[500];
T *data;
int capacity;
int top;

public:
Stack();
Stack(int );
T pop(T & p);
void push(T);
int isEmpty();
int isFull();
int getTop();
~Stack();
};



template<class T>
Stack<T> ::Stack()
{
capacity=0;
top=-1;
}
template<class T>
Stack<T>::Stack(int s=500)
{
capacity=s;
data=new T[capacity];
top=-1;
}
template<class T>
int Stack<T>:: isEmpty()
{
return (top==-1);
}
template<class T>
int Stack <T>:: isFull()
{
return (top==capacity-1);
}
template<class T>
void Stack<T>:: push(T p)
{
if(isFull())
{
cout<<"Stack is OverFlow";
exit(1);
}
else
{
top++;
data[top]=p;

}
}
template<class T>
T Stack<T>:: pop(T & p)
{
if(isEmpty())  {
cout<<"Under Flow";
exit(0);     }
else {

p=data[top];
top--;
}

}
template<class T>
Stack<T>::~Stack(){
delete[] data;
}
template<class T>
int Stack<T>::getTop()
{
return top;
}
int presidence(char A,char B)
{
if(A=='+' && B=='-' || A=='*' && B=='/')
{
return 1;
}

if(A=='-' && B=='+' || A=='/' && B=='*')
return 1;

if(A=='+' && B=='*' || B=='-'||A=='/' && B=='+' ||A=='/' && B=='-')
{
return 1;
}
if(A=='*' && B=='+' || A=='*' && B=='-')
return 0;
}
void infixToPostfix(char *infix)
{
int len=strlen(infix);
char postfix[20];
Stack <char> P(len+1);
int pi = 0;

for(int i=0;infix[i]!='\0';i++)
{
if(infix[i]>='A' ||infix[i]>='a' ||infix[i]>='0'&&
	infix[i]<='Z'||infix[i]>='z'||infix[i]>='9')
{
postfix[pi]=infix[i];
pi++;
}
if(infix[i]=='+' ||infix[i]=='-'||infix[i]=='*'||infix[i]=='/')
{
if(P.isEmpty() || infix[i-1]=='(')
{
P.push(infix[i]);
}
else if(!P.isEmpty() || infix[i-1]!='(')
{
// chage
int no=presidence(infix[i],P.pop(i));

while(!P.isEmpty() && no==1)
{
// change accour
postfix[pi]=P.pop(pi);
pi++;
}
P.push(infix[i]);
if(infix[i]!=')')
{
P.push(infix[i]);
}
else if(infix[i]==')')
{
//cahge accour
while(P.pop(i)!='(')
{
postfix[pi]=P.pop(pi);
pi++;
}

}
}

}
}
for( i=pi;!P.isEmpty() && i<=len;i++)
{
postfix[i]=P.pop(i);
}

for(int j=0;j<=pi;j++)
cout<<" "<<postfix[j];
}
void main()
{
clrscr();
char arr[] = "(A+B)*(C-D)";

infixToPostfix(arr);
Stack <int>s;
cout<<endl;
getch();
}

Recommended Answers

All 6 Replies

char arr[] = "(A+B)*(C-D)";

This obove xpression must have such out AB+CD-* but i does not provide this output
why is wrong with this code plxx tell me

OMG...

Try this code:

#include <iostream.h>
#include <assert.h>
#include <stdlib.h>

enum TokenType
{
    tkOpen,
    tkPlus, tkMinus,
    tkMult, tkDiv,
    tkClose,
    tkNumber,
    sizeTokenType
};


const char chars[sizeTokenType] =
{ '(', '+', '-', '*', '/', ')', ' '};
const int priority[sizeTokenType] =
{0, 1, 1, 2, 2, 5, 10};


template <class T>
class Stack
{
    int top;
    int size;
    T *data;

public:
    Stack(int sz = 100);
    ~Stack();

    int isEmpty()
    {
        return (top == -1);
    }
    int isFull()
    {
        return (top == size - 1);
    }

    void Push(T);
    T Pop();
    T Top()
    {
        return (data[top]);
    }

};

template <class T>
Stack<T>::Stack(int sz)
{
    size = sz;
    data = new T[size];
    assert(data != 0);
    top = -1;
}
template <class T>
Stack<T>::~Stack()
{
    delete data;
}

template <class T>
void Stack<T>::Push(T value)
{
    if(isFull())
    {
        cout << "Stack overflow" << endl;
        exit(-1);
    }
    data[++top] = value;
}

template <class T>
T Stack<T>::Pop()
{
    if(isEmpty())
    {
        cout << "Stack underflow" << endl;
        exit(-1);
    }
    return data[top--];
}

Stack<TokenType> s;

TokenType getNextToken(char*& s, char& sT)
{
    switch(*s)
    {
        case '(':
            sT = '\0'; s++; return tkOpen;
        case ')':
            sT = '\0'; s++; return tkClose;
        case '+':
            sT = *s++; return tkPlus;
        case '-':
            sT = *s++; return tkMinus;
        case '*':
            sT = *s++; return tkMult;
        case '/':
            sT = *s++; return tkDiv;

        default:
            sT = *s++; return tkNumber;
    }
}

// char *arr = "2*(3+4)*5";
char *arr = "(A+B)*(C-D)";

int main()
{
    char sToken;
    char *ps = arr;

    while(*ps)
    {
        TokenType nextToken = getNextToken(ps, sToken);
        switch(nextToken)
        {
            case tkNumber:
                cout << sToken;
                break;

            case tkOpen:
                s.Push(nextToken);
                break;

            case tkClose:
                {
                    TokenType t;
                    do
                    {
                        t = s.Pop();
                        if(t != tkOpen) cout << chars[t];
                    }
                    while(t != tkOpen);
                }
                break;

            case tkPlus:
            case tkMinus:
            case tkDiv:
            case tkMult:
                while(!s.isEmpty() && (priority[nextToken] <= priority[s.Top()]))
                {
                    TokenType t = s.Pop();
                    if(t != tkOpen) cout << chars[t];
                }
                s.Push(nextToken);
                break;
        }
    }

    while(!s.isEmpty())
    {
        TokenType t = s.Pop();
        if(t != tkOpen) cout << chars[t];
    }
    return 0;
}
commented: OMG -doing people's homework as NOT acceptable here. Please let them get their own grade. -4
commented: give me lot of help +1

this isnt a conversion dude.. it is simply showing the output.. it isnt even taking a value from user...

Try this code:

that's great bro , can you u add its Evaluation , if we have this arry 2*5+9/3 its answer is 6 ..how to Evaluate this ..

Yeah, can you finish his homework for him? He has to pass the class, you know.

commented: no need to tell me ..i know better -1

thnkx every one

commented: Don't give neg rep to someone for a comment not directed at you. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.