码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA实现简单四则混合运算

时间:2014-12-31 08:46:11      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
 
public class Test_03 {
     
    public static void main(String[] args){
         
//      String str=" 11 12 13 56 ";
//      String[] data=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=new InputStreamReader(System.in);
            reader=new BufferedReader(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();
            }
        }
    }
     
    public static float opt(String s) throws Exception{
        if(s == null || "".equals(s.trim())) {
            return 0f;
        }
        int a1=s.indexOf("+");
        int a2=s.indexOf("-");
        int a3=s.indexOf("*");
        int a4=s.indexOf("/");
        int a5=s.indexOf("(");
        if(a1==-1&&a2==-1&&a3==-1&&a4==-1){
            if(s.trim()==null||"".equals(s.trim())){
                throw new Exception("operate error");
            }
            return Float.parseFloat(s.trim());
        }
         
        if(a5!=-1){
        int a6=s.indexOf(")");
            if(a6==-1){
                throw new Exception("括号不匹配");
            }else{
                float f=opt(s.substring(a5+1,a6).trim());
                s=s.replace(s.substring(a5,a6+1), String.valueOf(f));
                return opt(s);
            }
        }
         
        if(a1!=-1){
            return opt(s.substring(0,a1))+opt(s.substring(a1+1,s.length()));
        }
        if(a2!=-1){
            return opt(s.substring(0,a2))-opt(s.substring(a2+1,s.length()));
        }
        if(a3!=-1){
            return opt(s.substring(0,a3))*opt(s.substring(a3+1,s.length()));
        }
        if(a4!=-1){
            return opt(s.substring(0,a4))/opt(s.substring(a4+1,s.length()));
        }
        return Integer.parseInt(s.trim());
    }
}

JAVA实现简单四则混合运算

标签:

原文地址:http://blog.csdn.net/u014311064/article/details/42284849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!