簡體   English   中英

使用加法器電路將兩個4位二進制數相加

[英]Adding two 4 digit binary numbers using adder circuits

我需要編寫一個程序,使用加法器電路將兩個4位二進制數字相加,從而給我們一個答案。 我需要使用int()函數將其從二進制轉換為十進制(兩個值都需要顯示)。 我正在努力創建可創建正確的二進制和十進制輸出的代碼。 我們要輸入2組四位數。 這些數字應該通過電路,並且結果應加在一起。 現在,大門很不錯。 但是,我無法將數字正確地加在一起。 通過電路的二進制加法不正確。 轉換器和輸入均正確。

用戶需要為x(x1,x2,x3,x4)和y(y1,y2,y3,y4)輸入4位二進制數,並且ci =0。例如:x = 1111 y = 0000

這是加法器電路,以及顯示兩個二進制數字相加后電路外觀的示意圖(我還不能嵌入圖像)

這是我當前的代碼:

import string

print('Simple Circuits for Final Project')

x = input('x| enter 4 digits: ')
y = input('y| enter 4 digits: ')

list1 = list(x)
print(list1)

list2 = list(y)
print(list2)



#define all the gates
def xorgate(x,y):
    if x == y:
        return '0'
    else:
        return '1'

def andgate (x,y):
    if x == '1' and y == '1':
        return '1'
    else:
        return '0'

def orgate (x,y):
    if x == '1' or y == '1':
        return '1'
    else:
        return '0'

#define the entire circuit and input the list of inputs for the circuit.
#include the outputs based on the gates defined above.
def circuit(x,y,ci):
    a = 3 #starting value for the while loop, get approp position for our list
    adder = ['0','0','0','0','0']#adder list only has strings of zero. list to hold binary numbers from the output of the adding circuit
    b = 4
    while a >= 0:



        xorout = xorgate(x[a],y[a])
        print("xor:",(xorout))
        s = xorgate(ci,xorout)
        print("Ci:",ci)
        andout = andgate(xorout, ci)
        print("and1:",andout)
        and2out = andgate(x[a],y[a])
        print("and2:",and2out)
        co = orgate(andout,and2out)

        print('s:',s)
        print('co:',co)
        print('-----------------')

        ci = co
        adder[b] = str(s)
        a-=1
        b-=1


    adder[4]=ci
    #print(final)
    print('The Final Binary Output is:', adder[::-1])

    #OUR CONVERTER IS RIGHT, BUT WE CAN'T GET THE BINARY ADDITION ACCURATE
    outputfinal = ((int(adder[4]) * 16) + (int(adder[3]) * 8)+(int(adder[2]) * 4)+(int(adder[1]) * 2)+(int(adder[0]) * 1))
    print('The Final Decimal Output is:', outputfinal)
hold = '0'
circuit(list1,list2,hold)# 3 value for circuit

這是我們認為是錯誤的部分:

    ci = co
    adder[b] = str(s)
    a-=1
    b-=1


adder[4]=ci
#print(final)
print('The Final Binary Output is:', adder[::-1])

這是我當前的輸出結果,這是錯誤的:

x| enter 4 digits: 1111
y| enter 4 digits: 0000
['1', '1', '1', '1']
['0', '0', '0', '0']
xor: 1
Ci: 0
and1: 0
and2: 0
s: 1
co: 0
-----------------
xor: 1
Ci: 0
and1: 0
and2: 0
s: 1
co: 0
-----------------
xor: 1
Ci: 0
and1: 0
and2: 0
s: 1
co: 0
-----------------
xor: 1
Ci: 0
and1: 0
and2: 0
s: 1
co: 0
-----------------
The Final Binary Output is: ['0', '1', '1', '1', '0']
The Final Decimal Output is: 14

您混淆了加法器列表的順序。

應該是第一個地址是0,而不是4:

adder[0]=ci
#print(final)

暫時不要反轉清單

print('The Final Binary Output is:', adder)

由於您的轉換器期望其順序相反,因此在這里我將其反轉而不是重寫您的轉換器:

adder = adder[::-1]
#OUR CONVERTER IS RIGHT, BUT WE CAN'T GET THE BINARY ADDITION ACCURATE
outputfinal = ((int(adder[4]) * 16) + (int(adder[3]) * 8)+(int(adder[2]) * 4)+(int(adder[1]) * 2)+(int(adder[0]) * 1))
print('The Final Decimal Output is:', outputfinal)

您可以在在線repl中試用整個程序(因為得到不同的結果): https : //repl.it/repls/CompetitiveSerpentineAccess

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM