前言:
本次作业主要考察类的设计,从第一次作业只需三到五个类就能解决问题到第三次需要六七个类以上才能解决问题,这要求我们在写程序之前必须设计好各个类之间的关系和职责,这需要我们有良好的设计习惯并且遵守面向对象程序设计的原则。这几次作业难度很大,第一次作业200行可以搞定,第二次作业起码要400行,第三次作业就来到了500行代码。
每次作业纯编码时间可能就需要20小时以上。难度可以说非常大。
设计与分析:
因为本体需要分析输入的各种复杂信息,所以学习正则表达式的使用是必要的。
第一次作业需要读取
1、 题目数量,格式为整数数值,若超过1位最高位不能为0;
2、 题目内容,格式为:"#N:"+题号+" "+"#Q:"+题目内容+" "#A:"+标准答案;
格式约束:题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
3、答题信息,答题信息按行输入,每一行为一组答案,每组答案包含第2部分所有题目的解题答案,答案的顺序号与题目题号相对应。
格式:"#A:"+答案内容
格式约束:答案数量与第2部分题目的数量相同,答案之间以英文空格分隔。
样例:#A:2 #A:78
所以以下三个类是需要的。
题目类(Question):用于保存一道题目的信息以及处理的方法。
private int num;// 题号
private String content, // 题目内容
standardAnswer;// 标准答案
boolean matchingStandardAnswers(String answer):判断是否符合标准答案
试卷类(TestPaper):保存和处理一张试卷的信息以及处理的方法
int questionNum;// 题目数量
Question[] questions;//题目清单
void inputQuestion(int num, Question question):添加一道题目
void printQuestions():输出题目的内容~标准答案
boolean markQuestion(int num, String answer):判断第 num 题的正确性。
答卷类(AnswerPaper):保存和处理一张答卷的信息以及处理的方法
TestPaper paper;//试卷信息
String[] answers;//每一题的答案信息
boolean[] marks;//每一题的判题结果(对/错)
void printQ_A(int num):输出第 num 题的题目和答案(卷面答案,非标准答案)
boolean getJudge(int num):获得第 num 题的判题结果
由于输入信息需要用正则表达式来读取,所以最好还要设计一个用来分析信息的类。
匹配输入类(InputMatching)
questionNumMatching:String =””//正则表达式用于匹配题号
questionMatching:String=””//匹配题目内容
standerAnswerMatching:String=“”//匹配标准答案
questionInput:String = questionNumMatching+“”+ questionMatching+“”standerAnswerMatching
answerMatching: String = “”//匹配答案
具体实现是这样的
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//首先读取题目数量
int number = input.nextInt();
//这一步是为了将换行符读取,以避免影响后续字符串的输入
String skipEnter = input.nextLine();
//创建问题类数组存取各个问题
Question[] q = new Question[number];
//创建字符串数组,将接下来所有要输入的字符串按行读入
String[] k = new String[number];
//设计正则表达式来分析各个字符串,建议大家这部分可以单独创建一个类,方便后续进行拓展,当时写这里的时候完全没考虑拓展的问题。
、、匹配题号
String regexN = "#N:(.)#Q";
、、匹配题目内容
String regexQ = "#Q:(.)#";
、、匹配标准答案
String regexA = "#A:(.*)";
Pattern patternN = Pattern.compile(regexN);
Pattern patternQ = Pattern.compile(regexQ);
Pattern patternA = Pattern.compile(regexA);
Matcher matcherA;
//将题目信息读入
for (int i = 0; i < number; i++) {
q[i] = new Question();
k[i] = input.nextLine();
Matcher matcherN = patternN.matcher(k[i]);
Matcher matcherQ = patternQ.matcher(k[i]);
matcherA = patternA.matcher(k[i]);
if(matcherN.find()) {
String num = matcherN.group(0);
//System.out.println(num);
num = num.substring(3,num.length() - 2);
int temp = Integer.parseInt(num.trim());
q[i].setID(temp);
}
if(matcherQ.find()){
String content = matcherQ.group(0);
content = content.substring(3,content.length() - 2);
q[i].setContent(content.trim());
//System.out.println(q[i].getContent());
}
if(matcherA.find()){
String standardAnswer = matcherA.group(0);
standardAnswer =standardAnswer.substring(3);
q[i].setStandardAnswer(standardAnswer.trim());
//System.out.println(q[i].getStandardAnswer());
}
}
//将答案读入
String str = input.nextLine();
String regex1 = "#A:(\w+)";
Pattern pattern = Pattern.compile(regex1);
matcherA = pattern.matcher(str);
String[] answerList = new String[100];
for(int i = 0; i < number; i++){
if(matcherA.find()){
answerList[i] = matcherA.group(0);
//System.out.println(answerList[i]);
answerList[i] = answerList[i].substring(3);
//System.out.println(answerList[i]);
}
}
str = input.nextLine();
// if(str.equals("end")){
// input.close();
// }
、、创建试卷类,并将问题数组和问题数量传给试卷类
ExaminationPaper exam = new ExaminationPaper(q,number);
boolean[] isTrueList = new boolean[100];
、、将问题按题号顺序排好
if(number >= 2){
for(int i = 0; i < number - 1; i++){
for(int j = 0; j < number - 1; j++){
if(q[j].getID() > q[j + 1].getID()){
Question temp = q[j + 1];
q[j + 1] = q[j];
q[j] = temp;
// String cnt = answerList[j];
// answerList[j] = answerList[j + 1];
// answerList[j + 1] = cnt;
}
}
}
}
、、将答案按格式输出
for(int i = 0; i < number; i++){
// System.out.println(q[i].getStandardAnswer());
// System.out.println(answerList[q[i].getID() - 1]);
isTrueList[q[i].getID() - 1] = q[i].getStandardAnswer().equals(answerList[q[i].getID() - 1]);
}
AnswerSheet answerSheet = new AnswerSheet( exam, answerList,isTrueList);
for(int i = 0; i < number; i++){
answerSheet.print(q[i].getID());
}
for(int i = 0; i < number; i++){
if(i + 1 == number){
System.out.print(isTrueList[i]);
}else{
System.out.print(isTrueList[i] + " ");
}
}
}
}
class Question{
private int ID;
private String content;
private String standardAnswer;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStandardAnswer() {
return standardAnswer;
}
public void setStandardAnswer(String standardAnswer) {
this.standardAnswer = standardAnswer;
}
public Question() {
}
public Question(int ID, String content, String standardAnswer) {
this.ID = ID;
this.content = content;
this.standardAnswer = standardAnswer;
}
public boolean isTrue(String answer){
return answer.equals(standardAnswer);
}
}
class ExaminationPaper {
public ExaminationPaper() {
}
public ExaminationPaper(Question[] questions, int number) {
this.questions = questions;
this.number = number;
}
private Question[] questions = new Question[100];
private int number;
public int getNumber() {
return number;
}
public void createArray(){
for(int i = 0; i < 100; i++) {
questions[i] = new Question();
}
}
public Question[] getQuestions(){
return questions;
}
public void saveQuestions(int number, String question) {
// for(int i = 0; i < 100; i++) {
// questions[i] = new Question();
// }
questions[number].setContent(question);
questions[number].setID(number);
}
public boolean isTrue(int num, String answer) {
return questions[num].isTrue(answer);
}
}
class AnswerSheet{
private ExaminationPaper paper = new ExaminationPaper();
private String[] answer = new String[100];
private boolean[] isTrue = new boolean[100];
public AnswerSheet(ExaminationPaper paper, String[] answer, boolean[] isTrue) {
this.paper = paper;
this.answer = answer;
this.isTrue = isTrue;
}
public String[] getAnswer() {
return answer;
}
public void setAnswer(String[] answer) {
this.answer = answer;
}
public boolean[] getIsTrue() {
return isTrue;
}
public void setIsTrue(boolean[] isTrue) {
this.isTrue = isTrue;
}
public AnswerSheet() {
}
public boolean judgment(int num){
return paper.isTrue(num,answer[num]);
}
public void print(int num){
Question[] question = paper.getQuestions();
System.out.print(question[num - 1].getContent().trim());
System.out.print("~");
System.out.println(answer[num - 1]);
}
public void saveAnswer(int num, String str){
answer[num] = str;
}
}
第二次作业相比于第一次作业,有一些输入的东西变多了,有部分输入的格式也有所改变,并且输入变为乱序输入,这里要注意一些很恶心细节问题,例如答卷答案与问题的匹配顺序,可以看到答案的顺序与试卷中的题目顺序是一致的。我帮大家理解理解这句话,因为这个题目非常长,通读完整个题目后不免会将一些细节忘记,而就是这些微小的细节,会让你在后续修改bug的过程非常痛苦。回到正题,例如试卷是#T:1 3-5 4-8 5-2,答卷是#S:1 #A:5 #A:22,这意味着#A:5是第3题的答案,#A:22第四题的答案,第5题没有答案。大家一定要创建一个记事本,把一些关键的要求记下来,不然纯靠脑子记真的会搞晕。第二次还涉及到了多张试卷多张答卷的问题,具体的看接下来的源码
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//int number = input.nextInt();
final int number = 100;
//String skipEnter = input.nextLine();
String[] answerList = new String[100];
Question[] q = new Question[number];
AnswerSheet[] a = new AnswerSheet[10];
ExaminationPaper[] e = new ExaminationPaper[10];
String[] k = new String[number];
int[] numList = new int[100];
int[] scoreList = new int[100];
int j = 0;
String regexN = "#N:(.*)#Q";
String regexQ = "#Q:(.*)#";
String regexA = "#A:(.*)";
String regexT = "#T:(\\w+)";
String regexS = "#S:(\\w+)";
String regex1 = "#A:(\\w+)";
String regexNum =" [\\w\\-]+";
Pattern patternN = Pattern.compile(regexN);
Pattern patternQ = Pattern.compile(regexQ);
Pattern patternA = Pattern.compile(regexA);
Pattern patternT = Pattern.compile(regexT);
Pattern patternS = Pattern.compile(regexS);
Pattern pattern1 = Pattern.compile(regex1);
Pattern patternNum = Pattern.compile(regexNum);
//将题目信息读入
for (int i = 0; i < number; i++) {
k[i] = input.nextLine();
if(k[i].equals("end")){
break;
}
Matcher matcherN = patternN.matcher(k[i]);
Matcher matcherQ = patternQ.matcher(k[i]);
Matcher matcherA = patternA.matcher(k[i]);
Matcher matcherS = patternS.matcher(k[i]);
Matcher matcherT = patternT.matcher(k[i]);
Matcher matcher1 = pattern1.matcher(k[i]);
Matcher matcherNum = patternNum.matcher(k[i]);
if(matcherN.find()) {
String num = matcherN.group(0);
//System.out.println(num);
num = num.substring(3,num.length() - 2);
int temp = Integer.parseInt(num.trim());
q[temp] = new Question();
q[temp].setID(temp);
if(matcherQ.find()){
String content = matcherQ.group(0);
content = content.substring(3,content.length() - 2);
q[temp].setContent(content);
//System.out.println(q[i].getContent());
}
if(matcherA.find()){
String standardAnswer = matcherA.group(0);
standardAnswer =standardAnswer.substring(3);
q[temp].setStandardAnswer(standardAnswer);
//System.out.println(q[i].getStandardAnswer());
}
}
if(matcherS.find()) {
String id = matcherS.group(1).trim();
int ID = Integer.parseInt(id);
if (a[ID] != null) {
ID++;
a[ID] = new AnswerSheet();
a[ID].setID(ID - 1);
}else {
a[ID] = new AnswerSheet();
a[ID].setID(ID);
}
int[] temp = new int[100];
if(e[a[ID].getID()] != null) {
temp = e[a[ID].getID()].getNumList();
}else{
break;
}
// System.out.println(e[ID].getNumLength());
for (i = 0; i < 10; i++) {
answerList[i] = null;
}
int w = 0;
while (w <= e[a[ID].getID()].getNumLength()) {
if (matcher1.find()) {
int p = temp[w];
answerList[p] = matcher1.group(0);
//System.out.println(answerList[i]);
//answerList[ID] = new String();
answerList[p] = answerList[p].substring(3);
//System.out.println(answerList[i]);
}
w++;
}
a[ID].setAnswer(answerList);
// if(a[1] != null) {
// for (i = 0; i < 100; i++) {
// System.out.println(a[1].getAnswer()[i]);
// }
// }
}
if(matcherT.find()){
String id = matcherT.group(0);
id = id.substring(3,id.length()).trim();
int ID = Integer.parseInt(id);
j = 0;
for(i = 0; i < j; i++){
numList[i] = 0;
scoreList[i] = 0;
}
while(matcherNum.find()){
String num = matcherNum.group(0);
int temp = num.indexOf("-");
String forward = num.substring(1, temp).trim();
String behind = num.substring(temp + 1).trim();
int f = Integer.parseInt(forward);
int b = Integer.parseInt(behind);
numList[j++] = f;
scoreList[f] = b;
}
e[ID] = new ExaminationPaper();
e[ID].setNumLength(j);
e[ID].setID(ID);
e[ID].setNumList(numList);
e[ID].setScoreList(scoreList);
}
}
//System.out.println(e[1].getID());
// 如何按入顺序匹配答案?
boolean[] isTrueList = new boolean[100];
int cnt = 1;
//判题
for(int i = 1; e[i] != null; i++){
e[i].setQuestions(q);
for(int p = 1; a[p] != null; p++){
a[p].setPaper(e[a[cnt].getID()]);
int cnt1 = 0;
int num = 0;
do{
if(e[a[p].getID()] != null) {
num = e[a[p].getID()].getNumList()[cnt1];
}else{
break;
}
isTrueList[num] = q[num].getStandardAnswer().equals(a[p].getAnswer()[num]);
a[p].setIsTrue(isTrueList);
cnt1++;
}while(e[a[p].getID()].getNumList()[cnt1] != 0);
}
}
cnt = 1;
//输出答案
while(e[cnt] != null){
if(e[cnt].totalPoint() == 100){
cnt++;
continue;
}else{
System.out.printf("alert: full score of test paper%d is not 100 points\n",e[cnt].getID());
}
cnt++;
}
cnt = 1;
int p = 0;
for(int i = 1; e[i] != null; i++){
for(p = 1; a[p] != null; p++){
if(a[p].getID() == e[i].getID()) {
for (int t = 0; t < e[a[p].getID()].getNumLength(); t++) {
int z = e[a[p].getID()].getNumList()[t];
a[p].print(z);
}
a[p].gotPointPrint(e[i].getNumList(), e[i].getNumLength());
}
}
if(a[p] == null){
if(a[p-1] != null){
continue;
}
System.out.print("The test paper number does not exist");
break;
}
}
}
}
class Question{
private int ID;
private String content;
private String standardAnswer;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStandardAnswer() {
return standardAnswer;
}
public void setStandardAnswer(String standardAnswer) {
this.standardAnswer = standardAnswer;
}
public Question() {
}
public Question(int ID, String content, String standardAnswer) {
this.ID = ID;
this.content = content;
this.standardAnswer = standardAnswer;
}
public boolean isTrue(String answer){
return answer.equals(standardAnswer);
}
}
class ExaminationPaper {
final int n = 10;
private int numLength;
private int ID;
private Question[] questions = new Question[100];
private int[] numList = new int[100];
private int[] scoreList = new int[100];
private int number;
public ExaminationPaper() {
}
public ExaminationPaper(int numLength, int ID, int[] numList) {
this.numLength = numLength;
this.ID = ID;
this.numList = numList;
}
public ExaminationPaper(Question[] questions, int number, int ID) {
this.questions = questions;
this.number = number;
this.ID = ID;
}
public int getNumLength() {
return numLength;
}
public void setNumLength(int numLength) {
this.numLength = numLength;
}
public int[] getNumList() {
return numList;
}
public int getN() {
return n;
}
public int[] getScoreList() {
return scoreList;
}
public void setScoreList(int[] scoreList) {
for(int i = 0; i < scoreList.length; i++){
this.scoreList[i] = scoreList[i];
}
}
public void setNumList(int[] numList) {
for(int i = 0; i < numLength; i++){
this.numList[i] = numList[i];
}
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public void setQuestions(Question[] questions) {
for(int i = 0; i < n; i++){
this.questions[i] = questions[i];
}
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void createArray(){
for(int i = 0; i < 100; i++) {
questions[i] = new Question();
}
}
public Question[] getQuestions(){
return questions;
}
public void saveQuestions(int number, String question) {
// for(int i = 0; i < 100; i++) {
// questions[i] = new Question();
// }
questions[number].setContent(question);
questions[number].setID(number);
}
public int totalPoint(){
int sum = 0;
int i = 0;
do{
sum += scoreList[numList[i]];
i++;
}while(numList[i] != 0);
return sum;
}
public boolean isTrue(int num, String answer) {
return questions[num].isTrue(answer);
}
}
class AnswerSheet{
final int n = 10;
private ExaminationPaper paper = new ExaminationPaper();
private String[] answer = new String[100];
private boolean[] isTrue = new boolean[100];
private int ID;
public AnswerSheet(int ID, ExaminationPaper paper, String[] answer, boolean[] isTrue) {
this.paper = paper;
this.answer = answer;
this.isTrue = isTrue;
this.ID = ID;
}
public ExaminationPaper getPaper() {
return paper;
}
public void setPaper(ExaminationPaper paper) {
this.paper = paper;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String[] getAnswer() {
return answer;
}
public void setAnswer(String[] answer) {
for(int i = 0; i < n; i++){
this.answer[i] = answer[i];
}
}
public boolean[] getIsTrue() {
return isTrue;
}
public void setIsTrue(boolean[] isTrue) {
for (int i = 0; i < n; i++) {
this.isTrue[i] = isTrue[i];
}
}
public AnswerSheet() {
}
public boolean judgment(int num){
return paper.isTrue(num,answer[num]);
}
public void print(int num){
//Question[] question = paper.getQuestions();
if(answer[num] != null){
System.out.print(paper.getQuestions()[num].getContent().trim());
System.out.print("~");
System.out.print(answer[num]);
System.out.print("~");
System.out.println(isTrue[num]);
}else{
System.out.println("answer is null");
}
}
public void gotPointPrint(int[] numlist, int lenth){
int got = 0;
int j = 0;
for(;j < lenth; j++) {
int i = numlist[j];
if (isTrue[i]) {
got += paper.getScoreList()[i];
System.out.print(paper.getScoreList()[i]);
if(j + 1 != lenth) {
System.out.print(" ");
}
}else{
System.out.print("0");
if(j + 1 != lenth) {
System.out.print(" ");
}
}
}
System.out.print("~");
System.out.println(got);
}
public void saveAnswer(int num, String str){
answer[num] = str;
}
}
第三次相比于第二次多增加了格式的检测,这就要求对正则表达式的使用要更加熟练。由于前两次没有自己设计类图,而且仅仅凭着题目的建议和自己的感觉硬写,导致写到后期思路很乱,各个类之间的关系不明了,也没做到单一职责,这导致后期代码量上来以后,每次开始写自己代码前还要浪费很多时间去回想这些代码的作用。痛定思痛,我终于明白设计的重要性,于是在第三次作业开始后,我并没有着急的进行编码,而是先使用powerdesigner进行了类的设计,如图
当时设计的时候还很青涩,如果你有能力的话可以设计更加完善的类图,如下
设计类图的好处是非常多的,其中我最喜欢的一点是我的心里对这个500多行的程序有了一个很清晰的结构。接下来看源码:
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
//初始化变量
Scanner input = new Scanner(System.in);
final int number = 100;
String[] answerList = new String[100];
int[] answerNumberList = new int[100];
Question[] Q = new Question[number];
AnswerSheet[] A = new AnswerSheet[10];
ExaminationPaper[] E = new ExaminationPaper[10];
Student[] S = new Student[10];
Judgment[] J = new Judgment[10];
String[] k = new String[number];
int[] numList = new int[100];
int[] scoreList = new int[100];
int[] deleteList = new int[100];
//用于迭代T
int j = 0;
//用于迭代A
int cntA = 0;
//用于迭代deleteList
int cntD = 0;
String regexN = "#N:(.*)#Q";
String regexN1 = "^#N:(\\d+) (#Q:[^#]*) (#A:(.*)){1}$";
String regexQ = "#Q:(.*)#";
String regexA = "#A:(.*)";
String regexA1 = "#A:(.*)";
String regexT = "#T:(\\w+)";
//检测是否符合问题的格式
String regexT1 = "#T:\\d+(\\s\\d-\\d)*(\\s\\d-\\d)$";
String regexS = "#S:(\\w+)";
String regexS1 = "#S:\\d+ \\d+(.*)";
String regex1 = "#A:(\\d-[\\w.]*)";
String regexNum = " [\\w\\-]+";
String regexX = "#X(.*)";
String regexX1 = "#X:(\\d{8} \\w*-)*(\\d{8} \\w{3})$";
String regexD = "#D(.*)";
String regexD1 = "#D:(N-\\d+)$";
String regexID = "\\w+";
String regexS2 = "( \\d+)";
String regexD2 = "(\\d+)";
Pattern patternN = Pattern.compile(regexN);
Pattern patternQ = Pattern.compile(regexQ);
Pattern patternA = Pattern.compile(regexA);
Pattern patternT = Pattern.compile(regexT);
Pattern patternS = Pattern.compile(regexS);
Pattern pattern1 = Pattern.compile(regex1);
Pattern patternNum = Pattern.compile(regexNum);
Pattern patternX = Pattern.compile(regexX);
Pattern patternD = Pattern.compile(regexD);
Pattern patternID = Pattern.compile(regexID);
Pattern patternS2 = Pattern.compile(regexS2);
Pattern patternD2 = Pattern.compile(regexD2);
Pattern patternD1 = Pattern.compile(regexD1);
Pattern patternN1 = Pattern.compile(regexN1);
Pattern patternS1 = Pattern.compile(regexS1);
Pattern patternX1 = Pattern.compile(regexX1);
Pattern patternT1 = Pattern.compile(regexT1);
//将题目信息读入
for (int i = 0; i < number; i++) {
k[i] = input.nextLine();
if (k[i].equals("end")) {
break;
}
Matcher matcherN = patternN.matcher(k[i]);
Matcher matcherQ = patternQ.matcher(k[i]);
Matcher matcherA = patternA.matcher(k[i]);
Matcher matcherS = patternS.matcher(k[i]);
Matcher matcherT = patternT.matcher(k[i]);
Matcher matcher1 = pattern1.matcher(k[i]);
Matcher matcherNum = patternNum.matcher(k[i]);
Matcher matcherX = patternX.matcher(k[i]);
Matcher matcherD = patternD.matcher(k[i]);
Matcher matcherS2 = patternS2.matcher(k[i]);
Matcher matcherD2 = patternD2.matcher(k[i]);
Matcher matcherN1 = patternN1.matcher(k[i]);
Matcher matcherT1 = patternT1.matcher(k[i]);
Matcher matcherS1 = patternS1.matcher(k[i]);
Matcher matcherD1 = patternD1.matcher(k[i]);
Matcher matcherX1 = patternX1.matcher(k[i]);
if (matcherN1.find()) {
if (matcherN.find()) {
String num = matcherN.group(0);
//System.out.println(num);
num = num.substring(3, num.length() - 2);
int temp = Integer.parseInt(num.trim());
Q[temp] = new Question();
Q[temp].setQuestionNumber(temp);
if (matcherQ.find()) {
String content = matcherQ.group(0);
content = content.substring(3, content.length() - 2);
Q[temp].setContent(content);
//System.out.println(Q[i].getContent());
}
if (matcherA.find()) {
String standardAnswer = matcherA.group(0);
standardAnswer = standardAnswer.substring(3);
Q[temp].setStandardAnswer(standardAnswer);
//System.out.println(Q[i].getStandardAnswer());
}
}
}
else if (matcherS1.find()) {
if (matcherS.find()) {
String id = matcherS.group(1).trim();
if (matcherS2.find()) {
String studentNumber = matcherS2.group(0).trim();
int ID = Integer.parseInt(id);
A[cntA] = new AnswerSheet();
A[cntA].setAnswerSheetNumber(ID);
A[cntA].setStudentNumber(studentNumber);
int p = 0;
for (int la = 0; la < 10; la++) {
answerList[la] = null;
answerNumberList[la] = 0;
}
while (matcher1.find()) {
String temp = matcher1.group(0);
String str = temp.substring(3, 4);
String temp1 = temp.substring(5);
answerNumberList[p] = Integer.parseInt(str);
answerList[p] = temp1;
p++;
}
A[cntA].setAnswerList(answerList);
A[cntA].setAnswerNumberList(answerNumberList);
cntA++;
}
}
}
else if (matcherT1.find()) {
matcherT1.reset();
if (matcherT.find()) {
String id = matcherT.group(0);
id = id.substring(3, id.length()).trim();
int ID = Integer.parseInt(id);
for (i = 0; i < j; i++) {
numList[i] = 0;
scoreList[i] = 0;
}
int z = 0;
if (matcherNum.find()) {
matcherNum.reset();
while (matcherNum.find()) {
String num = matcherNum.group(0);
int temp = num.indexOf("-");
String forward = num.substring(1, temp).trim();
String behind = num.substring(temp + 1).trim();
int f = Integer.parseInt(forward);
int b = Integer.parseInt(behind);
numList[z] = f;
scoreList[z] = b;
z++;
}
E[j] = new ExaminationPaper();
E[j].setTestNumber(ID);
E[j].setQuestionNumberList(numList);
E[j].setQuestionScoreList(scoreList);
E[j].setQuestions(Q);
j++;
}
}
}
else if (matcherX1.find()) {
if(matcherX.find()) {
String str = k[i].substring(3);
Matcher matcherID = patternID.matcher(str);
int cnt = 0;
String name = "";
String studentNumber = "";
boolean temp = false;
while (matcherID.find()) {
if (temp) {
name = matcherID.group(0);
temp = false;
S[cnt].setName(name);
cnt++;
} else {
S[cnt] = new Student();
studentNumber = matcherID.group(0);
temp = true;
S[cnt].setStudentNumber(studentNumber);
}
}
}
}
else if (matcherD1.find()) {
if(matcherD.find()) {
while (matcherD2.find()) {
deleteList[cntD++] = Integer.parseInt(matcherD2.group(0));
}
}
}
else{
//若格式错误
System.out.println("wrong format:" + k[i]);
}
}
//进行删除操作
for(int i = 0; E[i] != null; i++){
for(int r = 0; deleteList[r] != 0; r++){
int z = deleteList[r];
E[i].deleteQuestion(z);
}
}
int cnt = 0;
//输出答案
while(E[cnt] != null){
if(E[cnt].getTotalPoint() == 100){
cnt++;
continue;
}else{
System.out.println("alert: full score of test paper" + E[cnt].getTestNumber() + " is not 100 points");
cnt++;
}
}
cnt = 1;
int p = 0;
//从第一张答卷开始遍历
for(int i = 0; A[i] != null; i++){
//判断能否找到试卷
boolean isExist = false;
//遍历试卷
for(p = 0; E[p] != null; p++){
//若匹配到了试卷
if(A[i].getAnswerSheetNumber() == E[p].getTestNumber()) {
//判题
J[i] = new Judgment(E[p], A[i]);
J[i].setIsTrueList();
//用于迭代A[i].getAnswerNumberList()[t]
int a = 0;
for (int t = 0; E[p].getQuestionNumberList()[t] != 0; t++) {
//题目编号
int z = E[p].getQuestionNumberList()[t];
//若答案存在
boolean isAnswerExist = false;
for(a = 0; A[i].getAnswerNumberList()[a] != 0; a++){
if(A[i].getAnswerNumberList()[a] == t + 1){
isAnswerExist = true;
break;
}
}
if (isAnswerExist) {
//若题目存在
if (E[p].getQuestions()[z] != null) {
System.out.print(E[p].getQuestions()[z].getContent());
System.out.print("~");
System.out.print(A[i].getAnswerList()[a]);
System.out.print("~");
System.out.println(J[i].getIsTrueList()[a]);
} else {
//若题目不存在
//若题目是被删除的
boolean isDelete = false;
int test = 0;
while(deleteList[test] != 0){
if(z == deleteList[test]){
System.out.println("the question " + z + " invalid~0");
isDelete =true;
break;
}
test++;
}
//引用题号不存在
if(!isDelete){
System.out.println("non-existent question~0");
}
}
}else{
//答案不存在(优先级最高)
System.out.println("answer is null");
}
}
isExist =true;
break;
}else {
isExist =false;
}
}
//若未找到试卷
if(!isExist){
System.out.println("The test paper number does not exist");
//System.exit(0);
continue;
}
//判分
//先判断学号是否存在
boolean isStudentNumber = false;
for(int temp = 0; S[temp] != null; temp++){
if(A[i].getStudentNumber().equals(S[temp].getStudentNumber())){
System.out.print(S[temp].getStudentNumber() + " " + S[temp].getName() + ": ");
if(E[p] != null) {
for (int t = 0; E[p].getQuestionNumberList()[t] != 0; t++) {
if (J[i].getIsTrueList()[t]) {
System.out.print(E[p].getQuestionScoreList()[t]);
} else {
System.out.print("0");
}
if (E[p].getQuestionNumberList()[t + 1] == 0) {
System.out.println("~" + J[i].getTotalScore());
} else {
System.out.print(" ");
}
}
}
isStudentNumber = true;
break;
}
else{
isStudentNumber = false;
}
}
if(!isStudentNumber){
System.out.println(A[i].getStudentNumber() + " not found");
}
}
}
}
class Question{
private int QuestionNumber;
private String content;
private String standardAnswer;
public Question() {
}
public Question(int questionNumber, String content, String standardAnswer) {
QuestionNumber = questionNumber;
this.content = content;
this.standardAnswer = standardAnswer;
}
public int getQuestionNumber() {
return QuestionNumber;
}
public void setQuestionNumber(int questionNumber) {
QuestionNumber = questionNumber;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStandardAnswer() {
return standardAnswer;
}
public void setStandardAnswer(String standardAnswer) {
this.standardAnswer = standardAnswer;
}
}
class ExaminationPaper{
//下标即为题号
private Question[] questions = new Question[100];
//试卷编号
private int testNumber;
//试卷顺序对应的题号,下标从0开始
private int[] questionNumberList = new int[100];
//试卷顺序对应的题目的分值,下标从0开始
private int[] questionScoreList = new int[100];
private int totalPoint;
public ExaminationPaper() {
}
public Question[] getQuestions() {
return questions;
}
public void setQuestions(Question[] questions) {
for(int i = 0; i < questions.length; i++){
this.questions[i] = questions[i];
}
}
public int getTestNumber() {
return testNumber;
}
public void setTestNumber(int testNumber) {
this.testNumber = testNumber;
}
public int[] getQuestionNumberList() {
return questionNumberList;
}
public void setQuestionNumberList(int[] questionNumberList) {
for(int i = 0; i < questionNumberList.length; i++){
this.questionNumberList[i] = questionNumberList[i];
}
}
public int[] getQuestionScoreList() {
return questionScoreList;
}
public void setQuestionScoreList(int[] questionScoreList) {
for(int i = 0; i < questionScoreList.length; i++){
this.questionScoreList[i] = questionScoreList[i];
totalPoint += questionScoreList[i];
}
}
public int getTotalPoint(){
return totalPoint;
}
public void deleteQuestion(int number){
this.questions[number] = null;
}
}
class Student{
private String name;
private String studentNumber;
public Student() {
}
public Student(String name, String studentNumber) {
this.name = name;
this.studentNumber = studentNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
class AnswerSheet extends Student{
//答卷答案,与试卷题目顺序相同,下标从0开始
private String[] answerList = new String[100];
//试卷编号
private int answerSheetNumber;
//答卷每题的题号
private int[] answerNumberList = new int[100];
public AnswerSheet() {
}
public String[] getAnswerList() {
return answerList;
}
public void setAnswerList(String[] answerList) {
for(int i = 0; i < answerList.length; i++){
this.answerList[i] = answerList[i];
}
}
public int getAnswerSheetNumber() {
return answerSheetNumber;
}
public void setAnswerSheetNumber(int answerSheetNumber) {
this.answerSheetNumber = answerSheetNumber;
}
public int[] getAnswerNumberList() {
return answerNumberList;
}
public void setAnswerNumberList(int[] answerNumberList) {
for(int i = 0; i < answerNumberList.length; i++){
this.answerNumberList[i] = answerNumberList[i];
}
}
}
class Judgment{
private ExaminationPaper tests;
private AnswerSheet answerSheets;
private int totalScore;
//下标从0开始
private boolean[] isTrueList = new boolean[100];
public Judgment() {
}
public Judgment(ExaminationPaper tests, AnswerSheet answerSheets) {
this.tests = tests;
this.answerSheets = answerSheets;
}
public ExaminationPaper getTests() {
return tests;
}
public void setTests(ExaminationPaper tests) {
this.tests = tests;
}
public AnswerSheet getAnswerSheets() {
return answerSheets;
}
public void setAnswerSheets(AnswerSheet answerSheets) {
this.answerSheets = answerSheets;
}
public boolean[] getIsTrueList() {
return isTrueList;
}
public void setIsTrueList() {
for(int i = 0; tests.getQuestionNumberList()[i] != 0; i++){
//答案题号顺序
int j = 0;
while(answerSheets.getAnswerNumberList()[j] != 0){
if (answerSheets.getAnswerNumberList()[j] == i + 1) {
//int k = answerSheets.getAnswerNumberList()[i];
int number = tests.getQuestionNumberList()[i];
if (tests.getQuestions()[number] != null) {
if (answerSheets.getAnswerList()[i].equals(tests.getQuestions()[number].getStandardAnswer())) {
isTrueList[i] = true;
totalScore += tests.getQuestionScoreList()[i];
}
}
}
j++;
}
}
}
public int getTotalScore(){
return totalScore;
}
}
踩坑心得:
1、 跳出舒适圈,勇于学习新知识
这几个作业做下来,相信所有同学都能感受到正则表达式强大的功能,但是相信大家一开始接触正则表达式时都不免被它那奇怪的语法和符号给吓唬到了,认为它很难学,于是还是用自己老一套的循环去分析字符串。事实上,只要克服了一开始调到舒适圈外的恐惧,踏实的学习半个小时正则表达式,就已经可以上手去解决问题了,只是那种陌生感让人感觉不太自信,但只要多尝试几次,就能很快掌握这门强大的工具,大大节省做题时间,是一劳永逸的好事,所以大家不要惧怕新知识,要勇于跳出舒适区,积极去学习新知识。
2、 多写注释
相信大家刚接触编程时或者未接触编程前就早早的了解到如果一段长长的代码没有注释是一件多么痛苦的事。但你亲生体会真正意识到注释的重要性又是什么时候呢?我是这三次作业中才真正意识到了注释的重要性。大家可以看到,我前两次作业的源码注释几乎可以说没有,可能就只有一个总体性的注释,例如什么时候输入,什么时候输入,但这些注释是远远不够的。最好的是能将每一个变量的作用,每一个方法的作用,每一个代码块的作用都要给出注释,因为当代码量大起来后,你可以通过注释去了解过去的自己写这些代码的目的,而不用去逐条逐条的看代码,逐条逐条看代码是非常恐怖了,尤其是在debug的时候,由于作业的周期长,代码的量很大,写到后面你真的会看不懂自己以前写的代码,这会浪费你非常多时间去重新理解你之前的代码,大大降低debug的效率
3、 重视设计
好设计不仅可以让程序的拓展性大大提高,对于目前的我来说,好设计更重要的作用是能把我理清求解问题的思路。并且类图中有各个类的属性和方法,这让你写程序时不必再翻上翻下,只需要看看类图就知道有什么能用,或者有什么类有缺陷。并且设计类的时候不必将心神投入在如何实现上,你将有更多的脑力用于思考如何让类的设计更符合面向对面的设计原则。
改进建议:
我目前的编码类还是太少了,可以将正则表达式匹配输入单独列为一个类。
目前编码拓展性不足,几乎没有使用继承与多态,这个在后续可以加入。
总结:
这次作业收获良多,第一次写这么多行代码的程序,锻炼我的程序设计能力,并且让我意识到了注释的重要性。并且本次作业有不少课外的知识,这还锻炼我的自主学习能力。
建议方面主要是做题的时间太短了,虽然每次作业都有一周的时间,但实际上我们大部分的时间都有课,课外的时间本身就少,全部投入在java中会可能还做不完,但我们其他课程也是有作业需要做,并且数学类课程不进行预习的话上课真的不会有多少收获,我想提的建议是要么将数学课的排课排的更松散一点,要么将java排课排的更松散一点,让我们有时间同时学好数学和java,数学需要时间,java也需要时间,以现在的这种上课速度我真的做不到即学好数学又学好java。