EE308_lab2

Redamancy 2021-09-24 00:59:30
The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignment https://bbs.csdn.net/topics/600546911
The Aim of This AssignmentGit/GitHub Using & Keywords Capturing
MU STU ID and FZU STU ID<19105584_831902124>

🤖GitHub Repository

https://github.com/WuDiancong/MU_EE308_lab2

💤PSP form

Personal Software Process StagesEstimated Time/minutesCompleted Time/minutes
Planning3030
Estimate1515
Development1515
Analysis60100
Design Spec3035
Design Review1015
Coding Standard1520
Design6050
Coding7201000
Code Review6030
Test120150
Reporting300300
Test Report3045
Size Measurement1010
Postmortem&Process Improvement90120
total15651935

✊problem-solving ideas

1. Thinking collision

After seeing the request for this lab, a few of us friends chose to meet up in the evening to discuss our individual ideas. It was not a team assignment though. But each person's unique thinking can ignite inspiration in others. Indeed!!! Our meeting of minds helped me gain a deeper understanding of how I should complete it.

2. What programming language to choose

I have chosen JAVA as my programming language for this lab. In fact, my friends have suggested that it would be much easier to use Python for this problem. But I'm not too good at Python yet. Between JAVA and C I chose JAVA.

3. Solving Problem

I want to divide this task into sections and I can complete these sections separately and eventually compose them into a complete code.

  • Part 1: Read in a c/c++ code file, and pre-processing of the rest of the content..
  • Part 2: Matching and counting of keywords with corresponding results according to different completion levels.
  1. Count the number of keywords.

  2. Count the number of "switch case" structures and the number of "case" in each group.

  3. Count the number of "if else" structures.

  4. Count the number of "if, else if, else" structures.

By thinking about this and breaking it down, our task becomes much simpler and clearer. The rest is up to us to solve each of these problems individually.


🥱Design and implementation process

First of all, I read the content of the text and preprocessed the data. All symbols and numbers were replaced by blank "".

Then create a new array and split the string into the array with "". After that, we started to solve problems according to different levels.
For Level_1, I just set up a keyword count and counted the number of keywords.

For Level_2, I first count the number of keywords "switch ". Then, for "case", I used a vector object and solved it.

img

For Level_3, I'm using nested for and while loops here. The basic logic is to find an "if" first, and from there, continue searching until an "else" appears. (This "else" cannot be "else if".)
Mark the two positions and search the two positions to determine if there is an "if else" or "if else-if else" on the two positions. Finally, it is divided into two cases for data processing.

img

For Level_4, we count the number of "if else "structures in Level_3 and find the relationship between the number of "if else" structures and the number of "if else-if else "structures.
And from this relationship, we get the number of if else-if else structures.


😯Code description

1. Function of Level 1: count the number of all the keywords

//Function of Level 1:count the number of all the keywords
    public static int Level_1(String arr[],String key[]) {
        int num_keyWords = 0;
        for(int i=0;i < arr.length;i++){
            for(int j=0;j < key.length;j++) {
                if(arr[i].equals(key[j])) {
                    num_keyWords ++;
                }
            }
        }
        System.out.println("total num: " + num_keyWords);
        return num_keyWords;
    }

2. Function of Level 2: count the number of keywords switch and cases

//Level 2: count the number of keywords switch and cases
    public static int Level_2(String arr[],String key[]) {
        int num_switch = 0;
        for(int i=0;i < arr.length;i++) {
            if(arr[i].equals("switch")) {
                num_switch ++;
            }
        }
        System.out.println("switch num:  " + num_switch);
        
        //count the number of "switch case" structures
        Vector vec_case = new Vector(4);
        int num_case = 0;
        int index = -1;
        for(int i=0;i < arr.length;i++) {
            if(arr[i].equals("switch")) {
                 index++;
                 num_case=0;
            }
            if(arr[i].equals("case")) {
                 num_case++;
                 vec_case.add(index,num_case);
            }
        }
        
        System.out.print("case num:  ");
        if(num_switch == 0) {
            System.out.println(0);
        }else {
            for(int t=0;t <= index;t++) {
                System.out.print(vec_case.get(t)+" ");
            }
            System.out.println();  
        }
        return num_switch;
    }

3. Function of Level 3: count the number of if-else structure

//Level 3: count the number of if-else structure
    public static int Level_3(String arr[],String key[]) {
        int num_if_else = 0;
        for(int i=0;i<arr.length;i++) {    
            if(i!=0) {
                if(arr[i].equals("if")&&!arr[i-1].equals("else")) {
                    int init = i + 1;
                    if(i<arr.length-2) {
                        while((!(arr[i+1].equals("else")&&!arr[i+2].equals("if")))&&i<arr.length-3) {
                            i++;
                        }
                    }else{
                        break;
                    }
                    
                    int tmp = i;
                    boolean flag = false;
                    boolean flag2 = true;
                    for(int t = init;t<tmp;t++) {
                        if(arr[t].equals("if")&&!arr[t-1].equals("else")) {
                            
                            for(int k = t;k<tmp;k++) {
                                if(arr[k].equals("else")) {
                                    flag2 = false;
                                    break;
                                }
                            }
                            
                            if(flag2) num_if_else ++ ;
                            flag = true;
                            break;
                        }
                    }
                    
                    if(flag) {
                        if(i<arr.length-2) {
                            while(!(arr[i+1].equals("else")&&!arr[i+2].equals("if"))) {
                                i++;
                            }
                        }else {
                            break;
                        }
                        num_if_else ++;
                    }
                }
            }      
        }
        System.out.println("if-else num: "+num_if_else);        
        return num_if_else;
    }

4. Function of Level 4: count the number of if-else if-else structure

//Level 4:count the number of if-else if-else structure
    public static int Level_4(String arr[],String key[]) {
        int num_else = 0;
        int num_if_else_if_else = 0;
        int num_if_else = Level_3(arr,key);
        
        for(int i=0;i<arr.length;i++) {
            if(i < arr.length - 1) {
                if(arr[i].equals("else")&&!arr[i+1].equals("if")) {
                    num_else ++;
                }
                }
                if(i == arr.length-1) {
                    if(arr[i].equals("else")) {
                        num_else ++;
                    }
                }
            }
        
            num_if_else_if_else = num_else - num_if_else;
            System.out.println("if -else if- else num: "+num_if_else_if_else); 
            return num_if_else_if_else;
    }

🙃Unit test

1. test.c:

#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;
}

2. Input and Output

Input Data:

Please enter the path of the file: 
C:\Users\wdclovemm\Desktop\EE308-软件工程\实验\lab2\test.txt
Please enter the completion level what we want: 
4

Output Data:

total num: 35
switch num:  2
case num:  3 2 
if-else num: 2
if -else if- else num: 2

3. Unit test coverage optimization

img

Based on the results of our tests, we obtained a relatively high test coverage of the code, which clearly shows that the security of the code can be guaranteed.

4. Performance testing

img


😃Summarize

During this experiment, I learned a lot of new things on my own through self-learning, and I was able to think outside the box and get inspired by sharing good ideas with friends.
I learned how to use GitHub, and how to upload code from local to GitHub.
I learnt how to think about the perspective and time frame of a formal software program.
I learned how to use Unit test coverage optimization and performance testing.
I learned a lot and received a lot through this experiment, and I thank my teacher for giving us this opportunity. Maybe my homework wasn't perfect, but I did it!


Special Thanks: Thanks to my friends Jian Lang for helping me with some technical stuff!

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

183

社区成员

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

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