EE308Lab1-2

un火 2022-10-27 22:31:24
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907
The Aim of This AssignmentSlove the problem of Lab1-2
MU STU ID and FZU STU ID20123311 832002216

github address
https://github.com/HaooLi/EE308-LAB1-2

1.Give the PSP form for this work.

Framework activities:what I didESTIMATE TIME(MINUTE)
PlanUnderstand the meaning of the problem, think of a rough solution process40
designProgram the idea and write a rough outline150
Code ReviewCheck for compliance with programming specifications and check for syntax errors25
TestImport different data for detection20
Postmortem & Process Improvement PlanDo an overall review to make sure the program works10

2.Description of problem-solving ideas. This is the process of how to think and how to find information after getting the title at the beginning.

  1. Find the method to read the c file
  2. Put all keyword lists into string groups
  3. Find out the key words one by one and compare them

3.Design and implementation process. The design includes how the code is organized and the flow chart of the key functions.

  1. Read the file and combine it as a string
  2. Put the keyword table into the array
  3. Match all keywords according to the regular expression and count them
  4. The cases of Switch, case, if-else, and if-elseif-else are solved respectively

img

4.Code description. Show the key code of the project and explain the idea.

  • level 1:
String keywords="abstract、assert、boolean、break、byte、case、"
            + "catch、char、class、continue、default、do、double、else、"
            + "enum、extends、final、finally、float、for、if、implements、"
            + "import、int、interface、instanceof、long、native、new、"
            + "package、private、protected、public、return、short、static、"
            + "strictfp、super、switch、synchronized、this、throw、throws、"
            + "transient、try、void、volatile、while";
String []keyArr=keywords.split("、");

  public static void findKey(String[] keyArr,String c){
      int key_num = 0;
      for(int i = 0; i<keyArr.length; i++) {
          Pattern p=Pattern.compile("[^a-z]"+keyArr[i]+"[^a-z]");
          Matcher matcher=p.matcher(c);
          while(matcher.find()) {
              key_num++;
          }
      }
      System.out.println("key num is "+key_num);
  }

level 2: find the switch and output the number of "case" corresponding to each group

public static void findSwitch(String c){
        //check switch
        Pattern p=Pattern.compile("switch");
        Matcher matcher=p.matcher(c);
        while(matcher.find()) {
            switch_num++;
        }
 
        //find the number of case
        p=Pattern.compile("switch.*?}");
        matcher=p.matcher(c);
        List case_num=new ArrayList();
        while(matcher.find()) {
            String tempText=matcher.toString();//get one switch section
            Pattern temp_p=Pattern.compile("case");
            Matcher temp_matcher=temp_p.matcher(tempText);
            int temp_case_num=0;
            while(temp_matcher.find()) {
                temp_case_num++;
            }
            case_num.add(temp_case_num);
        }
        System.out.println("switch num is "+switch_num);
        System.out.print("case num are ");
        for(int i=0;i<case_num.size();i++) {
            System.out.print(case_num.get(i)+" ");
        }
        System.out.println();
    }

level 3 and 4: find else if and if-else if -else

public static void processElse(String c) {
        Pattern p = Pattern.compile("else\\s*if|else|if");
        Matcher matcher=p.matcher(c);
        Stack<String> s = new Stack();
        while(matcher.find()) {
            String temp=c.substring(matcher.start(),matcher.end());
            s.push(temp);
        }
        Stack<String> res = new Stack<String>();
        boolean flag = false;
        while (!s.isEmpty()) {
            String temp = s.pop();
            if (temp.equals("else")) {
                res.push(temp);
            } else if (temp.equals("else if")) {
                res.push(temp);
 
            } else {
             
                while (res.peek().equals("else if")) {
                    res.pop();
                    // make a little notation that its from else if, not else
                    flag = true;
                }
                if (res.peek().equals("else")) {
                    res.pop();
                }
                //if it's elseif ,add to ifElseIfNum
                if (flag) {
                    ifElseIfNum++;
                    flag = false;
                } else {
                    ifElseNum++;
                }
            }
        }
        System.out.println("if-else num: " + ifElseNum);
        System.out.println("if-elseif-else: " + ifElseIfNum);
    }

5.Unit test screenshots and description.

img

6.Unit test coverage optimization and performance testing, performance optimization screenshots and descriptions.

In the process of adding all the keywords and counting the numbers, I chose to use RE and stack for the statistics. This greatly reduces the amount of code and is easy to understand.

7.Summarize this assignment.

Through this experiment, I understand that if a project wants to be completed quickly and better, first of all we need to make a preliminary plan, and we need to have a full understanding and analysis of the subject. Draw the flowchart of the program to facilitate us to complete the implementation of the code.

...全文
131 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

285

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程(2022秋) 教学
软件工程 高校
社区管理员
  • LinQF39
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧