Java_计算器001,支持加减乘除,括号运算

暑假自学Java期间,为了练习Java编写的计算器001版本

  1 import java.util.ArrayList;
  2 //v1.0支持+-*/,负号
  3 import java.util.Scanner;
  4 public class Caculator_001
  5 {
  6     public static void main(String[] args)
  7     {
  8         Scanner input=new Scanner(System.in);
  9         double result;
 10         String strIn;//="1+(2-3*(5+1)/(-4+2*(-6))-3*(+6-(-2)))+6*(5-4)";
 11         System.out.println("请输入需要计算的式子:(不要有空格)");
 12         strIn=input.nextLine();
 13         center(strIn);
 14        
 15         input.close();
 16     }
 17     
 18     private static void center(String strIn)//计算器主体
 19     {
 20         //计算前进行验证处理
 21         judge(strIn);
 22         String str0=strIn;//留存备份
 23         strIn=minus(strIn);//负号
 24         strIn=plus(strIn);//正号
 25         strIn=addBrackets(strIn);//保持最外围有括号
 26         ArrayList<String> jiSuan1=new ArrayList<String>();
 27         ArrayList<Double> jiSuan2=new ArrayList<Double>();
 28         ArrayList<Double> re=new ArrayList<Double>();
 29         ArrayList<Double> num=separateNum(strIn);//分离数字
 30         ArrayList<Integer> serNum=new ArrayList<Integer>();
 31         ArrayList<String>  sym=separateSym(strIn);//分离运算符
 32         serNum=serialNumber(sym);//统计数字
 33         double result=0;//总计算结果
 34         int cm=brackets0(sym);//总括号数
 35         int bra [][]=new int[cm][2];
 36         bra=brackets(sym);//括号统计
 37         
 38         int m=0;//m 最大层数
 39         for(int i=0;i<bra.length;i++)
 40         {
 41             if(m<bra[i][0])
 42             {
 43                 m=bra[i][0];
 44             }
 45         }
 46         int i=0,k,t=0,d,f=1,g=1,n,r1=-1,r2=0,l1=-1,l2=-1;//t,d 相对应的一对括号
 47         for(i=m;i>=0;i--)//层数循环
 48         {
 49             for(t=0;t<bra.length;t++)
 50             {
 51                 if(bra[t][0]==i)//每一对括号
 52                 {
 53                     d=t;
 54                     jiSuan1.clear();//清空
 55                     jiSuan2.clear();
 56                     re.clear();
 57                     for(;;)//找出相对应的另一半括号
 58                     {
 59                         d++;
 60                         if(bra[d][0]==bra[t][0])
 61                         {
 62                             break;
 63                         }
 64                     }
 65                     l1=bra[t][1];
 66                     r1=bra[d][1];
 67                     for(k=bra[t][1]+2;k<bra[d][1];k+=2)//将需要计算的部分输入jiSuan1中
 68                     {
 69                         
 70                         jiSuan1.add(sym.get(k));
 71                         /*if(g1==1)
 72                         {
 73                             k1=k;
 74                             g1=0;
 75                         }*/
 76                     }
 77                 //    sym.add(bra[t][0],"0");
 78                     for(k=0,g=1,l2=0,r2=0;k<serNum.size();k++)//将需要计算的部分输入jiSuan2中serNum.get(k)
 79                     {
 80                         n=serNum.get(k);
 81                         if(n>bra[t][1]&&n<bra[d][1])
 82                         {
 83                             jiSuan2.add(num.get(k));
 84                             r2++;
 85                         if(g==1)
 86                             {
 87                                 l2=k;
 88                                 g=0;
 89                             }
 90                         }
 91                     }
 92                     for(int x=l1;x<=r1&&l1>=0&&r1>=0;x++)
 93                     {
 94                         sym.set(x, "@");
 95                     }
 96                     //调用函数计算
 97                     result=caculate(jiSuan1,jiSuan2);
 98                     //删除
 99                     int z=l1;
100                     while(z>=0)
101                     {
102                         sym.remove(z);
103                         z=sym.indexOf("@");
104                         
105                     }
106                     sym.add(l1,"0");
107                     for(z=0;z<r2;z++)
108                     {
109                         num.remove(l2);
110                     }
111                     num.add(l2, result);
112                     //计算
113                     bra=brackets(sym);
114                     serNum=serialNumber(sym);
115                     //System.out.println(d);
116                     t=0;
117                 }
118             }
119         }
120         output(str0,result);
121 
122     }
123 
124     private static ArrayList<String> sToA(String s)  //将 String 转化为 ArrayList
125     {
126        ArrayList<String> a=new ArrayList<String>();
127        for(int i=0;i<s.length();i++)
128        {
129            a.add(s.substring(i, i+1));
130        }
131        return a;
132     
133     }
134     private static String aToS(ArrayList<String> a)  //将 String 转化为 ArrayList
135     {
136        String s="";
137        for(int i=0;i<a.size();i++)
138        {
139            s=s+a.get(i);
140        }
141       return s;
142     }
143     
144     private static String minus(String s)//对负数进行处理:前面加0
145     {
146         String a,b;
147           int i=1;
148           if(s.charAt(0)=='-')
149           {
150               s="0"+s;
151           }
152           while(i!=-1)
153         {
154             i=s.indexOf('-', i);
155             if(i>=0)
156             {
157                 
158                 if(i>0&&s.charAt(i-1)=='(')
159                 {
160                 a=s.substring(0, i);
161                 b=s.substring(i);
162                 s=a+"0"+b;
163                 i+=2;
164                 }
165                 else
166                 {
167                     i++;
168                 }
169                 
170             }
171             else
172                 break;
173             //System.out.println(s);
174         }
175         return s;
176     }
177     private static String plus(String s)//对显式写出的正数进行处理:前面加0
178     {
179         String a,b;
180           int i=1;
181           if(s.charAt(0)=='+')
182           {
183               s="0"+s;
184           }
185           while(i!=-1)
186         {
187             i=s.indexOf('+', i);
188             if(i>=0)
189             {
190                 
191                 if(i>0&&s.charAt(i-1)=='(')
192                 {
193                 a=s.substring(0, i);
194                 b=s.substring(i);
195                 s=a+"0"+b;
196                 i+=2;
197                 }
198                 else
199                 {
200                     i++;
201                 }
202                 
203             }
204             else
205                 break;
206             //System.out.println(s);
207         }
208         return s;
209     }
210     private static String addBrackets(String s)
211     {
212         if(!(s.charAt(0)=='('&&s.charAt(s.length()-1)==')'))
213         {
214             s="("+s+")";
215         }
216         return s;
217     }
218     private static int brackets0(ArrayList<String> str)   //实现括号的粗略统计
219     {
220         ArrayList<String> s=new ArrayList<String>(str);
221         int c=0,i=0;
222         for(;;)
223         {
224             if((i=s.indexOf("("))<0)
225             {
226                 break;
227             }
228             s.remove(i);
229                 c++;
230         }
231         for(;;)
232         {
233             if((i=s.indexOf(")"))<0)
234             {
235                 break;
236             }
237             s.remove(i);
238                 c++;
239         }
240             return c;
241     }
242     
243     private static ArrayList<Integer> serialNumber(ArrayList<String> s) //实现数字的统计
244     {
245         ArrayList<Integer> a=new ArrayList<Integer>();
246         int i=0;
247         String str;
248         char c;
249         for(i=0;i<s.size();i++)
250         {
251             str=s.get(i);
252             c=str.charAt(0);
253             if(c>='0'&&c<='9')
254             {
255                 a.add(i);
256             }
257             
258         }
259         return a;
260     }
261     private static int[][] brackets( ArrayList<String> sym) //实现括号的统计
262     {
263        //                   +(-*(+)/(-+*(-))-*(+-(-)))+*(-)
264        ArrayList<Integer> b1=new ArrayList<Integer>();//层数
265        ArrayList<Integer> b2=new ArrayList<Integer>();//位置
266        int c=-1;//层数
267        int cm=0;//最大层数
268        int i,f=1;
269       String s=aToS(sym);
270        for( i=0;i<s.length();i++)
271        {
272        
273            if(s.charAt(i)=='(')
274            {
275                if(f==1)
276                {
277                c++;
278                }
279                f=1;
280                b1.add(c);
281             b2.add(i);
282            }
283            if(s.charAt(i)==')')
284            {
285                if(f==0)
286                {
287                c--;
288                }
289                f=0;
290                b1.add(c);
291             b2.add(i);
292            }
293            if(cm<c)
294            {
295                cm=c;
296            }
297            
298        }
299        
300 
301              int bra[][]=new int[b1.size()][2];//第一 维序号,第二维层数、位置
302              for(i=0;i<b1.size();i++)
303              {
304                  bra[i][0]=b1.get(i);
305                  bra[i][1]=b2.get(i);
306              }
307          
308         return bra;
309     
310     }
311     
312    private static double caculate(ArrayList<String> s,ArrayList<Double> a) //计算
313    
314     {
315        double result=0,left,right;
316        int i=0;
317        while(i>=0)
318        {
319                i=s.indexOf("/");
320                if(i<0)break;
321                left=a.remove(i);
322                right=a.remove(i);
323                try
324                {
325                    if(Math.abs(right)<10e-8)
326                    {
327                        throw new Exception("除数不能为零!");
328                    }
329                    a.add(i, left/right);
330                }
331                catch(Exception e)
332                {System.out.println(e.getMessage());
333                }
334                s.remove(i);
335        }
336        i=0;
337           while(i>=0)
338           {
339               i=s.indexOf("*");
340               if(i<0)break;
341               left=a.remove(i);
342               right=a.remove(i);
343               a.add(i, left*right);
344               s.remove(i);
345           }
346           i=0;
347           while(i>=0)
348           {
349               i=s.indexOf("-");
350               if(i<0)break;
351               left=a.remove(i);
352               right=a.remove(i);
353               a.add(i, left-right);
354               s.remove(i);
355           }
356           i=0;
357           while(i>=0)
358           {
359               i=s.indexOf("+");
360               if(i<0)break;
361               left=a.remove(i);
362               right=a.remove(i);
363               a.add(i, left+right);
364               s.remove(i);
365           }
366        
367     //end
368            result=a.get(0);
369         return result;
370     }
371 
372     
373     private static ArrayList<Double> separateNum(String s) 
374     {
375         ArrayList<Double> num=new ArrayList<Double>();
376         String c="";
377         int i=0,t=0,f=0,l;
378         double d=0,a;
379         for(i=0;i<s.length();i++)
380         {
381             if(s.charAt(i)>='0'&&s.charAt(i)<='9')
382             {
383                 c=c+s.charAt(i);
384                 f=1;
385             //    System.out.println("add"+c);
386             }
387             else if(f==1)
388             {
389                 //字符转数字
390                 l=c.length();
391                 for(t=0,d=0;t<l;t++)
392                 {
393                     a=c.charAt(t)-'0';
394                     
395                     d=d+a*Math.pow(10,l-1-t);    
396                     
397                 }
398   
399                 num.add(d);
400                 f=0;
401                 c="";
402             }
403         }
404  
405         return num;
406     }
407     private static ArrayList<String> separateSym(String s)
408     {
409         ArrayList<String> sym=new ArrayList<String>();
410         int f=1;
411         if(s.charAt(0)<'0'||s.charAt(0)>'9')
412         {
413             s="0+"+s;
414         }
415             for(int i=0;i<s.length();i++)
416             {
417                 if(s.charAt(i)>='0'&&s.charAt(i)<='9')
418                 {
419                     f=1;
420                 }
421                 else if(f==1)
422                 {
423                     sym.add("0");
424                     f=0;
425                 }
426                 if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='*'||s.charAt(i)=='/'||s.charAt(i)=='('||s.charAt(i)==')')
427                 {
428                     sym.add(s.substring(i,i+1));
429                 }
430                     
431             }
432             //System.out.println(sym);
433             sym.remove(0);
434             sym.remove(0);
435         return sym;
436     }
437     private static void judge(String s)//验证式子是否正确
438     {
439         try
440         {
441             //字符是否正确
442             for(int i=0,f=0;i<s.length();i++)
443             {
444                 f=0;
445                 if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='*'||s.charAt(i)=='/'||s.charAt(i)=='('||s.charAt(i)==')')
446                 {
447                     f=1;
448                 }
449                 if(s.charAt(i)>='a'&&s.charAt(i)<='z'||s.charAt(i)>='A'&&s.charAt(i)<='Z')
450                 {
451                     f=1;
452                 }
453                 if(s.charAt(i)>='0'&&s.charAt(i)<='9')
454                 {
455                     f=1;
456                 }
457                 if(f==0)throw  new Exception("未识别的符号\" "+s.charAt(i)+" \",位置:"+(i+1));
458             }
459             //括号是否匹配
460             int left=0,right=0;
461         for(int i=0;i<s.length();i++)
462         {
463             if(s.charAt(i)=='(')
464             {
465                 left++;
466             }
467             if(s.charAt(i)==')')
468             {
469                 right++;
470             }
471             
472         }
473         if(left!=right)throw  new Exception("括号不匹配");
474         //符号是否正确
475         int f=1;
476         if(s.charAt(0)=='-')
477         {
478             s="0"+s;
479             f=0;
480         }
481         for(int i=0;i<s.length();i++)
482         {
483             if(s.charAt(i)=='+'||s.charAt(i)=='-')
484             {
485                 if(s.charAt(i-1)=='+'||s.charAt(i-1)=='-'||s.charAt(i-1)=='*'||s.charAt(i-1)=='/')
486                 {
487                     throw  new Exception("运算符"+s.charAt(i)+"左边没有数字,位置:"+(i+f));
488                 }
489                 if(s.charAt(i+1)=='+'||s.charAt(i+1)=='-'||s.charAt(i+1)=='*'||s.charAt(i+1)=='/'||s.charAt(i+1)==')')    
490                 {
491                     throw  new Exception("运算符"+s.charAt(i)+"右边没有数字,位置:"+(i+f));
492                 }
493                 if(s.charAt(s.length()-1)=='+'||s.charAt(s.length()-1)=='-'||s.charAt(s.length()-1)=='*'||s.charAt(s.length()-1)=='/')    
494                 {
495                     throw  new Exception("运算符"+s.charAt(s.length()-1)+"右边没有数字,位置:"+(s.length()-1+f));
496                 }
497             }
498             if(s.charAt(i)=='*'||s.charAt(i)=='/')
499             {
500                 if(s.charAt(i-1)=='+'||s.charAt(i-1)=='-'||s.charAt(i-1)=='*'||s.charAt(i-1)=='/'||s.charAt(i-1)=='(')
501                 {
502                     throw  new Exception("运算符"+s.charAt(i)+"左边没有数字,位置:"+(i+f));
503                 }
504                 if(s.charAt(i+1)=='+'||s.charAt(i+1)=='-'||s.charAt(i+1)=='*'||s.charAt(i+1)=='/'||s.charAt(i+1)==')')    
505                 {
506                     throw  new Exception("运算符"+s.charAt(i)+"右边没有数字,位置:"+(i+f));
507                 }
508                 if(s.charAt(s.length()-1)=='+'||s.charAt(s.length()-1)=='-'||s.charAt(s.length()-1)=='*'||s.charAt(s.length()-1)=='/')    
509                 {
510                     throw  new Exception("运算符"+s.charAt(s.length()-1)+"右边没有数字,位置:"+(s.length()-1+f));
511                 }
512             }
513         }
514         }catch(Exception e)
515         {
516             System.out.println(e.getMessage());
517         }
518     }
519    
520     private static void output(String s,double b)
521     {
522         System.out.println(s+" = "+b);
523     }
524 }

猜你喜欢

转载自www.cnblogs.com/26never/p/9576473.html