项目源代码Gitee地址
作业要求
一:介绍
这次作业的主要任务:
WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。
wc.exe -c file.c //返回文件 file.c 的字符数
wc.exe -w file.c //返回文件 file.c 的单词总数
wc.exe -l file.c //返回文件 file.c 的总行数
wc.exe -o outputFile.txt //将结果输出到指定文件outputFile.txt
拿到题目的时候,思考使用近期使用比较多,也相对比较熟练的c#来写。计划使用两个类:MainScreen:用来进行控制台界面的输入输出,ProcessingDate:用来进行具体的操作。
二: 具体程序
1.主界面是最简单的方式,strcomm用来接收用户输入的指令
Console.WriteLine("----------------------------------------");
Console.WriteLine("--------------WordCount 1.0-------------");
Console.WriteLine("----------------------------------------");
Console.WriteLine("统计字符数 wc.exe -c file.c ");
Console.WriteLine("统计单词总数 wc.exe -w file.c ");
Console.WriteLine("统计总行数 wc.exe -l file.c ");
Console.WriteLine("统计输出到文件 wc.exe -o outputfile.txt ");
Console.WriteLine("----------------------------------------");
Console.WriteLine("请输入指令:");
string strcomm = Console.ReadLine();
2.对用户输入的指令进行分析。
对用户输入的指令,截取各个部分存入字符串数组,方便后续的使用,在这里,数组第一个元素始终是字符串wc.exe,最后一个始终是文件名。去空格截取(StringSplitOptions.RemoveEmptyEntries)可以避免许多不必要的麻烦。考虑到指令可以多个一起使用,在这里使用了for循环逐个对指令进行检查。同时输出了结果。
ProcessingData wc = new ProcessingData();
string[] sArray = strcomm.Split(new char[2] { ‘ ‘, ‘-‘ }, StringSplitOptions.RemoveEmptyEntries);//去空格截取各个字符串信息
for (int i = 1; i < sArray.Length - 1; i++)
{
switch (sArray[i])
{
//Convert .ToInt32 (
case "c":
Console.WriteLine(sArray[sArray.Length - 1] + " 字符数:" + wc.cProsess(sArray[sArray.Length - 1]));
break;
case "w":
Console.WriteLine(sArray[sArray.Length - 1] + " 单词数:" + wc.wProsess(sArray[sArray.Length - 1]));
break;
case "l":
Console.WriteLine(sArray[sArray.Length - 1] + " 行数:" + wc.lProsess(sArray[sArray.Length - 1]));
break;
case "o":
Console.WriteLine(wc.oProcess(sArray[sArray.Length - 1]));
break;
default:
Console.WriteLine("输入的指令 -" + sArray[i] + " 有误!");
break;
}
}
3.ProcessingDate类
3.1考虑到每一次的处理都会涉及到打开文件,将打开文件写为一个单独的方法,在具体的方法中直接调用即可。每一次对文件的操作都需要关闭文件。方法返回读取到的信息str.
public string openFile(string fstr)
{
FileStream fs = new FileStream(@fstr, FileMode.Open);//初始化文件流
byte[] array = new byte[fs.Length];//初始化字节数组
fs.Read(array, 0, array.Length);//读取流中数据到字节数组中
fs.Close();//关闭流
string str = Encoding.UTF8.GetString(array);
return str;
}
3.2:判断字符数(空格、换行、制表符)
只要循环到以上三种字符,则计数器加1.结束后将结果按要求写入result.txt文件,并在控制台输出结果。
public int cProsess(string fstr)
{
string str = openFile(fstr);
int charCount = 0;//记录字符个数
foreach (char c in str)
{
if (c == ‘ ‘ || c == ‘\t‘ || c == ‘\n‘)
{
charCount++;
}
}
StreamWriter sw = new StreamWriter("result.txt", true);
sw.WriteLine(fstr + " 字符数: " + Convert.ToString(charCount));
sw.Close();
return charCount;
}
判断行数的代码类似
public int lProsess(string lstr)
{
string str = openFile(lstr);
int count = 0;//记录行数
foreach (char s in str)
{
if (s == ‘\n‘)
{
count++;
}
}
StreamWriter sw = new StreamWriter("result.txt", true);
sw.WriteLine(lstr + " 行数: " + Convert.ToString(count - 1));
sw.Close();
return count - 1;
}
3.3判断单词数。
这个部分写的时候比较麻烦。考虑到作业要求,发现如果直接使用空格分隔开,那么如果两个单词之间存在多个空格,或者以空格开头的文件或者行,都会受到影响。想了许久,发现一个特别笨但还算行之有效的方法。
设置一个布尔变量 bool isblank = true;遍历字符串,当当前字符不为空格(换行)时,置为false,count+1,此时记录一个单词,直到当前字符为空格(或者换行)时,置为true,表示这个单词结束,可以进入下一个单词。这样,不论之间又多少空格或者换行,判断一个单词都是以一个字符开始,遇到空格(或者换行)时结束。
public int wProsess(string wstr)
{
string str = openFile(wstr);
int count = 0;//记录单词个数
bool isblank = true;
foreach (char s in str)//遍历字符串,
{
if (s != ‘ ‘ && s != ‘\n‘ && isblank == true)
{
count++;
isblank = false;
}
else if ((s == ‘ ‘ || s == ‘\n‘) && isblank == false)
{
isblank = true;
}
}
StreamWriter sw = new StreamWriter("result.txt", true);
sw.WriteLine(wstr + " 单词数:" + Convert.ToString(count - 1));//
sw.Close();
return count - 1;
}
3.4将结果输入指定文件。
public bool oProcess(string oFile)
{
string str = openFile("result.txt");//打开结果保存文件
StreamWriter sw = new StreamWriter(oFile, true);
sw.WriteLine(str);
sw.Close();
return true;
}
三:测试设计过程。
这里测试主要针对输入。
1.当输入一个指令时,程序能否正确执行,如果输入的指令不正确,程序能否处理并报告给用户。
2.当输入多个指令时,程序能否正确执行,如果输入的指令都不正确、有部分不正确,程序能否处理并正确的报告给用户哪些指令是错误的。
这两个方面已经实现。
3.当输入的文件名错误,程序找不到文件时,程序如果正确处理(不抛异常并且正确报告给用户)。
该方面还没有实现。
四:总结
这次作业一开始拿到,其实是非常难受的,因为看起来很复杂,后来仔细读了一下作业要求,发现需要实现的基础功能差不多都比较简单。在完成作业的过程中,难点就是统计单词的部分。
五:在作业完成过程中,学习了一些作者的文章中的部分方法。
关于写入文件的操作
关于字符串的分割
(第一次使用MarkDowm编辑,关于代码框部分出现的问题,修改了多次也没有找到解决的办法。Orz)
原文:https://www.cnblogs.com/xiao-ge-ge/p/9732956.html