The Link Your Class | https://bbs.youkuaiyun.com/forums/MUEE308FZU202201 |
---|---|
The Link of Requirement of This Assignment | https://bbs.youkuaiyun.com/topics/608734907 |
The Aim of This Assignment | Keywords Extraction & Counting |
MU STU ID and FZU STU ID | 20124007_832002128 |
EE308 Lab1-2
1.PSP FORM 🧾
Personal Software Process Stages | Estimated Time/minutes | Completed Time/minutes |
---|---|---|
Planning | - | - |
Estimate | 20 | 35 |
Development | - | - |
Analysis | 80 | 90 |
Design Spec | 50 | 55 |
Design Review | 15 | 15 |
Coding Standard | 30 | 20 |
Design | 90 | 80 |
Coding | 1100 | 1200 |
Code Review Planning | 80 | 70 |
Test | 20 | 20 |
Reporting | - | - |
Test Report | 50 | 50 |
Size Measurement | 10 | 10 |
Postmortem&Process Improvement | 120 | 60 |
total | 1665 | 1615 |
2.Problem-Solving Ideas
Programming Language:Python
Because the problem is mainly object-oriented, and I systematically learned object-oriented C++ programming on the basis of Python, so I use Python to do this laboratory.
3.Design and implementation process
we need to know the keywords about C++:
auto | break | case | char | levelst | leveltinue | 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 |
---|
-The file is read in the specified address
-Find the keywords
-Calculate the number of keywords and the times of switches and cases
-Use pointer function probably
4.Design and implementation process
Five modules, each encapsulated in a function, and as the level of completion increases, the implementation of each functional level depends on the implementation of the previous functional level. The diagram below shows how the code is organized, the arrows indicate the level of completion, all the functions defined in the code in the flowchart.
5.Code Description
(1)create a test-file:
The code is as follows :
#include <stdio.h>
int main(){
int i=1;
double j=0;
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)entire code:
The code is as follows :
import numpy as np
KeyWords=["auto","break","case","char","levelst","leveltinue","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"]
def main():
# input
path = input("file path: ")
level = input("level of stastic (from 1 to 4): ")
with open(path) as f: # read a File
code = [line.strip("\n") for line in f.readlines()]
KeyWords_len = 32
cnt = 0
count = np.zeros(KeyWords_len, dtype=int)
for lines in range(len(code)):
str = code[lines]
for i in range(KeyWords_len):
while 1:
if str.find(KeyWords[i]) != -1:
if str.find("//") != -1:
break
else:
pos = str.find(KeyWords[i])
count[i]+=1
str = str[pos+len(KeyWords[i]):len(str)]
else:
break
for i in range(KeyWords_len):
if count[i] != 0:
cnt += count[i]
num = [25, 2, 6, 15, 9]
x = [0, 0, -1]
struc = np.zeros(count[num[0]], dtype=int)
dir = ["else if", "if", "else"]
for lines in range(len(code)):
str = code[lines]
while 1:
if str.find(KeyWords[num[1]]) != -1:
if str.find("//") != -1:
break
else:
struc[x[0]] += 1
break
if str.find(KeyWords[num[2]]) != -1:
if str.find("//") != -1:
break
else:
x[0]+=1
break
if str.find(dir[1]) != -1:
if str.find("//") != -1:
break
else:
x[1]+=1
break
else:
break
divide = np.zeros(x[1], dtype=int)
s1 = 0
s2 = 0
for lines in range(len(code)):
str = code[lines]
while 1:
if str.find(dir[0]) != -1:
if str.find("//") != -1:
break
else:
divide[x[2]]+=1
break
if str.find(KeyWords[num[3]]) != -1:
if str.find("//") != -1:
break
else:
x[2]+=1
break
else:
break
for m in range(x[1]):
if divide[m] == 0:
s2+=1
else:
s1+=1
if int(level) >= 1:
print("KeyWords_len num is: ", cnt)
for j in range(KeyWords_len):
if count[j] != 0:
print(KeyWords[j], " num: ", count[j])
if int(level) >= 2:
if count[num[0]] != count[num[2]]:
print("wrong structure")
else:
print("The number of switch structure is:",count[num[0]])
for k in range(count[num[0]]):
print("switch ", k+1 ," number of case is: ", struc[k])
if int(level) >= 3:
print("the number of if-else: ", s2)
if int(level) >= 4:
print("the number of if-else if-else: ",s1)
default: print("wrong")
if __name__ == '__main__':
main()
(3)output
Summary
Through this experiment homework, I’ve gained a lot:
-How to use Github
-Planning before coding is so important
-Coding in the process of consolidating and reviewing previously learned professional courses.
The knowledge points of the document process need to be further strengthened.