pertama kita membuat mystack.java :
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);
}}
terus kita membuat intopost.java :
import java.util.*;
class HitungPostFix {
MyStack ss = new MyStack();
String postfix;
public HitungPostFix(String str)
{
postfix=str;
}
public boolean isOperand(char ch)
{
if(ch>='0' && ch<='9')
return true;
else
return false;
}
public int Hitung()
{
char po[]=postfix.toCharArray();
int i=0,a,b;
while (i<po.length)
{
if(isOperand(po[i]))
ss.push(po[i]-'0');
else
{
a=ss.pop();
b=ss.pop();
switch (po[i])
{
case '+' :ss.push(a+b);
break;
case '-' :ss.push(b-a);
break;
case '*' :ss.push(a*b);
break;
case '/' :ss.push(b/a);
break;
case '%' :ss.push(b%a);
break;
}
}
i++;
}
return(ss.pop());
}
}
public class intopost {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Masukkan sebuah ekspresi postfix : ");
String hasilscan = sc.nextLine();
System.out.println(" ");
HitungPostFix Hasilhitung=new HitungPostFix(hasilscan);
System.out.println("Hasilnya adalah :"+Hasilhitung.Hitung());
}
}
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);
}}
terus kita membuat intopost.java :
import java.util.*;
class HitungPostFix {
MyStack ss = new MyStack();
String postfix;
public HitungPostFix(String str)
{
postfix=str;
}
public boolean isOperand(char ch)
{
if(ch>='0' && ch<='9')
return true;
else
return false;
}
public int Hitung()
{
char po[]=postfix.toCharArray();
int i=0,a,b;
while (i<po.length)
{
if(isOperand(po[i]))
ss.push(po[i]-'0');
else
{
a=ss.pop();
b=ss.pop();
switch (po[i])
{
case '+' :ss.push(a+b);
break;
case '-' :ss.push(b-a);
break;
case '*' :ss.push(a*b);
break;
case '/' :ss.push(b/a);
break;
case '%' :ss.push(b%a);
break;
}
}
i++;
}
return(ss.pop());
}
}
public class intopost {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Masukkan sebuah ekspresi postfix : ");
String hasilscan = sc.nextLine();
System.out.println(" ");
HitungPostFix Hasilhitung=new HitungPostFix(hasilscan);
System.out.println("Hasilnya adalah :"+Hasilhitung.Hitung());
}
}
Untuk Download Artikel Klik Gambar
Tidak ada komentar:
Posting Komentar