码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-03-11 19:29:59      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

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

乘完后在补充小数点

        public static char[] SumF(string a, string b)
        {
            BuweiF(ref a, ref b);//小数点后补位
            List<char> item = new List<char>();
            List<char> ritems = new List<char>();
            int al = a.Length;
            int bl = b.Length;
            int r = 0;
            int jw = 0;//进位
            do
            {
                if (al > 0 && bl > 0)
                {
                    if (a[al - 1] == .)
                    {
                        r = .;
                    }
                    else
                    {
                        r = (int)a[al - 1] + (int)b[bl - 1] - 96 + jw;
                        jw = 0;
                        if (r >= 10)
                        {
                            jw++;
                            r = r - 10;
                        }
                    }
                }
                else if (al <= 0 && bl > 0)
                {
                    r = b[bl - 1] - 48 + jw;
                    jw = 0;
                    if (r >= 10)
                    {
                        jw++;
                        r = r - 10;
                    }
                }
                else if (al > 0 && bl <= 0)
                {
                    r = a[al - 1] - 48 + jw;
                    jw = 0;
                    if (r >= 10)
                    {
                        jw++;
                        r = r - 10;
                    }
                }

                if (r == 46)
                {
                    item.Add(Convert.ToChar(r));
                }
                else
                {
                    item.Add(Convert.ToChar(r.ToString()));
                }
                al--; bl--;
            } while (al > 0 || bl > 0);
            if (jw == 1)
            {
                item.Add(1);
            }
            for (int i = item.Count() - 1; i >= 0; i--)
            {
                ritems.Add(item[i]);
            }
            return ritems.ToArray();
        }

     

   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 bdot = 0;
            int adot = 0;
            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);

            }
            char[] re =new char[r.Length+1];
            int jjj = 0;
            for (int i = 0; i < re.Length; i++)
            {
                if (i ==re.Length-(a.Length - adot + b.Length - bdot))
                {
                    if (i != a.Length + b.Length)
                    {
                        re[i] = .;
                    }                   
                }
                else
                {
                    re[i] += r[jjj];
                    jjj++;
                }
               
            }
            return re;
        }
        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# 高精度乘法 支持小数(待优化)

标签:

原文地址:http://www.cnblogs.com/DhyDream/p/4330435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!