EE308FZ_Lab1-2

a95asd a95asd 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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 Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734618
The Aim of This AssignmentExtract 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

  1. Indentation: 4 space
  2. Variable naming: Camel-Case
  3. Maximum number of characters per line: 100
  4. Function and class naming: FunctionName(Type par_name1, Type par_name2)
  5. Blank line rule: Empty line between non-empty code blocks
  6. Annotation rules:Start with/and ends with /, that is, /* */

PSP Form

Personal Software Process StagesTime/minutes
Planning15
Estimate20
Analysis25
Design120
Code Review30
Test and debug50
Total260

Description of problem-solving ideas

  1. Consider how do I read c files into a program.
  2. In order to meet the different Program Requirements, it is necessary to make corresponding code design for four different levels of tasks.
  3. By using regular expression and stack to analysis the test code.
  4. 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

img

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.

img

 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 :

img

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.

img

//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 :

img

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.

img

//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 :

img

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 :

img

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

  1. I learned how to create repositories on github and upload and version control my code via git.
  2. I learned how to write a blog using Markdown syntax.
  3. I learned how to use Pattern and Matcher methods for regular expression which judge whether the keyword in the test code
  4. 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.

...全文

相关推荐

【MATLAB例程】基于APF的多无人机动态路径规划与避障仿真 四无人机协同避碰、动态障碍规避及安全性能分析(含公式)

程序基于多无人机二维运动学模型,采用人工势场法(APF)实现四架无人机在固定与动态障碍环境下的路径规划和协同避碰。绘制无人机运动轨迹、障碍物轨迹、安全净间隙、速度与加速度、航程及到达时间等图像,输出目标到达数量、碰撞统计和安全性能指标。无人机数量、障碍物分布、运动约束及APF算法参数均可自行调整。

MATLAB卡尔曼

《智能之门》配套PPT课件

《智能之门》配套PPT课件

Bryan Ding

chromedriver-win64-156.0.8078.80(Stable).zip

chromedriver-win64-156.0.8078.80(Stable).zip

LCG元

三相并网逆变器虚拟阻抗+统一有源阻尼策略SVPWM+SPWM调制仿真

内容概要:本文档系统梳理了电力电子与新能源并网领域的核心技术与仿真实践,聚焦三相并网逆变器的先进控制策略与调制技术,涵盖虚拟阻抗、统一有源阻尼、SVPWM与SPWM调制、预测电流控制、中点电位平衡、低电压穿越(LVRT)及孤岛检测等关键技术。文档整合了大量基于Matlab/Simulink的高保真仿真模型,涉及三电平ANPC/T型/Z源逆变器、LCL滤波器、虚拟同步发电机(VSG)、下垂控制、构网型变流器等多种主流拓扑与控制架构,并延伸至风光储联合系统、微电网能量管理、弱电网稳定性分析、序阻抗建模与扫频法等复杂应用场景,特别强调SCI论文成果复现与工程化设计实现,具有极强的科研指导性与实践价值。; 适合人群:面向电力电子、电气工程及其自动化等相关专业的研究生、高校科研人员及从事新能源发电、微电网控制、逆变器研发的工程技术人员,要求具备扎实的电路理论、自动控制原理基础及一定的Matlab/Simulink仿真能力。; 使用场景及目标:①支撑高水平学术论文的仿真实验设计与结果验证,尤其是构网型变流器、虚拟同步技术、主动阻尼机制等前沿方向的研究;②用于毕业设计、科研项目中复杂电力电子系统的建模与控制策略开发;③助力工程师掌握从单机逆变器控制到多能互补系统协同运行的全流程仿真技术体系。; 阅读建议:建议结合文档提供的参考文献与仿真资源,采用“由简入繁、循序渐进”的学习路径,先掌握三相并网逆变器PI控制与SVPWM调制等基础模型,再逐步深入VSG并离网切换、多机并联环流抑制、弱电网阻抗交互等高阶课题,注重理论推导、参数设计与仿真调试的深度融合,提升系统级建模与问题分析能力。

程序猿鑫

三相四桥臂电压不对称跌落补偿动态电压恢复器DVR(Simulink仿真实现)

内容概要:本文介绍了一种基于三相四桥臂拓扑结构的动态电压恢复器(DVR),用于补偿电网中由电压不对称跌落引起的电能质量问题。通过Simulink平台构建仿真模型,重点研究了在不平衡负载和电网电压跌落工况下,DVR对负载侧电压的快速补偿能力。系统采用多准PR(Proportional Resonant)并联控制策略,实现对各相电压的独立精确控制,有效抑制负序分量,提升系统在复杂工况下的稳定性和响应性能。仿真结果验证了所提控制方法在电压幅值、相位补偿及波形质量改善方面的有效性,适用于对供电连续性和电能质量要求较高的工业场景。; 适合人群:电力电子、电气工程及其自动化等相关专业的研究生、科研人员及从事电能质量治理的工程技术人员。; 使用场景及目标:①研究动态电压恢复器在电压跌落、三相不平衡等典型电能质量问题中的补偿机制;②掌握三相四桥臂逆变器的建模与多准PR控制策略的设计与实现;③为高精度电能质量调节装置的研发提供理论依据和仿真验证手段。; 阅读建议:建议结合提供的参考文献和说明文档,深入理解控制算法的设计原理,并在Simulink环境中动手复现仿真模型,通过调整故障条件和控制器参数,观察系统动态响应,从而全面掌握DVR的工作特性与优化方法。

大龙不吃小鱼干
评论
收藏 已收藏
分享
返回顶部
a95asd
FZU-SE-EE308-2022秋 286 成员 1,014 内容