Ask in C++ code to promp user for function expression input

Stack Overflow Asked by tbaracu on January 5, 2022

C++ code to prompt user for function expression input.

I try to make a code where after the RUN the interface to ask me to write the expression of function like pow(x,2)+5 and the program to read it and understand it with its mathematical expression.

So, instead to declare from the beginning:

double f(double x) {   pow(x,2)+5;

I want something like this:

#include <iostream>
#include <cmath>
using namespace std;

int main()
    int n,i;        //n is for subintervals and i is for loop
    double a,b,h,sum=0,integral;  
    double f(double x)  
    cout<<"Enter the expression of the function f(x)=";  // I will answer writing "pow(x,2)+5"

How I can do that ?

One Answer

You can split expression from user via standard input like string. User write string and your code parse it. For parser you can use Shunting-yard algorithm:

And you can check this github page (The Parsing Expression Grammar Template Library):

Answered by bogdyname on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP