要求:使用C或Java語言完成一個自動生成四則運算試題的程序html
軟件基本功能以下。java
某幼兒園,老師要天天出30道加減乘除法題目給孩子作。因而,想寫一個腳本完成這件事。git
項目已開源, 開源地址: https://github.com/Tomotoes/arithmetic-generatorgithub
點擊此處下載應用。apache
項目中所用到的庫以下:多線程
其中 Main 類, 是我最欣賞的代碼片斷, 能夠體現出本人的設計思想工具
package com.tomotoes; import lombok.extern.java.Log; import lombok.val; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; import java.util.stream.IntStream; /** * @author Simon * @project arithmetic-generator * @package com.tomotoes * @date 2019/9/7 15:39 */ @Log public class Main { public static ArrayList<String> results; public static Generator generator; public static Option option; public static List<String> getResult(int amount) { generator.setArithmetics(new CopyOnWriteArrayList<>()); IntStream.range(0, amount).parallel().forEach(generator::generate); val arithmetics = generator.getArithmetics(); Map<String, Double> resultsInArithmetics = new ConcurrentHashMap<>(arithmetics.size()); arithmetics.parallelStream() .filter(arithmetic -> results.parallelStream().noneMatch(result -> result.startsWith(arithmetic))) .forEach(arithmetic -> resultsInArithmetics.put(arithmetic, Script.eval(arithmetic))); return resultsInArithmetics.entrySet().parallelStream() .filter(entry -> entry.getValue() <= option.getBound()) .map(entry -> entry.getKey() + " = " + entry.getValue()) .collect(Collectors.toList()); } public static void main(String[] args) { option = Commander.parse(args); generator = new Generator(option); results = new ArrayList<>(option.getAmount()); int numberOfAttempts = 0; val maximumOfAttempts = 30; while (results.size() != option.getAmount()) { val result = getResult(option.getAmount() - results.size()); // There may be failures. // For example, requiring 1000 arithmetics to be generated, and the maximum number of operands is 2 and the maximum number of operands is 2. // Obviously, this is not possible. numberOfAttempts = result.size() != 0 ? 0 : numberOfAttempts + 1; if (numberOfAttempts == maximumOfAttempts) { log.warning("Unable to generate the specified number of arithmetic."); return; } results.addAll(result); } Loggerr logger = new Loggerr(option.getFilePath()); results.forEach(logger::log); } }
其中Commander
類用來解析命令行參數, 並配置成 Option
對象測試
Option
類,是項目的統一配置信息gradle
Generator
類負責生成四則運算方程式ui
Script
類負責生成方程式結果
而 Logger
類負責打印結果,和保存結果
呃.. Java
的輪子真圓~
PSP2.1 | 任務內容 | 計劃完成須要時間(min) | 實際完成須要時間(min) |
---|---|---|---|
Planning | 計劃 | 5 | 10 |
Estimate | 估計時間,規劃步驟 | 5 | 10 |
Development | 開發 | 30 | 145 |
Analysis | 需求分析 | 5 | 20 |
Coding | 具體編碼 | 20 | 120 |
Test | 測試 | 5 | 5 |
Reporting | 報告 | 20 | 40 |
Postmortem&Process Improvement Plan | 總結改進 | 20 | 40 |