| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 544 人关注过本帖
标题:关于java 中最表达式的问题
取消只看楼主 加入收藏
sjcawn
Rank: 2
等 级:论坛游民
帖 子:22
专家分:25
注 册:2010-5-23
结帖率:75%
收藏
已结贴  问题点数:15 回复次数:0 
关于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());
希望各位高手指导一下,谢谢了的

搜索更多相关主题的帖子: 表达式 java 
2011-03-30 22:18
快速回复:关于java 中最表达式的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021799 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved