要求:使用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 |