信息論與編碼 實驗1 哈夫曼編碼 實驗報告
一、運行源代碼所需要的依賴:
1、硬件支持
Windows 10,64位系統(tǒng)
2、編譯器
DEV-Redpanda IDE,小熊貓C++
二、算法實現(xiàn)及測試
1、C語言源程序
# define _CRT_SECURE_NO_WARNINGS
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
// 文件數(shù)據(jù)結(jié)構(gòu)
struct head
{
int b; //字符
long count; //文件中該字符出現(xiàn)的次數(shù)
long parent, lch, rch; //父、左右子節(jié)點,make a tree
char bits[256]; //存儲編碼 the huffuman code
};
struct head header[512], tmp; //節(jié)點樹
// 函數(shù):printfPercent()
// 作用:輸出壓縮進(jìn)度
void printfPercent(int per)
{
int i = 0;
printf("|");
for(i = 0; i < 10; i++)
{
if(i < per/10)
printf(">");
else
printf("-");
}
printf("|已完成%d%%\n",per);
}
//函數(shù):compress()
//作用:讀取文件內(nèi)容并加以壓縮
//將壓縮內(nèi)容寫入另一個文檔
int compress(const char *filename,const char *outputfile)
{
char buf[512];
unsigned char c;
long i, j, m, n, f;
long min1, pt1, flength;
FILE *ifp, *ofp;
int per = 10;
ifp = fopen(filename, "rb"); //打開原始文件
if (ifp == NULL)
{
printf("打開文件失敗:%s\n",filename);
return 0; //如果打開失敗,則輸出錯誤信息
}
ofp = fopen(outputfile,"wb"); //打開壓縮后存儲信息的文件
if (ofp == NULL)
{
printf("打開文件失敗:%s\n",outputfile);
return 0;
}
fprintf(ofp, "%s", "myzip");
flength = 0;
while (!feof(ifp))
{
fread(&c, 1, 1, ifp);
header[c].count ++; //讀文件,統(tǒng)計字符出現(xiàn)次數(shù)
flength ++; //記錄文件的字符總數(shù)
}
flength --;
header[c].count --;
for (i = 0; i < 512; i ++) //HUFFMAN算法中初始節(jié)點的設(shè)置
{
if (header[i].count != 0)
header[i].b = (unsigned char) i;
else
header[i].b = -1;
header[i].parent = -1;
header[i].lch = header[i].rch = -1;
}
for (i = 0; i < 256; i ++) //將節(jié)點按出現(xiàn)次數(shù)排序
{
for (j = i + 1; j < 256; j ++)
{
if (header[i].count < header[j].count)
{
tmp = header[i];
header[i] = header[j];
header[j] = tmp;
}
}
}
for (i = 0; i < 256; i ++) //統(tǒng)計不同字符的數(shù)量
{
if (header[i].count == 0)
break;
}
n = i;
m = 2 * n - 1;
for (i = n; i < m; i ++)
{
min1 = 999999999;
for (j = 0; j < i; j ++)
{
if (header[j].parent != -1) continue;
if (min1 > header[j].count)
{
pt1 = j;
min1 = header[j].count;
continue;
}
}
header[i].count = header[pt1].count;
header[pt1].parent = i;
header[i].lch = pt1;
min1 = 999999999;
for (j = 0; j < i; j ++)
{
if (header[j].parent != -1) continue;
if (min1 > header[j].count)
{
pt1 = j;
min1 = header[j].count;
continue;
}
}
header[i].count += header[pt1].count;
header[i].rch = pt1;
header[pt1].parent = i;
}
for (i = 0; i < n; i ++) //構(gòu)造HUFFMAN樹,設(shè)置字符的編碼
{
f = i;
header[i].bits[0] = 0;
while (header[f].parent != -1)
{
j = f;
f = header[f].parent;
if (header[f].lch == j)
{
j = strlen(header[i].bits);
memmove(header[i].bits + 1, header[i].bits, j + 1);
header[i].bits[0] = '0';
}
else
{
j = strlen(header[i].bits);
memmove(header[i].bits + 1, header[i].bits, j + 1);
header[i].bits[0] = '1';
}
}
}
//下面的就是讀原文件的每一個字符,按照設(shè)置好的編碼替換文件中的字符
fseek(ifp, 0, SEEK_SET); //將指針定在文件起始位置
fseek(ofp, 8, SEEK_SET); //以8位二進(jìn)制數(shù)為單位進(jìn)行讀取
buf[0] = 0;
f = 0;
pt1 = 8;
printf("讀取將要壓縮的文件:%s\n",filename);
printf("當(dāng)前文件有:%d字符\n",flength);
if(flength == 0){
printf("警告:該文件為空\n");
return 0;
}
printf("正在壓縮\n");
while (!feof(ifp))
{
c = fgetc(ifp);
f ++;
for (i = 0; i < n; i ++)
{
if (c == header[i].b) break;
}
strcat(buf, header[i].bits);
j = strlen(buf);
c = 0;
while (j >= 8) //當(dāng)剩余字符數(shù)量不小于8個時
{
for (i = 0; i < 8; i ++) //按照八位二進(jìn)制數(shù)轉(zhuǎn)化成十進(jìn)制ASCII碼寫入文件一次進(jìn)行壓縮
{
if (buf[i] == '1') c = (c << 1) | 1;
else c = c << 1;
}
fwrite(&c, 1, 1, ofp);
pt1 ++;
strcpy(buf, buf + 8);
j = strlen(buf);
}
if(100 * f/flength > per)
{
printfPercent(per);
per += 10;
}
if (f == flength)
break;
}
printfPercent(100);
if (j > 0) //當(dāng)剩余字符數(shù)量少于8個時
{
strcat(buf, "00000000");
for (i = 0; i < 8; i ++)
{
if (buf[i] == '1') c = (c << 1) | 1;
else c = c << 1; //對不足的位數(shù)進(jìn)行補零
}
fwrite(&c, 1, 1, ofp);
pt1 ++;
}
fseek(ofp, 0, SEEK_SET); //將編碼信息寫入存儲文件
fwrite(&flength,1,sizeof(flength),ofp);
fwrite(&pt1, sizeof(long), 1, ofp);
fseek(ofp, pt1, SEEK_SET);
fwrite(&n, sizeof(long), 1, ofp);
for (i = 0; i < n; i ++)
{
tmp = header[i];
fwrite(&(header[i].b), 1, 1, ofp);
pt1++;
c = strlen(header[i].bits);
fwrite(&c, 1, 1, ofp);
pt1++;
j = strlen(header[i].bits);
if (j % 8 != 0) //當(dāng)位數(shù)不滿8時,對該數(shù)進(jìn)行補零操作
{
for (f = j % 8; f < 8; f ++)
strcat(header[i].bits, "0");
}
while (header[i].bits[0] != 0)
{
c = 0;
for (j = 0; j < 8; j ++)
{
if (header[i].bits[j] == '1') c = (c << 1) | 1;
else c = c << 1;
}
strcpy(header[i].bits, header[i].bits + 8);
fwrite(&c, 1, 1, ofp); //將所得的編碼信息寫入文件
pt1++;
}
header[i] = tmp;
}
fclose(ifp);
fclose(ofp); //關(guān)閉文件
printf("壓縮后文件為:%s\n",outputfile);
printf("壓縮后文件有:%d字符\n",pt1 + 4);
return 1; //返回壓縮成功信息
}
//函數(shù):compress()
//作用:讀取文件內(nèi)容并加以壓縮
//將壓縮內(nèi)容寫入另一個文檔
int compress(const char *filename,const char *outputfile)
{
char buf[512];
unsigned char c;
long i, j, m, n, f;
long min1, pt1, flength;
FILE *ifp, *ofp;
int per = 10;
ifp = fopen(filename, "rb"); //打開原始文件
if (ifp == NULL)
{
printf("打開文件失敗:%s\n",filename);
return 0; //如果打開失敗,則輸出錯誤信息
}
ofp = fopen(outputfile,"wb"); //打開壓縮后存儲信息的文件
if (ofp == NULL)
{
printf("打開文件失敗:%s\n",outputfile);
return 0;
}
fprintf(ofp, "%s", "myzip");
flength = 0;
while (!feof(ifp))
{
fread(&c, 1, 1, ifp);
header[c].count ++; //讀文件,統(tǒng)計字符出現(xiàn)次數(shù)
flength ++; //記錄文件的字符總數(shù)
}
flength --;
header[c].count --;
for (i = 0; i < 512; i ++) //HUFFMAN算法中初始節(jié)點的設(shè)置
{
if (header[i].count != 0)
header[i].b = (unsigned char) i;
else
header[i].b = -1;
header[i].parent = -1;
header[i].lch = header[i].rch = -1;
}
for (i = 0; i < 256; i ++) //將節(jié)點按出現(xiàn)次數(shù)排序
{
for (j = i + 1; j < 256; j ++)
{
if (header[i].count < header[j].count)
{
tmp = header[i];
header[i] = header[j];
header[j] = tmp;
}
}
}
for (i = 0; i < 256; i ++) //統(tǒng)計不同字符的數(shù)量
{
if (header[i].count == 0)
break;
}
n = i;
m = 2 * n - 1;
for (i = n; i < m; i ++)
{
min1 = 999999999;
for (j = 0; j < i; j ++)
{
if (header[j].parent != -1) continue;
if (min1 > header[j].count)
{
pt1 = j;
min1 = header[j].count;
continue;
}
}
header[i].count = header[pt1].count;
header[pt1].parent = i;
header[i].lch = pt1;
min1 = 999999999;
for (j = 0; j < i; j ++)
{
if (header[j].parent != -1) continue;
if (min1 > header[j].count)
{
pt1 = j;
min1 = header[j].count;
continue;
}
}
header[i].count += header[pt1].count;
header[i].rch = pt1;
header[pt1].parent = i;
}
for (i = 0; i < n; i ++) //構(gòu)造HUFFMAN樹,設(shè)置字符的編碼
{
f = i;
header[i].bits[0] = 0;
while (header[f].parent != -1)
{
j = f;
f = header[f].parent;
if (header[f].lch == j)
{
j = strlen(header[i].bits);
memmove(header[i].bits + 1, header[i].bits, j + 1);
header[i].bits[0] = '0';
}
else
{
j = strlen(header[i].bits);
memmove(header[i].bits + 1, header[i].bits, j + 1);
header[i].bits[0] = '1';
}
}
}
//下面的就是讀原文件的每一個字符,按照設(shè)置好的編碼替換文件中的字符
fseek(ifp, 0, SEEK_SET); //將指針定在文件起始位置
fseek(ofp, 8, SEEK_SET); //以8位二進(jìn)制數(shù)為單位進(jìn)行讀取
buf[0] = 0;
f = 0;
pt1 = 8;
printf("讀取將要壓縮的文件:%s\n",filename);
printf("當(dāng)前文件有:%d字符\n",flength);
if(flength == 0){
printf("警告:該文件為空\n");
return 0;
}
printf("正在壓縮\n");
while (!feof(ifp))
{
c = fgetc(ifp);
f ++;
for (i = 0; i < n; i ++)
{
if (c == header[i].b) break;
}
strcat(buf, header[i].bits);
j = strlen(buf);
c = 0;
while (j >= 8) //當(dāng)剩余字符數(shù)量不小于8個時
{
for (i = 0; i < 8; i ++) //按照八位二進(jìn)制數(shù)轉(zhuǎn)化成十進(jìn)制ASCII碼寫入文件一次進(jìn)行壓縮
{
if (buf[i] == '1') c = (c << 1) | 1;
else c = c << 1;
}
fwrite(&c, 1, 1, ofp);
pt1 ++;
strcpy(buf, buf + 8);
j = strlen(buf);
}
if(100 * f/flength > per)
{
printfPercent(per);
per += 10;
}
if (f == flength)
break;
}
printfPercent(100);
if (j > 0) //當(dāng)剩余字符數(shù)量少于8個時
{
strcat(buf, "00000000");
for (i = 0; i < 8; i ++)
{
if (buf[i] == '1') c = (c << 1) | 1;
else c = c << 1; //對不足的位數(shù)進(jìn)行補零
}
fwrite(&c, 1, 1, ofp);
pt1 ++;
}
fseek(ofp, 0, SEEK_SET); //將編碼信息寫入存儲文件
fwrite(&flength,1,sizeof(flength),ofp);
fwrite(&pt1, sizeof(long), 1, ofp);
fseek(ofp, pt1, SEEK_SET);
fwrite(&n, sizeof(long), 1, ofp);
for (i = 0; i < n; i ++)
{
tmp = header[i];
fwrite(&(header[i].b), 1, 1, ofp);
pt1++;
c = strlen(header[i].bits);
fwrite(&c, 1, 1, ofp);
pt1++;
j = strlen(header[i].bits);
if (j % 8 != 0) //當(dāng)位數(shù)不滿8時,對該數(shù)進(jìn)行補零操作
{
for (f = j % 8; f < 8; f ++)
strcat(header[i].bits, "0");
}
while (header[i].bits[0] != 0)
{
c = 0;
for (j = 0; j < 8; j ++)
{
if (header[i].bits[j] == '1') c = (c << 1) | 1;
else c = c << 1;
}
strcpy(header[i].bits, header[i].bits + 8);
fwrite(&c, 1, 1, ofp); //將所得的編碼信息寫入文件
pt1++;
}
header[i] = tmp;
}
fclose(ifp);
fclose(ofp); //關(guān)閉文件
printf("壓縮后文件為:%s\n",outputfile);
printf("壓縮后文件有:%d字符\n",pt1 + 4);
return 1; //返回壓縮成功信息
}
// 主函數(shù)
int main(int argc,const char *argv[])
{
memset(&header,0,sizeof(header));
memset(&tmp,0,sizeof(tmp));
char tmpname[100] = { 0 };
char filename[100] = { 0 };
char zipname[100] = { 0 };
char recovername[100] = { 0 };
//用戶接口部分
printf("------------歡迎使用本文件壓縮程序,按任意鍵開始------------\n");
system("pause");
printf("------------請在下方輸入你想要壓縮的文件名------------\n");
scanf("%s", tmpname);
strcpy(filename, tmpname);
strcat(tmpname, ".zip");
strcpy(zipname, tmpname);
printf("-----------壓縮開始,請稍等片刻-----------\n");
compress(filename,zipname);
strcat(tmpname, "解壓后.txt");
strcpy(recovername, tmpname);
uncompress(zipname,recovername);
system("pause");
return 0;
}
2、算法性能測試
(1)測試文件1:article1.txt
文件說明:普通英文文檔,取自英國小說《哈利·波特》的一個章節(jié)
文件截圖:
運行時截圖:
(2)測試文件2:singlebyte.txt
文件說明:只含一種字節(jié)(字母a)
文件截圖:
運行時截圖:
(3)測試文件3:empty.txt
文件說明:空文本文件,大小為0KB。
運行時截圖:
三、實踐性問題
1、重復(fù)性的文件結(jié)構(gòu)
使用你在必選模組實現(xiàn)的程序,編碼 lab1_data/testfile1,lab1_data/testfile1 的文件內(nèi)容是:256 字節(jié) 0x00,256 字節(jié) 0x01,256 字節(jié) 0x02,…,256 字節(jié) 0xff,總計 64 KiB。觀察編碼后的文件大小,解釋為什么文件體積會發(fā)生這種變化(為什么會這樣(模組 1 和 模組 3) / 是什么發(fā)揮了作用(模組 2))。
解答:
文件名稱:testfile1.txt
壓縮前大小:65536字節(jié)(64KB)
壓縮后大?。?6249字節(jié)(65KB)
壓縮比例:101.6%
原文件SHA256值:
c3e145bc3d6790dab078e628cf8530b565c30aac7b5c70fd100b67651d064ad4
壓縮后文件SHA256值:
23321562a0a3a4d891de297dade09c3ab0794d3e222dc8a08206429f0dd59e8a
解壓縮后文件SHA256值:
c3e145bc3d6790dab078e628cf8530b565c30aac7b5c70fd100b67651d064ad4
運行時截圖:
現(xiàn)象:壓縮比例大于1,即壓縮后的文件比壓縮前的文件更大。
對該現(xiàn)象的解釋:對于testfile1.txt中的字符,即0x00, 0x01, 0x02, …0xff共計256種字節(jié),每種字節(jié)出現(xiàn)的概率相同,所生成的哈夫曼樹為一棵極不平衡的二叉樹,形如下圖:
其中,256種字節(jié)(與順序無關(guān))分別被編碼成碼長為1,2,3,4,5,……255,255(單位為bit)共256個碼元。故壓縮后的文件大小為256 * (1 + 2 + 3 + 4 + 5 + …… + 255 + 255)= 256 * 255 * 129 bits。原文件大小為64KB = 64 * 2^10 * 8 = 2^19 bits. 可見,壓縮后的文件體積比壓縮前的文件體積更大。
2、“黑洞”來了?
問: 如果我將一個超大的文件壓縮幾百次,他就會變得越來越小,最后達(dá)到幾 KB,這便是時間換空間!這種說法正確嗎?
答: 這種說法不正確。
實踐題: 對給定的 bmp 格式圖片迭代地編碼 10 次,觀察壓縮結(jié)果的體積變化。為什么會發(fā)生這種變化?結(jié)合你學(xué)過的知識解答。
答: 壓縮結(jié)果的體積變化如下表所示:
第n次壓縮 壓縮前大小(KB) 壓縮后大?。↘B)
1 24301 3687
2 3687 1069
3 1069 791
4 791 773
5 773 781
6 781 792
7 792 803
8 803 814
9 814 826
10 826 837
被壓縮的文件截圖:
現(xiàn)象: 從第3次壓縮操作之后,文件體積基本上沒有變化,從第5次壓縮操作開始,反而表現(xiàn)出越壓縮,文件體積越大。
對該現(xiàn)象的解釋: 香農(nóng)Shannon信息論中的信息熵。熵界的存在性,導(dǎo)致文件不可能被壓縮至 “無限小” (單個比特),所謂的 “文件黑洞” 違背信息論。文章來源:http://www.zghlxwxcb.cn/news/detail-821856.html
至此,本次實驗結(jié)束。文章來源地址http://www.zghlxwxcb.cn/news/detail-821856.html
到了這里,關(guān)于【信息論與編碼】【北京航空航天大學(xué)】實驗一、哈夫曼編碼【C語言實現(xiàn)】(上)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!