Advertisement
Yesver08

PostfixToInfix.java

Oct 8th, 2021
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. package praktikum5;
  2.  
  3. import java.util.Stack;
  4.  
  5. public class PostfixToInfix {
  6.     public static void main(String[] args) {
  7.         String input = "1234+*+";
  8.         char[] persamaan = input.toCharArray();
  9.  
  10.         Stack<String> stack = new Stack<>();
  11.         for (int i = 0; i < persamaan.length; i++) {
  12.             char c = persamaan[i];
  13.             if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
  14.                 String c1 = stack.pop();
  15.                 String c2 = stack.pop();
  16.  
  17.                 String s = "(" + c1 + c + c2 + ")";
  18.                 stack.push(s);
  19.             }
  20.             else {
  21.                 stack.push(Character.toString(c));
  22.             }
  23.         }
  24.  
  25.         System.out.println(stack.pop());
  26.     }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement