开发者

How can you add two fractions?

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

11/6 + 3/4 = 31/12

and开发者_如何学运维 then how would you simplify the answer?


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);
    }
}


.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


Find the LCD first then do the following

  num1 *= lcd / denum1;
  num2 *= lcd / denum2;
  sumNum = num1 + num2;


I have never used c# but I can give you an explanation of how to do it.

  1. check if bottom number are the same(equal)
  2. If not than create loop that will loop through all numbers and multiply them by bottom numbers. For example, let just say you started with 1 than you multiply 1 by 6 and then 1 by 4 if the result are same than you got right number.
  3. than you the number you got in #2 and multiply it by the top numbers
  4. than just add the top numbers
  5. keep the bottom number the same
  6. create a loop to see what number can be divided by both numbers(top and bottom)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜