5、(20 points)This programming assignment can be done by you alone or a group of two students. 写下合作同学的名字。
(A)习题2.10。试验x=128, y=8; x=129, y=8; x=1, y=12; x=2047, y=12 这些-x 的补码二进制值。
def TtoB(num):
L=[]
if num < 0: num = 0-num
while num>0:
i = num % 2
L = [i]+L
num = num//2
return L
def complement(L):
for i in range(len(L)):
if L[i]==0:L[i]=1
else : L[i]=0
L[len(L)-1]+=1
for i in range(len(L)-1,0,-1):
if L[i]==2:
L[i]=0
L[i-1]+=1
if L[0]==2:
L[0]=1
L=[1]+L
return L
a = int(input("请输入一个整数\n"))
b = int(input("请再输入一个整数\n"))
L1 = TtoB(a)
L1 = complement(L1)
if(len(L1)<=b):
for i in range(len(L1)):
print(L1[i],end="")
(B)类似习题 2.7,八位元CPU。但是输入x, y 是介于-128 到127间的任意整数。转为二进制后,做加法,如果有溢出,需要返回错误,否则返回十进制的结果。此题的重点是负数补码,二进制加法,和判断溢出。判断溢出必须用书上的方法,在二进制的结果上直接判断。
请同学试验各种情况,例如,64+65, 100+10, 100+(-128),10+(-100), (-30)+(-100), (-127)+127
def TtoB(num):
L=[]
while num>0:
i = num % 2
L = [i]+L
num = num//2
while(len(L)<8):L=[0] + L
return L
def complement(L):
for i in range(7):
if L[i]==0:L[i]=1
else : L[i]=0
L[len(L)-1]+=1
L[0]=1
for i in range(len(L)-1,0,-1):
if L[i]==2:
L[i]=0
L[i-1]+=1
if L[0]==2:L[0]=1
return L
def add(x,y):
L=[]
C=0
for i in range(len(x)-1,-1,-1):
if ((x[i]==1 and y[i]==0 and C==0)or(x[i]==0 and y[i]==0 and C==1)or(x[i]==0 and y[i]==1 and C==0)or(x[i]==1 and y[i]==1 and C == 1)):
S=1
else:
S=0
if ((x[i]==1 and y[i]==1 and C==1)or(x[i]==1 and y[i] ==0 and C==1)or(x[i]==1 and y[i] == 1 and C==0)or(x[i]==0 and y[i]==1 and C==1)):
C=1
else:
C=0
L=[S]+L
#print(L)
return C,L
"""
def standard(num):
if num < 0:
L = TtoB(num)
L = complement(L)
#print(L)
else:
L = TtoB(num)
#print(L)
return L"""
def standard(L):
a=[]
if L[0]=="-":
a=L[1:]
a = int(a)
a = TtoB(a)
L = complement(a)
#print(L)
else:
a = int(L[:])
L = TtoB(a)
#print(L)
return L
a = (input("请输入一个介于-128 到127间的任意整数\n"))
b = (input("请再输入一个介于-128 到127间的任意整数\n"))
L1 = standard(a)
L2 = standard(b)
if L1[0]==L2[0] and L1[0]==0:
Q=L1[0]
C,L=add(L1,L2)
if L[0]==1:
print("数据溢出")
else:print(int(a)+int(b))
elif L1[0]==L2[0] and L1[0]==1:
C,L=add(L1,L2)
if L[0]==0:
print("数据溢出")
else:
print("-",end="")
a = int(a[1:])
b= int(b[1:])
print(a+b)
else:print(a+b)
6、(15 points)这题可以两人合作,写下合作同学的名字及学号。请同学们把你的代码拷贝到word报告中,并将程序的运行结果截图粘贴到代码下方。
输入两个任意长度的字符串代表两个浮点数,假设没有符号,都是正数,输出字符串代表此两浮点数的和。例如,输入“12.123”,“1.2”输出为“13.323”;又例如,输入为“12.1234567890123456789”,“1.987654321”,输出为其和“14.1111111100123456789”。请你尽量利用Python整数可以任意多位的特性来完成此程序。你可以将一个浮点数分成两部分整数:小数点前和小数后,再分别处理。
def SPLIT(a):
u1,u2=a.split(".",1)
L1 = []
L2= []
for i in range(len(u1)):
L1 = L1 + [int(u1[i])]
for i in range(len(u2)):
L2 = L2 + [int(u2[i])]
#print(L1,L2)
return L1,L2
def Dadd(L1,L2):
L=[]
C=0
while len(L1)<len(L2):L1+=[0]
while len(L2)<len(L1):L2+=[0]
for i in range(len(L1)-1,-1,-1):
a = L1[i]+L2[i]
L = [a] + L
for i in range(len(L)-1,0,-1):
while L[i] >= 10:
L[i]-=10
L[i-1]+=1
while L[0] >= 10:
L[0] -= 10
C+=1
#print(L)
#print(C)
return L,C
def Iadd(L1,L2,C):
L=[]
while len(L1)<len(L2):L1=[0]+L1
while len(L2)<len(L1):L2=[0]+L2
a = L1[len(L1)-1] + L2[len(L2)-1] + C
L+=[a]
for i in range(len(L1)-2,-1,-1):
a = L1[i]+L2[i]
L = [a] + L
for i in range(len(L)-1,0,-1):
while L[i] >= 10:
L[i]-=10
L[i-1]+=1
a=0
while L[0] >= 10:
L[0] -= 10
a+=1
if a != 0:
L=[a]+L
#print(L)
return L
a=input("inter")
b=input("inter")
A1,A2 = SPLIT(a)
B1,B2 = SPLIT(b)
Dsum,judge = Dadd(A2,B2)
Isum = Iadd(A1,B1,judge)
for i in range(len(Isum)):
print(Isum[i],end="")
print(".",end="")
for i in range(len(Dsum)):
print(Dsum[i],end="")
(挑战题额外10分)处理任意负数,和任意乘法。建议没时间就跳过,有时间则练习,试试看,请告知我们你的程序能处理什么样的情况,例如加法,减法,什么样的乘法?
def SPLIT(a):
u1,u2=a.split(".",1)
if u1[0]=="-":
u1 = u1[1:]
L1 = []
L2= []
for i in range(len(u1)):
L1 = L1 + [int(u1[i])]
for i in range(len(u2)):
L2 = L2 + [int(u2[i])]
#print(L1,L2)##
return L1,L2
def multD(L1,L2,L3,L4):
#乘法就是错位的加法,L3是L1小数部分,L4是L2小数部分
L = []
for i in range(len(L1)):
L+=[0]
for i in range(len(L1)-1,-1,-1):
temporary = []
for j in range(len(L2)-1,-1,-1):
temporary = [L2[j]*L1[i]]+temporary
#print(temporary)
while len(L) > len(temporary):
temporary= temporary+[0]
#print(temporary,"$11")
for j in range(len(temporary)):
L[j] += temporary[j]
#print(L,"42")
if i != 0:
L = [0]+L
#print(L)
for i in range(len(L)-1,0,-1):
while L[i] >= 10:
L[i]-=10
L[i-1]+=1
a=0
while L[0] >= 10:
L[0] -= 10
a+=1
if a != 0:
L=[a]+L
L5 = L[0:len(L1)+len(L2)-len(L3)-len(L4)-1]
L6 = L[len(L1)+len(L2)-len(L3)-len(L4)-1:]
return L5,L6
a=input("inter")
b=input("inter")
A1,A2 = SPLIT(a)
B1,B2 = SPLIT(b)
L1=A1+A2
L2=B1+B2
L5,L6=multD(L1,L2,A2,B2)
def PUT(L5,L6):
for i in range(len(L5)):
print(L5[i],end="")
print(".",end="")
for i in range(len(L6)):
print(L6[i],end="")
if (a[0]=="-" and b[0]!="-") or (a[0]!="-" and b[0]=="-"):
print("-",end="")
PUT(L5,L6)
else:PUT(L5,L6)
7、(15 points) Python编程,递归练习。这题要自己写,可以与同学讨论,但是不是合作,请同学们把你的代码拷贝到word报告中,并将程序的运行结果截图粘贴到代码下方。输入字符串X代表一个无符号二进制数,输出它的十进制值。例如,X=”101”,输出5。注意,你必须要用二分法递归方式!!试试X=”101”*10, X=”1100”*30
a = input("请输入一个二进制数")
L=[]
for i in range(len(a)):
L += [int(a[i])]
#print(L)
def BtoD(L):
if len(L)==1:return L
else:
L1 = L[:len(L)//2]
L2 = L[len(L)//2:]
for i in range(len(L1)):
L1[i] *= int(pow(2,len(L2)))
return(BtoD(L1)+BtoD(L2))
b=0
L = BtoD(L)
for i in range(len(L)):
b+=L[i]
print(b)
#print(L)
8.、(20 points)This programming assignment can be done by you alone or a group of two students. 写下合作同学的名字。
输入2个十进制的整数,你的程序将它们转为二进制后,编写二进制乘法函数,函数返回二进制值,再转为十进制输出。我们现在有正负和位数限制。假设CPU是N位(你的程序要适用给定任意N介于8到20),用补位法表示正负值,例如N=12,则可表示范围是-2048 到 2047 之间的任意值。输入2个十进制的整数,检查介于可表示范围内,就马上转为二进制表示,编写二进制乘法函数mult(),限制在N位,函数返回是否溢出,如果没有溢出返回正确的结果二进制值,再转为十进制输出。例如给定N=12时:
例如x=-12, y=100, 最后输出是 -1200。
例如 x=-30, y=-10, 最后输出是 300
例如,x=4, y=-512, 最后输出是 -2048
例如,x=4, y=512, 最后输出是 “溢出错误”
例如 x=40, y=-80, 最后输出是 “溢出错误”
函数Mult(a, b , N): 输入a, b 是补位后二进制(可以是字符串),N 代表最大限制位数。例如, N=12。 建议如果a, b都是负数,将它们转为正数,再做正数相乘 ;假如a正 b负,则a,b可以交换。让b作为正数,然后一位位看b的位是否0、1,需要时加上移位后的a,如同多个负数相加,只需留下N位部分,仍然能保持它正确的补位值。在函数内检查是否有溢出,绝对不能转为十进制来检查,这不是CPU内使用的方式,在CPU内的运算都是二进制,所以必须在二进制中检查。
def DtoB(num,N):
L=[]
while num>0:
i = num % 2
L = [i]+L
num = num//2
while(len(L)<N):L=[0] + L
#print(L)
return L
def complement(L,N):
for i in range(N):
if L[i]==0:L[i]=1
else : L[i]=0
L[len(L)-1]+=1
for i in range(len(L)-1,0,-1):
if L[i]==2:
L[i]=0
L[i-1]+=1
#print(L)
if L[0]==2:L[0]=1
#print(L)
return L
def judge(L,N):
if L[0]=="-":
a=L[1:]
a = int(a)
if a > int(pow(2,N-1)+0.5):print("数据溢出")
else:
L = DtoB(a,N)
L = complement(L,N)
else:
a=int(L)
if a > int(pow(2,N-1)-0.5):print("数据溢出")
else: L = DtoB(a,N)
#print(L)
return L
def comp(L):
for i in range(len(L)):
if L[i]==0:L[i]=1
else : L[i]=0
L[len(L)-1]+=1
for i in range(len(L)-1,0,-1):
if L[i]==2:
L[i]=0
L[i-1]+=1
#print(L)
if L[0]==2:L[0]=0
return L
def BtoD(L):
if len(L)==1:return L
else:
L1 = L[:len(L)//2]
L2 = L[len(L)//2:]
for i in range(len(L1)):
L1[i] *= int(pow(2,len(L2)))
return(BtoD(L1)+BtoD(L2))
def multive(L1,L2):
L=[]
while len(L1)<len(L2):
L1=[0]+L1
while len(L1)>len(L2):
L2=[0]+L2
for i in range(len(L1)):
L += [0]
for i in range(len(L1)):
temp = []
for j in range(len(L1)-1,i-1,-1):
temp = [L2[j]*L1[len(L1)-i-1]]+temp
#print(temp,"temp")
for j in range(len(L2)-i):
L[j]+=temp[j]
#print(L,"L")
for i in range(len(L)-1,0,-1):
while L[i]>2:
L[i]-=2
L[i-1]+=1
if L[0] > 0:
print("数据溢出")
else:
Q = 0
L = BtoD(L)
for i in range(len(L)):
Q+=L[i]
print(Q)
def mult(L1,L2):
if L2[0]==0 and L1[0]==0:
multive(L1,L2)
elif L1[0]==1 and L2[0]==0:
L1 = comp(L1)
print("-",end="")
multive(L1,L2)
elif L2[0]==1 and L1[0]==0:
L2 = comp(L2)
print("-",end="")
multive(L1,L2)
else:
L1 = comp(L1)
L2 = comp(L2)
multive(L1,L2)
#N = int(input())
N=int(input("请输入一个正整数表示二进制位数"))
a=input("请输入一个整数")
b=input("请输入一个整数")
L1=judge(a,N)
L2=judge(b,N)
mult(L1,L2)