-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperator.java
More file actions
93 lines (89 loc) · 2.16 KB
/
Operator.java
File metadata and controls
93 lines (89 loc) · 2.16 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
/**
* @name Sterling Blevins
*
* @file Operator.java
* @description The operator enumeration contains all the operational equations
*
* @date 5/3/17
*/
public enum Operator {
LPAREN("("),
RPAREN(")"),
EXPONENT("^"),
MULTIPLY("*"),
DIVIDE("/"),
MODULO("%"),
ADD("+"),
SUBTRACT("-"),
GREATER(">"),
LESSER("<"),
LESSEREQ("<="),
GREATEREQ(">="),
EQUAL("==");
private String operator;
private Operator(String operator) {
this.operator = operator;
}
/**
* evaluate based on what the operator is
* @param a
* @param b
* @return
*/
public double eval(double a, double b) {
double temp = 0;
switch(this) {
case LPAREN: temp = Double.NaN;
break;
case RPAREN: temp = Double.NaN;
break;
case EXPONENT: temp = Math.pow(a, b);
break;
case MULTIPLY: temp = a * b;
break;
case DIVIDE: temp = a / b;
break;
case MODULO: temp = a % b;
break;
case ADD: temp = a + b;
break;
case SUBTRACT: temp = a - b;
break;
// conditionals return 1 for true, 0 for false
case GREATER:
if(a > b)
temp = 1;
else
temp = 0;
break;
case GREATEREQ:
if(a >= b)
temp = 1;
else
temp = 0;
break;
case LESSER:
if(a < b)
temp = 1;
else
temp = 0;
break;
case LESSEREQ:
if(a <= b)
temp = 1;
else
temp = 0;
break;
case EQUAL:
if(a <= b)
temp = 1;
else
temp = 0;
break;
}
return temp;
}
public String toString() {
return this.operator;
}
}