Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Get an equation (e.g. 2 3 +) from the user as input. Loop through the string value input by the user. When you encounter a

Get an equation (e.g. 2 3 +) from the user as input. Loop through the string value input by the user. When you encounter a number (remember that numbers can include decimal points), add it to the stack (push). When you encounter an operator (e.g. +, -, /, etc.), pop two numbers from the stack and perform the appropriate calculation. Display the answer of the calculation to the user.

import java.util.Scanner; class StackNode { public StackNode(double data, StackNode underneath) { this.data = data; this.underneath = underneath; } public StackNode underneath; public double data; } 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 it's a digit if(Character.isDigit(command.charAt(i))) { double number; // get a string of the number String temp = ""; 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; /* main method */ public static void main(String args[]) { 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 ", calc.get( )); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

Students also viewed these Databases questions

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago