【问题标题】:C# - Class that uses ILists to store huge integers without BigInt. Can't figure out how to use CompareTo and Int.TryParse to +, -, and * two ListsC# - 使用 ILists 来存储没有 BigInt 的大整数的类。不知道如何使用 CompareTo 和 Int.TryParse 到 +、- 和 * 两个列表
【发布时间】:2018-02-26 08:03:43
【问题描述】:

我一直在做一项任务,我是 C# 的初学者。我必须实现一个类似于 BigInt 可以做的程序:用两个大得离谱的值执行加法、减法或乘法(实际上不使用 BigInt 库)。有人告诉我使用 CompareTo,它可以使创建加法、减法和乘法方法变得容易,但我不知道如何实现 CompareTo。我什至不知道我的课程是否正确实施,或者我是否遗漏了一些重要的东西。 这是我的代码:

public class HugeInt
{
    char sign;
    public IList<int> theInt = new List<int>();


    public string ToString(IList<int> theInt)
    {
        string bigInt = theInt.ToString();
        return bigInt;
    }

    public HugeInt CompareTo(HugeInt num1)
    {
        int numParse;
        string number = ToString(theInt); /// I did this to convert the List into a string
        for(int i = 0; i < number.Length; i++)
        {
            bool temp = Int32.TryParse(number, out numParse); /// Supposed to change each index of the string to a separate integer (not sure how to properly do this)

            /// These are *supposed to* perform operations on two HugeInts ///
            num1.plus(numParse, num1);
            num1.minus(numParse, num1);
            num1.times(numParse, num1);
        }

        return num1;
    }

我不是来为这项任务寻求所有答案的,我已经为此工作了几个小时,无法弄清楚我做错了什么——我已经做了很多谷歌搜索。提前感谢所有建议和帮助!

【问题讨论】:

  • CompareTo 用于比较对象。 InfInt 是什么?您需要创建方法来执行加/减/倍操作。
  • 抱歉,InfInt 是该类的原始名称,但为了更清楚起见,我将其更改为 HugeInt,但错过了一个!我的任务是比较两个 HugeInts 并获取它们之间的距离值,但它们是我转换为字符串的列表,我需要解析它们以将它们转换回单独的整数。我需要通过两个 HugeInts 吗? CompareTo 是我目前陷入困境的地方,我可以在弄清楚这一点后创建 plus/minus/times 方法。提前感谢您的帮助!
  • 我猜你可能被要求使用IComparable,它有一个名为CompareTo的方法,我建议你通读这篇文章并相应地修改你的代码msdn.microsoft.com/en-us/library/…
  • 原来是这样,但分配已更新,我们被告知我们没有必须使用 IComparable 接口,因此我们可以返回一个带有 CompareTo 的 HugeInt,但我不知道哪个更简单。我要调查两者。谢谢!

标签: c# list class compareto bigint


【解决方案1】:

要写这样一门课,它需要你对如何手工做数学有一点了解。例如,当添加两个数字时,您首先添加它们的最低有效数字。如果结果大于 9,则必须将 1 带入下一位 (explanation)。然后你继续下一个数字。

现在,这是我的看法。我想将“巨大的 int”保存为从最低有效数字开始的数字列表。然后我如上所述实现Plus 方法。我可以通过查看位数来比较两个“巨大的整数”。位数最多的数是最大的。在位数相同的情况下,我需要从最高位开始逐位比较每个数字。

以下内容只是帮助您入门。它只处理正整数并且有PlusCompareTo 方法。请注意,我没有处理很多极端情况。

可以这样使用:

var num1 = new HugeInt("11112222333399998888777123123");
var num2 = new HugeInt("00194257297549");

Console.WriteLine(num1.Plus(num2).ToString()); // Writes 11112222333399999083034420672
Console.WriteLine(num1.CompareTo(num2)); // Writes -1 since num1 > num2

这是课程:

public class HugeInt
{
    // The array that contains all the digits of the number. To create a new number, you do not change this array but instead you create a new instance of HugeInt.
    // The first digit is the least significant digit.
    private readonly int[] digits; 

    public HugeInt(string number)
    {
        // Trim off the leading zeros
        number = number.TrimStart('0');
        if (number == "")
            number = "0";

        // Convert to digit array with the least significant digit first
        digits = number.ToCharArray().Select(c => int.Parse(c.ToString())).Reverse().ToArray();
    }

    public HugeInt(IList<int> digits)
    {
        // Trim off the leading zeros
        var d = digits.ToList();
        while (d.Count > 1 && d.Last() == 0)
            d.RemoveAt(d.Count - 1);

        // Convert to digit array with the least significant digit first
        this.digits = d.ToArray();
    }

    public HugeInt Plus(HugeInt num)
    {
        // Add two positive integers by adding each digit together, starting with the least significant digit. 
        var result = new List<int>();
        int carry = 0;
        for (var i = 0; i < this.digits.Length || i < num.digits.Length; i++)
        {
            var digit1 = i < this.digits.Length ? this.digits[i] : 0;
            var digit2 = i < num.digits.Length ? num.digits[i] : 0;
            var digitResult = digit1 + digit2 + carry;
            carry = 0;
            if (digitResult >= 10)
            {
                digitResult -= 10;
                carry = 1;
            }
            result.Add(digitResult);
        }
        if (carry > 0)
            result.Add(carry);

        return new HugeInt(result);
    }

    public int CompareTo(HugeInt num)
    {
        // First compare by length of number
        if (this.digits.Length > num.digits.Length)
            return -1;
        else if (this.digits.Length < num.digits.Length)
            return 1;
        else
        {
            // If lengths are equal, then compare each digit - starting with the most significant digit.
            for (var i = this.digits.Length - 1; i >= 0; i--)
            {
                var cmp = this.digits[i].CompareTo(num.digits[i]);
                if (cmp != 0)
                    return cmp;
            }
            return 0;
        }
    }

    public override string ToString()
    {
        return String.Join("", digits.Reverse());
    }
}

【讨论】:

  • 哇,谢谢!我已经阅读了您的代码,它非常有见地。我会尝试从中学习并继续我的计划。这为 CompareTo 方法和类本身提供了很多清晰的信息。再次,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2017-08-18
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多