DSA2
DSA2
DSA2
h>
#include<iostream>
#include<string.h>
#define max 30
using namespace std;
struct node
{
char data;
struct node* next;
};
class stack
{
node* top;
char x;
public:
stack()
{
top = NULL;
}
int empty()
{
if (top == NULL)
{
return(1);
}
else
{
return(0);
}
}
void push(char x)
{
node* p;
p = new node;
p->data = x;
p->next = top;
top = p;
}
char pop()
{
if (!empty())
{
node* p;
p = new node;
p = top;
top = top->next;
x = p->data;
delete p;
return(x);
}
else
{
cout << "stack is empty" << endl;
return(0);
}
}
char pop1()
{
if (!empty())
{
node* p;
p = new node;
p = top;
//top=top->next;
x = p->data;
//delete p;
return(x);
}
else
{
cout << "stack is empty" << endl;
return(0);
}
}
};
int precedence(char x);
void infix_to_prefix(char infix[], char prefix[]);
void infix_to_postfix(char infix[], char postfix[]);
void eval_prefix(char prefix[]);
void eval_postfix(char postfix[]);
int evaluate(char x, int op1, int op2);
int main()
{
char infix[30], prefix[30], postfix[30];
int op;
do {
cout<<endl<<"**************************************************";
cout<<endl<<"\t\t"<<"MAIN MENU";
cout<<endl<<"**************************************************";
cout<<endl<<"|\t"<<"1. Infix to Prefix with Evaluation |";
cout<<endl<<"|\t"<<"2. Infix to Postfix with Evaluation |";
cout<<endl<<"|\t"<<"3. Exit |";
cout<<endl<<"***************************************************";
cout<<endl<<"Enter your choice ";
cin>>op;
cout<<"........................................................."<<endl;
switch (op)
{
case 1:
cout << "\nEnter the infix expression::" << endl;
cin >> infix;
infix_to_prefix(infix, prefix);
cout << "\nPrefix expression is " << prefix << endl;
cout << "\n\nEvaluation of Prefix expression";
eval_prefix(prefix);
break;
case 2:
cout << "\nEnter the infix expression::" << endl;
cin >> infix;
infix_to_postfix(infix, postfix);
cout << "\nPostfix expression is " << postfix << endl;
cout << "\n\nEvaluation of Postfix expression";
eval_postfix(postfix);
break;
}
} while (op != 3);
return 0;
}
void infix_to_prefix(char infix[], char prefix[])
{
int i, j;
char temp, in1[30];
cout << "\n\n After step 1...\nEntered infix is...";
for (i = 0; i <= strlen(infix) - 1; i++)
{
cout << infix[i];
}
for (i = strlen(infix) - 1, j = 0; i >= 0; i--, j++)
in1[j] = infix[i];
in1[j] = '\0';
int precedence(char x)
{
if (x == '(')
{
return(0);
}
if (x == '+' || x == '-')
{
return(1);
}
if (x == '*' || x == '/' || x == '%')
{
return(2);
}
return(3);
}