CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Gaussian Rationals??

Hybrid View

  1. #1
    Join Date
    Mar 2015
    Posts
    7

    Post Gaussian Rationals??

    Hello everyone, I'm new to these boards but starting to get into C# programming and just wondering if anyone could help me out on this project i have going on. It is called a Gaussian Rational is where a number of the form a+bi where a and b are rationals and that a is called the real part of the number and b the imaginary part. Not i have made already the form and my CRational class. I'm just stuck on where to even begin on this one. Any help will do to get this going, here is the code that i have already made to get it started:

    I have a zip file i have attached too if this don't work for anyone.
    Any help would be so grateful

    Rational Class.zip

    namespace Rational_Class
    {
    public partial class Form1 : Form
    {

    public Form1()
    {
    InitializeComponent();
    }

    private void btnGo_Click(object sender, EventArgs e)
    {
    CRational ratNum1, ratNum2, ratSum,ratDiff,ratProd,ratQuot;

    ratNum1 = new CRational(txtRat1.Text);
    ratNum2 = new CRational(txtRat2.Text);
    ratSum = new CRational();
    ratDiff = new CRational();
    ratProd = new CRational();
    ratQuot = new CRational();

    ratSum = ratNum1 + ratNum2; //Calculate sum
    ratSum.Reduce();
    txtSum.Text = ratSum.ToString();

    ratDiff = ratNum1 - ratNum2; //Calculate difference
    ratDiff.Reduce();
    txtDiff.Text = ratDiff.ToString();

    ratProd = ratNum1 *ratNum2; //Calculate product
    ratProd.Reduce();
    txtProd.Text = ratProd.ToString();

    ratQuot = ratNum1 / ratNum2; //Calculate quotient
    ratQuot.Reduce();
    txtQuot.Text = ratQuot.ToString();

    }
    }
    }

    Here is the other half

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

    namespace Rational_Class
    {
    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
    { //fiDenom from / overload
    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
    #region Overloaded Operators

    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
    }
    }

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Gaussian Rationals??

    Hi and welcome to the forums.
    When posting code, please use the [code][/code] tags - they will keep existing code formatting and help others read your code.

    Now, about your question - um... what exactly is it? You only mentioned Gaussian rationals, but you didn't really say what you are trying to do? Print out some of them within same range (region)? Or use them for some purpose? What?

    P.S.
    I'm just stuck on where to even begin on this one.
    Just that, huh?

  3. #3
    Join Date
    Mar 2015
    Posts
    7

    Re: Gaussian Rationals??

    I have adjusted and used your advise but what I'm looking at is a Galois extension of the field Q of rational numbers arrived at by adjoining i to Q. A CGauss object will consist of a real part an imaginary part and a validity boolen.


    looking for is for two Gaussian Rationals a+bi and c+di
    (a+bi) + (c+di) = (a+c) +(b+d)i (a+bi) + (c+di) = (a-c) +(b-d)i
    (a+bi) * (c+di) = (ac-bd) +(ad-bc)i (a+bi) /(c+di) = (ac+bd\c2 +d2)+(bc-ad)/c2+d2)i

    so when you run the problem:
    3/4+1/2i * 1/4-2i = 19/16 - 11/8i

  4. #4
    Join Date
    Mar 2015
    Posts
    7

    Re: Gaussian Rationals??

    Code:
    namespace Rational_Class
    {
    public partial class Form1 : Form
    {
    
    public Form1()
    {
    InitializeComponent();
    }
    
    private void btnGo_Click(object sender, EventArgs e)
    {
    CRational ratNum1, ratNum2, ratSum,ratDiff,ratProd,ratQuot;
    
    ratNum1 = new CRational(txtRat1.Text);
    ratNum2 = new CRational(txtRat2.Text);
    ratSum = new CRational();
    ratDiff = new CRational();
    ratProd = new CRational();
    ratQuot = new CRational();
    
    ratSum = ratNum1 + ratNum2; //Calculate sum
    ratSum.Reduce();
    txtSum.Text = ratSum.ToString();
    
    ratDiff = ratNum1 - ratNum2; //Calculate difference
    ratDiff.Reduce();
    txtDiff.Text = ratDiff.ToString();
    
    ratProd = ratNum1 *ratNum2; //Calculate product
    ratProd.Reduce();
    txtProd.Text = ratProd.ToString();
    
    ratQuot = ratNum1 / ratNum2; //Calculate quotient
    ratQuot.Reduce();
    txtQuot.Text = ratQuot.ToString();
    
    }
    }
    }

  5. #5
    Join Date
    Mar 2015
    Posts
    7

    Re: Gaussian Rationals??

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Rational_Class
    {
    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
    { //fiDenom from / overload
    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
    #region Overloaded Operators
    
    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
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)