需求分析
随机四则运算生成器是一款非常实用的、能够随机出数学题的软件,让家里小孩能够随时做题做题,软件可以生成1指定范围内的加、减、乘、除,操作简单,采用的是科学的随机性出题,很适合家长们给小孩出题使用。
功能设计
基本功能
- 自动生成10道100以内的2个操作数的四则运算算式(+ - * /),要求运算结果也在100以内
- 剔除重复算式。 2 + 3 = 和 2 + 3 = 是重复算式 2 + 3 = 和 3 + 2 = 不属于重复算式
- 题目数量可定制
- 相关参数可控制是否包含乘法和除法,操作数数值范围可控(如操作数 在100以内 还是1000以内),操作数是否含负数。
- 生成的运算题存储到外部文件result.txt中
扩展功能
- 能够同时生成答案
- 控制算式输出样式,指定列数
- 答案的格式化输出
设计实现
用到了一个类Question,在该类中实现了创建算式函数、保存算式函数、算式格式函数。
这些函数存在先后的逻辑关系:先创建算式,再保存算式。
测试运行
运行代码:
生成的算式:
答案部分:
代码
产生算式的代码
1 // num 成程习题的数量 2 // is_include_Multiplication_division 是否包含乘除法 3 // size_operationNum 范围大小 4 // is_include_negative 是在否包负数 5 public String[][] CreateQuestion(int num, boolean is_include_Multiplication_division, int size_operationNum, 6 boolean is_include_negative) { 7 int num1, num2, result; 8 int type; 9 int count = 0; 10 String[][] Question_Answer = new String[99999][2]; 11 while (true) { 12 if (count >= num) 13 break; 14 if (is_include_negative) { 15 num1 = (int) ((Math.random() * size_operationNum) - 50); 16 num2 = (int) ((Math.random() * size_operationNum) - 50); 17 } else { 18 num1 = (int) (Math.random() * size_operationNum); 19 num2 = (int) (Math.random() * size_operationNum); 20 } 21 if (!is_include_negative) 22 if (num1 < 0 || num2 < 0) 23 continue; 24 type = 1 + (int) (Math.random() * 4); 25 if (type == 1) { 26 result = num1 + num2; 27 if (result < 100) { 28 Question_Answer[count][0] = num1 + " + " + num2 + " ="; 29 Question_Answer[count][1] = result+""; 30 count++; 31 } 32 } else if (type == 2) { 33 result = num1 - num2; 34 if (result < 100) { 35 Question_Answer[count][0] = num1 + " - " + num2 + " ="; 36 Question_Answer[count][1] = result+""; 37 count++; 38 } 39 } else if (type == 3 && is_include_Multiplication_division) { 40 result = num1 * num2; 41 if (result < 100) { 42 Question_Answer[count][0] = num1 + " * " + num2 + " ="; 43 Question_Answer[count][1] = result+""; 44 count++; 45 } 46 } else if (type == 4 && is_include_Multiplication_division) { 47 Double result_; 48 if (num2 == 0) 49 continue; 50 result_ = (double) num1 / num2; 51 if (result_ < 100) { 52 Question_Answer[count][0] = num1 + " / " + num2 + "="; 53 Question_Answer[count][1] = String.format("%.2f", result_); // 精度控制 54 count++; 55 } 56 } 57 } 58 return Question_Answer; 59 }
保存算式代码
1 /* 2 * rows控制输出列 3 */ 4 public void SaveToDisk(String[][] question_answer, int lines, int rows) { 5 // 定义输入输出类对象 6 OutputStream outputStream = null; 7 // 获取时间,用于生成随时间变化的文件名 8 LocalDate date = LocalDate.now(); 9 // 创建result.txt文件 10 try { 11 outputStream = new FileOutputStream("./result" + date + ".txt"); 12 } catch (FileNotFoundException e) { 13 e.printStackTrace(); 14 } 15 // 标题格式化并写入文件 16 byte[] header = "-*--*--*--*--*--*--*--*--*--*--*- 四 则 算 术 题 -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\r\n" 17 .getBytes(); 18 try { 19 outputStream.write(header); 20 } catch (IOException e1) { 21 e1.printStackTrace(); 22 } 23 // 题目主体部分格式化并写入文件 24 try { 25 byte[] bs = null; 26 String str = null; 27 int count = 0; 28 for (int i = 0; i < lines; i++) { 29 str = ""; 30 for (int j = 0; j < rows; j++) { 31 if (question_answer[count][0] == null) // 如果处算式为空,则表明到了算式尾,结束拼接 32 break; 33 str += "\t" + question_answer[count][0] + "____\t"; // 将一行算式拼接,指定rows列 34 count++; 35 } 36 str += "\r\n"; 37 bs = str.getBytes(); 38 outputStream.write(bs); 39 if (count >= lines) 40 break; 41 } 42 } catch (IOException e1) { 43 e1.printStackTrace(); 44 } 45 46 // 尾部格式化并写入文件 47 header = "\r\n\r\n-*--*--*--*--*--*--*--*--*--*--*- 参 考 答 案 -*--*--*--*--*--*--*--*--*--*--*--*--*--*-\r\n" 48 .getBytes(); 49 try { 50 outputStream.write(header); 51 } catch (IOException e1) { 52 e1.printStackTrace(); 53 } 54 55 // 答案格式化并写入文件 56 try { 57 String str_answer = ""; 58 int count_answer = 0; 59 byte[] bs = null; 60 for (int i = 0; i < lines; i++) { 61 str_answer=""; 62 for (int j = 0; j < 5; j++) { 63 if (question_answer[count_answer][1] == null) 64 break; 65 str_answer += "\t" + question_answer[count_answer][1] + "\t"; 66 count_answer++; 67 } 68 str_answer += "\r\n"; 69 bs = str_answer.getBytes(); 70 outputStream.write(bs); 71 if (count_answer >= lines) 72 break; 73 } 74 } catch (IOException e1) { 75 e1.printStackTrace(); 76 } 77 }
总结
模块化体现在将产生算式、格式化算式、保存算式分开,让程序的耦合度降低。尽量做到改变一个函数中的代码不会影响到其他函数,遵循了可插拔的原则。
PSP
PSP2.1 | 任务内容 | 计划共完成需要的时间(min) | 实际完成需要的时间(min) |
Planning | 计划 | 8 | 6 |
· Estimate | · 估计这个任务需要多少时间,并规划大致工作步骤 | 8 | 6 |
Development | 开发 | 82 | 88 |
·· Analysis | 需求分析 (包括学习新技术) | 6 | 10 |
· Design Spec | · 生成设计文档 | 5 | 6 |
· Design Review | · 设计复审 (和同事审核设计文档) | 4 | 6 |
· Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 3 | 3 |
· Design | 具体设计 | 10 | 12 |
· Coding | 具体编码 | 36 | 21 |
· Code Review | · 代码复审 | 7 | 9 |
· Test | · 测试(自我测试,修改代码,提交修改) | 13 | 21 |
Reporting | 报告 | 9 | 6 |
·· Test Report | · 测试报告 | 3 | 2 |
· Size Measurement | 计算工作量 | 2 | 1 |
· Postmortem & Process Improvement Plan | · 事后总结 ,并提出过程改进计划 | 3 | 3 |