Ekspresi Infix ke Ekspresi Postfix Menggunakan Konsep Queue Pada Pemograman Java
Dalam struktur data yang kita pelajari secara umum ada 3 notasi operasi yang dilakukan untuk suatu operasi aritmatika,yaitu Prefix,Infix,dan postfix.Dan untuk mengetahui notasi-notasi yang diatas itu,sebelumnya kita harus mengenal dan mengetahui indikator yang ada di notasi itu tersebut.Notasi ini terbentuk dari Operand dan Operator.Operand adalah data atau nilai yang membantu dalam proses,sedangkan Operasi adalah fungsi yang digunakan dalam proses.
contohnya:
A+B*C
2 + 5 * 3
Keterangan: A ,B ,C ,2 ,3 ,5 adalah Operand.
+,* adalah Operator.
Setelah kita mengenal dan mengetahui dengan Operand dan Operator, maka mari kita mengenal juga tingkat/ level yang ada didalam notasi tersebut:
-( ) (Kurung).
- ^ (Pangkat).
- * / (Perkalian / Pembagian).
- + - (Penjumlahan / Pengurangan).
Notasi ada 3 jenis, yaitu Prefix,Infix dan Postfix yang seperti kita ketahui di atas:
Infix adalah notasi yang membentuk atas operator dengan operand,dimana operator berada diantara operand.
Contoh :
- A + B * C
- (A + B) * C
- A - (B + C) * D ^ E
Postfix adalah notasi yang membentuk atas operator dengan operand, dimana operator berada dibelakang operand.
Contoh : A + B * C ( infix).
maka notasi postfix adalah ABC*+.
Pemecahannya:
A + B * C
Diketahui ada 3 operand yaitu : A,B,C dan 2 operator yaitu : +, *. proses dimulai dengan melihat dari hirarkhi operator.Contoh diatas operator yang tertinggi adalah * kemudian +.
Berikut Pemograman Stack:
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
/** | |
* StackMetode | |
* | |
* By Afira Rolobessy | |
* 27 Apr 21 | |
*/ | |
public class Stack | |
{ | |
private int maxSize; | |
private char[] stackArray; | |
private int top; | |
public Stack(int A) | |
{ | |
maxSize = A; | |
stackArray = new char[maxSize]; | |
top = -1; | |
} | |
public void push(char jv) | |
{ stackArray[++top] = jv; } | |
public char pop() | |
{ return stackArray[top--]; } | |
public char peek() | |
{ return stackArray[top]; } | |
public boolean isEmpty() | |
{ return (top ==-1); } | |
public int size() | |
{ return top+1; } | |
public char peekN(int n) | |
{ return stackArray[n]; } | |
} |
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
/** | |
* Queue Methods | |
* | |
* By Afira | |
* 27 Apr 21 | |
*/ | |
public class Queue { | |
private int jv; | |
char queueArr[]; | |
int front; | |
int rear; | |
int sizequeue= 0; | |
public Queue(int Queue) { | |
this.jv = Queue; | |
front = 0; | |
rear = -1; | |
queueArr = new char[this.jv]; | |
} | |
public void enqueue(char data) { | |
if (!isFull()){ | |
rear++; | |
if (rear == jv) | |
rear = 0; | |
queueArr[rear] = data; | |
sizequeue++; | |
} | |
} | |
public char dequeue() { | |
char fr = 0; | |
if (!isEmpty()){ | |
fr = queueArr[front]; | |
front++; | |
if (front == jv) | |
front = 0; | |
sizequeue--; | |
return fr; | |
} | |
return fr; | |
} | |
public boolean isFull() { | |
if (sizequeue == jv) { | |
return true; | |
} | |
return false; | |
} | |
public boolean isEmpty() { | |
if (sizequeue == 0) { | |
return true;} | |
return false; | |
} | |
public String getString(){ | |
String Q = ""; | |
while(!isEmpty()){ | |
Q = Q + dequeue(); | |
} | |
return Q; | |
} | |
} |
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
/** | |
* Mewakili Struktur data queue | |
* | |
* By Afira | |
* 27 Apr 21 | |
*/ | |
public class InToPost | |
{ | |
private Stack theStack; | |
private String input; | |
private Queue queue; | |
public InToPost(String in) | |
{ | |
input = in; | |
int stackSize = input.length(); | |
int queueSize = input.length(); | |
queue = new Queue(queueSize); | |
theStack = new Stack(stackSize); | |
} | |
public String doTrans() | |
{ | |
for(int j=0; j<input.length(); j++) | |
{ | |
char ch = input.charAt(j); | |
switch(ch) | |
{ | |
case '+': | |
case '-': | |
gotOper(ch, 1); | |
break; | |
case '*': | |
case '/': | |
gotOper(ch, 2); | |
break; | |
case '(': | |
theStack.push(ch); | |
break; | |
case ')': | |
gotParen(ch); | |
break; | |
default: | |
queue.enqueue(ch); | |
break; | |
} | |
} | |
while( !theStack.isEmpty() ) | |
{ | |
queue.enqueue(theStack.pop()); | |
} | |
return queue.getString(); | |
} | |
public void gotOper(char opThis, int prec1) | |
{ | |
while( !theStack.isEmpty() ) | |
{ | |
char opTop = theStack.pop(); | |
if( opTop == '(' ) | |
{ | |
theStack.push(opTop); | |
break; | |
} | |
else | |
{ | |
int prec2; | |
if(opTop=='+' || opTop=='-') | |
prec2 = 1; | |
else | |
prec2 = 2; | |
if(prec2 < prec1) | |
{ | |
theStack.push(opTop); | |
break; | |
} | |
else | |
queue.enqueue(opTop); | |
} | |
} | |
theStack.push(opThis); | |
} | |
public void gotParen(char ch) | |
{ | |
while( !theStack.isEmpty() ) | |
{ | |
char chx = theStack.pop(); | |
if( chx == '(' ) | |
break; | |
else | |
queue.enqueue(chx); | |
} | |
} | |
} |
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
/** | |
*App | |
* | |
* By Afira | |
* 27 Apr 21 | |
*/ | |
import java.io.*; | |
public class App | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
String input, output; | |
while(true) | |
{ | |
System.out.print("Enter infix: "); | |
System.out.flush(); | |
input = getString(); | |
if( input.equals("") ) | |
break; | |
InToPost theTrans = new InToPost(input); | |
output = theTrans.doTrans(); | |
System.out.println("Postfix is " + output + '\n'); | |
} | |
} | |
public static String getString() throws IOException | |
{ | |
InputStreamReader isr = new InputStreamReader(System.in); | |
BufferedReader br = new BufferedReader(isr); | |
String s = br.readLine(); | |
return s; | |
} | |
} |
Komentar
Posting Komentar