• 201571030134 四则运算练习


    Github地址:https://github.com/brucezhangi/sizeyunsuan.git

    项目要求

    1、需求分析

    *程序可接收一个输入参数n,然后随机产生n道加减乘除练习题,每个数字在 0 和 100 之间,运算符在3个到5个之间。

    *为了让小学生得到充分锻炼,每个练习题至少要包含2种运算符。同时,由于小学生没有分数与负数的概念,你所出的练习题在运算过程中不得出现负数与非整数,比如不能出 3/5+2=2.6,2-5+10=7等算式。

    *练习题生成好后,将学号与生成的n道练习题及其对应的正确答案输出到文件“result.txt中。

    *当程序接收的参数为3时,以下为输出文件示例。

    附加功能要求:

    *支持有括号的运算式,包括出题与求解正确答案。注意,算式中存在的括号必须大于2个,且不得超过运算符的个数。(5分)

    *扩展程序功能支持真分数的出题与运算,例如:1/6 + 1/8 + 2/3= 23/24。注意在实现本功能时,需支持运算时分数的自动化简,比如 1/2+1/6=2/3,而非4/6。(5分)

    2、功能分析

    *随机生成n个包含3个运算符0-100的加减乘除的等式。

    *等式结果包括运算途中不能产生负数。

    *结果不能为小数。

    *将结果输出到文件内,标注学号。

    3、实验设计

     4、测试运行

    eclipse测试:

    5、核心代码

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Random;
    import java.util.Scanner;
    
    public class sizeyunsuan {
    
        private static int fNum, sNum, tNum, result;
    
        private static String firstOperator, secondOperator;
    
        private final static String[] OPERATOR = { "+", "-", "*", "/" };
    
        private static int ac=1;
        public static void main(String[] args) {
            System.out.println("请输入产生题目数量:");
            Scanner s = new Scanner(System.in);
            int a=s.nextInt();
            for (int i = 0; i < a; i++) 
             {
                ys();
                    
             }
        }
    
        public static boolean ys() {
    
            while (true) {
                fNum = generateRandomNum(1, 99);
    
                sNum = generateRandomNum(1, 99);
    
                tNum = generateRandomNum(1, 99);
    
                firstOperator = OPERATOR[generateRandomNum(0, 3)];
    
                secondOperator = OPERATOR[generateRandomNum(0, 3)];
    
                if (firstOperator.equals(secondOperator)) {
                    continue;
                }
                
                try {
                    if (secondOperator.equals(Const.Operator.multiplication)
                            || secondOperator.equals(Const.Operator.division)) {
                        int preResult = ys(sNum, tNum, secondOperator);
                        if (preResult < 0) {
                            continue;
                        }
                        result = ys(fNum, preResult, firstOperator);
                    } else {
                        int preResult = ys(fNum, sNum, firstOperator);
                        if (preResult < 0) {
                            continue;
                        }
                        result = ys(preResult, tNum, secondOperator);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    continue;
                }
                
    
                if (result < 0) {
                    continue;
                } else {
    
                    StringBuffer buffer = new StringBuffer();
    
                    buffer.append(fNum).append(firstOperator).append(sNum).append(secondOperator).append(tNum)
                            .append("=").append(result).append("
    ");
                    
                    System.out.println(buffer);
    
                    if (writeToFile(buffer.toString())) {
    
                        return true;
    
                    } else {
    
                        return false;
    
                    }
    
                }
            }
        }
    
        public static int generateRandomNum(int min, int max) {
    
            Random random = new Random();
    
            return random.nextInt(max - min + 1) + min;
    
        }
    
        public static boolean writeToFile(String result) {
            
            try {
    
                 File f1 = new File("result.txt");
                 FileWriter fw = new FileWriter(f1, true);
                 PrintWriter pw = new PrintWriter(fw);
                 if(ac==1)
                 {
                     pw.println("201571030134");
                     ac=0;
                 }
                      pw.println(result);
                      fw.flush();
                      fw.close();
    
                return true;
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
                return false;
    
            }
    
        }
    
        public static Integer ys(int firstNum, int secNum, String operator) throws Exception{
    
            switch (operator) {
    
            case Const.Operator.add: {
    
                return firstNum + secNum;
    
            }
    
            case Const.Operator.subtraction: {
    
                return firstNum - secNum;
    
            }
    
            case Const.Operator.multiplication: {
    
                return firstNum * secNum;
    
            }
    
            case Const.Operator.division: {
                if (firstNum % secNum == 0) {
                    return firstNum / secNum;
                }
                else {
                    throw new Exception("");
                }
    
                
    
            }
    
            default: {
    
                return null;
    
            }
    
            }
    
        }
    
    }

    6、psp展示

    PSP2.1

    任务内容

    计划完成需要的时间(h)

    实际完成需要的时间(h)

    Planning

    计划

    0.5

    1

    ·       Estimate

    ·  估计这个任务需要多少时间,并规划大致工作步骤

    0.5

    1

    Development

    开发

    19

    24

    ··       Analysis

      需求分析 (包括学习新技术)

    5

    6

    ·       Design Spec

    ·  生成设计文档

    1

    1

    ·       Design Review

    ·  设计复审 (和同事审核设计文档)

    0.2

    0.3

    ·       Coding Standard

      代码规范 (为目前的开发制定合适的规范)

    0.2

    0.2

    ·       Design

      具体设计

    0.6

    0.6

    ·       Coding

      具体编码

    10

    12

    ·       Code Review

    ·  代码复审

    0.5

    0.5

    ·       Test

    ·  测试(自我测试,修改代码,提交修改)

    2

    4

    Reporting

    报告

    0.8

    1

    ··       Test Report

    ·  测试报告

    0.3

    0.4

    ·       Size Measurement

      计算工作量

    0.2

    0.2

    ·       Postmortem & Process Improvement Plan

    ·  事后总结 ,并提出过程改进计划

    0.3

    0.3

     7、总结

    本次设计使我印象深刻,耗时很多,由于我的JAVA基础很薄弱,没有很好的掌握语言及其应用,导致在写的过程中没有思路,丢失的内容很多,然后改起来很麻烦,设计中没能按要求完成所有任务。在以后的学习中我会继续加油,再接再厉,争取做得更出色! 

  • 相关阅读:
    docker 容器管理常用命令
    第一章 入门示例
    rsyslog 日志服务器端配置
    如何利用一个按钮绑定两个事件
    select下拉框有了空行怎么办
    如何设置select下拉禁止选择
    mysql utf8 中文
    数据化决策的魅力
    数据化决策的魅力
    minor.major version 详解
  • 原文地址:https://www.cnblogs.com/brucezhangi/p/8615054.html
Copyright © 2020-2023  润新知