How can you add two fraction in c# like this:
11/6 + 3/4 = 31/12
and then how would you simplify the answer?
As boredom strikes on a Friday night... resulting in a Fraction class with overloaded operators. Sobriety of design cannot be guaranteed.
It works like this...
Fraction left = "1/2";
Fraction right = "3/8";
Fraction result1 = ((left + right) * left / right).Simplify();
Console.WriteLine(result1);
Fraction test2 = "12/32";
test2 = test2.Simplify();
Console.WriteLine(test2);
Implementation something like below...
public struct Fraction
{
public int Numerator { get; set; }
public int Denominator { get; set; }
public Fraction(int numerator, int denominator)
: this()
{
Numerator = numerator;
Denominator = denominator;
}
public Fraction Simplify()
{
int gcd = GCD();
return new Fraction(Numerator / gcd, Denominator / gcd);
}
public Fraction InTermsOf(Fraction other)
{
return Denominator == other.Denominator ? this :
new Fraction(Numerator * other.Denominator, Denominator * other.Denominator);
}
public int GCD()
{
int a = Numerator;
int b = Denominator;
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
public Fraction Reciprocal()
{
return new Fraction(Denominator, Numerator);
}
public static Fraction operator +(Fraction left, Fraction right)
{
var left2 = left.InTermsOf(right);
var right2 = right.InTermsOf(left);
return new Fraction(left2.Numerator + right2.Numerator, left2.Denominator);
}
public static Fraction operator -(Fraction left, Fraction right)
{
var left2 = left.InTermsOf(right);
var right2 = right.InTermsOf(left);
return new Fraction(left2.Numerator - right2.Numerator, left2.Denominator);
}
public static Fraction operator *(Fraction left, Fraction right)
{
return new Fraction(left.Numerator * right.Numerator, left.Denominator * right.Denominator);
}
public static Fraction operator /(Fraction left, Fraction right)
{
return new Fraction(left.Numerator * right.Denominator, left.Denominator * right.Numerator);
}
public static implicit operator Fraction(string value)
{
var tokens = value.Split('/');
int num;
int den;
if (tokens.Length == 1 && int.TryParse(tokens[0], out num))
{
return new Fraction(num, 1);
}
else if (tokens.Length == 2 && int.TryParse(tokens[0], out num) && int.TryParse(tokens[1], out den))
{
return new Fraction(num, den);
}
throw new Exception("Invalid fraction format");
}
public override string ToString()
{
return string.Format("{0}/{1}", Numerator, Denominator);
}
}
.Net doesn't have built-in support (that I know of) for Rational Numbers, but there is at least one existing library out there.
Your solution will ultimately come down to storing the numerator and denominator (probably in a custom class that you create), doing arithmetic against other numerator/denominator pairs (possibly by implementing overloaded operators on your class), and applying a fraction simplification algorithm.
Here are some resources:
Existing implementations
Methods for reducing fractions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With