2012年9月30日 星期日

100模擬 Problem 1-2:字串的處理


子題 2:在此子題中,「邏輯表示式」除了「true」、「false」、「==」、「!=」及「空格」外,另外
有一個變數「a」。其中「a」的值可能是「true」或「false」其中之一。已知輸入檔中所有的邏
輯表示式「結果均為『真』」,請選手判斷輸入檔各表示式中「a」的值,並依序輸出至輸出檔
中。
輸入說明:
共有4 行資料,每行有1 個邏輯表示式,每行最多120 個字。
輸出說明:
共4 行,分別列出輸入檔中對應行數的邏輯表示式內「a」的值。若「a」的值為「true」,輸
出「TRUE」,反之輸出「FALSE」。(輸出均為大寫,選手請注意。)
輸入範例:【檔名:in-1-2.txt】
true==false==a==true!=false
a!=false==true!=false==true
false==true==true!=false!=true!=false==a==true
true==false==true!=false==a
輸出範例:【檔名:out-1-2.txt】
FALSE
TRUE
TRUE
FALSE

2 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\IN1-2.TXT" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    '== =And != = Xor
    Do While Not EOF(1)
    Input #1, x
    xx = x
    For i = 1 To Len(x)
    If Mid(x, i, 2) = "==" Then x = Replace(x, "==", "3")
    If Mid(x, i, 2) = "!=" Then x = Replace(x, "!=", "4")
    If Mid(x, i, 4) = "true" Then x = Replace(x, "true", "1")
    If Mid(x, i, 5) = "false" Then x = Replace(x, "false", "0")
    Next
    For i = 1 To Len(x)
    If Mid(x, i, 1) = "a" Then x = Replace(x, "a", "1")
    Next
    If Len(x) > 1 Then
    Do Until Len(x) = 1
    i = 2
    If Mid(x, i, 1) = "3" Then If Mid(x, i - 1, 1) = Mid(x, i + 1, 1) Then x = "1" & Mid(x, 4) Else: x = "0" & Mid(x, 4)
    If Mid(x, i, 1) = "4" Then If Mid(x, i - 1, 1) = Mid(x, i + 1, 1) Then x = "0" & Mid(x, 4) Else: x = "1" & Mid(x, 4)
    Loop
    End If
    If x = "1" Then Print #2, "TRUE" Else: Print #2, "FALSE"
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in-1-2.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Line Input #1, n
    For i = 1 To Len(n)
    If Mid(n, i, 2) = "==" Then n = Replace(n, "==", "3")
    If Mid(n, i, 2) = "!=" Then n = Replace(n, "!=", "4")
    If Mid(n, i, 4) = "true" Then n = Replace(n, "true", "1")
    If Mid(n, i, 5) = "false" Then n = Replace(n, "false", "0")
    Next i
    For i = 1 To Len(n)
    If Mid(n, i, 1) = "a" Then n = Replace(n, "a", "1")
    Next i
    Do Until Len(n) = 1
    Select Case Left(n, 3)
    Case "031"
    n = Replace(n, "031", 0)
    Case "030"
    n = Replace(n, "030", 1)
    Case "130"
    n = Replace(n, "130", 0)
    Case "131"
    n = Replace(n, "131", 1)
    Case "140"
    n = Replace(n, "140", 1)
    Case "141"
    n = Replace(n, "141", 0)
    Case "041"
    n = Replace(n, "041", 1)
    Case "040"
    n = Replace(n, "040", 0)
    End Select
    Loop
    If n = "1" Then Print #2, "TRUE" Else Print #2, "FALSE"
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除