public class PostFix { public static String postFix(String expression) { CS401StackLinkedListImpl<Character> operandStack = new CS401StackLinkedListImpl<Character>(); String postfix = ""; for (int i = 0; i < expression.length(); i++) { char c = expression.charAt(i); Character temp; switch (c) { case ' ': break; case '+': case '-': while (operandStack.size() != 0) { temp = operandStack.pop(); if (temp == '(') { operandStack.push(temp); break; } postfix += " " + temp; } operandStack.push(c); postfix += " "; break; case '*': case '/': while (operandStack.size() != 0) { temp = operandStack.pop(); if (temp == '(' || temp == '+' || temp == '-') { operandStack.push(temp); break; } postfix += " " + temp; } operandStack.push(c); postfix += " "; break; case '(': operandStack.push(c); break; case ')': while (operandStack.size() != 0) { temp = operandStack.pop(); if (temp == '(') { break; } postfix += " " + temp; } break; default: postfix += c; break; } } while (operandStack.size() != 0) { postfix += " " + operandStack.pop(); } return postfix; } public static int calculateArithmeticExp(String postfix) { CS401StackLinkedListImpl<Integer> stack = new CS401StackLinkedListImpl<Integer>(); String[] strings = postfix.split(" "); for (int i = 0; i < strings.length; i++) { String temp = strings[i].trim(); if (temp == "") continue; else if (temp.matches("\\d+")) { stack.push(Integer.parseInt(temp)); } else { int x = stack.pop(); int y = stack.pop(); if (temp.equals("+")) stack.push(x + y); else if (temp.equals("-")) stack.push(y - x); else if (temp.equals("*")) stack.push(x * y); else if (temp.equals("/")) stack.push(y / x); } } return stack.pop(); }}我写过的作业,自己改下linkedlist 和 stack 之类的,记得导包
public static int countString(String s){ System.out.println(s); int num = 0 ; String[] ss = s.split("[\\+\\-\\*/\\(\\)]") ; if( ss.length>2){ int si = 0 ; if( (si=s.indexOf("("))!=-1){ num = countString(s.substring(si+1 , s.indexOf(")"))); num =countString(s.substring(0 , si)+num + s.substring(s.indexOf(")")+1)); }else if((si=s.replaceAll("[\\*/]", ",").indexOf(","))!= -1 ){ int statr = s.substring(0, si).replaceAll("[\\+\\-\\*/\\(\\)]", ",").lastIndexOf(",") ; System.out.println(si); int end = s.substring( si+1).replaceAll("[\\+\\-\\*/\\(\\)]", ",").indexOf(",") ; num = countString(s.substring(statr==-1?0:statr+1, end==-1?s.length():si+end+1)); num =countString(s.substring(0 , statr==-1?0:statr+1)+num + s.substring(end==-1?s.length():si+end+1)); }else if((si=s.replaceAll("[\\+\\-]", ",").indexOf(","))!= -1 ){ if(si==0){ num = countString("&"+s.substring(1) ); }else{ int statr = s.substring(0, si).replaceAll("[\\+\\-\\*/\\(\\)]", ",").lastIndexOf(",") ; int end = s.substring( si+1).replaceAll("[\\+\\-\\*/\\(\\)]", ",").indexOf(",") ; num = countString(s.substring(statr==-1?0:statr+1, end==-1?s.length():end+2)); num = countString(s.substring(0 , statr==-1?0:statr+1)+num + s.substring(end==-1?s.length():end+2)); } } }else{ int index =s.replaceAll("[\\+\\-\\*/\\(\\)]", ",").indexOf(",") ; if(index!=-1){ String s2 = s.substring(index,index+1) ; if(ss[0].substring(0, 1).equals("&")) ss[0] ="-"+ ss[0].substring(1) ; if(s2.equals("*")) num = new Integer(ss[0]) * new Integer(ss[1]) ; if(s2.equals("/")) num = new Integer(ss[0]) / new Integer(ss[1]) ; if(s2.equals("+")) num = new Integer(ss[0]) + new Integer(ss[1]) ; if(s2.equals("-")) num = new Integer(ss[0]) - new Integer(ss[1]) ; }else{ num = new Integer(s) ; } } return num; } 试试
这一集的第16分钟开始,详细讲解了一元多项式。
public class PostFix { public static String postFix(String expression) {
CS401StackLinkedListImpl<Character> operandStack = new CS401StackLinkedListImpl<Character>();
String postfix = "";
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
Character temp;
switch (c) {
case ' ':
break; case '+':
case '-':
while (operandStack.size() != 0) {
temp = operandStack.pop();
if (temp == '(') {
operandStack.push(temp);
break;
}
postfix += " " + temp;
}
operandStack.push(c);
postfix += " ";
break; case '*':
case '/':
while (operandStack.size() != 0) {
temp = operandStack.pop();
if (temp == '(' || temp == '+' || temp == '-') {
operandStack.push(temp);
break;
}
postfix += " " + temp;
}
operandStack.push(c);
postfix += " ";
break; case '(':
operandStack.push(c);
break; case ')':
while (operandStack.size() != 0) {
temp = operandStack.pop();
if (temp == '(') {
break;
}
postfix += " " + temp;
}
break; default:
postfix += c;
break;
}
} while (operandStack.size() != 0) {
postfix += " " + operandStack.pop();
}
return postfix;
} public static int calculateArithmeticExp(String postfix) {
CS401StackLinkedListImpl<Integer> stack = new CS401StackLinkedListImpl<Integer>();
String[] strings = postfix.split(" "); for (int i = 0; i < strings.length; i++) {
String temp = strings[i].trim();
if (temp == "")
continue;
else if (temp.matches("\\d+")) {
stack.push(Integer.parseInt(temp));
} else {
int x = stack.pop();
int y = stack.pop();
if (temp.equals("+"))
stack.push(x + y);
else if (temp.equals("-"))
stack.push(y - x);
else if (temp.equals("*"))
stack.push(x * y);
else if (temp.equals("/"))
stack.push(y / x);
} }
return stack.pop(); }}我写过的作业,自己改下linkedlist 和 stack 之类的,记得导包
System.out.println(s);
int num = 0 ;
String[] ss = s.split("[\\+\\-\\*/\\(\\)]") ;
if( ss.length>2){
int si = 0 ;
if( (si=s.indexOf("("))!=-1){
num = countString(s.substring(si+1 , s.indexOf(")")));
num =countString(s.substring(0 , si)+num + s.substring(s.indexOf(")")+1));
}else if((si=s.replaceAll("[\\*/]", ",").indexOf(","))!= -1 ){
int statr = s.substring(0, si).replaceAll("[\\+\\-\\*/\\(\\)]", ",").lastIndexOf(",") ;
System.out.println(si);
int end = s.substring( si+1).replaceAll("[\\+\\-\\*/\\(\\)]", ",").indexOf(",") ;
num = countString(s.substring(statr==-1?0:statr+1, end==-1?s.length():si+end+1));
num =countString(s.substring(0 , statr==-1?0:statr+1)+num + s.substring(end==-1?s.length():si+end+1));
}else if((si=s.replaceAll("[\\+\\-]", ",").indexOf(","))!= -1 ){
if(si==0){
num = countString("&"+s.substring(1) );
}else{
int statr = s.substring(0, si).replaceAll("[\\+\\-\\*/\\(\\)]", ",").lastIndexOf(",") ;
int end = s.substring( si+1).replaceAll("[\\+\\-\\*/\\(\\)]", ",").indexOf(",") ;
num = countString(s.substring(statr==-1?0:statr+1, end==-1?s.length():end+2));
num = countString(s.substring(0 , statr==-1?0:statr+1)+num + s.substring(end==-1?s.length():end+2));
}
} }else{
int index =s.replaceAll("[\\+\\-\\*/\\(\\)]", ",").indexOf(",") ;
if(index!=-1){
String s2 = s.substring(index,index+1) ;
if(ss[0].substring(0, 1).equals("&"))
ss[0] ="-"+ ss[0].substring(1) ;
if(s2.equals("*"))
num = new Integer(ss[0]) * new Integer(ss[1]) ;
if(s2.equals("/"))
num = new Integer(ss[0]) / new Integer(ss[1]) ;
if(s2.equals("+"))
num = new Integer(ss[0]) + new Integer(ss[1]) ;
if(s2.equals("-"))
num = new Integer(ss[0]) - new Integer(ss[1]) ;
}else{
num = new Integer(s) ;
}
} return num;
}
试试
不知CS401StackLinkedListImpl里面有什么...