2

我对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 有正确的计算吗?

4

2 回答 2

3

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

首先,对于 Reduce 方法的放置,您可以采取以下几种方法:

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

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

至于方法的实际内容,这里有一些提示可以引导您找到答案。

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

其余的你应该能够用数学计算出来,但如果你仍然需要帮助,请发表评论。

于 2012-07-03T00:38:03.380 回答
0

你很亲密,所以你值得点赞。该Reduce()方法应该是分数类的一部分并在构造函数中调用。您应该使您的属性 WholeNumber,NumeratorDenominatorreadonly 以确保您的分数的一致性:

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));
    }
}
于 2012-07-03T01:55:59.210 回答