⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xordemo.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>
using namespace std;

#include "gates.h"
#include "wires.h"
#include "tvector.h"

// illustrate connecting wires to gates using Connect

CompositeGate * MakeXOR()
// post: return an xor-gate    
{
    CompositeGate * xorg = new CompositeGate(); // holds xor-gate
    Gate * ag = new AndGate();                  // build components
    Gate * ag2= ag->clone();                    // and gate a different way
    Gate * og  = new OrGate();
    Gate * inv = new Inverter();
    
    Connect(og->InWire(0),   ag->InWire(1) );   // wire components
    Connect(og->InWire(1),   ag->InWire(0) );
    Connect(ag->OutWire(0),  inv->InWire(0));
    Connect(inv->OutWire(0), ag2->InWire(1)); 
    Connect(og->OutWire(0),  ag2->InWire(0));
    
    xorg->AddGate(ag);  xorg->AddGate(ag2);     // add gates to xor-circuit
    xorg->AddGate(inv); xorg->AddGate(og);
    
    xorg->AddOut(ag2->OutWire(0));              // add inputs/outputs
    xorg->AddIn(og->InWire(0)); xorg->AddIn(og->InWire(1)); 
    
    return xorg;
}

CompositeGate * MakeXOR2()
// post: returns an xor-gate    
{
    CompositeGate * xorg = new CompositeGate();
    tvector<Wire *> w(6);     // need 6 wires to make circuit
    tvector<Gate *> gates;    // holds the gates in the xor-circuit
    int k;
    for(k=0; k < 6; k++)
    {   w[k] = new Wire();
    } 
    gates.push_back(new OrGate( w[0], w[1], w[2]) ); // create wired gates
    gates.push_back(new AndGate(w[0], w[1], w[3]) ); // share inputs
    gates.push_back(new Inverter(w[3], w[4]) );      // and out->inv in
    gates.push_back(new AndGate(w[2], w[4], w[5]) ); // combine or, inv
    
    for(k=0; k < gates.size();k++)                   // add gates to xor
    {   xorg->AddGate(gates[k]);
    }
    xorg->AddIn(w[0]); xorg->AddIn(w[1]);            // add inputs/outputs
    xorg->AddOut(w[5]);
    
    return xorg;
}

int main()
{
    CompositeGate * g = MakeXOR();
    CompositeGate *g2 = MakeXOR2();
    cout << "circuit has " << g->CountWires() << " wires" << endl;
    GateTester::Test(g);
    cout << "circuit has " << g2->CountWires() << " wires" << endl;
    GateTester::Test(g2);

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -