Ubah Ekspresi Infiks Menjadi Notasi Postfix
Infix adalah cara penulisan ungkapan dengan meletakkan operator di antara dua operand dalam hal ini pemakaian tanda kurung sangat menentukan hasil operasi.
Contoh pemakaian infix adalah A+B, A+B-C, (A+B)*(C-D).
Postfix adalah metode penulisan dengan menuliskan operator setelah operand dan tanpa menuliskan tanda kurung.
Salah satu contoh proses pengubahan infix menjadi postfix dari karakter:
( A + B ) / (( C – D ) * E ^ F)
1. Main
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class Main {
- 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;
- ToPostfix theTrans = new ToPostfix(input);
- output = theTrans.trabslation();
- 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;
- }
- }
2. Queue
- public class Queue {
- private int size;
- char queueArr[];
- int front;
- int rear;
- int currentSize = 0;
- public Queue(int size) {
- this.size = size;
- front = 0;
- rear = -1;
- queueArr = new char[this.size];
- }
- public void enqueue(char data) {
- if (!isFull()){
- rear++;
- if (rear == size) {
- rear = 0;
- }
- queueArr[rear] = data;
- currentSize++;
- }
- }
- public char dequeue() {
- char fr = 0;
- if (!isEmpty()){
- fr = queueArr[front];
- front++;
- if (front == size) {
- front = 0;
- }
- currentSize--;
- return fr;
- }
- return fr;
- }
- public boolean isFull() {
- if (currentSize == size) {
- return true;
- }
- return false;
- }
- public boolean isEmpty() {
- if (currentSize == 0) {
- return true;
- }
- return false;
- }
- public String getString(){
- String str = "";
- while(!isEmpty()){
- str = str + dequeue();
- }
- return str;
- }
- }
3. Stack
- class Stack
- {
- private int maxSize;
- private char[] stackArray;
- private int top;
- public Stack(int s){
- maxSize = s;
- stackArray = new char[maxSize];
- top = -1;
- }
- public void push(char j){
- stackArray[++top] = j;
- }
- 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];
- }
- }
4. ToPostfix
- class ToPostfix{
- private Stack theStack;
- private String input;
- private Queue queue;
- public ToPostfix(String in){
- input = in;
- int stackSize = input.length();
- int queueSize = input.length();
- queue = new Queue(queueSize);
- theStack = new Stack(stackSize);
- }
- public String trabslation(){
- for(int j=0; j<input.length(); j++){
- char ch = input.charAt(j);
- switch(ch){
- case '+':
- case '-':
- operand(ch, 1);
- break;
- case '*':
- case '/':
- operand(ch, 2);
- break;
- case '(':
- theStack.push(ch);
- break;
- case ')':
- parent(ch);
- break;
- default:
- queue.enqueue(ch);
- break;
- }
- }
- while( !theStack.isEmpty()){
- queue.enqueue(theStack.pop());
- }
- return queue.getString();
- }
- public void operand(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 parent(char ch){
- while( !theStack.isEmpty()){
- char chx = theStack.pop();
- if( chx == '(' )
- break;
- else
- queue.enqueue(chx);
- }
- }
- }
Output
Komentar
Posting Komentar