添加两个对象的数组元素

时间:2018-12-06 06:53:26

标签: c# math

class HugeInteger
{
    public const int HUGE_INTEGER_LIMIT = 40;
    public int[] hiDigits;
    public bool[] comparison;
    public int hiLength;
    private string hugeInteger;


    //constructor
    public HugeInteger()
    {
        hiDigits = new int[HUGE_INTEGER_LIMIT];
    }

    public HugeInteger(string hi)
    {
        hiDigits = new int[HUGE_INTEGER_LIMIT];
        hugeInteger = hi;
        Input(hi);   
    }

    public void Input(string input)
    {

        char[] hiDigitss = new char[HUGE_INTEGER_LIMIT];
        hiDigitss = input.ToCharArray();
        hiLength = hiDigits.Length;
        for (int i = hiLength - 1; i > 0; i--)
        {

            hiDigits[i] = hiDigitss[i] - '0';

    }

    public override string ToString()
    {
        string num = string.Join("", hiDigits.Select(x => x.ToString()).ToArray());
        return num;
    }

    public HugeInteger Add(HugeInteger val)
    {
        var result = new HugeInteger();
        int carry = 0;
        int sum = 0;
        hiLength = Math.Max(val.hiDigits.Length, this.hiDigits.Length);
        for (int i = 0; i < result.hiLength - 1; i++)
        {
            sum = this.hiDigits[i] + val.hiDigits[i] + carry;
            result.hiDigits[i] = sum % 10;
            carry = sum / 10;
        }

        //int[] result = new int[number1.length];
        //for (int i = number1.length - 1; i >= 0; i--)
        //{
        //    sum = number1[i] + number2[i] + carry;
        //    result[i] = sum % 10;
        //    carry = sum / 10;
        //}`enter code here`

        return result;
    }

    public bool IsEqualTo(HugeInteger hi)
    {
        comparison = new bool[hi.hiDigits.GetUpperBound(0)];
        for (int i = 0; i < this.hiDigits.GetUpperBound(0); i++)
        {
            if (this.hiDigits[i] == hi.hiDigits[i])
            {
                comparison[i] = true;
            }
            else
            {
                comparison[i] = false;
            }
        }
        if(comparison.All(c => c.Equals(true)))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

在上面的代码中,我试图在main中添加两个对象 使用

 num1.Add(num2)

num1num2都拥有一个整数数组int[]),它们表示一串数字中的数字。我正在尝试创建一种方法,该方法将通过添加result数组和num1数组来创建一个名为num2的新数组。经过调试,它给了我

  

索引超出范围

添加时未显示

val(num2),但显示了this(num1)

我也在尝试另一种减法。

编辑:按要求粘贴更多代码。当前正在尝试更改/修复input方法。

1 个答案:

答案 0 :(得分:3)

可能出现的情况是,您需要一个比初始数组更大的 数组:

    988 +  // 3 digits : int[3]
     45    // 2 digits : int[2]
   ----    
   1033    // 4 digits (cause of index out of range exception): should be int[4]

为此我们使用List<int>(假设两个值均为非负值,因此我们不必注意符号):

   public HugeInteger Add(HugeInteger val) {
     if (null == val)   
       throw new ArgumentNullException(nameof(val));

     int length = Math.Max(val.hiDigits.Length, this.hiDigits.Length);

     List<int> list = new List<int>(length + 1);

     int carry = 0;

     for (int i = 0; i < length; ++i) {
       // ? : - be careful; lengths can be different (another source of index out of range)
       int sum = ((i < val.hiDigits.Length) ? val.hiDigits[i] : 0) + 
                 ((i < this.hiDigits.Length) ? this.hiDigits[i] : 0) +
                 carry;

       list.Add(sum % 10);
       carry = sum / 10;
     }

     // do not forget to add carry (which can be 1)  
     list.Add(carry);
     list.Reverse();

     int[] array = list
       .SkipWhile(item => item == 0) // remove leading zeros: 00123 -> 123
       .DefaultIfEmpty()             // at least one digit: 000 -> 0
       .ToArray();

     //TODO: check the right syntax here
     return new HugeInteger(array);
   }