當前位置: 首頁>>代碼示例>>C#>>正文


C# BigInteger.CompareTo方法代碼示例

本文整理匯總了C#中System.Numerics.BigInteger.CompareTo方法的典型用法代碼示例。如果您正苦於以下問題:C# BigInteger.CompareTo方法的具體用法?C# BigInteger.CompareTo怎麽用?C# BigInteger.CompareTo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Numerics.BigInteger的用法示例。


在下文中一共展示了BigInteger.CompareTo方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: foreach

object[] values = { BigInteger.Pow(Int64.MaxValue, 10), null, 
                    12.534, Int64.MaxValue, BigInteger.One };
BigInteger number = UInt64.MaxValue;

foreach (object value in values)
{
   try {
      Console.WriteLine("Comparing {0} with '{1}': {2}", number, value, 
                        number.CompareTo(value));
   }
   catch (ArgumentException) {
      Console.WriteLine("Unable to compare the {0} value {1} with a BigInteger.",
                        value.GetType().Name, value);
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:15,代碼來源:BigInteger.CompareTo

輸出:

Comparing 18446744073709551615 with '4.4555084156466750133735972424E+189': -1
Comparing 18446744073709551615 with '': 1
Unable to compare the Double value 12.534 with a BigInteger.
Unable to compare the Int64 value 9223372036854775807 with a BigInteger.
Comparing 18446744073709551615 with '1': 1

示例2:

BigInteger bigIntValue = BigInteger.Parse("3221123045552");

byte byteValue = 16;
sbyte sbyteValue = -16;
short shortValue = 1233;      
ushort ushortValue = 1233;
int intValue = -12233;
uint uintValue = 12233;
long longValue = 12382222;
ulong ulongValue = 1238222;

Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, byteValue, 
                  bigIntValue.CompareTo(byteValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, sbyteValue, 
                  bigIntValue.CompareTo(sbyteValue)); 
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, shortValue, 
                  bigIntValue.CompareTo(shortValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, ushortValue, 
                  bigIntValue.CompareTo(ushortValue)); 
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, intValue, 
                  bigIntValue.CompareTo(intValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, uintValue, 
                  bigIntValue.CompareTo(uintValue)); 
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, longValue, 
                  bigIntValue.CompareTo(longValue));
Console.WriteLine("Comparing {0} with {1}: {2}", 
                  bigIntValue, ulongValue, 
                  bigIntValue.CompareTo(ulongValue));
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:35,代碼來源:BigInteger.CompareTo

輸出:

Comparing 3221123045552 with 16: 1
Comparing 3221123045552 with -16: 1
Comparing 3221123045552 with 1233: 1
Comparing 3221123045552 with 1233: 1
Comparing 3221123045552 with -12233: 1
Comparing 3221123045552 with 12233: 1
Comparing 3221123045552 with 12382222: 1
Comparing 3221123045552 with 1238222: 1

示例3: StarInfo

//引入命名空間
using System;
using System.Collections.Generic;
using System.Numerics;

public struct StarInfo : IComparable<StarInfo>
{
   // Define constructors.
   public StarInfo(string name, double lightYears)
   {
      this.Name = name;
      
      // Calculate distance in miles from light years.
      this.Distance = (BigInteger) Math.Round(lightYears * 5.88e12);
   }

   public StarInfo(string name, BigInteger distance)
   {
      this.Name = name;
      this.Distance = distance;
   }

   // Define public fields.
   public string Name;
   public BigInteger Distance;

   // Display name of star and its distance in parentheses.
   public override string ToString()
   {
      return String.Format("{0,-10} ({1:N0})", this.Name, this.Distance);
   }

   // Compare StarInfo objects by their distance from Earth.
   public int CompareTo(StarInfo other)
   {
      return this.Distance.CompareTo(other.Distance);
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:38,代碼來源:BigInteger.CompareTo

示例4: Main

public class Example
{
   public static void Main()
   {
      StarInfo star;
      List<StarInfo> stars = new List<StarInfo>();

      star = new StarInfo("Sirius", 8.6d);
      stars.Add(star);
      star = new StarInfo("Rigel", 1400d);
      stars.Add(star);
      star = new StarInfo("Castor", 49d);
      stars.Add(star);
      star = new StarInfo("Antares", 520d);
      stars.Add(star);

      stars.Sort();

      foreach (StarInfo sortedStar in stars)
         Console.WriteLine(sortedStar);
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:22,代碼來源:BigInteger.CompareTo

輸出:

Sirius     (50,568,000,000,000)
Castor     (288,120,000,000,000)
Antares    (3,057,600,000,000,000)
Rigel      (8,232,000,000,000,000)


注:本文中的System.Numerics.BigInteger.CompareTo方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。