Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you add two fractions?

Tags:

c#

math

How can you add two fraction in c# like this:

11/6 + 3/4 = 31/12

and then how would you simplify the answer?

like image 432
hi tech id Avatar asked Dec 10 '22 10:12

hi tech id


2 Answers

As boredom strikes on a Friday night... resulting in a Fraction class with overloaded operators. Sobriety of design cannot be guaranteed.

It works like this...

Fraction left = "1/2";
Fraction right = "3/8";

Fraction result1 = ((left + right) * left / right).Simplify();
Console.WriteLine(result1);

Fraction test2 = "12/32";
test2 = test2.Simplify();
Console.WriteLine(test2);

Implementation something like below...

public struct Fraction
{
    public int Numerator { get; set; }
    public int Denominator { get; set; }

    public Fraction(int numerator, int denominator)
        : this()
    {
        Numerator = numerator;
        Denominator = denominator;
    }

    public Fraction Simplify()
    {
        int gcd = GCD();
        return new Fraction(Numerator / gcd, Denominator / gcd);
    }

    public Fraction InTermsOf(Fraction other)
    {
        return Denominator == other.Denominator ? this :
            new Fraction(Numerator * other.Denominator, Denominator * other.Denominator);
    }

    public int GCD()
    {
        int a = Numerator;
        int b = Denominator;
        while (b != 0)
        {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }

    public Fraction Reciprocal()
    {
        return new Fraction(Denominator, Numerator);
    }


    public static Fraction operator +(Fraction left, Fraction right)
    {
        var left2 = left.InTermsOf(right);
        var right2 = right.InTermsOf(left);

        return new Fraction(left2.Numerator + right2.Numerator, left2.Denominator);
    }

    public static Fraction operator -(Fraction left, Fraction right)
    {
        var left2 = left.InTermsOf(right);
        var right2 = right.InTermsOf(left);

        return new Fraction(left2.Numerator - right2.Numerator, left2.Denominator);
    }

    public static Fraction operator *(Fraction left, Fraction right)
    {
        return new Fraction(left.Numerator * right.Numerator, left.Denominator * right.Denominator);
    }

    public static Fraction operator /(Fraction left, Fraction right)
    {
        return new Fraction(left.Numerator * right.Denominator, left.Denominator * right.Numerator);
    }

    public static implicit operator Fraction(string value)
    {
        var tokens = value.Split('/');
        int num;
        int den;
        if (tokens.Length == 1 && int.TryParse(tokens[0], out num))
        {
            return new Fraction(num, 1);
        }
        else if (tokens.Length == 2 && int.TryParse(tokens[0], out num) && int.TryParse(tokens[1], out den))
        {
            return new Fraction(num, den);
        }
        throw new Exception("Invalid fraction format");
    }

    public override string ToString()
    {
        return string.Format("{0}/{1}", Numerator, Denominator);
    }
}
like image 161
Jon Avatar answered Jan 11 '23 22:01

Jon


.Net doesn't have built-in support (that I know of) for Rational Numbers, but there is at least one existing library out there.

Your solution will ultimately come down to storing the numerator and denominator (probably in a custom class that you create), doing arithmetic against other numerator/denominator pairs (possibly by implementing overloaded operators on your class), and applying a fraction simplification algorithm.

Here are some resources:

Existing implementations

  • http://www.codeproject.com/KB/cs/RationalNumbers.aspx
  • http://www.codeproject.com/KB/recipes/fractiion.aspx

Methods for reducing fractions

  • http://www.mathsisfun.com/simplifying-fractions.html
  • http://en.wikipedia.org/wiki/Least_common_multiple
  • http://en.wikipedia.org/wiki/Greatest_common_divisor
like image 33
Merlyn Morgan-Graham Avatar answered Jan 11 '23 22:01

Merlyn Morgan-Graham