您的位置:首页 > 理论基础 > 数据结构算法

数据结构课程设计——编码与译码

2015-01-11 14:36 411 查看


一、问题描述:

本次所选的课程设计题目为字符的编码与译码。

内容要求:在一个加密应用中,要处理的信息来自下面的字符集,各个字符的相关使用频度如下:

字符  A   B    C   D    E   F    G    H    I     J    K    L   M

频度 180  64  13  23  32  103  22  15   47  57   15   31   20

字符  N   O    P     Q    R    S   T     U    V    W   X   Y    Z 

频度  55   63   15   1   48    56  80    25   7    18    2   16    1

现请编写程序你实现如下功能:


(1)运行时,由用户输入来初始化字符集大小和相应用字符。

(2)输入一个要加密的字符串,将其加密。

(3)输出解密字符串。

二、问题分析
1.系统分析
信息时代,信息贯穿在人们的日常生活中,对人们的影响日趋扩大,利用哈夫曼编码进行通信可以提高信息的保密程度,提高利用率,缩短信息传输时间,降低传输成本。
2.程序功能分析:
发送者:
1.输入相应字符和频率初始化字符集
2.输入要加密的字符
3.通过输入编码来获取字符
接受者:
1.接收发送者传递来的初始化字符集,进行哈夫曼树的构建;
2.哈夫曼编码的生成;
3.对编码信息的翻译;
4.输出哈夫曼树的结构
三、逻辑结构和存储结构设计

(一)逻辑结构:
字母按顺序排列,赋予不同的权值;
由课程设计的内容要求来定义函系统功能;
(二)存储结构:
1.通过多文件结构进行定义,头文件(1.h)包含程序文件的共享信息:哈夫曼树结构体、编码结构体和Huffmantree类等;
源程序文件(1-1.cpp)包含主函数和源程序(1-2.cpp)包含各种函数定义:
哈夫曼树构建—creathuffmantree
编码—huffcode  
译码—trancode 
打印哈夫曼树—printtree
2.各函数的结构图



3.哈夫曼树的结构体:权值(weigth)、左孩子(lchild)、右孩子(rchild)和双亲(parent)由hftree[maxnode+1]数组来定义;

编码结构体:

bit 为一维数组,用来保存字符的哈夫曼编码,

start 表示该编码在数组 bit 中的开始位置;

4.顺序存储结构来存储数据和查找

四、算法设计



五、源代码

(1)头文件(2.h)

#ifndef Huffmantree_H
#define Huffmantree_H
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<stdlib.h>
using namespace std;
const maxleaf=30; //maxsize或n
const maxnode=maxleaf*2-1; //m
typedef struct //定义一个哈夫曼树的结构
{
char ch;
int weight;
int parent;
int lchild;
int rchild;
}HNodeType; //定义类,给类的一个别名

typedef struct //定义一个编码的结构
{
int bit[maxleaf+1]; //二进制
int start;
}codetype;

class Huffmantree{
public:
Huffmantree(){}
~Huffmantree(){}
void creathuffmantree(int n);//创建哈夫曼树
void huffcode(int n); //编码
void trancode(int n); // 译码
void printtree(int n); //打印哈夫曼树并输出屏幕中
HNodeType hftree[maxnode+1];//定义一个哈夫曼树结构的数组,0不使用
codetype Huffcode[maxleaf];//定义一个编码的结构数组
private:
int n;
};
#endif

(2)源程序文件(2-2.cpp)

#include<iostream>
using namespace std;
#include "2.h"
void Huffmantree::creathuffmantree(int n)
{
int i,j,x1,x2,m1,m2,c,p;
codetype cd;
for(i=0;i<maxleaf;i++)               //对每个叶子结点进行初始化
{
hftree[i].parent=-1;
hftree[i].lchild=-1;
hftree[i].rchild=-1;
hftree[i].weight=-1;
}
cout<<"请输入字符和权值:\n"<<endl;
for(i=0;i<n;i++)                    //从键盘输入字符和权值
{
cout<<"输入第"<<i+1<<"个字符和权值:  ";
cin>>hftree[i].ch;
cin>>hftree[i].weight;
}
//构建哈夫曼树并对其进行编码和输出
for(i=0;i<n-1;i++)                 //构造哈夫曼树
{
m1=m2=10000;
x1=x2=0;
for(j=0;j<n+i;j++)
{
if(hftree[j].parent==-1&&hftree[j].weight<m1)
{
m2=m1;
x2=x1;
m1=hftree[j].weight;
x1=j;
}
else
if(hftree[j].parent==-1&&hftree[j].weight<m2)
{
m2=hftree[j].weight;
x2=j;
}
}
hftree[x1].parent=n+i;
hftree[x2].parent=n+i;
hftree[n+i].weight=hftree[x1].weight+hftree[x2].weight;
hftree[n+i].lchild=x1;
hftree[n+i].rchild=x2;
}
for(i=0;i<n;i++)
{
cd.start=n-1;
c=i;
p=hftree[c].parent;
while(p!=-1)
{
if(hftree[p].lchild==c)
cd.bit[cd.start]=0;
else cd.bit[cd.start]=1;
cd.start--;
c=p;
p=hftree[c].parent;
}
for(j=cd.start+1;j<n;j++)
Huffcode[i].bit[j]=cd.bit[j];

Huffcode[i].start=cd.start;
}
for(i=0;i<n;i++)
{
cout<<hftree[i].ch<<"的编码为:  ";
for(j=Huffcode[i].start+1;j<n;j++)
cout<<Huffcode[i].bit[j];
cout<<endl;
}

}
void Huffmantree::huffcode(int n)  //编码
{
ofstream file1("d:\\hftreecode.txt",ios::out);
char ch;
int q=0;
int j;
cout<<"请输入要编码的字符串(按@)结束"<<endl;
cin>>ch;     //对字符串进行编码
while(q<n&&ch!='#')
{
while(hftree[q].ch!=ch)
q++;
for(j=Huffcode[q].start+1;j<n;j++)
{
cout<<Huffcode[q].bit[j];
file1<<Huffcode[q].bit[j];
}
q=0;
cin>>ch;
}
file1.close();
cout<<endl;
}
void Huffmantree::trancode(int n)//译码
{
char c;
int m1=0,m2=0;
ifstream file1("d:\\hftreecode.txt",ios::in);
while(file1.get(c)!='\0')
m1++;
file1.close();
if(m1==0)
{
cout<<"请先编码!!"<<endl;
return;
}
ifstream file2("d:\\hftreecode.txt",ios::in);//文件以输入方式打开(文件数据输入到内存)
char *cc=new char[m1+1];
while(file2.get(c)!='\0')
{
cc[m2]=c;
m2++;
}
cc[m2]='\0';
int i,j=0;
i=2*n-2;
while(cc[j]!='\0')
{
if(cc[j]=='0')
i=hftree[i].lchild;
else
i=hftree[i].rchild;
if(hftree[i].lchild==-1)
{
cout<<hftree[i].ch<<endl;
i=2*n-2;
}
j++;
}
file2.close();

}
void Huffmantree::printtree(int n)//打印哈夫曼树
{
int i;
ofstream file("e:\\printhuffman.txt",io
4000
s::out);
cout<<"字符"<<setw(8)<<"权值"<<setw(8)<<"左孩子"<<setw(8)<<"右孩子"<<setw(8)<<"双亲"<<endl;
file<<"字符"<<setw(8)<<"权值"<<setw(8)<<"左孩子"<<setw(8)<<"右孩子"<<setw(8)<<"双亲"<<endl;
for(i=0;i<2*n-1;i++)
{
cout<<hftree[i].ch<<setw(8)<<hftree[i].weight<<setw(8)<<hftree[i].lchild<<setw(8)<<hftree[i].rchild<<setw(8)<<hftree[i].parent<<endl;
file<<hftree[i].ch<<setw(8)<<hftree[i].weight<<setw(8)<<hftree[i].lchild<<setw(8)<<hftree[i].rchild<<setw(8)<<hftree[i].parent<<endl;
}
cout<<endl;
file.close();
}

(3)主函数(2-1.cpp)
#include<iostream>
using namespace std;
#include"2.h"
int main()
{
int n,choice;
Huffmantree ht;
while(1)
{
system("cls");
cout<<"\t*******************★哈夫曼树的编码和译码★***************** \n";
cout<<"\t*******************请选择: **********************************\n";
cout<<"\t***********************1-----创建哈夫曼树********************\n";
cout<<"\t***********************2-----编码****************************\n";
cout<<"\t***********************3-----译码****************************\n";
cout<<"\t***********************4-----打印哈夫曼树********************\n";
cout<<"\t***********************0-----退出****************************\n";
cout<<"\t 你要输入的编号是(0--4):";
cin>>choice;
if(choice==0)cout<<"退出本系统"<<endl; break;
switch(choice)
{
case 1:
{
cout<<"输入叶子结点数n:";
cin>>n;
cout<<""""<<endl;
ht.creathuffmantree(n);
}
system("pause");
break;
case 2: ht.huffcode(n);
system("pause");
break;
case 3: ht.trancode(n);
system("pause");
break;
case 4: ht.printtree(n);
system("pause");
break;
default:
cout<<"没有此选项,请重新选择!"<<endl;
}
}
return 0;
}
七、程序运行结果

 


(一)创建哈夫曼树:

 


输出:



(二)编码:

 


输入:



(三)译码:



(四)打印哈夫曼树:



(五)退出

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: