2012年9月13日 星期四

100正式Problem1-2


在此子題中,「邏輯表示式」除了原有的「整數數字」、「+」、「-」、「==」及「空格」外,另外有一個運算符號「*」,代表「乘法運算」,其運算優先權「高於」加法及減法。請選手在輸出檔中輸出相對表示式檢查的結果。
輸入說明:
每個輸入檔案有 4 行資料,每行有1 個邏輯表示式,每行最多120 個字。「整數數字」介於0
到100 之間,數字運算結果介於正負1000 之間。
輸出說明:
依序輸出對應的邏輯表示式檢查結果。若輸入行之檢查結果為真,該行輸出為「TRUE」,反
之輸出「FALSE」。(輸出均為大寫,選手請注意。)
輸入檔案 1:【檔名:in1.txt】

20+3*5==7*5
13*3+2*3==123
6+5*4-8==18
4-5+15-3*3==0

輸入檔案 2:【檔名:in2.txt】

10*3+5==5*7
12*4-10+5==43
72-10*5==20+2
5-4*10+50==60

輸出檔案:【檔名:out.txt】
TRUE
FALSE
TRUE
FALSE

TRUE
TRUE
TRUE
FALSE

1 則留言:

  1. Dim ans, ans1 As Integer
    Dim x
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in2.txt" For Input As #3
    Do While Not EOF(1)
    Line Input #1, x
    Call abc
    Loop
    Print #2,
    Do While Not EOF(3)
    Line Input #3, x
    Call abc
    Loop
    Close
    Close
    Close
    End
    End Sub
    Sub abc()
    x = Replace(x, "+", " a ")
    x = Replace(x, "-", " b ")
    x = Replace(x, "==", " c ")
    x = Replace(x, "*", " d ")
    d = Split(x, " ")
    For i = 0 To UBound(d)
    If d(i) = "d" Then d(i - 1) = d(i - 1) * d(i + 1): d(i) = "": d(i + 1) = ""
    Next
    For i = 0 To UBound(d)
    If d(i) = "c" Then f = i: Exit For
    Next
    ans = d(0)
    For i = 0 To (f - 1)
    If f - 1 = 0 Then ans = d(0): Exit For
    Select Case d(i)
    Case "a"
    ans = ans + Val(d(i + 1))
    Case "b"
    ans = ans - Val(d(i + 1))
    End Select
    Next
    ans1 = d(f + 1)
    For i = (f + 1) To UBound(d)
    If f + 1 = UBound(d) Then ans1 = d(f + 1): Exit For
    Select Case d(i)
    Case "a"
    ans1 = ans1 + Val(d(i + 1))
    Case "b"
    ans1 = ans1 - Val(d(i + 1))
    End Select
    Next
    If ans = ans1 Then Print #2, "TRUE" Else: Print #2, "FALSE"
    End Sub

    回覆刪除