Esempio n. 1
0
        private static BigInt Reduce(BigInt a, BigInt b)//вычитает из большего меньшее два бигинта
        {
            if (a == b)
            {
                return(Zero);
            }
            if (b.sign == '-')
            {
                return(Addition(a, b));
            }
            a.DeleteNuls(); b.DeleteNuls();
            b.EqualizeTheDigits(a.number.Count);
            var res = new BigInt();
            var num1 = new List <int>(a.number); var num2 = new List <int>(b.number);

            for (var i = a.number.Count - 1; i >= 0; i--)
            {
                var r = num1[i] - num2[i];
                if (r < 0)
                {
                    num1[i - 1]--;
                    r = 10 + r;
                }
                res.SetDigit(i, r);
            }
            res.DeleteNuls();
            return(res);
        }
Esempio n. 2
0
        public static BigInt operator *(BigInt V1, BigInt V2)  // v1 * v2
        {
            V1.DeleteNuls(); V2.DeleteNuls();
            var result = new BigInt();

            result.EqualizeTheDigits(V1.number.Count + V2.number.Count);
            V1.number.Reverse(); V2.number.Reverse();
            for (var i = 0; i < V1.number.Count; ++i)
            {
                for (int j = 0, carry = 0; j < V2.number.Count || carry > 0; ++j)
                {
                    var cur = result.number[i + j] + V1.number[i] * (j < V2.number.Count ? V2.number[j] : 0) + carry;
                    result.number[i + j] = cur % 10;
                    carry = cur / 10;
                }
            }
            while (result.number.Count > 1 && result.number.Last() == 0)
            {
                result.number.RemoveAt(result.number.Count - 1);
            }
            result.sign = V1.sign == V2.sign ? '+' : '-';
            V1.number.Reverse(); V2.number.Reverse();
            result.number.Reverse();
            result.DeleteNuls();
            return(result);
        }
Esempio n. 3
0
        private static BigInt Addition(BigInt a, BigInt b)//суммирует два бигинта
        {
            var res = new BigInt();
            var maxLen = Math.Max(a.number.Count, b.number.Count) - 1;
            var enlarge = 0;
            var num1 = a.number; var num2 = b.number; var resNum = res.number;

            if (num1.Count < num2.Count)
            {
                a.EqualizeTheDigits(b.number.Count);
                res.EqualizeTheDigits(b.number.Count);
            }
            else
            {
                b.EqualizeTheDigits(a.number.Count);
                res.EqualizeTheDigits(a.number.Count);
            }
            for (var i = maxLen; i >= 0; i--)
            {
                var s = num1[i] + num2[i] + enlarge;
                if (s >= 10)
                {
                    enlarge   = 1;
                    resNum[i] = s - 10;
                }
                else
                {
                    enlarge   = 0;
                    resNum[i] = s;
                }
            }
            if (enlarge == 1)
            {
                resNum.Insert(0, 1);
            }
            res.DeleteNuls();
            return(res);
        }