20165225结对编程四则运算第二阶段总结

20165225结对编程四则运算第二阶段总结


需求分析:

  • 该程序能自动生成四则运算。

  • 需要有四种判断.要根据定义的范围输出题目.可生成真分数类计算式

  • 录入结果后能判断对错。

  • 有单独类提前知晓计算结果.有单独类可储存现有输入结果.两者可以比较.可统计输出与输入结果相比较的正确率。

设计思路:

  • 设计三个难度,对应运算符的多少以及是否加入括号
    设计两种类,一种用来生成题目,一种用来计算题目
    计算:现将中缀表达式转换为后缀,在进行计算

  • 完善了上次的代码。

UML类图:

image

代码:

import java.util.Random;


public class NewProject extends SetNum{
    public String Front(){
        String result;
        switch (getC()){

            case 0:
                result = getA()+Operator[getB()];

                break;
            case 1:
                result = getA()+Operator[getB()]+getA()+Operator[getB()];

                break;
            default:
                result = getA()+Operator[getB()]+getA()+Operator[getB()]+getA()+Operator[getB()];
        }
        return result;
    }
    public String Middle(){
        String result;
        switch (getC()){
            case 0:
                result = getA()+"";
                break;
            case 1:
                result ="("+getA()+Operator[getB()]+getA()+")";
                break;
                default:
                    result = "("+getA()+Operator[getB()]+"("+getA()+Operator[getB()]+getA()+")"+Operator[getB()]+getA()+")";
        }
        return result;
    }
    public String End(){
        String result;
        switch (getC()){
            case 0:
                result = Operator[getB()]+getA();
                break;
            case 1:
                result = Operator[getB()]+getA()+Operator[getB()]+getA();
                break;
                default:
                    result =  Operator[getB()]+getA()+Operator[getB()]+getA()+Operator[getB()]+getA();
        }
        return result;
    }
    public String Allpart(){
        String result;
        switch (getC()){
            case 0:
                result = Front()+Middle();
                break;
            case 1:
                result = Middle()+Front();
                default:
                    result = Front()+Middle()+End();
        }
        return result;
    }
    public String JudgeAllpart(){
        String result =Allpart();
        for(int i = 0;i<result.length();i++){
            if ((result.charAt(i)=='/')&&(result.charAt(i+1)=='0')){
                result = Allpart();
            }
        }
        return result;
    }

}

import java.util.Random;

public class SetNum {
    Random in = new Random();
    int a;
    int b;
    int c;

    public int getC() {
        setC();
        return c;
    }

    public void setC() {
        this.c = in.nextInt(3);
    }

    String [] Operator = {"+","-","*","/"};

    public int getB() {
        setB();
        return b;
    }

    public void setB() {
        this.b = in.nextInt(4);
    }

    public void setA() {
        this.a = in.nextInt(10);
    }

    public int getA() {
        setA();
        return a;
    }
}

测试代码:

public void testCount() throws Exception {
//TODO: Test goes here...
String line;
BufferedReader rdr = new BufferedReader(new FileReader("testdata.txt"));
while ((line = rdr.readLine()) != null) {
    if (line.startsWith("#")) {
        continue;
    }
    StringTokenizer st = new StringTokenizer(line);
    if (!st.hasMoreTokens()) {
        continue;
    }
    String val = st.nextToken();
    String expected = val;

    LinkedList<String> argument_list = new LinkedList();
    while (st.hasMoreTokens()) {
        argument_list.add(st.nextToken());
    }
    Postfix a = new Postfix();
    a.transferToPostfix(argument_list);
    assertEquals(expected, a.transferToPostfix(argument_list));;
}
}

测试结果:

image

功能截图:

image

结对感受:

  • 很开心这个又一次的合作,不过同时也明白了自己的不足。两个人确实能想到很多,也能相互学习到许多,总的来说还是挺有满足感的。

结对编程的照片:

image

posted @ 2018-04-22 23:02  牛奶仔丶  Views(117)  Comments(1Edit  收藏  举报