1

the result and everything was working for any input other than power(^) and modulo(%) operator can anyone tell me where my mistake is. here is my Github link: https://github.com/mikiyas-ahmed/RPN/tree/master/RPN/src

if the input is 3*(2+3)-7+8*8 it works correctly

if the input is 8%2 or 2^3 it gives me an error

the postfix and calculator class is here



import java.util.ArrayList;


public class Calculator{

    public double EvaluatePostfix(ArrayList<String> postfix) {

        Stack result = new Stack();
        for (int i = 0; i < postfix.size(); i++) {
            String token=postfix.get(i);
            if(tryParseInt(token)){
                result.push(Double.parseDouble(token));
            }
            else {
                double operand1=result.pop();

                double operand2=result.pop();

                System.out.println(operand2);
                System.out.println(operand1);
                System.out.println(token);

                double calc= calculate(operand1, operand2, token);
                System.out.println(calc);System.out.println();
                result.push(calc);
            }

        }
        return result.pop();
    }

    private static double  calculate(double operand1, double operand2, String token) {
        double result = 0.0;
        switch (token) 
        {
        case "+":
            result= operand2 + operand1;
            return result;
        case "-":
            result= operand2 - operand1;
            return result;
        case "/":
            result= operand2 / operand1;
            return result;
        case "*":
            result= operand2 * operand1;
            return result;
        case "%":
            result=operand2 % operand1;
            return result;
        case "^":
            result=operand1;
            for(int i=1; i<= operand2; i++) {
            result= result * operand1;}
            return result;
        }
        return result;
    }
    public boolean tryParseInt(String s) {  
        try {  
            Double.parseDouble(s);  
            return true;  
        } catch (NumberFormatException e) {  
            return false;  
        }  
    }
}

class Stack { 
        static final int MAX = 10; 
        int top; 
        double a[] = new double[MAX]; // Maximum size of Stack 

        boolean isEmpty() 
        { 
            return (top < 0); 
        } 
        Stack() 
        { 
            top = -1; 
        } 

        boolean push(double d){ 
            if (top >= (MAX - 1)) { 
                System.out.println("Stack Overflow"); 
                return false; 
            } 
            else { 
                a[++top] = d; 
                System.out.println(d + " pushed into stack"); 
                return true; 
            } 
        } 

        double pop() 
        { 
            if (top < 0) { 
                System.out.println("Stack Underflow"); 
                return 0; 
            } 
            else { 
                double x = a[top--]; 
                return x; 
            } 
        } 

        double peek() 
        { 
            if (top < 0) { 
                System.out.println("Stack Underflow"); 
                return 0; 
            } 
            else { 
                double x = a[top]; 
                return x; 
            } 
        } 
} 







import java.util.ArrayList;


public class PostfixCreater {

    private enum Precedence
    {
        lp(0), rp(1), add(2), minus(3), divide(4), mult(5), mod(6),pow(7), noth(8), number(9);

        private int index;
        Precedence(int index)
        {

            this.index = index;
        }
        public int getIndex()
        {
            return index;
        }        
    } 
    /** in stack precedence **/
    private static final int[] isp = {0, 19, 12, 12, 13, 13, 13, 14, 0};
    /** incoming character precedence **/
    private static final int[] icp = {20, 19, 12, 12, 13, 13, 13, 14, 0};
    /** operators **/
    private static final String[] operators = {"{", "}", "+", "-", "/", "*", "%", "^", " "};

    public Precedence getToken(String symbol)
    {
        switch (symbol)
        {
        case "("  : return Precedence.lp;
        case ")"  : return Precedence.rp;
        case "+"  : return Precedence.add;
        case "-"  : return Precedence.minus;
        case "/"  : return Precedence.divide;
        case "*"  : return Precedence.mult;
        case "%"  : return Precedence.mod;
        case "^"  : return Precedence.pow;
        case " "  : return Precedence.noth;
        default   : return Precedence.number;
        }
    }

    public boolean tryParseInt(String s) {  
        try {  
            Double.parseDouble(s);  
            return true;  
        } catch (NumberFormatException e) {  
            return false;  
        }  
    }
    public ArrayList<String> postfix(String infix)
    {
        ArrayList<String> postfix =new ArrayList<String>();
        String[] t = infix.split("(?<=[-+*/()])|(?=[-+*/()])");
        Stack stack = new Stack();
        Precedence pretoken;
        for (int i = 0; i < t.length; i++)
        {

            String token =t[i]; 
            pretoken=getToken(t[i]);

            /** if token is operand append to postfix **/
            if (tryParseInt(token)){
                postfix.add(t[i]);
            }
            /** if token is right parenthesis pop till matching left parenthesis **/

            else if(pretoken==Precedence.rp) {
                while (stack.peek() != Precedence.lp)
                    postfix.add(operators[stack.pop().getIndex()]);
                /** discard left parenthesis **/
                stack.pop();
            }

            else {
                System.out.print(pretoken.getIndex());
                System.out.println();
                while (!stack.isEmpty() && isp[stack.peek().getIndex()] >= icp[pretoken.getIndex()])
                {
                    postfix.add(operators[stack.pop().getIndex()]);
                }
                stack.push(pretoken);
            }


        }

        while(!stack.isEmpty())
            postfix.add(operators[stack.pop().getIndex()]);
        return postfix;
    }


    class Stack { 
        static final int MAX = 10; 
        int top; 
        Precedence a[] = new Precedence[MAX]; // Maximum size of Stack 

        boolean isEmpty() 
        { 
            return (top < 0); 
        } 
        Stack() 
        { 
            top = -1; 
        } 

        boolean push(Precedence x) 
        { 
            if (top >= (MAX - 1)) { 
                System.out.println("Stack Overflow"); 
                return false; 
            } 
            else { 
                a[++top] = x; 
                System.out.println(x + " pushed into stack"); 
                return true; 
            } 
        } 

        Precedence pop() 
        { 
            //          if (top < 0) { 
            //              System.out.println("Stack Underflow"); 
            //              return 0; 
            //          } 
            //          else { 
            Precedence x = a[top--]; 
            return x; 
            //          } 
        } 

        Precedence peek() 
        { 
            //          if (top < 0) { 
            //              System.out.println("Stack Underflow"); 
            //              return 0; 
            //          } 
            //          else { 
            Precedence x = a[top]; 
            return x; 
            //          } 
        } 
    } 




}


maik_eshe
  • 19
  • 4
  • Change Your regex to `String[] t = infix.split("(?<=[-+*/()^%])|(?=[-+*/()^%])");`. (^ and % forgotten) Now Your calculator should work. There are problems with the precedence. For cross-checks of the results take a look [here](https://7th-stone.com/index.php?id=14). I would print the `postfix` as `postfix.stream().collect(joining(" "))` _(w/o commas)_ So You could cut and paste Your RPN expressions for testing in another calculator and it just looks better. – Kaplan Dec 20 '20 at 13:15

0 Answers0