如何计算含有括号的表达式
例如:6+3*4+((6/5)+3)+(3+7)*10
用什么方法能方便计算

解决方案 »

  1.   

    沙发先坐了
    不要恶搞哈
    拿java做
      

  2.   

    在java中利用动态编译实现eval-JSP教程,Java技巧及代码 我们知道,在很多脚本语言中都有eval涵数,它可以把字符串转换为表态式并执行.如在javascript中 var str = aid.value + ".style.top = 10;" 把一个id为"aid"的控制的值取出来加合并成一个字符串,如果aid的值是"axman",则 
    str = "axman.style.top = 10" 
    现在我们要让控制axman移动到顶部为10的位置: eval(str); 这样这个字符串就变成了表达式或语句开始执行.这样的功能对于动态构造变量是有非常重要的 
    意义. 那么在java中,如果实现这个功能呢?其实我们可以用动态编译来实现: 假设有一组方法实现不同的功能,现在要根据传进来的方法名调用相应的方法,假如没有eval功能,我们 
    只能这么做: myclass mc = new myclass(); 
    if(str.equals("m1")) 
    mc.m1(); 
    else if(str.equals("m1")) 
    mc.m2(); 
    else if(str.equals("m3")) 
    mc.m3(); 
    else if(.........) 
    .........(); 如果有一百种情况呢? 如果我们用eval方法就可以直接这样: string str = ...........; 
    eval("mc"+str+"();"); 是不是非常方便?关键是如何实现eval()? 我们把要转换的字符串构造一个完整的类:如果方法是有返回值的.则: public object eval(string str){ 
    //生成java文件 
    string s = "class temp{"; 
    s += "object rt(){" 
    s += "myclass mc = new myclass();" 
    s += " return mc."+str+"();"; 
    s += "}" 
    s +="}"; 
    file f = new file("temp.java"); 
    printwriter pw = new printwriter(new filewriter(f)); 
    pw.println(s); 
    pw.close(); 
    //动态编译 
    com.sun.tools.javac.main javac = new com.sun.tools.javac.main(); 
    string[] cpargs = new string[] {"-d", "所在目录","temp.java"}; 
    int status = javac.compile(cpargs); 
    if(status!=0){ 
    system.out.println("没有成功编译源文件!"); 
    return null; 

    //调用temp的rt方法返回结果: 
    myclassloader mc = new myclassloader(); 
    class clasz = mc.loadclass("test.class",true); 
    method rt = clasz.getmethod("rt", new class[]{ string[].class }); 
    return rt.invoke(null, new object[] { new string[0] }); 
    //如果方法没有返回就直接调用 
    } 我们可以先写好多个重载的eval,有返回值和没有返回值的.以及可以传递参数的. 这样我们就可以用字符串转换为java的语句来执行. 本文只是一个例子,说明了一个动态编译的思想,更好的实现请各位朋友自己来完成.
      

  3.   

    我自己写的算法,给你参考一下:http://community.csdn.net/Expert/topic/4951/4951826.xml?temp=.2636072
      

  4.   

    不用那么麻烦,用java.lang.Math类,有一堆的函数,而且你这个表达式很简单,我看连java.lang.Math类都用不上,直接赋值计算即可。
      

  5.   

    double test = 6+3*4+((6.0/5.0)+3)+(3+7)*10;
      

  6.   

    湖北武汉王

    以前就做过
    不愧五颗星的牛同时感谢theforever(碧海情天)de Eval函数
      

  7.   

    为了自己看方便点
    hbwhwang
    把你那边帖子贴过来了import java.io.*;
    import java.util.*;public class ComputeExpression {
        public ComputeExpression() {
        }
        public static String readFromFile(String filename){
            String rtn=null;
            try{
                BufferedReader in = new BufferedReader( new FileReader(filename));
                rtn=in.readLine();
                in.close();
            }catch(Exception ex){
                ex.printStackTrace();
            }
            return rtn;
        }
        public static String processParentheses(String src){
            src=src.replace('{','(');
            src=src.replace('[','(');
            src=src.replace(']',')');
            src=src.replace('}',')');        return src;
        }
        public static int compute(String exp){
            List<Integer> num=new ArrayList<Integer>();
            List<Character> op=new ArrayList<Character>();        byte[] e=exp.getBytes();
            int pos=0;
            for (int i=0;i<e.length;i++){
                if (e[i]=='('){
                    int count=0;
                    for (int j=i+1;j<e.length;j++){
                        if (e[j]==')'&&count==0){
                            num.add(compute(exp.substring(i+1,j)));
                            i=j;
                            pos=-1;
                            break;
                        }else if(e[j]==')'){
                            count--;
                        }else if(e[j]=='('){
                            count++;
                        }
                    }
                } else if(e[i]=='+'||e[i]=='-'||e[i]=='*'||e[i]=='/'){
                    if (pos!=-1){
                        num.add(Integer.parseInt(exp.substring(pos, i).trim()));
                    }
                    op.add((char)e[i]);
                    pos=i+1;
                }
            }
            if (pos<e.length&&pos!=-1){
                num.add(Integer.parseInt(exp.substring(pos,e.length).trim()));
            }        System.out.print(num.get(0));
            for (int i=0;i<op.size();i++){
                System.out.print(op.get(i));
                System.out.print(num.get(i+1));
            }
            for (int i=0;i<op.size();i++){
                if (op.get(i).equals(new Character('*'))){
                    Integer i1=num.get(i);
                    Integer i2=num.get(i+1);
                    num.set(i,i1.intValue()*i2.intValue());
                    num.remove(i+1);
                    op.remove(i);
                    i--;
                } else if (op.get(i).equals(new Character('/'))){
                    Integer i1 = num.get(i);
                    Integer i2 = num.get(i + 1);
                    num.set(i, i1.intValue() / i2.intValue());
                    num.remove(i + 1);
                    op.remove(i);
                    i--;
                }
            }        for (int i=0;i<op.size();i++){
                if (op.get(i).equals(new Character('+'))){
                    Integer i1=num.get(i);
                    Integer i2=num.get(i+1);
                    num.set(i,i1.intValue()+i2.intValue());
                    num.remove(i+1);
                    op.remove(i);
                    i--;
                } else if (op.get(i).equals(new Character('-'))){
                    Integer i1 = num.get(i);
                    Integer i2 = num.get(i + 1);
                    num.set(i, i1.intValue() - i2.intValue());
                    num.remove(i + 1);
                    op.remove(i);
                    i--;
                }
            }        System.out.println("="+num.get(0));
            return num.get(0).intValue();
        }    public static void main(String[] args) {
            String exp=readFromFile("f:\\temp\\1.txt");
            exp=processParentheses(exp);
            System.out.println(exp+"="+compute(exp));
        }
    }