第十一周技术博客

数据结构

哈夫曼编码的生成

// 242陈坤鑫第十一周.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <stdio.h>

#include <string.h>

typedef char DataType;

 

struct element  //结点定义

{    

           DataType data;

           float weight;//此字符的权值

      int parent,lchild, rchild;//父结点,左孩子,右孩子存放位置

};

#define MAXLEAF 6  //最大叶子结点数目,待编码的字符数

#define MAXNODE MAXLEAF*2-1   //最大结点数

struct Huffmancode{

   DataType ch;//存放字符

   char bits[MAXLEAF];//存放字符的哈夫曼编码

};

Huffmancode hcode[MAXLEAF];

//element ht[ MAXNODE];

 

         //此函数为选取两个最小的权值结点的位置 分别分别存放在pn[0],pn[1]

void Select (element *pt,int n, int *pn){

    int i,iposi=0;

         float tmp;

         for(i=0;i<n;i++){

                    if(pt[i].parent==-1)

         {

                   tmp=pt[i].weight;pn[0]=i;

                              iposi=i;

                              break;

                    }

         }

         for(i=iposi;i<n;i++){

                   if(tmp>pt[i].weight && pt[i].parent==-1){

            pn[0]=i; tmp=pt[i].weight;                         

                   }

         }

 

 

     for(i=0;i<n;i++){

                    if(pt[i].parent==-1 && i!=pn[0])

         {

                   tmp=pt[i].weight;pn[1]=i;

                              iposi=i;

                              break;

                    }

         }

    for(i=iposi;i<n;i++){

                   if(tmp>pt[i].weight && pt[i].parent==-1 && i!=pn[0]){

            pn[1]=i; tmp=pt[i].weight;

                   }

         }

   return;

}

 

//此函数功能为创建哈夫曼树

void CreateHuffmanTree(element *pt){

    int i,k=0;

         int pn[2];

         for(i=MAXLEAF ;i<MAXNODE;i++){

                   //选取两个最小的权值结点的位置 分别分别存放在pn[0],pn[1]

        Select(pt,MAXLEAF+k,pn);

        k++;

                   pt[pn[0]].parent=pt[pn[1]].parent=i;

                   pt[i].lchild=pn[0]; pt[i].rchild=pn[1];

                   pt[i].weight=pt[pn[0]].weight+pt[pn[1]].weight;

         }

}

 

//此函数功能为生成哈夫曼编码

void CreateHuffmanCode(element *pt,int n){

   int i,p,j,start;

   char cd[MAXNODE];

   for(i=0;i<n;i++){

          start=n-1;

     cd[start]=0;

          p=pt[i].parent;

          j=i;

 

          //从叶子结点出发,逐层遍历到根结点,逆序求出每个结点的哈夫曼编码

          while(p!=-1){//当p为 -1时,表示遍历到根结点

        if(pt[p].lchild==j)

                            cd[--start]='0';//左孩子编码为0

                   else

                            cd[--start]='1'; //右孩子编码为1

                   j=p;

                   p=pt[p].parent;

          }

          strcpy(hcode[i].bits,&cd[start]);

   }

}

 

int main(int argc, char* argv[])

{

printf("242陈坤鑫第十一周\n");

 element ht[MAXNODE];

 int i;

 for(i=0;i<MAXNODE;i++) {

            ht[i].parent=-1;

            ht[i].lchild=-1;

            ht[i].rchild=-1;

            ht[i].data=' ';

            ht[i].weight=0;    

 }

//ht[0].data='A' ;ht[0].weight=2;  hcode[0].ch=ht[0].data;

//ht[1].data='B' ;ht[1].weight=4;  hcode[1].ch=ht[1].data;

//ht[2].data='C' ;ht[2].weight=5;  hcode[2].ch=ht[2].data;

//ht[3].data='D' ;ht[3].weight=3;  hcode[3].ch=ht[3].data;

 

ht[0].data='A' ;ht[0].weight=28;  hcode[0].ch=ht[0].data;

ht[1].data='B' ;ht[1].weight=13;  hcode[1].ch=ht[1].data;

ht[2].data='C' ;ht[2].weight=30;  hcode[2].ch=ht[2].data;

ht[3].data='D' ;ht[3].weight=10;  hcode[3].ch=ht[3].data;

ht[4].data='E' ;ht[4].weight=12;  hcode[4].ch=ht[4].data;

ht[5].data='F' ;ht[5].weight=7;  hcode[5].ch=ht[5].data;

 

 CreateHuffmanTree(ht);//生成哈夫曼树

CreateHuffmanCode(ht,MAXLEAF);//生成哈夫曼编码

 

//输出每个字符的编码

float weight=0;

for(i=0;i<MAXLEAF;i++){

   weight +=ht[i].weight*strlen(hcode[i].bits);

   printf("字符=%c 权值=%f 编码=%s\n",hcode[i].ch, ht[i].weight,hcode[i].bits);

}

printf("weight=%f\n",weight);

return 0;

}

 

 

Web技术

运算符

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>运算符</title>

</head>

 

<body>

<script type="text/javascript">

         var num1 = 120,num2 = 25;

         document.write("120+25="+(num1+num2)+"<br>");

         document.write("120-25="+(num1-num2)+"<br>");

         document.write("120*25="+(num1*num2)+"<br>");

         document.write("120/25="+(num1/num2)+"<br>");

         document.write("(120++)="+(num1++)+"<br>");

         document.write("++120="+(++num1)+"<br>");

</script>

</body>

</html>

 

 

typeof 运算符

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>typeof 运算符</title>

</head>

 

<body>

<script language="javascript">

         var a=3;

         var b="name";

         var c=null;

         alert("a的类型为"+(typeof a)+"\nb的类型为"+(typeof b)+"\nc的类型为"+(typeof c));

</script>

</body>

</html>

 

 

判断用户是否输入用户名与密码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>判断用户是否输入用户名与密码</title>

</head>

 

<body>

<form name="form1" method="post" action="">

         <table width="221" border="1" cellspacing="0" cellpadding="0" bordercolor="#FFFFFF" bordercolordark="#CCCCCC" bordercolorlight="#FFFFFF">

    <tr>

             <td height="30" colspan="2" bgcolor="#eeeeee">用户登录</td>

    </tr>

    <tr>

             <td width="59" height="30">用户名:</td>

        <td width="162"><input name="user" type="text" id="user" /></td>

    </tr>

    <tr>

             <td height="30">密码:</td>

             <td><input name="pwd" type="text" id="pwd"/></td>

    </tr>

    <tr>

             <td height="30" colspan="2" align="center"><input name="Button" type="button" class="btn_grey" value="登录" onclick="check()"/>

        <input name="Submit2" type="reset" class="btn_grey" value="重置"/></td>

    </tr>

    </table>

</form>

<script language="javascript">

         function check(){

                   var name=form1.user.value;

                   var pwd=form1.pwd.value;

                   if((name=="") || (name==null)){

                            alert("请输入用户名!");

                            form1.user.focus();

                            return;

                   }else if((pwd=="") || (name==null)){

                            alert("请输入密码!");

                            form1.pwd.focus();

                            return;

                   }else{

                            form1.submit();

                   }

         }

</script>

</body>

</html>

 

 

判断系统当前时间与2013年元旦相距的天数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>判断系统当前时间与2013年元旦相距的天数</title>

</head>

 

<body>

function countdown(title,Intime,divId){

         var online=new Date(Intime);

         var now=new Date();

         var leave=online.getTime() - now.getTime();

         var day=Math.floor(leave/(1000*60*60*24))+1;

         if(day>1){

                   if(document.all){

                            divId.innerHTML="<b>————距"+ title+"还有"+day+"天!</b>";

                   }

         }else{

                   if(day==1){

                 if(document.all){

                                     divId.innerHTML="<b>————明天就是"+title+"呀!</b>";

            }

                   }else{

                 if(day==0){divId.innerHTML="<b>今天就是"+title+"啦!</b>";

            }else{

                     if(document.all)

                                               divId.innerHTML="<b>————哎呀!"+title+"已经过了!</b>";

                                      }

                            }

                   }

         }

}

<table width="350" height="450" border="0" align="center" cellpadding="0" cellspacing="0">

         <tr>

             <td valign="bottom"><table width="346" height="418" border="0" cellpadding="0" cellspacing="0">

                       <tr>

                                     <td width="76"></td>

                                <td width="270">

                                         <div id="countDown">

                                             <b>——</b></div>

                                        <script language="javascript">

                                                                 countdown("2013年元旦","1/1/2013",countDown);

                                                                 <!--调用JavaScript 函数-->

                                                        </script>

                                </td>

                            </tr>

        </table></td>

    </tr>

</table>

</body>

</html>

周数

专业学习目标

专业学习时间

新增代码量

博客发表量

人文方面的学习

知识技能总结

 第十一周

数据结构哈夫曼编码的生成,Web技术函数的应用

5

350h

2

哈夫曼编码分析复杂,函数容易理解和方便

posted on 2016-05-08 18:52  废躯残骸  阅读(129)  评论(0编辑  收藏  举报