#include<iostream>
using namespace std;
class stack
{
char st[50];
int maxsize;
public:
int tos;
stack()
{
tos=-1;
maxsize=0;
}
stack(int size)
{
tos=-1;
maxsize=size;
}
char peek()
{
return (st[tos]);
}
void push(int item)
{
if(tos>=maxsize-1)
{
cout<<"stack overflow\n";
exit(0);
}
else
{
tos++;
st[tos]=item;
}
}
int pop()
{
int item;
if(tos<0)
{
cout<<"stack underflow\n";
exit(0);
}
else
{
item=st[tos];
tos--;
}
return item;
}
void display()
{
for(int i=0;i<=tos;i++)
{
cout<<st[i]<<"\t";
}
}
};
int precedence(char ch)
{
if(ch=='(')
return 0;
if(ch=='-')
return 1;
if(ch=='+')
return 2;
if(ch=='*')
return 3;
if(ch=='/')
return 4;
return 7;
}
int main()
{
int flag=0;
stack obj(20);
char exp[20]="(4*5-6*(2+3-2))",oexp[20],ch,ch1;
int i=0;
while(exp[i]!='\0')
{
ch1=exp[i];
switch(ch1)
{
case '[':
case '(':
case '{':
obj.push(exp[i]);
break;
case ']':
case '}':
case ')':
ch=obj.peek();
if(ch=='[' && ch1==']'||ch=='{' && ch1=='}'||ch=='(' && ch1==')')
{
obj.pop();
}
else
{
cout<<"UNBALANCED\n";
exit(0);
}
}
i++;
}
if(obj.tos==-1)
{
flag=1;
cout<<"BALANCED\n";
}
else
{
flag=2;
cout<<"UNBALANCED\n";
}
////////////////////////converting infix to postfix;///////////////////////////////////////////
if(flag==1)
{
int j1=0,j2=0;
while(exp[j1]!='\0')
{
switch(exp[j1])
{
case '(':
obj.push(exp[j1]);
break;
case ')':
while(obj.peek()!='(')
{
oexp[j2++]=obj.pop();
}
obj.pop();
break;
case '/':
case '*':
case '+':
case '-':
if(precedence(exp[j1])<=precedence(obj.peek()))
{
oexp[j2++]=obj.pop();
}
obj.push(exp[j1]);
break;
default:
oexp[j2++]=exp[j1];
}
j1++;
}
oexp[j2]='\0';
}
i=0;
while(oexp[i]!='\0')
{
cout<<oexp[i];
i++;
}
///////////////////////////evaluating postfix expression/////////////////////////////////
cout<<endl<<endl;
i=0;
char a,b;
while(oexp[i]!='\0')
{
switch(oexp[i])
{
case '/':
case '*':
case '+':
case '-':
b=obj.pop();
a=obj.pop();
if(oexp[i]=='/')
obj.push(a/b);
if(oexp[i]=='*')
obj.push(a*b);
if(oexp[i]=='+')
obj.push(a+b);
if(oexp[i]=='-')
obj.push(a-b);
break;
default:
obj.push(oexp[i]);
}
i++;
}
int res=obj.pop();
cout<<"result is:"<<res;
return 0;
}
using namespace std;
class stack
{
char st[50];
int maxsize;
public:
int tos;
stack()
{
tos=-1;
maxsize=0;
}
stack(int size)
{
tos=-1;
maxsize=size;
}
char peek()
{
return (st[tos]);
}
void push(int item)
{
if(tos>=maxsize-1)
{
cout<<"stack overflow\n";
exit(0);
}
else
{
tos++;
st[tos]=item;
}
}
int pop()
{
int item;
if(tos<0)
{
cout<<"stack underflow\n";
exit(0);
}
else
{
item=st[tos];
tos--;
}
return item;
}
void display()
{
for(int i=0;i<=tos;i++)
{
cout<<st[i]<<"\t";
}
}
};
int precedence(char ch)
{
if(ch=='(')
return 0;
if(ch=='-')
return 1;
if(ch=='+')
return 2;
if(ch=='*')
return 3;
if(ch=='/')
return 4;
return 7;
}
int main()
{
int flag=0;
stack obj(20);
char exp[20]="(4*5-6*(2+3-2))",oexp[20],ch,ch1;
int i=0;
while(exp[i]!='\0')
{
ch1=exp[i];
switch(ch1)
{
case '[':
case '(':
case '{':
obj.push(exp[i]);
break;
case ']':
case '}':
case ')':
ch=obj.peek();
if(ch=='[' && ch1==']'||ch=='{' && ch1=='}'||ch=='(' && ch1==')')
{
obj.pop();
}
else
{
cout<<"UNBALANCED\n";
exit(0);
}
}
i++;
}
if(obj.tos==-1)
{
flag=1;
cout<<"BALANCED\n";
}
else
{
flag=2;
cout<<"UNBALANCED\n";
}
////////////////////////converting infix to postfix;///////////////////////////////////////////
if(flag==1)
{
int j1=0,j2=0;
while(exp[j1]!='\0')
{
switch(exp[j1])
{
case '(':
obj.push(exp[j1]);
break;
case ')':
while(obj.peek()!='(')
{
oexp[j2++]=obj.pop();
}
obj.pop();
break;
case '/':
case '*':
case '+':
case '-':
if(precedence(exp[j1])<=precedence(obj.peek()))
{
oexp[j2++]=obj.pop();
}
obj.push(exp[j1]);
break;
default:
oexp[j2++]=exp[j1];
}
j1++;
}
oexp[j2]='\0';
}
i=0;
while(oexp[i]!='\0')
{
cout<<oexp[i];
i++;
}
///////////////////////////evaluating postfix expression/////////////////////////////////
cout<<endl<<endl;
i=0;
char a,b;
while(oexp[i]!='\0')
{
switch(oexp[i])
{
case '/':
case '*':
case '+':
case '-':
b=obj.pop();
a=obj.pop();
if(oexp[i]=='/')
obj.push(a/b);
if(oexp[i]=='*')
obj.push(a*b);
if(oexp[i]=='+')
obj.push(a+b);
if(oexp[i]=='-')
obj.push(a-b);
break;
default:
obj.push(oexp[i]);
}
i++;
}
int res=obj.pop();
cout<<"result is:"<<res;
return 0;
}
Comments