结对项目-四则运算

结对项目-四则运算

这个作业属于哪个课程 软件工程
这个作业要求在哪里 作业要求
这个作业的目标 生成四则运算题
组员 学号
曾家伟 3119009446
邹佳豪 3119009450

Github地址

PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟)
Planning 计划 120 200
· Estimate · 估计这个任务需要多少时间 30 30
· Analysis · 需求分析 (包括学习新技术) 30 30
· Design Spec · 生成设计文档 10 10
· Design Review · 设计复审 (和同事审核设计文档) 30 30
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 60 70
· Design · 具体设计 120 200
· Coding · 具体编码 300 380
· Code Review · 代码复审 30 50
· Test · 测试(自我测试,修改代码,提交修改) 90 100
Reporting 报告 120 110
· Test Report · 测试报告 10 15
· Size Measurement · 计算工作量 10 15
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划 60 70
合计 1020 1310

设计思路:
我上CSDN上看到用二叉树的办法,就用这个做了。大概的步骤是这样的:1.随机数生成算法2.利用二叉树构造四则运算表达式并生成运算结果流程图。之前数据结构学过二叉树,就采用二叉树的方式构建运算表达式,用非叶子结点存储运算符,用叶子结点存储数值。运算符号就存储在一个枚举类之中,包括加减乘除。随机数就用随机生成随机数的办法。关于分数的处理,因为java不存在可以处理分数运算的类,采用面向对象的思想,我们的处理方式是将整数与分数作为一个Fracton对象来处理,即整数就是分母为1的分数,即所有参与运算的数都可以看成是分数。从实验中发现会有负数的存在,因为负数是用较大的数减去较小的数菜出现的,那我们只要把两颗子树交换即可获得正确的式子。

运算符

点击查看代码
public enum OperationalCharEnum {
 PlUS("+"),
    SUBTRACT("-"),
    MULTIPLY("*"),
    DIVIDE("/"),
    LEFT_BRACKETS("("),
    RIGHT_BRACKETS(")");
    private String valueChar;

    OperationalCharEnum(String valueChar) {
        this.valueChar = valueChar;
    }

    public String getValueChar() {
        return valueChar;
    }

    public void setValueChar(String valueChar) {
        this.valueChar = valueChar;
    }
}

二叉树节点类

点击查看代码
public class BiTreeNode {
    // 存储当前节点以下的计算结果
    public Fraction result;
    public BiTreeNode left;
    public BiTreeNode right;
    public int high;

    public BiTreeNode() {
    }

    public BiTreeNode(Fraction result, BiTreeNode left, BiTreeNode right, int high) {
        this.result = result;
        this.left = left;
        this.right = right;
        this.high = high;
    }

    // 打印出表达式
    @Override
    public String toString() {
        return result.toString();
    }


    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass()) return false;

        BiTreeNode node = (BiTreeNode) o;

        if (result != null ? !result.equals(node.result) : node.result != null)
            return false;
        if (right != null ? !right.equals(node.right) : node.right != null)
            return false;
        return left != null ? left.equals(node.left) : node.left == null;
    }

    // 递归
    @Override
    public int hashCode() {
        int result1 = result != null ? result.hashCode() : 0;
        result1 = 31 * result1 + (right != null ? right.hashCode() : 0);
        result1 = 31 * result1 + (left != null ? left.hashCode() : 0);
        return result1;
    }

}
posted @ 2021-10-12 01:00  Snyder656  阅读(30)  评论(0编辑  收藏  举报