EE308FZ Lab1_2

weixin_54034824 2022-10-28 00:01:31
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 Assignmentfind keyword num and structure num
MU STU ID and FZU STU ID20123809_832002202

https://github.com/WhiLinhny/Lab1

目录

  • Program Requirements
  • Ⅰ、📋PSP form
  • Ⅱ、The basic idea flowchart
  • 1.Get total number of keywords
  • 2.Get total number of switch-case structure
  • 2.1find the position of switch
  • 2.2find the position of default
  • 2.3count the case between switch and default
  • 3.Get total number of if-else structure
  • 4.Get total number of if-elseif-else structure
  • Ⅲ、Source Code
  • 1.Level1 implementation:Get total number of keywords
  • 2.Get total number of switch-case structure
  • 2.1find the position of switch
  • 2.2find the position of default
  • 2.3count the case between switch and default
  • 3.Level3 implementation:Find the amount of if-else structure
  • 4.Level4 implementation:Find the amount of if-elseif-else structure
  • Ⅴ、Test results
  • Ⅵ、Summary
  • In this experiment, I mainly use simple judgment statements, and the code is too complicated, and the running speed is greatly reduced. In the future, I will continue to learn and improve my code writing ability.
(EE308FZ Lab1_2)


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

Ⅰ、📋PSP form

Personal Software Process StagesWhat should I doTime(min)
PlanningWhat problems should I solve40
DesignAnalyse the requirements and make a flowchart60
CodingWrite code and find some unfamiliar usage180
Code ReviewTest every level again and again and improve it after all levels are completed40
TestingTest every level once and correct the bad writings60
Postmortem & Process ImprovementImprove program and correct the bad writings100

Ⅱ、The basic idea flowchart

1.Get total number of keywords

请添加图片描述

2.Get total number of switch-case structure

2.1find the position of switch

2.2find the position of default

2.3count the case between switch and default

请添加图片描述

3.Get total number of if-else structure

请添加图片描述

4.Get total number of if-elseif-else structure

请添加图片描述

Ⅲ、Source Code

1.Level1 implementation:Get total number of keywords

        String word="abstract  default  goto*  switch  boolean  do  if  package  nchronzed  break  double  implements  private  this  byte  else  import  protected  throw  throws  case  extends  instanceof  public  transient  catch  int  return  char  final  interface  short  try  class  finally  long  static  void  const*  float  native  strictfp  volatile  continue  for  new  super  while  assert  enum";
        String arr[]=word.split("  ");
        String arr1[]= {"if","else","else if"};
        File file=new File("C:\\Users\\lenovo\\Desktop\\lab1code.txt");
        int times = 0;//出现的次数
        LineNumberReader lineReader = null;
        lineReader = new LineNumberReader(new FileReader(file));
        String readLine = null;
        while((readLine =lineReader.readLine()) != null){  
            //判断每一行中,出现关键词的次数
            String keyword;
            for(int i=0;i<arr.length;i++) {
                keyword=arr[i];
                int next = 0;  //定义开始查找关键字的序列号
                int index = 0; //获得readLine的对象值
                while((index = readLine.indexOf(keyword,next)) != -1&&readLine.charAt(index+keyword.length())!='u') {  //从每行的第0个索引开始遍历关键字
                    next = index + keyword.length();  //下一次的遍历序号为序列号+关键字长度
                    times++;//次数加1
                    }
            }        
    }
        lineReader.close();

2.Get total number of switch-case structure

2.1find the position of switch

    public static int findSwitch(File file,int lineNum) throws IOException {
        LineNumberReader lineReader;
        lineReader=new LineNumberReader(new FileReader(file));
        String readLine=null;
        int count=0;
        int numSwitch=0;
        String keyword="switch";
        while((readLine =lineReader.readLine()) != null){  
            count++;
         if(count>lineNum){
            int next = 0;  //定义开始查找关键字的序列号
            int index = 0; //获得readLine的对象值
            while((index = readLine.indexOf(keyword,next)) != -1) {  //从每行的第0个索引开始遍历关键字
                    numSwitch++;//次数加1
                    break;
                }
         } 
            if(numSwitch>=1) {
                break;
            }    
        }
        lineReader.close();
        if(numSwitch!=0) {
            return count;
    }else {
        return numSwitch;
    }
}

2.2find the position of default

    public static int findDefault(File file,int lineNum) throws IOException {
        String readLine=null;
        LineNumberReader lineReader;
        lineReader = new LineNumberReader(new FileReader(file));
        int count=0;
        int numDefault=0;
        String keyword="default";
        while((readLine =lineReader.readLine()) != null){  
            count++;
        if(count>lineNum) {
              int next = 0;  //定义开始查找关键字的序列号
            int index = 0; //获得readLine的对象值
            while((index = readLine.indexOf(keyword,next)) != -1) {  //从每行的第0个索引开始遍历关键字
                numDefault++;//次数加1
                break;
                }
            }
            if(numDefault>=1) {
                break;
            }
}  lineReader.close();
        return count;
    }

2.3count the case between switch and default

    public static int countCase(File file,int lineNumS,int lineNumD) throws IOException {
        String readLine=null;
        LineNumberReader lineReader;
        lineReader = new LineNumberReader(new FileReader(file));
        int count=0;
        int numCase=0;
        String keyword;
        keyword="case";
        while((readLine =lineReader.readLine()) != null){ 
            count++;
            if(count>lineNumS&&count<lineNumD) {
            int next = 0;  //定义开始查找关键字的序列号
            int index = 0; //获得readLine的对象值
            while((index = readLine.indexOf(keyword,next)) != -1) {  //从每行的第0个索引开始遍历关键字
                numCase++;//次数加1
                break;
            }
          }
        }
        lineReader.close();
    return numCase;    
    }

3.Level3 implementation:Find the amount of if-else structure

public static int findIfElse(File file,String arr1[]) throws IOException {
        LineNumberReader lineReader;
        lineReader = new LineNumberReader(new FileReader(file));
        String readLine = null;
        Stack<String> stack = new Stack<String>();
        int count=0;
        while((readLine =lineReader.readLine()) != null){  
            int next=0;
            int index=0;
        if((index = readLine.indexOf("if",next)) != -1&&readLine.charAt(index-2)!='e') { 
                    stack.push("if");
        }else if((index = readLine.indexOf("else",next)) != -1&&readLine.charAt(index+5)!='i'){
                    if(stack.isEmpty()) continue;
                    String top=stack.pop();
                    if(top=="if")
                            count++;
            }else if((index = readLine.indexOf("else if",next)) != -1){
                    stack.push("else if");
            }
        }    
                lineReader.close();
        return count;
    }

4.Level4 implementation:Find the amount of if-elseif-else structure

public static int findIfElseIfElse(File file,String arr1[]) throws IOException {
        LineNumberReader lineReader;
        lineReader = new LineNumberReader(new FileReader(file));
        String readLine = null;
        Stack<String> stack = new Stack<String>();
        int count=0;
        int count1=0;
    while((readLine =lineReader.readLine()) != null){  
        int next=0;
        int index=0;
    if((index = readLine.indexOf("if",next)) != -1&&readLine.charAt(index-2)!='e') {
        stack.push("if");
    }else if((index = readLine.indexOf("else",next)) != -1&&readLine.charAt(index+5)!='i'){
            if(stack.isEmpty()) continue;
            String top=null; 
        while((top=stack.pop())=="else if") {
                count1++;
            }
        if(count1>=1&&top=="if") {
                count++;
                count1=0;
            }                    
    }else if((index = readLine.indexOf("else if",next)) != -1){
                stack.push("else if");
            }
        }    
     lineReader.close();
        return count;
    }

Ⅴ、Test results

请添加图片描述

Ⅵ、Summary

In this experiment, I mainly use simple judgment statements, and the code is too complicated, and the running speed is greatly reduced. In the future, I will continue to learn and improve my code writing ability.

-

...全文
118 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

285

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程(2022秋) 教学
软件工程 高校
社区管理员
  • LinQF39
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧