EE308 Lab1_2

The Link Your Class

https: https://bbs.csdn.net/forums/MUEE308FZ?category=0

The Link of Requirement of This Assignment

https:https://bbs.csdn.net/forums/MUEE308FZ?category=0

The Aim of This Assignment

① Learn git and Github; Create a Github repository. ③ Code writing; ④ Update and optimize the code.

MU STU ID and FZU STU ID

eg. 20122543_832001317



目录

  PSP form for the work

 The preparatory work

Design and implementation process

Code Description 

Unit test

Summary 


  PSP form for the work

Personal Software Process StagesEstimated Time/minutesCompleted Time/minutes
PlanningPlanning3045
Estimate1515
Development--
Analysis2030
Design Spec4040
Design Review3040
Coding Standard2025
Design4060
Coding100120
Code Review5050
Test4035
Test Report80110
Size Measurement2010
Postmortem & Process Improvement Plan5045
Total535625


To see more detailed code go to:https://github.com/Mrhc159/EE308lab1.git 

 The preparatory work

I chose C++ to complete this project.


Thinking about problems and solutions

Task 1:Finds output "keyword" statistics, finds the keyword and counts it.

Solution:First create the text and enter the keywords; Then read the text, delete the text symbols and Spaces, and count the number of keyword occurrences.

Task 2:Finds the number of "switch case" structures, and the number of "case" corresponding to each group.

Solution:The input file is traversed, and the if-else is used to count the total number of switches and corresponding cases.

Task 3:Count the number of "if else" structures and "if, else if, else" structures.

Solution:To use the stack method, use the find function to find the "else if" and put it on stack 2; if "if" is found, it is placed on stack 1. If there is an "else", take if_else_num++ from stack 1. The same goes for "if-else-if". However, due to the lack of in-depth learning and understanding of this method, I have limited ability to write code by myself. Check the following information:

http://t.csdn.cn/3YbwY

http://t.csdn.cn/j66df

http://t.csdn.cn/wm0mB

http://t.csdn.cn/NwDQs



Design and implementation process

Task 1:                                                                 Task 2:

 Task 3:



Code Description 

Task 1:

(1)Open the target file

int main(){
	ifstream infile;
	infile.open("lab2.txt");

(2)Builds strings and keywords

	int total=0;
	int switch=0;
	int case=0;
	string keyword[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"};
	string keyword1[]={"switch"};
	string keyword2[]={"case"}; 
    string keyword3[]={"if"};
    string keyword4[]={"else"};

 (3)Delete Spaces and characters

while(getline(inFile,str)){
	str1=replace_all(str, "{", " ");
	str1=replace_all(str, "}", " ");
	str1=replace_all(str, "(", " ");
	str1=replace_all(str, ")", " ");
	str1=replace_all(str, ";", " ");
	str1=replace_all(str, ":", " ");
	str1=trim(str);
	stringstream input(str1);

 (4)Count the number of keywords by traversing the text

for(int j=0;j<100;j++){
		if(d==keyword[j]){
			tot+=1;

Task 2:

The length of the string is obtained, and then the string is traversed. When any letter in "switch" appears, the number is increased by one, while skipping four characters to continue traversing the string. "Case" in the same way.

l=str.length();//获取字符串长度
for(int m=0;m<l;m++){
	if(str[m]=='s'&&str[m+1]=='w'&&str[m+2]=='i'&&str[m+3]=='c'&&str[m+4]=='h')
	{
		switch+=1;
		m+4;
	}
	
}
for(int n=0;n<l;n++){
	if(str[n]=='c'&&str[n+1]=='a'&&str[n+2]=='s'&&str[n+3]=='e')
	{
		case+=1;
		n+3;

	}
	
}

  Task 3:

Count "if else","if,elseif, else" in a string using the stack method.

if(str.find("else if")!=string::npos){//string::npos表示直到字符串结束
	istack.push(2);//此时将"else if"压入堆栈2的栈顶
}else if(str.find("if")!=string::npos){
	istack.push(1);//此时将"if"压入堆栈1的栈顶
}else if(str.find("else")!=string::npos){
	if(istack.top() == 1){//".top()"返回栈顶元素(从堆栈1中取出)
		istack.pop();//".pop()"删除栈顶元素
		if_else_num++;
		continue;
	}
	while(istack.top() == 2) {
		istack.pop();
	}
	istack.pop();
	if_elseif_if_num++;
}

Unit test

Summary 

Through this experiment, I not only became more proficient in the use of github, but also strengthened my ability to write code. In the process of solving the problem, difficulties are inevitable, in the inquiry of a lot of relevant information, combined with the actual operation, many problems will be readily solved. I also learned a lot of new knowledge in it. I hope I can continue to work hard and persist in the future.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值