2011年1月28日 星期五

字串內容

請設計一程式,讀取"in.txt"中A、B兩字串(A長度小於B),並判斷字串A中之所有字元是否依序(不要求相鄰)出現在字串B中(大小寫不分)。

輸入範例:

A=dos
B=Doctor of philosophy

輸出範例:

輸出答案為 是

21 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a
    Input #1, b
    Close

    longs = Len(a) '抓長度

    Dim s(20)

    tmp = 0 '初始直 (以後當記錄用)

    For i = 1 To longs
    s(i) = UCase(Mid(a, i, 1)) ' UCASE 轉換成大寫 ( LCASE 則反之 )
    s(i) = InStr(UCase(b), s(i)) ' 抓 b 字串中的第一個出現位置

    If s(i) > tmp Or tmp = 0 Then tmp = s(i) Else Exit Sub
    '比對是否照順序 (不是的話直接跳離程式
    Next


    Open App.Path & "\out.txt" For Output As #2
    Print #2, "是"
    Close #2

    End
    End Sub

    ------------------------
    好像都忘了一天一題了吧...

    這題我之前"庫存"的

    沒想到有註解呢

    回覆刪除
  2. Dim A As String
    Dim B As String
    Dim Ast(20) As String
    Dim ALong As Byte
    Dim BLong As Byte
    Dim Re(20) As Byte
    Dim ans As Boolean

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, A
    Input #1, B
    Close #1

    ans = False
    ALong = Len(A)
    BLong = Len(B)
    B = LCase(B)
    For i = 1 To ALong
    Ast(i) = LCase(Mid(A, i, 1))
    Next i

    If ALong < BLong Then

    For i = 1 To ALong
    Re(i) = InStr(B, Ast(i))
    If Re(i) = 0 Then ans = False
    Next i

    If (Re(3) > Re(2)) And (Re(2) > Re(1)) Then ans = True

    Open App.Path & "\out.txt" For Output As #2
    If ans = True Then
    Print #2, "是"
    Else
    Print #2, "否"
    End If
    Close #2

    End If

    End
    End Sub

    用Instr詢字

    回覆刪除
  3. 你們好
    如果a="dso"會如何呢?

    回覆刪除
  4. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Dim d(20)
    Input #1, a
    Input #1, b
    X1 = Len(a)
    X2 = Len(b)
    t = 0
    If X1 < X2 Then
    For i = 1 To X1
    d(i) = UCase(Mid(a, i, 1))
    d(i) = InStr(UCase(b), d(i))
    If d(i) > t Or t = 0 Then
    t = d(i)
    Else
    Exit Sub
    End If
    Next i
    End If

    Print #2, "是"
    Close #1
    Close #2

    End
    End Sub

    回覆刪除
  5. 緣尉好,
    正確的部份,ok.
    (其他兩個人的程式是錯的)
    但是,錯誤的部份呢?
    另外,請選手們注意,比賽時,可以不用管錯誤輸入的部份)

    回覆刪除
  6. 更正


    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a
    Input #1, b
    Close

    longs = Len(a) '抓長度

    Dim s(20)

    tmp = 0 '初始直 (以後當記錄用)

    For i = 1 To longs
    s(i) = UCase(Mid(a, i, 1)) ' UCASE 轉換成大寫 ( LCASE 則反之 )
    s(i) = InStr(UCase(b), s(i)) ' 抓 b 字串中的第一個出現位置

    If s(i) > tmp Or tmp = 0 Then tmp = s(i) Else GoTo bye
    '比對是否照順序 (不是的話直接跳離程式
    Next


    Open App.Path & "\out.txt" For Output As #2
    Print #2, "是"
    Close #2


    End
    bye:
    Open App.Path & "\out.txt" For Output As #2
    Print #2, "否"
    Close #2
    End
    End Sub


    ---------

    應該是沒有判斷 錯的話的輸出

    所以第2次執行錯的

    OUT.TXT 不會改

    請問老師是錯在這邊嗎?

    回覆刪除
  7. Arro好,
    你有用我説的dso當輸入試試看嗎?o的位置有多個,第一個的話,不對,但是後面還有。

    回覆刪除
  8. Dim a As String
    Dim b As String
    Dim c As Byte '旗子
    Dim d As Byte '有相同次數
    Dim e As Boolean'判斷是否有相同

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a
    Input #1, b
    Close #1

    c = 1
    d = 0
    b = LCase(b)
    e = False
    For i = 1 To Len(a)
    For j = c To Len(b)
    If Mid(b, j, 1) = Mid(a, i, 1) Then c = (i + 1): e = True
    Next j
    If e = True Then d = d + 1
    e = False
    Next i

    Open App.Path & "\out.txt" For Output As #2
    If d = Len(a) Then Print #2, "是" Else Print #2, "否"
    Close #2
    End
    End Sub

    回覆刪除
  9. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a
    Input #1, b
    Close


    Dim checks As Boolean

    tmp = 0

    checks = True


    For i = 1 To Len(a)


    runs = UCase(Mid(a, i, 1))


    List1.Clear
    For j = 1 To Len(b)
    KK = UCase(Mid(b, j, 1))
    If runs = KK Then List1.AddItem j
    Next


    If List1.ListCount = 0 Then

    checks = False

    Else

    For k = 1 To List1.ListCount
    MM = List1.List(k - 1)
    If tmp = 0 Or MM > Int(tmp) Then tmp = MM: Exit For
    checks = False
    Next

    End If


    Next


    Open App.Path & "\out.txt" For Output As #2
    If checks = True Then Print #2, "是" Else Print #2, "否"
    Close #2



    End
    End Sub


    //-------------------------

    應該會正確吧~

    字串跟數字間真的還蠻需要注意到的

    字串間是比頭一個的大小的樣子

    但是

    If "40" > Int("23") Then Print "O" Else Print "X"

    這樣比 答案是 O 呢

    回覆刪除
  10. Private Sub Form_Load()
    Me.Hide
    Dim d(20)
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a, b
    X1 = Len(a)
    X2 = Len(b)
    d(0) = 1
    If X1 < X2 Then
    For i = 1 To X1
    n = UCase(Mid(a, i, 1))
    d(i) = UCase(Mid(a, i, 1))
    m = Mid(b, d(i - 1), X2 - d(i - 1) + 1)
    d(i) = InStr(UCase(m), d(i))
    If d(i) = 0 Then
    Close #1
    Close #2
    End
    Exit Sub
    End If
    Next i
    End If

    Print #2, "是"
    Close #1
    Close #2

    End
    End Sub
    --------------
    頭昏眼花~"~

    回覆刪除
  11. 佑好,
    程式正確,但是,
    1. e = false 應該放在for i = 1 to len(a)的後面,下一個for的前面就好。
    2. if e=true then d = d+1, 如果e =false呢? 是不是就是沒找到了,
    就可以提前出局了啊。
    ->所以,如果也沒提前出局,是不是就是全找到了,也不用d=d+1這個多的變數了。
    3.大小寫不管,你的b顧到了,a呢?

    arro好,
    1. int(tmp)做什麼呢? 反而是,你的mm是string。
    反正你的變數先去定義,就不會有這些問題了。
    (dim mm as integer, tmp as integer)
    2.當mm>tmp ,你去改了tmp,但是,如果沒有呢? 你就不理它了?
    程式做好了,自己要多做幾組輸入,去看看會不會正確。
    以這題的A為例,同樣的B,只是將A改成dys
    3.還有你的checks的值,也是亂設一通
    程式錯誤。哦,有去嘗試listbox,這是好的。

    緣尉好,
    的確是頭昏眼花,再寫一次吧。反正一定是錯的,因為你的程式中,沒有"否"。

    回覆刪除
  12. 熊掌好,
    1.恩,原本的做法的確多寫了。
    2.就是說能完整跑完就是正確了。
    3.a應該也要跟b一樣設為大寫or設為小寫。會注意到^^


    Dim a As String
    Dim b As String
    Dim c As Byte '旗子
    Dim d As Byte '有相同次數
    Dim e As Boolean '判斷是否有相同

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    Input #1, b
    Close #1

    c = 1
    d = 0
    b = LCase(b)
    a = LCase(a)

    For i = 1 To Len(a)
    e = False
    For j = c To Len(b)
    If Mid(b, j, 1) = Mid(a, i, 1) Then c = (i + 1): e = True
    Next j
    If (e = True) And (i = Len(a)) Then Print #2, "是"
    If (e = False) And (i = Len(a)) Then Print #2, "否"
    Next i

    Close #2
    End
    End Sub

    回覆刪除
  13. 佑好,
    這題,你改錯了哦。
    (哦,是我上次沒看出來,你原來的程式也是錯的。)
    自己試試不同的a,b字串,去試試,到底對不對。

    回覆刪除
  14. 熊掌好,

    恩a有重複字母時會出錯


    Dim a As String
    Dim b As String
    Dim c As Byte '旗子
    Dim d As Byte '有相同次數
    Dim e As Boolean '判斷是否有相同

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    Input #1, b
    Close #1


    d = 0
    b = LCase(b)
    a = LCase(a)

    For i = 1 To Len(a)
    e = False
    c = 1

    Do
    If Mid(b, c, 1) = Mid(a, i, 1) Then e = True
    c = c + 1
    Loop Until e = True

    If (e = True) And (i = Len(a)) Then Print #2, "是"
    If (e = False) And (i = Len(a)) Then Print #2, "否"
    Next i

    Close #2
    End
    End Sub

    偵錯真的蠻辛苦的,謝謝老師

    回覆刪除
  15. 佑好,
    別灰心,也別高興太早,這個程式,還有兩個錯的地方。加油。

    回覆刪除
  16. 熊掌好,
    用do loop改成後
    似乎更多錯:D

    然後解決一個錯再拿別的字去測又錯
    所以是很多錯~"~
    If (j = Len(b)) And (e = flase) Then i = Len(a): Exit For
    的 i = Len(a) 是用來提早結束


    Dim a As String
    Dim b As String
    Dim c As Byte '旗子
    Dim e As Boolean '判斷是否有相同

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    Input #1, b
    Close #1

    c = 1
    d = 0
    b = LCase(b)
    a = LCase(a)

    For i = 1 To Len(a)
    e = False
    For j = c To Len(b)
    If Mid(b, j, 1) = Mid(a, i, 1) Then c = (j + 1): e = True: Exit For
    If (j = Len(b)) And (e = flase) Then i = Len(a): Exit For
    Next j
    If (e = True) And (i = Len(a)) Then Print #2, "是"
    If (e = False) And (i = Len(a)) Then Print #2, "否"
    Next i

    Close #2
    End
    End Sub

    回覆刪除
  17. 佑好,
    1.If (j = Len(b)) And (e = flase) Then i = Len(a): Exit For
    的 i = Len(a) 是用來提早結束
    這是你說的,但是,會走到這一行的,e一定是false ,你還打錯字呢。
    而且,當j=len(b), 迴圈不也是走到最後一步了嗎? 所以,這一行的兩個條件,是不是,怪怪的呢。

    2.前一個程式的,c=1放到兩個迴圈的前頭去,再將loop的條件加上一個終止條件,loop until e=true or c> len(b)
    這樣的話,就成了。

    回覆刪除
  18. 熊掌好,
    很多情況要去判斷,
    腦袋會被打死結。就轉不出來了!!

    Dim a As String
    Dim b As String
    Dim c As Byte '旗子
    Dim d As Byte '有相同次數
    Dim e As Boolean '判斷是否有相同

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    Input #1, b
    Close #1

    c = 1
    d = 0
    b = LCase(b)
    a = LCase(a)

    For i = 1 To Len(a)
    e = False


    Do
    If Mid(b, c, 1) = Mid(a, i, 1) Then e = True
    c = c + 1
    Loop Until e = True or c> len(b)

    If (e = True) And (i = Len(a)) Then Print #2, "是"
    If (e = False) And (i = Len(a)) Then Print #2, "否"
    Next i

    Close #2
    End
    End Sub

    回覆刪除
  19. Private Sub Form_Load()
    Me.Hide
    Dim d(20)
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1
    Input #1, a, b
    X1 = Len(a)
    X2 = Len(b)
    d(0) = 1
    If X1 > X2 Then
    Print #2, "否"
    Close #1
    Close #2
    End
    Else
    For i = 1 To X1
    n = UCase(Mid(a, i, 1))
    m = Mid(b, d(i - 1), X2 - d(i - 1) + 1)
    d(i) = InStr(UCase(m), n)
    If d(i) = 0 Then
    Print #2, "否"
    Close #1
    Close #2
    End
    End If
    Next i
    End If

    Print #2, "是"
    Close #1
    Close #2

    'end
    End Sub
    ---------------------
    所以沒有明顯講出不用的話就是對錯都要做囉ㄎㄎ
    除了增加"否"之外沒有太大變更,嗯

    回覆刪除
  20. 緣尉好,
    1. 三天假期,又快過了,你們的進度啊...
    If X1 > X2 Then
    Print #2, "否"
    Close #1
    Close #2
    End
    Else
    這一段程式,是小捷徑,如果x1比較長,就得到答案,結束。
    但是,這一段也是浪費程式設計的時間,因為下面的程式,也應該能判斷出這樣的結果的。
    2. 利用 instr找出位置,配合mid去掉前頭已找過的字,很棒的想法。
    (雖然,我覺得用right來取代你用的mid,會更順)

    回覆刪除
  21. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a
    Input #1, b
    Call A1(UCase(a), UCase(b))
    Close
    Close
    End
    End Sub

    Sub A1(a, b)


    ch2 = 0
    k = 1
    For j = 1 To Len(a)
    ch = 0
    ma = Mid(a, j, 1)
    For i = k To Len(b)
    mb = Mid(b, i, 1)
    If ma = mb Then k = i: ch = 1: Exit For
    Next
    If ch = 0 Then ch2 = 1
    Next
    If ch2 = 0 Then Print #2, "是" Else Print #2, "否"

    End Sub


    5分

    回覆刪除