1

I'm trying to write a program that has the user input an equation f(x), and then solve it for any value of x. Say the equation given is 3x^4-cos(x). How would I go about replacing the x's with the desired value (let's say a) and then solve it? How do you change the caret sign to the pow() function?

user2736738
  • 30,591
  • 5
  • 42
  • 56
scb123
  • 11
  • 1
  • 1
    You should be able to pick up helpful hints from both links above, but the only one that discusses evaluating a variable as part of the expression is related to Objective-C, not C. While the principles are similar, C does not have anything similar to `NSPredicate`, `GCMathParser` or `DDMathParser`. It's up to you in C. – David C. Rankin Mar 02 '18 at 02:00
  • 1
    After the *user input an equation `f(x)`*, you want to *solve it for any value of `'x'`* -- not find the roots -- Correct? You simply want to input the equation and then take a value `x` to output the proper `f(x)` - right? – David C. Rankin Mar 02 '18 at 02:03
  • David, yes that's correct – scb123 Mar 02 '18 at 02:09
  • 1
    this is not solving the equation (which is like solving for which x values that `x^2 - 2x + 1 = 0`) but *evaluating the expression* – phuclv Mar 02 '18 at 03:10
  • The supposed duplicate question seems to have little to do with the one asked by @scb123: he was asking about the way to evaluate an expression string, in C, while the 'duplicate' question asks how to speed up the evaluation of an already parsed expression, in Objective C. – alexsh Mar 03 '18 at 04:05

1 Answers1

0

Short of using an existing library, you would have to write a parser that will translate your expression into some data structure (usually a tree) that can then be used to evaluate the expression, and a scanner to split your expressions into 'words' (or tokens). Tools like bison and flex (that replaced earlier yacc and lex) are helpful in writing the parser and the scanner.

alexsh
  • 1,121
  • 8
  • 12
  • After parsing the equation, how would you put the tokens back together in a way that can be solved? I feel like the only delimiters that could be used would be things like `^, +, -, *` etc. How would I keep track of those and use them in the mathematical expression? – scb123 Mar 02 '18 at 03:04
  • You effectively have to sum the terms. After splitting and parsing each 'term' within the expression, it will represent a value, e.g. `temp=3*x`, `pow(temp,4)` and `-cos(x)`.. Since it may take more than one trip down the string to parse it into groups that need to be evaluated, it may help to have your parser build either a simple array of pointers to the expressions that must be evaluated (or tree as mentioned above) and then sum your way to the final value as you evaluate each term. – David C. Rankin Mar 02 '18 at 07:42
  • You would do this by recursively descending the tree. For example if a node of the tree is '*' (multiplication) you would call the evaluation function on each of its leaves and then multiply the results. So you would call the 'evaluate' function for each leaf and then perform the operation called for by the node. An expression grammar can handle things like precedence, including delimiters (parentheses) – alexsh Mar 03 '18 at 03:57
  • Another way is to forgo building the tree (AST, stands for abstract syntax tree) altogether and evaluate the expression 'on the fly' as part of syntax-directed translation. The GNU manual for bison has an example that implements a simple expression evaluator that uses this approach. You might be able to adopt it to your needs – alexsh Mar 03 '18 at 03:58