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.githttps://git.coding.net/ygy_2018103005/calc.githttps://git.coding.net/ygy_2018103005/calc.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM