Generate...|

园龄:粉丝:关注:

2022-10-27 11:51阅读: 11评论: 0推荐: 0

C++ 的有理数类 Rational 实现

class Rational
{
static inline int gcd(int a, int b)
{
if (!b) return abs(a);
while ((a %= b) && (b %= a)) ; // do in while
return a + b;
}
public:
static const Rational Infinity;
static const Rational Nan;
Rational(int numerator, int denominator)
: n(numerator), d(denominator) { symplify(); }
constexpr Rational(int number) : n(number), d(1) {}
constexpr Rational() : n(0), d(1) {}
const Rational &operator=(const Rational &other)
{
n = other.n;
d = other.d;
return *this;
}
const int numerator() const { return n; }
const int denominator() const { return d; }
private:
void symplify()
{
int cd = gcd(n, d);
if (cd) {
n /= cd;
d /= cd;
}
if (d < 0) {
n = -n;
d = -d;
}
}
int n, d;
};
const Rational Rational::Infinity = Rational(1, 0);
const Rational Rational::Nan = Rational(0, 0);
inline const Rational operator-(const Rational &a)
{ return Rational(-a.numerator(), a.denominator()); }
inline const Rational operator+(const Rational &a, const Rational &b)
{
return Rational(a.numerator() * b.denominator() +
a.denominator() * b.numerator(),
a.denominator() * b.denominator());
}
inline const Rational operator-(const Rational &a, const Rational &b)
{ return a + -b; }
inline const Rational operator*(const Rational &a, const Rational &b)
{
return Rational(a.numerator() * b.numerator(),
a.denominator() * b.denominator());
}
inline const Rational operator/(const Rational &a, const Rational &b)
{
return Rational(a.numerator() * b.denominator(),
a.denominator() * b.numerator());
}
std::ostream &operator<<(std::ostream &os, const Rational &r)
{
switch (r.denominator())
{
case 0:
if (r.numerator() == 0) return os << "NaN";
if (r.numerator() < 0) os << '-';
return os << "Inf";
case 1:
return os << r.numerator();
default:
return os << '(' << r.numerator() << '/' << r.denominator() << ')';
}
}
void test_rational()
{
Rational a(6, -2);
cout << a << '\n';
cout << Rational(9, 33) + 5 << '\n';
cout << Rational(-9, 33) * 2 << '\n';
cout << Rational(-9, -33) - 1 << '\n';
cout << Rational(9, -33) / 8 << '\n';
cout << 1 / Rational(9, -33) << '\n';
cout << "-----Inf------\n";
cout << a * -Rational::Infinity << '\n';
cout << a * - -Rational::Infinity << '\n';
cout << a + Rational::Infinity << '\n';
cout << a - Rational::Infinity << '\n';
cout << "-----NaN------\n";
cout << Rational::Nan - Rational::Infinity << '\n';
cout << a * Rational::Nan << '\n';
cout << a / Rational::Nan << '\n';
cout << a + Rational::Nan << '\n';
cout << a - Rational::Nan << '\n';
}

本文作者:violeshnv

本文链接:https://www.cnblogs.com/violeshnv/p/16831728.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Violeshnv  阅读(11)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 とおいよびごえ 凋叶棕
  2. 2 かぜのねいろ 凋叶棕
  3. 3 Milky Way Train 流派未階堂
  4. 4 nostalgia 流派未階堂
  5. 5 桜花繚乱 はちみつれもん
  6. 6 胡蝶之夢 はちみつれもん
  7. 7 色は散りゆく はちみつれもん
  8. 8 暮色蒼然 はちみつれもん
  9. 9 追想、桜ノ國 はちみつれもん
  10. 10 意にそぐわぬリターニー 凋叶棕
nostalgia - 流派未階堂
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.