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 | find keyword num and structure num |
MU STU ID and FZU STU ID | 20123809_832002202 |
EE308FZ Lab1_2
Ⅰ、Program Requirements
- Basic requirement: output “keyword” statistics
- Advanced requirement: output the number of “switch case” structures, and output the number of “case” corresponding to each group
- Uplifting requirement: output the number of “if else” structures
- Ultimate requirement: output the number of “if, else if, else” structures
提示:以下是本篇文章正文内容,下面案例可供参考
Ⅱ、The basic idea flowchart
1.Get total number of keywords
2.Get total number of switch-case structure
2.1find the position of switch
2.2find the position of default
2.3count the case between switch and default
3.Get total number of if-else structure
4.Get total number of if-elseif-else structure
Ⅲ、Source Code
1.Level1 implementation:Get total number of keywords
String word="abstract default goto* switch boolean do if package nchronzed break double implements private this byte else import protected throw throws case extends instanceof public transient catch int return char final interface short try class finally long static void const* float native strictfp volatile continue for new super while assert enum";
String arr[]=word.split(" ");
String arr1[]= {"if","else","else if"};
File file=new File("C:\\Users\\lenovo\\Desktop\\lab1code.txt");
int times = 0;//出现的次数
LineNumberReader lineReader = null;
lineReader = new LineNumberReader(new FileReader(file));
String readLine = null;
while((readLine =lineReader.readLine()) != null){
//判断每一行中,出现关键词的次数
String keyword;
for(int i=0;i<arr.length;i++) {
keyword=arr[i];
int next = 0; //定义开始查找关键字的序列号
int index = 0; //获得readLine的对象值
while((index = readLine.indexOf(keyword,next)) != -1&&readLine.charAt(index+keyword.length())!='u') { //从每行的第0个索引开始遍历关键字
next = index + keyword.length(); //下一次的遍历序号为序列号+关键字长度
times++;//次数加1
}
}
}
lineReader.close();
2.Get total number of switch-case structure
2.1find the position of switch
public static int findSwitch(File file,int lineNum) throws IOException {
LineNumberReader lineReader;
lineReader=new LineNumberReader(new FileReader(file));
String readLine=null;
int count=0;
int numSwitch=0;
String keyword="switch";
while((readLine =lineReader.readLine()) != null){
count++;
if(count>lineNum){
int next = 0; //定义开始查找关键字的序列号
int index = 0; //获得readLine的对象值
while((index = readLine.indexOf(keyword,next)) != -1) { //从每行的第0个索引开始遍历关键字
numSwitch++;//次数加1
break;
}
}
if(numSwitch>=1) {
break;
}
}
lineReader.close();
if(numSwitch!=0) {
return count;
}else {
return numSwitch;
}
}
2.2find the position of default
public static int findDefault(File file,int lineNum) throws IOException {
String readLine=null;
LineNumberReader lineReader;
lineReader = new LineNumberReader(new FileReader(file));
int count=0;
int numDefault=0;
String keyword="default";
while((readLine =lineReader.readLine()) != null){
count++;
if(count>lineNum) {
int next = 0; //定义开始查找关键字的序列号
int index = 0; //获得readLine的对象值
while((index = readLine.indexOf(keyword,next)) != -1) { //从每行的第0个索引开始遍历关键字
numDefault++;//次数加1
break;
}
}
if(numDefault>=1) {
break;
}
} lineReader.close();
return count;
}
2.3count the case between switch and default
public static int countCase(File file,int lineNumS,int lineNumD) throws IOException {
String readLine=null;
LineNumberReader lineReader;
lineReader = new LineNumberReader(new FileReader(file));
int count=0;
int numCase=0;
String keyword;
keyword="case";
while((readLine =lineReader.readLine()) != null){
count++;
if(count>lineNumS&&count<lineNumD) {
int next = 0; //定义开始查找关键字的序列号
int index = 0; //获得readLine的对象值
while((index = readLine.indexOf(keyword,next)) != -1) { //从每行的第0个索引开始遍历关键字
numCase++;//次数加1
break;
}
}
}
lineReader.close();
return numCase;
}
3.Level3 implementation:Find the amount of if-else structure
public static int findIfElse(File file,String arr1[]) throws IOException {
LineNumberReader lineReader;
lineReader = new LineNumberReader(new FileReader(file));
String readLine = null;
Stack<String> stack = new Stack<String>();
int count=0;
while((readLine =lineReader.readLine()) != null){
int next=0;
int index=0;
if((index = readLine.indexOf("if",next)) != -1&&readLine.charAt(index-2)!='e') {
stack.push("if");
}else if((index = readLine.indexOf("else",next)) != -1&&readLine.charAt(index+5)!='i'){
if(stack.isEmpty()) continue;
String top=stack.pop();
if(top=="if")
count++;
}else if((index = readLine.indexOf("else if",next)) != -1){
stack.push("else if");
}
}
lineReader.close();
return count;
}
4.Level4 implementation:Find the amount of if-elseif-else structure
public static int findIfElseIfElse(File file,String arr1[]) throws IOException {
LineNumberReader lineReader;
lineReader = new LineNumberReader(new FileReader(file));
String readLine = null;
Stack<String> stack = new Stack<String>();
int count=0;
int count1=0;
while((readLine =lineReader.readLine()) != null){
int next=0;
int index=0;
if((index = readLine.indexOf("if",next)) != -1&&readLine.charAt(index-2)!='e') {
stack.push("if");
}else if((index = readLine.indexOf("else",next)) != -1&&readLine.charAt(index+5)!='i'){
if(stack.isEmpty()) continue;
String top=null;
while((top=stack.pop())=="else if") {
count1++;
}
if(count1>=1&&top=="if") {
count++;
count1=0;
}
}else if((index = readLine.indexOf("else if",next)) != -1){
stack.push("else if");
}
}
lineReader.close();
return count;
}
Ⅳ、Summary
In this experiment, I mainly use simple judgment statements, and the code is too complicated, and the running speed is greatly reduced. In the future, I will continue to learn and improve my code writing ability.