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..a6848cf --- /dev/null +++ b/guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {C3F8BE7B-DEF0-4EFC-8923-8968646BCF23} + 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..0aeb07e --- /dev/null +++ b/guozhaojie/Wordcount/ConsoleApp1/Program.cs @@ -0,0 +1,191 @@ +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];//从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();//将文档中的所有字符储存在字符数组中 + file.Close(); + file.Dispose(); + //调用Library类中的函数计算需要得到的各个属性 + string c = b.countchar(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文档中!"); + } + 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 int 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)//满足“连续4个字母”才能被计为一个单词 + { + count++; + letter = 0; + } + else letter = 0; + } + else letter = 0; + } + return count; + } + + public string countline(char[] filedata)//计算文档的总行数 + { + int count = 0; + for (int n = 0; n < filedata.Length; n++) + { + if (filedata[n] == 10) count++; + } + count++;//最后一行没有换行符故总行数需+1 + string countline = "lines:" + count; + Console.WriteLine(countline); + return countline; + } + + public string countfre(char[] 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)) + 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 < len; i++) + word[i] = word[i].ToLower(); + 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 < len; i++) + { + for (int j = 0; j < len; 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 < len; a++) + for (b = a + 1; b < len; 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; + } + } + //取得频数最高的10个单词的字符串和频数 + 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; + } + } +} \ No newline at end of file diff --git a/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs b/guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..13a8112 --- /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("c3f8be7b-def0-4efc-8923-8968646bcf23")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [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..c2accd2 --- /dev/null +++ b/guozhaojie/Wordcount/Wordcount.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +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 + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {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 = {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