西南大学
计算机与信息科学学院
课程设计报告
课 程: 编程导论
题 目: ISBN条码
班 级: 2015 级 计算机类 专业 3 班 大一上期
学生姓名: 余
指导教师: 周竹荣
提交日期: 2015 年 12 月 日
一、概述(设计主要完成的任务)
利用随机函数自动生成一个13位ISBN号,满足检验码规则。即校验符满足以下要求:12个数据符从左起将所有的偶数位相加的和乘以3加上所有奇数位得到一个数a,用10减去数a的个位数,如果结果不为10则检验符为结果本身,反之则为0。利用turtle画出该条形码。
|
||
二、算法描述
1.利用turtle定义一个写条形码数字的函数:给出数字,在给定的位置写出该数。 2.书的前3位为“978”,用随机数产生第4-12位数值,并按照校验码的计算方法算出前十二位的和。 3.计算校验码,对末位为0的情况进行处理。 4.根据EAN奇偶性编码字典,确定左手字符奇偶性,创建右手数位编码字典,创建左手奇数性编码字典,创建左手偶数性编码字典。 5.获得第2-7数位奇偶性,并将其制成元组作为字典的键。 6.编制条形码,添加警戒码num_string=”101”,获得左手数位条码总和并添加到num_string 7.利用函数写下条形码的13位数值。 8.对二进制符遍历,画出竖线。
|
||
三、源代码 import turtle import random #为在条形码写数字时定义一个海龟画图函数 def write_number(num,position): turtle.speed(10) turtle.penup() turtle.goto(position,-95) turtle.pendown() turtle.write(num) turtle.penup() a=9 b=7 c=8 d=random.randint(0,9) e=random.randint(0,9) f=random.randint(0,9) g=random.randint(0,9) h=random.randint(0,9) i=random.randint(0,9) j=random.randint(0,9) k=random.randint(0,9) l=random.randint(0,9) total=a+b*3+c+d*3+e+f*3+g+h*3+i+j*3+k+l*3 #对末尾数为零的情况进行处理 while True: if total%10==0: continue if total%10!=0: #获得第13位值 m=10-total%10 #EAN奇偶性编码字典,确定左手字符奇偶性 EAN_parity_encoding_Watch_dict=\ {0:["odd","odd","odd","odd","odd","odd"],\ 1:["odd","odd","even","odd","even","even"],\ 2:["odd","odd","even","even","odd","even"],\ 3:["odd","odd","even","even","even","odd"],\ 4:["odd","even","odd","odd","even","even"],\ 5:["odd","even","even","odd","odd","even"],\ 6:["odd","even","even","even","odd","odd"],\ 7:["odd","even","odd","even","odd","even"],\ 8:["odd","even","odd","even","even","odd"],\ 9:["odd","even","even","odd","even","odd"]} #创建右手数位编码字典 righthand_encoding_dict=\ {0:"1110010",\ 1:"1100110",\ 2:"1101100",\ 3:"1000010",\ 4:"1011100",\ 5:"1001110",\ 6:"1010000",\ 7:"1000100",\ 8:"1001000",\ 9:"1110100"} #创建左手奇数性编码字典 lefthand_odd_parity_encoding_dict=\ {(0,"odd"):"0001101",\ (1,"odd"):"0011001",\ (2,"odd"):"0010011",\ (3,"odd"):"0111101",\ (4,"odd"):"0100011",\ (5,"odd"):"0110001",\ (6,"odd"):"0101111",\ (7,"odd"):"0111011",\ (8,"odd"):"0110111",\ (9,"odd"):"0001011"} #创建左手偶数性编码字典 lefthand_even_parity_encoding_dict=\ {(0,"even"):"0100111",\ (1,"even"):"0110011",\ (2,"even"):"0011011",\ (3,"even"):"0100001",\ (4,"even"):"0011101",\ (5,"even"):"0111001",\ (6,"even"):"0000101",\ (7,"even"):"0010001",\ (8,"even"):"0001001",\ (9,"even"):"0010111"} #获得2-7数位奇偶性,并将其制成元组作为字典的键,便于翻译成二进制 num_parity_tuple_list=[] parity=EAN_parity_encoding_Watch_dict[a] num_list=[b,c,d,e,f,g] n=0 while n<=5: num_parity_tuple=(num_list[n],parity[n]) num_parity_tuple_list.append(num_parity_tuple) n+=1 #编制条码,先添加警戒码101 num_string="101" #获得左手数位条码总和并添加到num_string n=0 for x in num_parity_tuple_list: if x in lefthand_even_parity_encoding_dict: num_string+=lefthand_even_parity_encoding_dict[x] if x in lefthand_odd_parity_encoding_dict: num_string+=lefthand_odd_parity_encoding_dict[x] n+=1 num_string+="01010" num_string+=righthand_encoding_dict[h]+\ righthand_encoding_dict[i]+\ righthand_encoding_dict[j]+\ righthand_encoding_dict[k]+\ righthand_encoding_dict[l]+\ righthand_encoding_dict[m] num_string+="101" time=1 x=0 #写下条形码的13位数值 write_number(a,-10) write_number(b,3) write_number(c,9) write_number(d,15) write_number(e,22) write_number(f,29) write_number(g,35) write_number(h,41) write_number(i,57) write_number(j,64) write_number(k,71) write_number(l,78) write_number(m,85) turtle.goto(0,0) #对二进制符遍历,画出竖线 for number in num_string: turtle.penup() #左警戒条 if time<=3: if number=="1": turtle.pendown() turtle.goto(x,-100) turtle.penup() if number=="0": turtle.goto(x,-100) #中间警戒条 elif 46<=time<=50: if number=="1": turtle.pendown() turtle.goto(x,-100) turtle.penup() if number=="0": turtle.goto(x,-100) #右警戒条 elif 93<=time<=95: if number=="1": turtle.pendown() turtle.goto(x,-100) turtle.penup() if number=="0": turtle.goto(x,-100) #第二位数制位以及其它产品码数位 else: if number=="1": turtle.pendown() turtle.goto(x,-80) turtle.penup() if number=="0": turtle.goto(x,-80) turtle.goto(x+1,0) x+=1 time+=1 break print("随机生成的书码为:",a,b,c,d,e,f,g,h,i,j,k,l,m)
|
||
四、简要的使用说明
通过运行代码,得到一个条形码,用微信扫描改条形码,得到该条形码的相关信息。效果图如下:
用微信扫描的结果:
周竹荣,博士,计算机与信息科学学院,计算机科学与技术系主任 硕士导师
|