using System.Text;
private string AddNumber(string numA, string numB)
{
int lenA=0, lenB=0;
int lenI=0, intTen=0;
int intA=0, intB=0;
StringBuilder result=new StringBuilder("");
lenA=numA.Length;
lenB=numB.Length;
if(lenA>lenB)
lenI=lenA;
else
lenI=lenB;
for(int i=0; i<lenI; i++)
{
if(i<lenA)
intA=Convert.ToInt16(numA.Substring(lenA-i-1,1));
else
intA=0;
if(i<lenB)
intB=Convert.ToInt16(numB.Substring(lenB-i-1,1));
else
intB=0;
int temp=intA+intB+intTen;
result.Insert(0,Convert.ToString((int)temp%10));
intTen=(int)temp /10;
}
if(intTen>0)
result.Insert(0,Convert.ToString(intTen));
return result.ToString();
}
private string MultiNum(string NumA, string NumB)
{
int lenA=0, lenB=0;
int lenI=0;
string result="0";
string AddResult="0";
int Dig=0;
lenA=NumA.Length;
lenB=NumB.Length;
if(lenA>lenB)
{
lenI=lenB;
for(int i=0; i<lenI; i++)
{
AddResult="0";
Dig=Convert.ToInt16(NumB.Substring(lenB-i-1,1));
for(int j=0; j<Dig; j++)
AddResult=AddNumber(NumA,AddResult);
for(int k=0; k<i; k++)
AddResult=AddResult + "0";
result=AddNumber(AddResult,result);
}
}
else
{
lenI=lenA;
for(int i=0; i<lenI; i++)
{
AddResult="0";
Dig=Convert.ToInt16(NumA.Substring(lenA-i-1,1));
for(int j=0; j<Dig; j++)
AddResult=AddNumber(NumB,AddResult);
for(int k=0; k<i; k++)
AddResult=AddResult + "0";
result=AddNumber(AddResult,result);
}
}
return result;
}
private void button2_Click(object sender, System.EventArgs e)
{
DateTime dtm1=DateTime.Now;
Console.WriteLine(AddNumber("1225225236523456889","1255625622452552"));
Console.WriteLine(dtm1.Millisecond + " " + DateTime.Now.Millisecond );
}
private void button3_Click(object sender, System.EventArgs e)
{
DateTime dtm1=DateTime.Now;
string test="1";
test=MultiNum("453123123156341231651231563121563151213513422","1254564231541321541385435453453124563131541534313543");
Console.WriteLine(test);
Console.WriteLine(dtm1.Millisecond + " " + DateTime.Now.Millisecond );
}
超大正整数加法与乘法最笨方法实现
80酷酷网 80kuku.com