题目集1~3的总结性Blog

一、前言

  1. 第一次题集知识点主要是掌握类和对象的使用创建,以及类与类之间的关系,还有正则表达式的运用,动态数组ArrayList类的使用,以及方法。这一次的题集难度不是很大,因为是第一次所以来说题量有点大。也是艰难完成了。

  2. 第二次题集知识点与第一次的类似主要还是对正则表达式的使用,以及对题目意思的理解,难度有点大,题量也相对大。在第一次的基础下第二次做起来更加得心应手了。

  3. 第三次题集知识点同样与之前两次相似,在新加的内容的加持下题目量更加大,题目难度也进一步加大。是一个很大的挑战。
    二、 设计与分析

  4. 答题判断程序-1

    从类图可以看出有四个类,Paper,Answer,Problem,Main,在Answer类中包含了Paper类属性,Paper类中有Problem类动态数组属性。每个类中属性都给出了get,set方法,以及给出了每个类的构造方法,和一些需要用到的方法。源代码如下:
    import java.util.Scanner;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.util.ArrayList;
    class Problem{
    private int pnum;
    private String content;
    private String standardAnswer;
    int getPnum()
    {
    return pnum;
    }
    void setPnum(int pnum)
    {
    this.pnum=pnum;
    }
    String getContent()
    {
    return content;
    }
    void setContent(String content)
    {
    this.content=content;
    }
    String getStandardAnswer()
    {
    return standardAnswer;
    }
    void setStandardAnswer(String standardAnswer)
    {
    this.standardAnswer=standardAnswer;
    }
    public Problem(int pnum,String content,String standardAnswer)
    {
    setPnum( pnum);
    setContent(content);
    setStandardAnswer(standardAnswer);
    }
    public boolean pjudge(String answer)
    {
    if(answer.equals(standardAnswer))
    return true;
    else
    return false;
    }
    }
    class Paper{
    private int nump;
    private ArrayList problemlist=new ArrayList();
    public int getNump()
    {
    return nump;
    }
    public void setNump(int nump)
    {
    this.nump=nump;
    }
    public void setproblemlist(int num,Problem problem)
    {
    problemlist.add(num-1,problem);
    }
    public boolean ejudge(int num,String answer)
    {
    if(problemlist.get(num-1).getStandardAnswer().equals(answer))
    return true;
    else
    return false;
    }
    public void addproblem(int num,Problem question)
    {
    problemlist.add(num-1,question);
    }
    public Problem getproblem(int num)
    {
    return problemlist.get(num);
    }
    }
    class Answer{
    private Paper paper;
    private ArrayList answerlist=new ArrayList();
    private ArrayList judgement=new ArrayList();
    Paper getPaper()
    {
    return paper;
    }
    void setPaper(Paper paper)
    {
    this.paper=paper;
    }
    public void ajudge(int num)
    {
    if(getPaper().ejudge(num,answerlist.get(num-1))true)
    judgement.add(num-1,true);
    else
    judgement.add(num-1,false);
    }
    public void addanswer(int num,String answer)
    {
    answerlist.add(num-1,answer);
    }
    public void printf(int num)
    {
    for(int i=0;i<num;i++)
    {
    System.out.println(paper.getproblem(i).getContent()+"~"+answerlist.get(i));
    }
    for(int i=0;i<num;i++)
    {
    if(i<num-1)
    {
    if(judgement.get(i)
    true)
    System.out.printf("true ");
    else
    System.out.printf("false ");
    }
    else
    {
    if(judgement.get(i)true)
    System.out.printf("true");
    else
    System.out.printf("false");
    }
    }
    }
    }
    public class Main{
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    Paper p=new Paper();
    Answer answer=new Answer();
    int nump=sc.nextInt();
    p.setNump(nump);
    answer.setPaper(p);
    int questionNumber[]=new int[nump];
    String questionContent[]=new String[nump];
    String standardAnswer[]=new String[nump];
    String printfproblem[]=new String[nump];
    String printfanswer[]=new String[nump];
    String scanneranswer;
    String scannerproblem;
    String regex = "#N:(\d+)\s+#Q:(.+?)\s+#A:(.+)";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher[]=new Matcher[nump];
    String n=sc.nextLine();
    for(int i=0;i<nump;i++)
    {
    scannerproblem=sc.nextLine();
    if(scannerproblem.charAt(3)
    ' ')
    {
    StringBuilder sb=new StringBuilder(scannerproblem);
    sb.deleteCharAt(3);
    printfproblem[i]=sb.toString();
    }
    else
    printfproblem[i]=scannerproblem;
    matcher[i]= pattern.matcher(printfproblem[i]);
    if(matcher[i].find()){
    int x=Integer.parseInt(matcher[i].group(1).trim());
    questionNumber[x-1]=x;
    questionContent[x-1]= matcher[i].group(2).trim();
    standardAnswer[x-1]= matcher[i].group(3).trim();
    }
    }
    for(int i=0;i<nump;i++)
    {
    p.addproblem(i+1,new Problem(questionNumber[i],questionContent[i],standardAnswer[i]));
    }
    Pattern pattern1 = Pattern.compile("#A:(.*?)(\s+|$)");
    scanneranswer=sc.nextLine();
    String str[]=scanneranswer.split(" ");
    Matcher m[]=new Matcher[nump];
    for(int j=0;j<nump;j++)
    {
    m[j]= pattern1.matcher(str[j]);
    if(m[j].find())
    {
    printfanswer[j]=m[j].group(1).trim();

        }
        answer.addanswer(j+1,printfanswer[j]);
    }
    for(int i=0;i<nump;i++)
    {
        answer.ajudge(i+1);
    }
    answer.printf(nump);
    

    }
    }
    在Main类中创建的Paper,Answer对象以及引用,输入按照字符串数组的形式输入,做到对每行进行判断。完成此题是按照题目所给的测试点一个一个来做的,因此每次完成一个后去完成下一个需要花费很多时间去更改,没有对题目进行整体考虑。代码的复用性也很差,踩了不少的坑,即使是做完取得高分但是还是浪费了许多时间。
    2.答题判断程序-2

    从类图不难看出在第一次的基础上在Main类中加入了一个findindex的方法,用来做到分数对应题号,最后的输出顺序按照分数题号顺序输出。这里我并没有去新添加一个试卷题目类来表示题目和试卷间的关系,而是直接在原来的试卷类中加入总分这一属性,题目类中加入分数这一属性,然后添加相应的get,set方法。这样做应该会将题目复杂化了,做起来也没有那么方便。源代码如下:
    import java.util.Scanner;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.util.ArrayList;
    class Problem{
    private int pnum;
    private String content;
    private String standardAnswer;
    private int grade;
    public int getGrade()
    {
    return grade;
    }
    public void setGrade(int grade)
    {
    this.grade=grade;
    }
    int getPnum()
    {
    return pnum;
    }
    void setPnum(int pnum)
    {
    this.pnum=pnum;
    }
    String getContent()
    {
    return content;
    }
    void setContent(String content)
    {
    this.content=content;
    }
    String getStandardAnswer()
    {
    return standardAnswer;
    }
    void setStandardAnswer(String standardAnswer)
    {
    this.standardAnswer=standardAnswer;
    }
    public Problem(int pnum,String content,String standardAnswer,int grade)
    {
    setPnum( pnum);
    setContent(content);
    setStandardAnswer(standardAnswer);
    setGrade(grade);
    }
    public boolean pjudge(String answer)
    {
    if(answer.equals(standardAnswer))
    return true;
    else
    return false;
    }
    }
    class Paper{
    private int nump;
    private ArrayList problemlist=new ArrayList();
    private int allgrade;
    public int getNump()
    {
    return nump;
    }
    public void setNump(int nump)
    {
    this.nump=nump;
    }
    public int getallGrade()
    {
    return allgrade;
    }
    public void setallGrade(int allgrade)
    {
    this.allgrade=allgrade;
    }
    public void setproblemlist(int num,Problem problem)
    {
    problemlist.add(num-1,problem);
    }
    public boolean ejudge(int num,String answer)
    {
    if(problemlist.get(num-1).getStandardAnswer().equals(answer))
    return true;
    else
    return false;
    }
    public void addproblem(int num,Problem question)
    {
    problemlist.add(num-1,question);
    }
    public Problem getproblem(int num)
    {
    return problemlist.get(num);
    }
    }
    class Answer{
    private Paper paper;
    private ArrayList answerlist=new ArrayList();
    private ArrayList judgement=new ArrayList();
    private int answernum;
    public int getAnswernum()
    {
    return answernum;
    }
    public void setAnswernum(int answernum)
    {
    this.answernum=answernum;
    }
    Paper getPaper()
    {
    return paper;
    }
    void setPaper(Paper paper)
    {
    this.paper=paper;
    }
    public void ajudge(int num)
    {
    if(num<=answerlist.size())
    {
    if(getPaper().ejudge(num,answerlist.get(num-1))true)
    judgement.add(num-1,true);
    else
    judgement.add(num-1,false);
    }
    else
    judgement.add(num-1,false);
    }
    public void addanswer(int num,String answer)
    {
    answerlist.add(num-1,answer);
    }
    public void printf(int num)
    {
    for(int i=0;i<num;i++)
    {
    if(i<answerlist.size())
    {
    if(answerlist.get(i).equals(""))
    System.out.println("answer is null");
    else
    System.out.println(paper.getproblem(i).getContent()+""+answerlist.get(i)+""+judgement.get(i).toString());
    }
    else
    System.out.println("answer is null");
    }
    int a[]=new int[num];
    int sum=0;
    for(int j=0;j<num;j++)
    {
    if(judgement.get(j)
    true)
    a[j]=paper.getproblem(j).getGrade();
    else
    a[j]=0;
    sum=sum+a[j];
    }
    for(int i=0;i<num;i++)
    {
    if(i<num-1)
    System.out.printf("%d ",a[i]);
    else
    System.out.printf("%d",a[i]);
    }
    System.out.println("~"+sum);
    }
    }
    public class Main{
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String sf[]=new String[100];
    String t[]=new String[10];
    int j=0;
    int nump=0;
    ArrayList answerpaper=new ArrayList();
    String regex1="#S:(\d+)\s+#A:(.*?)(\s+|$)";
    Pattern pattern3 = Pattern.compile("#T:(\d+)\s+\d+-\d+");
    Pattern pattern4 = Pattern.compile("(\d+)-(\d+)");
    Pattern pattern2 = Pattern.compile(regex1);
    int k=0;
    int psum=0;
    for(j=0;;j++)
    {
    sf[j]=sc.nextLine();
    Matcher matcher1= pattern3.matcher(sf[j]);
    if(matcher1.find())
    {
    psum++;
    }
    if(sf[j].equals("end"))
    break;
    }
    Paper p[]=new Paper[psum];
    for(int i=0;i<psum;i++)
    p[i]=new Paper();
    for(int i=0;;i++) {
    Matcher matcher2= pattern3.matcher(sf[i]);

       if(matcher2.find()){
           String str[]=sf[i].split(" ");
           int q=Integer.parseInt(matcher2.group(1).trim());
           nump=str.length-1;
           p[q-1].setNump(nump);
       }
       if(sf[i].equals("end"))
           break;
    

    }

    for(j=0;;j++)
    {
        Matcher matcher1= pattern2.matcher(sf[j]);
        if(matcher1.find())
        {
            answerpaper.add(k++,sf[j]);
        }
        if(sf[j].equals("end"))
            break;
    }
    int size=answerpaper.size();  
    Answer answer[]=new Answer[size];
    for(int i=0;i<size;i++)
        answer[i]=new Answer();
    Pattern pattern1 = Pattern.compile("#A:(.*?)(\\s+|$)");
    Pattern pa=Pattern.compile("#S:(.+)");
    int b=0;
    int c;
    for(int i=0;i<size;i++)
    {
             String s[]=answerpaper.get(i).split(" ");
             for(int l=0;l<s.length;l++)
                 t[l]=s[l];
            c=s.length-1;
            String printfanswer[]=new String[c];
            Matcher o=pa.matcher(t[0]); 
            if(o.find())
            {
                b=Integer.parseInt(o.group(1).trim()); 
            }
            answer[i].setAnswernum(b);
          if(answer[i].getAnswernum()<=p.length)
                answer[i].setPaper(p[b-1]);
            else
                answer[i].setPaper(p[p.length-1]);
            for(j=1;j<c+1;j++)
            {
                Matcher m= pattern1.matcher(t[j]);
                if(m.find())
                {
                    printfanswer[j-1]=m.group(1).trim();
                }
                answer[i].addanswer(j,printfanswer[j-1]);
            }
    }
    int questionNumber[]=new int[10];
    String questionContent[]=new String[10];
    String standardAnswer[]=new String[10];
    int grade[]=new int[10];
    int num[]=new int[nump];
    int g=0;
    String regex = "#N:(\\d+)\\s+#Q:(.+)\\s+#A:(.+)";
    Pattern pattern = Pattern.compile(regex);
    for(int i=0;;i++)
    {
       
        Matcher matcher= pattern.matcher(sf[i]);
        if(matcher.find()){
                int x=Integer.parseInt(matcher.group(1).trim());
                questionNumber[g]=x;
                questionContent[g]= matcher.group(2).trim();
                standardAnswer[g]= matcher.group(3).trim();
                g++;
        }
        if(sf[i].equals("end"))
            break;
    }
    for(int i=0;;i++) {
         Matcher matcher2= pattern3.matcher(sf[i]);
         int sum=0;
        if(matcher2.find()){
            String str[]=sf[i].split(" ");
            int y=Integer.parseInt(matcher2.group(1).trim());
            for( j=1;j<str.length;j++)
            {
                Matcher matcher3= pattern4.matcher(str[j]);
                if(matcher3.find())
                {
                    num[j-1]=Integer.parseInt(matcher3.group(1).trim());
                     int index=findIndex(questionNumber,num[j-1]);
                    grade[index]=Integer.parseInt(matcher3.group(2).trim());
                      sum=sum+grade[index];
                }
            }
            p[y-1].setallGrade(sum);
            for(int u=0;u<str.length-1;u++)
            {
                int index=findIndex(questionNumber,num[u]);
                p[y-1].addproblem(u+1,new Problem(questionNumber[index],questionContent[index],standardAnswer[index],grade[index]));
            
            }
        }
        if(sf[i].equals("end"))
            break;
    }
    for(j=0;j<size;j++)
    {
        for(int i=0;i<answer[j].getPaper().getNump();i++)
        {
            answer[j].ajudge(i+1);
        }
    }
    for(int i=1;i<=psum;i++)
    {
        if(p[i-1].getallGrade()!=100)
            System.out.println("alert: full score of test paper"+i+" is not 100 points");
    }
    for(int i=0;i<size;i++)
    {
        if(answer[i].getAnswernum()>p.length)
             System.out.println("The test paper number does not exist");
        else
            answer[i].printf(answer[i].getPaper().getNump());
    }
    

    }
    public static int findIndex(int array[],int target){
    for (int i=0;i<array.length;i++){
    if (array[i]target){
    return i;
    }
    }
    return -1;
    }
    }
    从Main类中可以看出我用到了许多的for循环,以及大量的正则表达式进行识别操作,代码的重复使用性极差,看起来也很乱,不易理解。这道题目也是为了做题而去做题的,完全按照上一次的做法一个测试点一个测试点来做,每次进行许多的更改和调试,甚至会因为一个测试点花费大量的时间去调试,找出问题来。
    3.答题判断程序-3

    从类图不难看出在第二题的基础上加上了Student类,类中包含了学号,姓名,答案类属性,以及三个属性的set,get方法,以及该类的构造方法,还一个show方法用于输出学生的学号姓名。因为输出格式的问题在Answer类中的输出方法进行了更改。源代码如下:
    import java.util.Scanner;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.util.ArrayList;
    class Problem{
    private int pnum;
    private String content;
    private String standardAnswer;
    private int grade;
    public int getGrade()
    {
    return grade;
    }
    public void setGrade(int grade)
    {
    this.grade=grade;
    }
    int getPnum()
    {
    return pnum;
    }
    void setPnum(int pnum)
    {
    this.pnum=pnum;
    }
    String getContent()
    {
    return content;
    }
    void setContent(String content)
    {
    this.content=content;
    }
    String getStandardAnswer()
    {
    return standardAnswer;
    }
    void setStandardAnswer(String standardAnswer)
    {
    this.standardAnswer=standardAnswer;
    }
    public Problem(int pnum,String content,String standardAnswer,int grade)
    {
    setPnum( pnum);
    setContent(content);
    setStandardAnswer(standardAnswer);
    setGrade(grade);
    }
    public boolean pjudge(String answer)
    {
    if(answer.equals(standardAnswer))
    return true;
    else
    return false;
    }
    }
    class Paper{
    private int nump;
    private ArrayList problemlist=new ArrayList();
    private int allgrade;
    public int getNump()
    {
    return nump;
    }
    public void setNump(int nump)
    {
    this.nump=nump;
    }
    public int getallGrade()
    {
    return allgrade;
    }
    public void setallGrade(int allgrade)
    {
    this.allgrade=allgrade;
    }
    public ArrayList getproblemlist()
    {
    return problemlist;
    }
    public void setproblemlist(int num,Problem problem)
    {
    problemlist.add(num-1,problem);
    }
    public boolean ejudge(int num,String answer)
    {
    if(problemlist.get(num-1).getStandardAnswer().equals(answer))
    return true;
    else
    return false;
    }
    // public void addproblem(String question)
    // {
    // notproblem.add(question);
    // }
    public void addproblem(int num,Problem question)
    {
    problemlist.add(num-1,question);
    }
    public Problem getproblem(int num)
    {
    return problemlist.get(num);
    }
    }
    class Answer {
    private Paper paper;
    private ArrayList answerlist=new ArrayList();
    private ArrayList judgement=new ArrayList();
    private int answernum;
    public int getAnswernum()
    {
    return answernum;
    }
    public void setAnswernum(int answernum)
    {
    this.answernum=answernum;
    }
    Paper getPaper()
    {
    return paper;
    }
    void setPaper(Paper paper)
    {
    this.paper=paper;
    }
    public void ajudge(int num,int deleteproblem,int existproblem,int answernumber)
    {
    if(num<=answerlist.size())
    {
    if(existproblem
    1&&numanswernumber)
    judgement.add(num-1,false);
    if(deleteproblem!=0&&num
    deleteproblem)
    {
    judgement.add(num-1,false);
    }
    else if(getPaper().ejudge(num,answerlist.get(num-1))==true)
    judgement.add(num-1,true);

        else
            judgement.add(num-1,false);
    }
    else
         judgement.add(num-1,false);
    

    }
    public void addanswer(int num,String answer)
    {
    answerlist.add(num-1,answer);
    }
    public void printf1(int num,int deleteproblem,int existproblem,int answernumber)
    {
    for(int i=0;i<num;i++)
    {
    if(i<answerlist.size())
    {
    if(existproblem1&&ianswernumber)
    System.out.println("non-existent question~0");
    // else if(answerlist.get(i).equals("")||paper.getproblemlist().get(i).getContent().equals("1"))
    // System.out.println("answer is null");
    else if(deleteproblem!=0&&i==deleteproblem-1&&existproblem!=1)
    System.out.println("the question "+deleteproblem+" invalid~0");
    else{
    if(paper.getproblem(i).getContent().equals(""))
    System.out.println("non-existent question~0");
    else
    System.out.println(paper.getproblem(i).getContent()+""+answerlist.get(i)+""+judgement.get(i).toString());
    }

        }
        else if(existproblem==1&&i==answernumber)
                System.out.println("non-existent question~0");
        else
             System.out.println("answer is null");
    }
    

    }
    public void printf2(int num)
    {
    int a[]=new int[num];
    int sum=0;
    for(int j=0;j<num;j++)
    {
    if(judgement.get(j)true)
    a[j]=paper.getproblem(j).getGrade();
    else
    a[j]=0;
    sum=sum+a[j];
    }
    for(int i=0;i<num;i++)
    {
    if(i<num-1)
    System.out.printf("%d ",a[i]);
    else
    System.out.printf("%d",a[i]);
    }
    System.out.println("~"+sum);
    }
    }
    class Student{
    private String sid;
    private String name;
    private Answer answer;
    public String getSid() {
    return sid;
    }
    public void setSid(String sid) {
    this.sid = sid;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Answer getAnswer() {
    return answer;
    }
    public void setAnswer(Answer answer) {
    this.answer = answer;
    }
    public Student(String sid,String name)
    {
    setSid(sid);
    setName(name);
    }
    public void show(){
    System.out.printf(sid+" "+name+": ");
    }
    }
    public class Main{
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    String sf[]=new String[100];
    String t[]=new String[10];
    int j=0;
    int nump=0;
    ArrayList answerpaper=new ArrayList();
    ArrayList notproblem=new ArrayList();
    String regex1="#S:(\d+)\s(\d+)\s(#A:(.?)(\s+|$))";
    Pattern pattern3 = Pattern.compile("#T:(\d+)\s+\d+-\d+");
    Pattern pattern4 = Pattern.compile("(\d+)-(\d+)");
    Pattern pattern2 = Pattern.compile(regex1);
    Pattern pattern5 = Pattern.compile("#X:\d+\s[A-Za-z]+-\d");
    int k=0;
    int psum=0;
    for(j=0;;j++)
    {
    sf[j]=sc.nextLine();
    Matcher matcher1= pattern3.matcher(sf[j]);
    if(matcher1.find())
    {
    psum++;
    }
    if(sf[j].equals("end"))
    break;
    }
    Paper p[]=new Paper[psum];
    for(int i=0;i<psum;i++)
    p[i]=new Paper();
    int stulenth=0;
    for(int i=0;;i++)
    {
    Matcher ma=pattern5.matcher(sf[i]);
    if(ma.find())
    {
    String stulist[]=sf[i].split("-");
    stulenth=stulist.length;
    }
    if(sf[i].equals("end"))
    break;
    }
    Pattern pattern7=Pattern.compile("#D:N-(\d+)");
    int deleteproblem=0;
    for(int i=0;;i++)
    {
    Matcher mi=pattern7.matcher(sf[i]);
    if(mi.find())
    {
    deleteproblem=Integer.parseInt(mi.group(1));
    }
    if(sf[i].equals("end"))
    break;
    }
    Student student[]=new Student[stulenth];
    Pattern pattern6 = Pattern.compile("#X:(\d+)");
    for(int i=0;;i++)
    {
    String sid="";
    Matcher ma=pattern5.matcher(sf[i]);
    if(ma.find())
    {
    String stulist[]=sf[i].split("-");
    String stu1[]=stulist[0].split(" ");
    Matcher ma2=pattern6.matcher(stu1[0]);
    if(ma2.find()){
    sid=ma2.group(1);
    }
    String name=stu1[1];
    student[0]=new Student(sid,name);
    for(j=1;j<stulenth;j++)
    {
    String stu2[]=stulist[j].split(" ");
    String sid1=stu2[0];
    String name1=stu2[1];
    student[j]=new Student(sid1,name1);
    }
    }
    if(sf[i].equals("end"))
    break;
    }
    for(int i=0;;i++) {
    Matcher matcher2= pattern3.matcher(sf[i]);
    if(matcher2.find()){
    String str[]=sf[i].split(" ");
    int q=Integer.parseInt(matcher2.group(1).trim());
    nump=str.length-1;
    p[q-1].setNump(nump);
    }
    if(sf[i].equals("end"))
    break;
    }
    for(j=0;;j++)
    {
    Matcher matcher1= pattern2.matcher(sf[j]);
    if(matcher1.find())
    {
    answerpaper.add(k++,sf[j]);
    }
    if(sf[j].equals("end"))
    break;
    }
    int size=answerpaper.size();
    Answer answer[]=new Answer[size];
    for(int i=0;i<size;i++)
    answer[i]=new Answer();
    Pattern pattern1 = Pattern.compile("#A:(\d+)-(.
    )");
    Pattern pa=Pattern.compile("#S:(.+)");
    int b=0;
    int c=0;
    for(int i=0;i<size;i++)
    {
    String s[]=answerpaper.get(i).split(" ");
    for(int l=0;l<s.length;l++)
    t[l]=s[l];
    c=s.length-2;
    }
    int answernumber[]=new int[c];
    for(int i=0;i<size;i++)
    {
    // String s[]=answerpaper.get(i).split(" ");
    // for(int l=0;l<s.length;l++)
    // t[l]=s[l];
    // c=s.length-2;
    for(j=2;j<c+2;j++)
    {
    Matcher m= pattern1.matcher(t[j]);
    if(m.find())
    {
    answernumber[j-2]=Integer.parseInt(m.group(1).trim());
    }
    }
    }
    int questionNumber[]=new int[10];
    String questionContent[]=new String[10];
    String standardAnswer[]=new String[10];
    int grade[]=new int[10];
    int num[]=new int[nump];
    int g=0;
    String regex = "#N:(\d+)\s+#Q:(.+)\s+#A:(.+)";
    Pattern po=Pattern.compile("#N:(\d+).*");
    Pattern pattern = Pattern.compile(regex);
    for(int i=0;;i++)
    {
    Matcher matcher= pattern.matcher(sf[i]);
    Matcher mo=po.matcher(sf[i]);
    if(mo.find()&&matcher.find()
    false)
    {
    notproblem.add(sf[i]);
    int x=Integer.parseInt(mo.group(1).trim());
    questionNumber[g]=x;
    questionContent[g]="";
    standardAnswer[g]="";
    g++;
    }
    if(sf[i].equals("end"))
    break;
    }
    for(int i=0;;i++)
    {

        Matcher matcher= pattern.matcher(sf[i]);
        if(matcher.find()){
                int x=Integer.parseInt(matcher.group(1).trim());
                questionNumber[g]=x;
                questionContent[g]= matcher.group(2).trim();
                standardAnswer[g]= matcher.group(3).trim();
                g++;
        }
        if(sf[i].equals("end"))
            break;
    }
    Pattern pu=Pattern.compile(".*T[^a-zA-Z].*");
    ArrayList<String> gradepaper=new ArrayList<String>();
    int fg=0;
    ArrayList<String> paperexsist=new ArrayList<String>();
    for(int i=0;;i++) {
         Matcher mu=pu.matcher(sf[i]);
        if(mu.find())
        {
            gradepaper.add(sf[i]);
        }
        if(sf[i].equals("end"))
            break;
    }
    for(int i=0;i<gradepaper.size();i++) {
        Matcher matcher2= pattern3.matcher(gradepaper.get(i));
        if(matcher2.find()==false)
        {
            fg=1;
            paperexsist.add(gradepaper.get(i));
        }
    }
    if(fg==0){
    int existproblem=0;
    for(int i=0;;i++) {
         Matcher matcher2= pattern3.matcher(sf[i]);
         Matcher mu=pu.matcher(sf[i]);
         int sum=0;
        if(matcher2.find()){
            String str[]=sf[i].split(" ");
            int y=Integer.parseInt(matcher2.group(1).trim());
            for( j=1;j<str.length;j++)
            {
                Matcher matcher3= pattern4.matcher(str[j]);
                if(matcher3.find())
                {
                    num[j-1]=Integer.parseInt(matcher3.group(1).trim());
                     int index=findIndex(questionNumber,num[j-1]);
                    if(index!=-1)
                    {
                        grade[index]=Integer.parseInt(matcher3.group(2).trim());
                      sum=sum+grade[index];
                    }
                    else
                    {
                        grade[9]=0;
                        sum=sum+grade[9];
                    }
                }
            }
            p[y-1].setallGrade(sum);
            int a=0;
            for(int u=0;u<str.length-1;u++)
            {
            	int index1=-1;
                int index=findIndex(questionNumber,num[u]);
                if(u+1<=answernumber.length)
                	index1=findIndex(num,answernumber[u]);
                if(index!=-1&&index1!=-1)
                {
                	
                	p[y-1].addproblem(a+1,new Problem(questionNumber[index],questionContent[index],standardAnswer[index],grade[index]));
                    a++;
                }
                if(index==-1&&index1==-1)
                {
                	p[y-1].addproblem(a+1,new Problem(0,"","",0));
                    a++;
                }
                if(index1==-1||index==-1)
                {
                    if(str.length-1==answernumber.length)
                        existproblem=1;
                }
            }
        }
        if(sf[i].equals("end"))
            break;
    }
    String id="";
    for(int i=0;i<size;i++)
    {
             String s[]=answerpaper.get(i).split(" ");
             for(int l=0;l<s.length;l++)
                 t[l]=s[l];
            c=s.length-2;
           id=t[1];
           if(c==0)
           {
        	   String printfanswer[]=new String[nump];
        	   Matcher o=pa.matcher(t[0]); 
               if(o.find())
               {
                   b=Integer.parseInt(o.group(1).trim()); 
               }
               answer[i].setAnswernum(b);
               answernumber=new int[nump];
             if(answer[i].getAnswernum()<=p.length)
                   answer[i].setPaper(p[b-1]);
               else
                   answer[i].setPaper(p[p.length-1]);
               for(j=0;j<nump;j++)
               {
            	   printfanswer[j]="";
               }
               int y=0;
               for(int u=0;u<c;u++)
               {
                   int index=-1;
                   for(int v=0;v<answer[i].getPaper().getproblemlist().size();v++)
                   {
                   	if(answer[i].getPaper().getproblemlist().get(v).getPnum()==answernumber[u])
                   	{
                   		index=v;
                   	}
                   }
                   if(index!=-1)
                   {
                   	answer[i].addanswer(y+1,printfanswer[index]);
                   	y++;
                   }
                       
               }
               for(int z=0;z<stulenth;z++)
               {
            	   if(student[z].getSid().equals(t[1]))
            	   {
            		   student[z].setAnswer(answer[i]);
            	   }
               }
           }
           else {
            String printfanswer[]=new String[c];
            Matcher o=pa.matcher(t[0]); 
            if(o.find())
            {
                b=Integer.parseInt(o.group(1).trim()); 
            }
            answer[i].setAnswernum(b);
          if(answer[i].getAnswernum()<=p.length)
                answer[i].setPaper(p[b-1]);
            else
                answer[i].setPaper(p[p.length-1]);
            for(j=2;j<c+2;j++)
            {
                Matcher m= pattern1.matcher(t[j]);
                if(m.find())
                {
                    
                    printfanswer[j-2]=m.group(2).trim();
                }
            int y=0;
            for(int u=0;u<c;u++)
            {
                int index=-1;
                for(int v=0;v<answer[i].getPaper().getproblemlist().size();v++)
                {
                	if(answer[i].getPaper().getproblemlist().get(v).getPnum()==answernumber[u])
                	{
                		index=v;
                	}
                }
                if(index!=-1)
                {
                	answer[i].addanswer(y+1,printfanswer[index]);
                	y++;
                }
                    
            }
        for(int z=0;z<stulenth;z++)
        {
            if(student[z].getSid().equals(t[1]))
            {
                student[z].setAnswer(answer[i]);
            }
        }
           }
    }
    } 
    for(j=0;j<size;j++)
    {
        for(int i=0;i<answer[j].getPaper().getNump();i++)
        {
        	if(i+1<=answernumber.length)
        		answer[j].ajudge(i+1,deleteproblem,existproblem,answernumber[i]);
        	else
        		answer[j].ajudge(i+1,deleteproblem,existproblem,0);
        }
    }
    for(int i=1;i<=psum;i++)
    {
        if(notproblem.size()!=0)
        {
            for(String pro:notproblem)
                System.out.println("wrong format:"+pro);
        }
        if(p[i-1].getallGrade()!=100)
            System.out.println("alert: full score of test paper"+i+" is not 100 points");
    }
    int flag=0;
    for(int i=0;i<size;i++)
    {
        
        if(answer[i].getAnswernum()>p.length)
             System.out.println("The test paper number does not exist");
        else
        {
            answer[i].printf1(answer[i].getPaper().getNump(),deleteproblem,existproblem,answernumber[i]);
            for(j=0;j<student.length;j++)
            {
                if(student[j].getSid().equals(id))
                    flag=1;
            }
            if(flag==1)
            {
                student[i].show();
            answer[i].printf2(answer[i].getPaper().getNump());
            }
            else
                System.out.println(id+" not found");
        }
    }
    }
    else
    {
        for(String pro:paperexsist)
        {
            System.out.println("wrong format:"+pro);
            System.out.println("The test paper number does not exist");
        }
    }
    

    }
    public static int findIndex(int array[],int target){
    for (int i=0;i<array.length;i++){
    if (array[i]==target){
    return i;
    }
    }
    return -1;
    }
    }
    从Main类中可以看出在第二次的基础上加上了更多的for循环,复用性差的问题依然存在,可读性也差,在判断格式错误方面也很难完成,因为我没有直接将所有的分开用正则表达式进行判断,而是用循环,对输入的字符串数组遍历进行识别,工作量大。
    三、踩坑心得
    1.答题判断程序-1
    第一次的在输入问题上刚开始出现了问题,第一行我用的是nextInt()输入题目数量,而后面开始用nextLine()输入题目和答案,这导致在输入题目数后有个空格没有识别,也就是第二个问题的输入直接识别了空,而答案的输入识别了问题,导致问题答案都没有记录到。因此我在输入题目数量后加了一个无用的nextLine(),这样就解决了问题。
    2.答题判断程序-2
    第二次的在正则表达式的使用上出了错误,我错误的认为\s+是识别0~多个空格,导致这题中的识别出了问题,花费了大量时间去看别的代码,最后才发现是正则表达式出了问题,最后将\s+改成了\s*。
    3.答题判断程序-3
    第三次没有对输入进行全部判断,在后面的格式错误的测试中很难进行判断格式错误,做法是一个一个格式去判断,搞得很复杂,同时也有些情况判断不了,最后也是没有将这道题目全部做完。
    四、改进建议
    1.答题判断程序-1

    132行可以改为int nump=Integer.parseInt(sc.nextLine());然后把145行去掉。
    2.答题判断程序-2
    可以专门设计一个题目试卷类来管理试卷分数与题目之间的关系。
    3.答题判断程序-3
    可以设计一个匹配类来执行匹配步骤,用于方便对错误格式输入的判断,以及输出。这样可以避免for循环过多,使得程序复杂。
    五、总结
    1.这三次题目下来,反复的修改,大量的修改,使我意识到了提前设计好的重要性,而不是一步一步根据测试点来做题,这中的做题习惯很容易浪费时间,而且也学不到什么。
    2.意识到了提前学习的重要性,比如正则表达式,ArrayList类,这两个是这次题目集中得用到的,刚开始还是按照C语言的大部分方法去做,使得很复杂,很难实现。
    3.最后在面=面向对象的设计原则方面还得多加研究学习,避免在1中提到的问题,减少自己写代码量的同时也可以让程序简单可读。

posted on 2024-04-20 17:47  唱航java王  阅读(10)  评论(0编辑  收藏  举报