(2)類似題舉例2: 資商學院的學號有5位,第1碼為系碼,第2碼為年度,第3碼為班碼,後2碼為座號
系碼只能是SYZ其中之1,年度只能是2~5其中之一,班碼只能是1~3,座號一定是兩位01~50
讀入n列字串,判斷是否為正確的學號,正確則則輸出R,不正確則輸出E
解題提示:系碼可設定一字串 hstr=”SYZ”,可以instr(hstr,c)函數尋找也可以用IF判斷
參考程式碼:
Dim hstr As String =”SYZ” 'Instr用
Function m5p02(ByVal s As String) As String
m5p02 = "R"
s = Trim(s) '去掉首尾的空白
If s.Length <> 5 Then Return "E" '長度不對
Dim c As Char = Mid(s, 1, 1) '第1碼只能 SYZ之一
If c <> "S" And c <> "Y" And c <> "Z" Then Return "E"
' If
instr(hstr,c)=0 then return “E” '也可以改用 Instr找不到傳回0
c = Mid(s, 2, 1)
If c < "2" Or c > "5" Then Return "E" '第2碼只能 2~5
c = Mid(s, 3, 1)
If c < "1" Or c > "3" Then Return "E" '第3碼只能 1~3
c = Mid(s, 4, 1)
If c < "0" Or c > "5" Then Return "E" '第4碼只能 0~5
c = Mid(s, 5, 1)
If c < "0" Or c > "9" Then Return "E" '第5碼只能 0~9
Dim no As Integer = Val(Mid(s, 4, 5)) ' 4、5兩碼合成一個數 只能 1~50
If no < 1 Or no > 50 Then Return "E"
End Function
-----------------------------------------
in1.txt
-----------------------------------------
3
Z1345
Y3150
S4234
-----------------------------------------
in2.txt
-----------------------------------------
5
X5123
Z53450
Y3151
S234A
Z2209
-----------------------------------------
out.txt
-----------------------------------------
E
R
R
E
E
E
E
R
0 意見:
張貼留言