Advertisement
rivalcoba

U04E01 Rational

Jun 29th, 2021
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. using System;
  2. namespace Itgam.Math
  3. {
  4.   class Rational
  5.   {
  6.     // Fields
  7.     private int _numerator = 0;
  8.     private int _denominator = 1;
  9.  
  10.     // Properties
  11.     public int Numerator
  12.     {
  13.       get
  14.       {
  15.         // Forzoso retornar un valor
  16.         return this._numerator;
  17.       }
  18.       set
  19.       {
  20.         this._numerator = value;
  21.       }
  22.     }
  23.  
  24.     public int Denominator
  25.     {
  26.       get
  27.       {
  28.         return this._denominator;
  29.       }
  30.       set
  31.       {
  32.         if(value == 0){
  33.           throw new System.Exception("Un racional no debe tener un denominador cero");
  34.         }
  35.         this._denominator = value;
  36.       }
  37.     }
  38.     // Constructores
  39.     // Constructor sin parámetros
  40.     public Rational()
  41.     {
  42.       this.Numerator = 0;
  43.       this.Denominator = 1;
  44.     }
  45.     // Constructor Con parámetros
  46.     public Rational(int num, int den)
  47.     {
  48.       this.Numerator = num;
  49.       this.Denominator = den;
  50.     }
  51.  
  52.     public Rational(string num, string den)
  53.     {
  54.       int iNum = int.Parse(num);
  55.       int iDen = int.Parse(den);
  56.       this.Numerator = iNum;
  57.       this.Denominator = iDen;
  58.     }
  59.     // Functional Members
  60.     public string Print()
  61.     {
  62.       // Antes de imprimir aplico la reducción
  63.       this.Reduction();
  64.       // Se retorna en string la representacion
  65.       // del racional
  66.       if(this._denominator == 1)
  67.       {
  68.         return $"{this._numerator}";
  69.       }
  70.       return $"{this._numerator}/{this._denominator}";
  71.     }
  72.     // Perfor GCD
  73.     private int GCD(int a, int b)
  74.     {
  75.       // Console.WriteLine($"Par de entrada: ({a},{b})");
  76.       int remainder = 0;
  77.       while(b != 0)
  78.       {
  79.         remainder = a % b;
  80.         a = b;
  81.         b = remainder;
  82.         // Console.WriteLine($"Respuesta: ({a},{b})");
  83.       }
  84.       return a;
  85.     }
  86.     // Reduction Algorithm
  87.     private void Reduction()
  88.     {
  89.       int gcd = this.GCD(this._numerator,this._denominator);
  90.       this._numerator = this._numerator / gcd;
  91.       this._denominator = this._denominator / gcd;
  92.     }
  93.     // ------------------------------------------
  94.     // Crear metodos de clase / Estaticos
  95.     // ------------------------------------------
  96.     public static void AddOne(Rational input)
  97.     {
  98.       input._numerator += input._denominator;
  99.       input._denominator += input._denominator;
  100.     }
  101.  
  102.     // Metodo que suma racionales
  103.     public static Rational Add(Rational r1, Rational r2)
  104.     {
  105.       // Creando un nuevo racional para guardar
  106.       // el resultado de la suma
  107.       Rational result = new Rational();
  108.       // Sumando los racionales
  109.       result.Numerator = r1.Numerator * r2.Denominator  + r2.Numerator * r1.Denominator;
  110.       result.Denominator = r1.Denominator * r2.Denominator;
  111.       //
  112.       return result;
  113.     }
  114.  
  115.     public static Rational Sub(Rational r1, Rational r2)
  116.     {
  117.       // Creando un nuevo racional para guardar
  118.       // el resultado de la suma
  119.       Rational result = new Rational();
  120.       // Sumando los racionales
  121.       result.Numerator = r1.Numerator * r2.Denominator  - r2.Numerator * r1.Denominator;
  122.       result.Denominator = r1.Denominator * r2.Denominator;
  123.       //
  124.       return result;
  125.     }
  126.  
  127.     public static Rational Prod(Rational r1, Rational r2)
  128.     {
  129.       // Creando un nuevo racional para guardar
  130.       // el resultado de la suma
  131.       Rational result = new Rational();
  132.       // Sumando los racionales
  133.       result.Numerator = r1.Numerator * r2.Numerator;
  134.       result.Denominator = r1.Denominator * r2.Denominator;
  135.       //
  136.       return result;
  137.     }
  138.  
  139.     public static Rational Quot(Rational r1, Rational r2)
  140.     {
  141.       // Creando un nuevo racional para guardar
  142.       // el resultado de la suma
  143.       Rational result = new Rational();
  144.       // Sumando los racionales
  145.       result.Numerator = r1.Numerator * r2.Denominator;
  146.       result.Denominator = r1.Denominator * r2.Numerator;
  147.       //
  148.       return result;
  149.     }
  150.     // Parseo de string a Rational
  151.     public static Rational Parse(string str, char separator = '/')
  152.     {
  153.       // Crear el Racional de salida
  154.       var rational = new Rational();
  155.       // Buscamos el separador
  156.       if(str.IndexOf(separator) < 0)
  157.       {
  158.         throw new FormatException("No se pudo parsear racional");
  159.       }
  160.       // Partir la cadena con el separador
  161.       // "1/2" ==> Split(/) ==> {[1][2]}
  162.       var rationalArray = str.Split(separator);
  163.       // Se realiza Parseo
  164.       rational.Numerator = int.Parse(rationalArray[0]);
  165.       rational.Denominator = int.Parse(rationalArray[1]);
  166.       // Regreso el racional
  167.       return rational;
  168.     }
  169.    
  170.     // SOBRE CARGA DE OPERADOR +
  171.     public static Rational operator+(Rational input1, Rational input2)
  172.     {
  173.       // Realizo la suma y retorno el resultado que es un racional
  174.       return Rational.Add(input1, input2);
  175.     }
  176.  
  177.     public static Rational operator-(Rational input1, Rational input2)
  178.     {
  179.       // Realizo la suma y retorno el resultado que es un racional
  180.       return Rational.Sub(input1, input2);
  181.     }
  182.  
  183.     public static Rational operator*(Rational input1, Rational input2)
  184.     {
  185.       // Realizo la suma y retorno el resultado que es un racional
  186.       return Rational.Prod(input1, input2);
  187.     }
  188.  
  189.     public static Rational operator/(Rational input1, Rational input2)
  190.     {
  191.       // Realizo la suma y retorno el resultado que es un racional
  192.       return Rational.Quot(input1, input2);
  193.     }
  194.  
  195.     // Override del metodo ToString
  196.     public override string ToString()
  197.     {
  198.       return this.Print();
  199.     }
  200.   }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement