From 34f6a7b1cfca02a6b6f7641bb648411c3cf4096a Mon Sep 17 00:00:00 2001 From: guozhaojie <931122576@qq.com> Date: Fri, 5 Apr 2019 09:30:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- guozhaojie/Wordcount/ConsoleApp1/App.config | 6 + .../Wordcount/ConsoleApp1/ConsoleApp1.csproj | 53 +++++ guozhaojie/Wordcount/ConsoleApp1/Program.cs | 211 ++++++++++++++++++ .../ConsoleApp1/Properties/AssemblyInfo.cs | 36 +++ guozhaojie/Wordcount/Wordcount.sln | 25 +++ 5 files changed, 331 insertions(+) create mode 100644 guozhaojie/Wordcount/ConsoleApp1/App.config create mode 100644 guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj create mode 100644 guozhaojie/Wordcount/ConsoleApp1/Program.cs create mode 100644 guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs create mode 100644 guozhaojie/Wordcount/Wordcount.sln diff --git a/guozhaojie/Wordcount/ConsoleApp1/App.config b/guozhaojie/Wordcount/ConsoleApp1/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/guozhaojie/Wordcount/ConsoleApp1/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj b/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj new file mode 100644 index 0000000..828f9ad --- /dev/null +++ b/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {6A10DE88-6893-4C46-88FB-016726053936} + Exe + ConsoleApp1 + ConsoleApp1 + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/guozhaojie/Wordcount/ConsoleApp1/Program.cs b/guozhaojie/Wordcount/ConsoleApp1/Program.cs new file mode 100644 index 0000000..3345893 --- /dev/null +++ b/guozhaojie/Wordcount/ConsoleApp1/Program.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp1 +{ + class Program + { + static void Main(string[] args) + { + string filepath = args[0]; + Program a = new Program(); + Library b = new Library(); + if (System.IO.File.Exists(filepath)) + { + System.IO.StreamReader file = new System.IO.StreamReader(filepath); + char[] filedata = file.ReadToEnd().ToCharArray(); + file.Close(); + file.Dispose(); + string c = b.countchar(filedata); + string w = b.countword(filedata); + string l = b.countline(filedata); + string f = b.countfre(filedata); + a.savedata(c, w, l, f, "Wordcount"); + Console.WriteLine("以上数据已保存在文档目录下Save文件夹中的Wordcount文档中!"); + } + else Console.WriteLine("读取失败!该文件不存在"); + Console.ReadKey(); + } + + void savedata(string c, string w, string l, string f, string name) + { + string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Save\"; + if (!System.IO.Directory.Exists(CurDir)) System.IO.Directory.CreateDirectory(CurDir); + String filePath = CurDir + name + ".txt"; + System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, false); + file.WriteLine(c); + file.WriteLine(w); + file.WriteLine(l); + file.WriteLine(f); + file.Close(); + file.Dispose(); + } + } + public class Library + { + public string countchar(char[] filedata) + { + string countchar = "characters:" + filedata.Length; + Console.WriteLine(countchar); + return countchar; + } + + public string countword(char[] filedata) + { + int count = 0, letter = 0; + for (int n = 0; n < filedata.Length; n++) + { + if ((filedata[n] >= 65 && filedata[n] <= 90) || (filedata[n] >= 97 && filedata[n] <= 122)) + letter++; + else if (filedata[n] == 32) + { + if (letter >= 4) + { + count++; + letter = 0; + } + else letter = 0; + } + else letter = 0; + } + string countword = "words:" + count; + Console.WriteLine(countword); + return countword; + } + + public string countline(char[] filedata) + { + int count = 0; + for (int n = 0; n < filedata.Length; n++) + { + if (filedata[n] == 10) count++; + } + count++; + string countline = "lines:" + count; + Console.WriteLine(countline); + return countline; + } + + public string countfre(char[] filedata) + { + int letter = 0, count = 0, t; + string[] word = new string[words(filedata)]; + for (int n = 0; n < filedata.Length; n++) + { + if ((filedata[n] >= 97 && filedata[n] <= 122) || (filedata[n] >= 65 && filedata[n] <= 90)) + letter++; + else if (filedata[n] == 32) + { + if (letter >= 4) + { + string[] letter1 = new string[letter]; + t = n; + for (int i = 0; i < letter; i++) + { + letter1[i] = char.ToString(filedata[t - letter]); + t++; + } + + for (int i = 0; i < letter; i++) + word[count] = word[count] + letter1[i]; + count++; + letter = 0; + } + else letter = 0; + } + else letter = 0; + } + for (int i = 0; i < words(filedata); i++) + word[i] = word[i].ToLower(); + string[] word1 = new string[words(filedata)]; + int[] fre = new int[words(filedata)]; + for (int i = 0; i < words(filedata); i++) + fre[i] = 0; + t = 0; + for (int i = 0; i < words(filedata); i++) + { + for (int j = 0; j < words(filedata); j++) + { + if (word[i] == word1[j]) + { + fre[j]++; + break; + } + } + word1[t] = word[i]; + fre[t]++; + t++; + } + + int a, b; + string temp1; + int temp2; + for (a = 0; a < words(filedata); a++) + for (b = a + 1; b < words(filedata); b++) + { + if (fre[a] < fre[b]) + { + temp1 = word1[a]; + word1[a] = word1[b]; + word1[b] = temp1; + temp2 = fre[a]; + fre[a] = fre[b]; + fre[b] = temp2; + } + } + int[] maxf = new int[10]; + string[] maxw = new string[10]; + for (a = 0; a < 10; a++) + { + maxf[a] = fre[a]; + maxw[a] = word1[a]; + } + for (a = 0; a < 10; a++) + for (b = a + 1; b < 10; b++) + { + if (fre[a] == fre[b] && word1[a] != null && word1[b] != null) + { + if (word1[a].CompareTo(word1[b]) > 0) + { + temp1 = word1[a]; + word1[a] = word1[b]; + word1[b] = temp1; + } + } + } + string result = null, r; + for (int i = 0; i < 10; i++) + { + r = word1[i] + ":" + fre[i].ToString(); + Console.WriteLine(r); + result = result + r + " "; + } + return result; + } + + int words(char[] filedata) + { + int count = 0, letter = 0; + for (int n = 0; n < filedata.Length; n++) + { + if ((filedata[n] >= 65 && filedata[n] <= 90) || (filedata[n] >= 97 && filedata[n] <= 122)) + letter++; + else if (filedata[n] == 32) + { + if (letter >= 4) + { + count++; + letter = 0; + } + else letter = 0; + } + else letter = 0; + } + return count; + } + } +} + diff --git a/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs b/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..061a832 --- /dev/null +++ b/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("ConsoleApp1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ConsoleApp1")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("6a10de88-6893-4c46-88fb-016726053936")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/guozhaojie/Wordcount/Wordcount.sln b/guozhaojie/Wordcount/Wordcount.sln new file mode 100644 index 0000000..a84b607 --- /dev/null +++ b/guozhaojie/Wordcount/Wordcount.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.539 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{6A10DE88-6893-4C46-88FB-016726053936}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6A10DE88-6893-4C46-88FB-016726053936}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A10DE88-6893-4C46-88FB-016726053936}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A10DE88-6893-4C46-88FB-016726053936}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A10DE88-6893-4C46-88FB-016726053936}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4A80DB58-833A-4235-9BE6-BB5EC1B9A190} + EndGlobalSection +EndGlobal From c229ae4b938d917d12a9c1f16f1b45b2287a2a49 Mon Sep 17 00:00:00 2001 From: guozhaojie <931122576@qq.com> Date: Fri, 5 Apr 2019 13:15:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Wordcount/ConsoleApp1/ConsoleApp1.csproj | 2 +- guozhaojie/Wordcount/ConsoleApp1/Program.cs | 1 - .../ConsoleApp1/Properties/AssemblyInfo.cs | 2 +- guozhaojie/Wordcount/Wordcount.sln | 20 +++-- .../testWordCount/Properties/AssemblyInfo.cs | 20 +++++ .../Wordcount/testWordCount/UnitTest1.cs | 18 +++++ .../Wordcount/testWordCount/packages.config | 5 ++ .../testWordCount/testWordCount.csproj | 74 +++++++++++++++++++ 8 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 guozhaojie/Wordcount/testWordCount/Properties/AssemblyInfo.cs create mode 100644 guozhaojie/Wordcount/testWordCount/UnitTest1.cs create mode 100644 guozhaojie/Wordcount/testWordCount/packages.config create mode 100644 guozhaojie/Wordcount/testWordCount/testWordCount.csproj diff --git a/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj b/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj index 828f9ad..a6848cf 100644 --- a/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj +++ b/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj @@ -4,7 +4,7 @@ Debug AnyCPU - {6A10DE88-6893-4C46-88FB-016726053936} + {C3F8BE7B-DEF0-4EFC-8923-8968646BCF23} Exe ConsoleApp1 ConsoleApp1 diff --git a/guozhaojie/Wordcount/ConsoleApp1/Program.cs b/guozhaojie/Wordcount/ConsoleApp1/Program.cs index 3345893..e4634d2 100644 --- a/guozhaojie/Wordcount/ConsoleApp1/Program.cs +++ b/guozhaojie/Wordcount/ConsoleApp1/Program.cs @@ -208,4 +208,3 @@ int words(char[] filedata) } } } - diff --git a/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs b/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs index 061a832..13a8112 100644 --- a/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs +++ b/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs @@ -20,7 +20,7 @@ [assembly: ComVisible(false)] // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("6a10de88-6893-4c46-88fb-016726053936")] +[assembly: Guid("c3f8be7b-def0-4efc-8923-8968646bcf23")] // 程序集的版本信息由下列四个值组成: // diff --git a/guozhaojie/Wordcount/Wordcount.sln b/guozhaojie/Wordcount/Wordcount.sln index a84b607..c2accd2 100644 --- a/guozhaojie/Wordcount/Wordcount.sln +++ b/guozhaojie/Wordcount/Wordcount.sln @@ -1,9 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.28307.539 +VisualStudioVersion = 15.0.28010.2036 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{6A10DE88-6893-4C46-88FB-016726053936}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{C3F8BE7B-DEF0-4EFC-8923-8968646BCF23}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testWordCount", "testWordCount\testWordCount.csproj", "{6A6996DE-C822-4079-B76A-D38FE2957BF1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +13,19 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {6A10DE88-6893-4C46-88FB-016726053936}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6A10DE88-6893-4C46-88FB-016726053936}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6A10DE88-6893-4C46-88FB-016726053936}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6A10DE88-6893-4C46-88FB-016726053936}.Release|Any CPU.Build.0 = Release|Any CPU + {C3F8BE7B-DEF0-4EFC-8923-8968646BCF23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C3F8BE7B-DEF0-4EFC-8923-8968646BCF23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C3F8BE7B-DEF0-4EFC-8923-8968646BCF23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C3F8BE7B-DEF0-4EFC-8923-8968646BCF23}.Release|Any CPU.Build.0 = Release|Any CPU + {6A6996DE-C822-4079-B76A-D38FE2957BF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A6996DE-C822-4079-B76A-D38FE2957BF1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A6996DE-C822-4079-B76A-D38FE2957BF1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A6996DE-C822-4079-B76A-D38FE2957BF1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {4A80DB58-833A-4235-9BE6-BB5EC1B9A190} + SolutionGuid = {72D3667E-23EF-4C99-A824-FD966BB692DE} EndGlobalSection EndGlobal diff --git a/guozhaojie/Wordcount/testWordCount/Properties/AssemblyInfo.cs b/guozhaojie/Wordcount/testWordCount/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c71f41f --- /dev/null +++ b/guozhaojie/Wordcount/testWordCount/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("testWordCount")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("testWordCount")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("6a6996de-c822-4079-b76a-d38fe2957bf1")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/guozhaojie/Wordcount/testWordCount/UnitTest1.cs b/guozhaojie/Wordcount/testWordCount/UnitTest1.cs new file mode 100644 index 0000000..2990288 --- /dev/null +++ b/guozhaojie/Wordcount/testWordCount/UnitTest1.cs @@ -0,0 +1,18 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace testWordCount +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + + string filepath = (@"E:\gitlearn\WordCount\guozhaojie\WordCount\ConsoleApp1\bin\Debug\love.txt"); + Assert.IsTrue(System.IO.File.Exists (filepath)); + + } + } +} diff --git a/guozhaojie/Wordcount/testWordCount/packages.config b/guozhaojie/Wordcount/testWordCount/packages.config new file mode 100644 index 0000000..102a45c --- /dev/null +++ b/guozhaojie/Wordcount/testWordCount/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/guozhaojie/Wordcount/testWordCount/testWordCount.csproj b/guozhaojie/Wordcount/testWordCount/testWordCount.csproj new file mode 100644 index 0000000..dfea03d --- /dev/null +++ b/guozhaojie/Wordcount/testWordCount/testWordCount.csproj @@ -0,0 +1,74 @@ + + + + + + Debug + AnyCPU + {6A6996DE-C822-4079-B76A-D38FE2957BF1} + Library + Properties + testWordCount + testWordCount + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + {c3f8be7b-def0-4efc-8923-8968646bcf23} + ConsoleApp1 + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + + \ No newline at end of file From 69904152cefffb0b76a230503f965d33110389a6 Mon Sep 17 00:00:00 2001 From: guozhaojie <931122576@qq.com> Date: Fri, 5 Apr 2019 14:31:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- guozhaojie/Wordcount/ConsoleApp1/Program.cs | 73 ++++++++------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/guozhaojie/Wordcount/ConsoleApp1/Program.cs b/guozhaojie/Wordcount/ConsoleApp1/Program.cs index e4634d2..0aeb07e 100644 --- a/guozhaojie/Wordcount/ConsoleApp1/Program.cs +++ b/guozhaojie/Wordcount/ConsoleApp1/Program.cs @@ -10,19 +10,22 @@ class Program { static void Main(string[] args) { - string filepath = args[0]; + string filepath = args[0];//从cmd中读取文档路径 Program a = new Program(); Library b = new Library(); if (System.IO.File.Exists(filepath)) { System.IO.StreamReader file = new System.IO.StreamReader(filepath); - char[] filedata = file.ReadToEnd().ToCharArray(); + char[] filedata = file.ReadToEnd().ToCharArray();//将文档中的所有字符储存在字符数组中 file.Close(); file.Dispose(); + //调用Library类中的函数计算需要得到的各个属性 string c = b.countchar(filedata); - string w = b.countword(filedata); + string w = "words:" + b.countword(filedata).ToString(); + Console.WriteLine("words:{0}", b.countword(filedata)); string l = b.countline(filedata); string f = b.countfre(filedata); + //调用保存数据的函数将结果保存在文档中 a.savedata(c, w, l, f, "Wordcount"); Console.WriteLine("以上数据已保存在文档目录下Save文件夹中的Wordcount文档中!"); } @@ -46,14 +49,14 @@ void savedata(string c, string w, string l, string f, string name) } public class Library { - public string countchar(char[] filedata) + public string countchar(char[] filedata)//计算文档中的字符总数 { string countchar = "characters:" + filedata.Length; Console.WriteLine(countchar); return countchar; } - public string countword(char[] filedata) + public int countword(char[] filedata)//计算文档中的单词总数 { int count = 0, letter = 0; for (int n = 0; n < filedata.Length; n++) @@ -62,7 +65,7 @@ public string countword(char[] filedata) letter++; else if (filedata[n] == 32) { - if (letter >= 4) + if (letter >= 4)//满足“连续4个字母”才能被计为一个单词 { count++; letter = 0; @@ -71,28 +74,26 @@ public string countword(char[] filedata) } else letter = 0; } - string countword = "words:" + count; - Console.WriteLine(countword); - return countword; + return count; } - public string countline(char[] filedata) + public string countline(char[] filedata)//计算文档的总行数 { int count = 0; for (int n = 0; n < filedata.Length; n++) { if (filedata[n] == 10) count++; } - count++; + count++;//最后一行没有换行符故总行数需+1 string countline = "lines:" + count; Console.WriteLine(countline); return countline; } - public string countfre(char[] filedata) + public string countfre(char[] filedata)//计算文档中单词的出现频数 { - int letter = 0, count = 0, t; - string[] word = new string[words(filedata)]; + int letter = 0, count = 0, t, len = countword(filedata); + string[] word = new string[len]; for (int n = 0; n < filedata.Length; n++) { if ((filedata[n] >= 97 && filedata[n] <= 122) || (filedata[n] >= 65 && filedata[n] <= 90)) @@ -118,16 +119,16 @@ public string countfre(char[] filedata) } else letter = 0; } - for (int i = 0; i < words(filedata); i++) + for (int i = 0; i < len; i++) word[i] = word[i].ToLower(); - string[] word1 = new string[words(filedata)]; - int[] fre = new int[words(filedata)]; - for (int i = 0; i < words(filedata); i++) + string[] word1 = new string[len]; + int[] fre = new int[len]; + for (int i = 0; i < len; i++) fre[i] = 0; t = 0; - for (int i = 0; i < words(filedata); i++) + for (int i = 0; i < len; i++) { - for (int j = 0; j < words(filedata); j++) + for (int j = 0; j < len; j++) { if (word[i] == word1[j]) { @@ -140,11 +141,11 @@ public string countfre(char[] filedata) t++; } - int a, b; + int a, b;//按照频数大小从大到小进行排序 string temp1; int temp2; - for (a = 0; a < words(filedata); a++) - for (b = a + 1; b < words(filedata); b++) + for (a = 0; a < len; a++) + for (b = a + 1; b < len; b++) { if (fre[a] < fre[b]) { @@ -156,6 +157,7 @@ public string countfre(char[] filedata) fre[b] = temp2; } } + //取得频数最高的10个单词的字符串和频数 int[] maxf = new int[10]; string[] maxw = new string[10]; for (a = 0; a < 10; a++) @@ -163,7 +165,7 @@ public string countfre(char[] filedata) maxf[a] = fre[a]; maxw[a] = word1[a]; } - for (a = 0; a < 10; a++) + for (a = 0; a < 10; a++)//对于频数相等的单词按照字典顺序进行排序 for (b = a + 1; b < 10; b++) { if (fre[a] == fre[b] && word1[a] != null && word1[b] != null) @@ -185,26 +187,5 @@ public string countfre(char[] filedata) } return result; } - - int words(char[] filedata) - { - int count = 0, letter = 0; - for (int n = 0; n < filedata.Length; n++) - { - if ((filedata[n] >= 65 && filedata[n] <= 90) || (filedata[n] >= 97 && filedata[n] <= 122)) - letter++; - else if (filedata[n] == 32) - { - if (letter >= 4) - { - count++; - letter = 0; - } - else letter = 0; - } - else letter = 0; - } - return count; - } } -} +} \ No newline at end of file