a string expression calculator.
for example : 10*9 +10 would output : 100.0
calculator java class
DCalculatorV1 skill java class :
to add the skill go to the c'tor of the chobit class and add this one line of code :
dClassesLv1.add(new DCalculatorV1());
that simple. :grimoire: :shine:
for example : 10*9 +10 would output : 100.0
calculator java class
Code:
public class StrCalculatorV2 {
/*
* StrCalculatorV2 strCalculatorV2 = new StrCalculatorV2();
* System.out.println(strCalculatorV2.calcExp("8*8+36"));
* System.out.println(strCalculatorV2.eval("((4 - 2^3 + 1) * -sqrt(3*3+4*4)) / 2"));
*/
public String calcExp(final String str) {
String result = "";
try {
result = eval(str) + "";
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
public double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ')
nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length())
throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+'))
x += parseTerm(); // addition
else if (eat('-'))
x -= parseTerm(); // subtraction
else
return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*'))
x *= parseFactor(); // multiplication
else if (eat('/'))
x /= parseFactor(); // division
else
return x;
}
}
double parseFactor() {
if (eat('+'))
return parseFactor(); // unary plus
if (eat('-'))
return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.')
nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z')
nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt"))
x = Math.sqrt(x);
else if (func.equals("sin"))
x = Math.sin(Math.toRadians(x));
else if (func.equals("cos"))
x = Math.cos(Math.toRadians(x));
else if (func.equals("tan"))
x = Math.tan(Math.toRadians(x));
else
throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^'))
x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
}
DCalculatorV1 skill java class :
Code:
package chobit;
import java.util.ArrayList;
public class DCalculatorV1 extends AbsCmdReq implements Neuronable {
private StrCalculatorV2 strCalculatorV2 = new StrCalculatorV2();
private String Result = "";
@Override
public void output(Neuron noiron) {
// TODO Auto-generated method stub
if (!Result.isEmpty()) {
AbsAlgPart itte = new APSay(1, Result);
String representation = "calcexp";
ArrayList<AbsAlgPart> algParts1 = new ArrayList<>();
algParts1.add(itte);
Algorithm algorithm = new Algorithm("calcexp", representation, algParts1);
noiron.algParts.add(algorithm);
Result = "";
}
}
@Override
public void input(String ear, String skin, String eye) {
// TODO Auto-generated method stub
Result = strCalculatorV2.calcExp(ear);
}
}
to add the skill go to the c'tor of the chobit class and add this one line of code :
dClassesLv1.add(new DCalculatorV1());
that simple. :grimoire: :shine: