数据结构(Java语言版)实验指导书-栈和队列实验.docx
栈和队列实验答案public class 实验 3_1 /中缀转后缀表达式private void InfixToSuffix (String infix, StringBuffer suffix) throws Exception Stack<Character> s = new Stacko () ;/创建,个栈int iLen = infix . length () ;/计算长度用来控制循环double num = 0;for (int i = 0; i < iLen; i+) /I.略过空格if (infix.charAt(i) = ' 1) continue;/2.如果是数字(包括小数)直接输出else if (IsDigit(infix.charAt(i) suffix.append(infix.charAt(i);/3 .如果是左括号,则直接入栈else if (infix.charAt(i) = (')s.push (infix.charAt(i);/4 .如果是右扩号,则出栈,直到遇到' ”为止,并把、。出栈else if (infix.charAt(i) = 1) *) while (s.peek () != 1( 1) suffix.append(s.peek();s.popO ;)s.popO ;)/5 .如果是若栈中无元素或者栈顶有' (,则直接入栈,否则全部出栈(遇 到“ 也停止)后再入栈(因为+-运算符优先级最低)else if (infix . charAt (i) = 1+ 1 | | infix . charAt (i) = * - 1) (/加一个分隔符,区分2位以上的数字suffix.append(' 1);if (s.empty() | | s.peek () = f (1)s.push (infix.charAt(i);else do suffix.append(s.peek();s.pop (); while ( !s.empty() && s.peek () != f (T);s.push(infix.charAt(i);)/6.如果是*/,若栈顶优先级低于当前运算符,则直接入栈,否则先出栈再入栈 else if (infix . charAt (i) = 1* 1 | | infix . charAt (i) = 1/ 1) /加一个分隔符,区分2位以上的数字 suffix.append(' f;/当前运算符的优先级大于栈顶的优先级的时候直接入栈if (GetPriority(s.peek(), infix.charAt(i) = 1)s.push(infix.charAt(i);else /当栈不为空,H当前运算符的优先级小于等于栈顶的优先级,栈顶不是' (,才能出栈while (!s.empty() && (GetPriority(s.peek(), infix.charAt(i) < 1) && s,peek () != T( f) suffix.append(s peek ();s.pop (); s.push (infix.charAt(i); ) )/7.最后把栈中的运算符都出栈 while (!s.empty () suffix.append(s.peek ();s.pop (); )/后缀表达式计算private double CalcSuffix (String suffix) Stack<Double> s = new Stacko () ;/创建一个栈int iLen = suf fix . length () ;/计算长度用来控制循环double result = 0;/表达式最终结果StringBuf fer temp = new StringBuf fer () ; /临时存方攵数字 for (int i = 0; i < iLen; i+) /I.扫描到数字则入栈 if (IsDigit(suffix.charAt(i) temp append(suffix.charAt(i);else if (suffix.charAt(i) = f 1) if (temp . length () > 0)/tmp转换为数字入栈 s.push(Double.valueOf(temp.toString (); temp.setLength (0);) ) /2.扫描到运算符则将栈顶的两个元素依次出栈做相应的运算,把结果再入栈 else if (IsOperator(suffix.charAt(i) if (temp.length () > 0) s.push(Double . valueOf(temp.toString();temp setLength(0);double opl = s.peek ();s.popO ;double op2 = s.peek ();s .pop ();result = Calc (opl, op2, suffix.charAt(i);s.push(result);)return s . peek();)/判断是否是数字private boolean IsDigit (char ch) return (ch >= 101&& ch <= 191) I| ch = T.f? true : false; )/判断是否是操作符private boolean IsOperator (char ch) return ch = 1+1 | | ch = * -1| | ch = 1* * | | ch = 1/ 1? true : false;)/判断运算符的优先级,表示小于,。表示等于,1表示大于private int GetPriority(char opl, char op2) throws Exception if ( (opl = + | | opl =)&& (op2 =| | op2 = +) return 0;else if (opl = op2)return 0;else if ( (opl = * +1| | opl = 1 - 1) && (op2 = f * 1| | op2 = * / 1) return 1;else if ( (opl = 1 * 1| | opl = 1/ 1) && (op2 = ! -1 op2 =,+,)return -1;else if ( (opl = * * 1| | opl = * / 1) && (op2 = 1 * !| | op2 = * / * ) && opl != op2) return 0;elsethrow new Excption ( "两个运算符优先级未定义.”);)/根据运算符计算两数private double Calc(double oplrdouble op2, char op) switch (op) case 1+1:return op2 + opl;case 1 - 1:return op2 - opl;casereturn op2 * opl;case 1/ :if (opl != 0) return op2 / opl;else return 0; default: return 0; ) ) /获取结果 public double GetResult(String infix) throws Exception StringBuffer suffix = new StringBuffer();InfixToSuffix(infix, suffix);return CalcSuffix(suffix.toString();)/测试public static void main(String args) throws Exception 实验 3_1 calc = new 实验 3_1 ();System.out.printIn(calc.GetResult("2+5+1*(3-3/l+2*3)-1.2)*2H);System, out.print In (2 + 5 + 1* (3 - 3/1 + 2* 3) - 1.2) * 2); ) )/求解约瑟夫问题类ml)/构造方法/置数据成员值/求解约瑟夫序列LinkedList<Integer>();i+)/I到n进队i+)/共出列n个人m; j+)/ /数完鼠-1个人/出队第m个人class Joseph ( int n, m; public Joseph(int nl, int ( n = nl; m = ml;) public String Jsequence() (String ans = "n; Queue<Integer> qu = new for (int i = 1; i <= n; qu.offer(i);for (int i = 1; i <= n; ( for (int j = 1; j <qu.offer(qu.poll ();ans += " ” + qu.poll (); ) return ans;)public class 实验 3_2 public static void main(String args) System.out.printin();System, out.printin (n夫夫*丈夫测试夫夫*夫夫*-); int n = 6, m = 3;Joseph L = new Joseph(nf m);System, out .print In (" n=" + n + ”,m=" + m + ”的约瑟夫序歹U”);System.out.printin(L.Jsequence();System, out .printin ( n夫夫*丈测试 2夫夫*”); n = 8; m = 4;L = new Joseph(nz m);System, out. print In (" n=n+ n + ”,m=" + m + ”的约瑟夫序列”); System.out.printin(L.Jsequence();)