import java.util.Arrays; publicclassBinarySearch{ publicstaticintrank(int key,int a[]){ int lo = 0; int hi = a.length - 1; while (lo <= hi){ int mid = lo + (hi-lo)/2; if (key < a[mid]) hi = mid -1; elseif(key > a[mid]) lo = mid +1; elsereturn mid;
publicclassEvaluate{ publicstaticvoidmain(String[] args){ Stack<String> ops = new Stack<String>(); Stack<Double> vals = new Stack<Double>(); StdOut.println("请输入表达式:"); while (!StdIn.isEmpty()) { String s = StdIn.readString();
if (s.equals("(")) ; elseif (s.equals("+")) ops.push(s); elseif (s.equals("-")) ops.push(s); elseif (s.equals("*")) ops.push(s); elseif (s.equals("/")) ops.push(s); elseif (s.equals("sqrt")) ops.push(s); elseif (s.equals(")")) { double v = vals.pop(); String op = ops.pop(); if (op.equals("+")) v = v + vals.pop(); if (op.equals("-")) v = vals.pop() - v; if (op.equals("*")) v = v * vals.pop(); if (op.equals("/")) v = vals.pop() / v; if (op.equals("sqrt")) v = Math.sqrt(v); vals.push(v); } else { vals.push(Double.parseDouble(s)); double temp = vals.lastElement();