示例#1
0
        //chia chua sua
        public static bignum operator /(bignum a, bignum b)
        {
            int    i;
            bignum kq = null;

            kq.dau    = 1;
            kq.nguyen = "0";
            kq.tphan  = "0";
            kq.cham   = 1;
            //----------------noi phan nguyen va tphan lai----------------
            if (a.tphan.Length >= b.tphan.Length) //them so 0 vao phan thap phan ngan hon
            {
                for (i = b.tphan.Length; i < a.tphan.Length; i++)
                {
                    b.tphan += '0';
                }
            }
            else
            {
                for (i = a.tphan.Length; i < b.tphan.Length; i++)
                {
                    a.tphan += '0';
                }
            }
            char[] v1, v2;        //vector
            string t1, t2;

            t1 = a.nguyen.ToString() + a.tphan.ToString();
            t2 = b.nguyen.ToString() + b.tphan.ToString();
            v1 = t1.ToCharArray();
            v2 = t1.ToCharArray();
            v1.Reverse();
            v2.Reverse();
            //----------------chia 2 vector v1 & v2----------------
            char[] carry = null;
            carry[0] = '\0';
            int nho = 0;

            while (v1.Length > 0)                                    //duyet het v1
            {
                if (carry.Length == 0)                               //neu carry chua co' gi`
                {
                    while (compare(carry, v2) == 1 && v1.Length > 0) //tach carry tu` v1 cho den khi carry>v2
                    {
                        i = v1.Length - 1;
                        insert_begin(carry, v1[i]);
                        v1[i] = '\0';
                    }
                }
                else
                {
                    i = v1.Length - 1;
                    insert_begin(carry, v1[i]);
                    v1[i] = '\0';
                }
                xoaSo0(carry);
                if (carry.Length > v2.Length)     //them 1 so 0 vao duoi v2 neu carry > v2
                {
                    i         = v2.Length;
                    v2[i]     = '0';
                    v2[i + 1] = '\0';
                }
                int    dem = 0;
                char[] tam2 = null;
                int    a1, b1;
                while (compare(carry, v2) != 1)     //chia carry & v2, dem la ket qua
                {
                    dem++;
                    //su dung phep tru
                    for (i = 0; i < carry.Length; i++)
                    {
                        a1 = Convert.ToInt32(carry[i]);
                        b1 = Convert.ToInt32(v2[i]);
                        if (a1 < b1 + nho)        //so bi tru < so tru
                        {
                            tam2[i] = Convert.ToChar(a1 - b1 - nho + 10);
                            nho     = 1;
                        }
                        else            //so bi tru > so tru
                        {
                            tam2[i] = Convert.ToChar(a1 - b1 - nho);
                            nho     = 0;
                        }
                    }
                    tam2[i] = '\0';
                    //strcpy(carry, tam2);
                    for (i = 0; i <= tam2.Length; i++)
                    {
                        carry[i] = tam2[i];
                    }
                }
                xoaSo0(v2);
                xoaSo0(carry);
                //gan vao kq.nguyen
                insert_begin(kq.nguyen, Convert.ToChar(dem));
            }
            kq.nguyen.Reverse();
            //----------------chia tiep phan tphan neu carry > 0----------------
            int chuSo = 0;

            nho = 0;
            //while (strcmp(carry, "0") != 0 && chuSo <= 250)     //chia cho toi khi carry=0 hoac so chu so =250
            while (!carry.Equals("0") && chuSo <= 250)
            {
                themZeroVaoDau(carry);
                xoaSo0(carry);
                if (carry.Length > v2.Length)     //them 1 so 0 vao duoi v2 neu carry > v2
                {
                    i         = v2.Length;
                    v2[i]     = '0';
                    v2[i + 1] = '\0';
                }
                int    dem = 0;
                char[] tam2 = null;
                int    a2, b2;
                while (compare(carry, v2) != 1)     //chia carry & v2, dem la ket qua
                {
                    dem++;
                    //su dung phep tru
                    for (i = 0; i < carry.Length; i++)
                    {
                        a2 = Convert.ToInt32(carry[i]);
                        b2 = Convert.ToInt32(v2[i]);
                        if (a2 < b2 + nho)        //so bi tru < so tru
                        {
                            tam2[i] = Convert.ToChar(a2 - b2 - nho + 10);
                            nho     = 1;
                        }
                        else            //so bi tru > so tru
                        {
                            tam2[i] = Convert.ToChar(a2 - b2 - nho);
                            nho     = 0;
                        }
                    }
                    tam2[i] = '\0';
                    //strcpy(carry, tam2);
                    for (i = 0; i <= tam2.Length; i++)
                    {
                        carry[i] = tam2[i];
                    }
                }
                xoaSo0(v2);
                xoaSo0(carry);
                //gan vao kq.tphan
                insert_begin(kq.tphan, Convert.ToChar(dem));
                chuSo++;
            }
            kq.tphan.Reverse();
            xoaSo0(kq.tphan);
            return(kq);
        }
示例#2
0
        public static bignum operator +(bignum a, bignum b)
        {
            int    nho = 0;
            int    i;
            bignum kq = new bignum();

            kq.dau    = 1;
            kq.nguyen = null;
            kq.tphan  = null;
            kq.cham   = 1;
            //--------------xu li tphan------
            if (a.tphan.Length >= b.tphan.Length)
            {
                for (i = b.tphan.Length; i < a.tphan.Length; i++)
                {
                    b.tphan += 0;
                }
            }
            else
            {
                for (i = a.tphan.Length; i < b.tphan.Length; i++)
                {
                    a.tphan += 0;
                }
            }
            //cong
            for (i = a.tphan.Length - 1; i >= 0; i--)
            {
                kq.tphan += cong2so(a.tphan[i], b.tphan[i], ref nho);
            }
            Reverse(ref kq.tphan);
            xoaSo0(ref kq.tphan);   //xoa so 0 du o phan tphan vd: 12300->123
            //-----------xu li nguyen
            Reverse(ref a.nguyen);
            Reverse(ref b.nguyen);
            if (a.nguyen.Length >= b.nguyen.Length)
            {
                for (i = b.nguyen.Length; i < a.nguyen.Length; i++)
                {
                    b.nguyen += 0;
                }
            }
            else
            {
                for (i = a.nguyen.Length; i < b.nguyen.Length; i++)
                {
                    a.nguyen += 0;
                }
            }
            for (i = 0; i < a.nguyen.Length; i++)
            {
                kq.nguyen += cong2so(a.nguyen[i], b.nguyen[i], ref nho);
            }
            if (nho > 0)
            {
                kq.nguyen += nho.ToString();
            }
            Reverse(ref kq.nguyen);
            kq.cham = a.nguyen.Length;
            return(kq);
        }
示例#3
0
        public static bignum operator *(bignum a, bignum b)
        {
            int i = 0;

            //--------lam cho 2 phan tphan co cung chu so--------
            //---------tham so 0 vao cuoi------------
            if (a.tphan.Length >= b.tphan.Length)
            {
                for (i = b.tphan.Length; i < a.tphan.Length; i++)
                {
                    b.tphan += 0;
                }
            }
            else
            {
                for (i = a.tphan.Length; i < b.tphan.Length; i++)
                {
                    a.tphan += 0;
                }
            }
            //----------------noi phan nguyen va tphan lai----------------
            string v1 = a.nguyen + a.tphan;
            string v2 = b.nguyen + b.tphan;

            Reverse(ref v1);
            Reverse(ref v2);
            //----------------nhan v1 voi v2------------------------------
            bignum kq = new bignum();   //mac dinh gia tri la 0.0

            kq.nguyen = "0";
            kq.tphan  = "0";
            kq.dau    = 1;
            kq.cham   = 1;
            bignum c = new bignum();

            c.nguyen = null;
            c.tphan  = null;
            c.dau    = 1;
            c.cham   = 1;

            //-----------bat dau nhan tung ptu cua v1 cho v2 ---------


            for (i = 0; i < v1.Length; i++)
            {
                //c.tphan = "0";
                int nho = 0, tam, j = 0;
                //nhan v2 va v1[i]
                for (j = 0; j < b.tphan.Length; j++)
                {
                    tam      = (v1[i] - 48) * (v2[j] - 48) + nho;
                    c.tphan += (tam % 10).ToString();
                    nho      = tam / 10;
                }
                for (j = b.tphan.Length; j < v2.Length; j++)
                {
                    tam       = (v1[i] - 48) * (v2[j] - 48) + nho;
                    c.nguyen += (tam % 10).ToString();
                    nho       = tam / 10;
                }
                if (nho > 0)
                {
                    c.nguyen += nho.ToString();
                }
                Reverse(ref c.tphan);
                Reverse(ref c.nguyen);
                for (j = 0; j < i; j++) //moi lan nhan tang len gap 10 lan
                {
                    c.tphan  += "0";
                    c.nguyen += c.tphan[0];
                    c.tphan   = c.tphan.Remove(0, 1);
                }
                xoaSo0(ref c.tphan); //xoa so 0 phan tphan
                c.cham   = c.nguyen.Length;
                kq       = kq + c;   //cong hai bignum
                c.nguyen = null;     //tra c ve null
                c.tphan  = null;     //tra c ve null
            }

            // chia a.tphan lan kq cho 10 vi a nhan vao la so nguyen
            for (i = 0; i < a.tphan.Length; i++) //moi lan nhan tang len gap 10 lan
            {
                //dao chuoi-them so vao phia sau-dao chuoi
                Reverse(ref kq.tphan);
                kq.tphan += kq.nguyen[kq.nguyen.Length - 1];
                Reverse(ref kq.tphan);
                //dao chuoi--them so 0 phia sau--xoa ptu dau tien--xoa so 0--dao chuoi
                Reverse(ref kq.nguyen);
                kq.nguyen += '0';
                kq.nguyen  = kq.nguyen.Remove(0, 1);
                xoaSo0(ref kq.nguyen);
                Reverse(ref kq.nguyen);
            }
            return(kq);
        }
示例#4
0
        public static bignum operator -(bignum a, bignum b)
        {
            int    i;
            int    nho = 0;
            bignum kq  = new bignum();

            kq.dau    = 1;
            kq.nguyen = null;
            kq.tphan  = null;
            kq.cham   = 1;
            //------------tphan-------------
            //them so 0
            if (a.tphan.Length > b.tphan.Length)
            {
                for (i = b.tphan.Length; i < a.tphan.Length; i++)
                {
                    b.tphan += "0";
                }
            }
            else
            {
                for (i = a.tphan.Length; i < b.tphan.Length; i++)
                {
                    a.tphan += "0";
                }
            }
            //------tru-------
            for (i = a.tphan.Length - 1; i >= 0; i--)
            {
                kq.tphan += tru2so(a.tphan[i], b.tphan[i], ref nho);
            }
            Reverse(ref kq.tphan);
            xoaSo0(ref kq.tphan);
            //----------nguyen----------
            Reverse(ref a.nguyen);
            Reverse(ref b.nguyen);
            //them so 0
            if (a.nguyen.Length >= a.nguyen.Length)
            {
                for (i = b.nguyen.Length; i < a.nguyen.Length; i++)
                {
                    b.nguyen += 0;
                }
            }
            else
            {
                for (i = a.nguyen.Length; i < b.nguyen.Length; i++)
                {
                    a.nguyen += 0;
                }
            }
            for (i = 0; i < a.nguyen.Length; i++)
            {
                kq.nguyen += tru2so(a.nguyen[i], b.nguyen[i], ref nho);
            }
            if (nho > 0)
            {
                kq.nguyen += nho.ToString();
            }
            Reverse(ref kq.nguyen);
            kq.cham = a.nguyen.Length;
            return(kq);
        }