Program: Integer Postfix Calculator In this program, you will write acommand lin
ID: 3808970 • Letter: P
Question
Program: Integer Postfix Calculator In this program, you will write acommand line calculator that can evaluate simple mathematical expressions on ints 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 farst, and the sign last, giving 52 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 (anumber or variable), push the value of the operand on the stack (of integers 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 postfix 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.Explanation / Answer
import java.io.*;
import java.util.*;
//Class Stack definition
class Stack
{
//Instance variable declaration
private int[] a;
private int top,m;
//Constructor
public Stack(int max)
{
m=max;
//Creates an integer array of size m
a=new int[m];
//Initializes top to -1
top=-1;
}
//Push operation
public void push(int key)
{
//Increase the top by one and stores the key value on the top position
a[++top]=key;
}
//Pop operation
public int pop()
{
//Returns the top position of the array and decrease the top by one
return(a[top--]);
}
}//End of class
//Evaluation class definition
class Evaluation
{
//Method to calculate an expression
public int calculate(String s)
{
//Instance variable declaration
int n,r=0;
//Calculates the length of expression
n=s.length();
//Creates an object of Stack class and initializes the size integer array with the help of constructor
Stack a=new Stack(n);
//Loops till length of expression
for(int i=0;i<n;i++)
{
//Extracts the character at loop index position
char ch=s.charAt(i);
//If the character is space then skip it
if(ch == ' ')
continue;
//Converts the character to integer and push it to the stack top position
else if(ch>='0'&&ch<='9')
a.push((int)(ch-'0'));
//Otherwise if it is operator
else
{
//Extract the operand from the stack
int x=a.pop();
int y=a.pop();
//Checks the operator
switch(ch)
{
//Evaluate the expression as per the operator
case '+': r = x + y;
break;
case '-': r = y - x;
break;
case '*': r = x * y;
break;
case '/': r = y / x;
break;
case '^': r = (int)Math.pow(y, x);
break;
default:r=0;
}//End of switch
//Push the result to the stack top position
a.push(r);
}//End of else
}//End of for loop
//pops the result from the stack
r=a.pop();
//Returns the expression result
return(r);
}//End of method
}//End of class
//Driver class definition
class PostfixEvaluation
{
//Main method definition
public static void main(String[] args)throws IOException
{
String input;
//Accepts the expression till null express is entered
while(true)
{
//Accept an expression
System.out.println("Enter the postfix expresion");
input=getString();
//Checks if it is null then stop
if(input.equals(""))
break;
//Creates an object of Evaluation class
Evaluation e=new Evaluation();
//Calls the calculate method and display the result
System.out.println("Result:- "+e.calculate(input));
}//End of while
}//End of main method
//Method to accept an expression
public static String getString()throws IOException
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
return s;
}//End of method
}//End of class
Output:
Enter the postfix expresion
1 2 +
Result:- 3
Enter the postfix expresion
1 2 3 * + 4 +
Result:- 11
Enter the postfix expresion
2 3 ^
Result:- 8
Enter the postfix expresion
5 2 4 * + 7 -
Result:- 6
Enter the postfix expresion
2 3 + 4 5 * +
Result:- 25
Enter the postfix expresion
8 5 * 7 4 2 + * +
Result:- 82
Enter the postfix expresion
6 8 2 / 1 - *
Result:- 18
Enter the postfix expresion
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.