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
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());
     }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值