两个大数相加的问题

lcmidnight830118 2009-07-01 09:49:47
有两个非常大的数字M和N,存储形式为string,将其相加后得到的数字也存为string类型并返回
这个程序怎么写
...全文
675 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
大Y 2011-04-15
  • 打赏
  • 举报
回复


学习了
chen_dian_dian 2010-08-23
  • 打赏
  • 举报
回复
class LargeNum
{
string numValue;
public int Length
{
get { return numValue.Length; }
}
/// <summary>
/// 获取和设置当前大数的制定位的数字值
/// </summary>
/// <param name="index">指定位的索引</param>
/// <returns>指定位的数值</returns>
public int this[int index]
{
get { return Convert.ToInt32(numValue[index].ToString()); }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="numValue">大数的值</param>
public LargeNum(string numValue)
{
this.numValue = numValue;
}
/// <summary>
/// 计算两个指定大数的和
/// </summary>
/// <param name="num1">一个数</param>
/// <param name="num2">另一个数</param>
/// <returns></returns>
public static LargeNum operator +(LargeNum num1, LargeNum num2)
{
LargeNum numLong;
LargeNum numShort;
//根据两个数字的长度,分别设置为长的数和短的数
if (num1.Length < num2.Length)
{
numShort = num1;
numLong=num2;
}
else
{
numShort = num2;
numLong =num1 ;
}
int[] result = new int[numLong .Length+ 1];

//计算结果的后几位,从最后一位摆竖式开始算,计算到短的数那么多位
for (int i =numLong.Length;i> numLong.Length - numShort.Length; i--)
{
result[i] = numLong[i-1] + numShort[i - numLong.Length + numShort.Length-1]+result[i];
if (result[i] >= 10)
{
result[i-1] = 1;
result[i] = result[i] - 10;
}
}
//计算结果的前几位,把长的数剩下的几位加上
for (int i = numLong.Length - numShort.Length ; i >0; i--)
{
result[i] = numLong[i-1]+result[i];
if (result[i] >= 10)
{
result[i - 1] = 1;
result[i] = result[i] - 10;
}
}
return new LargeNum( ConvertIntArrayToString(result)/*将计算结果转换成字符串*/);//将计算结果转换成大数,返回
}

public override string ToString()
{
return numValue;
}
/// <summary>
/// 将指定的整型数组转换成字符串
/// </summary>
/// <param name="intArray">整型数组</param>
/// <returns>对应字符串,每个整数对应数组的一位</returns>
private static string ConvertIntArrayToString(int[] intArray)
{
string result="";
for (int i = 1; i < intArray.Length; i++)
{
result += intArray[i].ToString();
}
if (intArray[0] >0)
{
result =intArray[0]+ result;
}
return result;
}
}
Deathsign 2009-07-01
  • 打赏
  • 举报
回复
答案出来了吧

高精度计算
zgke 2009-07-01
  • 打赏
  • 举报
回复
自己算....


private void button2_Click(object sender, EventArgs e)
{
string _Value1 = "1231231231231231298379842391214331221361524398124391824311322131234918249";
string _Value2 = "9999999999999999999999999999999991232345234523452304952345230234523452349";


string _Text = AddNumbText(_Value1, _Value2);

}

public string AddNumbText(string p_Value1, string p_Value2)
{
if (p_Value2.Length < p_Value1.Length)
{
p_Value2 = p_Value2.PadLeft(p_Value1.Length, '0');

}
else
{
p_Value1 = p_Value1.PadLeft(p_Value2.Length, '0');
}

char[] _Test = new char[p_Value1.Length];
bool _Add = false;
int _Temp = 0;

for (int i = p_Value1.Length - 1; i != -1; i--)
{
byte _Int1 = Convert.ToByte(p_Value1[i].ToString(), 10);
byte _Int2 = Convert.ToByte(p_Value2[i].ToString(), 10);
_Temp = _Int1 + _Int2;
if (_Add) _Temp++;

if (_Temp > 10)
{
_Add = true;
_Test[i] = _Temp.ToString()[1];
}
else
{
_Add = false;
_Test[i] = _Temp.ToString()[0];
}
}

if (_Add)
{
return "1" + new string(_Test);
}
else
{
return new string(_Test);
}
}


啵比 2009-07-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kjiwu 的回复:]
自己写的,试了试可以。
C# codepublicclass MyFucntions
{privatestatic Boolean TwoBigNumbersAdd(string num1,string num2,outstring result)
{string bigone="";string tmpStr="";string smallone="";int len=0;if (num1.Length< num2.Length)
{
len= num1.Length;
bigone= num2;
smallone= num1;
}else
{
len= num2.Length;
bigone= num1;
smallone= num2;
}

tmpStr= bigone.Substring(bigone.Length- len);//取出len这么长的低位部分 bigone= bigone.Substring(0, bigone.Length- len);//取出大数剩余的高位部分int tmp=0;//进位 result="";//低位部分的计算for (int i= len-1; i>=0; i--)
{try
{int a= Convert.ToInt32(Convert.ToString( smallone[i]));int b= Convert.ToInt32(Convert.ToString(tmpStr[i]));int sum= a+ b+ tmp;

result= Convert.ToString( sum%10 )+ result;
tmp= sum/10;
}catch (Exception e)
{
result="error";returnfalse;
}
}//剩余高位部分的计算if (tmp!=0)
{for (int i= bigone.Length-1; i>=0; i--)
{try
{int a= Convert.ToInt32(Convert.ToString(bigone[i]));int sum= a+ tmp;

result= Convert.ToString(sum%10)+ result;
tmp= sum/10;
}catch (Exception e)
{
result="error";returnfalse;
}
}
}else
{
result= bigone+ result;
}returntrue;
}publicstatic Boolean BigNumbersAdd(string[] nums,outstring Result)
{string firstnum= nums[0];try
{for (int i=1; i< nums.Length; i++)
{if (!TwoBigNumbersAdd(firstnum, nums[i],out firstnum))
{
Result="Error";returnfalse;
}
}
}catch (Exception e)
{
Result="Error";returnfalse;
}

Result= firstnum;returntrue;
}
}
[/Quote]
正解!
angelfeng9 2009-07-01
  • 打赏
  • 举报
回复
楼上好强
kjiwu 2009-07-01
  • 打赏
  • 举报
回复
自己写的,试了试可以。

public class MyFucntions
{
private static Boolean TwoBigNumbersAdd(string num1, string num2, out string result)
{
string bigone = "";
string tmpStr = "";
string smallone = "";
int len = 0;
if (num1.Length < num2.Length)
{
len = num1.Length;
bigone = num2;
smallone = num1;
}
else
{
len = num2.Length;
bigone = num1;
smallone = num2;
}

tmpStr = bigone.Substring(bigone.Length - len);//取出len这么长的低位部分
bigone = bigone.Substring(0, bigone.Length - len); //取出大数剩余的高位部分

int tmp = 0; //进位
result = "";

//低位部分的计算
for (int i = len-1; i >= 0; i--)
{
try
{
int a = Convert.ToInt32(Convert.ToString( smallone[i]));
int b = Convert.ToInt32(Convert.ToString(tmpStr[i]));
int sum = a + b + tmp;

result = Convert.ToString( sum % 10 ) + result;
tmp = sum / 10;
}
catch (Exception e)
{
result = "error";
return false;
}
}

//剩余高位部分的计算
if (tmp != 0)
{
for (int i = bigone.Length - 1; i >= 0; i--)
{
try
{
int a = Convert.ToInt32(Convert.ToString(bigone[i]));
int sum = a + tmp;

result = Convert.ToString(sum % 10) + result;
tmp = sum / 10;
}
catch (Exception e)
{
result = "error";
return false;
}
}
}
else
{
result = bigone + result;
}

return true;
}

public static Boolean BigNumbersAdd(string[] nums, out string Result)
{
string firstnum = nums[0];
try
{
for (int i = 1; i < nums.Length; i++)
{
if (!TwoBigNumbersAdd(firstnum, nums[i], out firstnum))
{
Result = "Error";
return false;
}
}
}
catch (Exception e)
{
Result = "Error";
return false;
}

Result = firstnum;
return true;
}
}
showjim 2009-07-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cgabriel 的回复:]
一年级学的竖式加法没有忘记吧。 M 和 N 都从最后一位(个位)向前遍历, 取每个字符相加,中间准备个进位就是。

无论多大的数都可以加起来
[/Quote]
简单的正解
CSharpEx 2009-07-01
  • 打赏
  • 举报
回复
用大数值型class,网络上可以下载到的
yuanleijiwei 2009-07-01
  • 打赏
  • 举报
回复
string  m;
string n;
string sum =Convert .ToString( Convert.ToInt64(m)+Convert.ToInt64(n));
yangls06 2009-07-01
  • 打赏
  • 举报
回复
按照加法的法则来就行了
CGabriel 2009-07-01
  • 打赏
  • 举报
回复
一年级学的竖式加法没有忘记吧。 M 和 N 都从最后一位(个位)向前遍历, 取每个字符相加,中间准备个进位就是。

无论多大的数都可以加起来
delectation 2009-07-01
  • 打赏
  • 举报
回复
数字是什么类型的,要考虑溢出。

110,569

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧