22!! 2021-09-23 22:33 采纳率: 0%
浏览 13

EE308 LAB2

 

The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/600798588
The Aim of This AssignmentCode personally & learn git and github & learn the process of writing a project & learn unit test and performance test
MU STU ID and FZU STU ID19103239 & 831901306

 

PSP Form

Personal Software Process StagesEstimated Time/minutesCompleted Time/minutes
Planning6060
Estimate120200
Development--
Analysis6060
Design Spec5070
Design Review3040
Coding Standard3030
Design4060
Coding400500
Code ReviewPlanning7080
Test3040
Reporting--
Test Report4045
Size Measurement2020
Postmortem&Process Improvement12090
total10701295

Select the language

  When I first saw this topic, I had no idea at all. I had not written the code for too long. I just examined the topic for more than half of the time, and finally understood it. I was going to start with C, but I felt I had forgotten all about it and couldn't write it. I thought it would be better to write in Python, so I decided to start writing in Python.I used PyCharm to complete the work, but I forgot a lot of the content, so I learned some python knowledge by myself first.

Topic request

Basic requirements: Output keyword statistics
Advanced requirements: Several groups of switch case structures are output, and the number of cases corresponding to each group is output
High requirements: The output has several sets of if else structures
Final requirement: The output has several if, else if, else structures

Ideas of solving the problem


My idea is to turn those keywords into a list, and then read the text, the text import, then read one line, use the replace function use Spaces instead of those special symbols such need to consider only those letters went, again with an empty list with a split function put these letters in the inside. Because it has been too long without use, I first reviewed the use of files and some file functions, as well as the use of dictionaries.


Python file manipulation

🔗https://blog.csdn.net/weixin_40555670/article/details/78963801

The use of dictionaries in Python

🔗https://blog.csdn.net/csdnbian/article/details/113246009

If the element in the dictionary exists in the keyword list, it will be saved in the dictionary, and the key value of the key plus one, otherwise delete the key, and finally use the for loop to count the number of key words; 

Calculate the keywords number

def Get_keyword_num():
    key_word=["auto","break","case","char","const","continue","default","do",
              "double","else","enum","extern","float","for","goto","if",
	          "int","long","register","return","short","signed","sizeof","stastic",
	          "struct","switch","typedef","union","unsigned","void","volatile","while"]
    num = 0
    store={}
    file = open("code.txt", "r", encoding='utf-8')
    while(1):
        lines=file.readlines()
        for line in lines:
            ine = line.replace(",","").replace(".","").replace("{"," ").replace("}"," ").replace(":","").replace(";","").replace("?","").replace("("," ").replace(")"," ")
            count = line.split()
            for word in count:
                if len(word) < 2:
                    continue
                else:
                    store[word] = store.get(word, 0) + 1
        for key in list(store.keys()):
            if key not in key_word:
                del store[key]

        if not lines:
            break

        for key in store:
            if key in key_word:
                num = num + store[key]  

        print("total num: ", num)
        print("switch num: ", store['switch'])
        file.close()
        return

Calculate switch and case numbers

Consider switch-case. Therefore, every time I find a switch or case, I will save it in a newly created list. When I encounter a switch, I will continue to use sum to count the number of switches

def Get_switchcase_num(count):
    store = []
    store_1=[]
    store_2=[]
    for word in count:
        if word == "switch":
            store.append(word)
        if word == "case":
            store.append(word)
    return store
    cun =0
    sum = 0
    store_1.reverse()
    for word in store_1:
        if word == "case":
            cun = cun+1
        if word == "switch":
            if cun != "0":
                store_2.append(cun)
            cun = 0
            sum = sum + 1
    store_2.reverse()

When counting the number of cases, we also use a new list to save the number of cases in each switch-case structure. Here I take into account the convenience of counting, first I reverse the list of switch-case, count the number of cases first. Then, when the switch encounters, if the number of cases is not equal to 0, a list will be used to count the number of cases, and then the list of case numbers will be reversed in the final output. So we're waiting for the number of cases

Calculate if-else and if-elseif-else number

This is the part that I have been thinking about for the longest time. At the beginning, I also considered using the method of finding switch-case, but it didn't work. In this case, I would overcount the number of ifelse, and then I came up with the idea of checking whether there are three keywords, if, else, if and else, in each line. Using.find(), we create a new list to store the found if, else if, else, keywords, and if we find if, we mark 0 to store it in the list, if we find else if 1 to store it in the list, and if we find else 2 to store it in the list. But it's worth noting that elseif should be used first to ensure that the following data is correct.

num=[]
line_1 = " ".join(count)
            if line_1.find("else if") != -1:
                num.append('1')
            elif line_1.find("if") != -1:
                num.append('0')
            elif line_1.find("else") != -1:
                num.append('2')
            else:
                continue

Then determine if there is if-else, if-elseif-else in the list. I'm cheating here by default by saying that all if and else in this file have a unique match. So after you find the if-else statement, you find the number of else statements, and then you subtract the number of if-else statements to get the number of if-else statements.

    num_1 = 0
    num_2 = 0
    for x in range(len(num)):
        if num[x] == '0' and num[x+1] == '2':
            num_1 = num_1 +1
        if lay_1[x] == '2':
            num_2 = num_2 + 1

GitHub repository code

🔗https://github.com/hzh22/lab

Test file:


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

Accuracy and performance measurement

This is the output from the sample

The import cProfile was used for performance measurement

Summarize this assignment

This assignment is more difficult for me. I took one look at it and gave it up and did it the next day. During the experiment, I procrastinate because I couldn't write code many times. The code in this assignment has not been written for a long time, so it is not standardized or simple, and it has not been seriously reduced the number of code and improved the accuracy of the code. I myself was a little afraid of the difficulty in this homework. I felt it was very difficult shortly after the homework was released. At the beginning, I felt that I could not do it, but WHEN I did it, I referred to some other codes and saw that others could do it.

This experiment took me a lot of time, especially the last question took me a lot of time to think and look up. However, even after all this time, there are still many shortcomings in the code. I hope I can make progress through this software engineering.

 

 

  • 写回答

1条回答 默认 最新

  • 二老孟 2022-11-04 17:15
    关注

    ...

    评论

报告相同问题?

问题事件

  • 创建了问题 9月23日

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试