Program Postfix Calculator with Memory In this program you w
Program: Postfix Calculator with Memory
In this program, you will write a command line calculator that can evaluate simple mathematical expressions on doubles typed in postfix notation (also called reverse polish notation, or RPN), as well as store variables for later use in other expressions.
Postfix notation:
In a typical mathematical expression that you are probably used to, such as 5 + 2, the operator (+) goes in-between the two operands (5 and 2). This is called \"infix\" notation. Postfix notation places the two operands first, and the + sign last, giving 5 2 +. One of the main benefits of postfix notation is that it is easy to evaluate using a \"stack\". Parentheses are not needed, nor are precedence rules, since there is only one reasonable way to evaluate the expression. The algorithm to do this uses a stack of numbers, and can be stated in a few steps as follows:
Given a Postfix expression such as \"2 3 + x *\", break it into tokens, and initialize an empty stack of numbers. Then do the following steps:
1.Process the tokens from left to right. For each token, If the token is an operand (a number or variable), push the value of the operand on the stack (of doubles in our case). For an operator (+, *, etc.), pop the needed number of operands off the stack, compute the result, and push it back on the stack.
2.The expression\'s value will be on the top of the stack at the end.
Error checking is also easy. The following are error conditions:
1.If at any point there are not enough operands for an operation on the stack, the expression is not valid.
2.If more than one value is on the stack at the end, the expression is not valid.
Memory:
Your calculator must have a memory that allows it to store or modify named variables. These can easily be stored in a HashMap, or parallel ArrayLists.
Program Operation:
When the program starts, it should print your name and state that it is an RPN calculator. Then it should go into \"command mode\", showing a prompt character (in our case, >), and responding to inputs typed by the user. Example usage is given as follows.
java PostfixCalc
Postfix calculator with memory by <your name>
>a = 3 5 + 1 -
7.0
> bee = a 3 *
21.0
> a bee +
28.0
>a = 4
4.0
> a = a 3.2 /
1.25
> 2 c +
2.0
> delete a
> var
{ bee:21.0, c:0.0 }
> clear
> 2 5 ^
32.0
> var
{ }
> quit
The calculator must accept the following input types from the user:
1.A command (quit, clear, delete,var)Result: The program executes the command quit – exits the programclear – removes all variablesdelete varName – removes single variable with name varNamevar – prints all the variable names and values in the calculator\'s memory
2.A postfix expression (example: a b + 3.0 /)Result: The value of the expression is computed and printed on the screen. Operands can either be numbers or a variable namesIf an operand is a number, it can be positive or negativeOperators must be one of +, -, *, /, ^, where ^ means exponentiationSpaces will separate operands and operators. Thus \"ab+\" is not valid but \"a b +\" is valid.
3.An assignment (varName = postfixExpression)Example (myVar = a b ^ 33.0 *)Result: If the variable on the left does not exist, it is created. The variable on the left is then assigned the value of the right hand side. If a variable on the right hand side does not exist, it should be created and its value set to 0.
Note that if the input is not a command, it must either be (1) a postfix expression, or (2) a variable name, followed by =, followed by a postfix expression. If the input is any of these three types, the program should respond properly. If the input is not valid the program must not crash. Instead, it should print an error stating that the input is not valid and continue.
Solution
 import java.util.*;
 import java.io.*;
public class PostfixCalc {
public static void main(String[] args) {
 System.out.println(\"Postfix command line calculator\");
 Scanner s = new Scanner(System.in);
 System.out.print(\">\");
 while (s.hasNextLine()) {
 System.out.print(\"> \");
 String l = s.nextLine();
 String m = \"quit\";
 String n = \"mem\";
 String o = \"clear\";
 if (l.equals(m)) {
 System.exit(0);
 } else {
 System.out.println(calc(l));
 }
 System.out.print(\">\");
 }
 }
public static String calc(String input) {
 List<String> processedList = new ArrayList<String>();
 if (!input.isEmpty()) {
 StringTokenizer str = new StringTokenizer(input);
 while (str.hasMoreTokens()) {
 processedList.add(str.nextToken());
 }
 } else {
 return \"Error\";
 }
 Stack<String> tmpLst = new Stack<String>();
Iterator<String> itrr = processedList.iterator();
while (itrr.hasNext()) {
 String tmp = itrr.next();
 if (tmp.matches(\"[0-9]*\")) {
tmpLst.push(tmp);
 } else if (tmp.matches(\"[*-/+]\")) {
if (tmp.equals(\"*\")) {
 int sr = Integer.parseInt(tmpLst.pop());
 int sl = Integer.parseInt(tmpLst.pop());
 int result = sl * sr;
 tmpLst.push(\"\" + result);
 } else if (tmp.equals(\"-\")) {
 int sr = Integer.parseInt(tmpLst.pop());
 int sl = Integer.parseInt(tmpLst.pop());
 int result = sl - sr;
 tmpLst.push(\"\" + result);
 } else if (tmp.equals(\"/\")) {
 int sr = Integer.parseInt(tmpLst.pop());
 int sl = Integer.parseInt(tmpLst.pop());
 int result = sl / sr;
 tmpLst.push(\"\" + result);
 } else if (tmp.equals(\"+\")) {
 int sr = Integer.parseInt(tmpLst.pop());
 int sl = Integer.parseInt(tmpLst.pop());
 int result = sl + sr;
 tmpLst.push(\"\" + result);
 }
} else {
 return \"Error\";
 }
 }
return tmpLst.pop();
 }
private static String HashMap(String g) {
 List<String> mry = new ArrayList<String>();
 if (!g.isEmpty()) {
 StringTokenizer tok = new StringTokenizer(g);
 while (tok.hasMoreTokens()) {
 mry.add(tok.nextToken());
 }
 }
 HashMap hm = new HashMap();
 return null;
 }
 }




