簡體   English   中英

您可以在VB中使用數學方程式創建數組嗎?

[英]Can you create an array with mathematical equations in VB?

我已經嘗試了一段時間以創建一個充滿數學方程的鋸齒狀數組,例如a * b或(ab)/ c。 像這樣的方程式。 我已經創建了一個Windows窗體並在其中工作了相當長的時間,現在我想使代碼更緊湊。 我在這里和那里進行了調整,但在使用這些方程式組成數組時似乎很費勁。 到目前為止,我有:

Dim mathEqu()() As Object = {
{values(1) / values(15), values(2) - values(1), values(3) - values(4)},
{values(0) * values(15), values(2) - values(0), values(14) / values(7)},

等等

自然,此數組不起作用,並且由於我基本上是菜鳥,所以我看不到如何解決它。

順便說一下,“ values()”是一個數組,其中包含從文本框中獲取的16個不同值,並且缺少的值將計算並寫入包含“ 0”(用戶未觸摸)的文本框中。

有沒有辦法創建僅由方程式占據的數組?

請問,是否有什么需要幫助我的。

最后,我做了一個巨大的變通辦法,為values()和2個其他布爾數組的標志創建了一個布爾數組,每當mathEqu()中的一個方程可求解時(當一個方程中的所有值都可求解時)就設置一個標志/ expression不是0),另一個用於幫助設置標志(如我所說的,巨大的解決方法)。

這大致是數學在我的代碼中的工作方式:

Public Class NPNpositive

    Dim values(15) As Double
    Dim i = 0
    Dim j As Double = 0
    Dim k = 0
    Dim l = 0
    Dim p = 0

// Whenever you click the calculation-button

    Private Sub Calculation_Click(sender As Object, e As EventArgs) Handles Calculation.Click

        Dim tbArray() As TextBox = New TextBox() {IB, IC, IE, .. .., URC, hfe}
        Dim tbBool() As Boolean = New Boolean() {False, False, False, .. .., False, False}
        For Each number In values
            If i < 16 Then
                If Double.TryParse(tbArray(i).Text, j) Then
                    values(i) = j
                End If
            End If
            i += 1
            j = 0
        Next

        Dim mathBool As Boolean()() = {
            New Boolean() {False, False, False},
            New Boolean() {False, False, False},
            New Boolean() {False, False},
            ..,
            ..,
            New Boolean() {False, False}
            New Boolean() {False}}

Calculations:

        i = 0
        j = 0
        k = 0
        p = 0


// Raising a True-flag if the value in values(i) does not equal 0

        For Each n In values
            If Not values(i) = 0 Then
                tbBool(i) = True
            End If
            i += 1
        Next


// Raising a True-flag when an expression is do-able

        Dim mathBoolHelp As Boolean() = {
            tbBool(1) And tbBool(15),
            tbBool(2) And tbBool(1),
            tbBool(3) And tbBool(4),
            tbBool(0) And tbBool(15),
            tbBool(2) And tbBool(0),
            tbBool(14) And tbBool(7),
            tbBool(12) And tbBool(8),
            ..,
            ..,
            tbBool(11) And tbBool(10),
            tbBool(1) And tbBool(7),
            tbBool(1) And tbBool(0)}

// Here is where the calculations are happening

        Dim mathEqu As Double()() = {
            New Double() {values(1) / values(15), values(2) - values(1), values(3) - values(4)},
            New Double() {values(0) * vlaues(15), values(2) - values(0), values(14) / values(7)},
            New Double() {values(12) / values(8), values(0) + values(1)},
            ..,
            ..,
            New Double() {values(11) - values(10), values(1) * values(7)},
            New Double() {values(1) / values(0)}}

i = 0

// This is where things start to look interesting
// The mathBoolHelp puts in flags in the Boolean jagged Array

        For Each m in values
            j = mathBool(i).Length
            For value As Integer = 0 To j - 1
                If mathBoolHelp(p) = True Then
                    mathBool(i)(k) = True
                End If
                k += 1
                p += 1
            Next
            i += 1
            k = 0
        Next

        i = 0
        j = 0
        k = 0

// It checks now in the counter-Boolean Array for a True
// If it finds a true at dimension k, column i, in mathBool()(), it calculates
// the number from the same spot, just from mathEqu()() instead

        For Each m In values
            If values(i) = 0 Then
                j = mathEqu(i).Length
                For value As Integer = 0 To j - 1
                    If mathBool(i)(k) = True Then
                        values(i) = mathEqu(i)(k)
                    End If
                    If Not values(i) = 0 Then
                        GoTo forEnd
                    End If
                    k += 1
                Next
forEnd:
            End If
            i += 1
            k = 0
        Next

這只是整個程序的一小部分。 主要問題是僅在數組中該位置需要計算的兩個/所有值都不為0時才進行計算。我以前的代碼就是這樣工作的,但是它有16個長的If語句,其中長度。 而且看起來也不漂亮。

對於我的問題可能有更好的解決方案,所以在那之前,這將很好。

您可以采用回旋處的方式來做。

添加對您的程序的引用。 在引用窗口中,單擊“ Com”選項卡,然后搜索“ Microsoft Script Control 1.0”。選中其左側的框,然后單擊窗口底部的“確定”。

然后,您只需要定義一個簡單的公式字符串數組即可,例如..

Dim formula() As String = {"(a+b)/c", "(a^2+b^2)", "a^b"}

添加以下函數,該函數將使用字符串表達式以及該表達式的參數。 然后,它將公式中的字母替換為您提供的參數並評估結果表達式。 任務完成

Private Function EvaluateExpression(exp As String, Optional a As Double = 0, Optional b As Double = 0, Optional c As Double = 0) As Double
    Dim SC As New MSScriptControl.ScriptControl
    exp = exp.Replace("a", a.ToString)
    exp = exp.Replace("b", b.ToString)
    exp = exp.Replace("c", c.ToString)
    SC.Language = "VBSCRIPT"
    Dim Result As Double = Convert.ToDouble(SC.Eval(exp))
    Return Result
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM