Assalamualaikum Wr. Wb.
I think this first time , i'm writing on my blog with english language. It looks the way i write a very stiff and formal, hehe. Besides being able to improve my english skill, it's as my exercise in preparing my thesis later . Of course, with the help of google translate i can do this :D .
In data structure course, infix, postfix and prefix must be learned by you. So, you must know what the definitions and differences of all.
You can visit this link, for more information about it. Oh iya, one thing i learned from structure data course is ' how to store data in memory as small as possible and how to make the process as quickly as possible ' . After you visit and read the link i provided earlier, at least you have basic about infix, postfix, and prefix . Here, i wanna share coding about how to convert the notation infix into postfix with Java language. So, check this out and hope can be useful for you.
In data structure course, infix, postfix and prefix must be learned by you. So, you must know what the definitions and differences of all.
You can visit this link, for more information about it. Oh iya, one thing i learned from structure data course is ' how to store data in memory as small as possible and how to make the process as quickly as possible ' . After you visit and read the link i provided earlier, at least you have basic about infix, postfix, and prefix . Here, i wanna share coding about how to convert the notation infix into postfix with Java language. So, check this out and hope can be useful for you.
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 javax.swing.JOptionPane; //pengimportan window penginputan dan message dialog | |
public class InfPost { | |
private Stack stack; | |
private String input; | |
private String output = ""; | |
public InfPost(String in) { | |
input = in; | |
int stackSize = input.length(); | |
stack = 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); // apabila ada char '+' atau '-' pada string input, maka alur program loncat menuju fungsi gotOper dengan nilai prec1 = 1 | |
break; // setelah selesai, alur program lanjut ke perulangan for dengan identifier j selanjutnya | |
case '*': | |
case '/': | |
gotOper(ch, 2); // apabila ada char '*' atau '/' pada string input, maka alur program loncat menuju fungsi gotOper dengan nilai prec1 = 2 | |
break; // setelah selesai, alur program lanjut ke perulangan for dengan identifier j selanjutnya | |
case '^': | |
gotOper(ch, 3); // apabila ada char '^' pada string input, maka alur program loncat menuju fungsi gotOper dengan nilai prec1 = 3 | |
break; // setelah selesai, alur program lanjut ke perulangan for dengan identifier j selanjutnya | |
case '(': | |
stack.push(ch); // apabila ada char '(' pada string input, maka char tersebut akan dimasukan pada stack | |
break; // setelah selesai, alur program lanjut ke perulangan for dengan identifier j selanjutnya | |
case ')': | |
gotParen(ch); // apabila ada char ')' pada string input, maka nilai char tersebut akan dibawa menuju fungsi gotParen | |
break; | |
default: | |
output = output + ch; | |
break; | |
} | |
} | |
while (!stack.isEmpty()) { | |
output = output + stack.pop(); // selama isi stack tidak kosong , isi stack dikeluarkan | |
} | |
return output; | |
} | |
public void gotOper(char opThis, int prec1) { // fungsi gotParen dengan format (nilai char, nilai int prec1 dari fungsi doTrans) | |
while (!stack.isEmpty()) { | |
char opTop = stack.pop(); | |
if (opTop == '(') { | |
stack.push(opTop); | |
break; | |
} | |
else { // disini terdapat pengutamaan tingkat operator | |
int prec2; | |
if (opTop == '+' || opTop == '-') | |
prec2 = 1; // nilai prec2 = 1, bila kondisi diatas terpenuhi | |
else if (opTop == '*' || opTop == '/') | |
prec2 = 2; // nilai prec2 = 2, bila kondisi diatas terpenuhi | |
else | |
prec2 = 3; // nilai prec2 = 3, untuk operasi '^' | |
if (prec2 < prec1) // disini ada kondisi bila nilai penggutamaan(prec/precedence) operator yang baru < dari yang lama | |
{ | |
stack.push(opTop); // operator yang lama keluar, yang baru ditambahkan ke stack | |
break; | |
} else // jika tidak lebih kecil | |
output = output + opTop; // maka operator yang lama dipertahankan | |
} | |
} | |
stack.push(opThis); | |
} | |
public void gotParen(char ch){ // fungsi gotParen dengan nilai char var. ch dari fungsi doTrans | |
while (!stack.isEmpty()) { | |
char chx = stack.pop(); | |
if (chx == '(') | |
break; | |
else | |
output = output + chx; | |
} | |
} | |
public static void main(String[] args){ | |
JOptionPane.showMessageDialog(null, "NB : lebih baik input notasi infix tanpa spasi !!!"); | |
String input = JOptionPane.showInputDialog(null, "Masukkan Notasi Infix : "); // running program berawal dari statement ini | |
if (input.equals("")){ // apabila nilai pd var. input kosong, | |
JOptionPane.showMessageDialog(null, "Silahkan Inputkan Kembali Notasi Infix !"); // maka ada pesan berikut | |
System.exit(0); | |
} | |
else{ | |
String output; | |
InfPost theTrans = new InfPost(input); // pemanggilan method baru bernama theTrans, dan menjalankan perintah pada fungsi InfPost | |
output = theTrans.doTrans(); //method theTrans tersebut menjalankan perintah pada fungsi doTrans, kemudian hasilnya disimpan pada var. output | |
JOptionPane.showMessageDialog(null,"Notasi Infix : "+input+"\n"+"Notasi Postfix : "+output); | |
System.exit(0); | |
} | |
} | |
class Stack { // kelas Stack | |
private int maxSize; | |
private char[] stackArray; | |
private int top; | |
public Stack(int max) { | |
maxSize = max; | |
stackArray = new char[maxSize]; | |
top = 0; | |
} | |
public void push(char j) { | |
stackArray[++top] = j; // Array pada stack akan bertambah nilai sebanyak identifier pada perulangan fungsi doTrans | |
} | |
public char pop() { | |
return stackArray[top--]; // Array pada stack akan berkurang | |
} | |
public boolean isEmpty() { | |
return (top == 0); // mengembalikan nilai top samadengan 0 | |
} | |
} | |
} | |
/* | |
* @author : denmasmr | |
* you can contact me on : denmasmridwan@gmail.com | |
*/ |
Thanks.
Wassalamualaikum Wr. Wb.
Artikel Terkait:
struktur data
share
- Guru Kami, Pahlawan Kami.
- Belajar Augmented Reality dengan Objek Beranimasi | Unity3D 4
- ::: Tutorial Saving Energy Web | OnlineLeaf
- ::: Aplikasi keren buat blog | Paint
- Apa aja sih dampak positif & Negatif Internet itu ?
- Ber-Internetan Bersih, Aman, dan Sehat | Nawala’s Project
- Belajar Augmented Reality dengan Vuforia Extension | Unity3D 4
- Learn queue with Java | denmased.
- Bung, anda adalah Manusia paling Menyebalkan yang paling saya Kagumi.
- Pengenalan dan Sejarah Internet
- ::: Aplikasi keren buat blog | DuckMyLife
- ::: Tutorial membuat artikel terkait / related post di blog
- KOMPETISI | Blogging / Writing & Semi SEO
- Kebebasan berekspresi via Internet