本文整理汇总了C#中Fraction.Simplify方法的典型用法代码示例。如果您正苦于以下问题:C# Fraction.Simplify方法的具体用法?C# Fraction.Simplify怎么用?C# Fraction.Simplify使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Fraction
的用法示例。
在下文中一共展示了Fraction.Simplify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
/// <summary>
/// Subtraction
/// </summary>
/// <param name="fractA"></param>
/// <param name="fractB"></param>
/// <returns></returns>
public static Fraction operator -(Fraction fractA, Fraction fractB)
{
//addition
fractA = fractA.Simplify(); // simplifying
fractB = fractB.Simplify();
if (fractA.Denominator == fractB.Denominator)
{
var fraction = new Fraction //creating a new fraction
{
Numerator = fractA.Numerator - fractB.Numerator, // subtracting the nominators
Denominator = fractA.Denominator // assigning the denominator
};
return fraction.Simplify(); // a simplified version of the fraction
}
var gdc = Numbers.Get.Gdc(fractA.Denominator, fractB.Denominator);
if (gdc == 1)
{
// if the denominators are coprimes, i.e. no factors in common
var fraction = new Fraction
{
Numerator = fractA.Numerator * fractB.Denominator - fractB.Numerator * fractA.Denominator,
Denominator = fractA.Denominator * fractB.Denominator
};
return fraction.Simplify(); // a simplified version of the fraction
}
if (fractA.Denominator > fractB.Denominator) //fractA.Denominator will be the new denominator
{
var fraction = new Fraction
{
Numerator = fractA.Numerator - fractB.Numerator * gdc,
Denominator = fractA.Denominator
};
return fraction.Simplify();
}
else //fractB.Denominator will be the new denominator
{
var fraction = new Fraction
{
Numerator = fractB.Numerator - fractA.Numerator * gdc,
Denominator = fractB.Denominator
};
return fraction.Simplify();
}
}