关于java 中最表达式的问题
import java.util.Stack;public class OperN {
static Stack<String> stackO=new Stack<String>();
static Stack<Integer> stackN=new Stack<Integer>();
public static void judge(char x){
char i=x;
switch(i){
case '-':
case '+':if(stackO.empty()||stackO.peek().equals("("))
stackO.push(i+"");
else if(stackO.peek().equals("-")){
@SuppressWarnings("unused")
String y=stackO.pop();
stackO.push(i+"");
int m=stackN.pop();
int n=stackN.pop();
int sum=n-m;
stackN.push(sum);
System.out.println(stackN.peek());
}
else if(stackO.peek().equals("*")){
@SuppressWarnings("unused")
String y=stackO.pop();
stackO.push(i+"");
int m=stackN.pop();
int n=stackN.pop();
int sum=n*m;
stackN.push(sum);
System.out.println(stackN.peek());
}
else if(stackO.peek().equals("/")){
@SuppressWarnings("unused")
String y=stackO.pop();
stackO.push(i+"");
int m=stackN.pop();
int n=stackN.pop();
int sum=n*m;
stackN.push(sum);
System.out.println(stackN.peek());
}
else if(stackO.peek().equals("+")){
@SuppressWarnings("unused")
String y=stackO.pop();
stackO.push(i+"");
int m=stackN.pop();
int n=stackN.pop();
int sum=n+m;
stackN.push(sum);
System.out.println(stackN.peek());
}
System.out.println(stackO.peek());
break;
case '*':
case '/':if(stackO.empty()||stackO.peek().equals("+")||stackO.peek().equals("-")||stackO.peek().equals("("))
stackO.push(i+"");
else if(stackO.peek().equals("*")){
@SuppressWarnings("unused")
String y=stackO.pop();
stackO.push(i+"");
int m=stackN.pop();
int n=stackN.pop();
int sum=n*m;
stackN.push(sum);
System.out.println(stackN.peek());
}
else if(stackO.peek().equals("/")){
@SuppressWarnings("unused")
String y=stackO.pop();
stackO.push(i+"");
int m=stackN.pop();
int n=stackN.pop();
int sum=n/m;
stackN.push(sum);
System.out.println(stackN.peek());
}
System.out.println(stackO.peek());
break;
case '(':stackO.push(i+"");
break;
case ')':while (!stackO.peek().equals("(")) {
if(stackO.peek().equals('-')){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n-m;
stackN.push(sum);
}
else if(stackO.peek().equals("*")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n*m;
stackN.push(sum);
}
else if(stackO.peek().equals("/")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n*m;
stackN.push(sum);
}
else if(stackO.peek().equals("+")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n+m;
stackN.push(sum);
}
}
stackO.pop();
System.out.println(stackN.peek());
break;
default:
stackN.push(i-48);
System.out.println(stackN.peek());
break;
}
}
public static void main(String args[]){
String l="2*3*4";
int x=0;
int i=l.length();
while(x<i){
judge(l.charAt(x));
x++;
}
while (!stackO.empty()) {
if(stackO.peek().equals("-")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n-m;
stackN.push(sum);
}
else if(stackO.peek().equals("*")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n*m;
stackN.push(sum);
}
else if(stackO.peek().equals("/")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n/m;
stackN.push(sum);
}
else if(stackO.peek().equals("+")){
@SuppressWarnings("unused")
String y=stackO.pop();
int m=stackN.pop();
int n=stackN.pop();
int sum=n+m;
stackN.push(sum);
}
}
System.out.println(stackN.pop());
}
}
类写的比较不好点,,不懂怎么修改可以操作多位数的计算,操作部分在该处
default:
stackN.push(i-48);
System.out.println(stackN.peek());
希望各位高手指导一下,谢谢了的