【问题标题】:Beginner - C# Fractional Calculator simplification of negative fractions初学者 - 负分数的 C# 分数计算器简化
【发布时间】:2015-04-30 22:40:09
【问题描述】:

我在尝试让我的分数计算器工作时遇到了麻烦。我正在尝试简化工作,它可以正确地简化正分数,但是如果我要输入负分数,它不会简化它,我不确定我做错了什么,我已经阅读在它上面多次(Gcd 和 Reduce 函数)。

我对所有这些都是新手,感谢任何帮助。

我的 Reduce 和 GCD 函数:

public int gcd()
{
    // assigned x and y to the answer Numerator/Denominator, as well as an  
    // empty integer, this is to make code more simple and easier to read
    int x = answerNumerator;
    int y = answerDenominator;
    int m;

    // check if numerator is greater than the denominator, 
    // make m equal to denominator if so
    if (x > y)
        m = y;
    else
        // if not, make m equal to the numerator
        m = x;

    // assign i to equal to m, make sure if i is greater
    // than or equal to 1, then take away from it
    for (int i = m; i >= 1; i--)
    {
        if (x % i == 0 && y % i == 0)
        {
            //return the value of i
            return i;
        }
    }

    return 1;
}

public void Reduce()
{
    try
    {
        //assign an integer to the gcd value
        int gcdNum = gcd();

        if (gcdNum != 0)
        {
            answerNumerator = answerNumerator / gcdNum;
            answerDenominator = answerDenominator / gcdNum;
        }

        if (answerDenominator < 0)
        {
            answerDenominator = answerDenominator * -1;
            answerNumerator = answerNumerator * -1;
        }
    }
    catch (Exception exp)
    {
        // display the following error message 
        // if the fraction cannot be reduced
        throw new InvalidOperationException(
            "Cannot reduce Fraction: " + exp.Message);
    }
}

【问题讨论】:

  • 一般性评论:如果您的函数采用参数而不是依赖和作用于全局变量,您的函数会更好。
  • 你能举一个“负分数”的例子吗?是否只是answerNumeratoranswerDenominator 小于零?
  • 我的意思是负分数。我在计算器中输入 1 1/2 - 4 1/4 得到 -2 -6/8 而不是 -2 3/4。
  • 你这是什么意思?
  • 该代码未显示,但在内部您只存储分子和分母,对吗?因此,即使您显示 1 1/2,在内部您也将其存储为 numerator = 3; denominator = 2?

标签: c# calculator simplify


【解决方案1】:

我认为问题在于,当您确定 GCD 时,您正在检查 &gt;= 1 循环中的 &gt;= 1 值,即使它可能是负数。为了避免这种情况,您应该在确定 GCD 时捕获分子和分母的绝对值。

例如,这应该可以解决它:

public int gcd()
{
    // assigned x and y to the absolute values of the answer Numerator/Denominator, 
    // as well as an empty integer, this is to make code more simple and easier to read
    int x = Math.Abs(answerNumerator);
    int y = Math.Abs(answerDenominator);
    int m;

    // check if numerator is greater than the denominator, 
    // make m equal to denominator if so
    if (x > y)
        m = y;
    else
        // if not, make m equal to the numerator
        m = x;

    // assign i to equal to m, make sure if i is greater
    // than or equal to 1, then take away from it
    for (int i = m; i >= 1; i--)
    {
        if (x % i == 0 && y % i == 0)
        {
            //return the value of i
            return i;
        }
    }

    return 1;
}

【讨论】:

    【解决方案2】:

    简答

    你需要:

    int x = Math.Abs(answerNumerator);
    int y = Math.Abs(answerDenominator);
    

    运行代码

    这是一个正在运行的 Fiddle:https://dotnetfiddle.net/nBzr0i

    输出:

    Initial: 2/4
    Reduced: 1/2
    ---
    Initial: 2/-4
    Reduced: -1/2
    ---
    

    运行代码:

    using System;
    
    public class Program
    {
        public static void Main()
        {
            Calc.Reduce(2,4);
            Calc.Reduce(2,-4);
        }
    }
    
    public static class Calc
    {
        public static int gcd(int answerNumerator, int answerDenominator)
        {
            // assigned x and y to the answer Numerator/Denominator, as well as an  
            // empty integer, this is to make code more simple and easier to read
            int x = Math.Abs(answerNumerator);
            int y = Math.Abs(answerDenominator);
            int m;
            // check if numerator is greater than the denominator, 
            // make m equal to denominator if so
            if (x > y)
                m = y;
            else
                // if not, make m equal to the numerator
                m = x;
            // assign i to equal to m, make sure if i is greater
            // than or equal to 1, then take away from it
            for (int i = m; i >= 1; i--)
            {
                if (x % i == 0 && y % i == 0)
                {
                    //return the value of i
                    return i;
                }
            }
    
            return 1;
        }
    
        public static void Reduce(int answerNumerator, int answerDenominator)
        {
            Console.Write("Initial: ");
            WriteFraction(answerNumerator, answerDenominator);
    
            try
            {
                //assign an integer to the gcd value
                int gcdNum = gcd(answerNumerator, answerDenominator);
                if (gcdNum != 0)
                {
                    answerNumerator = answerNumerator / gcdNum;
                    answerDenominator = answerDenominator / gcdNum;
                }
    
                if (answerDenominator < 0)
                {
                    answerDenominator = answerDenominator * -1;
                    answerNumerator = answerNumerator * -1;
                }
            }
            catch (Exception exp)
            {
                // display the following error message 
                // if the fraction cannot be reduced
                throw new InvalidOperationException("Cannot reduce Fraction: " + exp.Message);
            }
    
            Console.Write("Reduced: ");
            WriteFraction(answerNumerator, answerDenominator);
            Console.WriteLine("---");
        }
    
        public static void WriteFraction(int answerNumerator, int answerDenominator)
        {
            Console.WriteLine(string.Format("{0}/{1}", answerNumerator, answerDenominator));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多