Esempio n. 1
0
        public static BigInt Div(BigInt a1, BigInt b) // деление нацело
        {
            var a = new BigInt('+', new List <int>(a1.number));

            a.DeleteNuls(); b.DeleteNuls();
            if (a < b)
            {
                return(Zero);
            }
            else if (a == b)
            {
                return(One);
            }
            else
            {
                var res = new List <BigInt>();
                while (a > b)
                {
                    var r           = new BigInt(b.sign, new List <int>(b.number));
                    var bSDopisNuls = new BigInt(b.sign, new List <int>(b.number));
                    var newrazRes   = Zero;
                    var newraz      = 0;
                    var razn        = a.number.Count - b.number.Count;//1
                    bSDopisNuls = bSDopisNuls.MultOn10(razn);
                    if (bSDopisNuls > a)
                    {
                        newraz = razn - 1;//2
                    }
                    else
                    {
                        newraz = razn;
                    }
                    var rabRazr = new BigInt('+' + (Math.Pow(10, newraz)).ToString());//3

                    for (var i = 1; b *rabRazr.MultOnDigit(i) < a; i++)
                    {
                        r         = rabRazr.MultOnDigit(i);
                        newrazRes = r;
                    }//4
                    if (a > newrazRes * b)
                    {
                        if (a - newrazRes * b < b)
                        {
                            res.Add(r);
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                    a -= newrazRes * b;
                    res.Add(r);
                }
                var result = Zero;
                foreach (var e in res)
                {
                    result += e;
                }
                result.sign = a.sign == b.sign ? '+' : '-';
                return(result);
            }
        }