第一个Sprint冲刺第十天

第一个Sprint冲刺第十天

组员:欧其锋 廖焯燊 林海信 何武鹏


 

 

工作:完成主要的核心代码

运算难度有简单、中等、困难三种,所以生成表达式分成 简单(ab[a*b=?]组合)、中等(abc[(a+b)*c]随机组合)、困难([(a+b)*(c+d)]随机组合);

分工和代码如下:

廖焯燊----(ab[a*b=?]组合)

 

private void easy() {

        Random ra = new Random();

        a = ra.nextInt(40) + 1;
        b = ra.nextInt(40) + 1;

        opnum = Math.abs(ra.nextInt()) % 4 + 1;
        if (a < b) {
            temp = a;
            a = b;
            b = temp;
        }
        switch (opnum) {
        case 1:
            op = "+";
            break;
        case 2:
            op = "-";
            break;
        case 3:
            op = "*";
            break;
        case 4:
            op = "/";
            break;
        }

        if ((op == "-") && (a < b)) {
            temp = a;
            a = b;
            b = temp;
        }

        if (op == "/") {
            if (b == 0) {
                b = 1;
            }
            a = a * b;
        }

        str = a + op + b + "";

    }
void easy()

 

何武鹏----(abc[(a+b)*c]随机组合)

private void Medium() {
        setData();
        Random ra = new Random();
        if (optype > optype1) {
            biaodashitype = Math.abs(ra.nextInt()) % 2 + 1;
            if (biaodashitype == 1) {
                str = a + op + b + op1 + c;
            } else {
                str = a + op + "(" + b + op1 + c + ")";
            }
        } else if (optype < optype1) {
            biaodashitype = Math.abs(ra.nextInt()) % 2 + 1;
            if (biaodashitype == 1) {
                str = a + op + b + op1 + c;
            } else {
                str = "(" + a + op + b + ")" + op1 + c;
            }
        } else {
            str = a + op + b + op1 + c;
        }
    }

    private void setData() {
        Random random = new Random();
        opnum = Math.abs(random.nextInt()) % 4 + 1;
        opnum1 = Math.abs(random.nextInt()) % 4 + 1;
        opnum2 = Math.abs(random.nextInt()) % 4 + 1;
        if (opnum < 3) {
            optype = 1;
        } else {
            optype = 2;
        }
        if (opnum1 < 3) {
            optype1 = 1;
        } else {
            optype1 = 2;
        }
        if (opnum2 < 3) {
            optype2 = 1;
        } else {
            optype2 = 2;
        }
        op = Setop(opnum);
        op1 = Setop(opnum1);
        op2 = Setop(opnum2);
        a = random.nextInt(10) + 1;
        b = random.nextInt(20) + 1;
        c = random.nextInt(70) + 1;
        d = random.nextInt(50) + 1;
    }
void medium()

林海信----(abcd[(a+b)*(c+d)]随机组合)

private void hard() {
        setData();
        Random ra = new Random();
        if (optype == optype1 && optype < optype2 && optype1 < optype2) {
            biaodashitype = Math.abs(ra.nextInt()) % 9 + 1;
            if (biaodashitype == 1) {
                str = a + op + b + op1 + c + op2 + d;
            } else if (biaodashitype == 2 || biaodashitype == 5
                    || biaodashitype == 8 || biaodashitype == 4) {
                str = a + op + "(" + b + op1 + c + ")" + op2 + d;
            } else {
                str = "(" + a + op + b + op1 + c + ")" + op2 + d;
            }
        } else if (optype < optype1 && optype == optype2 && optype1 > optype2) {
            biaodashitype = Math.abs(ra.nextInt()) % 12 + 1;
            if (biaodashitype == 1) {
                str = a + op + b + op1 + c + op2 + d;
            } else if (biaodashitype == 2 || biaodashitype == 6
                    || biaodashitype == 10 || biaodashitype == 5) {
                str = "(" + a + op + b + ")" + op1 + c + op2 + d;
            } else if (biaodashitype == 3 || biaodashitype == 7
                    || biaodashitype == 11 || biaodashitype == 9) {
                str = a + op + b + op1 + "(" + c + op2 + d + ")";
            } else {
                str = "(" + a + op + b + ")" + op1 + "(" + c + op2 + d + ")";
            }
        } else if (optype > optype1 && optype == optype2 && optype1 < optype2) {
            biaodashitype = Math.abs(ra.nextInt()) % 2 + 1;
            if (biaodashitype == 1) {
                str = a + op + b + op1 + c + op2 + d;
            } else {
                str = a + op + "(" + b + op1 + c + ")" + op2 + d;
            }
        } else if (optype == optype1 && optype > optype2 && optype1 > optype2) {
            biaodashitype = Math.abs(ra.nextInt()) % 2 + 1;
            if (biaodashitype == 1) {
                str = a + op + b + op1 + c + op2 + d;
            } else {
                str = a + op + b + op1 + "(" + c + op2 + d + ")";
            }
        } else {
            str = a + op + b + op1 + c + op2 + d;
        }
    }
void hard()

欧其锋----对生成的表达式计算

public class ToOperation {  
  
  
    public double computeWithVector(String computeExpr) {  
        StringTokenizer tokenizer = new StringTokenizer(computeExpr, "+-*/()", true);  
        Vector<Double> nums = new Vector<Double>();  
        Vector<Operator> operators = new Vector<Operator>();  
        Map<String, Operator> computeOper = this.getComputeOper();  
        Operator curOper;  
        String currentEle;  
        while (tokenizer.hasMoreTokens()) {  
            currentEle = tokenizer.nextToken().trim();  
            if (!"".equals(currentEle)) {  
                if (this.isNum(currentEle)) {  
                    nums.add(Double.valueOf(currentEle));  
                } else {
                    curOper = computeOper.get(currentEle);  
                    if (curOper != null) { 
                        while (!operators.isEmpty()  
                                && operators.lastElement().priority() >= curOper  
                                        .priority()) {  
                            compute(nums, operators);  
                        }  
                        operators.add(curOper);  
                    } else {   
                        if ("(".equals(currentEle)) {   
                            operators.add(Operator.BRACKETS);  
                        } else {
                            while (!operators.lastElement().equals(Operator.BRACKETS)) {  
                                compute(nums, operators);  
                            }  
                            operators.remove(operators.size()-1);  
                        }  
                    }  
                }  
            }  
        }  
        while (!operators.isEmpty()) {  
            compute(nums, operators);  
        }  
        return nums.firstElement();  
    }  
    public double computeWithStack(String computeExpr) {  
        StringTokenizer tokenizer = new StringTokenizer(computeExpr, "+-*/()", true);  
        Stack<Double> numStack = new Stack<Double>();    
        Stack<Operator> operStack = new Stack<Operator>();    
        Map<String, Operator> computeOper = this.getComputeOper();      
        String currentEle;  
        while (tokenizer.hasMoreTokens()) {  
            currentEle = tokenizer.nextToken().trim();    
            if (!"".equals(currentEle)) {    
                if (this.isNum(currentEle)) {   
                    numStack.push(Double.valueOf(currentEle));  
                } else {   
                    Operator currentOper = computeOper.get(currentEle); 
                    if (currentOper != null) { 
                        while (!operStack.empty() && operStack.peek().priority() >= currentOper.priority()) {  
                            compute(numStack, operStack);  
                        }  
                        
                        operStack.push(currentOper);  
                    } else {  
                        if ("(".equals(currentEle)) { 
                            operStack.push(Operator.BRACKETS);  
                        } else {   
                            while (!operStack.peek().equals(Operator.BRACKETS)) {  
                                compute(numStack, operStack);  
                            }  
                            operStack.pop();
                        }  
                    }  
                }  
            }  
        }  
        
        while (!operStack.empty()) {  
            compute(numStack, operStack);  
        }  
        return numStack.pop();  
    }  
      
    
    private boolean isNum(String str) {  
        String numRegex = "^\\d+(\\.\\d+)?$";   //数字的正则表达式  
        return Pattern.matches(numRegex, str);  
    }  
      
    /** 
     * 获取运算操作符 
     * @return 
     */  
    private Map<String, Operator> getComputeOper() {  
        return new HashMap<String, Operator>() { // 运算符  
            private static final long serialVersionUID = 7706718608122369958L;  
            {  
                put("+", Operator.PLUS);  
                put("-", Operator.MINUS);  
                put("*", Operator.MULTIPLY);  
                put("/", Operator.DIVIDE);  
            }  
        };  
    }  
  
    private void compute(Vector<Double> nums, Vector<Operator> operators) {  
        Double num2 = nums.remove(nums.size() - 1); 
        Double num1 = nums.remove(nums.size() - 1); 
        Double computeResult = operators.remove(operators.size() - 1).compute(  
                num1, num2);
        nums.add(computeResult);
    }  
      
    private void compute(Stack<Double> numStack, Stack<Operator> operStack) {  
        Double num2 = numStack.pop();
        Double num1 = numStack.pop();  
        Double computeResult = operStack.pop().compute(  
                num1, num2); 
        numStack.push(computeResult); 
    }  
 
    private enum Operator {  
        /** 
         * 加 
         */  
        PLUS {  
            @Override  
            public int priority() {  
                return 1;   
            }  
  
            @Override  
            public double compute(double num1, double num2) {  
                return num1 + num2;   
            }  
        },  
        /** 
         * 减 
         */  
        MINUS {  
            @Override  
            public int priority() {  
                return 1;   
            }  
  
            @Override  
            public double compute(double num1, double num2) {  
                return num1 - num2;   
            }  
        },  
        /** 
         * 乘 
         */  
        MULTIPLY {  
            @Override  
            public int priority() {  
                return 2;   
            }  
  
            @Override  
            public double compute(double num1, double num2) {  
                return num1 * num2;   
            }  
        },  
        /** 
         * 除 
         */  
        DIVIDE {  
            @Override  
            public int priority() {  
                return 2;   
            }  
  
            @Override  
            public double compute(double num1, double num2) {  
                return num1 / num2;   
            }  
        },  
        BRACKETS {  
            @Override  
            public int priority() {  
                return 0;   
            }  
  
            @Override  
            public double compute(double num1, double num2) {  
                return 0;   
            }  
        };  
        public abstract int priority();  
        public abstract double compute(double num1, double num2);  
    }  
}  
class ToOperation

 

 

 

 

 



posted @ 2015-11-20 21:16  27林海信  阅读(188)  评论(0编辑  收藏  举报