Huffman编/译码器(能够实现中英文)
数据结构实训的时候写的一些东西,希望对你们有帮助, 如果转载请标明出处
- 头文件为
#ifndef TREEHEAD_H_INCLUDED
#define TREEHEAD_H_INCLUDED #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define ERROR 0
#define OK 1
#define status int typedef struct HuffmanData
{
char *data;
int data2;
int weight;
struct HuffmanData* next;
}HFMData, *LHFMData; // Huffman辅助数组
typedef struct
{
char data[4];
int data2;
int weight;
int parent,lchild, rchild;
char *Code;
}HTNode, *HuffmanTree; void Choose();
// 输出*函数, n表示输出的个数,flag表示是否输入完之后回车0NO 1 YES
void print(int n, int flag);
status CreateHuffman();
void InitHfm(LHFMData d, int n); // 寻找中文
status SearchData(LHFMData d, char *data); // 进行huffman编码
status HfmEncoding(HuffmanTree &ht, int &flag);
// 寻找父节点为0 并且weight最小的两个元素;
status MinSearchData(HuffmanTree ht, int n, int &p1, int &p2); // 在进行编译的时候寻找相同的元素
int SearchEncoding(HuffmanTree hc, char *ch, int n); // 进行Huffman翻译
status Decoding(HuffmanTree ht, int flag); // 打印编码
status printCode(int flag); // 打印Huffman树
status TreePrint(HuffmanTree ht, int flag); #endif // TREEHEAD_H_INCLUDED
2. main文件
#include "TreeHead.h" int main()
{
Choose();
}
3.解释文件
#include "TreeHead.h" void Choose()
{
HuffmanTree ht;
int n, flag = 0;
do
{
printf("\t\t\t");print(32,1);
printf("\t\t\t*");
printf(" Huffman 缂?璇戠爜鍣?鏀寔涓枃) *\n");
printf("\t\t\t* 浣跨敤鍓嶈鍏堝皢闇€瑕佺紪鐮佺殑鏂囦欢 *\n\t\t\t* 鍐欏叆鍒癉atafile.txt鏂囦欢涓? *\n");
printf("\t\t\t");print(32,1);
printf("\t\t\t*\t ");
printf("1.寤虹珛HuffmanTree *\n");
printf("\t\t\t*\t ");
printf("2.杩涜Huffman缂栫爜 *\n");
printf("\t\t\t*\t ");
printf("3.杩涜Huffman璇戠爜 *\n");
printf("\t\t\t*\t ");
printf("4.鎵撳嵃浠g爜鏂囦欢 *\n");
printf("\t\t\t*\t ");
printf("5.鎵撳嵃HuffmanTree *\n");
printf("\t\t\t*\t ");
printf("6.閫€鍑? *\n");
printf("\t\t\t");print(32,1);
printf("\t\t\t\t璇疯緭鍏ヤ綘鐨勯€夋嫨锛?);
scanf("%d",&n);
switch(n)
{
case 1:CreateHuffman();break;
case 2:
{
HfmEncoding(ht, flag);
break;
}
case 3:Decoding(ht, flag);break;
case 4:printCode(flag);break;
case 5:TreePrint(ht, flag);break;
case 6: break;
default:
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf(" 杈撳叆鏈夎\t *\n");
printf("\t\t\t");print(30,1);
}
}
system("pause");
system("cls"); }while(n!=6);
}
status CreateHuffman()
{
char c, data[4];
int i, flag = 0;
LHFMData d1, d2, d3;
d1 = (LHFMData)malloc(sizeof(HFMData)*128);
InitHfm(d1, 128);
d2 = (LHFMData)malloc(sizeof(HFMData));
d2->next = NULL;
d2->weight = 0;
if(!d1 || !d2)
{
return ERROR;
}
FILE *fp;
if( (fp=fopen("Datafile.txt","r")) == NULL )
{
system("pause");
return ERROR;
}
while((c = fgetc(fp))!= EOF)
{
i = c;
if(i>0)
{
d1[i].data = (char*)malloc(sizeof(char));
*d1[i].data = c;
d1[i].data2 = i;
d1[i].weight++;
}
else if(i < 0)
{ data[0] = c;
c = fgetc(fp);
data[1] = c;
c = fgetc(fp);
data[2] = c;
data[3] = '\0';
if(!SearchData(d2, data))
{
d2->weight++;
d3 = (LHFMData)malloc(sizeof(HFMData));
d3->data = (char*)malloc(sizeof(char)*3);
strcpy(d3->data, data);
d3->weight = 1;
d3->next = d2->next;
d2->next = d3;
}
}
else if(c == '\n')
{
d1[95].data = (char*)malloc(sizeof(char));
*d1[95].data = c;
d1[95].weight++;
}
}
fclose(fp);
fp = fopen("hfmTree.txt", "w");
for(int i = 0; i < 128; i++)
{
if(d1[i].weight != 0)
{
fprintf(fp,"%d\t%d\n", d1[i].data2, d1[i].weight);
}
}
d3 = d2->next;
while(d3)
{
fprintf(fp, "%s\t%d\n", d3->data, d3->weight);
d3 = d3->next;
}
fclose(fp);
printf("\t\t\t");print(31,1);
printf("\t\t\t*\t");
printf(" 鍒涘缓瀹屾瘯\t *\n");
printf("\t\t\t*");
printf("缁撴灉宸茬粡瀛樺湪hfmTree.txt鏂囦欢涓?\n");
printf("\t\t\t");print(31,1); }
void InitHfm(LHFMData d, int n)
{
for(int i = 0; i < n; i++)
{
d[i].weight = 0;
d[i].data2 = -1;
d[i].next = NULL;
}
} // 杩涜huffman缂栫爜
status HfmEncoding(HuffmanTree &ht, int &flag)
{
int p1, p2;
char ch, data[4];
int n = 0, i = 0, m;
FILE *fp;
if((fp = fopen("hfmTree.txt", "r")) == NULL)
{
return ERROR;
}
while((ch = fgetc(fp))!= EOF)
{
if(ch == '\n')
{
n++;
}
}
//HTNode ht[2*n];
ht = (HuffmanTree)malloc(sizeof(HTNode)*(2*n));
m = n;
flag = m;
// 鍒濆鍖栦竴涓媓t
for(int i = 1; i < 2*n; i++)
{
ht[i].lchild = 0;
ht[i].parent = 0;
ht[i].rchild = 0;
ht[i].weight = 0;
} fseek(fp, 0, SEEK_SET);
i = 1;
while((ch = fgetc(fp))!= EOF)
{
if(ch < 0)
{
data[0] = ch;
ch = fgetc(fp);
data[1] = ch;
ch = fgetc(fp);
data[2] = ch;
data[3] = '\0';
strcpy(ht[i].data, data);
fscanf(fp, "%d", &n);
ht[i].weight = n;
fgetc(fp);
i++;
}
else
{
fseek(fp, -1, SEEK_CUR);
fscanf(fp, "%d %d", &ht[i].data2,&n);
ht[i].weight = n;
fgetc(fp);
i++;
}
}
fclose(fp); for(n = m+1; n < 2*m; n++)
{
MinSearchData(ht, m, p1, p2);
ht[n].lchild = p1;
ht[n].rchild = p2;
ht[n].weight = ht[p1].weight+ht[p2].weight;
ht[p1].parent = n;
ht[p2].parent = n;
}
char encode[m+1];
int p, k;
for(i = 1; i <= m; i++)
{
n = m;
k = i;
encode[n--] = '\0';
while(ht[k].parent != 0)
{
p = ht[k].parent;
if(ht[p].lchild == k)
{
encode[n--] = '1';
}
else
{
encode[n--] = '0';
}
k = p;
}
ht[i].Code = (char*)malloc(sizeof(char)*(m-n));
strcpy(ht[i].Code, &encode[n+1]);
}
fp = fopen("file.txt", "w");
fprintf(fp, "Data\tWeight\tParent\tLchild\tRchild\tHuffmanCode\n"); for(i = 1; i <= m; i++)
{
if(ht[i].data[0] >= 32)
{
fprintf(fp,"%c\t%d\t%d\t%d\t%d\t%s\n",ht[i].data2,ht[i].weight, ht[i].parent, ht[i].lchild,ht[i].rchild, ht[i].Code);
}
else if(ht[i].data[0] < 0)
{
fprintf(fp,"%s\t%d\t%d\t%d\t%d\t%s\n",ht[i].data,ht[i].weight, ht[i].parent, ht[i].lchild,ht[i].rchild, ht[i].Code);
}
else
{
fprintf(fp,"(绌哄瓧绗?\t%d\t%d\t%d\t%d\t%s\n",ht[i].weight, ht[i].parent, ht[i].lchild,ht[i].rchild, ht[i].Code); } }
fclose(fp); FILE *fpr;
if((fp = fopen("Datafile.txt", "r")) == NULL)
{
return ERROR;
}
fpr = fopen("CodeFile.txt", "w"); while((ch = fgetc(fp))!= EOF)
{
i = ch;
if(i >= 0)
{
data[0] = ch;
data[1] = '\0';
k = SearchEncoding(ht, data, m);
fprintf(fpr, "%s", ht[k].Code);
}
else if(i < 0)
{ data[0] = ch;
ch = fgetc(fp);
data[1] = ch;
ch = fgetc(fp);
data[2] = ch;
data[3] = '\0';
k = SearchEncoding(ht, data, m);
fprintf(fpr, "%s", ht[k].Code);
}
}
fclose(fp);
fclose(fpr);
printf("\t\t\t");print(32,1);
printf("\t\t\t*\t");
printf(" 缂栫爜瀹屾瘯\t *\n");
printf("\t\t\t*");
printf("缁撴灉宸茬粡瀛樺湪CodeFile.txt鏂囦欢涓?\n");
printf("\t\t\t");print(32,1); } // 鍦ㄨ繘琛岀紪璇戠殑鏃跺€欏鎵剧浉鍚岀殑鍏冪礌
int SearchEncoding(HuffmanTree hc, char *ch, int n)
{
int i;
for(i = 1; i <= n; i++)
{
if(ch[0] < 0)
{
if(!strcmp(hc[i].data, ch))
{
return i;
}
}
else
{
if(hc[i].data2 == ch[0])
{
return i;
}
} }
return 0;
} // 瀵绘壘鐖惰妭鐐逛负0 骞朵笖weight鏈€灏忕殑涓や釜鍏冪礌锛?
status MinSearchData(HuffmanTree ht, int n, int &p1, int &p2)
{
int i = 0;
p1 = 0, p2 = 0;
for( i = 0; i < 2*n; i++)
{
if(ht[i].parent == 0)
{
if(p1 == 0)
{
p1 = i;
}
else
{
p2 = i;
break;
} }
}
if(p2 == 0)
{
return OK;
}
if(ht[p1].weight > ht[p2].weight)
{
p2 = p1;
p1 = i;
}
for(i++;i < 2*n; i++)
{
if(ht[i].parent == 0)
{
if(ht[i].weight!=0)
{
if(ht[i].weight > ht[p1].weight)
{
if(ht[i].weight < ht[p2].weight)
{
p2 = i;
}
}
else
{
p2 = p1;
p1 = i;
}
}
}
}
} status SearchData(LHFMData d, char *data)
{
LHFMData p;
p = d->next;
while(p)
{
if(!strcmp(p->data, data))
{
p->weight++;
d->weight++;
return OK;
}
p = p->next;
}
return ERROR;
} // 鎵撳嵃缂栫爜
status printCode(int flag)
{
int n = 0;
char ch;
if(flag == 0)
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf("璇峰厛缂栬瘧锛?锛? *\n");
printf("\t\t\t");print(30,1);
return ERROR;
}
FILE *fp;
if((fp = fopen("CodeFile.txt", "r")) == NULL)
{
return ERROR;
}
printf("Huffman Code : ");
while((ch = fgetc(fp))!= EOF)
{
printf("%c", ch);
n++;
if(n == 50)
{
printf("\n\t\t");
n = 0;
}
}
printf("\n"); } // 杩涜Huffman缈昏瘧
status Decoding(HuffmanTree ht, int flag)
{
char ch;
int n ;
if(flag == 0)
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf("璇峰厛缂栬瘧锛?锛? *\n");
printf("\t\t\t");print(30,1);
return ERROR;
}
/*for(int n = 1; n < 2*flag; n++)
{
printf("%d %d %d\n", ht[n].parent, ht[n].lchild, ht[n].rchild);
}*/
FILE *fp,*fpr;
if((fp = fopen("CodeFile.txt", "r")) == NULL)
{
return ERROR;
}
fpr = fopen("TextFile.txt", "w");
while((ch = fgetc(fp))!= EOF)
{
n = flag*2-1;
do
{
if(ch == '1')
{
n = ht[n].lchild;
}
else
{
n = ht[n].rchild;
}
if(ht[n].lchild == 0 && ht[n].rchild == 0)
{
if(ht[n].data[0] < 0)
{
fprintf(fpr, "%s", ht[n].data);
}
else
{
fprintf(fpr, "%c", ht[n].data2);
}
break;
}
}while((ch = fgetc(fp))!= EOF);
}
fclose(fpr);
printf("\t\t\t");print(32,1);
printf("\t\t\t*\t");
printf(" 瑙g爜瀹屾瘯\t *\n");
printf("\t\t\t*");
printf("缁撴灉宸茬粡瀛樺湪TextFile.txt鏂囦欢涓?\n");
printf("\t\t\t");print(32,1);
} // 杈撳嚭*鍑芥暟锛?n琛ㄧず杈撳嚭鐨勪釜鏁帮紝flag琛ㄧず鏄惁杈撳叆瀹屼箣鍚庡洖杞?NO 1 YES
void print(int n, int flag)
{
for(int m = 1; m <= n; m++)
{
printf("*");
}
if(flag == 1)
{
printf("\n");
}
} status TreePrint(HuffmanTree ht, int flag)
{
system("cls");
if(flag == 0)
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf("璇峰厛缂栬瘧锛?锛? *\n");
printf("\t\t\t");print(30,1);
return ERROR;
} printf("\t\t");
print(65, 1);
printf("\t\t* \t\t\t鎵撳嵃HuffmanTree \t\t\t*\n");
printf("\t\t");
print(65, 1);
printf("\t\t* \t\t姹夊瓧鏄剧ず鏈夎锛岀敤锛堟眽瀛楋級鏉ヤ唬鏇挎眽瀛? \t\t*\n");
printf("\t\t");
print(65, 1);
printf("\t\t* 鏁版嵁\t 鏉冮噸\t 宸﹀瀛怽t 鍙冲瀛怽t Huffman Code *\n");
for(int i = 1; i < flag*2; i++)
{
if(i<=flag)
{
if(ht[i].data[0] < 0)
{ printf("\t\t*姹夊瓧\t %d\t %d\t\t %d\t\t ", ht[i].weight,ht[i].lchild,ht[i].rchild);
if(strlen(ht[i].Code) <= 3)
{
printf("%s\t\t*\n", ht[i].Code);
}
else
{
printf("%s\t*\n", ht[i].Code);
}
}
else
{ printf("\t\t* %c\t %d\t %d\t\t %d\t\t ", ht[i].data2, ht[i].weight,ht[i].lchild,ht[i].rchild);
if(strlen(ht[i].Code) <= 3)
{
printf("%s\t\t*\n", ht[i].Code);
}
else
{
printf("%s\t*\n", ht[i].Code);
}
} }
else
{
printf("\t\t*\t %d\t %d\t\t %d\t\t\t\t*\n", ht[i].weight,ht[i].lchild,ht[i].rchild);
} }
printf("\t\t");
print(65, 1);
system("pause");
}
Huffman编/译码器(能够实现中英文)的更多相关文章
- 数据结构实训——哈夫曼(Huffman)编/译码器
题目4.哈夫曼(Huffman)编/译码器(限1人完成) [问题描述] 利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本.但是,这要求在发送端通过一个编码系统对待传数据预先 ...
- JPEG标准推荐的亮度、色度DC、AC Huffman编码表
JPEG 标准推荐的亮度.色度DC.AC Huffman 编码表 博主在完成数字图像处理大作业时利用搜索引擎查找了很久完整的四张Huffman 编码表(亮度AC Huffman编码表.亮度DC Huf ...
- huffman编解码英文文本[Python]
对英文文本的字母进行huffman编码,heapq优先队列构建huffman树 python huffman.py source.txt result.txt import sys import he ...
- jpeg huffman coding table
亮度DC系数的取值范围及序号: 序号(size) 取值范围 0 0 1 - ...
- python实现Huffman编码
一.问题 利用二叉树的结构对Huffman树进行编码,实现最短编码 二.解决 # 构建节点类 class TreeNode: def __init__(self, data): "" ...
- Huffman编码和解码
一.Huffman树 定义: 给定n个权值作为n个叶子结点,构造一棵二叉树,若该树的带权路径达到最小,这样的二叉树称为最优二叉树,也称为霍夫曼树(Huffman树). 特点: Huffman树 ...
- TTL和CMOS
reprint from:http://blog.csdn.net/hemeinvyiqiluoben/article/details/9253249 TTL和COMS电平匹配以及电平转换的方法 一. ...
- TTL电平和CMOS电平总结
TTL电平和CMOS电平总结 1,TTL电平: 输出高电平>2.4V,输出低电平<0.4V.在室温下,一般输出高电平是3.5V,输出低电平是0.2V.最小输入高电平和低电 ...
- TTL和COMS电平匹配以及电平转换的方法
一.TTL TTL集成电路的主要型式为晶体管-晶体管逻辑门(transistor-transistor logic gate),TTL大部分都采用5V电源.1.输出高电平Uoh和输出低电平UolUoh ...
随机推荐
- 深入理解mysql锁与事务隔离级别
一.锁 1.锁的定义 锁即是一种用来协调多线程或进程并发使用同一共享资源的机制 2.锁的分类 从性能上分类:乐观锁和悲观锁 从数据库操作类型上分类:读锁和写锁 从操作粒度上分类:表锁和行锁 2 ...
- hadoop-uber作业模式
如果作业很小,就选择和自己在同一个JVM上运行任务,与在一个节点上顺序运行这些任务相比,当application master 判断在新的容器中的分配和运行任务的开销大于并行运行它们的开销时,就会发生 ...
- SpringBoot让测试类飞起来的方法
单元测试是项目开发中必不可少的一环,在 SpringBoot 的项目中,我们用 @SpringBootTest 注解来标注一个测试类,在测试类中注入这个接口的实现类之后对每个方法进行单独测试. 比如下 ...
- Output of C++ Program | Set 3
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...
- oracle 存储过程及REF CURSOR的使用
基本使用方法及示例 1.基本结构: CREATE OR REPLACE PROCEDURE 存储过程名字 (参数1 IN NUMBER,参数2 IN NUMBER) AS 变量1 INTEGER := ...
- 使用IntelliJ IDEA创建简单的Spring Boot项目
方法一: File - New -Project 创建结束后进行测试运行,修改代码如下: package com.springboot.testone; import org.springframew ...
- 分布式全局ID生成器原理剖析及非常齐全开源方案应用示例
为何需要分布式ID生成器 **本人博客网站 **IT小神 www.itxiaoshen.com **拿我们系统常用Mysql数据库来说,在之前的单体架构基本是单库结构,每个业务表的ID一般从1增,通过 ...
- 5、Redis五大基本数据类型——String类型
一.Redis支持数据类型简介 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 二.String类 ...
- Mysql解决主从慢同步问题
目录 一.简介 为何有延迟 二.观察 三.解决办法 参数 多线程 组提交 一.简介 一般主从复制,有三个线程参与,都是单线程:Binlog Dump(主) ----->IO Thread (从) ...
- Python连接MySQL数据库获取数据绘制柱状图
一.Python通过pymysql包获取MySQL数据库中的数据(没有对应包的可以通过pip install pymysql 安装对应的包) import matplotlib.pyplot as p ...