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);
}
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images