Exemple #1
0
tr1::shared_ptr<AbstractNumber> E::add(tr1::shared_ptr<AbstractNumber>number){

	if (number -> getName() == "E")
	{
		if (number -> getSign() == '+' && getSign() == '+')
		{
			vector<tr1::shared_ptr<AbstractNumber> > M;
			tr1::shared_ptr<AbstractNumber> two(new SmartInteger(2));
			M.push_back(two);
			M.push_back(shared_from_this());
			tr1::shared_ptr<AbstractNumber> output(new MultExpression(M, '+'));
			return output;
		}

		else if (number -> getSign() == '-' && getSign() == '-')
		{
			vector<tr1::shared_ptr<AbstractNumber> > N;
			tr1::shared_ptr<AbstractNumber> twoN(new SmartInteger(-2));
			N.push_back(twoN);
			tr1::shared_ptr<AbstractNumber> me(new E());
			N.push_back(me);
			tr1::shared_ptr<AbstractNumber> output1(new MultExpression(N, '+'));
			return output1;
		}

		else
		{
			tr1::shared_ptr<AbstractNumber> zero(new SmartInteger(0));
			return zero;
		}

	}
	else if(number->getName() == "MultExpression" || number->getName() == "SumExpression")
    {
        return number->add(shared_from_this());
    }

    vector<tr1::shared_ptr<AbstractNumber> > N;
    N.push_back(number);
    N.push_back(shared_from_this());
    tr1::shared_ptr<AbstractNumber> output1(new SumExpression(N));
    return output1;

}
Exemple #2
0
// Adds number to this and returns the sum
//
// Parameters:
// shared_ptr<AbstractNumber> number	number being added
//
// Returns:
// shared_ptr<AbstractNumber> 			resulting sum of addition
 tr1::shared_ptr<AbstractNumber> Exponent::add(tr1::shared_ptr<AbstractNumber> number){
 	// Checks for cancellation
 	number = number->simplify();
 	if(abs(toDouble() - number->toDouble()) < 0.000001 &&
 	   this->getSign() != number->getSign()){
 		tr1::shared_ptr<AbstractNumber> r(new SmartInteger(0));

 	    return r;
 	}
 	// Checks for duplication/simplification
 	else if(abs(toDouble() - number->toDouble()) < 0.000001 &&
 			this->getSign() == number->getSign()){
 		vector< tr1::shared_ptr<AbstractNumber> > MultVector;
		tr1::shared_ptr<AbstractNumber> i(new SmartInteger(2));
		MultVector.push_back(i);
		tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
		MultVector.push_back(me);
		tr1::shared_ptr<AbstractNumber> r(new MultExpression(MultVector, '+'));

	    return r;
  	}

    //Can't tell if they add or not
 	else if(number->getName() == "SumExpression" ||
            number->getName() == "MultExpression"){
 	    return number->add(shared_from_this());
 	}
    // Duplication necessary for simplification
 	// Assuming number is in simplest form

 	// No simplification possible
	else{
		vector< tr1::shared_ptr<AbstractNumber> > SumVector;
		tr1::shared_ptr<AbstractNumber> me(new Exponent(base, power, sign));
		SumVector.push_back(me);
		SumVector.push_back(number);
		tr1::shared_ptr<AbstractNumber> r(new SumExpression(SumVector));

	    return r;
	}

}