http://www.codeproject.com/Articles/2728/C-BigInteger-Class
------解决方案-------------------- 不用BigInteger的话,可以考虑使用数组存储各个位的数值
------解决方案-------------------- 2个数的和可以参看这个: http://www.cnblogs.com/chenping-987123/archive/2010/09/28/1837881.html 那其他的差,乘,除,阶乘就都差不多的做法。
------解决方案-------------------- 好吧看来你是想让我贴代码了 只写了个相加操作 给你点儿思路 public class BigInter { private string _strInt; private int[] _intArr = null; private int _length; public BigInter(string strInt) { this._strInt = strInt; var tempCharArray = this._strInt.ToCharArray(); this._length = tempCharArray.Count(); this._intArr = new int[tempCharArray.Length]; for (int i = 0; i < tempCharArray.Length; i++)//将string里面的数字放到int数组里面 { var currentStrNum = tempCharArray[i].ToString(); int intNum = 0; if (int.TryParse(currentStrNum, out intNum)) { _intArr[i] = intNum; } else { throw new ArgumentException(string.Format("输入数字在第{0}位不合法", i + 1)); } } }
public static string Add(BigInter left, BigInter right) { var minLength = Math.Min(left._length, right._length); var maxLength = Math.Max(left._length, right._length); var theBiggerInter = left._length > right._length ? left._intArr : right._intArr; var theSmallerInter = theBiggerInter==left._intArr ? right._intArr : left._intArr; var tempSmaller = new int[maxLength]; for (int i = 0; i < maxLength; i++) { tempSmaller[i] = 0; } for (int i =0; i <minLength; i++) { tempSmaller[maxLength-i-1] = theSmallerInter[minLength-i-1]; }//上面两个循环将比较小的那个数的前面几位用0填充 var resultIntArray = new int[maxLength + 1];//结果数组需要大一位以保留进位 for (int i = maxLength; i > 0; i--)//反着遍历 符合直觉上的相加 { var currentResult = theBiggerInter[i-1] + tempSmaller[i-1]; if (currentResult >= 10) { ++theBiggerInter[i - 1]; resultIntArray[i] = currentResult - 10; } else { resultIntArray[i] = currentResult; }//这一段其实就是小学数学,相加大于10就向上一位+1 } StringBuilder result = new StringBuilder();//输出为string不解释 foreach (var temp in resultIntArray) { result.Append(temp.ToString()); } return result.ToString(); } } 调用 class Program { static void Main(string[] args) { var left = new BigInter("12345678901234567890123");//这个应该超出int的范围了吧 var right = new BigInter("12345678901234567890123"); Console.Write(BigInter.Add(left, right)); Console.ReadLine(); } }