使用Java求加减乘除运算~该怎么做

需要输入一串字符串,(当然都是数字和运算符号)
然后得出结果
麻烦了
需要输入的是比如: 83-32+83*32/4+3
然后直接得出答案。

神不在的星期二
浏览 569回答 3
3回答

慕少森

实际上这相当于javascript的eval方法,以下是该方法的java实现://Eval.javaimport java.util.ArrayList;import java.util.List;import java.util.Stack;public class Eval {public int eval(String exp){List<String> list = infixExpToPostExp(exp);//转化成后缀表达式return doEval(list);//真正求值}//遇到操作符压栈,遇到表达式从后缀表达式中弹出两个数,计算出结果,压入堆栈private int doEval(List<String> list) {Stack<String> stack = new Stack<String>();String element;int n1,n2,result;try{for(int i = 0; i < list.size();i++){element = list.get(i);if(isOperator(element)){n1 = Integer.parseInt(stack.pop());n2 = Integer.parseInt(stack.pop());result = doOperate(n1,n2,element);stack.push(new Integer(result).toString());}else{stack.push(element);}}return Integer.parseInt(stack.pop());}catch(RuntimeException e){throw new IllegalExpressionException(e.getMessage());}}private int doOperate(int n1, int n2, String operator) {if(operator.equals("+"))return n1 + n2;else if(operator.equals("-"))return n1 - n2;else if(operator.equals("*"))return n1 * n2;elsereturn n1 / n2;}private boolean isOperator(String str){return str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/");}private List<String> infixExpToPostExp(String exp){//将中缀表达式转化成为后缀表达式List<String> postExp = new ArrayList<String>();//存放转化的后缀表达式的链表StringBuffer numBuffer = new StringBuffer();//用来保存一个数的Stack<Character> opStack = new Stack<Character>();//操作符栈char ch,preChar;opStack.push('#');try{for(int i = 0; i < exp.length();){ch = exp.charAt(i);switch(ch){case '+':case '-':case '*':case '/':preChar = opStack.peek();// 如果栈里面的操作符优先级比当前的大,则把栈中优先级大的都添加到后缀表达式列表中while(priority(preChar) >= priority(ch)){postExp.add(""+preChar);opStack.pop();preChar = opStack.peek();}opStack.push(ch);i++;break;case '(':// 左括号直接压栈opStack.push(ch);i++;break;case ')':// 右括号则直接把栈中左括号前面的弹出,并加入后缀表达式链表中char c = opStack.pop();while(c != '('){postExp.add("" + c);c = opStack.pop();}i++;break;// #号,代表表达式结束,可以直接把操作符栈中剩余的操作符全部弹出,并加入后缀表达式链表中case '#':char c1;while(!opStack.isEmpty()){c1 = opStack.pop();if(c1 != '#')postExp.add("" + c1);}i++;break;//过滤空白符case ' ':case '\t':i++;break;// 数字则凑成一个整数,加入后缀表达式链表中default:if(Character.isDigit(ch)){while(Character.isDigit(ch)){numBuffer.append(ch);ch = exp.charAt(++i);}postExp.add(numBuffer.toString());numBuffer = new StringBuffer();}else{throw new IllegalExpressionException("illegal operator");}}}}catch(RuntimeException e){throw new IllegalExpressionException(e.getMessage());}return postExp;}private int priority(char op){//定义优先级switch(op){case'+':case'-':return 1;case'*':case'/':return 2;case'(':case'#':return 0;}throw new IllegalExpressionException("Illegal operator");}}Main.java 主函数所在类public class Main{public static void main(String[] args) {try {InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);String exp=br.readLine();int result = eval.eval(exp);System.out.println(result);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}IllegalExpressionException异常类public class IllegalExpressionException extends RuntimeException{public IllegalExpressionException(){}public IllegalExpressionException(String info){super(info);}}

月关宝盒

//MathX.java//表达式计算程序,绝对原创,//与网上多数介绍的方法思路有点不同,//与采用直接调用js的方法有根本的不同,//支持&nbsp;加&nbsp;减&nbsp;乘&nbsp;除&nbsp;幂&nbsp;及&nbsp;开方&nbsp;运算,支持多级括号,//本程序在&nbsp;JDK1.6_11下开发,低版本JDK兼容性未测试,import&nbsp;java.text.DecimalFormat;public&nbsp;class&nbsp;MathX&nbsp;{&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{&nbsp;&nbsp;String&nbsp;str&nbsp;=&nbsp;"83-32+83*32/4+3";&nbsp;&nbsp;double&nbsp;d&nbsp;=&nbsp;Calc.doCalc(str).getValue();&nbsp;&nbsp;System.out.println(Calc.getStr()+"="+d);&nbsp;}//&nbsp;public&nbsp;static&nbsp;double&nbsp;calc(String&nbsp;s){//&nbsp;&nbsp;return&nbsp;Calc.doCalc(s).getValue();//&nbsp;}//&nbsp;public&nbsp;static&nbsp;String&nbsp;getStr(){//&nbsp;&nbsp;return&nbsp;Calc.getStr();//&nbsp;}//&nbsp;public&nbsp;static&nbsp;String&nbsp;getFormatted()&nbsp;{//&nbsp;&nbsp;return&nbsp;Calc.getFormatted();//&nbsp;}}class&nbsp;Calc{&nbsp;private&nbsp;static&nbsp;final&nbsp;DecimalFormat&nbsp;df&nbsp;=&nbsp;new&nbsp;DecimalFormat("0.000000");&nbsp;private&nbsp;static&nbsp;CalcCore&nbsp;co;&nbsp;private&nbsp;static&nbsp;NumberWrapper&nbsp;result;&nbsp;public&nbsp;static&nbsp;NumberWrapper&nbsp;doCalc(String&nbsp;exp){&nbsp;&nbsp;co&nbsp;=&nbsp;new&nbsp;CalcCore(exp);&nbsp;&nbsp;result&nbsp;=&nbsp;co.getResult();&nbsp;&nbsp;return&nbsp;result;&nbsp;}&nbsp;public&nbsp;static&nbsp;String&nbsp;getFormatted()&nbsp;{&nbsp;&nbsp;return&nbsp;df.format(result.getValue());&nbsp;}&nbsp;public&nbsp;static&nbsp;String&nbsp;getStr(){return&nbsp;co.toString();}}//数据外覆类class&nbsp;NumberWrapper{&nbsp;public&nbsp;static&nbsp;final&nbsp;int&nbsp;IaN&nbsp;=&nbsp;0;&nbsp;public&nbsp;static&nbsp;final&nbsp;int&nbsp;NaN&nbsp;=&nbsp;1;&nbsp;public&nbsp;static&nbsp;final&nbsp;NumberWrapper&nbsp;NOTHING&nbsp;=&nbsp;new&nbsp;NumberWrapper(Double.NaN,NumberWrapper.NaN);&nbsp;private&nbsp;double&nbsp;value;&nbsp;private&nbsp;int&nbsp;id;&nbsp;&nbsp;public&nbsp;NumberWrapper(double&nbsp;v){&nbsp;&nbsp;this(v,NumberWrapper.IaN);&nbsp;}&nbsp;public&nbsp;double&nbsp;getValue(){&nbsp;&nbsp;return&nbsp;id==NumberWrapper.IaN?value:Double.NaN;&nbsp;}&nbsp;public&nbsp;NumberWrapper(double&nbsp;v,int&nbsp;id){&nbsp;&nbsp;this.value=v;&nbsp;&nbsp;this.id=id;&nbsp;}&nbsp;//&nbsp;+-*/^~&nbsp;public&nbsp;NumberWrapper&nbsp;calc(NumberWrapper&nbsp;x,char&nbsp;o){&nbsp;&nbsp;if(this.NaN()||x.NaN())&nbsp;&nbsp;&nbsp;return&nbsp;NumberWrapper.NOTHING;&nbsp;&nbsp;return&nbsp;new&nbsp;NumberWrapper(calc(this.getValue(),x.getValue(),o));&nbsp;}&nbsp;private&nbsp;double&nbsp;calc(double&nbsp;a,double&nbsp;b,char&nbsp;o){&nbsp;&nbsp;try{&nbsp;&nbsp;&nbsp;switch(o){&nbsp;&nbsp;&nbsp;case&nbsp;OStack.PLUS:return&nbsp;a+b;&nbsp;&nbsp;&nbsp;case&nbsp;OStack.SUBTRACT:return&nbsp;a-b;&nbsp;&nbsp;&nbsp;case&nbsp;OStack.MULTIPLY:return&nbsp;a*b;&nbsp;&nbsp;&nbsp;case&nbsp;OStack.DIVIDE:return&nbsp;a/b;&nbsp;&nbsp;&nbsp;case&nbsp;OStack.POWER:return&nbsp;Math.pow(a,&nbsp;b);&nbsp;&nbsp;&nbsp;case&nbsp;OStack.EVOLUTION:return&nbsp;Math.pow(a,&nbsp;1d/b);&nbsp;&nbsp;&nbsp;default:return&nbsp;Double.NaN;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}catch(Exception&nbsp;e){}&nbsp;&nbsp;return&nbsp;Double.NaN;&nbsp;}&nbsp;&nbsp;public&nbsp;void&nbsp;setId(int&nbsp;id){this.id=id;}&nbsp;public&nbsp;boolean&nbsp;NaN(){return&nbsp;id==NaN;}}class&nbsp;CalcCore{&nbsp;private&nbsp;NStack&nbsp;n;&nbsp;private&nbsp;OStack&nbsp;o;&nbsp;private&nbsp;NumberWrapper&nbsp;result;&nbsp;private&nbsp;String&nbsp;src;&nbsp;&nbsp;public&nbsp;CalcCore(String&nbsp;src){&nbsp;&nbsp;this.src=src;&nbsp;&nbsp;rebuildString();&nbsp;&nbsp;this.n=new&nbsp;NStack();&nbsp;&nbsp;this.o=new&nbsp;OStack();&nbsp;&nbsp;this.split();&nbsp;&nbsp;this.calc();&nbsp;}&nbsp;public&nbsp;String&nbsp;toString(){&nbsp;&nbsp;return&nbsp;src;&nbsp;}&nbsp;private&nbsp;void&nbsp;rebuildString()&nbsp;{&nbsp;&nbsp;//&nbsp;(...)(...)&nbsp;-->&nbsp;(...)*(...)&nbsp;&nbsp;src=src.replaceAll("\\)\\(",")"+OStack.MULTIPLY+"(");&nbsp;&nbsp;//&nbsp;1234(...)&nbsp;-->&nbsp;1234*(...)&nbsp;&nbsp;//&nbsp;(...)1234&nbsp;-->&nbsp;(...)*1234&nbsp;&nbsp;int&nbsp;i=0;&nbsp;&nbsp;while(i<src.length()){&nbsp;&nbsp;&nbsp;if(hasNext(i+1)&&this.isNumber(i)&&src.charAt(i+1)==OStack.LB||&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hasNext(i+1)&&this.isNumber(i+1)&&src.charAt(i)==OStack.RB){&nbsp;&nbsp;&nbsp;&nbsp;src=src.substring(0,i+1)+OStack.MULTIPLY+src.substring(i+1);&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;++i;&nbsp;&nbsp;}&nbsp;&nbsp;//~1234&nbsp;-->2~1234&nbsp;&nbsp;//(~1234)&nbsp;-->(2~1234)&nbsp;&nbsp;i=0;&nbsp;&nbsp;while(i<src.length()){&nbsp;&nbsp;&nbsp;if(src.charAt(i)==OStack.EVOLUTION&nbsp;&&&nbsp;pio(i)){&nbsp;&nbsp;&nbsp;&nbsp;src=src.substring(0,i)+"2"+src.substring(i);&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;++i;&nbsp;&nbsp;}&nbsp;}&nbsp;private&nbsp;void&nbsp;calc()&nbsp;{&nbsp;&nbsp;for(int&nbsp;i=0;&nbsp;i<o.size();&nbsp;i++){&nbsp;&nbsp;&nbsp;char&nbsp;ch&nbsp;=&nbsp;o.get(i);&nbsp;&nbsp;&nbsp;if(ch==OStack.EVOLUTION||ch==OStack.POWER){&nbsp;&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;n0&nbsp;=&nbsp;n.remove(i);&nbsp;&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;n1&nbsp;=&nbsp;n.remove(i);&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;=&nbsp;o.remove(i);&nbsp;&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;rs;&nbsp;&nbsp;&nbsp;&nbsp;if(ch==OStack.EVOLUTION)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs&nbsp;=&nbsp;n0.calc(n1,&nbsp;ch);&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs&nbsp;=&nbsp;n1.calc(n0,&nbsp;ch);&nbsp;&nbsp;&nbsp;&nbsp;n.insert(i,&nbsp;rs);&nbsp;&nbsp;&nbsp;&nbsp;--i;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}&nbsp;&nbsp;for(int&nbsp;i=o.size()-1;&nbsp;i>=0;&nbsp;i--){&nbsp;&nbsp;&nbsp;char&nbsp;ch&nbsp;=&nbsp;o.get(i);&nbsp;&nbsp;&nbsp;if(ch==OStack.MULTIPLY||ch==OStack.DIVIDE){&nbsp;&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;n0&nbsp;=&nbsp;n.remove(i+1);&nbsp;&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;n1&nbsp;=&nbsp;n.remove(i);&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;=&nbsp;o.remove(i);&nbsp;&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;rs&nbsp;=&nbsp;n0.calc(n1,&nbsp;ch);&nbsp;&nbsp;&nbsp;&nbsp;n.insert(i,&nbsp;rs);&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}&nbsp;&nbsp;for(int&nbsp;i=o.size()-1;&nbsp;i>=0;&nbsp;i--){&nbsp;&nbsp;&nbsp;char&nbsp;ch&nbsp;=&nbsp;o.get(i);&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;n0&nbsp;=&nbsp;n.remove(i+1);&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;n1&nbsp;=&nbsp;n.remove(i);&nbsp;&nbsp;&nbsp;ch&nbsp;=&nbsp;o.remove(i);&nbsp;&nbsp;&nbsp;NumberWrapper&nbsp;rs&nbsp;=&nbsp;n0.calc(n1,&nbsp;ch);&nbsp;&nbsp;&nbsp;n.insert(i,&nbsp;rs);&nbsp;&nbsp;}&nbsp;&nbsp;if(n.isEmpty()||n.size()>1)&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;NumberWrapper.NOTHING;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;n.pop();&nbsp;}&nbsp;public&nbsp;NumberWrapper&nbsp;getResult(){&nbsp;&nbsp;return&nbsp;result;&nbsp;}&nbsp;private&nbsp;void&nbsp;split(){&nbsp;&nbsp;int&nbsp;cont;&nbsp;&nbsp;for(int&nbsp;i=0;&nbsp;i<src.length();&nbsp;i++){&nbsp;&nbsp;&nbsp;char&nbsp;c&nbsp;=&nbsp;src.charAt(i);&nbsp;&nbsp;&nbsp;switch(c){&nbsp;&nbsp;&nbsp;case&nbsp;'(':&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;pair&nbsp;=&nbsp;nextPair(src,i+1);&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;sub&nbsp;=&nbsp;substring(i+1,pair);&nbsp;&nbsp;&nbsp;&nbsp;n.push(Calc.doCalc(sub));&nbsp;&nbsp;&nbsp;&nbsp;i=pair;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;&nbsp;case&nbsp;'-':&nbsp;&nbsp;&nbsp;case&nbsp;'+':&nbsp;&nbsp;&nbsp;&nbsp;boolean&nbsp;iso&nbsp;=&nbsp;pio(i);&nbsp;&nbsp;&nbsp;&nbsp;cont&nbsp;=&nbsp;continuous(i+1);&nbsp;&nbsp;&nbsp;&nbsp;if(iso){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n.push(new&nbsp;NumberWrapper(parse(substring(i,cont))));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=cont-1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;case&nbsp;'*':&nbsp;&nbsp;&nbsp;case&nbsp;'/':&nbsp;&nbsp;&nbsp;case&nbsp;'%':&nbsp;&nbsp;&nbsp;case&nbsp;'^':&nbsp;&nbsp;&nbsp;case&nbsp;'~':o.push(c);break;&nbsp;&nbsp;&nbsp;default&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;cont&nbsp;=&nbsp;continuous(i+1);&nbsp;&nbsp;&nbsp;&nbsp;n.push(new&nbsp;NumberWrapper(parse(substring(i,cont))));&nbsp;&nbsp;&nbsp;&nbsp;i=cont-1;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}&nbsp;}&nbsp;private&nbsp;double&nbsp;parse(String&nbsp;s){&nbsp;&nbsp;try{&nbsp;&nbsp;&nbsp;return&nbsp;Double.parseDouble(s);&nbsp;&nbsp;}catch(Exception&nbsp;e){}&nbsp;&nbsp;return&nbsp;Double.NaN;&nbsp;}&nbsp;private&nbsp;String&nbsp;substring(int&nbsp;i,&nbsp;int&nbsp;cont)&nbsp;{&nbsp;&nbsp;return&nbsp;src.substring(i,cont);&nbsp;}&nbsp;private&nbsp;boolean&nbsp;hasNext(int&nbsp;i){&nbsp;&nbsp;return&nbsp;src.length()>i;&nbsp;}&nbsp;private&nbsp;int&nbsp;continuous(int&nbsp;i)&nbsp;{&nbsp;&nbsp;while(hasNext(i)&nbsp;&&&nbsp;isNumber(i))&nbsp;&nbsp;&nbsp;&nbsp;++i;&nbsp;&nbsp;return&nbsp;i;&nbsp;}&nbsp;private&nbsp;boolean&nbsp;pio(int&nbsp;i){&nbsp;&nbsp;return&nbsp;i<1?true:OStack.iso(src.charAt(i-1));&nbsp;}&nbsp;public&nbsp;boolean&nbsp;isNumber(int&nbsp;pos){&nbsp;&nbsp;char&nbsp;c&nbsp;=&nbsp;src.charAt(pos);&nbsp;&nbsp;return&nbsp;c<='9'&nbsp;&&&nbsp;c>='0'&nbsp;||&nbsp;c=='.';&nbsp;}&nbsp;public&nbsp;int&nbsp;nextPair(String&nbsp;src,int&nbsp;pos){&nbsp;&nbsp;int&nbsp;inner&nbsp;=&nbsp;0;&nbsp;&nbsp;int&nbsp;len&nbsp;=&nbsp;src.length();&nbsp;&nbsp;for(int&nbsp;i=pos;&nbsp;i<len;&nbsp;i++){&nbsp;&nbsp;&nbsp;char&nbsp;c&nbsp;=&nbsp;src.charAt(i);&nbsp;&nbsp;&nbsp;if(c==')')&nbsp;&nbsp;&nbsp;&nbsp;if(inner==0)return&nbsp;i;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;--inner;&nbsp;&nbsp;&nbsp;else&nbsp;if(c=='(')++inner;&nbsp;&nbsp;}&nbsp;&nbsp;return&nbsp;-1;&nbsp;}}//操作符栈(半角符号)class&nbsp;OStack{&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;PLUS&nbsp;=&nbsp;'+';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;SUBTRACT&nbsp;=&nbsp;'-';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;MULTIPLY&nbsp;=&nbsp;'*';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;DIVIDE&nbsp;=&nbsp;'/';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;POWER&nbsp;=&nbsp;'^';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;EVOLUTION&nbsp;=&nbsp;'~';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;LB&nbsp;=&nbsp;'(';&nbsp;public&nbsp;static&nbsp;final&nbsp;char&nbsp;RB&nbsp;=&nbsp;')';&nbsp;public&nbsp;static&nbsp;boolean&nbsp;iso(char&nbsp;c)&nbsp;{&nbsp;&nbsp;switch(c){&nbsp;&nbsp;case&nbsp;PLUS:&nbsp;&nbsp;case&nbsp;SUBTRACT:&nbsp;&nbsp;case&nbsp;MULTIPLY:&nbsp;&nbsp;case&nbsp;DIVIDE:&nbsp;&nbsp;case&nbsp;POWER:&nbsp;&nbsp;case&nbsp;EVOLUTION:&nbsp;&nbsp;case&nbsp;LB://&nbsp;&nbsp;case&nbsp;RB:&nbsp;&nbsp;&nbsp;return&nbsp;true;&nbsp;&nbsp;}&nbsp;&nbsp;return&nbsp;false;&nbsp;}&nbsp;&nbsp;public&nbsp;boolean&nbsp;hasNext(int&nbsp;i)&nbsp;{&nbsp;&nbsp;return&nbsp;this.length>i+1;&nbsp;}&nbsp;private&nbsp;char[]&nbsp;stack;&nbsp;private&nbsp;int&nbsp;length;&nbsp;&nbsp;public&nbsp;OStack(){&nbsp;&nbsp;this.stack=new&nbsp;char[0];&nbsp;&nbsp;this.length=0;&nbsp;}&nbsp;public&nbsp;boolean&nbsp;isEmpty(){&nbsp;&nbsp;return&nbsp;length==0;&nbsp;}&nbsp;public&nbsp;void&nbsp;push(char&nbsp;c){&nbsp;&nbsp;char[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;char[this.length+1];&nbsp;&nbsp;tmp[0]=c;&nbsp;&nbsp;System.arraycopy(stack,&nbsp;0,&nbsp;tmp,&nbsp;1,&nbsp;length);&nbsp;&nbsp;++length;&nbsp;&nbsp;this.stack=tmp;&nbsp;}&nbsp;public&nbsp;char&nbsp;pop(){&nbsp;&nbsp;if(this.isEmpty())throw&nbsp;new&nbsp;StackOverflowError();&nbsp;&nbsp;char&nbsp;c&nbsp;=&nbsp;stack[0];&nbsp;&nbsp;char[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;char[length-1];&nbsp;&nbsp;System.arraycopy(stack,&nbsp;1,&nbsp;tmp,&nbsp;0,&nbsp;tmp.length);&nbsp;&nbsp;--length;&nbsp;&nbsp;this.stack=tmp;&nbsp;&nbsp;return&nbsp;c;&nbsp;}&nbsp;public&nbsp;char&nbsp;remove(int&nbsp;i){&nbsp;&nbsp;if(i<0||i>=length)throw&nbsp;new&nbsp;StackOverflowError();&nbsp;&nbsp;char[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;char[length-1];&nbsp;&nbsp;System.arraycopy(stack,0,tmp,0,i);&nbsp;&nbsp;System.arraycopy(stack,i+1,tmp,i,tmp.length-i);&nbsp;&nbsp;this.length--;&nbsp;&nbsp;char&nbsp;n=stack[i];&nbsp;&nbsp;this.stack=tmp;&nbsp;&nbsp;return&nbsp;n;&nbsp;}&nbsp;public&nbsp;void&nbsp;insert(int&nbsp;i,char&nbsp;n){&nbsp;&nbsp;if(i<0||i>length)throw&nbsp;new&nbsp;StackOverflowError();&nbsp;&nbsp;char[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;char[length+1];&nbsp;&nbsp;System.arraycopy(stack,0,tmp,0,i);&nbsp;&nbsp;System.arraycopy(stack,i,tmp,i+1,this.length-i);&nbsp;&nbsp;tmp[i]=n;&nbsp;&nbsp;this.length++;&nbsp;&nbsp;this.stack=tmp;&nbsp;}&nbsp;public&nbsp;char&nbsp;get(int&nbsp;i){&nbsp;&nbsp;return&nbsp;this.stack[i];&nbsp;}&nbsp;public&nbsp;int&nbsp;size(){return&nbsp;this.length;}}//数据栈class&nbsp;NStack{&nbsp;private&nbsp;NumberWrapper[]&nbsp;stack;&nbsp;private&nbsp;int&nbsp;length;&nbsp;public&nbsp;NStack(){&nbsp;&nbsp;this.stack=new&nbsp;NumberWrapper[0];&nbsp;&nbsp;this.length=0;&nbsp;}&nbsp;public&nbsp;NStack(NStack&nbsp;n){&nbsp;&nbsp;this.stack=new&nbsp;NumberWrapper[n.length];&nbsp;&nbsp;this.length=n.length;&nbsp;&nbsp;for(int&nbsp;i=0;&nbsp;i<length;&nbsp;i++){&nbsp;&nbsp;&nbsp;this.stack[i]=n.stack[i];&nbsp;&nbsp;}&nbsp;}&nbsp;public&nbsp;void&nbsp;push(NumberWrapper&nbsp;d){&nbsp;&nbsp;NumberWrapper[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;NumberWrapper[this.length+1];&nbsp;&nbsp;System.arraycopy(stack,&nbsp;0,&nbsp;tmp,&nbsp;1,&nbsp;this.length);&nbsp;&nbsp;tmp[0]&nbsp;=&nbsp;d;&nbsp;&nbsp;this.stack=tmp;&nbsp;&nbsp;this.length++;&nbsp;}&nbsp;public&nbsp;NumberWrapper&nbsp;pop()&nbsp;throws&nbsp;StackOverflowError{&nbsp;&nbsp;if(this.isEmpty())throw&nbsp;new&nbsp;StackOverflowError();&nbsp;&nbsp;NumberWrapper[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;NumberWrapper[this.length-1];&nbsp;&nbsp;System.arraycopy(stack,&nbsp;1,&nbsp;tmp,&nbsp;0,&nbsp;tmp.length);&nbsp;&nbsp;NumberWrapper&nbsp;d&nbsp;=&nbsp;stack[0];&nbsp;&nbsp;this.stack=tmp;&nbsp;&nbsp;this.length--;&nbsp;&nbsp;return&nbsp;d;&nbsp;}&nbsp;public&nbsp;NumberWrapper&nbsp;get(int&nbsp;i){&nbsp;&nbsp;return&nbsp;this.stack[i];&nbsp;}&nbsp;public&nbsp;NumberWrapper&nbsp;remove(int&nbsp;i){&nbsp;&nbsp;if(i<0||i>=length)throw&nbsp;new&nbsp;StackOverflowError();&nbsp;&nbsp;NumberWrapper[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;NumberWrapper[length-1];&nbsp;&nbsp;System.arraycopy(stack,0,tmp,0,i);&nbsp;&nbsp;System.arraycopy(stack,i+1,tmp,i,tmp.length-i);&nbsp;&nbsp;this.length--;&nbsp;&nbsp;NumberWrapper&nbsp;n=stack[i];&nbsp;&nbsp;this.stack=tmp;&nbsp;&nbsp;return&nbsp;n;&nbsp;}&nbsp;public&nbsp;void&nbsp;insert(int&nbsp;i,NumberWrapper&nbsp;n){&nbsp;&nbsp;if(i<0||i>length)throw&nbsp;new&nbsp;StackOverflowError();&nbsp;&nbsp;NumberWrapper[]&nbsp;tmp&nbsp;=&nbsp;new&nbsp;NumberWrapper[length+1];&nbsp;&nbsp;System.arraycopy(stack,0,tmp,0,i);&nbsp;&nbsp;System.arraycopy(stack,i,tmp,i+1,this.length-i);&nbsp;&nbsp;tmp[i]=n;&nbsp;&nbsp;this.length++;&nbsp;&nbsp;this.stack=tmp;&nbsp;}&nbsp;public&nbsp;boolean&nbsp;isEmpty(){&nbsp;&nbsp;return&nbsp;this.length==0;&nbsp;}&nbsp;public&nbsp;int&nbsp;size(){return&nbsp;this.length;}}

HUH函数

class test3{public static void main(String[] args)throws Exception{try{System.out.println(myMath("(12+15)*56+45681"));}catch(javax.script.ScriptException se){}}public static double myMath(String s) throws javax.script.ScriptException{Object o = new javax.script.ScriptEngineManager().getEngineByName("JavaScript").eval(s);return (Double)o;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
JQuery