项目gitee链接:https://gitee.com/suiran90/wc
解题思路
1.在本项目中,我们需要对参数的进行分辨,然后根据参数来完成相关的功能
2.项目初期,与同学讨论思考,对题目的思考以及查阅相关资料,明白了项目所要求的功能:通过参数读取指定文件、统计文件类的字符数,单词数,总行数、将结果保存至本地文件。
程序实现过程:
对本项目,包括以下5个类
1.主类(Program.cs),负责程序的运行;
2.功能分别类(WordRead.cs),负责对传入参数的分辨,需要将指定的参数与其所期望的功能连接起来,将程序跳转至相应的功能处;
3.相关功能类(总行数类Rows.cs 、总单词类Word.cs、总字符类Character.cs、写入文件类WriteFile.cs),负责相关功能的实现。
相关函数
各相关功能的函数:读取文件,统计总字符数、总行数、总单词数,在这里,我们可以通过循环,通过sr.Read()实现对每一个字符的读取并记录,特别地,对统计总行数时,我们可以使用sr.ReadLine()来对每一行进行读取;
对Main() 函数,我选择将他作为众多功能的中枢,他负责调用相关的类,比如先调用WordRead.cs类,对参数进行分析,通过其返回结果来决定调用所指定的类;
特别说明:在c#中,关于文件的读取需要 using System.IO; using System.Text; ,在程序中,我选择
Encoding encode = Encoding.GetEncoding("GB2312");
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, encode);
来对文件操作,其中使用 "GB2312" 可以对中文字符正常读取,filename 参数为文件路径。
代码说明
Rows.cs(读取行数)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WordCount
{
class Rows//行数
{
//public = "2.txt";
public string ope;
public int Line(string filename)
{
//打开文件
Encoding encode = Encoding.GetEncoding("GB2312");
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, encode);
string line;
int CountLine = 0;//记录行数
//当当前行不为空,即当前行存在,即 +1
while ((line = sr.ReadLine()) != null)
{
CountLine++;
}
Console.WriteLine("line:"+CountLine);//显示行数
//关闭文件
sr.Close();
fs.Close();
return CountLine;//注意函数是有返回值的
//应该先关闭文件再return
}
}
}
特别地,当读取单词时
char[] symbol = { ' ', '\t', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '+' ,'-','*', '='};
while ((nChar = sr.Read()) != -1)
{
foreach (char c in symbol)
{
if (nChar == (int)c)
{
wordcount++; // 统计单词数
}
}
}
第一行表示所遇见的分割单词的标志(未作单词准确性检测),第二行表示读取未至最后,第六行表示遇见分隔符即默认其左右不是一个单词
主函数类(Program.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WordCount
{
class Program
{
static void Main(string[] args)
{
//新建对象
WordRead wordRead = new WordRead();
int ctr;
int Operand;
string theword = "";
int ope1, ope2, ope3;
ope1 = ope2 = ope3 = 0;
theword += string.Format(args[1]);//存取文件名
if (args.Length > 2)//读取参数并赋值
Console.WriteLine("error");
else
{
for (ctr = 0; ctr < args.Length; ctr++)
{
theword += string.Format("参数 {0} 为:{1}", ctr + 1, args[ctr]);//将参数存到字符串中
Console.WriteLine("参数 {0} 为:{1}", ctr + 1, args[ctr]);//读取参数
wordRead.ope = args[0];//读取操作符如-w ,传给对象
}
}
Operand = wordRead.LoadWord();//读取返回值
if (Operand == 1) //当用户选择读取总行数
{
Rows rows = new Rows();
ope1 = rows.Line(args[1]);
theword += string.Format("Total Line:{0}",ope1);
}
else if (Operand == 2) //当用户选择读取总单词数
{
Word word = new Word();
ope2 = word.TotalWord(args[1]);
theword += string.Format("Total Word:{0}", ope2);
}
else if (Operand == 3) //当用户选择读取总字符数
{
Character character = new Character();
ope3 = character.TotalWord(args[1]);
theword += string.Format("Total Character:{0}", ope3);
}
else
Console.WriteLine("error!");
//将结果写入txt
WriteFile writeFile = new WriteFile();
writeFile.Write(theword);
}
}
}
其余代码大多相关,在这里不再赘述,完整版请在文章首链接查看
测试设计过程
统计行数测试
统计单词测试
统计字符测试
生成文件
文件内容查看
参考文献链接
感谢以下文章作者的文献,让我受益良多
C#读写txt文件:https://blog.csdn.net/ybhjx/article/details/53706715
C#实现文件信息统计:https://www.cnblogs.com/libaoquan/p/5285209.html
特别说明
本程序尚有诸多不足,也有许多bug存在,时间关系仅完成些许简单功能,我将尽快改善