Question

In: Computer Science

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 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

BELOW IS THE PROGRAM IN JAVA TO CALCULATE YOUR POLISH EXPRESSION

PROGRAM IN JAVA : - HOPE IS HELP;

import java.util.Scanner;
import java.util.Stack;

public class post {
   public static Scanner sc=new Scanner(System.in);
  
       public static void main(String[] args) {
           String s;
           s=sc.nextLine();
           String a[]=s.split(" ");
           Stack<Integer> st=new Stack<>();
           for(int i=0;i<a.length;i++) {
               boolean numeric = true;
                try {
                    int num = Integer.parseInt(a[i]);
                } catch (NumberFormatException e) {
                    numeric = false;
                }
               if(numeric) {
                   st.push(Integer.parseInt(a[i]));
               }else {
                   int val1=st.pop();
                   int val2=st.pop();
                   switch(a[i]) {
                   case "+":
                       st.push(val2+val1);
                       break;
                   case "-":
                       st.push(val2-val1);
                       break;
                   case "*":
                       st.push(val2*val1);
                       break;
                   case "/":
                       st.push(val2/val1);
                       break;
                   }
               }
           }
          
           System.out.println(st.pop());
          
       }
}


Related Solutions

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...
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...
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...
Write every answer in Probability Notation, for example: Event: one marble selected is Red: P(Red) =...
Write every answer in Probability Notation, for example: Event: one marble selected is Red: P(Red) = 12/33 = .3636 For each problem below, we use the following: A bag contains 33 marbles each with a color and a number. 12 Red labeled 1 – 12, 8 blue labeled 1-8, and 13 whites labeled 1-13. 1. A single marble is drawn from the bag:Event: The marble selected is white Event:The marble selected is odd Event: The marble meets both of the...
write a program that replace each line of a file with its reverse. for example, if...
write a program that replace each line of a file with its reverse. for example, if you run: java Reverse HelloPrinter.java then the contents of HelloPrinter.java are changed to retnirPolleH ssalc clibup { )sgra ][gnirtS(niam diov citats clibup { wodniw elosnoc eht ni gniteerg a yalpsiD // ;) "!dlroW ,olleH " (nltnirp.tuo.mestyS } }
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...
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...
Can you make an example of those issues as follows: A clear list of all compensation...
Can you make an example of those issues as follows: A clear list of all compensation on an annual basis (presented in a spreadsheet format). Compensation in tax, and label each item as salary, taxable fringe, non-taxable fringe.
A convex polygon is defined as a polygon where all its internalangles are less than...
A convex polygon is defined as a polygon where all its internal angles are less than 180 degrees and no edges cross each other. You can assume the vertex with the smallest x-coordinate (assuming origin at bottom left) is the first coordinate and other vertices are numbered in a counter-clockwise direction. Figure 1 shows an example of such a polygon where V[1] is the first polygon. For simplicity, you can also assume all polygon vertices have distinct x and y...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT