EE308 Lab2 Individual programing work

He_Haoyuan 2021-09-22 18:04:42

EE308 Lab2 Individual programing work

The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ?category=0
The Link of Requirement of This Assignment https://bbs.csdn.net/topics/600798588
The Aim of This AssignmentGit/GitHub Using & Keywords Capturing
MU STU ID and FZU STU ID19104073_831901119

GitHub Repository

https://github.com/Howell-1108/EE308_LAB2

img

PSP

Personal Software Process StagesEstimated time(min)Time(min)
Planning2020
Estimate1010
Development--
Analysis180200
Design Spec1015
Design Review1015
Coding Standard3040
Design4060
Coding300440
Code Review60110
Test3001200
Test Report60110
Postmortem & Process Improvement Plan· Design Review2020
Summary10402240

Stating with Git

My another blog, in order not to be too messy.

Stating With Git to GitHub https://blog.csdn.net/qq_45572662/article/details/120392334?spm=1001.2014.3001.5502

Problem-Solving Ideas

img

The language I choose is java.

Basic requirement

In this problem, first we need to read a file from a given path.

BufferedReader is a good choice to read message from both console and file.

Then I read information line by line from the file.

In each file, we first need to delete annotation and strings. For keywords won't come from those blocks.

And we extract lowercase letter blocks from strings. Keywords must be lowercase right?

Then we can compare the letter block to our keyword Set. By this way we can finish our first level requirement.

Advanced requirement

Here we need to get switch number and case number for each switch.

Just take an array to store case number, the length is switch number.

Nothing important to say XD.

Uplifting requirement

We won't care about this because we will finish this requirement in Ultimate requirement.

Ultimate requirement

Oh my boy this is a big thing.

At first I thought a stack can easily finish this task. But after some test, I found that a simple stack which only involve "if" "else if" "else" can not solve this problem as I wish. A stack involving only "if" "else if" "else", but we can not solve complicated nested situations.

Observing the testing file, I thought I can use the situation of the keywords to find its nest level, but unfortunately, the file can be formatted in a mess (= =b , so it is still not a correct way.

And then I tried to add "{" and "}" into the stack, then I can solve the nesting problem for well braced files. But what if files that are no braces in if structures?

img

In my final solution, we should stack in "if" "else if" "else" "{" "}" ";" . Because I noticed that "if " structures without braces have a feature: for adjacent keywords, if they are nesting, there are no ";" in between. By this feature, I can solve the problem~

I will show you why and how later.

Algorithms

Deleting annotations and strings

When talking to this part, we need to make a classification first.

One is single line annotation, the other is multiline annotation and strings.

Single Line Annotation

img

public static String DeleteLineAnnotation(String str){
    for(int i = 0; i < str.length() ;i++){
        if(str.charAt(i) == '/' && str.charAt(i) == '/'){
            return str.substring(0,i);
        }
    }
    return str;
}

Single line annotation is very easy to handle. For we only need to throw the "//" and following things away~

Multiline annotation and strings

Lets take multiline annotation as example, for they are almost the same.

img

The key for this part is an "Annotation Flag".

Annotation Flag is initially "false". It is a flag to show whether it is in annotation mode.

In annotation mode, we will throw current char away until we find a "*/".

And when flag == false, we will keep current char until we find "/*".

Sounds easy right? So does the code.

public static String DeleteBarAnnotation(String str){
    StringBuilder sb = new StringBuilder(200);
    for(int i = 0; i < str.length() ;i++){
        if(!annotationFlag){
            if(str.charAt(i) == '/' && str.charAt(i) == '*'){
                annotationFlag = true;
            }else{
                sb.append(str.charAt(i));
            }
        }else{
            if(str.charAt(i) == '*' && str.charAt(i) == '/'){
                i++;
                annotationFlag = false;
            }
        }
    }
    return sb.toString();
}

Words extraction

As a green java programmer, I didn't know anything about regular expression before I finish this task (= =b

So I try to accomplish word extraction as a savage: dealing char by char.

img

So abstract ?

Lets make it clear !

When inWord is false:

img

When inWord is true:

img

Then we use function substring(4, 8) ==> we get a "else" !

But else if is something special, it should be a concrete, so when we find else, check if substring(headIndex, endIndex+3) is else if.

int headIndex = 0;
int endIndex = 0;
boolean inWord = false;
// ergodic every char in the line
for (int i = 0; i < lineLen; i++){
    if(IsWord(line.charAt(i))){
        if(!inWord){
            // find the head to a word
            headIndex = i;
            inWord = true;
        }
    }else{
        if(inWord){
            // find the end to a word
            endIndex = i;
            String currentWord = line.substring(headIndex, endIndex);

            // judge if the word is a keyword
            switch (currentWord){...}
            inWord = false;
        }
    }
    // check if this char is { or } or ;
    switch (line.charAt(i)) {...}
}

Solution to Ultimate Requirement

First thing first, I don't think my solution is a good solution, but it can actually solve the requirement.

In this solution, we stack in every "if" "else if" "else" "{" "}" ";"

After reading the file, we can start handling the stack:

Ergodic all elements in the stack, if the current element is :

  1. "{" or "if": stack in;

  2. ";": if the stack top is not ";" , stack in ;

  3. "}" : if the stack top is "{" , do situation 2 ( delete the "{" and stack in a ";" );

    "{}" => ";" also "{;}" => ";"

  4. "else if" : if the stack top is not "; else if" , stack in; "else if ; else if" => "else if"

  5. "else" : if the stack top is a "if ; else if ;" pop 4 elements, if the stack top is "if ;" pop 2 elements, count the corresponding number, do situation 2;

    "if;else" => ";"

    "if ; else if ; else" => ";"

Still abstract? Never mind, lets take an example:

using the example of the problem, we have the following stack:

{ ; { ; } { ; } if { if { } else { } } else if { if { } else if { } else if { } else { } } else { if { } else { } } ; }

And we will add them in the stack element by element:

img

img

This is how to handle the stack, and when we excite else, we can count the corresponding number. And finally print them as answers.

Unit Test

img


img


img


img


img

Performance Testing

img

Of course pass for the given file, so lets try another one XD.

#include <stdio.h>
int main(){
    int i=1;
    // int int int int int int int int int int 
    double j=0;
    long f;
    switch(i){
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        default:
            break;
    }
    if(mp3[(x-1)*n+y] && (x != p || y != q))return;
    if(mp1[(x-2)*n+y] == 'D')
    {
        totdj += mp2[(x-2)*n+y];
        dfs(x-1,y);
        mp3[(x-2)*n+y] = true;
    }
    if(){
        if(){
        
        }
        else{}
    }
    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;
}

img

Still correct though.

Summary

This lab is far more hard than I thought. From the first use to git to the first use to unit test.

I thought it is a easy lab but I'm totally wrong (= =b(

Cost a lot of effort to learn how to use git, and make a blog to it.

Then comes to the coding. The problem is harder than I thought, especially the 4th level. My algorithm is to savage, waiting for someone to teach me about it XD.

A super exhausting lab for I learnt too may things : from 0 to study java, the use of git, use of junit , hope next lab can be a little easier )

OK, see you next Lab~

img

...全文
405 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
He_Haoyuan 2021-09-22

A bug
for ”}“ in stacks, it should be set to find "{" and change everything in between to ";"

  • 打赏
  • 举报
回复 1
Yifan_Shen 2021-09-22

miao a
aile aile

  • 打赏
  • 举报
回复
发帖
福州大学-软件工程EE308-2021秋
加入

184

社区成员

福州大学 梅努斯国际工程学院 软件工程 教学
社区管理员
  • 单步调试
帖子事件
创建了帖子
2021-09-22 18:04
社区公告
暂无公告