本文整理汇总了C++中Rational::reduce方法的典型用法代码示例。如果您正苦于以下问题:C++ Rational::reduce方法的具体用法?C++ Rational::reduce怎么用?C++ Rational::reduce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rational
的用法示例。
在下文中一共展示了Rational::reduce方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
Rational Rational::operator*(const Rational & n) //overwriting operator
{
Rational temp {(n.top * top), (n.bottom * bottom)};
temp.reduce();
return temp;;
}
示例2: Rational
//分数减法
Number *Rational::sub(Number *number2)
{
Rational *tmp = SCAST_RATIONAL(number2);
Rational *result = new Rational();
result->numerator_ = numerator_*tmp->denominator_ - denominator_*tmp->numerator_;
result->denominator_ = denominator_ * tmp->denominator_;
result->reduce();//最后要进行约分
return result;
}
示例3: Rational
Number *Rational::sub(Number *number2)
{
Rational *tmp = SCAST_RATIONAL(number2);
Rational *result = new Rational();
result->num_ = num_*tmp->den_ - den_*tmp->num_;
result->den_ = den_ * tmp->den_;
result->reduce();
return result;
}
示例4: main
//.........这里部分代码省略.........
//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;
}