程式作業:OneBitHalfAdder 及 OneBitFullAdder (C++)

 Please finish the classes "OneBitHalfAdder" and "OneBitFullAdder" according to the given classes.

"OneBitHalfAdder" must be composed of "AND" and "XOR".

"OneBitFullAdder" must be composed of "OneBitHalfAdder" and "OR".

You could add any function if you need.

Default Constructor: create new entities for "component"
sum() :return the correct sum depending on the addition of two "input" while the return value is "Gate *

  • carryOut() : return the correct carry depending on the addition of two "input" while the return value is "Gate *".
  • 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 one gate to the assigned "input". For example, a.setValue(b, 0) means that the "input[0]" is b.
/////////////////////////////////////////////
/*
請根據給定的類完成類“OneBitHalfAdder”和“OneBitFullAdder”。

“OneBitHalfAdder”必須由“AND”和“XOR”組成。

“OneBitFullAdder”必須由“OneBitHalfAdder”和“OR”組成。

如果需要,您可以添加任何功能。
Default Constructor: create new entities for "component"
sum() :return the correct sum depending on the addition of two "input" while the return value is "Gate *"
carryOut() : return the correct carry depending on the addition of two "input" while the return value is "Gate *".
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 one gate to the assigned "input". For example, a.setValue(b, 0) means that the "input[0]" is b.
*/

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()) ; }
} ;


  //“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 Adder
{
    public :
        virtual void setValue(bool, int) = 0 ;
        virtual void setValue(Gate *, int) = 0 ;
        virtual Gate *sum() = 0 ;
        virtual Gate *carryOut() = 0 ;
} ;


// “OneBitHalfAdder”必須由“AND”和“XOR”組成。
class OneBitHalfAdder : public Adder
{
    public :
        OneBitHalfAdder() {
			
			 component[0] = new XOR ;
             component[1] = new AND ;
			
		}
        virtual void setValue(bool val, int pin) {
			
			component[0]->setValue(val, pin);
			component[1]->setValue(val, pin);
			
			
		}
        virtual void setValue(Gate *gate, int pin) {
			component[0]->setValue(gate, pin);
			component[1]->setValue(gate, pin);
						
		}
        virtual Gate *sum() { return component[0]; }
        virtual Gate *carryOut() { return component[1]; }
    private :
        Gate *component[2] ;
} ;

class OneBitFullAdder : public Adder
{
    public :
        OneBitFullAdder() {
			
			 a[0] = new OneBitHalfAdder ;
             a[1] = new OneBitHalfAdder ;
			 carry =new OR;
			
		}
        virtual void setValue(bool val, int pin) {
			
			// pin 0 =A  pin1=B  pin 2 = Cin
			
			if (pin>=0 && pin<=1) { a[0]->setValue(val,pin);  a[1]->setValue(a[0]->sum ,0);  }
			if (pin==2) { a[1]->setValue(val,1);    }
						
			
		}
        virtual void setValue(Gate *gate, int pin) {
			
			if (pin>=0 && pin<=1) { a[0]->setValue(gate,pin);  a[1]->setValue(a[0]->sum ,0);  }
			if (pin==2) { a[1]->setValue(gate,1);    }
						
		}
        virtual Gate *sum() {
			return  a[1]->sum();
		}
        virtual Gate *carryOut() {
			
			carry->setValue(a[0]->carryOut,0); carry->setValue(a[1]->carryOut,1);
			
			return  carry;
			
		}
    private :
        Adder *a[2] ;
        Gate *carry ;
} ;

沒有留言:

張貼留言