FatJohnson6
FatJohnson6
CC#
Created by FatJohnson6 on 3/3/2023 in #help
❔ Looking to turn this GUI integer calculator into a Fraction calculator using the provided class
Final project for school, need to create a GUI fraction calculator. I have a GUI integer calculator already built, and a Fraction class already built. Currently stuck on where to even begin to have the GUI calculate fractions vs. integers. GUI Code Attached Fraction Class
namespace FractionCalculatorWithGUI_Final
{
internal class Fraction : IComparable
{
private int numerator = 0;
private int denominator = 1;
public int CompareTo(object rightObject)
{
// typecast the object parameter to a Fraction
Fraction rightFrac = (Fraction)rightObject;

// get both Fractions as (comparable) doubles
double f1 = (double)(this.numerator) / (double)(this.denominator);
double f2 = (double)(rightFrac.numerator) / (double)(rightFrac.denominator);

// compare the 2 doubles
int retVal = 0;
if (f1 < f2)
retVal = -1;
else if (f1 == f2)
retVal = 0;
else
retVal = 1;

return retVal;
}
public override bool Equals(object obj)
{
bool equals = false;
if (obj is Fraction)
equals = (this == (Fraction)obj); // assumes you have operator == overridden
return equals;
}
public static Fraction Parse(String str)
{

Fraction newFraction = new Fraction();

int indexSlash = str.IndexOf("/");
if (indexSlash == -1)
{
throw new ArgumentException("'/' Character not detected. Please input '/' when writing the fraction.");
}
newFraction.numerator = int.Parse(str.Substring(0, indexSlash));
newFraction.denominator = int.Parse(str.Substring(indexSlash + 1));

return newFraction;
}

public static Fraction operator + (Fraction firstFrac, Fraction secondFrac)
{
Fraction sum = new Fraction();
sum.numerator = ((firstFrac.numerator * secondFrac.denominator) + (secondFrac.numerator * firstFrac.denominator));
sum.denominator = firstFrac.denominator * secondFrac.denominator;
return sum;
}
public static Fraction operator - (Fraction firstFrac, Fraction secondFrac)
{
Fraction difference = new Fraction();
difference.numerator = ((firstFrac.numerator * secondFrac.denominator) - (secondFrac.numerator * firstFrac.denominator));
difference.denominator = firstFrac.denominator * secondFrac.denominator;
return difference;
}
public static Fraction operator * (Fraction firstFrac, Fraction secondFrac)
{
Fraction product = new Fraction();
product.numerator = firstFrac.numerator * secondFrac.numerator;
product.denominator = firstFrac.denominator * secondFrac.denominator;
return product;
}
public static Fraction operator / (Fraction firstFrac, Fraction secondFrac)
{
Fraction dividend = new Fraction();
dividend.numerator = firstFrac.numerator * secondFrac.denominator;
dividend.denominator = firstFrac.denominator * secondFrac.numerator;
return dividend;
}



override public String ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}
}
}
namespace FractionCalculatorWithGUI_Final
{
internal class Fraction : IComparable
{
private int numerator = 0;
private int denominator = 1;
public int CompareTo(object rightObject)
{
// typecast the object parameter to a Fraction
Fraction rightFrac = (Fraction)rightObject;

// get both Fractions as (comparable) doubles
double f1 = (double)(this.numerator) / (double)(this.denominator);
double f2 = (double)(rightFrac.numerator) / (double)(rightFrac.denominator);

// compare the 2 doubles
int retVal = 0;
if (f1 < f2)
retVal = -1;
else if (f1 == f2)
retVal = 0;
else
retVal = 1;

return retVal;
}
public override bool Equals(object obj)
{
bool equals = false;
if (obj is Fraction)
equals = (this == (Fraction)obj); // assumes you have operator == overridden
return equals;
}
public static Fraction Parse(String str)
{

Fraction newFraction = new Fraction();

int indexSlash = str.IndexOf("/");
if (indexSlash == -1)
{
throw new ArgumentException("'/' Character not detected. Please input '/' when writing the fraction.");
}
newFraction.numerator = int.Parse(str.Substring(0, indexSlash));
newFraction.denominator = int.Parse(str.Substring(indexSlash + 1));

return newFraction;
}

public static Fraction operator + (Fraction firstFrac, Fraction secondFrac)
{
Fraction sum = new Fraction();
sum.numerator = ((firstFrac.numerator * secondFrac.denominator) + (secondFrac.numerator * firstFrac.denominator));
sum.denominator = firstFrac.denominator * secondFrac.denominator;
return sum;
}
public static Fraction operator - (Fraction firstFrac, Fraction secondFrac)
{
Fraction difference = new Fraction();
difference.numerator = ((firstFrac.numerator * secondFrac.denominator) - (secondFrac.numerator * firstFrac.denominator));
difference.denominator = firstFrac.denominator * secondFrac.denominator;
return difference;
}
public static Fraction operator * (Fraction firstFrac, Fraction secondFrac)
{
Fraction product = new Fraction();
product.numerator = firstFrac.numerator * secondFrac.numerator;
product.denominator = firstFrac.denominator * secondFrac.denominator;
return product;
}
public static Fraction operator / (Fraction firstFrac, Fraction secondFrac)
{
Fraction dividend = new Fraction();
dividend.numerator = firstFrac.numerator * secondFrac.denominator;
dividend.denominator = firstFrac.denominator * secondFrac.numerator;
return dividend;
}



override public String ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}
}
}
111 replies