Question

In: Computer Science

Polish notation, also known as Polish prefix notation or simply prefix notation, is a form of...

Polish notation, also known as Polish prefix notation or simply prefix notation, is a form of notation for logic, arithmetic, and algebra. Its distinguishing feature is that it places operators to the left of their operands.

The expression for adding the numbers 1 and 2, in prefix notation, is written as “+ 12” rather than “1 + 2”. In more complex expressions, the operands may be nontrivial expressions including operators of their own. For instance, the expression that would be written in conventional infix notation as (5 + 6) * 7 can be written in prefix as * (+ 5 6) 7

In this assignment, we only care about binary operators: + - * and /.

For binary operators, prefix representation is unambiguous and bracketing the prefix expression is unnecessary. As such, the previous expression can be further simplified to * + 5 6 7

The processing of the product is deferred until its two operands are available (i.e., 5 plus 6, then multiplies the result with 7). As with any notation, the innermost expressions are evaluated first, but in prefix notation this ”innermost-ness” is conveyed by order rather than bracketing.

Your task is to create a class that convert a prefix expression to a standard form (otherwise known as infix) and compute the prefix expression.

Main function

The test script will compile your code using

g++ -o main.out -std=c++11 -O2 -Wall *.cpp

It is your responsibility to ensure that your code compiles on the university system. g++ has too many versions, so being able to compile on your laptop does not guarantee that it compiles on the university system. You are encouraged to debug your code on a lab computer (or use SSH).

Create a main function that takes in one line. The line contains a list of operators and operands separated by spaces. The input doesn’t contain parenthesis. An operator is a character from +, -, *, and /. An operand is a nonnegative integer from 0 to 99. You are asked to convert the prefix expression to an infix form and output its value as well. If the expression is not a valid prefix expression, your program should output “Error”.

Sample input: * - 5 6 7

Sample output: (5 - 6) * 7 = -7

  

Sample input: * 5

Sample output: Error

Sample input: * 5 6 7

Sample output: Error

Please provide the .h, .cpp and main.cpp file for this, like Polish.h Polish.cpp and main.cpp thank you

Solutions

Expert Solution

The logic is defined in the program comments on every important line.

CODE:

Polish.h

#include <iostream>
#include <stack>
#include <string>

using namespace std;

string convertPrefixToInfix(string prefixExpression);

Polish.cpp

#include "Polish.h"

// Convert prefix to Infix expression
string convertPrefixToInfix(string prefixExpression)
{
   stack<string> infixStack;   // infix expression
   stack<long> evalStack;       // result of the infix expression
   char ch;
   string operand = "";
   long result = 0;

   // length of prefix expression
   int length = prefixExpression.size();

   // parse expression from end to start that is right to left
   for (int index = length - 1; index >= 0; index--)
   {
       ch = prefixExpression[index];
       if (ch == ' ' && !operand.empty())
       {
           infixStack.push(operand);
           evalStack.push(atol(operand.c_str()));
           operand = "";
           continue;
       }
       // if the read character is operator,
       // get top 2 values from the stack and pop them
       else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
       {
           // make sure there are enough operands on the stacks.
           // there should be atleast 2 operand on the stack.
           // if not expression is not valid. retun Error.
           if (infixStack.size() < 2)
           {
               return "Error";
           }

           // retrieve first operand from top of the stack
           string op1 = infixStack.top();
           infixStack.pop();

           // retrieve second operand from top of the stack
           string op2 = infixStack.top();
           infixStack.pop();

           // make the infix expression
           string temp = "(" + op1 + prefixExpression[index] + op2 + ")";

           // evaluate the expression
           // pop first operand value
           long op1Value = evalStack.top();
           evalStack.pop();

           // pop second operand value
           long op2Value = evalStack.top();
           evalStack.pop();

           // perform the operation and push the result on stack
           if (ch == '+')
               evalStack.push(op1Value + op2Value);
           else if (ch == '-')
               evalStack.push(op1Value - op2Value);
           else if (ch == '*')
               evalStack.push(op1Value * op2Value);
           else if (ch == '/')
               evalStack.push(op1Value / op2Value);

           // push the formed infix expression on the stack
           infixStack.push(temp);
       }
       // if character is an operand, simply push it on the stack as string
       else
       {
           operand += string(1, prefixExpression[index]);
       }
   }

   if (infixStack.size() > 1)
   {
       return "Error";
   }

   // at this point, the top of the stack would have the infix expression,
   // return it.
   return infixStack.top() + " = " + to_string(evalStack.top());
}
Main.cpp

#include "Polish.h"
int main()
{
   string prefixExpression;

   // prompt user for input
   cout << "Input prefix expression: ";
   getline(cin, prefixExpression);

   // call the convertPrefixToInfix function and display the infix expression
   cout << "Infix : " << convertPrefixToInfix(prefixExpression);

   return 0;
}

OUTPUT:


Related Solutions

The federal reserve system, also known simply as the fed, is the central bank of the...
The federal reserve system, also known simply as the fed, is the central bank of the united states. the fed has several important functions such as supplying the economy with currency, holding deposits of banks, lending money to banks, regulating the money supply, and supervising the banking system. explain how the federal reserve and the banking system creates money (i.e., increases the supply of money). is this an inherently inflationary practice? explain the factors that affect the demand for money.
The federal reserve system, also known simply as the fed, is the central bank of the...
The federal reserve system, also known simply as the fed, is the central bank of the united states. the fed has several important functions such as supplying the economy with currency, holding deposits of banks, lending money to banks, regulating the money supply, and supervising the banking system. explain how the federal reserve and the banking system creates money (i.e., increases the supply of money). is this an inherently inflationary practice? explain the factors that affect the demand for money.
The federal reserve system, also known simply as the fed, is the central bank of the...
The federal reserve system, also known simply as the fed, is the central bank of the united states. the fed has several important functions such as supplying the economy with currency, holding deposits of banks, lending money to banks, regulating the money supply, and supervising the banking system. explain how the federal reserve and the banking system creates money (i.e., increases the supply of money). is this an inherently inflationary practice? explain the factors that affect the demand for money.
Reverse Polish notation is a notation where every operator follows all of its operands. For example,...
Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which reads an expression in the Reverse Polish notation and prints the computational result. An expression in the Reverse Polish notation is calculated using...
Question: Reverse Polish notation is a notation where every operator follows all of its operands. For...
Question: Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which reads an expression in the Reverse Polish notation and prints the computational result. An expression in the Reverse Polish notation is calculated...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
The students in ACCT class (also known as “the fabulous fifteen”) decide to form a partnership...
The students in ACCT class (also known as “the fabulous fifteen”) decide to form a partnership in order to start a business that researches mergers and acquisitions and sells their analysis to investors via a monthly report. Further, they decide to split the responsibilities of the partnership into three different areas as follows: • Five of “the fabulous fifteen” invest the majority of the capital needed to get the business started, but do not want to be involved on the...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called...
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called Operand Give it a virtual destructor to avoid any weird problems later on! Derive a class called Number from Operand Maintain a double member variable in class Number For simplicity, you may make the member variable public if you would like Derive a class called Operator from Operand Derive a class called Add from Operator (2 + 3 = 5) Derive a class called...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack (or your own implementation) for this problem. Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which...
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve...
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve the classes that programming problems 4 and 5 describe This is to be written in C++. I cannot provide anymore information, I cannot provide any class information, etc. This is all the problem that book gave me. This is for Data Structures and algorithms.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT