-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcEval.java
80 lines (78 loc) · 2.66 KB
/
CalcEval.java
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
/*
*code by Reed Dahle
*last updated 01-31-2019 @ 21:44
*My dad works at oracle so if you steal my code without attributing me I can have you banned kid
*/
package calcUI;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class CalcEval
{
public double Calculate(String input) throws ScriptException
{
ScriptEngineManager mgr = new ScriptEngineManager(); //my favorite part of java is programming things that I don't know how it works or how to implement it correctly
ScriptEngine engine = mgr.getEngineByName("JavaScript"); //is this a Java "Script" engine or a JavaScript engine?
try
{
return ((double)engine.eval(input)); //first we try casting our answer to a double
}
catch(ClassCastException ex)
{
return(Double.valueOf((int)engine.eval(input))); //if that doesn't work well force it into a double
} //why can't we just have a single non primitive number object that can contain a decimal, negative sign, or scientific notation? My computer has like 16 gigs of ram... we don't need the wasted space
}
/*
public String addExp(String currentValue) //yeah just pretend this junk isn't here. like don't even read it
{
String newValue = "";
boolean numDone = false;
boolean parenthDone = false;
int endNumIndex = 0;
int openParenthCounter = 0;
int closedParenthCounter = 0;
char testChar;
if (currentValue.charAt(currentValue.length()-1) == ')')
{
for (int i = 1; parenthDone == false; i++)
{
testChar = (currentValue.charAt(currentValue.length()-i));
if (testChar == ')')
{
closedParenthCounter++;
}
if(testChar == '(')
{
openParenthCounter++;
}
if((openParenthCounter == closedParenthCounter) && (openParenthCounter != 0) && (closedParenthCounter != 0))
{
parenthDone = true;
endNumIndex = (currentValue.length()-i);
}
}
}
else
{
for (int i = 1; numDone == false; i++)
{
testChar = (currentValue.charAt(currentValue.length()-i));
if ((testChar != '1')&&(testChar != '2')&&(testChar != '3')&&(testChar != '4')&&(testChar != '5')&&(testChar != '6')&&(testChar != '7')&&(testChar != '8')&&(testChar != '9')&&(testChar != '0')&&(testChar != '.'))
{
numDone = true;
endNumIndex = (currentValue.length()-i+1);
}
if((currentValue.length()-i) == 0)
{
numDone = true;
endNumIndex = (currentValue.length()-i);
}
}
}
newValue = "Math.pow(" + currentValue.substring(endNumIndex) + ",";
currentValue = currentValue.substring(0, endNumIndex);
newValue = currentValue + newValue;
return newValue;
}
*/
}