請設計一個英文造句的程式,能將輸入的一些名詞,動詞(名詞及動詞的輸入順序是隨機)組成一些簡單句型。
所謂簡單句型是
主詞+及物動詞+受詞.
注意規則:
a.當主詞是He, She, Mary, John時,動詞要加s.
b.當主詞和受詞是同一類人稱代名詞時,受詞要改成反身代名詞。
I -- me -- myself
He/John -- him -- himself
She/Mary -- her -- herself
They -- them -- themselves
本題造句所需用到的主詞、動詞、受詞全部列出如下:
主詞: I、He、She、They、Mary、John
及物動詞: love、like、see、find
受詞:me、him、her、them、Mary、John
輸入規範
輸入檔案中第一列為一個正整數n,代表有n個英文造句練習。其後n列,每一列為一個英文造句練習,每一列有一個主詞、一個受詞、一個動詞(其順序是隨機),各個英文字之間以一個或多個空白分隔。注意:n 100。
輸出規範
輸出n列,每一列為一個英文造句之答案。若某個練習中,有多個造句是合於文法的,則需全部在同一列印出,且兩個英文句子之間需印出"或"這個字以示區隔。
輸入範例(test7.txt)
5
I her love
I love her
Mary love John
them see I
love I me
輸出範例(result7.txt)
I love her.
I love her.
Mary loves John. 或 John loves Mary.
I see them.
I love myself.
Dim k1 As String, k2 As String, k3 As String
回覆刪除Private Sub Form_Load()
Dim ans As Boolean
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, strin
Call ss(strin)
Call vv(strin)
Call oo(strin)
Print #2, k1; " ";
If (k1 = "He" Or k1 = "She" Or k1 = "Mary" Or k1 = "John") Then
Print #2, k2; "s"; " ";
Else
Print #2, k2; " ";
End If
ans = False
If (k1 = "Mary" And k3 = "her") Then
Print #2, "herself"
ans = True
End If
If (k1 = "John" And k3 = "him") Then
Print #2, "himself"
ans = True
End If
If (k1 = "Mary" Or k1 = "John") And (k3 = "Mary" Or k3 = "John") Then
Print #2, k3; " 或 "; k3; " "; k2; " "; k1
ans = True
End If
If ans = False Then
Print #2, k3
End If
Print #2,
Next i
Close #1
Close #2
End Sub
Private Sub ss(a)
s = Split("I He She They Mary John")
q = Split(a)
m = 0
Do Until m = 3
For i = 0 To UBound(s)
If q(m) = s(i) Then
k1 = s(i)
Exit Do
End If
Next i
m = m + 1
Loop
End Sub
Private Sub vv(a)
v = Split("love like see find")
q = Split(a)
m = 0
Do Until m = 3
For i = 0 To UBound(v)
If q(m) = v(i) Then
k2 = v(i)
Exit Do
End If
Next i
m = m + 1
Loop
End Sub
Private Sub oo(a)
o = Split("me him her them Mary John")
q = Split(a)
m = 0
Do Until m = 3
For i = 0 To UBound(o)
If q(m) = o(i) Then
If k1 <> o(i) Then
k3 = o(i)
Exit Do
End If
End If
Next i
m = m + 1
Loop
If k3 = "me" Then k3 = "myself"
End Sub
阿揚好,
回覆刪除這題ok,就是要將每個可能給想清楚而已。
前面的3個副程式的用法,還不錯。不過,因為是要找東西,是有要傳回值的,所以,如果用函數,會更符合。
主程式中的條件裡,寫了很多條件,如果加上instr這個函數,會簡單些。