RPN.java import java.util.Scanner; /** * Reverse Polish Notation calculator. It evaluates * a string with expressions in RPN format and prints * the results. Exampel of a stack use. */ public class RPN { /** * Given a string, return an integer version of * the string. Check if the string contains only * numbers, if so, then does the conversion. If * it does not, it reurns 0. * @param t string with numeric token * @return int version of the numeric token in t */ public int getValue(String t) { if (t.matches("[0-9]+")) { return Integer.parseInt(t); } else { return 0; } } /** * Evaluates a single token in an RPN expression. If it is * a number, it pushes the token to the stack. If it is an * operator, then it pulls 2 numbers from the stack, performs * the operation and pushes back into the stack the result. * @param token to be evaluated * @param stack holding values for expression evaluation */ public boolean evaluate(String token, Stack stack) throws ArithmeticException { boolean isANumber = token.matches("[0-9]+"); if (isANumber) { stack.push(token); return true; } else if (token.equals("+") && (stack.size() >= 2)) { String b = stack.pop(); String a = stack.pop(); int result = getValue(a) + getValue(b); stack.push("" + result); System.out.println(a + " + " + b + " = " + result); return true; } else if (token.equals("-") && (stack.size() >= 2)) { String b = stack.pop(); String a = stack.pop(); int result = getValue(a) - getValue(b); stack.push("" + result); System.out.println(a + " - " + b + " = " + result); return true; } else if (token.equals("*") && (stack.size() >= 2)) { String b = stack.pop(); String a = stack.pop(); int result = getValue(a) * getValue(b); stack.push("" + result); System.out.println(a + " * " + b + " = " + result); return true; } else if (token.equals("/") && (stack.size() >= 2)) { String b = stack.pop(); String a = stack.pop(); if (getValue(b) == 0) { throw new ArithmeticException("Cannot divide by zero!"); } int result = getValue(a) / getValue(b); stack.push("" + result); System.out.println(a + " / " + b + " = " + result); return true; } else if (token.equals("ac") && (stack.size() >= 0)) { System.out.println("Input recognized as [" + token + "]. so Stack cleared."); stack.clear(); return true; } else if (token.equals("pop") && (stack.size() >= 1)) { String p = stack.pop(); System.out.println("The popped up element is " + p); return true; } else if (token.equals("swap") && (stack.size() >= 2)) { System.out.println("Last two Stack elements Before Swaping "); String b = stack.pop(); System.out.println(b + " "); String a = stack.pop(); System.out.println(a + " "); int x = getValue(a); int y = getValue(b); x = x + y; y = x - y; x = x - y; stack.push("" + x); stack.push("" + y); System.out.println(); System.out.println("Last two Stack elements After Swaping "); while (!(stack.empty())) { System.out.println(stack.pop() + " "); } return true; } else { System.out.println("Input not recognized [" + token + "]"); return false; } } /** * Evaluates a full line of input with an RPN expression. * Splits the line into tokens and calls the evaluate() to * do the work. * @param line to be divided up into token * @param stack holding values for expression evaluation */ /* public void evaluateLine(String line, Stack stack) * { * Scanner sn = new Scanner(line); * while (sn.hasNext()) { * String token = sn.next(); * evaluate(token, stack); * } * } */ public static void main(String[] args) { Stack stack = new Stack(); RPN r = new RPN(); Scanner sn = new Scanner(System.in); while (sn.hasNext()) { String token = sn.next(); boolean b = r.evaluate(token, stack); if (!b) { System.exit(0); } } } }
RPN.java
import java.util.Scanner;
/**
* Reverse Polish Notation calculator. It evaluates
* a string with expressions in RPN format and prints
* the results. Exampel of a stack use.
*/
public class RPN
{
/**
* Given a string, return an integer version of
* the string. Check if the string contains only
* numbers, if so, then does the conversion. If
* it does not, it reurns 0.
* @param t string with numeric token
* @return int version of the numeric token in t
*/
public int getValue(String t)
{
if (t.matches("[0-9]+")) {
return Integer.parseInt(t);
}
else {
return 0;
}
}
/**
* Evaluates a single token in an RPN expression. If it is
* a number, it pushes the token to the stack. If it is an
* operator, then it pulls 2 numbers from the stack, performs
* the operation and pushes back into the stack the result.
* @param token to be evaluated
* @param stack holding values for expression evaluation
*/
public boolean evaluate(String token, Stack<String> stack)
throws
ArithmeticException
{
boolean isANumber = token.matches("[0-9]+");
if (isANumber) {
stack.push(token);
return true;
}
else if (token.equals("+") && (stack.size() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) + getValue(b);
stack.push("" + result);
System.out.println(a + " + " + b + " = " + result);
return true;
}
else if (token.equals("-") && (stack.size() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) - getValue(b);
stack.push("" + result);
System.out.println(a + " - " + b + " = " + result);
return true;
}
else if (token.equals("*") && (stack.size() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) * getValue(b);
stack.push("" + result);
System.out.println(a + " * " + b + " = " + result);
return true;
}
else if (token.equals("/") && (stack.size() >= 2)) {
String b = stack.pop();
String a = stack.pop();
if (getValue(b) == 0) {
throw new ArithmeticException("Cannot divide by zero!");
}
int result = getValue(a) / getValue(b);
stack.push("" + result);
System.out.println(a + " / " + b + " = " + result);
return true;
}
else if (token.equals("ac") && (stack.size() >= 0)) {
System.out.println("Input recognized as [" + token + "]. so Stack cleared.");
stack.clear();
return true;
}
else if (token.equals("pop") && (stack.size() >= 1)) {
String p = stack.pop();
System.out.println("The popped up element is " + p);
return true;
}
else if (token.equals("swap") && (stack.size() >= 2)) {
System.out.println("Last two Stack elements Before Swaping ");
String b = stack.pop();
System.out.println(b + " ");
String a = stack.pop();
System.out.println(a + " ");
int x = getValue(a);
int y = getValue(b);
x = x + y;
y = x - y;
x = x - y;
stack.push("" + x);
stack.push("" + y);
System.out.println();
System.out.println("Last two Stack elements After Swaping ");
while (!(stack.empty())) {
System.out.println(stack.pop() + " ");
}
return true;
}
else {
System.out.println("Input not recognized [" + token + "]");
return false;
}
}
/**
* Evaluates a full line of input with an RPN expression.
* Splits the line into tokens and calls the evaluate() to
* do the work.
* @param line to be divided up into token
* @param stack holding values for expression evaluation
*/
/* public void evaluateLine(String line, Stack<String> stack)
* {
* Scanner sn = new Scanner(line);
* while (sn.hasNext()) {
* String token = sn.next();
* evaluate(token, stack);
* }
* } */
public static void main(String[] args)
{
Stack<String> stack = new Stack<String>();
RPN r = new RPN();
Scanner sn = new Scanner(System.in);
while (sn.hasNext()) {
String token = sn.next();
boolean b = r.evaluate(token, stack);
if (!b) {
System.exit(0);
}
}
}
}

![20
21
22
23
24
25
26
27
28
29
30
!
32
33
34
35
36
37
38
40
41
42
43
44
45
0-
public class RPNMain
{
}
* Main program, manages input from keyboard and passing
* information to evaluation procedures above.
* @param args - command line arguments
*/
public static void main(String args[])
{
RPN interpreter = new RPN();
StackADT<String> stack = new Stack<>();
System.out.println("Welcome to mini RPN calculator\n");
System.out.print("$ ");
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
}
String line = sc.nextLine().trim();
interpreter.evaluateLine(line, stack);
System.out.print("\n$ ");
System.out.println("Bye");](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb6b9b709-ecc4-405f-be15-0cfcda40f349%2Fc2a0854f-c27f-496c-918a-57446b25be13%2F2nzqd5p_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images









