Question
Hello, I need help in evaluating a boolean postfix expression.The EvaluateBooleanPostfixExpressionaccepts a string( that is already in postfix form) and it is supposed to evaluate
Hello,
I need help in evaluating a boolean postfix expression.The EvaluateBooleanPostfixExpressionaccepts a string( that is already in postfix form) and it is supposed to evaluate it. I am stuck and need help in the evaluation part of the postfix expression where I am supposed to push the chars T and F into the stack and then pop the operators and then finally evaluate the expression. Thank you for your time and for your help in advance
The goal is to evaluate T and F expressions with AND(*), OR(+), and NOT(!). convert the input logical expression to postfix and then and then evaluate the expressions to evaluate the truth value of the logic expression now in postfix form.
(F+(!!F+(F)+!(T+T+!F+F+T)+F+F+F*T+F+F)+!F*F)*(!T+(T)) = F
T+!(T+T)*(T)*(!F)*T*T+!T+!T+F+T*!!F*!F*(F+!F*F)*F+F = T
(T*(!F*(F+T*!(F+T)+T*(!F*!!T+F*F))+F)+!(F+T)+F)*(T+!!F) = T
The code is below :
#include
#include
#include
#include
#include
#include
using namespace std;
//Function to return precedence of operators
int prec(char c)
{
if (c == '!')
return 3;
else if (c == '*')
return 2;
else if (c == '+')
return 1;
else
return -1;
}
string Postfix(string s)
{
std::stack
st.push('L');
int l = s.length();
string ns;
for (int i = 0; i < l; i++)
{
// If the scanned character is an operand, add it to output string.
if ((s[i] == 'T') || (s[i] == 'F'))
ns += 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() != 'L' && st.top() != '(')
{
char c = st.top();
st.pop();
ns += c;
}
if (st.top() == '(')
{
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else {
while (st.top() != 'L' && prec(s[i]) <= prec(st.top()))
{
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while (st.top() != 'L')
{
char c = st.top();
st.pop();
ns += c;
}
cout << ns << endl;
return ns;
}
char EvaluateBooleanPostFixExpression(string x)
{
int n = x.length();
char Op1, char Op2, char symbol;
// create a stack the size of the string expression,
stack
/*
Evaluate the postfix expression here with the operators
Traverse all operands by jumping
a character after every iteration. (scan the characters)
*/
for (int i = 0; i < n; i += 2) {
// if a scanned character is an operand, push to the stack ( Operands are T or F)
if (IsOperand(n[i]))
{
// Extract the T or F from the string but keep incrementing i
// as long as they are T or F
}
// if the scanned character is an operator, then pop two elements from the stack and apply the operator
// the operators are + for OR, * for AND, ! for NOT
// the following parts evaluate the boolean expression
// If symbol next to current operand
// is AND.
if (symbol == '*') {
if (Op1 == 'F' || Op2 == 'F')
return 'F';
else
return 'T';
}
// If operator next to current operand
// is OR.
if (symbol == '+') {
if (Op1 == 'T' || Op2 == 'T')
return 'T';
else
return 'F';
}
if (symbol == '!')
{
if (Op1 == 'T')
return 'F';
else
return 'T';
}
}
}
bool IsOperand(char Operand)
{
if(Operand == 'T' && Operand == 'F')
return true;
else
return false;
}
bool IsOperator(char Operator)
{
if(Operator == '!' && Operator == '*' && Operator =='+')
return true;
else
return false;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started