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

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...
All That Blooms provides environmentally friendly lawn services for homeowners. Its operating costs are as follows....
All That Blooms provides environmentally friendly lawn services for homeowners. Its operating costs are as follows. Depreciation $1,500 per month Advertising $2,671 per month Insurance $1,700 per month Weed and feed materials $16 per lawn Direct labor $13 per lawn Fuel $3 per lawn All That Blooms charges $89 per treatment for the average single-family lawn. 1. Determine the company’s break-even point in number of lawns serviced per month. a. Break even point __ lawns. 2. Determine the company’s break-even...
All That Blooms provides environmentally friendly lawn services for homeowners. Its operating costs are as follows....
All That Blooms provides environmentally friendly lawn services for homeowners. Its operating costs are as follows. Depreciation $1,400 per month Advertising $200 per month Insurance $2,000 per month Weed and feed materials $12 per lawn Direct labor $10 per lawn Fuel $2 per lawn All That Blooms charges $60 per treatment for the average single-family lawn. Determine the company’s break-even point in number of lawns serviced per month. Break-even point enter the break-even point in number of lawns serviced per...
Dalton Co. follows a policy of allocating all common costs equally among its profit centers. A...
Dalton Co. follows a policy of allocating all common costs equally among its profit centers. A partial responsibility income statement for a typical month is shown below: Dalton Co. Profit Center 1 Profit Center 2 Profit Center 3 Responsibility margins $ 200,000 $ 80,000 $ 70,000 $ 50,000 Common fixed costs $ 165,000 $ 55,000 $ 55,000 $ 55,000 Income from operations $ 35,000 $ 25,000 $ 15,000 $ (5,000 ) After evaluating these data, Dalton Co. decides to close...
Forward Company makes all its sales on account. Forward's accounts receivable payment experience is as follows:...
Forward Company makes all its sales on account. Forward's accounts receivable payment experience is as follows: Percent paid in the month of sale 30% Percent paid in the month after sale 65% Percent paid in the second month after sale 3% Forward provided information on sales as follows: September $120,000 October $140,000 November $220,000 December (expected) $260,000 ? What are the expected cash receipts in December? a. $210,400 b. $250,000 c. $225,200 d. $50,000 e. $179,000
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT