summaryrefslogtreecommitdiffstats
path: root/Project/fparser/examples/example.cc
diff options
context:
space:
mode:
authorThales Lima Oliveira <thaleslima.ufu@gmail.com>2017-12-19 22:00:05 -0200
committerThales Lima Oliveira <thaleslima.ufu@gmail.com>2017-12-19 22:00:05 -0200
commit54bb80251432ff49c967115f8401e7dafc5c57d6 (patch)
tree3de8eada5c3c601764f60734b71707e95260eeb7 /Project/fparser/examples/example.cc
parentc8193665975686fb8a344bee3dc94914a3d3558a (diff)
downloadPSP.git-54bb80251432ff49c967115f8401e7dafc5c57d6.tar.gz
PSP.git-54bb80251432ff49c967115f8401e7dafc5c57d6.tar.xz
PSP.git-54bb80251432ff49c967115f8401e7dafc5c57d6.zip
Several implementations on math expression block
Diffstat (limited to 'Project/fparser/examples/example.cc')
-rw-r--r--Project/fparser/examples/example.cc55
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;
+}