godness4

导航

作业总结3

 

第八次PTA作业 

这次的作业新开了一个类型对于课程的统计。首先分析下这个题目

 

 

 

 以下是为自己的类图,没有按照给出的类图来写,绝大部分的内容储存在Student的类里,并且定义了较多的List和map来储存信息

 以下是我的正则表达式,用来匹配各种可能的输入方式

    String regex = "^[a-zA-Z\\\\u4e00-\\\\u9fa5]{1,10}"
                + "+(必修|选修)\\s+(考试|考察)$";
        //输入课程
        String regexscore1 = "^[0-9]{8}\\s[a-zA-Z]+\\s[a-zA-Z]{1,10}"
                + "\\s[a-zA-Z]{1,10}\\s(100|[1-9]\\d|\\d)"
                + "(\\.\\d+)?\\s(100|[1-9]\\d|\\d)(\\.\\d+)?$";
        //输入考试成绩信息
        String regexscore2 = "^[0-9]{8}\\s[a-zA-Z]+\\s[a-zA-Z]{1,10}\\s[a-zA-Z]"
                + "{1,10}\\s(100|[1-9]\\d|\\d)(\\.\\d+)?$";

以下为我储存的学生各种信息,subname储存学科名字;inscore储存一个班的学生信息,inscoreMap按学科储存学生信息,建立以班号为键,内部以学科为键,SubListMap每个科目的分,classListMap2每个学生的分。后续通过计算来获取相应的结果

  HashMap<String, ArrayList<HashMap<String, Student> >> allscore = new HashMap<>();

          ArrayList<String> subname = new ArrayList<>();//储存学科名字
          ArrayList<HashMap<String, Student> > inscore = new ArrayList<>();//储存一个班的学生信息
          HashMap<String, Student>  inscoreMap = new HashMap<>();//按学科储存学生信息
          //建立以班号为键,内部以学科为键

          ArrayList<String>  classname = new ArrayList<>();
         Map<String, ArrayList<Student>> SubListMap = new HashMap<>();//每个科目的分     
         Map<String, ArrayList<Double>> classListMap = new HashMap<>();//每个班的分
         Map<String, ArrayList<Double>> classListMap2 = new HashMap<>();//每个学生的分
        Map<String,Course> courseMap = new HashMap<>();

而对数据进行计算后,对数据进行排序,分别按照班级排序和学号排序

 Collator collator = Collator.getInstance(Locale.CHINA); // 获取中文字符排序规则
 List<Map.Entry<String, ArrayList<Student>>> list2 = new ArrayList<>(SubListMap.entrySet());
 Collections.sort(list2, new Comparator<Map.Entry<String, ArrayList<Student>>>() {
     @Override
     public int compare(Map.Entry<String, ArrayList<Student>> o1, Map.Entry<String, ArrayList<Student>> o2) {
         return collator.compare(o1.getKey(), o2.getKey()); // 比较键名的字符顺序
     }
 });
 
        List<Map.Entry<String, ArrayList<Double>>> list = new ArrayList<>(classListMap2.entrySet());

     // 按键的字典序升序排序
     Collections.sort(list, (o1, o2) -> (o1.getKey().compareTo(o2.getKey())));
       

 对数据进行排序后用foreach对数据进行输出

 for (Entry<String, ArrayList<Double>> List : list) {
         String key = List.getKey();
         System.out.print(key+" ");
         if(classListMap2.get(key) == null) {
             System.out.println("did not take any exams");
             break;
         }
            double totalscore = 0;
            int sum = 0;
          List<Double> value = List.getValue();
          for (Double i : value) {
              totalscore += i;
              sum ++;
          }
          if(totalscore < 0) {
               System.out.println("did not take any exams");
               continue;
          }
          System.out.println((int)totalscore/sum);
      }
  
  for (Entry<String, ArrayList<Student>> entry : list2) { 
         String key = entry.getKey(); 
         System.out.print(key+" ");

           int totalscore = 0;
        int sum = 0;
        int finalscore = 0;
        int daily = 0;
            List<Student> value = entry.getValue();
           
            for (Student i : value) {
                if(i == null) {
                     System.out.println(" has no grades yet");
                     break;
                }
                totalscore += i.getTotleScore();
                daily += i.getDailyScore();
                finalscore +=  i.getFinalScore();
                sum ++;
            }       
            if(sum == 0) {
                break;
            }
            if(finalscore < 0) {
                System.out.println("has no grades yet");
           }    
            else if(daily/sum >= 0) {
            System.out.println(daily/sum+" "+finalscore/sum+" "+totalscore/sum);
            }else if(daily/sum < 0){
                  System.out.println(finalscore/sum+" "+totalscore/sum);
            }
        }
     
        for(String name : subname) {
            if(SubListMap.get(name) == null) {
                System.out.println(name + " has no grades yet");
            }
        
     }

以及班级平均分

 for (Entry<String, ArrayList<Double>> entry : classListMap.entrySet()) {
              int totalscore = 0;
              int sum = 0;
            String key = entry.getKey();
            List<Double> value = entry.getValue();

            System.out.print(key+" ");
            for (Double i : value) {
                totalscore += i;
                sum ++;
            }
            if(totalscore < 0) {
                 System.out.print("has no grades yet");
                 continue;
            }
            System.out.println(totalscore/sum);
        }
       }
    }

这题一开始我确实是嫌麻烦,因为题目长度太长,而且好多人也没得分,于是我也没怎么写,但经过听课,以及前面菜单的题目也没怎么写,所以必须要重新审视起来,先说这题的思路吧,首先需要把题目弄懂,然后再写相关的类,很明显有学生,而且每个桌子的属性有学号、平时成绩,期末成绩和平均分,所以需要设计一个Student类来处理这些信息,基本的类题目已经给出。其次是写主类,先要考虑如何输入,格式是如何的,一开始我想的太复杂,设计了一个嵌套了很多层的map来存储所有的信息,花了很长时间,虽然成功输入,但是后来信息的提取连我自己都搞晕了,于是将map拆开,分为不同的map和list来存不同类型的数据后面的提取就更简单一点。

总的来说:

这次作业是一次不小的挑战,非常长的题目考察了对我们的理解分析问题的能力。同时也让我对于正则表达式在代码中的运用有了更加深刻的理解,学会更加灵活地使用正则表达式,但是较难的还是无法掌握,明白了不同存储数据方式的优劣势,能够更加对于容器的存储与调用。

 

posted on 2023-06-27 20:10  水温寒  阅读(7)  评论(0编辑  收藏  举报