/// <summary> /// Multiply two fractions /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns>The product of a and b</returns> public static FractionModel Multiply(FractionModel a, FractionModel b) { var res = new FractionModel(a.Numerator * b.Numerator, a.Denominator * b.Denominator); res.Simplify(); return(res); }
/// <summary> /// Reduce a fraction /// </summary> /// <param name="m">The fraction to reduce</param> public static void Simplify(this FractionModel m) { int gcd = FindGreatestCommonFactor(m); m.Numerator /= gcd; m.Denominator /= gcd; //return new FractionModel(m.Numerator / gcd, m.Denominator / gcd); }
/// <summary> /// Subtracts two fractions /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns>The difference of a and f=b</returns> public static FractionModel Subtract(FractionModel a, FractionModel b) { int lcm = FindLeastCommonMultiple(a.Denominator, b.Denominator); int num = a.Numerator * (lcm / a.Denominator) - b.Numerator * (lcm / b.Denominator); FractionModel res = new FractionModel(num, lcm); res.Simplify(); return(res); }
private void buttonSimplify_Click(object sender, System.EventArgs e) { FractionModel f = (FractionModel)listBoxFractions.SelectedItem; if (listBoxFractions.SelectedItem != null) { f.Simplify(); fractions.ResetBindings(); } }
/// <summary> /// Convert a decimal to a fraction /// </summary> /// <param name="d">The decimal to convert</param> /// <returns>The decimal represented as a fraction</returns> public static FractionModel ConvertDecimalToFraction(decimal d) { double decimalPlaces = NumberOfDecimalPlaces(d); int multiplier = (int)Math.Pow(10, decimalPlaces); FractionModel fraction = new FractionModel((int)(d * multiplier), multiplier); fraction.Simplify(); return(fraction); }
/// <summary> /// Divides two fractions /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns>The quotient of a and b</returns> public static FractionModel Divide(FractionModel a, FractionModel b) { var res = Multiply(a, GetReciprocal(b)); res.Simplify(); if (res.Denominator < 0 && res.Numerator < 0) { res.Numerator = Math.Abs(res.Numerator); res.Denominator = Math.Abs(res.Denominator); } return(res); }
/// <summary> /// Try to parse a fraction /// </summary> /// <param name="value">The value to parse</param> /// <param name="fraction">The result of parsing value, or 0 on failure</param> /// <returns>True on success, false on failure</returns> public static bool TryParseFraction(string value, out FractionModel fraction) { fraction = new FractionModel(0, 1); try { fraction = ParseFraction(value); } catch (ArgumentException) { return(false); } return(true); }
private void AddFromFractionTextBox() { if (textBoxNumerator.Text.Length > 0 || textBoxDenominator.Text.Length > 0) { MessageBox.Show("Please use only one method of creating a fraction at a time!"); return; } if (FractionModel.TryParse(textBoxFraction.Text, out FractionModel res)) { fractions.Add(res); } else { MessageBox.Show("Please enter a valid fraction"); } }
private void AddFromNumDemTextBoxes() { int numerator; int denominator; if (!int.TryParse(textBoxNumerator.Text, out numerator)) { MessageBox.Show("Please enter a valid numerator!"); textBoxNumerator.Text = ""; return; } if (!int.TryParse(textBoxDenominator.Text, out denominator) || denominator == 0) { MessageBox.Show("Please enter a valid denominator!"); textBoxDenominator.Text = ""; return; } FractionModel fraction = new FractionModel(numerator, denominator); fractions.Add(fraction); }
/// <summary> /// Finds the greatest common divisor of a fraction /// </summary> /// <param name="model">The fraction model to find the GCD of</param> /// <returns>The greatest common divisor</returns> public static int FindGreatestCommonFactor(FractionModel model) { return(FindGreatestCommonFactor(model.Denominator, model.Numerator)); }
/// <summary> /// Return the number of places after the decimal point /// </summary> /// <param name="model">The FractionModel to check</param> /// <returns>The number of places after the decimal point</returns> public static int NumberOfDecimalPlaces(FractionModel model) { decimal number = decimal.Divide(model.Numerator, model.Denominator); return(NumberOfDecimalPlaces(number)); }
/// <summary> /// Get the Reciprocal of a fraction /// </summary> /// <param name="model">The fraction to get the Reciprocal of</param> /// <returns>The reciprocal of the fraction</returns> public static FractionModel GetReciprocal(FractionModel model) { return(new FractionModel(model.Denominator, model.Numerator)); }