【问题标题】:Change an int type into char type in C#在 C# 中将 int 类型更改为 char 类型
【发布时间】:2021-01-08 12:17:17
【问题描述】:

我正在尝试逐位分解非常大的数字并将它们逐个添加(对于大数计算器)。

条件是我不应该使用任何 Math 或 BigNumbers 库,并且输入必须采用字符串。

我这样做的方式是获取字符串,将每个字符串放入(char) 的列表中,然后转换每个(char) to (int) 并执行操作,然后将结果数字改回(char),然后添加将其添加到我命名为 Result 的列表中。

我想知道如何将 (int) 更改为 (char)? "Convert.ToChar()" 不起作用,因为它将数字转换为其 Unicode 字符。 这是代码,我标记了问题:

bool carry = false;
        int i = bigNumberLength - 1;
        List<char> result = new List<char>();
        char currentDigit;
        for (int j = smallNumberLength - 1; j >= 0; j--)
        {
            int tempDigit1 = (int)Char.GetNumericValue(bigNumber[i]);
            int tempDigit2 = (int)Char.GetNumericValue(smallNumber[j]);
            if (tempDigit1 + tempDigit2 < 10)
            {
                if (!carry)
                {
                    currentDigit = Convert.ToChar(tempDigit1 + tempDigit2);//this one
                    carry = false;
                }
                else
                {
                    if (tempDigit1 + tempDigit2 + 1 < 10)
                        currentDigit = Convert.ToChar(tempDigit1 + tempDigit2 + 1);//this one
                    else
                        currentDigit = Convert.ToChar(0); //this one
                }
                result.Add(currentDigit);
            }
            else
            {
                currentDigit = Convert.ToChar((tempDigit1 + tempDigit2) - 10); //this one
                carry = true;
                result.Add(currentDigit);
            }
            i--;
        }

【问题讨论】:

  • 请提供您当前的代码以及确切您遇到问题的地方。
  • 我假设您希望使用 int.Parse?
  • @DavidG 没有图书馆我倾向于理解这应该在没有int.Parse 或类似的情况下解决。
  • 当您开始谈论转换回 char 时,您失去了我。您要解决的问题是,给定数字的字符串表示形式,将所有数字相加。为什么要将任何东西转换回char,您必须提供的答案是总和。
  • @InBetween 通过库,我认为它们不允许使用 BigInteger 之类的东西,而不是 BCL 中的东西

标签: c# char integer calculator data-conversion


【解决方案1】:

举个例子,我已经完成了加法并只使用了字符串,但您可以将其替换为列表的逻辑,并以相同的方式进行其他数学运算,并进行一些更改:

    private const char O = '0';

    public static void Main()
    {
        var num1 = "55555555555555555555555555555555555555578955555555555555";
        var num2 = "55555555555555555555555555";
        var result = string.Empty;
        var num1Index = num1.Length - 1;
        var num2Index = num2.Length - 1;
        var temp = 0;

        while (true)
        {
            if (num1Index >= 0 && num2Index >= 0)
            {
                var sum = ((temp + num1[num1Index--] - O) + (num2[num2Index--] - O));
                result = sum % 10 + result;
                temp = sum / 10;
            }
            else if (num1Index < 0 && num2Index >= 0)
            {
                result = temp + (num2[num2Index--] - O) + result;
                temp = 0;
            }
            else if (num1Index >= 0 && num2Index < 0)
            {
                result = temp + (num1[num1Index--] - O) + result;
                temp = 0;
            }
            else
            {
                break;
            }
        }

        Console.WriteLine(result);
    }

第一个 if 语句进行实际的数学运算,其余的只是附加其余的数字大小写,它们具有不同的长度。

以及脚本产生的输出:

和 int 再次转换为 char 你可以通过添加例如转换

(char)([some digit hier] + '0')

【讨论】:

  • 哇!这工作得很好,比我想象的要好得多,也更干净。我需要更多地阅读您的算法以了解您到底做了什么。太感谢了!我不能将其标记为问题的答案,因为它可能会使将来想知道如何直接 (int) 到 (char) 的其他人感到困惑,但它肯定是完成手头任务的更好方法。
  • 哦,我刚刚注意到您的编辑,您也回答了问题本身。立即标记!
猜你喜欢
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多