183
社区成员
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 Assignment | Git/GitHub Using & Keywords Capturing |
MU STU ID and FZU STU ID | 19104073_831901119 |
https://github.com/Howell-1108/EE308_LAB2
Personal Software Process Stages | Estimated time(min) | Time(min) |
---|---|---|
Planning | 20 | 20 |
Estimate | 10 | 10 |
Development | - | - |
Analysis | 180 | 200 |
Design Spec | 10 | 15 |
Design Review | 10 | 15 |
Coding Standard | 30 | 40 |
Design | 40 | 60 |
Coding | 300 | 440 |
Code Review | 60 | 110 |
Test | 300 | 1200 |
Test Report | 60 | 110 |
Postmortem & Process Improvement Plan· Design Review | 20 | 20 |
Summary | 1040 | 2240 |
My another blog, in order not to be too messy.
The language I choose is java.
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.
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.
We won't care about this because we will finish this requirement in 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?
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.
When talking to this part, we need to make a classification first.
One is single line annotation, the other is multiline annotation and strings.
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~
Lets take multiline annotation as example, for they are almost the same.
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();
}
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.
So abstract ?
Lets make it clear !
When inWord is false:
When inWord is true:
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)) {...}
}
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 :
"{" or "if": stack in;
";": if the stack top is not ";" , stack in ;
"}" : if the stack top is "{" , do situation 2 ( delete the "{" and stack in a ";" );
"{}" => ";" also "{;}" => ";"
"else if" : if the stack top is not "; else if" , stack in; "else if ; else if" => "else if"
"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:
This is how to handle the stack, and when we excite else, we can count the corresponding number. And finally print them as answers.
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;
}
Still correct though.
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~
A bug
for ”}“ in stacks, it should be set to find "{" and change everything in between to ";"
miao a
aile aile