天天看點

周總結<4>

經過了一周的學習,我們在html以及C語言方面又有的新的知識點的學習。

html 自習表格,函數等
C語言 哈弗曼編碼

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>

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

<table width="221" border="1"  cellpadding="0" cellspacing="0">

<tr>

  <td height="30" colspan="2"> 使用者登入</td>

</tr>

  <td width="59" height="30"> 使用者名</td>

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

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

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

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

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

   </td>

</table>

</form>

<script type="text/javascript">

/*

P243 14-3

P246 14-6

P251 14-8 界面實作建議用表單的方式。

*/

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 >");

var a=3;

var b="name";

var c=null;

alert("a的類型為:"+(typeof a)+"\nb的類型為:"+(typeof b)+"\nc的類型為:"+(typeof c));

function check()

{

var name=form1.user.value;

var pwd=form1.pwd.value;

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

alert("請輸入使用者名");

form1.user.focus();

return;

}

else if((pwd=="")||(pwd=null))

alert("請輸入密碼");

form1.pwd.focus();

else

form1.submit();

</script>

<body>

</body>

</html>

二.

P258  14-13  學習函數  countdown ,

1 明白參數傳遞實作,掌握 "id"屬性作為實參的用法。

2 學會Date 的使用

3 嘗試将countdown改寫成switch ---case 的條件判斷

4 頁面實作改成成表單格式。

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>=0)

switch(day)

case 0:divId.innerHTML="<b>就是今天"+title+"呀!</b>";break;

case 1:divId.innerHTML="<b>-明天就是"+title+"啦!</b>";break;

default :divId.innerHTML="<b>-據"+title+"還有"+day+"天!</b>";break;

divId.innerHTML="<b>-哎呀"+title+"已經過了!</b>";

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

<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 type="text/javascript">

countdown("2016年母親節","5/8/2016",countDown);

            </td>

        </tr>

    </table></td>

</tr>    

C語言案例:

// 303.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;

    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("303 柳曉雅\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;