• Source
    1. import java.util.*;
    2.  
    3. class Main {
    4.  
    5. public static boolean isOperator(char c)
    6. {
    7. switch(c)
    8. {
    9. case '+':
    10. case '-':
    11. case '/':
    12. case '*':
    13. return true;
    14. default:
    15. }
    16.  
    17. return false;
    18. }
    19.  
    20. public static boolean isStartGroup(char c)
    21. {
    22. return c == '(';
    23. }
    24.  
    25. public static boolean isEndGroup(char c)
    26. {
    27. return c == ')';
    28. }
    29.  
    30. public static boolean isNumber(char c)
    31. {
    32. return Character.isDigit(c);
    33. }
    34.  
    35. public static String toInFix(String expression)
    36. {
    37. Stack<String> stack = new Stack<String>();
    38.  
    39. for (int x = 0; x < expression.length(); x++)
    40. {
    41. char c = expression.charAt(x);
    42.  
    43. if (isStartGroup(c))
    44. {
    45. continue;
    46. }
    47. else if ( isOperator(c) || isNumber(c))
    48. {
    49. stack.push(Character.toString(c));
    50. }
    51. else if (isEndGroup(c))
    52. {
    53. String arg2 = stack.pop();
    54. String oper = stack.pop();
    55. String arg1 = stack.pop();
    56.  
    57. stack.push(arg1 + " " + arg2 + " " + oper);
    58. }
    59. }
    60.  
    61. return stack.pop();
    62. }
    63.  
    64. public static void main(String args[]) {
    65. System.out.println(toInFix("(1+(2+3))"));
    66. System.out.println(toInFix("((1+2)+3)"));
    67. System.out.println(toInFix("(((1+2)+(3+4))+3)"));
    68. }
    69. }
    70.