bool Exponential::canExponentiate() { if(base->type == "euler"){ return false; }else if(base->type == "exponential"){ Exponential* ex = (Exponential *) base; this->exponent->multiply(ex->getExponent()); Integer* numSum = new Integer (1); ex->getExponent()->setNumerator(numSum); return false; // false is returned because the base itself would have already been exponentiated if it were possible }else if(base->type == "integer"){ return true; }else if(base->type == "logarithm"){ return false; }else if(base->type == "nthRoot"){ nthRoot* nr = (nthRoot *) base; Rational* r = new Rational(this->exponent->getNumerator(), nr->getRoot()*this->exponent->getDenominator()); //makes a new exponent, multiplying the denominator by the root, allowing the root to be simplified to one this->exponent = r; nr->setRoot(1); return false; }else if(base->type == "pi"){ return false; }else if(base->type == "rational"){ Rational* r = (Rational *) base; if (r->geteNumerator()->type == "integer" && r->geteDenominator()->type == "integer") { Exponential* nu = new Exponential(r->geteNumerator(), this->exponent); r->setNumerator(nu); Exponential* de = new Exponential(r->geteDenominator(), this->exponent); r->setDenominator(de); } }else{ cout << "type not recognized" << endl; } return false; }
void Exponential::exponentiate(){ Integer* one = new Integer(1); Rational* oneRat = new Rational(1, 1); if (this->base->type == "rational") { Rational* ratBase = (Rational *) this->base; Exponential* numAsExponential = new Exponential ((ratBase->geteNumerator()), (this->exponent)); //no matching constructor for exponential Exponential* denAsExponential = new Exponential ((ratBase->geteDenominator()), (this->exponent)); //same error Rational* newRatBase = new Rational(numAsExponential, denAsExponential); this->base = newRatBase; this->exponent = oneRat; } else { if (this->exponent->getNumerator()==0) { this->exponent=oneRat; this->base=one; } bool toFlip = false; if (exnu->getValue()<0) { exnu->setValue(exnu->getValue()*-1); toFlip = true; //handles negative exponents } Expression* constantBase = 0; if (base->type == "integer") { //fixed the problem for integers but nothing else Integer *a = (Integer *)base; constantBase = new Integer(a->getValue()); } while (exponent->getNumerator()>1) { base->multiply(constantBase); exponent->setNumerator(exponent->geteNumerator()->subtract(one)); } if (toFlip) { while (exnu->getValue() > 1) { base->multiply(constantBase); exnu->subtract(one); } Integer* one = new Integer(1); Rational* mouse = new Rational(one, base); base = mouse; exponent = new Rational(1,1); } } }
Expression* Exponential::divide(Expression* a){ if(a->type == "euler"){ if (this->base->type == "euler") { Rational* oneRat = new Rational(1, 1); this->exponent->subtract(oneRat); } }else if(a->type == "exponential"){ Exponential* ex = (Exponential *) a; //if (this->base == ex->getBase()) { // this->exponent->subtract(ex->getExponent()); //} if (ex->base->type == this->base->type) { if ((ex->base->type == "euler") || (ex->base->type == "pi")) { this->exponent->subtract(ex->exponent); } } }else if(a->type == "integer"){ }else if(a->type == "logarithm"){ }else if(a->type == "nthRoot"){ }else if(a->type == "pi"){ if (this->base->type == "pi") { Rational* oneRat = new Rational(1, 1); this->exponent->subtract(oneRat); } }else if(a->type == "rational"){ Rational* r = (Rational *) a; Expression* denToSet = r->geteDenominator(); denToSet->multiply(this); r->setDenominator(denToSet); return r; }else{ cout << "type not recognized" << endl; } return this; }