#include<iostream>
using namespace std;
class stacktype
{
private:
char num[10];
char ch;
int arr[100];
int stacktop;
int a;
int var1,var2,var3;
public:
stacktype()
{
stacktop=-1;
}
void input();
void scan();
void push(int ch);
void pop();
void calculation(int var1,int var2);
void print();
};
void stacktype:: input()
{
cout<<"enter experation"<<endl;
cin>>num;
}
void stacktype:: scan()
{
for(int i=0;num[i]!='\0';i++)
{
ch=num[i];
if(ch=='1'||ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='6'||ch=='7'||ch=='8'||ch=='9'||ch=='0')
{
if(ch=='1')
push(1);
else if(ch=='2')
push(2);
else if(ch=='3')
push(3);
else if(ch=='4')
push(4);
else if(ch=='5')
push(5);
else if(ch=='6')
push(6);
else if(ch=='7')
push(7);
else if(ch=='8')
push(8);
else if(ch=='9')
push(9);
else
push(0);
}
else if(ch!='+'||ch!='-'||ch!='/'||ch!='*')
{
pop();
}
}
}
void stacktype::push(int v)
{
stacktop++;
arr[stacktop]=(int)v;
cout<<arr[stacktop]<<endl;
}
void stacktype::pop()
{
var1=arr[stacktop];
stacktop--;
var2=arr[stacktop];
stacktop--;
calculation( var1,var2);
}
void stacktype::calculation(int var1,int var2)
{
switch(ch)
{
case'+':
{
var3=var1+var2;
push(var3);
break;
}
case'-':
{
var3=var1-var2;
push(var3);
break;
}
case'*':
{
var3=var1*var2;
push(var3);
break;
}
case'/':
var3=(int)var1/(int)var2;
push(var3);
break;
default:
cout<<"invalid case"<<endl;
}
}
void stacktype::print()
{
for(int i=0;i<=stacktop;i++)
cout<<arr[i]<<endl;
}
int main()
{
stacktype obj;
obj.input();
obj.scan();
obj.print();
return 0;
}
Great job......
ReplyDeleteThis code is really helpful for me to understand postfix solution
ReplyDeleteTHANKS
thankyou
ReplyDelete