diff options
author | Thales Lima Oliveira <thaleslima.ufu@gmail.com> | 2017-12-31 18:00:25 -0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 18:00:25 -0200 |
commit | 0c0280cfcf540f943fd2dbfdf7ac0304ea96a465 (patch) | |
tree | 51c89fc8155dcc447743dc89a1ab5ce36acc72cf /Project/fparser/examples/example.cc | |
parent | c8193665975686fb8a344bee3dc94914a3d3558a (diff) | |
parent | dbe0b112622f8e91fa3e44a6b6e1aa7e230d3ded (diff) | |
download | PSP.git-0c0280cfcf540f943fd2dbfdf7ac0304ea96a465.tar.gz PSP.git-0c0280cfcf540f943fd2dbfdf7ac0304ea96a465.tar.xz PSP.git-0c0280cfcf540f943fd2dbfdf7ac0304ea96a465.zip |
Merge pull request #43 from Thales1330/wip/math-expr-control-block
Wip math expr control block
Diffstat (limited to 'Project/fparser/examples/example.cc')
-rw-r--r-- | Project/fparser/examples/example.cc | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Project/fparser/examples/example.cc b/Project/fparser/examples/example.cc new file mode 100644 index 0000000..2d6b771 --- /dev/null +++ b/Project/fparser/examples/example.cc @@ -0,0 +1,55 @@ +// Simple example file for the function parser +// =========================================== + +/* When running the program, try for example with these values: + +f(x) = x^2 +min x: -5 +max x: 5 +step: 1 + +*/ + +#include "../fparser.hh" + +#include <iostream> +#include <string> + +int main() +{ + std::string function; + double minx, maxx, step; + FunctionParser fparser; + + fparser.AddConstant("pi", 3.1415926535897932); + + while(true) + { + std::cout << "f(x) = "; + std::getline(std::cin, function); + if(std::cin.fail()) return 0; + + int res = fparser.Parse(function, "x"); + if(res < 0) break; + + std::cout << std::string(res+7, ' ') << "^\n" + << fparser.ErrorMsg() << "\n\n"; + } + + std::cout << "min x: "; + std::cin >> minx; + std::cout << "max x: "; + std::cin >> maxx; + std::cout << "step: "; + std::cin >> step; + if(std::cin.fail()) return 0; + + double vals[] = { 0 }; + for(vals[0] = minx; vals[0] <= maxx; vals[0] += step) + { + std::cout << "f(" << vals[0] << ") = " << fparser.Eval(vals) + << std::endl; + } + + return 0; +} |