1

I've been trying to Implement a mathematical algorithm of Numerical computing, which evaluates root of a certain equation given a certain Value of X .
Actually I want to know if there's some shortcut through which I can take the function like f(x)=(x+2) from user using console and a value of int variable 'var' and than simply use f(var) to evaluate value of f(var).
below is a program which uses #define f(x) function, but my question is that :

is there a way I can take the function itself from the console via user input rather than hard coding it in the source code??

#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>

#define f(x) x+2
using namespace std;

int main()
{
    int var = 0;
    cout << "Enter value of var : ";
    cin >> var;
    cout << "\n Value of f(x) = " << f(var) << endl << endl;

}
273K
  • 29,503
  • 10
  • 41
  • 64
  • 1
    You are basically looking for a scripting language interpreter. You can write your own, or integrate a third-party one; there's no shortage of those. [Lua](https://www.lua.org/about.html) seems popular. – Igor Tandetnik May 11 '22 at 01:21
  • A scripting language interpreter? Can you please tell me a little bit more about it? – Qazi Zainullah May 11 '22 at 01:23
  • 1
    He could just be asking about how to evaluate mathematical expressions. Google around “recursive descent” or “shunting yard”. – Dúthomhas May 11 '22 at 01:24
  • 1
    Yes @Dúthomhas exactly, that's what I'm looking for. I don't want to make my own interpreter functionalities to interpret the function first and than evaluating the value of x. I want to know if there's a way that I can use to directly evaluate the value of the function via use of some library or built-in functionality. – Qazi Zainullah May 11 '22 at 01:28
  • 1
    Are you looking for something like https://stackoverflow.com/questions/9329406/evaluating-arithmetic-expressions-from-string-in-c? – cigien May 11 '22 at 02:37

0 Answers0