Python——二进制4位乘法器(采用原码一位乘计算原理实现)(tkinter实现)

本功能作者:
1.CSDN用户(TDTX),TDTX主页
   博客园用户(TDTX),TDTX主页

【原码一位乘】逻辑右移与加法运算组合实现,其中加法运算采用前面加法器中介绍的实现过程。

1.mupym4bit.py

def mupym4bit():
    import tkinter as tk
    wind1=tk.Tk()
    wind1.geometry('650x650+450+50')
    wind1.title('四位乘法器(原码一位乘)')

	#下面是清除所有文本框内容的函数功能
    def detxt():
        tx1.delete(0.0,tk.END)
        tx2.delete(0.0,tk.END)
        tx3.delete(0.0,tk.END)
        tx4.delete(0.0,tk.END)
        tx5.delete(0.0,tk.END)
        tx6.delete(0.0,tk.END)
        tx7.delete(0.0,tk.END)
        tx8.delete(0.0,tk.END)
        tx9.delete(0.0,tk.END)
        txbfj1.delete(0.0,tk.END)
        txcc1.delete(0.0,tk.END)
        txsm1.delete(0.0,tk.END)
        txbfj2.delete(0.0,tk.END)
        txcc2.delete(0.0,tk.END)
        txsm2.delete(0.0,tk.END)
        txbfj3.delete(0.0,tk.END)
        txcc3.delete(0.0,tk.END)
        txsm3.delete(0.0,tk.END)
        txbfj4.delete(0.0,tk.END)
        txcc4.delete(0.0,tk.END)
        txsm4.delete(0.0,tk.END)
        txbfj5.delete(0.0,tk.END)
        txcc5.delete(0.0,tk.END)
        txsm5.delete(0.0,tk.END)
        
    def mupym44bit():
        nu1_1=eval(ent4.get())   #获得文本框中的数字保存到二进制数每一位的变量中
        nu1_2=eval(ent3.get())
        nu1_3=eval(ent2.get())
        nu1_4=eval(ent1.get())
        nu1_11=nu1_1
        nu1_22=nu1_2
        nu1_33=nu1_3
        nu1_44=0
        
        nu2_1=eval(ent8.get())
        nu2_2=eval(ent7.get())
        nu2_3=eval(ent6.get())
        nu2_4=eval(ent5.get())
        nu2_11=nu2_1
        nu2_22=nu2_2
        nu2_33=nu2_3
        nu2_44=0

        nubfj1=0    #二进制数部分积每一位的初始值
        nubfj2=0
        nubfj3=0
        nubfj4=0


        nusp1=0
        nusp2=0
        nusp3=0
        nusp4=0
        nujw=0

        txcc1.insert('insert','{0} {1} {2} {3}'.format(nu2_44,nu2_33,nu2_22,nu2_11))
        #下面是根据乘数最低位是1还是0,给部分积加上被乘数或者加上0,并产生步骤说明
        if nu2_11==1:
            txbfj1.insert('insert',' {0} {1} {2} {3}\n'.format(0,0,0,0))
            txbfj1.insert('insert','+{0} {1} {2} {3}'.format(0,nu1_33,nu1_22,nu1_11))
            txsm1.insert('insert','开始部分积Z0=0\n乘数为1,加上x*')
        elif nu2_11==0:
            txbfj1.insert('insert',' {0} {1} {2} {3}\n'.format(0,0,0,0))
            txbfj1.insert('insert','+{0} {1} {2} {3}'.format(0,0,0,0))
            txsm1.insert('insert','开始部分积Z0=0\n乘数为0,加上0')

        if nu2_11==0:
            nu1_11=0
            nu1_22=0
            nu1_33=0
            nu1_44=0
        elif nu2_11==1:
            nu1_11=nu1_1
            nu1_22=nu1_2
            nu1_33=nu1_3
            nu1_44=0
        
        #下面开始部分积和被乘数或0作二进制加法运算
        if nubfj1+nu1_11+nujw==1:
            nusp1=1
            nujw=0
        elif nubfj1+nu1_11+nujw==0:
            nusp1=0
            nujw=0
        elif nubfj1+nu1_11+nujw==2:
            nusp1=0
            nujw=1
        elif nubfj1+nu1_11+nujw==3:
            nusp1=1
            nujw=1

        
        if nubfj2+nu1_22+nujw==1:
            nusp2=1
            nujw=0
        elif nubfj2+nu1_22+nujw==0:
            nusp2=0
            nujw=0
        elif nubfj2+nu1_22+nujw==2:
            nusp2=0
            nujw=1
        elif nubfj2+nu1_22+nujw==3:
            nusp2=1
            nujw=1


        if nubfj3+nu1_33+nujw==1:
            nusp3=1
            nujw=0
        elif nubfj3+nu1_33+nujw==0:
            nusp3=0
            nujw=0
        elif nubfj3+nu1_33+nujw==2:
            nusp3=0
            nujw=1
        elif nubfj3+nu1_33+nujw==3:
            nusp3=1
            nujw=1

            
        if nubfj4+nu1_44+nujw==1:
            nusp4=1
            nujw=0
        elif nubfj4+nu1_44+nujw==0:
            nusp4=0
            nujw=0
        elif nubfj4+nu1_44+nujw==2:
            nusp4=0
            nujw=1
        elif nubfj4+nu1_44+nujw==3:
            nusp4=1
            nujw=1

        #将加法的结果作为新的部分积插入到文本框中,即将进行逻辑右移
        txbfj2.insert('insert',' {0} {1} {2} {3}\n'.format(nusp4,nusp3,nusp2,nusp1))

		#下面是乘数的逻辑右移,并最高位是部分移出的最低位数字
        nu2_11=nu2_22
        nu2_22=nu2_33
        nu2_33=nu2_44
        nu2_44=nusp1

        txcc2.insert('insert','{0} {1} {2} {3}'.format(nu2_44,nu2_33,nu2_22,nu2_11))

            
        if nu2_11==0:
            nu1_11=0
            nu1_22=0
            nu1_33=0
            nu1_44=0
        elif nu2_11==1:
            nu1_11=nu1_1
            nu1_22=nu1_2
            nu1_33=nu1_3
            nu1_44=0
           
        #下面是部分积的逻辑右移
        nubfj1=nusp2
        nubfj2=nusp3
        nubfj3=nusp4
        nubfj4=0
        txbfj2.insert('insert',' {0} {1} {2} {3}\n'.format(nubfj4,nubfj3,nubfj2,nubfj1))
        
		#下面是根据乘数最低位是1还是0,给部分积加上被乘数或者加上0,并产生步骤说明
        if nu2_11==1:
            txbfj2.insert('insert','+{0} {1} {2} {3}'.format(0,nu1_33,nu1_22,nu1_11))
            txsm2.insert('insert','\n→1位得Z1,乘数同时→1位\n乘数为1,加上x*')
        elif nu2_11==0:
            txbfj2.insert('insert','+{0} {1} {2} {3}'.format(0,0,0,0))
            txsm2.insert('insert','\n→1位得Z1,乘数同时→1位\n乘数为0,加上0')

        if nubfj1+nu1_11+nujw==1:
            nusp1=1
            nujw=0
        elif nubfj1+nu1_11+nujw==0:
            nusp1=0
            nujw=0
        elif nubfj1+nu1_11+nujw==2:
            nusp1=0
            nujw=1
        elif nubfj1+nu1_11+nujw==3:
            nusp1=1
            nujw=1

        
        if nubfj2+nu1_22+nujw==1:
            nusp2=1
            nujw=0
        elif nubfj2+nu1_22+nujw==0:
            nusp2=0
            nujw=0
        elif nubfj2+nu1_22+nujw==2:
            nusp2=0
            nujw=1
        elif nubfj2+nu1_22+nujw==3:
            nusp2=1
            nujw=1


        if nubfj3+nu1_33+nujw==1:
            nusp3=1
            nujw=0
        elif nubfj3+nu1_33+nujw==0:
            nusp3=0
            nujw=0
        elif nubfj3+nu1_33+nujw==2:
            nusp3=0
            nujw=1
        elif nubfj3+nu1_33+nujw==3:
            nusp3=1
            nujw=1

            
        if nubfj4+nu1_44+nujw==1:
            nusp4=1
            nujw=0
        elif nubfj4+nu1_44+nujw==0:
            nusp4=0
            nujw=0
        elif nubfj4+nu1_44+nujw==2:
            nusp4=0
            nujw=1
        elif nubfj4+nu1_44+nujw==3:
            nusp4=1
            nujw=1

            
        txbfj3.insert('insert',' {0} {1} {2} {3}\n'.format(nusp4,nusp3,nusp2,nusp1))

        nu2_11=nu2_22
        nu2_22=nu2_33
        nu2_33=nu2_44
        nu2_44=nusp1

        txcc3.insert('insert','{0}\n{1} {2} {3} {4}'.format(nu2_2,nu2_44,nu2_33,nu2_22,nu2_11))

        if nu2_11==0:
            nu1_11=0
            nu1_22=0
            nu1_33=0
            nu1_44=0
        elif nu2_11==1:
            nu1_11=nu1_1
            nu1_22=nu1_2
            nu1_33=nu1_3
            nu1_44=0
        
        nubfj1=nusp2
        nubfj2=nusp3
        nubfj3=nusp4
        nubfj4=0
        txbfj3.insert('insert',' {0} {1} {2} {3}\n'.format(nubfj4,nubfj3,nubfj2,nubfj1))
        if nu2_11==1:
            txbfj3.insert('insert','+{0} {1} {2} {3}'.format(0,nu1_33,nu1_22,nu1_11))
            txsm3.insert('insert','\n→1位得Z2,乘数同时→1位\n乘数为1,加上x*')
        elif nu2_11==0:
            txbfj3.insert('insert','+{0} {1} {2} {3}'.format(0,0,0,0))
            txsm3.insert('insert','\n→1位得Z2,乘数同时→1位\n乘数为0,加上0')
            

        if nubfj1+nu1_11+nujw==1:
            nusp1=1
            nujw=0
        elif nubfj1+nu1_11+nujw==0:
            nusp1=0
            nujw=0
        elif nubfj1+nu1_11+nujw==2:
            nusp1=0
            nujw=1
        elif nubfj1+nu1_11+nujw==3:
            nusp1=1
            nujw=1

        
        if nubfj2+nu1_22+nujw==1:
            nusp2=1
            nujw=0
        elif nubfj2+nu1_22+nujw==0:
            nusp2=0
            nujw=0
        elif nubfj2+nu1_22+nujw==2:
            nusp2=0
            nujw=1
        elif nubfj2+nu1_22+nujw==3:
            nusp2=1
            nujw=1


        if nubfj3+nu1_33+nujw==1:
            nusp3=1
            nujw=0
        elif nubfj3+nu1_33+nujw==0:
            nusp3=0
            nujw=0
        elif nubfj3+nu1_33+nujw==2:
            nusp3=0
            nujw=1
        elif nubfj3+nu1_33+nujw==3:
            nusp3=1
            nujw=1

            
        if nubfj4+nu1_44+nujw==1:
            nusp4=1
            nujw=0
        elif nubfj4+nu1_44+nujw==0:
            nusp4=0
            nujw=0
        elif nubfj4+nu1_44+nujw==2:
            nusp4=0
            nujw=1
        elif nubfj4+nu1_44+nujw==3:
            nusp4=1
            nujw=1

            
        txbfj4.insert('insert',' {0} {1} {2} {3}\n'.format(nusp4,nusp3,nusp2,nusp1))

        nu2_11=nu2_22
        nu2_22=nu2_33
        nu2_33=nu2_44
        nu2_44=nusp1

        txcc4.insert('insert','{0} {1}\n{2} {3} {4} {5}'.format(nu2_3,nu2_2,nu2_44,nu2_33,nu2_22,nu2_11))

        if nu2_11==0:
            nu1_11=0
            nu1_22=0
            nu1_33=0
            nu1_44=0
        elif nu2_11==1:
            nu1_11=nu1_1
            nu1_22=nu1_2
            nu1_33=nu1_3
            nu1_44=0
        
        nubfj1=nusp2
        nubfj2=nusp3
        nubfj3=nusp4
        nubfj4=0
        txbfj4.insert('insert',' {0} {1} {2} {3}\n'.format(nubfj4,nubfj3,nubfj2,nubfj1))
        if nu2_11==1:
            txbfj4.insert('insert','+{0} {1} {2} {3}'.format(0,nu1_33,nu1_22,nu1_11))
            txsm4.insert('insert','\n→1位得Z3,乘数同时→1位\n乘数为1,加上x*')
        elif nu2_11==0:
            txbfj4.insert('insert','+{0} {1} {2} {3}'.format(0,0,0,0))
            txsm4.insert('insert','\n→1位得Z3,乘数同时→1位\n乘数为0,加上0')
            
        if nubfj1+nu1_11+nujw==1:
            nusp1=1
            nujw=0
        elif nubfj1+nu1_11+nujw==0:
            nusp1=0
            nujw=0
        elif nubfj1+nu1_11+nujw==2:
            nusp1=0
            nujw=1
        elif nubfj1+nu1_11+nujw==3:
            nusp1=1
            nujw=1

        
        if nubfj2+nu1_22+nujw==1:
            nusp2=1
            nujw=0
        elif nubfj2+nu1_22+nujw==0:
            nusp2=0
            nujw=0
        elif nubfj2+nu1_22+nujw==2:
            nusp2=0
            nujw=1
        elif nubfj2+nu1_22+nujw==3:
            nusp2=1
            nujw=1


        if nubfj3+nu1_33+nujw==1:
            nusp3=1
            nujw=0
        elif nubfj3+nu1_33+nujw==0:
            nusp3=0
            nujw=0
        elif nubfj3+nu1_33+nujw==2:
            nusp3=0
            nujw=1
        elif nubfj3+nu1_33+nujw==3:
            nusp3=1
            nujw=1

            
        if nubfj4+nu1_44+nujw==1:
            nusp4=1
            nujw=0
        elif nubfj4+nu1_44+nujw==0:
            nusp4=0
            nujw=0
        elif nubfj4+nu1_44+nujw==2:
            nusp4=0
            nujw=1
        elif nubfj4+nu1_44+nujw==3:
            nusp4=1
            nujw=1

        txbfj5.insert('insert',' {0} {1} {2} {3}\n'.format(nusp4,nusp3,nusp2,nusp1))

        nu2_11=nu2_22
        nu2_22=nu2_33
        nu2_33=nu2_44
        nu2_44=nusp1
        txcc5.insert('insert','0 {0} {1}\n{2} {3} {4} {5}'.format(nu2_3,nu2_2,nu2_44,nu2_33,nu2_22,nu2_11))

        nubfj1=nusp2
        nubfj2=nusp3
        nubfj3=nusp4
        nubfj4=0
        txbfj5.insert('insert',' {0} {1} {2} {3}\n'.format(nubfj4,nubfj3,nubfj2,nubfj1))

        txsm5.insert('insert','\n→1位得Z4,乘数已全部移出')

		#下面是两个4bit二进制数符号位作异或运算,结果是两数相乘结果数的最高位,即符号位
        if nu1_4==nu2_4:
            tx8.insert('insert',nubfj4)
            tx9.insert('insert',"{0}异或{1}是:0\n即为第八位符号位!".format(nu1_4,nu2_4))
        else:
            tx8.insert('insert',1)
            tx9.insert('insert',"{0}异或{1}是:1\n即为第八位符号位!".format(nu1_4,nu2_4))
        tx7.insert('insert',nubfj3)
        tx6.insert('insert',nubfj2)
        tx5.insert('insert',nubfj1)
        tx4.insert('insert',nu2_44)
        tx3.insert('insert',nu2_33)
        tx2.insert('insert',nu2_22)
        tx1.insert('insert',nu2_11)

	#下面是界面上各组件的生成与排布
    lb1=tk.Label(wind1,text='输入数字1:',fg='blue',width=10,height=2)
    lb1.place(x=100,y=30)
    ent1=tk.Entry(wind1,width=3)
    ent1.place(x=120,y=60)
    ent2=tk.Entry(wind1,width=3)
    ent2.place(x=146,y=60)
    ent3=tk.Entry(wind1,width=3)
    ent3.place(x=172,y=60)
    ent4=tk.Entry(wind1,width=3)
    ent4.place(x=198,y=60)

    lbs=tk.Label(wind1,text='x',font=('Arial', 14))
    lbs.place(x=240,y=60)

    lb2=tk.Label(wind1,text='输入数字2:',fg='blue',width=10,height=2)
    lb2.place(x=235,y=30)
    ent5=tk.Entry(wind1,width=3)
    ent5.place(x=276,y=60)
    ent6=tk.Entry(wind1,width=3)
    ent6.place(x=302,y=60)
    ent7=tk.Entry(wind1,width=3)
    ent7.place(x=328,y=60)
    ent8=tk.Entry(wind1,width=3)
    ent8.place(x=354,y=60)

    bt1=tk.Button(wind1,text='相乘',command=mupym44bit)
    bt1.place(x=440,y=60)

    btdetx=tk.Button(wind1,text='清除',command=detxt)
    btdetx.place(x=500,y=60)
    
    lbsm=tk.Label(wind1,text='部分积\t\t\t乘数\t\t\t说明',fg='blue',height=2)
    lbsm.place(x=100,y=90)

    lbfj1=tk.Label(wind1,text='-----------------------------------------------------------------------------------------------------------------')
    lbfj1.place(x=60,y=120)

    txbfj1=tk.Text(wind1,width=22,height=4)
    txbfj1.place(x=100,y=135)
    txcc1=tk.Text(wind1,width=22,height=4)
    txcc1.place(x=270,y=135)
    txsm1=tk.Text(wind1,width=25,height=4)
    txsm1.place(x=440,y=135)

    txbfj2=tk.Text(wind1,width=22,height=4)
    txbfj2.place(x=100,y=190)
    txcc2=tk.Text(wind1,width=22,height=4)
    txcc2.place(x=270,y=190)
    txsm2=tk.Text(wind1,width=25,height=4)
    txsm2.place(x=440,y=190)

    txbfj3=tk.Text(wind1,width=22,height=4)
    txbfj3.place(x=100,y=245)
    txcc3=tk.Text(wind1,width=22,height=4)
    txcc3.place(x=270,y=245)
    txsm3=tk.Text(wind1,width=25,height=4)
    txsm3.place(x=440,y=245)

    txbfj4=tk.Text(wind1,width=22,height=4)
    txbfj4.place(x=100,y=300)
    txcc4=tk.Text(wind1,width=22,height=4)
    txcc4.place(x=270,y=300)
    txsm4=tk.Text(wind1,width=25,height=4)
    txsm4.place(x=440,y=300)

    txbfj5=tk.Text(wind1,width=22,height=4)
    txbfj5.place(x=100,y=355)
    txcc5=tk.Text(wind1,width=22,height=4)
    txcc5.place(x=270,y=355)
    txsm5=tk.Text(wind1,width=25,height=4)
    txsm5.place(x=440,y=355)

    lbfj2=tk.Label(wind1,text='-----------------------------------------------------------------------------------------------------------------')
    lbfj2.place(x=60,y=410)

    lb4=tk.Label(wind1,text='数字1x数字2结果为(原码):')
    lb4.place(x=90,y=425)

    tx1=tk.Text(wind1,width=3,height=1)
    tx1.place(x=302,y=450)

    tx2=tk.Text(wind1,width=3,height=1)
    tx2.place(x=276,y=450)

    tx3=tk.Text(wind1,width=3,height=1)
    tx3.place(x=250,y=450)

    tx4=tk.Text(wind1,width=3,height=1)
    tx4.place(x=224,y=450)

    tx5=tk.Text(wind1,width=3,height=1)
    tx5.place(x=198,y=450)

    tx6=tk.Text(wind1,width=3,height=1)
    tx6.place(x=172,y=450)

    tx7=tk.Text(wind1,width=3,height=1)
    tx7.place(x=146,y=450)

    tx8=tk.Text(wind1,width=3,height=1)
    tx8.place(x=120,y=450)

    tx9=tk.Text(wind1,width=13,height=4)
    tx9.place(x=350,y=450)
    
    lbbs=tk.Label(wind1,text='7     6     5     4     3     2     1     0',fg='red')
    lbbs.place(x=120,y=468)

    wind1.mainloop()

2.结果示例

在这里插入图片描述
【重新输入数据后,点击“清除”按钮,会删除掉所有输出文本框中的内容!】

在这里插入图片描述

posted @ 2021-11-23 16:46  TDTX  阅读(303)  评论(0编辑  收藏  举报