簡單四則混合口算測試程序設計_第1頁
簡單四則混合口算測試程序設計_第2頁
簡單四則混合口算測試程序設計_第3頁
簡單四則混合口算測試程序設計_第4頁
簡單四則混合口算測試程序設計_第5頁
已閱讀5頁,還剩3頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

簡單四則混合口算測試程序設計packagecn.gao.algorithm2.service;importjava.util.ArrayList;/***四則混合運算運算,如給定一個字符串"a+b*c-d/e",計算該表達式值*@paramargs*/publicclassTest11{publicArrayList<Integer>objectList;/*保存操作數數組*/publicArrayList<Integer>operatorList;/*保存操作符數組*/publicStrings;/*保存四則運算表達式字符串*/publicTest11(Strings){/*四則運算表達式測試類構造器*/super();this.s=s;objectList=newArrayList<Integer>();operatorList=newArrayList<Integer>();}publicvoidprase()/*解析四則運算表達式字符串,將解析后的操作數及操作符分別保存在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("解析后的操作數:"+objectList);System.out.println("解析后的操作符:"+operatorList);}publicvoidcaculate()/*根據已有的objectList和operatorList來就算表達式的值*/{ArrayList<Integer>objectList2=newArrayList<Integer>();/*中間態操作數容器*/ArrayList<Integer>operatorList2=newArrayList<Integer>();/*中間態操作符容器*/booleanflag=false;/*中間態狀態輔助標志,標志上一次運算是優先級高的運算*//*以下for循環是將objectList與operatorList里面的東西經過一次高級運算后過濾,將操作數及低級運算保存便于后續計算*/for(inti=0,j=0;i<objectList.size()&&j<operatorList.size();i++,j++)/*i,j分別指向當前操作數容器指針和當前操作符指針*/{intopertator=operatorList.get(j);intobject=objectList.get(i);if(opertator=='*'||opertator=='/')/*處理優先級較高的運算*/{intone=objectList.get(i);intsecond=objectList.get(i+1);intresult=getResultByOperator(one,second,opertator);if(result!=-1){if(!flag)/*上次操作是普通優先級的*/{objectList2.add(result);}else{objectList2.remove(objectList2.size()-1);/*移除上次高優先級存的值*/objectList2.add(result);/*將二次高優先級的值存入*/}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);/*以下結算結果值*/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("表達式計算結果的值為:"+objectList2.get(i));}publicintgetResultByOperator(intone,intsecond,intoperator)/*二目運算函數的抽象,根據制定的操作符及操作數得出結果返回*/{if(operator=='+'){returnone+second;}if(operator=='-'){returnone-second;}if(operator=='*'){returnone*second;}if(operator=='/'){returnone/second;}return-1;}publicintgetNumberWithRange(intstartIndex,intendIndex)/*根據開頭索引和結尾索引獲得對應的整數值*/{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)/*判斷當前索引操作數是否屬于操作符*/{inttemp=s.charAt(index);if('+'==temp||'-'==temp||'*'==temp||'/'==temp){returntrue;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論