EE308 LAB2

yeelanshan 2021-09-23 15:25:38
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/600798588
The Aim of This AssignmentAchieve the program function
MU STU ID and FZU STU ID19103310_831901314

GitHub Repository:    https://github.com/lanshannn/EE308_LAB2

1. PSP FORM

Personal Software Process StageEstimated Time/hourReal Time/hour
Planning0.50.5
Estimate23
Development11
Analysis412
Design Spec1012
Design Review57
Coding Standard57
Design55
Coding1228
Code Review11
Test23
Reporting11
Test Report12
Size Measurement--
Postmortem&Process Improvement0.50.5
Summary5083

 

2. Problem-Solving Ideas

  In this lab, I chose C++ to extract different levels of keywords from the read C or C + + code files. In my opinion, we can determine the number of required keywords through the creation of string array and operations which includes erase(), replace(), find() and so on.

3. Design and Implementation Porcess

4. Code Description

  (1)I store the 32 keywords to be identified into an array and I read the contents of the code file line by line through fstream and getline(). Then I use the find () to judge whether each line of code read contains the keywords existing in the array.

string key[32]={"auto","break","case","char","const","continue","default","double","do",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"};
// store the 32 keywords into an array
ifstream out("test.cpp");
	while (!out.eof()){
		getline(out,str);
		// read the file line by line
		for(int i=0;i<=32;i++){
			string::size_type position;
			position=str.find(key[i]);
			if(position!=str.npos){
				total_num++;
				if(i==25){ // find the keyword "switch"
					switch_num++;
					case_num[switch_num]=0;
				}
				if(i==2){ // find the keyword "case"
					case_num[switch_num]++;
				}
				if(i==7){ //double & do will repeat
					total_num--;
				}
			}
		}

  (2) We need to achieve the if_else_num and if_else_if_num. This is difficult for me and I often found my code will out of range in the process of replace the string. At first, I change "else if" into "elif" by erase().

while (!out.eof()){
		getline(out,str);
		string strr="else if";
		string::size_type idx;
		idx=str.find(strr);
		if(idx!=str.npos){
			str.erase(str.find(strr)+2,3);
		}

  (3) Then in the loop, we extract all "else" "if" "else if (elif)" and save them as an new array "data".

string m[3]={"if","else","elif"};
for(int i=0;i<3;i++){
	string::size_type id;
	id=str.find(m[i]);
	if(id!=str.npos){
		data.append(m[i]);
		number++;
	}
}

  (4) Now we will find the number of "if_else_if_num" is equal to the number of discontinuous "elif". After determining the "if_else_if_num", we also use erase to delete all "elif" from this new array "data".

for(int i=0;i<number;i++){
	string::size_type pos;
	pos=data.find("elif");
	//cout<<pos<<endl;
	if(pos!=data.npos){
		if_else_if_num++;
		total_num++;
		data.erase(data.find("elif"),4);
		if(data[data.find("elif")]==data[data.find("elif")+4]){
			if_else_if_num--;
		}
	}
}

  (5) We determine the "if_else_num" in data by finding the number of "else" minus the number of "elif".

for(int i=0;i<number;i++){
	string::size_type poss;
	poss=data.find("else");
	if(poss!=data.npos){
		if_else_num++;
		data.erase(data.find("else"),4);
	}
}

  (6) Finally, we can print the output.

if(level>=1) {
	printf("total num:%d\n", total_num);
	if(level>=2) {
	    printf("switch num:%d\n", switch_num);
		printf("case num:");
		for(int i=1; i<=switch_num;i++){
			printf("%d", case_num[i]);
			if(i==switch_num){
				printf("\n");
			}
			else{
				printf(" ");
			}
		}
		if(level>=3) {
			printf("if_else num:%d\n", if_else_num-if_else_if_num);
			if(level==4) {
				printf("if_else_if num:%d\n", if_else_if_num);
			}
		}
	}
}

      The output results as following:

 

5. Unit test screenshots and description

 

6. Unit test coverage optimization and performance testing, performance optimization

7. Summary

  In this assignment, I spent a long time to overcome the difficulties, and finally basically completed the project requirements.

  In the process of completion, I learned the complex operations of stack (although it not be used) and string. In addition, I tried to count the number of parentheses or stack to solve the problem of determining "if_else_num" and "if_else_if_num". Besides, I tried to use dev C++, Visual Studio and GCC to test the coverage of the code, and optimized it on the basis of the test results. It all had trained my ability to build complex programs.

 

...全文
208 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

183

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程 教学
软件工程 高校
社区管理员
  • 单步调试
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧