本文介绍了如何增加.在C#中按顺序3个字母的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我知道这对这里的大多数人来说似乎很愚蠢.但是我陷入了一个问题
我想编写一个程序,该程序可以创建由3个字母和6个数字组成的用户ID,例如AAA000001应该是第一个数字. .进度应该是这样的AAA000001,AAA000002 ......... AAA999999,AAB000001 ........ AAB999999 ....... AAZ999999,ABA000001 ........依此类推请提供合适解决方案的任何人.帮助我并分享我们所有人.

Hi folks
I know it may seem quite stupid to most of the people here. But am stuck with a prblem
I want to write a programme which can create user id which consists of 3 letters and 6 digits e.G AAA000001 It should be the first no. . Progression should be like this AAA000001,AAA000002......... AAA999999,AAB000001........AAB999999 ....... AAZ999999 ,ABA000001........ AND so forth.Please anyone with suitable solution.Help me and share wid all of us.

推荐答案

void Main()
{
    List<long> numbers = new List<long>{
        999999, 1000000, 1999999,
        1000000*26+1,
        1000000*26*26-1};
    foreach(long number in numbers)
        Console.WriteLine (GetId(number));
}

public string GetId(long number){
    long numberPart = number % 1000000;
    int charPos = (int)number / 1000000;

    return string.Format("{2}{1}{0}{3:000000}",
        GetChar(ref charPos),GetChar(ref charPos),
        GetChar(ref charPos),numberPart);
}
public char GetChar(ref int position){
    int ind = position % 26;
    position = position / 26;
    return (char)(ind+65);
}
//Output
//AAA999999
//AAB000000
//AAB999999
//ABA000001
//AZZ999999



我还没有详尽检查.因此,请检查上述代码是否符合您的要求,并根据需要进行修改.



I have not checked exhaustively. So, check the above code for your requirement and make modifications as found necessary.


private void Form1_Load(object sender, EventArgs e)
{
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   m_nIntegerPart = 999998;
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   m_nIntegerPart = 999998;
   m_strStringPart = "AAZ";
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   m_nIntegerPart = 999998;
   m_strStringPart = "AZZ";
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
   MessageBox.Show(GetNewUserNumber());
}

private int m_nIntegerPart = 0;
private string m_strStringPart = "AAA";

private string GetNewUserNumber()
{
   if (m_nIntegerPart < 999999)
      m_nIntegerPart++;
   else
   {
      if (m_strStringPart == "ZZZ")
         return ""; //all numbers were already used once
      m_nIntegerPart = 1;

      m_strStringPart = Increment(m_strStringPart);
   }

   return m_strStringPart + m_nIntegerPart.ToString("000000");
}

private static String Increment(String s)
{
   String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   char lastChar = s[s.Length - 1];
   string fragment = s.Substring(0, s.Length - 1);

   //Last character increment
   if (chars.IndexOf(lastChar) < 25)
   {
      lastChar = chars[chars.IndexOf(lastChar) + 1];
      return fragment + lastChar;
   }
   else
   {
      lastChar = 'A';
      char middleChar = s[s.Length - 2];
      fragment = s.Substring(0, s.Length - 2);
      if (chars.IndexOf(middleChar) < 25)
      {
         middleChar = chars[chars.IndexOf(middleChar) + 1];
         return fragment + middleChar + lastChar;
      }
      else
      {
         lastChar = 'A';
         middleChar = 'A';
         char firstChar = s[s.Length - 3];
         if (chars.IndexOf(firstChar) < 25)
         {
            firstChar = chars[chars.IndexOf(firstChar) + 1];
            return "" + firstChar + middleChar + lastChar;
         }
      }
   }

   return Increment(fragment) + '0';
}


这篇关于如何增加.在C#中按顺序3个字母的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:58