级《数据结构》课程设计题目A210 陈建华 53090119Word文档下载推荐.docx
《级《数据结构》课程设计题目A210 陈建华 53090119Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《级《数据结构》课程设计题目A210 陈建华 53090119Word文档下载推荐.docx(26页珍藏版)》请在冰豆网上搜索。
文本文件自行选择,至少含3000个字符。
《数据结构》课程设计报告
姓名
陈建华
学号
53090119
2009级
1班
6组
实验室:
计算机楼A209
提交日期
2010.8.24
成绩
指导教师
题目三:
问题解析:
运用树和线性表的数据结构知识,设计huffman编码,建立一个栈,编码
从文件中读入任意一篇英文短文(文件为ASCII编码,扩展名为txt);
统计并输出不同字符在文章中出现的频率(空格、换行、标点等也按字符处理);
根据字符频率构造哈夫曼树,并给出每个字符的哈夫曼编码;
图形化输出哈夫曼树、哈夫曼编码;
将文本文件利用哈夫曼树进行编码,存储成压缩文件(编码文件后缀名.huf)
用哈夫曼编码来存储文件,并和输入文本文件大小进行比较,计算文件压缩率;
进行译码,将huf文件译码为ASCII编码的txt文件,与原txt文件进行比较
数据结构选择、算法设计:
本题采用了链表,树的数据结构。
任务分工及进度计划:
由陈建华同学完成编码和解码函数部分,由荣国东同学完成栈和队列的构造部分,由廖乐同学完成主函数,比较函数的调用方式。
用户手册用户输入file1.txt,当存在这个文件的时候,该程序可以将这个文件的字符(包括空格和换行)显示出来,并显示其个数,下一步,该程序希望用户建立一个文件(比如file2.txt),然后解码至file3.txt,将file1.txt和file.txt进行比较,核实是否相同。
同时将file2.txt和file1.txt比较输出压缩率。
,测试结果根据题目要求的测试结果正确
总结(对所作程序进行分析、评价运行效果,总结遇到的问题和解决办法)
在利用数组的建立过程中,发现比较函数使用很不方便,(例如每次要减少2个节点并插入一个),利用C++中的class可以更好的解决这个问题。
程序清单:
#include<
iostream>
fstream>
string>
math.h>
usingnamespacestd;
ifstreaminfile1;
//读原文件
ofstreamoutfile2;
ifstreaminfile2;
//读压缩文件
ofstreamoutfile3;
ifstreaminfile3;
//读解码后的文件
ofstreamoutfile4;
ifstreaminfile4;
classBTNode{
public:
chardata;
intflunt;
stringhuff;
BTNode*Left;
BTNode*Right;
BTNode(constcharitem,BTNode*Left1=NULL,BTNode*Right1=NULL):
data(item),Left(Left1),Right(Right1),flunt(0){}
voidSetLeft(BTNode*L){Left=L;
}
voidSetRight(BTNode*R){Right=R;
BTNode*GetLeft(){returnLeft;
BTNode*GetRight(){returnRight;
voidSetData(constint&
item){data=item;
char&
GetData(){returndata;
int&
GetFlunt(){returnflunt;
voidSetFlunt(constintf){flunt=f;
};
classSNode//*****************************************栈*****************************************************
{
BTNode*item;
SNode*next;
SNode(BTNode*t){item=t;
next=NULL;
classStack
SNode*top;
Stack(){top=NULL;
voidInsert(BTNode*t);
BTNode*Pop();
boolIsEmpty();
boolStack:
:
IsEmpty()
if(top==NULL)
returntrue;
else
returnfalse;
voidStack:
Insert(BTNode*t)
{
SNode*p;
p=newSNode(t);
if(IsEmpty())
top=p;
{p->
next=top;
top=p;
}
BTNode*Stack:
Pop()
BTNode*p;
SNode*q;
if(top==NULL){cout<
<
"
thestackisempty"
endl;
returntop->
item;
{p=top->
q=top;
top=top->
next;
deleteq;
returnp;
Stacks;
classLNode
{public:
charch;
intcount;
boolflag;
stringhuff;
LNode*next;
BTNode*root;
LNode(){next=NULL;
LNode(char&
ch1){ch=ch1;
count=1;
flag=0;
root=NULL;
classList
LNode*head;
LNode*current;
intcount;
List(){head=newLNode();
count=0;
current=head;
voidInsert(char&
ch);
voidDelete();
voidSortInsert(LNode*pt);
voidSort();
voidBackSort();
voidList:
Insert(char&
ch)
current->
next=newLNode(ch);
current=current->
boolList:
if(count==0)
return1;
return0;
SortInsert(LNode*pt)
LNode*p;
LNode*p1;
p1=head;
p=head->
while(p!
=NULL&
&
pt->
count>
p->
count){p1=p;
p=p->
if(p==NULL)
{
p1->
next=pt;
pt->
next=p;
count++;
Delete()
head->
next=p->
count--;
Sort()
intmin;
LNode*p;
LNode*q;
LNode*p1;
LNode*q1;
LNode*temp;
p=p1->
while(p->
next!
=NULL)
{q1=p;
q=p->
while(q!
{
if(q->
count<
count)
{if(p->
=q)
{
temp=p->
p1->
next=q;
p->
next=q->
q->
next=temp;
q1->
q=p;
p=p1->
}
else
q1=q;
q=q->
}
p1=p;
p=p->
BackSort()
Listl;
ListListSearch;
voidtranslate()//&
translate&
{charc;
while((c=infile1.get())!
=EOF)
{
p=(l.head);
while(c!
=p->
ch&
=NULL){p=p->
if(p->
next==NULL)
if(c==p->
ch){p->
count=p->
count+1;
{l.Insert(c);
l.count=l.count+1;
else
p->
l.Sort();
/*LNode*pp=newLNode();
pp->
pp->
ch='
z'
;
l.SortInsert(pp);
l.Delete();
*/
p=l.head->
//
cout<
文件字符统计如下:
cout<
ch<
'
classTree{
private:
BTNode*root;
Tree(BTNode*t=NULL){root=t;
BTNode*Create(LNode*h);
BTNode*Getroot(){returnroot;
voidSetroot(BTNode*t){root=t;
intHeight(BTNode*t2);
Treetr;
BTNode*root;
charresult[10000]={0};
//&
createlink(level)&
classQNode
BTNode*node;
QNode*next;
friendclassLinkQuene;
QNode(BTNode*t=NULL){node=t;
classLinkQuene
QNode*front,*rear;
LinkQuene(){rear=front=newQNode();
voidInQuene(BTNode*);
BTNode*OutQuene();
voidLinkQuene:
InQuene(BTNode*item=NULL)
rear->
next=newQNode(item);
rear=rear->
BTNode*LinkQuene:
OutQuene()
QNode*p;
p=front->
BTNode*t=p->
node;
front->
deletep;
if(!
front->
next)
rear=front;
returnt;
intTree:
Height(BTNode*t)
t)return0;
inthl,hr;
hl=Height(t->
GetLeft());
hr=Height(t->
GetRight());
returnhl>
hr?
++hl:
++hr;
BTNode*Tree:
Create(LNode*h)//&
createtree&
BTNode*p1=NULL,*p2=NULL;
LNode*t1=NULL,*t2=NULL;
BTNode*s;
BTNode*root1;
intt=l.count;
while(l.count>
1){
t1=h->
t2=h->
next->
l.Delete();
l.Delete();
if((t1->
flag==0)&
(t2->
flag!
=0))
p1=newBTNode(0);
p1->
SetData(t1->
ch);
SetFlunt(t1->
count);
SetLeft(NULL);
SetRight(NULL);
p2=t2->
root;
=0)&
flag==0))
p2=newBTNode(0);
p2->
SetData(t2->
SetFlunt(t2->
p1=t1->
{
s=newBTNode(0);
s->
SetFlunt(p1->
GetFlunt()+p2->
GetFlunt());
SetLeft(p1);
s->
SetRight(p2);
SetData('
*'
);
LNode*s1;
s1=newLNode();
s1->
count=p1->
GetFlunt();
flag=1;
root=s;
l.SortInsert(s1);
root1=h->
returnroot1;
voidBT(BTNode*a){
if(a!
a->
GetData()<
huff<
return;
BT(a->
voidCode(BTNode*t)//*********************************编码函数************************************************
staticstrings="
staticintcounter=0;
if(t==NULL)
s=s+'
0'
counter=counter+1;
Code(t->
counter=counter-1;
s=s.erase(counter,1);
1'
t->
huff=s;
voidCompress()//
p=root;
if(p==NULL)return;
=NULL||!
s.IsEmpty())//遍历
while(p!
s.Insert(p);
p=p->
GetLeft();
if(s.IsEmpty())return;
p=s.Pop();
GetLeft()==NULL&
GetRight()==NULL)
{ListSearch.Insert(p->
data);
ListSearch.current->
flunt;
huff=p->
huff;
GetRight();
ListSearch.BackSort();
LNode*l;
字符——哈夫曼码检索表如下:
l=ListSearch.head->
//打印检索表
while(l!
l->
huf