import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
class limit {
public static String formula = "x";
public static int tendencia = 0;
public static void main (String[] args) {
System.out.println(args.length);
if(args.length == 2) {
System.out.println("A fórmula é: " + args[0]);
formula = args[0];
tendencia = Integer.parseInt(args[1]);
double res = limit( tendencia );
System.out.println(" O limite é : " + res );
}else{
System.out.println("Maneira correta de uso: java limit \"FORMULA\" TENDENCIA");
}
}
public static double EvalFunction(String f, double x) {
try {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
f = f.replace("x", Double.toString(x));
return Double.parseDouble(engine.eval(f).toString());
} catch (Exception e) {
System.out.println(e.toString());
return Double.NaN;
}
}
public static double f (double x)
{
return EvalFunction(formula, x);
}
public static double poslimit( double a ) {
int a_n = (int) Math.round(a * 1000);
double minn = Integer.MAX_VALUE;
for(int x=a_n+20;x>a_n;x--) {
minn = Math.min(minn, f(x/1000.0));
}
System.out.println("LimPos: " + minn );
return minn;
}
public static double neglimit( double a ) {
int a_n = (int) Math.round(a * 1000);
double maxn = Integer.MIN_VALUE;
for(int x=a_n-20;x<a_n;x++) {
maxn = Math.max(maxn, f(x/1000.0));
}
System.out.println("LimNeg: " + maxn );
return maxn;
}
public static double limit( double a ) {
double neg = Math.round(neglimit(a)*100)/100.0;
double pos = Math.round(poslimit(a)*100)/100.0;
return ( neg==pos ) ? neg : Double.NaN;
}
}