程式作業: 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 為真,0 為假。
  請根據給定的類完成類“AND”、“OR”和“XOR”。
  “AND”必須由“NOT”和“NAND”組成。
  “OR”必須由“NOT”和“NOR”組成。
  “XOR” 可以由任何門組成(包括內置閘和自己實現的上面閘)。
  
默認構造函數:為“組件”創建新實體
output() :根據兩個“輸入”返回正確的結果


*/

class Gate   // 閘
{
    public :
        Gate *input[2] ;
        virtual bool output() = 0 ;
  		void setValue(Gate *, int) ;
        void setValue(bool, int) ;
} ;

class TRUE : public Gate
{
    public :
        virtual bool output() { return 1 ; }
        void setValue(Gate *, int) {}
        void setValue(bool, int) {}
} ;

class FALSE : public Gate
{
    public :
        virtual bool output() { return 0 ; }
        void setValue(Gate *, int) {}
        void setValue(bool, int) {}
} ;

TRUE t ;
FALSE f ;

void Gate::setValue(bool val, int pin)
{
    if(val) this -> input[pin] = &t ;
    else this -> input[pin] = &f ;
}

void Gate::setValue(Gate *gate, int pin) { this -> input[pin] = gate ; }

class NOT : public Gate
{
    public :
        virtual bool output() { return !(this -> input[0] -> output()) ; }
        void setValue(bool val, int pin = 0)
        {
        	if(val) this -> input[0] = &t ;
            else this -> input[0] = &f ;
        }
        void setValue(Gate* gate, int pin = 0) { this -> input[0] = gate ; }
} ;

class NAND : public Gate
{
    public :
        virtual bool output() { return !(this -> input[0] -> output()) || !(this -> input[1] -> output()) ; }
} ;

class NOR : public Gate
{
    public :
        virtual bool output() { return !(this -> input[0] -> output()) && !(this -> input[1] -> output()) ; }
} ;





class XOR : public Gate
{
    public :
        XOR() : Gate() {}
        virtual bool output() {}
    private :
        Gate *component[5] ;
} ;
// Reference example
class XNOR : public Gate
{
    public :
        XNOR() : Gate()
        {
            component[0] = new NOT ;
            component[1] = new XOR ;
        }
        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] ;
} ;



  //“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 :
       
} ;

沒有留言:

張貼留言