C++ getting some math function and draw its graph | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ getting some math function and draw its graph

in c++ how we can get the formula of a function from user which that convertable to instructions for drawing its graph? for example user inputs the sinx and its a string but the program needs it as mathematical function.

6th Jun 2019, 6:31 PM
Saeed Vahid
Saeed Vahid - avatar
3 Answers
+ 4
It probably would be really tough to make, since there isn't any evaluation function like eval() in javascript. Maybe you could do it in a way splitting it on any non-numeric characters and "saving" them.
6th Jun 2019, 7:21 PM
Airree
Airree - avatar
+ 2
Alternatively, you can use GNUPlot to plot a graph from a given mathematical equation. There is the following header based library for interfacing with the same: https://github.com/dstahlke/gnuplot-iostream All you need to know are the commands to use with GNUPlot and pass your custom string as a function to plot. The parsing and plotting will automatically be handled for you though, and so it my not be a great method for learning.
7th Jun 2019, 2:16 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
Understanding sin(x): https://en.m.wikipedia.org/wiki/Parsing https://en.m.wikipedia.org/wiki/File:Parser_Flow%D5%B8.gif Graphics are more difficult, as they require a graphical environment. On Windows, this means Windows API + OpenGL/Vulkan/DirectX, or some library that handles it for you QT/SDL etc. SDL is probably the simplest. Drawing the graph is another story. The simplest way is to give it tons of values, and plot the results. You can also use the double derivate to find intervals of concaivity and convexity, if you want a continuous line (although this is quite difficult).
6th Jun 2019, 7:32 PM
Vlad Serbu
Vlad Serbu - avatar