| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 544 人关注过本帖
标题:关于java 中最表达式的问题
只看楼主 加入收藏
sjcawn
Rank: 2
等 级:论坛游民
帖 子:22
专家分:25
注 册:2010-5-23
结帖率:75%
收藏
已结贴  问题点数:15 回复次数:3 
关于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
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:15 
这么多啊  ! 我看了都头痛

楼上,楼下的一定要幸福开心哦!
2011-04-01 17:23
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:0 
与楼上同感

java群
62635216
欢迎加入
2011-04-03 19:54
飒沓
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:10
专家分:7
注 册:2011-3-27
收藏
得分:0 
看不下去

沾衣欲湿杏花雨
吹面不寒杨柳风
2011-04-05 12:25
快速回复:关于java 中最表达式的问题
数据加载中...
 
   



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

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