一:题目描述
1.自动生成小学四则运算题目
2.用户能够选择数值范围以及题目个数
3.程序一次运行的题目不能重复
4.生成的题目存入执行程序的当前目录下的Exercises.txt文件、答案存入Answers.txt文件
5.程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计,并会输出所有题目中重复的题目,输入参数如下:
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:2
RepeatDetail:
(1) 2,45+32 Repeat 3,32+45
(2) 5,3+(2+1) Repeat 7,1+2+3
二:需求分析
1.生成四则运算题目,能够给定题目数量难度
2.题目及正确答案以文件形式保存
2.做答,并能判断是否正确,列出正确和错误题号
三:代码实现
1.操作数及运算符号的随机生成类num类
2.生成中缀表达式转成后缀表达式计算calculate类
3.程序入口类entrance类
后缀表达式计算
public String dealEquation(String equation){ String [] arr = equation.split(","); List<String> list = new ArrayList<String>(); for (int i = 0; i < arr.length; i++) { int size = list.size(); switch (arr[i]) { case "+": Num a = new Num(list.remove(size-2)).add(new Num(list.remove(size-2))); list.add(a.toString()); break; case "-": Num b = new Num(list.remove(size-2)).sub(new Num(list.remove(size-2))); list.add(b.toString()); break; case "*": Num c = new Num(list.remove(size-2)).muti(new Num(list.remove(size-2))); list.add(c.toString()); break; case "÷": Num d = new Num(list.remove(size-2)).division(new Num(list.remove(size-2))); list.add(d.toString()); break; default: list.add(arr[i]); break; } }
题目答案写入文件及回答的答案正确性判断
public static void run(){ String[] answer=new String[n]; try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(ename),true)); BufferedWriter awriter = new BufferedWriter(new FileWriter(new File(aname),true)); for(int i=0;i<n;i++){ Request req = new Request(); req.create(r); Node no = new Node(); no.requestToNode(req); System.out.print(i+":"+req.getStr()+'\n'); writer.write(i+":"+req.getStr()); writer.newLine(); answer[i]=req.getAnswer(); awriter.write(i+":"+req.getAnswer()); awriter.newLine(); } writer.close(); awriter.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("出题成功,请开始答题\n"); Scanner scanner = new Scanner(System.in); String[] myanswer=new String[n]; for(int i = 0;i<n;i++){ myanswer[i] = scanner.next(); } String rightAnswerResport = "Correct: ",rightAnswerList = "正确的题号";//构造正确部分呢过的答题报告 String wrongAnswerResport = "Wrong",wrongAnswerList = "错误的题号";//构造错误部分呢过的答题报告 int rightNumber = 0,wrongNumber = 0;//记录正确和错误的个数 for(int i = 0;i<n;i++){//开始校验用户的回答 if(myanswer[i].equals(answer[i])){ rightNumber++; rightAnswerList = rightAnswerList+ (i+1)+","; }else{ wrongNumber++; wrongAnswerList = wrongAnswerList + (i+1) +","; } } System.out.print(rightNumber+"道题正确"+" "+rightAnswerList+'\n'); System.out.print(wrongNumber+"道题错误"+" "+wrongAnswerList+'\n'); }
四:测试运行
五:小节
代码繁杂,有待优化。
git地址:https://git.coding.net/HHWORLD/Exp1.git