我制作了一个添加和减去分数的控制台应用程序,我添加了一个函数来简化:
public static Numbers Add(Numbers n1, Numbers n2)
{
int den1;
int num1;
int num2;
int dsimp;
int nsimp;
int numtop;
num1 = n1.Numerator * n2.Denominator;
num2 = n2.Numerator * n1.Denominator;
den1 = n1.Denominator * n2.Denominator;
numtop = num2 + num1;
if (numtop == 0)
{
return new Numbers(0);
}
if (numtop % n1.Denominator == 0)
{
nsimp = numtop / n1.Denominator;
dsimp = den1 / n1.Denominator;
return new Numbers(nsimp, dsimp);
}
else
{
return new Numbers(numtop, den1);
}
}
当我输入 1/2 + 4/8 时,它按照我告诉它的方式完美地简化了这一切,但是,它给了我 8/8。这需要简化为 1/1。我如何才能将已经简化的内容简化为尽可能低的分数?