EE308-Lab1_2

m0_57843239 2022-10-27 01:31:12
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907?spm=1001.2014.3001.6377
The Aim of This Assignment<Extract keywords of different levels from the C or C++ code files>
MU STU ID and FZU STU ID<20124279_832001119>

1.the PSP of this work

Program design processWhat I doESTIMATE TIME(MIN)
PlanningUnderstand the requirements of the question, roughly construct the idea20
designWrite down the general idea,draw a logical diagram20
Write the codeImport different data for detection120
TestImport different data for detection30
Improve my codeSummarize and improve the above tests30
Wring ReportWring Report60

After getting the problem, I solve the problem of general thinking, and process

  1. First of all, I think how to let program read c files
  2. The input c language file into an array form
  3. By looking up the data that through the positive expression can more easily calculate the required value, get the result(a Regular Expression is a pattern of text that includes both normal characters (for example, letters between A and z) and special characters (called "metacharacters"))

code

https://github.com/pikaquququ/lab1-2/blob/main/lab1-2

Test_code

1.    #include <stdio.h>  
2.    int main(){  
3.        int i=1;  
4.        double j=0;  
5.        long f;  
6.        switch(i){  
7.            case 0:  
8.                break;  
9.            case 1:  
10.                break;  
11.            case 2:  
12.                break;  
13.            default:  
14.                break;  
15.        }  
16.        switch(i){  
17.            case 0:  
18.                break;  
19.            case 1:  
20.                break;  
21.            default:  
22.                break;  
23.        }  
24.        if(i<0){  
25.            if(i<-1){}  
26.            else{}  
27.        }  
28.        else if(i>0){  
29.            if (i>2){}  
30.            else if (i==2) {}  
31.            else if (i>1) {}  
32.            else {}  
33.        }  
34.        else{  
35.            if(j!=0){}  
36.            else{}  
37.        }  
38.        return 0;  
39.    }  


Code writing process

Overall logic diagram

img

Reads the specified code file

public static void main(String[] args) throws IOException {
        
        //read the test file
        FileReader file = null;
        try {
            file = new FileReader("C:\\Users\\Desktop\\lab1-2.txt");
            
        } catch (Exception e) {
            e.getStackTrace();
        }
        BufferedReader input = new BufferedReader(file);
        String lineString = "";
        String tempString = input.readLine();
        while(tempString!=null) {
            lineString+=tempString;
            tempString = input.readLine();
        }
        
        solution1(lineString);
        solution2(lineString);
        solution3_4(lineString);

    }

Import c language keywords,And convert it into an array

c language keyword table(ANSI C standard C language has 32 keywords)

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";//all keywords



Calculate leve1

img

int num = 0;
        for(int i = 0; i < keywords.length; i++) {
            String regExpress = "[^a-zA-Z_]"+keywords[i]+"[^a-zA-Z_]";
            Pattern pattern = Pattern.compile(regExpress);
            Matcher matcher = pattern.matcher(input);
            while (matcher.find()) {
                num++;
            }
        }
        System.out.println("The number of keywords is "+num+".");
        System.out.println();
    }
    
    
    public static void solution2(String input) {
        
        int num_switch = 0;
        String regExpress = "[^a-zA-Z_]"+"switch"+"[^a-zA-Z_]";
        Pattern pattern1 = Pattern.compile(regExpress);
        Matcher matcher1 = pattern1.matcher(input);
        while(matcher1.find()) {
            num_switch++;
        }
        System.out.println("The number of 'switch' is "+num_switch+".");
        

Calculate the number of switches and cases

img

int num_case = 0;
        int num = 0;
        String regExpress2 = "switch.*?}";
        Pattern pattern2 = Pattern.compile(regExpress2);
        Matcher matcher2 = pattern2.matcher(input);
        while(matcher2.find()) {
            String regExpress3 = "[^a-zA-Z_]"+"case"+"[^a-zA-Z_]";
            String tempText=matcher2.toString();
            Pattern pattern3 = Pattern.compile(regExpress3);
            Matcher matcher3 = pattern3.matcher(tempText);
            while(matcher3.find()) {
                ++num_case;
            }
            num++;
            System.out.println("The number of 'case' in switch "+ num +" is "+num_case+".");
            num_case = 0;
        }
        System.out.println();
    }

Calculate the number of if-else and if-elseif-else


    public static void solution3_4(String input){
        
        Stack<String> s = new Stack<String>();
        int num_if_else = 0;
        int num_if_elseif_else = 0;
        
        String regExpress = "else *if|else|if";
        Pattern pattern = Pattern.compile(regExpress);
        Matcher matcher = pattern.matcher(input);
        
        while(matcher.find()) {
            String sub=input.substring(matcher.start(),matcher.end());

            if(sub.equals("if")||sub.equals("else if")){
                s.push(sub);

            }else {
                //s.pop() is 'if'
                if(s.pop().equals("if")) {
                    num_if_else++;
                }
                //s.pop() is 'else if'
                else{
                    num_if_elseif_else++;
                    //Keep popping the top of the stack until the top is 'if'
                    while(s.peek().equals("else if")) {
                        s.pop();
                    }

                    s.pop();
                }
            }
        }
        
        System.out.println("The number of 'if-else' is "+num_if_else+".");
        System.out.println("The number of 'if-elseif-else' is "+num_if_elseif_else+".");
        
    }
    
}


The results

img

summary

The result are all the correct with the reference ,Using regular expressions can simplify the code as much as possible, and in terms of code optimization, I use the Vector class to replace the previous string, which can save space.Because I have not used Java programming much, so I am not very skilled, took a long time, but at the same time got a lot of improvement

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

285

社区成员

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

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