数学表达式解析:当 + 后跟 - 时输出不正确 [英] Math expression parsing: incorrect output when a + followed by a -

查看:29
本文介绍了数学表达式解析:当 + 后跟 - 时输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序来解析诸如 3x^3 + 7x^2 + 6x - 9 之类的表达式.我正在使用递归系统来做到这一点.目前,我只是在测试是否仅通过在表达式中使用常量来获得我输入的表达式的正确输出.该代码适用于大多数表达式,但是当我有一个减号后跟一个正数时,两个项都被减去(应该减去第一项,应该加上第二项).一个例子是输入 4*3^2-7*3+5*3.答案是 30,但程序执行 4*3^2-7*3-5*3 而输出 0.我不确定如何解决这个问题.

I am trying to create a program that parses an expression such as 3x^3 + 7x^2 + 6x - 9. I am using a recursive system to do this. At the moment, I'm just testing to see if I get the correct output for the expression I input by only using constants in the expression. The code works fine with most expressions, but when I have a minus followed by a positive, both terms are subtracted (first term should be subtracted, second term should be added). An example of this is entering 4*3^2-7*3+5*3. The answer to this is 30 but the program does 4*3^2-7*3-5*3 instead and outputs 0. I am unsure how to solve this.

代码:

    Private Function ParseExpr(ByRef expression As String)

        Dim op, op1 As Integer
        op = ParseFactor(expression)
        If expression.Length <> 0 Then

            If (expression(0) = "+") Then
                expression = expression.Substring(1, expression.Length - 1)
                op1 = ParseExpr(expression)
                op += op1
            ElseIf (expression(0) = "-") Then
                expression = expression.Substring(1, expression.Length - 1)
                op1 = ParseExpr(expression)
                op -= op1
            End If
        End If
        Return op
    End Function

推荐答案

提升到乘法和乘法都很好.在这一点上,我们被简化为 36-21+15.在这里我们必须记住,我们正在添加 -21.所以我们需要回到 -7 .当我建立我的数字列表(它处理一位以上的数字)时,我会在它前面的数字上加上减号.

All is well with raising to a power and multiplication. At that point we are simplified to 36-21+15. Here we must remember that we are adding -21. So we need to go back to -7 . When I build my list of numbers (it handles numbers of more than one digit) I add the minus sign to the number it precedes.

此代码不处理十进制数或括号.如果您愿意,我认为您可以添加除法运算符.

This code does not handle decimal numbers or parenthesis. I think you will be able to add the division operator, if you wish.

Private NumList As New List(Of Double)
Private OperatorList As New List(Of String)

Private Sub OpCode()
    Dim Input = "14*3^2-7*3+15*3"
    PopulateLists(Input)
    Dim OpIndex As Integer
    Dim NewNum As Double
    Dim operators = {"^", "*", "+"} 'Note: no minus sign, the minus goes with the number
    For Each op In operators
        Do
            OpIndex = OperatorList.IndexOf(op)
            If OpIndex = -1 Then
                Exit Do
            End If
            Select Case op
                Case "^"
                    NewNum = NumList(OpIndex) ^ NumList(OpIndex + 1)
                Case "*"
                    NewNum = NumList(OpIndex) * NumList(OpIndex + 1)
                Case "+"
                    NewNum = NumList(OpIndex) + NumList(OpIndex + 1)
            End Select
            NumList.RemoveAt(OpIndex + 1)
            NumList(OpIndex) = NewNum
            OperatorList.RemoveAt(OpIndex)
        Loop
    Next
    MessageBox.Show(NumList(0).ToString) 'Displays 150
End Sub

Private Sub PopulateLists(Input As String)
    Dim strNum As String = ""
    For Each c As Char In Input 'Be careful here, the IDE wants to add () at the end of this line - it doesn't belong
        If Char.IsDigit(c) Then
            strNum &= c
        ElseIf c = "-" Then
            OperatorList.Add("+") 'We are adding a negative number
            NumList.Add(CDbl(strNum)) 'Add the last number we accumulated so we can start a new one with the minus sign
            strNum = "-" 'Start a new number with the minus sign
        Else 'The other operators are added to the list
            OperatorList.Add(c)
            NumList.Add(CDbl(strNum))
            strNum = ""
        End If
    Next
    NumList.Add(CInt(strNum)) 'The last number which will not be followed by an operator
End Sub

这篇关于数学表达式解析:当 + 后跟 - 时输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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