286
社区成员
The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907 |
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 | 20124163 832002230 |
https://github.com/ZhejiRuan/EE308_Lab1_2
Framework activities: | what I could do | ESTIMATE TIME(MINUTE) |
---|---|---|
Planning | Understand the meaning of the problem, think of a rough solution process | 60 |
design | Program the idea and write a rough outline | 150 |
Code Review | Check for compliance with programming specifications and check for syntax errors | 90 |
Test | Import different data for detection | 90 |
Postmortem & Process Improvement Plan | Do an overall review to make sure the program works | 30 |
Total | 420 |
First, find the method to read the c file.
Second, find c language keywords table, in the form of an array into the code,
Third, through the regular expression and stack to achieve the task.
Fourth, the requirements are divided into 4 levels, input the corresponding level can execute the corresponding code.
First, read the file from BufferedReader and put it together as a string.
Second, put the keyword table into the array,
Third, match all keywords according to the regular expression and count them.
Fourth, distribution solves Switch, case,if-else,if-elseif-else cases.
First, gets user's input, reads the specified file and judge requirements.
Scanner scanner = new Scanner(System.in);
//Read C file from file address
System.out.println("Please input the path of the code file");
//String fileName = "D:\\Code\\software_engineering_lab1\\src\\sample.c";
String fileName = scanner.nextLine();
//Read the file
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
String Cfilepath = "";
String line = bufferedReader.readLine();
while (line != null) {
Cfilepath += line;
line = bufferedReader.readLine();
}
//Completion Level: You can enter 4
System.out.println("Please input the completion level (from low to high as 1, 2, 3, 4 )");
int level = scanner.nextInt();
//Judge requirements
switch (level) {
case 1:
findKey(Cfilepath);
break;
case 2:
findKey(Cfilepath);
findSwitchAndCase(Cfilepath);
break;
case 3:
findKey(Cfilepath);
findSwitchAndCase(Cfilepath);
processElse(Cfilepath,3);
case 4:
findKey(Cfilepath);
findSwitchAndCase(Cfilepath);
processElse(Cfilepath,4);
break;
}
Second, level 1, this is the function code for extract the word to store in an array.
public static void findKey(String s) {
String keywords = "char,short,int,long,signed,unsigned,float,double,struct,union,enum,void,"
+ "for,do,while,break,continue,if,else,goto,switch,case,default,return,"
+ "auto,extern,register,static,typedef,const,sizeof,volatile";
Pattern p1 = Pattern.compile(",");
String[] cKeyword = p1.split(keywords);
int totalNum = 0;
for (String oneKey : cKeyword) {
Pattern p = Pattern.compile(oneKey + "[^a-z]");
Matcher matcher = p.matcher(s);
while (matcher.find()) {
totalNum++;
}
}
System.out.println("total num: " + totalNum);
}
Third, level 2, find the switch and output the number of "case" corresponding to each group.
public static void findSwitchAndCase(String s) {
Pattern p1 = Pattern.compile("switch");
String[] switchs = p1.split(s);
int switchNum = switchs.length - 1;
System.out.println("switch num: " + switchNum);
System.out.print("case num:");
for (int i = 1; i < switchs.length; i++) {
Pattern p2 = Pattern.compile("case");
String[] cases = p2.split(switchs[i]);
int caseNum = cases.length - 1;
System.out.print(" " + caseNum);
}
System.out.println("");
}
Fourth, level 3 and level 4, find the number of else if and if-else if -else.
public static void processElse(String code,int level) {
int ifelNum = 0;
int esifNum = 0;
boolean lock = true;
Stack stack = new Stack();
Pattern pattern = Pattern.compile("else if|if|else");
Matcher matcher = pattern.matcher(code);
while (matcher.find()) {
String temp = matcher.group();
if ("if".equals(temp)) {
stack.push(temp);
} else if ("else if".equals(temp)) {
stack.push(temp);
} else if ("else".equals(temp)) {
if ("if".equals(stack.peek())) {
stack.pop();
ifelNum++;
} else {
while (!"if".equals(stack.peek())) {
stack.pop();
if (lock == true) {
esifNum++;
lock = false;
}
}
stack.pop();
lock = true;
}
}
}
if (level == 3) {
System.out.println("if-else num: " + ifelNum);
} else if (level == 4) {
System.out.println("if-else num: " + ifelNum);
System.out.println("if-elseif-else: " + esifNum);
} else {
}
}
level 1:
level 2:
level 3 and level 4:
As shown in the figure below, there were problems with the previous codes at level 3 and level 4. After communicating with team members, we found the problem and corrected the code errors in time.
Through this experiment, I understand that in order to complete the experiment faster and better, we need to make a preliminary plan first, and we need to have a full understanding and analysis of the subject. Draw program flow chart, convenient for us to complete the implementation of the code. At the same time, when there is a problem in the experimental code, we should timely discuss the problem with the team members and solve the problem in time.