private static string FormatFraction(MixedFraction frac) { if (frac.IntegerPart == 0) { if (frac.Numerator == 0) { return("0"); } else { return($"{frac.Numerator}/{frac.Denominator}"); } } else { if (frac.Numerator == 0) { return(frac.IntegerPart.ToString()); } else { return($"{frac.IntegerPart} {frac.Numerator}/{frac.Denominator}"); } } }
public static void Main(string[] args) { MixedFraction mF = new MixedFraction(); string[] lines = System.IO.File.ReadAllLines("fracciones.in"); foreach (string actual in lines) { string[] getNumbers = actual.Split(' '); if (getNumbers[0].Equals("0") && getNumbers[1].Equals("0")) { break; } int n = Int32.Parse(getNumbers[0]); int d = Int32.Parse(getNumbers[1]); Console.WriteLine("Regular Form: " + n + '/' + d); Console.WriteLine("Mixed Fraction: " + mF.transform_MF(n, d)); Console.WriteLine("===================================="); } Console.Read(); }
internal FractionResult(DodoWareResult dodoWareResult) { DodoWareResult = dodoWareResult; var elemList = XmlHelper.SelectElements(dodoWareResult.XmlResult, "//div[@class='mixed-fraction']", 3); InputFraction1 = new MixedFraction(elemList[0]); InputFraction2 = new MixedFraction(elemList[1]); OutputFraction = new MixedFraction(elemList[2]); }
//multiply two fractions that may also be mixed fractions public static string Multiply(MixedFraction left, MixedFraction right) { //multiply the fraction var final = new MixedFraction() { Numerator = left.Numerator * right.Numerator, Denominator = left.Denominator * right.Denominator, WholeNumber = left.WholeNumber * right.WholeNumber }; return(ConvertFractionToString(final)); }
//Subtract two fractions that may also be mixed fractions public static string Subtract(MixedFraction left, MixedFraction right) { //Subtract the fraction var final = new MixedFraction() { Numerator = (left.Numerator * right.Denominator) - (left.Denominator * right.Numerator), Denominator = left.Denominator * right.Denominator, WholeNumber = left.WholeNumber - right.WholeNumber }; return(ConvertFractionToString(final)); }
//convert fraction to string output private static string ConvertFractionToString(MixedFraction fraction) { //whole number doesn't exist, expecting a fraction if (fraction.WholeNumber <= 0) { //if denominator is 1 or 0 then the numerator is a whole number if (fraction.Denominator <= 1) { return(fraction.Numerator.ToString()); } //if improper fraction convert to mixed fraction if (fraction.Numerator > fraction.Denominator) { //get whole number var wholeNumber = (int)(fraction.Numerator / fraction.Denominator); //get remainder var remainder = fraction.Numerator % fraction.Denominator; fraction.WholeNumber = wholeNumber; fraction.Numerator = remainder; if (remainder == 0) { return(fraction.WholeNumber.ToString()); } fraction.ReduceFraction(fraction.Numerator, fraction.Denominator); return(fraction.WholeNumber.ToString() + "_" + fraction.Numerator + "/" + fraction.Denominator); } //expected fraction //reduce the fraction fraction.ReduceFraction(fraction.Numerator, fraction.Denominator); return(fraction.Numerator + "/" + fraction.Denominator); } //expected to contain a whole number, return it else { return(fraction.WholeNumber.ToString()); } }
public static string CalculateOperations(string input) { //split the input based on spaces var operations = input.Split(' ').ToList(); //remove all extra spaces operations.RemoveAll(x => x == ""); //incorrect entry, missing spaces if (operations.Count < 3) { throw new Exception("Incorrect entry, please check your input for spaces in between the operator and the fraction"); } //list of legal operators var LegalOperators = new char[] { '*', '/', '+', '-' }; //loop through legal operators and perform calculations for (int i = 0; i < LegalOperators.Count(); i++) { //make sure there are enough fractions to perform a calculation if (operations.Count >= 3) { var _operator = LegalOperators[i]; //get the index of the matching legal operator var _operatorIndex = operations.IndexOf(_operator.ToString()); while (_operatorIndex != -1) { //get the left and right fractions to multiply, these would be before and after the operator var leftString = operations[_operatorIndex - 1]; var rightString = operations[_operatorIndex + 1]; //validate left and right functions, returns a Mixed Object var left = Validate(leftString); var right = Validate(rightString); //Convert the mixed number to improper fractions left.ConvertMixedFractionToImproperFraction(); right.ConvertMixedFractionToImproperFraction(); switch (_operator.ToString()) { //perform multiplication case "*": operations[_operatorIndex] = Operations.Multiply(left, right); break; //perform divide case "/": operations[_operatorIndex] = Operations.Divide(left, right);; break; //perform add case "+": operations[_operatorIndex] = Operations.Add(left, right);; break; //perform subtract case "-": operations[_operatorIndex] = Operations.Subtract(left, right);; break; } //add result and remove fractions operations.RemoveAt(_operatorIndex + 1); operations.RemoveAt(_operatorIndex - 1); _operatorIndex = operations.IndexOf(_operator.ToString()); } } if (operations.Count == 1) { //return result if only one entry left return(operations[0]); } } //couldn't find legal operators throw new Exception("Input didn't contain legal operators"); //validates string value, expected value to include mixed fractions ("2_1/2") fractions ("1/2") and whole numbers ("2") MixedFraction Validate(string value) { if (value.Contains('/')) { //if the value contains the character '_ 'it's expected to be a mixed number along with '/' if (value.Contains('_')) { var stringFraction = value.Split('_'); //validate and create the mixed fraction if (int.TryParse(stringFraction[0], out int wholeNumber)) { //convert the fraction from the mixed fraction to a MixedFraction object var mixedFraction = new MixedFraction(stringFraction[1]) { //set the whole number WholeNumber = wholeNumber }; return(mixedFraction); } else { throw new Exception("illegal character - " + wholeNumber); } } else { //expected to be a fraction return(new MixedFraction(value)); } } else { //expected to be a whole number if (int.TryParse(value, out int wholeNumber)) { return(new MixedFraction(wholeNumber)); } else { throw new Exception("illegal character - " + wholeNumber); } } } }