Example #1
0
        public void Multiply(LargeNumber secondNum)
        {
            List <int> helpList = new List <int>();

            if ((Negative && secondNum.Negative) || (!Negative && !secondNum.Negative))
            {
                Negative = false;
            }
            else if ((!Negative && secondNum.Negative) || (Negative && !secondNum.Negative))
            {
                Negative = true;
            }
            Array.Reverse(Number);
            for (int i = 0; i < secondNum.Number.Length; i++)
            {
                for (int j = 0; j < Number.Length; j++)
                {
                    if (i == 0)
                    {
                        helpList.Add(secondNum.Number[i] * Number[j]);
                    }
                    else
                    {
                        if (i + j >= helpList.Count)
                        {
                            helpList.Add(secondNum.Number[i] * Number[j]);
                        }
                        else
                        {
                            helpList[i + j] += secondNum.Number[i] * Number[j];
                        }
                    }
                }
            }
            Number = CutLargeValue(helpList).ToArray();
            Array.Reverse(Number);
        }
Example #2
0
        public void Add(LargeNumber secondNum)
        {
            int        lengthOfLargest, smallestLength;
            bool       ourNumSmaller;
            List <int> helpList = new List <int>();

            Array.Reverse(Number);
            if (Number.Length >= secondNum.Number.Length)
            {
                lengthOfLargest = Number.Length;
                smallestLength  = secondNum.Number.Length;
                ourNumSmaller   = false;
            }
            else
            {
                lengthOfLargest = secondNum.Number.Length;
                smallestLength  = Number.Length;
                ourNumSmaller   = true;
            }
            for (int i = 0; i < lengthOfLargest; i++)
            {
                if (i < smallestLength)
                {
                    helpList.Add(Number[i] + secondNum.Number[i]);
                }
                else if (ourNumSmaller)
                {
                    helpList.Add(secondNum.Number[i]);
                }
                else if (!ourNumSmaller)
                {
                    helpList.Add(Number[i]);
                }
            }
            Number = CutLargeValue(helpList).ToArray();
            Array.Reverse(Number);
        }
Example #3
0
        public void Substract(LargeNumber secondNum)
        {
            int        lengthOfLargest = 0, smallestLength = 0;
            int        ourNumSmaller = 0;
            List <int> helpList      = new List <int>();

            Array.Reverse(Number);
            if (Number.Length == secondNum.Number.Length)
            {
                for (int i = Number.Length - 1; i >= 0; i--)
                {
                    if (CheckSmaller(secondNum.Number[i], Number[i]))
                    {
                        lengthOfLargest = Number.Length;
                        smallestLength  = Number.Length;
                        ourNumSmaller   = -1;
                        break;
                    }
                    else if (CheckSmaller(Number[i], secondNum.Number[i]))
                    {
                        lengthOfLargest = Number.Length;
                        smallestLength  = Number.Length;
                        ourNumSmaller   = 1;
                        break;
                    }
                    if (i == 0)
                    {
                        Number = new int[] { 0 };
                        return;
                    }
                }
                if (ourNumSmaller == 1)
                {
                    for (int j = 0; j < lengthOfLargest; j++)
                    {
                        if (CheckSmaller(Number[j], secondNum.Number[j]))
                        {
                            Number[j]    += 10;
                            Number[j + 1] = Number[j + 1] - 1;
                            helpList.Add(Number[j] - secondNum.Number[j]);
                        }
                        else
                        {
                            helpList.Add(Number[j] - secondNum.Number[j]);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < lengthOfLargest; j++)
                    {
                        if (Number[j] <= secondNum.Number[j])
                        {
                            helpList.Add(secondNum.Number[j] - Number[j]);
                        }
                        else
                        {
                            secondNum.Number[j]     += 10;
                            secondNum.Number[j + 1] -= 1;
                            helpList.Add(secondNum.Number[j] - Number[j]);
                        }
                    }
                }
            }
            else if (Number.Length > secondNum.Number.Length)
            {
                lengthOfLargest = Number.Length;
                smallestLength  = secondNum.Number.Length;
                ourNumSmaller   = -1;
                for (int j = 0; j < smallestLength; j++)
                {
                    if (Number[j] >= secondNum.Number[j])
                    {
                        helpList.Add(Number[j] - secondNum.Number[j]);
                    }
                    else
                    {
                        Number[j]     += 10;
                        Number[j + 1] -= 1;
                        helpList.Add(Number[j] - secondNum.Number[j]);
                    }
                }
                for (int j = smallestLength; j < lengthOfLargest; j++)
                {
                    helpList.Add(Number[j]);
                }
            }
            else
            {
                lengthOfLargest = secondNum.Number.Length;
                smallestLength  = Number.Length;
                ourNumSmaller   = 1;
                for (int j = 0; j < smallestLength; j++)
                {
                    if (Number[j] <= secondNum.Number[j])
                    {
                        helpList.Add(secondNum.Number[j] - Number[j]);
                    }
                    else
                    {
                        secondNum.Number[j]     += 10;
                        secondNum.Number[j + 1] -= 1;
                        helpList.Add(secondNum.Number[j] - Number[j]);
                    }
                }
                for (int j = smallestLength; j < lengthOfLargest; j++)
                {
                    helpList.Add(secondNum.Number[j]);
                }
            }

            Number = CutLargeValue(helpList).ToArray();
            Array.Reverse(Number);
        }
Example #4
0
 public LargeNumber(LargeNumber inputNumber)
 {
     Number   = inputNumber.Number;
     Negative = inputNumber.Negative;
 }