diff --git a/131802125/README.md b/131802125/README.md new file mode 100644 index 00000000..7636d814 --- /dev/null +++ b/131802125/README.md @@ -0,0 +1,17 @@ +# 项目描述 +[toc] +--- +## 如何运行 +>先从cmd窗口进入src文件夹,利用javac WordCount.java 再使用java WordCount input.txt output.txt来运行项目。 + +## 功能简介 +>1. 对输入的input.txt的文本进行字符数统计。 +>2. 对文本的单词数统计 +>3. 对文本的有效行数进行统计 +>4. 对统计频率最高的10个单词进行输出,如果同频率则按字典序小的在前。 + +## 作业连接 +[软工实践寒假作业(2/2)](https://www.cnblogs.com/cj-whales/p/14488427.html) + +## 博客连接 +[我的博客](https://www.cnblogs.com/cj-whales/) \ No newline at end of file diff --git a/131802125/codestyle.md b/131802125/codestyle.md new file mode 100644 index 00000000..aa74f21a --- /dev/null +++ b/131802125/codestyle.md @@ -0,0 +1,63 @@ +# 代码规范制定 +[toc] +--- +## 缩进 +>程序块要采用缩进风格编写,缩进的空格数为4个。 +>如果使用TAB键,要设置空格数为4格。 +## 变量命名 +>使用正确的英文单词,可以让读者易于理解,力求简单清楚,避免使用引起误解的词汇和模糊的使人产生误解. +>采用驼峰命名法 +>变量的命名采用“小驼峰法”,如:camelCase,即第一个单词全小写,后面单词首字母大写。 + +## 每行最多字符数 +>较长的语句(>80字符)要分成多行书写。 +## 函数最大行 +>不超过120行 +## 函数、类命名 +>函数的命名,使用英文单词尽量可以描述该函数主要功能,可采用(动-名)或者(谓-宾)的结构。 +>二者皆采用大驼峰命名法,即所有单词首字母都大写。 + +>如下形式例外(领域模型的相关命名): +正例:MarcoPolo / UserDO / XmlService / TcpUdpDeal / TaPromotion +反例:macroPolo / UserDo / XMLService / TCPUDPDeal / TAPromotion +## 常量 +>常量命名全部大写,单词间用下划线隔开,力求语义表达完整清楚,不要嫌名字长。 +## 空行规则 +>1. 相对独立的程序块之间要使用空行分开; +>2. 变量声明应尽可能靠近第一次使用处,避免一次性声明一组没有马上使用的变量。 +>3. 函数间要使用空行分开; +>4. 每个类声明之后应该加入空格同 +其他代码分开 +## 注释规则 +>1. 注释应与其描述的代码相近,对代码的注释应放在其上方或右方(对单条语句的 +注释)相邻位置,不可放在下面,如放于上方则需与其上面的代码用空行隔开。 +>2. 注释要与内容有相同的缩排 +>3. 注释要与上面无关的代码用空行隔开 +>4. 变量与常量的注释放在其右方 +>5. 在源文件头部应列出,生成日期、作者、代码的主要功能。 +## 操作符前后空格 +>1. 值操作符、比较操作符、算术操作符、逻辑操作符、位域操作符,如“=”、“+=”、“>=”、“+”、“*”、“%”、“&&”、“||”、“<<”、“^”等二元操作符前后应当加空格。 +>2. 一元操作符“!”、“~”、“++”、“--”、“&”等前后不加空格。 +>3. 如“[]”、“.”、“->”这类操作符前后不加空格 +## 其他规则 +1. 关于命名的缩写: +>较短的单词可以通过去掉“元音”形成缩写;较长的单词可取单词头几个字母形成缩写;一些单词有公认的缩写,如: +temp 可缩写为 tmp ; +flag 可缩写为 flg ; +statistic 可缩写为 stat ; +increment 可缩写为 inc ; +message 可缩写为 msg ; +2. 程序的分界符如“{ }”要独占一行。如: +>for (...) +{ + ... // program code +} +if (...) +{ + ... // program code +} +void example_fun( void ) +{ + ... // program code +} +3. if、for、do、while、case、switch、default等语句自占一行,且if、for、do、while等语句的执行语句部分无论多少都要加括号{}。 \ No newline at end of file diff --git a/131802125/src/CompareRule.java b/131802125/src/CompareRule.java new file mode 100644 index 00000000..7d2ce11d --- /dev/null +++ b/131802125/src/CompareRule.java @@ -0,0 +1,28 @@ +import java.util.*; + +public class CompareRule implements Comparator +{ + public int compare(Word aWord,Word bWord) + { + if(aWord.GetFrequent() > bWord.GetFrequent()) //词频大的在前 + { + return -1; + } + else if(aWord.GetFrequent() == bWord.GetFrequent()) + { + if(aWord.GetWords().compareTo(bWord.GetWords()) < 0) //字典序小的在前 + { + return -1; + } + else + { + return 0; + } + } + else + { + return 1; + } + + } +} diff --git a/131802125/src/Function.java b/131802125/src/Function.java new file mode 100644 index 00000000..959ab8f9 --- /dev/null +++ b/131802125/src/Function.java @@ -0,0 +1,275 @@ +import java.util.*; +import java.io.*; + +public class Function { + public Function() + { + } + + public boolean IsEmptyLine(String wordLine) + { + if(wordLine.replaceAll("\\s*", "").equals("")) //替换掉输入行的空格、制表、换页符后是否为空 + { + return true; + } + else + { + return false; + } + } + + public int CountChar(File readFile) + { + int charNum=0; //用于统计字符数 + + try + { + if (readFile.isFile() && readFile.exists()) + { + FileInputStream fileIn = new FileInputStream(readFile); + int readChar =0; + while((readChar = fileIn.read())!=-1) //每读入一个字符,字符数自增1 + { + charNum++; + } + + fileIn.close(); + } + + } + catch(Exception e) + { + System.out.println("没有找到文件"); + e.printStackTrace(); + } + + return charNum; + + } + + public int CountLine(File readFile) + { + int lineNum=0; //用于统计有效行数 + try + { + if (readFile.isFile() && readFile.exists()) + { + InputStreamReader inReader = new InputStreamReader( + new FileInputStream(readFile)); + BufferedReader bufferedReader = new BufferedReader(inReader); + String wordLine; + + while((wordLine=bufferedReader.readLine())!=null) + { + if(!IsEmptyLine(wordLine)) + { + lineNum++; + } + } + } + + } + catch(Exception e) + { + System.out.println("没有找到文件"); + e.printStackTrace(); + } + + return lineNum; + } + + public int CountWord(File readFile) + { + int wordNum=0; //用于统计单词数 + int wordLength=0; //用于判断是否为一个单词,既4个英文字母开头 + int resetWord=0; //用于判断是否重新开始一个单词读入 + int isNotWord=0; //与wordLength共同作用,判断是不是一个单词 + + try + { + if (readFile.isFile() && readFile.exists()) + { + String wordLine; + FileInputStream fileIn = new FileInputStream(readFile); + int readChar=0; + String word=""; //用于拼接读入的字符成为单词 + + while((readChar = fileIn.read())!=-1) //每读入一个字符,字符数自增1 + { + resetWord = 0; + + if((readChar>='a'&&readChar<='z') + ||(readChar>='A'&&readChar<='Z') + ||(readChar>='0'&&readChar<='9')) + { + if(readChar>='0'&&readChar<='9') + { + if(wordLength>=4) + { + char[] ch = new char[1]; + ch[0] = (char)readChar; + word += ch[0]; + wordLength++; + } + else + { + isNotWord = 1; + } + } + else + { + char[] ch = new char[1]; + ch[0] = (char)readChar; + word += ch[0]; + wordLength++; + } + } + else + { + if(wordLength>=4 && isNotWord != 1) + { + wordNum++; + } + isNotWord = 0; + resetWord = 1; + } + + if(resetWord==1) + { + word = ""; + wordLength = 0; + } + } + if(wordLength>=4) + { + wordNum++; + } + } + + } + catch(Exception e) + { + System.out.println("没有找到文件"); + e.printStackTrace(); + } + + return wordNum; + } + + public Vector CountFrequentWord(File readFile) + { + int wordLength=0; //用于判断是否为一个单词,既4个英文字母开头 + int resetWord=0; //用于判断是否重新开始一个单词读入 + int isNotWord=0; //与wordLength共同作用,判断是不是一个单词 + + Vector allWords = new Vector(); + int noRepeatWordNum = 0; + try + { + if (readFile.isFile() && readFile.exists()) + { + String wordLine; + FileInputStream fileIn = new FileInputStream(readFile); + int readChar=0; + String word=""; //用于拼接读入的字符成为单词 + + while((readChar = fileIn.read())!=-1) //每读入一个字符,字符数自增1 + { + resetWord = 0; + + if((readChar>='a'&&readChar<='z') + ||(readChar>='A'&&readChar<='Z') + ||(readChar>='0'&&readChar<='9')) + { + if(readChar>='0'&&readChar<='9') + { + if(wordLength>=4) + { + char[] ch = new char[1]; + ch[0] = (char)readChar; + word += ch[0]; + wordLength++; + } + else + { + isNotWord = 1; + } + } + else + { + char[] ch = new char[1]; + ch[0] = (char)readChar; + word += ch[0]; + wordLength++; + } + } + else + { + if(wordLength>=4 && isNotWord !=1) + { + word = word.toLowerCase(); + int index = FindWord(allWords,word); //查找有是否重复,重复则返回下标 + if(index!=-1) + { + allWords.get(index).AddFrequent(); + } + else + { + noRepeatWordNum++; //用于计数总共有多少个单词存入了已经 + Word aWord = new Word(word,1); + allWords.add(aWord); + } + } + isNotWord = 0; + resetWord = 1; + } + + if(resetWord==1) + { + word = ""; + wordLength = 0; + } + } + + /*防止最后一次读入的是合理的字符,导致最后一个单词没有计入数据*/ + if(wordLength>=4 && isNotWord != 1) + { + word = word.toLowerCase(); + int index = FindWord(allWords,word); //查找有是否重复,重复则返回下标 + if(index!=-1) + { + allWords.get(index).AddFrequent(); + } + else + { + noRepeatWordNum++; //用于计数总共有多少个单词存入了已经 + Word aWord = new Word(word,1); + allWords.add(aWord); + } + } + + Comparator cmp = new CompareRule(); + Collections.sort(allWords,cmp); + } + + } + catch(Exception e) + { + System.out.println("没有找到文件"); + e.printStackTrace(); + } + return allWords; + } + + public int FindWord(Vector allWords,String word) + { + for(int i = 0;i allWords = functionMethod.CountFrequentWord(readFile); + + /*输出文件部分*/ + String outMsg = ""; + outMsg+="characters: "+charNum+"\n"; + outMsg+="words: "+wordNum+"\n"; + outMsg+="lines: "+lineNum+"\n"; + if(allWords.size() <= 10) + { + for(int i = 0;i < allWords.size();i++) + { + outMsg+=allWords.get(i).GetWords()+": " + +allWords.get(i).GetFrequent()+"\n"; + } + } + else + { + for(int i = 0;i < 10;i++) + { + outMsg+=allWords.get(i).GetWords()+": " + +allWords.get(i).GetFrequent()+"\n"; + } + } + try + { + File outFile = new File(outFileName); + PrintStream printStream = new PrintStream(new FileOutputStream(outFile)); + + printStream.print(outMsg); + } + catch(Exception e) + { + System.out.println("没有找到文件"); + e.printStackTrace(); + } + } +} \ No newline at end of file