### Implement C++ program for expression conversion as infix to postfix and its evaluation using stack based on given conditions i. Operands and operator, both must be single character. ii. Input Postfix expression must be in a desired format. iii. Only '+', '-', '*' and '/ ' operators are expected.

Implement C++ program for expression conversion as infix to postfix and its evaluation

using stack based on given conditions

i. Operands and operator, both must be single character.

ii. Input Postfix expression must be in a desired format.

iii. Only '+', '-', '*' and '/ ' operators are expected.

#include<iostream>

using namespace std;

// For Char Stack

char stk[10];

int size=10;

int top=-1;

char postfix[20];

// For Int Stack

int top1=-1;

int size1=10;

int stk1[10];

// For Char Stack

int stk_empty()

{

if(top==-1)

return 1;

else

return 0;

}

int stk_full()

{

if(top==size-1)

return 1;

else

return 0;

}

void push(char ch)

{

top ++;

stk[top]=ch;

}

char pop()

{

char x = stk[top];

top --;

return x;

}

void display()

{

for(int i=0;i<=top;i++)

cout<<" "<<stk[i];

}

void infix_to_postfix()

{

char ch;

char infix[20];

int i=0,j=0;

cout<<"Enter an infix expression: ";

cin>>infix;

for(i=0;infix[i]!='\0';i++)

{

if(infix[i]=='(' || infix[i]=='[' || infix[i]=='{')

{

if(!stk_full())

push(infix[i]);

}

if(infix[i] == ')' || infix[i] == ']' || infix[i] == '}' )

{

ch = pop();

while(ch!='(' && ch!='[' && ch!='{' )

{

postfix[j] = ch;

j++;

ch = pop();

}

}

if(infix[i] == '1' || infix[i] == '2' || infix[i] == '3' || infix[i] == '4' || infix[i] == '5' || infix[i] == '6' || infix[i] == '7' || infix[i] == '8' || infix[i] == '9')

{

postfix[j] = infix[i];

j++;

}

if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' )

{

if(!stk_full())

push(infix[i]);

}

}

while(!stk_empty())

{

postfix[j] = pop();

j++;

}

postfix[j] = '\0';

cout<<" \n\n Postfix Expression is :: "<<postfix;

}

// For Int Stack

int stk1_empty()

{

if(top1==-1)

return 1;

else

return 0;

}

int stk1_full()

{

if(top1==size1-1)

return 1;

else

return 0;

}

void push1(char ch)

{

top1 ++;

stk1[top1]=ch;

}

char pop1()

{

char x = stk1[top1];

top1 --;

return x;

}

// ********** Postfix Evaluation ****************

int postfix_evaluation()

{

int i, op1, op2;

char ch;

for(i=0;postfix[i] != '\0'; i++)

{

ch = postfix[i];

if(ch == '+' || ch == '-' || ch == '*' || ch == '/')

{

op2 = pop1();

op1 = pop1();

switch(ch)

{

case '+': push1(op1 + op2);

break;

case '-': push1(op1 - op2);

break;

case '*': push1(op1 * op2);

break;

case '/': push1(op1 / op2);

break;

}

}

else

push1(ch - '0');

}

cout<<"Answer is : "<<stk1[top1];

}

int main()

{

cout<<"\t********* Infix to postfix Conversion *********\n\n";

infix_to_postfix();

cout<<"\n\n";

cout<<"\t********* Postfix Evaluation *********\n\n";

postfix_evaluation();

cout<<"\n\n";

return 0;

}

using stack based on given conditions

i. Operands and operator, both must be single character.

ii. Input Postfix expression must be in a desired format.

iii. Only '+', '-', '*' and '/ ' operators are expected.

OUTPUT FOR THE PROGRAM |

using namespace std;

// For Char Stack

char stk[10];

int size=10;

int top=-1;

char postfix[20];

// For Int Stack

int top1=-1;

int size1=10;

int stk1[10];

// For Char Stack

int stk_empty()

{

if(top==-1)

return 1;

else

return 0;

}

int stk_full()

{

if(top==size-1)

return 1;

else

return 0;

}

void push(char ch)

{

top ++;

stk[top]=ch;

}

char pop()

{

char x = stk[top];

top --;

return x;

}

void display()

{

for(int i=0;i<=top;i++)

cout<<" "<<stk[i];

}

void infix_to_postfix()

{

char ch;

char infix[20];

int i=0,j=0;

cout<<"Enter an infix expression: ";

cin>>infix;

for(i=0;infix[i]!='\0';i++)

{

if(infix[i]=='(' || infix[i]=='[' || infix[i]=='{')

{

if(!stk_full())

push(infix[i]);

}

if(infix[i] == ')' || infix[i] == ']' || infix[i] == '}' )

{

ch = pop();

while(ch!='(' && ch!='[' && ch!='{' )

{

postfix[j] = ch;

j++;

ch = pop();

}

}

if(infix[i] == '1' || infix[i] == '2' || infix[i] == '3' || infix[i] == '4' || infix[i] == '5' || infix[i] == '6' || infix[i] == '7' || infix[i] == '8' || infix[i] == '9')

{

postfix[j] = infix[i];

j++;

}

if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' )

{

if(!stk_full())

push(infix[i]);

}

}

while(!stk_empty())

{

postfix[j] = pop();

j++;

}

postfix[j] = '\0';

cout<<" \n\n Postfix Expression is :: "<<postfix;

}

// For Int Stack

int stk1_empty()

{

if(top1==-1)

return 1;

else

return 0;

}

int stk1_full()

{

if(top1==size1-1)

return 1;

else

return 0;

}

void push1(char ch)

{

top1 ++;

stk1[top1]=ch;

}

char pop1()

{

char x = stk1[top1];

top1 --;

return x;

}

// ********** Postfix Evaluation ****************

int postfix_evaluation()

{

int i, op1, op2;

char ch;

for(i=0;postfix[i] != '\0'; i++)

{

ch = postfix[i];

if(ch == '+' || ch == '-' || ch == '*' || ch == '/')

{

op2 = pop1();

op1 = pop1();

switch(ch)

{

case '+': push1(op1 + op2);

break;

case '-': push1(op1 - op2);

break;

case '*': push1(op1 * op2);

break;

case '/': push1(op1 / op2);

break;

}

}

else

push1(ch - '0');

}

cout<<"Answer is : "<<stk1[top1];

}

int main()

{

cout<<"\t********* Infix to postfix Conversion *********\n\n";

infix_to_postfix();

cout<<"\n\n";

cout<<"\t********* Postfix Evaluation *********\n\n";

postfix_evaluation();

cout<<"\n\n";

return 0;

}

For more such posts click the link:-http://svencrai.com/G8W

## Comments

## Post a Comment