添加和减少粗俗分数

时间:2013-09-25 18:26:10

标签: c# fractions

我试图通过寻找最低公分母然后加入来将两个粗俗分数加在一起。但是,我的代码没有按预期运行,并输出两个非常高的负数。当我将第二个分数更改为3/15时,它输出0/0。

这是我的主程序代码:

class Program
{
    static void Main(string[] args)
    {
        Fraction n = new Fraction(2, 4);
        Fraction z = new Fraction(3, 12);

        Fraction sum = n.Add(z, n);
        int num = sum.Numerator;
        int den = sum.Denominator;

        Console.WriteLine("{0}/{1}", num, den);
        Console.ReadKey(true);
    }
}

这是我的Fraction班级代码:

internal class Fraction
{
    public Fraction(int numerator, int denominator)
    {
        Numerator = numerator;
        Denominator = denominator;
    }

    public int Numerator { get; private set; }

    public int Denominator { get; private set; }

    public Fraction Add(Fraction fraction2, Fraction fraction8)
    {
        int lcd = GetLCD(fraction8, fraction2);

        int x = lcd/fraction8.Denominator;
        int n = lcd/fraction2.Denominator;
        int f2num = fraction2.Numerator*n;
        int f8num = fraction8.Numerator*x;

        int t = fraction2.Numerator;

        Fraction Fraction3 = new Fraction(f2num+f8num,lcd);

        return Fraction3;
    }



    public int GetLCD(Fraction b, Fraction c)
    {
        int i = b.Denominator;
        int j = c.Denominator;

        while (true)
        {

            if (i == j)
            {
                return i;
            }
            j = j + j;
            i = i + i;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

在课堂上使用GetLCD, Add & Subtract方法没有意义。所以,我把它从课堂上移出来并制作了静态方法。

您的GetLCD功能无法正确显示LCD。这将为您提供所需的结果。(我没有费心去使用Subtract方法,您可以按照以下代码进行操作并使其自行运行)

PS:我没有改变你的所有变量名称和我建议你尽可能让它们变得有意义。 n,z,x,y,b,c不是好的变量名

    static void Main(string[] args)
    {
        Fraction n = new Fraction(2, 4);
        Fraction z = new Fraction(3, 12);

        Fraction sum = Add(z, n);
        int x = sum.Numerator;
        int y = sum.Denominator;

        Console.WriteLine("{0}/{1}", x, y);
        Console.ReadKey(true);
    }

    public static Fraction Add(Fraction fraction2, Fraction fraction8)
    {

        int lcd = GetLCD(fraction8, fraction2);
        int multiplier = 0;
        if (fraction2.Denominator < lcd)
        {
            multiplier = lcd / fraction2.Denominator;
            fraction2.Numerator = multiplier * (fraction2.Numerator);
            fraction2.Denominator = multiplier * (fraction2.Denominator);
        }
        else
        {
            multiplier = lcd / fraction8.Denominator;
            fraction8.Numerator = multiplier * (fraction8.Numerator);
            fraction8.Denominator = multiplier * (fraction8.Denominator);
        }

        Fraction Fraction3 = new Fraction(fraction2.Numerator + fraction8.Numerator, lcd);

        return Fraction3;
    }

    public static int GetLCD(Fraction b, Fraction c)
    {
        int i = b.Denominator;
        int j = c.Denominator;

        int greater = 0;
        int lesser = 0;

        if (i > j)
        {
            greater = i; lesser = j;
        }
        else if (i < j)
        {
            greater = j; lesser = i;
        }
        else
        {
            return i;
        }
        for (int iterator = 1; iterator <= lesser; iterator++)
        {
            if ((greater * iterator) % lesser == 0)
            {
                return iterator * greater;
            }
        }
        return 0;
    }

    internal class Fraction
    {
        public Fraction(int numerator, int denominator)
        {
            Numerator = numerator;
            Denominator = denominator;
        }
        public int Numerator { get; set; }
        public int Denominator { get; set; }
    }

答案 1 :(得分:1)

就个人而言,我认为你的第一个错误是试图计算最低公分母,而不是仅仅找到最简单的公分母。由于模式识别,找到液晶显示屏是人类在纸上工作的一个很好的策略:我们可以快速识别液晶显示器;但计算LCD,然后将分数转换为它,对于每次必须执行每一步(并且无法识别模式)的计算机来说,显着更多的步骤。此外,一旦您将两个分数转换为LCD后添加它们,它甚至不能保证降低结果。我假设需要减少的结果,正如通常用分数算术所预期的那样。因为它似乎很有用,我将缩减直接放入构造函数代码中:

internal class Fraction
{
public Fraction(int numerator, int denominator, bool reduce = false)
{
    if (!reduce)
    {
        Numerator = numerator;
        Denominator = denominator;
    }
    else
    {
        var GCD = GreatestCommonDivisor(numerator, denominator);
        Numerator = numerator / GCD;
        Denominator = denominator / GCD;
    }
}

public int Numerator { get; private set; }

public int Denominator { get; private set; }    

public static Fraction Add(Fraction first, Fraction second)
{
    return Combine(first, second, false);
}

public static Fraction Subtract(Fraction first, Fraction second)
{
    return Combine(first, second, true);
}

private static Fraction Combine(Fraction first, Fraction second, bool isSubtract)
{
    var newDenominator = first.Denominator * second.Denominator;
    var newFirst = first.Numerator * second.Denominator;
    var newSecond = first.Denominator * second.Denominator;

    if (isSubtract)
    {
        newSecond = newSecond * -1;
    }

    return new Fraction(newFirst + newSecond, newDenominator, true);
}

private static int GreatestCommonDivisor(int a, int b) 
{
    return b == 0 ? a : GreatestCommonDivisor(b, a % b);
}

}

编辑:从this answer

中偷走了最大的公约数代码