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;
}