Total members 11793 |It is currently Thu Aug 22, 2019 4:40 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





The Huffman Data compression and decompression algorithm implementation using Csharp (C# programming language) code
csharp code
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Huffman_Data_Comperision
{
class huffman
{
private int lenghtoftext;
//public static int arraysize = 1;
//public int size=arraysize;
private int lenght;
public void sort(ArrayList st,ArrayList doub)
{
int array_size = doub.Count;
int i, j;
int min;

for (i = 0; i < array_size - 1; i++)
{
min = i;
for (j = i + 1; j < array_size; j++)
{
if ((double)doub[j] > (double)doub[min])
min = j;
}
double temp1 = (double)doub[i];
doub[i] = doub[min];
doub[min] = temp1;

string temp2 = (string)st[i];
st[i] = st[min];
st[min] = temp2;

}
}

public bool FindEntry(string entry,ArrayList arr)
{
for (int i = 0; i < arr.Count; i++)
{
if (arr[i].Equals(entry))
return true;
}
return false;
}

public void pass(ArrayList st,ArrayList st_big)
{
for (int i = 0; i < st.Count; i++)
{
st_big.Add(st[i]);
// doub_big.Add(doub[i]);
}
}

public void setlenght(int l)
{
lenght = l;
}
public int getlenght()
{
return lenght;
}
public ArrayList dic_character = new ArrayList();
public ArrayList dic_code = new ArrayList();
public void set_dic_character(string arr)
{
// for (int i = 0; i < arr.Length; i++)
//{
dic_character.Add(arr);
//}
}
public void set_dic_code(string arr)
{
//for (int i = 0; i < arr.Length; i++)
//{
dic_code.Add(arr);
//}
}
}
}

/*public calculateprob [] array=new calculateprob [arraysize];
public void search(string var)
{
for (int i = 0; i < size; i++)
{
if (array[i].variable==var)
{
array[i].setcount(array[i].getcount() + 1);
array[i].probability = array[i].getcount()/getlenght();
// return true;
break;
}
else
{
array[i].variable = var;
array[i].setcount(array[i].getcount() + 1);
array[i].probability = array[i].getcount()/getlenght();
arraysize++;

}
}
}*/
/*public struct calculateprob
{
public string variable;
public double probability;
public int count;
public void intilizecount()
{
count = 0;
}
public void setcount(int c)
{
count=c;
}
public int getcount()
{
return count;
}
}
*/


csharp code
using System;
using System.Collections.Generic;
using System.Text;

namespace Huffman_Data_Comperision
{
class probarray
{
public string variable;
public double prob;
public int count;
public probarray()
{
variable = "";
prob = 0.0;
count = 0;
}
}
}


csharp code
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Huffman_Data_Comperision
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


csharp code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Huffman_Data_Comperision
{
public partial class Form1 : Form
{
ArrayList st_big = new ArrayList();
ArrayList st = new ArrayList();
ArrayList doub = new ArrayList();
ArrayList letters = new ArrayList();
ArrayList prob;
ArrayList st1 = new ArrayList();
ArrayList doub1 = new ArrayList();
ArrayList old_x = new ArrayList();
ArrayList new_x = new ArrayList();
ArrayList last_character_code = new ArrayList();
ArrayList last_code = new ArrayList();
static int x = 357;
static int x1 = -260;
static string codecomp;
public Form1()
{
InitializeComponent();
}
public void back()
{
Random r;
r = new Random();
int g = r.Next(1, 30);
switch (g)
{
case 1:
this.BackColor = Color.Thistle;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 2:
this.BackColor = Color.AliceBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;

case 3:
this.BackColor = Color.Beige;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;

case 4:
this.BackColor = Color.SkyBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 5:
this.BackColor = Color.SeaShell;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 6:
this.BackColor = Color.Lavender;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;

case 7:
this.BackColor = Color.Plum;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 8:
this.BackColor = Color.Ivory;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 9:
this.BackColor = Color.LightSkyBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 10:
this.BackColor = Color.PapayaWhip;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 11:
this.BackColor = Color.PaleTurquoise;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 12:
this.BackColor = Color.PaleGreen;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 13:
this.BackColor = Color.NavajoWhite;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 14:
this.BackColor = Color.MistyRose;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 15:
this.BackColor = Color.MintCream;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;

case 16:
this.BackColor = Color.LightSteelBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 17:
this.BackColor = Color.LightSkyBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 18:
this.BackColor = Color.LightCyan;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 19:
this.BackColor = Color.LightBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 20:
this.BackColor = Color.LavenderBlush;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 21:
this.BackColor = Color.Lavender;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 22:
this.BackColor = Color.Khaki;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 23:
this.BackColor = Color.LightBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 24:
this.BackColor = Color.DarkSeaGreen;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 25:
this.BackColor = Color.DarkKhaki;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 26:
this.BackColor = Color.BlanchedAlmond;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 27:
this.BackColor = Color.AliceBlue;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
case 28:
this.BackColor = Color.Azure;
label3.BackColor = this.BackColor;
label1.BackColor = this.BackColor;
menuStrip1.BackColor = this.BackColor;
g = r.Next(1, 37);
break;
}
}
private void timer1_Tick(object sender, EventArgs e)
{

DateTime.Now.TimeOfDay.Milliseconds.ToString();
if (DateTime.Now.TimeOfDay.Milliseconds % 40 == 0)
{
back();
}
label3.Visible = true;
timer1.Enabled = true;
x -= 5;
Point p = new Point(x, 244);
if (x <= -564)
{
x = 357;
p.X = x;
label3.Location = p;

}
else
{
label3.Location = p;
}
x1 += 5;
Point p1 = new Point(x1, 218);
if (x1 >= 357)
{
x1 = -260;
p1.X = x1;
label1.Location = p1;

}
else
{
label1.Location = p1;
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{

DialogResult exit;
exit = MessageBox.Show("Are you sure ?", "Exit", MessageBoxButtons.YesNo);
if (exit == DialogResult.Yes)
{
MessageBox.Show("Thanks for using my program", "Good bye");
}
else
{
e.Cancel = true;
}
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
huffman obj = new huffman();
OpenFileDialog openfilecomprese = new OpenFileDialog();
openfilecomprese.InitialDirectory = "@My computer";
openfilecomprese.Multiselect = true;
openfilecomprese.ReadOnlyChecked = true;
openfilecomprese.RestoreDirectory = true;
openfilecomprese.ShowReadOnly = true;
openfilecomprese.Title = "Choose File to Comprese";

if (openfilecomprese.ShowDialog() == DialogResult.OK)
{
string path=openfilecomprese.FileName;
using(StreamReader read = new StreamReader(path))
// using(StreamWriter write = new StreamWriter("D:\\comprese file.txt", false))
{
// letters = new ArrayList();
prob = new ArrayList();

//This iteration is for claculate the probability for each letter
int start = 0;
int count = 0;
string text = read.ReadLine();
textBox1.Text = "The data inside the file is = ";
textBox1.Text += text;
for (int i = 0; i < text.Length; i++)
{
string sub = text.Substring(start, 1);
if (!(obj.FindEntry(sub,letters)))
{
letters.Add(sub);
for (int k = i; k <text.Length; k++)
{

string current_in = text.Substring(k, 1);
if (current_in == sub)
count++;
}

prob.Add(((double)count / (double)text.Length));
count = 0;
}
start++;
}
// end of probability calculation

//this iteration is passing values between arraylists
for (int i = 0; i < letters.Count; i++)
{
st.Add(letters[i]);
doub.Add(prob[i]);
}
// end of passing iteration

//call the function sort, to sort the array lists
obj.sort(st,doub);
//end of sort calling
obj.pass(st,st_big);

for (int tempo = 0; tempo < letters.Count - 2; tempo++)
{
st1.Clear();
doub1.Clear();
for (int i = 0; i < st.Count; i++)
{
st1.Add(st[i]);
doub1.Add(doub[i]);
}
st.Clear();
doub.Clear();

string concate = (string)st1[st1.Count - 2] + (string)st1[st1.Count - 1];
double add = (double)doub1[doub1.Count - 2] + (double)doub1[doub1.Count - 1];
for (int i = 0; i < st1.Count - 2; i++)
{
st.Add(st1[i]);
doub.Add(doub1[i]);
}
st.Add(concate);
doub.Add(add);
obj.sort(st,doub);
obj.pass(st, st_big);
}
read.Close();
// write.Close();
}
///////*/////////*//////////*////////
ArrayList character_code = new ArrayList();
///put non repeted character in array//////////////
for (int o = 0; o < st_big.Count; o++)
{
if (!obj.FindEntry((string)st_big[st_big.Count - 1 - o], character_code))
{
character_code.Add(st_big[st_big.Count - 1 - o]);
}
}
//////put code of all non repeted character///////////
string[] code = new string[character_code.Count];
for (int i = 0; i < character_code.Count; i++)
{
if (i == 0)
{
code[i] = "0";
}
else
{
if (i == 1)
{
code[i] = "1";
}
else
{
if (i % 2 == 0)
{
code[i] =code[i - 1] + "0";
}
if (i % 2 == 1)
{
code[i] = code[i - 2] + "1";
}
}
}
}
int index = 0;
////////// put the basic charcter and code of it //////////////////
for (int i = 0; i < character_code.Count; i++)
{
string x=(string)character_code[i];
if (x.Length == 1)
{
last_character_code.Add((string)character_code[i]);
last_code.Add((string)code[i]);

// obj.set_dic_character(last_character_code[index]);
// obj.set_dic_code(last_code[index]);
// index++;
}
}
using (StreamWriter write = new StreamWriter("D:\\dictionary.txt"))
{
for (int i = 0; i < letters.Count; i++)
{
write.Write((string)last_character_code[i]);
write.Write(" ");
write.Write((string)last_code[i]);
write.Write(" ");
}
}
///////////////compresss the file form dictionary//////////////
using (StreamReader read = new StreamReader(path))
using(StreamReader readdic=new StreamReader ("D:\\dictionary.txt"))
using(StreamWriter writecomp=new StreamWriter ("D:\\compress.txt"))
{
string text = read.ReadLine();
string textdic = readdic.ReadLine();
int start=0;
string sub;
string subdic;
string subcodecomp;

do
{

sub = text.Substring(start, 1);
for (int i = 0; i < textdic.Length; i++)
{
subdic = textdic.Substring(i, 1);
if (subdic == sub)
{
int startsub = i + 2;
int end = i + 2;
int lenght = end - startsub + 1;
subcodecomp = textdic.Substring(end,1);
while (subcodecomp != " ")
{

lenght = end - startsub + 1;
codecomp =textdic.Substring(startsub, lenght);
end++;
subcodecomp = textdic.Substring(end, 1);
}
writecomp.Write(codecomp);
startsub = end;
}
else
continue;
}
start++;
}while(start<text.Length);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{

}

private void openDictionaryToolStripMenuItem_Click(object sender, EventArgs e)
{
//////////////dictionary/////////////////////
using (StreamReader read = new StreamReader("D:\\dictionary.txt"))
{
textBox1.Text = "The dictionary is = ";
string text = read.ReadLine();
textBox1.Text += text;
}
}

private void openCompresFileToolStripMenuItem_Click(object sender, EventArgs e)
{
////////////////compress file////////////////
using (StreamReader read = new StreamReader("D:\\compress.txt"))
{
textBox1.Text = "The compress file is = ";
string text = read.ReadLine();
textBox1.Text += text;
}
}

private void openDecompressFileToolStripMenuItem_Click(object sender, EventArgs e)
{
/////////////////decompress file///////////////////
using (StreamReader read = new StreamReader("D:\\decompress.txt"))
{
textBox1.Text = "The decompress file is = ";
string text = read.ReadLine();
textBox1.Text += text;
}
}

private void openFileToDecopresToolStripMenuItem_Click(object sender, EventArgs e)
{
/////////////////decompressssss////////////////
huffman obj = new huffman();
OpenFileDialog openfilecomprese = new OpenFileDialog();
openfilecomprese.InitialDirectory = "@My computer";
openfilecomprese.Multiselect = true;
openfilecomprese.ReadOnlyChecked = true;
openfilecomprese.RestoreDirectory = true;
openfilecomprese.ShowReadOnly = true;
openfilecomprese.Title = "Choose File to Comprese";

if (openfilecomprese.ShowDialog() == DialogResult.OK)
{
string path = openfilecomprese.FileName;
using(StreamReader read=new StreamReader (path))
using(StreamReader readdic=new StreamReader ("D:\\dictionary.txt"))
using (StreamWriter write = new StreamWriter("D:\\decompress.txt"))
{
textBox1.Text = "The data inside this file is = ";
string text = read.ReadLine();
string textdic = readdic.ReadLine();
textBox1.Text += text;
int start = 0;
int end = 0;
int lenght = end - start + 1;
string sub;
for (int i = 0; i < text.Length;i++)
{
int flag = 0;
if (start + lenght <= text.Length)
{
sub = text.Substring(start, lenght);
for (int j = 0; j < last_character_code.Count; j++)
{
if (sub == (string)last_code[j])
{
flag++;
write.Write((string)last_character_code[j]);
}
}
if (flag == 0)
{
end++;
}
else
{
end++;
start = end;
}

lenght = end - start + 1;
}
}
}
}
}
}
}
/* if (end == 3)
{
textBox1.Text = "karim";

}
*/
// end++;
//lenght = end - start + 1;
/* if (start2 == 0)
{
end++;
lenght = end - start + 1;
}
else
start2 = 0;
*/
// sub = text.Substring(start, lenght);
/*int lenght = end - start + 1;
ArrayList charater = new ArrayList();
ArrayList code = new ArrayList();
do
{
start2 = start + 2;
end = start2;
start = start2;
lenght = end - start + 1;
string sub = textdic.Substring(start, lenght);
charater.Add(sub);
string subsub = textdic.Substring(start2, 1);
//sub = textdic.Substring(start + 2, lenght);
while (subsub != " ")
{
start = start2;
start2++;
end = start2 - 1;
lenght = end - start + 1;
sub = textdic.Substring(start, lenght);
subsub = textdic.Substring(start2,1);
}
sub = textdic.Substring(start, lenght);
code.Add(sub);
start = end;
lenght = end - start + 1;
} while (start <textdic.Length);*/
/*do
{
if (start + lenght <= text.Length)
{
string sub = text.Substring(start, lenght);
for (int i = 0; i < textdic.Length; i++)
{
if (i + sub.Length <= textdic.Length)
{
string subdic = textdic.Substring(i, sub.Length);
if (subdic == sub)
{
string test = textdic.Substring(i + 1, 1);
if (test == " ")
{
string charcter = textdic.Substring(i - subdic.Length, 1);
textBox1.Text += charcter;
write.Write(charcter);
write.Write(" ");
start = end;
break;
}
else
{
end++;
lenght = end - start + 1;
}
}
else
{
continue;
}
}
}
}
end++;
lenght = end - start + 1;
start = end;
} while (start < text.Length);*/





Attachments:
File comment: Huffman algorithm implemented using c sharp (C#)
Huffman Data Comperision.rar [456.92 KiB]
Downloaded 1916 times

_________________
Please recommend my post if you found it helpful
Author:
Newbie
User avatar Posts: 9
Have thanks: 0 time

can you karim reupload this links:(


Author:
Newbie
User avatar Posts: 5
Have thanks: 0 time

updated.


Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : Huffman Data compression-decompression code C#-csharp
 LZW data compression-decompression algorithm java code     -  
 Huffman Compression-Decompression     -  
 Arithmetic data comparison and decompression java code     -  
 Huffman coding for image compression     -  
 source code of huffman coding ?     -  
 php code to get yahoo stock data     -  
 Image compression     -  
 Standard Huffman Coding     -  
 Arithmatic Coding Com-Decompression algorithm     -  
 Row Transposition cipher - encryption-decryption Csharp(C#)     -  



Topic Tags

C# Algorithms






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com