[源码分享]基于加法的高效极大数乘法

来源:互联网 发布:人工智能方面的书籍 编辑:程序博客网 时间:2024/05/16 12:28

数学乘法 从小学开始就一直是基于加法的,所以,我写了一个基于加法的大数乘法。扩展方法省去了类型转换的损耗,

原理如下:

         1 2 3

      *     5 6

------------------------

           7 3 8

+     6 1 5  这里加个0

-------------------------

=     6  8 8 8

 

好,直接上代码

 

 

using System.Text;namespace LongNumber{    public static class LongNumberHelper    {        /// <summary>        /// 大数相乘        /// </summary>        /// <param name="s1"></param>        /// <param name="s2"></param>        /// <returns></returns>        public static string LongNumberMul(string s1, string s2)        {            string result = "";            for (int i = s2.Length - 1; i >= 0; i--)            {                var s = StrMulChar(s1, s2[i], s2.Length - 1 - i);//计算s2每个数的乘积并给结果加足够的0                result = LongNumberAdd(result, s);//结果集累加            }            return result;        }        /// <summary>        /// 分步计算并进位        /// </summary>        /// <param name="s1"></param>        /// <param name="s2"></param>        /// <param name="count"></param>        /// <returns></returns>        private static string StrMulChar(string s1, char s2, int count)        {            StringBuilder sb = new StringBuilder();            int temp = 0;            for (int i = s1.Length - 1; i >= 0; i--)            {                var s = s1[i].CharToInt() * s2.CharToInt() + temp;                temp = 0;                while (s >= 10)                {                    temp++;                    s -= 10;                }                sb.Insert(0, s);            }            if (temp > 0)            {                sb.Insert(0, temp);            }            sb.Append('0', count);//给结果加0            return sb.ToString();        }        /// <summary>        /// 极大数加法        /// </summary>        /// <param name="s1"></param>        /// <param name="s2"></param>        /// <returns></returns>        public static string LongNumberAdd(string s1, string s2)        {            string result = "";            if (s1.Length > s2.Length)            {                result = AddToResult(s1, s2);            }            else            {                result = AddToResult(s2, s1);            }            return result;        }        /// <summary>        /// 长的加短的        /// </summary>        /// <param name="s1">长的数</param>        /// <param name="s2">短的数</param>        /// <returns></returns>        private static string AddToResult(string s1, string s2)        {            var len = s1.Length - s2.Length;            int temp = 0;            StringBuilder sb = new StringBuilder();            for (int i = s1.Length - 1; i >= 0; i--)            {                int s;                if (i - len >= 0)                {                    s = s1[i].CharToInt() + s2[i - len].CharToInt() + temp;                }                else                {                    s = s1[i].CharToInt() + temp;                }                temp = 0;                while (s >= 10)                {                    temp++;                    s -= 10;                }                sb.Insert(0, s);            }            if (temp > 0)            {                sb.Insert(0, temp);            }            return sb.ToString();        }        /// <summary>        /// 字符转换int        /// </summary>        /// <param name="s"></param>        /// <returns></returns>        static int CharToInt(this char s)        {            switch (s)            {                case '0': return 0;                case '1': return 1;                case '2': return 2;                case '3': return 3;                case '4': return 4;                case '5': return 5;                case '6': return 6;                case '7': return 7;                case '8': return 8;                case '9': return 9;                default:                    return 0;            }        }    }}