还剩2页未读,继续阅读
文本内容:
栈和队列实验答案实验public class3_1{//中缀转后缀表达式private voidInfixToSuffix Stringinfix,StringBuffer suffixthrows Exception{//创建—,个栈StackCharacter s=new Stacko;//计算长度用来控制循环int iLen=infix.length;double num=0;for int i=0;iiLen;i++{略过空格//I.1if infix.charAti==continue;如果是数字包括小数直接输出//
2.else ifIsDigitinfix.charAti suffix.appendinfix.charAti;.如果是左括号,则直接入栈//3else ifinfix.charAti==’s.push infix.charAti;.如果是右扩号,则出栈,直到遇到”为止,并把、出栈//41else ifinfix.charAti==*{while s.peek!=11{suffix.appends.peek;s.popO;;s.popO.如果是若栈中无元素或者栈顶有,,则直接入栈,否则全部出栈遇到“也//5停止后再入栈因为+-运算符优先级最低111else ifinfix.charAt i==+||infix.charAt i==*-//加一个分隔符,区分位以上的数字2suffix.append1;f1if s.empty||s.peek==s.push infix.charAti;else{do{suffix.appends.peek;s.pop;f T;}while!s.emptys.peek!=s.pushinfix.charAti;.如果是*/,若栈顶优先级低于当前运算符,则直接入栈,否则先出栈再入栈//6else if//加一个分1111infix.charAt i==*||infix.charAt i==/{隔符,区分位以上的数字2suffix.append f;//当前运算符的优先级大于栈顶的优先级的时候直接入栈if GetPrioritys.peek,infix.charAti==1s.pushinfix.charAti;//当栈不为空,当前运算符的优先级小于等于栈顶的优先级,Helse{栈顶不是,,才能出栈while!s.emptyGetPrioritys.peek,infix.charAti・T f1s,peek!={suffix.appends peek;s.pop;}s.push infix.charAti;最后把栈中的运算符都出栈//
7.while!s.empty{suffix.appends.peek;s.pop;//后缀表达式计算private doubleCalcSuffix Stringsuffix{//创建个栈一StackDouble s=new Stacko;//计算长度用来控制循环int iLen=suf fix.length;//表达式最终结果double result=0;//临时存方攵数字StringBuf fertemp=new StringBuffer;for inti=0;扫描到数字则入栈・iiLen;i++{//I.if IsDigitsuffix.charAti tempappendsuffix.charAti;f1else ifsuffix.charAti=={转换为数字入栈//t㊀mpif temp.length0valueOf{s.pushDouble.temp.toString;temp.setLength0;扫描到运算符则将栈顶的两个元素依次出栈做相应的运算,把结果再入//
2.栈else ifIsOperatorsuffix.charAti{if temp.length0{valueOftemp.s.pushDouble.toString;・temp setLength0;double opl=s.peek;s.popO;double op2=s.peek;s.pop;result=Calc opl,op2,suffix.charAti;s.pushresult;return s.peek;//判断是否是数字private booleanIsDigit charch{return ch=101ch=191I|ch==T.f true:false;//判断是否是操作符private booleanIsOperator charch{111111return ch==+||ch==*-||ch==**||ch==/true:false;//判断运算符的优先级,表示小于,表示等于,表示大于1private intGetPrioritychar opl,char op2throws Exception{ifopl==」+||opl==op2==…||op2==+return0;else if opl==op2return0;111f11else ifopl==*+||opl==-op2==*||op2==*/return1;1111!1,,else ifopl==*||opl==/op2==-\\op2==+return-1;else ifopl==**1||opl==*/1op2==1*!||op2==*/*opl!=op2return0;else两个运算符优先级未定义.”;㊀throw newExc ption//根据运算符计算两数11private doubleCalcdouble opldouble op2,char op{switch op{case+:rreturn op2+opl;11case-:return op2-opl;casereturn op2*opl;1case/ifopl!=0return op2/opl;//获取结果else return0;default:return0;public doubleGetResultStringinfix throwsException{StringBuffer suffix=newStringBuffer;InfixToSuffixinfix,suffix;return CalcSuffixsuffix.toString;//测试实验public staticvoid mainString[]args throwsException{3_1calc=new实验3_1;HSystem.out.printIncalc.GetResult2+5+1*3-3/l+2*3-
1.2*2;System,out.print In2+5+1*3-3/1+2*3-
1.2*2;class Josephint n,m;public Josephint nl,intn=nl;m=ml;public StringJsequence nStringans=;QueueInteger qu=new for inti=1;i=n;qu.offeri;for inti=1;i=n;forintj=1;j qu.offerqu.poll;ans+=”+qu.poll;return ans;实验public class3_2{public staticvoid mainString[]args{out.System.printin;夫夫*丈★★★夫测试]夫夫*夫夫★★****-;nSystem,out.printinintn=6,m=3;Joseph L=new Josephnm;f+”的约瑟夫序歹;System,out.print Inn=+n+”,m=+m U”System.out.printinL.Jsequence;夫夫*丈★★★★测试夫夫*★★★★****”;nSystem,out.printin2n=8;m=4;L=new Josephnm;znSystem,out.print Inn=++”的约瑟夫序//求解约瑟夫问题类n+”,m=+m列”;System.out.printinL.Jsequ//构造方法ence;ml//置数据成员值//求解约瑟夫序列LinkedListInteger;到进队//I ni++//共出列个人ni++//数完鼠-个人1m;j++//出队第个人m。
个人认证
优秀文档
获得点赞 0