/******************************************* * Math operators + - * / * *****************************************/ public static Fraction operator +(Fraction f1, Fraction f2) { Fraction f3 = new Fraction(); Fraction f4; //these recieve values from the out parameters Fraction f5; if (f1.denominator != f2.denominator) { //the denominators don't match //get the greatest common denominator //the out parameters provide a way of returning //more than one value from a method f3.GreatestCommonDenominator(f1, f2, out f4, out f5); } else { //if they do match we still need to assign //the values to the new fractions so we //can do the math below f4 = f1; f5 = f2; } f3.denominator = f4.denominator; f3.numerator = f4.numerator + f5.numerator; //reduce the resulting fraction f3 = f3.ReduceFraction(f3); return(f3); }
public static Fraction operator -(Fraction f1, Fraction f2) { Fraction f3 = new Fraction(); Fraction f4; Fraction f5; if (f1.denominator != f2.denominator) { f3.GreatestCommonDenominator(f1, f2, out f4, out f5); } else { f4 = f1; f5 = f2; } f3.denominator = f4.denominator; f3.numerator = f4.numerator - f5.numerator; f3 = f3.ReduceFraction(f3); return(f3); }
public static Fraction operator *(Fraction f1, Fraction f2) { Fraction f = new Fraction(); Fraction f3 = new Fraction(); f3.numerator = f1.numerator * f2.numerator; f3.denominator = f1.denominator * f2.denominator; f3 = f.ReduceFraction(f3); return(f3); }
public static Fraction operator /(Fraction f1, Fraction f2) { Fraction f = new Fraction(); Fraction f3 = new Fraction(f2.denominator, f2.numerator); Fraction f4 = new Fraction(); f4.numerator = f1.numerator * f3.numerator; f4.denominator = f1.denominator * f3.denominator; f4 = f.ReduceFraction(f4); return(f4); }