2016年11月5日 星期六

奇數卷
A1. 輸入 0 到 100 之間的成績 s,印出「優」「甲」「乙」「丙」「丁」五等第。限
用 Select Case 結構
Dim ans = ""
        Dim s As Single
        s = InputBox("s=", "", 10)
        Select Case s 
            Case Is < 60
                ans = "丁"
            Case Is < 70
                ans = "丙"
            Case Is < 80
                ans = "乙"
            Case Is < 90
                ans = "甲"
            Case Is <= 100
                ans = "優"
        End Select
        Me.TextBox1.Text = ans
A2. 求 2^2 + 4^2 + 6^2+ ⋯ + 100^2 之結果。
Dim ans = ""
        ans = 0
        For i = 2 To 100
            ans += i ^ 2
        Next
        Me.TextBox1.Text = ans

A3. 輸入整數 A, B, C,求一元二次方程式 AX^2 + BX + C = 0 的解。
Dim ans = ""
        Dim A As Single
        Dim B As Single
        Dim C As Single
        A = InputBox("A=", "", 1)
        B = InputBox("B=", "", 1)
        C = InputBox("C=", "", 1)
        Dim D = B ^ 2 - 4 * A * C
        If D > 0 Then
            ans = "有相異實根 " & (-B + (D) ^ 0.5) / (2 * A) & (-B - (D) ^ 0.5) / (2 * A)
        ElseIf D = 0 Then
            ans = "有重根 " & (-B) / (2 * A)
        Else : ans = "有相異虛根"
        End If
        Me.TextBox1.Text = ans

A4. 不換行印出 2.5 2 1.5 ... -2 -2.5。限用 Do while...loop
Dim X = ""
        Dim i = 2.5
        Do While i >= -2.5
            X &= i & "  "
            i -= 0.5
        Loop
        Me.TextBox1.Text = X

5. 輸入 x, y, z 三個邊長,若能構成三角形則印出計算此三角形面積;否則印出"非三角形"
Dim x, y, z As Single
        x = InputBox("x=", "", 1)
        y = InputBox("y=", "", 1)
        z = InputBox("z=", "", 1)
        Dim ans = ""
        Dim s = (x + y + z) / 2
        If x + y > z And x + z > y And y + z > x Then
            ans = "是三角形 面積為 " & (s * (s - x) * (s - y) * (s - z)) ^ 0.5
        Else : ans = "不是三角形"
        End If
        Me.TextBox1.Text = ans

A6. 限用 Do...Loop while 結構,由外部讀入整數數字,列印從 1 開始直到該數字
為止之直角三角形
Dim ans = ""
        Dim n As Integer

n = InputBox("n=", "", 1)
Dim w = 1
        Do
            Dim q = 1
            Do
                ans &= q & " "
                q += 1
            Loop While q <= w
            ans &= vbNewLine
            w += 1
        Loop While w <= n
        Me.TextBox1.Text = ans