In: Computer Science
Write a Reverse Polish Calculator, RPN in JAVA
Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /.
Implement a class RPN with a single static method evaluate. This method should have the following signature:
public static String evaluate(String expression)
It should split the passed expression into (a string array of) tokens by invoking split method of String class. This array can then be processed one element at a time (perhaps another method): each time a number is found – it is pushed on a stack. Each time an operation is found – two numbers are popped from the stack, the given operation is performed, and the result is pushed back on the stack. If the passed expression was a properly formed RPN, the last element on the stack represents the result. It should be returned as a string. If the given expression is not valid, evaluate should return a string denoting a syntax error.
Your main should continually ask the user to input a potential RPN string which would be passed to the evaluate method. In order to terminate the procedure, the user would input an empty string (carriage return).
Sample run of how it should look:
run:
Enter an RPN expression or to exit
2 3 4 +
7.0
extra junk ignored
Enter an RPN expression or to exit
2 3 4 + *
14.0
Enter an RPN expression or to exit
2 3 + +
Syntax error
Enter an RPN expression or to exit
10 6 1 - /
2.0
Enter an RPN expression or to exit
5 4 3 / /
3.75
Enter an RPN expression or to exit
good bye
Here is code in Java:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Solution{
// Method to evaluate value of a postfix expression
public static double evalRPN(String[] tokens) {
double a,b;
Stack<Double> S = new Stack<Double>();
for (String s : tokens) {
if(s.equals("+")) {
b= S.pop();
if(S.empty()){
System.out.println("Invalid input");
break;
}
a= S.pop();
S.add(a+b);
}
else if(s.equals("/")) {
b = S.pop();
if(S.empty()){
System.out.println("Invalid input");
break;
}
a = S.pop();
S.add(a / b);
}
else if(s.equals("*")) {
b= S.pop();
if(S.empty()){
System.out.println("Invalid input");
break;
}
a= S.pop();
S.add(a* b);
}
else if(s.equals("-")) {
b = S.pop();
if(S.empty()){
System.out.println("Invalid input");
break;
}
a = S.pop();
S.add(a - b);
}
else {
S.add(Double.parseDouble(s));
}
}
return S.pop();
}
// Driver program to test above functions
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("Enter an RPN expression or <CR> to exit\n");
String inp = sc.nextLine();
if(inp.equals("end"))
break;
List<String> L = new ArrayList<String>();
// to split the given input
for (String val: inp.split(" ")){
L.add(val);
}
String tokens[] = new String[L.size()];
tokens = L.toArray(tokens);
try {
double output = evalRPN(tokens);
System.out.println(output);
}catch (Exception e){
System.out.println("Invalid input");
}
}
}
}
Output :