從"in.txt"中輸入一個包含小括號()及中括號﹝﹞的字串。當字串符合下列條件時我們稱他為正確的運算式:
1.該字串為空字串。
2.如果A為正確的運算式,則(A)及﹝A﹞都為正確的運算式。
3.如果A和B都為正確的運算式,則AB也為正確的運算式。
4.字串最大長度為128個字元
5.輸入檔案的第一列為正整數n,代表接下來有n列的待測資料。
6.檢查每列待測資料,如果正確輸出Yes,否則輸出No。
輸入範例:
3
(﹝﹞)
((﹝()﹞)))
(﹝()﹝﹞()﹞)()
輸出範例:
Yes
No
Yes
老師 因為 這個禮拜我們真的都比較繁忙...
回覆刪除所以這禮拜只出三篇
也會回的比較慢
請老師諒解呀...
因為是寒假作業要到期了,是嗎?
回覆刪除哈,學生生活呢。
Private Sub Form_Load()
回覆刪除Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Input #1, b
ReDim A(b) As String
For i = 0 To b - 1
ans = True
Input #1, A(i)
Do Until Len(A(i)) = 0
z = Mid(A(i), 1, 1)
If z = "(" Then
A(i) = Replace(A(i), "(", "", , 1)
If InStr(A(i), ")") = 0 Then
ans = False: Exit Do
Else
A(i) = Replace(A(i), ")", "", , 1)
End If
End If
If z = "﹝" Then
A(i) = Replace(A(i), "﹝", "", , 1)
If InStr(A(i), "﹞") = 0 Then
ans = False: Exit Do
Else
A(i) = Replace(A(i), "﹞", "", , 1)
End If
End If
If z <> "" And z <> "(" And z <> "﹝" Then ans = False: Exit Do
Loop
If ans Then
Print #2, "Yes"
Else
Print #2, "No"
End If
Next i
Close #2
Close #1
End Sub
高仔好,
回覆刪除終於又是新學期開始了,地點時間也重新定了下來,咱們一塊兒試試看吧。
1. ReDim A(b) as string
這行在程式中,只執行一次,直接dim不行嗎?
用redim一般是在需要重新定義某個變數或某個陣列吧。
2.你用了replace 這個函數去取代相同個數的左右(中小)括號,程式是正確的。
3.進一步要正式解決這一題的話,是要用push pop的觀念。
遇到右邊的符號就push進去stack(堆疊),
遇到左邊的符號就pop 出來一個,如果是相對的那個符號就是正確的。
4.下一次社團課,我們來學學function(函數)
Dim R(64), L(64) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, nn
For ii = 1 To nn
tstr = "﹞)X(﹝"
Input #1, x
w = 1: h = 1: ans = True
n = Len(x)
For i = 1 To n
k = InStr(tstr, Mid(x, i, 1)) - 3
If k > 0 Then
R(h) = k
h = h + 1
Else
L(w) = k
If R(h - 1) + L(w) = 0 Then
h = h - 1
Else
ans = False
End If
w = w + 1
End If
Next i
If ans Then
Print #2, "YES"
Else
Print #2, "NO"
End If
Next ii
Close #1
Close #2
End Sub
阿瑋好,
回覆刪除你這個程式有用了我說的push/pop的想法了,很好。
小問題2:
1.輸出錯誤。是Yes/No,你用的是YES/NO
2.你的左右的英文,是不是用錯了? 左left,右right.
Dim q As String
回覆刪除Dim l1(15) As String
Dim l2(15) As String
Dim ans As Boolean
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
For i = 1 To x
Line Input #1, q
n1 = 1
For j = 1 To Len(q)
step = Mid(q, j, 1) '(﹝﹞)
If step = "(" Or step = "﹝" Then
l1(n1) = step
n1 = n1 + 1
Else
ans = False
If step = ")" And l1(n1 - 1) = "(" Then ans = True: n1 = n1 - 1
If step = "﹞" And l1(n1 - 1) = "﹝" Then ans = True: n1 = n1 - 1
End If
Next j
If ans Then
Print #2, "YES"
Else
Print #2, "NO"
End If
Next i
Close #2
Close #1
End Sub
n2陣列多的@@
回覆刪除Dim q As String, lin1(15) As String, lin2(15) As String
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, qq
For i = 1 To qq
Line Input #1, q
c = 1
For j = 1 To Len(q)
step = Mid(q, j, 1)
If step = "(" Or step = "﹝" Then
lin1(c) = step
c = c + 1
Else
ans = False
If step = ")" And lin1(c - 1) = "(" Then
ans = True
c = c - 1
End If
If step = "﹞" And lin1(c - 1) = "﹝" Then
ans = True
c = c - 1
End If
End If
Next j
If ans = True Then
Print #2, "Yes"
Else
Print #2, "No"
End If
Next i
Close #2
Close #1
End Sub