0

infix to postfix (existing only +, -, *, / ) need to add one more condition(to the power of " ^ ") into this coding

public static boolean operator(String s) { if(s.equals("+")) return true; else if(s.equals("-")) return true; else if(s.equals("*")) return true; else if(s.equals("/")) return true; else return false; } public static boolean precedence(String s1,String s2) { int precedence1 = 0; int precedence2 = 0; if(s1.equals("+") || s1.equals("-")) { precedence1 = 1; } else if(s1.equals("*") || s1.equals("/")) { precedence1 = 2; } if(s2.equals("+") || s2.equals("-")) { precedence2 = 1; } else if(s2.equals("*") || s2.equals("/")) { precedence2 = 2; } if(precedence1 > precedence2 || precedence1 == precedence2) { return true; } else return false; } public static int evaluatePostFix(String string) { Stack<Integer> stack = new Stack<Integer>(); int value = 0; String[] s = string.split(" "); for(int i = 0 ; i<s.length;i++) { if(!operator(s[i]) && !s[i].isEmpty()) { stack.push(Integer.parseInt(s[i])); } else if(operator(s[i])) { int op1 = stack.pop(); int op2 = stack.pop(); int value2 = 0; switch(s[i]) { case "+": value2 = op1 + op2; break; case "-": value2 = op2 - op1; break; case "*": value2 = op1 * op2; break; case "/":

14th Apr 2018, 6:34 PM
Minion Jin
Minion Jin - avatar
1 ответ
0
u gave not much explanation :/
14th Apr 2018, 7:23 PM
Farshaad Heydari
Farshaad Heydari - avatar