我已经开始开发一个BigInt类了,现在我被卡住了。问题是,当我尝试将两个不同长度的数字相加时,结果不正确。例如,123 +1将返回223。我知道问题出在哪里,但我需要帮助来解决它。
public static BigInt operator +(BigInt n1, BigInt n2)
{
Stack<char> sNew = new Stack<char>();
Stack<char> sTemp = new Stack<char>();
int currentDigit1, currentDigit2, sum;
int carry = 0;
//insert the digits, XXXyyy + ZZZ = first insert ___yyy and then calculate XXX+ZZZ
if (n1.GetLength() > n2.GetLength())
{
while (n1.GetLength() > n2.GetLength())
sNew.Push(n1.sDigits.Pop());
}
else if (n2.GetLength() > n1.GetLength())
{
while (n2.GetLength() > n1.GetLength())
sNew.Push(n2.sDigits.Pop());
}
while (n1.sDigits.Count > 0)
{
currentDigit1 = int.Parse(n1.sDigits.Pop().ToString());
currentDigit2 = int.Parse(n2.sDigits.Pop().ToString());
sum = currentDigit1 + currentDigit2 + carry;
carry = 0;
if (sum > 10)
{
carry = 1;
sum = sum % 10;
}
sNew.Push(char.Parse(sum.ToString()));
}
//if there is a carry, for example 95+18
if (carry > 0)
sNew.Push(char.Parse(carry.ToString()));
//flip the stack
while (sNew.Count > 0)
sTemp.Push(sNew.Pop());
while (sTemp.Count > 0)
sNew.Push(sTemp.Pop());
return new BigInt(sNew);
}不管这个问题,这个类设计的模式是有效的吗?有没有更好的方法来设计这种类呢?
发布于 2012-10-11 08:39:46
这是一个相当浪费的表示,一个十进制数字使用完整的8位-大约60%的空间浪费!
即使您继续使用此表示法,也应考虑将内部表示法从Stack<char>转换为List<char>,其中最低有效数字存储在位置0,十位数的数字存储在位置1,依此类推。这将允许您使用单个循环实现加法,如果两个数字都可用,则在相同位置添加数字,或者将进位添加到较长数字的数字。
一种更好的表示方法是使用基数为256的系统,并将单个“数字”存储为字节数组。
请注意,加法并不是最难实现的操作:等到你遇到乘法和除法!要了解您需要解决的复杂性,请下载Java's implementation of BigInteger。
我假设你这样做是为了好玩,而不是作为真正项目的一部分。否则,就没有理由不使用.NET内置的BigInteger表示形式。
https://stackoverflow.com/questions/12830462
复制相似问题