码迷,mamicode.com
首页 > 其他好文 > 详细

第四次作业

时间:2015-10-24 23:15:21      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

    import java.util.Stack;  
    
    public class Operate {      
        private Stack<Character> priStack = new Stack<Character>();      
        private Stack<Integer> numStack = new Stack<Integer>();;         
        public int caculate(String str) {      
             
            String temp;
           
            StringBuffer tempNum = new StringBuffer();
            StringBuffer string = new StringBuffer().append(str);
          
            while (string.length() != 0) {      
                temp = string.substring(0, 1);      
                string.delete(0, 1);      
               
                if (!isNum(temp)) {      
                   
                    if (!"".equals(tempNum.toString())) {      
                       
                        int num = Integer.parseInt(tempNum.toString());      
                        numStack.push(num);  
                        tempNum.delete(0, tempNum.length());      
                    }      
                   
                    while (ompare(temp.charAt(0)) && (!priStack.empty())) {   
                        int a = (int) numStack.pop();
                        int b = (int) numStack.pop();
                        char ope = priStack.pop();      
                        int result = 0;    
                        switch (ope) {      
                        
                        case ‘+‘:      
                            result = b + a;      
                           
                            numStack.push(result);      
                            break;      
                        case ‘-‘:      
                            result = b - a;      
                            
                            numStack.push(result);      
                            break;      
                        case ‘*‘:      
                            result = b * a;      
                           
                            numStack.push(result);      
                            break;      
                        case ‘/‘:      
                            result = b / a;    
                            numStack.push(result);      
                            break;      
                        }      
          
                    }      
                          
                    if (temp.charAt(0) != ‘#‘) {      
                        priStack.push(new Character(temp.charAt(0)));      
                        if (temp.charAt(0) == ‘)‘) {      
                            priStack.pop();      
                            priStack.pop();      
                        }      
                    }      
                } else      
                          
                    tempNum = tempNum.append(temp);      
            }      
            return numStack.pop();      
        }      
          
          
        private boolean isNum(String temp) {      
            return temp.matches("[0-9]");      
        }      
          
          
        private boolean compare(char str) {      
            if (priStack.empty()) {      
                     
                return true;      
            }      
            char last = (char) priStack.lastElement();      
                  
            if (last == ‘(‘) {      
                return true;      
            }      
            switch (str) {      
            case ‘#‘:      
                return false;      
            case ‘(‘:      
                      
                return true;      
            case ‘)‘:      
                     
                return false;      
            case ‘*‘: {      
                     
                if (last == ‘+‘ || last == ‘-‘)      
                    return true;      
                else      
                    return false;      
            }      
            case ‘/‘: {      
                if (last == ‘+‘ || last == ‘-‘)      
                    return true;      
                else      
                    return false;      
            }      
                      
            case ‘+‘:      
                return false;      
            case ‘-‘:      
                return false;      
            }      
            return true;      
        }      
          
        public static void main(String args[]) {      
            Operate operate = new Operate();      
            int t = operate.caculate("(3+4*(4*10-10/2)#");        
            System.out.println(t);      
        }      
          
    }      

 

需求分析:输入0-10的随机数,计算加减乘除的四则运算。

总结:随着第三次作业的完成,第四次作业只是增加一点内容,可是我还没一点思路,然而又看了看伙伴做的,才慢慢有了思路,写得不好请老师谅解。技术分享

第四次作业

标签:

原文地址:http://www.cnblogs.com/ww22/p/4907815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!