C# programming. 2. Create a fractions class that represents fractions in the for
ID: 3673507 • Letter: C
Question
C# programming.
2. Create a fractions class that represents fractions in the form a/b Your class should implement the following members int Numerator int Denominator Fraction(int numerator, int denominator) - creates a new Fraction double ToDecimal() - returns the fraction as a double Fraction Add(Fraction f) - adds the fraction to the one passed in and simplifies the result Fraction Multiply(Fraction f) - multiplies the fraction by the one passed in and simplifies the result Fraction Simplify() - simplifies the fraction using the GreatestCommonDivisor method from the first Exercise Create a test program that demonstrates the following 1/2 = 0.5 1/7 + 1/5 = 12/35 1/4 * 2/3 * 4/5 = 2/15
Here is the GCD method I already wrote:
public static int GreatestCommonDivisor(int a, int b)
{
if (b == 0)
return a;
else
return GreatestCommonDivisor(b, a % b);
}
}
}
Thanks!!
Explanation / Answer
Fraction.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication_GCDFraction
{
class Fraction
{
public int Numerator, Denominator;
// Initialize the fraction from a string A/B.
public Fraction(string txt)
{
string[] pieces = txt.Split('/');
Numerator = int.Parse(pieces[0]);
Denominator = int.Parse(pieces[1]);
Simplify();
}
// Initialize the fraction from numerator and denominator.
public Fraction(int numer, int denom)
{
Numerator = numer;
Denominator = denom;
Simplify();
}
// Return a * b.
public static Fraction operator *(Fraction a, Fraction b)
{
// Swap numerators and denominators to simplify.
Fraction result1 = new Fraction(a.Numerator, b.Denominator);
Fraction result2 = new Fraction(b.Numerator, a.Denominator);
return new Fraction(
result1.Numerator * result2.Numerator,
result1.Denominator * result2.Denominator);
}
// Return -a.
public static Fraction operator -(Fraction a)
{
return new Fraction(-a.Numerator, a.Denominator);
}
// Return a + b.
public static Fraction operator +(Fraction a, Fraction b)
{
// Get the denominators' greatest common divisor.
int gcd_ab = GCD(a.Denominator, b.Denominator);
int numer =
a.Numerator * (b.Denominator / gcd_ab) +
b.Numerator * (a.Denominator / gcd_ab);
int denom =
a.Denominator * (b.Denominator / gcd_ab);
return new Fraction(numer, denom);
}
// Return a - b.
public static Fraction operator -(Fraction a, Fraction b)
{
return a + -b;
}
// Return a / b.
public static Fraction operator /(Fraction a, Fraction b)
{
return a * new Fraction(b.Denominator, b.Numerator);
}
// Simplify the fraction.
private void Simplify()
{
// Simplify the sign.
if (Denominator < 0)
{
Numerator = -Numerator;
Denominator = -Denominator;
}
// Factor out the greatest common divisor of the
// numerator and the denominator.
int gcd_ab = GCD(Numerator, Denominator);
Numerator = Numerator / gcd_ab;
Denominator = Denominator / gcd_ab;
}
// Convert a to a double.
public static implicit operator double (Fraction a)
{
return (double)a.Numerator / a.Denominator;
}
// Return the fraction's value as a string.
public override string ToString()
{
return Numerator.ToString() + "/" + Denominator.ToString();
}
public static int GCD(int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
//if (b == 0)
// return a;
//else
//{
// GCD(b, a%b);
//}
}
}
}
________________________________________________________________________________
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication_GCDFraction
{
class Program
{
static void Main(string[] args)
{
string strf1 = "1/2";
string strf2 = "1/4";
string strf3 = "2/4";
string strf4 = "2/5";
Fraction f = new Fraction(strf1);
Fraction f1 = new Fraction(strf2);
Fraction b = new Fraction(strf3) + new Fraction(strf4);
Console.WriteLine("Fraction for " + strf1 + " is: " + f.ToString());
Console.WriteLine("Fraction for " + strf2 + " is: " + f1.ToString());
Console.WriteLine("Add Fraction for " + strf3 + " and " + strf4 + " is: " + b.ToString());
Console.WriteLine("Subtract Fraction for " + strf3 + " and " + strf4 + " is: " + (new Fraction(strf3) - new Fraction(strf4)).ToString());
Console.WriteLine("Multiplication Fraction for " + strf3 + " and " + strf4 + " is: " + (new Fraction(strf3) * new Fraction(strf4)).ToString());
Console.WriteLine("Divide Fraction for " + strf3 + " and " + strf4 + " is: " + (new Fraction(strf3) / new Fraction(strf4)).ToString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.