2017-2018-2 20172302 『Java程序设计』课程 结对编程练习_四则运算

1.结对对象

20172308周亚杰

2.本周内容

需求分析

  • (1).自动生成题目

  • 可独立使用(能实现自己编写测试类单独生成题目的功能)
    可生成不同等级题目,类似于:
    1级题目:2 + 5 = 、10 - 5 = 之类的两个数,一个运算符的题目

  • (2).题目运算(判题)
    可独立使用,实现中缀表达式转为后缀表达式并计算;判断用户答题正误,并输出正确结果

  • (3).支持真分数

  • 可独立使用,实现分数算式的计算

  • (4).题目去重(扩展需求,加分项)

  • 可独立使用,实现对自动生成表达式的去重:如下若生成:2 + 5 = & 5 + 2 = 为同一题目

设计分析

  • 1.自动生成题目:这里的题目的难度我是采用操作符的个数来定义的,根据操作符再产生操作符加一的数字(这里暂时没有编写真分数的情况),然后使用循环进行产生,然后以字符串形式输出出来。

  • 2.中缀转后缀:这个代码的编写我们两个是交流后各自独立完成的,我采用的是那个使用操作符优先级表生成二维数组,根据索引值进行判断优先级,决定操作符是入栈还是出栈,对于数字直接进行输出,然后最后把栈里所存的操作符再进行逐个输出,完成后缀表达式的输出。

  • 3.后缀求值:这里是由写一个方法:从左向右扫描后缀表达式,遇到操作数进栈,遇到操作符则弹出栈顶的两个元素,将结果计算出来再压进栈,最后栈内剩余一个元素即为最终答案

设计UML类图(重点!!!)

UML类图的设计见下:

遇到问题及解决方法

代码展示

  • 自动生成题目:
package arithmetic;
import java.util.Random;
import java.util.ArrayList;
public class Questions
{
    ArrayList<Object> array = new ArrayList<Object>();
    Random generator = new Random();
    char [] newchar = {'+','-','*','/'};
    protected int number;
    int NUM;
    public Questions()
    {
        number = 0 ;
    }
        public Object getQuestion(int num) {
            int num1 = num;
            while (num > 0) {
                int figure = (int) generator.nextInt(9) + 1;
                array.add(figure);
                number = (int) (Math.random() * 4);
                array.add(newchar[number]);
                num--;
            }
            String obj = "";
            while (num < 2*num1) {
                obj += array.get(num);
                num++;
            }
            int other = (int) generator.nextInt(9)+1;
            array.add(other);
            obj+=other+"=";
            return obj;
        }
    }

  • 中缀转后缀:
package arithmetic;
import java.util.Stack;
public class test
{
    private int number,num1,num2;
    private int value1,value2;
    private int index=0;
    private int[][] array = {{0,0,0,0,0},
                                {0,1,1,-1,-1},
                                {0,1,1,-1,-1},
                                {0,1,1,1,1},
                                {0,1,1,1,1}};
    public String obj="2+1-2*2-2/1=";
      Stack stack = new Stack();
    public test()
    {
        num1=0;
        num2=0;
        value1=0;
        value2=0;
    }
    public String getAnswer()
    {
        String result = "";
        while (index<obj.length()-1)
        {
            char x = obj.charAt(index);
            if(x>='0'&&x<='9')
                result += x+" ";
            else
            {
                switch (x)
                {
                    case '+':
                        number = 1;
                        break;
                    case '-':
                        number=2;
                        break;
                    case '*':
                        number =3;
                        break;
                    case '/':
                        number=4;
                        break;
                }
                if(stack.empty()) {
                    num1 = number;
                    number = 0;
                }
                else
                    {
                        num2 = number;
                        number=0;
            }
                if(array[num1][num2]<=0)
                {
                    stack.push(x);
                    value1 +=1;
                }
                else
                {
                    result+=stack.pop();
                    stack.push(x);
                    value1 +=1;
                    value2 +=1;
                }
            }
            index++;
            }
        for(int y =0;y<value1-value2;y++)
            result += stack.pop();
        return result;
    }
}
  • 后缀表达式求值:



3.PSP分析

|PSP2.1 | Personal Software Process Stages | 预估耗时(分钟)| 实际耗时(分钟)|
| -------- | :----------------😐:----------------😐
|Planning | 计划 | 60 | 65 |
|Estimate | 估计这个任务需要多少时间 | 3 | 2|
|Development | 开发 2000 | 3000 |
|Analysis | 需求分析 (包括学习新技术) | 350 | 300 |
|Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 60 | 10 |
|Design UML | 设计项目UML类图 | 60 | 60 |
|Coding | 具体编码 | 1500 | 2000 |
|Code Review | 代码复审 | 30 | 30|
|Test 测试|(自我测试,修改代码,提交修改)| 300| 300|
|Size Measurement 计算工作量(实际时间 )| 2 | 2|
|Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 30 | 10|
| | 合计 | 4395 | 5229|

posted @ 2018-05-01 22:41  ◕‿◕  阅读(235)  评论(1编辑  收藏  举报
© 2018 GitHub, Inc.