这里写自定义目录标题
The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907 |
The Aim of This Assignment | learn how to use git and submit codes , learn how to use unit test and performance test and extract keywords of different levels from the C or C++ code files that are read in. |
MU STU ID and FZU STU ID | 20123434/832001102 |
My github is here:https://github.com/yycgod123/EE308-Lab1-2
1.PSP
Personal Software Process Stages | Estimated Time/minutes | Completed Time/minutes |
---|---|---|
Planning | 10 | 10 |
Estimate | 20 | 20 |
Developmemt | None | None |
Analysis | 160 | 300 |
Design Spec | 50 | 50 |
Design Review | 20 | 30 |
Coding Standard | 10 | 10 |
Design | 200 | 300 |
Coding | 600 | 800 |
Code ReviewPlanning | 40 | 45 |
Test | 100 | 150 |
Reporting | 80 | 100 |
Test Report | - | - |
Size Measurement | 10 | 10 |
Postmortem&Process Improvement | 120 | 200 |
total | 1420 | 2025 |
2.Description of problem-solving ideas
In this lab, I decided to test the code in Python language. The first thing I’m going to do is use Python to filter out the symbols that I don’t want in the code I’m testing.[“+”, “-”, “*”, “/”, “<”, “<=”, “>”, “>=”, “=”, “==”, “!=”, “;”, “(”, “)”, “^”, “,”, “”“, “'”, “#”, “&”,&&”, “|”, “||”, “%”, “~”, “<<”, “>>”, “[”, “]”, “{”, “}”, “\”, “.”, “:”, “!”] ,so I will remove them before my testing.The next thing I need to do is write the code I’m going to test to a file and let Python read it, which makes it easier for me.After Python read the file, I removed all the symbols I didn’t need and used Python’s split function to change the article into English words, which was more convenient for my later code writing.For the first question, it is easy to collect all the keywords in C++, put them into a list, and then match the previously divided English words. In this way, it is easy to read out the total number of C++ keywords in the code.The same is true for the second problem, and a similar approach can be used.
For the third problem, I’m going to create a list, put the words I want in it, and have them go to the list in order. This can be solved by reusing the logic code.
I thought about the fourth question for a long time, and then I came up with the method of list deletion, as long as I can find a loop in the constant reading, delete the duplicate to find the if-elseif-else structure
Instead of a single pass, I think the parser should be designed as a subroutine. Whenever the parser needs a word symbol, it passes an input string to the parser, and the parser can analyze the words in the input string.
3.Design and implementation process.
4.Code description.
def get_file():#get the file we want to get
f = open(filename,'r',encoding='UTF-8')
file = f.read()
#Only leave the content we want to leave
symbol_list = ["+", "-", "*", "/", "<", "<=", ">", ">=", "=", "==",
"!=", ";", "(", ")", "^", ",", "\"", "\'", "#", "&",
"&&", "|", "||", "%", "~", "<<", ">>", "[", "]", "{",
"}", "\\", ".", ":", "!"] #the symbols in C++
for i in symbol_list:
file = file.replace(i," ")#replace unwanted symbols
f.close()
return file
In this code,we can read from the test code and transform it into a file which is convinent for us to work on the following steps.
And this is the result.It is expected to what I think.Very great!!!😊
word_dict = {}
word_list = []
filename = input("Please input the filename you want to test:")#input the filename you want to test
reserveWord = ["auto", "break", "case", "char", "const",
"continue", "default", "do", "double", "else",
"enum", "extern", "float", "for", "goto",
"if", "int", "long", "register", "return",
"short", "signed", "sizeof", "static", "struct",
"switch", "typedef", "union", "unsigned",
"void", "volatile", "while", "elseif"]#reserveWord in C++
def get_file():#get the file we want to get
f = open(filename,'r',encoding='UTF-8')
file = f.read()
#Only leave the content we want to leave
symbol_list = ["+", "-", "*", "/", "<", "<=", ">", ">=", "=", "==",
"!=", ";", "(", ")", "^", ",", "\"", "\'", "#", "&",
"&&", "|", "||", "%", "~", "<<", ">>", "[", "]", "{",
"}", "\\", ".", ":", "!"] #the symbols in C++
for i in symbol_list:
file = file.replace(i," ")#replace unwanted symbols
f.close()
return file
def get_words():#split the words in order for us to test
file = get_file().replace('else if','elseif')
separator = [r'//.*', r'\/\*(?:[^\*]|\*+[^\/\*])*\*+\/', r'".*"']
for i in separator:
word_list = re.split(i,file)
new_file = ""
for word in word_list:
new_file+=word
word_list = new_file.split()
return word_list
And this code is to help me get the word from the file and I have split the words.This is a very important step. It can help me count.
1.The first level
def first_level():
count = 0
words = get_words()
for i in words:
if i in reserveWord:
word_dict[i] = word_dict.get(i,0)+1
word_list.append(i)
count+=1
count = count+word_dict.get("elseif",0)
return count+1
And above is the idea.We can first split the words.And judge whether it is the keywords in C++.If it is,the number of the count can add one.If not,we ignore it.
2.The second level
def second_level():
new_list = []
count1 = 0
count2 = 0
count_list = []
for i in get_words():
if i=='switch' or i == 'case':
new_list.append(i)
for i in new_list:
if i=='switch':
count1 += 1
return count1
length = len(new_list)
print("case num:",end='')
for i in range(0,length-1):
if ((new_list[i]=='switch') and (new_list[i+1]=='case')) or ((new_list[i]=='case') and (new_list[i+1]=='case')):
count2=count2+1
if new_list[i]=='case' and new_list[i+1]=='switch' or i==length-2:
count_list.append(count2)
count2=0
return count_list
And the second level,we can count switch nums easily,and we can count the case between the two switch,if it is still case,the case num can add one,and if the next one is not case,we must set the count into 0,and count again.
3.The third level
def third_level():
print()
if_list= []
count = 0
for i in words:
if i=='if' or i=='else' or i=='elseif':
if_list.append(i)
length = len(if_list)
for i in range(length-1):
if if_list[i]=='if' and if_list[i+1]=='else':
count+=1
return count
The third level is a little easy.We can create a list contains"if",“elesif”,“else”,and place them in order by the order they appear.
And we can count,if the first word is “if” and the next is “else” we can add one,or we should pass it.
4.The fourth level
def fourth_level():
test_list=[]
count = 0
for i in words:
if i =='if':
test_list.append(i)
elif i == 'elseif' and test_list[-1]!='elseif':
test_list.append(i)
elif i == 'else':
if test_list[-1] == 'elseif':
test_list.pop()
test_list.pop()
count += 1
return count
This question disturbed me a lot time.And I remeber the pop method.I think if I want to count"if-elseif-else"structure,I just need to count one if and the rest elseif I can pop them and only leave one,then I just need to count one else,that is a complete structure.
And above all is four levels solution.
5.display
level = int(input("Please input the level you want to test:"))
def dispaly():
if level == 1:
first_level()
elif level == 2:
first_level()
second_level()
elif level == 3:
first_level()
second_level()
third_level()
elif level == 4:
first_level()
second_level()
third_level()
fourth_level()
else:
print("Please input valid number!")
This is easy.So I just show the result.
5.Unit test screenshots and description
import EE308_lab2
import unittest
class test(unittest.TestCase):
def test_first_level(self):
self.assertEqual(EE308_lab2.first_level(), 35)
def test_second_level(self):
self.assertEqual(EE308_lab2.second_level(),[3,2])
def test_third_level(self):
self.assertEqual(EE308_lab2.third_level(),2)
def test_fourth_level(self):
self.assertEqual(EE308_lab2.fourth_level(),2)
if __name__ == '__main__':
unittest.main()
Nice!All tests are OK!😊🌹
6.Summary
This lab takes me a lot of time and almost drives me crazy!!!🤯But I also learn a lot from this lab,this is an unforgertable experience.I will stay humble and continue to learn the programming language I love.I hope to learn more in future courses.