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

An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
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...
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
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,
Please make sure that these infix and postfix equations have these answers nothing else: Infix: (3...
Please make sure that these infix and postfix equations have these answers nothing else: Infix: (3 * 4 - (2 + 5)) * 4 / 2 = valid expression 10 + 6 * 11 -(3 * 2 + 14) / 2 = valid expression Postfix: 9 3 / 6 / 4 * 10 - = -8 9 3 / 6 / 4 * -10 - = 12 (a) Using java.util.stack to write a java program to validate and calculate the...
**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...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT