超大正整數加法與乘法最笨方法實現

來源:互聯網
上載者:User
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 );
}




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。