Rational operator+(Rational const & a, Rational const & b) { Rational rat; rat.setNumerator((a.getNumerator()*b.getDenominator()) + (b.getNumerator() * a.getDenominator())); rat.setDenominator(a.getDenominator() * b.getDenominator()); return rat; }
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; }
Expression* Exponential::multiply(Expression* a){ if(a->type == "euler"){ if (this->base->type == "euler") { Rational* oneRat = new Rational(1, 1); this->exponent->add(oneRat); } }else if(a->type == "exponential"){ Exponential* ex = (Exponential *) a; //if (this->base == ex->getBase()) { // this->exponent->add(ex->getExponent()); //} if (ex->base->type == this->base->type) { if ((ex->base->type == "euler") || (ex->base->type == "pi")) { this->exponent->add(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->add(oneRat); } }else if(a->type == "rational"){ Rational* r = (Rational *) a; Expression* numToSet = r->geteNumerator(); numToSet->multiply(this); r->setNumerator(numToSet); return r; }else{ cout << "type not recognized" << endl; } return this; }
void NatE::simplify(){ this->exponent->simplify(); this->coefficient->simplify(); if (this->coefficient->getType() == "Rational"){ Rational* newCo = dynamic_cast<Rational*>(this->coefficient); if (newCo->getNumerator()->getType() == "NatE"){ NatE* newPi = dynamic_cast<NatE*>(newCo->getNumerator()); Add* add = new Add(); this->exponent = add->evaluate(this->exponent, newPi->getExponent()); newCo->setNumerator(newPi->getCoefficient()); newCo->simplify(); this->coefficient = newCo; } else if (newCo->getDenominator()->getType() == "NatE"){ NatE* newPi = dynamic_cast<NatE*>(newCo->getDenominator()); Subtract* sub = new Subtract(); this->exponent = sub->evaluate(this->exponent, newPi->getExponent()); newCo->setDenominator(newPi->getCoefficient()); newCo->simplify(); this->coefficient = newCo; } } }
int main() { Rational p; // p uses the default constructor Rational q(1, 2); // q uses the other constructor Rational b(1); // uses constructor to initialize numerator and defaults the denominator to 1 cout << b << endl; p.setNumerator(1); // set p to be 1/4 p.setDenominator(4); p= p + q; p= p * q; //print out p and q p.streamInsert(cout); cout << "p is " << p.getNumerator() << "/" << p.getDenominator() << endl; cout << "q is " << q.getNumerator() << "/" << q.getDenominator() << endl; Rational r; Rational s; cout << "Enter a rational number (a/b): "; cin >> r; cout << "Enter a rational number (a/b): "; cin >> s; cout << "You entered the rational numbers " << r << " and " << s << endl; //Confirm +,-,*, and / works with rationals. Rational sum = r + s; Rational product = r * s; Rational difference = r - s; Rational divide = r/s; // Test greater than, greater than or equal to, and isEqual cout << "Changing value of r and s..." << endl; r.setNumerator(3); r.setDenominator(4); s.setNumerator(1); s. setDenominator(4); if (r >= s) { cout << r << " is greater than or equal to " << s << endl; if (r > s) { cout << "okay..." << r << " is actually greater than " << s << endl; } else if (r== s) { cout << "okay..." << r << " is actually equal to " << s << endl; } } else { cout << "Oops, I enter an incorrect rational to test greater than or equal to!" << endl; } // Test less than, less than or equal to, and isEqual(again) cout << "Changing value of r and s again..." << endl; r.setNumerator(1); r.setDenominator(4); s.setNumerator(3); s. setDenominator(4); if (r <= s) { cout << r << " is less than or equal to " << s << endl; if (r < s) { cout << "okay..." << r << " is actually less than " << s << endl; } else if (r== s) { cout << "okay..." << r << " is actually equal to " << s << endl; } } else { cout << "Oops, I enter an incorrect rational to test less than or equal to!" << endl; } // Test the ability to cout new ADT. cout << r << " + " << s << " = " << sum << endl; cout << r << " * " << s << " = " << product << endl; cout << r << " - " << s << " = " << difference << endl; cout << r << " / " << s << " = " << divide << endl; // Test cloning or coping Rational ADT Rational t(s); double tFloat = t.convertToFloatingPoint(); cout << "s copied the rational " << s << " to t " << t << "." << endl; cout << "t in decimal equals: " << tFloat << endl; // Test reducing rational **extra credit*** r.setNumerator(12); r.setDenominator(4); s.setNumerator(6); s. setDenominator(8); r.reduce(); s.reduce(); cout << "r is now reduced to " << r << endl; cout << "s is now reduced to " << s << endl; cin.ignore(); cin.get(); return 0; }