我这里有一个程序可以解决表达式......
首先,我需要在文本框中输入表达式。它将存储在CharArray中,然后使用输入框将变量替换为整数...
我的问题是:如何将整数存储到数组中并存储操作?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arr() As Char = TextBox1.Text.ToCharArray
Dim aChar As Char
Dim a As String
Dim calc() as String
'Me.Height = Me.Height + 90
Me.Button1.Enabled = False
Me.TextBox1.Enabled = False
For i = 0 To TextBox1.Text.Length() - 1
aChar = arr.ElementAt(i)
If Char.IsLetter(aChar) Then
a = InputBox("Enter value for " & aChar, "Expression")
'Here what code?
' Try
' calc(i) = a
' Catch ex As Exception
' MsgBox("eee")
' End Try
'Else
End If
Next i
End Sub
答案 0 :(得分:0)
我通常更喜欢使用通用列表,它们可以更轻松地添加和删除项目。下面的代码应该满足您的需求:
Dim arr() As Char = TextBox1.Text.ToCharArray
Dim aChar As Char
Dim a As String
Dim calc As New List(Of String)
Me.Button1.Enabled = False
Me.TextBox1.Enabled = False
For i = 0 To TextBox1.Text.Length() - 1
aChar = arr.ElementAt(i)
If Char.IsLetter(aChar) Then
a = InputBox("Enter value for " & aChar, "Expression")
''//Add the result to the list
calc.Add(a)
Else
''//Add the operator to the list
calc.Add(aChar)
End If
Next i
''//If you want to convert to list to an array you can use this
Dim CalcArray = calc.ToArray()
答案 1 :(得分:0)
我没有测试过这个或者任何东西,但我认为重构这个有点使用For Each循环并且确定范围或摆脱a和aChar变量将是一个更好的方法:
Dim arr() As Char = TextBox1.Text.ToCharArray
Dim calc As New List(Of String)
Me.Button1.Enabled = False
Me.TextBox1.Enabled = False
For Each aChar As Char In arr
If Char.IsLetter(aChar) Then
''//Add the result to the list
calc.Add(InputBox(String.Format("Enter value for {0} Expression", aChar.ToString)))
Else
''//Add the operator to the list
calc.Add(aChar.ToString)
End If
Next
''//If you want to convert to list to an array you can use this
Dim CalcArray = calc.ToArray()