如何使用Reduce()方法并在C#控制台程序中转换不正确的分数

时间:2012-07-03 00:08:41

标签: c#

我还是C#编程的新手,我被困在以下项目中。我知道这一定很容易,但是我对于放置Reduce()方法的位置以及如何通过消除整数并将其调用Reduce()方法来转换分数进行了大脑锁定。这是项目:

添加一个添加两个分数的运算符+()方法。要添加两个分数,首先要消除值的任何整数部分。例如,2 1/4变为9/4,1 3/5变为8/5。找到一个共同的分母并将分数转换为它。例如,添加9/4和8/5时,可以将它们转换为45/20和32/20。然后,您可以添加分子,得到77/20。最后,调用Reduce()方法来减少结果,恢复任何整数值,使得数字的小数部分小于1.例如,77/20变为3 17/20。

这是我到目前为止的代码。我也会遇到一个问题,即将这些分数相乘,然后再创建一个数组,但是现在我想尝试一步一步。任何和所有的回复将不胜感激。

public class Fraction
{
    public int WholeNumber { get; private set; }
    public int Numerator { get; private set; }
    public int Denominator { get; private set; } 

    private void Reduce()
    {
        if (Numerator < Denominator)
        {
            WholeNumber = 0;
            return;
        }
        if (Numerator == Denominator)
        {
            WholeNumber = 1;
            Numerator = 0;
            return;
        }
        if (Numerator > Denominator)
        {
            WholeNumber = (int)Math.Floor((decimal)Numerator / Denominator);
            Numerator = Numerator - WholeNumber * Denominator;
        }
    } 

    //constructor with three parameters 
    public Fraction(int wholenumber, int numerator, int denominator)
    {
        WholeNumber = wholenumber;
        Numerator = WholeNumber * Denominator + Numerator;
        Reduce();
    }

    //constructor with two parameters 
    public Fraction(int numerator, int denominator)
    {
        WholeNumber = 0;
        Numerator = numerator;
        Denominator = denominator;
        Reduce();
    } 

    //parameter less with set values
    public Fraction()
    {
        WholeNumber = 0;
        Numerator = 0;
        Denominator = 1;
    }

    public static Fraction operator +(Fraction firstfraction, Fraction secondfraction)
    {
        if (firstfraction.Denominator == secondfraction.Denominator)
        {
            int firstProduct = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
            int secondProduct = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
            return (new Fraction(firstProduct + secondProduct, firstfraction.Denominator));
        }
        else
        {
            int commondenominator = firstfraction.Denominator * secondfraction.Denominator;
            int firstProduct = ((firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator) * secondfraction.Denominator;
            int secondProduct = ((secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator) * firstfraction.Denominator;
            return (new Fraction(firstProduct + secondProduct, commondenominator));
        }
    } 


    public Fraction Reduce(int WholeNumber, int Numerator, int Denominator)
    {
        int reduceWhole = (int)WholeNumber;
        while (Numerator > Denominator)
        {
            Numerator -= Denominator;
            reduceWhole += 1;
        }
        while (Denominator % Numerator == 0)
        {
            int factor = (int)Denominator / Numerator;
            Numerator = 1;
            Denominator = factor;
        }

        return new Fraction(reduceWhole, Numerator, Denominator);
    } 

}//end class

class Program
{
    static void Main(string[] args)
    {
        Fraction firstfraction = new Fraction();
        Fraction secondfraction = new Fraction();
        Fraction total = firstfraction + secondfraction;
        Console.WriteLine(total);
        Console.Write("Enter whole number portion of fraction: ");
        firstfraction.WholeNumber = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter numerator: ");
        firstfraction.Numerator = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter denominator: ");
        firstfraction.Denominator = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter whole number portion of fraction: ");
        secondfraction.WholeNumber = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter numerator: ");
        secondfraction.Numerator = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter denominator: ");
        secondfraction.Denominator = Convert.ToInt32(Console.ReadLine());
        Console.Write("{0} {1}/{2}", firstfraction.WholeNumber, firstfraction.Numerator,
        firstfraction.Denominator);
        Console.WriteLine(" + {0} {1}/{2}", secondfraction.WholeNumber,
        secondfraction.Numerator, secondfraction.Denominator);
        Fraction add = new Fraction();
        add = firstfraction + secondfraction;
        Console.Write("Result of adding two fractions is:{0} {1}/{2}", add.WholeNumber,
        add.Numerator, add.Denominator);
        Console.ReadLine();
    }//end main
}//end class

现在我收到3条错误消息,说明属性或索引器

  

'FractionProgram.Fraction.Denominator'不能在此上下文中使用,因为set访问器不可访问。

另外,我是否对firstProduct和secondProduct进行了正确的计算?

2 个答案:

答案 0 :(得分:3)

我不打算为你做这个问题,但我会给你一些提示,帮助你自己解决问题并学习。

首先,对于Reduce方法的放置,有几种方法可以解决它:

  • 您可以将其作为修改分数(myFraction.Reduce()
  • 值的实例方法
  • 你可以让它成为一个不修改分数的实例方法,但会返回一个新的,减少的分数(如string或你的运算符)(myFraction = myFraction.Reduce()
  • 您可以将上述选项转换为静态方法(myFraction = Fraction.Reduce(myFraction)

通过阅读作业,看起来你应该使用第一个作业。请记住,通常有不止一种方法可以为您未来的项目实现这样的功能。

至于方法的实际内容,这里有一些提示引导你走向答案。

  • 考虑整数除法及其工作原理。写下几个例子(或在代码中测试它们),看看答案是什么。
  • 考虑模数运算符。如果你还没有了解它,它是一个运算符进行整数除法,但返回余数而不是商。如果你还不允许使用它,你可以很容易地复制它的功能。

其余的你应该能够用数学方法弄清楚,但是如果你还需要帮助就可以发表评论。

答案 1 :(得分:0)

你非常亲密,所以你值得赞美。 Reduce()方法应该是分数类的一部分,并在构造函数中调用。您应该将您的媒体资源WholeNumberNumeratorDenominator设为只读,以确保您的分数保持一致:

public int WholeNumber { get; private set; }
public int Numerator { get; private set; }
public int Denominator { get; private set; }

该方法将根据WholeNumberNumerator计算Denominator,就像整个数字始终包含在分数中一样。它应该处理分子大于分母,等于等等的情况。这是Reduce()方法:

private void Reduce()
{
    if (Numerator < Denominator)
    {
        WholeNumber = 0;
        return;
    }
    if (Numerator == Denominator)
    {
        WholeNumber = 1;
        Numerator = 0;
        return;
    }
    if (Numerator > Denominator)
    {
        WholeNumber = (int)Math.Floor((decimal)Numerator / Denominator);
        Numerator = Numerator - WholeNumber * Denominator;
    }
}

构造函数变为:

//constructor with three parameters
public Fraction(int wholenumber, int numerator, int denominator)
{
    WholeNumber = wholenumber;
    Numerator = WholeNumber * denominator + numerator;
    Denominator = denominator;

    Reduce();
}

//constructor with two parameters
public Fraction(int numerator, int denominator)
{
    WholeNumber = 0;
    Numerator = numerator;
    Denominator = denominator;
    Reduce();
}

然后,运营商+()变为:

public static Fraction operator+(Fraction firstfraction, Fraction secondfraction)
{
    if (firstfraction.Denominator == secondfraction.Denominator)
    {
        int firstProduct = (firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator;
        int secondProduct = (secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator;
        return (new Fraction(firstProduct + secondProduct, firstfraction.Denominator));
    }
    else
    {
        int commondenominator = firstfraction.Denominator * secondfraction.Denominator;
        int firstProduct = ((firstfraction.WholeNumber * firstfraction.Denominator) + firstfraction.Numerator) * secondfraction.Denominator;
        int secondProduct = ((secondfraction.WholeNumber * secondfraction.Denominator) + secondfraction.Numerator) * firstfraction.Denominator;
        return (new Fraction(firstProduct + secondProduct, commondenominator));
    }
}