-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.java
More file actions
59 lines (51 loc) · 1.84 KB
/
function.java
File metadata and controls
59 lines (51 loc) · 1.84 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
public class function {
private String expression; // e.g. "x^2 + 3x + 2"
// Constructor
public function(String f) {
this.expression = f.replaceAll("\\s+", ""); // clean spaces
}
// Example: identify what kind of function this is
public String identifyType() {
if (expression.contains("^2")) {
return "Quadratic";
} else if (expression.contains("^3")) {
return "Cubic";
} else if (expression.contains("x")) {
return "Linear";
} else {
return "Constant";
}
}
public double evaluate(double x) {
/* no bedmas yet
when i add it:
find "(" & ")" in expression. always look for farthest distance.
compute the expression within the indexes (always seek for another pair of parenthesis before computing)
*/
int result = (int) expression.charAt(0);
for (int i = 1; i < expression.length(); i += 2) {
char op = (char) expression.charAt(i);
if (op == '(') {
int prt1 = i; //paranthesis index
for (int v = expression.length(); v > 0; v --) {
int prt2 = v;
if (expression.charAt(v) == ')') {
break;
}
}
}
int num = (int) expression.charAt(i + 1);
switch (op) {
case '^': result ^= num; break;
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/': result /= num; break;
}
}
return result;
}
public String getExpression() {
return expression;
}
}