You are on page 1of 10

Name: Atharv Joshi

Class: IT-A
Roll Number: 201106013
Date: 25/11/2021

Experiment 2

Aim: Conversion and Evaluation


a. Implement program to convert infix to postfix and evaluate
expression.
b. Implement program to convert infix to prefix and evaluate
expression.
Theory:
One of the applications of Stack is in the conversion of arithmetic expressions in
high-level programming languages into machine readable form. As our
computer system can only understand and work on a binary language, it
assumes that an arithmetic operation can take place in two operands only
e.g., A+B, C*D,D/A etc. But in our usual form an arithmetic expression may
consist of more than one operator and two operands e.g. (A+B) *C(D/(J+D)).

These complex arithmetic operations can be converted into polish notation


using stacks which then can be executed in two operands and an operator form.

Infix Expression

It follows the scheme of <operand><operator><operand> i.e., an


<operator> is preceded and succeeded by an <operand>. Such an expression is
termed infix expression. E.g., A+B

Postfix Expression

It follows the scheme of <operand><operand><operator> i.e., an


<operator> is succeeded by both the <operand>. E.g., AB+
Algorithm to convert Infix to Postfix

Let, X is an arithmetic expression written in infix notation. This algorithm finds


the equivalent postfix expression Y.

1. Push “(“onto Stack, and add “)” to the end of X.


2. Scan X from left to right and repeat Step 3 to 6 for each element of X until
the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered, then:
1. Repeatedly pop from Stack and add to Y each operator (on the top
of Stack) which has the same precedence as or higher precedence
than operator.
2. Add operator to Stack.
[End of If]
6. If a right parenthesis is encountered, then:
1. Repeatedly pop from Stack and add to Y each operator (on the top
of Stack) until a left parenthesis is encountered.
2. Remove the left Parenthesis.
[End of If]
[End of If]
7. END.

Algorithm to convert infix to prefix

1. First, reverse the given infix expression.


2. Scan the characters one by one.
3. If the character is an operand, copy it to the prefix notation output.
4. If the character is a closing parenthesis, then push it to the stack.
5. If the character is an opening parenthesis, pop the elements in the stack
until we find the corresponding closing parenthesis.
6. If the character scanned is an operator

• If the operator has precedence greater than or equal to the top of the stack,
push the operator to the stack.
• If the operator has precedence lesser than the top of the stack, pop the
operator and output it to the prefix notation output and then check the
above condition again with the new top of the stack.
#include<iostream>

#include<string.h>

#include<math.h>

#include<algorithm>

using namespace std;

#define MAX 20

class stack{

private:

int top,a;

int arr[MAX];

public:

stack(){

top =-1;
}

void push(int a){

if (isfull())

cout<<"stack is full!!!\n";

else{

top++;

arr[top]=a;

void pop(){

if (isempty()){

cout<<"stack is empty!!!\n";

else{

top--;

int gettop(){

return arr[top];

bool isempty(){
if (top==-1)

return true;

else

return false;

bool isfull(){

if (top==MAX-1)

return true;

else

return false;

bool isoperator(char op){

if(op=='+'||op=='-'||op=='*'||op=='/'||op=='^')
return true;

else return false;

int priority(char c){

if(c=='^')

return 3;

else if(c=='/' or c=='*')

return 2;

else if(c=='-' or c=='+')

return 1;

else return -1;

int postfixevaluation(string s){

for(int i=0;i<s.length();i++){

if(isdigit(s[i])){

push(s[i]-'0');

else{

int op2=gettop();

pop();

int op1=gettop();
pop();

switch(s[i]){

case '+':push((op1+op2));break;

case '-':push((op1-op2));break;

case '*':push((op1*op2));break;

case '/':push((op1/op2));break;

case '^':push((pow(op1,op2)));break;

if(gettop()!=NULL)

return gettop();

else return -1;


}

int prefixevaluation(string s){

for(int i=s.length()-1;i>=0;i--){

if(isdigit(s[i])){

push(s[i]-'0');

else{

int op1=gettop();

pop();

int op2=gettop();

pop();

switch(s[i]){

case '+':push((op1+op2));break;

case '-':push((op1-op2));break;

case '*':push((op1*op2));break;

case '/':push((op1/op2));break;

case '^':push((pow(op1,op2)));break;

if(gettop()!=NULL)
return gettop();

else return -1;

string infixtopostfix(string inp){

string result;

for(int i=0;i<inp.length();i++){

if(isalpha(inp[i])||isdigit(inp[i])){

result+=inp[i];

}else if(inp[i]=='('){

push(inp[i]);

}else if(inp[i]==')'){

while(gettop()!='('){

result+=gettop();
pop();

pop();

}else{

while(!isempty() and priority(gettop())>=priority(inp[i])){

if(gettop()!='(' and gettop()!=')')

result+=gettop();

pop();

push(inp[i]);

while(!isempty()){

pop();

int res=postfixevaluation(result);

if(res<-150){

cout<<"\nNo numeric Solution\n\n";

}else

cout<<endl<<"Solution:"<<res<<endl<<endl;

return result;
}

string infixtoprefix(string inp){

string result;

for(int i=inp.length()-1;i>=0;i--){

if((isalpha(inp[i]))||isdigit(inp[i])){

result+=inp[i];

}else if(inp[i]==')'){

push(inp[i]);

}else if(inp[i]=='('){

while(gettop()!=')'){

result+=gettop();

pop();

}
pop();

}else{

while(!isempty() and priority(gettop())>=priority(inp[i])){

if(gettop()!='(' and gettop()!=')')

result+=gettop();

pop();

push(inp[i]);

while(!isempty()){

pop();

reverse(result.begin(),result.end());

int reso=prefixevaluation(result);

if(reso<-150){

cout<<"\nNo numeric Solution\n\n";

}else

cout<<endl<<"Solution:"<<reso<<endl<<endl;

return result;

}
};

int main(){

stack y;

int x;

do{

cout<<"1.Infix to Postfix"<<endl;

cout<<"2.Infix to Prefix"<<endl;

cout<<"0.Exit"<<endl;

cin>>x;

switch(x){

char c;
case 1: {

string str;

cout<<"Enter the Expression to convert into Postfix: ";

cin>>str;

cout<<"Postfix Expression:"<<endl<<y.infixtopostfix(str)<<endl<<endl;

break;

case 2:

string str;

cout<<"Enter the Expression to convert into Prefix: ";

cin>>str;

cout<<"Prefix Expression:"<<endl<<y.infixtoprefix(str)<<endl<<endl;

break;

case 0:

exit(1);

default :

cout<<"\nInvalid input!!"<<endl;

}while(x!=0);
}

7. After all the characters are scanned, reverse the prefix notation output.
CONCLUSION:
The C++ programs were successfully studied and
implemented.

You might also like