Question

In: Computer Science

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 using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.

Input

An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.

You can assume that +, - and * are given as the operator and an operand is a positive integer less than 106

Output

Print the computational result in a line.

Constraints

2 ≤ the number of operands in the expression ≤ 100
1 ≤ the number of operators in the expression ≤ 99
-1 × 109 ≤ values in the stack ≤ 109

Sample Input 1

1 2 +

Sample Output 1

3

Sample Input 2

1 2 + 3 4 - *

Sample Output 2

-3

Solutions

Expert Solution

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

bool isoperator(const string& input){
string ops[] = {"+" , "-" , "/" , "*"} ;
for(int i = 0 ; i<4 ; i++){
if(input == ops[i])
return true ;
}
return false ;
}
int main()
{
  
string input ;
stack<int> s ;
//s.push(1);
//s.push(2);
while(true){
cin >> input ;
if(input == "200") //Assume the user is required to Enter 200 to exit the program
// otherwise you can use getline() and tokenize the inputs but this approach is more readable and easy to understand
break ;
if(isoperator(input)){
int a = s.top() ;
s.pop();
int b =s.top();
s.pop() ;
if(input == "+" )
s.push(b+a);
else if(input == "-")
s.push(b-a);
else
s.push(b*a);
}
else{
s.push(atoi(input.c_str()));
}
}
cout<<"You want to see the result now" << endl;
if(s.empty() || s.size() > 1){
cout << "invalid expression" << endl ;
}
if( s.size() == 1){
cout << s.top() ;
}
return 0;
}

Runs successfully in the online compiler . You can try it too .

You can add more operators in the isoperator() function .

and add more functionality like is valid operator .

Thanks and Regards


Related Solutions

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...
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...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two...
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in "4 + 5". In postfix notation, the operator appears after its operands, as in "4 5 +". Here is a slightly more complex postfix expression: "25 12 7 - 2 * /". The equivalent infix expression is: "25 / ((12 - 7) * 2)". The result of that expression should be 2.5 (beware integer division). Postfix expressions don't require parentheses. Write...
For all problems below, use correct notation where appropriate. Round all proportions to 3 d.p. and...
For all problems below, use correct notation where appropriate. Round all proportions to 3 d.p. and standard errors to 4 d.p. 1. (up to 5 EC pts) Do we dream in color? In the 1940s, before the age of television, color movies, and video games, 29% of the American population reported dreaming in color. A psychologist suspects that the present-day proportion might be higher, now that we are surrounded with color imagery. In a random sample of 113 people, 92...
The question is as follows with all of the requirements commented in. /** * A class...
The question is as follows with all of the requirements commented in. /** * A class that represents a Hounsfield unit. Hounsfield units are the units of * measurement used in computed tomography (CT or CAT) scanning. * * <p> * The Hounsfield scale is defined by specifying the radiodensity of air as * {@code -1000} Hounsfield units and the radiodensity of distilled water as * {@code 0} Hounsfield units. Adjacent tissues in the human body can be * distinguished...
1. For all parts of this question, working is required, including combinatoric/factorial notation as needed as...
1. For all parts of this question, working is required, including combinatoric/factorial notation as needed as well as final answers. a) (*) A gym has the following group fitness sessions:  Zoomba  Body conditioning  Weight lifting  Boxing  Yoga A member can attend one session at most once a day (i.e., once a member has attended Zoomba classes, s/he cannot attend it again that day). The duration of sessions is also unimportant, however, for example, doing Zoomba...
PLEASE ANSWER ALL QUESTIONS. ALL THE QUESTION ARE RELATED IN EVERY NUMBER 1. "For each of...
PLEASE ANSWER ALL QUESTIONS. ALL THE QUESTION ARE RELATED IN EVERY NUMBER 1. "For each of the following questions, use a dry bulb temperature of +26°C and a wet bulb temperature of +15°C, in a room with 70 kg of dry air. The atmospheric pressure is 101,325 Pa. What is the relative humidity of this air? Enter your answer as a number from 0 to 100 without any units or percent sign. Your answer is correct if it is within...
Q2 A) Every business establishes & follows a certain procedure for executing its purchase operations. Such...
Q2 A) Every business establishes & follows a certain procedure for executing its purchase operations. Such procedure always involves the preparation of a number of documents and relevant authorities for their approval. One of these important documents involved in purchase procedure is called Quotation from supplier. Required: Explain why quotation is prepared by the supplier, what information is added in it, how many quotations a buyer should collect to minimum, use of quotation(s) for the buyer. Explain your answer with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT