• Tidak ada hasil yang ditemukan

Analisis Perbandingan Algoritma Huffman Dan Algoritma Sequtur Dalam Kompresi Data Text

N/A
N/A
Protected

Academic year: 2017

Membagikan "Analisis Perbandingan Algoritma Huffman Dan Algoritma Sequtur Dalam Kompresi Data Text"

Copied!
29
0
0

Teks penuh

(1)

LISTING PROGRAM KOMPRESI DAN DEKOMPRESI

Kompresi Huffman

Imports System.IO

Public Class frmK_Huffman

Public Stopwatch As Stopwatch = New Stopwatch

Public Log As String

Public Pjg_PT, Pjg_CT As Integer

Public Panjang_KS, JlhBit_PT, JlhBit_CT As Integer Public Karakter_Set As String

Public THuff As List(Of clsHuff) Public PT, CT, PTB, CTB As String Public PBytes(), CBytes() As Byte Public Input, Output As String

' ======================================================== FUNGSI UMUM ======================================================== '

Public Sub StopWatchStart() Stopwatch.Reset() Stopwatch.Start()

Cursor.Current = Cursors.WaitCursor txtWaktuProses.Text = ""

End Sub

Public Sub StopWatchStop() Stopwatch.Stop()

txtWaktuProses.Text = Stopwatch.Elapsed.ToString() Cursor.Current = Cursors.Arrow

End Sub

Public Function toBiner(ByVal desimal As Integer) As String Dim biner As String = ""

Dim buf As String = ""

Dim hasilBagi As Integer = desimal Dim p, sisa As Integer

While (hasilBagi > 1)

If (hasilBagi Mod 2 = 1) Then hasilBagi -= 1

hasilBagi = hasilBagi / 2 biner = "1" & biner Else

hasilBagi = hasilBagi / 2 biner = "0" & biner End If

End While

biner = hasilBagi & biner

p = biner.Length / 8

sisa = (8 - (biner.Length Mod 8)) Mod 8

For i = 1 To sisa buf &= "0"

Next

(2)

Return biner End Function

Public Function toDecimal(ByVal biner As String) As Integer Dim result As Integer = 0

Dim num As Integer = 0

For i = 1 To biner.Length

num = Convert.ToInt16(biner(i - 1)) - 48 If num <> 0 Then

result += Convert.ToInt16(Math.Pow(2, biner.Length - i)) * num End If

Next

Return result End Function

' ======================================================== HUFFMAN ======================================================== '

Public Function BuatKarakterSet(ByVal Teks As String) As String Dim R As String = ""

Dim Yes As Boolean

For i As Integer = 0 To Teks.Length - 1 Yes = False

For j As Integer = 0 To R.Length - 1 If Teks(i) = R(j) Then

Yes = True Continue For End If

Next

If Not Yes Then R &= Teks(i) End If

Next Return R End Function

Public Function HitungFreqKarakter(ByVal Teks As String, ByVal KS As String) As

List(Of clsHuff)

Dim R As List(Of clsHuff) = New List(Of clsHuff) Dim T As Integer

For j As Integer = 0 To KS.Length - 1 T = 0

For i As Integer = 0 To Teks.Length - 1 If KS(j) = Teks(i) Then

T += 1 End If Next

R.Add(New clsHuff(KS(j), T)) Next

Return R End Function

Public Function UrutkanKS(ByVal L As List(Of clsHuff)) As List(Of clsHuff) Dim Kar As Char

(3)

For j As Integer = 0 To L.Count - 2

For i As Integer = j + 1 To L.Count - 1 If L(j).Freq > L(i).Freq Then

Kar = L(j).Kar Freq = L(j).Freq L(j).Kar = L(i).Kar L(j).Freq = L(i).Freq L(i).Kar = Kar

L(i).Freq = Freq End If

Next Next Return L End Function

Public Function HuffmanTree(ByVal L As List(Of clsHuff)) As List(Of clsHuff) Dim i As Integer = 0

Dim Freq As Integer Dim Index As Integer

Dim L2 As List(Of clsHuff) = New List(Of clsHuff) While (i + 2 <= L.Count)

Freq = L(i).Freq + L(i + 1).Freq

For j As Integer = 0 To L.Count - 1 If Freq < L(j).Freq Then

L.Insert(j, New clsHuff("*", Freq)) 'Bentuk Parent dan Child

L(i).Parent = j L(i + 1).Parent = j Exit For

ElseIf j = L.Count - 1 Then

L.Insert(j + 1, New clsHuff("*", Freq)) L(i).Parent = j + 1

L(i + 1).Parent = j + 1 End If

Next i += 2 'Exit While End While

' Bentuk Kode

For j As Integer = 0 To L.Count - 1 Index = j

While L(Index).Parent <> 0 If (Index Mod 2 = 0) Then

L(j).Kode = "0" + L(j).Kode Else

L(j).Kode = "1" + L(j).Kode End If

Index = L(Index).Parent End While

Next

' Hilangkan Star

For j As Integer = 0 To L.Count - 1 If L(j).Kar <> "*" Then

L2.Add(L(j)) End If

(4)

Return L2 End Function

Private Function Kompres(ByVal Tabel As List(Of clsHuff), ByVal PT As String) As String

Dim R As String = ""

For i As Integer = 0 To PT.Count() - 1 For j As Integer = 0 To Tabel.Count - 1 If PT(i) = Tabel(j).Kar Then

R &= Tabel(j).Kode End If

Next Next Return R End Function

Private Function Dekompres(ByVal Tabel As List(Of clsHuff), ByVal CTB As String) As String

Dim R As String = ""

Log = ""

txtLog.Clear()

Dim S As String = ""

Dim KT As String Dim Kar As Char = "A"

Dim TC As Integer = 0 Dim Index As Integer = 0 Dim Ketemu As Boolean

For i As Integer = 0 To CTB.Count() - 1 S += CTB(i)

TC += 1 Ketemu = False

For j As Integer = 0 To Tabel.Count() - 1 Try

If (S = Tabel(j).Kode.Substring(0, TC)) Then KT = S

Kar = Tabel(j).Kar

Log &= "Bit " & Index & " ke " & (Index + TC - 1) & " = " & KT & vbCrLf

Ketemu = True Exit For End If

Catch ex As Exception

End Try Next

If (Ketemu = False) Then R &= Kar

S = ""

TC = 0 Index = i i -= 1

Log &= "============================== Karakter : " & Kar & vbCrLf & vbCrLf

(5)

Log &= "============================== Karakter : " & Kar & vbCrLf Return R

End Function

Public Function CetakTabel(ByVal Tabel As List(Of clsHuff)) Dim Log As String = ""

For i As Integer = 0 To Tabel.Count - 1

' Log &= (i) & vbTab & Tabel(i).Kar & vbTab & Tabel(i).Freq & vbTab & Tabel(i).Parent & vbTab & Tabel(i).Kode & vbCrLf

Log &= (i) & vbTab & Tabel(i).Kar & vbTab & Tabel(i).Freq & vbTab & Tabel(i).Kode & vbCrLf

Next

Return Log End Function

' ======================================================== EVENT HANDLER ======================================================== '

Private Sub btnCompress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKompres.Click

Dim ht As Integer Try

StopWatchStart()

Dim Log As String = vbCrLf & vbCrLf Dim Kar As String = ""

Dim Dec As Integer Dim Jlh As Integer

Dim Index As Integer = 0

CT = ""

PT = txtPT.Text

CTB = Kompres(THuff, PT) JlhBit_CT = CTB.Count()

ReDim CBytes(Math.Ceiling(CTB.Count() / 8)) 'Cetak versi 8 bit

For i As Integer = 0 To CTB.Count - 1 Step 8 If (i + 8 <= CTB.Count - 1) Then

Kar = CTB.Substring(i, 8) Else

Jlh = i

Kar = CTB.Substring(i, CTB.Count() - i) For j As Integer = 0 To 8 - Kar.Length - 1 Kar &= "1"

Next End If

Dec = toDecimal(Kar) CBytes(Index) = Dec Index += 1

CT &= Convert.ToChar(Dec) 'Tidak bisa jika bernilai 0 Log &= Kar & vbTab & Dec & vbCrLf

Next

CBytes(CBytes.Count() - 1) = CTB.Count() - Jlh

(6)

Log &= toBiner(CTB.Count() - Jlh) & vbTab & Convert.ToInt16(CT(CT.Count() - 1)) & vbCrLf

'Hitung Rasio dan Redudancy

'txtRC.Text = Format((JlhBit_PT / JlhBit_CT), "#.####")

'txtCR.Text = Format((JlhBit_CT / JlhBit_PT) * 100, "#,#.###") & " %" ' txtRed.Text = Format((JlhBit_PT - JlhBit_CT) / JlhBit_PT * 100, "#,#.##") & " %"

txtCTB.Text = CTB + Log txtJB_CT.Text = JlhBit_CT txtCT.Text = CT

Pjg_CT = txtCT.Text.Length 'CT.Count() txtPjg_CT.Text = Pjg_CT

StopWatchStop() Catch ex As Exception

End Try

ht = Len(txtCT.Text)

txtbiterkompres.Text = Val(ht) * 8 'txtJB_CT.Text = Val(ht) * 8

txtRC.Text = txtJB_PT.Text / txtbiterkompres.Text

txtCR.Text = Format(txtbiterkompres.Text / txtJB_PT.Text * 100, "#,#.###") & " %"

Dim txtctt As Integer

txtctt = Format(txtbiterkompres.Text / txtJB_PT.Text * 100, "#,#.###") ' txtRed.Text = Format(100 - txtctt, "#,#.###") & " %"

txtRed.Text = Format(((txtJB_PT.Text - txtbiterkompres.Text) / txtJB_PT.Text) * 100, "#,#.###") & " %"

End Sub

Private Sub btnKS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKS.Click

Log = ""

txtLog.Clear()

PT = txtPT.Text

JlhBit_PT = PT.Count() * 8 Pjg_PT = txtPT.TextLength()

Karakter_Set = BuatKarakterSet(PT) Panjang_KS = Karakter_Set.Length

THuff = HitungFreqKarakter(PT, Karakter_Set) THuff = UrutkanKS(THuff)

THuff = HuffmanTree(THuff) Log = CetakTabel(THuff)

txtPjg_PT.Text = Pjg_PT txtJB_PT.Text = JlhBit_PT txtKS.Text = Karakter_Set txtPjgKS.Text = Panjang_KS txtLog.Text = Log

End Sub

Private Sub btnDekompres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

(7)

PT = Dekompres(THuff, CTB) Pjg_PT = PT.Count()

JlhBit_PT = PT.Count() * 8

txtPT.Text = PT

txtJB_PT.Text = JlhBit_PT txtPjg_PT.Text = Pjg_PT

'Hitung Rasio dan Redudancy

'txtCR.Text = Format(JlhBit_CT / JlhBit_PT * 100, "#,#.##") & " %"

'txtRed.Text = Format((JlhBit_PT - JlhBit_CT) / JlhBit_PT * 100, "#,#.##") & " %"

txtLog.Text = Log Catch ex As Exception

End Try End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click

' frmMenu.GroupBox1.Visible = True GroupBox1.Visible = True

Me.Close()

End Sub

Private Sub btnSimpanFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpanFile.Click

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

File.WriteAllBytes(Output, CBytes) Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

PBytes = File.ReadAllBytes(Input) CT = ""

CTB = ""

For i = 0 To PBytes.Count - 1 CT &= Convert.ToChar(PBytes(i)) CTB &= toBiner(PBytes(i)) Next

CTB = CTB.Substring(0, CTB.Count() - 8 - (8 - PBytes(PBytes.Count() - 1)))

(8)

JlhBit_CT = CTB.Count() txtPjg_CT.Text = CT.Count() 'txtJB_CT.Text = JlhBit_CT

txtbiterkompres.Text = JlhBit_CT Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaFileTeks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBukaFileTeks.Click

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

PBytes = File.ReadAllBytes(Input) PT = ""

PTB = ""

For i = 0 To PBytes.Count - 1 PT &= Convert.ToChar(PBytes(i)) PTB &= toBiner(PBytes(i)) Next

txtPT.Text = PT 'txtPTB.Text = PTB JlhBit_PT = PTB.Count() txtPjg_PT.Text = PT.Count() txtJB_PT.Text = JlhBit_PT

txtBesarFile.Text = PT.Count() / 1024 Catch ex As Exception

End Try End If End Sub

Private Sub SFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SFD.FileOk

Output = SFD.FileName End Sub

Private Sub OFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OFD.FileOk

Input = OFD.FileName End Sub

Private Sub btnSimpanTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpanTabel.Click

Try

Dim Tabel(THuff.Count) As String

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else

For i As Integer = 0 To THuff.Count - 1 Tabel(i) = THuff(i).Kar & THuff(i).Kode Next

(9)

End If

Catch ex As Exception

End Try

End Sub

Private Sub btnBukaTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim Tabel() As String

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

Karakter_Set = ""

THuff = New List(Of clsHuff) Tabel = File.ReadAllLines(Input)

For i As Integer = 0 To Tabel.Count - 2

THuff.Add(New clsHuff(Tabel(i)(0), 0, Tabel(i).Substring(1, Tabel(i).Count() - 1)))

Karakter_Set &= Tabel(i)(0) Next

txtKS.Text = Karakter_Set txtPjgKS.Text = Tabel.Count - 1 Catch ex As Exception

End Try End If End Sub

Private Sub txtPT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPT.TextChanged

'Dim kecil As String = "ADA SAJA" End Sub

Private Sub frmK_Huffman_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub End Class

Kompresi Sequitur

Imports System.IO

Public Class frmK_Sequitur

Public Stopwatch As Stopwatch = New Stopwatch

Public Log As String

Public Pjg_PT, Pjg_CT As Integer Public PT, CT, PTB, CTB As String Public PBytes(), CBytes() As Byte Public Input, Output As String Public JlhSama As Integer

Public Tabel As List(Of String)

(10)

Public Sub StopWatchStart() Stopwatch.Reset() Stopwatch.Start()

Cursor.Current = Cursors.WaitCursor txtWaktuProses.Text = ""

End Sub

Public Sub StopWatchStop() Stopwatch.Stop()

txtWaktuProses.Text = Stopwatch.Elapsed.ToString() Cursor.Current = Cursors.Arrow

End Sub

Public Function GenerateChar(ByVal PT As String) As Char Dim R As Char = ""

Dim Rand As Random = New Random

Dim Ketemu As Boolean = True

While Ketemu Ketemu = False

R = Convert.ToChar(Rand.Next(1, 255)) For i As Integer = 0 To PT.Count() - 1 If R = PT(i) Then

Ketemu = True Exit For End If Next End While Return R End Function

' ======================================================== SEQUITUR ======================================================== '

Public Function Kompres(ByVal PT As String) As String Dim R As String = ""

Dim JlhSub As Integer Dim Index As Integer = 65 Dim Ketemu As Boolean Dim NT As Char

Dim i As Integer = 0

Log = ""

JlhSub = cmbJH.Text JlhSama = 0

R = PT

Tabel = New List(Of String) While (i <= R.Count - JlhSub)

'For i As Integer = 0 To R.Count - JlhSub

If Ketemu Then Log &= "Teks : " & R & vbCrLf & "========================"

& vbCrLf & vbCrLf

Ketemu = False

'Mencari Pola yang sama berdasarkan jumlah huruf For j As Integer = i + JlhSub To R.Count - JlhSub

If R.Substring(i, JlhSub) = R.Substring(j, JlhSub) Then

(11)

JlhSama += 1 Ketemu = True End If

Next

If Ketemu Then

'Karakter Non Terminal 'NT = GenerateChar(R) NT = Convert.ToChar(Index)

Tabel.Add(NT & R.Substring(i, JlhSub))

Log &= vbTab & R.Substring(i, JlhSub) & vbTab & "==>" & vbTab & NT & vbCrLf & vbCrLf

R = R.Replace(R.Substring(i, JlhSub), NT)

'Posisi Karakter Non Terminal Index += 1

i = 0 Else

i += 1 End If 'Next End While

CT = R

'Masukkan Ke CBytes

ReDim CBytes(CT.Count() - 1) For i = 0 To CT.Count() - 1

CBytes(i) = Convert.ToByte(CT(i)) Next

'Hitung Rasio dan Redudancy Pjg_PT = PT.Count()

Pjg_CT = CT.Count()

'txtRC.Text = Format(Pjg_CT / Pjg_PT * 100, "#,#.##") & " %" 'txtCR.Text = "0" + Format(Pjg_CT / Pjg_PT, "#,#.####")

'txtRed.Text = Format((Pjg_PT - Pjg_CT) / Pjg_PT * 100, "#,#.##") & " %" txtLog.Text = Log

Log += "Jumlah Sama : " & JlhSama & vbCrLf txtCT.Text = R

txtPjg_CT.Text = R.Count().ToString() txtLog.Text = Log

Return R End Function

Public Function Dekompres(ByVal CT As String) As String Pjg_CT = CT.Count()

PT = CT.Trim()

For i As Integer = Tabel.Count - 1 To 0 Step -1

PT = PT.Replace(Tabel(i)(0), Tabel(i).Substring(1, Tabel(i).Length - 1))

Next

'Hitung Rasio dan Redudancy Pjg_PT = PT.Count()

'txtRC.Text = Format(Pjg_CT / Pjg_PT * 100, "#,#.##") & " %"

(12)

txtPT.Text = PT

txtPjg_PT.Text = Pjg_PT.ToString() txtLog.Text = Log

Return CT End Function

' ======================================================== EVENT HANDLER ======================================================== '

Private Sub SFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SFD.FileOk

Output = SFD.FileName End Sub

Private Sub OFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OFD.FileOk

Input = OFD.FileName End Sub

Private Sub btnBukaFileTeks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBukaFileTeks.Click

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

PBytes = File.ReadAllBytes(Input) PT = ""

For i = 0 To PBytes.Count - 1 PT &= Convert.ToChar(PBytes(i)) Next

txtPT.Text = PT

txtPjg_PT.Text = PT.Count()

txtBesarFile.Text = PT.Count() / 1024 Catch ex As Exception

End Try End If End Sub

Private Sub btnSimpanFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpanFile.Click

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

File.WriteAllBytes(Output, CBytes) Catch ex As Exception

End Try End If End Sub

Private Sub btnSimpanTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpanTabel.Click

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

(13)

Try

File.WriteAllLines(Output, Tabel) Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

CBytes = File.ReadAllBytes(Input) CT = ""

For i = 0 To CBytes.Count - 1 CT &= Convert.ToChar(CBytes(i)) Next

txtCT.Text = CT.Trim() txtPjg_CT.Text = CT.Count() Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim Temp() As String

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

Log = ""

Temp = File.ReadAllLines(Input) Tabel = New List(Of String)

For i As Integer = 0 To Temp.Count - 1 Tabel.Add(Temp(i))

Log &= Tabel(i)(0) & vbTab & "==>" & vbTab & Tabel(i).Substring(1, Tabel(i).Count() - 1) & vbCrLf

Next

txtLog.Text = Log Catch ex As Exception

End Try End If End Sub

Private Sub btnDekompres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

'CT = txtCT.Text Try

(14)

End Try End Sub

Private Sub btnKompres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKompres.Click

PT = txtPT.Text Try

StopWatchStart() If cmbJH.Text <= 1 Then

MessageBox.Show("Jumlah substring tidak boleh lebih kecil dari 2",

"Peringatan")

Exit Sub End If Kompres(PT)

'txtCR.Text = txtPjg_PT.Text - txtPjg_CT.Text StopWatchStop()

Catch ex As Exception

End Try

txtPjg_PT.Text = Len(txtPT.Text) * 8

jlhbitterkompres.Text = Len(txtCT.Text) * 8

txtRC.Text = txtPjg_PT.Text / jlhbitterkompres.Text

txtCR.Text = Format(jlhbitterkompres.Text / txtPjg_PT.Text * 100, "#,#.####") & " %"

Dim ht As Integer

ht = Format(jlhbitterkompres.Text / txtPjg_PT.Text * 100, "#,#.####") 'txtRed.Text = Format(100 - ht, "#,#.###")

txtRed.Text = Format(((txtPjg_PT.Text - jlhbitterkompres.Text) / txtPjg_PT.Text) * 100, "#,#.###") & " %"

'txtRed.Text = Format((Pjg_PT - Pjg_CT) / Pjg_PT * 100, "#,#.##") & " %"

End Sub

Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click

'frmMenu.GroupBox1.Visible = True Me.Close()

End Sub

Private Sub txtPT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPT.TextChanged

txtPT.Text = StrConv(txtPT.Text, VbStrConv.Lowercase) End Sub

Private Sub frmK_Sequitur_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub End Class

Dekompresi Huffman

(15)

Public Class frmD_Huffman

Public Stopwatch As Stopwatch = New Stopwatch

Public Log As String

Public Pjg_PT, Pjg_CT As Integer

Public Panjang_KS, JlhBit_PT, JlhBit_CT As Integer Public Karakter_Set As String

Public THuff As List(Of clsHuff) Public PT, CT, PTB, CTB As String Public PBytes(), CBytes() As Byte Public Input, Output As String

' ======================================================== FUNGSI UMUM ======================================================== '

Public Sub StopWatchStart() Stopwatch.Reset() Stopwatch.Start()

Cursor.Current = Cursors.WaitCursor txtWaktuProses.Text = ""

End Sub

Public Sub StopWatchStop() Stopwatch.Stop()

txtWaktuProses.Text = Stopwatch.Elapsed.ToString() Cursor.Current = Cursors.Arrow

End Sub

Public Function toBiner(ByVal desimal As Integer) As String Dim biner As String = ""

Dim buf As String = ""

Dim hasilBagi As Integer = desimal Dim p, sisa As Integer

While (hasilBagi > 1)

If (hasilBagi Mod 2 = 1) Then hasilBagi -= 1

hasilBagi = hasilBagi / 2 biner = "1" & biner Else

hasilBagi = hasilBagi / 2 biner = "0" & biner End If

End While

biner = hasilBagi & biner

p = biner.Length / 8

sisa = (8 - (biner.Length Mod 8)) Mod 8

For i = 1 To sisa buf &= "0"

Next

biner = buf & biner Return biner

End Function

Public Function toDecimal(ByVal biner As String) As Integer Dim result As Integer = 0

(16)

For i = 1 To biner.Length

num = Convert.ToInt16(biner(i - 1)) - 48 If num <> 0 Then

result += Convert.ToInt16(Math.Pow(2, biner.Length - i)) * num End If

Next

Return result End Function

' ======================================================== HUFFMAN ======================================================== '

Public Function BuatKarakterSet(ByVal Teks As String) As String Dim R As String = ""

Dim Yes As Boolean

For i As Integer = 0 To Teks.Length - 1 Yes = False

For j As Integer = 0 To R.Length - 1 If Teks(i) = R(j) Then

Yes = True Continue For End If

Next

If Not Yes Then R &= Teks(i) End If

Next Return R End Function

Public Function HitungFreqKarakter(ByVal Teks As String, ByVal KS As String) As

List(Of clsHuff)

Dim R As List(Of clsHuff) = New List(Of clsHuff) Dim T As Integer

For j As Integer = 0 To KS.Length - 1 T = 0

For i As Integer = 0 To Teks.Length - 1 If KS(j) = Teks(i) Then

T += 1 End If Next

R.Add(New clsHuff(KS(j), T)) Next

Return R End Function

Public Function UrutkanKS(ByVal L As List(Of clsHuff)) As List(Of clsHuff) Dim Kar As Char

Dim Freq As Integer

For j As Integer = 0 To L.Count - 2

For i As Integer = j + 1 To L.Count - 1 If L(j).Freq > L(i).Freq Then

(17)

L(j).Kar = L(i).Kar L(j).Freq = L(i).Freq L(i).Kar = Kar

L(i).Freq = Freq End If

Next Next Return L End Function

Public Function HuffmanTree(ByVal L As List(Of clsHuff)) As List(Of clsHuff) Dim i As Integer = 0

Dim Freq As Integer Dim Index As Integer

Dim L2 As List(Of clsHuff) = New List(Of clsHuff) While (i + 2 <= L.Count)

Freq = L(i).Freq + L(i + 1).Freq

For j As Integer = 0 To L.Count - 1 If Freq < L(j).Freq Then

L.Insert(j, New clsHuff("*", Freq)) 'Bentuk Parent dan Child

L(i).Parent = j L(i + 1).Parent = j Exit For

ElseIf j = L.Count - 1 Then

L.Insert(j + 1, New clsHuff("*", Freq)) L(i).Parent = j + 1

L(i + 1).Parent = j + 1 End If

Next i += 2 'Exit While End While

' Bentuk Kode

For j As Integer = 0 To L.Count - 1 Index = j

While L(Index).Parent <> 0 If (Index Mod 2 = 0) Then

L(j).Kode = "0" + L(j).Kode Else

L(j).Kode = "1" + L(j).Kode End If

Index = L(Index).Parent End While

Next

' Hilangkan Star

For j As Integer = 0 To L.Count - 1 If L(j).Kar <> "*" Then

L2.Add(L(j)) End If

Next Return L End Function

Private Function Kompres(ByVal Tabel As List(Of clsHuff), ByVal PT As String) As String

(18)

For i As Integer = 0 To PT.Count() - 1 For j As Integer = 0 To Tabel.Count - 1 If PT(i) = Tabel(j).Kar Then

R &= Tabel(j).Kode End If

Next Next Return R End Function

Private Function Dekompres(ByVal Tabel As List(Of clsHuff), ByVal CTB As String) As String

Dim R As String = ""

Log = ""

txtLog.Clear()

Dim S As String = ""

Dim KT As String Dim Kar As Char = "A"

Dim TC As Integer = 0 Dim Index As Integer = 0 Dim Ketemu As Boolean

For i As Integer = 0 To CTB.Count() - 1 S += CTB(i)

TC += 1 Ketemu = False

For j As Integer = 0 To Tabel.Count() - 1 Try

If (S = Tabel(j).Kode.Substring(0, TC)) Then KT = S

Kar = Tabel(j).Kar

Log &= "Bit " & Index & " ke " & (Index + TC - 1) & " = " & KT & vbCrLf

Ketemu = True Exit For End If

Catch ex As Exception

End Try Next

If (Ketemu = False) Then R &= Kar

S = ""

TC = 0 Index = i i -= 1

Log &= "============================== Karakter : " & Kar & vbCrLf & vbCrLf

End If Next R &= Kar

Log &= "============================== Karakter : " & Kar & vbCrLf Return R

End Function

(19)

For i As Integer = 0 To Tabel.Count - 1

Log &= (i) & vbTab & Tabel(i).Kar & vbTab & Tabel(i).Freq & vbTab & Tabel(i).Parent & vbTab & Tabel(i).Kode & vbCrLf

Next

Return Log End Function

' ======================================================== EVENT HANDLER ======================================================== '

Private Sub btnCompress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Try

Dim Log As String = vbCrLf & vbCrLf Dim Kar As String = ""

Dim Dec As Integer Dim Jlh As Integer

Dim Index As Integer = 0

CT = ""

PT = txtPT.Text

CTB = Kompres(THuff, PT) JlhBit_CT = CTB.Count()

ReDim CBytes(Math.Ceiling(CTB.Count() / 8)) 'Cetak versi 8 bit

For i As Integer = 0 To CTB.Count - 1 Step 8 If (i + 8 <= CTB.Count - 1) Then

Kar = CTB.Substring(i, 8) Else

Jlh = i

Kar = CTB.Substring(i, CTB.Count() - i) For j As Integer = 0 To 8 - Kar.Length - 1 Kar &= "1"

Next End If

Dec = toDecimal(Kar) CBytes(Index) = Dec Index += 1

CT &= Convert.ToChar(Dec) 'Tidak bisa jika bernilai 0 Log &= Kar & vbTab & Dec & vbCrLf

Next

CBytes(CBytes.Count() - 1) = CTB.Count() - Jlh

CT &= Convert.ToChar((CTB.Count() - Jlh))

Log &= toBiner(CTB.Count() - Jlh) & vbTab & Convert.ToInt16(CT(CT.Count() - 1)) & vbCrLf

'Hitung Rasio dan Redudancy

txtRasio.Text = Format(JlhBit_CT / JlhBit_PT * 100, "#,#.##") & " %"

txtRed.Text = Format((JlhBit_PT - JlhBit_CT) / JlhBit_PT * 100, "#,#.##") & " %"

txtCTB.Text = CTB + Log txtJB_CT.Text = JlhBit_CT txtCT.Text = CT

(20)

txtPjg_CT.Text = Pjg_CT Catch ex As Exception

End Try End Sub

Private Sub btnKS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Log = ""

txtLog.Clear()

PT = txtPT.Text

JlhBit_PT = PT.Count() * 8 Pjg_PT = txtPT.TextLength()

Karakter_Set = BuatKarakterSet(PT) Panjang_KS = Karakter_Set.Length

THuff = HitungFreqKarakter(PT, Karakter_Set) THuff = UrutkanKS(THuff)

THuff = HuffmanTree(THuff) Log = CetakTabel(THuff)

txtPjg_PT.Text = Pjg_PT txtJB_PT.Text = JlhBit_PT txtKS.Text = Karakter_Set txtPjgKS.Text = Panjang_KS txtLog.Text = Log

End Sub

Private Sub btnDekompres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDekompres.Click

Try

StopWatchStart()

PT = Dekompres(THuff, CTB)

ReDim PBytes(PT.Count() - 1)

For i As Integer = 0 To PT.Count() - 1 PBytes(i) = Convert.ToByte(PT(i)) Next

Pjg_PT = PT.Count()

JlhBit_PT = PT.Count() * 8

txtPT.Text = PT

txtJB_PT.Text = JlhBit_PT txtPjg_PT.Text = Pjg_PT

'Hitung Rasio dan Redudancy

txtRasio.Text = Format(JlhBit_CT / JlhBit_PT * 100, "#,#.##") & " %"

txtRed.Text = Format((JlhBit_PT - JlhBit_CT) / JlhBit_PT * 100, "#,#.##") & " %"

txtLog.Text = Log StopWatchStop() Catch ex As Exception

End Try

(21)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click

'frmMenu.GroupBox1.Visible = True Me.Close()

End Sub

Private Sub btnBukaFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBukaFile.Click

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

PBytes = File.ReadAllBytes(Input) CT = ""

CTB = ""

For i = 0 To PBytes.Count - 1 CT &= Convert.ToChar(PBytes(i)) CTB &= toBiner(PBytes(i)) Next

CTB = CTB.Substring(0, CTB.Count() - 8 - (8 - PBytes(PBytes.Count() - 1)))

txtCT.Text = CT txtCTB.Text = CTB JlhBit_CT = CTB.Count() txtPjg_CT.Text = CT.Count() txtJB_CT.Text = JlhBit_CT

Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaFileTeks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

PBytes = File.ReadAllBytes(Input) PT = ""

PTB = ""

For i = 0 To PBytes.Count - 1 PT &= Convert.ToChar(PBytes(i)) PTB &= toBiner(PBytes(i)) Next

(22)

End Try End If End Sub

Private Sub SFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SFD.FileOk

Output = SFD.FileName End Sub

Private Sub OFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OFD.FileOk

Input = OFD.FileName End Sub

Private Sub btnSimpanTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim Tabel(THuff.Count) As String

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

For i As Integer = 0 To THuff.Count - 1 Tabel(i) = THuff(i).Kar & THuff(i).Kode Next

File.WriteAllLines(Output, Tabel) Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBukaTabel.Click

Dim Tabel() As String

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

Karakter_Set = ""

THuff = New List(Of clsHuff) Tabel = File.ReadAllLines(Input)

For i As Integer = 0 To Tabel.Count - 2

THuff.Add(New clsHuff(Tabel(i)(0), 0, Tabel(i).Substring(1, Tabel(i).Count() - 1)))

Karakter_Set &= Tabel(i)(0) Next

txtKS.Text = Karakter_Set txtPjgKS.Text = Tabel.Count - 1 Catch ex As Exception

End Try End If End Sub

Private Sub btnSimpanFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpanFile.Click

(23)

Else Try

File.WriteAllBytes(Output, PBytes) Catch ex As Exception

End Try End If End Sub

Private Sub frmD_Huffman_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub End Class

Dekompresi Sequitur

Imports System.IO

Public Class frmD_Sequitur

Public Stopwatch As Stopwatch = New Stopwatch

Public Log As String

Public Pjg_PT, Pjg_CT As Integer Public PT, CT, PTB, CTB As String Public PBytes(), CBytes() As Byte Public Input, Output As String Public JlhSama As Integer

Public Tabel As List(Of String)

' ======================================================== FUNGSI UMUM ======================================================== '

Public Sub StopWatchStart() Stopwatch.Reset() Stopwatch.Start()

Cursor.Current = Cursors.WaitCursor txtWaktuProses.Text = ""

End Sub

Public Sub StopWatchStop() Stopwatch.Stop()

txtWaktuProses.Text = Stopwatch.Elapsed.ToString() Cursor.Current = Cursors.Arrow

End Sub

Public Function GenerateChar(ByVal PT As String) As Char Dim R As Char = ""

Dim Rand As Random = New Random

Dim Ketemu As Boolean = True

While Ketemu Ketemu = False

R = Convert.ToChar(Rand.Next(1, 255)) For i As Integer = 0 To PT.Count() - 1 If R = PT(i) Then

(24)

Next End While Return R End Function

' ======================================================== SEQUITUR ======================================================== '

Public Function Kompres(ByVal PT As String) As String Dim R As String = ""

Dim JlhSub As Integer Dim Index As Integer = 65 Dim Ketemu As Boolean Dim NT As Char

Dim i As Integer = 0

Log = ""

JlhSama = 0 R = PT

Tabel = New List(Of String) While (i <= R.Count - JlhSub)

'For i As Integer = 0 To R.Count - JlhSub

If Ketemu Then Log &= "Teks : " & R & vbCrLf & "========================"

& vbCrLf & vbCrLf

Ketemu = False

'Mencari Pola yang sama berdasarkan jumlah huruf For j As Integer = i + JlhSub To R.Count - JlhSub

If R.Substring(i, JlhSub) = R.Substring(j, JlhSub) Then

Log &= "I = " & i & vbTab & "J = " & j & vbTab & R.Substring(i, JlhSub) & vbTab & "==" & vbTab & R.Substring(j, JlhSub) & vbCrLf

JlhSama += 1 Ketemu = True End If

Next

If Ketemu Then

'Karakter Non Terminal 'NT = GenerateChar(R) NT = Convert.ToChar(Index)

Tabel.Add(NT & R.Substring(i, JlhSub))

Log &= vbTab & R.Substring(i, JlhSub) & vbTab & "==>" & vbTab & NT & vbCrLf & vbCrLf

R = R.Replace(R.Substring(i, JlhSub), NT)

'Posisi Karakter Non Terminal Index += 1

i = 0 Else

i += 1 End If 'Next End While

CT = R

'Masukkan Ke CBytes

(25)

CBytes(i) = Convert.ToByte(CT(i)) Next

'Hitung Rasio dan Redudancy Pjg_PT = PT.Count()

Pjg_CT = CT.Count()

txtRC.Text = Format(Pjg_CT / Pjg_PT * 100, "#,#.##") & " %"

txtRed.Text = Format((Pjg_PT - Pjg_CT) / Pjg_PT * 100, "#,#.##") & " %"

txtLog.Text = Log

Log += "Jumlah Sama : " & JlhSama & vbCrLf txtCT.Text = R

txtPjg_CT.Text = R.Count().ToString() txtLog.Text = Log

Return R End Function

Public Function Dekompres(ByVal CT As String) As String Pjg_CT = CT.Count()

PT = CT.Trim()

For i As Integer = Tabel.Count - 1 To 0 Step -1

PT = PT.Replace(Tabel(i)(0), Tabel(i).Substring(1, Tabel(i).Length - 1))

Next

'Hitung Rasio dan Redudancy Pjg_PT = PT.Count()

txtRC.Text = Format(Pjg_CT / Pjg_PT * 100, "#,#.##") & " %"

txtCR.Text = "0" + Format(Pjg_CT / Pjg_PT, "#,#.####")

txtRed.Text = Format((Pjg_PT - Pjg_CT) / Pjg_PT * 100, "#,#.##") & " %"

txtPT.Text = PT

txtPjg_PT.Text = Pjg_PT.ToString() txtLog.Text = Log

Return CT End Function

' ======================================================== EVENT HANDLER ======================================================== '

Private Sub SFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SFD.FileOk

Output = SFD.FileName End Sub

Private Sub OFD_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OFD.FileOk

Input = OFD.FileName End Sub

Private Sub btnBukaFileTeks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

(26)

For i = 0 To PBytes.Count - 1 PT &= Convert.ToChar(PBytes(i)) Next

txtPT.Text = PT

txtPjg_PT.Text = PT.Count()

txtBesarFile.Text = PT.Count() / 1024 Catch ex As Exception

End Try End If End Sub

Private Sub btnSimpanFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpanFile.Click

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

File.WriteAllBytes(Output, PBytes) Catch ex As Exception

End Try End If End Sub

Private Sub btnSimpanTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

If (SFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

File.WriteAllLines(Output, Tabel) Catch ex As Exception

End Try End If End Sub

Private Sub btnBukaFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBukaFile.Click

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

CBytes = File.ReadAllBytes(Input) CT = ""

For i = 0 To CBytes.Count - 1 CT &= Convert.ToChar(CBytes(i)) Next

txtCT.Text = CT.Trim() txtPjg_CT.Text = CT.Count()

txtBesarFile.Text = CT.Count() / 1024 Catch ex As Exception

(27)

End Sub

Private Sub btnBukaTabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBukaTabel.Click

Dim Temp() As String

If (OFD.ShowDialog() <> DialogResult.OK) Then Exit Sub

Else Try

Log = ""

Temp = File.ReadAllLines(Input) Tabel = New List(Of String)

For i As Integer = 0 To Temp.Count - 1 Tabel.Add(Temp(i))

Log &= Tabel(i)(0) & vbTab & "==>" & vbTab & Tabel(i).Substring(1, Tabel(i).Count() - 1) & vbCrLf

Next

txtLog.Text = Log Catch ex As Exception

End Try End If End Sub

Private Sub btnDekompres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDekompres.Click

'CT = txtCT.Text Try

StopWatchStart() Dekompres(CT)

ReDim PBytes(PT.Count() - 1)

For i As Integer = 0 To PT.Count() - 1 PBytes(i) = Convert.ToByte(PT(i)) Next

StopWatchStop() Catch ex As Exception

End Try

txtPjg_PT.Text = Len(txtPT.Text) * 8 End Sub

Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click

'frmMenu.GroupBox1.Visible = True Me.Close()

End Sub

Private Sub frmD_Sequitur_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub End Class

Form Menu

(28)

'GroupBox1.Visible = False End Sub

Private Sub SequiturToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SequiturToolStripMenuItem1.Click

frmK_Sequitur.Show()

frmK_Sequitur.MdiParent = Me 'GroupBox1.Visible = False End Sub

Private Sub HuffmanToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HuffmanToolStripMenuItem2.Click

frmD_Huffman.Show()

frmD_Huffman.MdiParent = Me 'GroupBox1.Visible = False End Sub

Private Sub SequiturToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SequiturToolStripMenuItem2.Click

frmD_Sequitur.Show()

(29)

CURRICULUM VITAE

1.

Data Diri

Nama

: Elsya Sabrina Asmita Simorangkir

Alamat Rumah

: Jl. SM. Raja Gg. Batucuci No. 2 Medan

Agama

: Kristen Protestan

No. HP

: 082166297399

Email

: elsyasimorangkir@yahoo.com

2.

Riwayat Pendidikan

2013-2017

: S1 Ekstensi Ilmu Komputer Universitas Sumatera Utara, Medan

2008-2011

: D3 Manajemen Informatika STMIK Budidarma, Medan

2005-2008

: SMA Swasta Methodist 2, Tebing Tinggi

2002-2005

: SMP Negeri 2, Tebing Tinggi

1996-2002

: SD Negeri 163095, Tebing Tinggi

3.

Pengalaman Organisasi

2003-2004

: Sekretaris OSIS SMP Negeri 2, Tebing Tinggi

2006-2007

: Anggota Paskibraka Pemko Tebing Tinggi

2009-2010

: Bendahara KMK STMIK Budidarma, Medan

4. Seminar

2014

: Seminar Nasional Literasi Informasi “SENARAI”

5.

Pengalaman Bekerja

2011-2013

: Staff Administrasi di PT. Japan Servo Batam

Referensi

Dokumen terkait

Peraturan Pemerintah Nomor 8 Tahun 2008 tentang Tahapan, Tata Cara Penyusunan Pengendalian dan Evaluasi Pelaksanaan Rencana Pembangunan Daerah (Lembaran Negara

[r]

KETIGA : Dalam melaksanakan tugasnya Badan Koordinasi Penataan Ruang Daerah (BKPRD) bertanggung jawab kepada Bupati, Sekretariat BKPRD bertanggung jawab kepada

Adapun perlindungan hukum bagi konsumen adalah : Pembentukan undang-undang periklanan, penetapan pasal tentang badan sensor iklan media cetak, penetapan pasal yang mewajibkan

It is concluded that teaching writing by using Transition Action Details Strategy in writing recount text gives significant effect on students’ writing skill at class X

2.6.1 Hipotesis nol (H0): tidak ada hubungan yang signifikan pada pengaruh bermain game online terhadap risiko gangguan mata miopi pada mahasiswa FKIK dan FTI

Dengan demikian dapat dikatakan bahwa pemanfaatan tanah desa nagari talang binjai boleh digunakan dan di manfaatkan oleh masyarakat nagari Talang Binjai untuk

pengaruh game online terhadap risiko gangguan mata miopi berat pada mahasiswa serta reaksi individu yang mengalami. miopi dan koreksi lensa