分数 80
作者 蔡轲
单位 南昌航空大学
设计实现答题程序,模拟一个小型的测试,以下粗体字显示的是在答题判题程序-2基础上增补或者修改的内容,要求输入题目信息、试卷信息、答题信息、学生信息、删除题目信息,根据输入题目信息中的标准答案判断答题的结果。
输入格式:
程序输入信息分五种,信息可能会打乱顺序混合输入。
1、题目信息
题目信息为独行输入,一行为一道题,多道题可分多行输入。
格式:"#N:"+题目编号+" "+"#Q:"+题目内容+" "#A:"+标准答案
格式约束:
1、题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
2、允许题目编号有缺失,例如:所有输入的题号为1、2、5,缺少其中的3号题。此种情况视为正常。
样例:#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
2、试卷信息
试卷信息为独行输入,一行为一张试卷,多张卷可分多行输入数据。
格式:"#T:"+试卷号+" "+题目编号+"-"+题目分值+" "+题目编号+"-"+题目分值+...
格式约束:
题目编号应与题目信息中的编号对应。
一行信息中可有多项题目编号与分值。
样例:#T:1 3-5 4-8 5-2
3、学生信息
学生信息只输入一行,一行中包括所有学生的信息,每个学生的信息包括学号和姓名,格式如下。
格式:"#X:"+学号+" "+姓名+"-"+学号+" "+姓名....+"-"+学号+" "+姓名
格式约束:
答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
样例:
#S:1 #A:5 #A:22
1是试卷号
5是1号试卷的顺序第1题的题目答案
4、答卷信息
答卷信息按行输入,每一行为一张答卷的答案,每组答案包含某个试卷信息中的题目的解题答案,答案的顺序号与试 卷信息中的题目顺序相对应。答卷中:
格式:"#S:"+试卷号+" "+学号+" "+"#A:"+试卷题目的顺序号+"-"+答案内容+...
格式约束:
答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
答案内容可以为空,即””。
答案内容中如果首尾有多余的空格,应去除后再进行判断。
样例:
#T:1 1-5 3-2 2-5 6-9 4-10 7-3
#S:1 20201103 #A:2-5 #A:6-4
1是试卷号
20201103是学号
2-5中的2是试卷中顺序号,5是试卷第2题的答案,即T中3-2的答案
6-4中的6是试卷中顺序号,4是试卷第6题的答案,即T中7-3的答案
注意:不要混淆顺序号与题号
5、删除题目信息
删除题目信息为独行输入,每一行为一条删除信息,多条删除信息可分多行输入。该信息用于删除一道题目信息,题目被删除之后,引用该题目的试卷依然有效,但被删除的题目将以0分计,同时在输出答案时,题目内容与答案改为一条失效提示,例如:”the question 2 invalid~0”
格式:"#D:N-"+题目号
格式约束:
题目号与第一项”题目信息”中的题号相对应,不是试卷中的题目顺序号。
本题暂不考虑删除的题号不存在的情况。
样例:
#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack
#S:1 20201103 #A:1-5 #A:2-4
#D:N-2
end
输出
alert: full score of test paper1 is not 100 points
1+1=~5~false
the question 2 invalid~0
20201103 Tom: 0 0~0
答题信息以一行"end"标记结束,"end"之后的信息忽略。
输出格式:
1、试卷总分警示
该部分仅当一张试卷的总分分值不等于100分时作提示之用,试卷依然属于正常试卷,可用于后面的答题。如果总分等于100 分,该部分忽略,不输出。
格式:"alert: full score of test paper"+试卷号+" is not 100 points"
样例:alert: full score of test paper2 is not 100 points
2、答卷信息
一行为一道题的答题信息,根据试卷的题目的数量输出多行数据。
格式:题目内容+""+答案++""+判题结果(true/false)
约束:如果输入的答案信息少于试卷的题目数量,答案的题目要输"answer is null"
样例:
3+2=~5~true
4+6=~22~false.
answer is null
3、判分信息
判分信息为一行数据,是一条答题记录所对应试卷的每道小题的计分以及总分,计分输出的先后顺序与题目题号相对应。
格式:学号+" "+姓名+": "+题目得分+" "+....+题目得分+"~"+总分
格式约束:
1、没有输入答案的题目、被删除的题目、答案错误的题目计0分
2、判题信息的顺序与输入答题信息中的顺序相同
样例:20201103 Tom: 0 0~0
根据输入的答卷的数量以上2、3项答卷信息与判分信息将重复输出。
4、被删除的题目提示信息
当某题目被试卷引用,同时被删除时,答案中输出提示信息。样例见第5种输入信息“删除题目信息”。
5、题目引用错误提示信息
试卷错误地引用了一道不存在题号的试题,在输出学生答案时,提示”non-existent question~”加答案。例如:
输入:
#N:1 #Q:1+1= #A:2
#T:1 3-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-4
end
输出:
alert: full score of test paper1 is not 100 points
non-existent question~0
20201103 Tom: 0~0
如果答案输出时,一道题目同时出现答案不存在、引用错误题号、题目被删除,只提示一种信息,答案不存在的优先级最高,例如:
输入:
#N:1 #Q:1+1= #A:2
#T:1 3-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103
end
输出:
alert: full score of test paper1 is not 100 points
answer is null
20201103 Tom: 0~0
6、格式错误提示信息
输入信息只要不符合格式要求,均输出”wrong format:”+信息内容。
例如:wrong format:2 #Q:2+2= #4
7、试卷号引用错误提示输出
如果答卷信息中试卷的编号找不到,则输出”the test paper number does not exist”,答卷中的答案不用输出,参见样例8。
8、学号引用错误提示信息
如果答卷中的学号信息不在学生列表中,答案照常输出,判分时提示错误。参见样例9。
本题暂不考虑出现多张答卷的信息的情况。
输入样例1:
简单输入,不含删除题目信息。例如:
#N:1 #Q:1+1= #A:2
#T:1 1-5
#X:20201103 Tom
#S:1 20201103 #A:1-5
end
输出样例1:
在这里给出相应的输出。例如:
alert: full score of test paper1 is not 100 points
1+1=~5~false
20201103 Tom: 0~0
输入样例2:
简单输入,答卷中含多余题目信息(忽略不计)。例如:
#N:1 #Q:1+1= #A:2
#T:1 1-5
#X:20201103 Tom
#S:1 20201103 #A:1-2 #A:2-3
end
输出样例3
简单测试,含删除题目信息。例如:
alert: full score of test paper1 is not 100 points
1+1=~2~true
20201103 Tom: 5~5
输入样例3:
简单测试,含删除题目信息。例如:
#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-5 #A:2-4
#D:N-2
end
输出样例3:
在这里给出相应的输出,第二题由于被删除,输出题目失效提示。例如:
alert: full score of test paper1 is not 100 points
1+1=~5~false
the question 2 invalid~0
20201103 Tom: 0 0~0
输入样例4:
简单测试,含试卷无效题目的引用信息以及删除题目信息(由于题目本身无效,忽略)。例如:
#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 3-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-5 #A:2-4
#D:N-2
end
输出样例4:
输出不存在的题目提示信息。例如:
alert: full score of test paper1 is not 100 points
1+1=~5~false
non-existent question~0
20201103 Tom: 0 0~0
输入样例5:
综合测试,含错误格式输入、有效删除以及无效题目引用信息。例如:
#N:1 +1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-5 #A:2-4
#D:N-2
end
输出样例5:
在这里给出相应的输出。例如:
wrong format:#N:1 +1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
the question 2 invalid~0
20201103 Tom: 0 0~0
输入样例6:
综合测试,含错误格式输入、有效删除、无效题目引用信息以及答案没有输入的情况。例如:
#N:1 +1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-5
#D:N-2
end
输出样例6:
答案没有输入的优先级最高。例如:
wrong format:#N:1 +1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
answer is null
20201103 Tom: 0 0~0
输入样例7:
综合测试,正常输入,含删除信息。例如:
#N:2 #Q:2+2= #A:4
#N:1 #Q:1+1= #A:2
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:2-4 #A:1-5
#D:N-2
end
输出样例7:
例如:
alert: full score of test paper1 is not 100 points
1+1=~5~false
the question 2 invalid~0
20201103 Tom: 0 0~0
输入样例8:
综合测试,无效的试卷引用。例如:
#N:1 #Q:1+1= #A:2
#T:1 1-5
#X:20201103 Tom
#S:2 20201103 #A:1-5 #A:2-4
end
输出样例8:
例如:
alert: full score of test paper1 is not 100 points
The test paper number does not exist
输入样例9:
无效的学号引用。例如:
#N:1 #Q:1+1= #A:2
#T:1 1-5
#X:20201106 Tom
#S:1 20201103 #A:1-5 #A:2-4
end
输出样例9:
答案照常输出,判分时提示错误。例如:
alert: full score of test paper1 is not 100 points
1+1=~5~false
20201103 not found
输入样例10:
信息可打乱顺序输入:序号不是按大小排列,各类信息交错输入。但本题不考虑引用的题目在被引用的信息之后出现的情况(如试卷引用的所有题目应该在试卷信息之前输入),所有引用的数据应该在被引用的信息之前给出。例如:
#N:3 #Q:中国第一颗原子弹的爆炸时间 #A:1964.10.16
#N:1 #Q:1+1= #A:2
#X:20201103 Tom-20201104 Jack-20201105 Www
#T:1 1-5 3-8
#N:2 #Q:2+2= #A:4
#S:1 20201103 #A:1-5 #A:2-4
end
输出样例10:
答案按试卷中的题目顺序输出。例如:
alert: full score of test paper1 is not 100 points
1+1=~5~false
中国第一颗原子弹的爆炸时间~4~false
20201103 Tom: 0 0~0
我的代码:
import java.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class TextQuestion //N
{
public int id;
public String quess;
public String an;
TextQuestion(){}
public void setId(int id)
{
this.id=id;
}
public void setQuess(String quess)
{
this.quess=quess;
}
public void setAn(String an)
{
this.an=an;
}
public TextQuestion(int id, String quess, String an)
{
this.id=id;
this.quess=quess;
this.an=an;
}
}
class Scsbic
{
public int cid;
public int csore;
public Scsbic(int cid,int csore)
{
this.cid=cid;
this.csore=csore;
}
}
class Textmation //T
{
public int textid;
ArrayList<Scsbic> questionScores;
public Textmation(int textid,ArrayList<Scsbic> questionScores)
{
this.textid=textid;
this.questionScores=questionScores;
}
}
class Jisudi
{
public int shijuanti;
public String shijuandean;
public Jisudi(int shijuanti,String shijuandean)
{
this.shijuanti=shijuanti;
this.shijuandean=shijuandean;
}
}
class Textshijuan //S
{
public int shijuanid;
public String xuehao;
ArrayList<Jisudi> shijuanan;
public Textshijuan(int shijuanid,String xuehao,ArrayList<Jisudi> shijuanan)
{
this.shijuanid=shijuanid;
this.xuehao=xuehao;
this.shijuanan=shijuanan;
}
}
class Student //X
{
public String xuehao;
public String xuename;
public Student(String xuehao,String xuename)
{
this.xuehao=xuehao;
this.xuename=xuename;
}
public Student(){
}
}
class Delet //D
{
public int themu;
public Delet(int themu)
{
this.themu=themu;
}
}
public class Main{
public static void main(String[]args){
Scanner in= new Scanner(System.in);
String s,quess="",an="",shijuandean="",xuehao="",lkjop="",xuename="";
int i,id=0,mi=0,ni=0,bi=0,vi=0,shijuanti=0,mmm=0,pv=0,lll=0;
int shijuanid=0;
int textid=0;
ArrayList <Textmation>arrayList2=new ArrayList<>();
ArrayList <TextQuestion>arrayList3=new ArrayList<>();
ArrayList <Textshijuan>arrayList1=new ArrayList<>();
ArrayList <Student>arrayList4=new ArrayList<>();
ArrayList <Delet>arrayList5=new ArrayList<>();
while(true)
{
s = in.nextLine();
if("end".equals(s)) {
break;
}
if(s.startsWith("#N:"))
{
Pattern pattern = Pattern.compile("#N:(\\d*)\\s*#Q:\\s*(.*)\\s#A:(.*)");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
id=Integer.valueOf(matcher.group(1));
quess=String.valueOf(matcher.group(2));
an=String.valueOf(matcher.group(3));
}
else
System.out.printf("wrong format:%s\n",s);
arrayList3.add(new TextQuestion(id,quess,an));
mi++;
}
else if(s.startsWith("#T:"))
{
String[] parts = s.split(" ");
Pattern pattern = Pattern.compile("#T:\\s*(\\d*)\\s*");
Matcher matcher = pattern.matcher(parts[0]);
ArrayList<Scsbic> questionScores=new ArrayList<>();
if(matcher.find()){
textid = Integer.valueOf(matcher.group(1));
vi++;
}
int total=0;
for (i=1;i<parts.length;i++)
{
String[] sub=parts[i].split("-");
int cid = Integer.parseInt(sub[0]);
int csore = Integer.parseInt(sub[1]);
questionScores.add(new Scsbic(cid,csore));
total=total+csore;
ni++;
}
if(total!=100)
{
System.out.println("alert: full score of test paper" + textid + " is not 100 points");
}
arrayList2.add(new Textmation(textid,questionScores));
}
else if(s.startsWith("#S:"))
{
String[] parts = s.split(" ");
ArrayList<Jisudi> answers = new ArrayList<>();
Pattern pattern = Pattern.compile("#S:\\s*(\\d+)");
Matcher matcher = pattern.matcher(parts[0]);
if(matcher.find()){
shijuanid = Integer.valueOf(matcher.group(1));
}
pattern = Pattern.compile("(.*)");
matcher = pattern.matcher(parts[1]);
if(matcher.find()){
xuehao = String.valueOf(matcher.group(1));
}
for (i=2;i<parts.length;i++)
{
pattern = Pattern.compile("#A:\\s*(\\d+)-(.*)");
matcher = pattern.matcher(parts[i]);
if(matcher.find()){
shijuanti = Integer.valueOf(matcher.group(1));
shijuandean = String.valueOf(matcher.group(2));
}
answers.add(new Jisudi(shijuanti,shijuandean));
}
arrayList1.add(new Textshijuan(shijuanid,xuehao,answers));
bi++;
}
else if(s.startsWith("#X:")) {
String xuehao1 = "",xuename1 = "";
lkjop=s.substring(3);
String[] parts = lkjop.split("-");
for(i=0;i<parts.length;i++)
{
Pattern pattern = Pattern.compile("(.*)\\s(.*)");
Matcher matcher = pattern.matcher(parts[i]);
if(matcher.find()){
xuehao1 = String.valueOf(matcher.group(1));
xuename1 = String.valueOf(matcher.group(2));
}
arrayList4.add(new Student(xuehao1,xuename1));
}
}
else if (s.startsWith("#D")) {
int themu=0;
Pattern pattern = Pattern.compile("#D:N\\s*-(\\d+)\\s*");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
themu = Integer.valueOf(matcher.group(1));
}
arrayList5.add(new Delet(themu));
mmm++;
}
else
{
System.out.println("wrong format:"+s);
}
}
int op,ip,up,py,w=0,k,xp,flag5=0,ooo=0,ppp=0,www=0,jjj=0,uuu=0;
Delet delet1=null;
if(mmm!=0)
delet1=arrayList5.get(0);
for(w=0;w<arrayList1.size();w++)
{
www=0;
lll=0;
Textshijuan textshijuan=arrayList1.get(w);
if(textshijuan == null)continue;////
int oop=0;
String a="";
int sum=0;
Student student = new Student();
for(i=0;i<arrayList4.size();i++)
{
if(arrayList4.get(i) == null)continue;////
if(arrayList4.get(i).xuehao.equals(textshijuan.xuehao))
{
//System.out.println(textshijuan.xuehao);
student = arrayList4.get(i);
www=1;
break;
}
}//找学号
if(student == null)continue;////
Textmation textmation=null;
for(i=0;i<arrayList2.size();i++)
{
if(arrayList2.get(i) == null)continue;////
if(arrayList2.get(i).textid==textshijuan.shijuanid)
{
textmation = arrayList2.get(i);
break;
}
}//找试卷编号
if(textmation!=null)
{
k=0;
jjj=0;
uuu=0;
if(textmation.questionScores == null)continue;////
for(up=0;up<textmation.questionScores.size();up++)
{
lll++;
Scsbic scsbic = textmation.questionScores.get(up);
op=scsbic.cid;
py=scsbic.csore;
TextQuestion textQuestion=null;
for(ip=0;ip<arrayList3.size();ip++)
{
flag5=0;
if(arrayList3.get(ip) == null)continue;////
if(arrayList3.get(ip).id==op)
{
textQuestion = arrayList3.get(ip);
flag5=1;
break;
}
}
Jisudi jisudi=null;
for(ppp=0;ppp<textshijuan.shijuanan.size();ppp++)
{
ooo=0;
jisudi=textshijuan.shijuanan.get(ppp);
if(jisudi.shijuanti==lll)
{
//System.out.println(jisudi.shijuanti+""+op);
ooo=1;
break;
}
}//找答卷
if(flag5!=1)
{
System.out.printf("non-existent question~0\n");
if(k<textmation.questionScores.size()&&k!=0)
{
a=a+" ";
}
a=a+"0";
k++;
continue;
}
if(lll>textshijuan.shijuanan.size())
{
System.out.printf("answer is null\n");
if(k<textmation.questionScores.size()&&k!=0)
{
a=a+" ";
}
a=a+"0";
k++;
continue;
}
//jisudi=textshijuan.shijuanan.get(lll-1);
if(k<textmation.questionScores.size()&&k!=0)
{
a=a+" ";
}
if(textQuestion == null)
{
continue;
}
if(mmm!=0)
{
if(op==delet1.themu)
{
System.out.printf("the question %d invalid~0\n",delet1.themu);
a=a+"0";
continue;
}
}
if(oop>=textshijuan.shijuanan.size())
{
System.out.println("answer is null");
a=a+" 0";
continue;
}
// jisudi=textshijuan.shijuanan.get(k);
if(textQuestion.an.equals(jisudi.shijuandean))
{
System.out.println(textQuestion.quess+"~"+jisudi.shijuandean+"~true");
a=a+py;
sum=sum+py;
k++;
}
else
{
System.out.println(textQuestion.quess+"~"+jisudi.shijuandean+"~false");
a=a+"0";
k++;
}
oop++;
}
if(www==1)
System.out.printf("%s %s: ",student.xuehao,student.xuename);
else{
System.out.printf("%s not found",textshijuan.xuehao);
break;
}
System.out.printf("%s~%d",a,sum);
if(w!=arrayList1.size()-1)
{
System.out.printf("\n");
}
}
else
{
System.out.printf("The test paper number does not exist");
continue;
}
}
}
}


总结:
这次作业相较于上一次的作业来说,新增了学生类以及删除类,在第二题的基础上,对试卷题目输入时,对题目进行判断看是有合法,以及在输出时候进行判断,总的来说,这道题目非常的复杂,在写的时候总是想放弃,但每次有一次来了新的思路,对于题目的测试点,只知道题目所给出的十个输出样例,对于其他的测试点都要自己慢慢摸索
3.采坑心得:
题目所给的测试样例只是最终得到结果的一部分,还有一些测试点还需要自己慢慢去摸索,在输出的时候以字符串拼接作为输出,结果少了对题目数量的判断,导致最后输出的时候,空格有多有少,导致最后结果有问题,在第一次写的时候并未发现这一有误,然后在第二次中也这么实用,导致一错再错,最后在题目三中发现了自己的错误并加以改正,才正确。以及在写代码的时候,使用了大量的ArrayList,在输出的时候,总是会出现非零返回,没有及时的对ArrayList进行判断,以后在输出的时候要及时判断,减少这样的错误
4.改进建议:
在使用ArrayList的时候不注意判断的话,很容易出现非零返回,使用Hashmap的话,可能会好一点,以及在使用字符串拼接输出的时候,要注意空格,最好是使用一个参数来记录题目的多少,方便输出。
5.总结:
题目所给的测试样例只是最终得到结果的一部分,还有一些测试点还需要自己慢慢去摸索,要对题目了解清楚之后,在进行分析,才能知道如何去输出,以什么样的关系去输出,每一个类对应关系以及各自所代表的含义,题目确实很复杂,唯有沉下心去分析类的关系,才能把题目做好,比如在输出的时候以字符串拼接作为输出,结果少了对题目数量的判断,导致最后输出的时候,空格有多有少,导致最后结果有问题,在第一次写的时候并未发现这一有误,然后在第二次中也这么实用,导致一错再错,最后在题目三中发现了自己的错误并加以改正,才正确。以及在写代码的时候,使用了大量的ArrayList,在输出的时候,总是会出现非零返回,没有及时的对ArrayList进行判断,以后在输出的时候要及时判断,减少这样的错误,正则表达式则是一种强大的文本处理工具,我们可以通过他来获取重要的信息,并储存,最后用来输出。类与对象提供了更加结构化和模块化的编程方式,而正则表达式则提供了一种高效且灵活的文本处理方式。在平时写题目的时候要多用这些所需掌握的知识,不能原地踏步,应自觉获取新的知识,多听一听南昌航空大学的学堂在线网课。