#include 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 w(6); // need 6 wires to make circuit tvector 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; }