m0_61790062 2021-09-23 23:06 采纳率: 0%
浏览 8

E308 LAB2

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 AssignmentTo achieve a program function, it can extract keywords of different levels from the C or C++ code files that are read in.
MU STU ID and FZU STU ID19104669_831901311

Here is the GitHub repository link for this experiment:

https://github.com/hs1215/hs1215/tree/main

I.PSP Form

PSPEstimated time(min)Time(min)
Planning  
Estimate3030
Development  

Analysis

4560

Design Spec

7590

Design Review

1010

Coding Standard

6060

Design

6060

Coding

10001200

Code Review

3060

Test

3060
Reporting  

Test Report

3030

Size Measurement

2030

Postmortem&Process  Improvement

6060
total14501750

II.Description of Problem-Solving Ideas

Program Requirements:

1..Read in a c/c++ code file.

2. Basic requirement: output "keyword" statistics

3. Advanced requirement: output the number of "switch case" structures, and output the number of "case" corresponding to each group

4. Uplifting requirement: output the number of "if else" structures

5. Ultimate requirement: output the number of "if, else if, else" structures

Use the fopen function to open a file and read its contentsCombine letters in the file into words, check whether they are keywords, and count the number of keywordsNumber of switch case structures. The number of switches and cases is traversed using switches as the markIf-else and if-elseif-else structures are counted using a stack, referring to the parenthesis matching we did earlier.Count a string, iterate over the string "if" or "elseif" and push it onto the stack. If it's "else" and the stack is not empty, (1) the top element of the stack is "if",Calculate an if-else structure and delete this element;(2) If the top element of the stack is "elseif", delete the top element of the stack until the top element of the stack is "if", calculate an if-elseif-else structure.

 

III.Design and Implementation Process

1. Read files

   fp = fopen(file_path,"r");
   while((c = fgetc(fp))!=EOF){

2. Total number of keywords and number of Switch cases

 fp = fopen(file_path,"r");
   while((c = fgetc(fp))!=EOF){
   	  if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
   	  	str+=c;
	  }
	  else{
	  	if(str!=""){
		  for(i=0;i<35;i++){
	  	   if(str==a[i]){
	  		 cnt++;
	  		 if(i==25){
		   	   cnt1++;
		   	   if(s.empty()){
		   	     s.push('s');
		   	    }
		     }
		     if(i==2&&s.top()=='{'){
		     	cnt2++;
			 }
		   }
	      }
	      str="";
	    }
	    else{
	    	if((c=='{')&&(!s.empty())){
	          s.push(c);
			}	
			if((c=='}')&&(!s.empty())){
				s.pop();
				s.pop();
				ss[j++]=cnt2;
		   	    cnt2=0;
			}
		}
      }
      
  }
   cout<<"total num: "<<cnt<<endl;
   cout<<"switch num: "<<cnt1<<endl;
   cout<<"case num: ";
   for(i=0;i<j;i++){
   	cout<<ss[i]<<" ";
   }
   cout<<endl;
   fclose(fp);

3. The number of if else and if elseif else constructs

  fp = fopen("data.txt","r");
   while((c = fgetc(fp))!=EOF){
      if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
   	  	str+=c;
      }
     else{
	    if(str!=""){
	       if(str=="else"&&c==' '){
		  continue;
               }
	       else{
		  ans[l++]=str;
		  str="";
               }
           }
       }
   }
   int g=0,b=0;
   stack<string> m;
   for(i=0;i<l;i++){
   	 if(ans[i]=="if"||ans[i]=="elseif"){
   	 	m.push(ans[i]);
	 }
	 if(ans[i]=="else"&&!m.empty()){
	 	if(m.top()=="if"){
	 		g++;
	 		m.pop();
		 }
		if(m.top()=="elseif"){
			while(m.top()=="if"){
				m.pop();
			}
			b++;
			m.pop();
		}
	 }
	 
   } 
   cout<<"if-else num: "<<g<<endl;
   cout<<"if-elseif-else num: "<<b<<endl;
   fclose(fp);

 

Test

Output

 

Summary

  Way to complete this assignment is a little difficult, their thinking is a bit better, I haven't got the code, a code card, in the process of solving the a point, and stuck behind, the code is the most uncomfortable part of into dead circulation, see code feel have no problem, but that's not a test, spend a lot of time looking at the wrong is infinite loop problem is discovered,I'm glad we took care of the rest.
  The biggest gain of this assignment is that I have learned some new knowledge, such as how to use some new functions and how to use Github and Git.

  Secondly, this assignment also made me realize my shortcomings, such as algorithm problems. I feel that the code can be optimized but the ability is limited, so I still need to work hard.

 

 

 

 

 

 

 

  • 写回答

1条回答 默认 最新

  • 「已注销」 2021-09-24 11:01
    关注

    请问你发的这些是要解决什么问题?

    评论

报告相同问题?

问题事件

  • 创建了问题 9月23日

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题