Jump to content
  • Code and Algorithms


    andrewua
    Programming language: PHP
    import java.util.Stack; package calc; public class Calculator { public static String ExpressiontoRPN(String Expr) { String current = ""; Stack<Character> stack = new Stack<>(); int priority; for(int i = 0; i < Expr.length(); i++) { priority = getP(Expr.charAt(i)); if(priority == 0) current+=Expr.charAt(i); if(priority == 1)stack.push(Expr.charAt(i)); if(priority > 1) { current += ' '; while(!stack.empty()) { if(getP(stack.peek()) >= priority)current+=stack.pop(); else break; stack.push(Expr.charAt(i)); } if(priority == -1) { current +=' '; while(getP(stack.peek()) != 1)current += stack.pop(); stack.pop(); } } while(!stack.empty())current+=stack.pop(); return 0; } public static double RPNtoAnswer(String rpn){ return 0; } private static int getP(char token){ if(token == '*' || token == '/') return 3; else if(token == '+' || token == '-') return 2; else if(token == '(') return 1; else if(token == ')') return -1; else return 0; } }  

General chat

General chat

Please enter your display name

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.