簡單四則混合口算測試程序設(shè)計(jì)_第1頁
簡單四則混合口算測試程序設(shè)計(jì)_第2頁
簡單四則混合口算測試程序設(shè)計(jì)_第3頁
簡單四則混合口算測試程序設(shè)計(jì)_第4頁
簡單四則混合口算測試程序設(shè)計(jì)_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

簡單四則混合口算測試程序設(shè)計(jì)packagecn.gao.algorithm2.service;importjava.util.ArrayList;/***四則混合運(yùn)算運(yùn)算,如給定一個(gè)字符串"a+b*c-d/e",計(jì)算該表達(dá)式值*@paramargs*/publicclassTest11{publicArrayList<Integer>objectList;/*保存操作數(shù)數(shù)組*/publicArrayList<Integer>operatorList;/*保存操作符數(shù)組*/publicStrings;/*保存四則運(yùn)算表達(dá)式字符串*/publicTest11(Strings){/*四則運(yùn)算表達(dá)式測試類構(gòu)造器*/super();this.s=s;objectList=newArrayList<Integer>();operatorList=newArrayList<Integer>();}publicvoidprase()/*解析四則運(yùn)算表達(dá)式字符串,將解析后的操作數(shù)及操作符分別保存在objectList和operatorList中*/{System.out.println("表達(dá)式為:"+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來就算表達(dá)式的值*/{ArrayList<Integer>objectList2=newArrayList<Integer>();/*中間態(tài)操作數(shù)容器*/ArrayList<Integer>operatorList2=newArrayList<Integer>();/*中間態(tài)操作符容器*/booleanflag=false;/*中間態(tài)狀態(tài)輔助標(biāo)志,標(biāo)志上一次運(yùn)算是優(yōu)先級高的運(yùn)算*//*以下for循環(huán)是將objectList與operatorList里面的東西經(jīng)過一次高級運(yùn)算后過濾,將操作數(shù)及低級運(yùn)算保存便于后續(xù)計(jì)算*/for(inti=0,j=0;i<objectList.size()&&j<operatorList.size();i++,j++)/*i,j分別指向當(dāng)前操作數(shù)容器指針和當(dāng)前操作符指針*/{intopertator=operatorList.get(j);intobject=objectList.get(i);if(opertator=='*'||opertator=='/')/*處理優(yōu)先級較高的運(yùn)算*/{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("表達(dá)式計(jì)算結(jié)果的值為:"+objectList2.get(i));}publicintgetResultByOperator(intone,intsecond,intoperator)/*二目運(yùn)算函數(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)/*獲取字符串當(dāng)前index的下一個(gè)最近的操作符索引*/{if(index>=s.length()-1){return-1; }while(index<=s.length()-1){if(isOperator(index)){//System.out.println("獲得當(dāng)前操作索引符:"+s.charAt(index)+"索引位置:"+index);returnindex;}index++;}return-1;}publicbooleanisOperator(intindex)/*判斷當(dāng)前索引操作數(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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論