




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
簡單四則混合口算測試程序設計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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國機翼型中空鋁合金葉輪數據監測報告
- 2025-2030年中國三級綜合醫院行業市場運行調研與投資風險研究報告
- 2025年中國新鈴蘭醛數據監測報告
- 2025年中國數字音頻復接設備數據監測研究報告
- 2025年中國爐具銅分火器數據監測報告
- 肇慶市實驗中學高中生物三:2生長素的生理作用第2課時導學案
- 肇慶市實驗中學高中歷史三:第課音樂與美術高效課堂教學設計
- 新疆科信職業技術學院《人體解刨學》2023-2024學年第一學期期末試卷
- 新疆沙雅縣二中2025年高三三月模擬考試化學試題(文理)試卷含解析
- 新鄉工程學院《寫作思維學》2023-2024學年第二學期期末試卷
- 2024-2025學年全國中學生天文知識競賽考試題庫(含答案)
- 農產品包裝設計合同
- 巨量引擎信息流廣告(初級)認證理論試題庫資料(含答案)
- 建筑幕墻安裝工程安全施工施工工藝技術
- CJT 306-2009 建設事業非接觸式CPU卡芯片技術要求
- 情緒調節的認知神經機制
- 供貨方案及供貨計劃(2篇)
- 離散數學(下)智慧樹知到期末考試答案章節答案2024年桂林電子科技大學
- CATIA創成式曲面設計經典收藏
- QC/T 922-2023 汽車用空氣濾清器濾芯 (正式版)
- 2024年高考政治必修二 《經濟與社會》(思維導圖+核心考點+易混易錯)
評論
0/150
提交評論