一、开发者: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. SpringBoot的读取properties文件的方式

    转载:https://www.imooc.com/article/18252一.@ConfigurationProperties方式 自定义配置类:PropertiesConfig.java pack ...

  2. MySQL查找SQL耗时瓶颈 SHOW profiles

    http://blog.csdn.net/k_scott/article/details/8804384 1.首先查看是否开启profiling功能 SHOW VARIABLES LIKE '%pro ...

  3. python学习 day12 (3月18日)----(装饰器内置函数)

    读时间函数: # import time # def func(): # start_time = time.time() # 代码运行之前的时间 # print('这是一个func函数') # ti ...

  4. bootstrap之css样式

    一 bootstrap的介绍 Bootstrap是将html,css和js的代码打包好了,只管我们拿来调用.是基于jquery开发的. 使用BootCDN提供的免费CDN加速服务,同时支持http和h ...

  5. php中如何解决显示数据库中的内容乱码

    第一步: 第二步:在代码开始处加一行

  6. 2018.11.03 NOIP模拟 图(bfs/最短路)

    传送门 显然如果AAA到BBB或者CCC到DDD走的不是最短路一定是有一段路径重合了,于是可以O(n2)bfsO(n^2)bfsO(n2)bfs出两点之间的最短距离然后枚举两个点作为重合的端点来更新答 ...

  7. 牛客训练:小a与黄金街道(欧拉函数+快速幂)

    题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...

  8. C++ 中 new 操作符内幕:new operator、operator new、placement new

    一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...

  9. UVa 11427 Expect the Expected (数学期望 + 概率DP)

    题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...

  10. canvas制作表盘

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...