Ejemplo n.º 1
0
 public static bool TryParse(string text, out Rational result)
 {
     // depends on whether or not there is a '/' character.
     string[] fields = text.Split('/');
     if (fields.Length == 2)
     {
         if (Rational.TryParse(fields[0], out Rational num) && Rational.TryParse(fields[1], out Rational den))
         {
             result = num / den;
             return(true);
         }
         else
         {
             result = Rational.Zero;
             return(false);
         }
     }
     else if (fields.Length == 1)
     {
         // No. no slash.
         string[] decfields = text.Split('.');
         if (decfields.Length == 2)
         {
             if (long.TryParse(decfields[0], out long n) && long.TryParse(decfields[1], out long d))
             {
                 int  pow = decfields[1].Length;
                 long f   = 10;
                 for (int i = 1; i < pow; ++i)
                 {
                     f *= 10;
                 }
                 result = new Rational(n * f + d, f);
                 return(true);
             }
             else
             {
                 result = Rational.Zero;
                 return(false);
             }
         }
         else if (decfields.Length == 1)
         {
             if (long.TryParse(decfields[0], out long num))
             {
                 result = new Rational(num);
                 return(true);
             }
             else
             {
                 result = Rational.Zero;
                 return(false);
             }
         }
         else
         {
             result = Rational.Zero;
             return(false);
         }
     }
     else
     {
         result = Rational.Zero;
         return(false);
     }
 }
Ejemplo n.º 2
0
 public Rational2(Rational x, Rational y)
 {
     this.x = x;
     this.y = y;
 }