1. 程式人生 > 其它 >邏輯閘的Python實現與簡單應用

邏輯閘的Python實現與簡單應用

邏輯閘的Python實現與簡單應用

基本的門

#coding=utf-8
def roc_nor(a, b):
    return  int(not(a or b))

def roc_nand(a, b):
    return  int(not(a and b))

if __name__ == "__main__":
    print("1與0與非:",roc_nand(1,0))
    print("0與1與非:",roc_nand(0,1))
    print("0與0與非:",roc_nand(0,0))
    print("1與1與非:",roc_nand(1,1))
    print("1與0或非:",roc_nor(1,0))
    print("0與1或非:",roc_nor(0,1))
    print("0與0或非:",roc_nor(0,0))
    print("1與1或非:",roc_nor(1,1))

基礎的門(由與非門和或非門實現)

#coding=utf-8
import elementgate
def roc_not(a):
    return elementgate.roc_nand(a, a)

def roc_and(a, b):
    return roc_not(elementgate.roc_nand(a,b))

def roc_or(a,b):
    return roc_not(elementgate.roc_nor(a,b))

def roc_xor(a,b):
    return roc_and(roc_or(a,b),elementgate.roc_nand(a,b))

if __name__=="__main__":
    print("1與0與非:",elementgate.roc_nand(1,0))
    print("0與1與非:",elementgate.roc_nand(0,1))
    print("0與0與非:",elementgate.roc_nand(0,0))
    print("1與1與非:",elementgate.roc_nand(1,1))
    print("1與0或非:",elementgate.roc_nor(1,0))
    print("0與1或非:",elementgate.roc_nor(0,1))
    print("0與0或非:",elementgate.roc_nor(0,0))
    print("1與1或非:",elementgate.roc_nor(1,1))
    print("0非:",roc_not(0))
    print("1非:",roc_not(1))
    print("1與0與門:",roc_and(1,0))
    print("0與1與門:",roc_and(0,1))
    print("0與0與門:",roc_and(0,0))
    print("1與1與門:",roc_and(1,1))
    print("1與0或門:",roc_or(1,0))
    print("0與1或門:",roc_or(0,1))
    print("0與0或門:",roc_or(0,0))
    print("1與1或門:",roc_or(1,1))
    print("1與0異或門:",roc_xor(1,0))
    print("0與1異或門:",roc_xor(0,1))
    print("0與0異或門:",roc_xor(0,0))
    print("1與1異或門:",roc_xor(1,1))

邏輯閘的簡單實踐