2010年9月20日 星期一

阿揚丙級軟體設計

Private Sub Form_Load()
q1
q2
q3
q4
q5
End Sub


Public Sub q1()
Dim arr(9) As Integer
Open App.Path & "\1in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
Print x;
c = x
ans = True
For i = 1 To 9
If 10 ^ i > x Then k = i: Exit For
Next i
For i = 1 To k \ 2 + 1
a = c Mod 10
c = c \ 10
b = x \ 10 ^ (k - i)
x = x Mod 10 ^ (k - i)
If a <> b Then ans = False
Next i
If ans Then Print " is a palindrome." Else Print " is not a palindrome."

Close #2
Close #1
End Sub


Public Sub q2()
Open App.Path & "\2in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
For i = 1 To x
c = c & i
Print c
Next i
Close #2
Close #1
End Sub


Public Sub q3()
Open App.Path & "\3in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
ans = True
i = 2
Do
If x Mod i = 0 And x = 2 And x = 1 Then ans = False
i = i + 1
Loop While i <= (x ^ 0.5)
If ans Then Print x & " is a prime number." Else Print x & " is not a prime number."
Close #2
Close #1
End Sub


Public Sub q4()
Open App.Path & "\4in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
ans = 99
Do Until EOF(1)
Input #1, h, w
bmi = ((w / (h / 100) ^ 2 + 0.5) * 10) \ 10
If bmi < ans Then ans = bmi
Loop
If (ans > 20) And (ans < 25) Then Print ans & " 正常" Else Print ans & " 不正常"
Close #2
Close #1
End Sub


Public Sub q5()
Dim a(2, 2)
Dim b(2, 2)
Dim c(2, 2)
Open App.Path & "\5in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, a(1, 1), a(1, 2), a(2, 1), a(2, 2)
Input #1, b(1, 1), b(1, 2), b(2, 1), b(2, 2)
For i = 1 To 2
Print "[";
For j = 1 To 2
c(i, j) = a(i, j) + b(i, j)
Print c(i, j);
Next j
Print "]"
Next i
Close #2
Close #1
End Sub

1 則留言:

  1. 阿揚好,
    1.第4題的範圍判斷錯了吧?
    2.其它題雖然ok,請將第1題改用do while loop的方式改寫,因為第1題實在是比較適合用「條件迴圈」而不是「計次迴圈」。

    回覆刪除