Created
October 25, 2015 15:46
-
-
Save icalik/fcdb08fb6f215791be85 to your computer and use it in GitHub Desktop.
Postfix to Infix Converter
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
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