-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (73 loc) · 2.14 KB
/
main.cpp
File metadata and controls
101 lines (73 loc) · 2.14 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <exception>
#include <time.h>
#include "math_interpreter.h"
using namespace std;
int main() {
/*
Example 1: Expression with no variables.
Expression: 1.56 + sin(rad(37.81)) * log(sqrt(75))
Result : 2.88341...
*/
std::string expr1 = "1.56 + sin(rad(37.81)) * log(sqrt(75))";
try {
MathInterpreter inter;
inter.init_with_expr(expr1);
double result1 = inter.calculate();
std::cout << expr1 << " = " << result1 << std::endl;
}
catch(const std::exception& e) {
std::cout << e.what() << std::endl;
}
/*
Example 2: Expression with variables having a single value.
Expression: 1.56 + sin(rad('theta')) * log(sqrt('len'))
for theta = 37.81 degrees and len = 75
Result : 2.88341...
*/
std::string expr2 = "1.56 + sin(rad('theta')) * log(sqrt('len'))";
try {
MathInterpreter inter;
inter.init_with_expr(expr2);
std::string v1 = "theta";
std::string v2 = "len";
inter.set_value(v1, 37.81);
inter.set_value(v2, 75);
double result2 = inter.calculate();
std::cout << expr2 << " = " << result2 << std::endl;
}
catch(const std::exception& e) {
std::cout << e.what() << std::endl;
}
/*
Example 3: Expression with variables having multiple values.
Expression: 1.56 + sin(rad('theta')) * log(sqrt('len'))
for theta between 0 and 90 degrees and len = 75
Result : Multiple values
*/
std::string expr3 = "1.56 + sin(rad('theta')) * log(sqrt('len'))";
try {
clock_t t = clock();
MathInterpreter inter;
inter.init_with_expr(expr3);
std::string v1 = "theta";
std::string v2 = "len";
int numElems = 100001;
std::cout << "Beginning to calculate " << numElems << " elements."
<< std::endl;
// 10000 elements
for(size_t i = 0; i < numElems; i++) {
inter.set_value(v1, i*0.009);
inter.set_value(v2, 75);
double result3 = inter.calculate();
}
t = clock() - t;
std::cout << "Calculated " << numElems << " elements in " << t
<< " milliseconds." << std::endl;
}
catch(const std::exception& e) {
std::cout << e.what() << std::endl;
}
getchar();
return 0;
}