鱼C论坛

 找回密码
 立即注册
查看: 1228|回复: 1

[技术交流] 写了一个分数类

[复制链接]
发表于 2020-5-10 08:59:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 永恒的蓝色梦想 于 2020-5-10 09:53 编辑
  1. #pragma once
  2. #include<cmath>
  3. #include<iostream>



  4. namespace std {
  5.     using namespace std;
  6.     typedef long long ll;



  7.     ll gcd(ll a, ll b) {
  8.         ll c;
  9.         while (b) {
  10.             c = a % b;
  11.             a = b;
  12.             b = c;
  13.         }
  14.         return a;
  15.     }



  16.     class fraction {
  17.         /*
  18.         注意:
  19.             分子或分母可能会溢出
  20.             溢出后的结果是未定义的
  21.             分母为0的分数是 NaN(Not a Number)
  22.             Nan 和 其他数 == 永远返回 false,!=永远返回 true
  23.             对 NaN 运算的结果是未定义的
  24.         */
  25.     private:
  26.         ll nume, deno;

  27.     public:
  28.         fraction(ll a = 0, ll b = 1, bool reduction = true) {
  29.             if (reduction) {
  30.                 ll temp = gcd(a, b);
  31.                 if (b < 0) {
  32.                     this->nume = -a / temp;
  33.                     this->deno = -b / temp;
  34.                 }
  35.                 else {
  36.                     this->nume = a / temp;
  37.                     this->deno = b / temp;
  38.                 }
  39.             }
  40.             else if (b < 0) {
  41.                 this->nume = -a;
  42.                 this->deno = -b;
  43.             }
  44.             else {
  45.                 this->nume = a;
  46.                 this->deno = b;
  47.             }
  48.         }

  49.         fraction(fraction a, fraction b) {
  50.             *this = a / b;
  51.         }

  52.         fraction(fraction a, ll b) {
  53.             ll temp = gcd(a.deno, b);
  54.             this->nume = b / temp * a.nume;
  55.             this->deno = a.deno / temp;
  56.         }

  57.         fraction(ll a, fraction b) {
  58.             ll temp = gcd(a, b.nume);
  59.             this->nume = a / temp * b.deno;
  60.             this->deno = b.nume / temp;
  61.         }


  62.         fraction operator+() {
  63.             return fraction(*this);
  64.         }

  65.         fraction operator+(fraction other) {
  66.             ll temp = gcd(this->deno, other.deno), a = this->deno / temp, b = other.deno / temp;
  67.             return fraction(this->nume * b + other.nume * a, a * b);
  68.         }


  69.         fraction operator-() {
  70.             return fraction(-this->nume, this->deno);
  71.         }

  72.         fraction operator-(fraction other) {
  73.             ll temp = gcd(this->deno, other.deno), a = this->deno / temp, b = other.deno / temp;
  74.             return fraction(this->nume * b - other.nume * a, a * b);
  75.         }


  76.         fraction operator*(fraction other) {
  77.             ll a = gcd(this->nume, other.deno), b = gcd(this->deno, other.nume);

  78.             return fraction((this->nume / a) * (other.nume / b),
  79.                 (this->deno / b) * (other.deno / a));
  80.         }


  81.         fraction operator/(fraction other) {
  82.             ll a = gcd(this->nume, other.nume), b = gcd(this->deno, other.deno);

  83.             return fraction((this->nume / a) * (other.deno / b),
  84.                 (this->deno / b) * (other.nume / a),
  85.                 false);
  86.         }


  87.         fraction operator%(fraction other) {
  88.             ll temp = gcd(this->deno, other.deno), a = this->deno / temp, b = other.deno / temp;
  89.             return fraction((this->nume * b) % (other.nume * a), a * b);
  90.         }


  91.         bool operator<(fraction other) {
  92.             ll temp = gcd(this->deno, other.deno);
  93.             return other.deno / temp * this->nume < this->deno / temp * other.nume;
  94.         }


  95.         bool operator>(fraction other) {
  96.             ll temp = gcd(this->deno, other.deno);
  97.             return other.deno / temp * this->nume > this->deno / temp * other.nume;
  98.         }


  99.         bool operator<=(fraction other) {
  100.             ll temp = gcd(this->deno, other.deno);
  101.             return other.deno / temp * this->nume <= this->deno / temp * other.nume;
  102.         }


  103.         bool operator>=(fraction other) {
  104.             ll temp = gcd(this->deno, other.deno);
  105.             return other.deno / temp * this->nume >= this->deno / temp * other.nume;
  106.         }


  107.         bool operator==(fraction other) {
  108.             return this->deno && other.deno ? this->nume == other.nume && this->deno == other.deno : false;
  109.         }


  110.         bool operator!=(fraction other) {
  111.             return this->deno && other.deno ? this->nume != other.nume && this->deno != other.deno : true;
  112.         }


  113.         operator bool() {
  114.             return this->deno && this->nume;
  115.         }


  116.         operator ll() {
  117.             return this->nume / this->deno;
  118.         }


  119.         operator double() {
  120.             return double(this->nume) / this->deno;
  121.         }


  122.         ll denominator() {
  123.             return this->deno;
  124.         }


  125.         ll numerator() {
  126.             return this->nume;
  127.         }


  128.         static bool isnan(fraction& other) {
  129.             return !other.deno;
  130.         }


  131.         static bool isnumber(fraction& other) {
  132.             return other.deno;
  133.         }


  134.         friend ostream& operator<<(ostream& out, fraction other);
  135.         friend istream& operator>>(istream& in, fraction& other);
  136.         /*static double sqrt(fraction other) {
  137.             return std::sqrt(double(other.nume) * other.deno) / other.deno;
  138.         }


  139.         static double cbrt(fraction other) {
  140.             return std::cbrt(double(other.nume) * other.deno) / other.deno;
  141.         }


  142.         static fraction from_double(double other) {

  143.         }*/
  144.     };



  145.     ostream& operator<<(ostream& out, fraction other) {
  146.         return other.deno ? out << other.nume << '/' << other.deno : out << "NaN";
  147.     }



  148.     istream& operator>>(istream& in, fraction& other) {
  149.         char temp;
  150.         return in >> other.nume >> temp >> other.deno;
  151.     }
  152. }
复制代码
比起 ratio 的优点可能只有能用运算符了吧
目前这个类还有不少缺陷,希望各位大佬可以多包涵,多提建议,多指教!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-18 11:34:34 From FishC Mobile | 显示全部楼层
应该是 C++
对不起我都看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 18:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表