JAVA实现随机四则运算

https://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.githttps://git.coding.net/ygy_2018103005/calc_demo.gitcoding.net项目地址:https://git.coding.net/ygy_2018103005/calc_demo.git

  1.需求分析

(1)通过程序接收参数n,随机产生n道加减乘除练习题;

(2)每个数字在0-100之间,运算符在3-5个之间;

(3)每个练习题至少包含2种运算符;

(4)运算过程中不得出现负数和非整数;

(5)计算练习题,并将正确答案以及学号输出到“result.txt”文件中。

  2.功能设计

(1)编写主函数;

(2)判断输入参数是否合法;

(3)随机生成练习题;

(4)编写计算练习题方法;

(5)结果写入result.txt文件中。

  3.设计实现

(1)编写主函数并通过命令行接收参数;

(2)判断传入参数是否合法;

(3)随机选择加减乘除运算符;

(4)根据产生运算符的个数生成一定运算数的运算式;

(5)当随机产生的运算符是“/”时,判断被除数是否能够整除除数;如果不能,随机产生能够被整除的除数;

(6)遍历list集合通过函数计算集合中的每个运算式的结果;

(7)编写写入txt文件函数。

  4.算法详解

通过JavaScript的eval函数求解字符串的运算式。

  5.测试运行

  6.核心代码

(1)随机产生n个运算式:

if(judge(num)){
            
            char[] operator = new char[]{'+','-','*','/'};
            Random random = new Random();
            ArrayList<String> expression = new ArrayList<String>();
            for(int i=0 ; i<num ; i++){
                
                int n = random.nextInt(3) + 3;//3-5个运算符
                int[] number = new int[n+1];
                String ex = new String();
                for(int j=0 ; j<=n ; j++){
                    
                    number[j] = random.nextInt(100) + 1;//4-6个数字
                    
                }
                for(int j=0 ; j<n ; j++){
                    
                    int s = random.nextInt(4);//随机选择某个运算符
                    ex += String.valueOf(number[j]) + String.valueOf(operator[s]);
                    if(s==3){
                        
                        number[j+1] = decide(number[j],number[j+1]);
                        
                    }
                    
                }
                ex += String.valueOf(number[n]);
                expression.add(ex);
                
            }

(2)通过JavaScript的eval函数计算运算式结果

    /**
     * 计算每个等式的结果,并返回运算式
     */
    static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
    private static ArrayList<String> calculate(ArrayList<String> arrayList){
        ArrayList<String> ArithExpress=new ArrayList<String>();
        for(String ax:arrayList){
            try {
                ax=ax+"="+jse.eval(ax);
                System.out.println(ax);
                ArithExpress.add(ax);
            } catch (ScriptException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return ArithExpress;
    }
    
    private static boolean judge(int data){
        if(data>=0){
            int temp=(int)data;
            if(temp==data)
                return true;
            else
                return false;
        }
        else{
            return false;
        }
}

  7.项目总结

本项目的难点在于随机生成运算式以及计算运算式结果,前者将所生成的运算式放进list中,后者通过JavaScript的eval函数求解字符串的运算式。

  8.PSP

PSP 任务内容 计划共完成需要的时间(min) 实际完成需要的时间(min)
Planning 计划 10 15
Estimate 需求分析,函数实现 10 15
Development 开发 211 388
Analysis 需求分析 5 23
Design Spec 生成设计文档 10 13
Design Review 设计复审 8 12
Coding Standard 代码规范 8 14
Design 具体设计 30 40
Coding 具体编码 120 246
Code Review 代码复审 20 25
Text 测试 10 15
Reporting 报告 15 21
Test Report 经测试,程序编译正确,结果运行正常,符合需求 5 8
Size Measurement 本次项目主要工作在于编程实现 5 7
Postmortern & Process Improvement Plan

本次项目的计算部分还有很大的改进空间,可以使用调度场算法

将中缀表达式变成后缀表达式,进而计算运算式结果

5 6

猜你喜欢

转载自www.cnblogs.com/ygy-2018103005/p/9747110.html
今日推荐