📄 function.java
字号:
//base class for functionabstract class Function extends TreeNode { public TreeNode[] Subtree; public String toString(int level) { boolean allArgsAreTerminals = true; for (int a = 0; a < Subtree.length; a++) { allArgsAreTerminals = allArgsAreTerminals && (Subtree[a] instanceof Terminal); } String s = new String(); if (!allArgsAreTerminals) { s = indent(level) + getName() + "(\n"; int i = 0; while (i < Subtree.length-1) { s = s + Subtree[i].toString(level+1) + ",\n"; i++; } if (i < Subtree.length) { s = s + Subtree[i].toString(level+1) + "\n"; } s = s + indent(level) + ")"; } else { s = indent(level) + getName() + "("; int i = 0; while (i < Subtree.length-1) { s = s + Subtree[i].toString(0) + ","; i++; } if (i < Subtree.length) { s = s + Subtree[i].toString(0); } s = s + ")"; } return s; } protected Object clone() { Function temp = null; try { temp = (Function)getClass().newInstance(); for (int i = 0; i < Subtree.length; i++) { temp.Subtree[i] = (TreeNode)Subtree[i].clone(); } } catch (Exception e) { } return temp; } int countNodes() { int count = 1; for (int a = 0; a < Subtree.length; a++) { count = count + Subtree[a].countNodes(); } return count; } int countNodes(Condition cond) { int count = (cond.test(this) ? 1 : 0); for (int a = 0; a < Subtree.length; a++) { count = count + Subtree[a].countNodes(cond); } return count; }}class Addition extends Function { Addition() { Subtree = new TreeNode[2]; } String getName() { return "add"; } double eval(double x) { return Subtree[0].eval(x) + Subtree[1].eval(x); }}class Subtraction extends Function { Subtraction() { Subtree = new TreeNode[2]; } String getName() { return "sub"; } double eval(double x) { return Subtree[0].eval(x) - Subtree[1].eval(x); }}class Multiplication extends Function { Multiplication() { Subtree = new TreeNode[2]; } String getName() { return "mul"; } double eval(double x) { return Subtree[0].eval(x) * Subtree[1].eval(x); }}class Division extends Function { Division() { Subtree = new TreeNode[2]; } String getName() { return "div"; } double eval(double x) { double divisor = Subtree[1].eval(x); return (divisor == 0 ? 1 : Subtree[0].eval(x) / divisor); }}class Sine extends Function { Sine() { Subtree = new TreeNode[1]; } String getName() { return "sin"; } double eval(double x) { return Math.sin(Subtree[0].eval(x)); }}class Cosine extends Function { Cosine() { Subtree = new TreeNode[1]; } String getName() { return "cos"; } double eval(double x) { return Math.cos(Subtree[0].eval(x)); }}class Exp extends Function { Exp() { Subtree = new TreeNode[1]; } String getName() { return "exp"; } double eval(double x) { double v = Subtree[0].eval(x); if (v > 100) { v = 100; } else if (v < -100) { v = -100; } return Math.exp(v); }}class First extends Function { First() { Subtree = new TreeNode[2]; } String getName() { return "first"; } double eval(double x) { return Subtree[0].eval(x); }}class Second extends Function { Second() { Subtree = new TreeNode[2]; } String getName() { return "second"; } double eval(double x) { return Subtree[1].eval(x); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -