比如有一字符串(1*2)+3/4+5^2,如何分离字符,并实现运算的。。

解决方案 »

  1.   

    package com.self;public class Ex2 
    {
    public static void main(String[] args) 
    {
    String str="(1*2)+3/4+5^2";
    char [] ch=str.toCharArray();
    for(int i=0;i<ch.length;i++)
    {
    System.out.print(ch[i]+" ");
    }
    System.out.println();
    System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2)));
    }
    }这个简单,我来,呵呵~~~
      

  2.   

    package com.self;public class Ex2 
    {
    public static void main(String[] args) 
    {
    String str="(1*2)+3/4+5^2";
    char [] ch=str.toCharArray();
    for(int i=0;i<ch.length;i++)
    {
    System.out.print(ch[i]+" ");
    }
    System.out.println();
    System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2)));
    }
    }
      

  3.   

    [code=Java][
    package com.self;public class Ex2 
    {
    public static void main(String[] args) 
    {
    String str="(1*2)+3/4+5^2";
    char [] ch=str.toCharArray();
    for(int i=0;i<ch.length;i++)
    {
    System.out.print(ch[i]+" ");
    }
    System.out.println();
    System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2)));
    }
    }]
      

  4.   

    String类中的subString()和indexOf() 方法来做!
    具体自己想想想,要是还做不出来,就给留言!
      

  5.   

    用jdk1.6编译执行import javax.script.*;
    public class Test {
    public static void main(String[] args) throws Exception {
    String exp = "(1*2)+3/4+5^2";

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    Object result = engine.eval(exp);

    System.out.println("结果类型:" + result.getClass() + ",计算结果:" + result);
    }
      

  6.   

    这一个表达式,还可以用jexl来执行http://commons.apache.org/jexl/index.html
      

  7.   

    没太理解楼主的意思,如果楼主想将你的表达式分离的话直接用toArray方法生成一个char数据就好了,如果要是想计算结果的话,那你就直接将^2换成一个幂乘函数就可以了,java有相应的函数的。。