编程开发 > JAVA > 文章内容

java基础知识总结(33)

2016-5-25编辑:ljnbset

使用示例

        如前所述,集合框架中的类只是提供了一种数据存储的方式,在实际使用时,可以根据逻辑的需要选择合适的集合类进行使用。

        下面以一个字符串计算的示例演示集合类的实际使用。

        该程序的功能为计算一个数字字符串,例如”1+2*31-5”、”12*30/34-450”等,的计算结果,在该示例中支持四则运算,但是不支持括号。本示例中计算的字符串要求合法。

        该程序实现的原理是:首先按照运算符作为间隔,将字符串差分为数字字符串和运算符字符串的序列,由于分拆出的字符串数量不固定,所以存储到List系列的Vector容器中,然后按照运算符的优先级进行计算。

        该程序的代码如下:

                   import java.util.*;

/**

 *计算字符串的值

 */

public class CalcStr {

         public static void main(String[] args) {

                   String s = "1+20*3/5";

                   double d = calc(s);

                   System.out.println(d);

         }      

         /**

          *计算字符串的值

          * @param s需要计算的字符串

          * @return计算结果

          */

         public static double calc(String s){

                   //拆分字符串

                   Vector v = split(s);

                   //print(v); //测试代码

                   //计算字符串

                   double d = calcVector(v);

                   return d;

         }

       

         /**

          *将字符串拆分为数字和运算符。

          *例如:"1+23*4"则拆分为:"1"、"+"、"23"、"*"和"4"

          * @param s需要拆分的字符串

          * @return拆分以后的结果

          */

         private static Vector split(String s){

                   Vector v = new Vector();

                   String content = "";

                   int len = s.length(); //字符串长度

                   char c;

                   for(int i = 0;i < len;i++){

                            c = s.charAt(i);

                            //判断是否为运算符

                            if(c == '+' ||

                                     c == '-' ||

                                      c == '*' ||

                                      c == '/'){

                                     //存储数字

                                     v.add(content);

                                     //存储运算符

                                     v.add("" + c);

                                     //清除已有字符串

                                     content = "";

                            }else{

                                     content += c; //连接字符串

                            }

                   }

                   v.add(content); //添加最后一个数字

                   return v;

         }

       

         /**

          *测试代码,输出拆分以后的结果

          * @param v需要打印的Vector对象

          */

         private static void print(Vector v){

                   int size = v.size();

                   for(int i = 0;i < size;i++){

                            System.out.println((String)v.get(i));

                   }

         }

       

         /**

          *计算Vector中的数据

          * @param v存储拆分后字符串的Vector

          * @return计算结果

          */

         private static double calcVector(Vector v){

                   int index1;

                   int index2;

                   //计算乘除

                   while(true){

                            index1 = v.indexOf("*"); //乘号索引值

                            index2 = v.indexOf("/"); //除号索引值

                            //无乘除符号

                            if(index1 == - 1 && index2 == -1){

                                     break; //结束循环

                            }                         

                            //如果有乘号

                            if(index1 != -1){

                                     //没有除号或乘号在前

                                     if(index2 == -1 || index1 < index2){

                                               String s1 = (String)v.get(index1 - 1); //第一个数字

                                               String opr = (String)v.get(index1); //运算符

                                               String s2 = (String)v.get(index1 + 1); //第二个数字

                                               //计算

                                               String answer = calc(s1,s2,opr);

                                               //计算以后的处理

                                               handle(answer,index1 - 1,v);

                                     }

                            }

                            //有除号

                            if(index2 != -1){

                                     //没有乘号或除号在前

                                     if(index1 == -1 || index2 < index1){

                                               String s1 = (String)v.get(index2 - 1); //第一个数字

                                               String opr = (String)v.get(index2); //运算符

                                               String s2 = (String)v.get(index2 + 1); //第二个数字

                                               //计算

                                               String answer = calc(s1,s2,opr);

                                               //计算以后的处理

                                               handle(answer,index2 - 1,v);

                                     }

                            }

                   }

                   //计算加

                   int index3 = v.indexOf("+");

                   while(index3 != -1){ //有加号

                            String s1 = (String)v.get(index3 - 1); //第一个数字

                            String opr = (String)v.get(index3); //运算符

                            String s2 = (String)v.get(index3 + 1); //第二个数字

                            //计算

                            String answer = calc(s1,s2,opr);

                            //计算以后的处理

                            handle(answer,index3 - 1,v);

                            //获得下一个加号的位置

                            index3 = v.indexOf("+");

                   }

                 

                   //计算减

                   index3 = v.indexOf("-");

                   while(index3 != -1){ //有加号

                            String s1 = (String)v.get(index3 - 1); //第一个数字

                            String opr = (String)v.get(index3); //运算符

                            String s2 = (String)v.get(index3 + 1); //第二个数字

                            //计算

                            String answer = calc(s1,s2,opr);

                            //计算以后的处理

                            handle(answer,index3 - 1,v);

                            //获得下一个减号的位置

                            index3 = v.indexOf("-");

                   }

                   //反馈结果

                   String data = (String)v.get(0);

                   return Double.parseDouble(data);

         }

       

         /**

          *计算两个字符串类型的值运算结果

          * @param number1数字1

          * @param number2数字2

          * @param opr运算符

          * @return运算结果

          */

         private static String calc(String number1,String number2,String opr){

java基础知识总结(32)

热点推荐

登录注册
触屏版电脑版网站地图