MEMBUAT MYSTACK DAN PENGAPLIKASIANYA DALAM KONVERSI INFIX

stack adalah variabel dalam java yang merupakan abstrak dari pertumbukan.

ini adalah contoh code stack :

class MyStack {

    String data[];
    int ToS;
    boolean empty,full;
   
    public MyStack(int n){
    data = new String [n]; ToS = -1; empty = true ; full = false;
    }
   
    public String pop(){
    if(!isEmpty()) return (data[ToS--]);
    else
    System.out.print("Stack is empty");
    return ("");
    }
   
    public void push(String n) {
    if (!isFull()) data[++ToS] = n;
    else System.out.print("Stack is full");
    }
   
    public boolean isEmpty(){
        if (ToS<0) return (true);
        else return (false);
    }
   
    public boolean isFull(){
        if (ToS>=data.length-1) return (true);
        else return (false);
   
    }}




sedangkan ini adalah contoh pengaplikasianya.
yaitu dengan mengubah metode penjumlahan infix menjadi postfix :

import java.util.*;
class InToPost {

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Masukkan ekspresi infix : ");
        String infix = sc.nextLine();
        System.out.println(" ");
       
        int l = infix.length(), x;
        String postfix = "";
        String t = "", u = "";
        MyStack s = new    MyStack(l);

        for(int i = 0; i<l; i++) {
            t = infix.substring(i,i+1);
            try {
                x=Integer.parseInt(t);
                postfix+=t;
            } catch(Exception e) {
                if(s.isEmpty() && (isOperator(t) || t.equals("(") || t.equals(")"))) s.push(t);
                else {
                    if(isOperator(t)) {
                        u=s.pop();
                        if(!isOperator(u)) {
                            s.push(u); s.push(t);
                        } else if(greater(t,u)) {
                            s.push(u); s.push(t);
                        } else {
                            postfix+=t; s.push(u);
                        }
                    } else if(t.equals("(")) {
                        s.push(t);
                    } else if(t.equals(")")) {
                        u=s.pop();
                        while(!u.equals("(")) {
                              postfix+=u;
                            u=s.pop();
                        }
                    }
                }
            }
        }

        while(!s.isEmpty()) postfix+=s.pop();

        System.out.println("Hasil postfix adalah : "+postfix);
    }   

    public static boolean isOperator(String n) {
        if(n.equals("+") || n.equals("-") || n.equals("*") || n.equals("/")) return(true);
        return(false);   
    }

    public static boolean greater(String n, String m) {
        if(m.equals("*") || m.equals("/")) return(false);
        else if(n.equals("*") || n.equals("/")) return(true);
        return(false);
    }

Untuk Download Artikel Klik Gambar

Tidak ada komentar:

Posting Komentar

 
Powered By irul ganteng | Portal Design By irul nuno © 2009 | Resolution: 1024x768px | Best View: Firefox | Top