Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
swap(Me.TextBox1.Text, Me.TextBox2.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'P11. 輸入三數,印出最大值。
Dim w, x, y As Single
w = InputBox("w = ", "輸入三數,印出最大值。", 0)
x = InputBox("x = ", "輸入三數,印出最大值。", 0)
y = InputBox("y = ", "輸入三數,印出最大值。", 0)
Me.TextBox3.Text = max(w, x, y) & " 最大 "
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'P12. 輸入四數,印出最大值及最小值。
Dim w, x, y, z As Single
w = InputBox("w = ", "輸入四數,印出最大值及最小值。", 0)
x = InputBox("x = ", "輸入四數,印出最大值及最小值。", 0)
y = InputBox("y = ", "輸入四數,印出最大值及最小值。", 0)
z = InputBox("z = ", "輸入四數,印出最大值及最小值。", 0)
Me.TextBox4.Text = max(w, x, y, z) & " 最大 " & " , " & min(w, x, y, z) & " 最小 "
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'P31. 輸入正整數 n,計算 n! 的結果。(提示: n! = 1 × 2 × 3 × ... × n)
Dim n As Single
Dim ans = ""
n = InputBox("輸入正整數 n", "輸入正整數 n,計算 n! 的結果。", 1)
For i = 1 To n
ans &= i & "!= " & Fac(i) & vbNewLine
Next
Me.TextBox5.Text = ans
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'P40.質數判斷
Dim n As Integer
Dim ans As String
n = InputBox("輸入n = ", "判斷是否質數", 1)
If Prime(n) Then
ans = n & " is a prime number."
Else
ans = n & " is not a prime number."
End If
Me.TextBox6.Text = ans
End Sub
End Class
模組:
Module _1110534013
'交換
Sub swap(ByRef a, ByRef b) 'call ByRefer 傳址
Dim t = a
a = b
b = t
End Sub
'最大值 , 多型
Function max(x, y)
Return If(x > y, x, y)
End Function
Function max(w, x, y)
Return max(w, max(x, y))
End Function
Function max(w, x, y, z)
Return max(max(w, x), max(y, z))
End Function
'最小值 , 多型
Function min(x, y)
Return If(x < y, x, y)
End Function
Function min(w, x, y)
Return min(w, min(x, y))
End Function
Function min(w, x, y, z)
Return min(min(w, x), min(y, z))
End Function
'P31. 輸入正整數 n,計算 n! 的結果。(提示: n! = 1 × 2 × 3 × ... × n)
Function Fac(n)
Dim p As Double = 1
For i = 1 To n
p *= i
Next
Return p
End Function
'P40.判斷是否質數
Function Prime(n) As Boolean
Dim c = 0
For i = 2 To n ^ 0.5
If n Mod i = 0 Then c += 1 'i是因數,則c+1
Next
Return If(c = 0, True, False)
End Function
End Module
結果:
沒有留言:
張貼留言