blob: 2d6b7711a82ef0fe67a946c932b213d5c9a3ca8e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}
|