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