Skip to content

Instantly share code, notes, and snippets.

@icalik
Created October 25, 2015 15:46
Show Gist options
  • Save icalik/fcdb08fb6f215791be85 to your computer and use it in GitHub Desktop.
Save icalik/fcdb08fb6f215791be85 to your computer and use it in GitHub Desktop.
Postfix to Infix Converter
import java.util.Scanner;
import java.util.Stack;
public class PostfixConverter {
private static Scanner sc;
private boolean isOperator(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
return true;
return false;
}
public String convert(String postfix) {
Stack<String> s = new Stack<>();
for (int i = 0; i < postfix.length(); i++) {
char c = postfix.charAt(i);
if (isOperator(c)) {
String b = s.pop();
String a = s.pop();
s.push("(" + a + c + b + ")");
} else
s.push("" + c);
}
return s.pop();
}
public static void main(String[] args) {
PostfixConverter obj = new PostfixConverter();
sc = new Scanner(System.in);
System.out.print("Postfix ifadeyi giriniz : ");
String postfix = sc.next(); // abc++
System.out.println("Infix ifadeyi giriniz : " + obj.convert(postfix));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment