You are on page 1of 9

Submitted By: Submitted To:

Syeda Mehr un Nisa Sir Hussain ghoto


Subject
Data Structures and Algorithms
Roll No: Class:
010 BSIT-3rd-A

DEPARTMENT
INFORMATION TECHNOLOGY
MINHAJ UNIVERSITY
Data Structures and Algorithms
Write a program to convert an infix expression that includes (,), +, -, *, and /to postfix.
Add the exponentiation operator to your repertoire.
Write a program to convert a postfix expression to infix.

Answers:
1-Write a program to convert an infix expression that includes (,), +, -, *, and /to postfix.
INPUT:
#include<iostream>
#include<stack>
using namespace std;
int precedence_fun(char c)
{
if(c=='*' || c=='/')
return 2;
else if (c=='+' || c=='-')
return 1;
else
return -1;
}
int infix_to_postfix(string s)
{
std::stack<char> st;
st.push('N');
int len= s.length();
string ns;
for(int i=0; i<len; i++)
{
if((s[i]>='a'&&s[i]<='z') || (s[i]>='A'&& s[i]<='Z'))
ns+=s[i];
else if(s[i]=='(')
st.push('(');
else if(s[i]==')')
{
while (st.top() != 'N'&& st.top() !='(')
{
char c=st.top();
st.pop();
ns+=c;
}
if(st.top()=='(')
{
char c= st.top();
st.pop();
}
}
else
{
while(st.top()!='N'&& precedence_fun(s[i])<= precedence_fun(st.top()))
{
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
while (st.top()!='N')
{
char c = st.top();
st.pop();
ns += c;
}
cout<<ns<<endl;
}
int main()
{
string exps = "a+b*c-d";
infix_to_postfix(exps);
return 0;
}

OUTPUT:
2-Add the exponentiation operator to your repertoire.

To add the exponentiation operator to the repertoire we just add one more operator in
precedence_fun. And the rest of program remains the same.
INPUT:
#include<iostream>
#include<stack>
using namespace std;
int precedence_fun(char c)
{
if(c == '^')
return 3;
else if (c=='*' || c=='/')
return 2;
else if (c=='+' || c=='-')
return 1;
else
return -1;
}
int infix_to_postfix(string s)
{
std::stack<char> st;
st.push('N');
int len= s.length();
string ns;
for(int i=0; i<len; i++)
{
if((s[i]>='a'&&s[i]<='z') || (s[i]>='A'&& s[i]<='Z'))
ns+=s[i];
else if(s[i]=='(')
st.push('(');
else if(s[i]==')')
{
while (st.top() != 'N'&& st.top() !='(')
{
char c=st.top();
st.pop();
ns+=c;
}
if(st.top()=='(')
{
char c= st.top();
st.pop();
}
}
else
{
while(st.top()!='N'&& precedence_fun(s[i])<= precedence_fun(st.top()))
{
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
while (st.top()!='N')
{
char c = st.top();
st.pop();
ns += c;
}
cout<<ns<<endl;
}
int main()
{
string exps = "a+b*c-d";
infix_to_postfix(exps);
return 0;
}

OUTPUT:
3-Write a program to convert a postfix expression to infix.
INPUT:
#include<iostream>
#include<stack>
using namespace std;
bool isOperand(char x)
{
return(x>='a' && x<='z') || (x>='A' && x<='Z');
}
string postfix_to_infix(string exp)
{
stack<string>s;
for(int i=0; exp[i]!='\0'; i++)
{
if(isOperand(exp[i]))
{
string op(1,exp[i]);
s.push(op);
}
else
{
string op1=s.top();
s.pop();
string op2=s.top();
s.pop();
s.push("("+op2+exp[i]+op1+")");
}
}
return s.top();
}
int main()
{
string exp="ab*c+";
cout<<postfix_to_infix(exp);
return 0;
}
OUTPUT:

You might also like