-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluable.java
More file actions
161 lines (135 loc) · 3.27 KB
/
Evaluable.java
File metadata and controls
161 lines (135 loc) · 3.27 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package calc;
import java.util.LinkedList;
//common interface for all functions, variables and numerics
interface Evaluable{
Double evaluate() throws Exception;
}
//base class for all binary operators
abstract class BinOperator implements Evaluable {
Evaluable left;
Evaluable right;
void connect(Evaluable le, Evaluable ri){
right = ri;
left = le;
}
}
class Add extends BinOperator{
public Double evaluate() throws Exception {
return right.evaluate() + left.evaluate();
}
}
class Subtract extends BinOperator{
public Double evaluate() throws Exception {
return left.evaluate() - right.evaluate();
}
}
class Multiply extends BinOperator{
public Double evaluate() throws Exception {
return right.evaluate() * left.evaluate();
}
}
class Divide extends BinOperator{
public Double evaluate() throws Exception {
return left.evaluate() / right.evaluate();
}
}
class Exponentiate extends BinOperator{
public Double evaluate() throws Exception {
return Math.pow(left.evaluate(), right.evaluate());
}
}
class Log implements Evaluable{
Evaluable left;
Evaluable right;
Log(Evaluable le, Evaluable ri){
right = ri;
left = le;
}
public Double evaluate() throws Exception {
return Math.log(right.evaluate())/Math.log(left.evaluate());
}
}
class Sine implements Evaluable{
Evaluable right;
Sine(Evaluable ri){
right = ri;
}
public Double evaluate() throws Exception {
return Math.sin(right.evaluate());
}
}
class Cosine implements Evaluable{
Evaluable right;
Cosine(Evaluable ri){
right = ri;
}
public Double evaluate() throws Exception {
return Math.cos(right.evaluate());
}
}
class Tan implements Evaluable{
Evaluable right;
Tan(Evaluable ri){
right = ri;
}
public Double evaluate() throws Exception {
return Math.tan(right.evaluate());
}
}
//base class for functions and variables
//to use common hash table for functions and variables
abstract class UserDefined implements Evaluable {
String name;
}
class Function extends UserDefined{
LinkedList<String> tokens;
Evaluable arg = null;
Function(String f_name,String exp) throws Exception{
this.name = f_name;
this.tokens = new Tokenizer().tokenize(exp); //tokenizes the function's expression
for (int i = 0; i < tokens.size(); i ++) {
if (!valid(tokens.get(i)))
throw new UnknownSymbolException(tokens.get(i));
}
if (tokens.isEmpty())
throw new InvalidExpressionException();
}
//valid token
boolean valid(String t) {
return Evaluator.isNum(t) || Evaluator.isOp(t) || Evaluator.isTrig(t) || t.equals("log") || t.equals("x") || t.equals(")") || t.equals("(");
}
void set_arg(Evaluable e) {
arg = e;
}
public Double evaluate() throws Exception{
Double x = arg.evaluate();
String x_tok = x.toString();
for (int i = 0; i < tokens.size(); i ++)
{
if (tokens.get(i).equals("x"))
tokens.set(i, x_tok);
}
return new Evaluator(tokens).evaluate();
}
}
class Variable extends UserDefined{
double value;
Variable(String var_name,double val){
name = var_name;
this.value = val;
}
public Double evaluate() {
return value;
}
}
//for numeric values
class Numeric implements Evaluable{
double value;
Numeric(double val){
value = val;
}
public Double evaluate() {
return value;
}
}
// -------------------------------------------------------------