Question

C++ code: Problem 3. Convert the following infix expression to a prefix expression by Stack operation....

C++ code:

Problem 3. Convert the following infix expression to a prefix expression by Stack operation. A + B* C + (D^E) * F/G/H + I

Evaluate the value of prefix expression when A=5, B=10, C=3, D=12, E=3, F=5, G=8, H=4, I=100

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Source code*/


#include <bits/stdc++.h>
using namespace std;

bool isOperator(char c)
{
   return (!isalpha(c) && !isdigit(c));
}

int charPriority(char C)
{
   if (C == '-' || C == '+')
       return 1;
   else if (C == '*' || C == '/')
       return 2;
   else if (C == '^')
       return 3;
   return 0;
}

string infixToPostfix(string infix)
{
   infix = '(' + infix + ')';
   int l = infix.size();
   stack<char> char_stack;
   string output;

   for (int i = 0; i < l; i++) {

       // If the scanned character is an
       // operand, add it to output.
       if (isalpha(infix[i]) || isdigit(infix[i]))
           output += infix[i];

       // If the scanned character is an
       // ‘(‘, push it to the stack.
       else if (infix[i] == '(')
           char_stack.push('(');

       // If the scanned character is an
       // ‘)’, pop and output from the stack
       // until an ‘(‘ is encountered.
       else if (infix[i] == ')') {

           while (char_stack.top() != '(') {
               output += char_stack.top();
               char_stack.pop();
           }

           // Remove '(' from the stack
           char_stack.pop();
       }

       // Operator found
       else {

           if (isOperator(char_stack.top())) {
               while (charPriority(infix[i]) <= charPriority(char_stack.top())) {
                   output += char_stack.top();
                   char_stack.pop();
               }

               // Push current Operator on stack
               char_stack.push(infix[i]);
           }
       }
   }
   return output;
}

string infixToPrefix(string infix)
{
   /* Reverse String
   * Replace ( with ) and vice versa
   * Get Postfix
   * Reverse Postfix * */
   int l = infix.size();

   // Reverse infix
   reverse(infix.begin(), infix.end());

   // Replace ( with ) and vice versa
   for (int i = 0; i < l; i++) {

       if (infix[i] == '(') {
           infix[i] = ')';
           i++;
       }
       else if (infix[i] == ')') {
           infix[i] = '(';
           i++;
       }
   }

   string prefix = infixToPostfix(infix);

   // Reverse postfix
   reverse(prefix.begin(), prefix.end());

   return prefix;
}


/* These functions are for evaluate of prefix expressions*/


bool isOperand(char c)
{
// If the character is a digit then it must
// be an operand
return isdigit(c);
}

double evaluatePrefix(string exprsn)
{
stack<double> Stack;

for (int j = exprsn.size() - 1; j >= 0; j--) {

// Push operand to Stack
// To convert exprsn[j] to digit subtract
// '0' from exprsn[j].
if (isOperand(exprsn[j]))
Stack.push(exprsn[j] - '0');

else {

// Operator encountered
// Pop two elements from Stack
double o1 = Stack.top();
Stack.pop();
double o2 = Stack.top();
Stack.pop();

// Use switch case to operate on o1
// and o2 and perform o1 O o2.
switch (exprsn[j]) {
case '+':
Stack.push(o1 + o2);
break;
case '-':
Stack.push(o1 - o2);
break;
case '*':
Stack.push(o1 * o2);
break;
case '/':
Stack.push(o1 / o2);
break;
}
}
}

return Stack.top();
}
// Driver code
int main()
{
   string s = ("A+B*C+(D^E)*F/G/H+I");
   int A=5, B=10, C=3, D=12, E=3, F=5, G=8, H=4, I=100;
   string exprsn = infixToPrefix(s);
   cout << "INFIX to Prefix notation : " << exprsn << std::endl;
   string changedString;
   //now change charactor values with respective value
   for (int j = 0; j < exprsn.size(); j++) {
   switch(exprsn[j]){
case 'A': changedString.append("(5)"); break;
case 'B': changedString.append("10"); break;
case 'C' : changedString.append("3"); break;
case 'D': changedString.append("12"); break;
case 'E': changedString.append("3"); break;
case 'F': changedString.append("5"); break;
case 'G': changedString.append("8"); break;
case 'H': changedString.append("4"); break;
case 'I': changedString.append("100"); break;
default:
string s;
s.push_back(exprsn[j]);
changedString.append(s);
   }
   }
   cout<<"Changed string with the value is " << changedString<<endl;
   cout << "Evaluation of prefix expression with A=5, B=10, C=3, D=12, E=3, F=5, G=8, H=4, I=100" << evaluatePrefix(changedString) << endl;
   return 0;
}

/Output*/


Add a comment
Know the answer?
Add Answer to:
C++ code: Problem 3. Convert the following infix expression to a prefix expression by Stack operation....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT