C#版 大數計算機–乘法

來源:互聯網
上載者:User

class BigMultiply:BigCalculate
 

   {
       
public override string Oper(string num1, string num2)
       
{
           
if (num1.Equals("0")||num2.Equals("0"))
           
{
               
return "0";
           
}

           
List liAllNum = new List();//儲存乘數每位乘法的結果
           
List liCh = new List();//儲存乘數每位乘法中每一位的結果

           
//將長度較長的數字賦給num1,方便後面計算
           
if (num1.Length < num2.Length)
           
{
               
ChangeNum(ref num1, ref num2);
           
}
 
           
int flag;
           
for (int i = num2.Length-1; i >=0; i--)
           
{
               
//初始化上一位乘法中的資料
               
liCh.Clear();
               
flag = 0;
               
//計算第i位乘以第j位
               
for (int j = num1.Length - 1; j >= 0; j--)
               
{
                   
liCh.Add((char)((num2[i] - '0') * (num1[j] - '0')+flag+'0'));
                   
flag = (liCh[liCh.Count-1] -'0')/ 10;
                   
liCh[liCh.Count - 1] = (char)((liCh[liCh.Count - 1] - '0') %
10+'0');
               
}
               
//若進位不為0,則在加進位
               
if (flag>0)
               
{
                   
liCh.Add((char)(flag+'0'));
               
}
               
//添加結果集
               
liAllNum.Add(InvertOrder(new string(liCh.ToArray()))+new
string('0',liAllNum.Count));
           
}
           
int count = 0;
           
string res="0";
           
//調用add方法,將每一位乘法所得的結果相加,即為結果
           
BigAdd addOper= new BigAdd();
           
while (count
           
{
               
res = addOper.Oper(res,liAllNum[count]);
               
count++;
           
}

           
return res;  
       
}
}

相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.