• 大小: 0.6M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-27
  • 语言: 其他
  • 标签: 其他  

资源简介

哈夫曼编码.zip

资源截图

代码片段和文件信息

#include
#include
#include
#include
using namespace std;
#include
#include
#define MaxSize 100
#define MaxQueue 100
struct linklist                                        //单链表结点类型
{
char data;
linklist *next;
};
struct pindu                                           //每个字母出现的频度结点类型
{
char data;         //字母           
int weight;        //权值
};
template 
struct HTNode                                          //哈夫曼结点类型
{
T data;//结点值
double weight;//权值
int parent;//双亲结点
int lchild;//左孩子结点
int rchild;//右孩子结点
};

struct HCode                                           //哈夫曼编码类型
{
char cd[MaxSize];              //存放当前结点的哈夫曼编码
int start;                    //用cd[start..no]存放哈夫曼编码包括start和no
};
class sqlist
{
private:
linklist *head;
int Count;                          //计算个数 
public:
pindu aa[100];                      //存放频度的信息
sqlist()
{
head = new linklist[100];
head->next = NULL;
Count = 0;
}
void destorysqlist()                     //删除单链表,不能用析构,因为随时不用,会对哈夫曼数的构造造成影响
{
linklist *pre *p;
pre = head;
p = pre->next;
while (p != NULL)
{
delete pre;
pre = p; p = p->next;
}
delete pre;
}
int getCount()                       //得到权值个数
{
linklist *s;
int j = 0;
int count[127] = { 0 };             //设置初始化
s = head->next;
int num;
while (s)
{
num = s->data;
if (num != 32)               //不计算空格出现的频度
{
count[num - 0]++;
}
s = s->next;
}
for (int i = 0; i < 127; i++)
{
if (count[i])
{
aa[j].data = i;
aa[j].weight = count[i];
j++;
}
}
Count = j;
return Count;
}
void creatlist()                     //创建单链表,把文件中的字母写进单链表中
{
linklist *s *r;
r = head;
FILE *fin;
char ch;
/*char filename[100] = { 0 };
cout << “请输入文件路径:“;
cin >> filename;*/
if ((fin = fopen(“H:\\test.txt“ “r“)) == NULL)
{
cout << “无法打开此文件“ << endl;
exit(0);
}
ch = fgetc(fin);
while (!feof(fin))
{
s = new linklist;
s->data = ch;
r->next = s;
r = s;
ch = fgetc(fin);  //从fin 文件中打开
}
r->next = NULL;
fclose(fin);
/* cout << endl;
cout << endl;*/
}
void disp()                                 //输出单链表内容,即文章内容
{
cout << endl << “文件内容打开如下“ << endl;
linklist *s;
s = head->next;
int count[257] = { 0 };
while (s)
{
cout << s->data;
s = s->next;
}
cout << endl;
}
void total()                           //利用一个数组,统计字母出现次数
{
linklist *s;
int j = 0;
int count[127] = { 0 };             //设置初始化
s = head->next;
int num;
while (s)
{
num = s->data;
if (num != 32)               //不计算空格出现的频度
{
count[num - 0]++;
}
s = s->next;
}
cout << “统计“ << endl;
cout << “|---------------------------------------------|“ << endl;
cout << “|   字母              |               频度    |“ << endl;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-12-23 09:00  哈夫曼编码\
     文件       12257  2016-12-23 08:49  哈夫曼编码\Haffuman.cpp
     文件     1934225  2016-12-23 08:49  哈夫曼编码\Haffuman.exe
     目录           0  2016-12-23 08:36  哈夫曼编码\三个要创建的txt实例\
     文件          21  2016-12-23 07:52  哈夫曼编码\三个要创建的txt实例\test.txt
     文件          12  2016-12-23 08:22  哈夫曼编码\三个要创建的txt实例\test1.txt
     文件          46  2016-12-23 08:34  哈夫曼编码\三个要创建的txt实例\tex.txt
     文件         706  2016-12-23 08:50  哈夫曼编码\先读.txt
     文件      189212  2015-12-31 13:20  哈夫曼编码\实验报告.docx

评论

共有 条评论