C# 高精度乘法 支持小数(待优化)

将N*N乘法转化为(N*n1)+(N*n2)....(N*nn)

乘完后在补充小数点

        public static char[] Quadrature(string a, string b)

        {

            char[] r = new char[1] { '0' };

            List<int> bu = new List<int>();

            List<int> au = new List<int>();

            int adot = a.Length;

            int bdot = b.Length;

            for (int i = b.Length - 1; i >= 0; i--)

            {

                if (b[i] == '.')

                {

                    bdot = i;

                }

                else

                {

                    bu.Add(b[i] - 48);

                }

            }

            for (int i = a.Length - 1; i >= 0; i--)

            {

                if (a[i] == '.')

                {

                    adot = i;

                }

                else

                {

                    au.Add(a[i] - 48);

                }

            }

            a = "";

            for (int i = au.Count() - 1; i >= 0; i--)

            {

                a += au[i];

            }



            for (int i = 0; i < bu.Count(); i++)

            {

                char[] qq = QuadUnit(a, bu[i]);

                string qqq = "";

                for (int j = 1; j < i + 1; j++)

                {

                    qqq = "";

                    foreach (var item in qq)

                    {

                        qqq += item;

                    }

                    qq = QuadUnit(qqq, 10);

                }

                qqq = "";

                foreach (var item in qq)

                {

                    qqq += item;

                }

                string rr = "";

                foreach (var item in r)

                {

                    rr += item;

                }

                r = SumF(rr, qqq);



            }

            List<char> rer = new List<char>();

            for (int i = 0; i < r.Count(); i++)

            {

                rer.Add(r[i]);

            }

            int con = rer.Count();

            int dot = a.Length + b.Length - (adot + bdot);



            //Console.WriteLine(a);

            //Console.WriteLine(b);

            //Console.WriteLine(a.Length);

            //Console.WriteLine(b.Length);

            //Console.WriteLine(adot);

            //Console.WriteLine(bdot);

            //Console.WriteLine(con);

            //Console.WriteLine(dot);

            //Console.WriteLine(con-dot);



            if (con - dot > 0)

            {

                if (con - dot != con)

                {

                    rer.Insert(con - dot + 1, '.');

                }

            }

            else if (con - dot < 0)

            {

                rer.Insert(0, '.');

                rer.Insert(0, '0');



                int co = -1 * (con - dot) - 1;

                while (co > 0)

                {

                    rer.Insert(2, '0');

                    co--;

                }

            }

            else

            {

                rer.Insert(0, '.');

                rer.Insert(0, '0');

            }



            return rer.ToArray();

        }

        public static char[] QuadUnit(string a, int b)

        {

            int r = 0;

            int jw = 0;

            char[] c = a.ToCharArray();

            List<char> items = new List<char>();

            List<char> ritems = new List<char>();

            for (int i = c.Count() - 1; i >= 0; i--)

            {

                if (a[i] == '.')

                {

                    r = '.';

                    items.Add(Convert.ToChar(r));

                }

                else

                {

                    r = ((int)c[i] - 48) * b + jw;

                    jw = 0;

                    if (r >= 10)

                    {

                        jw = r / 10;

                        r %= 10;

                    }

                    items.Add(Convert.ToChar(r.ToString()));

                }



            }

            if (jw > 0)

            {

                items.Add(Convert.ToChar(jw.ToString()));

            }



            for (int i = (items.Count() - 1); i >= 0; i--)

            {

                ritems.Add(items[i]);

            }

            return ritems.ToArray();

        }

 

你可能感兴趣的:(C#)