EE301_LAB1-2
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 | Issue an self-introduction |
MU STU ID and FZU STU ID | MU: 20122977 FZU: 832002209 |
Github Repository
https://github.com/SCP-ZZJ/EE308FZ/tree/main/lab1
1. Give the PSP form for this work.
Framework activities: | what I could do | ESTIMATE TIME(MINUTE) |
---|---|---|
Planning | Understand the meaning of the problem, consider certain way to solve the question. | 30 |
design | Planning and visualizing my thoughts of solution | 120 |
Code Review | reexamination&debugging | 40 |
Test | Try different input data | 20 |
Improvement Plan | Check overall codes and add/revise comments | 30 |
2. Description of problem-solving ideas. This is the process of how to think and how to find information after getting the title at the beginning.
I chose to use regular expression for detecting keywords of C language; using array to store the data file.
So I could then using Regex to detect the keywords and count their times of occurrence while iterating the whole array.
For if-else/if-else if-else, using stack to handle.
3. Design and implementation process. The design includes how the code is organized and the flow chart of the key functions.
- Determine the particular regular expression
- Input data(C file) – Read every line of the file
- Determine if the line matches the regular expression(if matches, counter++)
- Using stack to contain “if, else…” structures(Property: First in Last out)
- Display the outcome
4. Code description. Show the key code of the project and explain the idea.
Level 1 for all keywords
public static void Level1(String[] keyArr,String code){
int total_num = 0;
for(int i=0; i<keyArr.length; i++) {
Pattern p=Pattern.compile("[^a-z]"+keyArr[i]+"[^a-z]");
Matcher matcher=p.matcher(code);
while(matcher.find()) {
total_num++;
}
}
System.out.println("total num: "+total_num);
}
Level 2 for switch-case
//check switch-case
public static void Level2(String[] keyArr, String code){
Level1(keyArr,code); // add extra function based on level1
//check switch
int switch_num = 0;
Pattern p=Pattern.compile("switch");
Matcher matcher=p.matcher(code);
while(matcher.find()) {
switch_num++;
}
//check case
p=Pattern.compile("switch.*?}");
matcher=p.matcher(code);
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: "+switch_num);
System.out.print("case num: ");
for(int i=0;i<case_num.size();i++) {
System.out.print(case_num.get(i)+" ");
}
System.out.println();
}
Level 3+ for if, else if, else … structures
public static void Level3(String[] keyArr, String code) {
Level2(keyArr, code);
Pattern p = Pattern.compile("if|else if|else");
Matcher matcher = p.matcher(code);
Stack<String> stack = new Stack();
int ifElseNum = 0;
int ifElseIfNum = 0;
boolean checkElseIf = false;
while (matcher.find()) {
String string = code.substring(matcher.start(), matcher.end());
if (string.equals("if")) {
stack.push(string);
}
else if (string.equals("else if")){
stack.push(string);
}
else{ // string == "else"
while (!stack.isEmpty()){
String s= stack.pop();
if(s.equals("else if")){
checkElseIf = true;
}else{ // s == "if"
if(checkElseIf == true){
ifElseIfNum++;
checkElseIf = false;
}else{
ifElseNum++;
break;
}
}
}
}
}
System.out.println("if-else num: " + ifElseNum);
System.out.println("if-elseif-else num: " + ifElseIfNum);
}
5&6. Unit test screenshots and description.
*Meet the requirement of level 3+
7. Summarize this assignment.
From designing small functions to developing big projects, it is crucial to do preliminary works such as learning how to access to coding related platforms like Github, reading instructors to formulate my own code specifications.
Everyone’s thinking mode is different, we can get to know various solutions to a single problem by communication, which may improve our efficiency.