一、开发者: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的作业的更多相关文章

  1. 软件工程:Wordcount程序作业

    由于时间的关系,急着交作业,加上这一次也不是那么很认真的去做,草草写了“Wordcount程序”几个功能,即是 .txt文件的读取,能计算出文件内容的单词数,文件内容的字符数,及行数. 这次选用C来做 ...

  2. HUST软测1504班第2周作业成绩:WordCount

    说明 本次公布的成绩为第2周个人作业WordCount的结果: 第2周个人作业:WordCount 如果同学对作业结果存在异议,可以: 在毕博平台讨论区的第2周作业第在线答疑区发帖申诉. 或直接在博客 ...

  3. 软件测试作业——WordCount的测试

    一.代码提交 1.代码地址:https://gitee.com/zst1978805482/WordCount 2.作业地址:https://edu.cnblogs.com/campus/xnsy/T ...

  4. Jason Wang: 结对编程 CountWord(第三次作业)

    本次作业地址: https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/2882 学号: 201731072323 ...

  5. Hadoop Steaming开发之WordCount

    简单的WordCount栗子--类似于编程语言中的hello world 1.shell脚本run.sh HADOOP_CMD="/usr/local/src/hadoop-1.2.1/bi ...

  6. 第三次作业 201731082208 黄亚恒&肖莉

    Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...

  7. Ganlia采样、统计及RRD记录周期(频次、间隔)的配置和更改

    Ganglia & RRD Ganglia是伯克利开发的一个集群监控软件.可以监视和显示集群中的节点的各种状态信息,比如如:cpu .mem.硬盘利用率, I/O负载.网络流量情况等,同时可以 ...

  8. Plink v0.1.0 发布——基于Flink的流处理平台

    Plink是一个基于Flink的流处理平台,旨在基于 [Apache Flink]封装构建上层平台. 提供常见的作业管理功能.如作业的创建,删除,编辑,更新,保存,启动,停止,重启,管理,多作业模板配 ...

  9. 第二周个人作业WordCount

    1.Github地址 https://github.com/JingzheWu/WordCount 2.PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...

随机推荐

  1. collection tree protocol

    本文所属图书 > 传感网原理与技术 本书根据<高等院校物联网工程专业发展战略研究报告暨专业规范(试行)>和物联网工程本科专业的教学需要,结合传感网的最新发展及其应用现状编写而成.主要 ...

  2. Java中的几种对象(POJO,PO,DTO,DAO,BO)

    j2ee中,经常提到几种对象(object),理解他们的含义有助于我们更好的理解面向对象的设计思维.     POJO(plain old java object):普通的java对象,有别于特殊的j ...

  3. CentOS下利用mysqlbinlog恢复MySQL数据库

    如果不小心对数据库进行误操作,而又没有及时备份怎么办?这恐怕是广大的coder经常遇到的一类问题.我今天就因为不小心删除了某个数据库,但最后的备份是1个礼拜前的,唯一能解决的办法就是通过mysqlbi ...

  4. MyBatis中log4j 和 参数 和 分页和别名 功能

    1.配置全局文件,注意各个配置标签的顺序 properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,   objectWr ...

  5. super-smack压测工具

    简介 super-smack是一款开源压测工具,支持MySQL.PostgreSQL.Oracle.本篇主要介绍一下使用super-smack压测MySQL体会. 1.SQL定义 2.数据字典定义 3 ...

  6. android studio友盟分享demo运行报错Gradle's dependency cache may be corrupt解决方法

    gradle-wrapper.properties里修改了gradle的版本,与之前没有报错的项目gradle版本一致.

  7. 解决python代码中含有中文报错

    python中写入中文时报错如下图所示: 依照网上解决方法:在py文件中加入:#encoding=utf-8 然后继续报错如下图所示: 解决方法: 在py文件中加入: import sysreload ...

  8. IDEA插件开发总结

    一:前置步骤 1.添加开发插件所需的SDK: 1.1先添加JDK 1.2打开Project Structure-Platform Settings-SDKs 1.3添加IntelliJ Platfor ...

  9. 预装apk

    一般是在device/rockchip/ LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := LanguageSetLOC ...

  10. jquery的call()和apply()方法

    call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call 方法可以用来 ...