28/04/2021
MENGUBAH EKSPRESI INFIX KE POSTFIX
-
PENGERTIAN :
- INFIX
Infix yaitu notasi yang terbentuk atas operator dengan operand, dimana operator berada diantara operand. Notasi ini hanya dikenal oleh manusia dan selalu digunakan dalam perhitungan aritmatika.
Contoh : A + B * C
( A + B ) * C
A – ( B + C ) * D ^ E
- POSTFIX
Postfix yaitu notasi yang terbentuk atas operator dengan operand, dimana operator berada dibelakang operand. Notasi ini hanya dikenal oleh processor dan dipahami dalam ALU.
Contoh : A + B * C (Infix)
maka notasi postfixnya adalah ABC*+
Berikut source code infix ke postfix :
This file contains hidden or 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.Stack;
import java.util.LinkedList;
import java.util.Queue;
public class InfixPostfix {
static int Prec(char chrctr)
{
switch (chrctr)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static Queue<Character> infixToPostfix(String exp)
{
Stack<Character> stack = new Stack<>();
Queue<Character> result = new LinkedList<>();
for (int i = 0; i<exp.length(); ++i) {
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c)) {
result.add(c) ;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(')
result.add(stack.pop());
stack.pop();
} else {
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek())){
result.add(stack.pop());
}
stack.push(c);
}
}
while (!stack.isEmpty()){
if(stack.peek() == '(') {
System.out.println("Invalid Expression");
return result;
}
result.add(stack.pop());
}
return result;
}
public static void main(String[] args)
{
String exp = "a+b*(c^d-e)^(f+g*h)-i";
Queue<Character> result = infixToPostfix(exp);
System.out.println("Infix expression: " + exp);
System.out.print("Postfix expression: ");
while (!result.isEmpty()) {
System.out.print(result.poll());
}
}
}
Output :
This file contains hidden or 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.Stack; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
public class InfixPostfix { | |
static int Prec(char chrctr) | |
{ | |
switch (chrctr) | |
{ | |
case '+': | |
case '-': | |
return 1; | |
case '*': | |
case '/': | |
return 2; | |
case '^': | |
return 3; | |
} | |
return -1; | |
} | |
static Queue<Character> infixToPostfix(String exp) | |
{ | |
Stack<Character> stack = new Stack<>(); | |
Queue<Character> result = new LinkedList<>(); | |
for (int i = 0; i<exp.length(); ++i) { | |
char c = exp.charAt(i); | |
if (Character.isLetterOrDigit(c)) { | |
result.add(c) ; | |
} else if (c == '(') { | |
stack.push(c); | |
} else if (c == ')') { | |
while (!stack.isEmpty() && stack.peek() != '(') | |
result.add(stack.pop()); | |
stack.pop(); | |
} else { | |
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek())){ | |
result.add(stack.pop()); | |
} | |
stack.push(c); | |
} | |
} | |
while (!stack.isEmpty()){ | |
if(stack.peek() == '(') { | |
System.out.println("Invalid Expression"); | |
return result; | |
} | |
result.add(stack.pop()); | |
} | |
return result; | |
} | |
public static void main(String[] args) | |
{ | |
String exp = "a+b*(c^d-e)^(f+g*h)-i"; | |
Queue<Character> result = infixToPostfix(exp); | |
System.out.println("Infix expression: " + exp); | |
System.out.print("Postfix expression: "); | |
while (!result.isEmpty()) { | |
System.out.print(result.poll()); | |
} | |
} | |
} |
Komentar
Posting Komentar