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
結果:
2016年11月20日 星期日
2016年11月13日 星期日
1028
'p17 輸入 A, B, C,求一元二次方程式 AXଶ + BX + C = 0 的解。 if
Dim A, B, C As Single
A = Me.TextBox2.Text
B = Me.TextBox3.Text
C = Me.TextBox4.Text
Dim ans As String = ""
Dim d As Single = (B ^ 2 - 4 * A * C)
If d > 0 Then
ans = "有相異實根 , " & "x = " & (-1 * B + d ^ 0.5) / (2 * A) & " , " & (-1 * B - d ^ 0.5) / (2 * A)
ElseIf d = 0 Then
ans = "有重根 , " & "x = " & (-1 * B) / (2 * A)
Else
ans = "有相異虛根 , " & "此方程式不存在"
End If
Me.TextBox1.Text = ans
'p17 輸入 A, B, C,求一元二次方程式 AXଶ + BX + C = 0 的解。 select
Dim A, B, C As Single
A = Me.TextBox7.Text
B = Me.TextBox6.Text
C = Me.TextBox5.Text
Dim ans As String = ""
Dim d As Single = (B ^ 2 - 4 * A * C)
Select Case d
Case Is > 0
ans = "有相異實根 , " & "x = " & (-1 * B + d ^ 0.5) / (2 * A) & " , " & (-1 * B - d ^ 0.5) / (2 * A)
Case Is = 0
ans = "有重根 , " & "x = " & (-1 * B) / (2 * A)
Case Else
ans = "有相異虛根 , " & "此方程式不存在"
End Select
Me.TextBox8.Text = ans
' 9x9
Dim ans = ""
For j = 1 To 9 ' 9列
For i = 1 To 9 '1...9
ans &= RSet(i * j, 4) '向右靠對齊字串
Next
ans &= vbNewLine ' 換行
Next
Me.TextBox9.Text = ans
' 9x9
Dim ans = ""
For j = 1 To 9 ' 9列
For i = 1 To 9 '1...9
ans &= j & "x" & i & "=" & RSet(i * j, 2) & " " '向右靠對齊字串
Next
ans &= vbNewLine ' 換行
Next
Me.TextBox10.Text = ans
' 9x9
Dim ans = ""
For j = 1 To 9 ' 9列
For i = 1 To j '1...9
ans &= RSet(i * j, 4) '向右靠對齊字串
Next
ans &= vbNewLine ' 換行
Next
Me.TextBox11.Text = ans
'p39. 【直角三角形列印:軟體設計丙級】利用廻圈控制指令,由外部讀入整數數字,列印從 1 開始直到該數字為止之直角三角形。
Dim n As Integer
Dim ans = ""
n = InputBox("n = ", "p39【直角三角形列印:軟體設計丙級】利用廻圈控制指令,由外部讀入整數數字,列印從 1 開始直到該數字為止之直角三角形。", 4)
For j = 1 To n
For i = 1 To j
ans &= i
Next
ans &= vbNewLine ' 換行
Next
Me.TextBox12.Text = ans
'p21.P22 輸入正整數 n,求 1 + 2 + ....+ n
Dim n As Integer
Dim sum = 0
n = InputBox("輸入正整數 n", "輸入正整數 n,求 1 + 2 + ....+ n")
For x = 1 To n
sum += x
Next
Me.TextBox13.Text = sum
'p25. 輸入正整數 n,求 1 ^ 2 + 2 ^ 2 +....n ^ 2
Dim n As Integer
Dim sum = 0
n = InputBox("輸入正整數 n", "輸入正整數 n,求 1 ^ 2 + 2 ^ 2 +....n ^ 2")
For i = 1 To n Step 1
sum += i ^ 2
Next
Me.TextBox14.Text = sum
'p24. 輸入正整數 n,求 1 + 3 + ⋯ + (2n + 1) 之結果。
Dim n As Integer
Dim sum = 0
n = InputBox("輸入正整數 n", "輸入正整數 n,求 1 + 3 + ⋯ + (2n + 1) 之結果。")
For i = 1 To 2 * n + 1 Step 2
sum += i
Next
Me.TextBox15.Text = sum
'p26. 輸入正整數 n,求1 ^ 2 - 2 ^ 2 + 3 ^ 2 .... n ^ 2 之結果。
Dim n As Integer
Dim sum = 0
n = InputBox("輸入正整數 n", "輸入正整數 n,求1 ^ 2 - 2 ^ 2 + 3 ^ 2 .... n ^ 2之結果。")
For i = 1 To n Step 1
sum += If(i Mod 2 = 0, -i ^ 2, i ^ 2)
Next
Me.TextBox16.Text = sum
'p27. 輸入正整數 n,求(1 - 1 / 2)(1 - 1 / 3)... (1 - 1 / n)之結果。
Dim n As Integer
Dim sum As Single = 0
n = InputBox("輸入正整數 n", "輸入正整數 n,求(1 - 1 / 2)(1 - 1 / 3)... (1 - 1 / n)之結果。")
sum = 1
For i = 2 To n Step 1
sum *= (1 - 1 / i)
Next
Me.TextBox17.Text = sum
'p28. 輸入正整數 n,求 = 1 - 1 / 2 + 1 / 3 - 1 / 4 .... 之結果。
Dim n As Integer
Dim sum As Single = 0
n = InputBox("輸入正整數 n", "輸入正整數 n,求 = 1 - 1 / 2 + 1 / 3 - 1 / 4 .... 之結果。")
sum = 1
For i = 2 To n Step 1
sum += If(i Mod 2 = 0, -1 / i, 1 / i)
Next
Me.TextBox18.Text = sum
1021
'14. 輸入西元年 y,判斷 y 是「閏年」還是「平年」?
'p14. 輸入西元年 y,
Dim y As Integer
Dim ans As String = ""
y = InputBox("西元年 = ", "輸入西元年 y,判斷 y 是「閏年」還是「平年」", 0)
'判斷 y 是「閏年」還是「平年」?
If (y Mod 400 = 0) Or (y Mod 4 = 0 And y Mod 100 <> 0) Then
ans = y & "是閏年"
Else
ans = y & "是平年"
End If
Me.TextBox1.Text = ans
'16. 輸入 a, b, c 三個邊長,判斷是否能構成三角形? 若能構成三角形,則計算此三角形面積。
'16. 輸入 a, b, c 三個邊長
Dim a, b, c As Single
Dim ans As String = ""
a = InputBox("a =", "判斷是否能構成三角形?", 0)
b = InputBox("b =", "判斷是否能構成三角形?", 0)
c = InputBox("c =", "判斷是否能構成三角形?", 0)
'判斷是否能構成三角形?
If a + b > c And a + c > b And b + c > a Then
Dim s As Single = (a + b + c) / 2
ans = "三角形面積 = " & (s * (s - a) * (s - b) * (s - c)) ^ 0.5
Else
ans = "不是三角形"
End If
Me.TextBox2.Text = ans
'輸入初值、終值、增量,印出迴圈控制變數值的 印出迴圈控制變數值的變化和執行次數()
Dim _start, _stop, _step '初值、終值、增量
_start = InputBox("_start =", "印出迴圈控制變數值的變化和執行次數")
_stop = InputBox("_stop =", "印出迴圈控制變數值的變化和執行次數")
_step = InputBox("_step =", "印出迴圈控制變數值的變化和執行次數")
Dim ans = "For i = " & _start & " To " & _stop & " Step " & _step & vbNewLine & _
"Next" & vbNewLine & _
"迴圈內 i = " & vbNewLine
Dim cnt = 0 'counter 計數器
For i = _start To _stop Step _step
cnt += 1
ans &= i & ","
Next
Me.TextBox5.Text = ans & vbNewLine & "====For...Next迴圈執行" & cnt & "次===="
'18. 解二方一次方程式 ax + by = m
' cx + dy = n 。輸入數值 a, b, m, c, d, n,則:
Dim a, b, m, c, d, n As Integer
Dim ans = ""
a = InputBox("二方一次方程式 ax + by = m " & vbNewLine & _
" cx + dy = n" & vbNewLine & _
"a = ", "二方一次方程式", 1)
b = InputBox("二方一次方程式 ax + by = m " & vbNewLine & _
" cx + dy = n" & vbNewLine & _
"b = ", "二方一次方程式", 1)
m = InputBox("二方一次方程式 ax + by = m " & vbNewLine & _
" cx + dy = n" & vbNewLine & _
"m = ", "二方一次方程式", 1)
c = InputBox("二方一次方程式 ax + by = m " & vbNewLine & _
" cx + dy = n" & vbNewLine & _
"c = ", "二方一次方程式", 1)
d = InputBox("二方一次方程式 ax + by = m " & vbNewLine & _
" cx + dy = n" & vbNewLine & _
"d = ", "二方一次方程式", 1)
n = InputBox("二方一次方程式 ax + by = m " & vbNewLine & _
" cx + dy = n" & vbNewLine & _
"n = ", "二方一次方程式", 1)
If a * d - b * c <> 0 Then
ans &= "x = " & (m * d - n * b) / (a * d - b * c)
ans &= " , y = " & (a * n - c * m) / (a * d - b * c)
ElseIf b * n - m * d <> 0 Then
ans = "無解"
Else
ans = "無限多組解"
End If
Me.TextBox6.Text = ans
程設小考 奇數卷
P1. 輸入數值A、B,輸出A-B之「絕對值」? If
Dim A, B As Single
Dim ans = ""
A = InputBox("輸入A=", "求A-B的絕對值", 0)
B = InputBox("輸入B=", "求A-B的絕對值", 0)
If A - B > 0 Then
ans = A - B
Else
ans = -(A - B)
End If
Me.TextBox1.Text = ans
P2. 輸入數值A、B,輸出A-B之「絕對值」? Select
Dim A, B As Single
Dim ans = ""
A = InputBox("輸入A=", "求A-B的絕對值", 0)
B = InputBox("輸入B=", "求A-B的絕對值", 0)
Select Case A - B
Case Is > 0
ans = A - B
Case Else
ans = -(A - B)
End Select
Me.TextBox2.Text = ans
P3.印出九九乘法表。 For
Dim ans = ""
For j = 1 To 9
For i = 1 To 9
ans &= j & "x" & i & "=" & RSet(i * j, 2) & " "
Next
ans &= vbNewLine
Next
Me.TextBox3.Text = ans
P5.輸入西元年 y,判斷 y 是「閏年」還是「平年」?
Dim y As Integer
Dim ans As String = ""
y = InputBox("西元年 = ", "輸入西元年 y,判斷 y 是「閏年」還是「平年」", 0)
If (y Mod 400 = 0) Or (y Mod 4 = 0 And y Mod 100 <> 0) Then
ans = y & "是閏年"
Else
ans = y & "是平年"
End If
Me.TextBox4.Text = ans
P6.不換行印出 -20 -24 -28 ... -44 -48 If
Dim ans As String = " "
For i = -20 To -48 Step -4
ans &= i & " , "
Next
Me.TextBox5.Text = ans
Dim A, B As Single
Dim ans = ""
A = InputBox("輸入A=", "求A-B的絕對值", 0)
B = InputBox("輸入B=", "求A-B的絕對值", 0)
If A - B > 0 Then
ans = A - B
Else
ans = -(A - B)
End If
Me.TextBox1.Text = ans
P2. 輸入數值A、B,輸出A-B之「絕對值」? Select
Dim A, B As Single
Dim ans = ""
A = InputBox("輸入A=", "求A-B的絕對值", 0)
B = InputBox("輸入B=", "求A-B的絕對值", 0)
Select Case A - B
Case Is > 0
ans = A - B
Case Else
ans = -(A - B)
End Select
Me.TextBox2.Text = ans
P3.印出九九乘法表。 For
Dim ans = ""
For j = 1 To 9
For i = 1 To 9
ans &= j & "x" & i & "=" & RSet(i * j, 2) & " "
Next
ans &= vbNewLine
Next
Me.TextBox3.Text = ans
P5.輸入西元年 y,判斷 y 是「閏年」還是「平年」?
Dim y As Integer
Dim ans As String = ""
y = InputBox("西元年 = ", "輸入西元年 y,判斷 y 是「閏年」還是「平年」", 0)
If (y Mod 400 = 0) Or (y Mod 4 = 0 And y Mod 100 <> 0) Then
ans = y & "是閏年"
Else
ans = y & "是平年"
End If
Me.TextBox4.Text = ans
P6.不換行印出 -20 -24 -28 ... -44 -48 If
Dim ans As String = " "
For i = -20 To -48 Step -4
ans &= i & " , "
Next
Me.TextBox5.Text = ans
習題練習
A1.印出等第
Dim a As Single
Dim s As String
a = InputBox("輸入a=", "成績", 0)
Select Case a
Case Is >= 90
s = "優"
Case Is >= 80
s = "甲"
Case Is >= 70
s = "乙"
Case Is >= 60
s = "丙"
Case Else
s = "丁"
End Select
Me.TextBox1.Text = s
A2.求 2^2 + 4^2 +..... + 100^2 結果
Dim sum = 0
For i = 2 To 100 Step 2
sum += i ^ 2
Next
Me.TextBox2.Text = sum
A3.一元二次方程式
Dim a, b, c As Integer
Dim ans = ""
a = InputBox("輸入a=", "求解", 0)
b = InputBox("輸入b=", "求解", 0)
c = InputBox("輸入c=", "求解", 0)
Dim d As Single = (b ^ 2 - 4 * a * c)
If d > 0 Then
ans &= "有相異實根" & "X = " & (-1 * b + d ^ 0.5) / (2 * a) & " , " & (-1 * b - d ^ 0.5) / (2 * a)
ElseIf d = 0 Then
ans = "有重根 , " & "x = " & (-1 * b) / (2 * a)
Else
ans = "有相異虛根 , " & "此方程式不存在"
End If
Me.TextBox3.Text = ans
A5. 三角形
Dim a, b, c As Single
Dim ans As String = ""
a = InputBox("a =", "判斷是否能構成三角形?", 0)
b = InputBox("b =", "判斷是否能構成三角形?", 0)
c = InputBox("c =", "判斷是否能構成三角形?", 0)
If a + b > c And a + c > b And b + c > a Then
Dim s As Single = (a + b + c) / 2
ans = "三角形面積 = " & (s * (s - a) * (s - b) * (s - c)) ^ 0.5
Else
ans = "非三角形"
End If
Me.TextBox5.Text = ans
Dim a As Single
Dim s As String
a = InputBox("輸入a=", "成績", 0)
Select Case a
Case Is >= 90
s = "優"
Case Is >= 80
s = "甲"
Case Is >= 70
s = "乙"
Case Is >= 60
s = "丙"
Case Else
s = "丁"
End Select
Me.TextBox1.Text = s
A2.求 2^2 + 4^2 +..... + 100^2 結果
Dim sum = 0
For i = 2 To 100 Step 2
sum += i ^ 2
Next
Me.TextBox2.Text = sum
A3.一元二次方程式
Dim a, b, c As Integer
Dim ans = ""
a = InputBox("輸入a=", "求解", 0)
b = InputBox("輸入b=", "求解", 0)
c = InputBox("輸入c=", "求解", 0)
Dim d As Single = (b ^ 2 - 4 * a * c)
If d > 0 Then
ans &= "有相異實根" & "X = " & (-1 * b + d ^ 0.5) / (2 * a) & " , " & (-1 * b - d ^ 0.5) / (2 * a)
ElseIf d = 0 Then
ans = "有重根 , " & "x = " & (-1 * b) / (2 * a)
Else
ans = "有相異虛根 , " & "此方程式不存在"
End If
Me.TextBox3.Text = ans
A5. 三角形
Dim a, b, c As Single
Dim ans As String = ""
a = InputBox("a =", "判斷是否能構成三角形?", 0)
b = InputBox("b =", "判斷是否能構成三角形?", 0)
c = InputBox("c =", "判斷是否能構成三角形?", 0)
If a + b > c And a + c > b And b + c > a Then
Dim s As Single = (a + b + c) / 2
ans = "三角形面積 = " & (s * (s - a) * (s - b) * (s - c)) ^ 0.5
Else
ans = "非三角形"
End If
Me.TextBox5.Text = ans
10/14 程式設計 學習單 單元:ch03. 決策指令
10.輸入 A、B、C 三個數,輸出最大值。想出三種不同的結構來完成。
作法 1:
'輸入 A、B、C 三個數,輸出最大值。
Dim a, b, c As Single
Dim ans As String = "" '答案
a = InputBox("a=", "輸入 A、B、C 三個數,輸出最大值。", 0)
b = InputBox("b=", "輸入 A、B、C 三個數,輸出最大值。", 0)
c = InputBox("c=", "輸入 A、B、C 三個數,輸出最大值。", 0)
ans = If(a > c, If(a > b, a, b), If(c > b, c, b)) & "大"
Me.TextBox4.Text = ans
2. '輸入 A、B、C 三個數,輸出最大值。想出三種不同的結構來完成。(1)
'輸入 A、B、C 三個數,輸出最大值。
Dim a, b, c As Single
Dim ans As String = "" '答案
a = InputBox("a=", "輸入 A、B、C 三個數,輸出最大值。", 0)
b = InputBox("b=", "輸入 A、B、C 三個數,輸出最大值。", 0)
c = InputBox("c=", "輸入 A、B、C 三個數,輸出最大值。", 0)
If a > b Then
If a > c Then
ans = a & "大"
Else
ans = c & "大"
End If
Else
If b > c Then
ans = b & "大"
Else
ans = c & "大"
End If
End If
Me.TextBox1.Text = ans
3. '輸入 A、B、C 三個數,輸出最大值。想出三種不同的結構來完成。(2)
'輸入 A、B、C 三個數,輸出最大值。
Dim a, b, c As Single
Dim ans As String = "" '答案
a = InputBox("a=", "輸入 A、B、C 三個數,輸出最大值。", 0)
b = InputBox("b=", "輸入 A、B、C 三個數,輸出最大值。", 0)
c = InputBox("c=", "輸入 A、B、C 三個數,輸出最大值。", 0)
If b > c Then
If b > a Then
ans = b & "大"
Else
ans = a & "大"
End If
Else
If c > a Then
ans = c & "大"
Else
ans = a & "大"
End If
End If
Me.TextBox2.Text = ans
'輸入 A、B、C 三個數,輸出最大值。想出三種不同的結構來完成。(1)
'輸入 A、B、C 三個數,輸出最大值。
Dim a, b, c As Single
Dim ans As String = "" '答案
a = InputBox("a=", "輸入 A、B、C 三個數,輸出最大值。", 0)
b = InputBox("b=", "輸入 A、B、C 三個數,輸出最大值。", 0)
c = InputBox("c=", "輸入 A、B、C 三個數,輸出最大值。", 0)
If a > c Then
If a > b Then
ans = a & "大"
Else
ans = b & "大"
End If
Else
If c > b Then
ans = c & "大"
Else
ans = b & "大"
End If
End If
Me.TextBox3.Text = ans
Dim ut As Integer '使用時間
Dim s As Long = 0 '費用
Dim ans As String = ""
ut = InputBox("使用時間=", "手機費用計算", 0)
If ut > 300 Then
s = 1350
ElseIf ut > 100 Thens = 200 + 70 * 5 + (ut - 100) * 4
ElseIf ut > 30 Then
s = 200 + (ut - 30) * 5
Else
s = 200
End If
Me.TextBox5.Text = ut & "分鐘 , 費用 = " & s
'寫一個手通信費計算的程式
Dim ut As Single '使用時間
Dim s As String = 0 '費用
Dim ans As String = ""
ut = InputBox("使用時間=", "手機費用計算", 0)
Select Case ut
Case Is > 300
s = 1350
Case Is > 100
s = 200 + 70 * 5 + (ut - 100) * 4
Case Is > 30
s = 200 + (ut - 30) * 5
Case Else
s = 200
End Select
Me.TextBox6.Text = ut & "分鐘 , 費用 = " & s
'成績等第判斷:操行成績0-100分成五等第
Dim a As Single '成績
Dim b As String = "" '等第
a = InputBox("成績=", "'成績等第判斷:操行成績0-100分成五等第", 0)
If a >= 90 Then
b = "優"
ElseIf a >= 80 Then
b = "甲"
ElseIf a >= 70 Then
b = "乙"
ElseIf a >= 60 Then
b = "丙"
Else
b = "丁"
End If
Me.TextBox7.Text = a & "成績 , 等第 = " & b
'成績等第判斷:操行成績0-100分成五等第
Dim a As Single '成績
Dim b As String = "" '等第
a = InputBox("成績=", "'成績等第判斷:操行成績0-100分成五等第", 0)
Select Case a
Case Is >= 90
b = "優"
Case Is >= 80
b = "甲"
Case Is >= 70
b = "乙"
Case Is >= 60
b = "丙"
Case Else
b = "丁"
End Select
Me.TextBox8.Text = a & "成績 , 等第 = " & b
訂閱:
文章 (Atom)