Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions guozhaojie/Wordcount/ConsoleApp1/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
53 changes: 53 additions & 0 deletions guozhaojie/Wordcount/ConsoleApp1/ConsoleApp1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C3F8BE7B-DEF0-4EFC-8923-8968646BCF23}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp1</RootNamespace>
<AssemblyName>ConsoleApp1</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
191 changes: 191 additions & 0 deletions guozhaojie/Wordcount/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
36 changes: 36 additions & 0 deletions guozhaojie/Wordcount/ConsoleApp1/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
31 changes: 31 additions & 0 deletions guozhaojie/Wordcount/Wordcount.sln
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions guozhaojie/Wordcount/testWordCount/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
18 changes: 18 additions & 0 deletions guozhaojie/Wordcount/testWordCount/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -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));

}
}
}
5 changes: 5 additions & 0 deletions guozhaojie/Wordcount/testWordCount/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net461" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net461" />
</packages>
Loading