please provide complete Java class code with main() function. Implement a progra
ID: 3808541 • Letter: P
Question
please provide complete Java class code with main() function.
Implement a program that can input an expression in postfix notation (see ExerciseC-6.19) and output its value.
C-6.19 Postfix notation is an unambiguousway of writing an arithmetic expressionwithout
parentheses. It is defined so that if “(exp1)op(exp2)” is a normal fully parenthesized
expression whose operation is op, the postfix version of this is “pexp1
pexp2 op”, where pexp1 is the postfix version of exp1 and pexp2 is the postfix version
of exp2. The postfix version of a single number or variable is just that number
or variable. So, for example, the postfix version of “((5+2) (83))/4” is “5
2 + 8 3 4 /”. Describe a nonrecursive way of evaluating an expression in
postfix notation.
Explanation / Answer
For evaluation of postfix we use the stack class to push pop operations of the input numbers and the operators.
import java.util.*;
class EvalPostfx {
private Stack inputStrngStck;
private String inputStrng;
private String otputStrng = "";
public EvalPostfx(String in) {
inputStrng = in;
int stackSize = inputStrng.length(); //get the lenght of the string and assign tit to stack size
inputStrngStck = new Stack(stackSize); //pull to the stack
}
public String evaluateStrng() {
for (int j = 0; j < inputStrng.length(); j++) {
char ch = inputStrng.charAt(j); //get each character of the input string
switch (ch) {
case '+':
case '-':
chckOprtr(ch, 1);
break;
case '*':
case '/':
chckOprtr(ch, 2);
break;
case '(':
inputStrngStck.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
otputStrng = otputStrng + ch;
break;
}
}
while (!inputStrngStck.isEmpty()) {
otputStrng = otputStrng + inputStrngStck.pop();
}
//System.out.println(otputStrng);
return otputStrng;
}
public void chckOprtr(char opThis, int prec1) {
while (!inputStrngStck.isEmpty()) {
char opTop = inputStrngStck.pop();
if (opTop == '(') {
inputStrngStck.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
inputStrngStck.push(opTop);
break;
}
else otputStrng = otputStrng + opTop;
}
}
inputStrngStck.push(opThis);
}
public void gotParen(char ch) {
while (!inputStrngStck.isEmpty()) {
char chx = inputStrngStck.pop();
if (chx == '(')
break;
else otputStrng = otputStrng + chx;
}
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
}
public static void main(String[] args) {
String inputStrng ;
String otputStrng;
Scanner sc= new Scanner(System.in); //scanner class to read input
System.out.println("Enter arthemetic expression to evaluate its postfix");
inputStrng=sc.nextLine(); //read the input string
EvalPostfx inStr = new EvalPostfx(inputStrng); //initialing object for evaluating the postfix
otputStrng = inStr.evaluateStrng(); //calling the method to evaluate string
System.out.println("The input expression is " + inputStrng + ' ');
System.out.println("Postfix is " + otputStrng + ' ');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.