-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppEntry.java
136 lines (120 loc) · 5.16 KB
/
AppEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package github.pair.generator;
import github.pair.generator.bean.tree.ExpressionTree;
import java.io.*;
import java.util.*;
/**
* 程序入口
*/
public class AppEntry {
private static final String SAVE_PATH = new File("").getAbsolutePath()
+ File.separator + "test-result" + File.separator;
private static final Random RANDOM = new Random();
public static void main( String[] args ) throws IOException {
if (args[0].equals("-r") && args[2].equals("-n")){
int bound = Integer.parseInt(args[3]);
int range = Integer.parseInt(args[1]);
generate(bound, range);
}
else if (args[0].equals("-e") && args[2].equals("-a")){// 比对答案
compareAnswer(args[1], args[3]);
}else {
System.out.println(printUsage());
}
}
/**
* 比对用户在路径为answerFiLeStr的文件中输入的对应题号题目的答案与正确的答案
* @param exerciseFileStr 练习文件的正确答案文件的存放的绝对路径
* @param answerFiLeStr 用户提交的答案文件存放的绝对路径
*/
private static void compareAnswer(String exerciseFileStr, String answerFiLeStr) throws IOException {
File resultFiLe = new File(exerciseFileStr);
File answerFiLe = new File(answerFiLeStr);
if (resultFiLe.exists() && answerFiLe.exists()) {
BufferedReader resultReader = new BufferedReader(new FileReader(resultFiLe));
BufferedReader answerReader = new BufferedReader(new FileReader(answerFiLe));
String resultLine, answerLine;
List<String> rightList = new ArrayList<>();
List<String> wrongList = new ArrayList<>();
while ((resultLine = resultReader.readLine()) != null &&
((answerLine = answerReader.readLine()) != null)) {
String[] strings = resultLine.split(". ");
if (resultLine.equals(answerLine)) {
rightList.add(strings[0]);
}else {
wrongList.add(strings[0]);
}
}
String rightInfo = "Correct: " + getListResult(rightList);
String wrongInfo = "Wrong: " + getListResult(wrongList);
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(SAVE_PATH + "Grade.txt")));
writer.write(rightInfo);
writer.newLine();
writer.write(wrongInfo);
writer.newLine();
writer.flush();
writer.close();
}else {
System.out.println("check your file path input");
}
}
/**
* 将正确或错误的题目标号集格式化成字符串
* @param list 正确或错误的题目标号集
* @return 格式化后的字符串
*/
private static String getListResult(List<String> list) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0;i < list.size(); i++) {
if (i != list.size() - 1) {
stringBuilder.append(list.get(i)).append(", ");
}else {
stringBuilder.append(list.get(i));
}
}
return list.size() + " (" + stringBuilder.toString() + ")";
}
/**
* 生成表达式树
* @param bound 获取多少条表达式
* @param range 表达式中的数字的最大值
*/
private static void generate(int bound, int range) throws IOException {
long start = System.currentTimeMillis();
List<ExpressionTree.TraverseResult> results = new ArrayList<>(bound);
for (int i = 0; i < bound; i++) {
ExpressionTree tree = new ExpressionTree(RANDOM.nextInt(3) + 1, range);
results.add(tree.getResult());
}
write (results);
long end = System.currentTimeMillis();
System.out.println("spent " + (end - start) + " milliseconds");
}
/**
* 写入题目及答案到不同的文件
* @param results 处理完的表达式集
*/
private static void write(List<ExpressionTree.TraverseResult> results) throws IOException {
BufferedWriter questionsWriter = new BufferedWriter(new FileWriter(new File(SAVE_PATH + "Exercises.txt")));
BufferedWriter answersWriter = new BufferedWriter(new FileWriter(new File(SAVE_PATH + "Answers.txt")));
for (int i=0; i < results.size(); i++) {
questionsWriter.write((i + 1) + ". " + results.get(i).getSubExpression() + " =");
questionsWriter.newLine();
answersWriter.write((i + 1) + ". " + results.get(i).getData().toString());
answersWriter.newLine();
}
questionsWriter.flush();
questionsWriter.close();
answersWriter.flush();
answersWriter.close();
}
/**
* 获取帮助信息
* @return 帮助信息
*/
private static String printUsage() {
return "Usage:" + "\t Myapp.exe [parameter] [value]\r\n" +
"\t-n\tspecify the total count of exercises\r\n" +
"\t-r\tspecify the maximum of the value generated in the exercises\r\n" +
"the pattern must be like this : Myapp.exe -r value -n value";
}
}