285
社区成员
发帖
与我相关
我的任务
分享
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 | Slove the problem of Lab1-2 |
MU STU ID and FZU STU ID | 20123311 832002216 |
github address
https://github.com/HaooLi/EE308-LAB1-2
Framework activities: | what I did | ESTIMATE TIME(MINUTE) |
---|---|---|
Plan | Understand the meaning of the problem, think of a rough solution process | 40 |
design | Program the idea and write a rough outline | 150 |
Code Review | Check for compliance with programming specifications and check for syntax errors | 25 |
Test | Import different data for detection | 20 |
Postmortem & Process Improvement Plan | Do an overall review to make sure the program works | 10 |
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";
String []keyArr=keywords.split("、");
public static void findKey(String[] keyArr,String c){
int key_num = 0;
for(int i = 0; i<keyArr.length; i++) {
Pattern p=Pattern.compile("[^a-z]"+keyArr[i]+"[^a-z]");
Matcher matcher=p.matcher(c);
while(matcher.find()) {
key_num++;
}
}
System.out.println("key num is "+key_num);
}
level 2: find the switch and output the number of "case" corresponding to each group
public static void findSwitch(String c){
//check switch
Pattern p=Pattern.compile("switch");
Matcher matcher=p.matcher(c);
while(matcher.find()) {
switch_num++;
}
//find the number of case
p=Pattern.compile("switch.*?}");
matcher=p.matcher(c);
List case_num=new ArrayList();
while(matcher.find()) {
String tempText=matcher.toString();//get one switch section
Pattern temp_p=Pattern.compile("case");
Matcher temp_matcher=temp_p.matcher(tempText);
int temp_case_num=0;
while(temp_matcher.find()) {
temp_case_num++;
}
case_num.add(temp_case_num);
}
System.out.println("switch num is "+switch_num);
System.out.print("case num are ");
for(int i=0;i<case_num.size();i++) {
System.out.print(case_num.get(i)+" ");
}
System.out.println();
}
level 3 and 4: find else if and if-else if -else
public static void processElse(String c) {
Pattern p = Pattern.compile("else\\s*if|else|if");
Matcher matcher=p.matcher(c);
Stack<String> s = new Stack();
while(matcher.find()) {
String temp=c.substring(matcher.start(),matcher.end());
s.push(temp);
}
Stack<String> res = new Stack<String>();
boolean flag = false;
while (!s.isEmpty()) {
String temp = s.pop();
if (temp.equals("else")) {
res.push(temp);
} else if (temp.equals("else if")) {
res.push(temp);
} else {
while (res.peek().equals("else if")) {
res.pop();
// make a little notation that its from else if, not else
flag = true;
}
if (res.peek().equals("else")) {
res.pop();
}
//if it's elseif ,add to ifElseIfNum
if (flag) {
ifElseIfNum++;
flag = false;
} else {
ifElseNum++;
}
}
}
System.out.println("if-else num: " + ifElseNum);
System.out.println("if-elseif-else: " + ifElseIfNum);
}
In the process of adding all the keywords and counting the numbers, I chose to use RE and stack for the statistics. This greatly reduces the amount of code and is easy to understand.
Through this experiment, I understand that if a project wants to be completed quickly and better, first of all we need to make a preliminary plan, and we need to have a full understanding and analysis of the subject. Draw the flowchart of the program to facilitate us to complete the implementation of the code.