我想使用C#将两个非常大的数字加在一起,而不使用BigInterger
之类的库。我正在考虑使用两串数字,并试图通过携带数字等来实现几乎小学的添加方式。我的代码是残暴的,我认为它不起作用的原因是因为我不太了解我需要做什么?
string firstNumber = "1769";
string secondNumber = "723";
string product = firstNumber;
int carry = 0;
int positionFirst;
int positionSecond;
for (positionFirst = firstNumber.Length; positionFirst >= 1; positionFirst--)
{
for (positionSecond = secondNumber.Length; positionSecond >= 1; positionSecond--)
{
string currentDigitFirst = firstNumber.Substring(positionFirst - 1, 1);
string currentDigitSecond = secondNumber.Substring(positionSecond - 1, 1);
int intDigitFirst = int.Parse(currentDigitFirst);
int intDigitSecond = int.Parse(currentDigitSecond);
int currentDigitProduct = intDigitFirst + intDigitSecond;
if (carry != 0)
{
currentDigitProduct += carry;
carry /= 10;
}
int currentCarry = currentDigitProduct / 10;
carry += currentCarry;
int numberReplace = currentDigitProduct % 10;
product = product.Remove(positionFirst - 1, 1);
product = product.Insert(positionFirst - 1, numberReplace.ToString());
break;
}
}