Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Rational{
- private:
- BigInteger num;
- BigInteger den;
- void make_irred(BigInteger &self, BigInteger &other){
- BigInteger gcd_ = gcd(self, other);
- self /= gcd_;
- other /= gcd_;
- }
- void Normalize(){
- if(den.get_sign() == -1){
- num.change_sign();
- den.change_sign();
- }
- make_irred(num, den);
- }
- public:
- Rational(const BigInteger &other){
- num = BigInteger(other);
- den = BigInteger(1);
- }
- Rational(const int other){
- num = BigInteger(other);
- den = BigInteger(1);
- }
- Rational(){
- num = 0;
- den = 1;
- }
- Rational& operator +=(const Rational &other){
- num *= other.den;
- num += other.num * den;
- den *= other.den;
- // make_irred(num, den);
- Normalize();
- return *this;
- }
- Rational operator -() const {
- Rational ans = *this;
- ans.num *= -1;
- return ans;
- }
- Rational& operator -=(const Rational &other){
- *this += -other;
- return *this;
- }
- Rational& operator *=(const Rational &other){
- num *= other.num;
- den *= other.den;
- // make_irred(num, den);
- Normalize();
- return *this;
- }
- Rational& operator /=(const Rational &other){
- num *= other.den;
- den *= other.num;
- // make_irred(num, den);
- Normalize();
- return *this;
- }
- Rational operator /(const Rational &other) const {
- Rational ans = *this;
- ans /= other;
- return ans;
- }
- Rational& operator %=(const Rational &other){
- *this = *this - *this / other * other;
- return *this;
- }
- Rational operator %(const Rational &other) const {
- Rational res = *this;
- res %= other;
- return res;
- }
- Rational operator *(const Rational &other) const {
- Rational ans = *this;
- ans *= other;
- return ans;
- }
- Rational operator +(const Rational &other) const {
- Rational ans = *this;
- ans += other;
- return ans;
- }
- Rational operator -(const Rational &other) const {
- Rational ans = *this;
- ans -= other;
- return ans;
- }
- std::string toString() const {
- std::string result = "";
- if(num.get_sign() == -1){
- result += '-';
- }
- result += num.toString();
- if(den != 1){
- result += '/';
- result += den.toString();
- }
- return result;
- }
- friend std::istream& operator>>(std::istream &in, Rational& self){
- std::string input1;
- std::string input2;
- in >> input1 >> input2;
- self.num = BigInteger(input1);
- self.den = BigInteger(input2);
- self.Normalize();
- return in;
- }
- friend std::ostream& operator<<(std::ostream &out, const Rational& P){
- //print(P.toString());
- out << P.toString();
- return out;
- }
- bool SmallerMod2(const Rational &self, const Rational &other) const {
- Rational q = self / other;
- // $$
- if(SmallerMod(q.num, q.den)){
- return 1;
- }
- return 0;
- }
- bool operator ==(const Rational &other) const {
- return toString() == other.toString();
- }
- bool operator <(const Rational &other) const {
- bool smaller = SmallerMod2(*this, other);
- if(num.get_sign() != other.num.get_sign()){
- if(num.get_sign() == 1){
- return 0;
- }else{
- return 1;
- }
- }
- if(*this == other){
- return 0;
- }
- if(num.get_sign() == 1){
- return smaller;
- }
- return smaller ^ 1;
- }
- bool operator <=(const Rational &other) const {
- return (*this < other) || (*this == other);
- }
- bool operator >(const Rational &other) const {
- return other < *this;
- }
- bool operator >=(const Rational &other) const {
- return (*this > other) || (*this == other);
- }
- bool operator !=(const Rational &other) const {
- return !(*this == other);
- }
- Rational& operator =(const Rational &other){
- num = other.num;
- den = other.den;
- return *this;
- }
- /* bool operator ==(const int &other) const {
- return *this == Rational(other);
- }
- bool operator <(const int &other) const {
- return *this < Rational(other);
- }
- bool operator <=(const int &other) const {
- return *this <= Rational(other);
- }
- bool operator >(const int &other) const {
- return *this > Rational(other);
- }
- bool operator >=(const int &other) const {
- return *this >= Rational(other);
- }
- bool operator !=(const int &other) const {
- return *this != Rational(other);
- }
- Rational& operator -=(const int other){
- *this += -Rational(other);
- return *this;
- }
- Rational& operator *=(const int other_){
- Rational other = other_;
- num *= other.num;
- den *= other.den;
- // make_irred(num, den);
- Normalize();
- return *this;
- }
- Rational& operator /=(const int other_){
- Rational other = other_;
- num *= other.den;
- den *= other.num;
- // make_irred(num, den);
- Normalize();
- return *this;
- }
- Rational operator /(const int other) const {
- Rational ans = *this;
- ans /= Rational(other);
- return ans;
- }
- Rational& operator %=(const int other_){
- Rational other = Rational(other_);
- *this = *this - *this / other * other;
- return *this;
- }
- Rational operator %(const int other) const {
- Rational res = *this;
- res %= Rational(other);
- return res;
- }
- Rational operator *(const int other) const {
- Rational ans = *this;
- ans *= Rational(other);
- return ans;
- }
- Rational operator +(const int other) const {
- Rational ans = *this;
- ans += Rational(other);
- return ans;
- }
- Rational operator -(const int other) const {
- Rational ans = *this;
- ans -= Rational(other);
- return ans;
- }*/
- friend Rational operator +(const int self, const Rational& other){
- return Rational(self) + other;
- }
- friend Rational operator -(const int self, const Rational& other){
- return Rational(self) - other;
- }
- friend Rational operator *(const int self, const Rational& other){
- return Rational(self) * other;
- }
- friend Rational operator /(const int self, const Rational& other){
- return Rational(self) / other;
- }
- friend Rational operator %(const int self, const Rational& other){
- return Rational(self) % other;
- }
- explicit operator bool() const {
- return num;
- }
- std::string asDecimal(size_t precision) const {
- BigInteger A = num % den;
- std::string ans = (num / den).toString();
- ans += '.';
- std::string degree = "1";
- for(size_t i = 0; i < precision; ++i){
- degree += '0';
- }
- std::cout << A << ' ' << den << ' ' << ans << '\n';
- A *= BigInteger(degree);
- ans += (A / den).toString();
- return ans;
- }
- explicit operator double() const {
- return std::stod(asDecimal(100));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement