Created
May 8, 2020 21:41
-
-
Save TheGloriousGenesis/ec4938317b45065af3c44cef1791fb0c to your computer and use it in GitHub Desktop.
Updated version of Infix-Postfix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// could also have a string that contains operators and check if contains; | |
public static HashMap<Character, Integer> operators = new HashMap<>(); | |
// only push to stack if order higher | |
public static Stack<Character> operatorStack = new Stack<Character>(); | |
public static StringBuilder postFixNotation = new StringBuilder(); | |
public InfixPostFixAlgo() { | |
// assign numerical order depending on order of operator in BODMAS | |
operators.put('^', 5); | |
operators.put('/', 4); | |
operators.put('*', 3); | |
operators.put('+', 2); | |
operators.put('-', 1); | |
} | |
private static String infixToPostfix(final String equation){ | |
int pointer = 0; | |
char[] equationChars = equation.toCharArray(); | |
while (pointer < equationChars.length) { | |
char currentChar = equationChars[pointer]; | |
if (currentChar == ' ') { | |
pointer++; | |
continue; | |
} | |
if (currentChar == '(') { | |
operatorStack.add(currentChar); | |
} else if (isOperator(currentChar)) { | |
while(!operatorStack.isEmpty() && checkPrecedence(checkStack()) >= checkPrecedence(currentChar)) { | |
postFixNotation.append(operatorStack.pop()); | |
} | |
operatorStack.add(currentChar); | |
} else if (currentChar == ')'){ | |
while(!operatorStack.isEmpty() && checkStack() != '(') { | |
postFixNotation.append(operatorStack.pop()); | |
} | |
operatorStack.pop(); | |
} else { | |
postFixNotation.append(currentChar); | |
} | |
pointer++; | |
} | |
for (int i=0; i<= operatorStack.size(); i++) { | |
postFixNotation.append(operatorStack.pop()); | |
} | |
return postFixNotation.toString(); | |
} | |
private static int checkPrecedence(final char value) { | |
return operators.getOrDefault(value, -1); | |
} | |
private static boolean isOperator(final char value) { | |
return operators.containsKey(value); | |
} | |
private static char checkStack() { | |
if (!operatorStack.isEmpty()) { | |
return operatorStack.peek(); | |
} | |
return ' '; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment