EE308FZ_Lab1-2

832002220赵云龙 2022-10-25 17:55:48

CATALOG

  • Table
  • GitHub Repository
  • Code Standard
  • PSP Form
  • Description of problem-solving ideas
  • Design and implementation process && Code description
  • test code
  • Read the test code and access to different levels of methods
  • level1()- Calculate the number of keywords in the test code
  • level2() - Calculate the number of "switch case" structure and the number of "case" corresponding to each group
  • level34() - //Calculate the number of "if, else" structures and the number of "if, else if, else" structures
  • Performance test
  • Summary

Table

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 AssignmentExtract keywords of different levels from the C code files that are read in
MU STU ID and FZU STU ID<20123531_832002220>

GitHub Repository

https://github.com/YUNLONG892a/EE308

Code Standard

  1. Indentation: 4 space
  2. Variable naming: Camel-Case
  3. Maximum number of characters per line: 100
  4. Function and class naming: FunctionName(Type par_name1, Type par_name2)
  5. Blank line rule: Empty line between non-empty code blocks
  6. Annotation rules:Start with/and ends with /, that is, /* */

PSP Form

Personal Software Process StagesTime/minutes
Planning15
Estimate20
Analysis25
Design120
Code Review30
Test and debug50
Total260

Description of problem-solving ideas

  1. Consider how do I read c files into a program.
  2. In order to meet the different Program Requirements, it is necessary to make corresponding code design for four different levels of tasks.
  3. By using regular expression and stack to analysis the test code.
  4. Learning how to do unit tests

Design and implementation process && Code description

test code

#include <stdio.h>  
int main(){  
  int i=1;  
  double j=0;  
  long f;  
  switch(i){  
    case 0:  
      break;  
    case 1:  
      break;  
    case 2:  
      break;  
    default:  
      break;  
  }  
  switch(i){    
    case 0: 
      break;  
    case 1:  
      break;  
    default:  
      break;  
  }  
  if(i<0){  
    if(i<-1){}  
    else{}   
  }  
  else if(i>0){  
    if (i>2){}  
    else if (i==2) {}  
    else if (i>1) {}  
    else {}  
  }  
  else{  
    if(j!=0){}  
    else{}  
  }  
  return 0;  
}  



Read the test code and access to different levels of methods

Enter the path to the test code and the required task level into the different code

img

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String location  = sc.nextLine();
        int n_value = sc.nextInt();///Users/zhaoyunlong/Desktop/EE308/EE308_LAB1-2.txt
        String txtFile = location;
        String line = "";
        String line1 = "";
        String[] data = new String[100000];
        int i = 0;
        try (BufferedReader br = new BufferedReader(new FileReader(txtFile))) {

            while ((line = br.readLine()) != null) {
                
                // use comma as separator
                data[i] = line;
                
                                i++;

            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(n_value == 1) {
          level1(data,i);
        }
        else if(n_value == 2) {
          level2(data,i);
        }
        else if(n_value == 3) {
        level34(data,i);
        }
    }

level1()- Calculate the number of keywords in the test code

Use the Pattern and Matcher methods to find all the key word in the test code,then print the result.

img

 public static void level1(String str[],int n) {
    int totalnum = 0;
    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
    String []keyArr=keywords.split("、");
    for(int  j = 0 ; j <= n - 1; j++) {
       String m = str[j];
        for(int q = 0 ; q <= keyArr.length - 1 ; q++) {
           Pattern p = Pattern.compile("[^a-z]"+keyArr[q]+"[^a-z]");//The keyword must be preceded and followed b                        
           Matcher match = p.matcher(m);                            //y non-letters to be recognized as a keyword
            if(match.find()) {
                totalnum++;                                                
               }                     
        }
    }
    System.out.println("total num: " + totalnum);
} 

Output :

img

level2() - Calculate the number of "switch case" structure and the number of "case" corresponding to each group

Again, using the Regular expression look for "switch" and count the number of cases until you find "default",then print the result.

img

//Calculate the number of "switch case" structure and  the number of "case" corresponding to each group
public static void level2(String str[],int n) {
    int switchnum = 0;
    int [] num = new int [10000];
    int casenum = 0;
    for(int  j = 0 ; j <= n - 1; j++) {
       String m = str[j];
           Pattern p = Pattern.compile("[^a-z]switch[^a-z]");
           Matcher match = p.matcher(m);
           Pattern p1 = Pattern.compile("[^a-z]case[^a-z]");
           Matcher match1 = p1.matcher(m);
           Pattern p2 = Pattern.compile("[^a-z]default[^a-z]");
           Matcher match2 = p2.matcher(m);
           if(match.find()) {
                 switchnum++;
                 continue;
          }
           if(match1.find()) {
              casenum++;
          }
          else if(match2.find()) {
              num[switchnum - 1] = casenum;
              casenum = 0;
          }   
         }                     
        
    
    System.out.println("switch num: " + switchnum);
    System.out.print("case num: " );
    for(int i = 0 ; i <= switchnum - 1 ; i++) {
        System.out.print(num[i] + " ");
    }
    System.out.println();
    
} 

Output :

img

level34() - //Calculate the number of "if, else" structures and the number of "if, else if, else" structures

To solve the problem of nesting "if_else if_else"/" if_else" statements,I chose the stack to store "if/else if/else". Whenever I found the "else" keyword, I would delete the last keyword "if" stored in the stack and the keyword "else if" above it, and count the number of different structures.

img

//Calculate the number of "if, else" structures and the number of "if, else if, else" structures
public static void level34(String str[],int n) {
    int ifnum = 0;
    int it = 0;
    Stack<String> s = new Stack();
    boolean [] bool = new boolean [10000];
    int ienum = 0;
    int ieenum = 0;
    for(int  j = 0 ; j <= n - 1; j++) {
       String m = str[j];
           Pattern p = Pattern.compile("[^a-z]if[^a-z]");
           Matcher match = p.matcher(m);
           Pattern p1 = Pattern.compile("[^a-z]else[^a-z]");
           Matcher match1 = p1.matcher(m);
           boolean a = match.find();
           boolean b = match1.find();
            if(a&&!b) {
               s.push("if");
             
            }
            else if(a&&b) {
                 s.push("else if");
                
            }
           else if(!a&&b) {
                   s.push("else");
                boolean c = false;              //The bool to see if the structure is "if_else" or" if_else"
                while(!s.isEmpty()) {
                   String u = s.pop();
                   if(u.equals("if")) {
                       if(c) {
                           ieenum++;
                           c = false;   
                       }                    
                       else {
                           ienum++;
                       }
                       break;
                   }
                   else if(u.equals("else if")) {   
                           c = true;        //The bool should be true if there is an "else if" in the middle
                   }                            //of the structure
               }
           }
                 
} 
   
    
    System.out.println("if-else num: " + ienum);
    System.out.println("if-elseif-else num: " + ieenum);
      
}

Output :

img

Performance test

Unit test code

public class Test {
Task2 t = new Task2();
@ParameterizedTest
@MethodSource("parameterDataProvider")
void Test(String url, int l, String result) throws IOException {
    t.read(url);
    assertEquals(result, t.toString());
}

public static Stream<Arguments> parameterDataProvider() {
    return Stream.of(
            Arguments.of("/Users/zhaoyunlong/Downloads/Loveyou/src/Lover/Task2.java", 1, "total num: 35"),
            Arguments.of("/Users/zhaoyunlong/Downloads/Loveyou/src/Lover/Task2.java", 2, "switch num: 2\ncase num   : 3 2"),
            Arguments.of("/Users/zhaoyunlong/Downloads/Loveyou/src/Lover/Task2.java", 3, "if-else num: 2\nif-elseif-else num: 2"),
    );
}

Performance :

img

Description :

At first, I chose to use string segmentation to find keywords and used string array and int array to find "if_else if_else"/" if_else "structure, which took up a lot of running space, reduced the efficiency of the code and prolonged the running time of the code. Later, I used Pattern and Matcher methods to judge whether the keyword in the test code and store keywords by stack, which improved the efficiency of subsequent code judgment.

Summary

  1. I learned how to create repositories on github and upload and version control my code via git.
  2. I learned how to write a blog using Markdown syntax.
  3. I learned how to use Pattern and Matcher methods for regular expression which judge whether the keyword in the test code
  4. I learned how to do unit test coverage optimization and performance testing, performance optimization screenshots and descriptions.

From this lab,I learned how to make a good project plan and understand the project requirements before doing a project, and improve the operation efficiency of the code and make the code easier to understand by optimizing the code. I hope I can summarize the experience and draw lessons from it.

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

285

社区成员

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

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