## Fraction Calculator

I have created a basic form with 2 inputs and 1 output with 4 buttons that have the addition, subtracting, multiple and divide. I have a class (CRational) made where it does all the calculation and reducing for me. Now the issue is I have to use what is there and now have it perform some advance calculations. For example:

(a+bi) + (c+di) = (a+c) +(b+d)i for adding
(a+bi) - (c+di) = (a-c) +(b-d)i for subtracting
(a+bi) * (c+di) = (ac-bd) +(ad+bc)i for multiplying
(a+bi)/(c+di) = (ac+bd/c2 +d2)/(bc-ad/c2+d2)i for dividing

In this case i have to use fractions in replace of the letters.
(3/4 + 1/2i) * (1/4 -2i)
would return 19/16 - 11/8i

Now the one other thing is i have to use another class to make this work or that is what I've been told. Here is my code up to now:

Code:
```namespace Rational
{
class CRational
{
#region Fields

private int fiNum;
private int fiDenom;
private bool fbValid;

#endregion
#region Constructors

public CRational()
{
fiNum = 0;
fiDenom = 1;
fbValid = true;
}
public CRational(string strRat)
{
Parse(strRat);
}

#endregion
#region Methods

public override string ToString()
{
string strRat;
if (!fbValid)
strRat = "NaN";
else
{
strRat = fiNum.ToString();
if (fiDenom > 1)
strRat += "/" + fiDenom.ToString();
}
return strRat;
}

public void Parse(string strRat)
{
bool bSlash = false, bLegal = true, bNeg = false;
string strNum = "0", strDenom = "0";
if (strRat != "")
{
for (int i = 0; i < strRat.Length; i++)
{
if (!char.IsDigit(strRat[i]))
{
if ((i == 0 && strRat[i] == '-'))
bNeg = true;
else if ((i > 0 && strRat[i] == '/' && !bSlash))
bSlash = true;
else
bLegal = false;
}
else
{
if (bSlash)
strDenom += strRat[i];
else
strNum += strRat[i];
}
}
if (bLegal)
{
fiNum = int.Parse(strNum);
if (bNeg)
fiNum *= -1;
fiDenom = int.Parse(strDenom);
if (!bSlash)
fiDenom = 1;
if (fiDenom == 0)
fbValid = false;
else
fbValid = true;
}
else
fbValid = false;
}
else
fbValid = false;
}

public void Reduce()
{
if (fbValid)    //Reduce only if valid CRational
{
if (fiNum == 0) //To avoid things like 0/8
fiDenom = 1;
else
{
if (fiDenom < 0)    //Handles the possibility of negative
fiNum *= -1;
fiDenom *= -1;
}
int iGCD = 1;
for (int i = 1; i <= fiDenom; i++)
if (fiNum % i == 0 && fiDenom % i == 0)
iGCD = i;   //GCD when loop is exited
fiNum = fiNum / iGCD;
fiDenom = fiDenom / iGCD;   //Reduced!
}
}
}

#endregion

public static CRational operator +(CRational ratNum1, CRational ratNum2)
{
CRational ratSum = new CRational();

if (ratNum1.fbValid && ratNum2.fbValid)
{
ratSum.fiNum = ratNum1.fiNum * ratNum2.fiDenom + ratNum2.fiNum * ratNum1.fiDenom;
ratSum.fiDenom = ratNum1.fiDenom * ratNum2.fiDenom;
ratSum.fbValid = true;
}
else
ratSum.fbValid = false;
return ratSum;
}
public static CRational operator -(CRational ratNum1, CRational ratNum2)
{
CRational ratDiff = new CRational();

if (ratNum1.fbValid && ratNum2.fbValid)
{
ratDiff.fiNum = ratNum1.fiNum * ratNum2.fiDenom - ratNum2.fiNum * ratNum1.fiDenom;
ratDiff.fiDenom = ratNum1.fiDenom * ratNum2.fiDenom;
ratDiff.fbValid = true;
}
else
ratDiff.fbValid = false;
return ratDiff;
}
public static CRational operator *(CRational ratNum1, CRational ratNum2)
{
CRational ratProd = new CRational();

if (ratNum1.fbValid && ratNum2.fbValid)
{
ratProd.fiNum = ratNum1.fiNum * ratNum2.fiNum;
ratProd.fiDenom = ratNum1.fiDenom * ratNum2.fiDenom;
ratProd.fbValid = true;
}
else
ratProd.fbValid = false;
return ratProd;
}
public static CRational operator /(CRational ratNum1, CRational ratNum2)
{
CRational ratQuot = new CRational();
//Handles division by zero
if (ratNum1.fbValid && ratNum2.fbValid && ratNum2.fiNum != 0)
{
ratQuot.fiNum = ratNum1.fiNum * ratNum2.fiDenom;
ratQuot.fiDenom = ratNum1.fiDenom * ratNum2.fiNum;
ratQuot.fbValid = true;
}
else
ratQuot.fbValid = false;
return ratQuot;
}

#endregion
}
}```
This is my simple form that i have also:
Code:
```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rational
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{

}

private void btnSub_Click(object sender, EventArgs e)
{

}

private void btnTime_Click(object sender, EventArgs e)
{

}

private void btnDivd_Click(object sender, EventArgs e)
{

}
}
}```
So any helpful this all made sense message me if you have questions