Arabic to Roman Converter

时间:2015-10-21 21:04:28

标签: vb.net

我正在尝试用+ - 函数创建一个阿拉伯小数到罗马转换器。但是无法获得理想的结果。例如596 + 535 = 1131所以在罗马文本框中它应该显示为DLXXXXVI + DXXXV = MCXXXI但不能得到它。尝试过使用while循环和math.floor,但不知道我错在哪里。请尽可能帮助我:)

Private Sub Btnplus_Click(sender As Object, e As EventArgs) Handles Btnplus.Click
    txtroman.Text = ""
    Txtromanappear.Text = ""
    txtarabic.Text = ""

    'Read number1
    num1 = Txtnum1.Text
    'Read number2
    num2 = Txtnum2.Text
    'sum1=num1+num2
    sum1 = num1 + num2
    'output=sum1
    txtarabic.Text = sum1
    If (num1 <= 10) Then
        Select Case num1
            Case 1
                Txtromanappear.Text = "I"
            Case 2
                Txtromanappear.Text = "II"
            Case 3
                Txtromanappear.Text = "III"
            Case 4
                Txtromanappear.Text = "IIII"
            Case 5
                Txtromanappear.Text = "V"
            Case 6
                Txtromanappear.Text = "VI"
            Case 7
                Txtromanappear.Text = "VII"
            Case 8
                Txtromanappear.Text = "VIII"
            Case 9
                Txtromanappear.Text = "VIIII"
            Case 10
                Txtromanappear.Text = "X"
                romanappear = Txtromanappear.Text

        End Select

    ElseIf (num1 > 5) Then
        sum2 = Math.Floor(num1 / 5)
        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "V"

            sum2 = sum2 - 1
        End While

        sum2 = num1 Mod 5
    End If
    If (num1 > 10) Then
        sum2 = Math.Floor(num1 / 10)
        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "X"

            sum2 = sum2 - 1
        End While
        sum2 = num1 Mod 5

    End If




    If (num1 > 50) Then
        sum2 = Math.Floor(num1 / 50)
        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "L"
            sum2 = sum2 - 1
        End While
        sum2 = num1 Mod 50
    End If

    If (num1 > 100) Then
        sum2 = Math.Floor(num1 / 100)
        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "C"
            sum2 = sum2 - 1
        End While
        sum2 = num1 Mod 100
    End If
    If (num1 > 500) Then
        sum2 = Math.Floor(num1 / 500)
        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "D"
            sum2 = sum2 - 1
        End While
        sum2 = num1 Mod 500
    End If
    If (num1 > 1000) Then
        sum2 = Math.Floor(num1 / 1000)
        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "M"
            sum2 = sum2 - 1
        End While
        sum2 = num1 Mod 1000
    End If


    Select Case sum2
            Case 1
                Txtromanappear.Text = Txtromanappear.Text + "I"
            Case 2
                Txtromanappear.Text = Txtromanappear.Text + "II"
            Case 3
                Txtromanappear.Text = Txtromanappear.Text + "III"
            Case 4
                Txtromanappear.Text = Txtromanappear.Text + "IIII"
            Case 5
                Txtromanappear.Text = Txtromanappear.Text + "V"
            Case 6
                Txtromanappear.Text = Txtromanappear.Text + "VI"
            Case 7
                Txtromanappear.Text = Txtromanappear.Text + "VII"
            Case 8
                Txtromanappear.Text = Txtromanappear.Text + "VIII"
            Case 9
                Txtromanappear.Text = Txtromanappear.Text + "VIIII"
            Case 10
                Txtromanappear.Text = Txtromanappear.Text + "X"
        End Select





        If (num2 <= 10) Then
        Select Case num2
            Case 1
                Txtromanappear.Text = Txtromanappear.Text + "+ " + "I"
            Case 2
                Txtromanappear.Text = Txtromanappear.Text + "+ " + "II"
            Case 3
                Txtromanappear.Text = Txtromanappear.Text + "+ " + "III"
            Case 4
                Txtromanappear.Text = Txtromanappear.Text + " +" + "IIII"
            Case 5
                Txtromanappear.Text = Txtromanappear.Text + " +" + "V"
            Case 6
                Txtromanappear.Text = Txtromanappear.Text + " +" + "VI"
            Case 7
                Txtromanappear.Text = Txtromanappear.Text + " +" + "VII"
            Case 8
                Txtromanappear.Text = Txtromanappear.Text + "+ " + "VIII"
            Case 9
                Txtromanappear.Text = Txtromanappear.Text + " +" + "VIIII"
            Case 10
                Txtromanappear.Text = Txtromanappear.Text + " +" + "X"
                romanappear = Txtromanappear.Text

        End Select
    ElseIf (num2 > 10) Then
        sum2 = Math.Floor(num2 / 10)
        Txtromanappear.Text = Txtromanappear.Text + "+"

        While (sum2 > 0)
            Txtromanappear.Text = Txtromanappear.Text + "X"

            sum2 = sum2 - 1
        End While
        sum2 = num2 Mod 10
        Select Case sum2
            Case 1
                Txtromanappear.Text = Txtromanappear.Text + "I"
            Case 2
                Txtromanappear.Text = Txtromanappear.Text + "II"
            Case 3
                Txtromanappear.Text = Txtromanappear.Text + "III"
            Case 4
                Txtromanappear.Text = Txtromanappear.Text + "IIII"
            Case 5
                Txtromanappear.Text = Txtromanappear.Text + "V"
            Case 6
                Txtromanappear.Text = Txtromanappear.Text + "VI"
            Case 7
                Txtromanappear.Text = Txtromanappear.Text + "VII"
            Case 8
                Txtromanappear.Text = Txtromanappear.Text + "VIII"
            Case 9
                Txtromanappear.Text = Txtromanappear.Text + "VIIII"
            Case 10
                Txtromanappear.Text = Txtromanappear.Text + "X"
                romanappear = Txtromanappear.Text
        End Select

    End If

    If sum1 <= 10 Then
        Select Case sum1
            Case 1
                txtroman.Text = "I"
            Case 2
                txtroman.Text = "II"
            Case 3
                txtroman.Text = "III"
            Case 4
                txtroman.Text = "IIII"
            Case 5
                txtroman.Text = "V"
            Case 6
                txtroman.Text = "VI"
            Case 7
                txtroman.Text = "VII"
            Case 8
                txtroman.Text = "VIII"
            Case 9
                txtroman.Text = "VIIII"
            Case 10
                txtroman.Text = "X"


        End Select

    ElseIf sum1 > 10 Then
        sum2 = Math.Floor(sum1 / 10)
        While (sum2 > 0)
            txtroman.Text = txtroman.Text + "X"

            sum2 = sum2 - 1
        End While
        sum2 = sum1 Mod 10
        Select Case sum2
            Case 1
                txtroman.Text = txtroman.Text + "I"
            Case 2
                txtroman.Text = txtroman.Text + "II"
            Case 3
                txtroman.Text = txtroman.Text + "III"
            Case 4
                txtroman.Text = txtroman.Text + "IIII"
            Case 5
                txtroman.Text = txtroman.Text + "V"
            Case 6
                txtroman.Text = txtroman.Text + "VI"
            Case 7
                txtroman.Text = txtroman.Text + "VII"
            Case 8
                txtroman.Text = txtroman.Text + "VIII"
            Case 9
                txtroman.Text = txtroman.Text + "VIIII"
            Case 10
                txtroman.Text = txtroman.Text + "X"
        End Select

    End If

1 个答案:

答案 0 :(得分:0)

好的,我已经重新订购了您的代码,以便您首先添加最大的罗马数字并更改行

sum2=num1 mod 1000

num1=num1 mod 100

以便num1正确传递if语句

我只重写了处理num1的代码,以便你可以......或者 - 将相同的代码应用于处理num2的代码,但我不知道你是否想要这样做,因为你可能被要求不同地编码num2的处理 - 在这种情况下,你可以将相同的原则应用于select case语句 - 首先测试最大的罗马数字

另外 - IF语句应该测试num1是&gt; =到1000等因为如果num1正好等于1000,那个块就不会执行并弄乱后续的IF..End IF块

     If (num1 >= 1000) Then
        sum2 = CInt(Math.Floor(num1 / 1000))
        While (sum2 > 0)
            txtRomanAppear.Text = txtRomanAppear.Text + "M"
            sum2 = sum2 - 1
        End While
        num1 = num1 Mod 1000
    End If

    If (num1 >= 500) Then
        sum2 = CInt(Math.Floor(num1 / 500))
        While (sum2 > 0)
            txtRomanAppear.Text = txtRomanAppear.Text + "D"
            sum2 = sum2 - 1
        End While
        num1 = num1 Mod 500
    End If

    If (num1 >= 100) Then
        sum2 = CInt(Math.Floor(num1 / 100))
        While (sum2 > 0)
            txtRomanAppear.Text = txtRomanAppear.Text + "C"
            sum2 = sum2 - 1
        End While
        num1 = num1 Mod 100
    End If

    If (num1 >= 50) Then
        sum2 = CInt(Math.Floor(num1 / 50))
        While (sum2 > 0)
            txtRomanAppear.Text = txtRomanAppear.Text + "L"
            sum2 = sum2 - 1
        End While
        num1 = num1 Mod 50
    End If

    If (num1 >= 10) Then
        sum2 = CInt(Math.Floor(num1 / 10))
        While (sum2 > 0)
            txtRomanAppear.Text = txtRomanAppear.Text + "X"
            sum2 = sum2 - 1
        End While
        num1 = num1 Mod 10
    End If

    If (num1 >= 5) Then
        sum2 = CInt(Math.Floor(num1 / 5))
        While (sum2 > 0)
            txtRomanAppear.Text = txtRomanAppear.Text + "V"
            sum2 = sum2 - 1
        End While
        num1 = num1 Mod 5
    End If

    'for the remaining digits <5, just create a string of i's eg if num1=4 then add a string of 4 i's
    If (num1 > 0) Then
        For i = 1 To num1
            txtRomanAppear.Text=txtRomanAppear.Text+"I"
        Next
    End If


    'This IF block can be removed as it's no longer needed
    'If (num1 < 10) Then
    '    Select Case num1
    '        Case 1
    '            txtRomanAppear.Text = "I"
    '        Case 2
    '            txtRomanAppear.Text = "II"
    '        Case 3
    '            txtRomanAppear.Text = "III"
    '        Case 4
    '            txtRomanAppear.Text = "IIII"
    '        Case 5
    '            txtRomanAppear.Text = "V"
    '        Case 6
    '            txtRomanAppear.Text = "VI"
    '        Case 7
    '            txtRomanAppear.Text = "VII"
    '        Case 8
    '            txtRomanAppear.Text = "VIII"
    '        Case 9
    '            txtRomanAppear.Text = "VIIII"
    '        Case 10
    '            txtRomanAppear.Text = "X"
    '            romanAppear = txtRomanAppear.Text

    '    End Select

为了使所有代码更紧凑,你可以编写几个函数,因为有大量的代码重复。希望这可以帮助。如果您有任何问题,请告诉我。如果这对您有用,请不要忘记将我的答案标记为已接受: - )

在这里睡觉时间。