C# !!求大神帮忙写一下以下的编码规则,急急急
下一个数就是B00000~B99999,…………以此类推直到ZZZZZZ 展开
不是大神,但是代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
NumberGenerator generator = new NumberGenerator();
string result = generator.Next("AB9999");
Console.WriteLine("result is {0}", result);
Console.Read();
}
}
class NumberGenerator
{
public string Next(string current)
{
int numIndex = 0;
//转为大写
current = current.ToUpper();
//判断位数是否合法
if (string.IsNullOrEmpty(current) || current.Length != 6)
{
throw new ArgumentException("错误的序列号");
}
char[] characters = current.ToCharArray();
char[] temp = new char[characters.Length];
for (int i = 0; i < characters.Length; i++)
{
if(characters[i] >= 65 && characters[i] <= 90)
{
//如果是以字母开头,继续向后找
numIndex++;
continue;
}
else if (characters[i] >= 48 && characters[i] <= 57)
{
//如果是数字,跳出循环
break;
}
}
if (numIndex >= characters.Length)
{
//全是字母
return ProcessCharacterPart(current, true);
}
else if (numIndex == 0)
{
//全是数字
return ProcessNumPart(current);
}
else
{
//字母与数字都有
string numPart = current.Substring(numIndex);
string characterPart = current.Substring(0, numIndex);
return ProcessCharacterPart(characterPart, false) + ProcessNumPart(numPart);
}
}
/// <summary>
/// 处理数字部分的加一
/// </summary>
/// <param name="value">拆分后的数字部分被加数</param>
/// <returns>数字部分加一的结果</returns>
public string ProcessNumPart(string value)
{
//将参数拆分为char数组
char[] numArray = value.ToCharArray();
//定义返回结果char数组
char[] resultArray = new char[numArray.Length];
//用于判断是不是到达了数字的最大值例如9、99、999...999999
bool isMax = true;
for (int i = 0; i < numArray.Length; i++)
{
if (numArray[i] != 57)
{
//非最大数字,跳出循环
isMax = false;
break;
}
}
if (isMax)
{
//如果是最大数字,高位变A,低位变0
resultArray[0] = 'A';
for (int j = 1; j < resultArray.Length; j++)
{
resultArray[j] = '0';
}
//返回结果
return new string(resultArray);
}
else
{
//如果不是最大数字,转为数字加法
int temp = Convert.ToInt32(value);
temp++;
//返回结果
return temp.ToString().PadLeft(value.Length,'0');
}
}
/// <summary>
/// 处理字母部分的加一
/// </summary>
/// <param name="value">拆分后的字母部分被加数</param>
/// <param name="isAddOne">
/// 是否需要加一
/// 当原始被加数全为字母时,需要对最低位加一
/// 当拆分后的字母,小于原被加数长度,即最低位有数字出现,不需要对字母位+1
/// 例如原被加数为AB9999,拆分后是AB,不需要对最低位B+1
/// 例如原被加数是ABCDEF,拆分后也是ABCDEF,需要对最低位F+1
/// </param>
/// <returns></returns>
public string ProcessCharacterPart(string value, bool isAddOne)
{
//将参数拆分为char数组
char[] characterArray = value.ToCharArray();
//定义返回结果char数组
char[] resultArray = new char[characterArray.Length];
//判断是否需要+1
int addOne = isAddOne ? 1 : 0;
for (int i = characterArray.Length - 1; i >= 0; i--)
{
//从后向前进位
if (characterArray[i] == 90 && addOne == 1)
{
//如果最后为Z且需要加1,那么最低位变0,进1
resultArray[i] = '0';
addOne = 1;
}
else if (characterArray[i] == 90 && addOne == 0)
{
//如果最后为Z且不需要加1,那么保持Z,不进1
resultArray[i] = characterArray[i];
addOne = 0;
}
else
{
//进位处理
resultArray[i] = (char)(characterArray[i] + addOne);
addOne = 0;
}
}
//得到结果
string result = new string(resultArray);
//如果已达到最大ZZZZZZ,再+1提示错误
if (result == "000000")
{
throw new ArgumentException("已到达最大序列号");
}
//返回结果
return result;
}
}
}
2024-08-22 广告
public class LSH
{
private char pro = '0';
private string f = "99999";
private string result = "00000";
public string Inc()
{
if (result.Equals(f))
{
if (pro.Equals('Z'))
throw new Exception("max");
else if (pro.Equals('9'))
pro = 'A';
else
pro = (pro + 1).ToString().ToCharArray()[0];
return pro.ToString() + "00000";
}
else
return pro.ToString() + string.Join(string.Empty, string.Join(string.Empty, ("000000" + (Convert.ToInt32(result) + 1)).Reverse()).Substring(0, 6).Reverse());
}
}