i have problem in convert infix to postfix

some expression evaluate it correctly

and other no

ex:
c*(a+b)

evaluate it correctly

but
A*(B*C+D)+E
doesn't evaluate correctly

and there is another problem in finding result

my code

    // convert.h
    const char SIZE=100;

    class stack
    {
    public:
            stack();
            bool isempty()const;
            bool isfull()const;
            void push(char num);
            void pop(char &elm);
            void cntres();
            int topnum;
            void print();
            void result(stack &post);
    private:
            char data[SIZE];
            int top;
    };

    // convert.cpp
    #include<iostream>
    #include"convert.h"
    #include<cstddef>
    #include<cstring>

    using namespace std;

    stack::stack()
    {
            top=-1;
            topnum=top;
    }

    bool stack::isempty()const
    {
            return top==-1;

    }

    bool stack::isfull()const
    {
            return top==SIZE-1;
    }

    void stack::push(char elm)
    {
    if(isfull())
            cout<<"Cannot Add New Item , No Free Space ."<<endl;
    else
            {
                    top++;
                    data[top]=elm;
                    topnum=top;
            }

    }
    void stack::pop(char &elm)
    {
            if(isempty())
                    cout<<"There's No Data ."<<endl;
            else
            {
                    topnum=top;
                    elm=data[top];
                    top--;

            }
    }


    void stack::print()
    {       int i;
            cout<<"PostFix Formula : ";
            for(i=0;i<=top;i++)
            cout<<data[i];
    }

    void stack::result(stack &post)
    {
            int i;
            int a,b,c;
            char oper;
            int res=0;
            int loop=strlen(post.data);
            for(i=0;i<=loop;i++)
    {
if(post.data[i]=='+'||post.data[i]=='-'||post.data[i]=='*'||post.data[i]=='/')
 {
pop(oper);
a=oper-48;
pop(oper);
b=oper-48;
if (post.data[i]=='+')
        c=a+b;
else if (post.data[i]=='-')
c=a-b;
else if (post.data[i]=='*')
 c=a*b;
 else c=a/b;

  oper=c+48;
    push(oper);

 }
 push(post.data[i]);

    }
    pop(oper);
    res=oper-48;
    cout<<res<<endl;

    }

    // main
    #include<iostream>
    #include"convert.h"
    #include<cstring>

    using namespace std;

    int main()
    {
stack op;
stack post;
stack res;
char infex[SIZE];
cout<<"Please Enter Infex Formula :";
cin.get(infex,SIZE);
char OpValue;
char ch;
int lenght;
lenght=strlen(infex);
for(int i=0;i<lenght;i++)
{
  if(infex[i]=='+'||infex[i]=='-'
  ||infex[i]=='*'||infex[i]=='/'
  ||infex[i]=='('||infex[i]==')')
    {
      if(infex[i]=='*'||infex[i]=='/'||infex[i]=='(')
             op.push(infex[i]);
                 else if(infex[i]=='+'||infex[i]=='-')
         {
    if(op.topnum=='*'||op.topnum=='/')
  {
       op.pop(ch);
       OpValue=ch;
     while(ch!='('&& !op.isempty())
     {
       post.push(ch);
      op.pop(ch);
     }
   op.push(infex[i]);
  }
   else
   op.push(infex[i]);
        }
   else if(infex[i]==')')
{
    op.pop(ch);
 OpValue=ch;
while(OpValue!='(')
   {
     post.push(OpValue);
    op.pop(ch);
   OpValue=ch;
 }

}    

      }
  else
post.push(infex[i]);

   }
     while(!op.isempty())
     {
        op.pop(ch);
        OpValue=ch;
      post.push(OpValue);     
    }

            post.print();
            cout<<endl;

            cout<<"RESULT OF INFIX : ";
            res.result(post);

    system("pause");
    return 0;
    }

Edited 3 Years Ago by deceptikon: Fixed formatting a bit in example expressions (Markdown was removing the asterisks)

At a glance, the following line is wrong:

if(op.topnum=='*'||op.topnum=='/')

topnum will never likely be either '*' or '/', it's a public alias for top, which is an index rather than a value. Unless the expression is rather large, this condition will never evaluate to true.

how to solve it ?
i wanna to cheak if top of stack is equal to * or /

i make this function

int topnum()const;



int stack::topnum()const
{
return top;
}



if(op.topnum()=='*'||op.topnum()=='/')

still error....

still error....

...so you wrote a member function that goes around your ass to get to your elbow and do exactly the same thing as just accessing topnum directly? What I meant was write a member function that returns the top value on the stack; a pop without the pop:

int top_value() const
{
    if (isempty())
        throw "Stack is empty";

    return data[top];
}

:(
this is the output
after i but ur code

Please Enter Infex Formula :2*(2+2)
PostFix Formula : 222+*
RESULT OF INFIX : -48

:(
this is the output
after i but ur code

The postfix is correct. The result is obviously not correct, but that's not relevant to the problem you stated of a more complex infix equation being converted incorrectly.

Anyway, I'm not telling you where the exact problem you're having is, I simply eyeballed the code and pointed out a very obvious bug. If fixing that bug doesn't fix the problem then the problem is rooted more deeply in your algorithm.

Please don't expect me to do your thinking for you.

i correct the evaluation to postfix
but still a problem in findong result

Compare and contrast:

void stack::result(stack &post)
{
    int i;
    int a,b,c;
    char oper;
    int res=0;
    for(i=0;i<=post.topnum;i++)
    {
        if(post.data[i]=='+'||post.data[i]=='-'||post.data[i]=='*'||post.data[i]=='/')
        {
            pop(oper);
            a=oper-'0';
            pop(oper);
            b=oper-'0';

            if (post.data[i]=='+')
                c=a+b;
            else if (post.data[i]=='-')
                c=a-b;
            else if (post.data[i]=='*')
                c=a*b;
            else
                c=a/b;

            oper=c+'0';
            push(oper);
        }
        else
        {
            push(post.data[i]);
        }

    }
    pop(oper);
    res=oper-'0';
    cout<<res<<endl;
}

Also take note that you'll have issues when the numbers reach more than a single digit. I'd personally also make result() a non-member function and create a local stack for the evaluation. The way you have it is unintuitive.

thx
i know that it doesn't work if there are more than digit

i will try to char array of char to array of integer
and if there any charachter i will do static_cast...

i try to make it array of integer but it seems to be more complex

look for output

Please Enter Infex Formula :(2+7)*6
PostFix Formula : 27+6*
RESULT OF INFIX : -48

proplem in finding result
in this function

void stack::result(stack &post)

RESULT OF INFIX : -48

Use a debugger and step through your code. Make sure that each line does what you think it does with the given input.

Pro Tip: The result always seems to be -48, and that's important to notice.

the problem out
when there are more than digit in a char var
ex :
char oper='48';
int =oper-'0';
out out:8
should output 48.....

here is the problem

here is the problem

Open a debugger and step through your code. I've done this and see what your problem is, but I won't always be around to hold your hand. So I want you to debug the code and see it for yourself. This is something you'll be doing a lot as a programmer. For every hour you spend writing code, you'll spend five hours debugging it.

i make
finding result in main
but still out error value
pls help to find where the problem i
should done it today

  cout<<"RESULT OF INFIX : ";
        while(!post.isempty())
        {
            post.pop(out);
            if(out=='+'||out=='-'||out=='*'||out=='/')
            {
                char num;
                int a,b,c;
                res.pop(num);
                    a=num-'0';
                res.pop(num);
                b=num-'0';

                switch(out)
                {
                case '+':
                    c=a+b;
                    break;
                case '-':
                    c=a-b;
                    break;
                case '*':
                    c=a*b;
                    break;
                case '/':
                    c=a/b;
                    break;


            }
                res.push(c);
            }
            else res.push(out);
        }

        char c;
        res.pop(out);
        c=char(out);
        cout<<c<<endl;

All of the code, please. It's a short enough program, and I'd like to make sure I'm seeing what you're seeing. There have been several changes since your original post where all of the code was listed.

this is the hole code

//convert.h
const char SIZE=100;

class stack
{
public:
    stack();
    bool isempty()const;
    bool isfull()const;
    void push(char num);
    void pop(char &elm);
    void cntres();
    char topnum()const;
    void print();
    void result(stack &post);
private:
    char data[SIZE];
    int top;
};

//convert.cpp
#include<iostream>
#include"convert.h"
#include<cstddef>
#include<cstring>
#include<ctype.h>

using namespace std;

stack::stack()
{
    top=-1;

}

bool stack::isempty()const
{
    return top==-1;

}

bool stack::isfull()const
{
    return top==SIZE-1;
}

void stack::push(char elm)
{
if(isfull())
    cout<<"Cannot Add New Item , No Free Space ."<<endl;
else
    {
        top++;
        data[top]=elm;

    }

}
void stack::pop(char &elm)
{
    if(isempty())
        cout<<"There's No Data ."<<endl;
    else
    {

        elm=data[top];
        top--;

    }
}


void stack::print()
{   int i;
    cout<<"PostFix Formula : ";
    for(i=0;i<=top;i++)
    cout<<data[i];
}



char stack::topnum()const
{

    return data[top];

}

// main file
#include<iostream>
#include"convert.h"
#include<cstring>

using namespace std;

int main()
{
    stack op;
    stack post;
    stack res;
    char infex[SIZE];
    cout<<"Please Enter Infex Formula :";
    cin.get(infex,SIZE);
    char OpValue;
    char ch;
    int lenght;
    lenght=strlen(infex);
    for(int i=0;i<lenght;i++)
    {
        if(infex[i]=='+'||infex[i]=='-'||infex[i]=='*'||infex[i]=='/'||infex[i]=='('||infex[i]==')')
        {
            if(infex[i]=='*'||infex[i]=='/'||infex[i]=='(')
                op.push(infex[i]);
            else if(infex[i]=='+'||infex[i]=='-')
            {
                if(op.topnum()=='*'||op.topnum()=='/')
                {
                    op.pop(ch);
                        post.push(ch);
                    op.push(infex[i]);
                }
                else
                    op.push(infex[i]);
                }
            else if(infex[i]==')')
            {
                op.pop(ch);
                OpValue=ch;
                while(OpValue!='(')
                {
                    post.push(OpValue);
                    op.pop(ch);
                    OpValue=ch;
                }

            }


            }
        else
            post.push(infex[i]);

        }
    while(!op.isempty())
    {
        op.pop(ch);
        OpValue=ch;
        post.push(OpValue); 
    }

    post.print();
    cout<<endl;
    char out;
    cout<<"RESULT OF INFIX : ";
        while(!post.isempty())
        {
            post.pop(out);
            if(out=='+'||out=='-'||out=='*'||out=='/')
            {
                char num;
                int a,b,c;
                res.pop(num);
                    a=num-'0';
                res.pop(num);
                b=num-'0';
                switch(out)
                {
                case '+':
                    c=a+b;
                    break;
                case '-':
                    c=a-b;
                    break;
                case '*':
                    c=a*b;
                    break;
                case '/':
                    c=a/b;
                    break;
            }
                res.push(c);
            }
            else res.push(out);
        }
        char c;
        res.pop(out);
        c=char(out);
        cout<<c<<endl;

system("pause");
return 0;
}

pls hlp i have to done today

Okay, so did you really use a debugger to step through the code? Because the problem is immediately obvious on the first line of your evaluation: you're processing the expression backward. A quick fix would be to use a temporary stack in the evaluation code instead of post, and that stack would be initialized using post like so:

stack temp;

/* Reverse post into temp */
while (!post.isempty()) {
    post.pop(out);
    temp.push(out);
}

// Begin expression evaluation code
// Use temp from here on out and not post

Also, when pushing the result back onto res, you need to reverse your integer to char conversion, otherwise you'll push the wrong value onto the stack:

res.push(c + '0');

O.o
i did not Watch that mistake

thanks very much :)

god bless u

This article has been dead for over six months. Start a new discussion instead.