diff --git a/201621123050/test/.classpath b/201621123050/test/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..97f72100ac9f2489ec137c679343a32d41834cc5 --- /dev/null +++ b/201621123050/test/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/201621123050/test/.project b/201621123050/test/.project new file mode 100644 index 0000000000000000000000000000000000000000..b0299dbe7631bd85179c8571c7b86da2b28dc7b4 --- /dev/null +++ b/201621123050/test/.project @@ -0,0 +1,17 @@ + + + test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/201621123050/test/.settings/org.eclipse.jdt.core.prefs b/201621123050/test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000000000000000000000000000000000..3a21537071bf4118b9e1ee864cb4bc258aa48211 --- /dev/null +++ b/201621123050/test/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/201621123050/test/bin/test/FileToStr.class b/201621123050/test/bin/test/FileToStr.class new file mode 100644 index 0000000000000000000000000000000000000000..82b6f52e9b59f1d91c4183cf92c4210326a97090 Binary files /dev/null and b/201621123050/test/bin/test/FileToStr.class differ diff --git a/201621123050/test/bin/test/main.class b/201621123050/test/bin/test/main.class new file mode 100644 index 0000000000000000000000000000000000000000..a9270864ddf4009b1cb0a96cf81c8427a7d04f39 Binary files /dev/null and b/201621123050/test/bin/test/main.class differ diff --git a/201621123050/test/bin/test/wordcount.class b/201621123050/test/bin/test/wordcount.class new file mode 100644 index 0000000000000000000000000000000000000000..277739305ae7855b0115224fa4fe9f550e7fd2d8 Binary files /dev/null and b/201621123050/test/bin/test/wordcount.class differ diff --git a/201621123050/test/bin/test/wordcountTest.class b/201621123050/test/bin/test/wordcountTest.class new file mode 100644 index 0000000000000000000000000000000000000000..4084dd2fc35326a9800e7d2cb22b39b0997d45f3 Binary files /dev/null and b/201621123050/test/bin/test/wordcountTest.class differ diff --git a/201621123050/test/result.txt b/201621123050/test/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..08b2ac6c204e65874569bd8dfebdb65ecf1418e1 --- /dev/null +++ b/201621123050/test/result.txt @@ -0,0 +1,8 @@ +字符数=1027 +空格数=238 +水平字符数=40 +换行符数=40 +均算字符数=1345 +行数=40 +单词数=290 +有效行数=41 \ No newline at end of file diff --git a/201621123050/test/src/test/FileToStr.java b/201621123050/test/src/test/FileToStr.java new file mode 100644 index 0000000000000000000000000000000000000000..0a693494be2089aaa465aa8c96ba4addd47f5872 --- /dev/null +++ b/201621123050/test/src/test/FileToStr.java @@ -0,0 +1,34 @@ +package test; + +import java.io.*; + +public class FileToStr { // 该类用于文件写入读取的处理 + + public String FileToString(String path) throws IOException { // 将文件转化为字符串 + File file = new File(path); + if (!file.exists() || file.isDirectory()) { + System.out.println("请输入正确文件名!"); + throw new FileNotFoundException(); + } + FileInputStream fis = new FileInputStream(file); + byte[] buf = new byte[1024]; + StringBuffer sb = new StringBuffer(); + while ((fis.read(buf)) != -1) { + sb.append(new String(buf)); + buf = new byte[1024];// + } + fis.close(); + return sb.toString(); + } + + public void WriteToFile(String str) throws IOException { // 将最后结果写入文件 + File writename = new File("result.txt"); // 相对路径,如果没有则要建立一个新的文件 + writename.createNewFile(); // 创建新文件 + BufferedWriter out = new BufferedWriter(new FileWriter(writename)); + out.write(str); + + out.flush(); // 把缓存区内容压入文件 + out.close(); // 关闭文件 + } + +} diff --git a/201621123050/test/src/test/main.java b/201621123050/test/src/test/main.java new file mode 100644 index 0000000000000000000000000000000000000000..3df61b5b0142d38cbdba6a3c842bdee4cb1384f1 --- /dev/null +++ b/201621123050/test/src/test/main.java @@ -0,0 +1,29 @@ +package test; + +import test.FileToStr; +import test.wordcount; +import java.io.IOException; +import java.util.*; + +public class main { + public static void main(String[] args) throws IOException { + Scanner sc = new Scanner(System.in); + String file = sc.next(); + String wordLong = sc.next(); + FileToStr fd = new FileToStr(); + String text = fd.FileToString(file); + String str = new String(); + wordcount wt = new wordcount(text); + wt.getCharacterCount(); // 字符、空格、制表、换行统计 + wt.getWordCount(); // 单词统计 + wt.getLineValidate(); // 有效行数统计 + System.out.println(wt.text); + wt.countLong(file); + str = "字符数=" + wt.charCount+"\n空格数=" + wt.blankCount+"\n水平字符数=" + wt.tabCount+"\n换行符数=" + + wt.enterCount+"\n均算字符数=" + wt.total+"\n行数=" + wt.lineCount+"\n单词数=" + wt.getWordCount()+"\n有效行数=" + wt.lineValidate; + fd.WriteToFile(str); + System.out.println(str); + + sc.close(); + } +} diff --git a/201621123050/test/src/test/wordcount.java b/201621123050/test/src/test/wordcount.java new file mode 100644 index 0000000000000000000000000000000000000000..f0cda31ca5e43f3beb9f046a8d9c37e69a974a8f --- /dev/null +++ b/201621123050/test/src/test/wordcount.java @@ -0,0 +1,241 @@ +package test; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.StringTokenizer; + +import org.junit.experimental.theories.Theories; + +public class wordcount { + String text; + int charCount; // 字符统计 + int blankCount; // 空格统计 + int tabCount; // 水平字符Count + int enterCount; // 换行符Count + int total; // 均算字符统计 + int noCount; // 非字母数字统计 + int lineCount; // 行数统计 + int wordCount; // 单词统计 + int lineValidate; // 有效行数 + String[] word = new String[10000000]; + int[] wordRep = new int[10000000]; + int wordlong = 4; + HashMap hashMap = new HashMap(); + + public int[] getWordRep() { // 单词按字典排序 + for (int i = 0; i < word.length - 1; i++) { + if (word[i].equals(null)) + break; + for (int j = i + 1; i < word.length; j++) { + if (word[j].equals(null)) + break; + if (word[i].equals(word[j])) + wordRep[i]++; + for (int k = j; k < word.length; k++) { + if (word[j].equals(null)) + break; + if (k == word.length) + continue; + word[k] = word[k + 1]; + } + } + } + return wordRep; + } + + public void setWordRep(int[] wordRep) { + this.wordRep = wordRep; + } + + public int getLineValidate() { // 有效行数统计 + String[] line = text.split("\n"); + char c = '\0'; + boolean flag = true; + for (int i = 0; i < line.length; i++) { + for (int j = 0; j < line[i].length(); j++) { + c = line[i].charAt(j); + if (c == ' ') { + flag = false; + } else { + flag = true; + break; + } + } + if (flag) + lineValidate++; + flag = true; + } + return lineValidate; + } + + public void setLineValidate(int lineValidate) { + this.lineValidate = lineValidate; + } + + public wordcount(String text) { + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public int getCharCount() { + return charCount; + } + + public void setCharCount(int charCount) { + this.charCount = charCount; + } + + public int getTabCount() { + return tabCount; + } + + public void setTabCount(int tabCount) { + this.tabCount = tabCount; + } + + public int getEnterCount() { + return enterCount; + } + + public void setEnterCount(int enterCount) { + this.enterCount = enterCount; + } + + public int getTotal() { + return total; + } + + public void setTotal(int total) { + this.total = total; + } + + public int getBlankCount() { + return blankCount; + } + + public void setBlankCount(int blankCount) { + this.blankCount = blankCount; + } + + public int getNoCount() { + return noCount; + } + + public void setNoCount(int noCount) { + this.noCount = noCount; + } + + public int getLineCount() { + return lineCount; + } + + public void setLineCount(int lineCount) { + this.lineCount = lineCount; + } + + public void setWordCount(int wordCount) { + this.wordCount = wordCount; + } + + public void getCharacterCount() { // 字符、空格、制表、换行统计 + char c = '\0'; + for (int i = 0; i < text.length(); i++) { + c = text.charAt(i); + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { + charCount++; + } else if (c == ' ') { + blankCount++; + } else if (c == '\r') { + tabCount++; + } else if (c == '\n') { + enterCount++; + lineCount++; + } + } + total = charCount + blankCount + tabCount + enterCount; + } + + public int getWordCount() { // 单词统计 + char c = '\0'; + int j = 0; + word[0] = ""; + boolean flag = false; + for (int i = 0; i < text.length(); i++) { + c = text.charAt(i); + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { + word[j] = word[j] + c; + flag = true; + } else if (flag == true) { + j++; + flag = false; + } + } + return j; + } + + public void orderWord() { // 单词频数 + String temp; + for (int i = 0; i < word.length - 1; i++) { + for (int j = i; j < word.length; j++) { + if (word[i].compareTo(word[j]) > 0) { + temp = word[i]; + word[i] = word[j]; + word[j] = temp; + } + } + } + } + + + public void countLong(String path) { + + + + String regex = "[【】、.。,\"!--;:?\'\\]]"; + try { + //读取要处理的文件 + BufferedReader br=new BufferedReader(new FileReader("E:\\eclipse\\workspace\\test\\tagore.txt")); + String value; + while((value=br.readLine())!=null){ + value=value.replaceAll(regex, " "); + //使用StringTokenizer来分词 + StringTokenizer tokenizer = new StringTokenizer(value); + while(tokenizer.hasMoreTokens()){ + String word=tokenizer.nextToken(); + System.out.println(word.length()); + if(word.length()!=wordlong) { + continue; + } + if(!hashMap.containsKey(word)){ + hashMap.put(word, new Integer(1)); + }else{ + int k=hashMap.get(word).intValue()+1; + hashMap.put(word, new Integer(k)); + } + } + } + //遍历HashMap,输出结果 + Iterator iterator=hashMap.keySet().iterator(); + while(iterator.hasNext()){ + String word=(String) iterator.next(); + System.out.println(word+":\t"+hashMap.get(word)); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/201621123050/test/src/test/wordcountTest.java b/201621123050/test/src/test/wordcountTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a3a5cfa585a5c635b7d69f0dd70d354e1a4492bb --- /dev/null +++ b/201621123050/test/src/test/wordcountTest.java @@ -0,0 +1,34 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class wordcountTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testSetLineValidate() { + fail("Not yet implemented"); + } + + @Test + public void testGetCharacterCount() { + fail("Not yet implemented"); + } + + @Test + public void testGetWordCount() { + fail("Not yet implemented"); + } + + @Test + public void testOrderWord() { + fail("Not yet implemented"); + } + +} diff --git a/201621123050/test/tagore.txt b/201621123050/test/tagore.txt new file mode 100644 index 0000000000000000000000000000000000000000..26d9e2e5da92084020801a436bbdeffa6e13d7fc --- /dev/null +++ b/201621123050/test/tagore.txt @@ -0,0 +1,41 @@ +The most distant way in the world +The most distant way in the world +is not the way from birth to the end. +it is when i sit near you +that you don't understand i love u. +The most distant way in the world +is not that you're not sure i love u. +It is when my love is bewildering the soul +but i can't speak it out. +The most distant way in the world +is not that i can't say i love u. +it is after looking into my heart +i can't change my love. +The most distant way in the world +is not that i'm loving u. +it is in our love +we are keeping between the distance +The most distant way in the world +is not the distance across us. +it is when we're breaking through the way +we deny the existance of love. +So the most distant way in the world +is not in two distant trees. +it is the same rooted branches +can't enjoy the co-existance. +So the most distant way in the world +is not in the being sepearated branches. +it is in the blinking stars +they can't burn the light. +So the most distant way in the world +is not the burning stars. +it is after the light +they can't be seen from afar. +So the most distant way in the world +is not the light that is fading away. +it is the coincidence of us +is not supposed for the love. +So the most distant way in the world +is the love between the fish and bird. +one is flying at the sky, +the other is looking upon into the sea \ No newline at end of file