2013年11月18日 星期一
2009年6月15日 星期一
副程式例
'副程式例
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'氣泡排序
Dim a() = {95, 90, 98, 92, 96}
sortIt(a)
printIt(a)
Dim b() = {95, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87}
sortIt(b)
printIt(b)
Dim c() = {95, 90}
sortIt(c)
printIt(c)
Dim a() = {95, 90, 98, 92, 96}
selSortIt(a)
printIt(a)
Dim b() = {95, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87, 90, 98, 92, 96, 87}
selSortIt(b)
printIt(b)
Dim c() = {95, 90}
selSortIt(c)
printIt(c)
End Sub
Sub sortIt(ByVal a)
Dim i, j
For j = 0 To a.length - 1 - 1
For i = 0 To a.length - 1 - 1
If a(i) > a(i + 1) Then
change(a(i), a(i + 1))
End If
Next
Next
End Sub
Sub selSortIt(ByVal a)
Dim i, j
For j = 0 To a.length - 1 - 1
For i = j + 1 To a.length - 1
If a(j) > a(i) Then
change(a(j), a(i))
End If
Next
Next
End Sub
Sub change(ByRef x, ByRef y)
Dim t = x
x = y
y = t
End Sub
Sub printIt(ByVal a)
Dim i
Dim str1 = ""
For i = 0 To a.length - 1
str1 = str1 & a(i) & " "
Next
MsgBox(str1)
End Sub
End Class
2009年6月4日 星期四
電子通訊錄地圖版
Public Class Form1
Private Sub 聯絡資料BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 聯絡資料BindingNavigatorSaveItem.Click
Me.Validate()
Me.聯絡資料BindingSource.EndEdit()
Me.聯絡資料TableAdapter.Update(Me.Db1DataSet.聯絡資料)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: 這行程式碼會將資料載入 'Db1DataSet.聯絡資料' 資料表。您可以視需要進行移動或移除。
Me.聯絡資料TableAdapter.Fill(Me.Db1DataSet.聯絡資料)
Label1.Text = "點按圖形可改變圖片大小"
Label1.ForeColor = Color.Red
End Sub
Private Sub 地圖PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 地圖PictureBox.Click
If 地圖PictureBox.SizeMode = PictureBoxSizeMode.CenterImage Then
地圖PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
Else
地圖PictureBox.SizeMode = PictureBoxSizeMode.CenterImage
End If
End Sub
End Class
2009年5月19日 星期二
多行資料求平均及最大最小值
'多行資料求平均及最大最小值
Public Class Form1
Dim i, s
Dim c(60) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText("C:\d1.txt")
'取代分行符號為空白間隔
fileContents = Replace(fileContents, vbNewLine, " ")
'去檔案後面空白
fileContents = Trim(fileContents)
Dim c1() = Split(fileContents, " ")
For i = 0 To UBound(c1)
c(i) = Val(c1(i))
Next
Dim cMax = c(0)
Dim cMin = c(0)
For i = 0 To UBound(c1)
If c(i) > cMax Then cMax = c(i)
If c(i) < cMin Then cMin = c(i)
s = s + c(i)
Next
Dim str1 = "最大:" & cMax & vbNewLine & "最小:" & cMin & vbNewLine & "平均:" & _
Int(s / (UBound(c1) + 1) * 10 + 0.5) / 10 & vbNewLine & "共" & UBound(c1) + 1 & "筆"
MsgBox(str1)
' My.Computer.FileSystem.WriteAllText("C:\d2.txt", str1, True)
End
End Sub
End Class
2009年5月18日 星期一
讀入多筆不定長度資料算平均
'讀入多筆不定長度資料算平均
Public Class Form1
Dim i, j
Dim s
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText("f:\T1.txt")
Dim r() = Split(fileContents, vbNewLine)
Dim n = 0
For j = 0 To UBound(r)
If r(j) <> "" Then
Dim c() = Split(r(j), " ")
For i = 0 To UBound(c)
n = n + 1
s = s + Val(c(i))
Next
End If
Next
MsgBox("共" & n & "筆,平均:" & Int(s / n + 0.5))
End Sub
End Class
2009年5月5日 星期二
求質因數
'求質因數
Public Class Form1
Dim i, j, k
Dim str1, str2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim n() = {24, 36, 55, 110, 2300}
For k = 0 To n.Length - 1
str1 = n(k) & "->"
For i = 1 To n(k)
If n(k) Mod i = 0 Then
Dim c = 0
For j = 1 To i
If i Mod j = 0 Then c = c + 1
Next
If c = 2 Then str1 = str1 & i & Space(3)
End If
Next
str2 = str2 & str1 & vbNewLine
Next
MsgBox(str2)
End Sub
End Class
2009年5月4日 星期一
字數統計
'1算多少個半形字母含空白 38
'2算多少個英文字母不含空白,含標點符號 30
'3算多少個英文字母不含空白,不含標點符號 28
'4算多少個英文字 9
'5算有多少個e 6
'6統計分別各有多少個字母 W: 1 h: 3 e: 6 ....
Public Class Form1
Dim i, c, j
Dim str1 = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s = " Where there is a will, there is a way."
'ans1
'MsgBox(s.length)
'ans2
'For i = 1 To s.length
' If Mid(s, i, 1) <> " " Then c = c + 1
'Next
'MsgBox(c)
'ans3
'For i = 1 To s.length
' If Mid(s, i, 1) <> " " And Mid(s, i, 1) <> "," And Mid(s, i, 1) <> "." Then c = c + 1
'Next
'MsgBox(c)
'ans4
'For i = 2 To s.length
' If Mid(s, i, 1) = " " And Mid(s, i - 1, 1) <> " " Then c = c + 1
'Next
'MsgBox(c + 1)
'ans5
'For i = 1 To s.length
' If Mid(s, i, 1) = "e" Then c = c + 1
'Next
'MsgBox(c)
'ans6
For j = 1 To s.length
If InStr(str1, Mid(s, j, 1)) = 0 Then
c = 0
For i = 1 To s.length
If Mid(s, i, 1) = Mid(s, j, 1) Then c = c + 1
Next
str1 = str1 & Mid(s, j, 1) & " : " & c & vbNewLine
End If
Next
MsgBox(str1)
End Sub
End Class