#include<iostream.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class stack
{
double x[10],TOP_p,TOP_e,TOP_s,i,j,t;
char exp[10][10],p[10][10],stack[10][10];
public:
stack();
void pop();
void push();
double check();
void intopre();
void show();
};
stack::stack()
{
TOP_p=TOP_s=TOP_e=0;
}
void stack::pop()
{
if(TOP_s<1)
{
cout<<"\nSORRY!Stack Underflow....";
exit(0);
}
TOP_p++;
strcpy(p[TOP_p],stack[TOP_s]);
TOP_s--;
cout<<"\nElement in Polish.:\t";
for(int t=TOP_p;t>=1;t--)
cout<<" "<<p[t];
}
void stack::push()
{
if(TOP_s==10)
{
cout<<"\nSORRY!Stack Overflow....";
exit(0);
}
TOP_s++;
strcpy(stack[TOP_s],exp[TOP_e]);
TOP_e--;
cout<<"\nElement in Stack.:\t";
for(int t=1;t<=TOP_s;t++)
cout<<" "<<stack[t];
}
double stack::check()
{
double s,e,t;
//stack[st] Vs exp[et]
if(TOP_s<1)return 1;
switch(stack[TOP_s][0])
{
case '^':
s=3;
break;
case '*':
case '/':
s=2;
break;
case '+':
case '-':
s=1;
break;
default:
s=4;
}
switch(exp[TOP_e][0])
{
case '^':
e=3;
break;
case '*':
case '/':
e=2;
break;
case '+':
case '-':
e=1;
break;
default:
e=4;
}
if(e>=s) return 1; //push
else return 0; //pop
}
void stack::intopre()
{
cout<<"Enter exp";
int i=0;
do
{
i++;
cin>>exp[i];
}while(exp[i][0]!='#');
puts("\n-------------------------------------------------");
TOP_e=i-1;
push();
while(TOP_e>=0)
{
i=check();
if(i==1)
push();
else
{
while(!i)
{
pop();
i=check();
}
push();
}
}
while(TOP_s>=1)
{
TOP_p++;
strcpy(p[TOP_p],stack[TOP_s]);
TOP_s--;
}
cout<<"\nElement in pol.:\t";
for(i=TOP_p;i>=1;i--)
cout<<" "<<p[i];
}
void stack::show()
{
cout<<"\nExpression in Prefix Form.:\n\n";
for(i=TOP_p;i>=1;i--)
cout<<" "<<p[i];
}
void main()
{
clrscr();
stack x;
puts("===================================================");
cout<<"Enter Expression in INFIX FORM.\n";
cout<<"Enter '#' at the End of Expression.\n";
puts("===================================================");
x.intopre();
puts("\n-------------------------------------------------");
x.show();
puts("\n-------------------------------------------------");
getch();
}
#include<process.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class stack
{
double x[10],TOP_p,TOP_e,TOP_s,i,j,t;
char exp[10][10],p[10][10],stack[10][10];
public:
stack();
void pop();
void push();
double check();
void intopre();
void show();
};
stack::stack()
{
TOP_p=TOP_s=TOP_e=0;
}
void stack::pop()
{
if(TOP_s<1)
{
cout<<"\nSORRY!Stack Underflow....";
exit(0);
}
TOP_p++;
strcpy(p[TOP_p],stack[TOP_s]);
TOP_s--;
cout<<"\nElement in Polish.:\t";
for(int t=TOP_p;t>=1;t--)
cout<<" "<<p[t];
}
void stack::push()
{
if(TOP_s==10)
{
cout<<"\nSORRY!Stack Overflow....";
exit(0);
}
TOP_s++;
strcpy(stack[TOP_s],exp[TOP_e]);
TOP_e--;
cout<<"\nElement in Stack.:\t";
for(int t=1;t<=TOP_s;t++)
cout<<" "<<stack[t];
}
double stack::check()
{
double s,e,t;
//stack[st] Vs exp[et]
if(TOP_s<1)return 1;
switch(stack[TOP_s][0])
{
case '^':
s=3;
break;
case '*':
case '/':
s=2;
break;
case '+':
case '-':
s=1;
break;
default:
s=4;
}
switch(exp[TOP_e][0])
{
case '^':
e=3;
break;
case '*':
case '/':
e=2;
break;
case '+':
case '-':
e=1;
break;
default:
e=4;
}
if(e>=s) return 1; //push
else return 0; //pop
}
void stack::intopre()
{
cout<<"Enter exp";
int i=0;
do
{
i++;
cin>>exp[i];
}while(exp[i][0]!='#');
puts("\n-------------------------------------------------");
TOP_e=i-1;
push();
while(TOP_e>=0)
{
i=check();
if(i==1)
push();
else
{
while(!i)
{
pop();
i=check();
}
push();
}
}
while(TOP_s>=1)
{
TOP_p++;
strcpy(p[TOP_p],stack[TOP_s]);
TOP_s--;
}
cout<<"\nElement in pol.:\t";
for(i=TOP_p;i>=1;i--)
cout<<" "<<p[i];
}
void stack::show()
{
cout<<"\nExpression in Prefix Form.:\n\n";
for(i=TOP_p;i>=1;i--)
cout<<" "<<p[i];
}
void main()
{
clrscr();
stack x;
puts("===================================================");
cout<<"Enter Expression in INFIX FORM.\n";
cout<<"Enter '#' at the End of Expression.\n";
puts("===================================================");
x.intopre();
puts("\n-------------------------------------------------");
x.show();
puts("\n-------------------------------------------------");
getch();
}
No comments:
Post a Comment