<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ALL is Well!

    敏捷是一條很長(zhǎng)的路,摸索著前進(jìn)著

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      30 隨筆 :: 23 文章 :: 71 評(píng)論 :: 0 Trackbacks

    廢話(huà)不說(shuō)了,

    文件:

    A{1,2,3,4,5,6}
    B{7,4,5,6,8}
    C{2,3,12,14,4,11}

     

    測(cè)試時(shí)輸入到控制臺(tái)的字符串為:

    C+B-(A*(C-A))+B

     

    結(jié)果:

    2 3 12 14 4 11 7 8 1 5 6

     

    自己算了一下,是正確的!

     

    代碼如下,注釋也寫(xiě)的蠻多的:

      1/** 
      2 * 從事先寫(xiě)好的Input.txt文件中讀取數(shù),  
      3 *  Input.txt 內(nèi)容  
      4 *  A{13,2,1,20,30,50}  
      5 *  B{1,2,34,5,6}  
      6 *  C{2,3,12,23,14,11}  
      7 *  用戶(hù)在鍵盤(pán)隨意敲入例如((A*B))+B-C,((C+B)*A)-B期中+,*,-,分別代表集合的并交差運(yùn)算,控制臺(tái)打印輸出。 
      8 */
     
      9package com.lim.test; 
     10
     11import java.io.BufferedReader; 
     12import java.io.FileInputStream; 
     13import java.io.IOException; 
     14import java.io.InputStreamReader; 
     15import java.lang.reflect.InvocationTargetException; 
     16import java.lang.reflect.Method; 
     17import java.util.ArrayList; 
     18import java.util.List; 
     19import java.util.Stack; 
     20
     21/** 
     22 * @author bzwm 
     23 *  
     24 */
     
     25public class EditorStringV2 
     26    // 保存住集合A中的數(shù)據(jù) 
     27    private Type typeA = null
     28
     29    // 保存住集合B中的數(shù)據(jù) 
     30    private Type typeB = null
     31
     32    // 保存住集合C中的數(shù)據(jù) 
     33    private Type typeC = null
     34
     35    private Stack stack = new Stack(); 
     36
     37    public EditorStringV2(String path) 
     38        readFile(path); 
     39    }
     
     40
     41    /** 
     42     * 讀入指定的文件 
     43     *  
     44     * @param path 
     45     */
     
     46    private void readFile(String path) 
     47        BufferedReader reader = null
     48        try 
     49            reader = new BufferedReader(new InputStreamReader( 
     50                    new FileInputStream(path))); 
     51            String str = null
     52            // 保存已經(jīng)寫(xiě)好的文件中A,B,C的集合 
     53            while ((str = reader.readLine()) != null
     54                // 保存集合A 
     55                if (str.substring(01).equals("A")) 
     56                    typeA = new Type(str); 
     57                }
     
     58                // 保存集合B 
     59                else if (str.substring(01).equals("B")) 
     60                    typeB = new Type(str); 
     61                }
     
     62                // 保存集合C 
     63                else if (str.substring(01).equals("C")) 
     64                    typeC = new Type(str); 
     65                }
     else 
     66                    System.out.println("no such type!"); 
     67                    return
     68                }
     
     69            }
     
     70        }
     catch (Exception e) 
     71            e.printStackTrace(); 
     72            return
     73        }
     
     74    }
     
     75
     76    /** 
     77     * 處理并、交、差操作,顯示結(jié)果 
     78     *  
     79     * @param rule 
     80     *            例:C-((C+B)-A)*B 
     81     * @throws InvocationTargetException 
     82     * @throws IllegalAccessException 
     83     * @throws NoSuchMethodException 
     84     * @throws IllegalArgumentException 
     85     * @throws SecurityException 
     86     */
     
     87    public Stack displayResult(Stack orgStack) throws SecurityException, 
     88            IllegalArgumentException, NoSuchMethodException, 
     89            IllegalAccessException, InvocationTargetException 
     90        // 左括號(hào)"("的計(jì)數(shù)器 
     91        int leftBracket = 0
     92        // 是否存在操作符標(biāo)志符 
     93        boolean hasOpe = false
     94        // 輸入的rule的長(zhǎng)度 
     95        int length = orgStack.size(); 
     96        Object obj = null
     97        if (length < 3
     98            System.out.println("input rule is illegal."); 
     99            return null
    100        }
     else 
    101            for (int i = 0; i < length; i++
    102                // 截取rule的每個(gè)字符 
    103                obj = orgStack.pop(); 
    104                // 如果是左括號(hào),則leftBracket加1 
    105                if (isLeftBracket(obj)) 
    106                    leftBracket += 1
    107                    stack.push(obj); 
    108                    continue
    109                }
     
    110                // 如果是操作符,則將操作符標(biāo)志符置為true 
    111                if (isOperator(obj)) 
    112                    hasOpe = true
    113                    stack.push(obj); 
    114                    continue
    115                }
     
    116                // 如果不是左括號(hào)和操作符則入棧 
    117                stack.push(obj); 
    118                // 如果左括號(hào)存在,且本次字符為右括號(hào) 
    119                if (leftBracket > 0 && isRightBracket(obj)) 
    120                    // 將右括號(hào)彈出棧 
    121                    stack.pop(); 
    122                    // 將形如typeA.bing(typeB)的方法調(diào)用的參數(shù)標(biāo)志彈出 
    123                    Type arg = (Type) stack.pop(); 
    124                    // 將形如typeA.bing(typeB)的方法調(diào)用的操作符標(biāo)志彈出:+/bing */jiao -/cha 
    125                    String ope = stack.pop().toString(); 
    126                    // 將形如typeA.bing(typeB)的方法調(diào)用的主調(diào)對(duì)象標(biāo)志彈出 
    127                    Type invokeObj = (Type) stack.pop(); 
    128                    // 通過(guò)對(duì)象工廠,構(gòu)造出Type對(duì)象,進(jìn)行并、交、差操作,返回得到的新Type對(duì)象 
    129                    Type typeTmp = execute(invokeObj, ope, arg); 
    130                    // 將左括號(hào)彈出棧 
    131                    stack.pop(); 
    132                    // 左括號(hào)計(jì)數(shù)器減1 
    133                    leftBracket -= 1
    134                    // 棧中加入臨時(shí)的Type對(duì)象標(biāo)志T,T代表本次操作后得到的新集合 
    135                    stack.push(typeTmp); 
    136                    // 當(dāng)棧中還有運(yùn)算時(shí),進(jìn)行遞歸 
    137                    if (stack.size() > 2
    138                        Stack tmpStack = new Stack(); 
    139                        while (stack.size() > 0
    140                            tmpStack.push(stack.pop()); 
    141                        }
     
    142                        stack = displayResult(tmpStack); 
    143                    }
     
    144                    continue
    145                }
     
    146
    147                // 如果1.棧中還沒(méi)有左括號(hào) 2.棧有操作符 3.本次字符是集合標(biāo)志A、B、C 
    148                // 則進(jìn)行并、交、差操作 
    149                if (leftBracket == 0 && hasOpe && isType(obj)) 
    150                    // 將形如typeA.bing(typeB)的方法調(diào)用的參數(shù)標(biāo)志彈出 
    151                    Type arg = (Type) stack.pop(); 
    152                    // 將形如typeA.bing(typeB)的方法調(diào)用的操作符標(biāo)志彈出:+/bing */jiao -/cha 
    153                    String ope = stack.pop().toString(); 
    154                    // 將形如typeA.bing(typeB)的方法調(diào)用的主調(diào)對(duì)象標(biāo)志彈出 
    155                    Type invokeObj = (Type) stack.pop(); 
    156                    // 通過(guò)對(duì)象工廠,構(gòu)造出Type對(duì)象,進(jìn)行并、交、差操作,返回得到的新Type對(duì)象 
    157                    Type typeTmp = execute(invokeObj, ope, arg); 
    158                    // 棧中加入臨時(shí)的Type對(duì)象標(biāo)志T,T代表本次操作后得到的新集合 
    159                    stack.push(typeTmp); 
    160                    // 將操作符標(biāo)志符置為false 
    161                    hasOpe = false
    162                    // 當(dāng)棧中還有運(yùn)算時(shí),進(jìn)行遞歸 
    163                    if (stack.size() > 2
    164                        Stack tmpStack = new Stack(); 
    165                        while (stack.size() > 0
    166                            tmpStack.push(stack.pop()); 
    167                        }
     
    168                        stack = displayResult(tmpStack); 
    169                    }
     
    170                    continue
    171                }
     
    172            }
     
    173            // 循環(huán)結(jié)束,得到最后結(jié)果 
    174            return stack; 
    175        }
     
    176    }
     
    177
    178    /** 
    179     * 判斷對(duì)象o是否為T(mén)ype的實(shí)例 
    180     *  
    181     * @param o 
    182     * @return 
    183     */
     
    184    private boolean isType(Object o) 
    185        return o instanceof Type; 
    186    }
     
    187
    188    /** 
    189     * 判斷對(duì)象o是否為操作符*,+,- 
    190     *  
    191     * @param o 
    192     * @return 
    193     */
     
    194    private boolean isOperator(Object o) 
    195        return !isType(o) && ((String) o).matches("[+\\*-]"); 
    196    }
     
    197
    198    /** 
    199     * 判斷對(duì)象o是否左括號(hào)"(" 
    200     *  
    201     * @param o 
    202     * @return 
    203     */
     
    204    private boolean isLeftBracket(Object o) 
    205        return !isType(o) && ((String) o).equals("("); 
    206    }
     
    207
    208    /** 
    209     * 判斷對(duì)象o是否右括號(hào)")" 
    210     *  
    211     * @param o 
    212     * @return 
    213     */
     
    214    private boolean isRightBracket(Object o) 
    215        return !isType(o) && ((String) o).equals(")"); 
    216    }
     
    217
    218    /** 
    219     * 利用反射機(jī)制,根據(jù)ope的不同,調(diào)用不同的方法 
    220     *  
    221     * @param obj 
    222     * @param arg 
    223     * @param ope 
    224     * @return 
    225     * @throws SecurityException 
    226     * @throws NoSuchMethodException 
    227     * @throws IllegalArgumentException 
    228     * @throws IllegalAccessException 
    229     * @throws InvocationTargetException 
    230     */
     
    231    private Type execute(Type obj, String ope, Type arg) 
    232            throws SecurityException, NoSuchMethodException, 
    233            IllegalArgumentException, IllegalAccessException, 
    234            InvocationTargetException 
    235        Class c = obj.getClass(); 
    236        Class[] args = new Class[1]; 
    237        args[0= arg.getClass(); 
    238        Method m = null
    239        // 如果操作符為"+",則執(zhí)行bing方法 
    240        if (ope.equals("+")) 
    241            m = c.getMethod("bing", args); 
    242        }
     
    243        // 如果操作符為"*",則執(zhí)行jiao方法 
    244        else if (ope.equals("*")) 
    245            m = c.getMethod("jiao", args); 
    246        }
     
    247        // 如果操作符為"-",則執(zhí)行cha方法 
    248        else if (ope.equals("-")) 
    249            m = c.getMethod("cha", args); 
    250        }
     else 
    251            System.out.println("NoSuchMethod"); 
    252            return null
    253        }
     
    254        return (Type) m.invoke(obj, new Object[] { arg }); 
    255    }
     
    256
    257    /** 
    258     * 讀入用戶(hù)輸入的匹配規(guī)則 如:((C+B)*A)-B 
    259     *  
    260     * @return 
    261     */
     
    262    private Stack readInput() 
    263        Stack ret = new Stack(); 
    264        String str = null
    265        String o = null
    266        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    267        try 
    268            str = br.readLine(); 
    269        }
     catch (IOException e) 
    270            e.printStackTrace(); 
    271            return null
    272        }
     
    273        for (int i = str.length(); i > 0; i--
    274            o = str.substring(i - 1, i); 
    275            // 當(dāng)遇到A,B,C時(shí),生成Type對(duì)象存入棧中 
    276            if (o.matches("[ABC]")) 
    277                ret.push(typeFactory(o)); 
    278                continue
    279            }
     
    280            ret.push(o); 
    281        }
     
    282        return ret; 
    283    }
     
    284
    285    /** 
    286     * 構(gòu)造工廠 根據(jù)傳入的type構(gòu)造Type對(duì)象 保存住原集合 
    287     *  
    288     * @param type 
    289     * @return 
    290     */
     
    291    private Type typeFactory(String type) 
    292        if (type.equals("A")) 
    293            return new Type(typeA.getArray()); 
    294        }
     else if (type.equals("B")) 
    295            return new Type(typeB.getArray()); 
    296        }
     else if (type.equals("C")) 
    297            return new Type(typeC.getArray()); 
    298        }
     else 
    299            return null
    300        }
     
    301    }
     
    302
    303    /** 
    304     * 把如{13,2,1,20,30,50}的集合抽象成一個(gè)類(lèi),提供并、交、差操作 
    305     *  
    306     * @author bzwm 
    307     *  
    308     */
     
    309    class Type 
    310        // 保存數(shù)據(jù)集合的List 
    311        private List array = new ArrayList(); 
    312
    313        public Type(String srt) 
    314            this.array = createList(srt); 
    315        }
     
    316
    317        public Type(List list) 
    318            this.array.addAll(list); 
    319        }
     
    320
    321        public List getArray() 
    322            return this.array; 
    323        }
     
    324
    325        /** 
    326         * 并操作 
    327         *  
    328         * @param arg 
    329         * @return 
    330         */
     
    331        public Type bing(Type arg) 
    332            // 是否加入到集合中的標(biāo)志 
    333            boolean add = true
    334            // 取出傳入的Type對(duì)象的List 
    335            List list = arg.getArray(); 
    336            // 遍歷傳入的Type對(duì)象的List 
    337            for (int i = 0; i < list.size(); i++
    338                add = true
    339                // 與array里的值一一進(jìn)行比較,如果全都不等,則加入到原array中,否則不加入 
    340                for (int j = 0; j < array.size(); j++
    341                    if (((Integer) list.get(i)).intValue() == ((Integer) array 
    342                            .get(j)).intValue()) 
    343                        add = false
    344                    }
     
    345                }
     
    346                if (add) 
    347                    array.add(list.get(i)); 
    348                }
     
    349            }
     
    350            // 返回新的Type對(duì)象 
    351            return new Type(array); 
    352        }
     
    353
    354        /** 
    355         * 交操作 
    356         *  
    357         * @param arg 
    358         * @return 
    359         */
     
    360        public Type jiao(Type arg) 
    361            // 是否加入到集合中的標(biāo)志 
    362            boolean add = false
    363            // 存放交集數(shù)據(jù)的List 
    364            List ret = new ArrayList(); 
    365            // 取出傳入的Type對(duì)象的List 
    366            List list = arg.getArray(); 
    367            // 遍歷傳入的Type對(duì)象的List 
    368            for (int i = 0; i < list.size(); i++
    369                add = false
    370                // 與array里的值一一進(jìn)行比較,如果有相等的,則加入到ret中,否則不加入 
    371                for (int j = 0; j < array.size(); j++
    372                    if (((Integer) list.get(i)).intValue() == ((Integer) array 
    373                            .get(j)).intValue()) 
    374                        add = true
    375                    }
     
    376                }
     
    377                if (add) 
    378                    ret.add(list.get(i)); 
    379                }
     
    380            }
     
    381            // 返回新的Type對(duì)象 
    382            return new Type(ret); 
    383        }
     
    384
    385        /** 
    386         * 差操作 
    387         *  
    388         * @param arg 
    389         * @return 
    390         */
     
    391        public Type cha(Type arg) 
    392            // 是否加入到集合中的標(biāo)志 
    393            boolean add = true
    394            // 存放交集數(shù)據(jù)的List 
    395            List list = arg.getArray(); 
    396            // 遍歷傳入的Type對(duì)象的List 
    397            for (int i = 0; i < list.size(); i++
    398                add = true
    399                // 與array里的值一一進(jìn)行比較,如果有相等的,則從原array中將其刪除,如果全都不等,則加入到原array中 
    400                for (int j = 0; j < array.size(); j++
    401                    if (((Integer) list.get(i)).intValue() == ((Integer) array 
    402                            .get(j)).intValue()) 
    403                        add = false
    404                        // 刪除相等的數(shù)據(jù) 
    405                        array.remove(j); 
    406                    }
     
    407                }
     
    408                if (add) 
    409                    array.add(list.get(i)); 
    410                }
     
    411            }
     
    412            // 返回新的Type對(duì)象 
    413            return new Type(array); 
    414        }
     
    415
    416        /** 
    417         * 解析字符串,將數(shù)字加入到List中 
    418         *  
    419         * @param str 
    420         * @return 
    421         */
     
    422        private List createList(String str) 
    423            // 將字符串解析成字符串?dāng)?shù)組A{13,2,1,20,30,50}-->new String[]{13,2,1,20,30,50} 
    424            String s[] = str.replaceAll(str.substring(01), "").replace("{"
    425                    "").replace("}""").split(","); 
    426            List list = new ArrayList(); 
    427            for (int i = 0; i < s.length; i++
    428                list.add(new Integer(s[i])); 
    429            }
     
    430            return list; 
    431        }
     
    432    }
     
    433
    434    /** 
    435     * 測(cè)試程序 
    436     *  
    437     * @param args 
    438     * @throws InvocationTargetException 
    439     * @throws IllegalAccessException 
    440     * @throws NoSuchMethodException 
    441     * @throws IllegalArgumentException 
    442     * @throws SecurityException 
    443     */
     
    444    public static void main(String args[]) throws SecurityException, 
    445            IllegalArgumentException, NoSuchMethodException, 
    446            IllegalAccessException, InvocationTargetException 
    447        EditorStringV2 es = new EditorStringV2("input.txt"); 
    448        Stack s = es.readInput(); 
    449        Stack result = es.displayResult(s);// ((C+B)*A)-B 
    450
    451        List list = ((Type) result.pop()).getArray(); 
    452        System.out.println("操作運(yùn)算后結(jié)果為:"); 
    453        for (int i = 0; i < list.size(); i++
    454            System.out.print(list.get(i) + " "); 
    455    }
     
    456}
     
    457


    ----2008年11月26日
    posted on 2010-09-01 11:01 李 明 閱讀(425) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java
    主站蜘蛛池模板: 中文字幕专区在线亚洲| 亚洲酒色1314狠狠做| 少妇太爽了在线观看免费视频| 亚洲高清视频在线播放| 在线免费视频一区| 国产视频精品免费视频| 亚洲欧洲自拍拍偷午夜色| 免费看国产一级片| 在线观看免费视频网站色| 亚洲国产视频久久| 亚洲色偷拍另类无码专区| 毛片免费视频在线观看| WWW国产成人免费观看视频| 亚洲精品在线播放| 久久久久亚洲av毛片大| 成年在线观看网站免费| 日本免费A级毛一片| 久久精品国产亚洲AV未满十八| 亚洲av无码乱码国产精品| 国产yw855.c免费视频| 99久久精品免费精品国产| 国产亚洲女在线线精品| 亚洲成aⅴ人片在线观| 精品亚洲一区二区三区在线观看| 免费黄色福利视频| 两个人看的www视频免费完整版| 亚洲一卡2卡三卡4卡无卡下载| 亚洲AV无码不卡在线播放| 四虎永久在线精品免费观看地址 | 国产V亚洲V天堂无码| 免费看片A级毛片免费看| 99精品热线在线观看免费视频| 黄色网址在线免费观看| 中文字幕 亚洲 有码 在线| 亚洲AV无一区二区三区久久| 免费一级特黄特色大片在线观看| 免费观看的毛片大全| 久久国产免费观看精品3| 韩国免费A级毛片久久| 鲁啊鲁在线视频免费播放| 亚洲私人无码综合久久网|