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

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";
int Dig=0;

lenA=NumA.Length;
lenB=NumB.Length;
if(lenA>lenB)
{
lenI=lenB;
for(int i=0; i<lenI; i++)
{
Dig=Convert.ToInt16(NumB.Substring(lenB-i-1,1));
for(int j=0; j<Dig; j++)
for(int k=0; k<i; k++)
}
}
else
{
lenI=lenA;
for(int i=0; i<lenI; i++)
{
Dig=Convert.ToInt16(NumA.Substring(lenA-i-1,1));
for(int j=0; j<Dig; j++)
for(int k=0; k<i; k++)
}
}
return result;
}

private void button2_Click(object sender, System.EventArgs e)
{
DateTime dtm1=DateTime.Now;
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

Apsara Conference 2019

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

Alibaba Cloud Free Trial

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