EE308FZ_Lab1-2
CATALOG
- Table
- GitHub Repository
- Code Standard
- PSP Form
- Description of problem-solving ideas
- Design and implementation process && Code description
- test code
- Read the test code and access to different levels of methods
- level1()- Calculate the number of keywords in the test code
- level2() - Calculate the number of "switch case" structure and the number of "case" corresponding to each group
- level34() - //Calculate the number of "if, else" structures and the number of "if, else if, else" structures
- Performance test
- Summary
Table
| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
|---|---|
| The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734618 |
| The Aim of This Assignment | Extract keywords of different levels from the C code files that are read in |
| MU STU ID and FZU STU ID | <20123531_832002220> |
GitHub Repository
https://github.com/YUNLONG892a/EE308
Code Standard
- Indentation: 4 space
- Variable naming: Camel-Case
- Maximum number of characters per line: 100
- Function and class naming: FunctionName(Type par_name1, Type par_name2)
- Blank line rule: Empty line between non-empty code blocks
- Annotation rules:Start with/and ends with /, that is, /* */
PSP Form
| Personal Software Process Stages | Time/minutes |
|---|---|
| Planning | 15 |
| Estimate | 20 |
| Analysis | 25 |
| Design | 120 |
| Code Review | 30 |
| Test and debug | 50 |
| Total | 260 |
Description of problem-solving ideas
- Consider how do I read c files into a program.
- In order to meet the different Program Requirements, it is necessary to make corresponding code design for four different levels of tasks.
- By using regular expression and stack to analysis the test code.
- Learning how to do unit tests
Design and implementation process && Code description
test code
#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(i){
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;
}
Read the test code and access to different levels of methods
Enter the path to the test code and the required task level into the different code

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String location = sc.nextLine();
int n_value = sc.nextInt();///Users/zhaoyunlong/Desktop/EE308/EE308_LAB1-2.txt
String txtFile = location;
String line = "";
String line1 = "";
String[] data = new String[100000];
int i = 0;
try (BufferedReader br = new BufferedReader(new FileReader(txtFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
data[i] = line;
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
if(n_value == 1) {
level1(data,i);
}
else if(n_value == 2) {
level2(data,i);
}
else if(n_value == 3) {
level34(data,i);
}
}
level1()- Calculate the number of keywords in the test code
Use the Pattern and Matcher methods to find all the key word in the test code,then print the result.

public static void level1(String str[],int n) {
int totalnum = 0;
String keywords="abstract、assert、boolean、break、byte、case、"
+ "catch、char、class、continue、default、do、double、else、"
+ "enum、extends、final、finally、float、for、if、implements、"
+ "import、int、interface、instanceof、long、native、new、"
+ "package、private、protected、public、return、short、static、"
+ "strictfp、super、switch、synchronized、this、throw、throws、"
+ "transient、try、void、volatile、while";//all keywords
String []keyArr=keywords.split("、");
for(int j = 0 ; j <= n - 1; j++) {
String m = str[j];
for(int q = 0 ; q <= keyArr.length - 1 ; q++) {
Pattern p = Pattern.compile("[^a-z]"+keyArr[q]+"[^a-z]");//The keyword must be preceded and followed b
Matcher match = p.matcher(m); //y non-letters to be recognized as a keyword
if(match.find()) {
totalnum++;
}
}
}
System.out.println("total num: " + totalnum);
}
Output :

level2() - Calculate the number of "switch case" structure and the number of "case" corresponding to each group
Again, using the Regular expression look for "switch" and count the number of cases until you find "default",then print the result.

//Calculate the number of "switch case" structure and the number of "case" corresponding to each group
public static void level2(String str[],int n) {
int switchnum = 0;
int [] num = new int [10000];
int casenum = 0;
for(int j = 0 ; j <= n - 1; j++) {
String m = str[j];
Pattern p = Pattern.compile("[^a-z]switch[^a-z]");
Matcher match = p.matcher(m);
Pattern p1 = Pattern.compile("[^a-z]case[^a-z]");
Matcher match1 = p1.matcher(m);
Pattern p2 = Pattern.compile("[^a-z]default[^a-z]");
Matcher match2 = p2.matcher(m);
if(match.find()) {
switchnum++;
continue;
}
if(match1.find()) {
casenum++;
}
else if(match2.find()) {
num[switchnum - 1] = casenum;
casenum = 0;
}
}
System.out.println("switch num: " + switchnum);
System.out.print("case num: " );
for(int i = 0 ; i <= switchnum - 1 ; i++) {
System.out.print(num[i] + " ");
}
System.out.println();
}
Output :

level34() - //Calculate the number of "if, else" structures and the number of "if, else if, else" structures
To solve the problem of nesting "if_else if_else"/" if_else" statements,I chose the stack to store "if/else if/else". Whenever I found the "else" keyword, I would delete the last keyword "if" stored in the stack and the keyword "else if" above it, and count the number of different structures.

//Calculate the number of "if, else" structures and the number of "if, else if, else" structures
public static void level34(String str[],int n) {
int ifnum = 0;
int it = 0;
Stack<String> s = new Stack();
boolean [] bool = new boolean [10000];
int ienum = 0;
int ieenum = 0;
for(int j = 0 ; j <= n - 1; j++) {
String m = str[j];
Pattern p = Pattern.compile("[^a-z]if[^a-z]");
Matcher match = p.matcher(m);
Pattern p1 = Pattern.compile("[^a-z]else[^a-z]");
Matcher match1 = p1.matcher(m);
boolean a = match.find();
boolean b = match1.find();
if(a&&!b) {
s.push("if");
}
else if(a&&b) {
s.push("else if");
}
else if(!a&&b) {
s.push("else");
boolean c = false; //The bool to see if the structure is "if_else" or" if_else"
while(!s.isEmpty()) {
String u = s.pop();
if(u.equals("if")) {
if(c) {
ieenum++;
c = false;
}
else {
ienum++;
}
break;
}
else if(u.equals("else if")) {
c = true; //The bool should be true if there is an "else if" in the middle
} //of the structure
}
}
}
System.out.println("if-else num: " + ienum);
System.out.println("if-elseif-else num: " + ieenum);
}
Output :

Performance test
Unit test code
public class Test {
Task2 t = new Task2();
@ParameterizedTest
@MethodSource("parameterDataProvider")
void Test(String url, int l, String result) throws IOException {
t.read(url);
assertEquals(result, t.toString());
}
public static Stream<Arguments> parameterDataProvider() {
return Stream.of(
Arguments.of("/Users/zhaoyunlong/Downloads/Loveyou/src/Lover/Task2.java", 1, "total num: 35"),
Arguments.of("/Users/zhaoyunlong/Downloads/Loveyou/src/Lover/Task2.java", 2, "switch num: 2\ncase num : 3 2"),
Arguments.of("/Users/zhaoyunlong/Downloads/Loveyou/src/Lover/Task2.java", 3, "if-else num: 2\nif-elseif-else num: 2"),
);
}
Performance :

Description :
At first, I chose to use string segmentation to find keywords and used string array and int array to find "if_else if_else"/" if_else "structure, which took up a lot of running space, reduced the efficiency of the code and prolonged the running time of the code. Later, I used Pattern and Matcher methods to judge whether the keyword in the test code and store keywords by stack, which improved the efficiency of subsequent code judgment.
Summary
- I learned how to create repositories on github and upload and version control my code via git.
- I learned how to write a blog using Markdown syntax.
- I learned how to use Pattern and Matcher methods for regular expression which judge whether the keyword in the test code
- I learned how to do unit test coverage optimization and performance testing, performance optimization screenshots and descriptions.
From this lab,I learned how to make a good project plan and understand the project requirements before doing a project, and improve the operation efficiency of the code and make the code easier to understand by optimizing the code. I hope I can summarize the experience and draw lessons from it.
相关推荐
AI Design Skills一个不断壮大的设计技能合集,适用于 AI 编码工具
用户可借助此项目为 Claude Code、Cursor 等 AI 编码工具添加设计技能,轻松构建如着陆页等内容。项目包含多个独立设计技能文件夹,支持自定义设计值,安装方式简单适配多种工具
Path-Planning-Algorithm-main.zip
1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
高校技术转移办公室人员如何提升科技成果推介书的撰写效率?.docx
科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。
三相并网逆变器无功功率扰动+孤岛检测控制算法仿真【参考文献+仿真】
内容概要:本文围绕“三相并网逆变器无功功率扰动+孤岛检测控制算法仿真”展开,系统阐述了在新能源并网背景下,利用无功功率扰动法实现孤岛效应检测的关键技术原理与仿真方法。依托MATLAB/Simulink平台构建完整的三相并网逆变器控制系统模型,详细展示了控制策略的设计流程、算法实现机制及动态响应特性分析,重点突出孤岛检测的判据设定、扰动注入方式与检测盲区(NDZ)的优化。文档配套提供参考文献、仿真模型文件及相关技术说明,具备较强的理论深度与工程复现价值,是研究并网逆变器保护控制的重要参考资料。该资源隶属于“荔枝科研社”系列科研资料,聚焦电力电子与新能源并网领域的前沿技术仿真与论文复现。; 适合人群:适用于电力系统、电气工程及其自动化、新能源科学与工程等相关专业的高校研究生、科研人员,以及从事光伏/风电并网、微电网控制等方向的技术研发工程师,要求具备扎实的电力电子技术基础和一定的MATLAB/Simulink仿真能力。; 使用场景及目标:①深入理解无功功率扰动法在孤岛检测中的应用机理与实现步骤;②支撑毕业设计、科研课题或学术论文的仿真验证与结果复现;③用于新能源发电系统并网安全性分析与保护策略开发的实际工程参考;④辅助教学与培训中对复杂控制算法的可视化讲解与实践操作。; 阅读建议:建议结合所提供的参考文献与仿真文件同步学习,按照目录顺序逐步调试模型参数,重点关注控制环路设计、扰动信号幅值与频率设置对检测精度的影响,并通过仿真波形分析系统在不同工况下的动态行为,全面提升对实际工程问题的建模与解决能力。
《跨媒体移动应用理论与实践》配套PPT课件
《跨媒体移动应用理论与实践》配套PPT课件


