285
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734618 |
The Aim of This Assignment | Extract keywords of different levels from the C code files that are read in |
MU STU ID and FZU STU ID | <20123531_832002220> |
https://github.com/YUNLONG892a/EE308
Personal Software Process Stages | Time/minutes |
---|---|
Planning | 15 |
Estimate | 20 |
Analysis | 25 |
Design | 120 |
Code Review | 30 |
Test and debug | 50 |
Total | 260 |
#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;
}
Enter the path to the test code and the required task level into the different code
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);
}
}
Use the Pattern and Matcher methods to find all the key word in the test code,then print the result.
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 :
Again, using the Regular expression look for "switch" and count the number of cases until you find "default",then print the result.
//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 :
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.
//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 :
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 :
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.
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.