NPSC補完計劃

NPSC國中組 => NPSC2001國中組決賽 => 主題作者是: lini 於 十一月 29, 2009, 07:34:09 pm

主題: [VB] NPSC 2001 決賽 pC 算盤
作者: lini十一月 29, 2009, 07:34:09 pm
字串處理
2001年的輸入有些技巧

代碼: [選擇]
Sub main()
    Open "abacus.in" For Input As #1
    Open "abacus.out" For Output As #2
    Do
        tn = "": pl = True: f = 0
        Do
            Input #1, n
            If n = 0 Then GoTo e2do
            If n <> "+" And n <> "-" And n <> "=" Then
                tn = gnum(n) & tn
            Else
                If pl = True Then
                    f = f + Val(tn)
                Else
                    f = f - Val(tn)
                End If
                tn = ""
                If n = "+" Then
                    pl = True
                ElseIf n = "-" Then
                    pl = False
                End If
            End If
            If n = "=" Then Exit Do
        Loop
        Print #2, Replace(gph(f), " ", vbCrLf) & vbCrLf & "="
    Loop
e2do:
    Close
End Sub

Function gnum(ph)
    t = Split(ph, "|")
    gnum = Len(Split(t(0), "_")(1)) * 5 + Len(Split(t(1), "_")(0))
End Function

Function gph(num)
    f = ""
    For i = Len(num) To 1 Step -1
        t = Val(Mid(num, i, 1)) \ 5
        t2 = Val(Mid(num, i, 1)) Mod 5
        f = f & " " & IIf(t = 1, "_*", "*_") & "|" & String(t2, "*") & "_" & String(4 - t2, "*")
    Next
    gph = Trim(f)
End Function