注册 登录
编程论坛 VB.NET论坛

求助纠错小程序,求各位大侠们的帮助

winorange 发布于 2008-05-05 20:35, 975 次点击
本来刚学没多久,想做个从数字转化为中文大写的小程序,可是当我输入“1”时,结果为 “壹拾壹” “2”时,结果为“贰拾贰”“123”时结果为 “壹仟壹佰贰拾”叁 。
Dim f, l, i As Integer
        Dim y As String
        Dim b As String
        Dim t As String
        f = Val(TextBox3.Text) ’先将输入框转成数字,以便将字符去除
        b = Str(f)  ’再将数字转为文
        l = Len(b)  ’求输入文本的长度
        For i = 1 To l ’对输入文本的最后一位开始,一位一位的转为中文大写的中文字符
            Select Case Mid$(b, l - i + 1, 1)
                Case 0
                    y = "零"
                Case 1
                    y = "壹"
                Case 2
                    y = "贰"
                Case 3
                    y = "叁"
                Case 4
                    y = "肆"
                Case 5
                    y = "伍"
                Case 6
                    y = "陆"
                Case 7
                    y = "柒"
                Case 8
                    y = "捌"
                Case 9
                    y = "玖"
                Case 10
                    y = "拾"
            End Select
            t = t & y ’将转换后的中文字符以原字符的最后一位为开始,一个一个相接,若输入时为“123”,则t为“叁贰壹”

        Next i

        Select Case Len(t) ‘对不同的输入长度作不同的输出判断
            Case 1
                TextBox4.Text = t
            Case 2
                TextBox4.Text = Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 3
                TextBox4.Text = Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 4
                TextBox4.Text = Mid$(t, 4, 1) & "仟" & Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 5
                TextBox4.Text = Mid$(t, 5, 1) & "万" & Mid$(t, 4, 1) & "仟" & Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 6
                TextBox4.Text = Mid$(t, 6, 1) & "拾" & Mid$(t, 5, 1) & "万" & Mid$(t, 4, 1) & "仟" & Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 7
                TextBox4.Text = Mid$(t, 7, 1) & "佰" & Mid$(t, 6, 1) & "拾" & Mid$(t, 5, 1) & "万" & Mid$(t, 4, 1) & "仟" & Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 8
                TextBox4.Text = Mid$(t, 8, 1) & "仟" & Mid$(t, 7, 1) & "佰" & Mid$(t, 6, 1) & "拾" & Mid$(t, 5, 1) & "万" & Mid$(t, 4, 1) & "仟" & Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
            Case 9
                TextBox4.Text = Mid$(t, 9, 1) & "亿" & Mid$(t, 8, 1) & "仟" & Mid$(t, 7, 1) & "佰" & Mid$(t, 6, 1) & "拾" & Mid$(t, 5, 1) & "万" & Mid$(t, 4, 1) & "仟" & Mid$(t, 3, 1) & "佰" & Mid$(t, 2, 1) & "拾" & Mid$(t, 1, 1)
        End Select
    End Sub
6 回复
#2
winorange2008-05-05 20:45
根据结果,我将后面的一个select的 每一个case结果的第一个mid及其参数去除后,又能显示正确的结果了,但我还是不知道是怎么回事。
#3
winorange2008-05-05 20:48
是不是起始位置的后面一位开始取,而不是从起始位置开始取。那样的话,就说的过去了。
#4
winorange2008-05-05 21:02
最后一位为时,如何让最后一位不显示出零,不然不合常规,比如“10”的时候,他会显示“壹拾零”
#5
fairy42008-05-06 13:27
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim strTemp01, intStr, NumStr, TrunStr As String
        Dim strLen As Integer
        Dim ZeroFlag As Boolean = False
        dim ZeroTooFlag as Boolean=False
        strTemp01 = Me.TextBox3.Text.Trim()
        strLen = strTemp01.Length
        For intX As Integer = 1 To strLen
            Select Case Mid(strTemp01, intX, 1)
                Case 0
                    intStr = "零"
                Case 1
                    intStr = "壹"
                Case 2
                    intStr = "贰"
                Case 3
                    intStr = "叁"
                Case 4
                    intStr = "肆"
                Case 5
                    intStr = "伍"
                Case 6
                    intStr = "陆"
                Case 7
                    intStr = "柒"
                Case 8
                    intStr = "捌"
                Case 9
                    intStr = "玖"
                    'Case 10
                    '    y = "拾"
            End Select
            NumStr = NumStr & intStr
        Next
        For intI As Integer = 1 To NumStr.Length
            If Mid(NumStr, intI, 1) <> "零" Then
                ZeroFlag = False
                TrunStr = TrunStr & Mid(NumStr, intI, 1) & FindDanwei(NumStr.Length - intI)
            Else
                If ZeroFlag = False Then

                    If NumStr.Length - intI = 4 Or NumStr.Length - intI = 8 Then
                        If intI <> NumStr.Length Then
                            TrunStr = TrunStr & FindDanwei(NumStr.Length - intI)
                        End If
                    Else
                        If intI <> NumStr.Length Then
                            TrunStr = TrunStr & Mid(NumStr, intI, 1)
                        End If
                    End If
                Else
                    If NumStr.Length - intI = 4 Or NumStr.Length - intI = 8 Then
                        If intI <> NumStr.Length Then
                            TrunStr = TrunStr & FindDanwei(NumStr.Length - intI)
                        End If
                    Else
                        If intI <> NumStr.Length Then
                            TrunStr = TrunStr
                        End If
                    End If
                End If
                ZeroFlag = True

            End If
        Next
        If TrunStr.Substring(TrunStr.Length - 1, 1) = "零" Then
            TrunStr = TrunStr.Substring(0, TrunStr.Length - 1)
        End If
        Me.TextBox4.Text = TrunStr
    End Sub
    Private Function FindDanwei(ByVal intX As Int32) As String
        Select Case intX
            Case 1
                Return "拾"
            Case 2
                Return "佰"
            Case 3
                Return "仟"
            Case 4
                Return "万"
            Case 5
                Return "拾"
            Case 6
                Return "佰"
            Case 7
                Return "仟"
            Case 8
                Return "亿"
            Case 9
                Return "拾"
            Case 10
                Return "佰"
            Case 11
                Return "仟"

        End Select
    End Function
你看看這個吧
#6
fairy42008-05-06 13:29
當然,這個程序也有一些小問題,比如1000001這樣的數字,處理出來的結果就不是我想要的看到的,
我也沒時間去改了,呵呵
#7
winorange2008-05-06 14:44
谢谢兄弟。
1