码云地址:https://gitee.com/a-heart/WC
1.word Count 需求说明
这个项目的需求主要包括以下几点:
1.)对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。
2.) 可执行程序命名为:wc.exe,该程序处理用户需求的模式为:wc.exe [parameter] [input_file_name]
3.) 存储统计结果的文件默认为result.txt,放在与wc.exe相同的目录下。
1.2 基本功能
wc.exe -c file.c //返回文件 file.c 的字符数
wc.exe -w file.c //返回文件 file.c 的单词总数
wc.exe -l file.c //返回文件 file.c 的总行数
2. 解题思路
在才开始看到这个题目的时候,看到有那么多的文字描述,以为会是一个比较难的东西,毕竟有那么多的要求嘛。但是后来细细的看了一下后,发现基础功能并不是那么难,就是实现字符,单词以及行数的统计。既然有统计,那么肯定就要有关文件的操作了。大致知道该怎么办了之后,做起来也就有了方向了,也就可以开始动手做了。
3. 具体代码
3.1 单词统计
public static int WordNum(int wordcount, string file) { StreamReader sr = StrReadr(file); nchar = sr.Read(); char[] symbol = { ' ', ',', ' ' };//判断单词个数的标准 while ((nchar = sr.Read()) != -1) { foreach (char c in symbol) { if (nchar == (int)c) { wordcount++; } } } sr.Close(); return wordcount; }
3.2 字符统计
public static int ChNum(int charcount, string file) { StreamReader sr = StrReadr(file); while ((nchar = sr.Read()) != -1) { charcount++; } sr.Close(); return charcount; }
3.3 行数统计
public static int LineNum(int linecount, string file) { int i = 0; StreamReader sr = StrReadr(file); sr.BaseStream.Seek(0, SeekOrigin.Begin); string strline = sr.ReadLine(); while (strline != null) { i++; strline = sr.ReadLine(); } sr.Close(); return i; }
4. 测试
5. 总结
这次的作业让我大致的了解到了c#中文件的部分操作,由于不是经常使用,所以有些地方不是很熟练,也很容易犯错,但是也给了自己一个熟悉这种操作。这次作业也让我学会了一样新的东西,那就是如何使用git。第一次使用git,说实话给我的感觉不是很友好,在我配置环境时,出了一点错误,让我花了挺长的时间去重新弄。但是,总的来说,还是到了一点东西。
6. 参考链接
c#文件操作: https://www.sogou.com/sie?hdq=sogou-wsse-90415f9b8d0fe2da-1218&query=c#文件操作&ie=utf8
如何使用码云 https://blog.csdn.net/s1120080286/article/details/79831146
如何安装git https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396287703354d8c6c01c904c7d9ff056ae23da865a000/