與往常一樣,我得到了忘乎所以的實施可能的解決方案,所以讓我們現在先用鹼性溶液中,然後用測試場景:)
分析文本的「全面」解決方案,您將需要確定:
- 您的輸入有效
- 如果它們是正數或負數
- 哪裏是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