自动编号问题 00,02,...99,A1,A2,...,A9,B1,B2,...,B9,...,1A,1B,..1Z,2A,..2Z,...AA,AB,...,AZ,BA,BB,...BZ,..ZA,...ZZ

JoyceSong 2008-03-06 06:34:04
还是求助高手来帮忙
目前需要自动产生流水码,编码规则如下:
00,02,...99,A1,A2,...,A9,B1,B2,...,B9,...,1A,1B,..1Z,2A,..2Z,...AA,AB,...,AZ,BA,BB,...BZ,..ZA,...ZZ
还是没有想出来该怎么写循环

例如判断目前已经产生号码"5C"
则通过循环计算出下一个是"5D"
...全文
4122 点赞 收藏 56
写回复
56 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
JoyceSong 2008-03-10
??没注意,我的贴子在论坛主页"头条推荐"显示了,看来还是有点技术含量,呵呵
还是采用了字典方法,其他计算没空研究了.
回复
b哈利路亚d 2008-03-10
要是我的话,就先建立字典库,然后依次+1取编号。。
实在犯不着用逻辑判断来取,效率不高,出错率高。
回复
vbman2003 2008-03-09
这样的编号规则真是奇怪,赞成of123的观点:如果能说服客户采用纯36进制最好.....
回复
Luoyulianshen 2008-03-08
Sub work()
Dim i As Integer, base As Integer
Dim s1 As String, s2 As String, Param As String * 2

Param = "9Z"

s1 = Left(Param, 1): s2 = Right(Param, 1)
Select Case Asc(s2)
Case 48 To 57
base = 49: i = 9
Case 65 To 90
base = 65: i = 26
Case Else
MsgBox "wrong Enter!"
Exit Sub
End Select
s1 = Chr$(Asc(s1) + Int((Asc(s2) - base + 1) / i))
If s1 > "Z" Then
If base = 49 Then
s1 = "1": base = 65: i = 26: s2 = "Z"
Else
MsgBox "Overflow!"
Exit Sub
End If
ElseIf s1 > "9" And s1 < "A" Then
s1 = "A"
End If
s2 = Chr$(base + (Asc(s2) - base + 1) Mod i)

Debug.Print s1 & s2
End Sub
回复
例如"S9"的结果是"SA" 正确的是"T1"

//



你这里的9到底是进位到A还是0呀????

如果是0,那前面的S又从何而来?

如果是A,那怎么又会是T1????

我看主要问题在于你那100大分~~~嘿嘿.

不过算了,不想再写代码了...

我发的是EXCEL的标志规律,楼主看着办...
回复
CommandButton 2008-03-07
有点像气车牌子升位,但又怕号码重复的那种工作。而且保留了一些特殊码禁用,
回复
CommandButton 2008-03-07
01,02,...99,A1,A2,...,A9,B1,B2,...,B9,...,Z1,Z2,...,Z9,
1A,1B,..1Z,2A,2B,..2Z,...,9A,..9Z,
AA,AB,...,AZ,BA,BB,...BZ,..ZA,...ZZ

实际上是这样的:
起初是两位十进制。
但到后来99不够用了,就在高位上采用36进制。低位保持10进制
到Z9后,有不够用了,高位重新循环36进制(但0不要了),底位36进制(但0-9不要)


CBM666说的好,题意理解起来很困难
回复
of123 2008-03-07
楼主在北京吗?能请你喝咖啡吗?
回复
of123 2008-03-07
Const strChars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Public Function Num2Str(ByVal x As Integer) As Integer
Dim tmp As String, n As Integer

n = x Mod (36 * 36)
If n < 360 Then
tmp = Mid(strChars, (n \ 10) + 1, 1)
tmp = tmp & Mid(strChars, (n Mod 10) + 1, 1)
Else
n = n - 360
tmp = Mid(strChars, (n \ 26) + 1, 1)
tmp = tmp & Mid(strChars, (n Mod 26) + 11, 1)
End If
Num2Str = tmp
End Function

Public Function Str2Num(ByVal s As String) As String
Dim tmp As String, n As Integer

tmp = Right("0" & s, 2)

If Right(tmp, 1) <= "9" Then
n = InStr(1, strChars, Left(tmp, 1)) - 1
n = n * 10
n = n + Val(Right(tmp, 1))
Else
n = InStr(1, strChars, Left(tmp, 1)) - 1
n = n * 26
n = n + InStr(1, strChars, Right(tmp, 1)) - 11
n = n + 360
End If
Str2Num = n
End Function

Public Function GetNextNum(ByVal s As String) As String
GetNextNum = Num2Str(Str2Num(s) + 1)
End Function


回复
mostice 2008-03-07
你可以测试下,我测试到 ZS 后出现的结果 是:[0。
回复
danjiewu 2008-03-07
[Quote=引用 37 楼 mostice 的回复:]
但是循环到 ZW 是就不正确了。出现[4。
[/Quote]

这个不太可能吧,应该到ZZ才会越界。
回复
of123 2008-03-07
大概明白楼主的意思了。实际上是个36进制码转换问题。
不过,客户的排序方式有点奇怪,实际上将数字标记法分成了两段:
1 低位数采用 10 进制,高位数采用 36 进制:01,02,...99,A1,A2,...,A9,B1,B2,...,B9,...,Z1,Z2,...,Z9
2 低位数采用 26 进制,高位数采用 36 进制:1A,1B,..1Z,2A,2B,..2Z,...,9A,..9Z, AA,AB,...,AZ,BA,BB,...BZ,..ZA,...ZZ

如果能说服客户采用纯 36 进制最好,可以简化运算。

否则:
Option Explicit
Const strChars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Private Sub Form_Load()
Dim i As Integer, n As Integer

For i = 0 To 36 * 36 - 1
List1.AddItem Num2Str(i)
Next i
End Sub

Public Function Num2Str(ByVal x As Integer)
Dim tmp As String, n As Integer

n = x Mod (36 * 36)
If n < 360 Then
tmp = Mid(strChars, (n \ 10) + 1, 1)
tmp = tmp & Mid(strChars, (n Mod 10) + 1, 1)
Else
n = n - 360
tmp = Mid(strChars, (n \ 26) + 1, 1)
tmp = tmp & Mid(strChars, (n Mod 26) + 11, 1)
End If
Num2Str = tmp
End Function

Public Function Str2Num(ByVal s As String)
Dim tmp As String, n As Integer

tmp = Right("0" & s, 2)

If Right(tmp, 1) <= "9" Then
n = InStr(1, strChars, Left(tmp, 1)) - 1
n = n * 10
n = n + Val(Right(tmp, 1))
Else
n = InStr(1, strChars, Left(tmp, 1)) - 1
n = n * 26
n = n + InStr(1, strChars, Right(tmp, 1)) - 11
n = n + 360
End If
Str2Num = n
End Function

Private Sub List1_Click()
MsgBox List1.ListIndex & ": " & Str2Num(List1.List(List1.ListIndex))
End Sub



回复
mostice 2008-03-07
但是循环到 ZW 是就不正确了。出现[4。
回复
mostice 2008-03-07
danjiewu 的方法很好。用了一下。呵呵
回复
cbm6666 2008-03-07
'呵呵...早说清楚嘛

'添加 Command1 List1

Dim i%, j%, aa$, bb$, s, ss
Private Sub Command1_Click()
s = Array("0 99", "65 90 1 9", "1 9 65 90", "65 90 65 90")
List1.Clear
For suzu = 0 To 3
ss = Split(s(suzu), " ")
If suzu = 0 Then
For i = Val(ss(0)) To Val(ss(1))
List1.AddItem Format(CStr(i), "00")
Next i
Else
For i = Val(ss(0)) To Val(ss(1))
aa = IIf(i <= 9, CStr(i), Chr(i))
For j = Val(ss(2)) To Val(ss(3))
bb = IIf(j <= 9, CStr(j), Chr(j))
List1.AddItem aa & bb
Next j
Next i
End If
Next suzu
End Sub

回复
danjiewu 2008-03-07
错了,26应该是36


string GetNextCode(string preCode)
{
int num = (preCode[0]-'0') * 36 + preCode[1] - '0' + 1;
return ""+ (char)(num / 36 +'0') + (char)(num % 36 + '0');
}
回复
danjiewu 2008-03-07

string GetNextCode(string preCode)
{
int num = (preCode[0]-'0') * 26 + preCode[1] - '0' + 1;
return ""+ (char)(num / 26 +'0') + (char)(num % 26 + '0');
}
回复
qiu5208 2008-03-07
早说清楚,
很简单的问题.
回复
JoyceSong 2008-03-07
29楼理解也对哦,可是客户给的资料01--99之间没有说要不要0结尾的
去问需求部门他们也不确定...
回复
理解楼主的想法,可以做为研究,做产品时就没必要了,俺这几年的干活用的最多的就是数组,for, do几种,端的就是瓷实,理论上就不会出错。
回复
加载更多回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-06 06:34
社区公告
暂无公告