撰寫一個大整數類,使得基于此類可以實作對最高有100位的整數進行加、減運算,以及進行大整數比較(包括相等、大于和小于的比較)
盡量簡潔的
uj5u.com熱心網友回復:
大數運算了解一下C#實作大數字的運算1、添加參考:System.Numerics.dll
2、添加命名空間:using System.Numerics;
uj5u.com熱心網友回復:
System.Numerics 命名空間uj5u.com熱心網友回復:
有大概的源代碼嗎,我還是小白,還是寫不出來uj5u.com熱心網友回復:
根據官方檔案和例子 https://docs.microsoft.com/zh-cn/dotnet/api/system.numerics.biginteger?view=netcore-1.0您可以使用 new 關鍵字并提供任何整型或浮點值作為 BigInteger 建構式的引數。 (浮點值在分配到 BigInteger之前會被截斷。)下面的示例演示如何使用 new 關鍵字來實體化 BigInteger 值。
BigInteger bigIntFromDouble = new BigInteger(179032.6541);
Console.WriteLine(bigIntFromDouble);
BigInteger bigIntFromInt64 = new BigInteger(934157136952);
Console.WriteLine(bigIntFromInt64);
// The example displays the following output:
// 179032
// 934157136952
您可以宣告一個 BigInteger 變數并為其分配一個值,就像對任何數值型別一樣,前提是該值是整型。 下面的示例使用賦值從 Int64創建 BigInteger 值
long longValue = 6315489358112;
BigInteger assignedFromLong = longValue;
Console.WriteLine(assignedFromLong);
// The example displays the following output:
// 6315489358112
您可以使用 BigInteger 實體,就像使用任何其他整型型別一樣。 BigInteger 多載標準數字運算子,以使您能夠執行基本的數學運算(例如加法、減法、除法、乘法、減法、求反和一元求反)。 您還可以使用標準數值運算子來比較兩個 BigInteger 值
uj5u.com熱心網友回復:
C#內置的大數類可能比較慢,網上有其它的代碼轉載請註明出處,本文鏈接:https://www.uj5u.com/net/43097.html
標籤:C#
上一篇:請問控制元件位置移位的問題
