Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need help in evaluating a postfix expression in a function.I am working on a program that evaluates a string of boolean expressions T

Hello,

I need help in evaluating a postfix expression in a function.I am working on a program that evaluates a string of boolean expressions T or F using AND, OR, and NOT.

I am stuck in the evaluation of the postfix exrpression functinon, EvaluateBooleanPostFixExpression(string x) by using the stack functions to do the postfix expression evaluation. Please help me with this, thank you for your help and time in advance

#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;

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;

}

// This function should receive the string passed from the Postfix function and evaluate the expression, returning true or false

char EvaluateBooleanPostFixExpression(string x)

{

int n = x.length();

char Op1, char Op2, char symbol;

// create a stack the size of the string expression,

// scan the characters

// if a scanned character is an operand, push to the stack ( Operands 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

/*

Evaluate the postfix expression here with the operators

Traverse all operands by jumping

a character after every iteration.

*/

for (int i = 0; i < n; i += 2) {

// 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';

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

7. Where Do We Begin?

Answered: 1 week ago