1.我有字符串
String _str = "1+6*6-2";
int k;
我要把_str执行里面的算法,得到结果赋值给k
即要相当于 k=1+6*6-2
要怎么样才可以执行.2.String str1 = null;
String _str = "str1 = \"stringvalue\""
怎么样实现
str1 = "stringvalue"
即执行_str后的结果谢谢
String _str = "1+6*6-2";
int k;
我要把_str执行里面的算法,得到结果赋值给k
即要相当于 k=1+6*6-2
要怎么样才可以执行.2.String str1 = null;
String _str = "str1 = \"stringvalue\""
怎么样实现
str1 = "stringvalue"
即执行_str后的结果谢谢
String =" StringBuffer b = new StringBuffer();b.append(\"jjj\")....;str1= b.toString()";
private String src;
/**
* constructor
* @param src the string(expression) to calculate
*/
public CalStr(String src) {
this.src = src;
}
/**
* calculate to get the result
* @return (double)result
*/
public double getResult() {
String postfix = getPostfix();
Stack<String> stk = new Stack<String>();
//System.out.println(postfix);
String parts[] = postfix.split(" +");
double result=0;
for(int i=0; i<parts.length; i++){
char tmp = parts[i].charAt(0);
if(!isOperator(tmp)){
stk.push(parts[i]);
}
else{
double a = Double.parseDouble(stk.pop());
double b = Double.parseDouble(stk.pop());
//b is followed by a in the orignal expression
result = calculate(b,a,tmp);
stk.push(String.valueOf(result));
}
}
return result;
}
/**
* test if the character is an operator,such +,-,*,/
* @param op the character to test
* @return true if op is an operator otherwise false
*/
private boolean isOperator(char op){
return (op=='+'||op=='-'||op=='*'||op=='/');
}
/**
* calculate an expression such (a op b)
* @param a number 1
* @param b number 2
* @param op the operator
* @return (double)(a op b)
*/
public double calculate(double a, double b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
/**
* convert the suffix to postfix
* @return the postfix as a string
*/
private String getPostfix() {
Stack<String> stk = new Stack<String>();
String postfix = new String();
char op;
int i = 0;
while (i < src.length()) {
if (Character.isDigit(src.charAt(i))||src.charAt(i)=='.') {
postfix += " ";
do {
postfix += src.charAt(i++);
} while ((i < src.length())
&& (Character.isDigit(src.charAt(i))));
postfix += " ";
} else {
switch (op = src.charAt(i++)) {
case '(':
stk.push("(");
break;
case ')':
while (stk.peek() != "(") {
String tmp = stk.pop();
postfix += tmp;
if(tmp.length()==1 && isOperator(tmp.charAt(0)))
postfix += " ";
}
stk.pop();
postfix += " ";
break;
case '+':
case '-':
while ((!stk.empty()) && (stk.peek() != "(")) {
postfix += stk.pop()+" ";
}
stk.push(new Character(op).toString());
break;
case '*':
case '/':
while ((!stk.empty())
&& ((stk.peek() == "*") || (stk.peek() == "/"))) {
postfix += stk.pop()+" ";
}
stk.push(new Character(op).toString());
break;
}
}
}
ListIterator it = stk.listIterator(stk.size());
while (it.hasPrevious())
postfix += it.previous() + " ";
return postfix.trim().replaceAll(" +\\.",".");
} /**
* main function
* @param args
*/
public static void main(String args[]) {
System.out.println(new CalStr("((1.5+6.000)*9+9.36)*(8+9-8*8+8*7)").getResult());
}
}
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Loader;
import javassist.NotFoundException;/**
* @author treeroot
*
*/
public class SystemEx {
private SystemEx() {
} public static int evalInt(String codes) {
return ((Integer) eval(codes, "int")).intValue();
} public static double evalDouble(String codes) {
return ((Double) eval(codes, "double")).doubleValue();
} public static String evalString(String codes) {
return (String) evalObject(codes);
} public static Object evalObject(String codes) {
return eval(codes, "Object");
} public static Object eval(String codes, String retType) {
try {
ClassPool cp = ClassPool.getDefault();
CtClass cc;
String cls="SystemEx$Eval";
try{
cc=cp.get(cls);
}
catch(NotFoundException e){
cc= cp.makeClass(cls);
}
cc.defrost();
try{
CtMethod t=cc.getDeclaredMethod("invoke");
t.setBody("{"+ codes+ "}");
}
catch(NotFoundException e){
CtMethod cm = CtNewMethod.make("public "+ retType +" invoke(){"
+ codes + "}", cc);
cc.addMethod(cm);
}
Loader l=new Loader(cp);
Class c=l.loadClass(cls);
Method m = c.getMethod("invoke", null);
return m.invoke(c.newInstance(), null);
} catch (Exception e) {
throw new RuntimeException(e);
}
} public static void main(String[] args) {
String s="1+2+3+4+5+6+7+8+9+10";
int a = SystemEx.evalInt("return "+s+";");
int b=SystemEx.evalInt("return "+"453*434+2453"+";");
System.out.println(a);
System.out.println(b);
}}
str1 = _str.substring(_str.lastIndexOf("=")+1).trim();