一、题目描述:
1. 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 -o Exercise.txt将生成10个题目。2. 使用 -r 参数控制题目中数值(自然数、真分数和真分数分母)的范围,例如 Myapp.exe -r 10 将生成10以内(不包括10)的四则运算题目。该参数可以设置为1或其他自然数。该参数必须给定,否则程序报错并给出帮助信息。3. 生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。4. 每道题目中出现的运算符个数不超过3个。5. 程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。例如,23 + 45 = 和45 + 23 = 是重复的题目,6 × 8 = 和8 × 6 = 也是重复的题目。3+(2+1)和1+2+3这两个题目是重复的,由于+是左结合的,1+2+3等价于(1+2)+3,也就是3+(1+2),也就是3+(2+1)。但是1+2+3和3+2+1是不重复的两道题,因为1+2+3等价于(1+2)+3,而3+2+1等价于(3+2)+1,它们之间不能通过有限次交换变成同一个题目。生成的题目存入执行程序的当前目录下的Exercises.txt文件,格式如下: 1. 四则运算题目1其中真分数在输入输出时采用如下格式,真分数五分之三表示为3/5,真分数二又八分之三表示为2’3/8。6. 在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件,格式如下: 1. 答案1 特别的,真分数的运算如下例所示:1/6 + 1/8 = 7/24。7. 程序应能支持一万道题目的生成。8. 程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计,并会输出所有题目中重复的题目,输入参数如下: Myapp.exe -e <exercisefile>.txt -a <answerfile>.txt -o Grade.txt统计结果输出到文件Grade.txt,格式如下:Correct: 5 (1, 3, 5, 7, 9)Wrong: 5 (2, 4, 6, 8, 10)Repeat:2RepeatDetail:(1) 2,45+32 Repeat 3,32+45 (2) 5,3+(2+1) Repeat 7,1+2+3解释:Correct: 5 ----5道题目正确,正确的题号 1,3,5,7,9Wrong:5 -----5道题目错误,错误的题号 2,4,6,8,10Repeat:2 2---组题目重复(1) 第一组 题号2,题目 45+32 与题号3的题目重复,题号3为 32+45(2)第二组 题号5,题目 3+(2+1) 与题号7的题目重复,题号7为 1+2+3其中“:”后面的数字5表示对/错的题目的数量,括号内的是对/错题目的编号。为简单起见,假设输入的题目都是按照顺序编号的符合规范的题目。二、.需求分析,题目难度太大,用JAVA实现简单的功能要求。
节约老师出题时间和计算时间,系统直接给出题目和答案。在测试中生成10题。
三、.功能设计
能生成表达式,并计算出答案
高级功能
四、 设计实现,设计包括代码如何组织,你会有哪些Java类,这些类分别负责什么功能,他们之间的关系怎样?你会设计哪些重要的函数,关键的函数是否需要画出流程图?
1、create类生成题目。
2、express计算生成题目的表达式并答案。
五、.代码说明,你一定花了最多时间在代码的编写与调试上,那么,这个环节请把你的重要代码展示出来,代码的排版请做好。
表达式的生成:将符号放在数组里面,随机生成数来决定生成的符号。在用随机数来决定表达式的长度。若生成一个左括号就将相应右括号生成的位置存进数组里面,
当循环时,若之前有左括号,就应该判断当前循环是否是右括号应生成的位置,进行相应的判断。生成表达式。若不是随机到左括号,则按照生成一个随机数加一个运算符。
表达式的解答:将中缀表达式转换成后缀表达式,然后进行计算。(参考他人)。
六、.测试运行
无
七、个人软件过程:
(以下参考博客链接:http://www.cnblogs.com/happyzm/p/6472120.html)在开始实现程序之前,理解下Personal Software Process Stages(http://www.cnblogs.com/xinz/archive/2011/10/22/2220872.html)开始实现程序之前,请使用以下PSP表格,在第3列填上自己对每个环节的估计时间(单位:分钟)。在做每个环节的过程中,请在第4列填上对应环节的实际消耗时间(单位:分钟)在软件开发中,掌握估计时间是一项重要的技能,请通过PSP表格做到:厘清开发的完整环节,争取在每个环节都有实际的工作,一开始边界不清楚是正常的,但请尝试按自己的理解去努力在对应环节做实际的工作。预估每个环节的时间,并真实统计这些环节的耗时,在完成后输出完整的PSP耗时估计表。注意:该表格是给自己看的,不要伪造数据!PSP模版表格如下,第3列和第4列分别对应第2列条目的估计时间和真实时间,模版表格里的时间只是示意。PSP2.1 Personal Software Process Stages Time Senior Student Time 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 4· Code Review 代码复审 7 9 · Test 测试(自我测试,修改代码,提交修改) 13 20 Reporting 报告 9 6 · 测试报告 3 2 · 计算工作量 2 1 · 并提出过程改进计划 3 3 七、实验心得:收获很多,就简单实现了出题和得出答案功能。通过询问他人参考网上代码实现这点功能,能力有限,还当继续学习。输出:
代码如下:
ceate类:
package imp;
import java.util.*;
public class create {
private int n; private int range;public int getN() {
return n; }public void setN(int n) {
this.n = n; }public int getRange() {
return range; }public void setRange(int range) {
this.range = range; }public void generate(int n, int range) {
char[] symbol = { '+', '-', '*', '/', '(' }; int op; // 操作数for (int i = 0; i < n; i++) {
String eHead = "" + (i + 1) + ". "; String e =""; int exlength = (int) (Math.random() * 4 + 2);// 表达式的长度 int lbracket = 0; int rbracket = 0; int[] whe = new int[exlength]; // 存放右括号出现的地方 for (int j = 0; j < exlength; j++) { char s = symbol[(int) (Math.random() * 5)]; if (j == exlength - 1 && s == '(') { // 如果最后一次是左括号则表达式不再增加 int count1 = lbracket; if (lbracket == 0) { e += "" + (int) (Math.random() * range) + ""; } else { e += "" + (int) (Math.random() * range) + ""; for (int q = 0; q < count1; q++) { e += ")"; lbracket--; } } e += "="; continue; } if (lbracket == 0) { // 之前没有左括号 if (s == '(') { e = e + "" + s + "" + (int) (Math.random() * range) + "" + symbol[(int) (Math.random() * 4)] + "";whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1); lbracket++; rbracket++; } else { e = e + "" + (int) (Math.random() * range) + "" + s + ""; } } else if (s != '(') {// 之前有左括号 int count2 = lbracket; int count_match = 0;// 匹配到左括号的次数 for (int l = 0; l < count2; l++) { if (whe[(rbracket-count2) + l ] == j) { if (count_match == 0) { e = e + "" + (int) (Math.random() * range) + ")"; count_match++; lbracket--; } else { e += ')'; lbracket--; } } } if (count_match == 0) { e = e + "" + (int) (Math.random() * range) + "" + s + ""; } else { e += "" + s + ""; } } else {// 之前有左括号,这次又随机到左括号 int count3 = lbracket; int count = 0;// 匹配到左括号的次数 for (int m = 0; m < count3; m++) {if (whe[rbracket-count3 + m] == j) {
if (count == 0) { e = e + "" + (int) (Math.random() * range) + ")"; count++; lbracket--;} else {
e += ')'; lbracket--;}
}
}
if (count == 0) { e = e + "(" + (int) (Math.random() * range) + "" + symbol[(int) (Math.random() * 4)] + ""; whe[rbracket] = (int) (Math.random() * (exlength - j - 1) + j + 1); lbracket++; rbracket++; } else { e += "" + symbol[(int) (Math.random() * 4)] + "(" + (int) (Math.random() * range) + "" + symbol[(int) (Math.random() * 4)] + ""; whe[rbracket] = (int) (Math.random() * (exlength - j - 1) + j + 1); rbracket++; lbracket++; } }}
char[] strChar = e.toCharArray(); strChar[strChar.length - 1] = '='; for(int g=0 ;g<strChar.length;g++){ if(strChar[g]=='/'){ if(strChar[g+1]=='0'){ i=i-1; System.out.print("非法表达式"); } } } System.out.print(eHead); System.out.println(strChar); Express ee = new Express(); double result = ee.calcualte(e); System.out.println("答案:"+result); } }}
express类:
package imp;
import java.util.*;
public class Express<optStack> {
// 运算符优先级关系表 private Map<String, Map<String, String>> priorityMap = new HashMap<String, Map<String, String>>(); private Stack<String> optStack = new Stack<String>(); // 运算符栈 private Stack<Double> numStack = new Stack<Double>();// 操作数栈
/** * 计算表达式 * @param exp 四则运算表达式, 每个符号必须以空格分隔 * @return */ public double calcualte1(String exp) { StringTokenizer st = new StringTokenizer(exp); while (st.hasMoreTokens()) { String token = st.nextToken(); process(token); } return numStack.pop(); } public double calcualte(String exp) { char[] strExp = exp.toCharArray(); for(char a: strExp){ process(""+a+""); } return numStack.pop(); } /** * 读入一个符号串。 * 如果是数字,则压入numStack * 如果是运算符,则将optStack栈顶元素与该运算符进行优先级比较 * 如果栈顶元素优先级低,则将运算符压入optStack栈,如果相同,则弹出左括号,如果高,则取出2个数字,取出一个运算符执行计算,然后将结果压入numStack栈中 * @param token */ private void process(String token) { while (false == "=".equals(optStack.peek()) || false == token.equals("=")) { // token is numeric if (true == isNumber(token)) { numStack.push(Double.parseDouble(token)); break; // token is operator } else { String priority = priority(optStack.peek(), token); if ("<".equals(priority)) { optStack.push(token); break; } else if ("=".equals(priority)) { optStack.pop(); break; } else { double res = calculate(optStack.pop(), numStack.pop(), numStack.pop()); numStack.push(res); } } } } /** * 执行四则运算 * @param opt * @param n1 * @param n2 * @return */ private double calculate(String opt, double n1, double n2) { if ("+".equals(opt)) { return n2 + n1; } else if ("-".equals(opt)) { return n2 - n1; } else if ("*".equals(opt)) { return n2 * n1; } else if ("/".equals(opt)) { return n2 / n1; } else { throw new RuntimeException("unsupported operator:" + opt); } } /** * 检查一个String是否为数字 * @param token * @return */ private boolean isNumber(String token) { int LEN = token.length(); for (int ix = 0 ; ix < LEN ; ++ix) { char ch = token.charAt(ix); // 跳过小数点 if (ch == '.') { continue; } if (false == isNumber(ch)) { return false; } } return true; } /** * 检查一个字符是否为数字 * @param ch * @return */ private boolean isNumber(char ch) { if (ch >= '0' && ch <= '9') { return true; } return false; } /** * 查表得到op1和op2的优先级 * @param op1 运算符1 * @param op2 运算符2 * @return ">", "<" 或 "=" */ public String priority(String op1, String op2) { return priorityMap.get(op1).get(op2); } /** * 构造方法,初始化优先级表 */ public Express() { optStack.push("="); Map<String, String> subMap = new HashMap<String, String>(); subMap.put("+", ">"); subMap.put("-", ">"); subMap.put("*", "<"); subMap.put("/", "<"); subMap.put("(", "<"); subMap.put(")", ">"); subMap.put("=", ">"); priorityMap.put("+", subMap); subMap = new HashMap<String, String>(); subMap.put("+", ">"); subMap.put("-", ">"); subMap.put("*", "<"); subMap.put("/", "<"); subMap.put("(", "<"); subMap.put(")", ">"); subMap.put("=", ">"); priorityMap.put("-", subMap); subMap = new HashMap<String, String>(); subMap.put("+", ">"); subMap.put("-", ">"); subMap.put("*", ">"); subMap.put("/", ">"); subMap.put("(", "<"); subMap.put(")", ">"); subMap.put("=", ">"); priorityMap.put("*", subMap); subMap = new HashMap<String, String>(); subMap.put("+", ">"); subMap.put("-", ">"); subMap.put("*", ">"); subMap.put("/", ">"); subMap.put("(", "<"); subMap.put(")", ">"); subMap.put("=", ">"); priorityMap.put("/", subMap); subMap = new HashMap<String, String>(); subMap.put("+", "<"); subMap.put("-", "<"); subMap.put("*", "<"); subMap.put("/", "<"); subMap.put("(", "<"); subMap.put(")", "="); priorityMap.put("(", subMap); subMap = new HashMap<String, String>(); subMap.put("+", ">"); subMap.put("-", ">"); subMap.put("*", ">"); subMap.put("/", ">"); subMap.put(")", ">"); subMap.put("=", ">"); priorityMap.put(")", subMap); // # subMap = new HashMap<String, String>(); subMap.put("+", "<"); subMap.put("-", "<"); subMap.put("*", "<"); subMap.put("/", "<"); subMap.put("(", "<"); subMap.put("=", "="); priorityMap.put("=", subMap); } }test类:
package test;
import java.util.*;import imp.*;
public class Test {/**
* @param args */ public static void main(String[] args) { // TODO Auto-generated method stub create s =new create(); s.setN(10); //题目数量 s.setRange(10); //范围 s.generate(s.getN(), s.getRange()); }}