• Java_计算器001,支持加减乘除,括号,小数点,√,^ 运算


    对于001版本变更了对于负数的处理方式,增加了小数、√、^

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