dポイントプレゼントキャンペーン実施中!

こんにちは。
現在大学2年生のたなひと申します。

授業でプログラム入門を取ってしまったのですが、コード作成方法が全く分からず困っています_:(´ཀ`」 ∠):
宿題で「数字を文字に変換するプログラムを作成する」(例:123→one hundred twenty three)という問題が出されたのですが、全くわかりません。
もし分かる方がいらっしゃれば教えていただきたいです!!!!!

デザインは添付した画像になります。

「visual sutudio2012での」の質問画像

A 回答 (1件)

【Form1.vb】


Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As Integer
If Not Integer.TryParse(TextBox2.Text, value) Then
MessageBox.Show("数値ではない。", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If

Try
TextBox1.Text = value.ToEnglish()

Catch ex As FormatException
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try
End Sub
End Class


【IntExtension.vb】
Imports System.Runtime.CompilerServices
Imports System.Text

Module IntExtension
''' <summary>
''' 指定された数値を英語表記に変換します。
''' </summary>
''' <param name="value">数値</param>
''' <returns>数字の英語表記</returns>
<Extension()>
Public Function ToEnglish(value As Integer) As String
Dim result As New StringBuilder()
Dim calcValue As Integer = value

If calcValue > 999 Then
Throw New FormatException("処理可能な数値は999まで")
End If

If calcValue >= 100 Then
Dim truncateValue As Integer = CType(Math.Truncate(calcValue / 100), Integer)
result.Append(IntExtension._toSingleEnglish(truncateValue))
result.Append(" hundred")
calcValue -= truncateValue * 100
End If

If calcValue >= 20 Then
Dim truncateValue As Integer = CType(Math.Truncate(calcValue / 10), Integer)
If result.Length > 0 Then
result.Append(" ")
End If
result.Append(IntExtension._toTyEnglish(truncateValue))
calcValue -= truncateValue * 10
End If

If calcValue >= 11 Then
If result.Length > 0 Then
result.Append(" ")
End If
result.Append(IntExtension._toTeenEnglish(calcValue))
calcValue -= calcValue
End If

If calcValue >= 1 Then
If result.Length > 0 Then
result.Append(" ")
End If
result.Append(IntExtension._toSingleEnglish(calcValue))
calcValue -= calcValue
End If

Return result.ToString()
End Function

''' <summary>
''' 1~10を英語に変換。
''' </summary>
''' <param name="value">対象桁の数値</param>
''' <returns>数字の英語表記</returns>
Private Function _toSingleEnglish(value As Integer) As String
Dim singleNumeric As New Dictionary(Of Integer, String)() From
{
{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"},
{6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"}, {10, "ten"}
}

Return singleNumeric(value)
End Function

''' <summary>
''' 20,30,40,50,60,70,80,90を英語に変換。
''' </summary>
''' <param name="value">対象桁の数値</param>
''' <returns>数字の英語表記</returns>
Private Function _toTyEnglish(value As Integer) As String
Dim singleNumeric As New Dictionary(Of Integer, String)() From
{
{2, "twenty"}, {3, "thirty"}, {4, "forty"}, {5, "fifty"},
{6, "sixty"}, {7, "seventy"}, {8, "eighty"}, {9, "ninety"}
}

Return singleNumeric(value)
End Function

''' <summary>
''' 11~19を英語に変換。
''' </summary>
''' <param name="value">数値</param>
''' <returns>数字の英語表記</returns>
Private Function _toTeenEnglish(value As Integer) As String
Dim singleNumeric As New Dictionary(Of Integer, String)() From
{
{11, "eleven"}, {12, "twelve"}, {13, "thirteen"}, {14, "fourteen"}, {15, "fifteen"},
{16, "sixteen"}, {17, "seventeen"}, {18, "eighteen"}, {19, "nineteen"}
}

Return singleNumeric(value)
End Function

End Module
    • good
    • 0
この回答へのお礼

とっても分かりやすく、詳細な回答ありがとうございます!!!
無事完成させることができました!!!
本当にありがとうございました!!!

お礼日時:2017/02/03 00:48

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!