Esempio n. 1
0
        // so sanh
        public int Compareto(SoLon b)
        {
            var ar = this.arr;
            var br = b.arr;

            if (ar.Length > br.Length)
            {
                return(1);
            }
            else if (ar.Length < br.Length)
            {
                return(-1);
            }
            else if (ar.Length == br.Length)
            {
                for (int i = 0; i < ar.Length; i++)
                {
                    if (ar[i] > br[i])
                    {
                        return(1);
                    }
                    if (ar[i] < br[i])
                    {
                        return(-1);
                    }
                }
            }
            return(0);
        }
Esempio n. 2
0
        //trừ
        SoLon Tru(SoLon b)
        {
            if (b.ToString() == "0")
            {
                return(this);
            }
            else if (ToString() == "0")
            {
                return(b);
            }
            var max = Math.Max(arr.Length, b.arr.Length);

            if (arr.Length < max)
            {
                arr = MakeSameLength(arr, max);
            }
            if (b.arr.Length < max)
            {
                b.arr = MakeSameLength(b.arr, max);
            }
            var nho = 0;
            var ret = new int[max];

            for (int i = max - 1; i >= 0; i--)
            {
                var hieu = arr[i] - b.arr[i];
                if (nho == 1)
                {
                    hieu -= 1;
                    nho   = 0;
                }
                if (hieu < 0)
                {
                    hieu += 10;
                    nho   = 1;
                }
                ret[i] = hieu;
            }
            if (nho != 0)
            {
                ret[0] = ret[0] - nho;
            }
            return(new SoLon(ret));
        }
Esempio n. 3
0
        //cộng
        SoLon Cong(SoLon b)
        {
            if (ToString() == "0")
            {
                return(b);
            }
            if (b.ToString() == "0")
            {
                return(this);
            }
            var max = Math.Max(this.arr.Length, b.arr.Length);

            if (this.arr.Length < max)
            {
                this.arr = MakeSameLength(arr, max);
            }
            if (b.arr.Length < max)
            {
                b.arr = MakeSameLength(b.arr, max);
            }
            var ret = new int[max + 1];
            int nho = 0;

            for (int i = max - 1; i >= 0; i--)
            {
                var tong = arr[i] + b.arr[i];
                if (nho != 0)
                {
                    tong++;
                    nho = 0;
                }
                if (tong > 9)
                {
                    nho   = 1;
                    tong -= 10;
                }
                ret[i + 1] = tong;
            }
            if (nho != 0)
            {
                ret[0] = nho;
            }
            return(new SoLon(ret));
        }