C# 如何比较整数数组中存储的大整数?

C# 如何比较整数数组中存储的大整数?,c#,arrays,biginteger,compareto,C#,Arrays,Biginteger,Compareto,我试图在C#中实现BigInteger类。现在我被一种叫做isLessThan(HugeInteger b)的方法卡住了。这是我的类和相应的方法 class HugeInteger { public int[] array = new int[40]; public HugeInteger(String s) { this.input(s); } // end constructor /*To read a huge integer h

我试图在C#中实现BigInteger类。现在我被一种叫做
isLessThan(HugeInteger b)
的方法卡住了。这是我的类和相应的方法

class HugeInteger
{
    public int[] array = new int[40];

    public HugeInteger(String s)
    {
        this.input(s);
    } // end constructor

    /*To read a huge integer having upto 40 digits, I am using an integer array to store 
      these values(input is read as a string for this matter) and later on will override 
      ToString() for output if needed.*/
    private void input(String s)
    {
        char[] charInteger = s.ToCharArray();
        int index = 0;
        for (int i = 0; i <= 39 - charInteger.Length; i++)
        {
            array[i] = 0;
        }
        for (int i = 39 - charInteger.Length + 1; i <= 39; i++)
        {

            array[i] = int.Parse(charInteger[index].ToString());
            index++;
        }
    }

    public bool isLessThan(HugeInteger that)
    {

        for (int i = 0; i <= 39; i++)
        {
            if (this.array[i] < that.array[i])
            {
                return true;
            }
        }
        return false;
    }
}
类HugeInteger
{
公共int[]数组=新int[40];
公共HugeInteger(字符串s)
{
这是一个或多个输入;
}//结束构造函数
/*要读取多达40位的大整数,我使用整数数组来存储
这些值(在这个问题上,输入是作为字符串读取的)稍后将重写
如果需要,输出的ToString()*/
专用无效输入(字符串s)
{
char[]charInteger=s.ToCharArray();
int指数=0;

对于(inti=0;i,以下是修复方法的方法

public bool isLessThan(HugeInteger that)
{
    for (int i = 0; i <= 39; i++)
    {
        if (this.array[i] < that.array[i])
        {
            return true;
        }
        if (this.array[i] > that.array[i])
        {
            return false;
        }
    }
    return false;
}
public bool isLessThan(HugeInteger that)
{
对于(int i=0;i表示.array[i])
{
返回false;
}
}
返回false;
}

基本上,当你比较两个数字时,如果它们不相等,那么你就知道其中一个大于或小于另一个。只有当它们相等时,你才能继续。你目前所做的只是检查第一个数字中是否至少有一个对应的数字小于第二个数字。所以对于9873和75,这是正确的,因为3<5。当与我的更改一样,当它比较9和0时,它将返回false。

好的,让我们实现比较;典型的方法是使用通用比较(在某些语言中,它甚至有一个特殊的运算符


当发生溢出时,CPU的条件代码被设置为后续指令可能无法提供正确的结果,因此,如果不在溢出发生之前预测溢出,那么所有花哨的逻辑都是徒劳的

(您无法检测到溢出:例如,如果我询问

if(MUCHTOOLARGENUMBER>somereasonalvalue){


那么,我无法保证测试将以何种方式进行评估。)

那么至少告诉我们哪里错了,或者你期望的是什么..?旁注:不要使用幻数,即
40
39
,但是声明一个常量,假设这是为了家庭作业,否则在C#中已经有一个
BigInteger
类。使用40是可以的,但是@DmitryBychenko说你应该在某个地方有一个常量at设置为40,然后在任何地方都使用常量。这样,如果你明天醒来决定将其更改为50,你只需更改常量的值。此外,最好执行类似于
i<40
的操作,或者在这一点上,你只需执行
i
。对不起,如果我调用
myHugeInteger.isLe怎么办ssThan(null)
?@DmitryBychenko我试图解释和涵盖一般情况,因为这只是OP的一个学习练习,但当然
null
检查可能是他们想要添加的内容,但可能是另一个问题的主题。如果我没记错的话,在大多数语言中(至少基于C),如果我增加一个数字超过它的最大值,它会被包装回最小值,对吗?(反之亦然,递减也超过一个最小值).这是真的--当然,细节取决于体系结构--但我的观点是,在处理器的条件代码溢出后,后续测试或测试和分支的结果可能不可靠。
  class HugeInteger {
    ...

    // Universal comparator
    //  +1 left > right
    //   0 left == right
    //  -1 left < right
    public static int Compare(HugeInteger left, HugeInteger right) {
      // first, we should deal with null's; let null be the leaset possible value

      // left and right are just the references of one inctance?
      if (Object.ReferenceEquals(left, right)) 
        return 0;
      else if (left == null)
        return -1;
      else if (right == null)
        return 1;

      // Now we checked for null, let's compare both arrays  

      //DONE: no 39, 40 or other magic numbers, but Length 
      for (int i = 0; i < left.array.Length; ++i)
        if (left.array[i] < right.array[i])
          return -1;
        else if (left.array[i] > right.array[i])
          return 1;

      return 0; // no differences found, so left equals to right
    }
    public bool isLessThan(HugeInteger other) {
      return Compare(this, other) < 0;
    }

    public bool isGreaterThan(HugeInteger other) {
      return Compare(this, other) > 0;
    }
    ....