In: Computer Science
Please Use the STL library for this question and Should be done in C++ and Provide screenshots of the OUTPUT
Topic: Stack
Infix to postfix conversion
Take Input mathematical
expression in infix notation, and convert it into a postfix
notation. The infix notation may or may not have round
parenthesis.
Postfix expression evaluation
Take Input mathematical
expression in postfix form, evaluate it, and display the result
The mathematical expression can contain
Numbers as operands (numbers can be of single or multiple
digits)
Operators (+ , - , * , / , ^ )
Round parenthesis
^ exponent operator
In postfix notation, operands should be separated from each
other with a space to avoid any
confusion. For example (32 – 4) when converted to postfix notation
should NOT be written as
324-, but as 32 4 -
/* C++ implementation to convert infix expression to
postfix*/
// Note that here we use std::stack for Stack operations STL
library
#include<bits/stdc++.h>
using namespace std;
//Function to return precedence of operators
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
int evaluatePostfix(char* exp)
{
//cout<<exp;
// Create a stack of capacity equal to expression size
std::stack<int> st;
int i;
// See if stack was created successfully
// Scan all characters one by one
for (i = 0; exp[i]; ++i)
{
// If the scanned character is an operand (number here),
// push it to the stack.
if (isdigit(exp[i]))
st.push(exp[i] - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = st.top();
st.pop();
int val2 = st.top();
st.pop();
switch (exp[i])
{
case '+': st.push(val2 + val1); break;
case '-': st.push(val2 - val1); break;
case '*': st.push(val2 * val1); break;
case '/': st.push(val2/val1); break;
}
}
}
return st.top();
}
// The main function to convert infix expression
//to postfix expression
void infixToPostfix(string s)
{
// cout<<"kk";
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
int i;
string postfix_exp;
char char_array[l + 1];
//cout<<"gg";
for(int i = 0; i < l; i++)
{ // cout<<i;
// If the scanned character is an
operand, add it to output string.
if((s[i] >= '0' && s[i]
<= '9')) {
ns=ns+" "+s[i];
postfix_exp =
postfix_exp+s[i];
}
// If the scanned character is an
‘(‘, push it to the stack.
else if(s[i] == '(')
st.push('(');
// If the scanned character is an
‘)’, pop and to output string from the stack
// until an ‘(‘ is
encountered.
else if(s[i] == ')')
{
while(st.top()
!= 'N' && st.top() != '(')
{
char c = st.top();
st.pop();
ns =ns+" "+
c;
postfix_exp =
postfix_exp+c;
}
if(st.top() ==
'(')
{
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else{
while(st.top()
!= 'N' && prec(s[i]) <= prec(st.top()))
{
char c = st.top();
st.pop();
ns =ns+" "+ c;
postfix_exp = postfix_exp+c;
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while(st.top() != 'N')
{
char c = st.top();
st.pop();
ns = ns+ " "+c;
postfix_exp = postfix_exp+c;
}
cout << ns << endl;
strcpy(char_array, postfix_exp.c_str());
cout<<"postfix evaluation: "<< evaluatePostfix(char_array);
}
//Driver program to test above functions
int main()
{
string exp = "3+4*5/6";
infixToPostfix(exp);
return 0;
}
Hope i helped you.