从C#中的int分数中获取分子和分母

时间:2018-03-11 17:10:15

标签: c# parsing fractions

我正在尝试编写一个C#程序,我需要将有理数转换为字符串。我正在努力的一点是,当数字(INT或DOUBLE)类似于-5/25时。在这种情况下,我首先应该首先简化分数,然后将'-1/5'作为字符串返回。我有几种方法可以解决这个问题,但我的第一个问题就是弄清楚如何分离出分子和分母,以便我可以操纵它们。任何想法将不胜感激!

非常感谢。

1 个答案:

答案 0 :(得分:1)

检查此解决方案:解决此问题的步骤的注释中提到了解释

    static void Main(string[] args)
    {
        string fraction = GetSimplifiedFraction("-5/25");// Here We are passing the fraction we want to simplyfy
        Console.WriteLine(fraction);
    }

    public static string GetSimplifiedFraction(string fraction)
    {
        string[] numbers = fraction.Split('/');// spliting the fraction to get numerator and denominator
        int numrator = int.Parse(numbers[0]);
        int denomenator = int.Parse(numbers[1]);
        List<int> numeratorFactors = GetFactors(numrator);// Getting the List of Fraction of the numerator in descending order
        List<int> denominatorFactors = GetFactors(denomenator);// Getting the List of Fraction of the numerator in descending order
        int commonFactor = int.MinValue;

        if (numeratorFactors.Count > denominatorFactors.Count)// Here we checking which fraction has more number of Factors. This is required to find the greatest common factor between both the numbers
        {
            foreach (var num in numeratorFactors)
            {
                if (denominatorFactors.Contains(num))
                {
                    commonFactor = num;// Here we are assigning the greatest common Factor
                    break;
                }
            }
        }
        else
        {
            foreach (var num in denominatorFactors)
            {
                if (numeratorFactors.Contains(num))
                {
                    commonFactor = num;// Here we are assigning the greatest common Factor
                    break;
                }
            }

        }

        if (commonFactor == int.MinValue)// If there is no common factor than the fraction can not be simplified hence returing the same fraction Back
        {
            return fraction;
        }
        else
        {
            numrator = numrator / commonFactor;// Once we found the common factor we need to divide the numerator and denominator with the fraction
            denomenator = denomenator / commonFactor;
            return numrator.ToString() + "/" + denomenator.ToString();// here we are forming the simplified fraction
        }

    }

    public static List<int> GetFactors(int number)
    {
        if (number < 0) number = -number;
        List<int> factors = new List<int>();
        factors.Add(number);
        for(int i = 1; i < number / 2; i++)
        {
            if (number % i == 0)
            {
                factors.Add(i);
            }
        }

        return factors.OrderByDescending(_=>_).ToList();//sorting the fraction in descending
    }