2017年1月20日 星期五

課本6-4-2 泡泡排序




Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim A() As Integer = {34, 67, 90, 87, 55, 62, 102, 23, 43, 53}
        Dim tempval, loop1, loop2, loop3 As Integer

        tempval = 0     '交換用的
        TextBox1.Text = "陣列傳統排序過程" & vbNewLine
        For loop1 = 0 To A.Length - 2             有 10 筆資料,比 10-1 回合: loop1 從0~10-2 有9個
            For loop2 = 1 To A.Length - (loop1 + 2)   剪掉一開頭相比的2個
                If A(loop2) > A(loop2 + 1) Then
                    tempval = A(loop2)
                    A(loop2) = A(loop2 + 1)
                    A(loop2 + 1) = tempval
                End If
            Next
            TextBox1.Text = TextBox1.Text & "第" & loop1 + 1 & "回"
            For loop3 = 0 To A.Length - 1              0~9有10個內容
                TextBox1.Text = TextBox1.Text & Format(A(loop3), " 00#")
            Next
            TextBox1.Text = TextBox1.Text & vbNewLine
        Next
    End Sub
End Class

2017年1月19日 星期四

課本5-2物件導向

要用overrides 時,父類別和子類別都要使用!!




'定義屬性
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim mycar1 As New car
        Dim mycar2 As New car
        mycar1.brand = "nissan"
        mycar1.cc = 2400
        mycar1.seats = 4
        mycar1.model = "X Trail"
        TextBox1.Text = mycar1.display()
        mycar2.brand = "Ford"
        mycar2.cc = 2300
        mycar2.model = "Escape"
        mycar2.seats = 5
        TextBox2.Text = mycar2.display
        Dim mytruck As New truck
        mytruck.brand = "Ford"
        mytruck.cc = 4600
        mytruck.loading = 11
        TextBox3.Text = mytruck.display
    End Sub
End Class
Public Class car
    Public brand As String
    Public model As String
    Public seats As Integer
    Public cc As Integer

    '定義方法
    Public Overridable Function display()
        Dim s As String
        s = "本車為" & brand & " 廠牌," & vbNewLine
        s = s & "車型為" & model & "," & vbNewLine
        s = s & seats & "人座," & cc & "CC"
        Return s
    End Function
End Class
Public Class truck
    Inherits car
    Public loading As Long
    Public Overrides Function display()
        Dim s As String = ""
        s = "本卡車為" & brand & "廠牌," & vbNewLine
        s = s & cc & "CC" & vbNewLine
        s = s & "目前載重量為" & loading & "ton" & vbNewLine
        If loading > 10 Then
        Else
            s = s & "符合規定"

        End If

        Return s
    End Function


End Class




5-10練習  Public Overloads Function X()


Public Class Form1
    Public Class mymath
        Public Overloads Function area(ByVal m1 As Single, ByVal m2 As Single)
            Return m1 * m2 / 2

        End Function
        Public Overloads Function area(ByVal m1 As Single)
            Return 3.14 * m1 * m1
        End Function
    End Class
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim math1 As New mymath
        Dim m1, m2 As Single
        m1 = Val(TextBox1.Text)
        m2 = Val(TextBox2.Text)
        If m2 = 0 Then
            TextBox3.Text = math1.area(m1)
        Else
            TextBox3.Text = math1.area(m1, m2)

        End If
    End Sub
End Class

2017年1月16日 星期一

課本_4-27~4-31 Timer

TIMER屬性:
Enabled:  True/Flase 是否啟動
Interval:   數字 1000=1秒

ex:
Now:               2017/1/16 下午 07:57:22
TimeOfDay:   下午 07:57:55
Today:             2017/1/16



4-29時鐘


.enabled =可不可以動



Public Class Form1

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Me.TextBox1.Text = Today
        Me.TextBox2.Text = Hour(Now)
        Me.TextBox3.Text = Minute(Now)
        Me.TextBox4.Text = Second(Now)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Timer1.Enabled = False Then
            Timer1.Enabled = True
            Button1.Text = "停止"
        Else
            Timer1.Enabled = False
            Button1.Text = "繼續"
        End If
    End Sub
End Class



4-30 紅綠燈


 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        '綠燈
        TextBox2.BackColor = Color.Yellow
        Timer2.Enabled = True
        Timer1.Enabled = False
        TextBox1.BackColor = Color.Silver
    End Sub


    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        '黃燈
        TextBox3.BackColor = Color.Red
        TextBox2.BackColor = Color.Silver
        Timer3.Enabled = True
        Timer2.Enabled = False

    End Sub

    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        '紅燈
        TextBox1.BackColor = Color.Green
        TextBox3.BackColor = Color.Silver
        Timer1.Enabled = True
        Timer3.Enabled = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.BackColor = Color.Green
        Timer1.Enabled = True
        Button1.Enabled = False
        Button2.Enabled = True
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        end
    End Sub

課本4-25~4-27




  Dim cnt, sum(10000) As Integer
        cnt = -1
        Dim ttl = 0
        Dim tmp = -1
        Dim max As Integer

        Do Until tmp = 0

            cnt += 1
            tmp = InputBox("輸入第" & cnt + 1 & "筆", "輸入對話框")
            ReDim Preserve sum(cnt)
            sum(cnt) = tmp
            ttl += sum(cnt)
        Loop
        Dim min = sum(0)
        For i = 0 To cnt - 1
            max = Math.Max(sum(i), sum(i + 1))
            min = Math.Min(sum(i), min)
        Next


        Me.TextBox4.Text = cnt
        Me.TextBox3.Text = (ttl / cnt)
        Me.TextBox5.Text = max
        Me.TextBox6.Text = min

2017年1月10日 星期二

氣泡排序法

重點:    有 n 筆資料,比 n-1 回合:第 i 回合:比 n-i 次

適用於數量小的資料
配合SWAP 模組

VB_fileopen 和 my.computer.filesystem.readalltext 的差別:

fileopen: 用input()可以個別讀入資料

my.computer.filesystem.readalltext:可以一次讀一整排資料,直到出現分隔符號

Q5 作法

自創:
 '.成績檔處理:計算所有學生的個人平均。
     
        Dim ans = "座號     平均" & vbNewLine
        FileOpen(1, "Q5.in", OpenMode.Input)
        Dim d(1000)
        Dim cnt = -1
        Do Until EOF(1)
            cnt += 1
            ReDim Preserve d(cnt)
            Input(1, d(cnt))
        Loop

        Dim no(1000) '座號
        Dim data(1000) '個人成績

        For i = 0 To UBound(d) Step 4
            ReDim Preserve no(i)
            no(i) = d(i)
        Next
        For i = 0 To UBound(no) Step 4
            ReDim Preserve data(i)
            data(i) = d(i + 1) + d(i + 2) + d(i + 3)
        Next
        For i = 0 To UBound(no) Step 4
            ans &= no(i) & "         " & Format(data(i) / 3, "##.0") & If(i = UBound(no), "", vbNewLine)
        Next
        Me.TextBox4.Text = ans
        FileClose()
        My.Computer.FileSystem.WriteAllText("Q5.out", ans, False)


老師教的:
  ' .成績檔處理:計算所有學生的個人平均。
        FileOpen(1, "Q5.in", OpenMode.Input)
        Dim no(1000) As String '座號
        Dim data1(1000) As Integer '成績
        Dim data2(1000) As Integer
        Dim data3(1000) As Integer
        Dim cnt As Integer = -1 '計數器
        Do Until EOF(1)
            cnt += 1
            ReDim Preserve no(cnt), data1(cnt), data2(cnt), data3(cnt) '動態宣告
            Input(1, no(cnt))
            Input(1, data1(cnt)) : Input(1, data2(cnt)) : Input(1, data3(cnt))

        Loop
        Dim ttl(1000) '總和
        For i = 0 To UBound(data1)
            ReDim Preserve ttl(i)
            ttl(i) += data1(i) + data2(i) + data3(i)
         
        Next

        Dim ans As String = "座號      平均" & vbNewLine
        For i = 0 To UBound(data1)
            ans &= no(i) & "     " & Format(ttl(i) / 3, "##.0") & If(i = UBound(data1), "", vbNewLine)

        Next
        Me.TextBox3.Text = ans
        FileClose()


兩個答案都一樣