AnswerBun.com

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"
    cin>>f;

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: https://en.wikipedia.org/wiki/Shunting-yard_algorithm

And you can check this github page (The Parsing Expression Grammar Template Library): https://github.com/taocpp/PEGTL

Answered by bogdyname on January 5, 2022

Add your own answers!

Related Questions

How to insert multiple images in the Django blog app?

1  Asked on November 29, 2021 by waw-waw

 

Filter out Certain Words and Match only EXACT Words

1  Asked on November 27, 2021 by askmeaquestion1234

         

soup.find returning “none” only sometimes?

2  Asked on November 27, 2021 by c120

   

Raise float into the power – print formula

1  Asked on November 27, 2021 by markl

   

Conditional Window Functions

1  Asked on November 27, 2021 by caerus

   

unable to load font awesome icons in html

1  Asked on November 27, 2021 by vasilis-skentos

     

Why the method is returning padEnd as undefined?

1  Asked on November 27, 2021 by dev101

   

React js .bind(this) in hooks

1  Asked on November 27, 2021 by soukaina-benchekroun

   

How to get a specific name from another array by its id?

1  Asked on November 27, 2021 by ahmed-eid

   

Insertion Sort on a String Array

1  Asked on November 27, 2021 by 101001

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir