Created
October 16, 2014 20:02
-
-
Save peanutwolf/cf721887e031f3f4dfdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct Rational | |
{ | |
Rational(int numerator = 0, int denominator = 1) | |
:numerator_(numerator), denominator_(denominator){}; | |
void add(Rational rational){ | |
this->numerator_ += (rational.numerator_ * this->denominator_); | |
}; | |
void sub(Rational rational){ | |
this->numerator_ -= (rational.numerator_ * this->denominator_); | |
}; | |
void mul(Rational rational); | |
void div(Rational rational); | |
void neg(){ | |
this->numerator_ = -this->numerator_; | |
}; | |
void inv(); | |
double to_double() const{ | |
return (double)numerator_/(double)denominator_; | |
}; | |
Rational& operator+=(const Rational rat){ | |
this->add(rat); | |
return (*this); | |
} | |
Rational& operator+=(const int rat){ | |
Rational r1; | |
r1.numerator_ = rat; | |
r1.denominator_ = 1; | |
(*this) += r1; | |
return (*this); | |
} | |
Rational& operator-=(const Rational rat){ | |
this->sub(rat); | |
return (*this); | |
} | |
Rational& operator-=(const int rat){ | |
Rational r1; | |
r1.numerator_ = rat; | |
r1.denominator_ = 1; | |
(*this) -= r1; | |
return (*this); | |
} | |
Rational& operator*=(const Rational rat){ | |
this->mul(rat); | |
return (*this); | |
} | |
Rational& operator*=(const int rat){ | |
Rational r1; | |
r1.numerator_ = rat; | |
r1.denominator_ = 1; | |
(*this) *= r1; | |
return (*this); | |
} | |
Rational& operator/=(const Rational rat){ | |
this->div(rat); | |
return (*this); | |
} | |
Rational& operator/=(const int rat){ | |
Rational r1; | |
r1.numerator_ = rat; | |
r1.denominator_ = 1; | |
(*this) /= r1; | |
return (*this); | |
} | |
Rational operator-(){ | |
// Rational r1; | |
// r1.numerator_ = this->numerator_; | |
// r1.denominator_ = this->denominator_; | |
this->neg(); | |
return *this; | |
} | |
Rational operator+(){ | |
Rational r1; | |
r1.numerator_ = this->numerator_; | |
r1.denominator_ = this->denominator_; | |
r1.neg(); | |
return r1; | |
//this->neg(); | |
// return *this; | |
} | |
Rational operator+(Rational r1){ | |
Rational r2; | |
r2.numerator_ = this->numerator_; | |
r2.denominator_ = this->denominator_; | |
r2.add(r1); | |
return r2; | |
} | |
Rational operator-(const Rational r1){ | |
Rational r2; | |
r2.numerator_ = (*this).numerator_; | |
r2.denominator_ = (*this).denominator_; | |
r2.sub(r1); | |
return r2; | |
} | |
Rational operator*(const Rational r1){ | |
Rational r2; | |
r2.numerator_ = (*this).numerator_; | |
r2.denominator_ = (*this).denominator_; | |
r2.mul(r1); | |
return r2; | |
} | |
Rational operator/(const Rational r1){ | |
Rational r2; | |
r2.numerator_ = (*this).numerator_; | |
r2.denominator_ = (*this).denominator_; | |
r2.div(r1); | |
return r2; | |
} | |
Rational operator+(const int rat){ | |
Rational r1(rat, 1); | |
Rational r2; | |
r2.numerator_ = this->numerator_; | |
r2.denominator_ = this->denominator_; | |
r2.add(r1); | |
return r2; | |
} | |
Rational operator-(const int rat){ | |
Rational r1(rat, 1); | |
Rational r2; | |
r2.numerator_ = (*this).numerator_; | |
r2.denominator_ = (*this).denominator_; | |
r2.sub(r1); | |
return r2; | |
} | |
Rational operator*(const int rat){ | |
Rational r1(rat, 1); | |
Rational r2; | |
r2.numerator_ = (*this).numerator_; | |
r2.denominator_ = (*this).denominator_; | |
r2.mul(r1); | |
return r2; | |
} | |
Rational operator/(const int rat){ | |
Rational r1(rat, 1); | |
Rational r2; | |
r2.numerator_ = (*this).numerator_; | |
r2.denominator_ = (*this).denominator_; | |
r2.div(r1); | |
return r2; | |
} | |
private: | |
int numerator_; | |
unsigned denominator_; | |
}; | |
Rational operator+(const int a, const Rational r1){ | |
Rational r2(a, 1); | |
r2.add(r1); | |
return r2; | |
} | |
Rational operator-(const int a, const Rational r1){ | |
Rational r2(a, 1); | |
r2.sub(r1); | |
return r2; | |
} | |
Rational operator*(const int a, const Rational r1){ | |
Rational r2(a, 1); | |
//r2.mul(r1); | |
return r2; | |
} | |
Rational operator/(const int a, const Rational r1){ | |
Rational r2(a, 1); | |
//r2.div(r1); | |
return r2; | |
} | |
int main(){ | |
Rational r1(6,1); | |
Rational r2(7,1); | |
int a = 1; | |
int b = 2; | |
r1 =r1 + r2; | |
cout<<r1.to_double()<<endl; | |
// -r1; | |
// cout<<r1.to_double()<<endl; | |
// r1 =-r1; | |
// cout<<r1.to_double()<<endl; | |
// -r1; | |
// cout<<r1.to_double()<<endl; | |
// r1 =-r1; | |
// cout<<r1.to_double()<<endl; | |
// int a = 1; | |
// cout<<a<<endl; | |
// -a; | |
// cout<<a<<endl; | |
// a=-a; | |
// cout<<a<<endl; | |
// -a; | |
// cout<<a<<endl; | |
// a=-a; | |
// cout<<a<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment