vendredi 7 avril 2023

C++ Sheffer functions (How the Boolean EXOR function can be composed entirely from NAND gates)

I am a first-year computer engineering student using C++ for one of my modules. We have an assignement due in a few days, and I would really appreciate if someone could help me check if my code is correct according to the given Requirements Specification. Also I don not know how to implement the requirement in the "my_EXOR_implementation" which says that truth values may not be represented numerically. (We are marked out of 1, so we will either get 1 or 0; and NULL marks are given to any submission that violates any of the given requirements).

I will place the Scenario and the requirements Specification below and then what I have so far.

Scenario: An undergraduate student of Electronic Engineering, Emily Esterhuizen, is currently learning that Sheffer functions are universal Boolean functions by means of which all other Boolean functions can be composed. Moreover, Emily has also already learned that the NAND function is indeed one of such Sheffer functions. Normally such Boolean functions are implemented in hardware (where they are also known as “gates”), but as an undergraduate student there is no way for Emily to make hardware gates. For this reason, Emily would like to simulate such gates in software, such that she will be able to study how such functional compositions are made and how they work. Unfortunately Emily is not very good at C++ programming, such that she is hoping for your help in this matter: she would like to see how the Boolean EXOR function can be composed entirely from NAND gates (which is certainly possible because NAND is a Sheffer function).

Your Task: Help Emily Esterhuizen to implement in C++ the following Requirements Specification:

MAIN Program • declares two Boolean variables: b1, b2 • asks the user to input truth-values for b1, b2 • calls the function my_EXOR_implementation with b1, b2 as its two actual parameters • outputs the truth-value which my_EXOR_implementation returns.

Boolean Function my_EXOR_implementation(Boolean input1, Boolean input2) • must return the correct truth-value of EXOR(input1,input2) • must utilize for this purpose a correct combination of several calls to the underlying Sheffer function my_NAND_implementation • May not contain any built-in logical C++ operators && (conjunction), || (disjunction), or ! (negation) • May not contain any mathematical-arithmetic operators (like + or −) for mimicking Boolean typed truth-values as numbers (0,1): the truth-values may not be represented numerically. • May not contain any if-statements, if-else-statements, nor switch-case statements.

Boolean Function my_NAND_implementation(Boolean input1, Boolean input2) • must return the correct truth-value of NAND(input1,input2) • must utilize for this purpose a suitable algorithmic combination of several if-statements or if-else-statements, • May not contain any built-in logical C++ operators && (conjunction), || (disjunction), or ! (negation) • May not contain any mathematical-arithmetic operators (like + or −) for mimicking Boolean typed truth-values as numbers (0,1): the truth-values may not be represented numerically.

`#include <iostream>

using namespace std;

bool my_NAND_implementation(bool input1, bool input2) {
  bool result = true;
  if (input1 == true) {
    if (input2 == true) {
      result = false;
    }
  }
  return result;
}

bool my_EXOR_implementation(bool input1, bool input2) {
  bool nand1 = my_NAND_implementation(input1, input2);
  bool nand2 = my_NAND_implementation(input1, nand1);
  bool nand3 = my_NAND_implementation(input2, nand1);
  bool nand4 = my_NAND_implementation(nand2, nand3);
  return nand4;
}

int main() {
  bool b1, b2;
  cout << "Enter truth value for b1 (0 or 1): ";
  cin >> b1;
  cout << "Enter truth value for b2 (0 or 1): ";
  cin >> b2;
  bool exor_result = my_EXOR_implementation(b1, b2);
  cout << "The result of EXOR(" << b1 << ", " << b2 << ") is: " << exor_result << endl;
  
return 0;
}`
`

Aucun commentaire:

Enregistrer un commentaire