Using a generic stack, you are writing a code that converts a median expression to a posterior expression, then outputs an expression, and then outputs an answer, but the expression is still printed during the conversion of the notation, and the calculation results in a nullPointerException error.
I couldn't find the answer to the nullPointerException, so I'd like to ask you a solution.
Stack
class GStack<T> {
int top;
Object[] stck;
public GStack() {
top = 0;
stck = new Object[100];
}
public void push(T item) {
if (top == 100)
return;
stck[top] = item;
top++;
}
public T pop() {
if (top == 0)
return null;
return (T) stck[top--];
}
public T peek() {
if (top == 0)
return null;
return (T)stck[top];
}
}
main
public class postfixCalc {
public static int priority(char op) {
if(op == '+' || op == '-')
return 1;
else if(op == '*' || op == '/')
return 2;
else
return 3;
}
public static void main(String[] args) {
GStack<Character> opStk = new GStack<>(); // Stack to store operators
ArrayListexp = newArrayList(); //ArrayList to store the intermediate notation formula
exp.add(2.0); exp.add('+'); exp.add(3.0); exp.add('*'); exp.add(2.0); exp.add('('); exp.add('('); exp.add(3.0); exp.add('-');
exp.add(2.0); exp.add(')'); exp.add('*'); exp.add(4.0); exp.add(')'); exp.add('+'); exp.add(5.0); exp.add('=');
ArrayList expr = newArrayList(); //ArrayList to store backward notation formulas
/********Middle patrol->Back patrol conversion**********/
for(int i=0; i<exp.size(); i++) {
If (exp.get(i) == "(") //Open is in parentheses '('), put it in the stack.
opStk.push((Character) exp.get(i));
else if(exp.get(i) == "+" || exp.get(i) == "-" || exp.get(i) == "*" || exp.get(i) == "/") {
if(priority((char) exp.get(i)) >= priority(opStk.peak()) { // when the current operator has a higher or equal priority than the one in the stack
expr.add(opStk.pop()); // Subtract the contained operator
opStk.push((char)exp.get(i)); //put the operator of the current position
}
else
opStk.push((char)exp.get(i)); // Otherwise, just insert the operator.
}
else if(exp.get(i) == ")") { // If you meet a closing parenthesis,
while(opStk.top != '(')
expr.add(opStk.pop(); // output until parentheses meet
}
else
exp.add(exp.get(i)); // output immediately (if operand)
}
while(((opStk.top!=0)) //Take out all that is left in the stack at the end of the tour.
expr.add(opStk.pop());
for(int j=0; j<expr.size(); j++) {
System.out.print(expr.get(j));
}
/*****************************************
double result;
GStack<Double> vstk = new GStack<>(); //stack to store operands
for (int i=0; i<expr.size(); i++) {
if(expr.get(i) instance of Double) { //push to operand surface stack
vStk.push((Double) expr.get(i));
}
else if(expr.get(i) == "+" || expr.get(i) == "-" || expr.get(i) == "*" || expr.get(i) == "/") { // operator to compute two times and push each other to the stack
double d1 = vStk.pop();
double d2 = vStk.pop();
if(expr.get(i) == "+")
vStk.push((d2+d1));
if(expr.get(i) == "-")
vStk.push((d2-d1));
if(expr.get(i) == "*")
vStk.push((d2*d1));
if(expr.get(i) == "/")
vStk.push((d2/d1));
}
else
System.out.print(" ");
}
result = vstk.pop(); // The last value remaining in the stack is the result, where NullPointerException
System.out.print(result);
}
}
We recommend that you try debugging and replace the code with an exception statement.
© 2024 OneMinuteCode. All rights reserved.