关于WordCount的作业
一、开发者:201631062418
二、代码地址:https://gitee.com/YsuLIyan/WordCount
三、作业地址:https://edu.cnblogs.com/campus/xnsy/Test/homework/2203
四、基本功能
WordCount.exe -c test.txt //返回文件 test.txt的字符数
WordCount.exe -w test.txt //返回文件 test.txt的单词总数
WordCount.exe -l test.txt //返回文件 test.txt的总行数
WordCount.exe -o outputFile.txt //将结果输出到指定文件outputFile.txt
五、扩展功能
-----------------------------------------------------------------------------------------------------------------------------------------------
已经实现:-c -w -l 可以同时使用,如果没有-o命令(或者说-0之后没有接文件名),则默认outputfile.txt作为输出结果的文件(strPath)。并且扫描-0命令之后的接收结果文件名称,如果接收结果文件在该目标文件夹中不存在,则会报路径错误
-----------------------------------------------------------------------------------------------------------------------------------------------
还未实现:接收结果文件在目标文件夹中不存在时,重新创建一个名为(strPath)的接收结果文件--待后期改善。
在命令读取时,程序对于用户输入的命令进行理解、更多的应答以及合适的补充。
-----------------------------------------------------------------------------------------------------------------------------------------------
六、项目的实现
这是一个专门处理命令的方法类,这也是我比较在意的地方,因为我很注重我写的程序对用户输入的参数的应答。
class CommandRead
{
bool isRows = false;
bool isChara = false;
bool isWord = false;
bool isAimPath = false;
string[] path = new string[];
string[] commands; public bool IsRows
{
get { return isRows; }
}
public bool IsChara
{
get { return isChara; }
}
public bool IsWord
{
get { return isWord; }
}
public bool IsAimPath
{
get { return isAimPath; }
} public string[] Path
{
get { return path; }
} public void CommandImport(string[] args)
{
commands = args;
if(commands == null)
{
Console.WriteLine("请输入指令");
}
else
{
int Pas = ; for (int i = ; i < commands.Length; i++)
{
if (commands[i] == "-l" || commands[i] == "-L")//行数
{
isRows = true;
}
else if (commands[i] == "-w" || commands[i] == "-W")//单词
{
isWord = true;
}
else if (commands[i] == "-c" || commands[i] == "-C")//字符
{
isChara = true;
}
else if (commands[i] == "-o" || commands[i] == "-O")//输出
{
if (i + == commands.Length)
isAimPath = true;
}
else
{
path[Pas] = commands[i];
Pas++;
}
}
}
} }
这是关于读取文件行数的类
class Rows //行数
{
public string ope; public int Line(string filename)
{
Encoding encod = Encoding.GetEncoding("GB2312");
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, encod); string line;
int CountLine = ;//记录行数 //当前行不为空
while((line = sr.ReadLine()) != null)
{
CountLine++;
} //应该先关闭文件再return
sr.Close();
fs.Close(); return CountLine;//注意函数是有返回值的
}
}
这是读取文件符号数的类
class Word//字符
{
public int TotalWord(string filename)
{
Encoding encod = Encoding.GetEncoding("GB2312");
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, encod); int nchar;
int WordCount = ;
char[] symbol = { ' ', '\t', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '+', '-', '*', '=' };//间隔符
while((nchar = sr.Read())!= -)
{
foreach(char c in symbol)
{
if(nchar == (int)c)
{
WordCount++;
}
}
} sr.Close();
fs.Close(); return WordCount;
}
}
这是读取单词数的类
class Character//单词
{
public string ope; public int TotalWord(string filename)
{
Encoding encod = Encoding.GetEncoding("GB2312");
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, encod); int charcount = ;
int nchar; while((nchar = sr.Read())!= -) //sr.Read()是以特定的编码方式读取流而获取一个接一个的字符,而ReadLine()是读取一行
{
charcount++; // 统计字符数
} //关闭文件
sr.Close();
fs.Close(); return charcount;
}
}
这是将得到的信息写入指定文件的方法类
class WriteFile
{
public void Write(string theword)
{
FileStream fs = new FileStream("outputfile.txt", FileMode.Create);
//获得字节数
byte[] data = System.Text.Encoding.Default.GetBytes(theword); //写入
fs.Write(data, , data.Length);
//清空缓存区,关闭流
fs.Flush();
fs.Close();
} public void Write(string path,string theword)
{
bool isInFile = false;
String rootPath = Directory.GetCurrentDirectory();
DirectoryInfo root = new DirectoryInfo(rootPath);
foreach(FileInfo f in root.GetFiles())
{
if(path == f.Name)
{
isInFile = true;
}
}
if (isInFile != false)
{
FileStream fs = new FileStream(path, FileMode.Create);
//获得字节数
byte[] data = System.Text.Encoding.Default.GetBytes(theword); //写入
fs.Write(data, , data.Length);
//清空缓存区,关闭流
fs.Flush();
fs.Close();
}
else
{
Console.WriteLine("Path Error!");
}
}
}
然后就是主函数
class Program
{ static void Main(string[] args)
{
string[] cmds = args;
string theWord = "";
int ope1 = ,ope2 = ,ope3 = ;
for(int i =; i<args.Length;i++)
Console.WriteLine(args[i]); //新建对象读取命令
CommandRead cmd = new CommandRead();
cmd.CommandImport(cmds); if (cmd.IsRows==true)
{
Rows rows = new Rows();
ope1 = rows.Line(cmd.Path[]);
Console.WriteLine("Total Line:{0}", ope1);
theWord += string.Format("Total Line:{0}\r\n", ope1);
}
if (cmd.IsChara==true)
{
Character ch = new Character();
ope2 = ch.TotalWord(cmd.Path[]);
Console.WriteLine("Total Character:{0}", ope2);
theWord += string.Format("Total Character:{0}\r\n", ope2);
}
if (cmd.IsWord==true)
{
Word word = new Word();
ope2 = word.TotalWord(cmd.Path[]);
Console.WriteLine("Total Word:{0}", ope3);
theWord += string.Format("Total Word:{0}\r\n", ope3);
}
WriteFile write = new WriteFile();
if (cmd.IsAimPath)
{
write.Write(cmd.Path[], theWord);
}
if(cmd.IsAimPath == false)
{
write.Write(theWord);
} }
}
七、项目测试
test.txt文件内容:

进行测试

测试结果

测试2

测试结果

心得感悟:其实刚好我在C#方面对于文件的读写这一块并不是很熟练,但是刚好该题就让我去读写文件,而且我并不是很清楚如何在cmd里面调用.exe以及传入参数。

关于WordCount的作业的更多相关文章
- 软件工程:Wordcount程序作业
由于时间的关系,急着交作业,加上这一次也不是那么很认真的去做,草草写了“Wordcount程序”几个功能,即是 .txt文件的读取,能计算出文件内容的单词数,文件内容的字符数,及行数. 这次选用C来做 ...
- HUST软测1504班第2周作业成绩:WordCount
说明 本次公布的成绩为第2周个人作业WordCount的结果: 第2周个人作业:WordCount 如果同学对作业结果存在异议,可以: 在毕博平台讨论区的第2周作业第在线答疑区发帖申诉. 或直接在博客 ...
- 软件测试作业——WordCount的测试
一.代码提交 1.代码地址:https://gitee.com/zst1978805482/WordCount 2.作业地址:https://edu.cnblogs.com/campus/xnsy/T ...
- Jason Wang: 结对编程 CountWord(第三次作业)
本次作业地址: https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/2882 学号: 201731072323 ...
- Hadoop Steaming开发之WordCount
简单的WordCount栗子--类似于编程语言中的hello world 1.shell脚本run.sh HADOOP_CMD="/usr/local/src/hadoop-1.2.1/bi ...
- 第三次作业 201731082208 黄亚恒&肖莉
Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...
- Ganlia采样、统计及RRD记录周期(频次、间隔)的配置和更改
Ganglia & RRD Ganglia是伯克利开发的一个集群监控软件.可以监视和显示集群中的节点的各种状态信息,比如如:cpu .mem.硬盘利用率, I/O负载.网络流量情况等,同时可以 ...
- Plink v0.1.0 发布——基于Flink的流处理平台
Plink是一个基于Flink的流处理平台,旨在基于 [Apache Flink]封装构建上层平台. 提供常见的作业管理功能.如作业的创建,删除,编辑,更新,保存,启动,停止,重启,管理,多作业模板配 ...
- 第二周个人作业WordCount
1.Github地址 https://github.com/JingzheWu/WordCount 2.PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...
随机推荐
- applicationContext-common.xml]; nested exception is java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
14:59:16,747 ERROR ContextLoader:350 - Context initialization failedorg.springframework.beans.factor ...
- Notepad++ tab替换为4个空格
- 2017/2/11CSS基础
一:html中div: 1.DIV标签应用于 Style Sheet(样式表)方面会更显威力,它最终目的是给设计者另一种组织能力,有 Class.Style.title.ID 等属性. 2.<d ...
- 2017/2/10:Manven简介与项目管理(入门)
1.Maven工程的创建 2.使用Manven manven配置文件主要集中在 http://m.blog.csdn.net/article/details?id=50316383
- mysql之数据库的介绍和基本的增删改查
一 学前知识 什么叫做静态页面:用户传入内容后,不能处理用户的请求,只能单纯的显示主页面的信息. 什么是负载均衡:通过计算服务器的性能,将客户发送过来的请求指派给某台服务器.一般还要有一个备份的负载均 ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Typecho 插件开发基础
<?php /** * 标题 插件说明 * * @package 添加标题 * @author Fan * @version 1.0.0 * @link http://cnblogs.com/f ...
- FastDFS与springBoot集成
参考博客http://blog.csdn.net/xyang81/article/details/52850667 tobato在今年9月份在官方Java客户端的基础上进行了大量重构,且提供了更多丰富 ...
- python基础回顾
1.第二个缺点就是代码不能加密.如果要发布你的Python程序,实际上就是发布源代码,这一点跟C语言不同,C语言不用发布源代码,只需要把编译后的机器码(也就是你在Windows 上常见的xxx.exe ...
- C++之引用和指针
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7050431.html C++之引用和指针 C++引用 引用的基本用法: in ...