2008年11月28日 星期五

技藝競竇97年模擬試題參考答案 Q6


' Problem6:中文大寫數字(13%)
' 我們在金融機構填寫金額時使用的不是阿拉伯數字,而是中文的大寫數字。請寫一個程式將數字轉換為中文大寫數
' 字。
' 標準大寫寫法如下:零、壹、貳、參、肆、伍、陸、柒、捌、玖、拾、佰、仟、萬、億
' 輸入說明:
' 整數數字n (0 ≤ n ≤ 2147483647)。
' 輸出說明:
' 文字字串,遇到10 時,請輸出『壹拾』。
' 輸入範例:
' 12345
' 10200
' 輸出範例:
' 壹萬貳仟參佰肆拾伍
' 壹萬零貳佰
'
' 這一題只考慮測試資料會錯的很離譜
' 這個程式可以處理102345=>壹拾萬貳仟参佰肆拾伍情況

Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim strB = "零壹貳參肆伍陸柒捌玖"
Dim strA = "0123456789"

Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test6.txt")
Dim recArray() = Split(fileContents, vbNewLine)

Dim recIndex = 0
While recArray(recIndex) <> ""
Dim strS = recArray(recIndex)
Dim strT = "" '暫存每次一筆記錄轉成之字串
Dim numT = Mid(strB, InStr(strA, Mid(strS, Len(strS), 1)), 1)
strT = numT
Dim level = 1 '單位
For i = Len(strS) - 1 To 1 Step -1
Dim strLevel = Choose(level, "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾")
numT = Mid(strB, InStr(strA, Mid(strS, i, 1)), 1)
If numT = "零" Then
If strLevel = "萬" Or strLevel = "億" Then
strT = numT & strLevel & strT
Else
strT = numT & strT
End If
Else
strT = numT & strLevel & strT
End If
level = level + 1
Next

'修飾strT
If Len(strT) > 3 Then
If Microsoft.VisualBasic.Left(strT, 3) = "壹拾零" Then
strT = Microsoft.VisualBasic.Left(strT, 2) & Mid(strT, 4, Len(strT) - 3)
End If
Else
strT = "壹拾"
End If

'改連續零為一個零
Dim pre = "零"
Dim strT1 = ""
For j = Len(strT) To 1 Step -1
If Not (Mid(strT, j, 1) = "零" And pre = "零") Then
strT1 = Mid(strT, j, 1) & strT1
End If
pre = Mid(strT, j, 1)
Next
strT = strT1

'處理零萬狀況
If Microsoft.VisualBasic.Right(strT, 2) = "零萬" Then
strT = Microsoft.VisualBasic.Left(strT, Len(strT) - 2)
End If

If InStr(strT, "拾零萬") <> 0 Then
strT = Replace(strT, "拾零萬", "拾萬")
End If

'處理零狀況
If recArray(recIndex) = "0" Then strT = "零"

resultStr = resultStr & strT & vbNewLine
recIndex = recIndex + 1
End While
MsgBox(resultStr)
' My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result5.txt", resultStr, False)
End
End Sub
End Class

沒有留言: