写了一个分数类
本帖最后由 永恒的蓝色梦想 于 2020-5-10 09:53 编辑#pragma once
#include<cmath>
#include<iostream>
namespace std {
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
ll c;
while (b) {
c = a % b;
a = b;
b = c;
}
return a;
}
class fraction {
/*
注意:
分子或分母可能会溢出
溢出后的结果是未定义的
分母为0的分数是 NaN(Not a Number)
Nan 和 其他数 == 永远返回 false,!=永远返回 true
对 NaN 运算的结果是未定义的
*/
private:
ll nume, deno;
public:
fraction(ll a = 0, ll b = 1, bool reduction = true) {
if (reduction) {
ll temp = gcd(a, b);
if (b < 0) {
this->nume = -a / temp;
this->deno = -b / temp;
}
else {
this->nume = a / temp;
this->deno = b / temp;
}
}
else if (b < 0) {
this->nume = -a;
this->deno = -b;
}
else {
this->nume = a;
this->deno = b;
}
}
fraction(fraction a, fraction b) {
*this = a / b;
}
fraction(fraction a, ll b) {
ll temp = gcd(a.deno, b);
this->nume = b / temp * a.nume;
this->deno = a.deno / temp;
}
fraction(ll a, fraction b) {
ll temp = gcd(a, b.nume);
this->nume = a / temp * b.deno;
this->deno = b.nume / temp;
}
fraction operator+() {
return fraction(*this);
}
fraction operator+(fraction other) {
ll temp = gcd(this->deno, other.deno), a = this->deno / temp, b = other.deno / temp;
return fraction(this->nume * b + other.nume * a, a * b);
}
fraction operator-() {
return fraction(-this->nume, this->deno);
}
fraction operator-(fraction other) {
ll temp = gcd(this->deno, other.deno), a = this->deno / temp, b = other.deno / temp;
return fraction(this->nume * b - other.nume * a, a * b);
}
fraction operator*(fraction other) {
ll a = gcd(this->nume, other.deno), b = gcd(this->deno, other.nume);
return fraction((this->nume / a) * (other.nume / b),
(this->deno / b) * (other.deno / a));
}
fraction operator/(fraction other) {
ll a = gcd(this->nume, other.nume), b = gcd(this->deno, other.deno);
return fraction((this->nume / a) * (other.deno / b),
(this->deno / b) * (other.nume / a),
false);
}
fraction operator%(fraction other) {
ll temp = gcd(this->deno, other.deno), a = this->deno / temp, b = other.deno / temp;
return fraction((this->nume * b) % (other.nume * a), a * b);
}
bool operator<(fraction other) {
ll temp = gcd(this->deno, other.deno);
return other.deno / temp * this->nume < this->deno / temp * other.nume;
}
bool operator>(fraction other) {
ll temp = gcd(this->deno, other.deno);
return other.deno / temp * this->nume > this->deno / temp * other.nume;
}
bool operator<=(fraction other) {
ll temp = gcd(this->deno, other.deno);
return other.deno / temp * this->nume <= this->deno / temp * other.nume;
}
bool operator>=(fraction other) {
ll temp = gcd(this->deno, other.deno);
return other.deno / temp * this->nume >= this->deno / temp * other.nume;
}
bool operator==(fraction other) {
return this->deno && other.deno ? this->nume == other.nume && this->deno == other.deno : false;
}
bool operator!=(fraction other) {
return this->deno && other.deno ? this->nume != other.nume && this->deno != other.deno : true;
}
operator bool() {
return this->deno && this->nume;
}
operator ll() {
return this->nume / this->deno;
}
operator double() {
return double(this->nume) / this->deno;
}
ll denominator() {
return this->deno;
}
ll numerator() {
return this->nume;
}
static bool isnan(fraction& other) {
return !other.deno;
}
static bool isnumber(fraction& other) {
return other.deno;
}
friend ostream& operator<<(ostream& out, fraction other);
friend istream& operator>>(istream& in, fraction& other);
/*static double sqrt(fraction other) {
return std::sqrt(double(other.nume) * other.deno) / other.deno;
}
static double cbrt(fraction other) {
return std::cbrt(double(other.nume) * other.deno) / other.deno;
}
static fraction from_double(double other) {
}*/
};
ostream& operator<<(ostream& out, fraction other) {
return other.deno ? out << other.nume << '/' << other.deno : out << "NaN";
}
istream& operator>>(istream& in, fraction& other) {
char temp;
return in >> other.nume >> temp >> other.deno;
}
}比起 ratio 的优点可能只有能用运算符了吧{:10_245:}
目前这个类还有不少缺陷,希望各位大佬可以多包涵,多提建议,多指教!{:10_254:} 应该是 C++
对不起我都看不懂
页:
[1]