需要帮助在Visual Basic上添加两个矩阵 [英] Need help adding two matrices on Visual Basic

查看:66
本文介绍了需要帮助在Visual Basic上添加两个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在为一个班级做一个项目,并且我试图添加两个矩阵.我设法通过按键制作矩阵,但是当我输入calculate函数的详细信息(cals_click)时,我找不到添加两个矩阵的方法(在设计中,这两个矩阵位于ListA和ListB框下).我该如何处理?

Hi there, I''m doing a project for one of my classes and I''m stuck trying to add two matrices. I managed to make the matrix via keypress, but when I input details for the calculate function (cals_click), I cannot find a way to add two matrices (which in design would be under ListA and ListB boxes). How do I approach this?

Private Sub Input_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Input.KeyPress
    Dim x1, x2, x3 As String
    Dim x23 As String
    Dim pos1, pos2 As Integer

    If Asc(e.KeyChar) = 13 Then
        pos1 = InStr(Input.Text, ";")
        x1 = Mid(Input.Text, 1, pos1 - 1)
        x23 = Mid(Input.Text, pos1 + 2)
        pos2 = InStr(x23, ";")
        x2 = Mid(x23, 1, pos2 - 1)
        x3 = Mid(x23, pos2 + 2)

        If MA.Checked Then
            ListA.Items.Add(x1)
            ListA.Items.Add(x2)
            ListA.Items.Add(x3)

        End If
        If MB.Checked Then
            ListB.Items.Add(x1)
            ListB.Items.Add(x2)
            ListB.Items.Add(x3)
        End If
        If MC.Checked Then
            ListC.Items.Add(x1)
            ListC.Items.Add(x2)
            ListC.Items.Add(x3)
        End If
        Input.Text = ""

    End If
End Sub

Private Sub midinstr(ByVal x123 As String, ByRef x1 As Single, ByRef x2 As Single, ByRef x3 As Single) 'byref = input, byval = output
    Dim x23 As String 'seperator
    Dim pos1, pos2 As Integer 'seperator

    pos1 = InStr(x123, ",")
    x1 = Val(Mid(x123, 1, pos1 - 1))
    x23 = Mid(x123, pos1 + 1)
    pos2 = InStr(x23, ",")
    x2 = Val(Mid(x23, 1, pos2 - 1))
    x3 = Val(Mid(x23, pos2 + 1))



End Sub


Private Sub Cals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cals.Click
    Dim A123, a2123, a3123 As String
    Dim a1, a2, a3 As Single
    Dim a21, a22, a23 As Single
    Dim a31, a32, a33 As Single
    Dim r As Double

    If DA.Checked Then
        A123 = ListA.Items(0)
        midinstr(A123, a1, a2, a3) 'from private sub below'
        a2123 = ListA.Items(1)
        midinstr(a2123, a21, a22, a23)
        a3123 = ListA.Items(2)
        midinstr(a3123, a31, a32, a33)

        r = a1 * (a22 * a33 - a23 * a32) - a2 * (a21 * a33 - a23 * a31) + a3 * (a21 * a32 - a22 * a31)

    End If

    If DB.Checked Then
        A123 = ListB.Items(0)
        midinstr(A123, a1, a2, a3) 'from private sub below'
        a2123 = ListB.Items(1)
        midinstr(a2123, a21, a22, a23)
        a3123 = ListB.Items(2)
        midinstr(a3123, a31, a32, a33)

        r = a1 * (a22 * a33 - a23 * a32) - a2 * (a21 * a33 - a23 * a31) + a3 * (a21 * a32 - a22 * a31)
    End If

    R_Val.Text = Str(r)

    If M1.Checked Then
        ListD.Items.Add()



    End If

End Sub

推荐答案

啊...典型的vb代码...(叹气)

您可能需要分离代码,而不是魔术按钮反模式( http://en.wikipedia.org/wiki/Magic_pushbutton [ ^ ]).

甚至没有深入探讨...按键处理程序中的代码可能在其余过程中都有一些非常关键的代码.列表项已添加到处理程序中,并在Cals_click中使用.

祝您好运!
Ah... typical vb code... (sigh)

You might want to separate the code instead of the magic pushbutton anti-pattern (http://en.wikipedia.org/wiki/Magic_pushbutton[^]).

Without even going into it to deep... the code in the keypress handler probably has some pretty critical code for the rest of the process. List items are added in the handler and used in Cals_click.

Good luck!


如果我有点理解所发布的代码,那么您正在使用字符串来存储矩阵项.请不要这样做:矩阵是数字的有序集合,因此您应该在程序中使用数字变量(的数组).这将在许多方面为您提供帮助.
If I understand a bit the posted code, you are using strings for storing matrix items. Please don''t do that: matrix is an ordered set of numbers hence you should use numeric variables (arrays of) in your program. This would help you in many ways.


这篇关于需要帮助在Visual Basic上添加两个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆