2011年10月19日 星期三

97模擬 Problem 6 (中文大寫數字

我們在金融機構填寫金額時使用的不是阿拉伯數字,而是中文的大寫數字。請寫一個程式將數字轉換為中文大寫數字。

標準大寫寫法如下:零、壹、貳、參、肆、伍、陸、柒、捌、玖、拾、佰、仟、萬、億

輸入說明:
整數數字n (0 ≤ n ≤ 2147483647)。

輸出說明:
文字字串,遇到10 時,請輸出『壹拾』。

輸入範例:
12345
10200

輸出範例:
壹萬貳仟參佰肆拾伍
壹萬零貳佰

2 則留言:

  1. Dim Ans
    Private Sub Form_Load()
    '零 壹 貳 參 肆 伍 陸 柒 捌 玖 拾 佰 仟 萬 億
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    Ans = ""
    Dim s

    s = Split("  拾 佰 仟 萬 億")
    For i = 1 To Len(n)
    m = Mid(n, i, 1)
    Call CHs(m)
    If m <> 0 Then
    If (Len(n) + 1 - i) <= 5 Then Ans = Ans & s((Len(n) - i))
    If (Len(n) + 1 - i) > 5 Then Ans = Ans & s((Len(n) + 1 - i) - 5) & s(4)
    If (Len(n) + 1 - i) > 9 Then Ans = Ans & s((Len(n) + 1 - i) - 9) & s(5)
    End If
    Next
    Do While Right(Ans, 1) = "零"
    Ans = Left(Ans, Len(Ans) - 1)
    Loop
    Print #2, Ans
    Loop
    Close
    Close
    End
    End Sub

    Sub CHs(a)
    Dim Q
    Q = Split("零 壹 貳 參 肆 伍 陸 柒 捌 玖")
    Ans = Ans & Q(a)
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide

    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    X = Split(" ,拾,佰,仟,萬,拾,佰,仟,億,拾,佰,仟,兆", ",")
    Do While Not EOF(1)
    Input #1, N
    ans = ""
    K = 0
    For i = Len(N) To 1 Step -1
    ans = Mid(N, i, 1) & X(K) & ans
    K = K + 1
    Next i

    ans = Replace(ans, "1", "壹")
    ans = Replace(ans, "2", "貳")
    ans = Replace(ans, "3", "參")
    ans = Replace(ans, "4", "肆")
    ans = Replace(ans, "5", "伍")
    ans = Replace(ans, "6", "陸")
    ans = Replace(ans, "7", "柒")
    ans = Replace(ans, "8", "捌")
    ans = Replace(ans, "9", "玖")
    ans = Replace(ans, "0", "零")
    ans = Replace(ans, "零拾", "零")
    ans = Replace(ans, "零佰", "零")
    ans = Replace(ans, "零仟", "零")
    ans = Replace(ans, "零萬", "零")
    Do
    ans = Replace(ans, "零零", "零")
    Loop Until InStr(ans, "零零") = 0
    ans = Replace(ans, " ", "")

    If Mid(ans, Len(ans), 1) = "零" Then ans = Mid(ans, 1, Len(ans) - 1)
    Print #2, ans
    Loop
    Close #2
    Close #1

    End
    End Sub

    回覆刪除