Skip to main content
2 of 5
deleted 28 characters in body; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Finding the lowest terms for fractions in C#

I have been working on this Rational Operations calculator where you can input two rational numbers and can add, subtract, multiply and divide and the final output is the reduced answer.

For example:

\$\dfrac{3}{4} * \dfrac{8}{15}\$ and the output would be \$\dfrac{2}{5}\$.

I have all but the reducing done i have some code in there that I think might work but I am brain dead from working on this and can't think were it would go or if it would even work. I am using classes so will post both sets of code.

Class part:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_5
{
    class Assignment5
    {
        #region Fields
        private int fiNum;
        private int fiDenom;
        private bool fbValid;
        #endregion

        #region Constructor
        public Assignment5(string strRat)//constructor, make it a public and name shoud have the same name of the class
        {
            Parse(strRat);
        }
        #endregion
        #region Methods
        public override string ToString()
        {
            string strRat;
            if (!fbValid)
                strRat = "NaN";     //if not a valid number
            else
            {
                strRat = fiNum.ToString();
                if (fiDenom > 1)            //if denom is 1 only output numbertor
                    strRat += "/" + fiDenom.ToString();
            }
            return strRat;
        }
        public void Parse(string strRat)
        {
            bool bNeg = false;
            bool bSlash = false;
            bool bLegal = true;
            string strDenom = "0";
            string strNum = "0";

            for (int i = 0; i < strRat.Length; i++)  // **** RETURN LENGTH OF THE STRING ****
            {
                if (!Char.IsDigit(strRat[i]))                   //boolean checks if charactor is a digit
                {
                    if (i == 0 && strRat[i] == '-')     //checks if it is negative
                        bNeg = true;
                    else if (i > 0 && strRat[i] == '/' && !bSlash)  //needing another boolean to check
                        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;
            }
        #endregion
        }

        //adding the two fractions together 
        public static Assignment5 operator +(Assignment5 rat1, Assignment5 rat2)
        {
            if (rat1.fbValid && rat2.fbValid)
            {
                rat1.fiNum = rat1.fiNum * rat2.fiDenom + rat1.fiDenom * rat2.fiNum;
                rat1.fiDenom = rat1.fiDenom * rat2.fiDenom;
            }
            else rat1.fbValid = false;
            return rat1;
        }

        //subtracting the two fractions 
        public static Assignment5 operator -(Assignment5 rat1, Assignment5 rat2)
        {
            if (rat1.fbValid && rat2.fbValid)
            {
                rat1.fiNum = rat1.fiNum * rat2.fiDenom - rat1.fiDenom * rat2.fiNum;
                rat1.fiDenom = rat1.fiDenom * rat2.fiDenom;
            }
            else rat1.fbValid = false;
            return rat1;
        }

        //multiplying the fractions 
        public static Assignment5 operator *(Assignment5 rat1, Assignment5 rat2)
        {
            if (rat1.fbValid && rat2.fbValid)
            {
                rat1.fiNum = rat1.fiNum * rat2.fiNum;
                rat1.fiDenom = rat1.fiDenom * rat2.fiDenom;
            }
            else rat1.fbValid = false;
            return rat1;
        }

        //dividing the fractions 
        public static Assignment5 operator /(Assignment5 rat1, Assignment5 rat2)
        {
            if (rat1.fbValid && rat2.fbValid)
            {
                rat1.fiNum = rat1.fiNum * rat2.fiDenom;
                rat1.fiDenom = rat1.fiDenom * rat2.fiNum;
            }
            else rat1.fbValid = false;
            return rat1;
        }
        //DONT KNOW IF THIS PART WORK AND THIS IS WERE I ENDED HELP!!!!
        public static int gcd(int rat1, int rat2)
        {
            if (rat2 == 0) return rat1;
            else
                return gcd(rat2, rat1 % rat2);
        }
       
    }
} 

Form part:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment_5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        #endregion  //blank form1 Load

        //Add button code
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
            ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
            ratNum2 = new Assignment5(txtRat2.Text);
            ratSum = new Assignment5("0/1");

            ratSum = ratNum1 + ratNum2;
            txtSum.Text = ratSum.ToString();
        }

        //Subtract button code
        private void btnSub_Click(object sender, EventArgs e)
        {
            Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
            ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
            ratNum2 = new Assignment5(txtRat2.Text);
            ratSum = new Assignment5("0/1");

            ratSum = ratNum1 - ratNum2;
            txtSum.Text = ratSum.ToString();
        }

        //Multiply button code
        private void btnMultiply_Click(object sender, EventArgs e)
        {
            Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
            ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
            ratNum2 = new Assignment5(txtRat2.Text);
            ratSum = new Assignment5("0/1");

            ratSum = ratNum1 * ratNum2;           
            txtSum.Text = ratSum.ToString();
        }

        //Divide button code
        private void btnDivd_Click(object sender, EventArgs e)
        {
            Assignment5 ratNum1, ratNum2, ratSum;      //declares new rational object
            ratNum1 = new Assignment5(txtRat1.Text);  //calls constructor           
            ratNum2 = new Assignment5(txtRat2.Text);
            ratSum = new Assignment5("0/1");

            ratSum = ratNum1 / ratNum2;
            txtSum.Text = ratSum.ToString();
        }
    }
}