Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Itgam.Math
- {
- class Rational
- {
- // Fields
- private int _numerator = 0;
- private int _denominator = 1;
- // Properties
- public int Numerator
- {
- get
- {
- // Forzoso retornar un valor
- return this._numerator;
- }
- set
- {
- this._numerator = value;
- }
- }
- public int Denominator
- {
- get
- {
- return this._denominator;
- }
- set
- {
- if(value == 0){
- throw new System.Exception("Un racional no debe tener un denominador cero");
- }
- this._denominator = value;
- }
- }
- // Constructores
- // Constructor sin parámetros
- public Rational()
- {
- this.Numerator = 0;
- this.Denominator = 1;
- }
- // Constructor Con parámetros
- public Rational(int num, int den)
- {
- this.Numerator = num;
- this.Denominator = den;
- }
- public Rational(string num, string den)
- {
- int iNum = int.Parse(num);
- int iDen = int.Parse(den);
- this.Numerator = iNum;
- this.Denominator = iDen;
- }
- // Functional Members
- public string Print()
- {
- // Antes de imprimir aplico la reducción
- this.Reduction();
- // Se retorna en string la representacion
- // del racional
- if(this._denominator == 1)
- {
- return $"{this._numerator}";
- }
- return $"{this._numerator}/{this._denominator}";
- }
- // Perfor GCD
- private int GCD(int a, int b)
- {
- // Console.WriteLine($"Par de entrada: ({a},{b})");
- int remainder = 0;
- while(b != 0)
- {
- remainder = a % b;
- a = b;
- b = remainder;
- // Console.WriteLine($"Respuesta: ({a},{b})");
- }
- return a;
- }
- // Reduction Algorithm
- private void Reduction()
- {
- int gcd = this.GCD(this._numerator,this._denominator);
- this._numerator = this._numerator / gcd;
- this._denominator = this._denominator / gcd;
- }
- // ------------------------------------------
- // Crear metodos de clase / Estaticos
- // ------------------------------------------
- public static void AddOne(Rational input)
- {
- input._numerator += input._denominator;
- input._denominator += input._denominator;
- }
- // Metodo que suma racionales
- public static Rational Add(Rational r1, Rational r2)
- {
- // Creando un nuevo racional para guardar
- // el resultado de la suma
- Rational result = new Rational();
- // Sumando los racionales
- result.Numerator = r1.Numerator * r2.Denominator + r2.Numerator * r1.Denominator;
- result.Denominator = r1.Denominator * r2.Denominator;
- //
- return result;
- }
- public static Rational Sub(Rational r1, Rational r2)
- {
- // Creando un nuevo racional para guardar
- // el resultado de la suma
- Rational result = new Rational();
- // Sumando los racionales
- result.Numerator = r1.Numerator * r2.Denominator - r2.Numerator * r1.Denominator;
- result.Denominator = r1.Denominator * r2.Denominator;
- //
- return result;
- }
- public static Rational Prod(Rational r1, Rational r2)
- {
- // Creando un nuevo racional para guardar
- // el resultado de la suma
- Rational result = new Rational();
- // Sumando los racionales
- result.Numerator = r1.Numerator * r2.Numerator;
- result.Denominator = r1.Denominator * r2.Denominator;
- //
- return result;
- }
- public static Rational Quot(Rational r1, Rational r2)
- {
- // Creando un nuevo racional para guardar
- // el resultado de la suma
- Rational result = new Rational();
- // Sumando los racionales
- result.Numerator = r1.Numerator * r2.Denominator;
- result.Denominator = r1.Denominator * r2.Numerator;
- //
- return result;
- }
- // Parseo de string a Rational
- public static Rational Parse(string str, char separator = '/')
- {
- // Crear el Racional de salida
- var rational = new Rational();
- // Buscamos el separador
- if(str.IndexOf(separator) < 0)
- {
- throw new FormatException("No se pudo parsear racional");
- }
- // Partir la cadena con el separador
- // "1/2" ==> Split(/) ==> {[1][2]}
- var rationalArray = str.Split(separator);
- // Se realiza Parseo
- rational.Numerator = int.Parse(rationalArray[0]);
- rational.Denominator = int.Parse(rationalArray[1]);
- // Regreso el racional
- return rational;
- }
- // SOBRE CARGA DE OPERADOR +
- public static Rational operator+(Rational input1, Rational input2)
- {
- // Realizo la suma y retorno el resultado que es un racional
- return Rational.Add(input1, input2);
- }
- public static Rational operator-(Rational input1, Rational input2)
- {
- // Realizo la suma y retorno el resultado que es un racional
- return Rational.Sub(input1, input2);
- }
- public static Rational operator*(Rational input1, Rational input2)
- {
- // Realizo la suma y retorno el resultado que es un racional
- return Rational.Prod(input1, input2);
- }
- public static Rational operator/(Rational input1, Rational input2)
- {
- // Realizo la suma y retorno el resultado que es un racional
- return Rational.Quot(input1, input2);
- }
- // Override del metodo ToString
- public override string ToString()
- {
- return this.Print();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement