二叉链表实现哈夫曼编码系统

题目说明

试编写一个Huffman编码系统,用于数据加密和解密。该系统应具有以下功能:

  1. 初始化:从键盘中可读取通信所使用的字符集和每个字符的权值。例如下表。
  2. 加密 :利用初始化中的数据建立Huffman树,对任意的可加密的字符串进行加密。
  3. 解密:对任何的加密结果进行解密。
字符 空格 A B C D E F G H
频数 186 64 13 22 32 103 21 15 47
字符 I J K L M N O P Q
频数 57 1 5 32 20 57 63 15 1
字符 R S T U V W X Y Z
频数 48 51 80 23 8 18 1 16 1

扩展功能

  1. 支持从Excel表格导入字符集和权重值,格式见压缩包中的weight.csv

测试数据

链接:点击这里下载

代码实现

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAXSIZE 100 //定义字符串最大长度
#define INIT_STRING (String)malloc(sizeof(char)*MAXSIZE)

typedef int Status;
typedef char* String;

typedef struct node Node;
struct node{
	char data;
	int weight;
	Node* Lchild;
	Node* Rchild;
	Node* Father;
};

//字符串替换 
String strrpl(String str){
	int i=0;
	char ch = ',',new_ch = ' ';
	for(i=0;i<strlen(str);i++){ 
		if(str[i]==ch){
			str[i]=new_ch;
		}
	}
	return str;
} 

//读取文件存入二叉链表中 
Node** readFile(){
	FILE* fp;
	fp = fopen("weight.csv","r");
	if(fp==NULL){
		printf("文件读取失败!");
		exit(0); 
	}
	int i = 0,count=0;
	Node** arr = (Node**)malloc(sizeof(Node*)*50); 
	String temp1 = INIT_STRING;
	int temp2 = 0;
	while(!feof(fp)){
		String temp = INIT_STRING;
		String str =INIT_STRING;
		fscanf(fp,"%s",temp);
		str = strrpl(temp);
		count = sscanf(str,"%s %d\n",temp1,&temp2);
		if(count<2){
			break;
		}
		arr[i] = (Node*)malloc(sizeof(Node));
		//printf("%s,%d\n",temp1,temp2);
		if(strcmp(temp1,"blank")==0){
			arr[i]->data = ' ';
		}else{
			arr[i]->data = temp1[0];
		}
		arr[i]->weight = temp2;
		arr[i]->Lchild = NULL;
		arr[i]->Rchild = NULL;
		i++;
	}
	arr[i] = NULL;
	return arr;
}

//*/
Node** readFile2(){
	FILE* fp;
	fp = fopen("data.csv","r");
	if(fp==NULL){
		printf("文件读取失败!");
		exit(0); 
	}
	int i = 0,count=0;
	Node** arr = (Node**)malloc(sizeof(Node*)*50); 
	String temp1 = INIT_STRING;
	int temp2 = 0;
	while(!feof(fp)){
		String temp = INIT_STRING;
		String str =INIT_STRING;
		fscanf(fp,"%s",temp);
		str = strrpl(temp);
		count = sscanf(str,"%s %d\n",temp1,&temp2);
		if(count<2){
			break;
		}
		arr[i] = (Node*)malloc(sizeof(Node));
		//printf("%s,%d\n",temp1,temp2);
		if(strcmp(temp1,"blank")==0){
			arr[i]->data = ' ';
		}else{
			arr[i]->data = temp1[0];
		}
		arr[i]->weight = temp2;
		arr[i]->Lchild = NULL;
		arr[i]->Rchild = NULL;
		i++;
	}
	arr[i] = NULL;
	return arr;
}

void getWeightFile(){
	FILE*fp;
	
	fp = fopen("data.csv","w+");	
	if(fp==NULL){
		printf("文件初始化失败!");
		exit(0); 
	}
	int i=0,length=0,flag=0;
	char* ch=INIT_STRING;
	int weight=0;
	printf("请规定长度:");
	scanf("%d",&length);
	for(i=0;i<length;i++){
		printf("请输入第%d组:\n",i+1);
		scanf("%s %d",ch,&weight);
		if(strcmp(ch,"blank")==0){
			fprintf(fp,"%s,%d\n","blank",weight);
			continue;
		}
		if(ch[0]<'A'||ch[0]>'Z'){
			flag = 1;
			break;
		}else{
			fprintf(fp,"%s,%d\n",ch,weight);
		}
	}
	if(flag){
		printf("输入数据不合法!请重试!");
	}else{
		printf("\n");
		printf("输入完毕");
	}
	printf("\n");
	printf("***********请按任意键返回***********");
	getch();
}
//*/

//从大到小排序 
void sort(Node** arr){
	int count = 0;
	for(count=0;arr[count]!=NULL;count++){
		//︿( ̄︶ ̄)︿ 数数看有多长 
	}
	int i,j;
	Node* temp;
	for(i=0;i<count;i++){
		for(j=i+1;j<count;j++){
			if(arr[j]->weight > arr[i]->weight){
				temp   = arr[j];
				arr[j] = arr[i];
				arr[i] = temp;
			}
		}
	}
	/*
	for(i=0;i<count;i++){
		printf("%c,%d\n",arr[i]->data,arr[i]->weight);
	}
	*/
	
}

//创建哈夫曼树 
Node* createHuffTree(Node** arr){
	Node *head,*temp;
	int count;
	while(1){
		for(count=0;arr[count]!=NULL;count++){
		//︿( ̄︶ ̄)︿ 数数看有多长
		}
		if(count==1){
			head = arr[0];
			return head;
		}
		sort(arr);
		temp = (Node*)malloc(sizeof(Node));
		if(arr[count-1]->weight>arr[count-2]->weight){
			temp->Lchild = arr[count-1];
			temp->Rchild = arr[count-2];
		}else{
			temp->Lchild = arr[count-2];
			temp->Rchild = arr[count-1];
		}
		temp->weight = arr[count-1]->weight + arr[count-2]->weight;
		temp->data   = '#';
		temp->Lchild->Father = temp;
		temp->Rchild->Father = temp;
		
		arr[count-2] = temp;
		arr[count-1] = NULL;
	}
}

//前序遍历 
void preOrderTravel(Node* np){
	if(np==NULL){
		return;
	}
	preOrderTravel(np->Lchild);
	printf("%c,%d\n",np->data,np->weight);
	preOrderTravel(np->Rchild);
}

//用前序遍历找目标节点 
Node* preOrderTravel2(Node* np,char target){
	Node* temp=NULL;
	if(np==NULL){
		return NULL;
	}
	if(np->data==target){
		return np;
	}
	temp = preOrderTravel2(np->Lchild,target);
	if(temp!=NULL){
		return temp;
	} 
	temp = preOrderTravel2(np->Rchild,target);
	if(temp!=NULL){
		return temp;
	} 
}

//加密 
void encode(Node *tree){
	system("cls");
	String str = INIT_STRING;
	printf("请输入要加密的字符串:"); 
	gets(str);
	int i,flag=0;
	for(i=0;str[i]!='\0';i++){
		if(str[i]==' '){
			continue;
		}
		if(str[i]<'A'||str[i]>'Z'){
			flag = 1;
			break;
		}
	}
	if(flag){
		printf("输入字符串不合法!请重试!");
	}else{
		for(i=0;str[i]!='\0';i++){
		Node *np = preOrderTravel2(tree,str[i]);
		String temp1 = INIT_STRING;
		int strlen = 0;
		while(1){
			if(np == tree){
				break;
			}
			if(np->Father->Lchild == np){
				temp1[strlen] = '1';
			}else{
				temp1[strlen] = '0';
			}
			strlen++;
			temp1[strlen] = '\0';
			np = np->Father;
		}
		temp1 = strrev(temp1);
		printf("%s",temp1);
		}
	}
	
	printf("\n");
	printf("***********请按任意键返回***********");
	getch();
}

//解密 
void decode(Node *head){
	system("cls");
	String str = INIT_STRING;
	printf("请输入要解密的字符串:"); 
	gets(str);
	int i,flag=0;
	for(i=0;str[i]!='\0';i++){
		if(str[i]!='0'&&str[i]!='1'){
			flag = 1;
		}
	}
	if(flag){
		printf("输入字符串不合法!请重试!");
	}else{
		Node *np = head;
		String temp = INIT_STRING;
		for(i=0;str[i-1]!='\0';i++){
			if(np->Lchild==NULL){
				printf("%c",np->data);
				i--;
				np = head;
				continue;
			}
			if(str[i]=='1'){
				np = np->Lchild;
			}else{
				np = np->Rchild;
			}
		}
	}
	
	printf("\n");
	printf("***********请按任意键返回***********");
	getch();
}

void menu(){
	system("cls");
	printf("**************************************\n");
	printf("        1、对字符串进行加密\n");
	printf("        2、对加密结果进行解密\n");
	printf("        3、从键盘输入\n");
	printf("        0、退出\n");
	printf("**************************************\n");
	Node **arr,*tree; 
	arr = readFile();
	tree = createHuffTree(arr);
	char n;
	n = getch();
	switch(n){
		case '1': encode(tree);break;
		case '2': decode(tree);break;
		case '3': getWeightFile();break;
		case '0': exit(1);
	}
}
int main(){
	while(1){
		menu();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章