首页 > 代码库 > JAVA实现简单四则混合运算

JAVA实现简单四则混合运算

JAVA实现简单四则混合运算,说明:该计算器支持实则混合运算,如 2*(3+1 )/ 4-3 *9+ 8/ 3*4- 5,则输出:-19.333332 需要说明的事括号必须是英文的。源码如下仅供学习:

运行后直接在Console里面敲然后回车即可

[1].[代码] [Java]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
 
 
publicclass Test_03 {
     
    publicstatic void main(String[] args){
         
//      String str=" 11 12 13 56 ";
//      String[] data=http://www.mamicode.com/str.split("//s");
//      for(int i=0;i<data.length;i++){
//          System.out.println(data[i]);
//      }
         
        BufferedReader reader=null;
        InputStreamReader inputStreamReader=null;
//      ArrayList<String>opt=new ArrayList<String>();
        try{
            System.out.println("请输入表达式:或直接输入#结束运算!");
            inputStreamReader=newInputStreamReader(System.in);
            reader=newBufferedReader(inputStreamReader);
            String str=reader.readLine();
            while(!str.equals("#")){
                System.out.println("运算结果为:"+opt(str)+"");
                System.out.println("请输入表达式:或直接输入#结束运算!");
                str=reader.readLine();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        if(reader!=null){
            try{
                reader.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
     
    publicstatic float opt(String s) throwsException{
        if(s == null|| "".equals(s.trim())) {
            return0f;
        }
        inta1=s.indexOf("+");
        inta2=s.indexOf("-");
        inta3=s.indexOf("*");
        inta4=s.indexOf("/");
        inta5=s.indexOf("(");
        if(a1==-1&&a2==-1&&a3==-1&&a4==-1){
            if(s.trim()==null||"".equals(s.trim())){
                thrownew Exception("operate error");
            }
            returnFloat.parseFloat(s.trim());
        }
         
        if(a5!=-1){
        inta6=s.indexOf(")");
            if(a6==-1){
                thrownew Exception("括号不匹配");
            }else{
                floatf=opt(s.substring(a5+1,a6).trim());
                s=s.replace(s.substring(a5,a6+1), String.valueOf(f));
                returnopt(s);
            }
        }
         
        if(a1!=-1){
            returnopt(s.substring(0,a1))+opt(s.substring(a1+1,s.length()));
        }
        if(a2!=-1){
            returnopt(s.substring(0,a2))-opt(s.substring(a2+1,s.length()));
        }
        if(a3!=-1){
            returnopt(s.substring(0,a3))*opt(s.substring(a3+1,s.length()));
        }
        if(a4!=-1){
            returnopt(s.substring(0,a4))/opt(s.substring(a4+1,s.length()));
        }
        returnInteger.parseInt(s.trim());
    }
}

JAVA实现简单四则混合运算