是否有一个“数字”? .NET 中的结构/类?

发布于 2024-08-20 03:44:41 字数 1027 浏览 8 评论 0原文

我正在尝试存储一个可变长度数字,该数字可以包含前导零作为该数字的一部分。

.NET 框架中是否有一个类能够存储这样的值而不会丢失有关前导零的信息,并且可以超越一个值的上限长

我目前将它们存储在这样的类中,如果 BCL 中没有可用的结构或类,有什么方法可以更好地编写这个类:将

[Serializable]
public class Number
{
    public int[] Array { get; set; }
    public int Length { get { return Array.Length; } }

    public Number(string number)
    {
        Array = new int[number.Length];

        for (int i = 0; i < number.Length; i++)
        {
            Array[i] = Convert.ToInt32(number[i].ToString());
        }
    }

    public Number(int[] array)
    {
        Array = array;
    }

    public int ToInt()
    {
        return Convert.ToInt32(ToString());
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(Array.Length);

        foreach (int i in Array)
            sb.Append(i);

        return sb.ToString();
    }
}

其用作结构的能力将非常有用,轻松检查相等性的能力也一样。

粗体/斜体的项目是此类的要求。

I am attempting to store a variable length number that can have leading zeros as a part of that number.

Is there a class in the .NET framework capable of storing values like this without losing information about leading zeros, and could surpass the upper limit of a long?

I am currently storing them in a class like this, is there any way I could write this class better in the event there isn't some struct or class available in the BCL:

[Serializable]
public class Number
{
    public int[] Array { get; set; }
    public int Length { get { return Array.Length; } }

    public Number(string number)
    {
        Array = new int[number.Length];

        for (int i = 0; i < number.Length; i++)
        {
            Array[i] = Convert.ToInt32(number[i].ToString());
        }
    }

    public Number(int[] array)
    {
        Array = array;
    }

    public int ToInt()
    {
        return Convert.ToInt32(ToString());
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(Array.Length);

        foreach (int i in Array)
            sb.Append(i);

        return sb.ToString();
    }
}

The ability to use this as a struct would be very useful, as would the ability to check equality easily.

Items in bold/italic are the requirements of such a class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

給妳壹絲溫柔 2024-08-27 03:44:41

我建议看一下关于 C# 中大整数的问题。您可以针对前导零问题扩展它们。

I'd suggest taking a look at this question about big integers in C#. You could expand them for the leading zeros issue.

江湖正好 2024-08-27 03:44:41

我发现您的班级存在许多问题,应该通过重新设计来解决。

  • 你的类代表一个值并且是可变的。
  • 采用 int 数组参数的构造函数仅复制引用。
  • 该值可以大于 long,但从 ToInt() 方法以 int 形式返回。

为了缩小此类的大小,我要做的第一件事是使用整数数组作为恒定的位流,它构成了数字的二进制表示形式。为了确保任何所需操作的正确性,这需要更加小心,但会节省大量空间。如果你这样做,我还会存储一个变量来跟踪前导 0,或者可能是一个变量“总数字”语义。

抱歉,这不是您问题的答案,但希望它能帮助您重新设计您的课程。

I see a number of problems with your class that should be addressed with the redesign.

  • Your class represents a value and is mutable.
  • The constructor taking an int array parameter only copies the reference.
  • The value can be larger than long but is returned as an int from the ToInt() method.

The first thing I would do to shrink the size of this class is use the array of integers as a constant stream of bits which make up a binary representation of your number. This would take a lot more care in order to ensure correctness of any desired operations, but would save you a significant amount of space. If you do that, I would also store a variable to keep track of leading 0's, or perhaps a variable "total digits" semantics.

Sorry this isn't an answer to your question, but hopefully it will help you in redesigning this your class.

凡间太子 2024-08-27 03:44:41

.Net 中的 BigInteger 4.0 满足您的上限要求。

我认为您仍然需要这样的课程来处理您的前导零要求。

The BigInteger in .Net 4.0 addresses your upper limit requirement.

I think you'd still need a class like this to handle your leading zeros requirement.

雨巷深深 2024-08-27 03:44:41

直到 BigInteger 公开可用,我将使用一个公共字符串属性创建您自己的类/结构来获取/设置初始数字(这将保留您的前导零)。然后有一个 ReadOnly 属性来解析字符串并返回一个 IntX 实例。

Until BigInteger is openly available, I would create your own class/struct with one public string property to get/set the initial number (this will maintain your leading zeros). Then have a ReadOnly property that parses the string and returns an IntX instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文