SOURCE CODE EKPRESI POSTFIX MENGGUNAKAN NETBEANS
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Fatur
*/
public class Postfix {
/**
* This enum data structure is defined to encapsulate
* conceptual operations from concrete symbol representations
*/
public enum Operation {TAMBAH, KURANG, KALI, BAGI, PANGKAT};
/**
* This HashMap ADT is defined to connect the
* conceptual operations and the corresponding symbol representations
*/
public static HashMap<String, Operation> operators =
new HashMap<String, Operation>();
static {
operators.put("+", Operation.TAMBAH);
operators.put("-", Operation.KURANG);
operators.put("*", Operation.KALI);
operators.put("/", Operation.BAGI);
operators.put("^", Operation.PANGKAT);
}
/**
* operand stack, the core data structure in the algorithm
*/
private Stack<Integer> stack;
/**
* Constructor, only to initialze the stack
*/
public Postfix(){
stack = new Stack<Integer>();
}
/**
* Evaluate a postfix expression in String, return
* the evaluated result in Integer
* @param Ekspresipostfix
* @return
*/
public Integer evaluasi(String Ekspresipostfix){
// tokenize the string first
LinkedList<Object> tokens = tokenize(Ekspresipostfix);
// process each item in the LinkedList
for(Object item : tokens){
// separate the current item into two categories
if (item instanceof Operation) {
/**
* for operators, pop the top two operands from the stack,
* calculate the result of the single operation, and push
* it to the top of the stack
*/
stack.push(operateSingle(stack.pop(), stack.pop(), (Operation)item) );
} else {
/**
* for operands, push it to the stack
*/
stack.push((Integer)item);
}
/**
* This line is for debugging purpose to demonstrate the intermediate status
* of the operand stack
*/
//System.out.println(stack+"");
}
// the current top item in the stack should be the result
return stack.pop();
}
/**
* This method takes the advantage of the HashMap ADT to separate
* each String token into its corresponding type, then store the converted
* token into a LinkedList ADT regardless of how many tokens there are,
* and finally returns the resulting list.
* @param input
* @return
*/
private LinkedList<Object> tokenize(String input){
Scanner tokens = new Scanner(input);
LinkedList<Object> list = new LinkedList<Object>();
String token;
while (tokens.hasNext()){
token = tokens.next();
if (operators.containsKey(token)) {
list.add(operators.get(token));
} else {
list.add(Integer.valueOf(token));
}
}
return list;
}
/**
* This method simply carry calculation of a specific operation
* @param op2
* @param op1
* @param operation
* @return
*/
private Integer operateSingle(Integer op2, Integer op1, Operation operation) {
switch(operation) {
case TAMBAH:
return op1+op2;
case KURANG:
return op1-op2;
case KALI:
return op1*op2;
case BAGI:
return op1/op2;
case PANGKAT:
return (int)(Math.pow((double)op1, (double)op2));
}
return null;
}
/**
* The driver method for testing
* @param args
*/
public static void main(String[] args){
String exp = "";
Scanner scan = new Scanner(System.in);
Postfix postfix = new Postfix();
while(true){
System.out.println("Masukkan ekspresi postfix");
System.out.println("Atau tekan Q untuk keluar");
exp = scan.nextLine();
if (exp.equals("Q"))
break;
System.out.println("Hasilnya adalah : " + postfix.evaluasi(exp));
}
// System.out.println("So long...");
}
}
Tidak ada komentar:
Posting Komentar