EE308Lab1-2

阮喆吉 2022-10-28 04:45:30
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 AssignmentExtract keywords of different levels from the C code files that are read in
MU STU ID and FZU STU ID20124163 832002230

github Repository

https://github.com/ZhejiRuan/EE308_Lab1_2

1.Give the PSP form for this work.

Framework activities:what I could doESTIMATE TIME(MINUTE)
PlanningUnderstand the meaning of the problem, think of a rough solution process60
designProgram the idea and write a rough outline150
Code ReviewCheck for compliance with programming specifications and check for syntax errors90
TestImport different data for detection90
Postmortem & Process Improvement PlanDo an overall review to make sure the program works30
Total420

2.Problem Solving Ideas

First, find the method to read the c file.
Second, find c language keywords table, in the form of an array into the code,
Third, through the regular expression and stack to achieve the task.
Fourth, the requirements are divided into 4 levels, input the corresponding level can execute the corresponding code.

3.Design and implementation process

First, read the file from BufferedReader and put it together as a string.
Second, put the keyword table into the array,
Third, match all keywords according to the regular expression and count them.
Fourth, distribution solves Switch, case,if-else,if-elseif-else cases.

img

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

First, gets user's input, reads the specified file and judge requirements.

        Scanner scanner = new Scanner(System.in);

        //Read C file from file address
        System.out.println("Please input the path of the code file");
        //String fileName = "D:\\Code\\software_engineering_lab1\\src\\sample.c";
        String fileName = scanner.nextLine();

        //Read the file
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
        String Cfilepath = "";
        String line = bufferedReader.readLine();
        while (line != null) {
            Cfilepath += line;
            line = bufferedReader.readLine();
        }
        
        //Completion Level: You can enter 4
        System.out.println("Please input the completion level (from low to high as 1, 2, 3, 4 )");
        int level = scanner.nextInt();

        //Judge requirements
        switch (level) {
            case 1:
                findKey(Cfilepath);
                break;
            case 2:  
                findKey(Cfilepath);
                findSwitchAndCase(Cfilepath);
                break;
            case 3:
                 findKey(Cfilepath);
                 findSwitchAndCase(Cfilepath);
                 processElse(Cfilepath,3);
            case 4:
                 findKey(Cfilepath);
                 findSwitchAndCase(Cfilepath);
                 processElse(Cfilepath,4);
                 break;
         }

Second, level 1, this is the function code for extract the word to store in an array.

public static void findKey(String s) {
            String keywords = "char,short,int,long,signed,unsigned,float,double,struct,union,enum,void,"
                    + "for,do,while,break,continue,if,else,goto,switch,case,default,return,"
                    + "auto,extern,register,static,typedef,const,sizeof,volatile";
            Pattern p1 = Pattern.compile(",");
            String[] cKeyword = p1.split(keywords);
            int totalNum = 0;
            for (String oneKey : cKeyword) {
                Pattern p = Pattern.compile(oneKey + "[^a-z]");
                Matcher matcher = p.matcher(s);
                while (matcher.find()) {
                    totalNum++;
                }
            }
            System.out.println("total num: " + totalNum);
        }

Third, level 2, find the switch and output the number of "case" corresponding to each group.

public static void findSwitchAndCase(String s) {
            Pattern p1 = Pattern.compile("switch");
            String[] switchs = p1.split(s);
            int switchNum = switchs.length - 1;
            System.out.println("switch num: " + switchNum);

            System.out.print("case num:");
            for (int i = 1; i < switchs.length; i++) {
                Pattern p2 = Pattern.compile("case");
                String[] cases = p2.split(switchs[i]);
                int caseNum = cases.length - 1;
                System.out.print(" " + caseNum);
            }
            System.out.println("");
        }

Fourth, level 3 and level 4, find the number of else if and if-else if -else.

public static void processElse(String code,int level) {
        int ifelNum = 0;
        int esifNum = 0;
        boolean lock = true;
        Stack stack = new Stack();
        Pattern pattern = Pattern.compile("else if|if|else");
        Matcher matcher = pattern.matcher(code);
        while (matcher.find()) {
            String temp = matcher.group();
            if ("if".equals(temp)) {
                stack.push(temp);
            } else if ("else if".equals(temp)) {
                stack.push(temp);
            } else if ("else".equals(temp)) {
                if ("if".equals(stack.peek())) {
                    stack.pop();
                    ifelNum++;
                } else {
                    while (!"if".equals(stack.peek())) {
                        stack.pop();
                        if (lock == true) {
                            esifNum++;
                            lock = false;
                        }
                    }
                    stack.pop();
                    lock = true;
                }
            }
        }
        if (level == 3) {
            System.out.println("if-else num: " + ifelNum);
        } else if (level == 4) {
            System.out.println("if-else num: " + ifelNum);
            System.out.println("if-elseif-else: " + esifNum);
        } else {
        }
    }

5.Unit test screenshots and description.

level 1:

img

level 2:

img

level 3 and level 4:

img

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

As shown in the figure below, there were problems with the previous codes at level 3 and level 4. After communicating with team members, we found the problem and corrected the code errors in time.

img

7.Summarize this assignment.

Through this experiment, I understand that in order to complete the experiment faster and better, we need to make a preliminary plan first, and we need to have a full understanding and analysis of the subject. Draw program flow chart, convenient for us to complete the implementation of the code. At the same time, when there is a problem in the experimental code, we should timely discuss the problem with the team members and solve the problem in time.

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

286

社区成员

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

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