与往常一样,我得到了忘乎所以的实施可能的解决方案,所以让我们现在先用碱性溶液中,然后用测试场景:)
分析文本的“全面”解决方案,您将需要确定:
- 您的输入有效
- 如果它们是正数或负数
- 哪里是A,B的位置,C
一个可能的解决方案可能是以下(我敢肯定有还有更多的方法可以做到这一点,并且我对+/-的解析不是100%满意,但它确实有效,并让你知道如何解析文本)。
注意,这仅仅是类的解决方法,全面过火的实施遵循以下
当不能得到解决,此方法将抛出错误,所以,无论是附上尝试在此方法中/ Catch块或使用的TryParse方法,你可以在下面找到:)
Public Overrides Sub Resolve(input As String)
' lets say 2x^2 + 4x - 4 = 0
' lets say 5x^2 + 3x + 1 = 0
If String.IsNullOrWhiteSpace(input) Then
Throw New ArgumentException("Input string cannot be null", "input")
End If
If input.IndexOf("=") >= 0 Then
input = input.Split("=")(0).Trim()
If String.IsNullOrWhiteSpace(input) Then
Throw New FormatException("'=' is at the beginning of the equation")
End If
End If
If input.Contains(" ") Then
input = input.Replace(" ", "")
End If
Dim lstGroup As New List(Of String)
Dim position As Integer = 0
Dim delimiters() As String = {"+", "-"}
Dim minIndex As Integer
Dim curDel As String
Dim lastDel As String = "+"
Dim part As String
While position < input.Length
minIndex = input.Length
curDel = "+"
For Each del In delimiters
Dim targetIndex As Integer = input.IndexOf(del, position)
If targetIndex > 0 AndAlso targetIndex < minIndex Then
minIndex = targetIndex
curDel = del
End If
Next
part = input.Substring(position, minIndex - position)
lstGroup.Add(lastDel + part.ToLower())
position = minIndex + 1
lastDel = curDel
End While
CoefficientA = 0
CoefficientB = 0
CoefficientC = 0
For Each group In lstGroup
If group.Contains("x^2") Then
If CoefficientA <> 0 Then
Throw New FormatException("'x^2' was already determined!")
End If
If Not Integer.TryParse(group.Replace("x^2", ""), CoefficientA) Then
' it is there so set it to 1
CoefficientA = 1
End If
Continue For
End If
If group.Contains("x") Then
If CoefficientB <> 0 Then
Throw New FormatException("'x' was already determined!")
End If
If Not Integer.TryParse(group.Replace("x", ""), CoefficientB) Then
CoefficientB = 1
End If
Continue For
End If
If CoefficientC <> 0 Then
Throw New FormatException("CoefficientC was already determined!")
End If
CoefficientC = Convert.ToInt32(group)
Next
End Sub
上述方法将设置你的职业等级,然后你可以解决所有必要的真实的值,并显示你觉得应该文本(见测试用例场景)
正如我所提到的那样,它在顶层实现方面有点过分,但如果你有更多的这个方程需要解决,你可以采用更灵活的方法来解析你的新类(它们只需要一个Solve/Resolve方法它可以节省你在未来的一些可能的麻烦:d)
走,接口IEquation,具有解决和解决方法,以及解决参数
Public Interface IEquation
Sub Resolve(input As String)
Sub Solve()
ReadOnly Property Solved As Boolean
End Interface
而那类的为MustInherit实施
''' <summary>Abstract implementation of IEquation, offers TryParse & Parse methods,that in their turn refer to the IEquation.Resolve() to do the actual parsing</summary>
''' <remarks>Any implementation of IEquation must have a paramless constructor</remarks>
Public MustInherit Class Equation
Implements IEquation
Public Shared Function TryParse(Of T As {New, IEquation})(input As String, ByRef equation As T) As Boolean
Dim succeeded As Boolean = True
Try
If String.IsNullOrWhiteSpace(input) Then
Throw New ArgumentException("Input string cannot be empty or nothing!", "input")
End If
equation = Parse(Of T)(input)
Catch ex As Exception
equation = Nothing
succeeded = False
End Try
Return succeeded
End Function
Public Shared Function Parse(Of T As {New, IEquation})(input As String) As T
Dim equation As New T()
equation.Resolve(input)
Return equation
End Function
Private _solved As Boolean = False
Public Property Solved As Boolean
Get
Return _solved
End Get
Protected Set(value As Boolean)
_solved = value
End Set
End Property
Public ReadOnly Property SolvedExplicit As Boolean Implements IEquation.Solved
Get
Return Solved
End Get
End Property
Public MustOverride Sub Resolve(input As String) Implements IEquation.Resolve
Public MustOverride Sub Solve() Implements IEquation.Solve
End Class
然后个
这个类可以作为你想创建,因为这样的一元二次方程的所有其他可解析式底座(再次,稍稍过了头实施)
Public Class QuadraticEquation
Inherits Equation
Private _cA As Integer
Public Property CoefficientA As Integer
Get
Return _cA
End Get
Set(value As Integer)
If _cA = value Then
Return
End If
_cA = value
Solved = False
End Set
End Property
Private _cB As Integer
Public Property CoefficientB As Integer
Get
Return _cB
End Get
Set(value As Integer)
If _cB = value Then
Return
End If
_cB = value
Solved = False
End Set
End Property
Private _cC As Integer
Public Property CoefficientC As Integer
Get
Return _cC
End Get
Set(value As Integer)
If _cC = value Then
Return
End If
_cC = value
Solved = False
End Set
End Property
Private _positiveRoot As Decimal
Public Property PositiveRoot As Decimal
Get
Return _positiveRoot
End Get
Protected Set(value As Decimal)
_positiveRoot = value
End Set
End Property
Private _negativeRoot As Decimal
Public Property NegativeRoot As Decimal
Get
Return _negativeRoot
End Get
Protected Set(value As Decimal)
_negativeRoot = value
End Set
End Property
Public Overrides Sub Resolve(input As String)
' lets say 2x^2 + 4x - 4 = 0
' lets say 5x^2 + 3x + 1 = 0
If String.IsNullOrWhiteSpace(input) Then
Throw New ArgumentException("Input string cannot be null", "input")
End If
If input.IndexOf("=") >= 0 Then
input = input.Split("=")(0).Trim()
If String.IsNullOrWhiteSpace(input) Then
Throw New FormatException("'=' is at the beginning of the equation")
End If
End If
If input.Contains(" ") Then
input = input.Replace(" ", "")
End If
Dim lstGroup As New List(Of String)
Dim position As Integer = 0
Dim delimiters() As String = {"+", "-"}
Dim minIndex As Integer
Dim curDel As String
Dim lastDel As String = "+"
Dim part As String
While position < input.Length
minIndex = input.Length
curDel = "+"
For Each del In delimiters
Dim targetIndex As Integer = input.IndexOf(del, position)
If targetIndex > 0 AndAlso targetIndex < minIndex Then
minIndex = targetIndex
curDel = del
End If
Next
part = input.Substring(position, minIndex - position)
lstGroup.Add(lastDel + part.ToLower())
position = minIndex + 1
lastDel = curDel
End While
CoefficientA = 0
CoefficientB = 0
CoefficientC = 0
For Each group In lstGroup
If group.Contains("x^2") Then
If CoefficientA <> 0 Then
Throw New FormatException("'x^2' was already determined!")
End If
If Not Integer.TryParse(group.Replace("x^2", ""), CoefficientA) Then
' it is there so set it to 1
CoefficientA = 1
End If
Continue For
End If
If group.Contains("x") Then
If CoefficientB <> 0 Then
Throw New FormatException("'x' was already determined!")
End If
If Not Integer.TryParse(group.Replace("x", ""), CoefficientB) Then
CoefficientB = 1
End If
Continue For
End If
If CoefficientC <> 0 Then
Throw New FormatException("CoefficientC was already determined!")
End If
CoefficientC = Convert.ToInt32(group)
Next
End Sub
Public Sub New()
End Sub
Public Overrides Sub Solve()
Solved = False
Try
Dim determinant As Decimal
Dim squareRootDeterminant As Decimal
Dim doubleA As Decimal
determinant = ((CoefficientB^2) - (4 * CoefficientA * CoefficientC))
If determinant >= 0 Then
squareRootDeterminant = Math.Sqrt(Math.Abs(determinant))
Else
Throw New InvalidOperationException("Cannot get Square root of negative determinant " & determinant)
End If
doubleA = 2 * CoefficientA
PositiveRoot = (-CoefficientB + squareRootDeterminant)/doubleA ' quadratic formula root1
NegativeRoot = (-CoefficientB - squareRootDeterminant)/doubleA ' quadratic formula root2
Solved = True
Catch ex As Exception
Solved = False
Console.WriteLine("{0}", ex.Message)
End Try
End Sub
End Class
那么这将是检验的这样
Sub Main()
Dim test() As String = {"2x^2 + 4x - 4 = 0", "5x^2 + 3x + 1", "2x^2+5x", "x^2+5", "5x - 5 + 3x^2"}
Dim eq As IEquation = Nothing
For Each expression In test
Console.WriteLine("Trying to resolve: {0}", expression)
If Equation.TryParse(Of QuadraticEquation)(expression, eq) Then
eq.Solve()
If Not eq.Solved Then
Console.WriteLine(vbTab & "Although it could be read, the equation failed to be solved!")
Else
Dim qe As QuadraticEquation = DirectCast(eq, QuadraticEquation)
Console.WriteLine(vbTab & "Result: [{0}; {1}]", qe.NegativeRoot, qe.PositiveRoot)
End If
Else
Console.WriteLine("Couldn't be resolved!")
End If
Next
Console.ReadLine()
End Sub
您将不得不解析输入,以便您可以读取要从输入框中读取的表达式。至于你的问题,到目前为止你有什么? – Icepickle 2014-12-13 13:47:52
我有处理输入所需的功能,但我被困在这个文本框的东西 – yawar 2014-12-13 14:01:40
@icepickle,你能详细解释一下“...解析你的输入...” – yawar 2014-12-18 04:29:54