EE308 Lab1-2
| 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 | Write code that can extract keywords of different levels from the C or C++ code files that are read in. |
| MU STU ID and FZU STU ID | 20122764_832001319 |
目录
3.Design and implementation process
Github Website Link
1.PSP Table
| Personal Software Process Stages | Estimated Time(minutes) | Completed Time(minutes) |
|---|---|---|
| Planning | 20 | 30 |
| Estimate | - | - |
| Development | 30 | 50 |
| Analysis | 200 | 300 |
| Design Spec | 10 | 20 |
| Design Review | 10 | 20 |
| Coding Standard | 10 | 20 |
| Design | 20 | 30 |
| Coding | 150 | 220 |
| Code Review Planning | 20 | 30 |
| Test | 10 | 15 |
| Reporting | 10 | 15 |
| Test Report | 10 | 10 |
| Size Measurement | 10 | 10 |
| Postmortem&Process Improvement | 20 | 20 |
| Total | 530 | 790 |
2.Problem-solving ideas
Program Requirements
- Basic requirement: output "keyword" statistics
- Advanced requirement: output the number of "switch case" structures, and output the number of "case" corresponding to each group
- Uplifting requirement: output the number of "if else" structures
- Ultimate requirement: output the number of "if, else if, else" structures
Before completing the more difficult requirements, you need to complete the Lower requirements.
Because I am familiar with c++, I decided to use c++ to solve this task. This project requires us to complete four levels of tasks. First, we had to input the file and preprocess the file, then step through each level of code, and finally merge it to implement the functionality.
3.Design and implementation process

4.Code description
Read the file
First,I store the keywords in an array,then I use ifstream to read the files, and after that I store them in a vector.
const string keywords[] = {"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"};
vector<string> file_content;
void file_read(string filename){
ifstream inFile;
string line;
inFile.open(filename.c_str());
if (!inFile.is_open()){
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
} // check for successful open
while (getline(inFile, line)) {
file_content.push_back(line);
}
inFile.close(); //close the file
}
Delete the useless contents
To prevent possible keywords in the comment statements and strings from interfering with the extraction results, we need to remove the statements and strings in the comments. Here I use the find() and substr() functions.
void delete_useless_content(){
string Line,words;
int index;
size_t tabs_location,annotation_location,quot_location,star1_location,star2_location;
for(int i=0;i<file_content.size(); i++){
Line=file_content[i];
if(Line.find("\t")!=Line.npos&&Line.find("\t")==0){
do{
tabs_location=Line.find("\t")+1;
Line=Line.substr(tabs_location,Line.length()-tabs_location);
}while(Line.find("\t") != Line.npos && Line.find("\t") == 0);
file_content[i] = Line;
}
if (Line.find("//") != Line.npos) {
annotation_location=Line.find("//");
if (annotation_location==0) {
file_content.erase(file_content.begin()+i);
i--;
} else {
words = Line.substr(0,annotation_location);
file_content[i] = words;
}
}else if(Line.find("\"") !=Line.npos){
size_t temp[50]={0};
tabs_location=0;
index=0;
while ((quot_location=Line.find("\"",tabs_location)) != Line.npos) {
temp[index]=quot_location;
index++;
tabs_location=quot_location+1;
}
temp[index]=Line.length();
words= Line.substr(0,temp[0]);
for (int j=1; temp[j]!=0; j+=2) {
words += Line.substr(temp[j]+1,temp[j+1]-temp[j]-1);
}
file_content[i]=words;
} else if(Line.find("/*") != Line.npos){
star1_location=Line.find("/*");
star2_location= Line.find("*/");
if (star2_location!= Line.npos) {
if (star1_location== 0) {
file_content[i] = Line.substr(star2_location+2,Line.length());
} else {
file_content[i] = Line.substr(0,star1_location);
}
i--;
}else{
file_content[i] = Line.substr(0,star1_location);
i += 1;
Line = file_content[i];
while (Line.find("*/") == Line.npos) {
file_content.erase(file_content.begin()+i);
Line = file_content[i];
}
star2_location = Line.find("*/")+2;
file_content[i] = Line.substr(star2_location,Line.length()-star2_location);
}
}
}
}
Judge keywords
To prevent incorrect identification of keywords in non-keyword fields, check whether the keywords are independent. I use ASCII code to differentiate.
bool independence_judge(string line,long i){
if (line[i] < 48 || (line[i] > 57 && line[i] < 65) || (line[i] > 90 && line[i] < 97) ||line[i] >122) {
return true;
} else {
return false;
}
}
bool involve_judge(string line, string word){
size_t word_location;
word_location=line.find(word);
if (word_location == 0) {
if (independence_judge(line, word.length()) || word.length() == line.length()) {
return true;
} else {
return false;
}
} else {
if (independence_judge(line, word_location-1) && (independence_judge(line, word_location+word.length()) || word_location+word.length() == line.length())) {
return true;
} else {
return false;
}
}
}
Calculate the keywords number
void cal_keywords_num(){
int keywords_num=0;
string line;
for (int i = 0; i<file_content.size(); i++) {
line = file_content[i];
for (int j=0; j<32; j++) {
size_t keywords_location = line.find(keywords[j], 0);
while (keywords_location!=line.npos && involve_judge(line,keywords[j])) {
keywords_num++;
keywords_location = line.find(keywords[j],keywords_location + 1);
}
}
}
cout<<"total num: "<<keywords_num<<endl;
}
Calculate switch and case numbers
void cal_switch_case_num(){
int switch_num = 0,last = -1,case_num[200]={0};
string line;
for (int i = 0; i<file_content.size(); i++) {
line = file_content[i];
if (line.find("switch") != line.npos && involve_judge(line, "switch")) {
switch_num += 1;
last += 1;
}
if (line.find("case") != line.npos && involve_judge(line, "case")) { /*²éÕÒcase*/
case_num[last] += 1;
}
}
cout<<"switch num: "<<switch_num<<endl;
cout<<"case num:";
for (int j = 0; j<=last; j++) {
cout<<" "<<case_num[j];
}
cout<<endl;
}
Calculate if-else and if-elseif-else number
void cal_if_else_num(int level) {
int if_else_num = 0,if_else_if_else_num= 0;
stack<int> s;
string line;
for (int i = 0; i<file_content.size(); i++) {
line = file_content[i];
if (line.find("if") != line.npos && line.find("else") == line.npos && involve_judge(line, "if")) {
s.push(1);
} else if (line.find("if") == line.npos && line.find("else") != line.npos && s.empty() == false) {
if (s.top() == 1) {
if_else_num++;
} else {
if_else_if_else_num++;
}
s.pop();
}else if (line.find("if") != line.npos && line.find("else") != line.npos && involve_judge(line, "if")) {
s.push(2);
}
}
if (level == 3) {
cout<<"if-else num: "<<if_else_num<<endl;
} else if (level == 4) {
cout<<"if-else num: "<<if_else_num<<endl;
cout<<"if-else-if num: "<<if_else_if_else_num<<endl;
}
}
5.Test
Level 1:

Level 2:

Level 3:

Level 4:

6.Performance analysis
The software I used has no performance test function, so this part was not carried out in this experiment.
7.Summary
Through this task, I mastered the use of CSDN and GitHub, and had a certain understanding of blog. I also learned some new codes by myself and got familiar with the task flow. I think this practice will enable me to better control the project schedule in future team projects.
相关推荐
EE-308 LAB4 final version.7z
这是本次实验的压缩包
技术转移联合体在跨境技术转移中面临哪些合规要求和标准限制?.docx
技术转移联合体在跨境技术转移中面临哪些合规要求和标准限制?
省级科技管理部门在制定技术交易补贴政策时,通常优先支持哪类平台?.docx
科易网(厦门科易网科技有限公司)创立于2007年,是技术转移与科技创新数智化服务平台,拥有国家科技成果转化服务示范基地、国家技术转移示范机构等资质。平台以“AI+科技成果转化”为核心,整合成果、专利、专家、政策等多维数据,构建知识图谱与科创智能体,面向企业、政府、高校院所提供成果转化、技术需求对接、产业服务等数智化解决方案。截至目前,已服务全国企业超43万家,推动落地项目11800余项,总金额逾80亿元
水泥稳定碎石基层施工安全交底要点(含现场管控项).docx
水泥稳定碎石基层施工安全交底要点(含现场管控项)
普通职员月度工作总结知识笔记(结构+话术+案例).docx
普通职员月度工作总结知识笔记(结构+话术+案例)


