EE308 Lab1-2

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

目录

1.PSP Table

2.Problem-solving ideas

3.Design and implementation process

4.Code description

5.Test

6.Performance analysis

7.Summary


Github Website Link

XINYUE1234/EE308 (github.com)


1.PSP Table

Personal Software Process StagesEstimated Time(minutes)Completed Time(minutes)
Planning2030
Estimate--
Development3050
Analysis200300
Design Spec1020
Design Review1020
Coding Standard1020
Design2030
Coding150220
Code Review Planning2030
Test1015
Reporting1015
Test Report1010
Size Measurement1010
Postmortem&Process Improvement2020
Total530790

2.Problem-solving ideas

Program Requirements

  1. Basic requirement: output "keyword" statistics
  2. Advanced requirement: output the number of "switch case" structures, and output the number of "case" corresponding to each group
  3. Uplifting requirement: output the number of "if else" structures
  4. 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

这是本次实验的压缩包

晨起剪桃花

【带状电容器计算用于电容式触摸传感器应用】半数值模型的电容式触摸传感器集成于金属基材涂层中研究(Matlab代码实现)

内容概要:本文针对电容式触摸传感器在金属基材上应用时易受干扰导致信号衰减的问题,提出一种集成于金属基材涂层中的带状电容器半数值计算模型。通过理论推导建立传感器电容的数学模型,设定合理的边界条件并采用数值方法求解,利用Matlab实现完整的仿真代码,对传感器在金属背景下的电容特性进行精确建模与性能分析。该方法有效提升了传感器的灵敏度与抗干扰能力,适用于高精度、复杂环境下的触摸感应场景,具有良好的工程应用价值。研究不仅提供了详细的建模流程与仿真验证,还开放了可复现的代码资源,便于后续优化与拓展。; 适合人群:从事电容式传感器设计、嵌入式系统开发、人机交互技术研究及相关领域的科研人员,以及具备电磁场理论基础和Matlab编程能力的硕士、博士研究生;; 使用场景及目标:①用于金属表面电容式触摸传感系统的建模与性能优化;②为抗金属干扰传感器提供有效的半数值仿真方法与技术支撑;③辅助高校科研与教学中对电容耦合机制的理解及仿真平台搭建; 阅读建议:建议结合Matlab代码逐模块调试,深入理解模型中几何参数、材料属性及边界条件对电容输出的影响规律,可进一步推广至其他复杂结构或非理想环境下的传感器设计中。

荔枝科研社

(Multisim课设资料)最全面函数信号发生器(三角波+正弦波+方波信号+锯齿波仿真设计+报告文档

内容概要:本文档是一份综合性科研技术资源汇总,系统梳理了工程与计算机科学领域的多项关键技术及其实现方法,涵盖图像处理、路径规划、无人机应用、通信技术、信号处理、电力系统优化、车间调度、雷达技术、微电网与综合能源系统调度等多个前沿方向。文档重点提供了基于Matlab和Simulink的丰富仿真案例,包括裂纹检测、双目视觉体积测量、卡尔曼滤波状态估计、永磁同步电机控制、无功优化、多目标调度算法(如NSGA-II、粒子群算法)在能源与交通系统中的应用,以及遗传算法、差分进化、人工蜂群等智能优化算法的代码实现。资源强调理论与实践深度融合,配套完整的仿真模型、算法代码与实验结果,具有较强的工程复现价值和科研指导意义。; 适合人群:具备一定编程基础的研究生、科研人员及从事自动化、电气工程、控制工程、通信、人工智能等相关领域工作的技术人员,尤其适合有1-3年工作经验的研发人员。; 使用场景及目标:①用于高校课程设计、毕业设计、科研项目开发和技术复现;②支撑电力系统优化、智能制造调度、智能交通路径规划、无人机控制等实际工程问题的算法研究与仿真验证;③帮助读者掌握主流优化算法与信号处理技术的核心原理与代码实现。; 阅读建议:建议读者结合文中提供的Matlab/Simulink代码与仿真模型进行实践操作,优先选择与自身研究方向相关的主题深入学习,并参考所提供的网盘链接获取完整资源以辅助调试与复现实验结果。

稷下科研社

BMS保护板原理图+PCB+固件源码+bin文件+上位机

BMS保护板原理图+PCB+固件源码+bin文件+上位机 单片机是STM32F303CCT6 1、带有软电源开关输入,带 LED (LOAD) 开/关 + 充电指示灯; 2、具有高压侧电源开关,控制通断; 3、隔离式 CAN 总线接口; 4、电流测量; 5、带有短路检测; 6、带有USB 接口通信和固件升级; 7、蜂鸣器指示错误状态; 8、体积小巧; 9、短时间最大负载电流+/- 100A,长时间约为70A; 10、最大充电电流 10A(带二极管)或 30A(带二极管旁路); 11、电池电压范围为 2.5V 至 4.5V; 12、包电压 12V 至 54V;

newzhpfree

无刷直流电机BLDC无感+滑模观测器+转速电流双闭环PI控制(总共三个仿真模型)(Simulink仿真实现)

内容概要:本文档系统阐述了无刷直流电机(BLDC)在无位置传感器条件下的高性能控制策略,基于Simulink平台构建了三个相互关联的仿真模型,重点实现无感控制与滑模观测器(Sliding Mode Observer, SMO)相结合的转速电流双闭环PI控制系统。该资源摒弃了传统的霍尔传感器,利用滑模观测器实时估算转子的位置与转速信息,有效降低了系统成本并提升了在恶劣环境下的运行可靠性。仿真模型完整涵盖了逆变电路、空间矢量脉宽调制(SVPWM)、三相电流采样、坐标变换(Clark & Park)、PI调节器以及滑模观测器等核心模块,形成了一个闭环的、高动态响应的电机驱动系统。此外,文档还集成了模糊PID、传统PID等多种控制算法的对比仿真模型,便于研究者深入分析不同控制策略在动态响应、抗干扰能力和稳态精度方面的性能差异,为电机控制算法的研究与工程化应用提供了坚实的仿真验证基础。; 适合人群:具备自动控制理论、电机拖动原理及电力电子技术基础知识,并熟悉MATLAB/Simulink软件操作的电气工程、自动化、机电一体化等专业的高年级本科生、硕士研究生,以及从事电机驱动、新能源汽车、工业自动化等领域的研发工程师和技术人员。; 使用场景及目标:①深入学习和掌握无刷直流电机无位置传感器控制的基本原理与实现方法;②系统性地理解滑模观测器的设计思想、稳定性分析及其在转子位置与速度估计中的关键技术;③对比研究传统PI控制、模糊PID控制及滑模观测器在BLDC调速系统中的综合性能,分析其在不同工况下的鲁棒性与适应性;④为毕业设计、科研课题或工业项目中的电机控制系统开发提供可复用的仿真平台与技术参考。; 阅读建议:本资源以Simulink仿真为核心载体,建议使用者结合文档中提供的详细说明报告与参考文献,循序渐进地理解各功能模块的设计逻辑与参数整定方法。在学习过程中,务必动手运行并调试仿真模型,仔细观察并记录在不同负载突变、转速指令变化等工况下,电机的三相电流、转速响应曲线及滑模观测器输出的转子位置与速度的动态波形,从而深刻理解控制算法的内在机理。为进一步提升实践与创新能力,可尝试修改控制器参数(如PI增益、滑模增益),或替换不同的控制策略进行对比实验,探究其对系统性能的影响。

天天程序猿
评论
收藏 已收藏
分享
返回顶部