实验6 编码
- 格式:docx
- 大小:110.61 KB
- 文档页数:9
实验6 编码6-1 Huffman编码1.实验任务与目的编辑并运行程序:构拟并输出霍夫曼编码。
编程序时需要注意以下问题:·森林用列表定义,列表中每一个元素都是一棵二叉树。
·采用简单选择法对森林中的树进行排序。
·采用递归法遍历二叉树,遍历时生成Huffman编码。
程序中需要定义二叉树的节点类,还要编写二叉树按逆序排序的函数。
通过本实验掌握编程构造霍夫曼树并最终完成霍夫曼编码的方法,了解压缩编码方案设计的一般程序。
2.实验步骤与运行结果(1)源代码:charSet=['a','b','c','d','e','f','g']weightSet=[21/93,26/93,11/93,8/93,10/93,11/93,6/93]size=len(charSet)class Node:def __init__(self):self.symbol=Noneself.weight=Noneself.left=Noneself.right=Nonedef sortList(sList,size):for i in range(size-1,0,-1):minWeight=sList[i].weightindex=ifor j in range(i-1,-1,-1):if minWeight>sList[j].weight:minWeight=sList[j].weightindex=jtemp=sList[i]sList[i]=sList[index]sList[index]=tempcharList=[]for i in range(size):charList.append(Node())charList[i].symbol=charSet[i]charList[i].weight=weightSet[i]while(size!=1):sortList(charList,size)small1=charList[size-2]small2=charList[size-1]NewNode=Node()NewNode.symbol=small1.symbol+small2.symbol NewNode.weight=small1.weight+small2.weightNewNode.left=small1NewNode.right=small2charList[size-2]=NewNodesize=size-1def traverse(preStr,root,output):if root.left==None and root.right==None:output[root.symbol]=preStrelse:if root.left!=None:traverse(preStr+'0',root.left,output) if root.right!=None:traverse(preStr+'1',root.right,output)output={'a':'','b':'','c':'','d':'','e':'','f':'','g':''}traverse('',charList[0],output)for i in range(0,len(charSet),1):print(charSet[i]+':',output[charSet[i]])(2)运行结果截图6-2 加密和解密1.实验任务与目的编辑并运行程序:输入一串英文字符(明文),输出经过加密后的“密文”。
加密方法包括:(1)单字符替代加密;(2)列换位加密;(3)位级加密。
通过本实验掌握简单的凯撒加密、换位加密及位级加密的编程实现方法,了解传统加密的一般思想。
2.实验步骤与运行结果(1)单字符替代加密①源代码:def CaesarEncrypt(text):Caesar=""for i in range(0,len(text),1):if(ord(text[i])>=ord('a'))and(ord(text[i])<=ord('z')):Caesar=Caesar+chr(ord('a')+((ord(Text[i])-ord('a')+3)%26))else:if(ord(text[i])>=ord('A'))and(ord(text[i])<=ord('Z')):Caesar=Caesar+chr(ord('A')+((ord(Text[i])-ord('A')+3)%26)) return CaesarText=input("Please input a plain text:\n")print("THe cipherText is:\n",CaesarEncrypt(Text))②运行结果截图:(2)列换位加密①源代码:def transpoEncrypt(text,n,sequence):List=[['']*10,['']*10,['']*10,['']*10,['']*10,['']*10,['']*10,['']*10,['']*10,['']*10]cipher=''for i in range(0,len(text),1):List[i//int(n)][i%int(n)]=text[i]for j in sequence:for k in range(0,int(n),1):cipher=cipher+List[k][int(j)-1]return cipherText=input("Please input a plain text:")n=input("Please input a n<=10:")Sequence=input("Please input a sequence:")print("Transposed encryption:",transpoEncrypt(Text,n,Sequence)) ②运行结果截图:(3)位级加密①源代码:def levelEncrypt(text,key):cipher=''for i in range(0,len(text),1):if int(text[i])^int(key[i])==1:cipher=cipher+'1'else:cipher=cipher+'0'return cipherText=input("please input a binary plain text:")Key=input("please input a binary key:")print("ciphertext:",levelEncrypt(Text,Key))②运行结果截图:实验6-3 校验码1.实验任务与目的(1)编程实现奇偶校验编码功能。
(2)编程实现CRC编码功能。
通过本实验理解检错和纠错编码的基本概念及一般方法:联系实现纠错功能的程序设计方法。
2.实验步骤与运行结果(1)编辑并运行程序:给二进制数添加奇偶校验位。
①源代码:strBinary=input('请输入一个8位二进制数:')Number=0for i in range(0,8,1):if strBinary[i]=='1':Number=Number+1if Number%2==0:oddCode=strBinary+'1'else:oddCode=strBinary+'0'print('添加了奇校验位的编码:',oddCode)if Number%2==0:evenCode=strBinary+'0'else:evenCode=strBinary+'1'print('添加了偶校验位的编码:',evenCode)②运行结果截图:(2)编辑并运行程序:实现CRC编码①源代码:def binTOdec(strBinary):nDecimal=0n=len(strBinary)for i in range(0,n,1):nDecimal=nDecimal+int(strBinary[i])*(2**(n-1-i)) return nDecimaldef decTObin(nDecimal):strBinary=''while nDecimal!=0:strBinary=str(nDecimal%2)+strBinarynDecimal=nDecimal//2return strBinarydef toKadd1(String,kadd1):n=len(String)for i in range(0,kadd1-n,1):String='0'+Stringreturn StringbinaryData=input('请输入10位二进制数:')Dividend=binaryData+'0000'genaratePoly=19p=0j=0Remainder=''while p<14:j=5-len(Remainder)if p+j<14:Remainder=decTObin(binTOdec(Remainder+Dividend[p:p+j])^genaratePoly) else:Remainder=Remainder+Dividend[p:p+j]p=p+jprint('CRC校验码:',binaryData+toKadd1(Remainder,4))②运行结果截图:上机环境1.计算机硬件处理器:Intel(R)Core(TM)i5-4210H CPU @2.90GHz 2.90GHz安装内存(RAM):4.00GB系统类型:64位操作系统2.计算机软件操作系统:Windows 10 家庭中文版使用软件:Python3.1 Microsoft Office WPS Office实验总结“实验6-编码”的程序长,运行原理复杂,在长语句的使用时经常出现错误而运行失败。