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.

3 odpowiedzi

+ 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.

+ 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.

+ 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).