Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Fraction : IComparable
- {
- private long nominator;
- private long denominator;
- public long Nominator
- {
- get
- {
- return this.nominator;
- }
- set
- {
- this.nominator = value;
- }
- }
- public long Denominator
- {
- get
- {
- return this.denominator;
- }
- set
- {
- if (value == 0)
- {
- throw new ArgumentException("Zero cannot be denominator!");
- }
- this.denominator = value;
- }
- }
- public Fraction(long nominator, long denominator)
- {
- this.Nominator = nominator;
- this.Denominator = denominator;
- }
- public Fraction() : this(1, 1)
- {
- }
- public Fraction(long numerator) : this(numerator, 1)
- {
- }
- public override string ToString()
- {
- if (this.Denominator == 1)
- {
- return this.Nominator.ToString();
- }
- else if (this.Nominator == 0)
- {
- return "0";
- }
- else if (this.Nominator * this.Denominator > 0)
- {
- return string.Format("{0}/{1}", Math.Abs(this.Nominator), Math.Abs(this.Denominator));
- }
- return string.Format("-{0}/{1}", Math.Abs(this.Nominator), Math.Abs(this.Denominator));
- }
- private long gcd(long a, long b)
- {
- if (b == 0)
- {
- return a;
- }
- return gcd(b, a % b);
- }
- public void Reduce()
- {
- long divider = gcd(this.Nominator, this.Denominator);
- this.denominator /= divider;
- this.nominator /= divider;
- }
- public int CompareTo(object obj)
- {
- Fraction other = (Fraction)obj;
- double comp = (double)this.Nominator / this.Denominator - (double)other.Nominator / other.Denominator;
- if (Math.Abs(comp) < 0.00001)
- {
- return 0;
- }
- else if (comp > 0)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- public static Fraction operator+(Fraction fr1, Fraction fr2)
- {
- Fraction fr = new Fraction();
- fr.Nominator = fr1.Nominator*fr2.Denominator + fr2.Nominator*fr1.Denominator;
- fr.Denominator = fr1.Denominator * fr2.Denominator;
- fr.Reduce();
- return fr;
- }
- public static bool operator<(Fraction fr1, Fraction fr2)
- {
- return fr1.CompareTo(fr2) == -1;
- }
- public static bool operator>(Fraction fr1, Fraction fr2)
- {
- return fr1.CompareTo(fr2) == 1; ;
- }
- public static bool operator <=(Fraction fr1, Fraction fr2)
- {
- return fr1.CompareTo(fr2) != 1; ;
- }
- public static bool operator >=(Fraction fr1, Fraction fr2)
- {
- return fr1.CompareTo(fr2) != -1; ;
- }
- public static bool operator ==(Fraction fr1, Fraction fr2)
- {
- return fr1.CompareTo(fr2) == 0; ;
- }
- public static bool operator !=(Fraction fr1, Fraction fr2)
- {
- return fr1.CompareTo(fr2) != 0; ;
- }
- public static Fraction operator-(Fraction fr1, Fraction fr2)
- {
- Fraction fr = new Fraction();
- fr.Nominator = fr1.Nominator * fr2.Denominator - fr2.Nominator * fr1.Denominator;
- fr.Denominator = fr1.Denominator * fr2.Denominator;
- fr.Reduce();
- return fr;
- }
- public static Fraction operator*(Fraction fr1, Fraction fr2)
- {
- Fraction fr = new Fraction();
- fr.Nominator = fr1.Nominator * fr2.Nominator;
- fr.Denominator = fr1.Denominator * fr2.Denominator;
- fr.Reduce();
- return fr;
- }
- public static Fraction operator /(Fraction fr1, Fraction fr2)
- {
- Fraction fr = new Fraction();
- fr.Nominator = fr1.Nominator * fr2.Denominator;
- fr.Denominator = fr1.Denominator * fr2.Nominator;
- fr.Reduce();
- return fr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement