本文整理汇总了C++中Rational::setDenominator方法的典型用法代码示例。如果您正苦于以下问题:C++ Rational::setDenominator方法的具体用法?C++ Rational::setDenominator怎么用?C++ Rational::setDenominator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rational
的用法示例。
在下文中一共展示了Rational::setDenominator方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
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;
}
示例2: canExponentiate
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;
}
示例3: divide
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;
}
示例4: simplify
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;
}
}
}
示例5: main
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);
//.........这里部分代码省略.........