EE308FZ Lab1_2

The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZU202201
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/608734907
The Aim of This Assignmentfind keyword num and structure num
MU STU ID and FZU STU ID20123809_832002202


Ⅰ、Program Requirements

  1. Basic requirement: output “keyword” statistics
  2. Advanced requirement: output the number of “switch case” structures, and output the number of “case” corresponding to each group
  3. Uplifting requirement: output the number of “if else” structures
  4. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值