⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 采用类平均法实现聚类分析的算法
💻 VB
📖 第 1 页 / 共 2 页
字号:
            Me.dataGrid1.DataSource = Me.myds.Tables(1)
            Me.dataGrid1.Refresh()
        End If
    End Sub

    Private Sub button9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button9.Click
        If Me.myds.Tables.Count < 3 Then
            MessageBox.Show("没有距离计算!")
            Return
        Else
            Me.dataGrid1.DataSource = Me.myds.Tables(2)
            Me.dataGrid1.Refresh()
        End If
    End Sub

    Private Sub button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        If Me.myds.Tables.Count < 3 Then
            MessageBox.Show("没有进行距离计算!")
            Return
        End If
        Try
            Me.richTextBox1.Text = ""
            Dim a As SortedList
            a = New SortedList
            Dim ClassCount As Int32 = Me.myds.Tables(0).Rows.Count - 1
            Dim RowCount As Int32 = Me.myds.Tables(0).Rows.Count
            Dim XY As Int32() = New Int32(RowCount - 1) {}
            Dim mini As Int32 = 0, minj As Int32 = 0, i As Int32, j As Int32, k As Int32
            For i = 0 To RowCount - 1
                XY(i) = i
                a.Add(i + 1, 1)
            Next

            Dim mind As Double, tempmind As Double = 0
            Me.Initjulimatrix()
            Me.Gramatrix = New Double(RowCount - 1, 2) {}
            Me.newClassCount = 0



            While RowCount > 1
                mind = 56767
                mini = 0
                minj = 1
                For i = 1 To RowCount - 1

                    For j = 0 To i - 1
                        If mind > Me.julimatrix(i, j) Then
                            '找出最小距离 
                            mind = Me.julimatrix(i, j)
                            mini = i
                            minj = j
                        End If
                    Next

                Next
                Dim tempstr As String
                If tempmind = mind Then
                    ClassCount += 1
                    a.Add(ClassCount + 1, a(mini) + a(minj))
                    tempstr = "将" + XY(mini).ToString + "类和" + XY(minj).ToString + "类合并成" + ClassCount.ToString + "类,合并距离为:" + mind.ToString + "" & Chr(10) & "" & Chr(10) & ""
                Else
                    '输出文字说明 
                    ClassCount += 1
                    a.Add(ClassCount + 1, a(mini) + a(minj))
                    tempstr = "将" + XY(mini).ToString + "类和" + XY(minj).ToString + "类合并成" + ClassCount.ToString + "类,合并距离为:" + mind.ToString + "" & Chr(10) & "" & Chr(10) & ""
                End If
                tempmind = mind
                Me.richTextBox1.Text += tempstr
                Me.Gramatrix(ClassCount - (Me.myds.Tables(0).Rows.Count - 1), 0) = XY(mini)
                '为画图阵增加数据 
                Me.Gramatrix(ClassCount - (Me.myds.Tables(0).Rows.Count - 1), 1) = XY(minj)
                Me.Gramatrix(ClassCount - (Me.myds.Tables(0).Rows.Count - 1), 2) = mind

                XY(mini) = ClassCount
                For k = minj To RowCount - 2
                    XY(k) = XY(k + 1)
                Next


                For j = 0 To RowCount - 1

                    Me.julimatrix(mini, j) = Math.Sqrt(Me.julimatrix(mini, j) * Me.julimatrix(mini, j) * a(mini) / (a(mini) + a(minj)) + Me.julimatrix(minj, j) * Me.julimatrix(minj, j) * a(minj) / (a(mini) + a(minj)))
                    Me.julimatrix(j, mini) = Me.julimatrix(mini, j)

                Next

                For i = minj To RowCount - 2
                    '删除minj行,列 
                    For j = 0 To RowCount - 1
                        Me.julimatrix(i, j) = Me.julimatrix(i + 1, j)
                        Me.julimatrix(j, i) = Me.julimatrix(i, j)

                    Next
                Next
                Me.newClassCount += 1
                RowCount -= 1

            End While

        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
        MessageBox.Show("计算完成!")
    End Sub

    Private Sub Initjulimatrix()
        Me.julimatrix = New Double(Me.myds.Tables(0).Rows.Count - 1, Me.myds.Tables(0).Rows.Count - 1) {}
        Dim i As Int32, j As Int32
        For i = 0 To Me.myds.Tables(0).Rows.Count - 1
            For j = 0 To Me.myds.Tables(0).Rows.Count - 1
                Me.julimatrix(i, j) = Convert.ToDouble(Me.myds.Tables(2).Rows(i)(j))
            Next
        Next
    End Sub
    Private Sub InitmyDraw()
        Dim myG As Graphics
        '= this.panel1.CreateGraphics(); 
        myG = Graphics.FromHwnd(Me.Panel1.Handle)
        Dim mypen As New Pen(Color.Red, 10)
        mypen.DashStyle = DashStyle.Solid
        myG.DrawLine(mypen, 10, 10, 100, 100)
        mypen.Dispose()
    End Sub

    Private Sub button10_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button10.Click
        Me.dataGrid1.AllowSorting = False
        Me.dataGrid1.Refresh()
        Me.dataGrid1.AllowSorting = True
    End Sub

    Private Sub panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        '画图 
        'Me.InitVarValuble()
        If Me.newClassCount < 1 Then
            Return
        End If
        Dim RowCount As Int32 = Me.myds.Tables(0).Rows.Count
        Me.Calzuobiao()
        Dim max_yy As Single = 0
        For i As Int32 = 0 To Me.newClassCount - 1
            max_yy = IIf(max_yy > Convert.ToSingle(Me.Gramatrix(i, 2)), max_yy, Convert.ToSingle(Me.Gramatrix(i, 2)))
        Next
        Dim temp1 As Single = Convert.ToSingle(Me.textBox2.Text)
        '左边宽 
        Dim temp2 As Single = Convert.ToSingle(Me.textBox3.Text)
        '上边宽 
        Dim temp3 As Single = Convert.ToSingle(Me.textBox4.Text)
        '间距倍数 
        Dim temp4 As Single = Convert.ToSingle(Me.textBox5.Text)
        '距离倍数 
        Dim myGa As Graphics = e.Graphics
        Dim mypen As New Pen(Color.Blue, 1)
        mypen.DashStyle = DashStyle.Solid

        myGa.DrawLine(mypen, Me.Panel1.Location.X + temp1, Me.Panel1.Location.Y + temp2 - 5, Me.Panel1.Location.X + temp1, Me.Panel1.Location.Y + temp3 * RowCount + 100)
        '画出坐标y 
        myGa.DrawLine(mypen, Me.Panel1.Location.X + temp1, Me.Panel1.Location.Y + temp3 * RowCount + 100, Me.Panel1.Location.X + temp1 - 3, Me.Panel1.Location.Y + temp3 * RowCount + 95)
        myGa.DrawLine(mypen, Me.Panel1.Location.X + temp1, Me.Panel1.Location.Y + temp3 * RowCount + 100, Me.Panel1.Location.X + temp1 + 3, Me.Panel1.Location.Y + temp3 * RowCount + 95)
        myGa.DrawLine(mypen, Me.Panel1.Location.X + temp1, Me.Panel1.Location.Y + temp2 - 5, Me.Panel1.Location.X + temp1 + temp4 * max_yy + 100, Me.Panel1.Location.Y + temp2 - 5)
        '画出坐标x 
        myGa.DrawLine(mypen, Me.Panel1.Location.X + temp1 + temp4 * max_yy + 100, Me.Panel1.Location.Y + temp2 - 5, Me.Panel1.Location.X + temp1 + temp4 * max_yy + 95, Me.Panel1.Location.Y + temp2 - 2)
        myGa.DrawLine(mypen, Me.Panel1.Location.X + temp1 + temp4 * max_yy + 100, Me.Panel1.Location.Y + temp2 - 5, Me.Panel1.Location.X + temp1 + temp4 * max_yy + 95, Me.Panel1.Location.Y + temp2 - 8)

        Me.label1.Location = New Point(Convert.ToInt32(temp4 * max_yy) + 100, Convert.ToInt32(temp3 * RowCount) + 100)
        mypen = New Pen(Color.Black, 1)
        Dim aVertialFont As New Font("Comic Sans Ms", 8, FontStyle.Italic, GraphicsUnit.Point)
        Dim aBrush As Brush = Brushes.Black
        Dim bBrush As Brush = Brushes.Red
        Dim x1 As Single, y1 As Single, x2 As Single, y2 As Single, x3 As Single, y3 As Single
        Dim t As Int32, t1 As Int32
        For i As Integer = 0 To Me.newClassCount - 1
            t = Convert.ToInt32(Me.Gramatrix(i, 0))
            t1 = Convert.ToInt32(Me.Gramatrix(i, 1))
            x1 = Me.Panel1.Top + temp1 + temp4 * Me.zuobiao(t, 0)
            y1 = Me.Panel1.Location.Y + temp2 + temp3 * Me.zuobiao(t, 1)
            x2 = Me.Panel1.Top + temp1 + temp4 * Me.zuobiao(i + RowCount, 0)
            y2 = Me.Panel1.Location.Y + temp2 + temp3 * Me.zuobiao(i + RowCount, 1)
            x3 = Me.Panel1.Top + temp1 + temp4 * Me.zuobiao(t1, 0)
            y3 = Me.Panel1.Location.Y + temp2 + temp3 * Me.zuobiao(t1, 1)
            myGa.DrawLine(mypen, x1, y1, x2, y1)
            '横线 
            myGa.DrawLine(mypen, x2, y1, x2, y3)
            '竖线 
            myGa.DrawLine(mypen, x3, y3, x2, y3)
            '横线 
            If t < RowCount Then
                myGa.DrawString(Me.myds.Tables(0).Rows(t)(0).ToString(), aVertialFont, aBrush, New Point(0, Convert.ToInt16(y1 - 5)))
            End If
            If t1 < RowCount Then
                myGa.DrawString(Me.myds.Tables(0).Rows(t1)(0).ToString(), aVertialFont, aBrush, New Point(0, Convert.ToInt16(y3 - 5)))
            End If
            myGa.DrawString(Me.zuobiao(RowCount + i, 0).ToString(), aVertialFont, bBrush, New Point(Convert.ToInt16(x2), Convert.ToInt16(y2)))
        Next
        mypen.Dispose()
        'aBrush.Dispose(); 
        aVertialFont.Dispose()
    End Sub
    Private Sub Calzuobiao()
        '计算坐标 
        Dim i As Int32
        Dim count As Int32 = 0
        Dim RowCount As Int32 = Me.myds.Tables(0).Rows.Count
        Me.zuobiao = New Single(RowCount + Me.newClassCount - 1, 1) {}
        For i = 0 To Me.newClassCount - 1
            If Me.Gramatrix(i, 0) < RowCount Then
                Me.zuobiao(Convert.ToInt32(Me.Gramatrix(i, 0)), 0) = 0


                Me.zuobiao(Convert.ToInt32(Me.Gramatrix(i, 0)), 1) = count
                count += 1
            End If
            If Me.Gramatrix(i, 1) < RowCount Then
                Me.zuobiao(Convert.ToInt32(Me.Gramatrix(i, 1)), 0) = 0
                Me.zuobiao(Convert.ToInt32(Me.Gramatrix(i, 1)), 1) = count
                count += 1
            End If
            Me.zuobiao(i + RowCount, 1) = CSng((Me.zuobiao(Convert.ToInt32(Me.Gramatrix(i, 0)), 1) + Me.zuobiao(Convert.ToInt32(Me.Gramatrix(i, 1)), 1))) / 2
            Me.zuobiao(i + RowCount, 0) = CSng((Me.Gramatrix(i, 2)))
        Next

    End Sub

    Private Sub button11_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button12.Click
        Me.Panel1.Invalidate()
    End Sub
    Private Sub InitVarValuble()
        'Me.newClassCount = 0
        'Me.zuobiao = Nothing
        Me.richTextBox1.Text = ""

        'this.coordinate_x=0; 
        'this.coordinate_y=0; 
        ' Me.panel1_Paint(pane)
    End Sub

    Private Sub jlfx2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -