Assignment No.2 - DSAL
Assignment No.2 - DSAL
#include<ctype.h>
#include<string.h>
#include<math.h>
using namespace std;
class stack
{
struct node
{
float data;
node *next;
};
node *top;
public:
stack()
{
top=NULL;
}
int empty();
void push(float);
float pop();
int Top();
};
int stack::empty()
{
if(top==NULL)
return 1;
else
return 0;
}
void stack::push(float x)
{ node *newnode;
newnode=new node;
newnode->data=x;
newnode->next=top;
top=newnode;
}
float stack::pop()
{
node *temp=top;
float x;
top=top->next;
x=temp->data;
delete temp;
return x;
}
int stack::Top()
{ return top->data; }
return P;
}
while(!s.empty())
post[j++]=s.pop();
post[j]='\0';
cout<<"\nPostfix Expression=>";
cout<<post;
}
void infixtoprefix(char infix[20])
{
char prefix[20],x,token;
int i,j=0;
stack S;
for(i=strlen(infix)-1;i>=0;i--)
{
token=infix[i];
if(isalnum(token))
prefix[j++]=token;
else
if(token == ')')
S.push(token);
else
if(token == '(')
while((x=S.pop())!=')')
prefix[j++]=x;
else
{
while(!S.empty() && Priority(token)<Priority(S.Top()))
{
prefix[j++]=S.pop();
}
S.push(token);
}
}
while(!S.empty())
prefix[j++]=S.pop();
prefix[j]='\0';
cout<<"\nPrefix Expression=>";
for(i=strlen(prefix)-1;i>=0;i--)
cout<<prefix[i];
}
int main()
{
int ch;
char infix[20],post[20],prefix[20];
do
{
cout<<"\n\nSELECT THE CHOICE FROM BELOW\n1:Infix to Postfix\n2:Infix to Prefix\
n3:Postfix Evaluation\n4:Prefix Evaluation";
cout<<"\nEnter your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter infix Expression=>";
cin>>infix;
infixtopostfix(infix);
break;
case 2:
cout<<"\nEnter infix Expression=>";
cin>>infix;
infixtoprefix(infix);
break;
case 3:
cout<<"\nEnter Postfix Expression=>";
cin>>post;
cout<<"\nEvaluated Result=> " <<Postfix_Evaluation(post);
break;
case 4:
cout<<"\nEnter Prefix Expression=>";
cin>>prefix;
cout<<"\nEvaluated Result=> " <<Prefix_Evaluation(prefix);
break;
}
}while(ch!=5);
}
/*output __
Postfix Expression=>ABC*+D+
Prefix Expression=>/*+AB+CDE
Evaluated Result=>
Enter the value of A=>2
Evaluated Result=>
Enter the value of E=>3
*/