#include<iostream>
#include<cctype>
class stack
{
public:
int tp,b,max,z;
char x[10];
int size();
int top();
int isempty();
int isfull();
void push(int b);
int pop();
stack(int c)
{
c=max;
tp=-1;
}
};
int stack::pop()
{
if(!isempty())
tp--;
}
int stack::top()
{
if(!isempty()){
z=x[tp+1];
return z;}
}
int stack::isempty()
{
if(tp<-1)
return 1;
else return 0;
}
int stack::size()
{
return (tp+2);
}
void stack::push(int b)
{
if(!isfull())
x[++tp]=b;
}
int stack::isfull()
{
if(tp>=max-1)
return 1;
else return 0;
}
using namespace std;
int main()
{
int i=0,a,d,x[10];
char c[10];
stack s(10);
cout<<"\n enter the post fix expresion to be solved";
cin.getline(c,10);
while(c[i]!='\0')
{
if (c[i]>=48&&c[i]<=57)
s.push(c[i]-48);
else if(s.size()>=2)
{
a=s.top();
s.pop();
d=s.top();
s.pop();
if(c[i]=='*')
d*=a;
else if(c[i]=='/')
d/=a;
else if(c[i]=='+')
d+=a;
else if(c[i]=='-')
d-=a;
s.push(d);
}
i++;}
cout<<s.top();
return 0;
}
#include<cctype>
class stack
{
public:
int tp,b,max,z;
char x[10];
int size();
int top();
int isempty();
int isfull();
void push(int b);
int pop();
stack(int c)
{
c=max;
tp=-1;
}
};
int stack::pop()
{
if(!isempty())
tp--;
}
int stack::top()
{
if(!isempty()){
z=x[tp+1];
return z;}
}
int stack::isempty()
{
if(tp<-1)
return 1;
else return 0;
}
int stack::size()
{
return (tp+2);
}
void stack::push(int b)
{
if(!isfull())
x[++tp]=b;
}
int stack::isfull()
{
if(tp>=max-1)
return 1;
else return 0;
}
using namespace std;
int main()
{
int i=0,a,d,x[10];
char c[10];
stack s(10);
cout<<"\n enter the post fix expresion to be solved";
cin.getline(c,10);
while(c[i]!='\0')
{
if (c[i]>=48&&c[i]<=57)
s.push(c[i]-48);
else if(s.size()>=2)
{
a=s.top();
s.pop();
d=s.top();
s.pop();
if(c[i]=='*')
d*=a;
else if(c[i]=='/')
d/=a;
else if(c[i]=='+')
d+=a;
else if(c[i]=='-')
d-=a;
s.push(d);
}
i++;}
cout<<s.top();
return 0;
}
No comments:
Post a Comment