Exemple #1
0
        public static void Main(string[] args)
        {
            Rational[] num_array = new Rational[3];
            try {
                Rational left = new Rational ();
                Rational right = new Rational ();
                Calculate (left, right, "+");
                Calculate (left, right, "-");
                Calculate (left, right, "*");
                Calculate (left, right, "/");
                Calculate (left, right, "==");
                Calculate (left, right, "!=");
                Calculate (left, right, ">");
                Calculate (left, right, "<");
            }
            catch (RationalException e) {
                Console.WriteLine ("Rational exception handled: {0}, {1}", e.GetType (), e.Message);
            }

            try {
                Console.WriteLine ("Please enter some nums to sort: ");
                for (int i = 0; i < num_array.Length; i++)
                    num_array [i] = new Rational ();

                Array.Sort (num_array, new Comparison<Rational> ((r1, r2) => r2.CompareTo (r1)));
                Console.WriteLine ("The entered rationals in ascending order: ");
                foreach (Rational r in num_array)
                    Console.WriteLine ("{0}", r.ToString());
            } catch (RationalException e) {
                Console.WriteLine ("Rational exception handled: {0}, {1}", e.GetType (), e.Message);
            }
        }
Exemple #2
0
 public static void Calculate(Rational r1, Rational r2, string op)
 {
     try {
     if (op.Contains ("+")) {
         Console.WriteLine("{0} + {1} = {2}",r1, r2, (r1 + r2));
         }
     else if (op.Contains ("-")) {
         Console.WriteLine("{0} - {1} = {2}",r1, r2, (r1 - r2));
         }
     else if (op.Contains ("*")) {
         Console.WriteLine("{0} * {1} = {2}",r1, r2, (r1 * r2));
         }
     else if (op.Contains ("/")) {
         Console.WriteLine("{0} / {1} = {2}",r1, r2, (r1 / r2));
         }
     else if (op.Contains ("==")) {
             if (r1.Equals(r2)) {
                 Console.WriteLine("{0} equals {1}", r1, r2);
             }
             else
             Console.WriteLine("{0} not equals {1}", r1, r2);
         }
     else if (op.Contains ("!=")) {
             if (!r1.Equals(r2))
                 Console.WriteLine("{0} not equals {1}", r1, r2);
             else
                 Console.WriteLine("{0} equals {1}", r1, r2);
         }
     else if (op.Contains ("<")) {
             if (r1.CompareTo(r2) == -1)
                 Console.WriteLine("True");
             else
                 Console.WriteLine("False");
         }
     else if (op.Contains (">")) {
             if (r1.CompareTo(r2) == 1)
                 Console.WriteLine("True");
             else
                 Console.WriteLine("False");
         }
     else
         throw new RationalException("Invalid Operation");
     }
     catch (RationalException ex)
     {
         throw ex;
     }
 }