Question

In: Computer Science

C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into...

C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into the other one. Should use stack and have infixToPrefix and prefixToInfix functions with a simple main function for execution.

Solutions

Expert Solution

(*Note: Please up-vote. If any doubt, please let me know in the comments)

C++ CODE:

#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;


bool
isOperator (char x)
{
switch (x)
{
case '+':
case '-':
case '/':
case '*':
return true; //return true if character is an operator, false otherwise
}
return false;
}

int
precedence (char C) //To return precedence as an integer
{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}

string
infixToPrefix (string infix)
{
string prefix;
stack < char >s;
reverse (infix.begin (), infix.end ());

for (int i = 0; i < infix.length (); i++)
{
if (infix[i] == '(')
   {
   infix[i] = ')';
   }
else if (infix[i] == ')')
   {
   infix[i] = '(';
   }
}
for (int i = 0; i < infix.length (); i++)
{
if ((infix[i] >= 'a' && infix[i] <= 'z')
   || (infix[i] >= 'A' && infix[i] <= 'Z'))
   {
   prefix += infix[i];
   }
else if (infix[i] == '(')
   {
   s.push (infix[i]);
   }
else if (infix[i] == ')')
   {
   while ((s.top () != '(') && (!s.empty ()))
   {
   prefix += s.top ();
   s.pop ();
   }

   if (s.top () == '(')
   {
   s.pop ();
   }
   }
else if (isOperator (infix[i]))
   {
   if (s.empty ())
   {
   s.push (infix[i]);
   }
   else
   {
   if (precedence (infix[i]) > precedence (s.top ()))
       {
       s.push (infix[i]);
       }
   else if ((precedence (infix[i]) == precedence (s.top ()))
       && (infix[i] == '^'))
       {
       while ((precedence (infix[i]) == precedence (s.top ()))
           && (infix[i] == '^'))
       {
       prefix += s.top ();
       s.pop ();
       }
       s.push (infix[i]);
       }
   else if (precedence (infix[i]) == precedence (s.top ()))
       {
       s.push (infix[i]);
       }
   else
       {
       while ((!s.empty ())
           && (precedence (infix[i]) < precedence (s.top ())))
       {
       prefix += s.top ();
       s.pop ();
       }
       s.push (infix[i]);
       }
   }
   }
}

while (!s.empty ())
{
prefix += s.top ();
s.pop ();
}

reverse (prefix.begin (), prefix.end ());
return prefix;
}

string
prefixToInfix (string pre_exp)
{
stack < string > s;

  
int length = pre_exp.size ();

  
for (int i = length - 1; i >= 0; i--)
{

  
if (isOperator (pre_exp[i]))
   {

  
   string op1 = s.top ();
   s.pop ();
   string op2 = s.top ();
   s.pop ();

   // concatenate the operands from stack and the currently read operator
   string temp = "(" + op1 + pre_exp[i] + op2 + ")";

   // Push temp to stack
   s.push (temp);
   }

// when symbol is not any operator
else
   {

   // push the operand on stack
   s.push (string (1, pre_exp[i]));
   }
}


return s.top ();
}


int
main ()
{
//string pre = "*+A/BC-/ADE", inf = "(A+B/C)*(A/D-E)";
string pre, inf;
int choice;
cout << "Enter your choice (1 or 2): " << endl;
cout << "1. Convert Infix to Prefix" << endl << "2. Convert Prefix to Infix"
<< endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter an Infix Expression" << endl;
cin >> inf;
cout << "Prefix expression : " << infixToPrefix (inf) << endl;
break;
case 2:
cout << "Enter a Prefix Expression" << endl;
cin >> pre;
cout << "Infix expression : " << prefixToInfix (pre);
break;
}

return 0;
}

OUTPUT SCREENSHOTS:


Related Solutions

Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token = nextToken() while (token != "#") if (token is an operand) append token to postfix expression else if (token is "(") // Left paren - Start of sub-expr OpStk.push( token ) else if (token is ")") // Right paren - End of sub-expr pop operators off the stack and append to the postfix expression - stop when you've popped a "(" else (token is...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
For this assignment you will write a class that transforms a Postfix expression (interpreted as a...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the tree in different ways. We will test this using our own program that instantiates your class and calls the expected methods. Do not use another class besides the tester and the ExpressionTree class. All work must be done in the class ExpressionTree. Your class must be called ExpressionTree and have...
Write a class PostFix that has one method covert that converts an infix expression (as a...
Write a class PostFix that has one method covert that converts an infix expression (as a string input) to a postfix. Then Test it in a different class. code in java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT