In: Computer Science
● For this task you are required to refactor the badly written program RPN.java. This program is a Reverse-Polish Notation calculator which uses a stack.
● You should also fix the indentation of the code.
I AM USING ECLIPSE. THANK YOU IN ADVANCE
import java.util.Scanner;
public class StackNode {
public StackNode(double data, StackNode underneath)
{
this.data = data;
this.underneath = underneath;
}
public StackNode underneath;
public double data;
}
public class RPN {
public void into(double new_data) {
StackNode new_node = new
StackNode(new_data, top);
top = new_node;
}
public double outof( ) {
double top_data = top.data;
top = top.underneath;
return top_data;
}
public RPN(String command) {
top = null;
this.command = command;
}
public double get( ) {
double a, b;
int j;
for(int i = 0; i <
command.length( ); i++) {
if(Character.isDigit(command.charAt(i))) {// if it's a digit
double number;
String temp = "";// get a string of
the number
for(j = 0; (j < 100) &&
(Character.isDigit(command.charAt(i)) || (command.charAt(i) ==
'.')); j++, i++) {
temp = temp +
String.valueOf(command.charAt(i));
}
// convert to double and add to the stack
number =
Double.parseDouble(temp);
into(number);
}else if(command.charAt(i) == '+')
{
b = outof(
);
a = outof(
);
into(a +
b);
}else if(command.charAt(i) == '-')
{
b = outof(
);
a = outof(
);
into(a -
b);
}else if(command.charAt(i) == '*')
{
b = outof(
);
a = outof(
);
into(a *
b);
}else if(command.charAt(i) == '/')
{
b = outof(
);
a = outof(
);
into(a /
b);
}else if(command.charAt(i) == '^')
{
b = outof(
);
a = outof(
);
into(Math.pow(a,
b));
}else if(command.charAt(i) != ' ')
{
throw new
IllegalArgumentException( );
}
}
double val = outof( );
if(top != null) {
throw new
IllegalArgumentException( );
}
return val;
}
private String command;
private StackNode top;
public static void main(String args[]) {/* main method
*/
while(true) {
Scanner in = new
Scanner(System.in);
System.out.println("Enter RPN expression or \"quit\".");
String line =
in.nextLine( );
if(line.equals("quit")) {
break;
}else {
RPN calc = new
RPN(line);
System.out.printf("Answer is %f\n", calc.get( ));
}
}
}
CODE
import java.util.Scanner;
class StackNode {
public double data;
public StackNode next;
public StackNode(double data) {
this.data = data;
this.next = null;
}
}
public class RPN {
private String command;
private StackNode top;
public RPN(String command) {
top = null;
this.command = command;
}
public void push(double new_data) {
StackNode new_node = new StackNode(new_data);
if (top == null) {
top = new_node;
return;
}
new_node.next = top;
top = new_node;
}
public double pop() {
if (top == null) {
throw new IllegalArgumentException();
}
double top_data = top.data;
top = top.next;
return top_data;
}
public double get( ) {
double a, b;
int j;
for(int i = 0; i < command.length( ); i++) {
if(Character.isDigit(command.charAt(i))) {// if it's a digit
double number;
String temp = "";// get a string of the number
for(j = 0; (j < 100) && (Character.isDigit(command.charAt(i)) || (command.charAt(i) == '.')); j++, i++) {
temp = temp + String.valueOf(command.charAt(i));
}
// convert to double and add to the stack
number = Double.parseDouble(temp);
push(number);
}else if(command.charAt(i) == '+') {
b = pop( );
a = pop( );
push(a + b);
}else if(command.charAt(i) == '-') {
b = pop( );
a = pop( );
push(a - b);
}else if(command.charAt(i) == '*') {
b = pop( );
a = pop( );
push(a * b);
}else if(command.charAt(i) == '/') {
b = pop();
a = pop();
push(a / b);
}else if(command.charAt(i) == '^') {
b = pop( );
a = pop( );
push(Math.pow(a, b));
}else if(command.charAt(i) != ' ') {
throw new IllegalArgumentException( );
}
}
double val = pop( );
if(top != null) {
throw new IllegalArgumentException( );
}
return val;
}
public static void main(String args[]) {/* main method */
while(true) {
Scanner in = new Scanner(System.in);
System.out.println("Enter RPN expression or \"quit\".");
String line = in.nextLine( );
if(line.equals("quit")) {
break;
}else {
RPN calc = new RPN(line);
System.out.printf("Answer is %f\n", calc.get( ));
}
}
}
}