程式作業: 2 questions for simulating logic gates ( C++)

 Logic gates is the basic of any computers. From now on, there will be 2 questions for simulating logic gates and their extension

1 is True and 0 is False. 

 Please finish the classes "AND", "OR" and "XOR" according to the given classes. 

 "AND" must be composed of "NOT" and "NAND". 
 "OR" must be composed of "NOT" and "NOR". 
 "XOR" can be composed of any gates (including build-in gates and above gates that implemented by yourself). 

  •  Default Constructor : create new entities for "component" 
  • output() : return the correct result depending on the two "input"
  1. /*
  2. 1 為真,0 為假。
  3. 請根據給定的類完成類“AND”、“OR”和“XOR”。
  4. “AND”必須由“NOT”和“NAND”組成。
  5. “OR”必須由“NOT”和“NOR”組成。
  6. “XOR” 可以由任何門組成(包括內置閘和自己實現的上面閘)。
  7. 默認構造函數:為“組件”創建新實體
  8. output() :根據兩個“輸入”返回正確的結果
  9.  
  10.  
  11. */
  12.  
  13. class Gate // 閘
  14. {
  15. public :
  16. Gate *input[2] ;
  17. virtual bool output() = 0 ;
  18. void setValue(Gate *, int) ;
  19. void setValue(bool, int) ;
  20. } ;
  21.  
  22. class TRUE : public Gate
  23. {
  24. public :
  25. virtual bool output() { return 1 ; }
  26. void setValue(Gate *, int) {}
  27. void setValue(bool, int) {}
  28. } ;
  29.  
  30. class FALSE : public Gate
  31. {
  32. public :
  33. virtual bool output() { return 0 ; }
  34. void setValue(Gate *, int) {}
  35. void setValue(bool, int) {}
  36. } ;
  37.  
  38. TRUE t ;
  39. FALSE f ;
  40.  
  41. void Gate::setValue(bool val, int pin)
  42. {
  43. if(val) this -> input[pin] = &t ;
  44. else this -> input[pin] = &f ;
  45. }
  46.  
  47. void Gate::setValue(Gate *gate, int pin) { this -> input[pin] = gate ; }
  48.  
  49. class NOT : public Gate
  50. {
  51. public :
  52. virtual bool output() { return !(this -> input[0] -> output()) ; }
  53. void setValue(bool val, int pin = 0)
  54. {
  55. if(val) this -> input[0] = &t ;
  56. else this -> input[0] = &f ;
  57. }
  58. void setValue(Gate* gate, int pin = 0) { this -> input[0] = gate ; }
  59. } ;
  60.  
  61. class NAND : public Gate
  62. {
  63. public :
  64. virtual bool output() { return !(this -> input[0] -> output()) || !(this -> input[1] -> output()) ; }
  65. } ;
  66.  
  67. class NOR : public Gate
  68. {
  69. public :
  70. virtual bool output() { return !(this -> input[0] -> output()) && !(this -> input[1] -> output()) ; }
  71. } ;
  72.  
  73.  
  74.  
  75.  
  76.  
  77. class XOR : public Gate
  78. {
  79. public :
  80. XOR() : Gate() {}
  81. virtual bool output() {}
  82. private :
  83. Gate *component[5] ;
  84. } ;
  85. // Reference example
  86. class XNOR : public Gate
  87. {
  88. public :
  89. XNOR() : Gate()
  90. {
  91. component[0] = new NOT ;
  92. component[1] = new XOR ;
  93. }
  94. virtual bool output()
  95. {
  96. component[1] -> input[0] = this -> input[0] ;
  97. component[1] -> input[1] = this -> input[1] ;
  98. component[0] -> input[0] = component[1] ;
  99. return component[0] -> output() ;
  100. }
  101. private :
  102. Gate *component[2] ;
  103. } ;
  104.  
  105.  
  106.  
  107. //“AND”必須由“NOT”和“NAND”組成。
  108. class AND : public Gate
  109. {
  110. public :
  111. AND() : Gate()
  112. {
  113. component[0] = new NOT ;
  114. component[1] = new NAND ;
  115. }
  116. virtual bool output()
  117. {
  118. component[1] -> input[0] = this -> input[0] ;
  119. component[1] -> input[1] = this -> input[1] ;
  120. component[0] -> input[0] = component[1] ;
  121. return component[0] -> output() ;
  122. }
  123. private :
  124. Gate *component[2] ;
  125. } ;
  126. //“OR”必須由“NOT”和“NOR”組成。
  127. class OR : public Gate
  128. {
  129. public :
  130. OR() : Gate()
  131. {
  132. component[0] = new NOT ;
  133. component[1] = new NOR ;
  134. }
  135. virtual bool output()
  136. {
  137. component[1] -> input[0] = this -> input[0] ;
  138. component[1] -> input[1] = this -> input[1] ;
  139. component[0] -> input[0] = component[1] ;
  140. return component[0] -> output() ;
  141. }
  142. private :
  143. Gate *component[2] ;
  144. } ;
  145. //“XOR” 可以由任何門組成(包括內置閘和自己實現的上面閘)。
  146. class XOR : public Gate
  147. {
  148. public :
  149. XOR() : Gate()
  150. {
  151. }
  152. virtual bool output() { return (this -> input[0] -> output()) ^ (this -> input[1] -> output()) ; }
  153. private :
  154. } ;

沒有留言:

張貼留言