zoukankan      html  css  js  c++  java
  • 第四次作业

    随着第三次作业的完成我又开始做第四次作业,依然是结对编程,一次次的作业让我对合作有了更重要的认识,话不多说开始工作。

    需求分析:输入0到10完成四则运算,用户输入一个数完成加减乘除,要求数随机生成......判断答题正确率

    代码部分

        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);      
            }      
              
        }      
    

     我的编程小伙伴:lmxhappy   姓名:吕明霞  学号:1033 

    总结:代码很耗时,同时很费脑子,但是只要加紧练习不抱怨端正学习态度,就能学好。

  • 相关阅读:
    DataSet中的数据全部插入数据库
    SQL养成一个好习惯是一笔财富
    C#不管什么四舍五入,只要是小数取整就得加1
    XMLNode与XmlNodeList
    ASP.NET2.0中配置文件的加密与解密
    编写一个文件目录常用操作的类
    上传文件的方法
    使用javascript 实现.net 验证控件功能
    SQLSERVER如何获取一个数据库中的所有表的名称、一个表中所有字段的名称
    Android之开启内置闹钟与已安装的应用程序设置
  • 原文地址:https://www.cnblogs.com/WANGDI1995/p/4907740.html
Copyright © 2011-2022 走看看