程式作業:The function of Decoder is converting binary number input to decimal number output. (C++)

 The function of Decoder is converting binary number input to decimal number output.

The decoder works only when "enable" is true.

Please finish the class "Decoder4_16" according to the given classes.

"Decoder4_16" should be composed of "Decoder2_4".

You could add any function if you need.

  • Decoder4_16() :create entities for class members.
  • Decoder4_16(bool) : set the value of  "enable" when declare a Decoder4_16 and create entities for class members.
  • setEnable(bool) : set the value of "enable"

  • setEnable(Gate *) : set the value of "enable"
  • setValue(bool, int) : set the value to the assigned "input". For example, a.setValue(false, 1) means that the "input[1]" of a is setting to false.
  • setValue(Gate *, int) : set the value to the assigned "input". For example, a.setValue(b, 1) means that the "input[1]" of a is b.
  • operator[ ](int) : return the the wanted output gate according to the parameter. For the example below, "dec[3]" is true, so the returned gate's output is true, assuming that dec is Decode4_16.
  • operator<<(ostream, Decoder4_16) : return all the value of the decoder. For the example below, "cout << dec" returns "0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 ",assuming that dec is Decode4_16.
  • output() : return the corresponding decimal value(0~15). For the example below, "output()" is 3.


  1. ////////////////////////////////////////////
  2. /*
  3. Decoder的作用是將輸入的二進制數轉換為十進制數輸出。
  4.  
  5. 解碼器僅在“enable”為真時工作。
  6.  
  7. 請根據給定的課程完成“Decoder4_16”課程。
  8.  
  9. “Decoder4_16”應該由“Decoder2_4”組成。
  10.  
  11. 如果需要,您可以添加任何功能。
  12.  
  13. Decoder4_16() :為類成員創建實體。
  14. Decoder4_16(bool) :在聲明 Decoder4_16 並為類成員創建實體時設置“enable”的值。
  15. setEnable(bool) :設置“enable”的值
  16. setEnable(Gate *) : 設置“enable”的值
  17. setValue(bool, int) :將值設置為指定的“輸入”。例如,a.setValue(false, 1) 表示 a 的“input[1]”設置為 false。
  18. setValue(Gate *, int) :將值設置為分配的“輸入”。例如a.setValue(b, 1)表示a的“input[1]”為b。
  19. operator[ ](int) :根據參數返回想要的輸出門。對於下面的示例,“dec[3]”為真,因此返回的門的輸出為真,假設 dec 是 Decode4_16。
  20. operator<<(ostream, Decoder4_16) :返回解碼器的所有值。對於下面的示例,“cout << dec”返回“0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0”,假設 dec 是 Decode4_16。
  21. output() : 返回對應的十進制值(0~15)。對於下面的示例,“output()”為 3。
  22.  
  23. */
  24. class Gate
  25. {
  26. public :
  27. Gate *input[2] ;
  28. virtual bool output() = 0 ;
  29. virtual void setValue(Gate *, int) = 0 ;
  30. virtual void setValue(bool, int) = 0 ;
  31. } ;
  32.  
  33. class TRUE : public Gate
  34. {
  35. public :
  36. bool output() { return 1 ; }
  37. virtual void setValue(Gate *, int) {}
  38. virtual void setValue(bool, int) {}
  39. } ;
  40.  
  41. class FALSE : public Gate
  42. {
  43. public :
  44. bool output() { return 0 ; }
  45. virtual void setValue(Gate *, int) {}
  46. virtual void setValue(bool, int) {}
  47. } ;
  48.  
  49. TRUE t ;
  50. FALSE f ;
  51.  
  52. class NOT : public Gate
  53. {
  54. public :
  55. bool output() { return !(this -> input[0] -> output()) ; }
  56. virtual void setValue(bool val, int pin = 0)
  57. {
  58. if(val) this -> input[0] = &t ;
  59. else this -> input[0] = &f ;
  60. }
  61. virtual void setValue(Gate* gate, int pin = 0) { this -> input[0] = gate ; }
  62. } ;
  63.  
  64. class NAND : public Gate
  65. {
  66. public :
  67. bool output() { return !(this -> input[0] -> output()) || !(this -> input[1] -> output()) ; }
  68. virtual void setValue(Gate *gate, int pin) { this -> input[pin] = gate ; }
  69. virtual void setValue(bool val, int pin)
  70. {
  71. if(val) setValue(&t, pin) ;
  72. else setValue(&f, pin) ;
  73. }
  74. } ;
  75.  
  76. class NOR : public Gate
  77. {
  78. public :
  79. bool output() { return !(this -> input[0] -> output()) && !(this -> input[1] -> output()) ; }
  80. virtual void setValue(Gate *gate, int pin) { this -> input[pin] = gate ; }
  81. virtual void setValue(bool val, int pin)
  82. {
  83. if(val) setValue(&t, pin) ;
  84. else setValue(&f, pin) ;
  85. }
  86. } ;
  87.  
  88. //“AND”必須由“NOT”和“NAND”組成。
  89. class AND : public Gate
  90. {
  91. public :
  92. AND() : Gate()
  93. {
  94. component[0] = new NOT ;
  95. component[1] = new NAND ;
  96. }
  97. virtual bool output()
  98. {
  99. component[1] -> input[0] = this -> input[0] ;
  100. component[1] -> input[1] = this -> input[1] ;
  101. component[0] -> input[0] = component[1] ;
  102. return component[0] -> output() ;
  103. }
  104. private :
  105. Gate *component[2] ;
  106. } ;
  107.  
  108. //“OR”必須由“NOT”和“NOR”組成。
  109. class OR : public Gate
  110. {
  111. public :
  112. OR() : Gate()
  113. {
  114. component[0] = new NOT ;
  115. component[1] = new NOR ;
  116. }
  117. virtual bool output()
  118. {
  119. component[1] -> input[0] = this -> input[0] ;
  120. component[1] -> input[1] = this -> input[1] ;
  121. component[0] -> input[0] = component[1] ;
  122. return component[0] -> output() ;
  123. }
  124. private :
  125. Gate *component[2] ;
  126. } ;
  127.  
  128. //“XOR” 可以由任何門組成(包括內置閘和自己實現的上面閘)。
  129. class XOR : public Gate
  130. {
  131. public :
  132. XOR() : Gate()
  133. {
  134. }
  135. virtual bool output() { return (this -> input[0] -> output()) ^ (this -> input[1] -> output()) ; }
  136. private :
  137. } ;
  138.  
  139. class Decoder
  140. {
  141. public :
  142. virtual void setValue(bool, int) = 0 ;
  143. virtual void setValue(Gate *, int) = 0 ;
  144. virtual void setEnable(bool) = 0 ;
  145. virtual void setEnable(Gate *) = 0 ;
  146. virtual int output() = 0 ;
  147. virtual Gate *operator[](int) = 0 ;
  148. protected :
  149. Gate *enable ;
  150. } ;
  151.  
  152. class Decoder2_4 : public Decoder
  153. {
  154. public :
  155. Decoder2_4() : Decoder2_4(0) {}
  156. Decoder2_4(bool val)
  157. {
  158. if(val) this -> enable = &t ;
  159. else this -> enable = &f ;
  160. for(int i = 0 ; i < 2 ; i++)
  161. component[i] = new NOT ;
  162. for(int i = 2 ; i < 6 ; i++)
  163. component[i] = new AND ;
  164. for(int i = 0 ; i < 4 ; i++)
  165. enables[i] = new AND ;
  166. }
  167. virtual void setEnable(bool val)
  168. {
  169. if(val) this -> enable = &t ;
  170. else this -> enable = &f ;
  171. }
  172. virtual void setEnable(Gate *gate) { this -> enable = gate ; }
  173. virtual void setValue(Gate *gate, int i) { component[i % 2] -> input[0] = gate ; }
  174. virtual void setValue(bool val, int i)
  175. {
  176. if(val) component[i % 2] -> input[0] = &t ;
  177. else component[i % 2] -> input[0] = &f ;
  178. }
  179. virtual Gate *operator[](int n)
  180. {
  181. _out() ;
  182. switch(n)
  183. {
  184. case 0 : return enables[0] ;
  185. case 1 : return enables[1] ;
  186. case 2 : return enables[2] ;
  187. case 3 : return enables[3] ;
  188. default : return nullptr ;
  189. }
  190. }
  191. friend ostream &operator<<(ostream &out, Decoder2_4 dec)
  192. {
  193. for(int i = 3 ; i >= 0 ; i--)
  194. out << dec[i] -> output() << " " ;
  195. return out ;
  196. }
  197. virtual int output()
  198. {
  199. for(int i = 0 ; i < 4 ; i++)
  200. if(enables[i] -> output()) return i ;
  201. }
  202. private :
  203. Gate *component[6] ;
  204. Gate *enables[4] ;
  205.  
  206. void _out()
  207. {
  208. component[2] -> input[0] = component[0] ;
  209. component[2] -> input[1] = component[1] ;
  210.  
  211. component[3] -> input[0] = component[0] -> input[0] ;
  212. component[3] -> input[1] = component[1] ;
  213.  
  214. component[4] -> input[0] = component[0] ;
  215. component[4] -> input[1] = component[1] -> input[0] ;
  216.  
  217. component[5] -> input[0] = component[0] -> input[0] ;
  218. component[5] -> input[1] = component[1] -> input[0] ;
  219.  
  220. for(int i = 0 ; i < 4 ; i++)
  221. {
  222. enables[i] -> input[0] = this -> enable ;
  223. enables[i] -> input[1] = component[i + 2] ;
  224. }
  225. }
  226. } ;
  227.  
  228. class Decoder4_16 : public Decoder
  229. {
  230. public :
  231. Decoder4_16() : Decoder4_16(0) {} {}
  232. // set the value of "enable" when declare a Decoder4_16 and create entities for class members.
  233. Decoder4_16(bool val) {
  234. for(int i = 0 ; i < 5 ; i++)
  235. dec2_4[i] = new Decoder ;
  236. if(val) dec2_4[0] -> enable = &t ;
  237. else dec2_4[0] -> enable = &f ;
  238. }
  239. virtual void setEnable(bool val) {
  240. dec2_4[0]->setEnable(val);
  241. }
  242. virtual void setEnable(Gate *gate) {
  243. dec2_4[0]->setEnable(gate);
  244. }
  245. virtual void setValue(bool val, int pin) {
  246. if (pin==0) { for(int i=1;i<5;i++) dec2_4[i]->setValue(val, 0); }
  247. else if (pin==1) { for(int i=1;i<5;i++) dec2_4[i]->setValue(val, 1); }
  248. else if (pin==2) { dec2_4[0]->setValue(val, 0); }
  249. else if (pin==3) { dec2_4[0]->setValue(val, 1);}
  250. _Decode();
  251. }
  252. virtual void setValue(Gate *gate, int pin) {
  253. if (pin==0) { for(int i=1;i<5;i++) dec2_4[i]->setValue(gate, 0); }
  254. else if (pin==1) { for(int i=1;i<5;i++) dec2_4[i]->setValue(gate, 1); }
  255. else if (pin==2) { dec2_4[0]->setValue(gate, 0); }
  256. else if (pin==3) { dec2_4[0]->setValue(gate, 1);}
  257. _Decode();
  258. }
  259. virtual Gate *operator[](int n) {
  260. switch(n)
  261. {
  262. case 0 : return dec2_4[0] ;
  263. case 1 : return dec2_4[1] ;
  264. case 2 : return dec2_4[2] ;
  265. case 3 : return dec2_4[3] ;
  266. case 4 : return dec2_4[4] ;
  267. default : return nullptr ;
  268. }
  269. }
  270. friend ostream &operator<<(ostream &out, Decoder4_16 dec) {
  271. for(int i = 4 ; i >= 1 ; i--)
  272. for(int j = 3 ; j >= 0 ; i--)
  273. out << dec.ec2_4[i][j] -> output() << " " ;
  274. return out ;
  275. }
  276. // output() : 返回對應的十進制值(0~15)。
  277. int output() {
  278. int i,j;
  279. i=dec2_4[1]->output() ;
  280. j:= dec2_4[2]-> output(); if (j>0) i=i+4+j;
  281. j:= dec2_4[3]-> output(); if (j>0) i=i+8+j;
  282. j:= dec2_4[4]-> output(); if (j>0) i=i+12+j;
  283. return i;
  284. }
  285. private :
  286. Decoder *dec2_4[5] ;
  287. void _Decode()
  288. {
  289. Decoder *dec=dec2_4[0];
  290. dec2_4[1]->setEnable(dec[0]);
  291. dec2_4[2]->setEnable(dec[1]);
  292. dec2_4[3]->setEnable(dec[2]);
  293. dec2_4[4]->setEnable(dec[3]);
  294. }
  295. } ;

沒有留言:

張貼留言