「eval函数java」eval函数JavaScript
今天给各位分享eval函数java的知识,其中也会对eval函数JavaScript进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、自定义函数怎么实现字符串的四则运算
- 2、java里面有没有类似JavaScript中的evel函数的方法
- 3、Java中有没有类似于eval()的函数?目的是计算一个字符串类型的数学表达式的值
- 4、eval()函数的作用是什么?
自定义函数怎么实现字符串的四则运算
一,第一种也是功能最强大的一种,可以使用Eval函数,像在Java中一样强大,几乎所有的运算符都可以实现,包括四则运算,与或非等。
添加COM引用:
private void button2_Click(object sender, EventArgs e)
{
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
sc.Language = "JavaScript";
MessageBox.Show(sc.Eval("((2*3)-5+(3*4))+6/2").ToString());//1+12+3
}
在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示。查阅资料,找到解决方案,记录如下:
选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。
二、第二种,简单的四则运算或判断可以使用DataTable.Compute来实现。
DataTable dt = new DataTable();
MessageBox.Show(dt.Compute("1*2+3", "false").ToString());
三、第三种比较耗费性能,局限性较大,就是在用SQL语句来实现。SQL中的select语句也可以实现计算。
string strConn = "Data Source=127.0.0.1;Initial Catalog=CementCartDB;Persist Security Info=True;User ID=sa;Password=123456"
conn = new SqlConnection(strConn);
conn.Open();
cmd = conn.CreateCommand();
string biaodashi = "11";
cmd.CommandText = "select "+biaodashi;
string o = cmd.ExecuteScalar().ToString();
MessageBox.Show(o);
java里面有没有类似JavaScript中的evel函数的方法
package mathTools;
/**
* Eval.java
*
* Created on 2006年4月10日, 下午3:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* 支持运算符:+ - * / % ] [ ! | =
* 其中:
* ] 表示大于等于
* [ 表示小于等于
* ! 表示不等于
* | 表示或
* & 表示与
* = 表示是否等于
*
* 支持函数:sqrt,square,ceil,sin,cos,asin,acon.tan.atan,log,exp 具体含义见 calFunction 代码
*
*/
/**
*
* @author Trumplet
*
*/
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Eval{
public static String OPTS="+-*/%][!|=#";
public Object calculate(String expression) throws ExpressionException{
try {
Stack Opts=new Stack();
Stack Values=new Stack();
String exp=expression+"#";
int nCount=exp.length(),nIn,nOut,nTemp;
Opts.push("#");
String temp="",optOut="",optIn="",value1="",value2="",optTemp="",opt="",temp1="";
int nFun=0;
boolean isFun=false;
for(int i=0;inCount;){
nTemp=0;
opt=exp.substring(i,i+1);
isFun=false;
temp1="";
while(inCount){
if(!temp1.equals("")){
if(opt.equals("(")){
nFun++;
isFun=true;
} else if(opt.equals(")")){
nFun--;
}
}
if((nFun0)||((!isFun)this.isValue(opt))){
temp1+=opt;
nTemp++;
opt=exp.substring(i+nTemp,i+nTemp+1);
} else{
if(isFun){
temp1+=opt;
nTemp++;
}
break;
}
}
if(temp1.equals("")){
temp=opt;
} else{
temp=temp1;
}
if(nTemp0){
i=i+nTemp-1;
}
temp=temp.trim();
if(this.isValue(temp)){
temp=this.getValue(temp);
Values.push(temp);
i++;
} else{
optIn=Opts.pop().toString();
nIn=this.getOptPriorityIn(optIn);
nOut=this.getOptPriorityOut(temp);
if(nIn==nOut){
i++;
} else if(nInnOut){
String ret="";
value1=Values.pop().toString();
value2=Values.pop().toString();
ret=String.valueOf(this.calValue(value2,optIn,value1));
Values.push(ret);
} else if(nInnOut){
Opts.push(optIn);
Opts.push(temp);
i++;
}
}
}
return Values.pop();
} catch(ExpressionException eE) {
throw eE;
} catch(Exception e) {
throw new ExpressionException("表达式"+expression+"格式非法!");
}
}
protected int getOptPriorityOut(String opt)throws ExpressionException{
if(opt.equals("+")){
return 1;
} else if(opt.equals("-")){
return 2;
} else if(opt.equals("*")){
return 5;
} else if(opt.equals("/")){
return 6;
} else if(opt.equals("%")){
return 7;
} else if(opt.equals("")){
return 11;
} else if(opt.equals("")){
return 12;
} else if(opt.equals("]")){
return 13;
} else if(opt.equals("[")){
return 14;
} else if(opt.equals("!")){
return 15;
} else if(opt.equals("|")){
return 16;
} else if(opt.equals("")) {
return 23;
} else if(opt.equals("=")) {
return 25;
} else if(opt.equals("#")) {
return 0;
} else if(opt.equals("(")){
return 1000;
} else if(opt.equals(")")){
return -1000;
}
throw new ExpressionException("运算符"+opt+"非法!");
}
protected int getOptPriorityIn(String opt) throws ExpressionException{
if(opt.equals("+")){
return 3;
} else if(opt.equals("-")){
return 4;
} else if(opt.equals("*")){
return 8;
} else if(opt.equals("/")){
return 9;
} else if(opt.equals("%")){
return 10;
} else if(opt.equals("")){
return 17;
} else if(opt.equals("")){
return 18;
} else if(opt.equals("]")){
return 19;
} else if(opt.equals("[")){
return 20;
} else if(opt.equals("!")){
return 21;
} else if(opt.equals("|")){
return 22;
} else if(opt.equals("")) {
return 24;
} else if(opt.equals("=")) {
return 26;
} else if(opt.equals("(")){
return -1000;
} else if(opt.equals(")")){
return 1000;
} else if(opt.equals("#")){
return 0;
}
throw new ExpressionException("运算符"+opt+"非法!");
}
protected String getOPTS() {
return OPTS;
}
protected boolean isValue(String cValue){
String notValue=this.getOPTS()+"()";
return notValue.indexOf(cValue)==-1;
}
protected boolean isOpt(String value){
return this.getOPTS().indexOf(value)=0;
}
protected double calValue(String value1,String opt,String value2) throws ExpressionException{
try {
double dbValue1=Double.valueOf(value1).doubleValue();
double dbValue2=Double.valueOf(value2).doubleValue();
long lg=0;
if(opt.equals("+")){
return dbValue1+dbValue2;
} else if(opt.equals("-")){
return dbValue1-dbValue2;
} else if(opt.equals("*")){
return dbValue1*dbValue2;
} else if(opt.equals("/")){
return dbValue1/dbValue2;
} else if(opt.equals("%")){
lg=(long)(dbValue1/dbValue2);
return dbValue1-lg*dbValue2;
} else if(opt.equals("")){
if(dbValue1dbValue2)
return 1;
else
return 0;
} else if(opt.equals("")){
if(dbValue1dbValue2)
return 1;
else
return 0;
} else if(opt.equals("]")){
if(dbValue1=dbValue2)
return 1;
else
return 0;
} else if(opt.equals("[")){
if(dbValue1=dbValue2)
return 1;
else
return 0;
} else if(opt.equals("!")){
if(dbValue1!=dbValue2)
return 1;
else
return 0;
} else if(opt.equals("|")){
if(dbValue10||dbValue20)
return 1;
else
return 0;
} else if(opt.equals("")){
if(dbValue10dbValue20)
return 1;
else
return 0;
} else if(opt.equals("=")){
if(dbValue1==dbValue2)
return 1;
else
return 0;
}
}catch(Exception e) {
throw new ExpressionException("值"+value1+"或"+value2+"在进行"+ opt+"运算时非法!");
}
throw new ExpressionException("运算符"+opt+"非法!");
}
protected String getValue(String oldValue) throws ExpressionException{
String reg="^([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_.()]+)\\)$";
if(this.isFunctionCal(oldValue)){
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(oldValue);
m.find();
return calFunction(m.group(1),m.group(2));
}
return oldValue;
}
protected boolean isFunctionCal(String value){
String reg="^([a-zA-Z0-9_]+)\\(([a-zA-Z0-9_.()]+)\\)$";
return value.matches(reg);
}
protected String calFunction(String function,String value) throws ExpressionException{
String lowerFun=function.toLowerCase();
double db=0;
try {
db=Double.valueOf(this.getValue(value)).doubleValue();
if(lowerFun.equals("log")){
return String.valueOf(Math.log(db));
} else if(lowerFun.equals("square")){
return String.valueOf(Math.pow(db,2));
} else if(lowerFun.equals("sqrt")){
return String.valueOf(Math.sqrt(db));
} else if(lowerFun.equals("sin")){
return String.valueOf(Math.sin(db));
} else if(lowerFun.equals("asin")){
return String.valueOf(Math.asin(db));
} else if(lowerFun.equals("cos")){
return String.valueOf(Math.cos(db));
} else if(lowerFun.equals("tan")){
return String.valueOf(Math.tan(db));
} else if(lowerFun.equals("atan")){
return String.valueOf(Math.atan(db));
} else if(lowerFun.equals("ceil")){
return String.valueOf(Math.ceil(db));
} else if(lowerFun.equals("exp")){
return String.valueOf(Math.exp(db));
}
}catch(Exception e) {
throw new ExpressionException("函数"+function+"值"+value+"非法!");
}
throw new ExpressionException("函数"+function+"不支持!");
}
public static void main(String[]args) {
Eval be=new Eval();
//String exp="sin(ceil(100))*29+20+30*3+0|0|1+11*5+2=2";
//String exp="sin(ceil(sqrt(100)))*29+20+30*3+0|0|1+11*5+2=2";
String exp="8/(3-8/3)";
try {
System.out.println(be.calculate(exp));
} catch(ExpressionException eE) {
System.out.println(eE.getMessage());
}
}
//表达式异常类代码:
public class ExpressionException extends Exception{
public ExpressionException(String msg) {
super(msg);
}
}
}
Java中有没有类似于eval()的函数?目的是计算一个字符串类型的数学表达式的值
有啊,脚本引擎
public class Test {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("js");
String str = "1+2*(3+6)-5/2";
Double result =(Double) se.eval(str);
System.out.println(result);
}
}
eval()函数的作用是什么?
作用是把对应的字符串解析成js代码并运行。eval()()是程序语言中的函数,功能是获取返回值,不同语言大同小异,函数基础是返回值= eval(codeString),如果eval函数在执行时遇到错误,则抛出异常给调用者。
如果eval函数在执行时遇到错误,则抛出异常给调用者。类似的函数是loadcode,loadcode不立即执行代码,另外返回一个函数对象。并且loadcode支持路径参数,评估不支持。评估不支持代码中的返回语句,将代码作为表达式直接计算出结果。
相关信息
Eval函数在PHP代码中的用法:eval()函数把字符串按照PHP代码来计算。该字符串必须是合法的PHP代码,且必须以分号结尾。如果没有在代码字符串中调用返回语句,则返回NULL。如果代码中存在解析错误,则eval()函数返回false。
Eval函数在VBScript脚本语言中的使用:在VB脚本语言中,Eval函数具有两层英文,一是实现计算表达的值,即eval()函数可将字符串转换为代码执行,并返回一个或多个值;二是运行指定的代码。
关于eval函数java和eval函数JavaScript的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。