コード例 #1
0
        static bool IsFirstIntegerSmaller(LargeInteger firstLongInteger, LargeInteger secondLongInteger)
        {
            bool smaller = false;

            if (Convert.ToInt32(firstLongInteger.ToString()[0]) <= Convert.ToInt32(secondLongInteger.ToString()[0]))
            {
                int i;
                if (Convert.ToInt32(firstLongInteger.ToString()[0]) < Convert.ToInt32(secondLongInteger.ToString()[0]))
                {
                    i = 0;
                }
                else
                {
                    i = 1;
                }

                for (; i < firstLongInteger.Length;)
                {
                    if (Convert.ToInt32(firstLongInteger.ToString()[i]) >= Convert.ToInt32(secondLongInteger.ToString()[i]))
                    {
                        smaller = false;
                        return(false);
                    }
                    else
                    {
                        smaller = true;
                        return(true);
                    }
                }
            }
            else
            {
                smaller = false;
                return(false);
            }

            return(smaller);
        }
コード例 #2
0
        public static LargeInteger operator /(LargeInteger firstLongInteger, LargeInteger secondLongInteger)
        {
            var diff = Math.Abs(firstLongInteger.Length - secondLongInteger.Length);

            LargeInteger difference        = new LargeInteger();
            LargeInteger multiplyer        = secondLongInteger;
            LargeInteger subtractionResult = firstLongInteger;
            LargeInteger sumFinalResult    = new LargeInteger();


            if (firstLongInteger.ToString().Equals("0") || secondLongInteger.ToString().Equals("0"))
            {
                throw new DivideByZeroException("You can't divide by 0!");
            }

            if (firstLongInteger.Length < secondLongInteger.Length)
            {
                throw new NotSupportedException("Second long integer is bigger than first long integer! Result is 0!");
            }

            if (secondLongInteger.Length == 1 && secondLongInteger.ToString().Equals("1"))
            {
                return(firstLongInteger);
            }

            if (firstLongInteger.Length == secondLongInteger.Length)
            {
                if (IsFirstIntegerSmaller(firstLongInteger, secondLongInteger) == true)
                {
                    throw new ArgumentException("First integer is smaller than second number!");
                }
            }


            while (subtractionResult.Length >= secondLongInteger.Length)
            {
                if (subtractionResult.Length == secondLongInteger.Length && subtractionResult.ToString()[0] < secondLongInteger.ToString()[0])
                {
                    break;
                }

                if (subtractionResult.Length > secondLongInteger.Length)
                {
                    if (subtractionResult.ToString().First() > secondLongInteger.ToString().First())
                    {
                        diff = Math.Abs(subtractionResult.Length - secondLongInteger.Length);
                    }
                    else
                    {
                        diff = Math.Abs(subtractionResult.Length - secondLongInteger.Length) - 1;
                    }

                    difference        = new LargeInteger("1" + GenerateZeroes(diff));
                    multiplyer        = secondLongInteger * difference;
                    subtractionResult = subtractionResult - multiplyer;
                    sumFinalResult   += difference;
                }
                else
                {
                    if (IsFirstIntegerSmaller(subtractionResult, secondLongInteger) == false)
                    {
                        difference = new LargeInteger("1");

                        subtractionResult = subtractionResult - secondLongInteger;
                        sumFinalResult   += difference;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(sumFinalResult);
        }