




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
簡單四則混合口算測試程序設(shè)計packagecn.gao.algorithm2.service;importjava.util.ArrayList;/***四則混合運算運算,如給定一個字符串"a+b*c-d/e",計算該表達式值*@paramargs*/publicclassTest11{publicArrayList<Integer>objectList;/*保存操作數(shù)數(shù)組*/publicArrayList<Integer>operatorList;/*保存操作符數(shù)組*/publicStrings;/*保存四則運算表達式字符串*/publicTest11(Strings){/*四則運算表達式測試類構(gòu)造器*/super();this.s=s;objectList=newArrayList<Integer>();operatorList=newArrayList<Integer>();}publicvoidprase()/*解析四則運算表達式字符串,將解析后的操作數(shù)及操作符分別保存在objectList和operatorList中*/{System.out.println("表達式為:"+s);intindex=0;intobject;intoperatorIndex;while(index<=s.length()-1){operatorIndex=getNextOperator(index);//System.out.println("sssssssss:"+operatorIndex);if(operatorIndex==-1){object=getNumberWithRange(index,s.length());objectList.add(object);break;}else{object=getNumberWithRange(index,operatorIndex);objectList.add(object);operatorList.add(Integer.valueOf(s.charAt(operatorIndex)));index=operatorIndex+1;}}System.out.println("解析后的操作數(shù):"+objectList);System.out.println("解析后的操作符:"+operatorList);}publicvoidcaculate()/*根據(jù)已有的objectList和operatorList來就算表達式的值*/{ArrayList<Integer>objectList2=newArrayList<Integer>();/*中間態(tài)操作數(shù)容器*/ArrayList<Integer>operatorList2=newArrayList<Integer>();/*中間態(tài)操作符容器*/booleanflag=false;/*中間態(tài)狀態(tài)輔助標志,標志上一次運算是優(yōu)先級高的運算*//*以下for循環(huán)是將objectList與operatorList里面的東西經(jīng)過一次高級運算后過濾,將操作數(shù)及低級運算保存便于后續(xù)計算*/for(inti=0,j=0;i<objectList.size()&&j<operatorList.size();i++,j++)/*i,j分別指向當前操作數(shù)容器指針和當前操作符指針*/{intopertator=operatorList.get(j);intobject=objectList.get(i);if(opertator=='*'||opertator=='/')/*處理優(yōu)先級較高的運算*/{intone=objectList.get(i);intsecond=objectList.get(i+1);intresult=getResultByOperator(one,second,opertator);if(result!=-1){if(!flag)/*上次操作是普通優(yōu)先級的*/{objectList2.add(result);}else{objectList2.remove(objectList2.size()-1);/*移除上次高優(yōu)先級存的值*/objectList2.add(result);/*將二次高優(yōu)先級的值存入*/}objectList.set(i+1,result);flag=true;}}else{if(!flag){objectList2.add(object);} operatorList2.add(opertator);flag=false;}}System.out.println("aaaaaa:"+objectList2);System.out.println("bbbbb:"+operatorList2);/*以下結(jié)算結(jié)果值*/inti;for(i=0;i<operatorList2.size();i++){intone=objectList2.get(i);intsecond=objectList2.get(i+1);intoperator=operatorList2.get(i);objectList2.set(i+1,getResultByOperator(one,second,operator));}System.out.println("表達式計算結(jié)果的值為:"+objectList2.get(i));}publicintgetResultByOperator(intone,intsecond,intoperator)/*二目運算函數(shù)的抽象,根據(jù)制定的操作符及操作數(shù)得出結(jié)果返回*/{if(operator=='+'){returnone+second;}if(operator=='-'){returnone-second;}if(operator=='*'){returnone*second;}if(operator=='/'){returnone/second;}return-1;}publicintgetNumberWithRange(intstartIndex,intendIndex)/*根據(jù)開頭索引和結(jié)尾索引獲得對應(yīng)的整數(shù)值*/{returnInteger.parseInt(s.substring(startIndex,endIndex));}publicintgetNextOperator(intindex)/*獲取字符串當前index的下一個最近的操作符索引*/{if(index>=s.length()-1){return-1; }while(index<=s.length()-1){if(isOperator(index)){//System.out.println("獲得當前操作索引符:"+s.charAt(index)+"索引位置:"+index);returnindex;}index++;}return-1;}publicbooleanisOperator(intindex)/*判斷當前索引操作數(shù)是否屬于操作符*/{inttemp=s.charAt(index);if('+'==temp||'-'==temp||'*'==temp||'/'==temp){returntrue;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T/CHTS 10095-2023雄安新區(qū)高速公路景觀設(shè)計導(dǎo)則
- T/CHC 115.3-2021 T/CAS 115.3-2021保健紡織品第3部分:磁
- T/CGCC 85-2023城市綠色貨運配送企業(yè)評價指標
- T/CGCC 15-2018定制商品服務(wù)規(guī)范
- T/CECS 10315-2023砌體結(jié)構(gòu)修復(fù)和加固用置換砂漿
- T/CECS 10183-2022槽式預(yù)埋件及系統(tǒng)性能試驗方法
- T/CECS 10025-2019綠色建材評價預(yù)制構(gòu)件
- T/CATCM 025-2023中藥工業(yè)固體廢棄物循環(huán)利用指導(dǎo)原則
- T/CARSA 1.8-2022基于低空無人機的高分衛(wèi)星遙感產(chǎn)品真實性檢驗第8部分:質(zhì)量控制
- T/CAQI 247-2022燃氣輪機進氣過濾器試驗裝置
- 人教版(PEP)2024年小升初英語試卷(含答案)
- DB36-T 1983-2024 安寧療護服務(wù)規(guī)范
- 2024-2025學(xué)年全國中學(xué)生天文知識競賽考試題庫(含答案)
- 心衰健康宣教課件
- 馬工程《公共財政概論》課后習(xí)題庫(含)參考答案(可做期末復(fù)習(xí)和試卷)
- DB36T 1968-2024 公路路基工程利用鋰渣技術(shù)規(guī)范(試行)
- 醫(yī)療機構(gòu)工作人員廉潔從業(yè)九項準則自查自糾報告
- (正式版)JC∕T 60021-2024 石膏基自流平砂漿應(yīng)用技術(shù)規(guī)程
- 日雜店購銷合同清單
- 非遺文化傳承課件
- 小程序合作協(xié)議書
評論
0/150
提交評論