當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 編程軟體 » 哈夫曼編譯課程設計

哈夫曼編譯課程設計-九游会j9娱乐平台

發布時間: 2024-01-19 01:07:08

ⅰ 哈夫曼編、解碼器

這個是我課程設計弄的,也是哈弗曼編碼解碼器
#include
#include
#include
using namespace std;

typedef struct{
int weight;
int parent,lchild,rchild;
int asc;
}htnode,*huffmantree; //定義赫夫曼存儲結構

struct node{
int ascii;
int n;
};
struct node a[127];
struct node w[127];
//一些全局變數
int count;
htnode h_t[250];
char ** hc;
char filename[20];

//函數聲明
void menu1(); //菜單1
huffmantree heapsort(huffmantree ht,int length); //堆排序
void minheapify(huffmantree ht, int k,int len); //調整成一個小頂堆
void buildminheap(huffmantree ht,int len); //建一個小頂堆
void swap(htnode &t1,htnode &t2); //交換兩結構體
void savefile(); //把輸入的英文文章保存到文件
void loadfile(); //從文件中讀取文章
huffmantree createhuffman(huffmantree &ht,struct node *w,int n); //建立赫夫曼數並存入文件
void bianma(huffmantree ht,int n ); //字元編碼
void bianma_all(huffmantree ht,char**hc,char *filename); //整篇文章編碼
int loadfile2(); //從文件讀入文章
void jiema(); //解碼

//主函數
int main()
{
char s;
while(s!='0')
{
system("cls");
cout<<"\n\n\n";
cout<<"\t\t\t\t赫夫曼編碼/解碼器"< cout<<"\t\t\t\t 1.編碼"< cout<<"\t\t\t\t 2.解碼"< cout<<"\t\t\t\t 0.退出"< cout<<"\t請輸入0—2進行選擇,按回車確認";
cin>>s;
switch(s)
{
case '1': menu1(); break;
case '2':
{
system("cls");
jiema();
system("pause");
break;
}

}

}
}

//菜單1
void menu1(){
char s;
int i;
int a;
char c;
char fpname[20]="article.txt";
huffmantree ht;
while(s!='0'){

system("cls");
cout<<"\n\t\t\t\t編碼界面";
cout<<"\n\n\n\t\t\t\t1.輸入英文文章"< cout<<"\n\n\t\t\t\t2.從文件中讀入文章"< cout<<"\n\n\t\t\t\t0.返回上一層"< cout<<"\n\t請輸入0—2進行選擇,按回車確認";
cin>>s;
switch(s){
case'1':
system("cls");
savefile();
loadfile();
createhuffman(ht,w,count);
bianma(ht,count);
bianma_all(ht,hc,fpname);
system("cls");
cout<<"出現字元種類共計:";
cout< for(i=1;i<=count;i )
{ a=ht[i].asc;
c=(char)a;

cout<<"________________________________________________________________________________"< cout<<"\t\t\t字元:";
cout< cout<<"\t\t\tascii碼:";
cout< cout<<"\t\t\t頻數:";
cout< cout<<"\t\t\t赫夫曼編碼:";
cout<
}
cout<<"________________________________________________________________________________";
cout<<"\n\t\t整篇文章的編碼已存入文件「赫夫曼編碼.txt」"<
system("pause");
break;

case'2':
system("cls");
if(loadfile2())
{ system("pause");
return;}
createhuffman(ht,w,count);
bianma(ht,count);
bianma_all(ht,hc,filename);
system("cls");
cout<<"出現字元種類共計:";
cout< for(i=1;i<=count;i )
{ a=ht[i].asc;
c=(char)a;

cout<<"________________________________________________________________________________"< cout<<"\t\t\t字元:";
cout< cout<<"\t\t\tascii碼:";
cout< cout<<"\t\t\t頻數:";
cout< cout<<"\t\t\t赫夫曼編碼:";
cout<
}
cout<<"________________________________________________________________________________";
cout<<"\n\t\t整篇文章的編碼已存入文件「赫夫曼編碼.txt」"< system("pause");
break;
}

}

}

//交換結構體
void swap(htnode &t1,htnode &t2)
{
htnode m;

m = t1;
t1 = t2;
t2 = m;
}

//從鍵盤輸入文章並保存
void savefile(){

file *fp;
char article;
if((fp=fopen("article.txt","w"))==null){

printf("打開文件不成功!");
exit(0);
}
cout<<"請輸入英文文章,以#結束:";
getchar();
article=getchar();
while(article!='#'){

fputc(article,fp);

article=getchar();
}
fclose(fp);
}

//從文件讀取文章,並統計字元出現頻數
void loadfile(){

file *fp;
char ch;
int i,k,j=0;
count=0;
for(i=0;i<=127;i ) //把所有字元的ascii碼存在數組
{ a[i].ascii=i;
a[i].n=0;

}
if((fp=fopen("article.txt","r"))==null){

printf("打開文件不成功!");
exit(0);
}
ch=fgetc(fp);
k=(int)ch;
a[k].n ;
while(!feof(fp)){
ch=fgetc(fp);
k=(int)ch;
a[k].n ;
}
fclose(fp);

for(i=0;i<=127;i ) //統計字元種類總數
{
ch=(char)i;
if(a[i].n){
count ;
}
}
for(i=0;i<=127;i )
{
for(;j {
if(a[i].n)
{
w[j].n=a[i].n;
w[j].ascii=a[i].ascii;
j ;
break;
}
else break;
}
}

}

//調整為小頂堆
void minheapify(huffmantree ht, int k,int len)
{
int left=k*2;
int right=k*2 1;
int large;
int l=len;

large = k;
if (left<=l&&ht[left].weight large = left;

if (right<=l&& ht[right].weight large=right;

if (large!=k)
{
swap(ht[k],ht[large]);
minheapify(ht,large,l);
}
}

//建立小頂堆
void buildminheap(huffmantree ht,int len)
{
int i;
for (i=len/2;i>=1;i--)
{
minheapify(ht,i,len);
}
}

//堆排序
huffmantree heapsort(huffmantree ht,int length)
{
int i;
int l=length;
buildminheap(ht,length);
for (i=length;i>= 2;i--)
{
swap(ht[1],ht[i]);
length--;
minheapify(ht,1,length);
}

return ht;
}

//建立赫夫曼數
huffmantree createhuffman(huffmantree &ht,struct node *w,int n)
{
int i,m,s1,s2,k1,k2,j,x,a;
file *fp,*fp2;

if(n<=1) return ht;
m=2*n-1;
ht=(huffmantree)malloc((m 1)*sizeof(htnode));//0不用

for(i=1,j=0;i<=n;i ,j )
{ ht[i].asc=w[j].ascii;
ht[i].weight=w[j].n;
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(;i<=m;i )
{ a=250 i;
ht[i].asc=a;//父親節點的asc可以是大於127的任意值
ht[i].weight=0;
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=1;i<=n;i ){

h_t[i].asc=ht[i].asc;
h_t[i].parent=ht[i].parent;
h_t[i].lchild=ht[i].lchild;
h_t[i].rchild=ht[i].rchild;
h_t[i].weight=ht[i].weight;
}

for(i=n 1,x=n;i<=m;i ,x--)
{

heapsort(h_t,x);
k1=h_t[x].asc;
k2=h_t[x-1].asc;
for(j=1;j<=127;j )
{
if(ht[j].asc==k1)

}

for(j=1;j<=127;j )
{
if(ht[j].asc==k2)

}

ht[s2].parent=i;
ht[s1].parent=i;
ht[i].lchild=s1;
ht[i].rchild=s2;
ht[i].weight=ht[s1].weight ht[s2].weight;

h_t[x-1].asc=ht[i].asc;
h_t[x-1].lchild=ht[i].lchild;
h_t[x-1].parent=ht[i].parent;
h_t[x-1].rchild=ht[i].rchild;
h_t[x-1].weight=ht[i].weight;

}
if((fp2=fopen("count.txt","w"))==null) //保存赫夫曼樹
{
cout<<"文件打開不成功!"< exit(0);
}
fputc(count,fp2);
if((fp=fopen("huffmantree.dat","wb"))==null)
{ cout<<"文件打開不成功!"< exit(0);

}

for(i=1;i<=(2*count-1);i ){
fwrite(&ht[i],sizeof(htnode),1,fp);
}
fclose(fp);
fclose(fp2);
return ht;

}

//逆向編碼
void bianma(huffmantree ht,int n){
char *cd,temp;

int c,f,i,j,len,p,q;

cd=(char *)malloc(n*sizeof(char));
hc=(char * *)malloc(n*sizeof(char*));
for(i=1;i<=n;i ){
for(c=i,f=ht[i].parent,j=0;f!=0;c=f,f=ht[f].parent,j )
{ if(ht[f].lchild==c) cd[j]='0';
else cd[j]='1';
if(ht[f].parent==0)
cd[j 1]='\0';

}

len=strlen(cd);
for(p=0,q=len-1;p<=q;p ,q--)
{
temp=cd[q];
cd[q]=cd[p];
cd[p]=temp;
}
cd[len]='\0';
hc[i]=(char*)malloc((len 10)*sizeof(char));
strcpy(hc[i],cd);

}

}

//整篇文章編碼,並存入文件
void bianma_all(huffmantree ht,char**hc,char *filename){
char ch;
int k,i;
file *fp,*fp2;

char code[100];
if((fp=fopen(filename,"r"))==null){

printf("打開文件不成功!");
exit(0);
}
if((fp2=fopen("赫夫曼編碼.txt","w"))==null){

printf("打開文件不成功!");
exit(0);
}
ch=fgetc(fp);
k=(int)ch;
while(!feof(fp))
{

for(i=1;i<=count;i )
{
if(k==ht[i].asc)
strcpy(code,hc[i]);

}
fputs(code,fp2);
ch=fgetc(fp);
k=(int)ch;

}
fclose(fp);
fclose(fp2);

}

void jiema(){
int i,k,a,t,n=0;
file *fp1,*fp2,*fp3;
char ch,c;
huffmantree ht;
if((fp3=fopen("count.txt","r"))==null) //從文件讀出字元總數
{
printf("打開文件不成功!");
exit(0);
}
n=fgetc(fp3);
ht=(huffmantree)malloc(2*n*sizeof(htnode));
if((fp1=fopen("赫夫曼編碼.txt","r"))==null)
{

printf("打開文件不成功!");
exit(0);
}

if((fp2=fopen("huffmantree.dat","rb"))==null)
{ cout<<"文件打開不成功!"< exit(0);

}
for(i=1;i<=2*n-1;i )

fread(&ht[i],sizeof(htnode),1,fp2);

for(i=1;i<=2*n-1;i )
{
if(ht[i].parent==0)
}

ch=fgetc(fp1);
while(!feof(fp1)){
if(ch=='0')
{
k=ht[k].lchild;
if(ht[k].lchild==0)
{a=ht[k].asc;
c=(char)a;
printf("%c",c);;

k=t;
}
}
if(ch=='1')
{
k=ht[k].rchild;
if(ht[k].lchild==0)
{ a=ht[k].asc;
c=(char)a;
printf("%c",c);

k=t;
}

}

ch=fgetc(fp1);
}
fclose(fp1);
fclose(fp2);

}

//讀取文件中的文章,可自己選擇文件
int loadfile2(){

file *fp;
char ch;
int i,k,j=0;
count=0;
for(i=0;i<=127;i )
{ a[i].ascii=i;
a[i].n=0;

}
cout<<"\n\n\n\t\t\t請輸入你要打開的文件名:";
cin>>filename;
if((fp=fopen(filename,"r"))==null){

printf("打開文件不成功!");
return 1;
}
ch=fgetc(fp);
k=(int)ch;
a[k].n ;
while(!feof(fp)){
ch=fgetc(fp);
k=(int)ch;
a[k].n ;
}
fclose(fp);

for(i=0;i<=127;i ){
ch=(char)i;
if(a[i].n){
count ;
}
}
for(i=0;i<=127;i )
{
for(;j {
if(a[i].n)
{
w[j].n=a[i].n;
w[j].ascii=a[i].ascii;
j ;
break;
}
else break;
}
}
return 0;
}

ⅱ 我們有個數據結構的哈夫曼編碼解碼的課程設計,你能幫幫我嗎

樹和哈夫曼樹實驗報告

一.實驗目的
練習樹和哈夫曼樹的有關操作,和各個演算法程序,理解哈夫曼樹的編碼和解碼
二.實驗環境
microsoft visual c
三.實驗問題描述
1. 問題描述:建立一棵用二叉鏈表方式存儲的二叉樹,並對其進行遍歷(先序、中序和後序),列印輸出遍歷結果。
基本要求:從鍵盤接受輸入先序序列,以二叉鏈表作為存儲結構,建立二叉樹(以先序來建立),並將此二叉樹按照「樹狀形式」列印輸出,然後對其進行遍歷(先序、中序和後序),最後將遍歷結果列印輸出。在遍歷演算法中要求至少有一種遍歷採用非遞歸方法。
測試數據:
abcøødeøgøøføøø(其中ø表示空格字元)
輸出結果為:
先序:abcdegf
先序:cbegdfa
先序:cgefdba
2. 問題描述:利用哈夫曼編碼進行通信可以大大提高信道利用率,縮簡訊息傳輸時間,降低傳輸成本。但是,這要求在發送端通過一個編碼系統對待傳數據預先編碼,在接受端將傳來的數據進行解碼(復原)。對於雙工信道(即可以雙向傳輸信息的信道),每端都需要一個完整的編/解碼系統。試為這樣的信息收發站寫一個哈夫曼碼的編/解碼系統。
基本要求:(至少完成功能1-2)
一個完整的系統應具有以下功能:
i:初始化(initialization)。從終端讀入字元集大小n,以及n個字元和n個權值,建立哈夫曼樹,並將它存於文件hfmtree中。
基本要求:
e:編碼(encoding)。利用已建好的哈夫曼樹(如不在內存,則從文件hfmtree中讀入),對文件tobetran中的正文進行編碼,然後將結果存入文件codefile中。
d:解碼(decoding )。利用已建好的哈夫曼樹將文件codefile中的代碼進行解碼,結果存入文件textfile中。
p:印代碼文件(print)。將文件codefile以緊湊格式顯示在終端上,每行50個代碼。同時將此字元形式的編碼文件寫入文件codeprint中。
t:印哈夫曼樹(treeprinting)。將已在內存中的哈夫曼樹以直觀的方式(樹或凹入表形式)顯示在終端上,同時將此字元形式的哈夫曼樹寫入文件treeprint中。
測試數據:
設權值w=(5,29,7,8,14,23,3,11),n=8。
按照字元『0』或『1』確定找左孩子或右孩子,則權值對應的編碼為:
5:0001,29:11,7:1110,8:1111
14:110,23:01,3:0000,11:001
用下表給出的字元集和頻度的實際統計數據建立哈夫曼樹,並實現以下報文的編碼和解碼:「this program is my favorite」。
四.實驗主要程序流

實驗題目一主要程序:

1.
void creatbitree(bittree *bt)//用擴展先序遍歷序列創建二叉樹,如果是#當前樹根置為空,否則申請一個新節點//
{
char ch;
ch=getchar();
if(ch=='.')*bt=null;
else
{
*bt=(bittree)malloc(sizeof(bitnode));
(*bt)->data=ch;
creatbitree(&((*bt)->lchild));
creatbitree(&((*bt)->rchild));
}
}
2.void visit(char ch)//訪問根節點
{
printf("%c ",ch);
}
3.
void preorder(bittree root)
{
if (root!=null)
{
visit(root ->data);
preorder(root ->lchild);
preorder(root ->rchild);
}
}
4. void inorder(bittree root)

{
if (root!=null)
{
inorder(root ->lchild);
visit(root ->data);
inorder(root ->rchild);
}
}
5.int posttreedepth(bittree bt) //後序遍歷求二叉樹的高度遞歸演算法//
{
int hl,hr,max;
if(bt!=null)
{
hl=posttreedepth(bt->lchild); //求左子樹的深度
hr=posttreedepth(bt->rchild); //求右子樹的深度
max=hl>hr?hl:hr; //得到左、右子樹深度較大者
return(max 1); //返回樹的深度
}
else return(0); //如果是空樹,則返回0
}
6.void printtree(bittree boot,int nlayer) //按豎向樹狀列印的二叉樹 //
{
int i;
if(boot==null) return;
printtree(boot->rchild,nlayer 1);
for(i=0;i printf(" ");
printf("%c\n",boot->data);
printtree(boot->lchild,nlayer 1);
}
7.void main()
{
bittree t;
int h;
int layer;
int treeleaf;
layer=0;
printf("請輸入二叉樹中的元素(以擴展先序遍歷序列輸入,其中.代表空子樹):\n");
creatbitree(&t);
printf("先序遍歷序列為:");
preorder(t);
printf("\n中序遍歷序列為:");
inorder(t);
printf("\n後序遍歷序列為:");
postorder(t);
h=posttreedepth(t);
printf("\此二叉樹的深度為:%d\n",h);
printf("此二叉樹的橫向顯示為:\n");
printtree(t,layer);
}
實驗二主要程序流:
1.int main(){
huffmantree huftree;
char choose;
while(1){
cout<<"\n**********************歡迎使用哈夫曼編碼/解碼系統**********************\n";
cout<<"*您可以進行以下操作: *\n";
cout<<"*1.建立哈夫曼樹 *\n";
cout<<"*2.編碼(源文已在文件tobetra中,或鍵盤輸入) *\n";
cout<<"* 3.解碼(碼文已在文件codefile中) *\n";
cout<<"* 4.顯示碼文 *\n";
cout<<"* 5.顯示哈夫曼樹 *\n";
cout<<"* 6.退出 *\n"; cout<<"***********************************************************************\n";
cout<<"請選擇一個操作:";
cin>>choose;
switch(choose)
{
case '1':
huftree.createhuffmantree();
break;
case '2':
huftree.encoder();
break;
case '3':
huftree.decoder();
break;
case '4':
huftree.printcodefile();
break;
case '5':
huftree.printhuffmantree();
break;
case '6':
cout<<"\n**********************感謝使用本系統!*******************\n\n";
system("pause");
return 0;
}//switch
}//while
}//main
2.// 建立哈夫曼樹函數
// 函數功能:建立哈夫曼樹(調用鍵盤建立哈夫曼樹或調用從文件建立哈夫曼樹的函數)
void huffmantree::createhuffmantree()
{char choose;
cout<<"你要從文件中讀入哈夫曼樹(按1),還是從鍵盤輸入哈夫曼樹(按2)?";
cin>>choose;
if(choose=='2') { //鍵盤輸入建立哈夫曼樹 createhuffmantreefromkeyboard();
}//choose=='2'
else { //從哈夫曼樹文件hfmtree.dat中讀入信息並建立哈夫曼樹
createhuffmantreefromfile();
}
}
3. // 從鍵盤建立哈夫曼樹函數
// 函數功能:從鍵盤建立哈夫曼樹
//函數參數:無
//參數返回值:無
void huffmantree::createhuffmantreefromkeyboard(){
int num;
cout<<"\n請輸入源碼字元集個數:";
cin>>num;
if (num<=1) {
cout<<"無法建立少於2個葉子結點的哈夫曼樹。\n\n";
return;
}
leafnum=num;
node=new huffmannode[2*num-1];
for(int i=0;i cout<<"請輸入第"< getchar();
node[i].sourcecode=getchar(); //源文的字元存入字元數組info[]
getchar();
cout<<"請輸入該字元的權值或頻度";
cin>>node[i].weight; //源文的字元權重存入node[].weight
node[i].parent=-1;
node[i].lchild=-1;
node[i].rchild=-1;
node[i].code="\0";
}
for(int j=num;j<2*num-1;j ) {//循環建立哈夫曼樹內部結點
int pos1,pos2;
int max1,max2;
pos2=pos1=j;
max2=max1=numeric_limits::max( );
//在所有子樹的根結點中,選權重最小的兩個根結點,pos1最後應指向權重最小的根結點的下標
//pos2最後應指向權重第二小的根結點的下標
//max1存放當前找到的權重最小的根結點的權重
//max2存放當前找到的權重第二小的根結點的權重
for(int k=j-1;k>=0;k--) {
if (node[k].parent==-1){//如果是某棵子樹的根結點
if (node[k].weight max2=max1;
max1=node[k].weight;
pos2=pos1;
pos1=k;
}
else
if(node[k].weight max2=node[k].weight;
pos2=k;
}
}//if (node[j].parent==-1)
} //for
//在下標i處新構造一個哈夫曼樹的內部結點,其左、右孩子就是以上pos1、pos2所指向的結點
node[pos1].parent=j;
node[pos2].parent=j;
node[j].lchild=pos1;
node[j].rchild=pos2;
node[j].parent=-1;
node[j].weight=node[pos1].weight node[pos2].weight;
} //for

//產生所有葉子結點中字元的編碼
for (int m=0;m //產生node[i].sourcecode的編碼,存入node[i].code中
int j=m;
int j1;
while(node[j].parent!=-1) { //從葉結點開始往根結點走,每往上走一層,就產生一位編碼存入code[]
j1=node[j].parent;
if(node[j1].lchild==j)
node[m].code.insert(0,"0");
else
node[m].code.insert(0,"1");
j=j1; }}
cout<<"哈夫曼樹已成功構造完成。\n";

//把建立好的哈夫曼樹寫入文件hfmtree.dat
char ch;
cout<<"是否要替換原來的哈夫曼樹文件(y/n):";
cin>>ch;
if (ch!='y'&&ch!='y') return;
ofstream fop;
fop.open("hfmtree.dat",ios::out|ios::binary|ios::trunc); //打開文件
if(fop.fail()) {
cout<<"\n哈夫曼樹文件打開失敗,無法將哈夫曼樹寫入hfmtree.dat文件。\n";
return;
}
fop.write((char*)&num,sizeof(num)); //先寫入哈夫曼樹的葉子結點個數
for(int n=0;n<2*num-1;n ) { //最後寫入哈夫曼樹的各個結點(存儲在node[]中)
fop.write((char*)&node[n],sizeof(node[n]));
flush(cout); }
fop.close(); //關閉文件
cout<<"\n哈夫曼樹已成功寫入hfmtree.dat文件。\n";}
4. // 從文件建立哈夫曼樹函數
// 函數功能:從文件建立哈夫曼樹
//函數參數:無
//參數返回值:無
void huffmantree::createhuffmantreefromfile(){
ifstream fip;
fip.open("hfmtree.dat",ios::binary|ios::in);
if(fip.fail()) {
cout<<"哈夫曼樹文件hfmtree.dat打開失敗,無法建立哈夫曼樹。\n";
return;
}
fip.read((char*)&leafnum,sizeof(leafnum));
if (leafnum<=1) {
cout<<"哈夫曼樹文件中的數據有誤,葉子結點個數少於2個,無法建立哈夫曼樹。\n";
fip.close();
return;
}
node=new huffmannode[2*leafnum-1];
for(int i=0;i<2*leafnum-1;i )
fip.read((char*)&node[i],sizeof(node[i]));
fip.close();
cout<<"哈夫曼樹已從文件成功構造完成。\n";
}
5. // 編碼函數
// 函數功能:為哈夫曼樹編碼
//函數參數:無
//參數返回值:無
void huffmantree::encoder()
{
if(node==null) { //內存沒有哈夫曼樹,則從哈夫曼樹文件hfmtree.dat中讀入信息並建立哈夫曼樹
createhuffmantreefromfile();
if (leafnum<=1) {
cout<<"內存無哈夫曼樹。操作撤銷。\n\n";
return;
}
}//if
char *sourcetext; //字元串數組,用於存放源文
//讓用戶選擇源文是從鍵盤輸入,還是從源文文件tobetran.txt中讀入
char choose;
cout<<"你要從文件中讀入源文(按1),還是從鍵盤輸入源文(按2)?";
cin>>choose;
if(choose=='1') {
ifstream fip1("tobetran.txt");
if(fip1.fail()) {
cout<<"源文文件打開失敗!無法繼續執行。\n";
return;
}
char ch;
int k=0;
while(fip1.get(ch)) k ; //第一次讀文件只統計文件中有多少個字元,將字元數存入k
fip1.close();
sourcetext=new char[k 1]; //申請存放源文的字元數組空間
ifstream fip2("tobetran.txt"); //第二次讀源文文件,把內容寫入sourcetext[]
k=0;
while(fip2.get(ch)) sourcetext[k ]=ch;
fip2.close();
sourcetext[k]='\0';
}
else { //從鍵盤輸入源文
string sourcebuff;
cin.ignore();
cout<<"請輸入需要編碼的源文(可輸入任意長,按回車鍵結束):\n";
getline(cin,sourcebuff,'\n');
int k=0;
while(sourcebuff[k]!='\0')
k ;
sourcetext=new char[k 1];
k=0;
while(sourcebuff[k]!='\0') {
sourcetext[k]=sourcebuff[k];
k ;
}
sourcetext[k]='\0';
}
cout<<"需編碼的源文為:";
cout< //開始解碼
ofstream fop("codefile.dat",ios::trunc); //打開碼文存放文件

int k=0;
while(sourcetext[k]!='\0') //源文串中從第一個字元開始逐個編碼
{
int i;
for(i=0;i if(node[i].sourcecode==sourcetext[k]) { //將對應編碼寫入碼文文件
fop< break;
};
}
if (i>=leafnum) {
cout<<"源文中存在不可編碼的字元。無法繼續執行。\n"< fop.close();
return;
}
k ; //源文串中的字元後移一個
}
fop.close();
cout<<"已完成編碼,碼文已寫入文件codefile.dat中。\n\n";
}
6. // 解碼函數
// 函數功能:對哈夫曼樹進行解碼
//函數參數:無
//參數返回值:無
void huffmantree::decoder()
{//如果內存沒有哈夫曼樹,則從哈夫曼樹文件hfmtree.dat中讀入信息並建立哈夫曼樹
if(node==null)
{
createhuffmantreefromfile();
if (leafnum<=1) {
cout<<"內存無哈夫曼樹。操作撤銷。\n\n";
return;
}
}

//將碼文從文件codefile.dat中讀入 codestr[]
ifstream fip1("codefile.dat");
if(fip1.fail()) {
cout<<"沒有碼文,無法解碼。\n";
return;
}

char* codestr;
int k=0;
char ch;
while(fip1.get(ch)){
k ;
}
fip1.close();
codestr=new char[k 1];
ifstream fip2("codefile.dat");
k=0;
while(fip2.get(ch))
codestr[k ]=ch;
fip2.close();
codestr[k]='\0';

cout<<"經解碼得到的源文為:";
ofstream fop("textfile.dat");

int j=leafnum*2-1-1; //j指向哈夫曼樹的根

int i=0; //碼文從第一個符號開始,順著哈夫曼樹由根下行,按碼文的當前符號決定下行到左孩子還是右孩子
while(codestr[i]!='\0') { //下行到哈夫曼樹的葉子結點處,則譯出葉子結點對應的源文字元
if(codestr[i]=='0')
j=node[j].lchild;
else
j=node[j].rchild;
if(node[j].rchild==-1) { //因為哈夫曼樹沒有度為1的結點,所以此條件等同於node[j]為葉結點
cout< fop< j=leafnum*2-1-1; //j再指向哈夫曼樹的根
}
i ;
}
fop.close();

cout<<"\n解碼成功且已存到文件textfile.dat中。\n\n";
}
7. // 輸出碼文函數
// 函數功能:從文件中輸出哈夫曼樹的碼文
//函數參數:無
//參數返回值:無
void huffmantree::printcodefile()
{
char ch;
int i=1;
ifstream fip("codefile.dat");
ofstream fop("codeprin.dat");
if(fip.fail())
{
cout<<"沒有碼文文件,無法顯示碼文文件內容。\n";
return;
}
while(fip.get(ch))
{cout< fop< if(i==50)
{
cout< fop< i=0;
}
i ;
}
cout< fop< fip.close();
fop.close();
}
8. // 輸出函數
// 函數功能:從內存或文件中直接輸出哈夫曼樹
//函數參數:無
//參數返回值:無
void huffmantree::printhuffmantree()
{
//如果內存沒有哈夫曼樹,則從哈夫曼樹文件hfmtree.dat中讀入信息並建立哈夫曼樹
if(node==null)
{
createhuffmantreefromfile();
if (leafnum<=1) {
cout<<"內存無哈夫曼樹。操作撤銷。\n\n";
return; }}
ofstream fop("treeprint.dat",ios_base::trunc);
fop.close();
printhuffmantree_aoru(2*leafnum-1-1);
return;
}

熱點內容
愛奇藝正義聯盟為啥不能緩存 發布:2024-01-20 00:52:13 瀏覽:248
caccess查詢資料庫 發布:2024-01-20 00:43:10 瀏覽:769
xp文件夾圖標更改 發布:2024-01-20 00:43:03 瀏覽:19
python和node 發布:2024-01-20 00:37:12 瀏覽:194
android拖拉 發布:2024-01-20 00:00:49 瀏覽:583
少兒編程課程體系介紹 發布:2024-01-20 00:00:48 瀏覽:846
我說你做下載ftp 發布:2024-01-20 00:00:47 瀏覽:8
安卓驅動培訓哪裡好 發布:2024-01-19 23:55:41 瀏覽:987
python轉為字元串 發布:2024-01-19 23:51:39 瀏覽:272
合同文件夾 發布:2024-01-19 23:50:52 瀏覽:740
网站地图