]> Shamusworld >> Repos - architektonas/blob - fparser/examples/example.cc
Fixed problem with MDI activation.
[architektonas] / fparser / examples / example.cc
1 // Simple example file for the function parser\r
2 // ===========================================\r
3 \r
4 /* When running the program, try for example with these values:\r
5 \r
6 f(x) = x^2\r
7 min x: -5\r
8 max x: 5\r
9 step: 1\r
10 \r
11 */\r
12 \r
13 #include "../fparser.hh"\r
14 \r
15 #include <iostream>\r
16 #include <string>\r
17 \r
18 int main()\r
19 {\r
20     std::string function;\r
21     double minx, maxx, step;\r
22     FunctionParser fparser;\r
23 \r
24     fparser.AddConstant("pi", 3.1415926535897932);\r
25 \r
26     while(true)\r
27     {\r
28         std::cout << "f(x) = ";\r
29         std::getline(std::cin, function);\r
30         if(std::cin.fail()) return 0;\r
31 \r
32         int res = fparser.Parse(function, "x");\r
33         if(res < 0) break;\r
34 \r
35         std::cout << std::string(res+7, ' ') << "^\n"\r
36                   << fparser.ErrorMsg() << "\n\n";\r
37     }\r
38 \r
39     std::cout << "min x: ";\r
40     std::cin >> minx;\r
41     std::cout << "max x: ";\r
42     std::cin >> maxx;\r
43     std::cout << "step: ";\r
44     std::cin >> step;\r
45     if(std::cin.fail()) return 0;\r
46 \r
47     double vals[] = { 0 };\r
48     for(vals[0] = minx; vals[0] <= maxx; vals[0] += step)\r
49     {\r
50         std::cout << "f(" << vals[0] << ") = " << fparser.Eval(vals)\r
51                   << std::endl;\r
52     }\r
53 \r
54     return 0;\r
55 }\r