285
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZU202201 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/608734907?spm=1001.2014.3001.6377 |
The Aim of This Assignment | <Extract keywords of different levels from the C or C++ code files> |
MU STU ID and FZU STU ID | <20124279_832001119> |
Program design process | What I do | ESTIMATE TIME(MIN) |
---|---|---|
Planning | Understand the requirements of the question, roughly construct the idea | 20 |
design | Write down the general idea,draw a logical diagram | 20 |
Write the code | Import different data for detection | 120 |
Test | Import different data for detection | 30 |
Improve my code | Summarize and improve the above tests | 30 |
Wring Report | Wring Report | 60 |
https://github.com/pikaquququ/lab1-2/blob/main/lab1-2
1. #include <stdio.h>
2. int main(){
3. int i=1;
4. double j=0;
5. long f;
6. switch(i){
7. case 0:
8. break;
9. case 1:
10. break;
11. case 2:
12. break;
13. default:
14. break;
15. }
16. switch(i){
17. case 0:
18. break;
19. case 1:
20. break;
21. default:
22. break;
23. }
24. if(i<0){
25. if(i<-1){}
26. else{}
27. }
28. else if(i>0){
29. if (i>2){}
30. else if (i==2) {}
31. else if (i>1) {}
32. else {}
33. }
34. else{
35. if(j!=0){}
36. else{}
37. }
38. return 0;
39. }
Overall logic diagram
public static void main(String[] args) throws IOException {
//read the test file
FileReader file = null;
try {
file = new FileReader("C:\\Users\\Desktop\\lab1-2.txt");
} catch (Exception e) {
e.getStackTrace();
}
BufferedReader input = new BufferedReader(file);
String lineString = "";
String tempString = input.readLine();
while(tempString!=null) {
lineString+=tempString;
tempString = input.readLine();
}
solution1(lineString);
solution2(lineString);
solution3_4(lineString);
}
c language keyword table(ANSI C standard C language has 32 keywords)
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
int num = 0;
for(int i = 0; i < keywords.length; i++) {
String regExpress = "[^a-zA-Z_]"+keywords[i]+"[^a-zA-Z_]";
Pattern pattern = Pattern.compile(regExpress);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
num++;
}
}
System.out.println("The number of keywords is "+num+".");
System.out.println();
}
public static void solution2(String input) {
int num_switch = 0;
String regExpress = "[^a-zA-Z_]"+"switch"+"[^a-zA-Z_]";
Pattern pattern1 = Pattern.compile(regExpress);
Matcher matcher1 = pattern1.matcher(input);
while(matcher1.find()) {
num_switch++;
}
System.out.println("The number of 'switch' is "+num_switch+".");
int num_case = 0;
int num = 0;
String regExpress2 = "switch.*?}";
Pattern pattern2 = Pattern.compile(regExpress2);
Matcher matcher2 = pattern2.matcher(input);
while(matcher2.find()) {
String regExpress3 = "[^a-zA-Z_]"+"case"+"[^a-zA-Z_]";
String tempText=matcher2.toString();
Pattern pattern3 = Pattern.compile(regExpress3);
Matcher matcher3 = pattern3.matcher(tempText);
while(matcher3.find()) {
++num_case;
}
num++;
System.out.println("The number of 'case' in switch "+ num +" is "+num_case+".");
num_case = 0;
}
System.out.println();
}
public static void solution3_4(String input){
Stack<String> s = new Stack<String>();
int num_if_else = 0;
int num_if_elseif_else = 0;
String regExpress = "else *if|else|if";
Pattern pattern = Pattern.compile(regExpress);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
String sub=input.substring(matcher.start(),matcher.end());
if(sub.equals("if")||sub.equals("else if")){
s.push(sub);
}else {
//s.pop() is 'if'
if(s.pop().equals("if")) {
num_if_else++;
}
//s.pop() is 'else if'
else{
num_if_elseif_else++;
//Keep popping the top of the stack until the top is 'if'
while(s.peek().equals("else if")) {
s.pop();
}
s.pop();
}
}
}
System.out.println("The number of 'if-else' is "+num_if_else+".");
System.out.println("The number of 'if-elseif-else' is "+num_if_elseif_else+".");
}
}
The result are all the correct with the reference ,Using regular expressions can simplify the code as much as possible, and in terms of code optimization, I use the Vector class to replace the previous string, which can save space.Because I have not used Java programming much, so I am not very skilled, took a long time, but at the same time got a lot of improvement