From b2e898a256a4b9b6415b59a382cf67963894ed63 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Tue, 2 Mar 2021 16:15:26 +0800
Subject: [PATCH 01/16] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=AD=A6=E5=8F=B7?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=EF=BC=8C=E5=A4=8D=E5=88=B6example?=
=?UTF-8?q?=E7=9A=84=E5=86=85=E5=AE=B9=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/README.md | 0
221801132/codestyle.md | 0
221801132/src/WordCount.java | 0
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 221801132/README.md
create mode 100644 221801132/codestyle.md
create mode 100644 221801132/src/WordCount.java
diff --git a/221801132/README.md b/221801132/README.md
new file mode 100644
index 00000000..e69de29b
diff --git a/221801132/codestyle.md b/221801132/codestyle.md
new file mode 100644
index 00000000..e69de29b
diff --git a/221801132/src/WordCount.java b/221801132/src/WordCount.java
new file mode 100644
index 00000000..e69de29b
From aa65f6e3b07336338d38caa4dfa5782b27273a98 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Tue, 2 Mar 2021 16:16:58 +0800
Subject: [PATCH 02/16] .
---
221801132/README.md | 0
221801132/codestyle.md | 0
221801132/src/WordCount.java | 0
3 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 221801132/README.md
delete mode 100644 221801132/codestyle.md
delete mode 100644 221801132/src/WordCount.java
diff --git a/221801132/README.md b/221801132/README.md
deleted file mode 100644
index e69de29b..00000000
diff --git a/221801132/codestyle.md b/221801132/codestyle.md
deleted file mode 100644
index e69de29b..00000000
diff --git a/221801132/src/WordCount.java b/221801132/src/WordCount.java
deleted file mode 100644
index e69de29b..00000000
From 6b72dcbb68b06ad8a63c6d64488996da715952b6 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Tue, 2 Mar 2021 16:28:24 +0800
Subject: [PATCH 03/16] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=AD=A6=E5=8F=B7?=
=?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/README.md | 0
221801132/codestyle.md | 0
221801132/src/WordCount.java | 0
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 221801132/README.md
create mode 100644 221801132/codestyle.md
create mode 100644 221801132/src/WordCount.java
diff --git a/221801132/README.md b/221801132/README.md
new file mode 100644
index 00000000..e69de29b
diff --git a/221801132/codestyle.md b/221801132/codestyle.md
new file mode 100644
index 00000000..e69de29b
diff --git a/221801132/src/WordCount.java b/221801132/src/WordCount.java
new file mode 100644
index 00000000..e69de29b
From fd92c1b052f7e1a1f7a654e8a138878164cef73d Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Wed, 3 Mar 2021 09:58:50 +0800
Subject: [PATCH 04/16] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 1 +
221801132/.classpath | 10 +++++
221801132/.gitignore | 1 +
221801132/.project | 17 +++++++
.../.settings/org.eclipse.jdt.core.prefs | 14 ++++++
221801132/README.md | 0
221801132/codestyle.md | 0
221801132/src/WordCount.java | 0
221801132/src/test/Lib.java | 44 +++++++++++++++++++
9 files changed, 87 insertions(+)
create mode 100644 .gitignore
create mode 100644 221801132/.classpath
create mode 100644 221801132/.gitignore
create mode 100644 221801132/.project
create mode 100644 221801132/.settings/org.eclipse.jdt.core.prefs
delete mode 100644 221801132/README.md
delete mode 100644 221801132/codestyle.md
delete mode 100644 221801132/src/WordCount.java
create mode 100644 221801132/src/test/Lib.java
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..e10e727b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/.metadata/
diff --git a/221801132/.classpath b/221801132/.classpath
new file mode 100644
index 00000000..1cd9d959
--- /dev/null
+++ b/221801132/.classpath
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/221801132/.gitignore b/221801132/.gitignore
new file mode 100644
index 00000000..ae3c1726
--- /dev/null
+++ b/221801132/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/221801132/.project b/221801132/.project
new file mode 100644
index 00000000..a22b2f10
--- /dev/null
+++ b/221801132/.project
@@ -0,0 +1,17 @@
+
+
+ 221801132
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/221801132/.settings/org.eclipse.jdt.core.prefs b/221801132/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 00000000..910a770e
--- /dev/null
+++ b/221801132/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=14
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=14
+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.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=14
diff --git a/221801132/README.md b/221801132/README.md
deleted file mode 100644
index e69de29b..00000000
diff --git a/221801132/codestyle.md b/221801132/codestyle.md
deleted file mode 100644
index e69de29b..00000000
diff --git a/221801132/src/WordCount.java b/221801132/src/WordCount.java
deleted file mode 100644
index e69de29b..00000000
diff --git a/221801132/src/test/Lib.java b/221801132/src/test/Lib.java
new file mode 100644
index 00000000..192f1a97
--- /dev/null
+++ b/221801132/src/test/Lib.java
@@ -0,0 +1,44 @@
+package test;
+
+import java.io.*;
+
+public class Lib {
+ public static Reader InputFile(String fileName) {
+ File file = new File(fileName);
+ Reader reader = null;
+ try {
+ reader = new InputStreamReader(new FileInputStream(file));
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ System.out.println("找不到输入文件!");
+ }
+ return reader;
+ }
+
+ public static BufferedWriter OutputFile(String fileName) throws IOException {
+ BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
+ return write;
+ }
+
+ public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
+ Reader reader = InputFile(InputFile);
+ Writer writer = new FileWriter(OutputFile);
+ int CharactersNum = 0;
+ while (reader.read() != -1) {
+ CharactersNum++;
+ }
+ writer.write("characters: " + CharactersNum + '\n');
+ writer.close();
+ reader.close();
+ return CharactersNum;
+ } // 统计字符数
+
+
+
+
+ public static void main(String[] args) throws IOException {
+ String inputFile = args[0];
+ String outputFile = args[1];
+ Lib.CountCharacters(inputFile, outputFile);
+ }
+}
\ No newline at end of file
From 6673cf30ba36895ec425ce0ce50c0dcae2713b83 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Wed, 3 Mar 2021 17:10:34 +0800
Subject: [PATCH 05/16] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=BB=9F?=
=?UTF-8?q?=E8=AE=A1=E8=A1=8C=E6=95=B0=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=86=99?=
=?UTF-8?q?=E4=BA=86=E4=B8=A4=E4=B8=AA=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?=
=?UTF-8?q?=E5=92=8C=E4=B8=80=E4=BA=9B=E6=B3=A8=E9=87=8A=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/.classpath | 2 ++
221801132/src/test/Lib.java | 41 +++++++++++++++++++++++--------
221801132/src/test/WordCount.java | 12 +++++++++
221801132/test/test/LibTest.java | 30 ++++++++++++++++++++++
4 files changed, 75 insertions(+), 10 deletions(-)
create mode 100644 221801132/src/test/WordCount.java
create mode 100644 221801132/test/test/LibTest.java
diff --git a/221801132/.classpath b/221801132/.classpath
index 1cd9d959..7929670b 100644
--- a/221801132/.classpath
+++ b/221801132/.classpath
@@ -6,5 +6,7 @@
+
+
diff --git a/221801132/src/test/Lib.java b/221801132/src/test/Lib.java
index 192f1a97..bbed926b 100644
--- a/221801132/src/test/Lib.java
+++ b/221801132/src/test/Lib.java
@@ -24,21 +24,42 @@ public static int CountCharacters(String InputFile, String OutputFile) throws IO
Reader reader = InputFile(InputFile);
Writer writer = new FileWriter(OutputFile);
int CharactersNum = 0;
- while (reader.read() != -1) {
+ while (reader.read() != -1) //读取到-1时停止。
+ {
CharactersNum++;
}
- writer.write("characters: " + CharactersNum + '\n');
+ writer.write("characters:" + CharactersNum + '\n');
writer.close();
reader.close();
return CharactersNum;
- } // 统计字符数
+ } // 统计字符数。
+ public static int CountLines(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ int LinesNum = 0;
+ String line = ""; //初始化行的内容。
+ while ((temp = reader.read()) != -1)
+ {
+ while (temp != -1 && (char) temp != '\n')
+ {
+ if (temp != ' ' && temp != '\t' && temp != '\r')
+ {
+ line += temp;
+ }
+ temp = reader.read();
+ }
+ if (line != " ")
+ {
+ LinesNum++;
+ }
+ line = " ";
+ }
+ writer.append("lines:" + LinesNum + "\n");
+ reader.close();
+ writer.close();
+ return LinesNum;
+ } //统计行数。
-
-
- public static void main(String[] args) throws IOException {
- String inputFile = args[0];
- String outputFile = args[1];
- Lib.CountCharacters(inputFile, outputFile);
- }
}
\ No newline at end of file
diff --git a/221801132/src/test/WordCount.java b/221801132/src/test/WordCount.java
new file mode 100644
index 00000000..cecc1f69
--- /dev/null
+++ b/221801132/src/test/WordCount.java
@@ -0,0 +1,12 @@
+package test;
+
+import java.io.IOException;
+
+public class WordCount {
+ public static void main(String[] args) throws IOException {
+ String inputFile = args[0];
+ String outputFile = args[1];
+ Lib.CountCharacters(inputFile, outputFile);
+ Lib.CountLines(inputFile, outputFile);
+ }
+}
diff --git a/221801132/test/test/LibTest.java b/221801132/test/test/LibTest.java
new file mode 100644
index 00000000..b308006f
--- /dev/null
+++ b/221801132/test/test/LibTest.java
@@ -0,0 +1,30 @@
+package test;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class LibTest {
+
+ @Test
+ public void testCharactersCount() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
+ String inputFile2 = "D:\\test\\test2.txt";
+ String outputFile = "D:\\test\\out.txt";
+ assertEquals(15,Lib.CountCharacters(inputFile1, outputFile));
+ assertEquals(14,Lib.CountCharacters(inputFile2, outputFile));
+
+ }
+
+ @Test
+ public void testCountLines() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
+ String inputFile2 = "D:\\test\\test2.txt";
+ String outputFile = "D:\\test\\out.txt";
+ assertEquals(2,Lib.CountLines(inputFile1, outputFile));
+ assertEquals(3,Lib.CountLines(inputFile2, outputFile));
+ }
+
+}
From 0128ba2ee4b5e16965924225e3247af778b49c68 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 00:12:16 +0800
Subject: [PATCH 06/16] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=BB=9F?=
=?UTF-8?q?=E8=AE=A1=E5=8D=95=E8=AF=8D=E6=95=B0=E5=8A=9F=E8=83=BD=E4=BB=A5?=
=?UTF-8?q?=E5=8F=8A=E5=AE=83=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/src/test/Lib.java | 65 --------------
221801132/src/test/WordCount.java | 113 +++++++++++++++++++++++--
221801132/test/test/LibTest.java | 30 -------
221801132/test/test/WordCountTest.java | 38 +++++++++
4 files changed, 143 insertions(+), 103 deletions(-)
delete mode 100644 221801132/src/test/Lib.java
delete mode 100644 221801132/test/test/LibTest.java
create mode 100644 221801132/test/test/WordCountTest.java
diff --git a/221801132/src/test/Lib.java b/221801132/src/test/Lib.java
deleted file mode 100644
index bbed926b..00000000
--- a/221801132/src/test/Lib.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package test;
-
-import java.io.*;
-
-public class Lib {
- public static Reader InputFile(String fileName) {
- File file = new File(fileName);
- Reader reader = null;
- try {
- reader = new InputStreamReader(new FileInputStream(file));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.out.println("找不到输入文件!");
- }
- return reader;
- }
-
- public static BufferedWriter OutputFile(String fileName) throws IOException {
- BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
- return write;
- }
-
- public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
- Reader reader = InputFile(InputFile);
- Writer writer = new FileWriter(OutputFile);
- int CharactersNum = 0;
- while (reader.read() != -1) //读取到-1时停止。
- {
- CharactersNum++;
- }
- writer.write("characters:" + CharactersNum + '\n');
- writer.close();
- reader.close();
- return CharactersNum;
- } // 统计字符数。
-
- public static int CountLines(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int temp;
- int LinesNum = 0;
- String line = ""; //初始化行的内容。
- while ((temp = reader.read()) != -1)
- {
- while (temp != -1 && (char) temp != '\n')
- {
- if (temp != ' ' && temp != '\t' && temp != '\r')
- {
- line += temp;
- }
- temp = reader.read();
- }
- if (line != " ")
- {
- LinesNum++;
- }
- line = " ";
- }
- writer.append("lines:" + LinesNum + "\n");
- reader.close();
- writer.close();
- return LinesNum;
- } //统计行数。
-
-}
\ No newline at end of file
diff --git a/221801132/src/test/WordCount.java b/221801132/src/test/WordCount.java
index cecc1f69..a9b36fd4 100644
--- a/221801132/src/test/WordCount.java
+++ b/221801132/src/test/WordCount.java
@@ -1,12 +1,109 @@
package test;
-import java.io.IOException;
+import java.io.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class WordCount {
- public static void main(String[] args) throws IOException {
- String inputFile = args[0];
- String outputFile = args[1];
- Lib.CountCharacters(inputFile, outputFile);
- Lib.CountLines(inputFile, outputFile);
- }
-}
+ public static Reader InputFile(String fileName) {
+ File file = new File(fileName);
+ Reader reader = null;
+ try {
+ reader = new InputStreamReader(new FileInputStream(file));
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ System.out.println("找不到输入文件!");
+ }
+ return reader;
+ }
+
+ public static BufferedWriter OutputFile(String fileName) throws IOException {
+ BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
+ return write;
+ }
+
+ public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
+ Reader reader = InputFile(InputFile);
+ Writer writer = new FileWriter(OutputFile);
+ int CharactersNum = 0;
+ while (reader.read() != -1) //读取到-1时停止。
+ {
+ CharactersNum++;
+ }
+ writer.write("characters:" + CharactersNum + '\n');
+ writer.close();
+ reader.close();
+ return CharactersNum;
+ } // 统计字符数。
+
+ public static int CountWords(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int length; //表示单词的长度,大于等于4合法
+ int temp;
+ int WordsNum = 0;
+ String word = "";
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //正则表达式判断是否为四个英文开头
+ Pattern p = Pattern.compile(regex);
+ while ((temp = reader.read()) != -1)
+ {
+ while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) {
+ word += (char) temp;
+ temp = reader.read();
+ }
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //去除空白字符和分隔符
+ {
+ temp = reader.read();
+ }
+ Matcher m = p.matcher(word);
+ length = word.length();
+ if (length >= 4 && m.matches())
+ {
+ WordsNum++;
+ }
+ word = "" + (char) temp;
+ }
+ writer.append("words: " + WordsNum + '\n');
+ writer.close();
+ reader.close();
+ return WordsNum;
+ } //统计单词数
+
+
+ public static int CountLines(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ int LinesNum = 0;
+ String line = "";
+ while ((temp = reader.read()) != -1)
+ {
+ while (temp != -1 && (char) temp != '\n')
+ {
+ if (temp != ' ' && temp != '\t' && temp != '\r')
+ {
+ line += (char)temp;
+ }
+ temp = reader.read();
+ }
+ if (line != " ")
+ {
+ LinesNum++;
+ }
+ line = " ";
+ }
+ writer.append("lines:" + LinesNum + "\n");
+ reader.close();
+ writer.close();
+ return LinesNum;
+ } //统计行数。
+
+
+ public static void main(String[] args) throws IOException {
+ String inputFile = args[0];
+ String outputFile = args[1];
+ WordCount.CountCharacters(inputFile, outputFile);
+ WordCount.CountWords(inputFile, outputFile);
+ WordCount.CountLines(inputFile, outputFile);
+ }
+}
\ No newline at end of file
diff --git a/221801132/test/test/LibTest.java b/221801132/test/test/LibTest.java
deleted file mode 100644
index b308006f..00000000
--- a/221801132/test/test/LibTest.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package test;
-
-import static org.junit.Assert.*;
-
-import java.io.IOException;
-
-import org.junit.Test;
-
-public class LibTest {
-
- @Test
- public void testCharactersCount() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
- String inputFile2 = "D:\\test\\test2.txt";
- String outputFile = "D:\\test\\out.txt";
- assertEquals(15,Lib.CountCharacters(inputFile1, outputFile));
- assertEquals(14,Lib.CountCharacters(inputFile2, outputFile));
-
- }
-
- @Test
- public void testCountLines() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
- String inputFile2 = "D:\\test\\test2.txt";
- String outputFile = "D:\\test\\out.txt";
- assertEquals(2,Lib.CountLines(inputFile1, outputFile));
- assertEquals(3,Lib.CountLines(inputFile2, outputFile));
- }
-
-}
diff --git a/221801132/test/test/WordCountTest.java b/221801132/test/test/WordCountTest.java
new file mode 100644
index 00000000..ca1e655c
--- /dev/null
+++ b/221801132/test/test/WordCountTest.java
@@ -0,0 +1,38 @@
+package test;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class WordCountTest {
+
+ @Test
+ public void testCharactersCount() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
+ String inputFile2 = "D:\\test\\test2.txt";
+ String outputFile = "D:\\test\\out.txt";
+ assertEquals(15,WordCount.CountCharacters(inputFile1, outputFile));
+ assertEquals(14,WordCount.CountCharacters(inputFile2, outputFile));
+
+ }
+
+ @Test
+ public void testCountLines() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
+ String inputFile2 = "D:\\test\\test2.txt";
+ String outputFile = "D:\\test\\out.txt";
+ assertEquals(2,WordCount.CountLines(inputFile1, outputFile));
+ assertEquals(3,WordCount.CountLines(inputFile2, outputFile));
+ }
+
+ @Test
+ public void testCountWords() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
+ String inputFile2 = "D:\\test\\test2.txt";
+ String outputFile = "D:\\test\\out.txt";
+ assertEquals(0,WordCount.CountWords(inputFile1, outputFile));
+ assertEquals(0,WordCount.CountWords(inputFile2, outputFile));
+ }
+}
From 7d197d2039538c3e82300438bc3ec2333e3eb486 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 00:15:51 +0800
Subject: [PATCH 07/16] =?UTF-8?q?=E6=94=B9=E4=B8=BAUTF-8=E7=BC=96=E7=A0=81?=
=?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3=E4=BA=86=E4=B8=AD=E6=96=87=E4=B9=B1?=
=?UTF-8?q?=E7=A0=81=E9=97=AE=E9=A2=98=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/src/test/WordCount.java | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/221801132/src/test/WordCount.java b/221801132/src/test/WordCount.java
index a9b36fd4..acd0911e 100644
--- a/221801132/src/test/WordCount.java
+++ b/221801132/src/test/WordCount.java
@@ -12,7 +12,7 @@ public static Reader InputFile(String fileName) {
reader = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
- System.out.println("找不到输入文件!");
+ System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
}
return reader;
}
@@ -26,7 +26,7 @@ public static int CountCharacters(String InputFile, String OutputFile) throws IO
Reader reader = InputFile(InputFile);
Writer writer = new FileWriter(OutputFile);
int CharactersNum = 0;
- while (reader.read() != -1) //读取到-1时停止。
+ while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
{
CharactersNum++;
}
@@ -34,16 +34,16 @@ public static int CountCharacters(String InputFile, String OutputFile) throws IO
writer.close();
reader.close();
return CharactersNum;
- } // 统计字符数。
+ } // 缁熻瀛楃鏁般
public static int CountWords(String inputFile, String outputFile) throws IOException {
Reader reader = InputFile(inputFile);
Writer writer = OutputFile(outputFile);
- int length; //表示单词的长度,大于等于4合法
+ int length; //琛ㄧず鍗曡瘝鐨勯暱搴︼紝澶т簬绛変簬4鍚堟硶
int temp;
int WordsNum = 0;
String word = "";
- String regex = "[a-zA-Z]{4}[^ ,.]+"; //正则表达式判断是否为四个英文开头
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
Pattern p = Pattern.compile(regex);
while ((temp = reader.read()) != -1)
{
@@ -51,7 +51,7 @@ public static int CountWords(String inputFile, String outputFile) throws IOExcep
word += (char) temp;
temp = reader.read();
}
- while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //去除空白字符和分隔符
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //鍘婚櫎绌虹櫧瀛楃鍜屽垎闅旂
{
temp = reader.read();
}
@@ -67,7 +67,7 @@ public static int CountWords(String inputFile, String outputFile) throws IOExcep
writer.close();
reader.close();
return WordsNum;
- } //统计单词数
+ } //缁熻鍗曡瘝鏁
public static int CountLines(String inputFile, String outputFile) throws IOException {
@@ -96,7 +96,7 @@ public static int CountLines(String inputFile, String outputFile) throws IOExcep
reader.close();
writer.close();
return LinesNum;
- } //统计行数。
+ } //缁熻琛屾暟銆
public static void main(String[] args) throws IOException {
From 08ae3cb2a2ae4d3fe3bc18bb129cb70d2f367cfd Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 11:31:47 +0800
Subject: [PATCH 08/16] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E8=AF=8D?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=8A=9F=E8=83=BD=E5=92=8C=E5=AE=83=E7=9A=84?=
=?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/src/test/WordCount.java | 75 +++++++++++++++++++
.../{test => src}/test/WordCountTest.java | 26 ++++---
2 files changed, 92 insertions(+), 9 deletions(-)
rename 221801132/{test => src}/test/WordCountTest.java (67%)
diff --git a/221801132/src/test/WordCount.java b/221801132/src/test/WordCount.java
index acd0911e..0cdcc661 100644
--- a/221801132/src/test/WordCount.java
+++ b/221801132/src/test/WordCount.java
@@ -1,8 +1,13 @@
package test;
import java.io.*;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.util.stream.Collectors;
public class WordCount {
public static Reader InputFile(String fileName) {
@@ -98,6 +103,75 @@ public static int CountLines(String inputFile, String outputFile) throws IOExcep
return LinesNum;
} //缁熻琛屾暟銆
+ public static String WordsNumSort(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ String word = "";
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
+ Pattern p = Pattern.compile(regex);
+ Map words = new HashMap();
+ while ((temp = reader.read()) != -1)
+ {
+ while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57))
+ {
+ if (temp >= 65 && temp <= 90)
+ {
+ temp += 32;
+ }
+ word += (char)temp;
+ temp = reader.read();
+ }
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1)
+ {
+ temp = reader.read();
+ }
+ Matcher m = p.matcher(word);
+ if (m.matches()) {
+ if (words.get(word) == null)
+ {
+ words.put(word, Integer.valueOf(1));
+ }
+ else
+ {
+ words.put(word, Integer.valueOf(words.get(word).intValue() + 1));
+ }
+ }
+ if (temp >= 65 && temp <= 90)
+ {
+ temp += 32;
+ }
+ word = "" + (char) temp;
+ } //涓庣粺璁″崟璇嶆暟鐨勬柟娉曠被浼硷紝涓嶅悎娉曠殑鍗曡瘝涓嶈繘琛屾帓搴忋
+ Map WordsSort = words.entrySet().stream().sorted(new Comparator>()
+ {
+ public int compare(Map.Entry w1, Map.Entry w2)
+ {
+ if (w1.getValue().equals(w2.getValue()))
+ {
+ return w1.getKey().compareTo(w2.getKey());
+ }
+ else
+ {
+ return w2.getValue().compareTo(w1.getValue());
+ }
+ }
+ })
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
+ String test = null; //璁惧畾棰戠巼鏈浣庣殑璇嶄互渚垮崟鍏冩祴璇
+ int i = 0;
+ for (Map.Entry entry : WordsSort.entrySet())
+ {
+ test = entry.getKey();
+ writer.write(entry.getKey() + ":" + entry.getValue() + "\n");
+ if (i++ >= 9) {
+ break;
+ }
+ } ////鎵撳嵃棰戠巼鍓嶅崄鐨勫崟璇
+ reader.close();
+ writer.close();
+ return test; //杩斿洖棰戠巼鏈浣庣殑璇
+ }
public static void main(String[] args) throws IOException {
String inputFile = args[0];
@@ -105,5 +179,6 @@ public static void main(String[] args) throws IOException {
WordCount.CountCharacters(inputFile, outputFile);
WordCount.CountWords(inputFile, outputFile);
WordCount.CountLines(inputFile, outputFile);
+ WordCount.WordsNumSort(inputFile, outputFile);
}
}
\ No newline at end of file
diff --git a/221801132/test/test/WordCountTest.java b/221801132/src/test/WordCountTest.java
similarity index 67%
rename from 221801132/test/test/WordCountTest.java
rename to 221801132/src/test/WordCountTest.java
index ca1e655c..938d4e6f 100644
--- a/221801132/test/test/WordCountTest.java
+++ b/221801132/src/test/WordCountTest.java
@@ -13,26 +13,34 @@ public void testCharactersCount() throws IOException {
String inputFile1 = "D:\\test\\test1.txt";
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
- assertEquals(15,WordCount.CountCharacters(inputFile1, outputFile));
+ assertEquals(521,WordCount.CountCharacters(inputFile1, outputFile));
assertEquals(14,WordCount.CountCharacters(inputFile2, outputFile));
}
-
+
@Test
- public void testCountLines() throws IOException {
+ public void testCountWords() throws IOException {
String inputFile1 = "D:\\test\\test1.txt";
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
- assertEquals(2,WordCount.CountLines(inputFile1, outputFile));
- assertEquals(3,WordCount.CountLines(inputFile2, outputFile));
+ assertEquals(20,WordCount.CountWords(inputFile1, outputFile));
+ assertEquals(0,WordCount.CountWords(inputFile2, outputFile));
}
-
+
@Test
- public void testCountWords() throws IOException {
+ public void testCountLines() throws IOException {
String inputFile1 = "D:\\test\\test1.txt";
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
- assertEquals(0,WordCount.CountWords(inputFile1, outputFile));
- assertEquals(0,WordCount.CountWords(inputFile2, outputFile));
+ assertEquals(3,WordCount.CountLines(inputFile1, outputFile));
+ assertEquals(3,WordCount.CountLines(inputFile2, outputFile));
+ }
+
+ @Test
+ public void WordsNumSort() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
+ String outputFile = "D:\\test\\out.txt";
+ String test = "round";
+ test.equals( WordCount.WordsNumSort(inputFile1, outputFile));
}
}
From 95bf4c9b91af64bfda00665f2de5f3cc74a95e60 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 17:09:25 +0800
Subject: [PATCH 09/16] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80?=
=?UTF-8?q?=E4=BA=9B=E6=B3=A8=E9=87=8A=E5=92=8C=E4=B8=80=E4=B8=AA=E5=8D=95?=
=?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/{test => WordCount}/WordCount.java | 11 +++++-----
.../WordCount}/WordCountTest.java | 22 ++++++++++++-------
2 files changed, 20 insertions(+), 13 deletions(-)
rename 221801132/src/{test => WordCount}/WordCount.java (96%)
rename 221801132/{src/test => test/WordCount}/WordCountTest.java (68%)
diff --git a/221801132/src/test/WordCount.java b/221801132/src/WordCount/WordCount.java
similarity index 96%
rename from 221801132/src/test/WordCount.java
rename to 221801132/src/WordCount/WordCount.java
index 0cdcc661..e5b20c89 100644
--- a/221801132/src/test/WordCount.java
+++ b/221801132/src/WordCount/WordCount.java
@@ -1,4 +1,4 @@
-package test;
+package WordCount;
import java.io.*;
import java.util.Comparator;
@@ -18,7 +18,7 @@ public static Reader InputFile(String fileName) {
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
- }
+ }
return reader;
}
@@ -66,7 +66,7 @@ public static int CountWords(String inputFile, String outputFile) throws IOExcep
{
WordsNum++;
}
- word = "" + (char) temp;
+ word = "" + (char)temp;
}
writer.append("words: " + WordsNum + '\n');
writer.close();
@@ -143,6 +143,7 @@ public static String WordsNumSort(String inputFile, String outputFile) throws IO
}
word = "" + (char) temp;
} //涓庣粺璁″崟璇嶆暟鐨勬柟娉曠被浼硷紝涓嶅悎娉曠殑鍗曡瘝涓嶈繘琛屾帓搴忋
+
Map WordsSort = words.entrySet().stream().sorted(new Comparator>()
{
public int compare(Map.Entry w1, Map.Entry w2)
@@ -156,8 +157,8 @@ public int compare(Map.Entry w1, Map.Entry w2)
return w2.getValue().compareTo(w1.getValue());
}
}
- })
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
+ }
+ ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
String test = null; //璁惧畾棰戠巼鏈浣庣殑璇嶄互渚垮崟鍏冩祴璇
int i = 0;
for (Map.Entry entry : WordsSort.entrySet())
diff --git a/221801132/src/test/WordCountTest.java b/221801132/test/WordCount/WordCountTest.java
similarity index 68%
rename from 221801132/src/test/WordCountTest.java
rename to 221801132/test/WordCount/WordCountTest.java
index 938d4e6f..41a423ea 100644
--- a/221801132/src/test/WordCountTest.java
+++ b/221801132/test/WordCount/WordCountTest.java
@@ -1,4 +1,4 @@
-package test;
+package WordCount;
import static org.junit.Assert.*;
@@ -7,6 +7,12 @@
import org.junit.Test;
public class WordCountTest {
+
+ @Test
+ public void InputFile() throws IOException {
+ String inputFile3 = "D:\\test\\test3.txt";
+ assertEquals(null,WordCount.InputFile(inputFile3));
+ } //娴嬭瘯璇诲彇鏂囦欢澶辫触鏃剁殑杩愯缁撴灉
@Test
public void testCharactersCount() throws IOException {
@@ -14,9 +20,9 @@ public void testCharactersCount() throws IOException {
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
assertEquals(521,WordCount.CountCharacters(inputFile1, outputFile));
- assertEquals(14,WordCount.CountCharacters(inputFile2, outputFile));
+ assertEquals(15,WordCount.CountCharacters(inputFile2, outputFile));
- }
+ } //娴嬭瘯瀛楃鏁扮粺璁″姛鑳
@Test
public void testCountWords() throws IOException {
@@ -24,8 +30,8 @@ public void testCountWords() throws IOException {
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
assertEquals(20,WordCount.CountWords(inputFile1, outputFile));
- assertEquals(0,WordCount.CountWords(inputFile2, outputFile));
- }
+ assertEquals(2,WordCount.CountWords(inputFile2, outputFile));
+ } //娴嬭瘯鍗曡瘝鏁扮粺璁″姛鑳
@Test
public void testCountLines() throws IOException {
@@ -33,8 +39,8 @@ public void testCountLines() throws IOException {
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
assertEquals(3,WordCount.CountLines(inputFile1, outputFile));
- assertEquals(3,WordCount.CountLines(inputFile2, outputFile));
- }
+ assertEquals(1,WordCount.CountLines(inputFile2, outputFile));
+ } //娴嬭瘯琛屾暟缁熻鍔熻兘
@Test
public void WordsNumSort() throws IOException {
@@ -42,5 +48,5 @@ public void WordsNumSort() throws IOException {
String outputFile = "D:\\test\\out.txt";
String test = "round";
test.equals( WordCount.WordsNumSort(inputFile1, outputFile));
- }
+ } //娴嬭瘯鍗曡瘝棰戠巼缁熻鍔熻兘
}
From 75aaf7be4f07883eb47ce2becceead7c271950a9 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 17:59:19 +0800
Subject: [PATCH 10/16] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E8=A7=84=E8=8C=83=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/README.md | 0
221801132/codestyle.md | 51 ++++++++++++++++++++++++++
221801132/src/WordCount/WordCount.java | 2 +-
3 files changed, 52 insertions(+), 1 deletion(-)
create mode 100644 221801132/README.md
create mode 100644 221801132/codestyle.md
diff --git a/221801132/README.md b/221801132/README.md
new file mode 100644
index 00000000..e69de29b
diff --git a/221801132/codestyle.md b/221801132/codestyle.md
new file mode 100644
index 00000000..f63e22cd
--- /dev/null
+++ b/221801132/codestyle.md
@@ -0,0 +1,51 @@
+锘夸唬鐮佽鑼
+----
+
+
+----------
+
+**缂╄繘**
+
+ - 4涓┖鏍
+
+**姣忚鏈澶氬瓧绗︽暟**
+
+ - 80瀛楃
+
+**鍑芥暟鏈澶ц鏁**
+
+ - 100琛
+
+
+**鍑芥暟銆佺被鍛藉悕**
+
+ - 绫诲悕浣跨敤UpperCamelCase椋庢牸锛屽繀椤婚伒浠庨┘宄板舰寮忋
+ - 鍛藉悕灏介噺浣跨敤鑻辨枃鍗曡瘝锛屽姏姹傜畝鍗曟竻妤
+
+**鍙橀噺鍛藉悕**
+
+ - 涓斿敖閲忎娇鐢ㄥ崟璇嶅懡鍚嶏紝涓寰嬪皬鍐
+ - 绂佹鍙栧崟涓瓧绗︼紙濡俰銆乯銆乲锛夛紝浣 i銆乯銆乲浣滃眬閮ㄥ惊鐜彉閲忔槸鍏佽鐨勩
+
+**甯搁噺**
+
+ - 鍏ㄩ儴澶у啓
+ - 涓嶅厑璁告湭缁忓畾涔夌殑甯搁噺鐩存帴鍑虹幇鍦ㄤ唬鐮佷腑
+
+**绌鸿瑙勫垯**
+
+ - 鐩稿鐙珛鐨勭▼搴忓潡涔嬮棿銆佸彉閲忚鏄庝箣鍚庡繀椤诲姞绌鸿
+ - 涓嶅厑璁告妸澶氫釜鐭鍙ュ啓鍦ㄤ竴琛屼腑锛屼竴琛屽彧鍐欎竴鏉¤鍙
+ - if銆乫or銆乨o銆亀hile銆乧ase銆乻witch銆乨efault 绛夎鍙ヨ嚜鍗犱竴琛岋紝涓攊f銆乫or銆乨o銆亀hile绛夎鍙ョ殑鎵ц璇彞閮ㄥ垎鏃犺澶氬皯閮借鍔犳嫭鍙穥}銆
+
+**娉ㄩ噴瑙勫垯**
+
+ - 浣跨敤//
+ - 娉ㄩ噴鐨勫唴瀹硅娓呮銆佹槑浜嗭紝涓嶈兘鏈変簩涔夋
+ - 鎿嶄綔绗﹀墠鍚庣┖鏍 鎿嶄綔绗﹀墠鍚庡繀椤诲姞涓涓┖鏍
+
+**鍏朵粬瑙勫垯**
+
+ - 涓ョ浣跨敤鎷奸煶涓庤嫳鏂囨贩鍚堢殑鏂瑰紡锛屾洿涓嶅厑璁哥洿鎺ヤ娇鐢ㄤ腑鏂囩殑鏂瑰紡
+ - 鐢ㄥぇ鍐欑殑鈥橪鈥欎唬鏇库檒鈥
+
diff --git a/221801132/src/WordCount/WordCount.java b/221801132/src/WordCount/WordCount.java
index e5b20c89..92994fa2 100644
--- a/221801132/src/WordCount/WordCount.java
+++ b/221801132/src/WordCount/WordCount.java
@@ -45,7 +45,7 @@ public static int CountWords(String inputFile, String outputFile) throws IOExcep
Reader reader = InputFile(inputFile);
Writer writer = OutputFile(outputFile);
int length; //琛ㄧず鍗曡瘝鐨勯暱搴︼紝澶т簬绛変簬4鍚堟硶
- int temp;
+ int temp;
int WordsNum = 0;
String word = "";
String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
From 9ea8dea01c2c339736a2a42eb29e9bc0430bd944 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 18:12:16 +0800
Subject: [PATCH 11/16] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=B8=80=E4=B8=8B.giti?=
=?UTF-8?q?gnore?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
221801132/.gitignore | 5 +
221801132/src/.gitignore | 1 +
221801132/src/WordCount.java | 185 +++++++++++++++++++++++++++++++++++
3 files changed, 191 insertions(+)
create mode 100644 221801132/src/.gitignore
create mode 100644 221801132/src/WordCount.java
diff --git a/221801132/.gitignore b/221801132/.gitignore
index ae3c1726..377d4e77 100644
--- a/221801132/.gitignore
+++ b/221801132/.gitignore
@@ -1 +1,6 @@
/bin/
+/test/
+/.settings/
+/.classpath/
+/.project/
+
diff --git a/221801132/src/.gitignore b/221801132/src/.gitignore
new file mode 100644
index 00000000..1a5fbc3d
--- /dev/null
+++ b/221801132/src/.gitignore
@@ -0,0 +1 @@
+/WordCount/
diff --git a/221801132/src/WordCount.java b/221801132/src/WordCount.java
new file mode 100644
index 00000000..92994fa2
--- /dev/null
+++ b/221801132/src/WordCount.java
@@ -0,0 +1,185 @@
+package WordCount;
+
+import java.io.*;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class WordCount {
+ public static Reader InputFile(String fileName) {
+ File file = new File(fileName);
+ Reader reader = null;
+ try {
+ reader = new InputStreamReader(new FileInputStream(file));
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
+ }
+ return reader;
+ }
+
+ public static BufferedWriter OutputFile(String fileName) throws IOException {
+ BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
+ return write;
+ }
+
+ public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
+ Reader reader = InputFile(InputFile);
+ Writer writer = new FileWriter(OutputFile);
+ int CharactersNum = 0;
+ while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
+ {
+ CharactersNum++;
+ }
+ writer.write("characters:" + CharactersNum + '\n');
+ writer.close();
+ reader.close();
+ return CharactersNum;
+ } // 缁熻瀛楃鏁般
+
+ public static int CountWords(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int length; //琛ㄧず鍗曡瘝鐨勯暱搴︼紝澶т簬绛変簬4鍚堟硶
+ int temp;
+ int WordsNum = 0;
+ String word = "";
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
+ Pattern p = Pattern.compile(regex);
+ while ((temp = reader.read()) != -1)
+ {
+ while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) {
+ word += (char) temp;
+ temp = reader.read();
+ }
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //鍘婚櫎绌虹櫧瀛楃鍜屽垎闅旂
+ {
+ temp = reader.read();
+ }
+ Matcher m = p.matcher(word);
+ length = word.length();
+ if (length >= 4 && m.matches())
+ {
+ WordsNum++;
+ }
+ word = "" + (char)temp;
+ }
+ writer.append("words: " + WordsNum + '\n');
+ writer.close();
+ reader.close();
+ return WordsNum;
+ } //缁熻鍗曡瘝鏁
+
+
+ public static int CountLines(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ int LinesNum = 0;
+ String line = "";
+ while ((temp = reader.read()) != -1)
+ {
+ while (temp != -1 && (char) temp != '\n')
+ {
+ if (temp != ' ' && temp != '\t' && temp != '\r')
+ {
+ line += (char)temp;
+ }
+ temp = reader.read();
+ }
+ if (line != " ")
+ {
+ LinesNum++;
+ }
+ line = " ";
+ }
+ writer.append("lines:" + LinesNum + "\n");
+ reader.close();
+ writer.close();
+ return LinesNum;
+ } //缁熻琛屾暟銆
+
+ public static String WordsNumSort(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ String word = "";
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
+ Pattern p = Pattern.compile(regex);
+ Map words = new HashMap();
+ while ((temp = reader.read()) != -1)
+ {
+ while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57))
+ {
+ if (temp >= 65 && temp <= 90)
+ {
+ temp += 32;
+ }
+ word += (char)temp;
+ temp = reader.read();
+ }
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1)
+ {
+ temp = reader.read();
+ }
+ Matcher m = p.matcher(word);
+ if (m.matches()) {
+ if (words.get(word) == null)
+ {
+ words.put(word, Integer.valueOf(1));
+ }
+ else
+ {
+ words.put(word, Integer.valueOf(words.get(word).intValue() + 1));
+ }
+ }
+ if (temp >= 65 && temp <= 90)
+ {
+ temp += 32;
+ }
+ word = "" + (char) temp;
+ } //涓庣粺璁″崟璇嶆暟鐨勬柟娉曠被浼硷紝涓嶅悎娉曠殑鍗曡瘝涓嶈繘琛屾帓搴忋
+
+ Map WordsSort = words.entrySet().stream().sorted(new Comparator>()
+ {
+ public int compare(Map.Entry w1, Map.Entry w2)
+ {
+ if (w1.getValue().equals(w2.getValue()))
+ {
+ return w1.getKey().compareTo(w2.getKey());
+ }
+ else
+ {
+ return w2.getValue().compareTo(w1.getValue());
+ }
+ }
+ }
+ ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
+ String test = null; //璁惧畾棰戠巼鏈浣庣殑璇嶄互渚垮崟鍏冩祴璇
+ int i = 0;
+ for (Map.Entry entry : WordsSort.entrySet())
+ {
+ test = entry.getKey();
+ writer.write(entry.getKey() + ":" + entry.getValue() + "\n");
+ if (i++ >= 9) {
+ break;
+ }
+ } ////鎵撳嵃棰戠巼鍓嶅崄鐨勫崟璇
+ reader.close();
+ writer.close();
+ return test; //杩斿洖棰戠巼鏈浣庣殑璇
+ }
+
+ public static void main(String[] args) throws IOException {
+ String inputFile = args[0];
+ String outputFile = args[1];
+ WordCount.CountCharacters(inputFile, outputFile);
+ WordCount.CountWords(inputFile, outputFile);
+ WordCount.CountLines(inputFile, outputFile);
+ WordCount.WordsNumSort(inputFile, outputFile);
+ }
+}
\ No newline at end of file
From 78cb1f3974b427c6a571a6aac336d4ffb5bef581 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 18:18:19 +0800
Subject: [PATCH 12/16] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=B8=80=E4=B8=8B.giti?=
=?UTF-8?q?gnore?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 2 +-
221801132/.gitignore | 10 +++++-----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/.gitignore b/.gitignore
index e10e727b..ac50e4fd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-/.metadata/
+.metadata/
diff --git a/221801132/.gitignore b/221801132/.gitignore
index 377d4e77..c644f9b8 100644
--- a/221801132/.gitignore
+++ b/221801132/.gitignore
@@ -1,6 +1,6 @@
-/bin/
-/test/
-/.settings/
-/.classpath/
-/.project/
+bin/
+test/
+.settings/
+.classpath/
+.project/
From 39fc6c1467c845f357552ffac13af0241d07e7a4 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 19:30:13 +0800
Subject: [PATCH 13/16] =?UTF-8?q?=E5=86=8D=E8=AE=BE=E7=BD=AE=E4=B8=80?=
=?UTF-8?q?=E4=B8=8B.gitinore?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
涔嬪墠璇曡繃涓ゆ閮芥病鏈夊睆钄芥垚鍔燂紝绱㈡х洿鎺ラ噸鏂板垱寤轰竴涓鍙锋枃浠跺す鎶婁笢瑗挎斁杩涘幓銆
---
.gitignore | 1 +
221801132/src/.gitignore | 1 -
{221801132 => 221801132test}/.classpath | 0
{221801132 => 221801132test}/.gitignore | 0
{221801132 => 221801132test}/.project | 2 +-
.../.settings/org.eclipse.jdt.core.prefs | 0
221801132test/README.md | 0
221801132test/codestyle.md | 51 +++++++++++++++++++
.../src/WordCount/WordCount.java | 0
.../test/WordCount/WordCountTest.java | 0
10 files changed, 53 insertions(+), 2 deletions(-)
delete mode 100644 221801132/src/.gitignore
rename {221801132 => 221801132test}/.classpath (100%)
rename {221801132 => 221801132test}/.gitignore (100%)
rename {221801132 => 221801132test}/.project (92%)
rename {221801132 => 221801132test}/.settings/org.eclipse.jdt.core.prefs (100%)
create mode 100644 221801132test/README.md
create mode 100644 221801132test/codestyle.md
rename {221801132 => 221801132test}/src/WordCount/WordCount.java (100%)
rename {221801132 => 221801132test}/test/WordCount/WordCountTest.java (100%)
diff --git a/.gitignore b/.gitignore
index ac50e4fd..f8b55dc2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
.metadata/
+221801132test/
\ No newline at end of file
diff --git a/221801132/src/.gitignore b/221801132/src/.gitignore
deleted file mode 100644
index 1a5fbc3d..00000000
--- a/221801132/src/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/WordCount/
diff --git a/221801132/.classpath b/221801132test/.classpath
similarity index 100%
rename from 221801132/.classpath
rename to 221801132test/.classpath
diff --git a/221801132/.gitignore b/221801132test/.gitignore
similarity index 100%
rename from 221801132/.gitignore
rename to 221801132test/.gitignore
diff --git a/221801132/.project b/221801132test/.project
similarity index 92%
rename from 221801132/.project
rename to 221801132test/.project
index a22b2f10..d4f939ed 100644
--- a/221801132/.project
+++ b/221801132test/.project
@@ -1,6 +1,6 @@
- 221801132
+ 221801132test
diff --git a/221801132/.settings/org.eclipse.jdt.core.prefs b/221801132test/.settings/org.eclipse.jdt.core.prefs
similarity index 100%
rename from 221801132/.settings/org.eclipse.jdt.core.prefs
rename to 221801132test/.settings/org.eclipse.jdt.core.prefs
diff --git a/221801132test/README.md b/221801132test/README.md
new file mode 100644
index 00000000..e69de29b
diff --git a/221801132test/codestyle.md b/221801132test/codestyle.md
new file mode 100644
index 00000000..f63e22cd
--- /dev/null
+++ b/221801132test/codestyle.md
@@ -0,0 +1,51 @@
+锘夸唬鐮佽鑼
+----
+
+
+----------
+
+**缂╄繘**
+
+ - 4涓┖鏍
+
+**姣忚鏈澶氬瓧绗︽暟**
+
+ - 80瀛楃
+
+**鍑芥暟鏈澶ц鏁**
+
+ - 100琛
+
+
+**鍑芥暟銆佺被鍛藉悕**
+
+ - 绫诲悕浣跨敤UpperCamelCase椋庢牸锛屽繀椤婚伒浠庨┘宄板舰寮忋
+ - 鍛藉悕灏介噺浣跨敤鑻辨枃鍗曡瘝锛屽姏姹傜畝鍗曟竻妤
+
+**鍙橀噺鍛藉悕**
+
+ - 涓斿敖閲忎娇鐢ㄥ崟璇嶅懡鍚嶏紝涓寰嬪皬鍐
+ - 绂佹鍙栧崟涓瓧绗︼紙濡俰銆乯銆乲锛夛紝浣 i銆乯銆乲浣滃眬閮ㄥ惊鐜彉閲忔槸鍏佽鐨勩
+
+**甯搁噺**
+
+ - 鍏ㄩ儴澶у啓
+ - 涓嶅厑璁告湭缁忓畾涔夌殑甯搁噺鐩存帴鍑虹幇鍦ㄤ唬鐮佷腑
+
+**绌鸿瑙勫垯**
+
+ - 鐩稿鐙珛鐨勭▼搴忓潡涔嬮棿銆佸彉閲忚鏄庝箣鍚庡繀椤诲姞绌鸿
+ - 涓嶅厑璁告妸澶氫釜鐭鍙ュ啓鍦ㄤ竴琛屼腑锛屼竴琛屽彧鍐欎竴鏉¤鍙
+ - if銆乫or銆乨o銆亀hile銆乧ase銆乻witch銆乨efault 绛夎鍙ヨ嚜鍗犱竴琛岋紝涓攊f銆乫or銆乨o銆亀hile绛夎鍙ョ殑鎵ц璇彞閮ㄥ垎鏃犺澶氬皯閮借鍔犳嫭鍙穥}銆
+
+**娉ㄩ噴瑙勫垯**
+
+ - 浣跨敤//
+ - 娉ㄩ噴鐨勫唴瀹硅娓呮銆佹槑浜嗭紝涓嶈兘鏈変簩涔夋
+ - 鎿嶄綔绗﹀墠鍚庣┖鏍 鎿嶄綔绗﹀墠鍚庡繀椤诲姞涓涓┖鏍
+
+**鍏朵粬瑙勫垯**
+
+ - 涓ョ浣跨敤鎷奸煶涓庤嫳鏂囨贩鍚堢殑鏂瑰紡锛屾洿涓嶅厑璁哥洿鎺ヤ娇鐢ㄤ腑鏂囩殑鏂瑰紡
+ - 鐢ㄥぇ鍐欑殑鈥橪鈥欎唬鏇库檒鈥
+
diff --git a/221801132/src/WordCount/WordCount.java b/221801132test/src/WordCount/WordCount.java
similarity index 100%
rename from 221801132/src/WordCount/WordCount.java
rename to 221801132test/src/WordCount/WordCount.java
diff --git a/221801132/test/WordCount/WordCountTest.java b/221801132test/test/WordCount/WordCountTest.java
similarity index 100%
rename from 221801132/test/WordCount/WordCountTest.java
rename to 221801132test/test/WordCount/WordCountTest.java
From 30a1189ffbc7537cddb19019f1373452322b34fd Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Thu, 4 Mar 2021 20:35:21 +0800
Subject: [PATCH 14/16] Update .gitignore
---
.gitignore | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
index f8b55dc2..dc93f7f6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
-.metadata/
-221801132test/
\ No newline at end of file
+/.metadata/
+
+/221801132test/
\ No newline at end of file
From f4697d4bf7d9cc59ba032e2cb67f894cb53aec78 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Fri, 5 Mar 2021 15:10:33 +0800
Subject: [PATCH 15/16] =?UTF-8?q?=E6=8A=8A=E4=BB=A3=E7=A0=81=E6=94=BE?=
=?UTF-8?q?=E8=BF=9B=E7=9B=B8=E5=BA=94=E4=BD=8D=E7=BD=AE=E9=87=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 4 +-
.../WordCount.java => 221801132/src/Lib.java | 24 +--
221801132/src/WordCount.java | 191 ++----------------
221801132test/src/WordCount/Lib.java | 173 ++++++++++++++++
.../test/WordCount/WordCountTest.java | 52 +++--
5 files changed, 232 insertions(+), 212 deletions(-)
rename 221801132test/src/WordCount/WordCount.java => 221801132/src/Lib.java (90%)
create mode 100644 221801132test/src/WordCount/Lib.java
diff --git a/.gitignore b/.gitignore
index dc93f7f6..0e2216ee 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,3 @@
-/.metadata/
+.metadata
-/221801132test/
\ No newline at end of file
+221801132test
\ No newline at end of file
diff --git a/221801132test/src/WordCount/WordCount.java b/221801132/src/Lib.java
similarity index 90%
rename from 221801132test/src/WordCount/WordCount.java
rename to 221801132/src/Lib.java
index 92994fa2..19c52bea 100644
--- a/221801132test/src/WordCount/WordCount.java
+++ b/221801132/src/Lib.java
@@ -1,26 +1,23 @@
package WordCount;
import java.io.*;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
+import java.util.*;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
-public class WordCount {
+public class Lib {
public static Reader InputFile(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e) {
- e.printStackTrace();
System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
}
return reader;
- }
+ }
public static BufferedWriter OutputFile(String fileName) throws IOException {
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
@@ -30,13 +27,13 @@ public static BufferedWriter OutputFile(String fileName) throws IOException {
public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
Reader reader = InputFile(InputFile);
Writer writer = new FileWriter(OutputFile);
- int CharactersNum = 0;
- while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
+ int CharactersNum = 0; //鏂囦欢瀛楃鏁
+ while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
{
CharactersNum++;
}
writer.write("characters:" + CharactersNum + '\n');
- writer.close();
+ writer.close();
reader.close();
return CharactersNum;
} // 缁熻瀛楃鏁般
@@ -173,13 +170,4 @@ public int compare(Map.Entry w1, Map.Entry w2)
writer.close();
return test; //杩斿洖棰戠巼鏈浣庣殑璇
}
-
- public static void main(String[] args) throws IOException {
- String inputFile = args[0];
- String outputFile = args[1];
- WordCount.CountCharacters(inputFile, outputFile);
- WordCount.CountWords(inputFile, outputFile);
- WordCount.CountLines(inputFile, outputFile);
- WordCount.WordsNumSort(inputFile, outputFile);
- }
}
\ No newline at end of file
diff --git a/221801132/src/WordCount.java b/221801132/src/WordCount.java
index 92994fa2..5698b7c7 100644
--- a/221801132/src/WordCount.java
+++ b/221801132/src/WordCount.java
@@ -1,185 +1,24 @@
package WordCount;
import java.io.*;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
+import java.util.*;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class WordCount {
- public static Reader InputFile(String fileName) {
- File file = new File(fileName);
- Reader reader = null;
- try {
- reader = new InputStreamReader(new FileInputStream(file));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
- }
- return reader;
- }
-
- public static BufferedWriter OutputFile(String fileName) throws IOException {
- BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
- return write;
- }
-
- public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
- Reader reader = InputFile(InputFile);
- Writer writer = new FileWriter(OutputFile);
- int CharactersNum = 0;
- while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
- {
- CharactersNum++;
- }
- writer.write("characters:" + CharactersNum + '\n');
- writer.close();
- reader.close();
- return CharactersNum;
- } // 缁熻瀛楃鏁般
-
- public static int CountWords(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int length; //琛ㄧず鍗曡瘝鐨勯暱搴︼紝澶т簬绛変簬4鍚堟硶
- int temp;
- int WordsNum = 0;
- String word = "";
- String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
- Pattern p = Pattern.compile(regex);
- while ((temp = reader.read()) != -1)
- {
- while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) {
- word += (char) temp;
- temp = reader.read();
- }
- while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //鍘婚櫎绌虹櫧瀛楃鍜屽垎闅旂
- {
- temp = reader.read();
- }
- Matcher m = p.matcher(word);
- length = word.length();
- if (length >= 4 && m.matches())
- {
- WordsNum++;
- }
- word = "" + (char)temp;
- }
- writer.append("words: " + WordsNum + '\n');
- writer.close();
- reader.close();
- return WordsNum;
- } //缁熻鍗曡瘝鏁
-
-
- public static int CountLines(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int temp;
- int LinesNum = 0;
- String line = "";
- while ((temp = reader.read()) != -1)
- {
- while (temp != -1 && (char) temp != '\n')
- {
- if (temp != ' ' && temp != '\t' && temp != '\r')
- {
- line += (char)temp;
- }
- temp = reader.read();
- }
- if (line != " ")
- {
- LinesNum++;
- }
- line = " ";
- }
- writer.append("lines:" + LinesNum + "\n");
- reader.close();
- writer.close();
- return LinesNum;
- } //缁熻琛屾暟銆
-
- public static String WordsNumSort(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int temp;
- String word = "";
- String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
- Pattern p = Pattern.compile(regex);
- Map words = new HashMap();
- while ((temp = reader.read()) != -1)
- {
- while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57))
- {
- if (temp >= 65 && temp <= 90)
- {
- temp += 32;
- }
- word += (char)temp;
- temp = reader.read();
- }
- while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1)
- {
- temp = reader.read();
- }
- Matcher m = p.matcher(word);
- if (m.matches()) {
- if (words.get(word) == null)
- {
- words.put(word, Integer.valueOf(1));
- }
- else
- {
- words.put(word, Integer.valueOf(words.get(word).intValue() + 1));
- }
- }
- if (temp >= 65 && temp <= 90)
- {
- temp += 32;
- }
- word = "" + (char) temp;
- } //涓庣粺璁″崟璇嶆暟鐨勬柟娉曠被浼硷紝涓嶅悎娉曠殑鍗曡瘝涓嶈繘琛屾帓搴忋
-
- Map WordsSort = words.entrySet().stream().sorted(new Comparator>()
- {
- public int compare(Map.Entry w1, Map.Entry w2)
- {
- if (w1.getValue().equals(w2.getValue()))
- {
- return w1.getKey().compareTo(w2.getKey());
- }
- else
- {
- return w2.getValue().compareTo(w1.getValue());
- }
- }
- }
- ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
- String test = null; //璁惧畾棰戠巼鏈浣庣殑璇嶄互渚垮崟鍏冩祴璇
- int i = 0;
- for (Map.Entry entry : WordsSort.entrySet())
- {
- test = entry.getKey();
- writer.write(entry.getKey() + ":" + entry.getValue() + "\n");
- if (i++ >= 9) {
- break;
- }
- } ////鎵撳嵃棰戠巼鍓嶅崄鐨勫崟璇
- reader.close();
- writer.close();
- return test; //杩斿洖棰戠巼鏈浣庣殑璇
- }
-
- public static void main(String[] args) throws IOException {
- String inputFile = args[0];
- String outputFile = args[1];
- WordCount.CountCharacters(inputFile, outputFile);
- WordCount.CountWords(inputFile, outputFile);
- WordCount.CountLines(inputFile, outputFile);
- WordCount.WordsNumSort(inputFile, outputFile);
- }
-}
\ No newline at end of file
+ public static void main(String[] args) throws IOException {
+ if (args.length != 2)
+ {
+ System.out.println("鍛戒护琛屽弬鏁伴敊璇紝闇瑕佷袱涓枃浠跺悕锛");
+ System.exit(0);
+ }
+ String inputFile = args[0];
+ String outputFile = args[1];
+ Lib.CountCharacters(inputFile, outputFile);
+ Lib.CountWords(inputFile, outputFile);
+ Lib.CountLines(inputFile, outputFile);
+ Lib.WordsNumSort(inputFile, outputFile);
+ }
+}
diff --git a/221801132test/src/WordCount/Lib.java b/221801132test/src/WordCount/Lib.java
new file mode 100644
index 00000000..19c52bea
--- /dev/null
+++ b/221801132test/src/WordCount/Lib.java
@@ -0,0 +1,173 @@
+package WordCount;
+
+import java.io.*;
+import java.util.*;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+public class Lib {
+ public static Reader InputFile(String fileName) {
+ File file = new File(fileName);
+ Reader reader = null;
+ try {
+ reader = new InputStreamReader(new FileInputStream(file));
+ } catch (FileNotFoundException e) {
+ System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
+ }
+ return reader;
+ }
+
+ public static BufferedWriter OutputFile(String fileName) throws IOException {
+ BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
+ return write;
+ }
+
+ public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
+ Reader reader = InputFile(InputFile);
+ Writer writer = new FileWriter(OutputFile);
+ int CharactersNum = 0; //鏂囦欢瀛楃鏁
+ while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
+ {
+ CharactersNum++;
+ }
+ writer.write("characters:" + CharactersNum + '\n');
+ writer.close();
+ reader.close();
+ return CharactersNum;
+ } // 缁熻瀛楃鏁般
+
+ public static int CountWords(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int length; //琛ㄧず鍗曡瘝鐨勯暱搴︼紝澶т簬绛変簬4鍚堟硶
+ int temp;
+ int WordsNum = 0;
+ String word = "";
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
+ Pattern p = Pattern.compile(regex);
+ while ((temp = reader.read()) != -1)
+ {
+ while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) {
+ word += (char) temp;
+ temp = reader.read();
+ }
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //鍘婚櫎绌虹櫧瀛楃鍜屽垎闅旂
+ {
+ temp = reader.read();
+ }
+ Matcher m = p.matcher(word);
+ length = word.length();
+ if (length >= 4 && m.matches())
+ {
+ WordsNum++;
+ }
+ word = "" + (char)temp;
+ }
+ writer.append("words: " + WordsNum + '\n');
+ writer.close();
+ reader.close();
+ return WordsNum;
+ } //缁熻鍗曡瘝鏁
+
+
+ public static int CountLines(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ int LinesNum = 0;
+ String line = "";
+ while ((temp = reader.read()) != -1)
+ {
+ while (temp != -1 && (char) temp != '\n')
+ {
+ if (temp != ' ' && temp != '\t' && temp != '\r')
+ {
+ line += (char)temp;
+ }
+ temp = reader.read();
+ }
+ if (line != " ")
+ {
+ LinesNum++;
+ }
+ line = " ";
+ }
+ writer.append("lines:" + LinesNum + "\n");
+ reader.close();
+ writer.close();
+ return LinesNum;
+ } //缁熻琛屾暟銆
+
+ public static String WordsNumSort(String inputFile, String outputFile) throws IOException {
+ Reader reader = InputFile(inputFile);
+ Writer writer = OutputFile(outputFile);
+ int temp;
+ String word = "";
+ String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
+ Pattern p = Pattern.compile(regex);
+ Map words = new HashMap();
+ while ((temp = reader.read()) != -1)
+ {
+ while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57))
+ {
+ if (temp >= 65 && temp <= 90)
+ {
+ temp += 32;
+ }
+ word += (char)temp;
+ temp = reader.read();
+ }
+ while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1)
+ {
+ temp = reader.read();
+ }
+ Matcher m = p.matcher(word);
+ if (m.matches()) {
+ if (words.get(word) == null)
+ {
+ words.put(word, Integer.valueOf(1));
+ }
+ else
+ {
+ words.put(word, Integer.valueOf(words.get(word).intValue() + 1));
+ }
+ }
+ if (temp >= 65 && temp <= 90)
+ {
+ temp += 32;
+ }
+ word = "" + (char) temp;
+ } //涓庣粺璁″崟璇嶆暟鐨勬柟娉曠被浼硷紝涓嶅悎娉曠殑鍗曡瘝涓嶈繘琛屾帓搴忋
+
+ Map WordsSort = words.entrySet().stream().sorted(new Comparator>()
+ {
+ public int compare(Map.Entry w1, Map.Entry w2)
+ {
+ if (w1.getValue().equals(w2.getValue()))
+ {
+ return w1.getKey().compareTo(w2.getKey());
+ }
+ else
+ {
+ return w2.getValue().compareTo(w1.getValue());
+ }
+ }
+ }
+ ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
+ String test = null; //璁惧畾棰戠巼鏈浣庣殑璇嶄互渚垮崟鍏冩祴璇
+ int i = 0;
+ for (Map.Entry entry : WordsSort.entrySet())
+ {
+ test = entry.getKey();
+ writer.write(entry.getKey() + ":" + entry.getValue() + "\n");
+ if (i++ >= 9) {
+ break;
+ }
+ } ////鎵撳嵃棰戠巼鍓嶅崄鐨勫崟璇
+ reader.close();
+ writer.close();
+ return test; //杩斿洖棰戠巼鏈浣庣殑璇
+ }
+}
\ No newline at end of file
diff --git a/221801132test/test/WordCount/WordCountTest.java b/221801132test/test/WordCount/WordCountTest.java
index 41a423ea..da64770d 100644
--- a/221801132test/test/WordCount/WordCountTest.java
+++ b/221801132test/test/WordCount/WordCountTest.java
@@ -7,46 +7,66 @@
import org.junit.Test;
public class WordCountTest {
-
+
@Test
public void InputFile() throws IOException {
String inputFile3 = "D:\\test\\test3.txt";
- assertEquals(null,WordCount.InputFile(inputFile3));
+ assertEquals(null,Lib.InputFile(inputFile3));
} //娴嬭瘯璇诲彇鏂囦欢澶辫触鏃剁殑杩愯缁撴灉
-
+
@Test
- public void testCharactersCount() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
+ public void testCountCharacters() throws IOException {
+ String inputFile1 = "D:\\test\\test1.txt";
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
- assertEquals(521,WordCount.CountCharacters(inputFile1, outputFile));
- assertEquals(15,WordCount.CountCharacters(inputFile2, outputFile));
-
+ if(Lib.CountCharacters(inputFile1, outputFile) == 521)
+ {
+ System.out.println("瀛楃鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
+ }
+ if(Lib.CountCharacters(inputFile2, outputFile) == 15)
+ {
+ System.out.println("瀛楃鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
+ }
} //娴嬭瘯瀛楃鏁扮粺璁″姛鑳
-
+
@Test
public void testCountWords() throws IOException {
String inputFile1 = "D:\\test\\test1.txt";
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
- assertEquals(20,WordCount.CountWords(inputFile1, outputFile));
- assertEquals(2,WordCount.CountWords(inputFile2, outputFile));
- } //娴嬭瘯鍗曡瘝鏁扮粺璁″姛鑳
+ if(Lib.CountWords(inputFile1, outputFile) == 20)
+ {
+ System.out.println("鍗曡瘝鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
+ }
+ if(Lib.CountWords(inputFile2, outputFile) == 2)
+ {
+ System.out.println("鍗曡瘝鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
+ }
+ } //娴嬭瘯鍗曡瘝鏁扮粺璁″姛鑳
@Test
public void testCountLines() throws IOException {
String inputFile1 = "D:\\test\\test1.txt";
String inputFile2 = "D:\\test\\test2.txt";
String outputFile = "D:\\test\\out.txt";
- assertEquals(3,WordCount.CountLines(inputFile1, outputFile));
- assertEquals(1,WordCount.CountLines(inputFile2, outputFile));
+ if(Lib.CountLines(inputFile1, outputFile) == 3)
+ {
+ System.out.println("琛屾暟缁熻鍔熻兘娴嬭瘯鎴愬姛");
+ }
+ if(Lib.CountLines(inputFile2, outputFile) == 1)
+ {
+ System.out.println("琛屾暟缁熻鍔熻兘娴嬭瘯鎴愬姛");
+ }
} //娴嬭瘯琛屾暟缁熻鍔熻兘
@Test
public void WordsNumSort() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
+ String inputFile1 = "D:\\test\\test1.txt";
String outputFile = "D:\\test\\out.txt";
String test = "round";
- test.equals( WordCount.WordsNumSort(inputFile1, outputFile));
+ if(test.equals(Lib.WordsNumSort(inputFile1, outputFile)))
+ {
+ System.out.println("鍗曡瘝棰戠巼缁熻鍔熻兘娴嬭瘯鎴愬姛");
+ };
} //娴嬭瘯鍗曡瘝棰戠巼缁熻鍔熻兘
}
From 69c4a32fc50a5583b84e4529da133d6fc1814258 Mon Sep 17 00:00:00 2001
From: LQ <1064957296@qq.com>
Date: Fri, 5 Mar 2021 15:58:09 +0800
Subject: [PATCH 16/16] =?UTF-8?q?=E5=BA=94=E8=AF=A5=E6=98=AF=E6=9C=80?=
=?UTF-8?q?=E5=90=8E=E4=B8=80=E6=AC=A1Commit?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
鍘绘帀澶氫綑鐨勪笢瑗匡紝缂栬緫README.md銆
---
.gitignore | 2 -
221801132/README.md | 48 +++++
221801132test/.classpath | 12 --
221801132test/.gitignore | 6 -
221801132test/.project | 17 --
.../.settings/org.eclipse.jdt.core.prefs | 14 --
221801132test/README.md | 0
221801132test/codestyle.md | 51 ------
221801132test/src/WordCount/Lib.java | 173 ------------------
.../test/WordCount/WordCountTest.java | 72 --------
10 files changed, 48 insertions(+), 347 deletions(-)
delete mode 100644 221801132test/.classpath
delete mode 100644 221801132test/.gitignore
delete mode 100644 221801132test/.project
delete mode 100644 221801132test/.settings/org.eclipse.jdt.core.prefs
delete mode 100644 221801132test/README.md
delete mode 100644 221801132test/codestyle.md
delete mode 100644 221801132test/src/WordCount/Lib.java
delete mode 100644 221801132test/test/WordCount/WordCountTest.java
diff --git a/.gitignore b/.gitignore
index 0e2216ee..1ff03788 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1 @@
.metadata
-
-221801132test
\ No newline at end of file
diff --git a/221801132/README.md b/221801132/README.md
index e69de29b..8c02ae79 100644
--- a/221801132/README.md
+++ b/221801132/README.md
@@ -0,0 +1,48 @@
+锘縒ordCount
+---------------
+
+
+**鍩烘湰鍔熻兘**
+
+鍋囪鏈変竴涓蒋浠舵瘡闅斾竴灏忔鏃堕棿浼氳褰曚竴娆$敤鎴风殑鎼滅储璁板綍锛岃褰曚负鑻辨枃銆
+杈撳叆鏂囦欢鍜岃緭鍑烘枃浠朵互鍛戒护琛屽弬鏁颁紶鍏ャ備緥濡傛垜浠湪鍛戒护琛岀獥鍙(cmd)涓緭鍏ワ細
+
+
+>java WordCount input.txt output.txt
+
+鍒欎細缁熻input.txt涓殑浠ヤ笅鍑犱釜鎸囨爣
+
+**1銆佺粺璁℃枃浠剁殑瀛楃鏁帮紙瀵瑰簲杈撳嚭绗竴琛岋級锛**
+
+ - 鍙渶瑕佺粺璁scii鐮侊紝姹夊瓧涓嶉渶鑰冭檻
+ - 绌烘牸锛屾按骞冲埗琛ㄧ锛屾崲琛岀锛屽潎绠楀瓧绗
+
+
+
+**2銆佺粺璁℃枃浠剁殑鍗曡瘝鎬绘暟锛堝搴旇緭鍑虹浜岃锛夛紝鍗曡瘝锛氳嚦灏戜互4涓嫳鏂囧瓧姣嶅紑澶达紝璺熶笂瀛楁瘝鏁板瓧绗﹀彿锛屽崟璇嶄互鍒嗛殧绗﹀垎鍓诧紝涓嶅尯鍒嗗ぇ灏忓啓銆**
+
+ - 鑻辨枃瀛楁瘝锛 A-Z锛宎-z
+ - 瀛楁瘝鏁板瓧绗﹀彿锛欰-Z锛 a-z锛0-9
+ - 鍒嗗壊绗︼細绌烘牸锛岄潪瀛楁瘝鏁板瓧绗﹀彿
+ - 渚嬶細file123鏄竴涓崟璇嶏紝 123file涓嶆槸涓涓崟璇嶃俧ile锛孎ile鍜孎ILE鏄悓涓涓崟璇
+
+
+
+**3銆佺粺璁℃枃浠剁殑鏈夋晥琛屾暟锛堝搴旇緭鍑虹涓夎锛夛細浠讳綍鍖呭惈闈炵┖鐧藉瓧绗︾殑琛岋紝閮介渶瑕佺粺璁°**
+
+**4銆佺粺璁℃枃浠朵腑鍚勫崟璇嶇殑鍑虹幇娆℃暟锛堝搴旇緭鍑烘帴涓嬫潵10琛岋級锛屾渶缁堝彧杈撳嚭棰戠巼鏈楂樼殑10涓**
+
+ - 棰戠巼鐩稿悓鐨勫崟璇嶏紝浼樺厛杈撳嚭瀛楀吀搴忛潬鍓嶇殑鍗曡瘝銆
+
+>渚嬪锛寃indows95锛寃indows98鍜寃indows2000鍚屾椂鍑虹幇鏃讹紝鍒欏厛杈撳嚭windows2000
+
+ - 杈撳嚭鐨勫崟璇嶇粺涓涓哄皬鍐欐牸寮
+
+**鐒跺悗灏嗙粺璁$粨鏋滆緭鍑哄埌output.txt锛岃緭鍑虹殑鏍煎紡濡備笅锛涘叾涓瓀ord1鍜寃ord2 瀵瑰簲鍏蜂綋鐨勫崟璇嶏紝number涓虹粺璁″嚭鐨勪釜鏁帮紱鎹㈣浣跨敤'\n'锛岀紪鐮佺粺涓浣跨敤UTF-8銆**
+
+>characters: number
+words: number
+lines: number
+word1: number
+word2: number
+...
diff --git a/221801132test/.classpath b/221801132test/.classpath
deleted file mode 100644
index 7929670b..00000000
--- a/221801132test/.classpath
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/221801132test/.gitignore b/221801132test/.gitignore
deleted file mode 100644
index c644f9b8..00000000
--- a/221801132test/.gitignore
+++ /dev/null
@@ -1,6 +0,0 @@
-bin/
-test/
-.settings/
-.classpath/
-.project/
-
diff --git a/221801132test/.project b/221801132test/.project
deleted file mode 100644
index d4f939ed..00000000
--- a/221801132test/.project
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
- 221801132test
-
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
-
- org.eclipse.jdt.core.javanature
-
-
diff --git a/221801132test/.settings/org.eclipse.jdt.core.prefs b/221801132test/.settings/org.eclipse.jdt.core.prefs
deleted file mode 100644
index 910a770e..00000000
--- a/221801132test/.settings/org.eclipse.jdt.core.prefs
+++ /dev/null
@@ -1,14 +0,0 @@
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=14
-org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
-org.eclipse.jdt.core.compiler.compliance=14
-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.enablePreviewFeatures=disabled
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
-org.eclipse.jdt.core.compiler.release=enabled
-org.eclipse.jdt.core.compiler.source=14
diff --git a/221801132test/README.md b/221801132test/README.md
deleted file mode 100644
index e69de29b..00000000
diff --git a/221801132test/codestyle.md b/221801132test/codestyle.md
deleted file mode 100644
index f63e22cd..00000000
--- a/221801132test/codestyle.md
+++ /dev/null
@@ -1,51 +0,0 @@
-锘夸唬鐮佽鑼
-----
-
-
-----------
-
-**缂╄繘**
-
- - 4涓┖鏍
-
-**姣忚鏈澶氬瓧绗︽暟**
-
- - 80瀛楃
-
-**鍑芥暟鏈澶ц鏁**
-
- - 100琛
-
-
-**鍑芥暟銆佺被鍛藉悕**
-
- - 绫诲悕浣跨敤UpperCamelCase椋庢牸锛屽繀椤婚伒浠庨┘宄板舰寮忋
- - 鍛藉悕灏介噺浣跨敤鑻辨枃鍗曡瘝锛屽姏姹傜畝鍗曟竻妤
-
-**鍙橀噺鍛藉悕**
-
- - 涓斿敖閲忎娇鐢ㄥ崟璇嶅懡鍚嶏紝涓寰嬪皬鍐
- - 绂佹鍙栧崟涓瓧绗︼紙濡俰銆乯銆乲锛夛紝浣 i銆乯銆乲浣滃眬閮ㄥ惊鐜彉閲忔槸鍏佽鐨勩
-
-**甯搁噺**
-
- - 鍏ㄩ儴澶у啓
- - 涓嶅厑璁告湭缁忓畾涔夌殑甯搁噺鐩存帴鍑虹幇鍦ㄤ唬鐮佷腑
-
-**绌鸿瑙勫垯**
-
- - 鐩稿鐙珛鐨勭▼搴忓潡涔嬮棿銆佸彉閲忚鏄庝箣鍚庡繀椤诲姞绌鸿
- - 涓嶅厑璁告妸澶氫釜鐭鍙ュ啓鍦ㄤ竴琛屼腑锛屼竴琛屽彧鍐欎竴鏉¤鍙
- - if銆乫or銆乨o銆亀hile銆乧ase銆乻witch銆乨efault 绛夎鍙ヨ嚜鍗犱竴琛岋紝涓攊f銆乫or銆乨o銆亀hile绛夎鍙ョ殑鎵ц璇彞閮ㄥ垎鏃犺澶氬皯閮借鍔犳嫭鍙穥}銆
-
-**娉ㄩ噴瑙勫垯**
-
- - 浣跨敤//
- - 娉ㄩ噴鐨勫唴瀹硅娓呮銆佹槑浜嗭紝涓嶈兘鏈変簩涔夋
- - 鎿嶄綔绗﹀墠鍚庣┖鏍 鎿嶄綔绗﹀墠鍚庡繀椤诲姞涓涓┖鏍
-
-**鍏朵粬瑙勫垯**
-
- - 涓ョ浣跨敤鎷奸煶涓庤嫳鏂囨贩鍚堢殑鏂瑰紡锛屾洿涓嶅厑璁哥洿鎺ヤ娇鐢ㄤ腑鏂囩殑鏂瑰紡
- - 鐢ㄥぇ鍐欑殑鈥橪鈥欎唬鏇库檒鈥
-
diff --git a/221801132test/src/WordCount/Lib.java b/221801132test/src/WordCount/Lib.java
deleted file mode 100644
index 19c52bea..00000000
--- a/221801132test/src/WordCount/Lib.java
+++ /dev/null
@@ -1,173 +0,0 @@
-package WordCount;
-
-import java.io.*;
-import java.util.*;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-
-public class Lib {
- public static Reader InputFile(String fileName) {
- File file = new File(fileName);
- Reader reader = null;
- try {
- reader = new InputStreamReader(new FileInputStream(file));
- } catch (FileNotFoundException e) {
- System.out.println("鎵句笉鍒拌緭鍏ユ枃浠讹紒");
- }
- return reader;
- }
-
- public static BufferedWriter OutputFile(String fileName) throws IOException {
- BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"utf-8"));
- return write;
- }
-
- public static int CountCharacters(String InputFile, String OutputFile) throws IOException {
- Reader reader = InputFile(InputFile);
- Writer writer = new FileWriter(OutputFile);
- int CharactersNum = 0; //鏂囦欢瀛楃鏁
- while (reader.read() != -1) //璇诲彇鍒-1鏃跺仠姝€
- {
- CharactersNum++;
- }
- writer.write("characters:" + CharactersNum + '\n');
- writer.close();
- reader.close();
- return CharactersNum;
- } // 缁熻瀛楃鏁般
-
- public static int CountWords(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int length; //琛ㄧず鍗曡瘝鐨勯暱搴︼紝澶т簬绛変簬4鍚堟硶
- int temp;
- int WordsNum = 0;
- String word = "";
- String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
- Pattern p = Pattern.compile(regex);
- while ((temp = reader.read()) != -1)
- {
- while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) {
- word += (char) temp;
- temp = reader.read();
- }
- while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1) //鍘婚櫎绌虹櫧瀛楃鍜屽垎闅旂
- {
- temp = reader.read();
- }
- Matcher m = p.matcher(word);
- length = word.length();
- if (length >= 4 && m.matches())
- {
- WordsNum++;
- }
- word = "" + (char)temp;
- }
- writer.append("words: " + WordsNum + '\n');
- writer.close();
- reader.close();
- return WordsNum;
- } //缁熻鍗曡瘝鏁
-
-
- public static int CountLines(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int temp;
- int LinesNum = 0;
- String line = "";
- while ((temp = reader.read()) != -1)
- {
- while (temp != -1 && (char) temp != '\n')
- {
- if (temp != ' ' && temp != '\t' && temp != '\r')
- {
- line += (char)temp;
- }
- temp = reader.read();
- }
- if (line != " ")
- {
- LinesNum++;
- }
- line = " ";
- }
- writer.append("lines:" + LinesNum + "\n");
- reader.close();
- writer.close();
- return LinesNum;
- } //缁熻琛屾暟銆
-
- public static String WordsNumSort(String inputFile, String outputFile) throws IOException {
- Reader reader = InputFile(inputFile);
- Writer writer = OutputFile(outputFile);
- int temp;
- String word = "";
- String regex = "[a-zA-Z]{4}[^ ,.]+"; //姝e垯琛ㄨ揪寮忓垽鏂槸鍚︿负鍥涗釜鑻辨枃寮澶
- Pattern p = Pattern.compile(regex);
- Map words = new HashMap();
- while ((temp = reader.read()) != -1)
- {
- while ((temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57))
- {
- if (temp >= 65 && temp <= 90)
- {
- temp += 32;
- }
- word += (char)temp;
- temp = reader.read();
- }
- while ((!(temp >= 97 && temp <= 122) || (temp >= 65 && temp <= 90) || (temp >= 48 && temp <= 57)) && temp != -1)
- {
- temp = reader.read();
- }
- Matcher m = p.matcher(word);
- if (m.matches()) {
- if (words.get(word) == null)
- {
- words.put(word, Integer.valueOf(1));
- }
- else
- {
- words.put(word, Integer.valueOf(words.get(word).intValue() + 1));
- }
- }
- if (temp >= 65 && temp <= 90)
- {
- temp += 32;
- }
- word = "" + (char) temp;
- } //涓庣粺璁″崟璇嶆暟鐨勬柟娉曠被浼硷紝涓嶅悎娉曠殑鍗曡瘝涓嶈繘琛屾帓搴忋
-
- Map WordsSort = words.entrySet().stream().sorted(new Comparator>()
- {
- public int compare(Map.Entry w1, Map.Entry w2)
- {
- if (w1.getValue().equals(w2.getValue()))
- {
- return w1.getKey().compareTo(w2.getKey());
- }
- else
- {
- return w2.getValue().compareTo(w1.getValue());
- }
- }
- }
- ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); //瀵瑰崟璇嶉鐜囪繘琛屾帓搴
- String test = null; //璁惧畾棰戠巼鏈浣庣殑璇嶄互渚垮崟鍏冩祴璇
- int i = 0;
- for (Map.Entry entry : WordsSort.entrySet())
- {
- test = entry.getKey();
- writer.write(entry.getKey() + ":" + entry.getValue() + "\n");
- if (i++ >= 9) {
- break;
- }
- } ////鎵撳嵃棰戠巼鍓嶅崄鐨勫崟璇
- reader.close();
- writer.close();
- return test; //杩斿洖棰戠巼鏈浣庣殑璇
- }
-}
\ No newline at end of file
diff --git a/221801132test/test/WordCount/WordCountTest.java b/221801132test/test/WordCount/WordCountTest.java
deleted file mode 100644
index da64770d..00000000
--- a/221801132test/test/WordCount/WordCountTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-package WordCount;
-
-import static org.junit.Assert.*;
-
-import java.io.IOException;
-
-import org.junit.Test;
-
-public class WordCountTest {
-
- @Test
- public void InputFile() throws IOException {
- String inputFile3 = "D:\\test\\test3.txt";
- assertEquals(null,Lib.InputFile(inputFile3));
- } //娴嬭瘯璇诲彇鏂囦欢澶辫触鏃剁殑杩愯缁撴灉
-
- @Test
- public void testCountCharacters() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
- String inputFile2 = "D:\\test\\test2.txt";
- String outputFile = "D:\\test\\out.txt";
- if(Lib.CountCharacters(inputFile1, outputFile) == 521)
- {
- System.out.println("瀛楃鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
- }
- if(Lib.CountCharacters(inputFile2, outputFile) == 15)
- {
- System.out.println("瀛楃鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
- }
- } //娴嬭瘯瀛楃鏁扮粺璁″姛鑳
-
- @Test
- public void testCountWords() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
- String inputFile2 = "D:\\test\\test2.txt";
- String outputFile = "D:\\test\\out.txt";
- if(Lib.CountWords(inputFile1, outputFile) == 20)
- {
- System.out.println("鍗曡瘝鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
- }
- if(Lib.CountWords(inputFile2, outputFile) == 2)
- {
- System.out.println("鍗曡瘝鏁扮粺璁″姛鑳芥祴璇曟垚鍔");
- }
- } //娴嬭瘯鍗曡瘝鏁扮粺璁″姛鑳
-
- @Test
- public void testCountLines() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
- String inputFile2 = "D:\\test\\test2.txt";
- String outputFile = "D:\\test\\out.txt";
- if(Lib.CountLines(inputFile1, outputFile) == 3)
- {
- System.out.println("琛屾暟缁熻鍔熻兘娴嬭瘯鎴愬姛");
- }
- if(Lib.CountLines(inputFile2, outputFile) == 1)
- {
- System.out.println("琛屾暟缁熻鍔熻兘娴嬭瘯鎴愬姛");
- }
- } //娴嬭瘯琛屾暟缁熻鍔熻兘
-
- @Test
- public void WordsNumSort() throws IOException {
- String inputFile1 = "D:\\test\\test1.txt";
- String outputFile = "D:\\test\\out.txt";
- String test = "round";
- if(test.equals(Lib.WordsNumSort(inputFile1, outputFile)))
- {
- System.out.println("鍗曡瘝棰戠巼缁熻鍔熻兘娴嬭瘯鎴愬姛");
- };
- } //娴嬭瘯鍗曡瘝棰戠巼缁熻鍔熻兘
-}