EE308-Lab1_2

m0_57843239 m0_57843239 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907?spm=1001.2014.3001.6377
The Aim of This Assignment<Extract keywords of different levels from the C or C++ code files>
MU STU ID and FZU STU ID<20124279_832001119>

1.the PSP of this work

Program design processWhat I doESTIMATE TIME(MIN)
PlanningUnderstand the requirements of the question, roughly construct the idea20
designWrite down the general idea,draw a logical diagram20
Write the codeImport different data for detection120
TestImport different data for detection30
Improve my codeSummarize and improve the above tests30
Wring ReportWring Report60

After getting the problem, I solve the problem of general thinking, and process

  1. First of all, I think how to let program read c files
  2. The input c language file into an array form
  3. By looking up the data that through the positive expression can more easily calculate the required value, get the result(a Regular Expression is a pattern of text that includes both normal characters (for example, letters between A and z) and special characters (called "metacharacters"))

code

https://github.com/pikaquququ/lab1-2/blob/main/lab1-2

Test_code

1.    #include <stdio.h>  
2.    int main(){  
3.        int i=1;  
4.        double j=0;  
5.        long f;  
6.        switch(i){  
7.            case 0:  
8.                break;  
9.            case 1:  
10.                break;  
11.            case 2:  
12.                break;  
13.            default:  
14.                break;  
15.        }  
16.        switch(i){  
17.            case 0:  
18.                break;  
19.            case 1:  
20.                break;  
21.            default:  
22.                break;  
23.        }  
24.        if(i<0){  
25.            if(i<-1){}  
26.            else{}  
27.        }  
28.        else if(i>0){  
29.            if (i>2){}  
30.            else if (i==2) {}  
31.            else if (i>1) {}  
32.            else {}  
33.        }  
34.        else{  
35.            if(j!=0){}  
36.            else{}  
37.        }  
38.        return 0;  
39.    }  


Code writing process

Overall logic diagram

img

Reads the specified code file

public static void main(String[] args) throws IOException {
        
        //read the test file
        FileReader file = null;
        try {
            file = new FileReader("C:\\Users\\Desktop\\lab1-2.txt");
            
        } catch (Exception e) {
            e.getStackTrace();
        }
        BufferedReader input = new BufferedReader(file);
        String lineString = "";
        String tempString = input.readLine();
        while(tempString!=null) {
            lineString+=tempString;
            tempString = input.readLine();
        }
        
        solution1(lineString);
        solution2(lineString);
        solution3_4(lineString);

    }

Import c language keywords,And convert it into an array

c language keyword table(ANSI C standard C language has 32 keywords)

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



Calculate leve1

img

int num = 0;
        for(int i = 0; i < keywords.length; i++) {
            String regExpress = "[^a-zA-Z_]"+keywords[i]+"[^a-zA-Z_]";
            Pattern pattern = Pattern.compile(regExpress);
            Matcher matcher = pattern.matcher(input);
            while (matcher.find()) {
                num++;
            }
        }
        System.out.println("The number of keywords is "+num+".");
        System.out.println();
    }
    
    
    public static void solution2(String input) {
        
        int num_switch = 0;
        String regExpress = "[^a-zA-Z_]"+"switch"+"[^a-zA-Z_]";
        Pattern pattern1 = Pattern.compile(regExpress);
        Matcher matcher1 = pattern1.matcher(input);
        while(matcher1.find()) {
            num_switch++;
        }
        System.out.println("The number of 'switch' is "+num_switch+".");
        

Calculate the number of switches and cases

img

int num_case = 0;
        int num = 0;
        String regExpress2 = "switch.*?}";
        Pattern pattern2 = Pattern.compile(regExpress2);
        Matcher matcher2 = pattern2.matcher(input);
        while(matcher2.find()) {
            String regExpress3 = "[^a-zA-Z_]"+"case"+"[^a-zA-Z_]";
            String tempText=matcher2.toString();
            Pattern pattern3 = Pattern.compile(regExpress3);
            Matcher matcher3 = pattern3.matcher(tempText);
            while(matcher3.find()) {
                ++num_case;
            }
            num++;
            System.out.println("The number of 'case' in switch "+ num +" is "+num_case+".");
            num_case = 0;
        }
        System.out.println();
    }

Calculate the number of if-else and if-elseif-else


    public static void solution3_4(String input){
        
        Stack<String> s = new Stack<String>();
        int num_if_else = 0;
        int num_if_elseif_else = 0;
        
        String regExpress = "else *if|else|if";
        Pattern pattern = Pattern.compile(regExpress);
        Matcher matcher = pattern.matcher(input);
        
        while(matcher.find()) {
            String sub=input.substring(matcher.start(),matcher.end());

            if(sub.equals("if")||sub.equals("else if")){
                s.push(sub);

            }else {
                //s.pop() is 'if'
                if(s.pop().equals("if")) {
                    num_if_else++;
                }
                //s.pop() is 'else if'
                else{
                    num_if_elseif_else++;
                    //Keep popping the top of the stack until the top is 'if'
                    while(s.peek().equals("else if")) {
                        s.pop();
                    }

                    s.pop();
                }
            }
        }
        
        System.out.println("The number of 'if-else' is "+num_if_else+".");
        System.out.println("The number of 'if-elseif-else' is "+num_if_elseif_else+".");
        
    }
    
}


The results

img

summary

The result are all the correct with the reference ,Using regular expressions can simplify the code as much as possible, and in terms of code optimization, I use the Vector class to replace the previous string, which can save space.Because I have not used Java programming much, so I am not very skilled, took a long time, but at the same time got a lot of improvement

...全文

相关推荐

EE-308 LAB4 final version.7z

这是本次实验的压缩包

晨起剪桃花

复现基于李雅普诺夫的模型预测控制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
评论
收藏 已收藏
分享
返回顶部