EE308FZ_Lab1-2_832001302_20123108

qq_52981234 qq_52981234 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608745426
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 IDFZU ID : 832001302  MU ID : 20123108

Content

1.PSP Table

2.Description of the problem-solving ideas

3.Design and implementation process

4.Code description

5.Unit test and performance testing

6.Summarize this assignment


1.PSP Table

PSPESTIMATE TIME(MINUTE)ESTIMATE TIME(MINUTE)
Planning4530
 Estimate55
Development3050
Analysis6070
Design Spec1010
 Design Review00
 Coding Standard2015
Design4060
Coding240300
Code Review1015
 Test6080
Reporting\\
 Test Report1020
Size Measurement1010
Postmortem & Process Improvement Plan2050
Total560715

2.Description of the problem-solving ideas

        When I first got this topic, the first thing that came to my mind was what I learned in object-oriented programming last semester about reading and writing C files.

 Requirement 1 : Basic requirement  i.e.  output "keyword" statistics

          For the Requirement 1 : to read the number of keywords in a file, my idea is to use getline to read the file line by line, read all the characters, then compare the read characters to the elements in the keyword array, increment the total number of keywords if included, and finally output the total number of keywords in the file.

Requirement 2 : output the number of "switch case" structures, and output the number of "case" corresponding to each group

         For the Requirement 2 : Actually, step 2 is easier when you've done step 1. After finding the switch, find the next switch and the case between the two switches, count the number of cases between the two switches, use two for loops (i, j), when j encounters the next switch, assign the value to i, and carry out the next iteration, Iterate all the way to the end.

Requirement 3 :  Uplifting requirement  i.e.  output the number of "if else" structures

Requirement 4 :   Ultimate requirement: output the number of "if, else if, else" structures

        I put the Requirement 3 and Requirement 4 together. In fact, I think the two questions are similar, and the answer to both questions can be calculated by an algorithm:

        When I first got this question, my mind was completely blank. I thought about dividing them into basic units. I thought that all complex structures are the combination of basic units, but later I thought it was not feasible.

         Next, it occurred to me that if does not necessarily need to be accompanied by else, but the presence of else and else if must be predicated on the existence of if. This idea gave me the initial enlightenment, and I had an idea of inferring if by else and thus introducing else if, but I did not give it a try either, but this idea gave me the inspiration to look for the relationship between if,else if,else relationship, which helped me to get the final algorithm:

          As with the previous solutions, I want to extract all the if,else, and else if in the file. (NOTE : Make sure that the else and if are not repeatedly extracted while the else if is present. The detailed code will be shown in the following section.)

  • Step 1: if the else is followed by an if, it can be considered an if-else structure. The number of if-else structures is increased by one, and the number of if and else is decreased by one。
  • Step 2: if the else is followed by the else if, then it might be an if-else if-else structure, but at this point we need to count the number of times the else if is repeated, and this is used to count how many times we need to reduce the number of else if (NOTE : if the else if is repeated n times, The else if is reduced by n+1.)
  • Step 3: Leave the first one in the else if of the repeating  unit, and determine whether this else if is followed by an if. if so, it is an if-else if-else structure, and the number of if-else if-else structures is increased by one.
  • Step 4: The rest of the else if must come from the if-else if-else structure, the number of if-else if-else structure is increased by one.

  • Step 5: The remaining if must come from the if-else structure, and the number of if-else structures is increased by one.

3.Design and implementation process

The Flow Chart is shown below

 4.Code description

All code including test file(named file1.cpp) is uploaded on GitHub

Click here to get the code!

Header File and Initialization

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{	
	string text;
	vector<string> nums; 
	string filename;
	string keywords[33]={"auto", "break", "case", "char", "const", "continue",
		"default", "do", "double", "else", "enum", "extern", "float",
		"for", "foto", "if", "int", "long", "main", "register", "return", "short",
		"signed", "sizeof", "static", "struct", "switch", "typeof", "union",
		"unsigned", "void", "volatile", "while"};
	string ifelsekeywords[3]={"else if","if","else"};
	int keywdcnt[33] = {0};
	int ifelsekeywdcnt[3] = {0};
	int keywordcount=0;	
	int switchcount=0;
	int casecount=0;
	int elseifcount=0;
	vector<int> casenum;
	vector<string> ifelsearr; 
	int ifelsecount=0;
	int ifelseifcount=0;
	int ifnum=0;
	int elseifnum=0;
	int elsenum=0;

Input File Name , Choose Processing Level  , Open File 

    ifstream inFile;
	cout<<"Please type the file name: ";
	cin>>filename;
	cout<<"Please choose the level(1,2,3,4): ";
	int level;
	cin>>level;
	inFile.open(filename.c_str());
	if(inFile.fail())
	{
		cout<<"The file was not successfully opend"<<endl;
		exit(1);
	}
	string buf;

 Requirement 1 : Basic requirement  i.e.  output "keyword" statistics

	//part 1
	while (getline(inFile,buf))
	{
		cout << buf << endl;
		int i = 0, j = 0;	
		while(i < int(buf.size()) && j < int(buf.size())) 
		{
			if (buf[i] < 'z' || buf[i] > 'a') 
			{
				if (buf[j] > 'z' || buf[j] < 'a') 
				{
					string s = buf.substr(i, j-i);
					nums.push_back(s);
					for (int k = 0; k < 33; k++) {
						if (s == keywords[k]) {
							keywordcount++;
							keywdcnt[k]+=1;
						}
					}
					i = j+1;j=i;
				}
				else
					j++;
			}
			else
				{i++;j=i;}
		}
	}
	if(level>=1)
	cout<<"total number of keywords is: "<<keywordcount<<endl;

Requirement 2 : output the number of "switch case" structures, and output the number of "case" corresponding to each group

	//part 2
	if(level>1)
	{
		for(int i=0;i<int(nums.size());i++)
		{
			if(nums[i]=="switch")
			{
				switchcount+=1;
				//cout<<switchcount<<endl;
				for(int j=i+1;j<int(nums.size());j++)
				{
					//cout<<"nums[j] is now: "<<nums[j]<<endl;
					if (nums[j]=="case")
						casecount+=1;
						//cout<<"casecount is now: "<<casecount<<endl;}
					else if(nums[j]=="switch" )
					{
						i=j-1;
						casenum.push_back(casecount);
						casecount=0;
						break;
					}
					else if(j==int(nums.size()-1))
					{
						i=j;
						casenum.push_back(casecount);
						break;
					}
				}
			}
		} 
			cout<<"total number of switch is: "<<switchcount<<endl;
			cout<<"total number of case is: ";
			for(int i=0;i<int(casenum.size());i++)
			{
				cout<<casenum[i]<<"  ";
			}
			cout<<endl;
			inFile.close();
	}

Requirement 3 :  Uplifting requirement  i.e.  output the number of "if else" structures

Requirement 4 :   Ultimate requirement: output the number of "if, else if, else" structures

		//part 3 & 4
		if(level>2)
		{
			inFile.open(filename.c_str());
			if(inFile.fail())
			{
				cout<<"The file was not successfully opend";
				exit(1);
			}
			while (getline(inFile,buf))
			{
				for(int i=0;i<3;i++)
				{
					if(int(buf.find(ifelsekeywords[i]))!=-1)
						ifelsekeywdcnt[i]=1;
					else
						ifelsekeywdcnt[i]=0;
				}	
				if(ifelsekeywdcnt[1]==1 && ifelsekeywdcnt[2]==1)
				{
					ifelsekeywdcnt[0]=1;
					ifelsekeywdcnt[1]=0;
					ifelsekeywdcnt[2]=0;
				}	
				for(int i=0;i<3;i++)
				{
					if(ifelsekeywdcnt[i]==1)
						ifelsearr.push_back(ifelsekeywords[i]);	
				}
			}
			for(int i=0;i<int(ifelsearr.size());i++)
			{
				if(ifelsearr[i]=="if")
					ifnum+=1;
				else if(ifelsearr[i]=="else if")
					elseifnum+=1;
				else if(ifelsearr[i]=="else")
					elsenum+=1;
			}
			for(int i=0;i<int(ifelsearr.size())-1;i++)
			{
				if(ifelsearr[i]=="if" && ifelsearr[i+1]=="else")
				{
					ifelsecount+=1;
					ifnum-=1;
					elsenum-=1;
				}	
				else if(ifelsearr[i]=="else if" && ifelsearr[i+1]=="else")
				{
					ifnum-=1;elsenum-=1;
					for(int j=i;j>0;j--)
					{
						if(ifelsearr[j]=="else if" && ifelsearr[j-1]=="else if")
						{
							elseifcount+=1;
						}
						else
						{
								ifelseifcount+=1;
							elseifnum-=(elseifcount+1);
							elseifcount=0;
							break;
						}
					}		
				}	
			}
			ifelseifcount+=elseifnum;
			ifnum-=elseifnum;
			ifelsecount+=ifnum;
			if(level>2)
				cout<<"total number of if-else structure is: "<<ifelsecount<<endl;
			if(level>3)
				cout<<"total number of if-else if-else structure is: "<<ifelseifcount<<endl;
			//finish
		}
		return 0;
}


 Sample Test File  &   Output

#include <stdio.h>
int main()
{
	int i = 1;
	double j = 0;
	long f;
	switch (i)
	{
	case 0:
		break;
	case 1:
		break;
	case 2:
		break;
		default:
			break;
	}
	switch(j)
	{
	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;
}

5.Unit test and performance testing

 

 

6.Summarize this assignment

         After this lab, I think it is not easy to complete a project, we have to choose our own suitable programming language to solve the corresponding problem. Moreover, the idea of solving the problem is very important, we need to do a complete idea description and preparation before the whole project. But, after all, I really benefited a lot and my programming level will be improved. 

 

 

...全文

相关推荐

复现基于李雅普诺夫的模型预测控制MPC水下机器人AUV方法路径跟踪(Matlab代码实现)

内容概要:本文系统介绍了基于李雅普诺夫稳定理论的模型预测控制(MPC)方法在水下机器人(AUV)路径跟踪中的应用,并提供了完整的Matlab代码实现。该方法融合MPC的多步预测与滚动优化优势,通过构造李雅普诺夫函数严格证明闭环系统的渐近稳定性,有效提升了AUV在复杂海洋环境下的轨迹跟踪精度、抗干扰能力与控制鲁棒性。文档不仅详述了控制系统建模、约束处理与稳定性分析的关键步骤,还配套列举了大量相关科研资源,涵盖路径规划、运动控制、智能算法等多个方向,突出其在顶刊算法复现、科研验证与工程仿真中的高参考价值。; 适合人群:具备自动控制理论、机器人学或海洋工程等相关专业背景,熟悉Matlab/Simulink仿真工具,且具有一定控制算法基础的研究生、科研人员及自动化领域工程技术人员。; 使用场景及目标:①开展水下机器人高精度路径跟踪控制算法的研究与仿真验证;②深入学习李雅普诺夫稳定性理论与MPC控制策略的有机结合方法;③作为高水平期刊论文复现的技术支撑,服务于科研项目推进、学术论文撰写与算法创新设计; 阅读建议:建议结合文档中提及的反步法、滑模控制、非线性MPC等先进控制技术进行对比分析,重点研读代码中的动力学建模、优化问题构建、约束处理机制与稳定性证明逻辑,根据具体研究需求灵活调整系统参数与仿真场景,以实现最优控制性能并深化理论理解。

程序辅导帮

风光火储多源协同参与电网一次调频与二次 AGC 调频控制策略研究(Simulink仿真实现)

内容概要:本文聚焦于风光火储多源协同参与电网一次调频与二次AGC调频控制策略的研究,基于Simulink平台构建完整的多能源系统仿真模型,深入探讨风力发电、光伏发电、火力发电与储能系统在电网频率调节中的协同工作机制。研究系统分析了一次调频的快速响应特性与二次自动发电控制(AGC)的动态调节能力,重点设计并验证了多源协调控制算法,通过仿真评估不同电源间的出力互补性与动态响应性能,有效提升了含高比例新能源电网的频率稳定性和运行可靠性,具有突出的理论深度与工程应用价值。; 适合人群:适用于具备电力系统分析、自动控制理论及新能源并网技术背景的研究生、科研人员以及从事电网调度、新能源集成与储能系统设计的工程技术人员。; 使用场景及目标:①研究多能源系统协同参与电网频率调节的动态机制;②设计与优化面向复杂电网环境的一次调频与AGC协调控制策略;③开展新能源大规模接入背景下电网稳定性仿真分析;④支撑高水平学术论文撰写、科研项目申报或实际工程方案的技术验证与原型开发。; 阅读建议:建议结合Simulink仿真环境动手复现文中控制模型,对照控制逻辑与时序响应曲线深入理解多源协同控制的设计原理,同时可参考文中关联的MATLAB/Simulink案例进行拓展性研究与算法改进。

荔枝科研社

厂外外包管制作业规范.doc

制造技术

可小已

科技型中小企业申请政府资助时,孵化器管理系统需要提供哪些关键数据支撑材料?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

地方发改委在评审成果孵化转化载体资质时,主要依据哪些核心指标体系?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN
评论
收藏 已收藏
分享
返回顶部