calculating user defined formulas (with c++)

前端 未结 8 1624
萌比男神i
萌比男神i 2020-12-29 12:07

We would like to have user defined formulas in our c++ program. e.g. The value v = x + ( y - (z - 2)) / 2. Later in the program the user would define x,y and z -> t

相关标签:
8条回答
  • 2020-12-29 12:12

    To make your life easier, I think getting this kind of input is best done through a GUI where users are restricted in what they can type in.

    If you plan on doing it from the command line (that is the impression I get from your post), then you should probably define a strict set of allowable inputs (e.g. only single letter variables, no whitespace, and only certain mathematical symbols: ()+-*/ etc.).

    Then, you will need to:
    Read in the input char array
    Parse it in order to build up a list of variables and actions
    Carry out those actions - in BOMDAS order

    0 讨论(0)
  • 2020-12-29 12:13

    There's generally two ways of doing it, with three possible implementations:

    1. as you've touched on yourself, a library to evaluate formulas
    2. compiling the formula into code

    The second option here is usually done either by compiling something that can be loaded in as a kind of plugin, or it can be compiled into a separate program that is then invoked and produces the necessary output.

    For C++ I would guess that a library for evaluation would probably exist somewhere so that's where I would start.

    0 讨论(0)
  • 2020-12-29 12:14

    Using Spirit (for example) to parse (and the 'semantic actions' it provides to construct an expression tree that you can then manipulate, e.g., evaluate) seems like quite a simple solution. You can find a grammar for arithmetic expressions there for example, if needed... (it's quite simple to come up with your own).

    Note: Spirit is very simple to learn, and quite adapted for such tasks.

    0 讨论(0)
  • 2020-12-29 12:22

    With ANTLR you can create a parser/compiler that will interpret the user input, then execute the calculations using the Visitor pattern. A good example is here, but it is in C#. You should be able to adapt it quickly to your needs and remain using C++ as your development platform.

    0 讨论(0)
  • 2020-12-29 12:25

    Building your own parser for this should be a straight-forward operation:

    ) convert the equation from infix to postfix notation (a typical compsci assignment) (I'd use a stack) ) wait to get the values you want ) pop the stack of infix items, dropping the value for the variable in where needed ) display results

    0 讨论(0)
  • 2020-12-29 12:29

    You can represent your formula as a tree of operations and sub-expressions. You may want to define types or constants for Operation types and Variables.

    You can then easily enough write a method that recurses through the tree, applying the appropriate operations to whatever values you pass in.

    0 讨论(0)
提交回复
热议问题