EE308FZ_Lab1-2

YUZUq 2022-10-24 22:07:30
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608745426
The Aim of This AssignmentTo achieve a program function, it can extract keywords of different levels from the C or C++ code files that are read in.
MU STU ID and FZU STU ID20124261_832002225

github Link:https://github.com/YUZUq/Lab1-for-EE308

1. PSP form for this work

Personal Software Process StagesTime (min)
Planning20
Analysis (including learning new knowledge)90
Development60
Test20
Postmortem & Process Improvement Plan20

2. Description of problem-solving ideas

  1. Need a method to read the c file.
  2. Use regular expression and stack to search keywords.
  3. Learn how to do unit test.

3. Design and implementation process

img

4. Code description.

  1. main method

    public static void main(String[] args) throws Exception {
         Scanner sc=new Scanner(System.in);
         System.out.println("Pleaase input the path of the code file.");
         String path=sc.nextLine();
         System.out.println("Pleaase input the completion level.");
         int level = Integer.parseInt(sc.nextLine());            //询问文件地址和任务等级
         Lab1 l = new Lab1();
         String s = l.read(path);            //字符串形式读文件
         if (level == 1) {            //根据等级不同完成任务
             lv1(s);
         } else if (level == 2) {
             lv2(s);
         } else if (level == 3) {
             lv3(s);
         } else if (level == 4) {
             lv4(s);
         }
         sc.close();
     }
    
  2. read the file

    public String read(String url) throws IOException {
         File file = new File(url);
         FileReader reader = new FileReader(file);
         BufferedReader br = new BufferedReader(reader);
         StringBuilder sb = new StringBuilder();
         String s;
         while ((s = br.readLine()) != null) {
             sb.append(s);
         }
         return sb.toString();
     }
    
  3. How to search key word

    public static int match(String str, String target) {            //查找关键词
         int start = 0;
         int count = 0;
         int length = target.length();
         while (true) {
             if(str.indexOf(target,start)!=-1 && !str.substring(str.indexOf(target,start)-1, str.indexOf(target,start)).matches("[a-zA-Z]")
                     && !str.substring(str.indexOf(target,start)+length, str.indexOf(target,start)+length+1).matches("[a-zA-Z]")) {
                 //关键词前后必须为非字母才能确认为关键词
                 count++;
                 start = str.indexOf(target,start);
                 start += length;
             } else {
                 break;
             }
         }
         return count;
     }
    
  4. Task 1 and task 2

    public static void lv1(String str) {
         int count = 0;
         String keyword[] = {"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",
                             "unsigned", "union", "void", "volatile", "while"};
         for (int i = 0; i < keyword.length; i++) {
             count += match(str, keyword[i]);
         }
         System.out.println("total num: " + count);
     }
    
     public static void lv2(String str) {
         lv1(str);
         System.out.println("switch num: " + match(str, "switch"));
         String[] temp = str.split("switch");
         System.out.print("case num: ");
         for (int i = 1; i < temp.length; i++) {
             System.out.print(match(temp[i], "case") + " ");
         }
         System.out.println();
     }
    
  5. How to search if/else or if/else if

    public static int match2(String str, int level) {
         Stack<String> s1 = new Stack();
         Pattern pattern = Pattern.compile("else *if|else|if");
         Matcher matcher = pattern.matcher(str);
         int esif = 0;
         int ifes = 0;
         boolean noEsif = true;
         while (matcher.find()) {
             String subs = str.substring(matcher.start(), matcher.end());
             if (!subs.equals("else")) {            //向栈中存贮“if”/“else if”
                 s1.push(subs);
             } else {            //检索到“else”
                 while (!s1.peek().equals("if")) {            //栈最后一项为“else if”,说明是if else if结构
                     s1.pop();
                     if (noEsif) {
                         esif++;
                         noEsif = false;
                     }
                 }
                 if (noEsif == true) {            //栈最后一项为“if”,说明是if else结构
                     ifes++;
                 }
                 s1.pop();
                 noEsif = true;
             }
         }
         if (level == 3) {
             return ifes;
         } else {
             return esif;
         }
     }
    

    5. Unit test

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class test {
    Lab1 t = new Lab1();

    @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("C:\\Users\\sunfu\\Desktop\\codeExample.c", 1, "total num: 35"),
                Arguments.of("C:\\Users\\sunfu\\Desktop\\codeExample.c", 2, "switch num: 2\ncase num: 3 2"),
                Arguments.of("C:\\Users\\sunfu\\Desktop\\codeExample.c", 3, "if-else num: 2"),
                Arguments.of("C:\\Users\\sunfu\\Desktop\\codeExample.c", 4, "if-elseif-else num: 2")
        );
    }
}

img

6. Summary

In this lab, I develop a Java project and tried to advance it, I found it is really important to have a plan and analysis before to do it. What's more I also tried to grasp the knowledge of markdown and how to upload project to github.

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

285

社区成员

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

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