The first personal programming operation of soft worker

The first personal programming operation of soft worker

Which course does this assignment belong toConstruction method - Autumn 2021 - Software Engineering of Fuzhou University
What are the requirements for this assignmentThe first personal programming operation of software engineering practice in autumn 2021.
The goal of this assignmentImplement a program function, which can extract different levels of keywords from the read C or C + + code files.
Student number031902432

PSP table

PSPEstimated time (h)Practice time (h)
Estimate (estimated time)4045
Analysis & Learning (demand analysis (including learning new technologies))1214
Design Spec (generate design document)22
-Coding Standard0.50.5
Design Review11.5
Test (test, modify code)78
Test Report22.5
Size Measurement11
Postmortem & process improvement plan22
Total (total time)45.555.5

Code specification

Code specification

Job address github warehouse

commit screenshot

Problem solving ideas

When I first saw this topic, my brain was blank. My first feeling was that it would be troublesome to use c + +, and I needed to call many functions to implement it (just my own feeling) Later, I heard from the students of other software engineering classes that it would be much easier to do software engineering homework with python, so I went to station B to find a py tutorial for self-study and grope while learning. In the process of learning, I slowly recognized the strength of py. After learning the basics, I began to find out whether python has a problem similar to this problem on the Internet, which is convenient to refer to the ideas and code of online leaders. First, I have to finish For the read in of files, there are some on the Internet that use the open function to open, and then use the cycle to read in by line or by word. Some are direct read. I chose read in to store the target file. The following is to deal with the redundant characters in the file, such as {}, (), []. In Baidu Rookie tutorial It is explained that regular expressions can be used to remove characters by matching.

def read_text(path):# Path is the incoming c,cpp path                                                  
        fp = open(path, 'r', encoding='utf-8')                                         
        str_str = '~`!#$%^&*()_+-=|\';":/.,?><~·!@#¥%......&*()-+-=": ';,. ,?><{}'          
        file_text = re.sub(r"[%s]+" % str_str, " ", # All symbols are removed by regularizing the original text content     
        file_text = file_text.split('\n')   # Split text by newline                                 
        print("File open error")                                                                
    return file_text    

Basic requirements: output keyword statistics: to find the keyword and the total number, you can use the matching method to match the keyword. If you find it, it will be marked with + 1. Use the same method to traverse the code and cycle until the end. However, this will be slow and time complexity is high. I think the dictionary function can be used to store the keyword, but I haven't learned the dictionary, so I can only use Li first St (list).

def key_word(key_list):                                                                       
    key_word0= {'else if', 'char', 'double', 'enum', 'float', 'int', 'long',                  
                'short', 'signed', 'struct','void', 'for', 'do', 'while', 'break',            
                'continue',  'union', 'unsigned', 'const', 'sizeof', 'typedef', 'volatile' ,  
                'if', 'else', 'goto', 'switch', 'case', 'default', 'return', 'auto', 'extern',
                'register', 'static'                                                          
                }    #Create a dictionary that contains all c + + built-in keywords                                                       
    key_num= 0                                                                                
    for word in key_list:                                                                     
        if word in key_word0:                                                                 
            key_num += 1                                                                      
    return key_num   # Returns the total number of C and CPP keywords in the file                                                       

Advanced requirements: output several groups of switch case structures, and output the number of corresponding cases of each group: matching principle. The specific mark can be determined by one of the following switches. This method is the same as above.

 def case_num(ed_key_list):# Cases can only be counted if there is a switch in the file                                                                     
    length = len(ed_key_list)                                                                                      
    if (ed_key_list.count('switch',0)):#Find the number of switch es from index 0, and execute when found                                                     
        num_case = []                                                                                              
        for i in range(length):                                                                                    
            if ed_key_list[i] == 'switch':                                                                         
                if (i + 1) == length or ed_key_list[i + 1] != 'case':  #switch is the last element in the list or the next position is not a case                  
                elif i < length - 1:  #switch is the middle element of the list                                                               
                    flag = 1  #Tag find time step                                                                             
                    if ed_key_list[i + flag] == 'case': #The next position is case                                                 
                        case_cnt = 0  #Number of marked case s                                                                    
                        while i + flag < length: #Search in the list elements to avoid crossing the boundary and the system reports an error                                               
                            if ed_key_list[flag + i] == 'case':                                                    
                                case_cnt += 1                                                                      
                                flag += 1                                                                          
        return num_case                                                                                            
        return [0]                                                                                                          

Higher requirements: there are several groups of if and else structures in the output. The ultimate requirements: there are several groups of if, else, if and else structures in the output: at the beginning, I had no idea about this. Later, I looked at the homework submitted by the bosses in front and was inspired (in other words, after seeing it, I thought it could be solved by stack. The idea behind it is not mine, but I think I can't find other ideas, so I have to think about it.) : find the if else and if else if structures. You can use the stack. First, define if for 1, else for 2, and else for 3. Every time you encounter an if, the stack will press a 1. Every time you encounter an else, you will press a 3. When you encounter an else, you will start to delete circularly until the deletion stops at 1. If there are 3 in the deleted number, this is an if else if structure. If there is no 3, it is an if else structure , the push in number and delete number can be completed by append and pop. I Baidu how to use stack in py. Unfortunately, I can't do this, emo.

def count_if_else(key_list):
    count_if_else = 0
    count_if_else_if = 0
    if l[0] == 'i' and l[1] == 'f':#Judge if
    if l[0] == 'e' and l[4] == 'i':#Judge else if
    elif l[0] == 'e' and l[1] == 'l':#Judge else
        while stack[-1] != 1:
            if stack[-1] == 3:
        if flag == 0:
            count_if_else += 1
        if flag == 1:
            count_if_else_if += 1
    temp= list([count_if_else,count_if_else_if])
    list_key = temp
    return list_key

unit testing

Test one

Test two

Test three

Test four

performance testing

Difficulties encountered

The first is the construction of functions, file input and if_else_if search. These two are very difficult for me. At the beginning, I really didn't understand how to realize the input of files. It's difficult to choose whether to input by line, cycle through or read directly, and I won't do this. The main reason is that there is less code, and the debt owed in the past has borne its own consequences. Later, I consulted the online materials and materials The explanation of friends can barely be realized. I used the stack for the if_els_if structure, and specially went to the rookie tutorial and Baidu to find the usage. What suffocated me was that there was no result output. Give up and listen to other people's solutions later.
The second is python's functions. There are many functions in it, but it's troublesome to remember. You need to go to Baidu and know its specific usage. However, the algorithm to realize the function is still much simpler than c + +. It is possible that the function realized by c + + in ten lines only needs one line. At the beginning of self-learning py, there are still many unclear places, such as the relationship between dictionaries, lists and collections The application scenario needs to be explored slowly in the later time.
Then there is the performance test. I haven't found the test on pycharm for a long time (people split). I haven't figured it out according to the steps of the online tutorial. The tutorial has a configuration step. I don't understand the later ones. I still need to find them myself slowly.
Finally, there is the GIT with many holes. After downloading git, I read the practical tutorial, followed it and forgot it next time. There are a lot of bug s when uploading files. The most uncomfortable thing is the SSH key. I can't push when uploading files. I went to Baidu to know that the SSH key is not configured. It's not easy to find the key in the GIT command. A small part was missed when copying, and it took a lot of money later It's only after checking that we know. After SSH is completed, we still can't push. After checking the data, we know that the local file library branch is not connected with the remote library branch. After solving these problems, we can finally upload it.


Quite a lot of knowledge has been learned. The use of git, the basic knowledge and practice of python, and the gradual deepening of understanding in the process of tapping the code are simpler, faster and easier to operate than c + +. Some functions can be roughly used, such as the basic functions of count, endswitch and startswitch.
When I finished my homework, I also learned to formulate my own code specification, the production of PSP table and the planning of time.

Keywords: Python regex

Added by hanwei on Tue, 21 Sep 2021 13:17:52 +0300