EE308_LAB1_2

EE301_LAB1-2

The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734618
The Aim of This AssignmentIssue an self-introduction
MU STU ID and FZU STU IDMU: 20122977 FZU: 832002209

Github Repository

https://github.com/SCP-ZZJ/EE308FZ/tree/main/lab1

1. Give the PSP form for this work.

Framework activities:what I could doESTIMATE TIME(MINUTE)
PlanningUnderstand the meaning of the problem, consider certain way to solve the question.30
designPlanning and visualizing my thoughts of solution120
Code Reviewreexamination&debugging40
TestTry different input data20
Improvement PlanCheck overall codes and add/revise comments30

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.

I chose to use regular expression for detecting keywords of C language; using array to store the data file.

So I could then using Regex to detect the keywords and count their times of occurrence while iterating the whole array.

For if-else/if-else if-else, using stack to handle.

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

  1. Determine the particular regular expression
  2. Input data(C file) – Read every line of the file
  3. Determine if the line matches the regular expression(if matches, counter++)
  4. Using stack to contain “if, else…” structures(Property: First in Last out)
  5. Display the outcome

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

Level 1 for all keywords

    public static void Level1(String[] keyArr,String code){

        int total_num = 0;
        for(int i=0; i<keyArr.length; i++) {
            Pattern p=Pattern.compile("[^a-z]"+keyArr[i]+"[^a-z]");
            Matcher matcher=p.matcher(code);
            while(matcher.find()) {
                total_num++;
            }
        }
        System.out.println("total num: "+total_num);
    }

Level 2 for switch-case

    //check switch-case
    public static void Level2(String[] keyArr, String code){
        Level1(keyArr,code);    // add extra function based on level1

        //check switch
        int switch_num = 0;
        Pattern p=Pattern.compile("switch");
        Matcher matcher=p.matcher(code);
        while(matcher.find()) {
            switch_num++;
        }

        //check case
        p=Pattern.compile("switch.*?}");
        matcher=p.matcher(code);
        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: "+switch_num);
        System.out.print("case num: ");
        for(int i=0;i<case_num.size();i++) {
            System.out.print(case_num.get(i)+" ");
        }
        System.out.println();
    }

Level 3+ for if, else if, else … structures

public static void Level3(String[] keyArr, String code) {
        Level2(keyArr, code);
        Pattern p = Pattern.compile("if|else if|else");
        Matcher matcher = p.matcher(code);
        Stack<String> stack = new Stack();
        int ifElseNum = 0;
        int ifElseIfNum = 0;
        boolean checkElseIf = false;
        while (matcher.find()) {
            String string = code.substring(matcher.start(), matcher.end());
            if (string.equals("if")) {
                stack.push(string);
            }
            else if (string.equals("else if")){
                stack.push(string);
            }
            else{  // string == "else"
                while (!stack.isEmpty()){
                    String s= stack.pop();
                    if(s.equals("else if")){
                        checkElseIf = true;
                    }else{ // s == "if"
                        if(checkElseIf == true){
                            ifElseIfNum++;
                            checkElseIf = false;
                        }else{
                            ifElseNum++;
                            break;
                        }
                    }
                }
            }
        }
        System.out.println("if-else num: " + ifElseNum);
        System.out.println("if-elseif-else num: " + ifElseIfNum);
    }

5&6. Unit test screenshots and description.

在这里插入图片描述

*Meet the requirement of level 3+

7. Summarize this assignment.

From designing small functions to developing big projects, it is crucial to do preliminary works such as learning how to access to coding related platforms like Github, reading instructors to formulate my own code specifications.

Everyone’s thinking mode is different, we can get to know various solutions to a single problem by communication, which may improve our efficiency.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值