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.
//////////////////////////////////////////// /* Decoder的作用是將輸入的二進制數轉換為十進制數輸出。 解碼器僅在“enable”為真時工作。 請根據給定的課程完成“Decoder4_16”課程。 “Decoder4_16”應該由“Decoder2_4”組成。 如果需要,您可以添加任何功能。 Decoder4_16() :為類成員創建實體。 Decoder4_16(bool) :在聲明 Decoder4_16 並為類成員創建實體時設置“enable”的值。 setEnable(bool) :設置“enable”的值 setEnable(Gate *) : 設置“enable”的值 setValue(bool, int) :將值設置為指定的“輸入”。例如,a.setValue(false, 1) 表示 a 的“input[1]”設置為 false。 setValue(Gate *, int) :將值設置為分配的“輸入”。例如a.setValue(b, 1)表示a的“input[1]”為b。 operator[ ](int) :根據參數返回想要的輸出門。對於下面的示例,“dec[3]”為真,因此返回的門的輸出為真,假設 dec 是 Decode4_16。 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。 output() : 返回對應的十進制值(0~15)。對於下面的示例,“output()”為 3。 */ class Gate { public : Gate *input[2] ; virtual bool output() = 0 ; virtual void setValue(Gate *, int) = 0 ; virtual void setValue(bool, int) = 0 ; } ; class TRUE : public Gate { public : bool output() { return 1 ; } virtual void setValue(Gate *, int) {} virtual void setValue(bool, int) {} } ; class FALSE : public Gate { public : bool output() { return 0 ; } virtual void setValue(Gate *, int) {} virtual void setValue(bool, int) {} } ; TRUE t ; FALSE f ; class NOT : public Gate { public : bool output() { return !(this -> input[0] -> output()) ; } virtual void setValue(bool val, int pin = 0) { if(val) this -> input[0] = &t ; else this -> input[0] = &f ; } virtual void setValue(Gate* gate, int pin = 0) { this -> input[0] = gate ; } } ; class NAND : public Gate { public : bool output() { return !(this -> input[0] -> output()) || !(this -> input[1] -> output()) ; } virtual void setValue(Gate *gate, int pin) { this -> input[pin] = gate ; } virtual void setValue(bool val, int pin) { if(val) setValue(&t, pin) ; else setValue(&f, pin) ; } } ; class NOR : public Gate { public : bool output() { return !(this -> input[0] -> output()) && !(this -> input[1] -> output()) ; } virtual void setValue(Gate *gate, int pin) { this -> input[pin] = gate ; } virtual void setValue(bool val, int pin) { if(val) setValue(&t, pin) ; else setValue(&f, pin) ; } } ; //“AND”必須由“NOT”和“NAND”組成。 class AND : public Gate { public : AND() : Gate() { component[0] = new NOT ; component[1] = new NAND ; } virtual bool output() { component[1] -> input[0] = this -> input[0] ; component[1] -> input[1] = this -> input[1] ; component[0] -> input[0] = component[1] ; return component[0] -> output() ; } private : Gate *component[2] ; } ; //“OR”必須由“NOT”和“NOR”組成。 class OR : public Gate { public : OR() : Gate() { component[0] = new NOT ; component[1] = new NOR ; } virtual bool output() { component[1] -> input[0] = this -> input[0] ; component[1] -> input[1] = this -> input[1] ; component[0] -> input[0] = component[1] ; return component[0] -> output() ; } private : Gate *component[2] ; } ; //“XOR” 可以由任何門組成(包括內置閘和自己實現的上面閘)。 class XOR : public Gate { public : XOR() : Gate() { } virtual bool output() { return (this -> input[0] -> output()) ^ (this -> input[1] -> output()) ; } private : } ; class Decoder { public : virtual void setValue(bool, int) = 0 ; virtual void setValue(Gate *, int) = 0 ; virtual void setEnable(bool) = 0 ; virtual void setEnable(Gate *) = 0 ; virtual int output() = 0 ; virtual Gate *operator[](int) = 0 ; protected : Gate *enable ; } ; class Decoder2_4 : public Decoder { public : Decoder2_4() : Decoder2_4(0) {} Decoder2_4(bool val) { if(val) this -> enable = &t ; else this -> enable = &f ; for(int i = 0 ; i < 2 ; i++) component[i] = new NOT ; for(int i = 2 ; i < 6 ; i++) component[i] = new AND ; for(int i = 0 ; i < 4 ; i++) enables[i] = new AND ; } virtual void setEnable(bool val) { if(val) this -> enable = &t ; else this -> enable = &f ; } virtual void setEnable(Gate *gate) { this -> enable = gate ; } virtual void setValue(Gate *gate, int i) { component[i % 2] -> input[0] = gate ; } virtual void setValue(bool val, int i) { if(val) component[i % 2] -> input[0] = &t ; else component[i % 2] -> input[0] = &f ; } virtual Gate *operator[](int n) { _out() ; switch(n) { case 0 : return enables[0] ; case 1 : return enables[1] ; case 2 : return enables[2] ; case 3 : return enables[3] ; default : return nullptr ; } } friend ostream &operator<<(ostream &out, Decoder2_4 dec) { for(int i = 3 ; i >= 0 ; i--) out << dec[i] -> output() << " " ; return out ; } virtual int output() { for(int i = 0 ; i < 4 ; i++) if(enables[i] -> output()) return i ; } private : Gate *component[6] ; Gate *enables[4] ; void _out() { component[2] -> input[0] = component[0] ; component[2] -> input[1] = component[1] ; component[3] -> input[0] = component[0] -> input[0] ; component[3] -> input[1] = component[1] ; component[4] -> input[0] = component[0] ; component[4] -> input[1] = component[1] -> input[0] ; component[5] -> input[0] = component[0] -> input[0] ; component[5] -> input[1] = component[1] -> input[0] ; for(int i = 0 ; i < 4 ; i++) { enables[i] -> input[0] = this -> enable ; enables[i] -> input[1] = component[i + 2] ; } } } ; class Decoder4_16 : public Decoder { public : Decoder4_16() : Decoder4_16(0) {} {} // set the value of "enable" when declare a Decoder4_16 and create entities for class members. Decoder4_16(bool val) { for(int i = 0 ; i < 5 ; i++) dec2_4[i] = new Decoder ; if(val) dec2_4[0] -> enable = &t ; else dec2_4[0] -> enable = &f ; } virtual void setEnable(bool val) { dec2_4[0]->setEnable(val); } virtual void setEnable(Gate *gate) { dec2_4[0]->setEnable(gate); } virtual void setValue(bool val, int pin) { if (pin==0) { for(int i=1;i<5;i++) dec2_4[i]->setValue(val, 0); } else if (pin==1) { for(int i=1;i<5;i++) dec2_4[i]->setValue(val, 1); } else if (pin==2) { dec2_4[0]->setValue(val, 0); } else if (pin==3) { dec2_4[0]->setValue(val, 1);} _Decode(); } virtual void setValue(Gate *gate, int pin) { if (pin==0) { for(int i=1;i<5;i++) dec2_4[i]->setValue(gate, 0); } else if (pin==1) { for(int i=1;i<5;i++) dec2_4[i]->setValue(gate, 1); } else if (pin==2) { dec2_4[0]->setValue(gate, 0); } else if (pin==3) { dec2_4[0]->setValue(gate, 1);} _Decode(); } virtual Gate *operator[](int n) { switch(n) { case 0 : return dec2_4[0] ; case 1 : return dec2_4[1] ; case 2 : return dec2_4[2] ; case 3 : return dec2_4[3] ; case 4 : return dec2_4[4] ; default : return nullptr ; } } friend ostream &operator<<(ostream &out, Decoder4_16 dec) { for(int i = 4 ; i >= 1 ; i--) for(int j = 3 ; j >= 0 ; i--) out << dec.ec2_4[i][j] -> output() << " " ; return out ; } // output() : 返回對應的十進制值(0~15)。 int output() { int i,j; i=dec2_4[1]->output() ; j:= dec2_4[2]-> output(); if (j>0) i=i+4+j; j:= dec2_4[3]-> output(); if (j>0) i=i+8+j; j:= dec2_4[4]-> output(); if (j>0) i=i+12+j; return i; } private : Decoder *dec2_4[5] ; void _Decode() { Decoder *dec=dec2_4[0]; dec2_4[1]->setEnable(dec[0]); dec2_4[2]->setEnable(dec[1]); dec2_4[3]->setEnable(dec[2]); dec2_4[4]->setEnable(dec[3]); } } ;