Modul 1 Jarkom silahkan download disini
Modul 2 Jarkom silahkan download disini
Selasa, 30 April 2013
Jumat, 26 April 2013
Tugas Pendahuluan Jaringan Komputer
INFIX TO POSTFIX (Struktur Data)
package javaapplication24;
import java.util.Scanner;
public class InToPost {
private Stack theStack;
private String input;
private String output = "";
public InToPost(String in) {
input = in;
int stackSize = input.length();
theStack = new Stack(stackSize);
}
public String cekoperator() {
for (int j = 0; j < input.length();
j++) {
char cek = input.charAt(j);
switch (cek) {
case '+':
case '-':
gotOper(cek, 1);
break;
case '*':
case '/':
gotOper(cek, 2);
break;
case '^':
case '(':
theStack.push(cek);
break;
case ')':
gotParen(cek);
break;
default:
output = output + cek;
break;
}
}
while (!theStack.isEmpty()) {
output = output + theStack.pop();
}
return output;
}
public void gotOper(char opThis, int cek1) {
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') {
theStack.push(opTop);
break;
}
else {
int cek2;
if (opTop == '+' || opTop == '-')
cek2 =1;
else
cek2 = 2;
if (cek2 < cek1)
{
theStack.push(opTop);
break;
} else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char cek ){
while (!theStack.isEmpty()) {
char pop = theStack.pop();
if (pop == '(')
break;
else
output = output +pop;
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Masukkan nilai
infix : ");
String a = input.next();
String output;
System.out.println();
InToPost operator = new InToPost(a);
output = operator.cekoperator();
System.out.println("Hasil postfix :
" + output);
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
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);
}
}
}
QUEUE (Struktur Data)
package coba;
import java.util.Scanner;
class queue{
int data[]=new int[5];
int head;
int tail;
}
public class Coba {
static queue s = new queue();
static void create(){
s.head=s.tail=-1;
}
static int isEmpty(){
if(s.tail==-1){
return 1;
}else{
return 0;
}
}
static int isFull(){
if(s.tail==4){
return 1;
}else{
return 0;
}
}
static void Enqueue(int isi){
if(isEmpty()==1){
s.head=s.tail=0;
s.data[s.tail]=isi;
}else if(isFull()==0){
s.tail++;
s.data[s.tail]=isi;
}
}
static int Dequeue(){
int e=s.data[s.head];
for(int i=s.head; i<s.tail; i++){
s.data[i]=s.data[i+1];
}
s.tail--;
return e;
}
static void print(){
if(isEmpty()==0){
for(int j=s.head; j<=s.tail; j++){
System.out.println(s.data[j]+" ");
}
}else{
System.out.println("kosong");
}
}
static int total(){
int t=0;
for(int i=s.head; i<=s.tail; i++){
t += s.data[i];
}
return t;
}
static int max(){
int m = s.data[s.head];
for(int i=s.head; i<=s.tail; i++){
if(s.data[i] > m){
m=s.data[i];
}
}
return m;
}
static int min(){
int m = s.data[s.head];
for(int i=s.head; i<=s.tail; i++){
if(s.data[i] < m){
m=s.data[i];
}
}
return m;
}
static double rata(){
double r;
double t=0;
for(int i=s.head; i<=s.tail; i++){
t += s.data[i];
}
r = t / (s.tail+1);
return r;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int pil=0;
Coba q = new Coba();
q.create();
do{
System.out.println("Menu");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Tampilkan");
System.out.println("4. Keluar");
System.out.print("\tPilihan Anda : ");
pil = in.nextInt();
switch (pil){
case 1 : System.out.print("\tMasukkan Nilai : ");
int x = in.nextInt();
q.Enqueue(x);
System.out.println("\tTotal = "+q.total());
System.out.println("\tTerbesar = "+q.max());
System.out.println("\tTerkecil = "+q.min());
System.out.println("\tRata-rata = "+q.rata());
break;
case 2 : q.Dequeue();
break;
case 3 : q.print();
break;
}
}while(pil != 4);
}
}
Langganan:
Postingan (Atom)