public int CompareTo(INumber other)
        {
            RationalNumber comparable = other as RationalNumber;

            if (comparable == null)
            {
                throw new InvalidCastException();
            }
            int left  = this.Numerator * comparable.Denominator;
            int right = comparable.Numerator * this.Denominator;

            if (left > right)
            {
                return(1);
            }
            else if (left < right)
            {
                return(-1);
            }

            return(0);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            try
            {
                INumber[] integerArray     = Utils.CreateArrayOfType <IntegerNumber>(Utils.GetArrayLength());
                var       integerArrayCopy = Task.CloneArray(integerArray);
                Task.SortAscending(integerArrayCopy);
                IntegerNumber integerSum         = Task.Sum(integerArrayCopy) as IntegerNumber;
                IntegerNumber integerMultiplying = Task.Multiplying(integerArrayCopy) as IntegerNumber;
                IntegerNumber integerAvgNumber   = Task.AvgNumber(integerArrayCopy) as IntegerNumber;
                Utils.MultiParamsFormatOutput(integerSum, integerMultiplying, integerAvgNumber);

                INumber[] rationalArray     = Utils.CreateArrayOfType <RationalNumber>(Utils.GetArrayLength());
                var       rationalArrayCopy = Task.CloneArray(rationalArray);
                Task.SortDescending(rationalArrayCopy);
                RationalNumber rationalSum         = Task.Sum(rationalArrayCopy) as RationalNumber;
                RationalNumber rationalMultiplying = Task.Multiplying(rationalArrayCopy) as RationalNumber;
                RationalNumber rationalAvgNumber   = Task.AvgNumber(rationalArrayCopy) as RationalNumber;
                Utils.MultiParamsFormatOutput(rationalSum, rationalMultiplying, rationalAvgNumber);
            }
            catch (ArithmeticException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (InvalidCastException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Something bad happend : " + e.Message);
            }

            Console.ReadKey();
        }
コード例 #3
0
        public static INumber Multiplying(INumber[] arr)
        {
            INumber mul = null;

            if (Array.TrueForAll(arr, item => item is RationalNumber))
            {
                mul = new RationalNumber(1, 1);
            }
            else if (Array.TrueForAll(arr, item => item is IntegerNumber))
            {
                mul = new IntegerNumber(1);
            }
            else
            {
                throw new ArgumentException("cannot calculate sum of array which elements are of different types");
            }

            foreach (var item in arr)
            {
                mul = mul.Multiply(item);
            }
            return(mul);
        }
コード例 #4
0
        public static INumber Sum(INumber[] arr)
        {
            INumber sum = null;

            if (Array.TrueForAll(arr, item => item is RationalNumber))
            {
                sum = new RationalNumber();
            }
            else if (Array.TrueForAll(arr, item => item is IntegerNumber))
            {
                sum = new IntegerNumber();
            }
            else
            {
                throw new ArgumentException("cannot calculate sum of array which elements are of different types");
            }

            foreach (var item in arr)
            {
                sum = sum.Add(item);
            }

            return(sum);
        }