【问题标题】:Increment Of Variable in another way in c# win. form在 c# win 中以另一种方式增加变量。形式
【发布时间】:2014-11-20 05:23:53
【问题描述】:

我正在发明一种算法,我在其中创建了一个新数字“z”(实际上不是z),并且我将它与一起使用旧数字(0123456789)和我的新数字系列看起来像这样(0123456789z)但这里的问题是如何编写一个程序,将“19 + 1”作为“z”和“z + 1”作为“20”。

【问题讨论】:

    标签: c# winforms increment


    【解决方案1】:

    尝试类似的方法,但如果最后一个字符达到 0,您必须实施额外的编码来检查参数并增加倒数第二个字符。

    这是基本逻辑:

    private const string DIGITS = "0123456789z";
    
    static void Main(string[] args)
    {
        Console.WriteLine(NextValue("9"));
        Console.ReadKey();
    }
    
    private static string NextValue(string value)
    {
        char nextChar = '\0';
        if(!string.IsNullOrEmpty(value))
        {
            char lastChar = value[value.Length - 1];
            int nextCharIndex = DIGITS.IndexOf(lastChar) + 1;
            if (nextCharIndex > DIGITS.Length)
                nextChar = DIGITS[0];
            else
                nextChar = DIGITS[nextCharIndex];
        }
        return nextChar.ToString();
    }
    

    【讨论】:

    • 我的问题已经尽可能简单了,请看看您是否可以帮助我。
    • 我发布的这个解决方案将对您有所帮助.. 只需添加额外的逻辑来检查它是否已经是 DIGITS 中的最后一个,然后添加 z
    【解决方案2】:

    在你编辑了你的问题之后,我想你想要的是这个

    public int incrementNumber(string number)
    {
        var lastNumber = int.Parse(number.Last());
        return (number.Length - 1) + ((lastNumber + 1) % 10).ToString();
    }
    

    % 10 或 Mod 10 提醒数字除以 10。因此,如果您传入 9,它将递增到 10,然后回绕到 0。当您传入 5 时,它将递增到 6 并保持为 6,因为它不能被 10 整除。

    【讨论】:

    • 先生,据说我正在研究“++”的概念,但我不能使用它,也不能使用“+1”。
    • 您好,在您更新了您的问题后,我已经更新了我的答案。希望对您有所帮助。
    • 我的问题已经尽可能简单了,请看看您是否可以帮助我。
    • 我的新答案有帮助吗?如果没有,为什么不呢?
    • 不,先生,请再次查看上述问题,他们现在是 11 个数字
    【解决方案3】:

    这可能没有经过优化,但如果您允许 +1 递增,则可以使用。

     public static string NextValue(string Counting)
        {
            int nextVal;
            if(int.TryParse(Counting, out nextVal))
            {
                return (nextVal + 1).ToString();
            }
            else 
            {
                char[] numbers = Counting.ToCharArray();
                StringBuilder incremented = new StringBuilder();
                foreach (char digit in numbers.Reverse())
                {
                    if (digit == 'z')
                    {
                       incremented.Append("0");
                    }
                    else if (int.TryParse(digit.ToString(), out nextVal))
                    {
                        nextVal = nextVal + 1;
                        if (nextVal == 10)
                        {
                            incremented.Append("z");
                        }
                        else
                        {
                            incremented.Append(nextVal.ToString());
                        }
    
                        incremented.Append(string.Concat(numbers.Reverse().Skip(incremented.Length)));
                        break;
                    }
                    else
                    {
                        //Invalid character in number except for z
                        return string.Empty;
                    }
                }
                if (incremented[incremented.Length - 1] == '0')
                    incremented.Append("1");
                return Reverse(incremented.ToString()); 
            }    
        }
    
         public static string Reverse(string s)
         {
             char[] charArray = s.ToCharArray();
             Array.Reverse(charArray);
             return new string(charArray);
         }
    

    【讨论】:

    • 不,先生,我正在研究“++”的概念,但我不能使用它,也不能使用“+1”。
    • @user3779606 所以 0123456789z 是你的最终数字系统吗?或者它可以是任何组合?
    • 那么我们现在可以使用 +1 或 ++ 吗?因为它已从问题中删除?
    • 我又更新了一次。但是算法使用+1。否则,它假定您的数字系统中有 11 位数字。检查是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多