Last active
October 8, 2024 17:56
-
-
Save ProfAvery/ac1a68b7cc737348bd41853611f23ba0 to your computer and use it in GitHub Desktop.
CPSC 439 - Spring 2022 - Week 5 - NAND-CIRC-IF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
/* | |
C++ version of | |
https://nbviewer.org/github/boazbk/tcscode/blob/master/Chap_04_Syntactic_Sugar.ipynb#Even-more-sugar | |
*/ | |
using std::cout; | |
using std::endl; | |
using std::string; | |
using std::to_string; | |
string NAND(string a, string b) | |
{ | |
static auto counter = 1; | |
auto v = "temp_" + to_string(counter++); | |
cout << v << " = NAND(" << a << "," << b << ")" << endl; | |
return v; | |
} | |
string NOT(string a) | |
{ | |
return NAND(a, a); | |
} | |
string AND(string a, string b) | |
{ | |
auto temp = NAND(a, b); | |
return NOT(temp); | |
} | |
string OR(string a, string b) | |
{ | |
auto temp1 = NOT(a); | |
auto temp2 = NOT(b); | |
return NAND(temp1, temp2); | |
} | |
string MAJ(string a, string b, string c) | |
{ | |
auto and1 = AND(a, b); | |
auto and2 = AND(a, c); | |
auto and3 = AND(b, c); | |
auto or1 = OR(and1, and2); | |
return OR(or1, and3); | |
} | |
int main() | |
{ | |
MAJ("X[0]", "X[1]", "X[1]"); | |
/* | |
When IF, LOOKUP1, and LOOKUP2 have been implemented correctly, | |
you should be able to run the following instead: | |
LOOKUP2("X[0]", "X[1]", "X[2]", "X[3]", "I[0]", "I[1]"); | |
*/ | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment