简体   繁体   English

用户输入方程的计算值

[英]Calculating value of user-inputted equation

I'm working on building a simple limit calculator in Visual Studio 2013. Right now I have my program set up in a way so that the user first enters the equation for the limit, and then the limit value. 我正在Visual Studio 2013中构建一个简单的极限计算器。现在,我以某种方式设置程序,以便用户首先输入极限方程,然后输入极限值。 (ex. equation of x+3, limit value of 3 - the answer should then be 6) (例如x + 3的方程,极限值为3-则答案应为6)

I'm designing it so that it takes the limit from both the left and right side of the user inputted number, and if the two answers then differ by quite a bit, the program will say the limit doesn't exist. 我正在设计它,以便它从用户输入的数字的左侧和右侧获得限制,如果两个答案相差很大,程序将说该限制不存在。 (However this code isn't in place yet - I plan to work on it after I can solve this problem) (但是此代码尚未到位,我计划在解决此问题后进行处理)

Anyway, right now I'm stuck on one part of my code. 无论如何,现在我只停留在代码的一部分。 The user can input their own custom function, as I mentioned above. 如上所述,用户可以输入自己的自定义功能。 I then have the program automatically replace all "x" values with the limit value that the user input as well. 然后,我让程序自动将所有“ x”值替换为用户输入的极限值。 However, when I have the answer display, it will give me an answer of 5.0001 + 3 instead of just 8.0001. 但是,当我有答案显示时,它将给我答案5.0001 + 3而不是8.0001。 Below is my code, do you know of any way I can fix this so that it will perform the calculations needed, and just spit out a single number rather than the equation with x substituted out? 以下是我的代码,您是否知道有什么办法可以解决此问题,以便它将执行所需的计算,并且只吐出一个数字而不是用x替换出方程式?

Public Class Form1

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim Limit As Double = txtLimit.Text
        Dim Equation As String = txtEquation.Text
        Dim LeftTest As Double
        Dim RightTest As Double

        LeftTest = (Limit - 0.0001)
        RightTest = (Limit + 0.0001)

        Dim NewEquation = Replace(Equation, "x", RightTest)
        Dim FinalAnswer
        FinalAnswer = NewEquation
        MsgBox("The limit for this equation is " & NewEquation)
    End Sub
End Class

Any help would be appreciated with this. 任何帮助将不胜感激与此。

You can download and use third party Parser.dll from the following link 您可以从以下链接下载和使用第三方Parser.dll

http://simplemathparser.codeplex.com/ http://simplemathparser.codeplex.com/

and code as shown below 和代码如下所示

    Dim Limit As Double = txtLimit.Text
    Dim Equation As String = txtEquation.Text
    Dim LeftTest As Double
    Dim RightTest As Double

    LeftTest = (Limit - 0.0001)
    RightTest = (Limit + 0.0001)

    Dim parser As New MathParser.Parser("x")
    Dim parsingResult = parser.Parse(Equation)
    Dim result = parsingResult.Evaluate(RightTest)
    MsgBox("The limit for this equation is " & result )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM