文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>大位数的加法和乘法

大位数的加法和乘法

时间:2011-05-14  来源:xiaying

因为计算机能表示的最大位数有限,有时计算的结果超出计算机所能表示的范围。用C#有两种解决的办法,一是用数组,二是用字符串,以下是用字符串实现的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace bignumcal
{
    class Program
    {
        #region
        private static string getmultiplyresult="";
        private static string getaddresult = "";
        private static string getmultiplybyonebit = "";
        #endregion

        static void Main(string[] args)
        {
            string result="";
            string string1 = "53888888888888888888888888888888888888888888888888884444444444444444444444449999999999999999999999999999999900000000000000000023333333333339";
            string string2 = "322222222222222222222257657652222222222222222222222222227777777777777777779999999999999999999999996";
            string string3 = "3485";
            string string4 = "";

            getaddresult = add(string1, string2);
            Console.WriteLine("两数相加:{0}", getaddresult);

            //getmultiplybyonebit = multiply(string3,'7',1);
            //Console.WriteLine("一个数乘以一个位:{0}", getmultiplybyonebit);

            string4 = multiply(string1, string2);
            Console.WriteLine("相乘结果:{0}",string4);

            Console.ReadKey();
           
        }

        /// <summary>两个大位数的乘法
        ///
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        public static string multiply(string str1, string str2)
        {
            int i = 0;
           
            string temp="";
            string strResult = "";                      //最终结果
            string strBitResult = "";                   //乘以每一位后的结果

            if (str1.Length < str2.Length)
            {
                temp = str2;
                str2 = str1;
                str1 = temp;
            }
           
            int length = str2.Length;
            char[] bitofstring = new char[length];      //分解出小串的每一位
            bitofstring = str2.ToCharArray();
            Array.Reverse(bitofstring);
            for (i = 0; i < length; i++)
            {
                strBitResult = multiply(str1, bitofstring[i], i);
                strResult = add(strResult,strBitResult);
            }
            return strResult;
        }

        /// <summary>一个一位数乘以一个大位数
       ///
       /// </summary>
       /// <param name="str1">被乘数</param>
       /// <param name="mulbit">乘数的一个位</param>
       /// <param name="x">该位是第几位,0表示各位</param>
       /// <returns></returns>
        public static string multiply(string str1, char mulbit, int x)
        {
            int i = 0;
            int upbit = 0;
            int length = str1.Length;
            int getbit = 0;                 //得到被乘数中的一位
            int bybit = (int)(mulbit - 48);   //乘数(1位)
            int multiresult = 0;            //两个数(位和位)相乘的结果

            string firststr = "";           //个位串
            string secondstr = "";          //十位串
            string lastresult = "";         //最后的结果

            char[] arr = str1.ToCharArray();

            char[] gettwobit = new char[2] { '0', '0' };

            Array.Reverse(arr);

            char[] sencondbit = new char[length+1];
            char[] firstbit = new char[length];
            sencondbit[0] = '0';

            for (i = 0; i < length; i++)
            {
                getbit = (int)(arr[i] - 48);
                multiresult = getbit * bybit;
                if (multiresult > 9)
                {
                    gettwobit = multiresult.ToString().ToCharArray();
                    firstbit[i] = gettwobit[1];
                    sencondbit[i+1] = gettwobit[0];
                }
                else
                {
                    firstbit[i] = (char)(multiresult + 48);
                    sencondbit[i+1] = '0';
                }

            }

            Array.Reverse(firstbit);
            Array.Reverse(sencondbit);

            firststr = new string(firstbit);
            secondstr = new string(sencondbit);

            lastresult = add(firststr,secondstr);
            for (i = 0; i < x; i++)
            {
                lastresult += "0";
            }

            return lastresult;

        }

        /// <summary>两个大位数的加法
        ///
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        public static string add(string str1, string str2)
        {
            #region
            int i = 0;
            int arrInt1 = 0;
            int arrInt2 = 0;
            int upbit = 0;                  //表示进位
            int length = 0;                 //表示较长的字符串的长度
            int twoBitResult = 0;           //表示两个数相加后的结果
            string getresult = "";
            #endregion

            //交换str1和str2,确保str1比str2长
            if (str1.Length < str2.Length)
            {
                string swap = "";
                swap = str2;
                str2 = str1;
                str1 = swap;
            }
            length = str1.Length;

            char[] arr1 = str1.ToCharArray();
            char[] arr2 = str2.ToCharArray();
            char[] get = new char[2] { '0', '0' };//表示加法之后两位数,get[0]表示本位数,get[1]表示进位数

            //大位数相加结果的字符数组
            char[] arrResult = new char[length + 1];
            for (i = 0; i < length + 1; i++)
            {
                arrResult[i] = '0';
            }

            //反转字符串
            Array.Reverse(arr1);
            Array.Reverse(arr2);

            #region 相加
            //进行相加运算
            for (i = 0; i < length; i++)
            {
                if (i < str2.Length)
                {
                    arrInt1 = (int)(arr1[i] - 48);
                    arrInt2 = (int)(arr2[i] - 48);
                    twoBitResult = arrInt1 + arrInt2 + upbit;
                    if (twoBitResult <= 9)
                    {
                        arrResult[i] = (char)(twoBitResult+48);
                        upbit = 0;
                    }
                    else
                    {

                        get = twoBitResult.ToString().ToCharArray();
                        arrResult[i] = get[1];
                        upbit = (int)(get[0]-48);
                    }
                }
                else
                {
                    if (upbit != 0)
                    {
                        arrInt1 = (int)(arr1[i] - 48);
                        twoBitResult = arrInt1 + upbit;
                        if (twoBitResult <= 9)
                        {
                            arrResult[i] = (char)(twoBitResult+48);
                            upbit = 0;
                        }
                        else
                        {

                            get = twoBitResult.ToString().ToCharArray();
                            arrResult[i] = get[1];
                            upbit = (int)(get[0] - 48);
                        }
                    }
                    else
                    {
                        arrResult[i] = arr1[i];
                    }
                }
            }
            #endregion

            arrResult[i] = Convert.ToChar(upbit+48);//进位

            if (upbit == 0)
            {
                char[] strResult = new char[length];
                for (int j = 0; j < length; j++)
                {
                    strResult[j] = arrResult[j];
                }
                Array.Reverse(strResult);
                getresult = new String(strResult);

            }
            else
            {
                Array.Reverse(arrResult);
                getresult = new String(arrResult);
            }
            return getresult;
        }

        /// <summary>两个数的减法
        ///
        /// </summary>
        /// <param name="str1">被减数</param>
        /// <param name="str2">减数</param>
        /// <returns></returns>
        public static string subtract(string str1, string str2)
        {
            string subResult = "";
            return subResult;
        }

        /// <summary>两个数的除法
        ///
        /// </summary>
        /// <param name="str1">被除数</param>
        /// <param name="str2">除数</param>
        /// <returns></returns>
        public static string divide(string str1, string str2)
        {
            string divideResult = "";
            return divideResult;
        }
    }

  
}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载