From 120c58c040677631aa0d44c0761642518d85b149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=AA0010?= <1357146670@qq.com> Date: Sat, 15 Sep 2018 13:06:51 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20201621123001/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123001/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 201621123001/.keep diff --git a/201621123001/.keep b/201621123001/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From 6fafcc00c8dacdcd423a3123e3cde92073b2d08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=AA0010?= <1357146670@qq.com> Date: Sat, 15 Sep 2018 13:07:00 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20src/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123001/src/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 201621123001/src/.keep diff --git a/201621123001/src/.keep b/201621123001/src/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From bf19f7b85ee060c169808211702f42cec15ec272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=AA0010?= <1357146670@qq.com> Date: Sat, 15 Sep 2018 13:09:22 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Main=20=E6=89=80=E6=9C=89=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=9F=BA=E6=9C=AC=E9=83=BD=E5=AE=9E=E7=8E=B0=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123001/src/WordCount.java | 120 ++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 201621123001/src/WordCount.java diff --git a/201621123001/src/WordCount.java b/201621123001/src/WordCount.java new file mode 100644 index 0000000..217be21 --- /dev/null +++ b/201621123001/src/WordCount.java @@ -0,0 +1,120 @@ +package Mysoftwork; + +import java.io.*; +import java.util.*; + +public class WordCount { + public static String resultPath = "/Users/zhangyilin/IdeaProjects/softwork2/src/result.txt"; + public static List lists = new ArrayList<>(); + + public static void main(String[] args) throws Exception { + Scanner input = new Scanner(System.in); + Map treeMap = new TreeMap(); + System.out.println("请输入文件名:"); + String path = input.next(); +// int wordsNum = 0;//单词总数 +// InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象 +// BufferedReader br = new BufferedReader(isr); +// String str = br.readLine(); +// isr.close();//关闭 + System.out.println(charCount(path)); + System.out.println(wordCount(path)); + System.out.println(lineCount(path)); + for (int i = 0; i < lists.size(); i++) { + String s = lists.get(i).toLowerCase(); + if (!treeMap.containsKey(s)) { + treeMap.put(s, 1); + } else { + int n = treeMap.get(s); + treeMap.put(s, n + 1); + } + } + System.out.println(sortWord(treeMap)); + writefile(resultPath,charCount(path)+"\n"+wordCount(path)+"\n"+ + lineCount(path)+"\n"+sortWord(treeMap)); + +// + } + + public static String sortWord(Map treeMap) throws Exception{ + List> list = new ArrayList>(treeMap.entrySet()); + Collections.sort(list, ((o1, o2) -> o2.getValue().compareTo(o1.getValue()))); + int n = 10; + String s[] = new String[n]; + + int i =0; + for (Map.Entry entry : list) { + if (n-- == 0) + break; + String key = entry.getKey(); + Integer value = entry.getValue(); + s[i] = "<" + key + ">:" + value; +// System.out.print("<" + key + ">:" + value); +// System.out.println(s[i]); + i++; + } + String string = s[0]+"\n"; + for (int j = 1; j 31 && getchar < 127 || getchar == 10) { + charNum++; + } + } + return "characters: " + charNum; + } + + public static void writefile(String path, String content) throws Exception { + try { + OutputStream out = new FileOutputStream(path); + out.write(content.getBytes()); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + public static String lineCount(String path)throws Exception{ + int lineNum = 0; + InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象 + BufferedReader br = new BufferedReader(isr); + String str = br.readLine(); + while (str != null) {//统计有效行的字符 + lineNum++; + str = br.readLine(); + } + + return "lines:" + lineNum; + } + public static String wordCount(String path)throws Exception{ + int wordsNum = 0;//单词总数 + InputStreamReader isr = new InputStreamReader(new FileInputStream(path));// 建立一个输入流对象 + BufferedReader br = new BufferedReader(isr); + String str = br.readLine(); + while (str != null) {//统计有效行的字符 + String wordsArr[] = str.split("\\s*[^0-9a-zA-Z]+");//根据分隔符为数字字母以外的存放在数组 + for (String word : wordsArr) { + //以4个英文字母开头,跟上字母数字符号 + if (word.matches("[a-zA-Z]{4,}[a-zA-Z0-9]*")) { + lists.add(word); + } + } + str = br.readLine(); + } + wordsNum = lists.size(); + + return "words:" + wordsNum; + } + + +} -- Gitee From f929ff5b844b577ed1820bba319f04811db3ed99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=AA0010?= <1357146670@qq.com> Date: Sat, 15 Sep 2018 13:14:37 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E4=BB=A5=E5=8F=8A=E6=B5=8B=E8=AF=95=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 201621123001/WordCountTest.java | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 201621123001/WordCountTest.java diff --git a/201621123001/WordCountTest.java b/201621123001/WordCountTest.java new file mode 100644 index 0000000..89f1b73 --- /dev/null +++ b/201621123001/WordCountTest.java @@ -0,0 +1,70 @@ +package Mysoftwork; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class WordCountTest { + + @Test + public void testCharCount1() throws Exception{//测试中文无效 + //fail("Not yet implemented"); + String path ="input1.txt"; + assertEquals("characters: 0",WordCount.charCount(path)); + } + + @Test + public void testWordCount1() throws Exception{ + String path ="input1.txt"; + assertEquals("words:0",WordCount.wordCount(path)); + } + + @Test + public void testLineCount1() throws Exception { + String path ="input1.txt"; + assertEquals("lines:1",WordCount.lineCount(path)); + } + + + @Test + public void testCharCount2() throws Exception{//测试按顺序输出 + //fail("Not yet implemented"); + String path ="input2.txt"; + assertEquals("characters: 21",WordCount.charCount(path)); + } + + @Test + public void testWordCount2() throws Exception{ + String path ="input2.txt"; + assertEquals("words:2",WordCount.wordCount(path)); + } + + @Test + public void testLineCount2() throws Exception { + String path ="input2.txt"; + assertEquals("lines:1",WordCount.lineCount(path)); + } + + + @Test + public void testCharCount3() throws Exception{//测试不区分大小写 + //fail("Not yet implemented"); + String path ="input3.txt"; + assertEquals("characters: 21",WordCount.charCount(path)); + } + + @Test + public void testWordCount3() throws Exception{ + String path ="input3.txt"; + assertEquals("words:2",WordCount.wordCount(path)); + } + + @Test + public void testLineCount3() throws Exception { + String path ="input3.txt"; + assertEquals("lines:1",WordCount.lineCount(path)); + } + + + +} -- Gitee From 9e9415ae4b60fbac67caa4aada0e923e48772707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=AA0010?= <1357146670@qq.com> Date: Sat, 15 Sep 2018 13:15:00 +0800 Subject: [PATCH 5/5] Upload input1.txt input2.txt --- 201621123001/input1.txt | 1 + 201621123001/input2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 201621123001/input1.txt create mode 100644 201621123001/input2.txt diff --git a/201621123001/input1.txt b/201621123001/input1.txt new file mode 100644 index 0000000..c15250b --- /dev/null +++ b/201621123001/input1.txt @@ -0,0 +1 @@ +我是张艺琳 \ No newline at end of file diff --git a/201621123001/input2.txt b/201621123001/input2.txt new file mode 100644 index 0000000..fac3e26 --- /dev/null +++ b/201621123001/input2.txt @@ -0,0 +1 @@ +windows2000 windows98 \ No newline at end of file -- Gitee