课程设计


一、实训目的

通过课程设计,学会运用数据结构知识,针对具体应用,自己设计合理数据结构,确定存储结构,并能设计具体操作算法,选择使用具体语言进行实现。掌握C++较复杂程序的组织和设计过程,调试技巧。学习解决实际问题的能力。

二、实训环境

计算机windows xp或其它版本,VC6.0或更高版本,或其它语言环境。

三、实习题目

题目3.

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

字符空格 A  B  C D   E F  G   H  I   J K  L M
频度 180 64 13 23  32 103 22 15  47 57  1 5  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、依次输入27个频度:

 

2、分别得出编码:

*把‘@’看成空格

空格的编码:1  1  1

A的编码:1  0 1  1

B的编码:1  0 0  0  0  0

C的编码:0  1 0  0  0 

D的编码:1  0 1  0  1

E的编码:0  0  1

F的编码:1  1 0  0  1  1

G的编码:1  0 0  0  0  1

H的编码:0  0 0  0

I的编码:0  1 1  1

J的编码:1  1 0  0  0 0  1  1 0  0  

K的编码:1  1 0  0  0 0  1  0

L的编码:1  0 0  0  1

M的编码:1  1 0  0  1  0

N的编码:0  1 0  1

O的编码:1  0 0  1

P的编码:1  0 1  0  0  0

Q的编码:1  1 0  0  0 0  1  1 0  1

R的编码:0  0 0  1

S的编码:0  1 1  0

T的编码:1  1 0  1

U的编码:0  1 0  0  1

V的编码:1  1 0  0  0 0  0

W的编码:1  1 0  0  0  1

X的编码:1  1  0  0 0  0  1 1  1  1

Y的编码:1  0 1  0  0 0  1

Z的编码:1  1 0  0  0 0  1  1 1  0

 

 

3、输入27个字符的编码以及加密编码:

 

 

4、结束后的界面

 

 

 

五、源代码

#include <iostream>

#include<iomanip>

 

using namespace std;

 

const int n=27;// 叶子

const int m=2*n-1;//结点

 

 

struct Tree

{

       intweight;//权值

       intlchild,rchild,parent;

};

 

 

Tree hft[m+1];

 

 

void HuffmanTree()

{

       //----------------初始化-------------------

       inti,j,p1,p2;

       floats1,s2;

       for(i=1;i<=n;i++)

       {

              hft[i].parent=0;

              hft[i].lchild=0;

              hft[i].rchild=0;

              hft[i].weight=0;

       }

      

       //----------------初始化-------------------

       cout<<"◇〇依次输入27个权值〇◇:";

 

       //----------------比较-------------------

       for(i=1;i<=n;i++)

              cin>>hft[i].weight;     //输入权值

 

       for(i=n+1;i<=m;i++)          

       {

              p1=p2=0;                  

              s1=s2=32767;              

              for(j=1;j<=i-1;j++)         

              {

                     if(hft[j].parent==0)

                            if(hft[j].weight<s1)

                            {

                                   s2=s1;

                                   s1=hft[j].weight;

                                   p2=p1;p1=j;

                            }

                            else

                                   if(hft[j].weight<s2)

                                   {

                                          s2=hft[j].weight;

                                          p2=j;

                                   }

              }

                           

                                   hft[p1].parent=i;

                                   hft[p2].parent=i;

                                   hft[i].lchild=p1;

                                   hft[i].rchild=p2;

                                   hft[i].weight=hft[p1].weight+hft[p2].weight;

       }

       cout<<endl;

       cout<<"-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-"<<endl;

}

 

 

struct Code

{

       intbits[n+1];

       intstart;

       charch;

 

};

Code code[n+1];

void HuffmanCode()

{

   Code cd;

       intc,p;

       for(inti=1;i<=n;i++)

       {

              cd.start=n+1;

              cd.ch=63+i;                   

              c=i;

              p=hft[i].parent;

              while(p!=0)

              {

                     cd.start--;

                     if(hft[p].lchild==c)cd.bits[cd.start]=0;

                     elsecd.bits[cd.start]=1;

                     c=p;

                     p=hft[p].parent;

              }

              code[i]=cd;

       }

       cout<<"注意!这里‘@’为空格!"<<endl;

      

       for(i=1;i<=n;i++)

       {

              cout<<"字符"<<code[i].ch<<"的权值为:"<<hft[i].weight<<setw(5)<<"编码为:";

              for(intj=code[i].start;j<=n;j++)

                     cout<<code[i].bits[j]<<"  ";

              cout<<endl;

       }

       cout<<"-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-◇-"<<endl;

       cout<<"输入的字符:空格ABCDEFGHIJKLMNOPQRSTUVWXYZ的编码为:"<<endl;

 

 

       for(i=1;i<=n;i++)

       {

        

              for(intj=code[i].start;j<=n;j++)

                     cout<<code[i].bits[j];

       }

 cout<<endl;

       cout<<"加密编码:"<<endl;

              for(i=1;i<=n;i++)

       {

        

              for(intj=code[i].start;j<=n;j++)

              {

                     for(inta=1;a<=n;a++)

                     {code[i].bits[a]=9;}

                     cout<<code[i].bits[j];

              }

       }

       system("pause");

}

void End()

{

              system("cls");

       cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;

       cout<<"┃                                                                     ┃"<<endl;

       cout<<"┃                      感谢使用ヽ(′▽`)ノ                           ┃"<<endl;

       cout<<"┃                                                                      ┃"<<endl;

       cout<<"┃                           bye~bye~                                 ┃"<<endl;

       cout<<"┃                                                                ┃"<<endl;

       cout<<"┃                           ☆^▽^☆                                ┃"<<endl;

       cout<<"┃                                                                     ┃"<<endl;

       cout<<"┃                           The End                                  ┃"<<endl;

       cout<<"┃                                                                     ┃"<<endl;

       cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫"<<endl;

       cout<<"┃                           广东海洋大学                              ┃"<<endl;

       cout<<"┃                                                                      ┃"<<endl;

       cout<<"┃                        班级:信管1133班                              ┃"<<endl;

       cout<<"┃                                                                     ┃"<<endl;

       cout<<"┃                        姓名:麦婉霞                                  ┃"<<endl;

       cout<<"┃                                                                     ┃"<<endl;

       cout<<"┃                        学号:201311671318                            ┃"<<endl;

       cout<<"┃                                                                      ┃"<<endl;

       cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;

 

}

int main()

{

       HuffmanTree();

       HuffmanCode();

       End();

       return0;

}                               


                                    

 

六、总结

此次用到的是哈夫曼树原理,根据所得的频率不同,构造出来的编码也不一样。输出了哈弗曼编码,并且进行了简单的加密。

经过这个星期的课程设计,我更加巩固了c++和数据结构之间的相关知识,学会了运用哈夫曼树。在编写代码的过程中,我也掌握了很多技巧和重点。

第一,在编写的过程中注意随时检查能不能成功运行,不能在最后才进行调试,这样的话修改程序的时候会比较麻烦。

第二,遇到不会的问题可以上网搜索答案或寻求网上的人帮助,这比起自己在对着书本解决更有效。

虽然还有很多点子想加入程序当中,但因为技术不过关而无法实现。因此经过这次的课程设计,我知道了自己还是有很大的空间去提升自己,希望在日后能够做得更加好。

这次的课程设计虽然做得不算太好,还有很多功能没有实现,以后会作补充。我相信只要努力,一定会得到更好的劳动成果。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值