當前位置:首頁 » 編程軟體 » 哈夫曼c語言編譯

哈夫曼c語言編譯

發布時間: 2024-04-25 05:43:20

『壹』 求哈夫曼編譯c語言代碼

// huffman.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#include <stdio.h>
#include <string.h>
const long wordnum = 1000;//最大不同單詞數
const long code_len = wordnum/10;
const long wordlen = 30;//單詞最長長度
const long codemax = 10000;//最大haffuman編碼長度
const long wordmax = 10000;//最大單詞數
//str類定義
class str
{
public:
str();
bool operator<(str obj);//運算符重載方便對str的排序使用二分搜索模板
bool operator>(str obj);
str operator=(char p[]);
str operator++(int);//重載後綴方式的++
char *getword();
long getfre();
protected:
private:
char word[wordlen];
long frequence;
};
str::str()
{
frequence = 0;
}
//以下為重載str類中的運算符
bool str::operator<(str obj)
{
if(strcmp(word,obj.word) < 0)
return true;
else
return false;
}
bool str::operator>(str obj)
{
if(strcmp(word,obj.word) > 0)
return true;
else
return false;
}
str str::operator=(char p[])
{
strcpy(word,p);
return *this;
}
str str::operator++(int x)
{
frequence ++;
return *this;
}
char * str::getword()
{
return word;
}
long str::getfre()
{
return frequence;
}
//str類定義結束

//Node類定義
class Node
{
public:
Node();
bool operator<(Node obj);//運算符重載方便對Node的排序使用堆模板
bool operator<=(Node obj);
bool operator>(Node obj);
Node operator=(str obj);
Node operator+(Node &obj);
Node *leftp();//類外獲取指向當前節點的孩子的指針
Node *rightp();
char *getword(Node *p);//類外獲取節點信息
long getfrequence();
protected:
private:
char word[wordlen];
long frequence;
Node *left, *right;
};

Node::Node()
{
strcpy(word,"NULL");
left = NULL;
right = NULL;
}
//以下為重載Node類中的運算符
bool Node::operator<(Node obj)
{
if(frequence < obj.frequence)
return true;
else
return false;
}
bool Node::operator<=(Node obj)
{
if(frequence <= obj.frequence)
return true;
else
return false;
}
bool Node::operator>(Node obj)
{
if(frequence > obj.frequence)
return true;
else
return false;
}
Node Node::operator+(Node &obj)
{
Node sum;
sum.frequence = frequence + obj.frequence;//指針指向了NULL
sum.left = this;
sum.right = &obj;
return sum;
}
Node Node::operator=(str obj)
{
strcpy(word,obj.getword());
frequence = obj.getfre();
return *this;
}
Node * Node::leftp()
{
return left;
}
Node * Node::rightp()
{
return right;
}
char * Node::getword(Node *p)
{
return p->word;
}
long Node::getfrequence()
{
return frequence;
}
//Node類定義結束

//str類專門用於統計詞頻使用,Node則用於構造huffman樹,由於兩者使用的key不同,前者是word的字典序
//後者是詞頻,於是使用了兩個類來實現。

class huffman
{
public:
huffman();
template<typename entry>
friend bool binarysearch(entry list[wordnum],entry target,long bottom,long top,long &position);
template<typename entry>
friend void buidheap(entry a[wordnum], long number);
template<typename entry>
friend void heapify(entry a[wordnum], long high, long low);
template<typename entry>
friend void swap(entry a[wordnum], long i, long j);
bool Stat();
void Encode();
bool Decode(char code[]);
Node update(long end);
void proce_code();
void Inorder(Node *current, char currentcode[], long &num);
protected:
private:
Node SortedNode[wordnum];//從中產生huffman樹
char NodeCode[wordnum][wordnum/10];//相應編碼
Node root;
bool sign;//用於標記是否已經建立了huffman樹
long n;//葉子節點個數
};
huffman::huffman()
{
sign = false;
}

//二分用於統計詞頻
template<typename entry>
bool binarysearch(entry list[wordnum], entry target, long bottom, long top, long &position)
{
while(bottom <= top)
{
position = (bottom + top)/2;
if(list[position] < target)
bottom = position + 1;
else if(list[position] > target)
top = position - 1;
else
return true;
}
return false;
}

//建立最小堆及調整為最小堆
template<typename entry>
void swap(entry a[wordnum], long i, long j)
{
entry s;
s = a[i];
a[i] = a[j];
a[j] = s;
}

template<typename entry>
void buidheap(entry a[wordnum], long number)
{
long i ,j;
for(i = number/2; i >= 1; i--)
{
j = i;
while(j <= number/2)
{
if(a[j] > a[2*j] || (a[j] > a[2*j + 1] && 2*j + 1 <= number))
{
if(a[2*j] > a[2*j + 1] && 2*j + 1 <= number)
{
swap(a, j, 2*j+1);
j = 2*j + 1;
}
else
{
swap(a, j ,2*j);
j = 2*j;
}
}
else
break;
}
}
}

template<typename entry>
void heapify(entry a[wordnum], long high, long low)
{
long j = low;
while(j <= high/2)
{
if(a[j] > a[2*j] && a[j] > a[2*j + 1])
{
if(a[2*j] > a[2*j + 1] && 2*j + 1 <= high)
{
swap(a, j, 2*j+1);
j = 2*j + 1;
}
else if(2*j <= high)
{
swap(a, j, 2*j);
j = 2*j;
}
}
else if(a[j] <= a[2*j] && a[j] > a[2*j + 1] && 2*j + 1 <= high)
{
swap(a, j, 2*j+1);
j = 2*j + 1;
}
else if(a[j] <= a[2*j + 1] && a[j] > a[2*j] && 2*j <= high)
{
swap(a, j, 2*j);
j = 2*j;
}
else
break;
}
}
//詞頻統計函數Stat()
bool huffman::Stat()
{
long i,position;
char p[wordmax],*get;
str s[wordnum],current;
n = 0;
while(gets(p) != NULL && strcmp(p,"@") != 0)
{
get = strtok(p," ,.!/-:;?");
while(get != NULL)
{
current = get;
if(binarysearch(s,current,0,n,position) == true && n < wordnum - 1)
s[position] ++;
else
{
n ++;
if(n < wordnum - 1)
{
if(s[position] < current && current.getfre() < s[position].getfre())
position ++;
for(i = n; i >= position; i --)
s[i+1] = s[i];
s[position] = current;
s[position] ++;
}
}
get = strtok(NULL," ,.!/-:;?");
}
}
for(i = 1; i <= n && i < wordnum; i ++)
SortedNode[i] = s[i-1];
if(n < wordnum)
return true;
else
{
n = wordnum - 1;
return false;
}
}

//建立huffman樹函數
void huffman::Encode()
{
int i;
sign = true;
buidheap(SortedNode,n);
for(i = 1; i < n; i ++)
root = update(n-i+1);
}

Node huffman::update(long end)
{
Node *p,*q;
Node newNode;
p = new Node;
q = new Node;
*p = SortedNode[1];
swap(SortedNode,1,end);
heapify(SortedNode,end-1,1);//取出了一個最小元,然後將堆進行了調整
*q = SortedNode[1];
newNode = *p + *q;
SortedNode[1] = newNode;
heapify(SortedNode,end-1,1);//又取出最小元,並且把新節點賦為SortedNode[1],調整了堆
return SortedNode[1];
}

//解碼函數
bool huffman::Decode(char code[codemax])
{
int i;
Node *find = &root;
Node *l = NULL,*r = NULL;
bool flag = true;
if(sign == true)
{
for(i = 0; code[i] != '\0' && flag == true; i ++)
{
l = find->leftp();
r = find->rightp();
if(code[i] == '0' && l != NULL)
find = l;
else if(code[i] == '1' && r != NULL)
find = r;
else flag = false;
if(find->leftp() == NULL && find->rightp() == NULL)
{
printf("%s ",find->getword(find));
find = &root;
}
}
if(!((find->leftp() == NULL && find->rightp() == NULL) || find == &root))
{
cout << "There are some wrong codes in th input!" << endl;
flag = false;
}
}
else
flag = false;
return flag;
}
void huffman::Inorder(Node *current, char currentcode[], long &num)
{
Node *l, *r;
char cl[code_len], cr[code_len];
if(current != NULL)
{
l = current->leftp();
r = current->rightp();
strcpy(cl,currentcode);
strcat(cl,"0");
strcpy(cr,currentcode);
strcat(cr,"1");
Inorder(l, cl, num);
if(l == NULL && r == NULL)
{
SortedNode[num] = *current;
strcpy(NodeCode[num],currentcode);
num ++;
}
Inorder(r, cr, num);
}
}
void huffman::proce_code()//利用中序遍歷來得到相應的編碼
{
char current[code_len] = "";
long num = 1;
Inorder(&root, current,num);
for(long i = 1; i <= n; i ++)
{
cout << SortedNode[i].getword(&SortedNode[i]) << "----" << NodeCode[i]<<" " ;
if(i%3 == 0) cout << endl;
}
}

int main()
{
huffman test;
char code[codemax];
char order;
cout << "顯示編碼(Show)" << " "<< "解碼(Decode)" << " " <<"退出(Quit)" << endl;
cout << "Input the passage, to end with a single @ in a single line:" << endl;
test.Stat();
test.Encode();
cout << "Encoded!" << endl;
while(cin >> order)
{
if(order == 's' || order == 'S')
{
test.proce_code();
cout << "Proced!" << endl;
}
else if(order == 'd' || order == 'D')
{
cout << "Input the codes:" << endl;
cin >> code;
while(test.Decode(code))
cin >> code;
}
else if(order == 'q' || order == 'Q')
break;
}
return 0;
}

『貳』 怎麼樣用c語言程序編碼哈夫曼樹

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>
#include<limits.h>
int function1(char ch,char *s)
{
int i;
for(i=0; s[i]!='\0'; i++)
{
if(ch==s[i])return 0;
}
return 1;
}
typedef struct
{
unsigned int weight;
unsigned int parent,lchild,rchild;
} HTNode,*HuffmanTree; // 動態分配數組存儲赫夫曼樹
typedef char **HuffmanCode; // 動態分配數組存儲赫夫曼編碼表
// algo6-1.cpp 求赫夫曼編碼。實現演算法6.12的程序

int min(HuffmanTree t,int i)
{
// 函數void select()調用
int j,flag;
unsigned int k=UINT_MAX; // 取k為不小於可能的值
for(j=1; j<=i; j++)
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
t[flag].parent=1;
return flag;
}

void select(HuffmanTree t,int i,int &s1,int &s2)
{
// s1為最小的兩個值中序號小的那個

s1=min(t,i);
s2=min(t,i);
/* if(s1>s2)
{
j=s1;
s1=s2;
s2=j;
}*/
}

void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n) // 演算法6.12
{
// w存放n個字元的權值(均>0),構造赫夫曼樹HT,並求出n個字元的赫夫曼編碼HC
int m,i,s1,s2,start;
unsigned c,f;
HuffmanTree p;
char *cd;
if(n<=1)
return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); // 0號單元未用
for(p=HT+1,i=1; i<=n; ++i,++p,++w)
{
(*p).weight=*w;
(*p).parent=0;
(*p).lchild=0;
(*p).rchild=0;
}
for(; i<=m; ++i,++p)
(*p).parent=0;
for(i=n+1; i<=m; ++i) // 建赫夫曼樹
{
// 在HT[1~i-1]中選擇parent為0且weight最小的兩個結點,其序號分別為s1和s2
select(HT,i-1,s1,s2);
HT[s1].parent=HT[s2].parent=i;
HT[i].rchild=s1;
HT[i].lchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
// printf("HT[%d].lchild:%d HT[%d].rchild:%d\n",i,s2,i,s1);
}
// 從葉子到根逆向求每個字元的赫夫曼編碼
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
// 分配n個字元編碼的頭指針向量([0]不用)
cd=(char*)malloc(n*sizeof(char)); // 分配求編碼的工作空間
cd[n-1]='\0'; // 編碼結束符
for(i=1; i<=n; i++)
{
// 逐個字元求赫夫曼編碼
start=n-1; // 編碼結束符位置
for(c=i,f=HT[i].parent; f!=0; c=f,f=HT[f].parent)
// 從葉子到根逆向求編碼
if(HT[f].lchild==c)
cd[--start]='1';
else
cd[--start]='0';
HC[i]=(char*)malloc((n-start)*sizeof(char));
// 為第i個字元編碼分配空間
strcpy(HC[i],&cd[start]); // 從cd復制編碼(串)到HC
}
free(cd); // 釋放工作空間
}
void swap1(int *a ,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void swap2(char *a,char *b)
{
char ch;
ch=*a;
*a=*b;
*b=ch;
}
int main(void)
{
HuffmanTree HT;
HuffmanCode HC;
char *s1,*s2;
int i,j=0,n,count,*m,t,flag=1;
scanf("%d",&n);
getchar();
s1=(char*)malloc((n+n)*sizeof(char));
s2=(char*)malloc(n*sizeof(char));
memset(s2,'\0',n*sizeof(char));
gets(s1);
count=strlen(s1);
for(i=0; i<count; i++)
{
if(!isspace(*(s1+i)))
{
if(function1(*(s1+i),s2))
{
*(s2+j)=*(s1+i);
j++;
}
}
else;
}
m=(int*)malloc(j*sizeof(int));
for(i=0; i<j; i++)
*(m+i)=0;
for(t=0; t<j; t++)
{
for(i=0; i<count; i++)
{
if(*(s2+t)==*(s1+i))
*(m+t)+=1;
}
}
for(i=0;i<j;i++)
while(flag)
{
flag = 0;
for (t=0; t<j-1; t++)
{
if(*(m+t)<*(m+t+1))
{
swap1(m+t,m+t+1);
swap2(s2+t,s2+t+1);
flag=1;
}
}
}
HuffmanCoding(HT,HC,m,j);
for(i=1,t=0; i<=j; i++,t++)
{
printf("%c %d %s\n",*(s2+t),*(m+t),HC[i]);

}
return 0;
}

『叄』 C語言題:哈夫曼編碼(coding)求代碼,謝謝~急~滿意加分

#include<stdio.h>
#include<stdlib.h>

typedefstructnode
{
charc;
intcount;
}nd;

intcmp(constvoid*p1,constvoid*p2)
{
nd*c=(nd*)p1;
nd*d=(nd*)p2;
if(c->count!=d->count)returnd->count-c->count;
elsereturnc->c-d->c;
}
intmain()
{
nda[26];
inti;
for(i=0;i<26;i++)
{
a[i].c='a'+i;
a[i].count=0;
}

chars[128];
while(scanf("%s",s)!=EOF)
{
for(i=0;s[i]!=0;i++)
{
a[s[i]-'a'].count++;
}
qsort(a,26,sizeof(a[0]),cmp);
for(i=0;a[i].count;i++)
{
printf("%c%d ",a[i].c,a[i].count);
}
}
return0;
}

『肆』 哈夫曼編/解碼器問題:C語言版的數據結構,我急啊!那位朋友幫幫忙,結果必需是我的問題的結果,不能有錯啊

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//////////////////////////////////////////////////////////////////////////////
/*定義赫夫曼樹結點的結構體變數,存放結點的權值、字元、雙親、坐孩子和右孩子*/
typedef struct{
int weight;
char ch; //增加一個域用於存放該節點的字元
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode; //指向赫夫曼編碼的指針

//////////////////////////////////////////////////////////////////////////////
/*本程序用到的函數原型*/
void welcome(); //列印操作選擇界面
void HuffmanCoding(HuffmanTree &,char *,int *,int);//建立赫夫曼樹的演算法
void select(HuffmanTree HT,int j,int *s1,int *s2); //從目前已建好的赫夫曼樹中選擇parent為0且weight最小的兩個結點
void Init(); //輸入n個字元及其對應的權值,根據權值建立哈夫曼樹
void Coding(); //編碼
void Decoding(); //解碼
void Print_code(); //列印解碼好的代碼文件
void Print_tree(); //以凹凸表形式列印哈夫曼樹
int Read_tree(HuffmanTree &); //從文件中讀入赫夫曼樹
void find(HuffmanTree &HT,char *code,char *text,int i,int m);//解碼時根據01字元串尋找相應葉子節點的遞歸演算法
void Convert_tree(unsigned char T[100][100],int s,int *i,int j);//將內存中的赫夫曼樹轉換成凹凸表形式的赫夫曼樹

HuffmanTree HT; //全局變數,指向存放赫夫曼樹的存儲空間
int n=0; //全局變數,存放赫夫曼樹葉子結點的數目

int main()
{
char select;

while(1)
{
welcome();
scanf("%c",&select);
switch(select)
{
case 'i':
case 'I':Init();break;
case 'c':
case 'C':Coding();break;
case 'd':
case 'D':Decoding();break;
case 'p':
case 'P':Print_code();break;
case 't':
case 'T':Print_tree();break;
case 'e':
case 'E':exit(1);
default :printf("Input error!\n");
}
getchar();
}
return 0;
}

void welcome() //列印操作選擇界面
{
printf("*-----------------------------------------------------*\n");
printf("| What do you want to do? |\n");
printf("|-----------------------------------------------------|\n");
printf("| |\n");
printf("| I--------------------------Init the Huffman tree. |\n");
printf("| C--------------------------Code your file. |\n");
printf("| D--------------------------Decode the code. |\n");
printf("| P--------------------------Print the codefile. |\n");
printf("| T--------------------------Print the Huffman tree. |\n");
printf("| |\n");
printf("*-----------------------------------------------------*\n");
}

//////////////////////////////////////////////////////////////////////////////////////
/*初始化函數,輸入n個字元及其對應的權值,根據權值建立哈夫曼樹,並將其存於文件hfmtree中*/
void Init()
{
FILE *fp;
int i,n,w[52]; //w數組存放n個字元的權值
char character[52]; //存放n個字元
printf("\n輸入字元個數 n:");
scanf("%d",&n); //輸入字元集大小
printf("輸入%d個字元及其對應的權值:\n",n);
for (i=0;i<n;i++)
{
char b=getchar();
scanf("%c",&character[i]);
scanf("%d",&w[i]); //輸入n個字元和對應的權值
}
HuffmanCoding(HT,character,w,n); //建立赫夫曼樹

if((fp=fopen("hfmtree.txt","w"))==NULL)
printf("Open file hfmtree.txt error!\n");
for (i=1;i<=2*n-1;i++)
{
if(fwrite(&HT[i],sizeof(HTNode),1,fp)!=1) //將建立的赫夫曼樹存入文件hfmtree.txt中
printf("File write error!\n");
}
printf("\n建立赫夫曼樹成功,已將其存於文件hfmtree.txt中\n");
fclose(fp);

}

///////////////////////////////////////////////////////////////////////////////////
//////建立赫夫曼樹的演算法///////////////////////////////////////////////////////////
void HuffmanCoding(HuffmanTree &HT,char *character,int *w,int n)
{
int m,i,s1,s2;
HuffmanTree p;
if(n<=1) return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;++i,++p,++character,++w)
{p->ch=*character;p->weight=*w;p->parent=0;p->lchild=0;p->rchild=0;}
for(;i<=m;++i,++p) {p->ch=0;p->weight=0;p->parent=0;p->lchild=0;p->rchild=0;}
for(i=n+1;i<=m;++i)
{
select(HT,i-1,&s1,&s2);
HT[s1].parent=i;HT[s2].parent=i;
HT[i].lchild=s1;HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}

}

///////////////////////////////////////////////////////////////////////////////
/*從HT[1]到HT[j]中選擇parent為0且weight最小的兩個結點,用s1和s2返回其序號*/
void select(HuffmanTree HT,int j,int *s1,int *s2)
{
int i;
//找weight最小的結點
for (i=1;i<=j;i++)
if (HT[i].parent==0)
{*s1=i;break;}
for (;i<=j;i++)
if ((HT[i].parent==0)&&(HT[i].weight<HT[*s1].weight))
*s1=i;
HT[*s1].parent=1;
//找weight次小的結點
for (i=1;i<=j;i++)
if (HT[i].parent==0)
{*s2=i;break;}
for (;i<=j;i++)
if ((HT[i].parent==0)&&(i!=*s1)&&(HT[i].weight<HT[*s2].weight))
*s2=i;

}

///////////////////////////////////////////////////////////////////////////////
/*對文件tobetrans中的正文進行編碼,然後將結果存入文件codefile中*/
void Coding()
{
FILE *fp,*fw;
int i,f,c,start;
char *cd;
HuffmanCode HC;
if(n==0)
n=Read_tree(HT);//從文件hfmtree.txt中讀入赫夫曼樹,返回葉子結點數

/////以下程序段求赫夫曼樹中各葉子節點的字元對應的的編碼,並存於HC指向的空間中
{
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char *)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;++i)
{
start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c)
cd[--start]='0';
else cd[--start]='1';
HC[i]=(char *)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}
/////////////////////////////////////////////////////////////////////////////////////

if((fp=fopen("tobetrans.txt","rb"))==NULL)
printf("Open file tobetrans.txt error!\n");
if((fw=fopen("codefile.txt","wb+"))==NULL)
printf("Open file codefile.txt error!\n");
char temp;
fscanf(fp,"%c",&temp); //從文件讀入第一個字元
while(!feof(fp))
{
for(i=1;i<=n;i++)
if(HT[i].ch==temp) break; //在赫夫曼樹中查找字元所在的位置
for(int r=0;HC[i][r]!='\0';r++) //將字元對應的編碼存入文件
fputc(HC[i][r],fw);
fscanf(fp,"%c",&temp); //從文件讀入下一個字元

}
fclose(fw);
fclose(fp);
printf("\n對文件hfmtree.txt編碼成功,結果已存入codefile.txt中。\n\n");
}

/////////////////////////////////////////////////////////////////////////
/*將文件codefile中的代碼進行解碼,結果存入文件textfile中*/
void Decoding()
{
FILE *fp,*fw;
int m,i;
char *code,*text,*p;

if(n==0)
n=Read_tree(HT);//從文件hfmtree.txt中讀入赫夫曼樹,返回葉子結點數

if((fp=fopen("codefile.txt","rb"))==NULL)
printf("Open file codefile.txt error!\n");
if((fw=fopen("textfile.txt","wb+"))==NULL)
printf("Open file textfile.txt error!\n");
code=(char *)malloc(sizeof(char));
fscanf(fp,"%c",code); //從文件讀入一個字元
for(i=1;!feof(fp);i++)
{
code=(char *)realloc(code,(i+1)*sizeof(char)); //增加空間
fscanf(fp,"%c",&code[i]); //從文件讀入下一個字元
}
code[i-1]='\0';
/////////到此codefile.txt文件中的字元已全部讀入,存放在code數組中
text=(char *)malloc(100*sizeof(char));
p=text;
m=2*n-1;
if(*code=='0')
find(HT,code,text,HT[m].lchild,m); //從根節點的左子樹去找
else
find(HT,code,text,HT[m].rchild,m); //從根節點的右子樹去找

for(i=0;p[i]!='\0';i++) //把解碼好的字元存入文件textfile.txt中
fputc(p[i],fw);

fclose(fp);
fclose(fw);

printf("\n對codefile.txt文件解碼成功,結果已存入textfile.txt文件。\n\n");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
/*將文件codefi1e以緊湊格式顯示在終端上,每行50個代碼。同時將此字元形式的編碼文件寫入文件codeprint中。*/
void Print_code()
{
FILE *fp,*fw;
char temp;
int i;

if((fp=fopen("codefile.txt","rb"))==NULL)
printf("Open file codefile.txt error!\n");
if((fw=fopen("codeprint.txt","wb+"))==NULL)
printf("Open file codeprint.txt error!\n");

printf("\n文件codefi1e以緊湊格式顯示如下:\n");
fscanf(fp,"%c",&temp); //從文件讀入一個字元
for (i=1;!feof(fp);i++)
{
printf("%c",temp);
if(i%50==0) printf("\n");
fputc(temp,fw); //將該字元存入文件codeprint.txt中
fscanf(fp,"%c",&temp); //從文件讀入一個字元
}
printf("\n\n此字元形式的編碼已寫入文件codeprint.txt中.\n\n");

fclose(fp);
fclose(fw);

}

//////////////////////////////////////////////////////////////////////////////////////////////////
/*將已在內存中的哈夫曼樹以凹凸表形式顯示在屏幕上,同時將此字元形式的哈夫曼樹寫入文件treeprint中。*/
void Print_tree()
{
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
if(n==0)
n=Read_tree(HT);//從文件hfmtree.txt中讀入赫夫曼樹,返回葉子結點數

Convert_tree(T,0,&m,2*n-1); //將內存中的赫夫曼樹轉換成凹凸表形式的樹,存於數組T中

if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹凸表形式列印已建好的赫夫曼樹:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字元形式的哈夫曼樹已寫入文件treeprint.txt中.\n\n");

}

//////////////////////////////////////////////////////////////////////////////////
/*從文件hfmtree.txt中讀入赫夫曼樹,返回葉子節點數*/
int Read_tree(HuffmanTree &HT)
{
FILE *fp;
int i,n;
HT=(HuffmanTree)malloc(sizeof(HTNode));
if((fp=fopen("hfmtree.txt","r"))==NULL)
printf("Open file hfmtree.txt error!\n");
for (i=1;!feof(fp);i++)
{
HT=(HuffmanTree)realloc(HT,(i+1)*sizeof(HTNode)); //增加空間
fread(&HT[i],sizeof(HTNode),1,fp); //讀入一個節點信息
}
fclose(fp);
n=(i-1)/2;
return n;
}

////////////////////////////////////////////////////////////////
/*解碼時根據01字元串尋找相應葉子節點的遞歸演算法*/
void find(HuffmanTree &HT,char *code,char *text,int i,int m)
{

if(*code!='\0') //若解碼未結束
{
code++;
if(HT[i].lchild==0&&HT[i].rchild==0) //若找到葉子節點
{

*text=HT[i].ch; //將葉子節點的字元存入text中

text++;
if((*code=='0'))
find(HT,code,text,HT[m].lchild,m); //繼續從根節點的左子樹找
else
find(HT,code,text,HT[m].rchild,m); //繼續從根節點的右子樹找
}
else //如果不是葉子節點
if(*code=='0')
find(HT,code,text,HT[i].lchild,m); //從該節點的左子樹去找
else
find(HT,code,text,HT[i].rchild,m); //從該節點的右子樹去找

}
else
*text='\0'; //解碼結束

}

////////////////////////////////////////////////////////////////////////
/*將內存中的赫夫曼樹轉換成凹凸表形式的赫夫曼樹*/
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild)
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}

為了您的安全,請只打開來源可靠的網址
打開網站 取消
來自: http://hi..com/laij/blog/item/4f3edefb6008321e6c22eb34.html

熱點內容
如何區分安卓原裝充電器 發布:2024-05-05 01:41:23 瀏覽:71
怎麼從蘋果轉移到安卓 發布:2024-05-05 01:41:20 瀏覽:720
支付寶付款碼怎麼設置密碼 發布:2024-05-05 01:27:36 瀏覽:877
qtp錄制的腳本 發布:2024-05-05 01:14:04 瀏覽:367
如何安裝卡羅拉安卓系統 發布:2024-05-05 01:09:00 瀏覽:984
sql創建表查詢表 發布:2024-05-05 01:00:12 瀏覽:799
食色抖音上傳 發布:2024-05-05 00:55:56 瀏覽:657
java圖片下載 發布:2024-05-05 00:50:45 瀏覽:598
唱吧如何上傳伴奏 發布:2024-05-05 00:49:04 瀏覽:444
什麼配置單反拍視頻最好 發布:2024-05-05 00:30:56 瀏覽:479