ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

实现一个自动生成小学四则运算题目的命令行程序

2020-10-12 22:00:44  阅读:256  来源: 互联网

标签:题目 String num2 int 四则运算 命令行 n1 parseInt Integer


github地址

软件工程 https://edu.cnblogs.com/campus/gdgy/informationsecurity1812
作业要求 https://edu.cnblogs.com/campus/gdgy/informationsecurity1812/homework/11157
作业目标 四则运算生成算法+单元测试+Jprofiler使用+psp表格+github使用+如何与同学合作完成项目

PSP表格

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

整体流程

首先写四种不同的运算方法,通过随机调用随机生成字符串数组,[0]为题目,[1]为答案。分别将题目与答案分别写入两个txt文档中。

主要的类

  • Creator类用于生成表达式与答案

  • Main类调用与运行

  • ReadWriTeTxt类进行写入与读取操作

主要的函数

/**
     * 题目生成函数
     *
     * @param n 需要输出的题目数量
     * @param r 题目的数值范围
     * @return exerciseArray 返回值为一个二位数组,[0]为题目,[1]为答案
     */
    public String[][] createExercise(int n, int r) {

        String[][] exerciseArray = new String[n][2];
        String[][] exerciseCheckingArray = new String[n][4];
        String[] checkingArray = new String[4];
        StringBuilder exercise;
        String part = "";
        String ans = "-1";
        String num1;
        String num2;
        double type = 0;
        Boolean repeat = false;

        for (int i = 0; i < n; i++) {
            do {
                num1 = getNum(r);
                ans = "-1";
                exercise = new StringBuilder(num1);
                for (int j = 0; j < 3; j++) {
                    do {
                        type = Math.random();
                        num2 = getNum(r);
                        if (type < 0.25) {
                            part = num1 + " + " + num2;
                            ans = plusNum(num1, num2);
                        } else if (type < 0.5) {
                            part = num1 + " - " + num2;
                            ans = minusNum(num1, num2);
                        } else if (type < 0.75) {
                            while ("1".equals(num2)) {
                                num2 = getNum(r);
                            }
                            part = num1 + " * " + num2;
                            ans = multiplyNum(num1, num2);
                        } else {
                            while ("1".equals(num2)) {
                                num2 = getNum(r);
                            }
                            part = num1 + " / " + num2;
                            ans = divideNum(num1, num2);
                        }
                    } while (!ifPositive(ans));
                    if (type >= 0.5 && (exercise.toString().contains("+") || exercise.toString().contains("-"))
                            && (!exercise.toString().contains("*") || !exercise.toString().contains("/"))) {
                        exercise = new StringBuilder("( " + exercise + " )");
                    }
                    checkingArray[j] = num1;
                    num1 = ans;
                    exercise = new StringBuilder(exercise + part.substring(part.indexOf(" ")));
                }
                exerciseArray[i][0] = exercise.toString();
                exerciseArray[i][1] = ans;
                checkingArray[3] = ans;
                sortArray(checkingArray);
                if (i != 0) {
                    repeat = ifRepeat(checkingArray, exerciseCheckingArray, i);
                }
                if (!repeat) {
                    exerciseCheckingArray[i][0] = checkingArray[0];
                    exerciseCheckingArray[i][1] = checkingArray[1];
                    exerciseCheckingArray[i][2] = checkingArray[2];
                    exerciseCheckingArray[i][3] = checkingArray[3];
                }
            } while (repeat);
        }
        return exerciseArray;
    }

程序运行结果
练习题

答案

性能分析

代码的改良

**最初为每一种类型的数分别设计其的四则运算,**
**后直接将所有数都先化为假分数,设计一种四则运算,计算结果后进行约分化简。**
/**
     * 取最大公约数函数
     *
     * @param m 分子
     * @param n 分母
     * @return 最大公约数
     */
    static int getGreatestCommonDivisor(int m, int n) {
        if (n == 0) {
            return m;
        }
        return getGreatestCommonDivisor(n, m % n);
    }

    /**
     * 约分函数
     *
     * @param str 需要约分的分数
     * @return 约分完后的分数
     */
     static String reduction(String str) {
        if (getType(str) == GREATER_TRUE_SCORE) {
            String[] s = str.split("['/]");
            int m = Integer.parseInt(s[1]);
            int n = Integer.parseInt(s[2]);
            int gcd = getGreatestCommonDivisor(m, n);
            String m1 = String.valueOf(m / gcd);
            String n1 = String.valueOf(n / gcd);
            if (Integer.parseInt(m1) == 0) {
                return s[0];
            } else if (Integer.parseInt(m1) > Integer.parseInt(n1)) {
                int integer = Integer.parseInt(s[0]) + Integer.parseInt(m1) / Integer.parseInt(n1);
                return String.valueOf(integer) + "'" + String.valueOf(Integer.parseInt(m1) % Integer.parseInt(n1)) + "/"
                        + n1;
            }
            return s[0] + "'" + m1 + "/" + n1;
        } else if (getType(str) == TRUE_SCORE) {
            int m = Integer.parseInt(str.substring(0, str.indexOf("/")));
            int n = Integer.parseInt(str.substring(str.indexOf("/") + 1));
            int gcd = getGreatestCommonDivisor(m, n);
            String m1 = String.valueOf(m / gcd);
            String n1 = String.valueOf(n / gcd);
            if (Integer.parseInt(m1) > Integer.parseInt(n1)) {
                if (Integer.parseInt(m1) % Integer.parseInt(n1) == 0) {
                    return String.valueOf(Integer.parseInt(m1) / Integer.parseInt(n1));
                }
                return String.valueOf(Integer.parseInt(m1) / Integer.parseInt(n1)) + "'"
                        + String.valueOf(Integer.parseInt(m1) % Integer.parseInt(n1)) + "/" + n1;
            }
            return m1 + "/" + n1;
        } else {
            return str;
        }
    }

单元测试

结语

本次作业是由我与刘志鸿同学一起完成的,他负责方法的内部实现,我负责方法的外部调用,刘志鸿同学非常优秀,负责大量的代码,
内部实现是复杂且繁琐的,但仍然能很好的写完,我要向其好好学习,而且本次的作业,也让我对github的使用有了更加清晰的认知。
结对作业不是一个人的作业,团队的合作更加重要。

标签:题目,String,num2,int,四则运算,命令行,n1,parseInt,Integer
来源: https://www.cnblogs.com/lovegc/p/13805821.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有